Transforms-bc45e707.js 496 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699
  1. define(['require', 'exports', './Matrix3-41c58dde', './Check-6ede7e26', './defaultValue-fe22d8c0', './Math-0a2ac845', './Matrix2-e1298525', './combine-d9581036', './RuntimeError-ef395448'], (function (require, exports, Matrix3, Check, defaultValue, Math$1, Matrix2, combine, RuntimeError) { 'use strict';
  2. function _interopNamespaceDefault(e) {
  3. var n = Object.create(null);
  4. if (e) {
  5. Object.keys(e).forEach(function (k) {
  6. if (k !== 'default') {
  7. var d = Object.getOwnPropertyDescriptor(e, k);
  8. Object.defineProperty(n, k, d.get ? d : {
  9. enumerable: true,
  10. get: function () { return e[k]; }
  11. });
  12. }
  13. });
  14. }
  15. n.default = e;
  16. return Object.freeze(n);
  17. }
  18. /**
  19. * A simple map projection where longitude and latitude are linearly mapped to X and Y by multiplying
  20. * them by the {@link Ellipsoid#maximumRadius}. This projection
  21. * is commonly known as geographic, equirectangular, equidistant cylindrical, or plate carrée. It
  22. * is also known as EPSG:4326.
  23. *
  24. * @alias GeographicProjection
  25. * @constructor
  26. *
  27. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  28. *
  29. * @see WebMercatorProjection
  30. */
  31. function GeographicProjection(ellipsoid) {
  32. this._ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix3.Ellipsoid.WGS84);
  33. this._semimajorAxis = this._ellipsoid.maximumRadius;
  34. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  35. }
  36. Object.defineProperties(GeographicProjection.prototype, {
  37. /**
  38. * Gets the {@link Ellipsoid}.
  39. *
  40. * @memberof GeographicProjection.prototype
  41. *
  42. * @type {Ellipsoid}
  43. * @readonly
  44. */
  45. ellipsoid: {
  46. get: function () {
  47. return this._ellipsoid;
  48. },
  49. },
  50. });
  51. /**
  52. * Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters.
  53. * X and Y are the longitude and latitude, respectively, multiplied by the maximum radius of the
  54. * ellipsoid. Z is the unmodified height.
  55. *
  56. * @param {Cartographic} cartographic The coordinates to project.
  57. * @param {Cartesian3} [result] An instance into which to copy the result. If this parameter is
  58. * undefined, a new instance is created and returned.
  59. * @returns {Cartesian3} The projected coordinates. If the result parameter is not undefined, the
  60. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  61. * created and returned.
  62. */
  63. GeographicProjection.prototype.project = function (cartographic, result) {
  64. // Actually this is the special case of equidistant cylindrical called the plate carree
  65. const semimajorAxis = this._semimajorAxis;
  66. const x = cartographic.longitude * semimajorAxis;
  67. const y = cartographic.latitude * semimajorAxis;
  68. const z = cartographic.height;
  69. if (!defaultValue.defined(result)) {
  70. return new Matrix3.Cartesian3(x, y, z);
  71. }
  72. result.x = x;
  73. result.y = y;
  74. result.z = z;
  75. return result;
  76. };
  77. /**
  78. * Unprojects a set of projected {@link Cartesian3} coordinates, in meters, to {@link Cartographic}
  79. * coordinates, in radians. Longitude and Latitude are the X and Y coordinates, respectively,
  80. * divided by the maximum radius of the ellipsoid. Height is the unmodified Z coordinate.
  81. *
  82. * @param {Cartesian3} cartesian The Cartesian position to unproject with height (z) in meters.
  83. * @param {Cartographic} [result] An instance into which to copy the result. If this parameter is
  84. * undefined, a new instance is created and returned.
  85. * @returns {Cartographic} The unprojected coordinates. If the result parameter is not undefined, the
  86. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  87. * created and returned.
  88. */
  89. GeographicProjection.prototype.unproject = function (cartesian, result) {
  90. //>>includeStart('debug', pragmas.debug);
  91. if (!defaultValue.defined(cartesian)) {
  92. throw new Check.DeveloperError("cartesian is required");
  93. }
  94. //>>includeEnd('debug');
  95. const oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  96. const longitude = cartesian.x * oneOverEarthSemimajorAxis;
  97. const latitude = cartesian.y * oneOverEarthSemimajorAxis;
  98. const height = cartesian.z;
  99. if (!defaultValue.defined(result)) {
  100. return new Matrix3.Cartographic(longitude, latitude, height);
  101. }
  102. result.longitude = longitude;
  103. result.latitude = latitude;
  104. result.height = height;
  105. return result;
  106. };
  107. /**
  108. * This enumerated type is used in determining where, relative to the frustum, an
  109. * object is located. The object can either be fully contained within the frustum (INSIDE),
  110. * partially inside the frustum and partially outside (INTERSECTING), or somewhere entirely
  111. * outside of the frustum's 6 planes (OUTSIDE).
  112. *
  113. * @enum {number}
  114. */
  115. const Intersect = {
  116. /**
  117. * Represents that an object is not contained within the frustum.
  118. *
  119. * @type {number}
  120. * @constant
  121. */
  122. OUTSIDE: -1,
  123. /**
  124. * Represents that an object intersects one of the frustum's planes.
  125. *
  126. * @type {number}
  127. * @constant
  128. */
  129. INTERSECTING: 0,
  130. /**
  131. * Represents that an object is fully within the frustum.
  132. *
  133. * @type {number}
  134. * @constant
  135. */
  136. INSIDE: 1,
  137. };
  138. var Intersect$1 = Object.freeze(Intersect);
  139. /**
  140. * Represents the closed interval [start, stop].
  141. * @alias Interval
  142. * @constructor
  143. *
  144. * @param {number} [start=0.0] The beginning of the interval.
  145. * @param {number} [stop=0.0] The end of the interval.
  146. */
  147. function Interval(start, stop) {
  148. /**
  149. * The beginning of the interval.
  150. * @type {number}
  151. * @default 0.0
  152. */
  153. this.start = defaultValue.defaultValue(start, 0.0);
  154. /**
  155. * The end of the interval.
  156. * @type {number}
  157. * @default 0.0
  158. */
  159. this.stop = defaultValue.defaultValue(stop, 0.0);
  160. }
  161. /**
  162. * A bounding sphere with a center and a radius.
  163. * @alias BoundingSphere
  164. * @constructor
  165. *
  166. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  167. * @param {number} [radius=0.0] The radius of the bounding sphere.
  168. *
  169. * @see AxisAlignedBoundingBox
  170. * @see BoundingRectangle
  171. * @see Packable
  172. */
  173. function BoundingSphere(center, radius) {
  174. /**
  175. * The center point of the sphere.
  176. * @type {Cartesian3}
  177. * @default {@link Cartesian3.ZERO}
  178. */
  179. this.center = Matrix3.Cartesian3.clone(defaultValue.defaultValue(center, Matrix3.Cartesian3.ZERO));
  180. /**
  181. * The radius of the sphere.
  182. * @type {number}
  183. * @default 0.0
  184. */
  185. this.radius = defaultValue.defaultValue(radius, 0.0);
  186. }
  187. const fromPointsXMin = new Matrix3.Cartesian3();
  188. const fromPointsYMin = new Matrix3.Cartesian3();
  189. const fromPointsZMin = new Matrix3.Cartesian3();
  190. const fromPointsXMax = new Matrix3.Cartesian3();
  191. const fromPointsYMax = new Matrix3.Cartesian3();
  192. const fromPointsZMax = new Matrix3.Cartesian3();
  193. const fromPointsCurrentPos = new Matrix3.Cartesian3();
  194. const fromPointsScratch = new Matrix3.Cartesian3();
  195. const fromPointsRitterCenter = new Matrix3.Cartesian3();
  196. const fromPointsMinBoxPt = new Matrix3.Cartesian3();
  197. const fromPointsMaxBoxPt = new Matrix3.Cartesian3();
  198. const fromPointsNaiveCenterScratch = new Matrix3.Cartesian3();
  199. const volumeConstant = (4.0 / 3.0) * Math$1.CesiumMath.PI;
  200. /**
  201. * Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
  202. * The bounding sphere is computed by running two algorithms, a naive algorithm and
  203. * Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
  204. *
  205. * @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.
  206. * @param {BoundingSphere} [result] The object onto which to store the result.
  207. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  208. *
  209. * @see {@link http://help.agi.com/AGIComponents/html/BlogBoundingSphere.htm|Bounding Sphere computation article}
  210. */
  211. BoundingSphere.fromPoints = function (positions, result) {
  212. if (!defaultValue.defined(result)) {
  213. result = new BoundingSphere();
  214. }
  215. if (!defaultValue.defined(positions) || positions.length === 0) {
  216. result.center = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO, result.center);
  217. result.radius = 0.0;
  218. return result;
  219. }
  220. const currentPos = Matrix3.Cartesian3.clone(positions[0], fromPointsCurrentPos);
  221. const xMin = Matrix3.Cartesian3.clone(currentPos, fromPointsXMin);
  222. const yMin = Matrix3.Cartesian3.clone(currentPos, fromPointsYMin);
  223. const zMin = Matrix3.Cartesian3.clone(currentPos, fromPointsZMin);
  224. const xMax = Matrix3.Cartesian3.clone(currentPos, fromPointsXMax);
  225. const yMax = Matrix3.Cartesian3.clone(currentPos, fromPointsYMax);
  226. const zMax = Matrix3.Cartesian3.clone(currentPos, fromPointsZMax);
  227. const numPositions = positions.length;
  228. let i;
  229. for (i = 1; i < numPositions; i++) {
  230. Matrix3.Cartesian3.clone(positions[i], currentPos);
  231. const x = currentPos.x;
  232. const y = currentPos.y;
  233. const z = currentPos.z;
  234. // Store points containing the the smallest and largest components
  235. if (x < xMin.x) {
  236. Matrix3.Cartesian3.clone(currentPos, xMin);
  237. }
  238. if (x > xMax.x) {
  239. Matrix3.Cartesian3.clone(currentPos, xMax);
  240. }
  241. if (y < yMin.y) {
  242. Matrix3.Cartesian3.clone(currentPos, yMin);
  243. }
  244. if (y > yMax.y) {
  245. Matrix3.Cartesian3.clone(currentPos, yMax);
  246. }
  247. if (z < zMin.z) {
  248. Matrix3.Cartesian3.clone(currentPos, zMin);
  249. }
  250. if (z > zMax.z) {
  251. Matrix3.Cartesian3.clone(currentPos, zMax);
  252. }
  253. }
  254. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  255. const xSpan = Matrix3.Cartesian3.magnitudeSquared(
  256. Matrix3.Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  257. );
  258. const ySpan = Matrix3.Cartesian3.magnitudeSquared(
  259. Matrix3.Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  260. );
  261. const zSpan = Matrix3.Cartesian3.magnitudeSquared(
  262. Matrix3.Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  263. );
  264. // Set the diameter endpoints to the largest span.
  265. let diameter1 = xMin;
  266. let diameter2 = xMax;
  267. let maxSpan = xSpan;
  268. if (ySpan > maxSpan) {
  269. maxSpan = ySpan;
  270. diameter1 = yMin;
  271. diameter2 = yMax;
  272. }
  273. if (zSpan > maxSpan) {
  274. maxSpan = zSpan;
  275. diameter1 = zMin;
  276. diameter2 = zMax;
  277. }
  278. // Calculate the center of the initial sphere found by Ritter's algorithm
  279. const ritterCenter = fromPointsRitterCenter;
  280. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  281. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  282. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  283. // Calculate the radius of the initial sphere found by Ritter's algorithm
  284. let radiusSquared = Matrix3.Cartesian3.magnitudeSquared(
  285. Matrix3.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  286. );
  287. let ritterRadius = Math.sqrt(radiusSquared);
  288. // Find the center of the sphere found using the Naive method.
  289. const minBoxPt = fromPointsMinBoxPt;
  290. minBoxPt.x = xMin.x;
  291. minBoxPt.y = yMin.y;
  292. minBoxPt.z = zMin.z;
  293. const maxBoxPt = fromPointsMaxBoxPt;
  294. maxBoxPt.x = xMax.x;
  295. maxBoxPt.y = yMax.y;
  296. maxBoxPt.z = zMax.z;
  297. const naiveCenter = Matrix3.Cartesian3.midpoint(
  298. minBoxPt,
  299. maxBoxPt,
  300. fromPointsNaiveCenterScratch
  301. );
  302. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  303. let naiveRadius = 0;
  304. for (i = 0; i < numPositions; i++) {
  305. Matrix3.Cartesian3.clone(positions[i], currentPos);
  306. // Find the furthest point from the naive center to calculate the naive radius.
  307. const r = Matrix3.Cartesian3.magnitude(
  308. Matrix3.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  309. );
  310. if (r > naiveRadius) {
  311. naiveRadius = r;
  312. }
  313. // Make adjustments to the Ritter Sphere to include all points.
  314. const oldCenterToPointSquared = Matrix3.Cartesian3.magnitudeSquared(
  315. Matrix3.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  316. );
  317. if (oldCenterToPointSquared > radiusSquared) {
  318. const oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  319. // Calculate new radius to include the point that lies outside
  320. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  321. radiusSquared = ritterRadius * ritterRadius;
  322. // Calculate center of new Ritter sphere
  323. const oldToNew = oldCenterToPoint - ritterRadius;
  324. ritterCenter.x =
  325. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  326. oldCenterToPoint;
  327. ritterCenter.y =
  328. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  329. oldCenterToPoint;
  330. ritterCenter.z =
  331. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  332. oldCenterToPoint;
  333. }
  334. }
  335. if (ritterRadius < naiveRadius) {
  336. Matrix3.Cartesian3.clone(ritterCenter, result.center);
  337. result.radius = ritterRadius;
  338. } else {
  339. Matrix3.Cartesian3.clone(naiveCenter, result.center);
  340. result.radius = naiveRadius;
  341. }
  342. return result;
  343. };
  344. const defaultProjection = new GeographicProjection();
  345. const fromRectangle2DLowerLeft = new Matrix3.Cartesian3();
  346. const fromRectangle2DUpperRight = new Matrix3.Cartesian3();
  347. const fromRectangle2DSouthwest = new Matrix3.Cartographic();
  348. const fromRectangle2DNortheast = new Matrix3.Cartographic();
  349. /**
  350. * Computes a bounding sphere from a rectangle projected in 2D.
  351. *
  352. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  353. * @param {object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  354. * @param {BoundingSphere} [result] The object onto which to store the result.
  355. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  356. */
  357. BoundingSphere.fromRectangle2D = function (rectangle, projection, result) {
  358. return BoundingSphere.fromRectangleWithHeights2D(
  359. rectangle,
  360. projection,
  361. 0.0,
  362. 0.0,
  363. result
  364. );
  365. };
  366. /**
  367. * Computes a bounding sphere from a rectangle projected in 2D. The bounding sphere accounts for the
  368. * object's minimum and maximum heights over the rectangle.
  369. *
  370. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  371. * @param {object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  372. * @param {number} [minimumHeight=0.0] The minimum height over the rectangle.
  373. * @param {number} [maximumHeight=0.0] The maximum height over the rectangle.
  374. * @param {BoundingSphere} [result] The object onto which to store the result.
  375. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  376. */
  377. BoundingSphere.fromRectangleWithHeights2D = function (
  378. rectangle,
  379. projection,
  380. minimumHeight,
  381. maximumHeight,
  382. result
  383. ) {
  384. if (!defaultValue.defined(result)) {
  385. result = new BoundingSphere();
  386. }
  387. if (!defaultValue.defined(rectangle)) {
  388. result.center = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO, result.center);
  389. result.radius = 0.0;
  390. return result;
  391. }
  392. projection = defaultValue.defaultValue(projection, defaultProjection);
  393. Matrix2.Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
  394. fromRectangle2DSouthwest.height = minimumHeight;
  395. Matrix2.Rectangle.northeast(rectangle, fromRectangle2DNortheast);
  396. fromRectangle2DNortheast.height = maximumHeight;
  397. const lowerLeft = projection.project(
  398. fromRectangle2DSouthwest,
  399. fromRectangle2DLowerLeft
  400. );
  401. const upperRight = projection.project(
  402. fromRectangle2DNortheast,
  403. fromRectangle2DUpperRight
  404. );
  405. const width = upperRight.x - lowerLeft.x;
  406. const height = upperRight.y - lowerLeft.y;
  407. const elevation = upperRight.z - lowerLeft.z;
  408. result.radius =
  409. Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
  410. const center = result.center;
  411. center.x = lowerLeft.x + width * 0.5;
  412. center.y = lowerLeft.y + height * 0.5;
  413. center.z = lowerLeft.z + elevation * 0.5;
  414. return result;
  415. };
  416. const fromRectangle3DScratch = [];
  417. /**
  418. * Computes a bounding sphere from a rectangle in 3D. The bounding sphere is created using a subsample of points
  419. * on the ellipsoid and contained in the rectangle. It may not be accurate for all rectangles on all types of ellipsoids.
  420. *
  421. * @param {Rectangle} [rectangle] The valid rectangle used to create a bounding sphere.
  422. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine positions of the rectangle.
  423. * @param {number} [surfaceHeight=0.0] The height above the surface of the ellipsoid.
  424. * @param {BoundingSphere} [result] The object onto which to store the result.
  425. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  426. */
  427. BoundingSphere.fromRectangle3D = function (
  428. rectangle,
  429. ellipsoid,
  430. surfaceHeight,
  431. result
  432. ) {
  433. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix3.Ellipsoid.WGS84);
  434. surfaceHeight = defaultValue.defaultValue(surfaceHeight, 0.0);
  435. if (!defaultValue.defined(result)) {
  436. result = new BoundingSphere();
  437. }
  438. if (!defaultValue.defined(rectangle)) {
  439. result.center = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO, result.center);
  440. result.radius = 0.0;
  441. return result;
  442. }
  443. const positions = Matrix2.Rectangle.subsample(
  444. rectangle,
  445. ellipsoid,
  446. surfaceHeight,
  447. fromRectangle3DScratch
  448. );
  449. return BoundingSphere.fromPoints(positions, result);
  450. };
  451. /**
  452. * Computes a tight-fitting bounding sphere enclosing a list of 3D points, where the points are
  453. * stored in a flat array in X, Y, Z, order. The bounding sphere is computed by running two
  454. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  455. * ensure a tight fit.
  456. *
  457. * @param {number[]} [positions] An array of points that the bounding sphere will enclose. Each point
  458. * is formed from three elements in the array in the order X, Y, Z.
  459. * @param {Cartesian3} [center=Cartesian3.ZERO] The position to which the positions are relative, which need not be the
  460. * origin of the coordinate system. This is useful when the positions are to be used for
  461. * relative-to-center (RTC) rendering.
  462. * @param {number} [stride=3] The number of array elements per vertex. It must be at least 3, but it may
  463. * be higher. Regardless of the value of this parameter, the X coordinate of the first position
  464. * is at array index 0, the Y coordinate is at array index 1, and the Z coordinate is at array index
  465. * 2. When stride is 3, the X coordinate of the next position then begins at array index 3. If
  466. * the stride is 5, however, two array elements are skipped and the next position begins at array
  467. * index 5.
  468. * @param {BoundingSphere} [result] The object onto which to store the result.
  469. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  470. *
  471. * @example
  472. * // Compute the bounding sphere from 3 positions, each specified relative to a center.
  473. * // In addition to the X, Y, and Z coordinates, the points array contains two additional
  474. * // elements per point which are ignored for the purpose of computing the bounding sphere.
  475. * const center = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  476. * const points = [1.0, 2.0, 3.0, 0.1, 0.2,
  477. * 4.0, 5.0, 6.0, 0.1, 0.2,
  478. * 7.0, 8.0, 9.0, 0.1, 0.2];
  479. * const sphere = Cesium.BoundingSphere.fromVertices(points, center, 5);
  480. *
  481. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  482. */
  483. BoundingSphere.fromVertices = function (positions, center, stride, result) {
  484. if (!defaultValue.defined(result)) {
  485. result = new BoundingSphere();
  486. }
  487. if (!defaultValue.defined(positions) || positions.length === 0) {
  488. result.center = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO, result.center);
  489. result.radius = 0.0;
  490. return result;
  491. }
  492. center = defaultValue.defaultValue(center, Matrix3.Cartesian3.ZERO);
  493. stride = defaultValue.defaultValue(stride, 3);
  494. //>>includeStart('debug', pragmas.debug);
  495. Check.Check.typeOf.number.greaterThanOrEquals("stride", stride, 3);
  496. //>>includeEnd('debug');
  497. const currentPos = fromPointsCurrentPos;
  498. currentPos.x = positions[0] + center.x;
  499. currentPos.y = positions[1] + center.y;
  500. currentPos.z = positions[2] + center.z;
  501. const xMin = Matrix3.Cartesian3.clone(currentPos, fromPointsXMin);
  502. const yMin = Matrix3.Cartesian3.clone(currentPos, fromPointsYMin);
  503. const zMin = Matrix3.Cartesian3.clone(currentPos, fromPointsZMin);
  504. const xMax = Matrix3.Cartesian3.clone(currentPos, fromPointsXMax);
  505. const yMax = Matrix3.Cartesian3.clone(currentPos, fromPointsYMax);
  506. const zMax = Matrix3.Cartesian3.clone(currentPos, fromPointsZMax);
  507. const numElements = positions.length;
  508. let i;
  509. for (i = 0; i < numElements; i += stride) {
  510. const x = positions[i] + center.x;
  511. const y = positions[i + 1] + center.y;
  512. const z = positions[i + 2] + center.z;
  513. currentPos.x = x;
  514. currentPos.y = y;
  515. currentPos.z = z;
  516. // Store points containing the the smallest and largest components
  517. if (x < xMin.x) {
  518. Matrix3.Cartesian3.clone(currentPos, xMin);
  519. }
  520. if (x > xMax.x) {
  521. Matrix3.Cartesian3.clone(currentPos, xMax);
  522. }
  523. if (y < yMin.y) {
  524. Matrix3.Cartesian3.clone(currentPos, yMin);
  525. }
  526. if (y > yMax.y) {
  527. Matrix3.Cartesian3.clone(currentPos, yMax);
  528. }
  529. if (z < zMin.z) {
  530. Matrix3.Cartesian3.clone(currentPos, zMin);
  531. }
  532. if (z > zMax.z) {
  533. Matrix3.Cartesian3.clone(currentPos, zMax);
  534. }
  535. }
  536. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  537. const xSpan = Matrix3.Cartesian3.magnitudeSquared(
  538. Matrix3.Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  539. );
  540. const ySpan = Matrix3.Cartesian3.magnitudeSquared(
  541. Matrix3.Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  542. );
  543. const zSpan = Matrix3.Cartesian3.magnitudeSquared(
  544. Matrix3.Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  545. );
  546. // Set the diameter endpoints to the largest span.
  547. let diameter1 = xMin;
  548. let diameter2 = xMax;
  549. let maxSpan = xSpan;
  550. if (ySpan > maxSpan) {
  551. maxSpan = ySpan;
  552. diameter1 = yMin;
  553. diameter2 = yMax;
  554. }
  555. if (zSpan > maxSpan) {
  556. maxSpan = zSpan;
  557. diameter1 = zMin;
  558. diameter2 = zMax;
  559. }
  560. // Calculate the center of the initial sphere found by Ritter's algorithm
  561. const ritterCenter = fromPointsRitterCenter;
  562. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  563. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  564. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  565. // Calculate the radius of the initial sphere found by Ritter's algorithm
  566. let radiusSquared = Matrix3.Cartesian3.magnitudeSquared(
  567. Matrix3.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  568. );
  569. let ritterRadius = Math.sqrt(radiusSquared);
  570. // Find the center of the sphere found using the Naive method.
  571. const minBoxPt = fromPointsMinBoxPt;
  572. minBoxPt.x = xMin.x;
  573. minBoxPt.y = yMin.y;
  574. minBoxPt.z = zMin.z;
  575. const maxBoxPt = fromPointsMaxBoxPt;
  576. maxBoxPt.x = xMax.x;
  577. maxBoxPt.y = yMax.y;
  578. maxBoxPt.z = zMax.z;
  579. const naiveCenter = Matrix3.Cartesian3.midpoint(
  580. minBoxPt,
  581. maxBoxPt,
  582. fromPointsNaiveCenterScratch
  583. );
  584. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  585. let naiveRadius = 0;
  586. for (i = 0; i < numElements; i += stride) {
  587. currentPos.x = positions[i] + center.x;
  588. currentPos.y = positions[i + 1] + center.y;
  589. currentPos.z = positions[i + 2] + center.z;
  590. // Find the furthest point from the naive center to calculate the naive radius.
  591. const r = Matrix3.Cartesian3.magnitude(
  592. Matrix3.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  593. );
  594. if (r > naiveRadius) {
  595. naiveRadius = r;
  596. }
  597. // Make adjustments to the Ritter Sphere to include all points.
  598. const oldCenterToPointSquared = Matrix3.Cartesian3.magnitudeSquared(
  599. Matrix3.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  600. );
  601. if (oldCenterToPointSquared > radiusSquared) {
  602. const oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  603. // Calculate new radius to include the point that lies outside
  604. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  605. radiusSquared = ritterRadius * ritterRadius;
  606. // Calculate center of new Ritter sphere
  607. const oldToNew = oldCenterToPoint - ritterRadius;
  608. ritterCenter.x =
  609. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  610. oldCenterToPoint;
  611. ritterCenter.y =
  612. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  613. oldCenterToPoint;
  614. ritterCenter.z =
  615. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  616. oldCenterToPoint;
  617. }
  618. }
  619. if (ritterRadius < naiveRadius) {
  620. Matrix3.Cartesian3.clone(ritterCenter, result.center);
  621. result.radius = ritterRadius;
  622. } else {
  623. Matrix3.Cartesian3.clone(naiveCenter, result.center);
  624. result.radius = naiveRadius;
  625. }
  626. return result;
  627. };
  628. /**
  629. * Computes a tight-fitting bounding sphere enclosing a list of EncodedCartesian3s, where the points are
  630. * stored in parallel flat arrays in X, Y, Z, order. The bounding sphere is computed by running two
  631. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  632. * ensure a tight fit.
  633. *
  634. * @param {number[]} [positionsHigh] An array of high bits of the encoded cartesians that the bounding sphere will enclose. Each point
  635. * is formed from three elements in the array in the order X, Y, Z.
  636. * @param {number[]} [positionsLow] An array of low bits of the encoded cartesians that the bounding sphere will enclose. Each point
  637. * is formed from three elements in the array in the order X, Y, Z.
  638. * @param {BoundingSphere} [result] The object onto which to store the result.
  639. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  640. *
  641. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  642. */
  643. BoundingSphere.fromEncodedCartesianVertices = function (
  644. positionsHigh,
  645. positionsLow,
  646. result
  647. ) {
  648. if (!defaultValue.defined(result)) {
  649. result = new BoundingSphere();
  650. }
  651. if (
  652. !defaultValue.defined(positionsHigh) ||
  653. !defaultValue.defined(positionsLow) ||
  654. positionsHigh.length !== positionsLow.length ||
  655. positionsHigh.length === 0
  656. ) {
  657. result.center = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO, result.center);
  658. result.radius = 0.0;
  659. return result;
  660. }
  661. const currentPos = fromPointsCurrentPos;
  662. currentPos.x = positionsHigh[0] + positionsLow[0];
  663. currentPos.y = positionsHigh[1] + positionsLow[1];
  664. currentPos.z = positionsHigh[2] + positionsLow[2];
  665. const xMin = Matrix3.Cartesian3.clone(currentPos, fromPointsXMin);
  666. const yMin = Matrix3.Cartesian3.clone(currentPos, fromPointsYMin);
  667. const zMin = Matrix3.Cartesian3.clone(currentPos, fromPointsZMin);
  668. const xMax = Matrix3.Cartesian3.clone(currentPos, fromPointsXMax);
  669. const yMax = Matrix3.Cartesian3.clone(currentPos, fromPointsYMax);
  670. const zMax = Matrix3.Cartesian3.clone(currentPos, fromPointsZMax);
  671. const numElements = positionsHigh.length;
  672. let i;
  673. for (i = 0; i < numElements; i += 3) {
  674. const x = positionsHigh[i] + positionsLow[i];
  675. const y = positionsHigh[i + 1] + positionsLow[i + 1];
  676. const z = positionsHigh[i + 2] + positionsLow[i + 2];
  677. currentPos.x = x;
  678. currentPos.y = y;
  679. currentPos.z = z;
  680. // Store points containing the the smallest and largest components
  681. if (x < xMin.x) {
  682. Matrix3.Cartesian3.clone(currentPos, xMin);
  683. }
  684. if (x > xMax.x) {
  685. Matrix3.Cartesian3.clone(currentPos, xMax);
  686. }
  687. if (y < yMin.y) {
  688. Matrix3.Cartesian3.clone(currentPos, yMin);
  689. }
  690. if (y > yMax.y) {
  691. Matrix3.Cartesian3.clone(currentPos, yMax);
  692. }
  693. if (z < zMin.z) {
  694. Matrix3.Cartesian3.clone(currentPos, zMin);
  695. }
  696. if (z > zMax.z) {
  697. Matrix3.Cartesian3.clone(currentPos, zMax);
  698. }
  699. }
  700. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  701. const xSpan = Matrix3.Cartesian3.magnitudeSquared(
  702. Matrix3.Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  703. );
  704. const ySpan = Matrix3.Cartesian3.magnitudeSquared(
  705. Matrix3.Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  706. );
  707. const zSpan = Matrix3.Cartesian3.magnitudeSquared(
  708. Matrix3.Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  709. );
  710. // Set the diameter endpoints to the largest span.
  711. let diameter1 = xMin;
  712. let diameter2 = xMax;
  713. let maxSpan = xSpan;
  714. if (ySpan > maxSpan) {
  715. maxSpan = ySpan;
  716. diameter1 = yMin;
  717. diameter2 = yMax;
  718. }
  719. if (zSpan > maxSpan) {
  720. maxSpan = zSpan;
  721. diameter1 = zMin;
  722. diameter2 = zMax;
  723. }
  724. // Calculate the center of the initial sphere found by Ritter's algorithm
  725. const ritterCenter = fromPointsRitterCenter;
  726. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  727. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  728. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  729. // Calculate the radius of the initial sphere found by Ritter's algorithm
  730. let radiusSquared = Matrix3.Cartesian3.magnitudeSquared(
  731. Matrix3.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  732. );
  733. let ritterRadius = Math.sqrt(radiusSquared);
  734. // Find the center of the sphere found using the Naive method.
  735. const minBoxPt = fromPointsMinBoxPt;
  736. minBoxPt.x = xMin.x;
  737. minBoxPt.y = yMin.y;
  738. minBoxPt.z = zMin.z;
  739. const maxBoxPt = fromPointsMaxBoxPt;
  740. maxBoxPt.x = xMax.x;
  741. maxBoxPt.y = yMax.y;
  742. maxBoxPt.z = zMax.z;
  743. const naiveCenter = Matrix3.Cartesian3.midpoint(
  744. minBoxPt,
  745. maxBoxPt,
  746. fromPointsNaiveCenterScratch
  747. );
  748. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  749. let naiveRadius = 0;
  750. for (i = 0; i < numElements; i += 3) {
  751. currentPos.x = positionsHigh[i] + positionsLow[i];
  752. currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
  753. currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
  754. // Find the furthest point from the naive center to calculate the naive radius.
  755. const r = Matrix3.Cartesian3.magnitude(
  756. Matrix3.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  757. );
  758. if (r > naiveRadius) {
  759. naiveRadius = r;
  760. }
  761. // Make adjustments to the Ritter Sphere to include all points.
  762. const oldCenterToPointSquared = Matrix3.Cartesian3.magnitudeSquared(
  763. Matrix3.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  764. );
  765. if (oldCenterToPointSquared > radiusSquared) {
  766. const oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  767. // Calculate new radius to include the point that lies outside
  768. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  769. radiusSquared = ritterRadius * ritterRadius;
  770. // Calculate center of new Ritter sphere
  771. const oldToNew = oldCenterToPoint - ritterRadius;
  772. ritterCenter.x =
  773. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  774. oldCenterToPoint;
  775. ritterCenter.y =
  776. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  777. oldCenterToPoint;
  778. ritterCenter.z =
  779. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  780. oldCenterToPoint;
  781. }
  782. }
  783. if (ritterRadius < naiveRadius) {
  784. Matrix3.Cartesian3.clone(ritterCenter, result.center);
  785. result.radius = ritterRadius;
  786. } else {
  787. Matrix3.Cartesian3.clone(naiveCenter, result.center);
  788. result.radius = naiveRadius;
  789. }
  790. return result;
  791. };
  792. /**
  793. * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
  794. * tightly and fully encompasses the box.
  795. *
  796. * @param {Cartesian3} [corner] The minimum height over the rectangle.
  797. * @param {Cartesian3} [oppositeCorner] The maximum height over the rectangle.
  798. * @param {BoundingSphere} [result] The object onto which to store the result.
  799. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  800. *
  801. * @example
  802. * // Create a bounding sphere around the unit cube
  803. * const sphere = Cesium.BoundingSphere.fromCornerPoints(new Cesium.Cartesian3(-0.5, -0.5, -0.5), new Cesium.Cartesian3(0.5, 0.5, 0.5));
  804. */
  805. BoundingSphere.fromCornerPoints = function (corner, oppositeCorner, result) {
  806. //>>includeStart('debug', pragmas.debug);
  807. Check.Check.typeOf.object("corner", corner);
  808. Check.Check.typeOf.object("oppositeCorner", oppositeCorner);
  809. //>>includeEnd('debug');
  810. if (!defaultValue.defined(result)) {
  811. result = new BoundingSphere();
  812. }
  813. const center = Matrix3.Cartesian3.midpoint(corner, oppositeCorner, result.center);
  814. result.radius = Matrix3.Cartesian3.distance(center, oppositeCorner);
  815. return result;
  816. };
  817. /**
  818. * Creates a bounding sphere encompassing an ellipsoid.
  819. *
  820. * @param {Ellipsoid} ellipsoid The ellipsoid around which to create a bounding sphere.
  821. * @param {BoundingSphere} [result] The object onto which to store the result.
  822. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  823. *
  824. * @example
  825. * const boundingSphere = Cesium.BoundingSphere.fromEllipsoid(ellipsoid);
  826. */
  827. BoundingSphere.fromEllipsoid = function (ellipsoid, result) {
  828. //>>includeStart('debug', pragmas.debug);
  829. Check.Check.typeOf.object("ellipsoid", ellipsoid);
  830. //>>includeEnd('debug');
  831. if (!defaultValue.defined(result)) {
  832. result = new BoundingSphere();
  833. }
  834. Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO, result.center);
  835. result.radius = ellipsoid.maximumRadius;
  836. return result;
  837. };
  838. const fromBoundingSpheresScratch = new Matrix3.Cartesian3();
  839. /**
  840. * Computes a tight-fitting bounding sphere enclosing the provided array of bounding spheres.
  841. *
  842. * @param {BoundingSphere[]} [boundingSpheres] The array of bounding spheres.
  843. * @param {BoundingSphere} [result] The object onto which to store the result.
  844. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  845. */
  846. BoundingSphere.fromBoundingSpheres = function (boundingSpheres, result) {
  847. if (!defaultValue.defined(result)) {
  848. result = new BoundingSphere();
  849. }
  850. if (!defaultValue.defined(boundingSpheres) || boundingSpheres.length === 0) {
  851. result.center = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO, result.center);
  852. result.radius = 0.0;
  853. return result;
  854. }
  855. const length = boundingSpheres.length;
  856. if (length === 1) {
  857. return BoundingSphere.clone(boundingSpheres[0], result);
  858. }
  859. if (length === 2) {
  860. return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
  861. }
  862. const positions = [];
  863. let i;
  864. for (i = 0; i < length; i++) {
  865. positions.push(boundingSpheres[i].center);
  866. }
  867. result = BoundingSphere.fromPoints(positions, result);
  868. const center = result.center;
  869. let radius = result.radius;
  870. for (i = 0; i < length; i++) {
  871. const tmp = boundingSpheres[i];
  872. radius = Math.max(
  873. radius,
  874. Matrix3.Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) +
  875. tmp.radius
  876. );
  877. }
  878. result.radius = radius;
  879. return result;
  880. };
  881. const fromOrientedBoundingBoxScratchU = new Matrix3.Cartesian3();
  882. const fromOrientedBoundingBoxScratchV = new Matrix3.Cartesian3();
  883. const fromOrientedBoundingBoxScratchW = new Matrix3.Cartesian3();
  884. /**
  885. * Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
  886. *
  887. * @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
  888. * @param {BoundingSphere} [result] The object onto which to store the result.
  889. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  890. */
  891. BoundingSphere.fromOrientedBoundingBox = function (
  892. orientedBoundingBox,
  893. result
  894. ) {
  895. //>>includeStart('debug', pragmas.debug);
  896. Check.Check.defined("orientedBoundingBox", orientedBoundingBox);
  897. //>>includeEnd('debug');
  898. if (!defaultValue.defined(result)) {
  899. result = new BoundingSphere();
  900. }
  901. const halfAxes = orientedBoundingBox.halfAxes;
  902. const u = Matrix3.Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
  903. const v = Matrix3.Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
  904. const w = Matrix3.Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
  905. Matrix3.Cartesian3.add(u, v, u);
  906. Matrix3.Cartesian3.add(u, w, u);
  907. result.center = Matrix3.Cartesian3.clone(orientedBoundingBox.center, result.center);
  908. result.radius = Matrix3.Cartesian3.magnitude(u);
  909. return result;
  910. };
  911. const scratchFromTransformationCenter = new Matrix3.Cartesian3();
  912. const scratchFromTransformationScale = new Matrix3.Cartesian3();
  913. /**
  914. * Computes a tight-fitting bounding sphere enclosing the provided affine transformation.
  915. *
  916. * @param {Matrix4} transformation The affine transformation.
  917. * @param {BoundingSphere} [result] The object onto which to store the result.
  918. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  919. */
  920. BoundingSphere.fromTransformation = function (transformation, result) {
  921. //>>includeStart('debug', pragmas.debug);
  922. Check.Check.typeOf.object("transformation", transformation);
  923. //>>includeEnd('debug');
  924. if (!defaultValue.defined(result)) {
  925. result = new BoundingSphere();
  926. }
  927. const center = Matrix2.Matrix4.getTranslation(
  928. transformation,
  929. scratchFromTransformationCenter
  930. );
  931. const scale = Matrix2.Matrix4.getScale(
  932. transformation,
  933. scratchFromTransformationScale
  934. );
  935. const radius = 0.5 * Matrix3.Cartesian3.magnitude(scale);
  936. result.center = Matrix3.Cartesian3.clone(center, result.center);
  937. result.radius = radius;
  938. return result;
  939. };
  940. /**
  941. * Duplicates a BoundingSphere instance.
  942. *
  943. * @param {BoundingSphere} sphere The bounding sphere to duplicate.
  944. * @param {BoundingSphere} [result] The object onto which to store the result.
  945. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided. (Returns undefined if sphere is undefined)
  946. */
  947. BoundingSphere.clone = function (sphere, result) {
  948. if (!defaultValue.defined(sphere)) {
  949. return undefined;
  950. }
  951. if (!defaultValue.defined(result)) {
  952. return new BoundingSphere(sphere.center, sphere.radius);
  953. }
  954. result.center = Matrix3.Cartesian3.clone(sphere.center, result.center);
  955. result.radius = sphere.radius;
  956. return result;
  957. };
  958. /**
  959. * The number of elements used to pack the object into an array.
  960. * @type {number}
  961. */
  962. BoundingSphere.packedLength = 4;
  963. /**
  964. * Stores the provided instance into the provided array.
  965. *
  966. * @param {BoundingSphere} value The value to pack.
  967. * @param {number[]} array The array to pack into.
  968. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  969. *
  970. * @returns {number[]} The array that was packed into
  971. */
  972. BoundingSphere.pack = function (value, array, startingIndex) {
  973. //>>includeStart('debug', pragmas.debug);
  974. Check.Check.typeOf.object("value", value);
  975. Check.Check.defined("array", array);
  976. //>>includeEnd('debug');
  977. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  978. const center = value.center;
  979. array[startingIndex++] = center.x;
  980. array[startingIndex++] = center.y;
  981. array[startingIndex++] = center.z;
  982. array[startingIndex] = value.radius;
  983. return array;
  984. };
  985. /**
  986. * Retrieves an instance from a packed array.
  987. *
  988. * @param {number[]} array The packed array.
  989. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  990. * @param {BoundingSphere} [result] The object into which to store the result.
  991. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  992. */
  993. BoundingSphere.unpack = function (array, startingIndex, result) {
  994. //>>includeStart('debug', pragmas.debug);
  995. Check.Check.defined("array", array);
  996. //>>includeEnd('debug');
  997. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  998. if (!defaultValue.defined(result)) {
  999. result = new BoundingSphere();
  1000. }
  1001. const center = result.center;
  1002. center.x = array[startingIndex++];
  1003. center.y = array[startingIndex++];
  1004. center.z = array[startingIndex++];
  1005. result.radius = array[startingIndex];
  1006. return result;
  1007. };
  1008. const unionScratch = new Matrix3.Cartesian3();
  1009. const unionScratchCenter = new Matrix3.Cartesian3();
  1010. /**
  1011. * Computes a bounding sphere that contains both the left and right bounding spheres.
  1012. *
  1013. * @param {BoundingSphere} left A sphere to enclose in a bounding sphere.
  1014. * @param {BoundingSphere} right A sphere to enclose in a bounding sphere.
  1015. * @param {BoundingSphere} [result] The object onto which to store the result.
  1016. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1017. */
  1018. BoundingSphere.union = function (left, right, result) {
  1019. //>>includeStart('debug', pragmas.debug);
  1020. Check.Check.typeOf.object("left", left);
  1021. Check.Check.typeOf.object("right", right);
  1022. //>>includeEnd('debug');
  1023. if (!defaultValue.defined(result)) {
  1024. result = new BoundingSphere();
  1025. }
  1026. const leftCenter = left.center;
  1027. const leftRadius = left.radius;
  1028. const rightCenter = right.center;
  1029. const rightRadius = right.radius;
  1030. const toRightCenter = Matrix3.Cartesian3.subtract(
  1031. rightCenter,
  1032. leftCenter,
  1033. unionScratch
  1034. );
  1035. const centerSeparation = Matrix3.Cartesian3.magnitude(toRightCenter);
  1036. if (leftRadius >= centerSeparation + rightRadius) {
  1037. // Left sphere wins.
  1038. left.clone(result);
  1039. return result;
  1040. }
  1041. if (rightRadius >= centerSeparation + leftRadius) {
  1042. // Right sphere wins.
  1043. right.clone(result);
  1044. return result;
  1045. }
  1046. // There are two tangent points, one on far side of each sphere.
  1047. const halfDistanceBetweenTangentPoints =
  1048. (leftRadius + centerSeparation + rightRadius) * 0.5;
  1049. // Compute the center point halfway between the two tangent points.
  1050. const center = Matrix3.Cartesian3.multiplyByScalar(
  1051. toRightCenter,
  1052. (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation,
  1053. unionScratchCenter
  1054. );
  1055. Matrix3.Cartesian3.add(center, leftCenter, center);
  1056. Matrix3.Cartesian3.clone(center, result.center);
  1057. result.radius = halfDistanceBetweenTangentPoints;
  1058. return result;
  1059. };
  1060. const expandScratch = new Matrix3.Cartesian3();
  1061. /**
  1062. * Computes a bounding sphere by enlarging the provided sphere to contain the provided point.
  1063. *
  1064. * @param {BoundingSphere} sphere A sphere to expand.
  1065. * @param {Cartesian3} point A point to enclose in a bounding sphere.
  1066. * @param {BoundingSphere} [result] The object onto which to store the result.
  1067. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1068. */
  1069. BoundingSphere.expand = function (sphere, point, result) {
  1070. //>>includeStart('debug', pragmas.debug);
  1071. Check.Check.typeOf.object("sphere", sphere);
  1072. Check.Check.typeOf.object("point", point);
  1073. //>>includeEnd('debug');
  1074. result = BoundingSphere.clone(sphere, result);
  1075. const radius = Matrix3.Cartesian3.magnitude(
  1076. Matrix3.Cartesian3.subtract(point, result.center, expandScratch)
  1077. );
  1078. if (radius > result.radius) {
  1079. result.radius = radius;
  1080. }
  1081. return result;
  1082. };
  1083. /**
  1084. * Determines which side of a plane a sphere is located.
  1085. *
  1086. * @param {BoundingSphere} sphere The bounding sphere to test.
  1087. * @param {Plane} plane The plane to test against.
  1088. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  1089. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  1090. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  1091. * intersects the plane.
  1092. */
  1093. BoundingSphere.intersectPlane = function (sphere, plane) {
  1094. //>>includeStart('debug', pragmas.debug);
  1095. Check.Check.typeOf.object("sphere", sphere);
  1096. Check.Check.typeOf.object("plane", plane);
  1097. //>>includeEnd('debug');
  1098. const center = sphere.center;
  1099. const radius = sphere.radius;
  1100. const normal = plane.normal;
  1101. const distanceToPlane = Matrix3.Cartesian3.dot(normal, center) + plane.distance;
  1102. if (distanceToPlane < -radius) {
  1103. // The center point is negative side of the plane normal
  1104. return Intersect$1.OUTSIDE;
  1105. } else if (distanceToPlane < radius) {
  1106. // The center point is positive side of the plane, but radius extends beyond it; partial overlap
  1107. return Intersect$1.INTERSECTING;
  1108. }
  1109. return Intersect$1.INSIDE;
  1110. };
  1111. /**
  1112. * Applies a 4x4 affine transformation matrix to a bounding sphere.
  1113. *
  1114. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  1115. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  1116. * @param {BoundingSphere} [result] The object onto which to store the result.
  1117. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1118. */
  1119. BoundingSphere.transform = function (sphere, transform, result) {
  1120. //>>includeStart('debug', pragmas.debug);
  1121. Check.Check.typeOf.object("sphere", sphere);
  1122. Check.Check.typeOf.object("transform", transform);
  1123. //>>includeEnd('debug');
  1124. if (!defaultValue.defined(result)) {
  1125. result = new BoundingSphere();
  1126. }
  1127. result.center = Matrix2.Matrix4.multiplyByPoint(
  1128. transform,
  1129. sphere.center,
  1130. result.center
  1131. );
  1132. result.radius = Matrix2.Matrix4.getMaximumScale(transform) * sphere.radius;
  1133. return result;
  1134. };
  1135. const distanceSquaredToScratch = new Matrix3.Cartesian3();
  1136. /**
  1137. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  1138. *
  1139. * @param {BoundingSphere} sphere The sphere.
  1140. * @param {Cartesian3} cartesian The point
  1141. * @returns {number} The distance squared from the bounding sphere to the point. Returns 0 if the point is inside the sphere.
  1142. *
  1143. * @example
  1144. * // Sort bounding spheres from back to front
  1145. * spheres.sort(function(a, b) {
  1146. * return Cesium.BoundingSphere.distanceSquaredTo(b, camera.positionWC) - Cesium.BoundingSphere.distanceSquaredTo(a, camera.positionWC);
  1147. * });
  1148. */
  1149. BoundingSphere.distanceSquaredTo = function (sphere, cartesian) {
  1150. //>>includeStart('debug', pragmas.debug);
  1151. Check.Check.typeOf.object("sphere", sphere);
  1152. Check.Check.typeOf.object("cartesian", cartesian);
  1153. //>>includeEnd('debug');
  1154. const diff = Matrix3.Cartesian3.subtract(
  1155. sphere.center,
  1156. cartesian,
  1157. distanceSquaredToScratch
  1158. );
  1159. const distance = Matrix3.Cartesian3.magnitude(diff) - sphere.radius;
  1160. if (distance <= 0.0) {
  1161. return 0.0;
  1162. }
  1163. return distance * distance;
  1164. };
  1165. /**
  1166. * Applies a 4x4 affine transformation matrix to a bounding sphere where there is no scale
  1167. * The transformation matrix is not verified to have a uniform scale of 1.
  1168. * This method is faster than computing the general bounding sphere transform using {@link BoundingSphere.transform}.
  1169. *
  1170. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  1171. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  1172. * @param {BoundingSphere} [result] The object onto which to store the result.
  1173. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1174. *
  1175. * @example
  1176. * const modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid);
  1177. * const boundingSphere = new Cesium.BoundingSphere();
  1178. * const newBoundingSphere = Cesium.BoundingSphere.transformWithoutScale(boundingSphere, modelMatrix);
  1179. */
  1180. BoundingSphere.transformWithoutScale = function (sphere, transform, result) {
  1181. //>>includeStart('debug', pragmas.debug);
  1182. Check.Check.typeOf.object("sphere", sphere);
  1183. Check.Check.typeOf.object("transform", transform);
  1184. //>>includeEnd('debug');
  1185. if (!defaultValue.defined(result)) {
  1186. result = new BoundingSphere();
  1187. }
  1188. result.center = Matrix2.Matrix4.multiplyByPoint(
  1189. transform,
  1190. sphere.center,
  1191. result.center
  1192. );
  1193. result.radius = sphere.radius;
  1194. return result;
  1195. };
  1196. const scratchCartesian3 = new Matrix3.Cartesian3();
  1197. /**
  1198. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  1199. * plus/minus the radius of the bounding sphere.
  1200. * <br>
  1201. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  1202. * closest and farthest planes from position that intersect the bounding sphere.
  1203. *
  1204. * @param {BoundingSphere} sphere The bounding sphere to calculate the distance to.
  1205. * @param {Cartesian3} position The position to calculate the distance from.
  1206. * @param {Cartesian3} direction The direction from position.
  1207. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  1208. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  1209. */
  1210. BoundingSphere.computePlaneDistances = function (
  1211. sphere,
  1212. position,
  1213. direction,
  1214. result
  1215. ) {
  1216. //>>includeStart('debug', pragmas.debug);
  1217. Check.Check.typeOf.object("sphere", sphere);
  1218. Check.Check.typeOf.object("position", position);
  1219. Check.Check.typeOf.object("direction", direction);
  1220. //>>includeEnd('debug');
  1221. if (!defaultValue.defined(result)) {
  1222. result = new Interval();
  1223. }
  1224. const toCenter = Matrix3.Cartesian3.subtract(
  1225. sphere.center,
  1226. position,
  1227. scratchCartesian3
  1228. );
  1229. const mag = Matrix3.Cartesian3.dot(direction, toCenter);
  1230. result.start = mag - sphere.radius;
  1231. result.stop = mag + sphere.radius;
  1232. return result;
  1233. };
  1234. const projectTo2DNormalScratch = new Matrix3.Cartesian3();
  1235. const projectTo2DEastScratch = new Matrix3.Cartesian3();
  1236. const projectTo2DNorthScratch = new Matrix3.Cartesian3();
  1237. const projectTo2DWestScratch = new Matrix3.Cartesian3();
  1238. const projectTo2DSouthScratch = new Matrix3.Cartesian3();
  1239. const projectTo2DCartographicScratch = new Matrix3.Cartographic();
  1240. const projectTo2DPositionsScratch = new Array(8);
  1241. for (let n = 0; n < 8; ++n) {
  1242. projectTo2DPositionsScratch[n] = new Matrix3.Cartesian3();
  1243. }
  1244. const projectTo2DProjection = new GeographicProjection();
  1245. /**
  1246. * Creates a bounding sphere in 2D from a bounding sphere in 3D world coordinates.
  1247. *
  1248. * @param {BoundingSphere} sphere The bounding sphere to transform to 2D.
  1249. * @param {object} [projection=GeographicProjection] The projection to 2D.
  1250. * @param {BoundingSphere} [result] The object onto which to store the result.
  1251. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1252. */
  1253. BoundingSphere.projectTo2D = function (sphere, projection, result) {
  1254. //>>includeStart('debug', pragmas.debug);
  1255. Check.Check.typeOf.object("sphere", sphere);
  1256. //>>includeEnd('debug');
  1257. projection = defaultValue.defaultValue(projection, projectTo2DProjection);
  1258. const ellipsoid = projection.ellipsoid;
  1259. let center = sphere.center;
  1260. const radius = sphere.radius;
  1261. let normal;
  1262. if (Matrix3.Cartesian3.equals(center, Matrix3.Cartesian3.ZERO)) {
  1263. // Bounding sphere is at the center. The geodetic surface normal is not
  1264. // defined here so pick the x-axis as a fallback.
  1265. normal = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.UNIT_X, projectTo2DNormalScratch);
  1266. } else {
  1267. normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
  1268. }
  1269. const east = Matrix3.Cartesian3.cross(
  1270. Matrix3.Cartesian3.UNIT_Z,
  1271. normal,
  1272. projectTo2DEastScratch
  1273. );
  1274. Matrix3.Cartesian3.normalize(east, east);
  1275. const north = Matrix3.Cartesian3.cross(normal, east, projectTo2DNorthScratch);
  1276. Matrix3.Cartesian3.normalize(north, north);
  1277. Matrix3.Cartesian3.multiplyByScalar(normal, radius, normal);
  1278. Matrix3.Cartesian3.multiplyByScalar(north, radius, north);
  1279. Matrix3.Cartesian3.multiplyByScalar(east, radius, east);
  1280. const south = Matrix3.Cartesian3.negate(north, projectTo2DSouthScratch);
  1281. const west = Matrix3.Cartesian3.negate(east, projectTo2DWestScratch);
  1282. const positions = projectTo2DPositionsScratch;
  1283. // top NE corner
  1284. let corner = positions[0];
  1285. Matrix3.Cartesian3.add(normal, north, corner);
  1286. Matrix3.Cartesian3.add(corner, east, corner);
  1287. // top NW corner
  1288. corner = positions[1];
  1289. Matrix3.Cartesian3.add(normal, north, corner);
  1290. Matrix3.Cartesian3.add(corner, west, corner);
  1291. // top SW corner
  1292. corner = positions[2];
  1293. Matrix3.Cartesian3.add(normal, south, corner);
  1294. Matrix3.Cartesian3.add(corner, west, corner);
  1295. // top SE corner
  1296. corner = positions[3];
  1297. Matrix3.Cartesian3.add(normal, south, corner);
  1298. Matrix3.Cartesian3.add(corner, east, corner);
  1299. Matrix3.Cartesian3.negate(normal, normal);
  1300. // bottom NE corner
  1301. corner = positions[4];
  1302. Matrix3.Cartesian3.add(normal, north, corner);
  1303. Matrix3.Cartesian3.add(corner, east, corner);
  1304. // bottom NW corner
  1305. corner = positions[5];
  1306. Matrix3.Cartesian3.add(normal, north, corner);
  1307. Matrix3.Cartesian3.add(corner, west, corner);
  1308. // bottom SW corner
  1309. corner = positions[6];
  1310. Matrix3.Cartesian3.add(normal, south, corner);
  1311. Matrix3.Cartesian3.add(corner, west, corner);
  1312. // bottom SE corner
  1313. corner = positions[7];
  1314. Matrix3.Cartesian3.add(normal, south, corner);
  1315. Matrix3.Cartesian3.add(corner, east, corner);
  1316. const length = positions.length;
  1317. for (let i = 0; i < length; ++i) {
  1318. const position = positions[i];
  1319. Matrix3.Cartesian3.add(center, position, position);
  1320. const cartographic = ellipsoid.cartesianToCartographic(
  1321. position,
  1322. projectTo2DCartographicScratch
  1323. );
  1324. projection.project(cartographic, position);
  1325. }
  1326. result = BoundingSphere.fromPoints(positions, result);
  1327. // swizzle center components
  1328. center = result.center;
  1329. const x = center.x;
  1330. const y = center.y;
  1331. const z = center.z;
  1332. center.x = z;
  1333. center.y = x;
  1334. center.z = y;
  1335. return result;
  1336. };
  1337. /**
  1338. * Determines whether or not a sphere is hidden from view by the occluder.
  1339. *
  1340. * @param {BoundingSphere} sphere The bounding sphere surrounding the occludee object.
  1341. * @param {Occluder} occluder The occluder.
  1342. * @returns {boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  1343. */
  1344. BoundingSphere.isOccluded = function (sphere, occluder) {
  1345. //>>includeStart('debug', pragmas.debug);
  1346. Check.Check.typeOf.object("sphere", sphere);
  1347. Check.Check.typeOf.object("occluder", occluder);
  1348. //>>includeEnd('debug');
  1349. return !occluder.isBoundingSphereVisible(sphere);
  1350. };
  1351. /**
  1352. * Compares the provided BoundingSphere componentwise and returns
  1353. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1354. *
  1355. * @param {BoundingSphere} [left] The first BoundingSphere.
  1356. * @param {BoundingSphere} [right] The second BoundingSphere.
  1357. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1358. */
  1359. BoundingSphere.equals = function (left, right) {
  1360. return (
  1361. left === right ||
  1362. (defaultValue.defined(left) &&
  1363. defaultValue.defined(right) &&
  1364. Matrix3.Cartesian3.equals(left.center, right.center) &&
  1365. left.radius === right.radius)
  1366. );
  1367. };
  1368. /**
  1369. * Determines which side of a plane the sphere is located.
  1370. *
  1371. * @param {Plane} plane The plane to test against.
  1372. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  1373. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  1374. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  1375. * intersects the plane.
  1376. */
  1377. BoundingSphere.prototype.intersectPlane = function (plane) {
  1378. return BoundingSphere.intersectPlane(this, plane);
  1379. };
  1380. /**
  1381. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  1382. *
  1383. * @param {Cartesian3} cartesian The point
  1384. * @returns {number} The estimated distance squared from the bounding sphere to the point.
  1385. *
  1386. * @example
  1387. * // Sort bounding spheres from back to front
  1388. * spheres.sort(function(a, b) {
  1389. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  1390. * });
  1391. */
  1392. BoundingSphere.prototype.distanceSquaredTo = function (cartesian) {
  1393. return BoundingSphere.distanceSquaredTo(this, cartesian);
  1394. };
  1395. /**
  1396. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  1397. * plus/minus the radius of the bounding sphere.
  1398. * <br>
  1399. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  1400. * closest and farthest planes from position that intersect the bounding sphere.
  1401. *
  1402. * @param {Cartesian3} position The position to calculate the distance from.
  1403. * @param {Cartesian3} direction The direction from position.
  1404. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  1405. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  1406. */
  1407. BoundingSphere.prototype.computePlaneDistances = function (
  1408. position,
  1409. direction,
  1410. result
  1411. ) {
  1412. return BoundingSphere.computePlaneDistances(
  1413. this,
  1414. position,
  1415. direction,
  1416. result
  1417. );
  1418. };
  1419. /**
  1420. * Determines whether or not a sphere is hidden from view by the occluder.
  1421. *
  1422. * @param {Occluder} occluder The occluder.
  1423. * @returns {boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  1424. */
  1425. BoundingSphere.prototype.isOccluded = function (occluder) {
  1426. return BoundingSphere.isOccluded(this, occluder);
  1427. };
  1428. /**
  1429. * Compares this BoundingSphere against the provided BoundingSphere componentwise and returns
  1430. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1431. *
  1432. * @param {BoundingSphere} [right] The right hand side BoundingSphere.
  1433. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1434. */
  1435. BoundingSphere.prototype.equals = function (right) {
  1436. return BoundingSphere.equals(this, right);
  1437. };
  1438. /**
  1439. * Duplicates this BoundingSphere instance.
  1440. *
  1441. * @param {BoundingSphere} [result] The object onto which to store the result.
  1442. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  1443. */
  1444. BoundingSphere.prototype.clone = function (result) {
  1445. return BoundingSphere.clone(this, result);
  1446. };
  1447. /**
  1448. * Computes the radius of the BoundingSphere.
  1449. * @returns {number} The radius of the BoundingSphere.
  1450. */
  1451. BoundingSphere.prototype.volume = function () {
  1452. const radius = this.radius;
  1453. return volumeConstant * radius * radius * radius;
  1454. };
  1455. let _supportsFullscreen;
  1456. const _names = {
  1457. requestFullscreen: undefined,
  1458. exitFullscreen: undefined,
  1459. fullscreenEnabled: undefined,
  1460. fullscreenElement: undefined,
  1461. fullscreenchange: undefined,
  1462. fullscreenerror: undefined,
  1463. };
  1464. /**
  1465. * Browser-independent functions for working with the standard fullscreen API.
  1466. *
  1467. * @namespace Fullscreen
  1468. *
  1469. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  1470. */
  1471. const Fullscreen = {};
  1472. Object.defineProperties(Fullscreen, {
  1473. /**
  1474. * The element that is currently fullscreen, if any. To simply check if the
  1475. * browser is in fullscreen mode or not, use {@link Fullscreen#fullscreen}.
  1476. * @memberof Fullscreen
  1477. * @type {object}
  1478. * @readonly
  1479. */
  1480. element: {
  1481. get: function () {
  1482. if (!Fullscreen.supportsFullscreen()) {
  1483. return undefined;
  1484. }
  1485. return document[_names.fullscreenElement];
  1486. },
  1487. },
  1488. /**
  1489. * The name of the event on the document that is fired when fullscreen is
  1490. * entered or exited. This event name is intended for use with addEventListener.
  1491. * In your event handler, to determine if the browser is in fullscreen mode or not,
  1492. * use {@link Fullscreen#fullscreen}.
  1493. * @memberof Fullscreen
  1494. * @type {string}
  1495. * @readonly
  1496. */
  1497. changeEventName: {
  1498. get: function () {
  1499. if (!Fullscreen.supportsFullscreen()) {
  1500. return undefined;
  1501. }
  1502. return _names.fullscreenchange;
  1503. },
  1504. },
  1505. /**
  1506. * The name of the event that is fired when a fullscreen error
  1507. * occurs. This event name is intended for use with addEventListener.
  1508. * @memberof Fullscreen
  1509. * @type {string}
  1510. * @readonly
  1511. */
  1512. errorEventName: {
  1513. get: function () {
  1514. if (!Fullscreen.supportsFullscreen()) {
  1515. return undefined;
  1516. }
  1517. return _names.fullscreenerror;
  1518. },
  1519. },
  1520. /**
  1521. * Determine whether the browser will allow an element to be made fullscreen, or not.
  1522. * For example, by default, iframes cannot go fullscreen unless the containing page
  1523. * adds an "allowfullscreen" attribute (or prefixed equivalent).
  1524. * @memberof Fullscreen
  1525. * @type {boolean}
  1526. * @readonly
  1527. */
  1528. enabled: {
  1529. get: function () {
  1530. if (!Fullscreen.supportsFullscreen()) {
  1531. return undefined;
  1532. }
  1533. return document[_names.fullscreenEnabled];
  1534. },
  1535. },
  1536. /**
  1537. * Determines if the browser is currently in fullscreen mode.
  1538. * @memberof Fullscreen
  1539. * @type {boolean}
  1540. * @readonly
  1541. */
  1542. fullscreen: {
  1543. get: function () {
  1544. if (!Fullscreen.supportsFullscreen()) {
  1545. return undefined;
  1546. }
  1547. return Fullscreen.element !== null;
  1548. },
  1549. },
  1550. });
  1551. /**
  1552. * Detects whether the browser supports the standard fullscreen API.
  1553. *
  1554. * @returns {boolean} <code>true</code> if the browser supports the standard fullscreen API,
  1555. * <code>false</code> otherwise.
  1556. */
  1557. Fullscreen.supportsFullscreen = function () {
  1558. if (defaultValue.defined(_supportsFullscreen)) {
  1559. return _supportsFullscreen;
  1560. }
  1561. _supportsFullscreen = false;
  1562. const body = document.body;
  1563. if (typeof body.requestFullscreen === "function") {
  1564. // go with the unprefixed, standard set of names
  1565. _names.requestFullscreen = "requestFullscreen";
  1566. _names.exitFullscreen = "exitFullscreen";
  1567. _names.fullscreenEnabled = "fullscreenEnabled";
  1568. _names.fullscreenElement = "fullscreenElement";
  1569. _names.fullscreenchange = "fullscreenchange";
  1570. _names.fullscreenerror = "fullscreenerror";
  1571. _supportsFullscreen = true;
  1572. return _supportsFullscreen;
  1573. }
  1574. //check for the correct combination of prefix plus the various names that browsers use
  1575. const prefixes = ["webkit", "moz", "o", "ms", "khtml"];
  1576. let name;
  1577. for (let i = 0, len = prefixes.length; i < len; ++i) {
  1578. const prefix = prefixes[i];
  1579. // casing of Fullscreen differs across browsers
  1580. name = `${prefix}RequestFullscreen`;
  1581. if (typeof body[name] === "function") {
  1582. _names.requestFullscreen = name;
  1583. _supportsFullscreen = true;
  1584. } else {
  1585. name = `${prefix}RequestFullScreen`;
  1586. if (typeof body[name] === "function") {
  1587. _names.requestFullscreen = name;
  1588. _supportsFullscreen = true;
  1589. }
  1590. }
  1591. // disagreement about whether it's "exit" as per spec, or "cancel"
  1592. name = `${prefix}ExitFullscreen`;
  1593. if (typeof document[name] === "function") {
  1594. _names.exitFullscreen = name;
  1595. } else {
  1596. name = `${prefix}CancelFullScreen`;
  1597. if (typeof document[name] === "function") {
  1598. _names.exitFullscreen = name;
  1599. }
  1600. }
  1601. // casing of Fullscreen differs across browsers
  1602. name = `${prefix}FullscreenEnabled`;
  1603. if (document[name] !== undefined) {
  1604. _names.fullscreenEnabled = name;
  1605. } else {
  1606. name = `${prefix}FullScreenEnabled`;
  1607. if (document[name] !== undefined) {
  1608. _names.fullscreenEnabled = name;
  1609. }
  1610. }
  1611. // casing of Fullscreen differs across browsers
  1612. name = `${prefix}FullscreenElement`;
  1613. if (document[name] !== undefined) {
  1614. _names.fullscreenElement = name;
  1615. } else {
  1616. name = `${prefix}FullScreenElement`;
  1617. if (document[name] !== undefined) {
  1618. _names.fullscreenElement = name;
  1619. }
  1620. }
  1621. // thankfully, event names are all lowercase per spec
  1622. name = `${prefix}fullscreenchange`;
  1623. // event names do not have 'on' in the front, but the property on the document does
  1624. if (document[`on${name}`] !== undefined) {
  1625. //except on IE
  1626. if (prefix === "ms") {
  1627. name = "MSFullscreenChange";
  1628. }
  1629. _names.fullscreenchange = name;
  1630. }
  1631. name = `${prefix}fullscreenerror`;
  1632. if (document[`on${name}`] !== undefined) {
  1633. //except on IE
  1634. if (prefix === "ms") {
  1635. name = "MSFullscreenError";
  1636. }
  1637. _names.fullscreenerror = name;
  1638. }
  1639. }
  1640. return _supportsFullscreen;
  1641. };
  1642. /**
  1643. * Asynchronously requests the browser to enter fullscreen mode on the given element.
  1644. * If fullscreen mode is not supported by the browser, does nothing.
  1645. *
  1646. * @param {object} element The HTML element which will be placed into fullscreen mode.
  1647. * @param {object} [vrDevice] The HMDVRDevice device.
  1648. *
  1649. * @example
  1650. * // Put the entire page into fullscreen.
  1651. * Cesium.Fullscreen.requestFullscreen(document.body)
  1652. *
  1653. * // Place only the Cesium canvas into fullscreen.
  1654. * Cesium.Fullscreen.requestFullscreen(scene.canvas)
  1655. */
  1656. Fullscreen.requestFullscreen = function (element, vrDevice) {
  1657. if (!Fullscreen.supportsFullscreen()) {
  1658. return;
  1659. }
  1660. element[_names.requestFullscreen]({ vrDisplay: vrDevice });
  1661. };
  1662. /**
  1663. * Asynchronously exits fullscreen mode. If the browser is not currently
  1664. * in fullscreen, or if fullscreen mode is not supported by the browser, does nothing.
  1665. */
  1666. Fullscreen.exitFullscreen = function () {
  1667. if (!Fullscreen.supportsFullscreen()) {
  1668. return;
  1669. }
  1670. document[_names.exitFullscreen]();
  1671. };
  1672. //For unit tests
  1673. Fullscreen._names = _names;
  1674. var Fullscreen$1 = Fullscreen;
  1675. let theNavigator;
  1676. if (typeof navigator !== "undefined") {
  1677. theNavigator = navigator;
  1678. } else {
  1679. theNavigator = {};
  1680. }
  1681. function extractVersion(versionString) {
  1682. const parts = versionString.split(".");
  1683. for (let i = 0, len = parts.length; i < len; ++i) {
  1684. parts[i] = parseInt(parts[i], 10);
  1685. }
  1686. return parts;
  1687. }
  1688. let isChromeResult;
  1689. let chromeVersionResult;
  1690. function isChrome() {
  1691. if (!defaultValue.defined(isChromeResult)) {
  1692. isChromeResult = false;
  1693. // Edge contains Chrome in the user agent too
  1694. if (!isEdge()) {
  1695. const fields = / Chrome\/([\.0-9]+)/.exec(theNavigator.userAgent);
  1696. if (fields !== null) {
  1697. isChromeResult = true;
  1698. chromeVersionResult = extractVersion(fields[1]);
  1699. }
  1700. }
  1701. }
  1702. return isChromeResult;
  1703. }
  1704. function chromeVersion() {
  1705. return isChrome() && chromeVersionResult;
  1706. }
  1707. let isSafariResult;
  1708. let safariVersionResult;
  1709. function isSafari() {
  1710. if (!defaultValue.defined(isSafariResult)) {
  1711. isSafariResult = false;
  1712. // Chrome and Edge contain Safari in the user agent too
  1713. if (
  1714. !isChrome() &&
  1715. !isEdge() &&
  1716. / Safari\/[\.0-9]+/.test(theNavigator.userAgent)
  1717. ) {
  1718. const fields = / Version\/([\.0-9]+)/.exec(theNavigator.userAgent);
  1719. if (fields !== null) {
  1720. isSafariResult = true;
  1721. safariVersionResult = extractVersion(fields[1]);
  1722. }
  1723. }
  1724. }
  1725. return isSafariResult;
  1726. }
  1727. function safariVersion() {
  1728. return isSafari() && safariVersionResult;
  1729. }
  1730. let isWebkitResult;
  1731. let webkitVersionResult;
  1732. function isWebkit() {
  1733. if (!defaultValue.defined(isWebkitResult)) {
  1734. isWebkitResult = false;
  1735. const fields = / AppleWebKit\/([\.0-9]+)(\+?)/.exec(theNavigator.userAgent);
  1736. if (fields !== null) {
  1737. isWebkitResult = true;
  1738. webkitVersionResult = extractVersion(fields[1]);
  1739. webkitVersionResult.isNightly = !!fields[2];
  1740. }
  1741. }
  1742. return isWebkitResult;
  1743. }
  1744. function webkitVersion() {
  1745. return isWebkit() && webkitVersionResult;
  1746. }
  1747. let isInternetExplorerResult;
  1748. let internetExplorerVersionResult;
  1749. function isInternetExplorer() {
  1750. if (!defaultValue.defined(isInternetExplorerResult)) {
  1751. isInternetExplorerResult = false;
  1752. let fields;
  1753. if (theNavigator.appName === "Microsoft Internet Explorer") {
  1754. fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  1755. if (fields !== null) {
  1756. isInternetExplorerResult = true;
  1757. internetExplorerVersionResult = extractVersion(fields[1]);
  1758. }
  1759. } else if (theNavigator.appName === "Netscape") {
  1760. fields = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(
  1761. theNavigator.userAgent
  1762. );
  1763. if (fields !== null) {
  1764. isInternetExplorerResult = true;
  1765. internetExplorerVersionResult = extractVersion(fields[1]);
  1766. }
  1767. }
  1768. }
  1769. return isInternetExplorerResult;
  1770. }
  1771. function internetExplorerVersion() {
  1772. return isInternetExplorer() && internetExplorerVersionResult;
  1773. }
  1774. let isEdgeResult;
  1775. let edgeVersionResult;
  1776. function isEdge() {
  1777. if (!defaultValue.defined(isEdgeResult)) {
  1778. isEdgeResult = false;
  1779. const fields = / Edg\/([\.0-9]+)/.exec(theNavigator.userAgent);
  1780. if (fields !== null) {
  1781. isEdgeResult = true;
  1782. edgeVersionResult = extractVersion(fields[1]);
  1783. }
  1784. }
  1785. return isEdgeResult;
  1786. }
  1787. function edgeVersion() {
  1788. return isEdge() && edgeVersionResult;
  1789. }
  1790. let isFirefoxResult;
  1791. let firefoxVersionResult;
  1792. function isFirefox() {
  1793. if (!defaultValue.defined(isFirefoxResult)) {
  1794. isFirefoxResult = false;
  1795. const fields = /Firefox\/([\.0-9]+)/.exec(theNavigator.userAgent);
  1796. if (fields !== null) {
  1797. isFirefoxResult = true;
  1798. firefoxVersionResult = extractVersion(fields[1]);
  1799. }
  1800. }
  1801. return isFirefoxResult;
  1802. }
  1803. let isWindowsResult;
  1804. function isWindows() {
  1805. if (!defaultValue.defined(isWindowsResult)) {
  1806. isWindowsResult = /Windows/i.test(theNavigator.appVersion);
  1807. }
  1808. return isWindowsResult;
  1809. }
  1810. let isIPadOrIOSResult;
  1811. function isIPadOrIOS() {
  1812. if (!defaultValue.defined(isIPadOrIOSResult)) {
  1813. isIPadOrIOSResult =
  1814. navigator.platform === "iPhone" ||
  1815. navigator.platform === "iPod" ||
  1816. navigator.platform === "iPad";
  1817. }
  1818. return isIPadOrIOSResult;
  1819. }
  1820. function firefoxVersion() {
  1821. return isFirefox() && firefoxVersionResult;
  1822. }
  1823. let hasPointerEvents;
  1824. function supportsPointerEvents() {
  1825. if (!defaultValue.defined(hasPointerEvents)) {
  1826. //While navigator.pointerEnabled is deprecated in the W3C specification
  1827. //we still need to use it if it exists in order to support browsers
  1828. //that rely on it, such as the Windows WebBrowser control which defines
  1829. //PointerEvent but sets navigator.pointerEnabled to false.
  1830. //Firefox disabled because of https://github.com/CesiumGS/cesium/issues/6372
  1831. hasPointerEvents =
  1832. !isFirefox() &&
  1833. typeof PointerEvent !== "undefined" &&
  1834. (!defaultValue.defined(theNavigator.pointerEnabled) || theNavigator.pointerEnabled);
  1835. }
  1836. return hasPointerEvents;
  1837. }
  1838. let imageRenderingValueResult;
  1839. let supportsImageRenderingPixelatedResult;
  1840. function supportsImageRenderingPixelated() {
  1841. if (!defaultValue.defined(supportsImageRenderingPixelatedResult)) {
  1842. const canvas = document.createElement("canvas");
  1843. canvas.setAttribute(
  1844. "style",
  1845. "image-rendering: -moz-crisp-edges;" + "image-rendering: pixelated;"
  1846. );
  1847. //canvas.style.imageRendering will be undefined, null or an empty string on unsupported browsers.
  1848. const tmp = canvas.style.imageRendering;
  1849. supportsImageRenderingPixelatedResult = defaultValue.defined(tmp) && tmp !== "";
  1850. if (supportsImageRenderingPixelatedResult) {
  1851. imageRenderingValueResult = tmp;
  1852. }
  1853. }
  1854. return supportsImageRenderingPixelatedResult;
  1855. }
  1856. function imageRenderingValue() {
  1857. return supportsImageRenderingPixelated()
  1858. ? imageRenderingValueResult
  1859. : undefined;
  1860. }
  1861. function supportsWebP() {
  1862. //>>includeStart('debug', pragmas.debug);
  1863. if (!supportsWebP.initialized) {
  1864. throw new Check.DeveloperError(
  1865. "You must call FeatureDetection.supportsWebP.initialize and wait for the promise to resolve before calling FeatureDetection.supportsWebP"
  1866. );
  1867. }
  1868. //>>includeEnd('debug');
  1869. return supportsWebP._result;
  1870. }
  1871. supportsWebP._promise = undefined;
  1872. supportsWebP._result = undefined;
  1873. supportsWebP.initialize = function () {
  1874. // From https://developers.google.com/speed/webp/faq#how_can_i_detect_browser_support_for_webp
  1875. if (defaultValue.defined(supportsWebP._promise)) {
  1876. return supportsWebP._promise;
  1877. }
  1878. supportsWebP._promise = new Promise((resolve) => {
  1879. const image = new Image();
  1880. image.onload = function () {
  1881. supportsWebP._result = image.width > 0 && image.height > 0;
  1882. resolve(supportsWebP._result);
  1883. };
  1884. image.onerror = function () {
  1885. supportsWebP._result = false;
  1886. resolve(supportsWebP._result);
  1887. };
  1888. image.src =
  1889. "data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA";
  1890. });
  1891. return supportsWebP._promise;
  1892. };
  1893. Object.defineProperties(supportsWebP, {
  1894. initialized: {
  1895. get: function () {
  1896. return defaultValue.defined(supportsWebP._result);
  1897. },
  1898. },
  1899. });
  1900. const typedArrayTypes = [];
  1901. if (typeof ArrayBuffer !== "undefined") {
  1902. typedArrayTypes.push(
  1903. Int8Array,
  1904. Uint8Array,
  1905. Int16Array,
  1906. Uint16Array,
  1907. Int32Array,
  1908. Uint32Array,
  1909. Float32Array,
  1910. Float64Array
  1911. );
  1912. if (typeof Uint8ClampedArray !== "undefined") {
  1913. typedArrayTypes.push(Uint8ClampedArray);
  1914. }
  1915. if (typeof Uint8ClampedArray !== "undefined") {
  1916. typedArrayTypes.push(Uint8ClampedArray);
  1917. }
  1918. if (typeof BigInt64Array !== "undefined") {
  1919. // eslint-disable-next-line no-undef
  1920. typedArrayTypes.push(BigInt64Array);
  1921. }
  1922. if (typeof BigUint64Array !== "undefined") {
  1923. // eslint-disable-next-line no-undef
  1924. typedArrayTypes.push(BigUint64Array);
  1925. }
  1926. }
  1927. /**
  1928. * A set of functions to detect whether the current browser supports
  1929. * various features.
  1930. *
  1931. * @namespace FeatureDetection
  1932. */
  1933. const FeatureDetection = {
  1934. isChrome: isChrome,
  1935. chromeVersion: chromeVersion,
  1936. isSafari: isSafari,
  1937. safariVersion: safariVersion,
  1938. isWebkit: isWebkit,
  1939. webkitVersion: webkitVersion,
  1940. isInternetExplorer: isInternetExplorer,
  1941. internetExplorerVersion: internetExplorerVersion,
  1942. isEdge: isEdge,
  1943. edgeVersion: edgeVersion,
  1944. isFirefox: isFirefox,
  1945. firefoxVersion: firefoxVersion,
  1946. isWindows: isWindows,
  1947. isIPadOrIOS: isIPadOrIOS,
  1948. hardwareConcurrency: defaultValue.defaultValue(theNavigator.hardwareConcurrency, 3),
  1949. supportsPointerEvents: supportsPointerEvents,
  1950. supportsImageRenderingPixelated: supportsImageRenderingPixelated,
  1951. supportsWebP: supportsWebP,
  1952. imageRenderingValue: imageRenderingValue,
  1953. typedArrayTypes: typedArrayTypes,
  1954. };
  1955. /**
  1956. * Detects whether the current browser supports Basis Universal textures and the web assembly modules needed to transcode them.
  1957. *
  1958. * @param {Scene} scene
  1959. * @returns {boolean} true if the browser supports web assembly modules and the scene supports Basis Universal textures, false if not.
  1960. */
  1961. FeatureDetection.supportsBasis = function (scene) {
  1962. return FeatureDetection.supportsWebAssembly() && scene.context.supportsBasis;
  1963. };
  1964. /**
  1965. * Detects whether the current browser supports the full screen standard.
  1966. *
  1967. * @returns {boolean} true if the browser supports the full screen standard, false if not.
  1968. *
  1969. * @see Fullscreen
  1970. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  1971. */
  1972. FeatureDetection.supportsFullscreen = function () {
  1973. return Fullscreen$1.supportsFullscreen();
  1974. };
  1975. /**
  1976. * Detects whether the current browser supports typed arrays.
  1977. *
  1978. * @returns {boolean} true if the browser supports typed arrays, false if not.
  1979. *
  1980. * @see {@link https://tc39.es/ecma262/#sec-typedarray-objects|Typed Array Specification}
  1981. */
  1982. FeatureDetection.supportsTypedArrays = function () {
  1983. return typeof ArrayBuffer !== "undefined";
  1984. };
  1985. /**
  1986. * Detects whether the current browser supports BigInt64Array typed arrays.
  1987. *
  1988. * @returns {boolean} true if the browser supports BigInt64Array typed arrays, false if not.
  1989. *
  1990. * @see {@link https://tc39.es/ecma262/#sec-typedarray-objects|Typed Array Specification}
  1991. */
  1992. FeatureDetection.supportsBigInt64Array = function () {
  1993. return typeof BigInt64Array !== "undefined";
  1994. };
  1995. /**
  1996. * Detects whether the current browser supports BigUint64Array typed arrays.
  1997. *
  1998. * @returns {boolean} true if the browser supports BigUint64Array typed arrays, false if not.
  1999. *
  2000. * @see {@link https://tc39.es/ecma262/#sec-typedarray-objects|Typed Array Specification}
  2001. */
  2002. FeatureDetection.supportsBigUint64Array = function () {
  2003. return typeof BigUint64Array !== "undefined";
  2004. };
  2005. /**
  2006. * Detects whether the current browser supports BigInt.
  2007. *
  2008. * @returns {boolean} true if the browser supports BigInt, false if not.
  2009. *
  2010. * @see {@link https://tc39.es/ecma262/#sec-bigint-objects|BigInt Specification}
  2011. */
  2012. FeatureDetection.supportsBigInt = function () {
  2013. return typeof BigInt !== "undefined";
  2014. };
  2015. /**
  2016. * Detects whether the current browser supports Web Workers.
  2017. *
  2018. * @returns {boolean} true if the browsers supports Web Workers, false if not.
  2019. *
  2020. * @see {@link http://www.w3.org/TR/workers/}
  2021. */
  2022. FeatureDetection.supportsWebWorkers = function () {
  2023. return typeof Worker !== "undefined";
  2024. };
  2025. /**
  2026. * Detects whether the current browser supports Web Assembly.
  2027. *
  2028. * @returns {boolean} true if the browsers supports Web Assembly, false if not.
  2029. *
  2030. * @see {@link https://developer.mozilla.org/en-US/docs/WebAssembly}
  2031. */
  2032. FeatureDetection.supportsWebAssembly = function () {
  2033. return typeof WebAssembly !== "undefined";
  2034. };
  2035. /**
  2036. * Detects whether the current browser supports a WebGL2 rendering context for the specified scene.
  2037. *
  2038. * @param {Scene} scene the Cesium scene specifying the rendering context
  2039. * @returns {boolean} true if the browser supports a WebGL2 rendering context, false if not.
  2040. *
  2041. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext|WebGL2RenderingContext}
  2042. */
  2043. FeatureDetection.supportsWebgl2 = function (scene) {
  2044. //>>includeStart('debug', pragmas.debug);
  2045. Check.Check.defined("scene", scene);
  2046. //>>includeEnd('debug');
  2047. return scene.context.webgl2;
  2048. };
  2049. var FeatureDetection$1 = FeatureDetection;
  2050. /**
  2051. * A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
  2052. * @alias Quaternion
  2053. * @constructor
  2054. *
  2055. * @param {number} [x=0.0] The X component.
  2056. * @param {number} [y=0.0] The Y component.
  2057. * @param {number} [z=0.0] The Z component.
  2058. * @param {number} [w=0.0] The W component.
  2059. *
  2060. * @see PackableForInterpolation
  2061. */
  2062. function Quaternion(x, y, z, w) {
  2063. /**
  2064. * The X component.
  2065. * @type {number}
  2066. * @default 0.0
  2067. */
  2068. this.x = defaultValue.defaultValue(x, 0.0);
  2069. /**
  2070. * The Y component.
  2071. * @type {number}
  2072. * @default 0.0
  2073. */
  2074. this.y = defaultValue.defaultValue(y, 0.0);
  2075. /**
  2076. * The Z component.
  2077. * @type {number}
  2078. * @default 0.0
  2079. */
  2080. this.z = defaultValue.defaultValue(z, 0.0);
  2081. /**
  2082. * The W component.
  2083. * @type {number}
  2084. * @default 0.0
  2085. */
  2086. this.w = defaultValue.defaultValue(w, 0.0);
  2087. }
  2088. let fromAxisAngleScratch = new Matrix3.Cartesian3();
  2089. /**
  2090. * Computes a quaternion representing a rotation around an axis.
  2091. *
  2092. * @param {Cartesian3} axis The axis of rotation.
  2093. * @param {number} angle The angle in radians to rotate around the axis.
  2094. * @param {Quaternion} [result] The object onto which to store the result.
  2095. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  2096. */
  2097. Quaternion.fromAxisAngle = function (axis, angle, result) {
  2098. //>>includeStart('debug', pragmas.debug);
  2099. Check.Check.typeOf.object("axis", axis);
  2100. Check.Check.typeOf.number("angle", angle);
  2101. //>>includeEnd('debug');
  2102. const halfAngle = angle / 2.0;
  2103. const s = Math.sin(halfAngle);
  2104. fromAxisAngleScratch = Matrix3.Cartesian3.normalize(axis, fromAxisAngleScratch);
  2105. const x = fromAxisAngleScratch.x * s;
  2106. const y = fromAxisAngleScratch.y * s;
  2107. const z = fromAxisAngleScratch.z * s;
  2108. const w = Math.cos(halfAngle);
  2109. if (!defaultValue.defined(result)) {
  2110. return new Quaternion(x, y, z, w);
  2111. }
  2112. result.x = x;
  2113. result.y = y;
  2114. result.z = z;
  2115. result.w = w;
  2116. return result;
  2117. };
  2118. const fromRotationMatrixNext = [1, 2, 0];
  2119. const fromRotationMatrixQuat = new Array(3);
  2120. /**
  2121. * Computes a Quaternion from the provided Matrix3 instance.
  2122. *
  2123. * @param {Matrix3} matrix The rotation matrix.
  2124. * @param {Quaternion} [result] The object onto which to store the result.
  2125. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  2126. *
  2127. * @see Matrix3.fromQuaternion
  2128. */
  2129. Quaternion.fromRotationMatrix = function (matrix, result) {
  2130. //>>includeStart('debug', pragmas.debug);
  2131. Check.Check.typeOf.object("matrix", matrix);
  2132. //>>includeEnd('debug');
  2133. let root;
  2134. let x;
  2135. let y;
  2136. let z;
  2137. let w;
  2138. const m00 = matrix[Matrix3.Matrix3.COLUMN0ROW0];
  2139. const m11 = matrix[Matrix3.Matrix3.COLUMN1ROW1];
  2140. const m22 = matrix[Matrix3.Matrix3.COLUMN2ROW2];
  2141. const trace = m00 + m11 + m22;
  2142. if (trace > 0.0) {
  2143. // |w| > 1/2, may as well choose w > 1/2
  2144. root = Math.sqrt(trace + 1.0); // 2w
  2145. w = 0.5 * root;
  2146. root = 0.5 / root; // 1/(4w)
  2147. x = (matrix[Matrix3.Matrix3.COLUMN1ROW2] - matrix[Matrix3.Matrix3.COLUMN2ROW1]) * root;
  2148. y = (matrix[Matrix3.Matrix3.COLUMN2ROW0] - matrix[Matrix3.Matrix3.COLUMN0ROW2]) * root;
  2149. z = (matrix[Matrix3.Matrix3.COLUMN0ROW1] - matrix[Matrix3.Matrix3.COLUMN1ROW0]) * root;
  2150. } else {
  2151. // |w| <= 1/2
  2152. const next = fromRotationMatrixNext;
  2153. let i = 0;
  2154. if (m11 > m00) {
  2155. i = 1;
  2156. }
  2157. if (m22 > m00 && m22 > m11) {
  2158. i = 2;
  2159. }
  2160. const j = next[i];
  2161. const k = next[j];
  2162. root = Math.sqrt(
  2163. matrix[Matrix3.Matrix3.getElementIndex(i, i)] -
  2164. matrix[Matrix3.Matrix3.getElementIndex(j, j)] -
  2165. matrix[Matrix3.Matrix3.getElementIndex(k, k)] +
  2166. 1.0
  2167. );
  2168. const quat = fromRotationMatrixQuat;
  2169. quat[i] = 0.5 * root;
  2170. root = 0.5 / root;
  2171. w =
  2172. (matrix[Matrix3.Matrix3.getElementIndex(k, j)] -
  2173. matrix[Matrix3.Matrix3.getElementIndex(j, k)]) *
  2174. root;
  2175. quat[j] =
  2176. (matrix[Matrix3.Matrix3.getElementIndex(j, i)] +
  2177. matrix[Matrix3.Matrix3.getElementIndex(i, j)]) *
  2178. root;
  2179. quat[k] =
  2180. (matrix[Matrix3.Matrix3.getElementIndex(k, i)] +
  2181. matrix[Matrix3.Matrix3.getElementIndex(i, k)]) *
  2182. root;
  2183. x = -quat[0];
  2184. y = -quat[1];
  2185. z = -quat[2];
  2186. }
  2187. if (!defaultValue.defined(result)) {
  2188. return new Quaternion(x, y, z, w);
  2189. }
  2190. result.x = x;
  2191. result.y = y;
  2192. result.z = z;
  2193. result.w = w;
  2194. return result;
  2195. };
  2196. const scratchHPRQuaternion$1 = new Quaternion();
  2197. let scratchHeadingQuaternion = new Quaternion();
  2198. let scratchPitchQuaternion = new Quaternion();
  2199. let scratchRollQuaternion = new Quaternion();
  2200. /**
  2201. * Computes a rotation from the given heading, pitch and roll angles. Heading is the rotation about the
  2202. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  2203. * the positive x axis.
  2204. *
  2205. * @param {HeadingPitchRoll} headingPitchRoll The rotation expressed as a heading, pitch and roll.
  2206. * @param {Quaternion} [result] The object onto which to store the result.
  2207. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  2208. */
  2209. Quaternion.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  2210. //>>includeStart('debug', pragmas.debug);
  2211. Check.Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  2212. //>>includeEnd('debug');
  2213. scratchRollQuaternion = Quaternion.fromAxisAngle(
  2214. Matrix3.Cartesian3.UNIT_X,
  2215. headingPitchRoll.roll,
  2216. scratchHPRQuaternion$1
  2217. );
  2218. scratchPitchQuaternion = Quaternion.fromAxisAngle(
  2219. Matrix3.Cartesian3.UNIT_Y,
  2220. -headingPitchRoll.pitch,
  2221. result
  2222. );
  2223. result = Quaternion.multiply(
  2224. scratchPitchQuaternion,
  2225. scratchRollQuaternion,
  2226. scratchPitchQuaternion
  2227. );
  2228. scratchHeadingQuaternion = Quaternion.fromAxisAngle(
  2229. Matrix3.Cartesian3.UNIT_Z,
  2230. -headingPitchRoll.heading,
  2231. scratchHPRQuaternion$1
  2232. );
  2233. return Quaternion.multiply(scratchHeadingQuaternion, result, result);
  2234. };
  2235. const sampledQuaternionAxis = new Matrix3.Cartesian3();
  2236. const sampledQuaternionRotation = new Matrix3.Cartesian3();
  2237. const sampledQuaternionTempQuaternion = new Quaternion();
  2238. const sampledQuaternionQuaternion0 = new Quaternion();
  2239. const sampledQuaternionQuaternion0Conjugate = new Quaternion();
  2240. /**
  2241. * The number of elements used to pack the object into an array.
  2242. * @type {number}
  2243. */
  2244. Quaternion.packedLength = 4;
  2245. /**
  2246. * Stores the provided instance into the provided array.
  2247. *
  2248. * @param {Quaternion} value The value to pack.
  2249. * @param {number[]} array The array to pack into.
  2250. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  2251. *
  2252. * @returns {number[]} The array that was packed into
  2253. */
  2254. Quaternion.pack = function (value, array, startingIndex) {
  2255. //>>includeStart('debug', pragmas.debug);
  2256. Check.Check.typeOf.object("value", value);
  2257. Check.Check.defined("array", array);
  2258. //>>includeEnd('debug');
  2259. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2260. array[startingIndex++] = value.x;
  2261. array[startingIndex++] = value.y;
  2262. array[startingIndex++] = value.z;
  2263. array[startingIndex] = value.w;
  2264. return array;
  2265. };
  2266. /**
  2267. * Retrieves an instance from a packed array.
  2268. *
  2269. * @param {number[]} array The packed array.
  2270. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  2271. * @param {Quaternion} [result] The object into which to store the result.
  2272. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  2273. */
  2274. Quaternion.unpack = function (array, startingIndex, result) {
  2275. //>>includeStart('debug', pragmas.debug);
  2276. Check.Check.defined("array", array);
  2277. //>>includeEnd('debug');
  2278. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2279. if (!defaultValue.defined(result)) {
  2280. result = new Quaternion();
  2281. }
  2282. result.x = array[startingIndex];
  2283. result.y = array[startingIndex + 1];
  2284. result.z = array[startingIndex + 2];
  2285. result.w = array[startingIndex + 3];
  2286. return result;
  2287. };
  2288. /**
  2289. * The number of elements used to store the object into an array in its interpolatable form.
  2290. * @type {number}
  2291. */
  2292. Quaternion.packedInterpolationLength = 3;
  2293. /**
  2294. * Converts a packed array into a form suitable for interpolation.
  2295. *
  2296. * @param {number[]} packedArray The packed array.
  2297. * @param {number} [startingIndex=0] The index of the first element to be converted.
  2298. * @param {number} [lastIndex=packedArray.length] The index of the last element to be converted.
  2299. * @param {number[]} [result] The object into which to store the result.
  2300. */
  2301. Quaternion.convertPackedArrayForInterpolation = function (
  2302. packedArray,
  2303. startingIndex,
  2304. lastIndex,
  2305. result
  2306. ) {
  2307. Quaternion.unpack(
  2308. packedArray,
  2309. lastIndex * 4,
  2310. sampledQuaternionQuaternion0Conjugate
  2311. );
  2312. Quaternion.conjugate(
  2313. sampledQuaternionQuaternion0Conjugate,
  2314. sampledQuaternionQuaternion0Conjugate
  2315. );
  2316. for (let i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
  2317. const offset = i * 3;
  2318. Quaternion.unpack(
  2319. packedArray,
  2320. (startingIndex + i) * 4,
  2321. sampledQuaternionTempQuaternion
  2322. );
  2323. Quaternion.multiply(
  2324. sampledQuaternionTempQuaternion,
  2325. sampledQuaternionQuaternion0Conjugate,
  2326. sampledQuaternionTempQuaternion
  2327. );
  2328. if (sampledQuaternionTempQuaternion.w < 0) {
  2329. Quaternion.negate(
  2330. sampledQuaternionTempQuaternion,
  2331. sampledQuaternionTempQuaternion
  2332. );
  2333. }
  2334. Quaternion.computeAxis(
  2335. sampledQuaternionTempQuaternion,
  2336. sampledQuaternionAxis
  2337. );
  2338. const angle = Quaternion.computeAngle(sampledQuaternionTempQuaternion);
  2339. if (!defaultValue.defined(result)) {
  2340. result = [];
  2341. }
  2342. result[offset] = sampledQuaternionAxis.x * angle;
  2343. result[offset + 1] = sampledQuaternionAxis.y * angle;
  2344. result[offset + 2] = sampledQuaternionAxis.z * angle;
  2345. }
  2346. };
  2347. /**
  2348. * Retrieves an instance from a packed array converted with {@link convertPackedArrayForInterpolation}.
  2349. *
  2350. * @param {number[]} array The array previously packed for interpolation.
  2351. * @param {number[]} sourceArray The original packed array.
  2352. * @param {number} [firstIndex=0] The firstIndex used to convert the array.
  2353. * @param {number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
  2354. * @param {Quaternion} [result] The object into which to store the result.
  2355. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  2356. */
  2357. Quaternion.unpackInterpolationResult = function (
  2358. array,
  2359. sourceArray,
  2360. firstIndex,
  2361. lastIndex,
  2362. result
  2363. ) {
  2364. if (!defaultValue.defined(result)) {
  2365. result = new Quaternion();
  2366. }
  2367. Matrix3.Cartesian3.fromArray(array, 0, sampledQuaternionRotation);
  2368. const magnitude = Matrix3.Cartesian3.magnitude(sampledQuaternionRotation);
  2369. Quaternion.unpack(sourceArray, lastIndex * 4, sampledQuaternionQuaternion0);
  2370. if (magnitude === 0) {
  2371. Quaternion.clone(Quaternion.IDENTITY, sampledQuaternionTempQuaternion);
  2372. } else {
  2373. Quaternion.fromAxisAngle(
  2374. sampledQuaternionRotation,
  2375. magnitude,
  2376. sampledQuaternionTempQuaternion
  2377. );
  2378. }
  2379. return Quaternion.multiply(
  2380. sampledQuaternionTempQuaternion,
  2381. sampledQuaternionQuaternion0,
  2382. result
  2383. );
  2384. };
  2385. /**
  2386. * Duplicates a Quaternion instance.
  2387. *
  2388. * @param {Quaternion} quaternion The quaternion to duplicate.
  2389. * @param {Quaternion} [result] The object onto which to store the result.
  2390. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided. (Returns undefined if quaternion is undefined)
  2391. */
  2392. Quaternion.clone = function (quaternion, result) {
  2393. if (!defaultValue.defined(quaternion)) {
  2394. return undefined;
  2395. }
  2396. if (!defaultValue.defined(result)) {
  2397. return new Quaternion(
  2398. quaternion.x,
  2399. quaternion.y,
  2400. quaternion.z,
  2401. quaternion.w
  2402. );
  2403. }
  2404. result.x = quaternion.x;
  2405. result.y = quaternion.y;
  2406. result.z = quaternion.z;
  2407. result.w = quaternion.w;
  2408. return result;
  2409. };
  2410. /**
  2411. * Computes the conjugate of the provided quaternion.
  2412. *
  2413. * @param {Quaternion} quaternion The quaternion to conjugate.
  2414. * @param {Quaternion} result The object onto which to store the result.
  2415. * @returns {Quaternion} The modified result parameter.
  2416. */
  2417. Quaternion.conjugate = function (quaternion, result) {
  2418. //>>includeStart('debug', pragmas.debug);
  2419. Check.Check.typeOf.object("quaternion", quaternion);
  2420. Check.Check.typeOf.object("result", result);
  2421. //>>includeEnd('debug');
  2422. result.x = -quaternion.x;
  2423. result.y = -quaternion.y;
  2424. result.z = -quaternion.z;
  2425. result.w = quaternion.w;
  2426. return result;
  2427. };
  2428. /**
  2429. * Computes magnitude squared for the provided quaternion.
  2430. *
  2431. * @param {Quaternion} quaternion The quaternion to conjugate.
  2432. * @returns {number} The magnitude squared.
  2433. */
  2434. Quaternion.magnitudeSquared = function (quaternion) {
  2435. //>>includeStart('debug', pragmas.debug);
  2436. Check.Check.typeOf.object("quaternion", quaternion);
  2437. //>>includeEnd('debug');
  2438. return (
  2439. quaternion.x * quaternion.x +
  2440. quaternion.y * quaternion.y +
  2441. quaternion.z * quaternion.z +
  2442. quaternion.w * quaternion.w
  2443. );
  2444. };
  2445. /**
  2446. * Computes magnitude for the provided quaternion.
  2447. *
  2448. * @param {Quaternion} quaternion The quaternion to conjugate.
  2449. * @returns {number} The magnitude.
  2450. */
  2451. Quaternion.magnitude = function (quaternion) {
  2452. return Math.sqrt(Quaternion.magnitudeSquared(quaternion));
  2453. };
  2454. /**
  2455. * Computes the normalized form of the provided quaternion.
  2456. *
  2457. * @param {Quaternion} quaternion The quaternion to normalize.
  2458. * @param {Quaternion} result The object onto which to store the result.
  2459. * @returns {Quaternion} The modified result parameter.
  2460. */
  2461. Quaternion.normalize = function (quaternion, result) {
  2462. //>>includeStart('debug', pragmas.debug);
  2463. Check.Check.typeOf.object("result", result);
  2464. //>>includeEnd('debug');
  2465. const inverseMagnitude = 1.0 / Quaternion.magnitude(quaternion);
  2466. const x = quaternion.x * inverseMagnitude;
  2467. const y = quaternion.y * inverseMagnitude;
  2468. const z = quaternion.z * inverseMagnitude;
  2469. const w = quaternion.w * inverseMagnitude;
  2470. result.x = x;
  2471. result.y = y;
  2472. result.z = z;
  2473. result.w = w;
  2474. return result;
  2475. };
  2476. /**
  2477. * Computes the inverse of the provided quaternion.
  2478. *
  2479. * @param {Quaternion} quaternion The quaternion to normalize.
  2480. * @param {Quaternion} result The object onto which to store the result.
  2481. * @returns {Quaternion} The modified result parameter.
  2482. */
  2483. Quaternion.inverse = function (quaternion, result) {
  2484. //>>includeStart('debug', pragmas.debug);
  2485. Check.Check.typeOf.object("result", result);
  2486. //>>includeEnd('debug');
  2487. const magnitudeSquared = Quaternion.magnitudeSquared(quaternion);
  2488. result = Quaternion.conjugate(quaternion, result);
  2489. return Quaternion.multiplyByScalar(result, 1.0 / magnitudeSquared, result);
  2490. };
  2491. /**
  2492. * Computes the componentwise sum of two quaternions.
  2493. *
  2494. * @param {Quaternion} left The first quaternion.
  2495. * @param {Quaternion} right The second quaternion.
  2496. * @param {Quaternion} result The object onto which to store the result.
  2497. * @returns {Quaternion} The modified result parameter.
  2498. */
  2499. Quaternion.add = function (left, right, result) {
  2500. //>>includeStart('debug', pragmas.debug);
  2501. Check.Check.typeOf.object("left", left);
  2502. Check.Check.typeOf.object("right", right);
  2503. Check.Check.typeOf.object("result", result);
  2504. //>>includeEnd('debug');
  2505. result.x = left.x + right.x;
  2506. result.y = left.y + right.y;
  2507. result.z = left.z + right.z;
  2508. result.w = left.w + right.w;
  2509. return result;
  2510. };
  2511. /**
  2512. * Computes the componentwise difference of two quaternions.
  2513. *
  2514. * @param {Quaternion} left The first quaternion.
  2515. * @param {Quaternion} right The second quaternion.
  2516. * @param {Quaternion} result The object onto which to store the result.
  2517. * @returns {Quaternion} The modified result parameter.
  2518. */
  2519. Quaternion.subtract = function (left, right, result) {
  2520. //>>includeStart('debug', pragmas.debug);
  2521. Check.Check.typeOf.object("left", left);
  2522. Check.Check.typeOf.object("right", right);
  2523. Check.Check.typeOf.object("result", result);
  2524. //>>includeEnd('debug');
  2525. result.x = left.x - right.x;
  2526. result.y = left.y - right.y;
  2527. result.z = left.z - right.z;
  2528. result.w = left.w - right.w;
  2529. return result;
  2530. };
  2531. /**
  2532. * Negates the provided quaternion.
  2533. *
  2534. * @param {Quaternion} quaternion The quaternion to be negated.
  2535. * @param {Quaternion} result The object onto which to store the result.
  2536. * @returns {Quaternion} The modified result parameter.
  2537. */
  2538. Quaternion.negate = function (quaternion, result) {
  2539. //>>includeStart('debug', pragmas.debug);
  2540. Check.Check.typeOf.object("quaternion", quaternion);
  2541. Check.Check.typeOf.object("result", result);
  2542. //>>includeEnd('debug');
  2543. result.x = -quaternion.x;
  2544. result.y = -quaternion.y;
  2545. result.z = -quaternion.z;
  2546. result.w = -quaternion.w;
  2547. return result;
  2548. };
  2549. /**
  2550. * Computes the dot (scalar) product of two quaternions.
  2551. *
  2552. * @param {Quaternion} left The first quaternion.
  2553. * @param {Quaternion} right The second quaternion.
  2554. * @returns {number} The dot product.
  2555. */
  2556. Quaternion.dot = function (left, right) {
  2557. //>>includeStart('debug', pragmas.debug);
  2558. Check.Check.typeOf.object("left", left);
  2559. Check.Check.typeOf.object("right", right);
  2560. //>>includeEnd('debug');
  2561. return (
  2562. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  2563. );
  2564. };
  2565. /**
  2566. * Computes the product of two quaternions.
  2567. *
  2568. * @param {Quaternion} left The first quaternion.
  2569. * @param {Quaternion} right The second quaternion.
  2570. * @param {Quaternion} result The object onto which to store the result.
  2571. * @returns {Quaternion} The modified result parameter.
  2572. */
  2573. Quaternion.multiply = function (left, right, result) {
  2574. //>>includeStart('debug', pragmas.debug);
  2575. Check.Check.typeOf.object("left", left);
  2576. Check.Check.typeOf.object("right", right);
  2577. Check.Check.typeOf.object("result", result);
  2578. //>>includeEnd('debug');
  2579. const leftX = left.x;
  2580. const leftY = left.y;
  2581. const leftZ = left.z;
  2582. const leftW = left.w;
  2583. const rightX = right.x;
  2584. const rightY = right.y;
  2585. const rightZ = right.z;
  2586. const rightW = right.w;
  2587. const x = leftW * rightX + leftX * rightW + leftY * rightZ - leftZ * rightY;
  2588. const y = leftW * rightY - leftX * rightZ + leftY * rightW + leftZ * rightX;
  2589. const z = leftW * rightZ + leftX * rightY - leftY * rightX + leftZ * rightW;
  2590. const w = leftW * rightW - leftX * rightX - leftY * rightY - leftZ * rightZ;
  2591. result.x = x;
  2592. result.y = y;
  2593. result.z = z;
  2594. result.w = w;
  2595. return result;
  2596. };
  2597. /**
  2598. * Multiplies the provided quaternion componentwise by the provided scalar.
  2599. *
  2600. * @param {Quaternion} quaternion The quaternion to be scaled.
  2601. * @param {number} scalar The scalar to multiply with.
  2602. * @param {Quaternion} result The object onto which to store the result.
  2603. * @returns {Quaternion} The modified result parameter.
  2604. */
  2605. Quaternion.multiplyByScalar = function (quaternion, scalar, result) {
  2606. //>>includeStart('debug', pragmas.debug);
  2607. Check.Check.typeOf.object("quaternion", quaternion);
  2608. Check.Check.typeOf.number("scalar", scalar);
  2609. Check.Check.typeOf.object("result", result);
  2610. //>>includeEnd('debug');
  2611. result.x = quaternion.x * scalar;
  2612. result.y = quaternion.y * scalar;
  2613. result.z = quaternion.z * scalar;
  2614. result.w = quaternion.w * scalar;
  2615. return result;
  2616. };
  2617. /**
  2618. * Divides the provided quaternion componentwise by the provided scalar.
  2619. *
  2620. * @param {Quaternion} quaternion The quaternion to be divided.
  2621. * @param {number} scalar The scalar to divide by.
  2622. * @param {Quaternion} result The object onto which to store the result.
  2623. * @returns {Quaternion} The modified result parameter.
  2624. */
  2625. Quaternion.divideByScalar = function (quaternion, scalar, result) {
  2626. //>>includeStart('debug', pragmas.debug);
  2627. Check.Check.typeOf.object("quaternion", quaternion);
  2628. Check.Check.typeOf.number("scalar", scalar);
  2629. Check.Check.typeOf.object("result", result);
  2630. //>>includeEnd('debug');
  2631. result.x = quaternion.x / scalar;
  2632. result.y = quaternion.y / scalar;
  2633. result.z = quaternion.z / scalar;
  2634. result.w = quaternion.w / scalar;
  2635. return result;
  2636. };
  2637. /**
  2638. * Computes the axis of rotation of the provided quaternion.
  2639. *
  2640. * @param {Quaternion} quaternion The quaternion to use.
  2641. * @param {Cartesian3} result The object onto which to store the result.
  2642. * @returns {Cartesian3} The modified result parameter.
  2643. */
  2644. Quaternion.computeAxis = function (quaternion, result) {
  2645. //>>includeStart('debug', pragmas.debug);
  2646. Check.Check.typeOf.object("quaternion", quaternion);
  2647. Check.Check.typeOf.object("result", result);
  2648. //>>includeEnd('debug');
  2649. const w = quaternion.w;
  2650. if (Math.abs(w - 1.0) < Math$1.CesiumMath.EPSILON6) {
  2651. result.x = result.y = result.z = 0;
  2652. return result;
  2653. }
  2654. const scalar = 1.0 / Math.sqrt(1.0 - w * w);
  2655. result.x = quaternion.x * scalar;
  2656. result.y = quaternion.y * scalar;
  2657. result.z = quaternion.z * scalar;
  2658. return result;
  2659. };
  2660. /**
  2661. * Computes the angle of rotation of the provided quaternion.
  2662. *
  2663. * @param {Quaternion} quaternion The quaternion to use.
  2664. * @returns {number} The angle of rotation.
  2665. */
  2666. Quaternion.computeAngle = function (quaternion) {
  2667. //>>includeStart('debug', pragmas.debug);
  2668. Check.Check.typeOf.object("quaternion", quaternion);
  2669. //>>includeEnd('debug');
  2670. if (Math.abs(quaternion.w - 1.0) < Math$1.CesiumMath.EPSILON6) {
  2671. return 0.0;
  2672. }
  2673. return 2.0 * Math.acos(quaternion.w);
  2674. };
  2675. let lerpScratch = new Quaternion();
  2676. /**
  2677. * Computes the linear interpolation or extrapolation at t using the provided quaternions.
  2678. *
  2679. * @param {Quaternion} start The value corresponding to t at 0.0.
  2680. * @param {Quaternion} end The value corresponding to t at 1.0.
  2681. * @param {number} t The point along t at which to interpolate.
  2682. * @param {Quaternion} result The object onto which to store the result.
  2683. * @returns {Quaternion} The modified result parameter.
  2684. */
  2685. Quaternion.lerp = function (start, end, t, result) {
  2686. //>>includeStart('debug', pragmas.debug);
  2687. Check.Check.typeOf.object("start", start);
  2688. Check.Check.typeOf.object("end", end);
  2689. Check.Check.typeOf.number("t", t);
  2690. Check.Check.typeOf.object("result", result);
  2691. //>>includeEnd('debug');
  2692. lerpScratch = Quaternion.multiplyByScalar(end, t, lerpScratch);
  2693. result = Quaternion.multiplyByScalar(start, 1.0 - t, result);
  2694. return Quaternion.add(lerpScratch, result, result);
  2695. };
  2696. let slerpEndNegated = new Quaternion();
  2697. let slerpScaledP = new Quaternion();
  2698. let slerpScaledR = new Quaternion();
  2699. /**
  2700. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  2701. *
  2702. * @param {Quaternion} start The value corresponding to t at 0.0.
  2703. * @param {Quaternion} end The value corresponding to t at 1.0.
  2704. * @param {number} t The point along t at which to interpolate.
  2705. * @param {Quaternion} result The object onto which to store the result.
  2706. * @returns {Quaternion} The modified result parameter.
  2707. *
  2708. * @see Quaternion#fastSlerp
  2709. */
  2710. Quaternion.slerp = function (start, end, t, result) {
  2711. //>>includeStart('debug', pragmas.debug);
  2712. Check.Check.typeOf.object("start", start);
  2713. Check.Check.typeOf.object("end", end);
  2714. Check.Check.typeOf.number("t", t);
  2715. Check.Check.typeOf.object("result", result);
  2716. //>>includeEnd('debug');
  2717. let dot = Quaternion.dot(start, end);
  2718. // The angle between start must be acute. Since q and -q represent
  2719. // the same rotation, negate q to get the acute angle.
  2720. let r = end;
  2721. if (dot < 0.0) {
  2722. dot = -dot;
  2723. r = slerpEndNegated = Quaternion.negate(end, slerpEndNegated);
  2724. }
  2725. // dot > 0, as the dot product approaches 1, the angle between the
  2726. // quaternions vanishes. use linear interpolation.
  2727. if (1.0 - dot < Math$1.CesiumMath.EPSILON6) {
  2728. return Quaternion.lerp(start, r, t, result);
  2729. }
  2730. const theta = Math.acos(dot);
  2731. slerpScaledP = Quaternion.multiplyByScalar(
  2732. start,
  2733. Math.sin((1 - t) * theta),
  2734. slerpScaledP
  2735. );
  2736. slerpScaledR = Quaternion.multiplyByScalar(
  2737. r,
  2738. Math.sin(t * theta),
  2739. slerpScaledR
  2740. );
  2741. result = Quaternion.add(slerpScaledP, slerpScaledR, result);
  2742. return Quaternion.multiplyByScalar(result, 1.0 / Math.sin(theta), result);
  2743. };
  2744. /**
  2745. * The logarithmic quaternion function.
  2746. *
  2747. * @param {Quaternion} quaternion The unit quaternion.
  2748. * @param {Cartesian3} result The object onto which to store the result.
  2749. * @returns {Cartesian3} The modified result parameter.
  2750. */
  2751. Quaternion.log = function (quaternion, result) {
  2752. //>>includeStart('debug', pragmas.debug);
  2753. Check.Check.typeOf.object("quaternion", quaternion);
  2754. Check.Check.typeOf.object("result", result);
  2755. //>>includeEnd('debug');
  2756. const theta = Math$1.CesiumMath.acosClamped(quaternion.w);
  2757. let thetaOverSinTheta = 0.0;
  2758. if (theta !== 0.0) {
  2759. thetaOverSinTheta = theta / Math.sin(theta);
  2760. }
  2761. return Matrix3.Cartesian3.multiplyByScalar(quaternion, thetaOverSinTheta, result);
  2762. };
  2763. /**
  2764. * The exponential quaternion function.
  2765. *
  2766. * @param {Cartesian3} cartesian The cartesian.
  2767. * @param {Quaternion} result The object onto which to store the result.
  2768. * @returns {Quaternion} The modified result parameter.
  2769. */
  2770. Quaternion.exp = function (cartesian, result) {
  2771. //>>includeStart('debug', pragmas.debug);
  2772. Check.Check.typeOf.object("cartesian", cartesian);
  2773. Check.Check.typeOf.object("result", result);
  2774. //>>includeEnd('debug');
  2775. const theta = Matrix3.Cartesian3.magnitude(cartesian);
  2776. let sinThetaOverTheta = 0.0;
  2777. if (theta !== 0.0) {
  2778. sinThetaOverTheta = Math.sin(theta) / theta;
  2779. }
  2780. result.x = cartesian.x * sinThetaOverTheta;
  2781. result.y = cartesian.y * sinThetaOverTheta;
  2782. result.z = cartesian.z * sinThetaOverTheta;
  2783. result.w = Math.cos(theta);
  2784. return result;
  2785. };
  2786. const squadScratchCartesian0 = new Matrix3.Cartesian3();
  2787. const squadScratchCartesian1 = new Matrix3.Cartesian3();
  2788. const squadScratchQuaternion0 = new Quaternion();
  2789. const squadScratchQuaternion1 = new Quaternion();
  2790. /**
  2791. * Computes an inner quadrangle point.
  2792. * <p>This will compute quaternions that ensure a squad curve is C<sup>1</sup>.</p>
  2793. *
  2794. * @param {Quaternion} q0 The first quaternion.
  2795. * @param {Quaternion} q1 The second quaternion.
  2796. * @param {Quaternion} q2 The third quaternion.
  2797. * @param {Quaternion} result The object onto which to store the result.
  2798. * @returns {Quaternion} The modified result parameter.
  2799. *
  2800. * @see Quaternion#squad
  2801. */
  2802. Quaternion.computeInnerQuadrangle = function (q0, q1, q2, result) {
  2803. //>>includeStart('debug', pragmas.debug);
  2804. Check.Check.typeOf.object("q0", q0);
  2805. Check.Check.typeOf.object("q1", q1);
  2806. Check.Check.typeOf.object("q2", q2);
  2807. Check.Check.typeOf.object("result", result);
  2808. //>>includeEnd('debug');
  2809. const qInv = Quaternion.conjugate(q1, squadScratchQuaternion0);
  2810. Quaternion.multiply(qInv, q2, squadScratchQuaternion1);
  2811. const cart0 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian0);
  2812. Quaternion.multiply(qInv, q0, squadScratchQuaternion1);
  2813. const cart1 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian1);
  2814. Matrix3.Cartesian3.add(cart0, cart1, cart0);
  2815. Matrix3.Cartesian3.multiplyByScalar(cart0, 0.25, cart0);
  2816. Matrix3.Cartesian3.negate(cart0, cart0);
  2817. Quaternion.exp(cart0, squadScratchQuaternion0);
  2818. return Quaternion.multiply(q1, squadScratchQuaternion0, result);
  2819. };
  2820. /**
  2821. * Computes the spherical quadrangle interpolation between quaternions.
  2822. *
  2823. * @param {Quaternion} q0 The first quaternion.
  2824. * @param {Quaternion} q1 The second quaternion.
  2825. * @param {Quaternion} s0 The first inner quadrangle.
  2826. * @param {Quaternion} s1 The second inner quadrangle.
  2827. * @param {number} t The time in [0,1] used to interpolate.
  2828. * @param {Quaternion} result The object onto which to store the result.
  2829. * @returns {Quaternion} The modified result parameter.
  2830. *
  2831. *
  2832. * @example
  2833. * // 1. compute the squad interpolation between two quaternions on a curve
  2834. * const s0 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i - 1], quaternions[i], quaternions[i + 1], new Cesium.Quaternion());
  2835. * const s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i], quaternions[i + 1], quaternions[i + 2], new Cesium.Quaternion());
  2836. * const q = Cesium.Quaternion.squad(quaternions[i], quaternions[i + 1], s0, s1, t, new Cesium.Quaternion());
  2837. *
  2838. * // 2. compute the squad interpolation as above but where the first quaternion is a end point.
  2839. * const s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[0], quaternions[1], quaternions[2], new Cesium.Quaternion());
  2840. * const q = Cesium.Quaternion.squad(quaternions[0], quaternions[1], quaternions[0], s1, t, new Cesium.Quaternion());
  2841. *
  2842. * @see Quaternion#computeInnerQuadrangle
  2843. */
  2844. Quaternion.squad = function (q0, q1, s0, s1, t, result) {
  2845. //>>includeStart('debug', pragmas.debug);
  2846. Check.Check.typeOf.object("q0", q0);
  2847. Check.Check.typeOf.object("q1", q1);
  2848. Check.Check.typeOf.object("s0", s0);
  2849. Check.Check.typeOf.object("s1", s1);
  2850. Check.Check.typeOf.number("t", t);
  2851. Check.Check.typeOf.object("result", result);
  2852. //>>includeEnd('debug');
  2853. const slerp0 = Quaternion.slerp(q0, q1, t, squadScratchQuaternion0);
  2854. const slerp1 = Quaternion.slerp(s0, s1, t, squadScratchQuaternion1);
  2855. return Quaternion.slerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  2856. };
  2857. const fastSlerpScratchQuaternion = new Quaternion();
  2858. // eslint-disable-next-line no-loss-of-precision
  2859. const opmu = 1.90110745351730037;
  2860. const u = FeatureDetection$1.supportsTypedArrays() ? new Float32Array(8) : [];
  2861. const v = FeatureDetection$1.supportsTypedArrays() ? new Float32Array(8) : [];
  2862. const bT = FeatureDetection$1.supportsTypedArrays() ? new Float32Array(8) : [];
  2863. const bD = FeatureDetection$1.supportsTypedArrays() ? new Float32Array(8) : [];
  2864. for (let i = 0; i < 7; ++i) {
  2865. const s = i + 1.0;
  2866. const t = 2.0 * s + 1.0;
  2867. u[i] = 1.0 / (s * t);
  2868. v[i] = s / t;
  2869. }
  2870. u[7] = opmu / (8.0 * 17.0);
  2871. v[7] = (opmu * 8.0) / 17.0;
  2872. /**
  2873. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  2874. * This implementation is faster than {@link Quaternion#slerp}, but is only accurate up to 10<sup>-6</sup>.
  2875. *
  2876. * @param {Quaternion} start The value corresponding to t at 0.0.
  2877. * @param {Quaternion} end The value corresponding to t at 1.0.
  2878. * @param {number} t The point along t at which to interpolate.
  2879. * @param {Quaternion} result The object onto which to store the result.
  2880. * @returns {Quaternion} The modified result parameter.
  2881. *
  2882. * @see Quaternion#slerp
  2883. */
  2884. Quaternion.fastSlerp = function (start, end, t, result) {
  2885. //>>includeStart('debug', pragmas.debug);
  2886. Check.Check.typeOf.object("start", start);
  2887. Check.Check.typeOf.object("end", end);
  2888. Check.Check.typeOf.number("t", t);
  2889. Check.Check.typeOf.object("result", result);
  2890. //>>includeEnd('debug');
  2891. let x = Quaternion.dot(start, end);
  2892. let sign;
  2893. if (x >= 0) {
  2894. sign = 1.0;
  2895. } else {
  2896. sign = -1.0;
  2897. x = -x;
  2898. }
  2899. const xm1 = x - 1.0;
  2900. const d = 1.0 - t;
  2901. const sqrT = t * t;
  2902. const sqrD = d * d;
  2903. for (let i = 7; i >= 0; --i) {
  2904. bT[i] = (u[i] * sqrT - v[i]) * xm1;
  2905. bD[i] = (u[i] * sqrD - v[i]) * xm1;
  2906. }
  2907. const cT =
  2908. sign *
  2909. t *
  2910. (1.0 +
  2911. bT[0] *
  2912. (1.0 +
  2913. bT[1] *
  2914. (1.0 +
  2915. bT[2] *
  2916. (1.0 +
  2917. bT[3] *
  2918. (1.0 +
  2919. bT[4] *
  2920. (1.0 + bT[5] * (1.0 + bT[6] * (1.0 + bT[7]))))))));
  2921. const cD =
  2922. d *
  2923. (1.0 +
  2924. bD[0] *
  2925. (1.0 +
  2926. bD[1] *
  2927. (1.0 +
  2928. bD[2] *
  2929. (1.0 +
  2930. bD[3] *
  2931. (1.0 +
  2932. bD[4] *
  2933. (1.0 + bD[5] * (1.0 + bD[6] * (1.0 + bD[7]))))))));
  2934. const temp = Quaternion.multiplyByScalar(
  2935. start,
  2936. cD,
  2937. fastSlerpScratchQuaternion
  2938. );
  2939. Quaternion.multiplyByScalar(end, cT, result);
  2940. return Quaternion.add(temp, result, result);
  2941. };
  2942. /**
  2943. * Computes the spherical quadrangle interpolation between quaternions.
  2944. * An implementation that is faster than {@link Quaternion#squad}, but less accurate.
  2945. *
  2946. * @param {Quaternion} q0 The first quaternion.
  2947. * @param {Quaternion} q1 The second quaternion.
  2948. * @param {Quaternion} s0 The first inner quadrangle.
  2949. * @param {Quaternion} s1 The second inner quadrangle.
  2950. * @param {number} t The time in [0,1] used to interpolate.
  2951. * @param {Quaternion} result The object onto which to store the result.
  2952. * @returns {Quaternion} The modified result parameter or a new instance if none was provided.
  2953. *
  2954. * @see Quaternion#squad
  2955. */
  2956. Quaternion.fastSquad = function (q0, q1, s0, s1, t, result) {
  2957. //>>includeStart('debug', pragmas.debug);
  2958. Check.Check.typeOf.object("q0", q0);
  2959. Check.Check.typeOf.object("q1", q1);
  2960. Check.Check.typeOf.object("s0", s0);
  2961. Check.Check.typeOf.object("s1", s1);
  2962. Check.Check.typeOf.number("t", t);
  2963. Check.Check.typeOf.object("result", result);
  2964. //>>includeEnd('debug');
  2965. const slerp0 = Quaternion.fastSlerp(q0, q1, t, squadScratchQuaternion0);
  2966. const slerp1 = Quaternion.fastSlerp(s0, s1, t, squadScratchQuaternion1);
  2967. return Quaternion.fastSlerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  2968. };
  2969. /**
  2970. * Compares the provided quaternions componentwise and returns
  2971. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2972. *
  2973. * @param {Quaternion} [left] The first quaternion.
  2974. * @param {Quaternion} [right] The second quaternion.
  2975. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  2976. */
  2977. Quaternion.equals = function (left, right) {
  2978. return (
  2979. left === right ||
  2980. (defaultValue.defined(left) &&
  2981. defaultValue.defined(right) &&
  2982. left.x === right.x &&
  2983. left.y === right.y &&
  2984. left.z === right.z &&
  2985. left.w === right.w)
  2986. );
  2987. };
  2988. /**
  2989. * Compares the provided quaternions componentwise and returns
  2990. * <code>true</code> if they are within the provided epsilon,
  2991. * <code>false</code> otherwise.
  2992. *
  2993. * @param {Quaternion} [left] The first quaternion.
  2994. * @param {Quaternion} [right] The second quaternion.
  2995. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  2996. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2997. */
  2998. Quaternion.equalsEpsilon = function (left, right, epsilon) {
  2999. epsilon = defaultValue.defaultValue(epsilon, 0);
  3000. return (
  3001. left === right ||
  3002. (defaultValue.defined(left) &&
  3003. defaultValue.defined(right) &&
  3004. Math.abs(left.x - right.x) <= epsilon &&
  3005. Math.abs(left.y - right.y) <= epsilon &&
  3006. Math.abs(left.z - right.z) <= epsilon &&
  3007. Math.abs(left.w - right.w) <= epsilon)
  3008. );
  3009. };
  3010. /**
  3011. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 0.0).
  3012. *
  3013. * @type {Quaternion}
  3014. * @constant
  3015. */
  3016. Quaternion.ZERO = Object.freeze(new Quaternion(0.0, 0.0, 0.0, 0.0));
  3017. /**
  3018. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 1.0).
  3019. *
  3020. * @type {Quaternion}
  3021. * @constant
  3022. */
  3023. Quaternion.IDENTITY = Object.freeze(new Quaternion(0.0, 0.0, 0.0, 1.0));
  3024. /**
  3025. * Duplicates this Quaternion instance.
  3026. *
  3027. * @param {Quaternion} [result] The object onto which to store the result.
  3028. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  3029. */
  3030. Quaternion.prototype.clone = function (result) {
  3031. return Quaternion.clone(this, result);
  3032. };
  3033. /**
  3034. * Compares this and the provided quaternion componentwise and returns
  3035. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3036. *
  3037. * @param {Quaternion} [right] The right hand side quaternion.
  3038. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3039. */
  3040. Quaternion.prototype.equals = function (right) {
  3041. return Quaternion.equals(this, right);
  3042. };
  3043. /**
  3044. * Compares this and the provided quaternion componentwise and returns
  3045. * <code>true</code> if they are within the provided epsilon,
  3046. * <code>false</code> otherwise.
  3047. *
  3048. * @param {Quaternion} [right] The right hand side quaternion.
  3049. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  3050. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3051. */
  3052. Quaternion.prototype.equalsEpsilon = function (right, epsilon) {
  3053. return Quaternion.equalsEpsilon(this, right, epsilon);
  3054. };
  3055. /**
  3056. * Returns a string representing this quaternion in the format (x, y, z, w).
  3057. *
  3058. * @returns {string} A string representing this Quaternion.
  3059. */
  3060. Quaternion.prototype.toString = function () {
  3061. return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`;
  3062. };
  3063. /**
  3064. * Finds an item in a sorted array.
  3065. *
  3066. * @function
  3067. * @param {Array} array The sorted array to search.
  3068. * @param {*} itemToFind The item to find in the array.
  3069. * @param {binarySearchComparator} comparator The function to use to compare the item to
  3070. * elements in the array.
  3071. * @returns {number} The index of <code>itemToFind</code> in the array, if it exists. If <code>itemToFind</code>
  3072. * does not exist, the return value is a negative number which is the bitwise complement (~)
  3073. * of the index before which the itemToFind should be inserted in order to maintain the
  3074. * sorted order of the array.
  3075. *
  3076. * @example
  3077. * // Create a comparator function to search through an array of numbers.
  3078. * function comparator(a, b) {
  3079. * return a - b;
  3080. * };
  3081. * const numbers = [0, 2, 4, 6, 8];
  3082. * const index = Cesium.binarySearch(numbers, 6, comparator); // 3
  3083. */
  3084. function binarySearch(array, itemToFind, comparator) {
  3085. //>>includeStart('debug', pragmas.debug);
  3086. Check.Check.defined("array", array);
  3087. Check.Check.defined("itemToFind", itemToFind);
  3088. Check.Check.defined("comparator", comparator);
  3089. //>>includeEnd('debug');
  3090. let low = 0;
  3091. let high = array.length - 1;
  3092. let i;
  3093. let comparison;
  3094. while (low <= high) {
  3095. i = ~~((low + high) / 2);
  3096. comparison = comparator(array[i], itemToFind);
  3097. if (comparison < 0) {
  3098. low = i + 1;
  3099. continue;
  3100. }
  3101. if (comparison > 0) {
  3102. high = i - 1;
  3103. continue;
  3104. }
  3105. return i;
  3106. }
  3107. return ~(high + 1);
  3108. }
  3109. /**
  3110. * A set of Earth Orientation Parameters (EOP) sampled at a time.
  3111. *
  3112. * @alias EarthOrientationParametersSample
  3113. * @constructor
  3114. *
  3115. * @param {number} xPoleWander The pole wander about the X axis, in radians.
  3116. * @param {number} yPoleWander The pole wander about the Y axis, in radians.
  3117. * @param {number} xPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  3118. * @param {number} yPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  3119. * @param {number} ut1MinusUtc The difference in time standards, UT1 - UTC, in seconds.
  3120. *
  3121. * @private
  3122. */
  3123. function EarthOrientationParametersSample(
  3124. xPoleWander,
  3125. yPoleWander,
  3126. xPoleOffset,
  3127. yPoleOffset,
  3128. ut1MinusUtc
  3129. ) {
  3130. /**
  3131. * The pole wander about the X axis, in radians.
  3132. * @type {number}
  3133. */
  3134. this.xPoleWander = xPoleWander;
  3135. /**
  3136. * The pole wander about the Y axis, in radians.
  3137. * @type {number}
  3138. */
  3139. this.yPoleWander = yPoleWander;
  3140. /**
  3141. * The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  3142. * @type {number}
  3143. */
  3144. this.xPoleOffset = xPoleOffset;
  3145. /**
  3146. * The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  3147. * @type {number}
  3148. */
  3149. this.yPoleOffset = yPoleOffset;
  3150. /**
  3151. * The difference in time standards, UT1 - UTC, in seconds.
  3152. * @type {number}
  3153. */
  3154. this.ut1MinusUtc = ut1MinusUtc;
  3155. }
  3156. /**
  3157. * Represents a Gregorian date in a more precise format than the JavaScript Date object.
  3158. * In addition to submillisecond precision, this object can also represent leap seconds.
  3159. * @alias GregorianDate
  3160. * @constructor
  3161. *
  3162. * @param {number} [year] The year as a whole number.
  3163. * @param {number} [month] The month as a whole number with range [1, 12].
  3164. * @param {number} [day] The day of the month as a whole number starting at 1.
  3165. * @param {number} [hour] The hour as a whole number with range [0, 23].
  3166. * @param {number} [minute] The minute of the hour as a whole number with range [0, 59].
  3167. * @param {number} [second] The second of the minute as a whole number with range [0, 60], with 60 representing a leap second.
  3168. * @param {number} [millisecond] The millisecond of the second as a floating point number with range [0.0, 1000.0).
  3169. * @param {boolean} [isLeapSecond] Whether this time is during a leap second.
  3170. *
  3171. * @see JulianDate#toGregorianDate
  3172. */
  3173. function GregorianDate(
  3174. year,
  3175. month,
  3176. day,
  3177. hour,
  3178. minute,
  3179. second,
  3180. millisecond,
  3181. isLeapSecond
  3182. ) {
  3183. /**
  3184. * Gets or sets the year as a whole number.
  3185. * @type {number}
  3186. */
  3187. this.year = year;
  3188. /**
  3189. * Gets or sets the month as a whole number with range [1, 12].
  3190. * @type {number}
  3191. */
  3192. this.month = month;
  3193. /**
  3194. * Gets or sets the day of the month as a whole number starting at 1.
  3195. * @type {number}
  3196. */
  3197. this.day = day;
  3198. /**
  3199. * Gets or sets the hour as a whole number with range [0, 23].
  3200. * @type {number}
  3201. */
  3202. this.hour = hour;
  3203. /**
  3204. * Gets or sets the minute of the hour as a whole number with range [0, 59].
  3205. * @type {number}
  3206. */
  3207. this.minute = minute;
  3208. /**
  3209. * Gets or sets the second of the minute as a whole number with range [0, 60], with 60 representing a leap second.
  3210. * @type {number}
  3211. */
  3212. this.second = second;
  3213. /**
  3214. * Gets or sets the millisecond of the second as a floating point number with range [0.0, 1000.0).
  3215. * @type {number}
  3216. */
  3217. this.millisecond = millisecond;
  3218. /**
  3219. * Gets or sets whether this time is during a leap second.
  3220. * @type {boolean}
  3221. */
  3222. this.isLeapSecond = isLeapSecond;
  3223. }
  3224. /**
  3225. * Determines if a given date is a leap year.
  3226. *
  3227. * @function isLeapYear
  3228. *
  3229. * @param {number} year The year to be tested.
  3230. * @returns {boolean} True if <code>year</code> is a leap year.
  3231. *
  3232. * @example
  3233. * const leapYear = Cesium.isLeapYear(2000); // true
  3234. */
  3235. function isLeapYear(year) {
  3236. //>>includeStart('debug', pragmas.debug);
  3237. if (year === null || isNaN(year)) {
  3238. throw new Check.DeveloperError("year is required and must be a number.");
  3239. }
  3240. //>>includeEnd('debug');
  3241. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  3242. }
  3243. /**
  3244. * Describes a single leap second, which is constructed from a {@link JulianDate} and a
  3245. * numerical offset representing the number of seconds TAI is ahead of the UTC time standard.
  3246. * @alias LeapSecond
  3247. * @constructor
  3248. *
  3249. * @param {JulianDate} [date] A Julian date representing the time of the leap second.
  3250. * @param {number} [offset] The cumulative number of seconds that TAI is ahead of UTC at the provided date.
  3251. */
  3252. function LeapSecond(date, offset) {
  3253. /**
  3254. * Gets or sets the date at which this leap second occurs.
  3255. * @type {JulianDate}
  3256. */
  3257. this.julianDate = date;
  3258. /**
  3259. * Gets or sets the cumulative number of seconds between the UTC and TAI time standards at the time
  3260. * of this leap second.
  3261. * @type {number}
  3262. */
  3263. this.offset = offset;
  3264. }
  3265. /**
  3266. * Constants for time conversions like those done by {@link JulianDate}.
  3267. *
  3268. * @namespace TimeConstants
  3269. *
  3270. * @see JulianDate
  3271. *
  3272. * @private
  3273. */
  3274. const TimeConstants = {
  3275. /**
  3276. * The number of seconds in one millisecond: <code>0.001</code>
  3277. * @type {number}
  3278. * @constant
  3279. */
  3280. SECONDS_PER_MILLISECOND: 0.001,
  3281. /**
  3282. * The number of seconds in one minute: <code>60</code>.
  3283. * @type {number}
  3284. * @constant
  3285. */
  3286. SECONDS_PER_MINUTE: 60.0,
  3287. /**
  3288. * The number of minutes in one hour: <code>60</code>.
  3289. * @type {number}
  3290. * @constant
  3291. */
  3292. MINUTES_PER_HOUR: 60.0,
  3293. /**
  3294. * The number of hours in one day: <code>24</code>.
  3295. * @type {number}
  3296. * @constant
  3297. */
  3298. HOURS_PER_DAY: 24.0,
  3299. /**
  3300. * The number of seconds in one hour: <code>3600</code>.
  3301. * @type {number}
  3302. * @constant
  3303. */
  3304. SECONDS_PER_HOUR: 3600.0,
  3305. /**
  3306. * The number of minutes in one day: <code>1440</code>.
  3307. * @type {number}
  3308. * @constant
  3309. */
  3310. MINUTES_PER_DAY: 1440.0,
  3311. /**
  3312. * The number of seconds in one day, ignoring leap seconds: <code>86400</code>.
  3313. * @type {number}
  3314. * @constant
  3315. */
  3316. SECONDS_PER_DAY: 86400.0,
  3317. /**
  3318. * The number of days in one Julian century: <code>36525</code>.
  3319. * @type {number}
  3320. * @constant
  3321. */
  3322. DAYS_PER_JULIAN_CENTURY: 36525.0,
  3323. /**
  3324. * One trillionth of a second.
  3325. * @type {number}
  3326. * @constant
  3327. */
  3328. PICOSECOND: 0.000000001,
  3329. /**
  3330. * The number of days to subtract from a Julian date to determine the
  3331. * modified Julian date, which gives the number of days since midnight
  3332. * on November 17, 1858.
  3333. * @type {number}
  3334. * @constant
  3335. */
  3336. MODIFIED_JULIAN_DATE_DIFFERENCE: 2400000.5,
  3337. };
  3338. var TimeConstants$1 = Object.freeze(TimeConstants);
  3339. /**
  3340. * Provides the type of time standards which JulianDate can take as input.
  3341. *
  3342. * @enum {number}
  3343. *
  3344. * @see JulianDate
  3345. */
  3346. const TimeStandard = {
  3347. /**
  3348. * Represents the coordinated Universal Time (UTC) time standard.
  3349. *
  3350. * UTC is related to TAI according to the relationship
  3351. * <code>UTC = TAI - deltaT</code> where <code>deltaT</code> is the number of leap
  3352. * seconds which have been introduced as of the time in TAI.
  3353. *
  3354. * @type {number}
  3355. * @constant
  3356. */
  3357. UTC: 0,
  3358. /**
  3359. * Represents the International Atomic Time (TAI) time standard.
  3360. * TAI is the principal time standard to which the other time standards are related.
  3361. *
  3362. * @type {number}
  3363. * @constant
  3364. */
  3365. TAI: 1,
  3366. };
  3367. var TimeStandard$1 = Object.freeze(TimeStandard);
  3368. const gregorianDateScratch = new GregorianDate();
  3369. const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  3370. const daysInLeapFeburary = 29;
  3371. function compareLeapSecondDates$1(leapSecond, dateToFind) {
  3372. return JulianDate.compare(leapSecond.julianDate, dateToFind.julianDate);
  3373. }
  3374. // we don't really need a leap second instance, anything with a julianDate property will do
  3375. const binarySearchScratchLeapSecond = new LeapSecond();
  3376. function convertUtcToTai(julianDate) {
  3377. //Even though julianDate is in UTC, we'll treat it as TAI and
  3378. //search the leap second table for it.
  3379. binarySearchScratchLeapSecond.julianDate = julianDate;
  3380. const leapSeconds = JulianDate.leapSeconds;
  3381. let index = binarySearch(
  3382. leapSeconds,
  3383. binarySearchScratchLeapSecond,
  3384. compareLeapSecondDates$1
  3385. );
  3386. if (index < 0) {
  3387. index = ~index;
  3388. }
  3389. if (index >= leapSeconds.length) {
  3390. index = leapSeconds.length - 1;
  3391. }
  3392. let offset = leapSeconds[index].offset;
  3393. if (index > 0) {
  3394. //Now we have the index of the closest leap second that comes on or after our UTC time.
  3395. //However, if the difference between the UTC date being converted and the TAI
  3396. //defined leap second is greater than the offset, we are off by one and need to use
  3397. //the previous leap second.
  3398. const difference = JulianDate.secondsDifference(
  3399. leapSeconds[index].julianDate,
  3400. julianDate
  3401. );
  3402. if (difference > offset) {
  3403. index--;
  3404. offset = leapSeconds[index].offset;
  3405. }
  3406. }
  3407. JulianDate.addSeconds(julianDate, offset, julianDate);
  3408. }
  3409. function convertTaiToUtc(julianDate, result) {
  3410. binarySearchScratchLeapSecond.julianDate = julianDate;
  3411. const leapSeconds = JulianDate.leapSeconds;
  3412. let index = binarySearch(
  3413. leapSeconds,
  3414. binarySearchScratchLeapSecond,
  3415. compareLeapSecondDates$1
  3416. );
  3417. if (index < 0) {
  3418. index = ~index;
  3419. }
  3420. //All times before our first leap second get the first offset.
  3421. if (index === 0) {
  3422. return JulianDate.addSeconds(julianDate, -leapSeconds[0].offset, result);
  3423. }
  3424. //All times after our leap second get the last offset.
  3425. if (index >= leapSeconds.length) {
  3426. return JulianDate.addSeconds(
  3427. julianDate,
  3428. -leapSeconds[index - 1].offset,
  3429. result
  3430. );
  3431. }
  3432. //Compute the difference between the found leap second and the time we are converting.
  3433. const difference = JulianDate.secondsDifference(
  3434. leapSeconds[index].julianDate,
  3435. julianDate
  3436. );
  3437. if (difference === 0) {
  3438. //The date is in our leap second table.
  3439. return JulianDate.addSeconds(
  3440. julianDate,
  3441. -leapSeconds[index].offset,
  3442. result
  3443. );
  3444. }
  3445. if (difference <= 1.0) {
  3446. //The requested date is during the moment of a leap second, then we cannot convert to UTC
  3447. return undefined;
  3448. }
  3449. //The time is in between two leap seconds, index is the leap second after the date
  3450. //we're converting, so we subtract one to get the correct LeapSecond instance.
  3451. return JulianDate.addSeconds(
  3452. julianDate,
  3453. -leapSeconds[--index].offset,
  3454. result
  3455. );
  3456. }
  3457. function setComponents(wholeDays, secondsOfDay, julianDate) {
  3458. const extraDays = (secondsOfDay / TimeConstants$1.SECONDS_PER_DAY) | 0;
  3459. wholeDays += extraDays;
  3460. secondsOfDay -= TimeConstants$1.SECONDS_PER_DAY * extraDays;
  3461. if (secondsOfDay < 0) {
  3462. wholeDays--;
  3463. secondsOfDay += TimeConstants$1.SECONDS_PER_DAY;
  3464. }
  3465. julianDate.dayNumber = wholeDays;
  3466. julianDate.secondsOfDay = secondsOfDay;
  3467. return julianDate;
  3468. }
  3469. function computeJulianDateComponents(
  3470. year,
  3471. month,
  3472. day,
  3473. hour,
  3474. minute,
  3475. second,
  3476. millisecond
  3477. ) {
  3478. // Algorithm from page 604 of the Explanatory Supplement to the
  3479. // Astronomical Almanac (Seidelmann 1992).
  3480. const a = ((month - 14) / 12) | 0;
  3481. const b = year + 4800 + a;
  3482. let dayNumber =
  3483. (((1461 * b) / 4) | 0) +
  3484. (((367 * (month - 2 - 12 * a)) / 12) | 0) -
  3485. (((3 * (((b + 100) / 100) | 0)) / 4) | 0) +
  3486. day -
  3487. 32075;
  3488. // JulianDates are noon-based
  3489. hour = hour - 12;
  3490. if (hour < 0) {
  3491. hour += 24;
  3492. }
  3493. const secondsOfDay =
  3494. second +
  3495. (hour * TimeConstants$1.SECONDS_PER_HOUR +
  3496. minute * TimeConstants$1.SECONDS_PER_MINUTE +
  3497. millisecond * TimeConstants$1.SECONDS_PER_MILLISECOND);
  3498. if (secondsOfDay >= 43200.0) {
  3499. dayNumber -= 1;
  3500. }
  3501. return [dayNumber, secondsOfDay];
  3502. }
  3503. //Regular expressions used for ISO8601 date parsing.
  3504. //YYYY
  3505. const matchCalendarYear = /^(\d{4})$/;
  3506. //YYYY-MM (YYYYMM is invalid)
  3507. const matchCalendarMonth = /^(\d{4})-(\d{2})$/;
  3508. //YYYY-DDD or YYYYDDD
  3509. const matchOrdinalDate = /^(\d{4})-?(\d{3})$/;
  3510. //YYYY-Www or YYYYWww or YYYY-Www-D or YYYYWwwD
  3511. const matchWeekDate = /^(\d{4})-?W(\d{2})-?(\d{1})?$/;
  3512. //YYYY-MM-DD or YYYYMMDD
  3513. const matchCalendarDate = /^(\d{4})-?(\d{2})-?(\d{2})$/;
  3514. // Match utc offset
  3515. const utcOffset = /([Z+\-])?(\d{2})?:?(\d{2})?$/;
  3516. // Match hours HH or HH.xxxxx
  3517. const matchHours = /^(\d{2})(\.\d+)?/.source + utcOffset.source;
  3518. // Match hours/minutes HH:MM HHMM.xxxxx
  3519. const matchHoursMinutes = /^(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  3520. // Match hours/minutes HH:MM:SS HHMMSS.xxxxx
  3521. const matchHoursMinutesSeconds =
  3522. /^(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  3523. const iso8601ErrorMessage = "Invalid ISO 8601 date.";
  3524. /**
  3525. * Represents an astronomical Julian date, which is the number of days since noon on January 1, -4712 (4713 BC).
  3526. * For increased precision, this class stores the whole number part of the date and the seconds
  3527. * part of the date in separate components. In order to be safe for arithmetic and represent
  3528. * leap seconds, the date is always stored in the International Atomic Time standard
  3529. * {@link TimeStandard.TAI}.
  3530. * @alias JulianDate
  3531. * @constructor
  3532. *
  3533. * @param {number} [julianDayNumber=0.0] The Julian Day Number representing the number of whole days. Fractional days will also be handled correctly.
  3534. * @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.
  3535. * @param {TimeStandard} [timeStandard=TimeStandard.UTC] The time standard in which the first two parameters are defined.
  3536. */
  3537. function JulianDate(julianDayNumber, secondsOfDay, timeStandard) {
  3538. /**
  3539. * Gets or sets the number of whole days.
  3540. * @type {number}
  3541. */
  3542. this.dayNumber = undefined;
  3543. /**
  3544. * Gets or sets the number of seconds into the current day.
  3545. * @type {number}
  3546. */
  3547. this.secondsOfDay = undefined;
  3548. julianDayNumber = defaultValue.defaultValue(julianDayNumber, 0.0);
  3549. secondsOfDay = defaultValue.defaultValue(secondsOfDay, 0.0);
  3550. timeStandard = defaultValue.defaultValue(timeStandard, TimeStandard$1.UTC);
  3551. //If julianDayNumber is fractional, make it an integer and add the number of seconds the fraction represented.
  3552. const wholeDays = julianDayNumber | 0;
  3553. secondsOfDay =
  3554. secondsOfDay +
  3555. (julianDayNumber - wholeDays) * TimeConstants$1.SECONDS_PER_DAY;
  3556. setComponents(wholeDays, secondsOfDay, this);
  3557. if (timeStandard === TimeStandard$1.UTC) {
  3558. convertUtcToTai(this);
  3559. }
  3560. }
  3561. /**
  3562. * Creates a new instance from a GregorianDate.
  3563. *
  3564. * @param {GregorianDate} date A GregorianDate.
  3565. * @param {JulianDate} [result] An existing instance to use for the result.
  3566. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  3567. *
  3568. * @exception {DeveloperError} date must be a valid GregorianDate.
  3569. */
  3570. JulianDate.fromGregorianDate = function (date, result) {
  3571. //>>includeStart('debug', pragmas.debug);
  3572. if (!(date instanceof GregorianDate)) {
  3573. throw new Check.DeveloperError("date must be a valid GregorianDate.");
  3574. }
  3575. //>>includeEnd('debug');
  3576. const components = computeJulianDateComponents(
  3577. date.year,
  3578. date.month,
  3579. date.day,
  3580. date.hour,
  3581. date.minute,
  3582. date.second,
  3583. date.millisecond
  3584. );
  3585. if (!defaultValue.defined(result)) {
  3586. return new JulianDate(components[0], components[1], TimeStandard$1.UTC);
  3587. }
  3588. setComponents(components[0], components[1], result);
  3589. convertUtcToTai(result);
  3590. return result;
  3591. };
  3592. /**
  3593. * Creates a new instance from a JavaScript Date.
  3594. *
  3595. * @param {Date} date A JavaScript Date.
  3596. * @param {JulianDate} [result] An existing instance to use for the result.
  3597. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  3598. *
  3599. * @exception {DeveloperError} date must be a valid JavaScript Date.
  3600. */
  3601. JulianDate.fromDate = function (date, result) {
  3602. //>>includeStart('debug', pragmas.debug);
  3603. if (!(date instanceof Date) || isNaN(date.getTime())) {
  3604. throw new Check.DeveloperError("date must be a valid JavaScript Date.");
  3605. }
  3606. //>>includeEnd('debug');
  3607. const components = computeJulianDateComponents(
  3608. date.getUTCFullYear(),
  3609. date.getUTCMonth() + 1,
  3610. date.getUTCDate(),
  3611. date.getUTCHours(),
  3612. date.getUTCMinutes(),
  3613. date.getUTCSeconds(),
  3614. date.getUTCMilliseconds()
  3615. );
  3616. if (!defaultValue.defined(result)) {
  3617. return new JulianDate(components[0], components[1], TimeStandard$1.UTC);
  3618. }
  3619. setComponents(components[0], components[1], result);
  3620. convertUtcToTai(result);
  3621. return result;
  3622. };
  3623. /**
  3624. * Creates a new instance from a from an {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} date.
  3625. * This method is superior to <code>Date.parse</code> because it will handle all valid formats defined by the ISO 8601
  3626. * specification, including leap seconds and sub-millisecond times, which discarded by most JavaScript implementations.
  3627. *
  3628. * @param {string} iso8601String An ISO 8601 date.
  3629. * @param {JulianDate} [result] An existing instance to use for the result.
  3630. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  3631. *
  3632. * @exception {DeveloperError} Invalid ISO 8601 date.
  3633. */
  3634. JulianDate.fromIso8601 = function (iso8601String, result) {
  3635. //>>includeStart('debug', pragmas.debug);
  3636. if (typeof iso8601String !== "string") {
  3637. throw new Check.DeveloperError(iso8601ErrorMessage);
  3638. }
  3639. //>>includeEnd('debug');
  3640. //Comma and decimal point both indicate a fractional number according to ISO 8601,
  3641. //start out by blanket replacing , with . which is the only valid such symbol in JS.
  3642. iso8601String = iso8601String.replace(",", ".");
  3643. //Split the string into its date and time components, denoted by a mandatory T
  3644. let tokens = iso8601String.split("T");
  3645. let year;
  3646. let month = 1;
  3647. let day = 1;
  3648. let hour = 0;
  3649. let minute = 0;
  3650. let second = 0;
  3651. let millisecond = 0;
  3652. //Lacking a time is okay, but a missing date is illegal.
  3653. const date = tokens[0];
  3654. const time = tokens[1];
  3655. let tmp;
  3656. let inLeapYear;
  3657. //>>includeStart('debug', pragmas.debug);
  3658. if (!defaultValue.defined(date)) {
  3659. throw new Check.DeveloperError(iso8601ErrorMessage);
  3660. }
  3661. let dashCount;
  3662. //>>includeEnd('debug');
  3663. //First match the date against possible regular expressions.
  3664. tokens = date.match(matchCalendarDate);
  3665. if (tokens !== null) {
  3666. //>>includeStart('debug', pragmas.debug);
  3667. dashCount = date.split("-").length - 1;
  3668. if (dashCount > 0 && dashCount !== 2) {
  3669. throw new Check.DeveloperError(iso8601ErrorMessage);
  3670. }
  3671. //>>includeEnd('debug');
  3672. year = +tokens[1];
  3673. month = +tokens[2];
  3674. day = +tokens[3];
  3675. } else {
  3676. tokens = date.match(matchCalendarMonth);
  3677. if (tokens !== null) {
  3678. year = +tokens[1];
  3679. month = +tokens[2];
  3680. } else {
  3681. tokens = date.match(matchCalendarYear);
  3682. if (tokens !== null) {
  3683. year = +tokens[1];
  3684. } else {
  3685. //Not a year/month/day so it must be an ordinal date.
  3686. let dayOfYear;
  3687. tokens = date.match(matchOrdinalDate);
  3688. if (tokens !== null) {
  3689. year = +tokens[1];
  3690. dayOfYear = +tokens[2];
  3691. inLeapYear = isLeapYear(year);
  3692. //This validation is only applicable for this format.
  3693. //>>includeStart('debug', pragmas.debug);
  3694. if (
  3695. dayOfYear < 1 ||
  3696. (inLeapYear && dayOfYear > 366) ||
  3697. (!inLeapYear && dayOfYear > 365)
  3698. ) {
  3699. throw new Check.DeveloperError(iso8601ErrorMessage);
  3700. }
  3701. //>>includeEnd('debug')
  3702. } else {
  3703. tokens = date.match(matchWeekDate);
  3704. if (tokens !== null) {
  3705. //ISO week date to ordinal date from
  3706. //http://en.wikipedia.org/w/index.php?title=ISO_week_date&oldid=474176775
  3707. year = +tokens[1];
  3708. const weekNumber = +tokens[2];
  3709. const dayOfWeek = +tokens[3] || 0;
  3710. //>>includeStart('debug', pragmas.debug);
  3711. dashCount = date.split("-").length - 1;
  3712. if (
  3713. dashCount > 0 &&
  3714. ((!defaultValue.defined(tokens[3]) && dashCount !== 1) ||
  3715. (defaultValue.defined(tokens[3]) && dashCount !== 2))
  3716. ) {
  3717. throw new Check.DeveloperError(iso8601ErrorMessage);
  3718. }
  3719. //>>includeEnd('debug')
  3720. const january4 = new Date(Date.UTC(year, 0, 4));
  3721. dayOfYear = weekNumber * 7 + dayOfWeek - january4.getUTCDay() - 3;
  3722. } else {
  3723. //None of our regular expressions succeeded in parsing the date properly.
  3724. //>>includeStart('debug', pragmas.debug);
  3725. throw new Check.DeveloperError(iso8601ErrorMessage);
  3726. //>>includeEnd('debug')
  3727. }
  3728. }
  3729. //Split an ordinal date into month/day.
  3730. tmp = new Date(Date.UTC(year, 0, 1));
  3731. tmp.setUTCDate(dayOfYear);
  3732. month = tmp.getUTCMonth() + 1;
  3733. day = tmp.getUTCDate();
  3734. }
  3735. }
  3736. }
  3737. //Now that we have all of the date components, validate them to make sure nothing is out of range.
  3738. inLeapYear = isLeapYear(year);
  3739. //>>includeStart('debug', pragmas.debug);
  3740. if (
  3741. month < 1 ||
  3742. month > 12 ||
  3743. day < 1 ||
  3744. ((month !== 2 || !inLeapYear) && day > daysInMonth[month - 1]) ||
  3745. (inLeapYear && month === 2 && day > daysInLeapFeburary)
  3746. ) {
  3747. throw new Check.DeveloperError(iso8601ErrorMessage);
  3748. }
  3749. //>>includeEnd('debug')
  3750. //Now move onto the time string, which is much simpler.
  3751. //If no time is specified, it is considered the beginning of the day, UTC to match Javascript's implementation.
  3752. let offsetIndex;
  3753. if (defaultValue.defined(time)) {
  3754. tokens = time.match(matchHoursMinutesSeconds);
  3755. if (tokens !== null) {
  3756. //>>includeStart('debug', pragmas.debug);
  3757. dashCount = time.split(":").length - 1;
  3758. if (dashCount > 0 && dashCount !== 2 && dashCount !== 3) {
  3759. throw new Check.DeveloperError(iso8601ErrorMessage);
  3760. }
  3761. //>>includeEnd('debug')
  3762. hour = +tokens[1];
  3763. minute = +tokens[2];
  3764. second = +tokens[3];
  3765. millisecond = +(tokens[4] || 0) * 1000.0;
  3766. offsetIndex = 5;
  3767. } else {
  3768. tokens = time.match(matchHoursMinutes);
  3769. if (tokens !== null) {
  3770. //>>includeStart('debug', pragmas.debug);
  3771. dashCount = time.split(":").length - 1;
  3772. if (dashCount > 2) {
  3773. throw new Check.DeveloperError(iso8601ErrorMessage);
  3774. }
  3775. //>>includeEnd('debug')
  3776. hour = +tokens[1];
  3777. minute = +tokens[2];
  3778. second = +(tokens[3] || 0) * 60.0;
  3779. offsetIndex = 4;
  3780. } else {
  3781. tokens = time.match(matchHours);
  3782. if (tokens !== null) {
  3783. hour = +tokens[1];
  3784. minute = +(tokens[2] || 0) * 60.0;
  3785. offsetIndex = 3;
  3786. } else {
  3787. //>>includeStart('debug', pragmas.debug);
  3788. throw new Check.DeveloperError(iso8601ErrorMessage);
  3789. //>>includeEnd('debug')
  3790. }
  3791. }
  3792. }
  3793. //Validate that all values are in proper range. Minutes and hours have special cases at 60 and 24.
  3794. //>>includeStart('debug', pragmas.debug);
  3795. if (
  3796. minute >= 60 ||
  3797. second >= 61 ||
  3798. hour > 24 ||
  3799. (hour === 24 && (minute > 0 || second > 0 || millisecond > 0))
  3800. ) {
  3801. throw new Check.DeveloperError(iso8601ErrorMessage);
  3802. }
  3803. //>>includeEnd('debug');
  3804. //Check the UTC offset value, if no value exists, use local time
  3805. //a Z indicates UTC, + or - are offsets.
  3806. const offset = tokens[offsetIndex];
  3807. const offsetHours = +tokens[offsetIndex + 1];
  3808. const offsetMinutes = +(tokens[offsetIndex + 2] || 0);
  3809. switch (offset) {
  3810. case "+":
  3811. hour = hour - offsetHours;
  3812. minute = minute - offsetMinutes;
  3813. break;
  3814. case "-":
  3815. hour = hour + offsetHours;
  3816. minute = minute + offsetMinutes;
  3817. break;
  3818. case "Z":
  3819. break;
  3820. default:
  3821. minute =
  3822. minute +
  3823. new Date(
  3824. Date.UTC(year, month - 1, day, hour, minute)
  3825. ).getTimezoneOffset();
  3826. break;
  3827. }
  3828. }
  3829. //ISO8601 denotes a leap second by any time having a seconds component of 60 seconds.
  3830. //If that's the case, we need to temporarily subtract a second in order to build a UTC date.
  3831. //Then we add it back in after converting to TAI.
  3832. const isLeapSecond = second === 60;
  3833. if (isLeapSecond) {
  3834. second--;
  3835. }
  3836. //Even if we successfully parsed the string into its components, after applying UTC offset or
  3837. //special cases like 24:00:00 denoting midnight, we need to normalize the data appropriately.
  3838. //milliseconds can never be greater than 1000, and seconds can't be above 60, so we start with minutes
  3839. while (minute >= 60) {
  3840. minute -= 60;
  3841. hour++;
  3842. }
  3843. while (hour >= 24) {
  3844. hour -= 24;
  3845. day++;
  3846. }
  3847. tmp = inLeapYear && month === 2 ? daysInLeapFeburary : daysInMonth[month - 1];
  3848. while (day > tmp) {
  3849. day -= tmp;
  3850. month++;
  3851. if (month > 12) {
  3852. month -= 12;
  3853. year++;
  3854. }
  3855. tmp =
  3856. inLeapYear && month === 2 ? daysInLeapFeburary : daysInMonth[month - 1];
  3857. }
  3858. //If UTC offset is at the beginning/end of the day, minutes can be negative.
  3859. while (minute < 0) {
  3860. minute += 60;
  3861. hour--;
  3862. }
  3863. while (hour < 0) {
  3864. hour += 24;
  3865. day--;
  3866. }
  3867. while (day < 1) {
  3868. month--;
  3869. if (month < 1) {
  3870. month += 12;
  3871. year--;
  3872. }
  3873. tmp =
  3874. inLeapYear && month === 2 ? daysInLeapFeburary : daysInMonth[month - 1];
  3875. day += tmp;
  3876. }
  3877. //Now create the JulianDate components from the Gregorian date and actually create our instance.
  3878. const components = computeJulianDateComponents(
  3879. year,
  3880. month,
  3881. day,
  3882. hour,
  3883. minute,
  3884. second,
  3885. millisecond
  3886. );
  3887. if (!defaultValue.defined(result)) {
  3888. result = new JulianDate(components[0], components[1], TimeStandard$1.UTC);
  3889. } else {
  3890. setComponents(components[0], components[1], result);
  3891. convertUtcToTai(result);
  3892. }
  3893. //If we were on a leap second, add it back.
  3894. if (isLeapSecond) {
  3895. JulianDate.addSeconds(result, 1, result);
  3896. }
  3897. return result;
  3898. };
  3899. /**
  3900. * Creates a new instance that represents the current system time.
  3901. * This is equivalent to calling <code>JulianDate.fromDate(new Date());</code>.
  3902. *
  3903. * @param {JulianDate} [result] An existing instance to use for the result.
  3904. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  3905. */
  3906. JulianDate.now = function (result) {
  3907. return JulianDate.fromDate(new Date(), result);
  3908. };
  3909. const toGregorianDateScratch = new JulianDate(0, 0, TimeStandard$1.TAI);
  3910. /**
  3911. * Creates a {@link GregorianDate} from the provided instance.
  3912. *
  3913. * @param {JulianDate} julianDate The date to be converted.
  3914. * @param {GregorianDate} [result] An existing instance to use for the result.
  3915. * @returns {GregorianDate} The modified result parameter or a new instance if none was provided.
  3916. */
  3917. JulianDate.toGregorianDate = function (julianDate, result) {
  3918. //>>includeStart('debug', pragmas.debug);
  3919. if (!defaultValue.defined(julianDate)) {
  3920. throw new Check.DeveloperError("julianDate is required.");
  3921. }
  3922. //>>includeEnd('debug');
  3923. let isLeapSecond = false;
  3924. let thisUtc = convertTaiToUtc(julianDate, toGregorianDateScratch);
  3925. if (!defaultValue.defined(thisUtc)) {
  3926. //Conversion to UTC will fail if we are during a leap second.
  3927. //If that's the case, subtract a second and convert again.
  3928. //JavaScript doesn't support leap seconds, so this results in second 59 being repeated twice.
  3929. JulianDate.addSeconds(julianDate, -1, toGregorianDateScratch);
  3930. thisUtc = convertTaiToUtc(toGregorianDateScratch, toGregorianDateScratch);
  3931. isLeapSecond = true;
  3932. }
  3933. let julianDayNumber = thisUtc.dayNumber;
  3934. const secondsOfDay = thisUtc.secondsOfDay;
  3935. if (secondsOfDay >= 43200.0) {
  3936. julianDayNumber += 1;
  3937. }
  3938. // Algorithm from page 604 of the Explanatory Supplement to the
  3939. // Astronomical Almanac (Seidelmann 1992).
  3940. let L = (julianDayNumber + 68569) | 0;
  3941. const N = ((4 * L) / 146097) | 0;
  3942. L = (L - (((146097 * N + 3) / 4) | 0)) | 0;
  3943. const I = ((4000 * (L + 1)) / 1461001) | 0;
  3944. L = (L - (((1461 * I) / 4) | 0) + 31) | 0;
  3945. const J = ((80 * L) / 2447) | 0;
  3946. const day = (L - (((2447 * J) / 80) | 0)) | 0;
  3947. L = (J / 11) | 0;
  3948. const month = (J + 2 - 12 * L) | 0;
  3949. const year = (100 * (N - 49) + I + L) | 0;
  3950. let hour = (secondsOfDay / TimeConstants$1.SECONDS_PER_HOUR) | 0;
  3951. let remainingSeconds = secondsOfDay - hour * TimeConstants$1.SECONDS_PER_HOUR;
  3952. const minute = (remainingSeconds / TimeConstants$1.SECONDS_PER_MINUTE) | 0;
  3953. remainingSeconds =
  3954. remainingSeconds - minute * TimeConstants$1.SECONDS_PER_MINUTE;
  3955. let second = remainingSeconds | 0;
  3956. const millisecond =
  3957. (remainingSeconds - second) / TimeConstants$1.SECONDS_PER_MILLISECOND;
  3958. // JulianDates are noon-based
  3959. hour += 12;
  3960. if (hour > 23) {
  3961. hour -= 24;
  3962. }
  3963. //If we were on a leap second, add it back.
  3964. if (isLeapSecond) {
  3965. second += 1;
  3966. }
  3967. if (!defaultValue.defined(result)) {
  3968. return new GregorianDate(
  3969. year,
  3970. month,
  3971. day,
  3972. hour,
  3973. minute,
  3974. second,
  3975. millisecond,
  3976. isLeapSecond
  3977. );
  3978. }
  3979. result.year = year;
  3980. result.month = month;
  3981. result.day = day;
  3982. result.hour = hour;
  3983. result.minute = minute;
  3984. result.second = second;
  3985. result.millisecond = millisecond;
  3986. result.isLeapSecond = isLeapSecond;
  3987. return result;
  3988. };
  3989. /**
  3990. * Creates a JavaScript Date from the provided instance.
  3991. * Since JavaScript dates are only accurate to the nearest millisecond and
  3992. * cannot represent a leap second, consider using {@link JulianDate.toGregorianDate} instead.
  3993. * If the provided JulianDate is during a leap second, the previous second is used.
  3994. *
  3995. * @param {JulianDate} julianDate The date to be converted.
  3996. * @returns {Date} A new instance representing the provided date.
  3997. */
  3998. JulianDate.toDate = function (julianDate) {
  3999. //>>includeStart('debug', pragmas.debug);
  4000. if (!defaultValue.defined(julianDate)) {
  4001. throw new Check.DeveloperError("julianDate is required.");
  4002. }
  4003. //>>includeEnd('debug');
  4004. const gDate = JulianDate.toGregorianDate(julianDate, gregorianDateScratch);
  4005. let second = gDate.second;
  4006. if (gDate.isLeapSecond) {
  4007. second -= 1;
  4008. }
  4009. return new Date(
  4010. Date.UTC(
  4011. gDate.year,
  4012. gDate.month - 1,
  4013. gDate.day,
  4014. gDate.hour,
  4015. gDate.minute,
  4016. second,
  4017. gDate.millisecond
  4018. )
  4019. );
  4020. };
  4021. /**
  4022. * Creates an ISO8601 representation of the provided date.
  4023. *
  4024. * @param {JulianDate} julianDate The date to be converted.
  4025. * @param {number} [precision] The number of fractional digits used to represent the seconds component. By default, the most precise representation is used.
  4026. * @returns {string} The ISO8601 representation of the provided date.
  4027. */
  4028. JulianDate.toIso8601 = function (julianDate, precision) {
  4029. //>>includeStart('debug', pragmas.debug);
  4030. if (!defaultValue.defined(julianDate)) {
  4031. throw new Check.DeveloperError("julianDate is required.");
  4032. }
  4033. //>>includeEnd('debug');
  4034. const gDate = JulianDate.toGregorianDate(julianDate, gregorianDateScratch);
  4035. let year = gDate.year;
  4036. let month = gDate.month;
  4037. let day = gDate.day;
  4038. let hour = gDate.hour;
  4039. const minute = gDate.minute;
  4040. const second = gDate.second;
  4041. const millisecond = gDate.millisecond;
  4042. // special case - Iso8601.MAXIMUM_VALUE produces a string which we can't parse unless we adjust.
  4043. // 10000-01-01T00:00:00 is the same instant as 9999-12-31T24:00:00
  4044. if (
  4045. year === 10000 &&
  4046. month === 1 &&
  4047. day === 1 &&
  4048. hour === 0 &&
  4049. minute === 0 &&
  4050. second === 0 &&
  4051. millisecond === 0
  4052. ) {
  4053. year = 9999;
  4054. month = 12;
  4055. day = 31;
  4056. hour = 24;
  4057. }
  4058. let millisecondStr;
  4059. if (!defaultValue.defined(precision) && millisecond !== 0) {
  4060. //Forces milliseconds into a number with at least 3 digits to whatever the default toString() precision is.
  4061. millisecondStr = (millisecond * 0.01).toString().replace(".", "");
  4062. return `${year.toString().padStart(4, "0")}-${month
  4063. .toString()
  4064. .padStart(2, "0")}-${day
  4065. .toString()
  4066. .padStart(2, "0")}T${hour
  4067. .toString()
  4068. .padStart(2, "0")}:${minute
  4069. .toString()
  4070. .padStart(2, "0")}:${second
  4071. .toString()
  4072. .padStart(2, "0")}.${millisecondStr}Z`;
  4073. }
  4074. //Precision is either 0 or milliseconds is 0 with undefined precision, in either case, leave off milliseconds entirely
  4075. if (!defaultValue.defined(precision) || precision === 0) {
  4076. return `${year.toString().padStart(4, "0")}-${month
  4077. .toString()
  4078. .padStart(2, "0")}-${day
  4079. .toString()
  4080. .padStart(2, "0")}T${hour
  4081. .toString()
  4082. .padStart(2, "0")}:${minute
  4083. .toString()
  4084. .padStart(2, "0")}:${second.toString().padStart(2, "0")}Z`;
  4085. }
  4086. //Forces milliseconds into a number with at least 3 digits to whatever the specified precision is.
  4087. millisecondStr = (millisecond * 0.01)
  4088. .toFixed(precision)
  4089. .replace(".", "")
  4090. .slice(0, precision);
  4091. return `${year.toString().padStart(4, "0")}-${month
  4092. .toString()
  4093. .padStart(2, "0")}-${day
  4094. .toString()
  4095. .padStart(2, "0")}T${hour
  4096. .toString()
  4097. .padStart(2, "0")}:${minute
  4098. .toString()
  4099. .padStart(2, "0")}:${second
  4100. .toString()
  4101. .padStart(2, "0")}.${millisecondStr}Z`;
  4102. };
  4103. /**
  4104. * Duplicates a JulianDate instance.
  4105. *
  4106. * @param {JulianDate} julianDate The date to duplicate.
  4107. * @param {JulianDate} [result] An existing instance to use for the result.
  4108. * @returns {JulianDate} The modified result parameter or a new instance if none was provided. Returns undefined if julianDate is undefined.
  4109. */
  4110. JulianDate.clone = function (julianDate, result) {
  4111. if (!defaultValue.defined(julianDate)) {
  4112. return undefined;
  4113. }
  4114. if (!defaultValue.defined(result)) {
  4115. return new JulianDate(
  4116. julianDate.dayNumber,
  4117. julianDate.secondsOfDay,
  4118. TimeStandard$1.TAI
  4119. );
  4120. }
  4121. result.dayNumber = julianDate.dayNumber;
  4122. result.secondsOfDay = julianDate.secondsOfDay;
  4123. return result;
  4124. };
  4125. /**
  4126. * Compares two instances.
  4127. *
  4128. * @param {JulianDate} left The first instance.
  4129. * @param {JulianDate} right The second instance.
  4130. * @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.
  4131. */
  4132. JulianDate.compare = function (left, right) {
  4133. //>>includeStart('debug', pragmas.debug);
  4134. if (!defaultValue.defined(left)) {
  4135. throw new Check.DeveloperError("left is required.");
  4136. }
  4137. if (!defaultValue.defined(right)) {
  4138. throw new Check.DeveloperError("right is required.");
  4139. }
  4140. //>>includeEnd('debug');
  4141. const julianDayNumberDifference = left.dayNumber - right.dayNumber;
  4142. if (julianDayNumberDifference !== 0) {
  4143. return julianDayNumberDifference;
  4144. }
  4145. return left.secondsOfDay - right.secondsOfDay;
  4146. };
  4147. /**
  4148. * Compares two instances and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  4149. *
  4150. * @param {JulianDate} [left] The first instance.
  4151. * @param {JulianDate} [right] The second instance.
  4152. * @returns {boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  4153. */
  4154. JulianDate.equals = function (left, right) {
  4155. return (
  4156. left === right ||
  4157. (defaultValue.defined(left) &&
  4158. defaultValue.defined(right) &&
  4159. left.dayNumber === right.dayNumber &&
  4160. left.secondsOfDay === right.secondsOfDay)
  4161. );
  4162. };
  4163. /**
  4164. * Compares two instances and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  4165. * each other. That is, in order for the dates to be considered equal (and for
  4166. * this function to return <code>true</code>), the absolute value of the difference between them, in
  4167. * seconds, must be less than <code>epsilon</code>.
  4168. *
  4169. * @param {JulianDate} [left] The first instance.
  4170. * @param {JulianDate} [right] The second instance.
  4171. * @param {number} [epsilon=0] The maximum number of seconds that should separate the two instances.
  4172. * @returns {boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  4173. */
  4174. JulianDate.equalsEpsilon = function (left, right, epsilon) {
  4175. epsilon = defaultValue.defaultValue(epsilon, 0);
  4176. return (
  4177. left === right ||
  4178. (defaultValue.defined(left) &&
  4179. defaultValue.defined(right) &&
  4180. Math.abs(JulianDate.secondsDifference(left, right)) <= epsilon)
  4181. );
  4182. };
  4183. /**
  4184. * Computes the total number of whole and fractional days represented by the provided instance.
  4185. *
  4186. * @param {JulianDate} julianDate The date.
  4187. * @returns {number} The Julian date as single floating point number.
  4188. */
  4189. JulianDate.totalDays = function (julianDate) {
  4190. //>>includeStart('debug', pragmas.debug);
  4191. if (!defaultValue.defined(julianDate)) {
  4192. throw new Check.DeveloperError("julianDate is required.");
  4193. }
  4194. //>>includeEnd('debug');
  4195. return (
  4196. julianDate.dayNumber +
  4197. julianDate.secondsOfDay / TimeConstants$1.SECONDS_PER_DAY
  4198. );
  4199. };
  4200. /**
  4201. * Computes the difference in seconds between the provided instance.
  4202. *
  4203. * @param {JulianDate} left The first instance.
  4204. * @param {JulianDate} right The second instance.
  4205. * @returns {number} The difference, in seconds, when subtracting <code>right</code> from <code>left</code>.
  4206. */
  4207. JulianDate.secondsDifference = function (left, right) {
  4208. //>>includeStart('debug', pragmas.debug);
  4209. if (!defaultValue.defined(left)) {
  4210. throw new Check.DeveloperError("left is required.");
  4211. }
  4212. if (!defaultValue.defined(right)) {
  4213. throw new Check.DeveloperError("right is required.");
  4214. }
  4215. //>>includeEnd('debug');
  4216. const dayDifference =
  4217. (left.dayNumber - right.dayNumber) * TimeConstants$1.SECONDS_PER_DAY;
  4218. return dayDifference + (left.secondsOfDay - right.secondsOfDay);
  4219. };
  4220. /**
  4221. * Computes the difference in days between the provided instance.
  4222. *
  4223. * @param {JulianDate} left The first instance.
  4224. * @param {JulianDate} right The second instance.
  4225. * @returns {number} The difference, in days, when subtracting <code>right</code> from <code>left</code>.
  4226. */
  4227. JulianDate.daysDifference = function (left, right) {
  4228. //>>includeStart('debug', pragmas.debug);
  4229. if (!defaultValue.defined(left)) {
  4230. throw new Check.DeveloperError("left is required.");
  4231. }
  4232. if (!defaultValue.defined(right)) {
  4233. throw new Check.DeveloperError("right is required.");
  4234. }
  4235. //>>includeEnd('debug');
  4236. const dayDifference = left.dayNumber - right.dayNumber;
  4237. const secondDifference =
  4238. (left.secondsOfDay - right.secondsOfDay) / TimeConstants$1.SECONDS_PER_DAY;
  4239. return dayDifference + secondDifference;
  4240. };
  4241. /**
  4242. * Computes the number of seconds the provided instance is ahead of UTC.
  4243. *
  4244. * @param {JulianDate} julianDate The date.
  4245. * @returns {number} The number of seconds the provided instance is ahead of UTC
  4246. */
  4247. JulianDate.computeTaiMinusUtc = function (julianDate) {
  4248. binarySearchScratchLeapSecond.julianDate = julianDate;
  4249. const leapSeconds = JulianDate.leapSeconds;
  4250. let index = binarySearch(
  4251. leapSeconds,
  4252. binarySearchScratchLeapSecond,
  4253. compareLeapSecondDates$1
  4254. );
  4255. if (index < 0) {
  4256. index = ~index;
  4257. --index;
  4258. if (index < 0) {
  4259. index = 0;
  4260. }
  4261. }
  4262. return leapSeconds[index].offset;
  4263. };
  4264. /**
  4265. * Adds the provided number of seconds to the provided date instance.
  4266. *
  4267. * @param {JulianDate} julianDate The date.
  4268. * @param {number} seconds The number of seconds to add or subtract.
  4269. * @param {JulianDate} result An existing instance to use for the result.
  4270. * @returns {JulianDate} The modified result parameter.
  4271. */
  4272. JulianDate.addSeconds = function (julianDate, seconds, result) {
  4273. //>>includeStart('debug', pragmas.debug);
  4274. if (!defaultValue.defined(julianDate)) {
  4275. throw new Check.DeveloperError("julianDate is required.");
  4276. }
  4277. if (!defaultValue.defined(seconds)) {
  4278. throw new Check.DeveloperError("seconds is required.");
  4279. }
  4280. if (!defaultValue.defined(result)) {
  4281. throw new Check.DeveloperError("result is required.");
  4282. }
  4283. //>>includeEnd('debug');
  4284. return setComponents(
  4285. julianDate.dayNumber,
  4286. julianDate.secondsOfDay + seconds,
  4287. result
  4288. );
  4289. };
  4290. /**
  4291. * Adds the provided number of minutes to the provided date instance.
  4292. *
  4293. * @param {JulianDate} julianDate The date.
  4294. * @param {number} minutes The number of minutes to add or subtract.
  4295. * @param {JulianDate} result An existing instance to use for the result.
  4296. * @returns {JulianDate} The modified result parameter.
  4297. */
  4298. JulianDate.addMinutes = function (julianDate, minutes, result) {
  4299. //>>includeStart('debug', pragmas.debug);
  4300. if (!defaultValue.defined(julianDate)) {
  4301. throw new Check.DeveloperError("julianDate is required.");
  4302. }
  4303. if (!defaultValue.defined(minutes)) {
  4304. throw new Check.DeveloperError("minutes is required.");
  4305. }
  4306. if (!defaultValue.defined(result)) {
  4307. throw new Check.DeveloperError("result is required.");
  4308. }
  4309. //>>includeEnd('debug');
  4310. const newSecondsOfDay =
  4311. julianDate.secondsOfDay + minutes * TimeConstants$1.SECONDS_PER_MINUTE;
  4312. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  4313. };
  4314. /**
  4315. * Adds the provided number of hours to the provided date instance.
  4316. *
  4317. * @param {JulianDate} julianDate The date.
  4318. * @param {number} hours The number of hours to add or subtract.
  4319. * @param {JulianDate} result An existing instance to use for the result.
  4320. * @returns {JulianDate} The modified result parameter.
  4321. */
  4322. JulianDate.addHours = function (julianDate, hours, result) {
  4323. //>>includeStart('debug', pragmas.debug);
  4324. if (!defaultValue.defined(julianDate)) {
  4325. throw new Check.DeveloperError("julianDate is required.");
  4326. }
  4327. if (!defaultValue.defined(hours)) {
  4328. throw new Check.DeveloperError("hours is required.");
  4329. }
  4330. if (!defaultValue.defined(result)) {
  4331. throw new Check.DeveloperError("result is required.");
  4332. }
  4333. //>>includeEnd('debug');
  4334. const newSecondsOfDay =
  4335. julianDate.secondsOfDay + hours * TimeConstants$1.SECONDS_PER_HOUR;
  4336. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  4337. };
  4338. /**
  4339. * Adds the provided number of days to the provided date instance.
  4340. *
  4341. * @param {JulianDate} julianDate The date.
  4342. * @param {number} days The number of days to add or subtract.
  4343. * @param {JulianDate} result An existing instance to use for the result.
  4344. * @returns {JulianDate} The modified result parameter.
  4345. */
  4346. JulianDate.addDays = function (julianDate, days, result) {
  4347. //>>includeStart('debug', pragmas.debug);
  4348. if (!defaultValue.defined(julianDate)) {
  4349. throw new Check.DeveloperError("julianDate is required.");
  4350. }
  4351. if (!defaultValue.defined(days)) {
  4352. throw new Check.DeveloperError("days is required.");
  4353. }
  4354. if (!defaultValue.defined(result)) {
  4355. throw new Check.DeveloperError("result is required.");
  4356. }
  4357. //>>includeEnd('debug');
  4358. const newJulianDayNumber = julianDate.dayNumber + days;
  4359. return setComponents(newJulianDayNumber, julianDate.secondsOfDay, result);
  4360. };
  4361. /**
  4362. * Compares the provided instances and returns <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  4363. *
  4364. * @param {JulianDate} left The first instance.
  4365. * @param {JulianDate} right The second instance.
  4366. * @returns {boolean} <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  4367. */
  4368. JulianDate.lessThan = function (left, right) {
  4369. return JulianDate.compare(left, right) < 0;
  4370. };
  4371. /**
  4372. * 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.
  4373. *
  4374. * @param {JulianDate} left The first instance.
  4375. * @param {JulianDate} right The second instance.
  4376. * @returns {boolean} <code>true</code> if <code>left</code> is earlier than or equal to <code>right</code>, <code>false</code> otherwise.
  4377. */
  4378. JulianDate.lessThanOrEquals = function (left, right) {
  4379. return JulianDate.compare(left, right) <= 0;
  4380. };
  4381. /**
  4382. * Compares the provided instances and returns <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  4383. *
  4384. * @param {JulianDate} left The first instance.
  4385. * @param {JulianDate} right The second instance.
  4386. * @returns {boolean} <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  4387. */
  4388. JulianDate.greaterThan = function (left, right) {
  4389. return JulianDate.compare(left, right) > 0;
  4390. };
  4391. /**
  4392. * 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.
  4393. *
  4394. * @param {JulianDate} left The first instance.
  4395. * @param {JulianDate} right The second instance.
  4396. * @returns {boolean} <code>true</code> if <code>left</code> is later than or equal to <code>right</code>, <code>false</code> otherwise.
  4397. */
  4398. JulianDate.greaterThanOrEquals = function (left, right) {
  4399. return JulianDate.compare(left, right) >= 0;
  4400. };
  4401. /**
  4402. * Duplicates this instance.
  4403. *
  4404. * @param {JulianDate} [result] An existing instance to use for the result.
  4405. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  4406. */
  4407. JulianDate.prototype.clone = function (result) {
  4408. return JulianDate.clone(this, result);
  4409. };
  4410. /**
  4411. * Compares this and the provided instance and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  4412. *
  4413. * @param {JulianDate} [right] The second instance.
  4414. * @returns {boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  4415. */
  4416. JulianDate.prototype.equals = function (right) {
  4417. return JulianDate.equals(this, right);
  4418. };
  4419. /**
  4420. * Compares this and the provided instance and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  4421. * each other. That is, in order for the dates to be considered equal (and for
  4422. * this function to return <code>true</code>), the absolute value of the difference between them, in
  4423. * seconds, must be less than <code>epsilon</code>.
  4424. *
  4425. * @param {JulianDate} [right] The second instance.
  4426. * @param {number} [epsilon=0] The maximum number of seconds that should separate the two instances.
  4427. * @returns {boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  4428. */
  4429. JulianDate.prototype.equalsEpsilon = function (right, epsilon) {
  4430. return JulianDate.equalsEpsilon(this, right, epsilon);
  4431. };
  4432. /**
  4433. * Creates a string representing this date in ISO8601 format.
  4434. *
  4435. * @returns {string} A string representing this date in ISO8601 format.
  4436. */
  4437. JulianDate.prototype.toString = function () {
  4438. return JulianDate.toIso8601(this);
  4439. };
  4440. /**
  4441. * Gets or sets the list of leap seconds used throughout Cesium.
  4442. * @memberof JulianDate
  4443. * @type {LeapSecond[]}
  4444. */
  4445. JulianDate.leapSeconds = [
  4446. new LeapSecond(new JulianDate(2441317, 43210.0, TimeStandard$1.TAI), 10), // January 1, 1972 00:00:00 UTC
  4447. new LeapSecond(new JulianDate(2441499, 43211.0, TimeStandard$1.TAI), 11), // July 1, 1972 00:00:00 UTC
  4448. new LeapSecond(new JulianDate(2441683, 43212.0, TimeStandard$1.TAI), 12), // January 1, 1973 00:00:00 UTC
  4449. new LeapSecond(new JulianDate(2442048, 43213.0, TimeStandard$1.TAI), 13), // January 1, 1974 00:00:00 UTC
  4450. new LeapSecond(new JulianDate(2442413, 43214.0, TimeStandard$1.TAI), 14), // January 1, 1975 00:00:00 UTC
  4451. new LeapSecond(new JulianDate(2442778, 43215.0, TimeStandard$1.TAI), 15), // January 1, 1976 00:00:00 UTC
  4452. new LeapSecond(new JulianDate(2443144, 43216.0, TimeStandard$1.TAI), 16), // January 1, 1977 00:00:00 UTC
  4453. new LeapSecond(new JulianDate(2443509, 43217.0, TimeStandard$1.TAI), 17), // January 1, 1978 00:00:00 UTC
  4454. new LeapSecond(new JulianDate(2443874, 43218.0, TimeStandard$1.TAI), 18), // January 1, 1979 00:00:00 UTC
  4455. new LeapSecond(new JulianDate(2444239, 43219.0, TimeStandard$1.TAI), 19), // January 1, 1980 00:00:00 UTC
  4456. new LeapSecond(new JulianDate(2444786, 43220.0, TimeStandard$1.TAI), 20), // July 1, 1981 00:00:00 UTC
  4457. new LeapSecond(new JulianDate(2445151, 43221.0, TimeStandard$1.TAI), 21), // July 1, 1982 00:00:00 UTC
  4458. new LeapSecond(new JulianDate(2445516, 43222.0, TimeStandard$1.TAI), 22), // July 1, 1983 00:00:00 UTC
  4459. new LeapSecond(new JulianDate(2446247, 43223.0, TimeStandard$1.TAI), 23), // July 1, 1985 00:00:00 UTC
  4460. new LeapSecond(new JulianDate(2447161, 43224.0, TimeStandard$1.TAI), 24), // January 1, 1988 00:00:00 UTC
  4461. new LeapSecond(new JulianDate(2447892, 43225.0, TimeStandard$1.TAI), 25), // January 1, 1990 00:00:00 UTC
  4462. new LeapSecond(new JulianDate(2448257, 43226.0, TimeStandard$1.TAI), 26), // January 1, 1991 00:00:00 UTC
  4463. new LeapSecond(new JulianDate(2448804, 43227.0, TimeStandard$1.TAI), 27), // July 1, 1992 00:00:00 UTC
  4464. new LeapSecond(new JulianDate(2449169, 43228.0, TimeStandard$1.TAI), 28), // July 1, 1993 00:00:00 UTC
  4465. new LeapSecond(new JulianDate(2449534, 43229.0, TimeStandard$1.TAI), 29), // July 1, 1994 00:00:00 UTC
  4466. new LeapSecond(new JulianDate(2450083, 43230.0, TimeStandard$1.TAI), 30), // January 1, 1996 00:00:00 UTC
  4467. new LeapSecond(new JulianDate(2450630, 43231.0, TimeStandard$1.TAI), 31), // July 1, 1997 00:00:00 UTC
  4468. new LeapSecond(new JulianDate(2451179, 43232.0, TimeStandard$1.TAI), 32), // January 1, 1999 00:00:00 UTC
  4469. new LeapSecond(new JulianDate(2453736, 43233.0, TimeStandard$1.TAI), 33), // January 1, 2006 00:00:00 UTC
  4470. new LeapSecond(new JulianDate(2454832, 43234.0, TimeStandard$1.TAI), 34), // January 1, 2009 00:00:00 UTC
  4471. new LeapSecond(new JulianDate(2456109, 43235.0, TimeStandard$1.TAI), 35), // July 1, 2012 00:00:00 UTC
  4472. new LeapSecond(new JulianDate(2457204, 43236.0, TimeStandard$1.TAI), 36), // July 1, 2015 00:00:00 UTC
  4473. new LeapSecond(new JulianDate(2457754, 43237.0, TimeStandard$1.TAI), 37), // January 1, 2017 00:00:00 UTC
  4474. ];
  4475. var URI = {exports: {}};
  4476. var punycode = {exports: {}};
  4477. /*! https://mths.be/punycode v1.4.0 by @mathias */
  4478. punycode.exports;
  4479. var hasRequiredPunycode;
  4480. function requirePunycode () {
  4481. if (hasRequiredPunycode) return punycode.exports;
  4482. hasRequiredPunycode = 1;
  4483. (function (module, exports) {
  4484. (function(root) {
  4485. /** Detect free variables */
  4486. var freeExports = exports &&
  4487. !exports.nodeType && exports;
  4488. var freeModule = module &&
  4489. !module.nodeType && module;
  4490. var freeGlobal = typeof Math$1.commonjsGlobal == 'object' && Math$1.commonjsGlobal;
  4491. if (
  4492. freeGlobal.global === freeGlobal ||
  4493. freeGlobal.window === freeGlobal ||
  4494. freeGlobal.self === freeGlobal
  4495. ) {
  4496. root = freeGlobal;
  4497. }
  4498. /**
  4499. * The `punycode` object.
  4500. * @name punycode
  4501. * @type Object
  4502. */
  4503. var punycode,
  4504. /** Highest positive signed 32-bit float value */
  4505. maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1
  4506. /** Bootstring parameters */
  4507. base = 36,
  4508. tMin = 1,
  4509. tMax = 26,
  4510. skew = 38,
  4511. damp = 700,
  4512. initialBias = 72,
  4513. initialN = 128, // 0x80
  4514. delimiter = '-', // '\x2D'
  4515. /** Regular expressions */
  4516. regexPunycode = /^xn--/,
  4517. regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars
  4518. regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators
  4519. /** Error messages */
  4520. errors = {
  4521. 'overflow': 'Overflow: input needs wider integers to process',
  4522. 'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
  4523. 'invalid-input': 'Invalid input'
  4524. },
  4525. /** Convenience shortcuts */
  4526. baseMinusTMin = base - tMin,
  4527. floor = Math.floor,
  4528. stringFromCharCode = String.fromCharCode,
  4529. /** Temporary variable */
  4530. key;
  4531. /*--------------------------------------------------------------------------*/
  4532. /**
  4533. * A generic error utility function.
  4534. * @private
  4535. * @param {String} type The error type.
  4536. * @returns {Error} Throws a `RangeError` with the applicable error message.
  4537. */
  4538. function error(type) {
  4539. throw new RangeError(errors[type]);
  4540. }
  4541. /**
  4542. * A generic `Array#map` utility function.
  4543. * @private
  4544. * @param {Array} array The array to iterate over.
  4545. * @param {Function} callback The function that gets called for every array
  4546. * item.
  4547. * @returns {Array} A new array of values returned by the callback function.
  4548. */
  4549. function map(array, fn) {
  4550. var length = array.length;
  4551. var result = [];
  4552. while (length--) {
  4553. result[length] = fn(array[length]);
  4554. }
  4555. return result;
  4556. }
  4557. /**
  4558. * A simple `Array#map`-like wrapper to work with domain name strings or email
  4559. * addresses.
  4560. * @private
  4561. * @param {String} domain The domain name or email address.
  4562. * @param {Function} callback The function that gets called for every
  4563. * character.
  4564. * @returns {Array} A new string of characters returned by the callback
  4565. * function.
  4566. */
  4567. function mapDomain(string, fn) {
  4568. var parts = string.split('@');
  4569. var result = '';
  4570. if (parts.length > 1) {
  4571. // In email addresses, only the domain name should be punycoded. Leave
  4572. // the local part (i.e. everything up to `@`) intact.
  4573. result = parts[0] + '@';
  4574. string = parts[1];
  4575. }
  4576. // Avoid `split(regex)` for IE8 compatibility. See #17.
  4577. string = string.replace(regexSeparators, '\x2E');
  4578. var labels = string.split('.');
  4579. var encoded = map(labels, fn).join('.');
  4580. return result + encoded;
  4581. }
  4582. /**
  4583. * Creates an array containing the numeric code points of each Unicode
  4584. * character in the string. While JavaScript uses UCS-2 internally,
  4585. * this function will convert a pair of surrogate halves (each of which
  4586. * UCS-2 exposes as separate characters) into a single code point,
  4587. * matching UTF-16.
  4588. * @see `punycode.ucs2.encode`
  4589. * @see <https://mathiasbynens.be/notes/javascript-encoding>
  4590. * @memberOf punycode.ucs2
  4591. * @name decode
  4592. * @param {String} string The Unicode input string (UCS-2).
  4593. * @returns {Array} The new array of code points.
  4594. */
  4595. function ucs2decode(string) {
  4596. var output = [],
  4597. counter = 0,
  4598. length = string.length,
  4599. value,
  4600. extra;
  4601. while (counter < length) {
  4602. value = string.charCodeAt(counter++);
  4603. if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
  4604. // high surrogate, and there is a next character
  4605. extra = string.charCodeAt(counter++);
  4606. if ((extra & 0xFC00) == 0xDC00) { // low surrogate
  4607. output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  4608. } else {
  4609. // unmatched surrogate; only append this code unit, in case the next
  4610. // code unit is the high surrogate of a surrogate pair
  4611. output.push(value);
  4612. counter--;
  4613. }
  4614. } else {
  4615. output.push(value);
  4616. }
  4617. }
  4618. return output;
  4619. }
  4620. /**
  4621. * Creates a string based on an array of numeric code points.
  4622. * @see `punycode.ucs2.decode`
  4623. * @memberOf punycode.ucs2
  4624. * @name encode
  4625. * @param {Array} codePoints The array of numeric code points.
  4626. * @returns {String} The new Unicode string (UCS-2).
  4627. */
  4628. function ucs2encode(array) {
  4629. return map(array, function(value) {
  4630. var output = '';
  4631. if (value > 0xFFFF) {
  4632. value -= 0x10000;
  4633. output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
  4634. value = 0xDC00 | value & 0x3FF;
  4635. }
  4636. output += stringFromCharCode(value);
  4637. return output;
  4638. }).join('');
  4639. }
  4640. /**
  4641. * Converts a basic code point into a digit/integer.
  4642. * @see `digitToBasic()`
  4643. * @private
  4644. * @param {Number} codePoint The basic numeric code point value.
  4645. * @returns {Number} The numeric value of a basic code point (for use in
  4646. * representing integers) in the range `0` to `base - 1`, or `base` if
  4647. * the code point does not represent a value.
  4648. */
  4649. function basicToDigit(codePoint) {
  4650. if (codePoint - 48 < 10) {
  4651. return codePoint - 22;
  4652. }
  4653. if (codePoint - 65 < 26) {
  4654. return codePoint - 65;
  4655. }
  4656. if (codePoint - 97 < 26) {
  4657. return codePoint - 97;
  4658. }
  4659. return base;
  4660. }
  4661. /**
  4662. * Converts a digit/integer into a basic code point.
  4663. * @see `basicToDigit()`
  4664. * @private
  4665. * @param {Number} digit The numeric value of a basic code point.
  4666. * @returns {Number} The basic code point whose value (when used for
  4667. * representing integers) is `digit`, which needs to be in the range
  4668. * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
  4669. * used; else, the lowercase form is used. The behavior is undefined
  4670. * if `flag` is non-zero and `digit` has no uppercase form.
  4671. */
  4672. function digitToBasic(digit, flag) {
  4673. // 0..25 map to ASCII a..z or A..Z
  4674. // 26..35 map to ASCII 0..9
  4675. return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
  4676. }
  4677. /**
  4678. * Bias adaptation function as per section 3.4 of RFC 3492.
  4679. * https://tools.ietf.org/html/rfc3492#section-3.4
  4680. * @private
  4681. */
  4682. function adapt(delta, numPoints, firstTime) {
  4683. var k = 0;
  4684. delta = firstTime ? floor(delta / damp) : delta >> 1;
  4685. delta += floor(delta / numPoints);
  4686. for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) {
  4687. delta = floor(delta / baseMinusTMin);
  4688. }
  4689. return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
  4690. }
  4691. /**
  4692. * Converts a Punycode string of ASCII-only symbols to a string of Unicode
  4693. * symbols.
  4694. * @memberOf punycode
  4695. * @param {String} input The Punycode string of ASCII-only symbols.
  4696. * @returns {String} The resulting string of Unicode symbols.
  4697. */
  4698. function decode(input) {
  4699. // Don't use UCS-2
  4700. var output = [],
  4701. inputLength = input.length,
  4702. out,
  4703. i = 0,
  4704. n = initialN,
  4705. bias = initialBias,
  4706. basic,
  4707. j,
  4708. index,
  4709. oldi,
  4710. w,
  4711. k,
  4712. digit,
  4713. t,
  4714. /** Cached calculation results */
  4715. baseMinusT;
  4716. // Handle the basic code points: let `basic` be the number of input code
  4717. // points before the last delimiter, or `0` if there is none, then copy
  4718. // the first basic code points to the output.
  4719. basic = input.lastIndexOf(delimiter);
  4720. if (basic < 0) {
  4721. basic = 0;
  4722. }
  4723. for (j = 0; j < basic; ++j) {
  4724. // if it's not a basic code point
  4725. if (input.charCodeAt(j) >= 0x80) {
  4726. error('not-basic');
  4727. }
  4728. output.push(input.charCodeAt(j));
  4729. }
  4730. // Main decoding loop: start just after the last delimiter if any basic code
  4731. // points were copied; start at the beginning otherwise.
  4732. for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) {
  4733. // `index` is the index of the next character to be consumed.
  4734. // Decode a generalized variable-length integer into `delta`,
  4735. // which gets added to `i`. The overflow checking is easier
  4736. // if we increase `i` as we go, then subtract off its starting
  4737. // value at the end to obtain `delta`.
  4738. for (oldi = i, w = 1, k = base; /* no condition */; k += base) {
  4739. if (index >= inputLength) {
  4740. error('invalid-input');
  4741. }
  4742. digit = basicToDigit(input.charCodeAt(index++));
  4743. if (digit >= base || digit > floor((maxInt - i) / w)) {
  4744. error('overflow');
  4745. }
  4746. i += digit * w;
  4747. t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
  4748. if (digit < t) {
  4749. break;
  4750. }
  4751. baseMinusT = base - t;
  4752. if (w > floor(maxInt / baseMinusT)) {
  4753. error('overflow');
  4754. }
  4755. w *= baseMinusT;
  4756. }
  4757. out = output.length + 1;
  4758. bias = adapt(i - oldi, out, oldi == 0);
  4759. // `i` was supposed to wrap around from `out` to `0`,
  4760. // incrementing `n` each time, so we'll fix that now:
  4761. if (floor(i / out) > maxInt - n) {
  4762. error('overflow');
  4763. }
  4764. n += floor(i / out);
  4765. i %= out;
  4766. // Insert `n` at position `i` of the output
  4767. output.splice(i++, 0, n);
  4768. }
  4769. return ucs2encode(output);
  4770. }
  4771. /**
  4772. * Converts a string of Unicode symbols (e.g. a domain name label) to a
  4773. * Punycode string of ASCII-only symbols.
  4774. * @memberOf punycode
  4775. * @param {String} input The string of Unicode symbols.
  4776. * @returns {String} The resulting Punycode string of ASCII-only symbols.
  4777. */
  4778. function encode(input) {
  4779. var n,
  4780. delta,
  4781. handledCPCount,
  4782. basicLength,
  4783. bias,
  4784. j,
  4785. m,
  4786. q,
  4787. k,
  4788. t,
  4789. currentValue,
  4790. output = [],
  4791. /** `inputLength` will hold the number of code points in `input`. */
  4792. inputLength,
  4793. /** Cached calculation results */
  4794. handledCPCountPlusOne,
  4795. baseMinusT,
  4796. qMinusT;
  4797. // Convert the input in UCS-2 to Unicode
  4798. input = ucs2decode(input);
  4799. // Cache the length
  4800. inputLength = input.length;
  4801. // Initialize the state
  4802. n = initialN;
  4803. delta = 0;
  4804. bias = initialBias;
  4805. // Handle the basic code points
  4806. for (j = 0; j < inputLength; ++j) {
  4807. currentValue = input[j];
  4808. if (currentValue < 0x80) {
  4809. output.push(stringFromCharCode(currentValue));
  4810. }
  4811. }
  4812. handledCPCount = basicLength = output.length;
  4813. // `handledCPCount` is the number of code points that have been handled;
  4814. // `basicLength` is the number of basic code points.
  4815. // Finish the basic string - if it is not empty - with a delimiter
  4816. if (basicLength) {
  4817. output.push(delimiter);
  4818. }
  4819. // Main encoding loop:
  4820. while (handledCPCount < inputLength) {
  4821. // All non-basic code points < n have been handled already. Find the next
  4822. // larger one:
  4823. for (m = maxInt, j = 0; j < inputLength; ++j) {
  4824. currentValue = input[j];
  4825. if (currentValue >= n && currentValue < m) {
  4826. m = currentValue;
  4827. }
  4828. }
  4829. // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>,
  4830. // but guard against overflow
  4831. handledCPCountPlusOne = handledCPCount + 1;
  4832. if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
  4833. error('overflow');
  4834. }
  4835. delta += (m - n) * handledCPCountPlusOne;
  4836. n = m;
  4837. for (j = 0; j < inputLength; ++j) {
  4838. currentValue = input[j];
  4839. if (currentValue < n && ++delta > maxInt) {
  4840. error('overflow');
  4841. }
  4842. if (currentValue == n) {
  4843. // Represent delta as a generalized variable-length integer
  4844. for (q = delta, k = base; /* no condition */; k += base) {
  4845. t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
  4846. if (q < t) {
  4847. break;
  4848. }
  4849. qMinusT = q - t;
  4850. baseMinusT = base - t;
  4851. output.push(
  4852. stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0))
  4853. );
  4854. q = floor(qMinusT / baseMinusT);
  4855. }
  4856. output.push(stringFromCharCode(digitToBasic(q, 0)));
  4857. bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
  4858. delta = 0;
  4859. ++handledCPCount;
  4860. }
  4861. }
  4862. ++delta;
  4863. ++n;
  4864. }
  4865. return output.join('');
  4866. }
  4867. /**
  4868. * Converts a Punycode string representing a domain name or an email address
  4869. * to Unicode. Only the Punycoded parts of the input will be converted, i.e.
  4870. * it doesn't matter if you call it on a string that has already been
  4871. * converted to Unicode.
  4872. * @memberOf punycode
  4873. * @param {String} input The Punycoded domain name or email address to
  4874. * convert to Unicode.
  4875. * @returns {String} The Unicode representation of the given Punycode
  4876. * string.
  4877. */
  4878. function toUnicode(input) {
  4879. return mapDomain(input, function(string) {
  4880. return regexPunycode.test(string)
  4881. ? decode(string.slice(4).toLowerCase())
  4882. : string;
  4883. });
  4884. }
  4885. /**
  4886. * Converts a Unicode string representing a domain name or an email address to
  4887. * Punycode. Only the non-ASCII parts of the domain name will be converted,
  4888. * i.e. it doesn't matter if you call it with a domain that's already in
  4889. * ASCII.
  4890. * @memberOf punycode
  4891. * @param {String} input The domain name or email address to convert, as a
  4892. * Unicode string.
  4893. * @returns {String} The Punycode representation of the given domain name or
  4894. * email address.
  4895. */
  4896. function toASCII(input) {
  4897. return mapDomain(input, function(string) {
  4898. return regexNonASCII.test(string)
  4899. ? 'xn--' + encode(string)
  4900. : string;
  4901. });
  4902. }
  4903. /*--------------------------------------------------------------------------*/
  4904. /** Define the public API */
  4905. punycode = {
  4906. /**
  4907. * A string representing the current Punycode.js version number.
  4908. * @memberOf punycode
  4909. * @type String
  4910. */
  4911. 'version': '1.3.2',
  4912. /**
  4913. * An object of methods to convert from JavaScript's internal character
  4914. * representation (UCS-2) to Unicode code points, and back.
  4915. * @see <https://mathiasbynens.be/notes/javascript-encoding>
  4916. * @memberOf punycode
  4917. * @type Object
  4918. */
  4919. 'ucs2': {
  4920. 'decode': ucs2decode,
  4921. 'encode': ucs2encode
  4922. },
  4923. 'decode': decode,
  4924. 'encode': encode,
  4925. 'toASCII': toASCII,
  4926. 'toUnicode': toUnicode
  4927. };
  4928. /** Expose `punycode` */
  4929. // Some AMD build optimizers, like r.js, check for specific condition patterns
  4930. // like the following:
  4931. if (freeExports && freeModule) {
  4932. if (module.exports == freeExports) {
  4933. // in Node.js, io.js, or RingoJS v0.8.0+
  4934. freeModule.exports = punycode;
  4935. } else {
  4936. // in Narwhal or RingoJS v0.7.0-
  4937. for (key in punycode) {
  4938. punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
  4939. }
  4940. }
  4941. } else {
  4942. // in Rhino or a web browser
  4943. root.punycode = punycode;
  4944. }
  4945. }(Math$1.commonjsGlobal));
  4946. } (punycode, punycode.exports));
  4947. return punycode.exports;
  4948. }
  4949. var IPv6 = {exports: {}};
  4950. /*!
  4951. * URI.js - Mutating URLs
  4952. * IPv6 Support
  4953. *
  4954. * Version: 1.19.11
  4955. *
  4956. * Author: Rodney Rehm
  4957. * Web: http://medialize.github.io/URI.js/
  4958. *
  4959. * Licensed under
  4960. * MIT License http://www.opensource.org/licenses/mit-license
  4961. *
  4962. */
  4963. var hasRequiredIPv6;
  4964. function requireIPv6 () {
  4965. if (hasRequiredIPv6) return IPv6.exports;
  4966. hasRequiredIPv6 = 1;
  4967. (function (module) {
  4968. (function (root, factory) {
  4969. // https://github.com/umdjs/umd/blob/master/returnExports.js
  4970. if (module.exports) {
  4971. // Node
  4972. module.exports = factory();
  4973. } else {
  4974. // Browser globals (root is window)
  4975. root.IPv6 = factory(root);
  4976. }
  4977. }(Math$1.commonjsGlobal, function (root) {
  4978. /*
  4979. var _in = "fe80:0000:0000:0000:0204:61ff:fe9d:f156";
  4980. var _out = IPv6.best(_in);
  4981. var _expected = "fe80::204:61ff:fe9d:f156";
  4982. console.log(_in, _out, _expected, _out === _expected);
  4983. */
  4984. // save current IPv6 variable, if any
  4985. var _IPv6 = root && root.IPv6;
  4986. function bestPresentation(address) {
  4987. // based on:
  4988. // Javascript to test an IPv6 address for proper format, and to
  4989. // present the "best text representation" according to IETF Draft RFC at
  4990. // http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04
  4991. // 8 Feb 2010 Rich Brown, Dartware, LLC
  4992. // Please feel free to use this code as long as you provide a link to
  4993. // http://www.intermapper.com
  4994. // http://intermapper.com/support/tools/IPV6-Validator.aspx
  4995. // http://download.dartware.com/thirdparty/ipv6validator.js
  4996. var _address = address.toLowerCase();
  4997. var segments = _address.split(':');
  4998. var length = segments.length;
  4999. var total = 8;
  5000. // trim colons (:: or ::a:b:c… or …a:b:c::)
  5001. if (segments[0] === '' && segments[1] === '' && segments[2] === '') {
  5002. // must have been ::
  5003. // remove first two items
  5004. segments.shift();
  5005. segments.shift();
  5006. } else if (segments[0] === '' && segments[1] === '') {
  5007. // must have been ::xxxx
  5008. // remove the first item
  5009. segments.shift();
  5010. } else if (segments[length - 1] === '' && segments[length - 2] === '') {
  5011. // must have been xxxx::
  5012. segments.pop();
  5013. }
  5014. length = segments.length;
  5015. // adjust total segments for IPv4 trailer
  5016. if (segments[length - 1].indexOf('.') !== -1) {
  5017. // found a "." which means IPv4
  5018. total = 7;
  5019. }
  5020. // fill empty segments them with "0000"
  5021. var pos;
  5022. for (pos = 0; pos < length; pos++) {
  5023. if (segments[pos] === '') {
  5024. break;
  5025. }
  5026. }
  5027. if (pos < total) {
  5028. segments.splice(pos, 1, '0000');
  5029. while (segments.length < total) {
  5030. segments.splice(pos, 0, '0000');
  5031. }
  5032. }
  5033. // strip leading zeros
  5034. var _segments;
  5035. for (var i = 0; i < total; i++) {
  5036. _segments = segments[i].split('');
  5037. for (var j = 0; j < 3 ; j++) {
  5038. if (_segments[0] === '0' && _segments.length > 1) {
  5039. _segments.splice(0,1);
  5040. } else {
  5041. break;
  5042. }
  5043. }
  5044. segments[i] = _segments.join('');
  5045. }
  5046. // find longest sequence of zeroes and coalesce them into one segment
  5047. var best = -1;
  5048. var _best = 0;
  5049. var _current = 0;
  5050. var current = -1;
  5051. var inzeroes = false;
  5052. // i; already declared
  5053. for (i = 0; i < total; i++) {
  5054. if (inzeroes) {
  5055. if (segments[i] === '0') {
  5056. _current += 1;
  5057. } else {
  5058. inzeroes = false;
  5059. if (_current > _best) {
  5060. best = current;
  5061. _best = _current;
  5062. }
  5063. }
  5064. } else {
  5065. if (segments[i] === '0') {
  5066. inzeroes = true;
  5067. current = i;
  5068. _current = 1;
  5069. }
  5070. }
  5071. }
  5072. if (_current > _best) {
  5073. best = current;
  5074. _best = _current;
  5075. }
  5076. if (_best > 1) {
  5077. segments.splice(best, _best, '');
  5078. }
  5079. length = segments.length;
  5080. // assemble remaining segments
  5081. var result = '';
  5082. if (segments[0] === '') {
  5083. result = ':';
  5084. }
  5085. for (i = 0; i < length; i++) {
  5086. result += segments[i];
  5087. if (i === length - 1) {
  5088. break;
  5089. }
  5090. result += ':';
  5091. }
  5092. if (segments[length - 1] === '') {
  5093. result += ':';
  5094. }
  5095. return result;
  5096. }
  5097. function noConflict() {
  5098. /*jshint validthis: true */
  5099. if (root.IPv6 === this) {
  5100. root.IPv6 = _IPv6;
  5101. }
  5102. return this;
  5103. }
  5104. return {
  5105. best: bestPresentation,
  5106. noConflict: noConflict
  5107. };
  5108. }));
  5109. } (IPv6));
  5110. return IPv6.exports;
  5111. }
  5112. var SecondLevelDomains = {exports: {}};
  5113. /*!
  5114. * URI.js - Mutating URLs
  5115. * Second Level Domain (SLD) Support
  5116. *
  5117. * Version: 1.19.11
  5118. *
  5119. * Author: Rodney Rehm
  5120. * Web: http://medialize.github.io/URI.js/
  5121. *
  5122. * Licensed under
  5123. * MIT License http://www.opensource.org/licenses/mit-license
  5124. *
  5125. */
  5126. var hasRequiredSecondLevelDomains;
  5127. function requireSecondLevelDomains () {
  5128. if (hasRequiredSecondLevelDomains) return SecondLevelDomains.exports;
  5129. hasRequiredSecondLevelDomains = 1;
  5130. (function (module) {
  5131. (function (root, factory) {
  5132. // https://github.com/umdjs/umd/blob/master/returnExports.js
  5133. if (module.exports) {
  5134. // Node
  5135. module.exports = factory();
  5136. } else {
  5137. // Browser globals (root is window)
  5138. root.SecondLevelDomains = factory(root);
  5139. }
  5140. }(Math$1.commonjsGlobal, function (root) {
  5141. // save current SecondLevelDomains variable, if any
  5142. var _SecondLevelDomains = root && root.SecondLevelDomains;
  5143. var SLD = {
  5144. // list of known Second Level Domains
  5145. // converted list of SLDs from https://github.com/gavingmiller/second-level-domains
  5146. // ----
  5147. // publicsuffix.org is more current and actually used by a couple of browsers internally.
  5148. // downside is it also contains domains like "dyndns.org" - which is fine for the security
  5149. // issues browser have to deal with (SOP for cookies, etc) - but is way overboard for URI.js
  5150. // ----
  5151. list: {
  5152. 'ac':' com gov mil net org ',
  5153. 'ae':' ac co gov mil name net org pro sch ',
  5154. 'af':' com edu gov net org ',
  5155. 'al':' com edu gov mil net org ',
  5156. 'ao':' co ed gv it og pb ',
  5157. 'ar':' com edu gob gov int mil net org tur ',
  5158. 'at':' ac co gv or ',
  5159. 'au':' asn com csiro edu gov id net org ',
  5160. 'ba':' co com edu gov mil net org rs unbi unmo unsa untz unze ',
  5161. 'bb':' biz co com edu gov info net org store tv ',
  5162. 'bh':' biz cc com edu gov info net org ',
  5163. 'bn':' com edu gov net org ',
  5164. 'bo':' com edu gob gov int mil net org tv ',
  5165. 'br':' adm adv agr am arq art ato b bio blog bmd cim cng cnt com coop ecn edu eng esp etc eti far flog fm fnd fot fst g12 ggf gov imb ind inf jor jus lel mat med mil mus net nom not ntr odo org ppg pro psc psi qsl rec slg srv tmp trd tur tv vet vlog wiki zlg ',
  5166. 'bs':' com edu gov net org ',
  5167. 'bz':' du et om ov rg ',
  5168. 'ca':' ab bc mb nb nf nl ns nt nu on pe qc sk yk ',
  5169. 'ck':' biz co edu gen gov info net org ',
  5170. 'cn':' ac ah bj com cq edu fj gd gov gs gx gz ha hb he hi hl hn jl js jx ln mil net nm nx org qh sc sd sh sn sx tj tw xj xz yn zj ',
  5171. 'co':' com edu gov mil net nom org ',
  5172. 'cr':' ac c co ed fi go or sa ',
  5173. 'cy':' ac biz com ekloges gov ltd name net org parliament press pro tm ',
  5174. 'do':' art com edu gob gov mil net org sld web ',
  5175. 'dz':' art asso com edu gov net org pol ',
  5176. 'ec':' com edu fin gov info med mil net org pro ',
  5177. 'eg':' com edu eun gov mil name net org sci ',
  5178. 'er':' com edu gov ind mil net org rochest w ',
  5179. 'es':' com edu gob nom org ',
  5180. 'et':' biz com edu gov info name net org ',
  5181. 'fj':' ac biz com info mil name net org pro ',
  5182. 'fk':' ac co gov net nom org ',
  5183. 'fr':' asso com f gouv nom prd presse tm ',
  5184. 'gg':' co net org ',
  5185. 'gh':' com edu gov mil org ',
  5186. 'gn':' ac com gov net org ',
  5187. 'gr':' com edu gov mil net org ',
  5188. 'gt':' com edu gob ind mil net org ',
  5189. 'gu':' com edu gov net org ',
  5190. 'hk':' com edu gov idv net org ',
  5191. 'hu':' 2000 agrar bolt casino city co erotica erotika film forum games hotel info ingatlan jogasz konyvelo lakas media news org priv reklam sex shop sport suli szex tm tozsde utazas video ',
  5192. 'id':' ac co go mil net or sch web ',
  5193. 'il':' ac co gov idf k12 muni net org ',
  5194. 'in':' ac co edu ernet firm gen gov i ind mil net nic org res ',
  5195. 'iq':' com edu gov i mil net org ',
  5196. 'ir':' ac co dnssec gov i id net org sch ',
  5197. 'it':' edu gov ',
  5198. 'je':' co net org ',
  5199. 'jo':' com edu gov mil name net org sch ',
  5200. 'jp':' ac ad co ed go gr lg ne or ',
  5201. 'ke':' ac co go info me mobi ne or sc ',
  5202. 'kh':' com edu gov mil net org per ',
  5203. 'ki':' biz com de edu gov info mob net org tel ',
  5204. 'km':' asso com coop edu gouv k medecin mil nom notaires pharmaciens presse tm veterinaire ',
  5205. 'kn':' edu gov net org ',
  5206. 'kr':' ac busan chungbuk chungnam co daegu daejeon es gangwon go gwangju gyeongbuk gyeonggi gyeongnam hs incheon jeju jeonbuk jeonnam k kg mil ms ne or pe re sc seoul ulsan ',
  5207. 'kw':' com edu gov net org ',
  5208. 'ky':' com edu gov net org ',
  5209. 'kz':' com edu gov mil net org ',
  5210. 'lb':' com edu gov net org ',
  5211. 'lk':' assn com edu gov grp hotel int ltd net ngo org sch soc web ',
  5212. 'lr':' com edu gov net org ',
  5213. 'lv':' asn com conf edu gov id mil net org ',
  5214. 'ly':' com edu gov id med net org plc sch ',
  5215. 'ma':' ac co gov m net org press ',
  5216. 'mc':' asso tm ',
  5217. 'me':' ac co edu gov its net org priv ',
  5218. 'mg':' com edu gov mil nom org prd tm ',
  5219. 'mk':' com edu gov inf name net org pro ',
  5220. 'ml':' com edu gov net org presse ',
  5221. 'mn':' edu gov org ',
  5222. 'mo':' com edu gov net org ',
  5223. 'mt':' com edu gov net org ',
  5224. 'mv':' aero biz com coop edu gov info int mil museum name net org pro ',
  5225. 'mw':' ac co com coop edu gov int museum net org ',
  5226. 'mx':' com edu gob net org ',
  5227. 'my':' com edu gov mil name net org sch ',
  5228. 'nf':' arts com firm info net other per rec store web ',
  5229. 'ng':' biz com edu gov mil mobi name net org sch ',
  5230. 'ni':' ac co com edu gob mil net nom org ',
  5231. 'np':' com edu gov mil net org ',
  5232. 'nr':' biz com edu gov info net org ',
  5233. 'om':' ac biz co com edu gov med mil museum net org pro sch ',
  5234. 'pe':' com edu gob mil net nom org sld ',
  5235. 'ph':' com edu gov i mil net ngo org ',
  5236. 'pk':' biz com edu fam gob gok gon gop gos gov net org web ',
  5237. 'pl':' art bialystok biz com edu gda gdansk gorzow gov info katowice krakow lodz lublin mil net ngo olsztyn org poznan pwr radom slupsk szczecin torun warszawa waw wroc wroclaw zgora ',
  5238. 'pr':' ac biz com edu est gov info isla name net org pro prof ',
  5239. 'ps':' com edu gov net org plo sec ',
  5240. 'pw':' belau co ed go ne or ',
  5241. 'ro':' arts com firm info nom nt org rec store tm www ',
  5242. 'rs':' ac co edu gov in org ',
  5243. 'sb':' com edu gov net org ',
  5244. 'sc':' com edu gov net org ',
  5245. 'sh':' co com edu gov net nom org ',
  5246. 'sl':' com edu gov net org ',
  5247. 'st':' co com consulado edu embaixada gov mil net org principe saotome store ',
  5248. 'sv':' com edu gob org red ',
  5249. 'sz':' ac co org ',
  5250. 'tr':' av bbs bel biz com dr edu gen gov info k12 name net org pol tel tsk tv web ',
  5251. 'tt':' aero biz cat co com coop edu gov info int jobs mil mobi museum name net org pro tel travel ',
  5252. 'tw':' club com ebiz edu game gov idv mil net org ',
  5253. 'mu':' ac co com gov net or org ',
  5254. 'mz':' ac co edu gov org ',
  5255. 'na':' co com ',
  5256. 'nz':' ac co cri geek gen govt health iwi maori mil net org parliament school ',
  5257. 'pa':' abo ac com edu gob ing med net nom org sld ',
  5258. 'pt':' com edu gov int net nome org publ ',
  5259. 'py':' com edu gov mil net org ',
  5260. 'qa':' com edu gov mil net org ',
  5261. 're':' asso com nom ',
  5262. 'ru':' ac adygeya altai amur arkhangelsk astrakhan bashkiria belgorod bir bryansk buryatia cbg chel chelyabinsk chita chukotka chuvashia com dagestan e-burg edu gov grozny int irkutsk ivanovo izhevsk jar joshkar-ola kalmykia kaluga kamchatka karelia kazan kchr kemerovo khabarovsk khakassia khv kirov koenig komi kostroma kranoyarsk kuban kurgan kursk lipetsk magadan mari mari-el marine mil mordovia mosreg msk murmansk nalchik net nnov nov novosibirsk nsk omsk orenburg org oryol penza perm pp pskov ptz rnd ryazan sakhalin samara saratov simbirsk smolensk spb stavropol stv surgut tambov tatarstan tom tomsk tsaritsyn tsk tula tuva tver tyumen udm udmurtia ulan-ude vladikavkaz vladimir vladivostok volgograd vologda voronezh vrn vyatka yakutia yamal yekaterinburg yuzhno-sakhalinsk ',
  5263. 'rw':' ac co com edu gouv gov int mil net ',
  5264. 'sa':' com edu gov med net org pub sch ',
  5265. 'sd':' com edu gov info med net org tv ',
  5266. 'se':' a ac b bd c d e f g h i k l m n o org p parti pp press r s t tm u w x y z ',
  5267. 'sg':' com edu gov idn net org per ',
  5268. 'sn':' art com edu gouv org perso univ ',
  5269. 'sy':' com edu gov mil net news org ',
  5270. 'th':' ac co go in mi net or ',
  5271. 'tj':' ac biz co com edu go gov info int mil name net nic org test web ',
  5272. 'tn':' agrinet com defense edunet ens fin gov ind info intl mincom nat net org perso rnrt rns rnu tourism ',
  5273. 'tz':' ac co go ne or ',
  5274. 'ua':' biz cherkassy chernigov chernovtsy ck cn co com crimea cv dn dnepropetrovsk donetsk dp edu gov if in ivano-frankivsk kh kharkov kherson khmelnitskiy kiev kirovograd km kr ks kv lg lugansk lutsk lviv me mk net nikolaev od odessa org pl poltava pp rovno rv sebastopol sumy te ternopil uzhgorod vinnica vn zaporizhzhe zhitomir zp zt ',
  5275. 'ug':' ac co go ne or org sc ',
  5276. 'uk':' ac bl british-library co cym gov govt icnet jet lea ltd me mil mod national-library-scotland nel net nhs nic nls org orgn parliament plc police sch scot soc ',
  5277. 'us':' dni fed isa kids nsn ',
  5278. 'uy':' com edu gub mil net org ',
  5279. 've':' co com edu gob info mil net org web ',
  5280. 'vi':' co com k12 net org ',
  5281. 'vn':' ac biz com edu gov health info int name net org pro ',
  5282. 'ye':' co com gov ltd me net org plc ',
  5283. 'yu':' ac co edu gov org ',
  5284. 'za':' ac agric alt bourse city co cybernet db edu gov grondar iaccess imt inca landesign law mil net ngo nis nom olivetti org pix school tm web ',
  5285. 'zm':' ac co com edu gov net org sch ',
  5286. // https://en.wikipedia.org/wiki/CentralNic#Second-level_domains
  5287. 'com': 'ar br cn de eu gb gr hu jpn kr no qc ru sa se uk us uy za ',
  5288. 'net': 'gb jp se uk ',
  5289. 'org': 'ae',
  5290. 'de': 'com '
  5291. },
  5292. // gorhill 2013-10-25: Using indexOf() instead Regexp(). Significant boost
  5293. // in both performance and memory footprint. No initialization required.
  5294. // http://jsperf.com/uri-js-sld-regex-vs-binary-search/4
  5295. // Following methods use lastIndexOf() rather than array.split() in order
  5296. // to avoid any memory allocations.
  5297. has: function(domain) {
  5298. var tldOffset = domain.lastIndexOf('.');
  5299. if (tldOffset <= 0 || tldOffset >= (domain.length-1)) {
  5300. return false;
  5301. }
  5302. var sldOffset = domain.lastIndexOf('.', tldOffset-1);
  5303. if (sldOffset <= 0 || sldOffset >= (tldOffset-1)) {
  5304. return false;
  5305. }
  5306. var sldList = SLD.list[domain.slice(tldOffset+1)];
  5307. if (!sldList) {
  5308. return false;
  5309. }
  5310. return sldList.indexOf(' ' + domain.slice(sldOffset+1, tldOffset) + ' ') >= 0;
  5311. },
  5312. is: function(domain) {
  5313. var tldOffset = domain.lastIndexOf('.');
  5314. if (tldOffset <= 0 || tldOffset >= (domain.length-1)) {
  5315. return false;
  5316. }
  5317. var sldOffset = domain.lastIndexOf('.', tldOffset-1);
  5318. if (sldOffset >= 0) {
  5319. return false;
  5320. }
  5321. var sldList = SLD.list[domain.slice(tldOffset+1)];
  5322. if (!sldList) {
  5323. return false;
  5324. }
  5325. return sldList.indexOf(' ' + domain.slice(0, tldOffset) + ' ') >= 0;
  5326. },
  5327. get: function(domain) {
  5328. var tldOffset = domain.lastIndexOf('.');
  5329. if (tldOffset <= 0 || tldOffset >= (domain.length-1)) {
  5330. return null;
  5331. }
  5332. var sldOffset = domain.lastIndexOf('.', tldOffset-1);
  5333. if (sldOffset <= 0 || sldOffset >= (tldOffset-1)) {
  5334. return null;
  5335. }
  5336. var sldList = SLD.list[domain.slice(tldOffset+1)];
  5337. if (!sldList) {
  5338. return null;
  5339. }
  5340. if (sldList.indexOf(' ' + domain.slice(sldOffset+1, tldOffset) + ' ') < 0) {
  5341. return null;
  5342. }
  5343. return domain.slice(sldOffset+1);
  5344. },
  5345. noConflict: function(){
  5346. if (root.SecondLevelDomains === this) {
  5347. root.SecondLevelDomains = _SecondLevelDomains;
  5348. }
  5349. return this;
  5350. }
  5351. };
  5352. return SLD;
  5353. }));
  5354. } (SecondLevelDomains));
  5355. return SecondLevelDomains.exports;
  5356. }
  5357. /*!
  5358. * URI.js - Mutating URLs
  5359. *
  5360. * Version: 1.19.11
  5361. *
  5362. * Author: Rodney Rehm
  5363. * Web: http://medialize.github.io/URI.js/
  5364. *
  5365. * Licensed under
  5366. * MIT License http://www.opensource.org/licenses/mit-license
  5367. *
  5368. */
  5369. (function (module) {
  5370. (function (root, factory) {
  5371. // https://github.com/umdjs/umd/blob/master/returnExports.js
  5372. if (module.exports) {
  5373. // Node
  5374. module.exports = factory(requirePunycode(), requireIPv6(), requireSecondLevelDomains());
  5375. } else {
  5376. // Browser globals (root is window)
  5377. root.URI = factory(root.punycode, root.IPv6, root.SecondLevelDomains, root);
  5378. }
  5379. }(Math$1.commonjsGlobal, function (punycode, IPv6, SLD, root) {
  5380. /*global location, escape, unescape */
  5381. // FIXME: v2.0.0 renamce non-camelCase properties to uppercase
  5382. /*jshint camelcase: false */
  5383. // save current URI variable, if any
  5384. var _URI = root && root.URI;
  5385. function URI(url, base) {
  5386. var _urlSupplied = arguments.length >= 1;
  5387. var _baseSupplied = arguments.length >= 2;
  5388. // Allow instantiation without the 'new' keyword
  5389. if (!(this instanceof URI)) {
  5390. if (_urlSupplied) {
  5391. if (_baseSupplied) {
  5392. return new URI(url, base);
  5393. }
  5394. return new URI(url);
  5395. }
  5396. return new URI();
  5397. }
  5398. if (url === undefined) {
  5399. if (_urlSupplied) {
  5400. throw new TypeError('undefined is not a valid argument for URI');
  5401. }
  5402. if (typeof location !== 'undefined') {
  5403. url = location.href + '';
  5404. } else {
  5405. url = '';
  5406. }
  5407. }
  5408. if (url === null) {
  5409. if (_urlSupplied) {
  5410. throw new TypeError('null is not a valid argument for URI');
  5411. }
  5412. }
  5413. this.href(url);
  5414. // resolve to base according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#constructor
  5415. if (base !== undefined) {
  5416. return this.absoluteTo(base);
  5417. }
  5418. return this;
  5419. }
  5420. function isInteger(value) {
  5421. return /^[0-9]+$/.test(value);
  5422. }
  5423. URI.version = '1.19.11';
  5424. var p = URI.prototype;
  5425. var hasOwn = Object.prototype.hasOwnProperty;
  5426. function escapeRegEx(string) {
  5427. // https://github.com/medialize/URI.js/commit/85ac21783c11f8ccab06106dba9735a31a86924d#commitcomment-821963
  5428. return string.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
  5429. }
  5430. function getType(value) {
  5431. // IE8 doesn't return [Object Undefined] but [Object Object] for undefined value
  5432. if (value === undefined) {
  5433. return 'Undefined';
  5434. }
  5435. return String(Object.prototype.toString.call(value)).slice(8, -1);
  5436. }
  5437. function isArray(obj) {
  5438. return getType(obj) === 'Array';
  5439. }
  5440. function filterArrayValues(data, value) {
  5441. var lookup = {};
  5442. var i, length;
  5443. if (getType(value) === 'RegExp') {
  5444. lookup = null;
  5445. } else if (isArray(value)) {
  5446. for (i = 0, length = value.length; i < length; i++) {
  5447. lookup[value[i]] = true;
  5448. }
  5449. } else {
  5450. lookup[value] = true;
  5451. }
  5452. for (i = 0, length = data.length; i < length; i++) {
  5453. /*jshint laxbreak: true */
  5454. var _match = lookup && lookup[data[i]] !== undefined
  5455. || !lookup && value.test(data[i]);
  5456. /*jshint laxbreak: false */
  5457. if (_match) {
  5458. data.splice(i, 1);
  5459. length--;
  5460. i--;
  5461. }
  5462. }
  5463. return data;
  5464. }
  5465. function arrayContains(list, value) {
  5466. var i, length;
  5467. // value may be string, number, array, regexp
  5468. if (isArray(value)) {
  5469. // Note: this can be optimized to O(n) (instead of current O(m * n))
  5470. for (i = 0, length = value.length; i < length; i++) {
  5471. if (!arrayContains(list, value[i])) {
  5472. return false;
  5473. }
  5474. }
  5475. return true;
  5476. }
  5477. var _type = getType(value);
  5478. for (i = 0, length = list.length; i < length; i++) {
  5479. if (_type === 'RegExp') {
  5480. if (typeof list[i] === 'string' && list[i].match(value)) {
  5481. return true;
  5482. }
  5483. } else if (list[i] === value) {
  5484. return true;
  5485. }
  5486. }
  5487. return false;
  5488. }
  5489. function arraysEqual(one, two) {
  5490. if (!isArray(one) || !isArray(two)) {
  5491. return false;
  5492. }
  5493. // arrays can't be equal if they have different amount of content
  5494. if (one.length !== two.length) {
  5495. return false;
  5496. }
  5497. one.sort();
  5498. two.sort();
  5499. for (var i = 0, l = one.length; i < l; i++) {
  5500. if (one[i] !== two[i]) {
  5501. return false;
  5502. }
  5503. }
  5504. return true;
  5505. }
  5506. function trimSlashes(text) {
  5507. var trim_expression = /^\/+|\/+$/g;
  5508. return text.replace(trim_expression, '');
  5509. }
  5510. URI._parts = function() {
  5511. return {
  5512. protocol: null,
  5513. username: null,
  5514. password: null,
  5515. hostname: null,
  5516. urn: null,
  5517. port: null,
  5518. path: null,
  5519. query: null,
  5520. fragment: null,
  5521. // state
  5522. preventInvalidHostname: URI.preventInvalidHostname,
  5523. duplicateQueryParameters: URI.duplicateQueryParameters,
  5524. escapeQuerySpace: URI.escapeQuerySpace
  5525. };
  5526. };
  5527. // state: throw on invalid hostname
  5528. // see https://github.com/medialize/URI.js/pull/345
  5529. // and https://github.com/medialize/URI.js/issues/354
  5530. URI.preventInvalidHostname = false;
  5531. // state: allow duplicate query parameters (a=1&a=1)
  5532. URI.duplicateQueryParameters = false;
  5533. // state: replaces + with %20 (space in query strings)
  5534. URI.escapeQuerySpace = true;
  5535. // static properties
  5536. URI.protocol_expression = /^[a-z][a-z0-9.+-]*$/i;
  5537. URI.idn_expression = /[^a-z0-9\._-]/i;
  5538. URI.punycode_expression = /(xn--)/i;
  5539. // well, 333.444.555.666 matches, but it sure ain't no IPv4 - do we care?
  5540. URI.ip4_expression = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  5541. // credits to Rich Brown
  5542. // source: http://forums.intermapper.com/viewtopic.php?p=1096#1096
  5543. // specification: http://www.ietf.org/rfc/rfc4291.txt
  5544. URI.ip6_expression = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
  5545. // expression used is "gruber revised" (@gruber v2) determined to be the
  5546. // best solution in a regex-golf we did a couple of ages ago at
  5547. // * http://mathiasbynens.be/demo/url-regex
  5548. // * http://rodneyrehm.de/t/url-regex.html
  5549. URI.find_uri_expression = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig;
  5550. URI.findUri = {
  5551. // valid "scheme://" or "www."
  5552. start: /\b(?:([a-z][a-z0-9.+-]*:\/\/)|www\.)/gi,
  5553. // everything up to the next whitespace
  5554. end: /[\s\r\n]|$/,
  5555. // trim trailing punctuation captured by end RegExp
  5556. trim: /[`!()\[\]{};:'".,<>?«»“”„‘’]+$/,
  5557. // balanced parens inclusion (), [], {}, <>
  5558. parens: /(\([^\)]*\)|\[[^\]]*\]|\{[^}]*\}|<[^>]*>)/g,
  5559. };
  5560. URI.leading_whitespace_expression = /^[\x00-\x20\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/;
  5561. // https://infra.spec.whatwg.org/#ascii-tab-or-newline
  5562. URI.ascii_tab_whitespace = /[\u0009\u000A\u000D]+/g;
  5563. // http://www.iana.org/assignments/uri-schemes.html
  5564. // http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports
  5565. URI.defaultPorts = {
  5566. http: '80',
  5567. https: '443',
  5568. ftp: '21',
  5569. gopher: '70',
  5570. ws: '80',
  5571. wss: '443'
  5572. };
  5573. // list of protocols which always require a hostname
  5574. URI.hostProtocols = [
  5575. 'http',
  5576. 'https'
  5577. ];
  5578. // allowed hostname characters according to RFC 3986
  5579. // ALPHA DIGIT "-" "." "_" "~" "!" "$" "&" "'" "(" ")" "*" "+" "," ";" "=" %encoded
  5580. // I've never seen a (non-IDN) hostname other than: ALPHA DIGIT . - _
  5581. URI.invalid_hostname_characters = /[^a-zA-Z0-9\.\-:_]/;
  5582. // map DOM Elements to their URI attribute
  5583. URI.domAttributes = {
  5584. 'a': 'href',
  5585. 'blockquote': 'cite',
  5586. 'link': 'href',
  5587. 'base': 'href',
  5588. 'script': 'src',
  5589. 'form': 'action',
  5590. 'img': 'src',
  5591. 'area': 'href',
  5592. 'iframe': 'src',
  5593. 'embed': 'src',
  5594. 'source': 'src',
  5595. 'track': 'src',
  5596. 'input': 'src', // but only if type="image"
  5597. 'audio': 'src',
  5598. 'video': 'src'
  5599. };
  5600. URI.getDomAttribute = function(node) {
  5601. if (!node || !node.nodeName) {
  5602. return undefined;
  5603. }
  5604. var nodeName = node.nodeName.toLowerCase();
  5605. // <input> should only expose src for type="image"
  5606. if (nodeName === 'input' && node.type !== 'image') {
  5607. return undefined;
  5608. }
  5609. return URI.domAttributes[nodeName];
  5610. };
  5611. function escapeForDumbFirefox36(value) {
  5612. // https://github.com/medialize/URI.js/issues/91
  5613. return escape(value);
  5614. }
  5615. // encoding / decoding according to RFC3986
  5616. function strictEncodeURIComponent(string) {
  5617. // see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
  5618. return encodeURIComponent(string)
  5619. .replace(/[!'()*]/g, escapeForDumbFirefox36)
  5620. .replace(/\*/g, '%2A');
  5621. }
  5622. URI.encode = strictEncodeURIComponent;
  5623. URI.decode = decodeURIComponent;
  5624. URI.iso8859 = function() {
  5625. URI.encode = escape;
  5626. URI.decode = unescape;
  5627. };
  5628. URI.unicode = function() {
  5629. URI.encode = strictEncodeURIComponent;
  5630. URI.decode = decodeURIComponent;
  5631. };
  5632. URI.characters = {
  5633. pathname: {
  5634. encode: {
  5635. // RFC3986 2.1: For consistency, URI producers and normalizers should
  5636. // use uppercase hexadecimal digits for all percent-encodings.
  5637. expression: /%(24|26|2B|2C|3B|3D|3A|40)/ig,
  5638. map: {
  5639. // -._~!'()*
  5640. '%24': '$',
  5641. '%26': '&',
  5642. '%2B': '+',
  5643. '%2C': ',',
  5644. '%3B': ';',
  5645. '%3D': '=',
  5646. '%3A': ':',
  5647. '%40': '@'
  5648. }
  5649. },
  5650. decode: {
  5651. expression: /[\/\?#]/g,
  5652. map: {
  5653. '/': '%2F',
  5654. '?': '%3F',
  5655. '#': '%23'
  5656. }
  5657. }
  5658. },
  5659. reserved: {
  5660. encode: {
  5661. // RFC3986 2.1: For consistency, URI producers and normalizers should
  5662. // use uppercase hexadecimal digits for all percent-encodings.
  5663. expression: /%(21|23|24|26|27|28|29|2A|2B|2C|2F|3A|3B|3D|3F|40|5B|5D)/ig,
  5664. map: {
  5665. // gen-delims
  5666. '%3A': ':',
  5667. '%2F': '/',
  5668. '%3F': '?',
  5669. '%23': '#',
  5670. '%5B': '[',
  5671. '%5D': ']',
  5672. '%40': '@',
  5673. // sub-delims
  5674. '%21': '!',
  5675. '%24': '$',
  5676. '%26': '&',
  5677. '%27': '\'',
  5678. '%28': '(',
  5679. '%29': ')',
  5680. '%2A': '*',
  5681. '%2B': '+',
  5682. '%2C': ',',
  5683. '%3B': ';',
  5684. '%3D': '='
  5685. }
  5686. }
  5687. },
  5688. urnpath: {
  5689. // The characters under `encode` are the characters called out by RFC 2141 as being acceptable
  5690. // for usage in a URN. RFC2141 also calls out "-", ".", and "_" as acceptable characters, but
  5691. // these aren't encoded by encodeURIComponent, so we don't have to call them out here. Also
  5692. // note that the colon character is not featured in the encoding map; this is because URI.js
  5693. // gives the colons in URNs semantic meaning as the delimiters of path segements, and so it
  5694. // should not appear unencoded in a segment itself.
  5695. // See also the note above about RFC3986 and capitalalized hex digits.
  5696. encode: {
  5697. expression: /%(21|24|27|28|29|2A|2B|2C|3B|3D|40)/ig,
  5698. map: {
  5699. '%21': '!',
  5700. '%24': '$',
  5701. '%27': '\'',
  5702. '%28': '(',
  5703. '%29': ')',
  5704. '%2A': '*',
  5705. '%2B': '+',
  5706. '%2C': ',',
  5707. '%3B': ';',
  5708. '%3D': '=',
  5709. '%40': '@'
  5710. }
  5711. },
  5712. // These characters are the characters called out by RFC2141 as "reserved" characters that
  5713. // should never appear in a URN, plus the colon character (see note above).
  5714. decode: {
  5715. expression: /[\/\?#:]/g,
  5716. map: {
  5717. '/': '%2F',
  5718. '?': '%3F',
  5719. '#': '%23',
  5720. ':': '%3A'
  5721. }
  5722. }
  5723. }
  5724. };
  5725. URI.encodeQuery = function(string, escapeQuerySpace) {
  5726. var escaped = URI.encode(string + '');
  5727. if (escapeQuerySpace === undefined) {
  5728. escapeQuerySpace = URI.escapeQuerySpace;
  5729. }
  5730. return escapeQuerySpace ? escaped.replace(/%20/g, '+') : escaped;
  5731. };
  5732. URI.decodeQuery = function(string, escapeQuerySpace) {
  5733. string += '';
  5734. if (escapeQuerySpace === undefined) {
  5735. escapeQuerySpace = URI.escapeQuerySpace;
  5736. }
  5737. try {
  5738. return URI.decode(escapeQuerySpace ? string.replace(/\+/g, '%20') : string);
  5739. } catch(e) {
  5740. // we're not going to mess with weird encodings,
  5741. // give up and return the undecoded original string
  5742. // see https://github.com/medialize/URI.js/issues/87
  5743. // see https://github.com/medialize/URI.js/issues/92
  5744. return string;
  5745. }
  5746. };
  5747. // generate encode/decode path functions
  5748. var _parts = {'encode':'encode', 'decode':'decode'};
  5749. var _part;
  5750. var generateAccessor = function(_group, _part) {
  5751. return function(string) {
  5752. try {
  5753. return URI[_part](string + '').replace(URI.characters[_group][_part].expression, function(c) {
  5754. return URI.characters[_group][_part].map[c];
  5755. });
  5756. } catch (e) {
  5757. // we're not going to mess with weird encodings,
  5758. // give up and return the undecoded original string
  5759. // see https://github.com/medialize/URI.js/issues/87
  5760. // see https://github.com/medialize/URI.js/issues/92
  5761. return string;
  5762. }
  5763. };
  5764. };
  5765. for (_part in _parts) {
  5766. URI[_part + 'PathSegment'] = generateAccessor('pathname', _parts[_part]);
  5767. URI[_part + 'UrnPathSegment'] = generateAccessor('urnpath', _parts[_part]);
  5768. }
  5769. var generateSegmentedPathFunction = function(_sep, _codingFuncName, _innerCodingFuncName) {
  5770. return function(string) {
  5771. // Why pass in names of functions, rather than the function objects themselves? The
  5772. // definitions of some functions (but in particular, URI.decode) will occasionally change due
  5773. // to URI.js having ISO8859 and Unicode modes. Passing in the name and getting it will ensure
  5774. // that the functions we use here are "fresh".
  5775. var actualCodingFunc;
  5776. if (!_innerCodingFuncName) {
  5777. actualCodingFunc = URI[_codingFuncName];
  5778. } else {
  5779. actualCodingFunc = function(string) {
  5780. return URI[_codingFuncName](URI[_innerCodingFuncName](string));
  5781. };
  5782. }
  5783. var segments = (string + '').split(_sep);
  5784. for (var i = 0, length = segments.length; i < length; i++) {
  5785. segments[i] = actualCodingFunc(segments[i]);
  5786. }
  5787. return segments.join(_sep);
  5788. };
  5789. };
  5790. // This takes place outside the above loop because we don't want, e.g., encodeUrnPath functions.
  5791. URI.decodePath = generateSegmentedPathFunction('/', 'decodePathSegment');
  5792. URI.decodeUrnPath = generateSegmentedPathFunction(':', 'decodeUrnPathSegment');
  5793. URI.recodePath = generateSegmentedPathFunction('/', 'encodePathSegment', 'decode');
  5794. URI.recodeUrnPath = generateSegmentedPathFunction(':', 'encodeUrnPathSegment', 'decode');
  5795. URI.encodeReserved = generateAccessor('reserved', 'encode');
  5796. URI.parse = function(string, parts) {
  5797. var pos;
  5798. if (!parts) {
  5799. parts = {
  5800. preventInvalidHostname: URI.preventInvalidHostname
  5801. };
  5802. }
  5803. string = string.replace(URI.leading_whitespace_expression, '');
  5804. // https://infra.spec.whatwg.org/#ascii-tab-or-newline
  5805. string = string.replace(URI.ascii_tab_whitespace, '');
  5806. // [protocol"://"[username[":"password]"@"]hostname[":"port]"/"?][path]["?"querystring]["#"fragment]
  5807. // extract fragment
  5808. pos = string.indexOf('#');
  5809. if (pos > -1) {
  5810. // escaping?
  5811. parts.fragment = string.substring(pos + 1) || null;
  5812. string = string.substring(0, pos);
  5813. }
  5814. // extract query
  5815. pos = string.indexOf('?');
  5816. if (pos > -1) {
  5817. // escaping?
  5818. parts.query = string.substring(pos + 1) || null;
  5819. string = string.substring(0, pos);
  5820. }
  5821. // slashes and backslashes have lost all meaning for the web protocols (https, http, wss, ws)
  5822. string = string.replace(/^(https?|ftp|wss?)?:+[/\\]*/i, '$1://');
  5823. // slashes and backslashes have lost all meaning for scheme relative URLs
  5824. string = string.replace(/^[/\\]{2,}/i, '//');
  5825. // extract protocol
  5826. if (string.substring(0, 2) === '//') {
  5827. // relative-scheme
  5828. parts.protocol = null;
  5829. string = string.substring(2);
  5830. // extract "user:pass@host:port"
  5831. string = URI.parseAuthority(string, parts);
  5832. } else {
  5833. pos = string.indexOf(':');
  5834. if (pos > -1) {
  5835. parts.protocol = string.substring(0, pos) || null;
  5836. if (parts.protocol && !parts.protocol.match(URI.protocol_expression)) {
  5837. // : may be within the path
  5838. parts.protocol = undefined;
  5839. } else if (string.substring(pos + 1, pos + 3).replace(/\\/g, '/') === '//') {
  5840. string = string.substring(pos + 3);
  5841. // extract "user:pass@host:port"
  5842. string = URI.parseAuthority(string, parts);
  5843. } else {
  5844. string = string.substring(pos + 1);
  5845. parts.urn = true;
  5846. }
  5847. }
  5848. }
  5849. // what's left must be the path
  5850. parts.path = string;
  5851. // and we're done
  5852. return parts;
  5853. };
  5854. URI.parseHost = function(string, parts) {
  5855. if (!string) {
  5856. string = '';
  5857. }
  5858. // Copy chrome, IE, opera backslash-handling behavior.
  5859. // Back slashes before the query string get converted to forward slashes
  5860. // See: https://github.com/joyent/node/blob/386fd24f49b0e9d1a8a076592a404168faeecc34/lib/url.js#L115-L124
  5861. // See: https://code.google.com/p/chromium/issues/detail?id=25916
  5862. // https://github.com/medialize/URI.js/pull/233
  5863. string = string.replace(/\\/g, '/');
  5864. // extract host:port
  5865. var pos = string.indexOf('/');
  5866. var bracketPos;
  5867. var t;
  5868. if (pos === -1) {
  5869. pos = string.length;
  5870. }
  5871. if (string.charAt(0) === '[') {
  5872. // IPv6 host - http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04#section-6
  5873. // I claim most client software breaks on IPv6 anyways. To simplify things, URI only accepts
  5874. // IPv6+port in the format [2001:db8::1]:80 (for the time being)
  5875. bracketPos = string.indexOf(']');
  5876. parts.hostname = string.substring(1, bracketPos) || null;
  5877. parts.port = string.substring(bracketPos + 2, pos) || null;
  5878. if (parts.port === '/') {
  5879. parts.port = null;
  5880. }
  5881. } else {
  5882. var firstColon = string.indexOf(':');
  5883. var firstSlash = string.indexOf('/');
  5884. var nextColon = string.indexOf(':', firstColon + 1);
  5885. if (nextColon !== -1 && (firstSlash === -1 || nextColon < firstSlash)) {
  5886. // IPv6 host contains multiple colons - but no port
  5887. // this notation is actually not allowed by RFC 3986, but we're a liberal parser
  5888. parts.hostname = string.substring(0, pos) || null;
  5889. parts.port = null;
  5890. } else {
  5891. t = string.substring(0, pos).split(':');
  5892. parts.hostname = t[0] || null;
  5893. parts.port = t[1] || null;
  5894. }
  5895. }
  5896. if (parts.hostname && string.substring(pos).charAt(0) !== '/') {
  5897. pos++;
  5898. string = '/' + string;
  5899. }
  5900. if (parts.preventInvalidHostname) {
  5901. URI.ensureValidHostname(parts.hostname, parts.protocol);
  5902. }
  5903. if (parts.port) {
  5904. URI.ensureValidPort(parts.port);
  5905. }
  5906. return string.substring(pos) || '/';
  5907. };
  5908. URI.parseAuthority = function(string, parts) {
  5909. string = URI.parseUserinfo(string, parts);
  5910. return URI.parseHost(string, parts);
  5911. };
  5912. URI.parseUserinfo = function(string, parts) {
  5913. // extract username:password
  5914. var _string = string;
  5915. var firstBackSlash = string.indexOf('\\');
  5916. if (firstBackSlash !== -1) {
  5917. string = string.replace(/\\/g, '/');
  5918. }
  5919. var firstSlash = string.indexOf('/');
  5920. var pos = string.lastIndexOf('@', firstSlash > -1 ? firstSlash : string.length - 1);
  5921. var t;
  5922. // authority@ must come before /path or \path
  5923. if (pos > -1 && (firstSlash === -1 || pos < firstSlash)) {
  5924. t = string.substring(0, pos).split(':');
  5925. parts.username = t[0] ? URI.decode(t[0]) : null;
  5926. t.shift();
  5927. parts.password = t[0] ? URI.decode(t.join(':')) : null;
  5928. string = _string.substring(pos + 1);
  5929. } else {
  5930. parts.username = null;
  5931. parts.password = null;
  5932. }
  5933. return string;
  5934. };
  5935. URI.parseQuery = function(string, escapeQuerySpace) {
  5936. if (!string) {
  5937. return {};
  5938. }
  5939. // throw out the funky business - "?"[name"="value"&"]+
  5940. string = string.replace(/&+/g, '&').replace(/^\?*&*|&+$/g, '');
  5941. if (!string) {
  5942. return {};
  5943. }
  5944. var items = {};
  5945. var splits = string.split('&');
  5946. var length = splits.length;
  5947. var v, name, value;
  5948. for (var i = 0; i < length; i++) {
  5949. v = splits[i].split('=');
  5950. name = URI.decodeQuery(v.shift(), escapeQuerySpace);
  5951. // no "=" is null according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#collect-url-parameters
  5952. value = v.length ? URI.decodeQuery(v.join('='), escapeQuerySpace) : null;
  5953. if (name === '__proto__') {
  5954. // ignore attempt at exploiting JavaScript internals
  5955. continue;
  5956. } else if (hasOwn.call(items, name)) {
  5957. if (typeof items[name] === 'string' || items[name] === null) {
  5958. items[name] = [items[name]];
  5959. }
  5960. items[name].push(value);
  5961. } else {
  5962. items[name] = value;
  5963. }
  5964. }
  5965. return items;
  5966. };
  5967. URI.build = function(parts) {
  5968. var t = '';
  5969. var requireAbsolutePath = false;
  5970. if (parts.protocol) {
  5971. t += parts.protocol + ':';
  5972. }
  5973. if (!parts.urn && (t || parts.hostname)) {
  5974. t += '//';
  5975. requireAbsolutePath = true;
  5976. }
  5977. t += (URI.buildAuthority(parts) || '');
  5978. if (typeof parts.path === 'string') {
  5979. if (parts.path.charAt(0) !== '/' && requireAbsolutePath) {
  5980. t += '/';
  5981. }
  5982. t += parts.path;
  5983. }
  5984. if (typeof parts.query === 'string' && parts.query) {
  5985. t += '?' + parts.query;
  5986. }
  5987. if (typeof parts.fragment === 'string' && parts.fragment) {
  5988. t += '#' + parts.fragment;
  5989. }
  5990. return t;
  5991. };
  5992. URI.buildHost = function(parts) {
  5993. var t = '';
  5994. if (!parts.hostname) {
  5995. return '';
  5996. } else if (URI.ip6_expression.test(parts.hostname)) {
  5997. t += '[' + parts.hostname + ']';
  5998. } else {
  5999. t += parts.hostname;
  6000. }
  6001. if (parts.port) {
  6002. t += ':' + parts.port;
  6003. }
  6004. return t;
  6005. };
  6006. URI.buildAuthority = function(parts) {
  6007. return URI.buildUserinfo(parts) + URI.buildHost(parts);
  6008. };
  6009. URI.buildUserinfo = function(parts) {
  6010. var t = '';
  6011. if (parts.username) {
  6012. t += URI.encode(parts.username);
  6013. }
  6014. if (parts.password) {
  6015. t += ':' + URI.encode(parts.password);
  6016. }
  6017. if (t) {
  6018. t += '@';
  6019. }
  6020. return t;
  6021. };
  6022. URI.buildQuery = function(data, duplicateQueryParameters, escapeQuerySpace) {
  6023. // according to http://tools.ietf.org/html/rfc3986 or http://labs.apache.org/webarch/uri/rfc/rfc3986.html
  6024. // being »-._~!$&'()*+,;=:@/?« %HEX and alnum are allowed
  6025. // the RFC explicitly states ?/foo being a valid use case, no mention of parameter syntax!
  6026. // URI.js treats the query string as being application/x-www-form-urlencoded
  6027. // see http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type
  6028. var t = '';
  6029. var unique, key, i, length;
  6030. for (key in data) {
  6031. if (key === '__proto__') {
  6032. // ignore attempt at exploiting JavaScript internals
  6033. continue;
  6034. } else if (hasOwn.call(data, key)) {
  6035. if (isArray(data[key])) {
  6036. unique = {};
  6037. for (i = 0, length = data[key].length; i < length; i++) {
  6038. if (data[key][i] !== undefined && unique[data[key][i] + ''] === undefined) {
  6039. t += '&' + URI.buildQueryParameter(key, data[key][i], escapeQuerySpace);
  6040. if (duplicateQueryParameters !== true) {
  6041. unique[data[key][i] + ''] = true;
  6042. }
  6043. }
  6044. }
  6045. } else if (data[key] !== undefined) {
  6046. t += '&' + URI.buildQueryParameter(key, data[key], escapeQuerySpace);
  6047. }
  6048. }
  6049. }
  6050. return t.substring(1);
  6051. };
  6052. URI.buildQueryParameter = function(name, value, escapeQuerySpace) {
  6053. // http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type -- application/x-www-form-urlencoded
  6054. // don't append "=" for null values, according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#url-parameter-serialization
  6055. return URI.encodeQuery(name, escapeQuerySpace) + (value !== null ? '=' + URI.encodeQuery(value, escapeQuerySpace) : '');
  6056. };
  6057. URI.addQuery = function(data, name, value) {
  6058. if (typeof name === 'object') {
  6059. for (var key in name) {
  6060. if (hasOwn.call(name, key)) {
  6061. URI.addQuery(data, key, name[key]);
  6062. }
  6063. }
  6064. } else if (typeof name === 'string') {
  6065. if (data[name] === undefined) {
  6066. data[name] = value;
  6067. return;
  6068. } else if (typeof data[name] === 'string') {
  6069. data[name] = [data[name]];
  6070. }
  6071. if (!isArray(value)) {
  6072. value = [value];
  6073. }
  6074. data[name] = (data[name] || []).concat(value);
  6075. } else {
  6076. throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
  6077. }
  6078. };
  6079. URI.setQuery = function(data, name, value) {
  6080. if (typeof name === 'object') {
  6081. for (var key in name) {
  6082. if (hasOwn.call(name, key)) {
  6083. URI.setQuery(data, key, name[key]);
  6084. }
  6085. }
  6086. } else if (typeof name === 'string') {
  6087. data[name] = value === undefined ? null : value;
  6088. } else {
  6089. throw new TypeError('URI.setQuery() accepts an object, string as the name parameter');
  6090. }
  6091. };
  6092. URI.removeQuery = function(data, name, value) {
  6093. var i, length, key;
  6094. if (isArray(name)) {
  6095. for (i = 0, length = name.length; i < length; i++) {
  6096. data[name[i]] = undefined;
  6097. }
  6098. } else if (getType(name) === 'RegExp') {
  6099. for (key in data) {
  6100. if (name.test(key)) {
  6101. data[key] = undefined;
  6102. }
  6103. }
  6104. } else if (typeof name === 'object') {
  6105. for (key in name) {
  6106. if (hasOwn.call(name, key)) {
  6107. URI.removeQuery(data, key, name[key]);
  6108. }
  6109. }
  6110. } else if (typeof name === 'string') {
  6111. if (value !== undefined) {
  6112. if (getType(value) === 'RegExp') {
  6113. if (!isArray(data[name]) && value.test(data[name])) {
  6114. data[name] = undefined;
  6115. } else {
  6116. data[name] = filterArrayValues(data[name], value);
  6117. }
  6118. } else if (data[name] === String(value) && (!isArray(value) || value.length === 1)) {
  6119. data[name] = undefined;
  6120. } else if (isArray(data[name])) {
  6121. data[name] = filterArrayValues(data[name], value);
  6122. }
  6123. } else {
  6124. data[name] = undefined;
  6125. }
  6126. } else {
  6127. throw new TypeError('URI.removeQuery() accepts an object, string, RegExp as the first parameter');
  6128. }
  6129. };
  6130. URI.hasQuery = function(data, name, value, withinArray) {
  6131. switch (getType(name)) {
  6132. case 'String':
  6133. // Nothing to do here
  6134. break;
  6135. case 'RegExp':
  6136. for (var key in data) {
  6137. if (hasOwn.call(data, key)) {
  6138. if (name.test(key) && (value === undefined || URI.hasQuery(data, key, value))) {
  6139. return true;
  6140. }
  6141. }
  6142. }
  6143. return false;
  6144. case 'Object':
  6145. for (var _key in name) {
  6146. if (hasOwn.call(name, _key)) {
  6147. if (!URI.hasQuery(data, _key, name[_key])) {
  6148. return false;
  6149. }
  6150. }
  6151. }
  6152. return true;
  6153. default:
  6154. throw new TypeError('URI.hasQuery() accepts a string, regular expression or object as the name parameter');
  6155. }
  6156. switch (getType(value)) {
  6157. case 'Undefined':
  6158. // true if exists (but may be empty)
  6159. return name in data; // data[name] !== undefined;
  6160. case 'Boolean':
  6161. // true if exists and non-empty
  6162. var _booly = Boolean(isArray(data[name]) ? data[name].length : data[name]);
  6163. return value === _booly;
  6164. case 'Function':
  6165. // allow complex comparison
  6166. return !!value(data[name], name, data);
  6167. case 'Array':
  6168. if (!isArray(data[name])) {
  6169. return false;
  6170. }
  6171. var op = withinArray ? arrayContains : arraysEqual;
  6172. return op(data[name], value);
  6173. case 'RegExp':
  6174. if (!isArray(data[name])) {
  6175. return Boolean(data[name] && data[name].match(value));
  6176. }
  6177. if (!withinArray) {
  6178. return false;
  6179. }
  6180. return arrayContains(data[name], value);
  6181. case 'Number':
  6182. value = String(value);
  6183. /* falls through */
  6184. case 'String':
  6185. if (!isArray(data[name])) {
  6186. return data[name] === value;
  6187. }
  6188. if (!withinArray) {
  6189. return false;
  6190. }
  6191. return arrayContains(data[name], value);
  6192. default:
  6193. throw new TypeError('URI.hasQuery() accepts undefined, boolean, string, number, RegExp, Function as the value parameter');
  6194. }
  6195. };
  6196. URI.joinPaths = function() {
  6197. var input = [];
  6198. var segments = [];
  6199. var nonEmptySegments = 0;
  6200. for (var i = 0; i < arguments.length; i++) {
  6201. var url = new URI(arguments[i]);
  6202. input.push(url);
  6203. var _segments = url.segment();
  6204. for (var s = 0; s < _segments.length; s++) {
  6205. if (typeof _segments[s] === 'string') {
  6206. segments.push(_segments[s]);
  6207. }
  6208. if (_segments[s]) {
  6209. nonEmptySegments++;
  6210. }
  6211. }
  6212. }
  6213. if (!segments.length || !nonEmptySegments) {
  6214. return new URI('');
  6215. }
  6216. var uri = new URI('').segment(segments);
  6217. if (input[0].path() === '' || input[0].path().slice(0, 1) === '/') {
  6218. uri.path('/' + uri.path());
  6219. }
  6220. return uri.normalize();
  6221. };
  6222. URI.commonPath = function(one, two) {
  6223. var length = Math.min(one.length, two.length);
  6224. var pos;
  6225. // find first non-matching character
  6226. for (pos = 0; pos < length; pos++) {
  6227. if (one.charAt(pos) !== two.charAt(pos)) {
  6228. pos--;
  6229. break;
  6230. }
  6231. }
  6232. if (pos < 1) {
  6233. return one.charAt(0) === two.charAt(0) && one.charAt(0) === '/' ? '/' : '';
  6234. }
  6235. // revert to last /
  6236. if (one.charAt(pos) !== '/' || two.charAt(pos) !== '/') {
  6237. pos = one.substring(0, pos).lastIndexOf('/');
  6238. }
  6239. return one.substring(0, pos + 1);
  6240. };
  6241. URI.withinString = function(string, callback, options) {
  6242. options || (options = {});
  6243. var _start = options.start || URI.findUri.start;
  6244. var _end = options.end || URI.findUri.end;
  6245. var _trim = options.trim || URI.findUri.trim;
  6246. var _parens = options.parens || URI.findUri.parens;
  6247. var _attributeOpen = /[a-z0-9-]=["']?$/i;
  6248. _start.lastIndex = 0;
  6249. while (true) {
  6250. var match = _start.exec(string);
  6251. if (!match) {
  6252. break;
  6253. }
  6254. var start = match.index;
  6255. if (options.ignoreHtml) {
  6256. // attribut(e=["']?$)
  6257. var attributeOpen = string.slice(Math.max(start - 3, 0), start);
  6258. if (attributeOpen && _attributeOpen.test(attributeOpen)) {
  6259. continue;
  6260. }
  6261. }
  6262. var end = start + string.slice(start).search(_end);
  6263. var slice = string.slice(start, end);
  6264. // make sure we include well balanced parens
  6265. var parensEnd = -1;
  6266. while (true) {
  6267. var parensMatch = _parens.exec(slice);
  6268. if (!parensMatch) {
  6269. break;
  6270. }
  6271. var parensMatchEnd = parensMatch.index + parensMatch[0].length;
  6272. parensEnd = Math.max(parensEnd, parensMatchEnd);
  6273. }
  6274. if (parensEnd > -1) {
  6275. slice = slice.slice(0, parensEnd) + slice.slice(parensEnd).replace(_trim, '');
  6276. } else {
  6277. slice = slice.replace(_trim, '');
  6278. }
  6279. if (slice.length <= match[0].length) {
  6280. // the extract only contains the starting marker of a URI,
  6281. // e.g. "www" or "http://"
  6282. continue;
  6283. }
  6284. if (options.ignore && options.ignore.test(slice)) {
  6285. continue;
  6286. }
  6287. end = start + slice.length;
  6288. var result = callback(slice, start, end, string);
  6289. if (result === undefined) {
  6290. _start.lastIndex = end;
  6291. continue;
  6292. }
  6293. result = String(result);
  6294. string = string.slice(0, start) + result + string.slice(end);
  6295. _start.lastIndex = start + result.length;
  6296. }
  6297. _start.lastIndex = 0;
  6298. return string;
  6299. };
  6300. URI.ensureValidHostname = function(v, protocol) {
  6301. // Theoretically URIs allow percent-encoding in Hostnames (according to RFC 3986)
  6302. // they are not part of DNS and therefore ignored by URI.js
  6303. var hasHostname = !!v; // not null and not an empty string
  6304. var hasProtocol = !!protocol;
  6305. var rejectEmptyHostname = false;
  6306. if (hasProtocol) {
  6307. rejectEmptyHostname = arrayContains(URI.hostProtocols, protocol);
  6308. }
  6309. if (rejectEmptyHostname && !hasHostname) {
  6310. throw new TypeError('Hostname cannot be empty, if protocol is ' + protocol);
  6311. } else if (v && v.match(URI.invalid_hostname_characters)) {
  6312. // test punycode
  6313. if (!punycode) {
  6314. throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-:_] and Punycode.js is not available');
  6315. }
  6316. if (punycode.toASCII(v).match(URI.invalid_hostname_characters)) {
  6317. throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-:_]');
  6318. }
  6319. }
  6320. };
  6321. URI.ensureValidPort = function (v) {
  6322. if (!v) {
  6323. return;
  6324. }
  6325. var port = Number(v);
  6326. if (isInteger(port) && (port > 0) && (port < 65536)) {
  6327. return;
  6328. }
  6329. throw new TypeError('Port "' + v + '" is not a valid port');
  6330. };
  6331. // noConflict
  6332. URI.noConflict = function(removeAll) {
  6333. if (removeAll) {
  6334. var unconflicted = {
  6335. URI: this.noConflict()
  6336. };
  6337. if (root.URITemplate && typeof root.URITemplate.noConflict === 'function') {
  6338. unconflicted.URITemplate = root.URITemplate.noConflict();
  6339. }
  6340. if (root.IPv6 && typeof root.IPv6.noConflict === 'function') {
  6341. unconflicted.IPv6 = root.IPv6.noConflict();
  6342. }
  6343. if (root.SecondLevelDomains && typeof root.SecondLevelDomains.noConflict === 'function') {
  6344. unconflicted.SecondLevelDomains = root.SecondLevelDomains.noConflict();
  6345. }
  6346. return unconflicted;
  6347. } else if (root.URI === this) {
  6348. root.URI = _URI;
  6349. }
  6350. return this;
  6351. };
  6352. p.build = function(deferBuild) {
  6353. if (deferBuild === true) {
  6354. this._deferred_build = true;
  6355. } else if (deferBuild === undefined || this._deferred_build) {
  6356. this._string = URI.build(this._parts);
  6357. this._deferred_build = false;
  6358. }
  6359. return this;
  6360. };
  6361. p.clone = function() {
  6362. return new URI(this);
  6363. };
  6364. p.valueOf = p.toString = function() {
  6365. return this.build(false)._string;
  6366. };
  6367. function generateSimpleAccessor(_part){
  6368. return function(v, build) {
  6369. if (v === undefined) {
  6370. return this._parts[_part] || '';
  6371. } else {
  6372. this._parts[_part] = v || null;
  6373. this.build(!build);
  6374. return this;
  6375. }
  6376. };
  6377. }
  6378. function generatePrefixAccessor(_part, _key){
  6379. return function(v, build) {
  6380. if (v === undefined) {
  6381. return this._parts[_part] || '';
  6382. } else {
  6383. if (v !== null) {
  6384. v = v + '';
  6385. if (v.charAt(0) === _key) {
  6386. v = v.substring(1);
  6387. }
  6388. }
  6389. this._parts[_part] = v;
  6390. this.build(!build);
  6391. return this;
  6392. }
  6393. };
  6394. }
  6395. p.protocol = generateSimpleAccessor('protocol');
  6396. p.username = generateSimpleAccessor('username');
  6397. p.password = generateSimpleAccessor('password');
  6398. p.hostname = generateSimpleAccessor('hostname');
  6399. p.port = generateSimpleAccessor('port');
  6400. p.query = generatePrefixAccessor('query', '?');
  6401. p.fragment = generatePrefixAccessor('fragment', '#');
  6402. p.search = function(v, build) {
  6403. var t = this.query(v, build);
  6404. return typeof t === 'string' && t.length ? ('?' + t) : t;
  6405. };
  6406. p.hash = function(v, build) {
  6407. var t = this.fragment(v, build);
  6408. return typeof t === 'string' && t.length ? ('#' + t) : t;
  6409. };
  6410. p.pathname = function(v, build) {
  6411. if (v === undefined || v === true) {
  6412. var res = this._parts.path || (this._parts.hostname ? '/' : '');
  6413. return v ? (this._parts.urn ? URI.decodeUrnPath : URI.decodePath)(res) : res;
  6414. } else {
  6415. if (this._parts.urn) {
  6416. this._parts.path = v ? URI.recodeUrnPath(v) : '';
  6417. } else {
  6418. this._parts.path = v ? URI.recodePath(v) : '/';
  6419. }
  6420. this.build(!build);
  6421. return this;
  6422. }
  6423. };
  6424. p.path = p.pathname;
  6425. p.href = function(href, build) {
  6426. var key;
  6427. if (href === undefined) {
  6428. return this.toString();
  6429. }
  6430. this._string = '';
  6431. this._parts = URI._parts();
  6432. var _URI = href instanceof URI;
  6433. var _object = typeof href === 'object' && (href.hostname || href.path || href.pathname);
  6434. if (href.nodeName) {
  6435. var attribute = URI.getDomAttribute(href);
  6436. href = href[attribute] || '';
  6437. _object = false;
  6438. }
  6439. // window.location is reported to be an object, but it's not the sort
  6440. // of object we're looking for:
  6441. // * location.protocol ends with a colon
  6442. // * location.query != object.search
  6443. // * location.hash != object.fragment
  6444. // simply serializing the unknown object should do the trick
  6445. // (for location, not for everything...)
  6446. if (!_URI && _object && href.pathname !== undefined) {
  6447. href = href.toString();
  6448. }
  6449. if (typeof href === 'string' || href instanceof String) {
  6450. this._parts = URI.parse(String(href), this._parts);
  6451. } else if (_URI || _object) {
  6452. var src = _URI ? href._parts : href;
  6453. for (key in src) {
  6454. if (key === 'query') { continue; }
  6455. if (hasOwn.call(this._parts, key)) {
  6456. this._parts[key] = src[key];
  6457. }
  6458. }
  6459. if (src.query) {
  6460. this.query(src.query, false);
  6461. }
  6462. } else {
  6463. throw new TypeError('invalid input');
  6464. }
  6465. this.build(!build);
  6466. return this;
  6467. };
  6468. // identification accessors
  6469. p.is = function(what) {
  6470. var ip = false;
  6471. var ip4 = false;
  6472. var ip6 = false;
  6473. var name = false;
  6474. var sld = false;
  6475. var idn = false;
  6476. var punycode = false;
  6477. var relative = !this._parts.urn;
  6478. if (this._parts.hostname) {
  6479. relative = false;
  6480. ip4 = URI.ip4_expression.test(this._parts.hostname);
  6481. ip6 = URI.ip6_expression.test(this._parts.hostname);
  6482. ip = ip4 || ip6;
  6483. name = !ip;
  6484. sld = name && SLD && SLD.has(this._parts.hostname);
  6485. idn = name && URI.idn_expression.test(this._parts.hostname);
  6486. punycode = name && URI.punycode_expression.test(this._parts.hostname);
  6487. }
  6488. switch (what.toLowerCase()) {
  6489. case 'relative':
  6490. return relative;
  6491. case 'absolute':
  6492. return !relative;
  6493. // hostname identification
  6494. case 'domain':
  6495. case 'name':
  6496. return name;
  6497. case 'sld':
  6498. return sld;
  6499. case 'ip':
  6500. return ip;
  6501. case 'ip4':
  6502. case 'ipv4':
  6503. case 'inet4':
  6504. return ip4;
  6505. case 'ip6':
  6506. case 'ipv6':
  6507. case 'inet6':
  6508. return ip6;
  6509. case 'idn':
  6510. return idn;
  6511. case 'url':
  6512. return !this._parts.urn;
  6513. case 'urn':
  6514. return !!this._parts.urn;
  6515. case 'punycode':
  6516. return punycode;
  6517. }
  6518. return null;
  6519. };
  6520. // component specific input validation
  6521. var _protocol = p.protocol;
  6522. var _port = p.port;
  6523. var _hostname = p.hostname;
  6524. p.protocol = function(v, build) {
  6525. if (v) {
  6526. // accept trailing ://
  6527. v = v.replace(/:(\/\/)?$/, '');
  6528. if (!v.match(URI.protocol_expression)) {
  6529. throw new TypeError('Protocol "' + v + '" contains characters other than [A-Z0-9.+-] or doesn\'t start with [A-Z]');
  6530. }
  6531. }
  6532. return _protocol.call(this, v, build);
  6533. };
  6534. p.scheme = p.protocol;
  6535. p.port = function(v, build) {
  6536. if (this._parts.urn) {
  6537. return v === undefined ? '' : this;
  6538. }
  6539. if (v !== undefined) {
  6540. if (v === 0) {
  6541. v = null;
  6542. }
  6543. if (v) {
  6544. v += '';
  6545. if (v.charAt(0) === ':') {
  6546. v = v.substring(1);
  6547. }
  6548. URI.ensureValidPort(v);
  6549. }
  6550. }
  6551. return _port.call(this, v, build);
  6552. };
  6553. p.hostname = function(v, build) {
  6554. if (this._parts.urn) {
  6555. return v === undefined ? '' : this;
  6556. }
  6557. if (v !== undefined) {
  6558. var x = { preventInvalidHostname: this._parts.preventInvalidHostname };
  6559. var res = URI.parseHost(v, x);
  6560. if (res !== '/') {
  6561. throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
  6562. }
  6563. v = x.hostname;
  6564. if (this._parts.preventInvalidHostname) {
  6565. URI.ensureValidHostname(v, this._parts.protocol);
  6566. }
  6567. }
  6568. return _hostname.call(this, v, build);
  6569. };
  6570. // compound accessors
  6571. p.origin = function(v, build) {
  6572. if (this._parts.urn) {
  6573. return v === undefined ? '' : this;
  6574. }
  6575. if (v === undefined) {
  6576. var protocol = this.protocol();
  6577. var authority = this.authority();
  6578. if (!authority) {
  6579. return '';
  6580. }
  6581. return (protocol ? protocol + '://' : '') + this.authority();
  6582. } else {
  6583. var origin = URI(v);
  6584. this
  6585. .protocol(origin.protocol())
  6586. .authority(origin.authority())
  6587. .build(!build);
  6588. return this;
  6589. }
  6590. };
  6591. p.host = function(v, build) {
  6592. if (this._parts.urn) {
  6593. return v === undefined ? '' : this;
  6594. }
  6595. if (v === undefined) {
  6596. return this._parts.hostname ? URI.buildHost(this._parts) : '';
  6597. } else {
  6598. var res = URI.parseHost(v, this._parts);
  6599. if (res !== '/') {
  6600. throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
  6601. }
  6602. this.build(!build);
  6603. return this;
  6604. }
  6605. };
  6606. p.authority = function(v, build) {
  6607. if (this._parts.urn) {
  6608. return v === undefined ? '' : this;
  6609. }
  6610. if (v === undefined) {
  6611. return this._parts.hostname ? URI.buildAuthority(this._parts) : '';
  6612. } else {
  6613. var res = URI.parseAuthority(v, this._parts);
  6614. if (res !== '/') {
  6615. throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
  6616. }
  6617. this.build(!build);
  6618. return this;
  6619. }
  6620. };
  6621. p.userinfo = function(v, build) {
  6622. if (this._parts.urn) {
  6623. return v === undefined ? '' : this;
  6624. }
  6625. if (v === undefined) {
  6626. var t = URI.buildUserinfo(this._parts);
  6627. return t ? t.substring(0, t.length -1) : t;
  6628. } else {
  6629. if (v[v.length-1] !== '@') {
  6630. v += '@';
  6631. }
  6632. URI.parseUserinfo(v, this._parts);
  6633. this.build(!build);
  6634. return this;
  6635. }
  6636. };
  6637. p.resource = function(v, build) {
  6638. var parts;
  6639. if (v === undefined) {
  6640. return this.path() + this.search() + this.hash();
  6641. }
  6642. parts = URI.parse(v);
  6643. this._parts.path = parts.path;
  6644. this._parts.query = parts.query;
  6645. this._parts.fragment = parts.fragment;
  6646. this.build(!build);
  6647. return this;
  6648. };
  6649. // fraction accessors
  6650. p.subdomain = function(v, build) {
  6651. if (this._parts.urn) {
  6652. return v === undefined ? '' : this;
  6653. }
  6654. // convenience, return "www" from "www.example.org"
  6655. if (v === undefined) {
  6656. if (!this._parts.hostname || this.is('IP')) {
  6657. return '';
  6658. }
  6659. // grab domain and add another segment
  6660. var end = this._parts.hostname.length - this.domain().length - 1;
  6661. return this._parts.hostname.substring(0, end) || '';
  6662. } else {
  6663. var e = this._parts.hostname.length - this.domain().length;
  6664. var sub = this._parts.hostname.substring(0, e);
  6665. var replace = new RegExp('^' + escapeRegEx(sub));
  6666. if (v && v.charAt(v.length - 1) !== '.') {
  6667. v += '.';
  6668. }
  6669. if (v.indexOf(':') !== -1) {
  6670. throw new TypeError('Domains cannot contain colons');
  6671. }
  6672. if (v) {
  6673. URI.ensureValidHostname(v, this._parts.protocol);
  6674. }
  6675. this._parts.hostname = this._parts.hostname.replace(replace, v);
  6676. this.build(!build);
  6677. return this;
  6678. }
  6679. };
  6680. p.domain = function(v, build) {
  6681. if (this._parts.urn) {
  6682. return v === undefined ? '' : this;
  6683. }
  6684. if (typeof v === 'boolean') {
  6685. build = v;
  6686. v = undefined;
  6687. }
  6688. // convenience, return "example.org" from "www.example.org"
  6689. if (v === undefined) {
  6690. if (!this._parts.hostname || this.is('IP')) {
  6691. return '';
  6692. }
  6693. // if hostname consists of 1 or 2 segments, it must be the domain
  6694. var t = this._parts.hostname.match(/\./g);
  6695. if (t && t.length < 2) {
  6696. return this._parts.hostname;
  6697. }
  6698. // grab tld and add another segment
  6699. var end = this._parts.hostname.length - this.tld(build).length - 1;
  6700. end = this._parts.hostname.lastIndexOf('.', end -1) + 1;
  6701. return this._parts.hostname.substring(end) || '';
  6702. } else {
  6703. if (!v) {
  6704. throw new TypeError('cannot set domain empty');
  6705. }
  6706. if (v.indexOf(':') !== -1) {
  6707. throw new TypeError('Domains cannot contain colons');
  6708. }
  6709. URI.ensureValidHostname(v, this._parts.protocol);
  6710. if (!this._parts.hostname || this.is('IP')) {
  6711. this._parts.hostname = v;
  6712. } else {
  6713. var replace = new RegExp(escapeRegEx(this.domain()) + '$');
  6714. this._parts.hostname = this._parts.hostname.replace(replace, v);
  6715. }
  6716. this.build(!build);
  6717. return this;
  6718. }
  6719. };
  6720. p.tld = function(v, build) {
  6721. if (this._parts.urn) {
  6722. return v === undefined ? '' : this;
  6723. }
  6724. if (typeof v === 'boolean') {
  6725. build = v;
  6726. v = undefined;
  6727. }
  6728. // return "org" from "www.example.org"
  6729. if (v === undefined) {
  6730. if (!this._parts.hostname || this.is('IP')) {
  6731. return '';
  6732. }
  6733. var pos = this._parts.hostname.lastIndexOf('.');
  6734. var tld = this._parts.hostname.substring(pos + 1);
  6735. if (build !== true && SLD && SLD.list[tld.toLowerCase()]) {
  6736. return SLD.get(this._parts.hostname) || tld;
  6737. }
  6738. return tld;
  6739. } else {
  6740. var replace;
  6741. if (!v) {
  6742. throw new TypeError('cannot set TLD empty');
  6743. } else if (v.match(/[^a-zA-Z0-9-]/)) {
  6744. if (SLD && SLD.is(v)) {
  6745. replace = new RegExp(escapeRegEx(this.tld()) + '$');
  6746. this._parts.hostname = this._parts.hostname.replace(replace, v);
  6747. } else {
  6748. throw new TypeError('TLD "' + v + '" contains characters other than [A-Z0-9]');
  6749. }
  6750. } else if (!this._parts.hostname || this.is('IP')) {
  6751. throw new ReferenceError('cannot set TLD on non-domain host');
  6752. } else {
  6753. replace = new RegExp(escapeRegEx(this.tld()) + '$');
  6754. this._parts.hostname = this._parts.hostname.replace(replace, v);
  6755. }
  6756. this.build(!build);
  6757. return this;
  6758. }
  6759. };
  6760. p.directory = function(v, build) {
  6761. if (this._parts.urn) {
  6762. return v === undefined ? '' : this;
  6763. }
  6764. if (v === undefined || v === true) {
  6765. if (!this._parts.path && !this._parts.hostname) {
  6766. return '';
  6767. }
  6768. if (this._parts.path === '/') {
  6769. return '/';
  6770. }
  6771. var end = this._parts.path.length - this.filename().length - 1;
  6772. var res = this._parts.path.substring(0, end) || (this._parts.hostname ? '/' : '');
  6773. return v ? URI.decodePath(res) : res;
  6774. } else {
  6775. var e = this._parts.path.length - this.filename().length;
  6776. var directory = this._parts.path.substring(0, e);
  6777. var replace = new RegExp('^' + escapeRegEx(directory));
  6778. // fully qualifier directories begin with a slash
  6779. if (!this.is('relative')) {
  6780. if (!v) {
  6781. v = '/';
  6782. }
  6783. if (v.charAt(0) !== '/') {
  6784. v = '/' + v;
  6785. }
  6786. }
  6787. // directories always end with a slash
  6788. if (v && v.charAt(v.length - 1) !== '/') {
  6789. v += '/';
  6790. }
  6791. v = URI.recodePath(v);
  6792. this._parts.path = this._parts.path.replace(replace, v);
  6793. this.build(!build);
  6794. return this;
  6795. }
  6796. };
  6797. p.filename = function(v, build) {
  6798. if (this._parts.urn) {
  6799. return v === undefined ? '' : this;
  6800. }
  6801. if (typeof v !== 'string') {
  6802. if (!this._parts.path || this._parts.path === '/') {
  6803. return '';
  6804. }
  6805. var pos = this._parts.path.lastIndexOf('/');
  6806. var res = this._parts.path.substring(pos+1);
  6807. return v ? URI.decodePathSegment(res) : res;
  6808. } else {
  6809. var mutatedDirectory = false;
  6810. if (v.charAt(0) === '/') {
  6811. v = v.substring(1);
  6812. }
  6813. if (v.match(/\.?\//)) {
  6814. mutatedDirectory = true;
  6815. }
  6816. var replace = new RegExp(escapeRegEx(this.filename()) + '$');
  6817. v = URI.recodePath(v);
  6818. this._parts.path = this._parts.path.replace(replace, v);
  6819. if (mutatedDirectory) {
  6820. this.normalizePath(build);
  6821. } else {
  6822. this.build(!build);
  6823. }
  6824. return this;
  6825. }
  6826. };
  6827. p.suffix = function(v, build) {
  6828. if (this._parts.urn) {
  6829. return v === undefined ? '' : this;
  6830. }
  6831. if (v === undefined || v === true) {
  6832. if (!this._parts.path || this._parts.path === '/') {
  6833. return '';
  6834. }
  6835. var filename = this.filename();
  6836. var pos = filename.lastIndexOf('.');
  6837. var s, res;
  6838. if (pos === -1) {
  6839. return '';
  6840. }
  6841. // suffix may only contain alnum characters (yup, I made this up.)
  6842. s = filename.substring(pos+1);
  6843. res = (/^[a-z0-9%]+$/i).test(s) ? s : '';
  6844. return v ? URI.decodePathSegment(res) : res;
  6845. } else {
  6846. if (v.charAt(0) === '.') {
  6847. v = v.substring(1);
  6848. }
  6849. var suffix = this.suffix();
  6850. var replace;
  6851. if (!suffix) {
  6852. if (!v) {
  6853. return this;
  6854. }
  6855. this._parts.path += '.' + URI.recodePath(v);
  6856. } else if (!v) {
  6857. replace = new RegExp(escapeRegEx('.' + suffix) + '$');
  6858. } else {
  6859. replace = new RegExp(escapeRegEx(suffix) + '$');
  6860. }
  6861. if (replace) {
  6862. v = URI.recodePath(v);
  6863. this._parts.path = this._parts.path.replace(replace, v);
  6864. }
  6865. this.build(!build);
  6866. return this;
  6867. }
  6868. };
  6869. p.segment = function(segment, v, build) {
  6870. var separator = this._parts.urn ? ':' : '/';
  6871. var path = this.path();
  6872. var absolute = path.substring(0, 1) === '/';
  6873. var segments = path.split(separator);
  6874. if (segment !== undefined && typeof segment !== 'number') {
  6875. build = v;
  6876. v = segment;
  6877. segment = undefined;
  6878. }
  6879. if (segment !== undefined && typeof segment !== 'number') {
  6880. throw new Error('Bad segment "' + segment + '", must be 0-based integer');
  6881. }
  6882. if (absolute) {
  6883. segments.shift();
  6884. }
  6885. if (segment < 0) {
  6886. // allow negative indexes to address from the end
  6887. segment = Math.max(segments.length + segment, 0);
  6888. }
  6889. if (v === undefined) {
  6890. /*jshint laxbreak: true */
  6891. return segment === undefined
  6892. ? segments
  6893. : segments[segment];
  6894. /*jshint laxbreak: false */
  6895. } else if (segment === null || segments[segment] === undefined) {
  6896. if (isArray(v)) {
  6897. segments = [];
  6898. // collapse empty elements within array
  6899. for (var i=0, l=v.length; i < l; i++) {
  6900. if (!v[i].length && (!segments.length || !segments[segments.length -1].length)) {
  6901. continue;
  6902. }
  6903. if (segments.length && !segments[segments.length -1].length) {
  6904. segments.pop();
  6905. }
  6906. segments.push(trimSlashes(v[i]));
  6907. }
  6908. } else if (v || typeof v === 'string') {
  6909. v = trimSlashes(v);
  6910. if (segments[segments.length -1] === '') {
  6911. // empty trailing elements have to be overwritten
  6912. // to prevent results such as /foo//bar
  6913. segments[segments.length -1] = v;
  6914. } else {
  6915. segments.push(v);
  6916. }
  6917. }
  6918. } else {
  6919. if (v) {
  6920. segments[segment] = trimSlashes(v);
  6921. } else {
  6922. segments.splice(segment, 1);
  6923. }
  6924. }
  6925. if (absolute) {
  6926. segments.unshift('');
  6927. }
  6928. return this.path(segments.join(separator), build);
  6929. };
  6930. p.segmentCoded = function(segment, v, build) {
  6931. var segments, i, l;
  6932. if (typeof segment !== 'number') {
  6933. build = v;
  6934. v = segment;
  6935. segment = undefined;
  6936. }
  6937. if (v === undefined) {
  6938. segments = this.segment(segment, v, build);
  6939. if (!isArray(segments)) {
  6940. segments = segments !== undefined ? URI.decode(segments) : undefined;
  6941. } else {
  6942. for (i = 0, l = segments.length; i < l; i++) {
  6943. segments[i] = URI.decode(segments[i]);
  6944. }
  6945. }
  6946. return segments;
  6947. }
  6948. if (!isArray(v)) {
  6949. v = (typeof v === 'string' || v instanceof String) ? URI.encode(v) : v;
  6950. } else {
  6951. for (i = 0, l = v.length; i < l; i++) {
  6952. v[i] = URI.encode(v[i]);
  6953. }
  6954. }
  6955. return this.segment(segment, v, build);
  6956. };
  6957. // mutating query string
  6958. var q = p.query;
  6959. p.query = function(v, build) {
  6960. if (v === true) {
  6961. return URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
  6962. } else if (typeof v === 'function') {
  6963. var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
  6964. var result = v.call(this, data);
  6965. this._parts.query = URI.buildQuery(result || data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
  6966. this.build(!build);
  6967. return this;
  6968. } else if (v !== undefined && typeof v !== 'string') {
  6969. this._parts.query = URI.buildQuery(v, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
  6970. this.build(!build);
  6971. return this;
  6972. } else {
  6973. return q.call(this, v, build);
  6974. }
  6975. };
  6976. p.setQuery = function(name, value, build) {
  6977. var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
  6978. if (typeof name === 'string' || name instanceof String) {
  6979. data[name] = value !== undefined ? value : null;
  6980. } else if (typeof name === 'object') {
  6981. for (var key in name) {
  6982. if (hasOwn.call(name, key)) {
  6983. data[key] = name[key];
  6984. }
  6985. }
  6986. } else {
  6987. throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
  6988. }
  6989. this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
  6990. if (typeof name !== 'string') {
  6991. build = value;
  6992. }
  6993. this.build(!build);
  6994. return this;
  6995. };
  6996. p.addQuery = function(name, value, build) {
  6997. var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
  6998. URI.addQuery(data, name, value === undefined ? null : value);
  6999. this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
  7000. if (typeof name !== 'string') {
  7001. build = value;
  7002. }
  7003. this.build(!build);
  7004. return this;
  7005. };
  7006. p.removeQuery = function(name, value, build) {
  7007. var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
  7008. URI.removeQuery(data, name, value);
  7009. this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
  7010. if (typeof name !== 'string') {
  7011. build = value;
  7012. }
  7013. this.build(!build);
  7014. return this;
  7015. };
  7016. p.hasQuery = function(name, value, withinArray) {
  7017. var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
  7018. return URI.hasQuery(data, name, value, withinArray);
  7019. };
  7020. p.setSearch = p.setQuery;
  7021. p.addSearch = p.addQuery;
  7022. p.removeSearch = p.removeQuery;
  7023. p.hasSearch = p.hasQuery;
  7024. // sanitizing URLs
  7025. p.normalize = function() {
  7026. if (this._parts.urn) {
  7027. return this
  7028. .normalizeProtocol(false)
  7029. .normalizePath(false)
  7030. .normalizeQuery(false)
  7031. .normalizeFragment(false)
  7032. .build();
  7033. }
  7034. return this
  7035. .normalizeProtocol(false)
  7036. .normalizeHostname(false)
  7037. .normalizePort(false)
  7038. .normalizePath(false)
  7039. .normalizeQuery(false)
  7040. .normalizeFragment(false)
  7041. .build();
  7042. };
  7043. p.normalizeProtocol = function(build) {
  7044. if (typeof this._parts.protocol === 'string') {
  7045. this._parts.protocol = this._parts.protocol.toLowerCase();
  7046. this.build(!build);
  7047. }
  7048. return this;
  7049. };
  7050. p.normalizeHostname = function(build) {
  7051. if (this._parts.hostname) {
  7052. if (this.is('IDN') && punycode) {
  7053. this._parts.hostname = punycode.toASCII(this._parts.hostname);
  7054. } else if (this.is('IPv6') && IPv6) {
  7055. this._parts.hostname = IPv6.best(this._parts.hostname);
  7056. }
  7057. this._parts.hostname = this._parts.hostname.toLowerCase();
  7058. this.build(!build);
  7059. }
  7060. return this;
  7061. };
  7062. p.normalizePort = function(build) {
  7063. // remove port of it's the protocol's default
  7064. if (typeof this._parts.protocol === 'string' && this._parts.port === URI.defaultPorts[this._parts.protocol]) {
  7065. this._parts.port = null;
  7066. this.build(!build);
  7067. }
  7068. return this;
  7069. };
  7070. p.normalizePath = function(build) {
  7071. var _path = this._parts.path;
  7072. if (!_path) {
  7073. return this;
  7074. }
  7075. if (this._parts.urn) {
  7076. this._parts.path = URI.recodeUrnPath(this._parts.path);
  7077. this.build(!build);
  7078. return this;
  7079. }
  7080. if (this._parts.path === '/') {
  7081. return this;
  7082. }
  7083. _path = URI.recodePath(_path);
  7084. var _was_relative;
  7085. var _leadingParents = '';
  7086. var _parent, _pos;
  7087. // handle relative paths
  7088. if (_path.charAt(0) !== '/') {
  7089. _was_relative = true;
  7090. _path = '/' + _path;
  7091. }
  7092. // handle relative files (as opposed to directories)
  7093. if (_path.slice(-3) === '/..' || _path.slice(-2) === '/.') {
  7094. _path += '/';
  7095. }
  7096. // resolve simples
  7097. _path = _path
  7098. .replace(/(\/(\.\/)+)|(\/\.$)/g, '/')
  7099. .replace(/\/{2,}/g, '/');
  7100. // remember leading parents
  7101. if (_was_relative) {
  7102. _leadingParents = _path.substring(1).match(/^(\.\.\/)+/) || '';
  7103. if (_leadingParents) {
  7104. _leadingParents = _leadingParents[0];
  7105. }
  7106. }
  7107. // resolve parents
  7108. while (true) {
  7109. _parent = _path.search(/\/\.\.(\/|$)/);
  7110. if (_parent === -1) {
  7111. // no more ../ to resolve
  7112. break;
  7113. } else if (_parent === 0) {
  7114. // top level cannot be relative, skip it
  7115. _path = _path.substring(3);
  7116. continue;
  7117. }
  7118. _pos = _path.substring(0, _parent).lastIndexOf('/');
  7119. if (_pos === -1) {
  7120. _pos = _parent;
  7121. }
  7122. _path = _path.substring(0, _pos) + _path.substring(_parent + 3);
  7123. }
  7124. // revert to relative
  7125. if (_was_relative && this.is('relative')) {
  7126. _path = _leadingParents + _path.substring(1);
  7127. }
  7128. this._parts.path = _path;
  7129. this.build(!build);
  7130. return this;
  7131. };
  7132. p.normalizePathname = p.normalizePath;
  7133. p.normalizeQuery = function(build) {
  7134. if (typeof this._parts.query === 'string') {
  7135. if (!this._parts.query.length) {
  7136. this._parts.query = null;
  7137. } else {
  7138. this.query(URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace));
  7139. }
  7140. this.build(!build);
  7141. }
  7142. return this;
  7143. };
  7144. p.normalizeFragment = function(build) {
  7145. if (!this._parts.fragment) {
  7146. this._parts.fragment = null;
  7147. this.build(!build);
  7148. }
  7149. return this;
  7150. };
  7151. p.normalizeSearch = p.normalizeQuery;
  7152. p.normalizeHash = p.normalizeFragment;
  7153. p.iso8859 = function() {
  7154. // expect unicode input, iso8859 output
  7155. var e = URI.encode;
  7156. var d = URI.decode;
  7157. URI.encode = escape;
  7158. URI.decode = decodeURIComponent;
  7159. try {
  7160. this.normalize();
  7161. } finally {
  7162. URI.encode = e;
  7163. URI.decode = d;
  7164. }
  7165. return this;
  7166. };
  7167. p.unicode = function() {
  7168. // expect iso8859 input, unicode output
  7169. var e = URI.encode;
  7170. var d = URI.decode;
  7171. URI.encode = strictEncodeURIComponent;
  7172. URI.decode = unescape;
  7173. try {
  7174. this.normalize();
  7175. } finally {
  7176. URI.encode = e;
  7177. URI.decode = d;
  7178. }
  7179. return this;
  7180. };
  7181. p.readable = function() {
  7182. var uri = this.clone();
  7183. // removing username, password, because they shouldn't be displayed according to RFC 3986
  7184. uri.username('').password('').normalize();
  7185. var t = '';
  7186. if (uri._parts.protocol) {
  7187. t += uri._parts.protocol + '://';
  7188. }
  7189. if (uri._parts.hostname) {
  7190. if (uri.is('punycode') && punycode) {
  7191. t += punycode.toUnicode(uri._parts.hostname);
  7192. if (uri._parts.port) {
  7193. t += ':' + uri._parts.port;
  7194. }
  7195. } else {
  7196. t += uri.host();
  7197. }
  7198. }
  7199. if (uri._parts.hostname && uri._parts.path && uri._parts.path.charAt(0) !== '/') {
  7200. t += '/';
  7201. }
  7202. t += uri.path(true);
  7203. if (uri._parts.query) {
  7204. var q = '';
  7205. for (var i = 0, qp = uri._parts.query.split('&'), l = qp.length; i < l; i++) {
  7206. var kv = (qp[i] || '').split('=');
  7207. q += '&' + URI.decodeQuery(kv[0], this._parts.escapeQuerySpace)
  7208. .replace(/&/g, '%26');
  7209. if (kv[1] !== undefined) {
  7210. q += '=' + URI.decodeQuery(kv[1], this._parts.escapeQuerySpace)
  7211. .replace(/&/g, '%26');
  7212. }
  7213. }
  7214. t += '?' + q.substring(1);
  7215. }
  7216. t += URI.decodeQuery(uri.hash(), true);
  7217. return t;
  7218. };
  7219. // resolving relative and absolute URLs
  7220. p.absoluteTo = function(base) {
  7221. var resolved = this.clone();
  7222. var properties = ['protocol', 'username', 'password', 'hostname', 'port'];
  7223. var basedir, i, p;
  7224. if (this._parts.urn) {
  7225. throw new Error('URNs do not have any generally defined hierarchical components');
  7226. }
  7227. if (!(base instanceof URI)) {
  7228. base = new URI(base);
  7229. }
  7230. if (resolved._parts.protocol) {
  7231. // Directly returns even if this._parts.hostname is empty.
  7232. return resolved;
  7233. } else {
  7234. resolved._parts.protocol = base._parts.protocol;
  7235. }
  7236. if (this._parts.hostname) {
  7237. return resolved;
  7238. }
  7239. for (i = 0; (p = properties[i]); i++) {
  7240. resolved._parts[p] = base._parts[p];
  7241. }
  7242. if (!resolved._parts.path) {
  7243. resolved._parts.path = base._parts.path;
  7244. if (!resolved._parts.query) {
  7245. resolved._parts.query = base._parts.query;
  7246. }
  7247. } else {
  7248. if (resolved._parts.path.substring(-2) === '..') {
  7249. resolved._parts.path += '/';
  7250. }
  7251. if (resolved.path().charAt(0) !== '/') {
  7252. basedir = base.directory();
  7253. basedir = basedir ? basedir : base.path().indexOf('/') === 0 ? '/' : '';
  7254. resolved._parts.path = (basedir ? (basedir + '/') : '') + resolved._parts.path;
  7255. resolved.normalizePath();
  7256. }
  7257. }
  7258. resolved.build();
  7259. return resolved;
  7260. };
  7261. p.relativeTo = function(base) {
  7262. var relative = this.clone().normalize();
  7263. var relativeParts, baseParts, common, relativePath, basePath;
  7264. if (relative._parts.urn) {
  7265. throw new Error('URNs do not have any generally defined hierarchical components');
  7266. }
  7267. base = new URI(base).normalize();
  7268. relativeParts = relative._parts;
  7269. baseParts = base._parts;
  7270. relativePath = relative.path();
  7271. basePath = base.path();
  7272. if (relativePath.charAt(0) !== '/') {
  7273. throw new Error('URI is already relative');
  7274. }
  7275. if (basePath.charAt(0) !== '/') {
  7276. throw new Error('Cannot calculate a URI relative to another relative URI');
  7277. }
  7278. if (relativeParts.protocol === baseParts.protocol) {
  7279. relativeParts.protocol = null;
  7280. }
  7281. if (relativeParts.username !== baseParts.username || relativeParts.password !== baseParts.password) {
  7282. return relative.build();
  7283. }
  7284. if (relativeParts.protocol !== null || relativeParts.username !== null || relativeParts.password !== null) {
  7285. return relative.build();
  7286. }
  7287. if (relativeParts.hostname === baseParts.hostname && relativeParts.port === baseParts.port) {
  7288. relativeParts.hostname = null;
  7289. relativeParts.port = null;
  7290. } else {
  7291. return relative.build();
  7292. }
  7293. if (relativePath === basePath) {
  7294. relativeParts.path = '';
  7295. return relative.build();
  7296. }
  7297. // determine common sub path
  7298. common = URI.commonPath(relativePath, basePath);
  7299. // If the paths have nothing in common, return a relative URL with the absolute path.
  7300. if (!common) {
  7301. return relative.build();
  7302. }
  7303. var parents = baseParts.path
  7304. .substring(common.length)
  7305. .replace(/[^\/]*$/, '')
  7306. .replace(/.*?\//g, '../');
  7307. relativeParts.path = (parents + relativeParts.path.substring(common.length)) || './';
  7308. return relative.build();
  7309. };
  7310. // comparing URIs
  7311. p.equals = function(uri) {
  7312. var one = this.clone();
  7313. var two = new URI(uri);
  7314. var one_map = {};
  7315. var two_map = {};
  7316. var checked = {};
  7317. var one_query, two_query, key;
  7318. one.normalize();
  7319. two.normalize();
  7320. // exact match
  7321. if (one.toString() === two.toString()) {
  7322. return true;
  7323. }
  7324. // extract query string
  7325. one_query = one.query();
  7326. two_query = two.query();
  7327. one.query('');
  7328. two.query('');
  7329. // definitely not equal if not even non-query parts match
  7330. if (one.toString() !== two.toString()) {
  7331. return false;
  7332. }
  7333. // query parameters have the same length, even if they're permuted
  7334. if (one_query.length !== two_query.length) {
  7335. return false;
  7336. }
  7337. one_map = URI.parseQuery(one_query, this._parts.escapeQuerySpace);
  7338. two_map = URI.parseQuery(two_query, this._parts.escapeQuerySpace);
  7339. for (key in one_map) {
  7340. if (hasOwn.call(one_map, key)) {
  7341. if (!isArray(one_map[key])) {
  7342. if (one_map[key] !== two_map[key]) {
  7343. return false;
  7344. }
  7345. } else if (!arraysEqual(one_map[key], two_map[key])) {
  7346. return false;
  7347. }
  7348. checked[key] = true;
  7349. }
  7350. }
  7351. for (key in two_map) {
  7352. if (hasOwn.call(two_map, key)) {
  7353. if (!checked[key]) {
  7354. // two contains a parameter not present in one
  7355. return false;
  7356. }
  7357. }
  7358. }
  7359. return true;
  7360. };
  7361. // state
  7362. p.preventInvalidHostname = function(v) {
  7363. this._parts.preventInvalidHostname = !!v;
  7364. return this;
  7365. };
  7366. p.duplicateQueryParameters = function(v) {
  7367. this._parts.duplicateQueryParameters = !!v;
  7368. return this;
  7369. };
  7370. p.escapeQuerySpace = function(v) {
  7371. this._parts.escapeQuerySpace = !!v;
  7372. return this;
  7373. };
  7374. return URI;
  7375. }));
  7376. } (URI));
  7377. var URIExports = URI.exports;
  7378. var Uri = /*@__PURE__*/Math$1.getDefaultExportFromCjs(URIExports);
  7379. /**
  7380. * @private
  7381. */
  7382. function appendForwardSlash(url) {
  7383. if (url.length === 0 || url[url.length - 1] !== "/") {
  7384. url = `${url}/`;
  7385. }
  7386. return url;
  7387. }
  7388. /**
  7389. * Clones an object, returning a new object containing the same properties.
  7390. *
  7391. * @function
  7392. *
  7393. * @param {object} object The object to clone.
  7394. * @param {boolean} [deep=false] If true, all properties will be deep cloned recursively.
  7395. * @returns {object} The cloned object.
  7396. */
  7397. function clone(object, deep) {
  7398. if (object === null || typeof object !== "object") {
  7399. return object;
  7400. }
  7401. deep = defaultValue.defaultValue(deep, false);
  7402. const result = new object.constructor();
  7403. for (const propertyName in object) {
  7404. if (object.hasOwnProperty(propertyName)) {
  7405. let value = object[propertyName];
  7406. if (deep) {
  7407. value = clone(value, deep);
  7408. }
  7409. result[propertyName] = value;
  7410. }
  7411. }
  7412. return result;
  7413. }
  7414. /**
  7415. * A function used to resolve a promise upon completion .
  7416. * @callback defer.resolve
  7417. *
  7418. * @param {*} value The resulting value.
  7419. */
  7420. /**
  7421. * A function used to reject a promise upon failure.
  7422. * @callback defer.reject
  7423. *
  7424. * @param {*} error The error.
  7425. */
  7426. /**
  7427. * An object which contains a promise object, and functions to resolve or reject the promise.
  7428. *
  7429. * @typedef {object} defer.deferred
  7430. * @property {defer.resolve} resolve Resolves the promise when called.
  7431. * @property {defer.reject} reject Rejects the promise when called.
  7432. * @property {Promise} promise Promise object.
  7433. */
  7434. /**
  7435. * Creates a deferred object, containing a promise object, and functions to resolve or reject the promise.
  7436. * @returns {defer.deferred}
  7437. * @private
  7438. */
  7439. function defer() {
  7440. let resolve;
  7441. let reject;
  7442. const promise = new Promise(function (res, rej) {
  7443. resolve = res;
  7444. reject = rej;
  7445. });
  7446. return {
  7447. resolve: resolve,
  7448. reject: reject,
  7449. promise: promise,
  7450. };
  7451. }
  7452. /**
  7453. * Given a relative Uri and a base Uri, returns the absolute Uri of the relative Uri.
  7454. * @function
  7455. *
  7456. * @param {string} relative The relative Uri.
  7457. * @param {string} [base] The base Uri.
  7458. * @returns {string} The absolute Uri of the given relative Uri.
  7459. *
  7460. * @example
  7461. * //absolute Uri will be "https://test.com/awesome.png";
  7462. * const absoluteUri = Cesium.getAbsoluteUri('awesome.png', 'https://test.com');
  7463. */
  7464. function getAbsoluteUri(relative, base) {
  7465. let documentObject;
  7466. if (typeof document !== "undefined") {
  7467. documentObject = document;
  7468. }
  7469. return getAbsoluteUri._implementation(relative, base, documentObject);
  7470. }
  7471. getAbsoluteUri._implementation = function (relative, base, documentObject) {
  7472. //>>includeStart('debug', pragmas.debug);
  7473. if (!defaultValue.defined(relative)) {
  7474. throw new Check.DeveloperError("relative uri is required.");
  7475. }
  7476. //>>includeEnd('debug');
  7477. if (!defaultValue.defined(base)) {
  7478. if (typeof documentObject === "undefined") {
  7479. return relative;
  7480. }
  7481. base = defaultValue.defaultValue(documentObject.baseURI, documentObject.location.href);
  7482. }
  7483. const relativeUri = new Uri(relative);
  7484. if (relativeUri.scheme() !== "") {
  7485. return relativeUri.toString();
  7486. }
  7487. return relativeUri.absoluteTo(base).toString();
  7488. };
  7489. /**
  7490. * Given a URI, returns the base path of the URI.
  7491. * @function
  7492. *
  7493. * @param {string} uri The Uri.
  7494. * @param {boolean} [includeQuery = false] Whether or not to include the query string and fragment form the uri
  7495. * @returns {string} The base path of the Uri.
  7496. *
  7497. * @example
  7498. * // basePath will be "/Gallery/";
  7499. * const basePath = Cesium.getBaseUri('/Gallery/simple.czml?value=true&example=false');
  7500. *
  7501. * // basePath will be "/Gallery/?value=true&example=false";
  7502. * const basePath = Cesium.getBaseUri('/Gallery/simple.czml?value=true&example=false', true);
  7503. */
  7504. function getBaseUri(uri, includeQuery) {
  7505. //>>includeStart('debug', pragmas.debug);
  7506. if (!defaultValue.defined(uri)) {
  7507. throw new Check.DeveloperError("uri is required.");
  7508. }
  7509. //>>includeEnd('debug');
  7510. let basePath = "";
  7511. const i = uri.lastIndexOf("/");
  7512. if (i !== -1) {
  7513. basePath = uri.substring(0, i + 1);
  7514. }
  7515. if (!includeQuery) {
  7516. return basePath;
  7517. }
  7518. uri = new Uri(uri);
  7519. if (uri.query().length !== 0) {
  7520. basePath += `?${uri.query()}`;
  7521. }
  7522. if (uri.fragment().length !== 0) {
  7523. basePath += `#${uri.fragment()}`;
  7524. }
  7525. return basePath;
  7526. }
  7527. /**
  7528. * Given a URI, returns the extension of the URI.
  7529. * @function getExtensionFromUri
  7530. *
  7531. * @param {string} uri The Uri.
  7532. * @returns {string} The extension of the Uri.
  7533. *
  7534. * @example
  7535. * //extension will be "czml";
  7536. * const extension = Cesium.getExtensionFromUri('/Gallery/simple.czml?value=true&example=false');
  7537. */
  7538. function getExtensionFromUri(uri) {
  7539. //>>includeStart('debug', pragmas.debug);
  7540. if (!defaultValue.defined(uri)) {
  7541. throw new Check.DeveloperError("uri is required.");
  7542. }
  7543. //>>includeEnd('debug');
  7544. const uriObject = new Uri(uri);
  7545. uriObject.normalize();
  7546. let path = uriObject.path();
  7547. let index = path.lastIndexOf("/");
  7548. if (index !== -1) {
  7549. path = path.substr(index + 1);
  7550. }
  7551. index = path.lastIndexOf(".");
  7552. if (index === -1) {
  7553. path = "";
  7554. } else {
  7555. path = path.substr(index + 1);
  7556. }
  7557. return path;
  7558. }
  7559. const context2DsByWidthAndHeight = {};
  7560. /**
  7561. * Extract a pixel array from a loaded image. Draws the image
  7562. * into a canvas so it can read the pixels back.
  7563. *
  7564. * @function getImagePixels
  7565. *
  7566. * @param {HTMLImageElement|ImageBitmap} image The image to extract pixels from.
  7567. * @param {number} width The width of the image. If not defined, then image.width is assigned.
  7568. * @param {number} height The height of the image. If not defined, then image.height is assigned.
  7569. * @returns {ImageData} The pixels of the image.
  7570. */
  7571. function getImagePixels(image, width, height) {
  7572. if (!defaultValue.defined(width)) {
  7573. width = image.width;
  7574. }
  7575. if (!defaultValue.defined(height)) {
  7576. height = image.height;
  7577. }
  7578. let context2DsByHeight = context2DsByWidthAndHeight[width];
  7579. if (!defaultValue.defined(context2DsByHeight)) {
  7580. context2DsByHeight = {};
  7581. context2DsByWidthAndHeight[width] = context2DsByHeight;
  7582. }
  7583. let context2d = context2DsByHeight[height];
  7584. if (!defaultValue.defined(context2d)) {
  7585. const canvas = document.createElement("canvas");
  7586. canvas.width = width;
  7587. canvas.height = height;
  7588. // Since we re-use contexts, use the willReadFrequently option – See https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-will-read-frequently
  7589. context2d = canvas.getContext("2d", { willReadFrequently: true });
  7590. context2d.globalCompositeOperation = "copy";
  7591. context2DsByHeight[height] = context2d;
  7592. }
  7593. context2d.drawImage(image, 0, 0, width, height);
  7594. return context2d.getImageData(0, 0, width, height).data;
  7595. }
  7596. const blobUriRegex = /^blob:/i;
  7597. /**
  7598. * Determines if the specified uri is a blob uri.
  7599. *
  7600. * @function isBlobUri
  7601. *
  7602. * @param {string} uri The uri to test.
  7603. * @returns {boolean} true when the uri is a blob uri; otherwise, false.
  7604. *
  7605. * @private
  7606. */
  7607. function isBlobUri(uri) {
  7608. //>>includeStart('debug', pragmas.debug);
  7609. Check.Check.typeOf.string("uri", uri);
  7610. //>>includeEnd('debug');
  7611. return blobUriRegex.test(uri);
  7612. }
  7613. let a$1;
  7614. /**
  7615. * Given a URL, determine whether that URL is considered cross-origin to the current page.
  7616. *
  7617. * @private
  7618. */
  7619. function isCrossOriginUrl(url) {
  7620. if (!defaultValue.defined(a$1)) {
  7621. a$1 = document.createElement("a");
  7622. }
  7623. // copy window location into the anchor to get consistent results
  7624. // when the port is default for the protocol (e.g. 80 for HTTP)
  7625. a$1.href = window.location.href;
  7626. // host includes both hostname and port if the port is not standard
  7627. const host = a$1.host;
  7628. const protocol = a$1.protocol;
  7629. a$1.href = url;
  7630. // IE only absolutizes href on get, not set
  7631. // eslint-disable-next-line no-self-assign
  7632. a$1.href = a$1.href;
  7633. return protocol !== a$1.protocol || host !== a$1.host;
  7634. }
  7635. const dataUriRegex$1 = /^data:/i;
  7636. /**
  7637. * Determines if the specified uri is a data uri.
  7638. *
  7639. * @function isDataUri
  7640. *
  7641. * @param {string} uri The uri to test.
  7642. * @returns {boolean} true when the uri is a data uri; otherwise, false.
  7643. *
  7644. * @private
  7645. */
  7646. function isDataUri(uri) {
  7647. //>>includeStart('debug', pragmas.debug);
  7648. Check.Check.typeOf.string("uri", uri);
  7649. //>>includeEnd('debug');
  7650. return dataUriRegex$1.test(uri);
  7651. }
  7652. /**
  7653. * @private
  7654. */
  7655. function loadAndExecuteScript(url) {
  7656. const script = document.createElement("script");
  7657. script.async = true;
  7658. script.src = url;
  7659. return new Promise((resolve, reject) => {
  7660. if (window.crossOriginIsolated) {
  7661. script.setAttribute("crossorigin", "anonymous");
  7662. }
  7663. const head = document.getElementsByTagName("head")[0];
  7664. script.onload = function () {
  7665. script.onload = undefined;
  7666. head.removeChild(script);
  7667. resolve();
  7668. };
  7669. script.onerror = function (e) {
  7670. reject(e);
  7671. };
  7672. head.appendChild(script);
  7673. });
  7674. }
  7675. /**
  7676. * Converts an object representing a set of name/value pairs into a query string,
  7677. * with names and values encoded properly for use in a URL. Values that are arrays
  7678. * will produce multiple values with the same name.
  7679. * @function objectToQuery
  7680. *
  7681. * @param {object} obj The object containing data to encode.
  7682. * @returns {string} An encoded query string.
  7683. *
  7684. *
  7685. * @example
  7686. * const str = Cesium.objectToQuery({
  7687. * key1 : 'some value',
  7688. * key2 : 'a/b',
  7689. * key3 : ['x', 'y']
  7690. * });
  7691. *
  7692. * @see queryToObject
  7693. * // str will be:
  7694. * // 'key1=some%20value&key2=a%2Fb&key3=x&key3=y'
  7695. */
  7696. function objectToQuery(obj) {
  7697. //>>includeStart('debug', pragmas.debug);
  7698. if (!defaultValue.defined(obj)) {
  7699. throw new Check.DeveloperError("obj is required.");
  7700. }
  7701. //>>includeEnd('debug');
  7702. let result = "";
  7703. for (const propName in obj) {
  7704. if (obj.hasOwnProperty(propName)) {
  7705. const value = obj[propName];
  7706. const part = `${encodeURIComponent(propName)}=`;
  7707. if (Array.isArray(value)) {
  7708. for (let i = 0, len = value.length; i < len; ++i) {
  7709. result += `${part + encodeURIComponent(value[i])}&`;
  7710. }
  7711. } else {
  7712. result += `${part + encodeURIComponent(value)}&`;
  7713. }
  7714. }
  7715. }
  7716. // trim last &
  7717. result = result.slice(0, -1);
  7718. // This function used to replace %20 with + which is more compact and readable.
  7719. // However, some servers didn't properly handle + as a space.
  7720. // https://github.com/CesiumGS/cesium/issues/2192
  7721. return result;
  7722. }
  7723. /**
  7724. * Parses a query string into an object, where the keys and values of the object are the
  7725. * name/value pairs from the query string, decoded. If a name appears multiple times,
  7726. * the value in the object will be an array of values.
  7727. * @function queryToObject
  7728. *
  7729. * @param {string} queryString The query string.
  7730. * @returns {object} An object containing the parameters parsed from the query string.
  7731. *
  7732. *
  7733. * @example
  7734. * const obj = Cesium.queryToObject('key1=some%20value&key2=a%2Fb&key3=x&key3=y');
  7735. * // obj will be:
  7736. * // {
  7737. * // key1 : 'some value',
  7738. * // key2 : 'a/b',
  7739. * // key3 : ['x', 'y']
  7740. * // }
  7741. *
  7742. * @see objectToQuery
  7743. */
  7744. function queryToObject(queryString) {
  7745. //>>includeStart('debug', pragmas.debug);
  7746. if (!defaultValue.defined(queryString)) {
  7747. throw new Check.DeveloperError("queryString is required.");
  7748. }
  7749. //>>includeEnd('debug');
  7750. const result = {};
  7751. if (queryString === "") {
  7752. return result;
  7753. }
  7754. const parts = queryString.replace(/\+/g, "%20").split(/[&;]/);
  7755. for (let i = 0, len = parts.length; i < len; ++i) {
  7756. const subparts = parts[i].split("=");
  7757. const name = decodeURIComponent(subparts[0]);
  7758. let value = subparts[1];
  7759. if (defaultValue.defined(value)) {
  7760. value = decodeURIComponent(value);
  7761. } else {
  7762. value = "";
  7763. }
  7764. const resultValue = result[name];
  7765. if (typeof resultValue === "string") {
  7766. // expand the single value to an array
  7767. result[name] = [resultValue, value];
  7768. } else if (Array.isArray(resultValue)) {
  7769. resultValue.push(value);
  7770. } else {
  7771. result[name] = value;
  7772. }
  7773. }
  7774. return result;
  7775. }
  7776. /**
  7777. * State of the request.
  7778. *
  7779. * @enum {number}
  7780. */
  7781. const RequestState = {
  7782. /**
  7783. * Initial unissued state.
  7784. *
  7785. * @type {number}
  7786. * @constant
  7787. */
  7788. UNISSUED: 0,
  7789. /**
  7790. * Issued but not yet active. Will become active when open slots are available.
  7791. *
  7792. * @type {number}
  7793. * @constant
  7794. */
  7795. ISSUED: 1,
  7796. /**
  7797. * Actual http request has been sent.
  7798. *
  7799. * @type {number}
  7800. * @constant
  7801. */
  7802. ACTIVE: 2,
  7803. /**
  7804. * Request completed successfully.
  7805. *
  7806. * @type {number}
  7807. * @constant
  7808. */
  7809. RECEIVED: 3,
  7810. /**
  7811. * Request was cancelled, either explicitly or automatically because of low priority.
  7812. *
  7813. * @type {number}
  7814. * @constant
  7815. */
  7816. CANCELLED: 4,
  7817. /**
  7818. * Request failed.
  7819. *
  7820. * @type {number}
  7821. * @constant
  7822. */
  7823. FAILED: 5,
  7824. };
  7825. var RequestState$1 = Object.freeze(RequestState);
  7826. /**
  7827. * An enum identifying the type of request. Used for finer grained logging and priority sorting.
  7828. *
  7829. * @enum {number}
  7830. */
  7831. const RequestType = {
  7832. /**
  7833. * Terrain request.
  7834. *
  7835. * @type {number}
  7836. * @constant
  7837. */
  7838. TERRAIN: 0,
  7839. /**
  7840. * Imagery request.
  7841. *
  7842. * @type {number}
  7843. * @constant
  7844. */
  7845. IMAGERY: 1,
  7846. /**
  7847. * 3D Tiles request.
  7848. *
  7849. * @type {number}
  7850. * @constant
  7851. */
  7852. TILES3D: 2,
  7853. /**
  7854. * Other request.
  7855. *
  7856. * @type {number}
  7857. * @constant
  7858. */
  7859. OTHER: 3,
  7860. };
  7861. var RequestType$1 = Object.freeze(RequestType);
  7862. /**
  7863. * Stores information for making a request. In general this does not need to be constructed directly.
  7864. *
  7865. * @alias Request
  7866. * @constructor
  7867. * @param {object} [options] An object with the following properties:
  7868. * @param {string} [options.url] The url to request.
  7869. * @param {Request.RequestCallback} [options.requestFunction] The function that makes the actual data request.
  7870. * @param {Request.CancelCallback} [options.cancelFunction] The function that is called when the request is cancelled.
  7871. * @param {Request.PriorityCallback} [options.priorityFunction] The function that is called to update the request's priority, which occurs once per frame.
  7872. * @param {number} [options.priority=0.0] The initial priority of the request.
  7873. * @param {boolean} [options.throttle=false] Whether to throttle and prioritize the request. If false, the request will be sent immediately. If true, the request will be throttled and sent based on priority.
  7874. * @param {boolean} [options.throttleByServer=false] Whether to throttle the request by server.
  7875. * @param {RequestType} [options.type=RequestType.OTHER] The type of request.
  7876. * @param {string} [options.serverKey] A key used to identify the server that a request is going to.
  7877. */
  7878. function Request(options) {
  7879. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  7880. const throttleByServer = defaultValue.defaultValue(options.throttleByServer, false);
  7881. const throttle = defaultValue.defaultValue(options.throttle, false);
  7882. /**
  7883. * The URL to request.
  7884. *
  7885. * @type {string}
  7886. */
  7887. this.url = options.url;
  7888. /**
  7889. * The function that makes the actual data request.
  7890. *
  7891. * @type {Request.RequestCallback}
  7892. */
  7893. this.requestFunction = options.requestFunction;
  7894. /**
  7895. * The function that is called when the request is cancelled.
  7896. *
  7897. * @type {Request.CancelCallback}
  7898. */
  7899. this.cancelFunction = options.cancelFunction;
  7900. /**
  7901. * The function that is called to update the request's priority, which occurs once per frame.
  7902. *
  7903. * @type {Request.PriorityCallback}
  7904. */
  7905. this.priorityFunction = options.priorityFunction;
  7906. /**
  7907. * Priority is a unit-less value where lower values represent higher priority.
  7908. * For world-based objects, this is usually the distance from the camera.
  7909. * A request that does not have a priority function defaults to a priority of 0.
  7910. *
  7911. * If priorityFunction is defined, this value is updated every frame with the result of that call.
  7912. *
  7913. * @type {number}
  7914. * @default 0.0
  7915. */
  7916. this.priority = defaultValue.defaultValue(options.priority, 0.0);
  7917. /**
  7918. * Whether to throttle and prioritize the request. If false, the request will be sent immediately. If true, the
  7919. * request will be throttled and sent based on priority.
  7920. *
  7921. * @type {boolean}
  7922. * @readonly
  7923. *
  7924. * @default false
  7925. */
  7926. this.throttle = throttle;
  7927. /**
  7928. * Whether to throttle the request by server. Browsers typically support about 6-8 parallel connections
  7929. * for HTTP/1 servers, and an unlimited amount of connections for HTTP/2 servers. Setting this value
  7930. * to <code>true</code> is preferable for requests going through HTTP/1 servers.
  7931. *
  7932. * @type {boolean}
  7933. * @readonly
  7934. *
  7935. * @default false
  7936. */
  7937. this.throttleByServer = throttleByServer;
  7938. /**
  7939. * Type of request.
  7940. *
  7941. * @type {RequestType}
  7942. * @readonly
  7943. *
  7944. * @default RequestType.OTHER
  7945. */
  7946. this.type = defaultValue.defaultValue(options.type, RequestType$1.OTHER);
  7947. /**
  7948. * A key used to identify the server that a request is going to. It is derived from the url's authority and scheme.
  7949. *
  7950. * @type {string}
  7951. *
  7952. * @private
  7953. */
  7954. this.serverKey = options.serverKey;
  7955. /**
  7956. * The current state of the request.
  7957. *
  7958. * @type {RequestState}
  7959. * @readonly
  7960. */
  7961. this.state = RequestState$1.UNISSUED;
  7962. /**
  7963. * The requests's deferred promise.
  7964. *
  7965. * @type {object}
  7966. *
  7967. * @private
  7968. */
  7969. this.deferred = undefined;
  7970. /**
  7971. * Whether the request was explicitly cancelled.
  7972. *
  7973. * @type {boolean}
  7974. *
  7975. * @private
  7976. */
  7977. this.cancelled = false;
  7978. }
  7979. /**
  7980. * Mark the request as cancelled.
  7981. *
  7982. * @private
  7983. */
  7984. Request.prototype.cancel = function () {
  7985. this.cancelled = true;
  7986. };
  7987. /**
  7988. * Duplicates a Request instance.
  7989. *
  7990. * @param {Request} [result] The object onto which to store the result.
  7991. *
  7992. * @returns {Request} The modified result parameter or a new Resource instance if one was not provided.
  7993. */
  7994. Request.prototype.clone = function (result) {
  7995. if (!defaultValue.defined(result)) {
  7996. return new Request(this);
  7997. }
  7998. result.url = this.url;
  7999. result.requestFunction = this.requestFunction;
  8000. result.cancelFunction = this.cancelFunction;
  8001. result.priorityFunction = this.priorityFunction;
  8002. result.priority = this.priority;
  8003. result.throttle = this.throttle;
  8004. result.throttleByServer = this.throttleByServer;
  8005. result.type = this.type;
  8006. result.serverKey = this.serverKey;
  8007. // These get defaulted because the cloned request hasn't been issued
  8008. result.state = RequestState$1.UNISSUED;
  8009. result.deferred = undefined;
  8010. result.cancelled = false;
  8011. return result;
  8012. };
  8013. /**
  8014. * Parses the result of XMLHttpRequest's getAllResponseHeaders() method into
  8015. * a dictionary.
  8016. *
  8017. * @function parseResponseHeaders
  8018. *
  8019. * @param {string} headerString The header string returned by getAllResponseHeaders(). The format is
  8020. * described here: http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders()-method
  8021. * @returns {object} A dictionary of key/value pairs, where each key is the name of a header and the corresponding value
  8022. * is that header's value.
  8023. *
  8024. * @private
  8025. */
  8026. function parseResponseHeaders(headerString) {
  8027. const headers = {};
  8028. if (!headerString) {
  8029. return headers;
  8030. }
  8031. const headerPairs = headerString.split("\u000d\u000a");
  8032. for (let i = 0; i < headerPairs.length; ++i) {
  8033. const headerPair = headerPairs[i];
  8034. // Can't use split() here because it does the wrong thing
  8035. // if the header value has the string ": " in it.
  8036. const index = headerPair.indexOf("\u003a\u0020");
  8037. if (index > 0) {
  8038. const key = headerPair.substring(0, index);
  8039. const val = headerPair.substring(index + 2);
  8040. headers[key] = val;
  8041. }
  8042. }
  8043. return headers;
  8044. }
  8045. /**
  8046. * An event that is raised when a request encounters an error.
  8047. *
  8048. * @constructor
  8049. * @alias RequestErrorEvent
  8050. *
  8051. * @param {number} [statusCode] The HTTP error status code, such as 404.
  8052. * @param {object} [response] The response included along with the error.
  8053. * @param {string|object} [responseHeaders] The response headers, represented either as an object literal or as a
  8054. * string in the format returned by XMLHttpRequest's getAllResponseHeaders() function.
  8055. */
  8056. function RequestErrorEvent(statusCode, response, responseHeaders) {
  8057. /**
  8058. * The HTTP error status code, such as 404. If the error does not have a particular
  8059. * HTTP code, this property will be undefined.
  8060. *
  8061. * @type {number}
  8062. */
  8063. this.statusCode = statusCode;
  8064. /**
  8065. * The response included along with the error. If the error does not include a response,
  8066. * this property will be undefined.
  8067. *
  8068. * @type {object}
  8069. */
  8070. this.response = response;
  8071. /**
  8072. * The headers included in the response, represented as an object literal of key/value pairs.
  8073. * If the error does not include any headers, this property will be undefined.
  8074. *
  8075. * @type {object}
  8076. */
  8077. this.responseHeaders = responseHeaders;
  8078. if (typeof this.responseHeaders === "string") {
  8079. this.responseHeaders = parseResponseHeaders(this.responseHeaders);
  8080. }
  8081. }
  8082. /**
  8083. * Creates a string representing this RequestErrorEvent.
  8084. * @memberof RequestErrorEvent
  8085. *
  8086. * @returns {string} A string representing the provided RequestErrorEvent.
  8087. */
  8088. RequestErrorEvent.prototype.toString = function () {
  8089. let str = "Request has failed.";
  8090. if (defaultValue.defined(this.statusCode)) {
  8091. str += ` Status Code: ${this.statusCode}`;
  8092. }
  8093. return str;
  8094. };
  8095. /**
  8096. * A generic utility class for managing subscribers for a particular event.
  8097. * This class is usually instantiated inside of a container class and
  8098. * exposed as a property for others to subscribe to.
  8099. *
  8100. * @alias Event
  8101. * @template Listener extends (...args: any[]) => void = (...args: any[]) => void
  8102. * @constructor
  8103. * @example
  8104. * MyObject.prototype.myListener = function(arg1, arg2) {
  8105. * this.myArg1Copy = arg1;
  8106. * this.myArg2Copy = arg2;
  8107. * }
  8108. *
  8109. * const myObjectInstance = new MyObject();
  8110. * const evt = new Cesium.Event();
  8111. * evt.addEventListener(MyObject.prototype.myListener, myObjectInstance);
  8112. * evt.raiseEvent('1', '2');
  8113. * evt.removeEventListener(MyObject.prototype.myListener);
  8114. */
  8115. function Event() {
  8116. this._listeners = [];
  8117. this._scopes = [];
  8118. this._toRemove = [];
  8119. this._insideRaiseEvent = false;
  8120. }
  8121. Object.defineProperties(Event.prototype, {
  8122. /**
  8123. * The number of listeners currently subscribed to the event.
  8124. * @memberof Event.prototype
  8125. * @type {number}
  8126. * @readonly
  8127. */
  8128. numberOfListeners: {
  8129. get: function () {
  8130. return this._listeners.length - this._toRemove.length;
  8131. },
  8132. },
  8133. });
  8134. /**
  8135. * Registers a callback function to be executed whenever the event is raised.
  8136. * An optional scope can be provided to serve as the <code>this</code> pointer
  8137. * in which the function will execute.
  8138. *
  8139. * @param {Listener} listener The function to be executed when the event is raised.
  8140. * @param {object} [scope] An optional object scope to serve as the <code>this</code>
  8141. * pointer in which the listener function will execute.
  8142. * @returns {Event.RemoveCallback} A function that will remove this event listener when invoked.
  8143. *
  8144. * @see Event#raiseEvent
  8145. * @see Event#removeEventListener
  8146. */
  8147. Event.prototype.addEventListener = function (listener, scope) {
  8148. //>>includeStart('debug', pragmas.debug);
  8149. Check.Check.typeOf.func("listener", listener);
  8150. //>>includeEnd('debug');
  8151. this._listeners.push(listener);
  8152. this._scopes.push(scope);
  8153. const event = this;
  8154. return function () {
  8155. event.removeEventListener(listener, scope);
  8156. };
  8157. };
  8158. /**
  8159. * Unregisters a previously registered callback.
  8160. *
  8161. * @param {Listener} listener The function to be unregistered.
  8162. * @param {object} [scope] The scope that was originally passed to addEventListener.
  8163. * @returns {boolean} <code>true</code> if the listener was removed; <code>false</code> if the listener and scope are not registered with the event.
  8164. *
  8165. * @see Event#addEventListener
  8166. * @see Event#raiseEvent
  8167. */
  8168. Event.prototype.removeEventListener = function (listener, scope) {
  8169. //>>includeStart('debug', pragmas.debug);
  8170. Check.Check.typeOf.func("listener", listener);
  8171. //>>includeEnd('debug');
  8172. const listeners = this._listeners;
  8173. const scopes = this._scopes;
  8174. let index = -1;
  8175. for (let i = 0; i < listeners.length; i++) {
  8176. if (listeners[i] === listener && scopes[i] === scope) {
  8177. index = i;
  8178. break;
  8179. }
  8180. }
  8181. if (index !== -1) {
  8182. if (this._insideRaiseEvent) {
  8183. //In order to allow removing an event subscription from within
  8184. //a callback, we don't actually remove the items here. Instead
  8185. //remember the index they are at and undefined their value.
  8186. this._toRemove.push(index);
  8187. listeners[index] = undefined;
  8188. scopes[index] = undefined;
  8189. } else {
  8190. listeners.splice(index, 1);
  8191. scopes.splice(index, 1);
  8192. }
  8193. return true;
  8194. }
  8195. return false;
  8196. };
  8197. function compareNumber(a, b) {
  8198. return b - a;
  8199. }
  8200. /**
  8201. * Raises the event by calling each registered listener with all supplied arguments.
  8202. *
  8203. * @param {...Parameters<Listener>} arguments This method takes any number of parameters and passes them through to the listener functions.
  8204. *
  8205. * @see Event#addEventListener
  8206. * @see Event#removeEventListener
  8207. */
  8208. Event.prototype.raiseEvent = function () {
  8209. this._insideRaiseEvent = true;
  8210. let i;
  8211. const listeners = this._listeners;
  8212. const scopes = this._scopes;
  8213. let length = listeners.length;
  8214. for (i = 0; i < length; i++) {
  8215. const listener = listeners[i];
  8216. if (defaultValue.defined(listener)) {
  8217. listeners[i].apply(scopes[i], arguments);
  8218. }
  8219. }
  8220. //Actually remove items removed in removeEventListener.
  8221. const toRemove = this._toRemove;
  8222. length = toRemove.length;
  8223. if (length > 0) {
  8224. toRemove.sort(compareNumber);
  8225. for (i = 0; i < length; i++) {
  8226. const index = toRemove[i];
  8227. listeners.splice(index, 1);
  8228. scopes.splice(index, 1);
  8229. }
  8230. toRemove.length = 0;
  8231. }
  8232. this._insideRaiseEvent = false;
  8233. };
  8234. /**
  8235. * Array implementation of a heap.
  8236. *
  8237. * @alias Heap
  8238. * @constructor
  8239. * @private
  8240. *
  8241. * @param {object} options Object with the following properties:
  8242. * @param {Heap.ComparatorCallback} options.comparator The comparator to use for the heap. If comparator(a, b) is less than 0, sort a to a lower index than b, otherwise sort to a higher index.
  8243. */
  8244. function Heap(options) {
  8245. //>>includeStart('debug', pragmas.debug);
  8246. Check.Check.typeOf.object("options", options);
  8247. Check.Check.defined("options.comparator", options.comparator);
  8248. //>>includeEnd('debug');
  8249. this._comparator = options.comparator;
  8250. this._array = [];
  8251. this._length = 0;
  8252. this._maximumLength = undefined;
  8253. }
  8254. Object.defineProperties(Heap.prototype, {
  8255. /**
  8256. * Gets the length of the heap.
  8257. *
  8258. * @memberof Heap.prototype
  8259. *
  8260. * @type {number}
  8261. * @readonly
  8262. */
  8263. length: {
  8264. get: function () {
  8265. return this._length;
  8266. },
  8267. },
  8268. /**
  8269. * Gets the internal array.
  8270. *
  8271. * @memberof Heap.prototype
  8272. *
  8273. * @type {Array}
  8274. * @readonly
  8275. */
  8276. internalArray: {
  8277. get: function () {
  8278. return this._array;
  8279. },
  8280. },
  8281. /**
  8282. * Gets and sets the maximum length of the heap.
  8283. *
  8284. * @memberof Heap.prototype
  8285. *
  8286. * @type {number}
  8287. */
  8288. maximumLength: {
  8289. get: function () {
  8290. return this._maximumLength;
  8291. },
  8292. set: function (value) {
  8293. //>>includeStart('debug', pragmas.debug);
  8294. Check.Check.typeOf.number.greaterThanOrEquals("maximumLength", value, 0);
  8295. //>>includeEnd('debug');
  8296. const originalLength = this._length;
  8297. if (value < originalLength) {
  8298. const array = this._array;
  8299. // Remove trailing references
  8300. for (let i = value; i < originalLength; ++i) {
  8301. array[i] = undefined;
  8302. }
  8303. this._length = value;
  8304. array.length = value;
  8305. }
  8306. this._maximumLength = value;
  8307. },
  8308. },
  8309. /**
  8310. * The comparator to use for the heap. If comparator(a, b) is less than 0, sort a to a lower index than b, otherwise sort to a higher index.
  8311. *
  8312. * @memberof Heap.prototype
  8313. *
  8314. * @type {Heap.ComparatorCallback}
  8315. */
  8316. comparator: {
  8317. get: function () {
  8318. return this._comparator;
  8319. },
  8320. },
  8321. });
  8322. function swap(array, a, b) {
  8323. const temp = array[a];
  8324. array[a] = array[b];
  8325. array[b] = temp;
  8326. }
  8327. /**
  8328. * Resizes the internal array of the heap.
  8329. *
  8330. * @param {number} [length] The length to resize internal array to. Defaults to the current length of the heap.
  8331. */
  8332. Heap.prototype.reserve = function (length) {
  8333. length = defaultValue.defaultValue(length, this._length);
  8334. this._array.length = length;
  8335. };
  8336. /**
  8337. * Update the heap so that index and all descendants satisfy the heap property.
  8338. *
  8339. * @param {number} [index=0] The starting index to heapify from.
  8340. */
  8341. Heap.prototype.heapify = function (index) {
  8342. index = defaultValue.defaultValue(index, 0);
  8343. const length = this._length;
  8344. const comparator = this._comparator;
  8345. const array = this._array;
  8346. let candidate = -1;
  8347. let inserting = true;
  8348. while (inserting) {
  8349. const right = 2 * (index + 1);
  8350. const left = right - 1;
  8351. if (left < length && comparator(array[left], array[index]) < 0) {
  8352. candidate = left;
  8353. } else {
  8354. candidate = index;
  8355. }
  8356. if (right < length && comparator(array[right], array[candidate]) < 0) {
  8357. candidate = right;
  8358. }
  8359. if (candidate !== index) {
  8360. swap(array, candidate, index);
  8361. index = candidate;
  8362. } else {
  8363. inserting = false;
  8364. }
  8365. }
  8366. };
  8367. /**
  8368. * Resort the heap.
  8369. */
  8370. Heap.prototype.resort = function () {
  8371. const length = this._length;
  8372. for (let i = Math.ceil(length / 2); i >= 0; --i) {
  8373. this.heapify(i);
  8374. }
  8375. };
  8376. /**
  8377. * Insert an element into the heap. If the length would grow greater than maximumLength
  8378. * of the heap, extra elements are removed.
  8379. *
  8380. * @param {*} element The element to insert
  8381. *
  8382. * @return {*} The element that was removed from the heap if the heap is at full capacity.
  8383. */
  8384. Heap.prototype.insert = function (element) {
  8385. //>>includeStart('debug', pragmas.debug);
  8386. Check.Check.defined("element", element);
  8387. //>>includeEnd('debug');
  8388. const array = this._array;
  8389. const comparator = this._comparator;
  8390. const maximumLength = this._maximumLength;
  8391. let index = this._length++;
  8392. if (index < array.length) {
  8393. array[index] = element;
  8394. } else {
  8395. array.push(element);
  8396. }
  8397. while (index !== 0) {
  8398. const parent = Math.floor((index - 1) / 2);
  8399. if (comparator(array[index], array[parent]) < 0) {
  8400. swap(array, index, parent);
  8401. index = parent;
  8402. } else {
  8403. break;
  8404. }
  8405. }
  8406. let removedElement;
  8407. if (defaultValue.defined(maximumLength) && this._length > maximumLength) {
  8408. removedElement = array[maximumLength];
  8409. this._length = maximumLength;
  8410. }
  8411. return removedElement;
  8412. };
  8413. /**
  8414. * Remove the element specified by index from the heap and return it.
  8415. *
  8416. * @param {number} [index=0] The index to remove.
  8417. * @returns {*} The specified element of the heap.
  8418. */
  8419. Heap.prototype.pop = function (index) {
  8420. index = defaultValue.defaultValue(index, 0);
  8421. if (this._length === 0) {
  8422. return undefined;
  8423. }
  8424. //>>includeStart('debug', pragmas.debug);
  8425. Check.Check.typeOf.number.lessThan("index", index, this._length);
  8426. //>>includeEnd('debug');
  8427. const array = this._array;
  8428. const root = array[index];
  8429. swap(array, index, --this._length);
  8430. this.heapify(index);
  8431. array[this._length] = undefined; // Remove trailing reference
  8432. return root;
  8433. };
  8434. function sortRequests(a, b) {
  8435. return a.priority - b.priority;
  8436. }
  8437. const statistics = {
  8438. numberOfAttemptedRequests: 0,
  8439. numberOfActiveRequests: 0,
  8440. numberOfCancelledRequests: 0,
  8441. numberOfCancelledActiveRequests: 0,
  8442. numberOfFailedRequests: 0,
  8443. numberOfActiveRequestsEver: 0,
  8444. lastNumberOfActiveRequests: 0,
  8445. };
  8446. let priorityHeapLength = 20;
  8447. const requestHeap = new Heap({
  8448. comparator: sortRequests,
  8449. });
  8450. requestHeap.maximumLength = priorityHeapLength;
  8451. requestHeap.reserve(priorityHeapLength);
  8452. const activeRequests = [];
  8453. let numberOfActiveRequestsByServer = {};
  8454. const pageUri =
  8455. typeof document !== "undefined" ? new Uri(document.location.href) : new Uri();
  8456. const requestCompletedEvent = new Event();
  8457. /**
  8458. * The request scheduler is used to track and constrain the number of active requests in order to prioritize incoming requests. The ability
  8459. * to retain control over the number of requests in CesiumJS is important because due to events such as changes in the camera position,
  8460. * a lot of new requests may be generated and a lot of in-flight requests may become redundant. The request scheduler manually constrains the
  8461. * number of requests so that newer requests wait in a shorter queue and don't have to compete for bandwidth with requests that have expired.
  8462. *
  8463. * @namespace RequestScheduler
  8464. *
  8465. */
  8466. function RequestScheduler() {}
  8467. /**
  8468. * The maximum number of simultaneous active requests. Un-throttled requests do not observe this limit.
  8469. * @type {number}
  8470. * @default 50
  8471. */
  8472. RequestScheduler.maximumRequests = 50;
  8473. /**
  8474. * The maximum number of simultaneous active requests per server. Un-throttled requests or servers specifically
  8475. * listed in {@link requestsByServer} do not observe this limit.
  8476. * @type {number}
  8477. * @default 6
  8478. */
  8479. RequestScheduler.maximumRequestsPerServer = 6;
  8480. /**
  8481. * A per server key list of overrides to use for throttling instead of <code>maximumRequestsPerServer</code>.
  8482. * Useful when streaming data from a known HTTP/2 or HTTP/3 server.
  8483. * @type {object}
  8484. *
  8485. * @example
  8486. * RequestScheduler.requestsByServer["myserver.com:443"] = 18;
  8487. *
  8488. * @example
  8489. * RequestScheduler.requestsByServer = {
  8490. * "api.cesium.com:443": 18,
  8491. * "assets.cesium.com:443": 18,
  8492. * };
  8493. */
  8494. RequestScheduler.requestsByServer = {
  8495. "api.cesium.com:443": 18,
  8496. "assets.ion.cesium.com:443": 18,
  8497. "ibasemaps-api.arcgis.com:443": 18,
  8498. };
  8499. /**
  8500. * Specifies if the request scheduler should throttle incoming requests, or let the browser queue requests under its control.
  8501. * @type {boolean}
  8502. * @default true
  8503. */
  8504. RequestScheduler.throttleRequests = true;
  8505. /**
  8506. * When true, log statistics to the console every frame
  8507. * @type {boolean}
  8508. * @default false
  8509. * @private
  8510. */
  8511. RequestScheduler.debugShowStatistics = false;
  8512. /**
  8513. * An event that's raised when a request is completed. Event handlers are passed
  8514. * the error object if the request fails.
  8515. *
  8516. * @type {Event}
  8517. * @default Event()
  8518. * @private
  8519. */
  8520. RequestScheduler.requestCompletedEvent = requestCompletedEvent;
  8521. Object.defineProperties(RequestScheduler, {
  8522. /**
  8523. * Returns the statistics used by the request scheduler.
  8524. *
  8525. * @memberof RequestScheduler
  8526. *
  8527. * @type {object}
  8528. * @readonly
  8529. * @private
  8530. */
  8531. statistics: {
  8532. get: function () {
  8533. return statistics;
  8534. },
  8535. },
  8536. /**
  8537. * The maximum size of the priority heap. This limits the number of requests that are sorted by priority. Only applies to requests that are not yet active.
  8538. *
  8539. * @memberof RequestScheduler
  8540. *
  8541. * @type {number}
  8542. * @default 20
  8543. * @private
  8544. */
  8545. priorityHeapLength: {
  8546. get: function () {
  8547. return priorityHeapLength;
  8548. },
  8549. set: function (value) {
  8550. // If the new length shrinks the heap, need to cancel some of the requests.
  8551. // Since this value is not intended to be tweaked regularly it is fine to just cancel the high priority requests.
  8552. if (value < priorityHeapLength) {
  8553. while (requestHeap.length > value) {
  8554. const request = requestHeap.pop();
  8555. cancelRequest(request);
  8556. }
  8557. }
  8558. priorityHeapLength = value;
  8559. requestHeap.maximumLength = value;
  8560. requestHeap.reserve(value);
  8561. },
  8562. },
  8563. });
  8564. function updatePriority(request) {
  8565. if (defaultValue.defined(request.priorityFunction)) {
  8566. request.priority = request.priorityFunction();
  8567. }
  8568. }
  8569. /**
  8570. * Check if there are open slots for a particular server key. If desiredRequests is greater than 1, this checks if the queue has room for scheduling multiple requests.
  8571. * @param {string} serverKey The server key returned by {@link RequestScheduler.getServerKey}.
  8572. * @param {number} [desiredRequests=1] How many requests the caller plans to request
  8573. * @return {boolean} True if there are enough open slots for <code>desiredRequests</code> more requests.
  8574. * @private
  8575. */
  8576. RequestScheduler.serverHasOpenSlots = function (serverKey, desiredRequests) {
  8577. desiredRequests = defaultValue.defaultValue(desiredRequests, 1);
  8578. const maxRequests = defaultValue.defaultValue(
  8579. RequestScheduler.requestsByServer[serverKey],
  8580. RequestScheduler.maximumRequestsPerServer
  8581. );
  8582. const hasOpenSlotsServer =
  8583. numberOfActiveRequestsByServer[serverKey] + desiredRequests <= maxRequests;
  8584. return hasOpenSlotsServer;
  8585. };
  8586. /**
  8587. * Check if the priority heap has open slots, regardless of which server they
  8588. * are from. This is used in {@link Multiple3DTileContent} for determining when
  8589. * all requests can be scheduled
  8590. * @param {number} desiredRequests The number of requests the caller intends to make
  8591. * @return {boolean} <code>true</code> if the heap has enough available slots to meet the desiredRequests. <code>false</code> otherwise.
  8592. *
  8593. * @private
  8594. */
  8595. RequestScheduler.heapHasOpenSlots = function (desiredRequests) {
  8596. const hasOpenSlotsHeap =
  8597. requestHeap.length + desiredRequests <= priorityHeapLength;
  8598. return hasOpenSlotsHeap;
  8599. };
  8600. function issueRequest(request) {
  8601. if (request.state === RequestState$1.UNISSUED) {
  8602. request.state = RequestState$1.ISSUED;
  8603. request.deferred = defer();
  8604. }
  8605. return request.deferred.promise;
  8606. }
  8607. function getRequestReceivedFunction(request) {
  8608. return function (results) {
  8609. if (request.state === RequestState$1.CANCELLED) {
  8610. // If the data request comes back but the request is cancelled, ignore it.
  8611. return;
  8612. }
  8613. // explicitly set to undefined to ensure GC of request response data. See #8843
  8614. const deferred = request.deferred;
  8615. --statistics.numberOfActiveRequests;
  8616. --numberOfActiveRequestsByServer[request.serverKey];
  8617. requestCompletedEvent.raiseEvent();
  8618. request.state = RequestState$1.RECEIVED;
  8619. request.deferred = undefined;
  8620. deferred.resolve(results);
  8621. };
  8622. }
  8623. function getRequestFailedFunction(request) {
  8624. return function (error) {
  8625. if (request.state === RequestState$1.CANCELLED) {
  8626. // If the data request comes back but the request is cancelled, ignore it.
  8627. return;
  8628. }
  8629. ++statistics.numberOfFailedRequests;
  8630. --statistics.numberOfActiveRequests;
  8631. --numberOfActiveRequestsByServer[request.serverKey];
  8632. requestCompletedEvent.raiseEvent(error);
  8633. request.state = RequestState$1.FAILED;
  8634. request.deferred.reject(error);
  8635. };
  8636. }
  8637. function startRequest(request) {
  8638. const promise = issueRequest(request);
  8639. request.state = RequestState$1.ACTIVE;
  8640. activeRequests.push(request);
  8641. ++statistics.numberOfActiveRequests;
  8642. ++statistics.numberOfActiveRequestsEver;
  8643. ++numberOfActiveRequestsByServer[request.serverKey];
  8644. request
  8645. .requestFunction()
  8646. .then(getRequestReceivedFunction(request))
  8647. .catch(getRequestFailedFunction(request));
  8648. return promise;
  8649. }
  8650. function cancelRequest(request) {
  8651. const active = request.state === RequestState$1.ACTIVE;
  8652. request.state = RequestState$1.CANCELLED;
  8653. ++statistics.numberOfCancelledRequests;
  8654. // check that deferred has not been cleared since cancelRequest can be called
  8655. // on a finished request, e.g. by clearForSpecs during tests
  8656. if (defaultValue.defined(request.deferred)) {
  8657. const deferred = request.deferred;
  8658. request.deferred = undefined;
  8659. deferred.reject();
  8660. }
  8661. if (active) {
  8662. --statistics.numberOfActiveRequests;
  8663. --numberOfActiveRequestsByServer[request.serverKey];
  8664. ++statistics.numberOfCancelledActiveRequests;
  8665. }
  8666. if (defaultValue.defined(request.cancelFunction)) {
  8667. request.cancelFunction();
  8668. }
  8669. }
  8670. /**
  8671. * Sort requests by priority and start requests.
  8672. * @private
  8673. */
  8674. RequestScheduler.update = function () {
  8675. let i;
  8676. let request;
  8677. // Loop over all active requests. Cancelled, failed, or received requests are removed from the array to make room for new requests.
  8678. let removeCount = 0;
  8679. const activeLength = activeRequests.length;
  8680. for (i = 0; i < activeLength; ++i) {
  8681. request = activeRequests[i];
  8682. if (request.cancelled) {
  8683. // Request was explicitly cancelled
  8684. cancelRequest(request);
  8685. }
  8686. if (request.state !== RequestState$1.ACTIVE) {
  8687. // Request is no longer active, remove from array
  8688. ++removeCount;
  8689. continue;
  8690. }
  8691. if (removeCount > 0) {
  8692. // Shift back to fill in vacated slots from completed requests
  8693. activeRequests[i - removeCount] = request;
  8694. }
  8695. }
  8696. activeRequests.length -= removeCount;
  8697. // Update priority of issued requests and resort the heap
  8698. const issuedRequests = requestHeap.internalArray;
  8699. const issuedLength = requestHeap.length;
  8700. for (i = 0; i < issuedLength; ++i) {
  8701. updatePriority(issuedRequests[i]);
  8702. }
  8703. requestHeap.resort();
  8704. // Get the number of open slots and fill with the highest priority requests.
  8705. // Un-throttled requests are automatically added to activeRequests, so activeRequests.length may exceed maximumRequests
  8706. const openSlots = Math.max(
  8707. RequestScheduler.maximumRequests - activeRequests.length,
  8708. 0
  8709. );
  8710. let filledSlots = 0;
  8711. while (filledSlots < openSlots && requestHeap.length > 0) {
  8712. // Loop until all open slots are filled or the heap becomes empty
  8713. request = requestHeap.pop();
  8714. if (request.cancelled) {
  8715. // Request was explicitly cancelled
  8716. cancelRequest(request);
  8717. continue;
  8718. }
  8719. if (
  8720. request.throttleByServer &&
  8721. !RequestScheduler.serverHasOpenSlots(request.serverKey)
  8722. ) {
  8723. // Open slots are available, but the request is throttled by its server. Cancel and try again later.
  8724. cancelRequest(request);
  8725. continue;
  8726. }
  8727. startRequest(request);
  8728. ++filledSlots;
  8729. }
  8730. updateStatistics();
  8731. };
  8732. /**
  8733. * Get the server key from a given url.
  8734. *
  8735. * @param {string} url The url.
  8736. * @returns {string} The server key.
  8737. * @private
  8738. */
  8739. RequestScheduler.getServerKey = function (url) {
  8740. //>>includeStart('debug', pragmas.debug);
  8741. Check.Check.typeOf.string("url", url);
  8742. //>>includeEnd('debug');
  8743. let uri = new Uri(url);
  8744. if (uri.scheme() === "") {
  8745. uri = uri.absoluteTo(pageUri);
  8746. uri.normalize();
  8747. }
  8748. let serverKey = uri.authority();
  8749. if (!/:/.test(serverKey)) {
  8750. // If the authority does not contain a port number, add port 443 for https or port 80 for http
  8751. serverKey = `${serverKey}:${uri.scheme() === "https" ? "443" : "80"}`;
  8752. }
  8753. const length = numberOfActiveRequestsByServer[serverKey];
  8754. if (!defaultValue.defined(length)) {
  8755. numberOfActiveRequestsByServer[serverKey] = 0;
  8756. }
  8757. return serverKey;
  8758. };
  8759. /**
  8760. * Issue a request. If request.throttle is false, the request is sent immediately. Otherwise the request will be
  8761. * queued and sorted by priority before being sent.
  8762. *
  8763. * @param {Request} request The request object.
  8764. *
  8765. * @returns {Promise|undefined} A Promise for the requested data, or undefined if this request does not have high enough priority to be issued.
  8766. *
  8767. * @private
  8768. */
  8769. RequestScheduler.request = function (request) {
  8770. //>>includeStart('debug', pragmas.debug);
  8771. Check.Check.typeOf.object("request", request);
  8772. Check.Check.typeOf.string("request.url", request.url);
  8773. Check.Check.typeOf.func("request.requestFunction", request.requestFunction);
  8774. //>>includeEnd('debug');
  8775. if (isDataUri(request.url) || isBlobUri(request.url)) {
  8776. requestCompletedEvent.raiseEvent();
  8777. request.state = RequestState$1.RECEIVED;
  8778. return request.requestFunction();
  8779. }
  8780. ++statistics.numberOfAttemptedRequests;
  8781. if (!defaultValue.defined(request.serverKey)) {
  8782. request.serverKey = RequestScheduler.getServerKey(request.url);
  8783. }
  8784. if (
  8785. RequestScheduler.throttleRequests &&
  8786. request.throttleByServer &&
  8787. !RequestScheduler.serverHasOpenSlots(request.serverKey)
  8788. ) {
  8789. // Server is saturated. Try again later.
  8790. return undefined;
  8791. }
  8792. if (!RequestScheduler.throttleRequests || !request.throttle) {
  8793. return startRequest(request);
  8794. }
  8795. if (activeRequests.length >= RequestScheduler.maximumRequests) {
  8796. // Active requests are saturated. Try again later.
  8797. return undefined;
  8798. }
  8799. // Insert into the priority heap and see if a request was bumped off. If this request is the lowest
  8800. // priority it will be returned.
  8801. updatePriority(request);
  8802. const removedRequest = requestHeap.insert(request);
  8803. if (defaultValue.defined(removedRequest)) {
  8804. if (removedRequest === request) {
  8805. // Request does not have high enough priority to be issued
  8806. return undefined;
  8807. }
  8808. // A previously issued request has been bumped off the priority heap, so cancel it
  8809. cancelRequest(removedRequest);
  8810. }
  8811. return issueRequest(request);
  8812. };
  8813. function updateStatistics() {
  8814. if (!RequestScheduler.debugShowStatistics) {
  8815. return;
  8816. }
  8817. if (
  8818. statistics.numberOfActiveRequests === 0 &&
  8819. statistics.lastNumberOfActiveRequests > 0
  8820. ) {
  8821. if (statistics.numberOfAttemptedRequests > 0) {
  8822. console.log(
  8823. `Number of attempted requests: ${statistics.numberOfAttemptedRequests}`
  8824. );
  8825. statistics.numberOfAttemptedRequests = 0;
  8826. }
  8827. if (statistics.numberOfCancelledRequests > 0) {
  8828. console.log(
  8829. `Number of cancelled requests: ${statistics.numberOfCancelledRequests}`
  8830. );
  8831. statistics.numberOfCancelledRequests = 0;
  8832. }
  8833. if (statistics.numberOfCancelledActiveRequests > 0) {
  8834. console.log(
  8835. `Number of cancelled active requests: ${statistics.numberOfCancelledActiveRequests}`
  8836. );
  8837. statistics.numberOfCancelledActiveRequests = 0;
  8838. }
  8839. if (statistics.numberOfFailedRequests > 0) {
  8840. console.log(
  8841. `Number of failed requests: ${statistics.numberOfFailedRequests}`
  8842. );
  8843. statistics.numberOfFailedRequests = 0;
  8844. }
  8845. }
  8846. statistics.lastNumberOfActiveRequests = statistics.numberOfActiveRequests;
  8847. }
  8848. /**
  8849. * For testing only. Clears any requests that may not have completed from previous tests.
  8850. *
  8851. * @private
  8852. */
  8853. RequestScheduler.clearForSpecs = function () {
  8854. while (requestHeap.length > 0) {
  8855. const request = requestHeap.pop();
  8856. cancelRequest(request);
  8857. }
  8858. const length = activeRequests.length;
  8859. for (let i = 0; i < length; ++i) {
  8860. cancelRequest(activeRequests[i]);
  8861. }
  8862. activeRequests.length = 0;
  8863. numberOfActiveRequestsByServer = {};
  8864. // Clear stats
  8865. statistics.numberOfAttemptedRequests = 0;
  8866. statistics.numberOfActiveRequests = 0;
  8867. statistics.numberOfCancelledRequests = 0;
  8868. statistics.numberOfCancelledActiveRequests = 0;
  8869. statistics.numberOfFailedRequests = 0;
  8870. statistics.numberOfActiveRequestsEver = 0;
  8871. statistics.lastNumberOfActiveRequests = 0;
  8872. };
  8873. /**
  8874. * For testing only.
  8875. *
  8876. * @private
  8877. */
  8878. RequestScheduler.numberOfActiveRequestsByServer = function (serverKey) {
  8879. return numberOfActiveRequestsByServer[serverKey];
  8880. };
  8881. /**
  8882. * For testing only.
  8883. *
  8884. * @private
  8885. */
  8886. RequestScheduler.requestHeap = requestHeap;
  8887. /**
  8888. * A singleton that contains all of the servers that are trusted. Credentials will be sent with
  8889. * any requests to these servers.
  8890. *
  8891. * @namespace TrustedServers
  8892. *
  8893. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  8894. */
  8895. const TrustedServers = {};
  8896. let _servers = {};
  8897. /**
  8898. * Adds a trusted server to the registry
  8899. *
  8900. * @param {string} host The host to be added.
  8901. * @param {number} port The port used to access the host.
  8902. *
  8903. * @example
  8904. * // Add a trusted server
  8905. * TrustedServers.add('my.server.com', 80);
  8906. */
  8907. TrustedServers.add = function (host, port) {
  8908. //>>includeStart('debug', pragmas.debug);
  8909. if (!defaultValue.defined(host)) {
  8910. throw new Check.DeveloperError("host is required.");
  8911. }
  8912. if (!defaultValue.defined(port) || port <= 0) {
  8913. throw new Check.DeveloperError("port is required to be greater than 0.");
  8914. }
  8915. //>>includeEnd('debug');
  8916. const authority = `${host.toLowerCase()}:${port}`;
  8917. if (!defaultValue.defined(_servers[authority])) {
  8918. _servers[authority] = true;
  8919. }
  8920. };
  8921. /**
  8922. * Removes a trusted server from the registry
  8923. *
  8924. * @param {string} host The host to be removed.
  8925. * @param {number} port The port used to access the host.
  8926. *
  8927. * @example
  8928. * // Remove a trusted server
  8929. * TrustedServers.remove('my.server.com', 80);
  8930. */
  8931. TrustedServers.remove = function (host, port) {
  8932. //>>includeStart('debug', pragmas.debug);
  8933. if (!defaultValue.defined(host)) {
  8934. throw new Check.DeveloperError("host is required.");
  8935. }
  8936. if (!defaultValue.defined(port) || port <= 0) {
  8937. throw new Check.DeveloperError("port is required to be greater than 0.");
  8938. }
  8939. //>>includeEnd('debug');
  8940. const authority = `${host.toLowerCase()}:${port}`;
  8941. if (defaultValue.defined(_servers[authority])) {
  8942. delete _servers[authority];
  8943. }
  8944. };
  8945. function getAuthority(url) {
  8946. const uri = new Uri(url);
  8947. uri.normalize();
  8948. // Removes username:password@ so we just have host[:port]
  8949. let authority = uri.authority();
  8950. if (authority.length === 0) {
  8951. return undefined; // Relative URL
  8952. }
  8953. uri.authority(authority);
  8954. if (authority.indexOf("@") !== -1) {
  8955. const parts = authority.split("@");
  8956. authority = parts[1];
  8957. }
  8958. // If the port is missing add one based on the scheme
  8959. if (authority.indexOf(":") === -1) {
  8960. let scheme = uri.scheme();
  8961. if (scheme.length === 0) {
  8962. scheme = window.location.protocol;
  8963. scheme = scheme.substring(0, scheme.length - 1);
  8964. }
  8965. if (scheme === "http") {
  8966. authority += ":80";
  8967. } else if (scheme === "https") {
  8968. authority += ":443";
  8969. } else {
  8970. return undefined;
  8971. }
  8972. }
  8973. return authority;
  8974. }
  8975. /**
  8976. * Tests whether a server is trusted or not. The server must have been added with the port if it is included in the url.
  8977. *
  8978. * @param {string} url The url to be tested against the trusted list
  8979. *
  8980. * @returns {boolean} Returns true if url is trusted, false otherwise.
  8981. *
  8982. * @example
  8983. * // Add server
  8984. * TrustedServers.add('my.server.com', 81);
  8985. *
  8986. * // Check if server is trusted
  8987. * if (TrustedServers.contains('https://my.server.com:81/path/to/file.png')) {
  8988. * // my.server.com:81 is trusted
  8989. * }
  8990. * if (TrustedServers.contains('https://my.server.com/path/to/file.png')) {
  8991. * // my.server.com isn't trusted
  8992. * }
  8993. */
  8994. TrustedServers.contains = function (url) {
  8995. //>>includeStart('debug', pragmas.debug);
  8996. if (!defaultValue.defined(url)) {
  8997. throw new Check.DeveloperError("url is required.");
  8998. }
  8999. //>>includeEnd('debug');
  9000. const authority = getAuthority(url);
  9001. if (defaultValue.defined(authority) && defaultValue.defined(_servers[authority])) {
  9002. return true;
  9003. }
  9004. return false;
  9005. };
  9006. /**
  9007. * Clears the registry
  9008. *
  9009. * @example
  9010. * // Remove a trusted server
  9011. * TrustedServers.clear();
  9012. */
  9013. TrustedServers.clear = function () {
  9014. _servers = {};
  9015. };
  9016. var TrustedServers$1 = TrustedServers;
  9017. const xhrBlobSupported = (function () {
  9018. try {
  9019. const xhr = new XMLHttpRequest();
  9020. xhr.open("GET", "#", true);
  9021. xhr.responseType = "blob";
  9022. return xhr.responseType === "blob";
  9023. } catch (e) {
  9024. return false;
  9025. }
  9026. })();
  9027. /**
  9028. * @typedef {object} Resource.ConstructorOptions
  9029. *
  9030. * Initialization options for the Resource constructor
  9031. *
  9032. * @property {string} url The url of the resource.
  9033. * @property {object} [queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  9034. * @property {object} [templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  9035. * @property {object} [headers={}] Additional HTTP headers that will be sent.
  9036. * @property {Proxy} [proxy] A proxy to be used when loading the resource.
  9037. * @property {Resource.RetryCallback} [retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  9038. * @property {number} [retryAttempts=0] The number of times the retryCallback should be called before giving up.
  9039. * @property {Request} [request] A Request object that will be used. Intended for internal use only.
  9040. * @property {boolean} [parseUrl=true] If true, parse the url for query parameters; otherwise store the url without change
  9041. */
  9042. /**
  9043. * A resource that includes the location and any other parameters we need to retrieve it or create derived resources. It also provides the ability to retry requests.
  9044. *
  9045. * @alias Resource
  9046. * @constructor
  9047. *
  9048. * @param {string|Resource.ConstructorOptions} options A url or an object describing initialization options
  9049. *
  9050. * @example
  9051. * function refreshTokenRetryCallback(resource, error) {
  9052. * if (error.statusCode === 403) {
  9053. * // 403 status code means a new token should be generated
  9054. * return getNewAccessToken()
  9055. * .then(function(token) {
  9056. * resource.queryParameters.access_token = token;
  9057. * return true;
  9058. * })
  9059. * .catch(function() {
  9060. * return false;
  9061. * });
  9062. * }
  9063. *
  9064. * return false;
  9065. * }
  9066. *
  9067. * const resource = new Resource({
  9068. * url: 'http://server.com/path/to/resource.json',
  9069. * proxy: new DefaultProxy('/proxy/'),
  9070. * headers: {
  9071. * 'X-My-Header': 'valueOfHeader'
  9072. * },
  9073. * queryParameters: {
  9074. * 'access_token': '123-435-456-000'
  9075. * },
  9076. * retryCallback: refreshTokenRetryCallback,
  9077. * retryAttempts: 1
  9078. * });
  9079. */
  9080. function Resource(options) {
  9081. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  9082. if (typeof options === "string") {
  9083. options = {
  9084. url: options,
  9085. };
  9086. }
  9087. //>>includeStart('debug', pragmas.debug);
  9088. Check.Check.typeOf.string("options.url", options.url);
  9089. //>>includeEnd('debug');
  9090. this._url = undefined;
  9091. this._templateValues = defaultClone(options.templateValues, {});
  9092. this._queryParameters = defaultClone(options.queryParameters, {});
  9093. /**
  9094. * Additional HTTP headers that will be sent with the request.
  9095. *
  9096. * @type {object}
  9097. */
  9098. this.headers = defaultClone(options.headers, {});
  9099. /**
  9100. * A Request object that will be used. Intended for internal use only.
  9101. *
  9102. * @type {Request}
  9103. */
  9104. this.request = defaultValue.defaultValue(options.request, new Request());
  9105. /**
  9106. * A proxy to be used when loading the resource.
  9107. *
  9108. * @type {Proxy}
  9109. */
  9110. this.proxy = options.proxy;
  9111. /**
  9112. * Function to call when a request for this resource fails. If it returns true or a Promise that resolves to true, the request will be retried.
  9113. *
  9114. * @type {Function}
  9115. */
  9116. this.retryCallback = options.retryCallback;
  9117. /**
  9118. * The number of times the retryCallback should be called before giving up.
  9119. *
  9120. * @type {number}
  9121. */
  9122. this.retryAttempts = defaultValue.defaultValue(options.retryAttempts, 0);
  9123. this._retryCount = 0;
  9124. const parseUrl = defaultValue.defaultValue(options.parseUrl, true);
  9125. if (parseUrl) {
  9126. this.parseUrl(options.url, true, true);
  9127. } else {
  9128. this._url = options.url;
  9129. }
  9130. }
  9131. /**
  9132. * Clones a value if it is defined, otherwise returns the default value
  9133. *
  9134. * @param {object} [value] The value to clone.
  9135. * @param {object} [defaultValue] The default value.
  9136. *
  9137. * @returns {object} A clone of value or the defaultValue.
  9138. *
  9139. * @private
  9140. */
  9141. function defaultClone(value, defaultValue$1) {
  9142. return defaultValue.defined(value) ? clone(value) : defaultValue$1;
  9143. }
  9144. /**
  9145. * A helper function to create a resource depending on whether we have a String or a Resource
  9146. *
  9147. * @param {Resource|string} resource A Resource or a String to use when creating a new Resource.
  9148. *
  9149. * @returns {Resource} If resource is a String, a Resource constructed with the url and options. Otherwise the resource parameter is returned.
  9150. *
  9151. * @private
  9152. */
  9153. Resource.createIfNeeded = function (resource) {
  9154. if (resource instanceof Resource) {
  9155. // Keep existing request object. This function is used internally to duplicate a Resource, so that it can't
  9156. // be modified outside of a class that holds it (eg. an imagery or terrain provider). Since the Request objects
  9157. // are managed outside of the providers, by the tile loading code, we want to keep the request property the same so if it is changed
  9158. // in the underlying tiling code the requests for this resource will use it.
  9159. return resource.getDerivedResource({
  9160. request: resource.request,
  9161. });
  9162. }
  9163. if (typeof resource !== "string") {
  9164. return resource;
  9165. }
  9166. return new Resource({
  9167. url: resource,
  9168. });
  9169. };
  9170. let supportsImageBitmapOptionsPromise;
  9171. /**
  9172. * A helper function to check whether createImageBitmap supports passing ImageBitmapOptions.
  9173. *
  9174. * @returns {Promise<boolean>} A promise that resolves to true if this browser supports creating an ImageBitmap with options.
  9175. *
  9176. * @private
  9177. */
  9178. Resource.supportsImageBitmapOptions = function () {
  9179. // Until the HTML folks figure out what to do about this, we need to actually try loading an image to
  9180. // know if this browser supports passing options to the createImageBitmap function.
  9181. // https://github.com/whatwg/html/pull/4248
  9182. //
  9183. // We also need to check whether the colorSpaceConversion option is supported.
  9184. // We do this by loading a PNG with an embedded color profile, first with
  9185. // colorSpaceConversion: "none" and then with colorSpaceConversion: "default".
  9186. // If the pixel color is different then we know the option is working.
  9187. // As of Webkit 17612.3.6.1.6 the createImageBitmap promise resolves but the
  9188. // option is not actually supported.
  9189. if (defaultValue.defined(supportsImageBitmapOptionsPromise)) {
  9190. return supportsImageBitmapOptionsPromise;
  9191. }
  9192. if (typeof createImageBitmap !== "function") {
  9193. supportsImageBitmapOptionsPromise = Promise.resolve(false);
  9194. return supportsImageBitmapOptionsPromise;
  9195. }
  9196. const imageDataUri =
  9197. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAABGdBTUEAAE4g3rEiDgAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAADElEQVQI12Ng6GAAAAEUAIngE3ZiAAAAAElFTkSuQmCC";
  9198. supportsImageBitmapOptionsPromise = Resource.fetchBlob({
  9199. url: imageDataUri,
  9200. })
  9201. .then(function (blob) {
  9202. const imageBitmapOptions = {
  9203. imageOrientation: "flipY", // default is "none"
  9204. premultiplyAlpha: "none", // default is "default"
  9205. colorSpaceConversion: "none", // default is "default"
  9206. };
  9207. return Promise.all([
  9208. createImageBitmap(blob, imageBitmapOptions),
  9209. createImageBitmap(blob),
  9210. ]);
  9211. })
  9212. .then(function (imageBitmaps) {
  9213. // Check whether the colorSpaceConversion option had any effect on the green channel
  9214. const colorWithOptions = getImagePixels(imageBitmaps[0]);
  9215. const colorWithDefaults = getImagePixels(imageBitmaps[1]);
  9216. return colorWithOptions[1] !== colorWithDefaults[1];
  9217. })
  9218. .catch(function () {
  9219. return false;
  9220. });
  9221. return supportsImageBitmapOptionsPromise;
  9222. };
  9223. Object.defineProperties(Resource, {
  9224. /**
  9225. * Returns true if blobs are supported.
  9226. *
  9227. * @memberof Resource
  9228. * @type {boolean}
  9229. *
  9230. * @readonly
  9231. */
  9232. isBlobSupported: {
  9233. get: function () {
  9234. return xhrBlobSupported;
  9235. },
  9236. },
  9237. });
  9238. Object.defineProperties(Resource.prototype, {
  9239. /**
  9240. * Query parameters appended to the url.
  9241. *
  9242. * @memberof Resource.prototype
  9243. * @type {object}
  9244. *
  9245. * @readonly
  9246. */
  9247. queryParameters: {
  9248. get: function () {
  9249. return this._queryParameters;
  9250. },
  9251. },
  9252. /**
  9253. * The key/value pairs used to replace template parameters in the url.
  9254. *
  9255. * @memberof Resource.prototype
  9256. * @type {object}
  9257. *
  9258. * @readonly
  9259. */
  9260. templateValues: {
  9261. get: function () {
  9262. return this._templateValues;
  9263. },
  9264. },
  9265. /**
  9266. * The url to the resource with template values replaced, query string appended and encoded by proxy if one was set.
  9267. *
  9268. * @memberof Resource.prototype
  9269. * @type {string}
  9270. */
  9271. url: {
  9272. get: function () {
  9273. return this.getUrlComponent(true, true);
  9274. },
  9275. set: function (value) {
  9276. this.parseUrl(value, false, false);
  9277. },
  9278. },
  9279. /**
  9280. * The file extension of the resource.
  9281. *
  9282. * @memberof Resource.prototype
  9283. * @type {string}
  9284. *
  9285. * @readonly
  9286. */
  9287. extension: {
  9288. get: function () {
  9289. return getExtensionFromUri(this._url);
  9290. },
  9291. },
  9292. /**
  9293. * True if the Resource refers to a data URI.
  9294. *
  9295. * @memberof Resource.prototype
  9296. * @type {boolean}
  9297. */
  9298. isDataUri: {
  9299. get: function () {
  9300. return isDataUri(this._url);
  9301. },
  9302. },
  9303. /**
  9304. * True if the Resource refers to a blob URI.
  9305. *
  9306. * @memberof Resource.prototype
  9307. * @type {boolean}
  9308. */
  9309. isBlobUri: {
  9310. get: function () {
  9311. return isBlobUri(this._url);
  9312. },
  9313. },
  9314. /**
  9315. * True if the Resource refers to a cross origin URL.
  9316. *
  9317. * @memberof Resource.prototype
  9318. * @type {boolean}
  9319. */
  9320. isCrossOriginUrl: {
  9321. get: function () {
  9322. return isCrossOriginUrl(this._url);
  9323. },
  9324. },
  9325. /**
  9326. * True if the Resource has request headers. This is equivalent to checking if the headers property has any keys.
  9327. *
  9328. * @memberof Resource.prototype
  9329. * @type {boolean}
  9330. */
  9331. hasHeaders: {
  9332. get: function () {
  9333. return Object.keys(this.headers).length > 0;
  9334. },
  9335. },
  9336. });
  9337. /**
  9338. * Override Object#toString so that implicit string conversion gives the
  9339. * complete URL represented by this Resource.
  9340. *
  9341. * @returns {string} The URL represented by this Resource
  9342. */
  9343. Resource.prototype.toString = function () {
  9344. return this.getUrlComponent(true, true);
  9345. };
  9346. /**
  9347. * Parse a url string, and store its info
  9348. *
  9349. * @param {string} url The input url string.
  9350. * @param {boolean} merge If true, we'll merge with the resource's existing queryParameters. Otherwise they will be replaced.
  9351. * @param {boolean} preserveQuery If true duplicate parameters will be concatenated into an array. If false, keys in url will take precedence.
  9352. * @param {string} [baseUrl] If supplied, and input url is a relative url, it will be made absolute relative to baseUrl
  9353. *
  9354. * @private
  9355. */
  9356. Resource.prototype.parseUrl = function (url, merge, preserveQuery, baseUrl) {
  9357. let uri = new Uri(url);
  9358. const query = parseQueryString(uri.query());
  9359. this._queryParameters = merge
  9360. ? combineQueryParameters(query, this.queryParameters, preserveQuery)
  9361. : query;
  9362. // Remove unneeded info from the Uri
  9363. uri.search("");
  9364. uri.fragment("");
  9365. if (defaultValue.defined(baseUrl) && uri.scheme() === "") {
  9366. uri = uri.absoluteTo(getAbsoluteUri(baseUrl));
  9367. }
  9368. this._url = uri.toString();
  9369. };
  9370. /**
  9371. * Parses a query string and returns the object equivalent.
  9372. *
  9373. * @param {string} queryString The query string
  9374. * @returns {object}
  9375. *
  9376. * @private
  9377. */
  9378. function parseQueryString(queryString) {
  9379. if (queryString.length === 0) {
  9380. return {};
  9381. }
  9382. // Special case where the querystring is just a string, not key/value pairs
  9383. if (queryString.indexOf("=") === -1) {
  9384. return { [queryString]: undefined };
  9385. }
  9386. return queryToObject(queryString);
  9387. }
  9388. /**
  9389. * This combines a map of query parameters.
  9390. *
  9391. * @param {object} q1 The first map of query parameters. Values in this map will take precedence if preserveQueryParameters is false.
  9392. * @param {object} q2 The second map of query parameters.
  9393. * @param {boolean} preserveQueryParameters If true duplicate parameters will be concatenated into an array. If false, keys in q1 will take precedence.
  9394. *
  9395. * @returns {object} The combined map of query parameters.
  9396. *
  9397. * @example
  9398. * const q1 = {
  9399. * a: 1,
  9400. * b: 2
  9401. * };
  9402. * const q2 = {
  9403. * a: 3,
  9404. * c: 4
  9405. * };
  9406. * const q3 = {
  9407. * b: [5, 6],
  9408. * d: 7
  9409. * }
  9410. *
  9411. * // Returns
  9412. * // {
  9413. * // a: [1, 3],
  9414. * // b: 2,
  9415. * // c: 4
  9416. * // };
  9417. * combineQueryParameters(q1, q2, true);
  9418. *
  9419. * // Returns
  9420. * // {
  9421. * // a: 1,
  9422. * // b: 2,
  9423. * // c: 4
  9424. * // };
  9425. * combineQueryParameters(q1, q2, false);
  9426. *
  9427. * // Returns
  9428. * // {
  9429. * // a: 1,
  9430. * // b: [2, 5, 6],
  9431. * // d: 7
  9432. * // };
  9433. * combineQueryParameters(q1, q3, true);
  9434. *
  9435. * // Returns
  9436. * // {
  9437. * // a: 1,
  9438. * // b: 2,
  9439. * // d: 7
  9440. * // };
  9441. * combineQueryParameters(q1, q3, false);
  9442. *
  9443. * @private
  9444. */
  9445. function combineQueryParameters(q1, q2, preserveQueryParameters) {
  9446. if (!preserveQueryParameters) {
  9447. return combine.combine(q1, q2);
  9448. }
  9449. const result = clone(q1, true);
  9450. for (const param in q2) {
  9451. if (q2.hasOwnProperty(param)) {
  9452. let value = result[param];
  9453. const q2Value = q2[param];
  9454. if (defaultValue.defined(value)) {
  9455. if (!Array.isArray(value)) {
  9456. value = result[param] = [value];
  9457. }
  9458. result[param] = value.concat(q2Value);
  9459. } else {
  9460. result[param] = Array.isArray(q2Value) ? q2Value.slice() : q2Value;
  9461. }
  9462. }
  9463. }
  9464. return result;
  9465. }
  9466. /**
  9467. * Returns the url, optional with the query string and processed by a proxy.
  9468. *
  9469. * @param {boolean} [query=false] If true, the query string is included.
  9470. * @param {boolean} [proxy=false] If true, the url is processed by the proxy object, if defined.
  9471. *
  9472. * @returns {string} The url with all the requested components.
  9473. */
  9474. Resource.prototype.getUrlComponent = function (query, proxy) {
  9475. if (this.isDataUri) {
  9476. return this._url;
  9477. }
  9478. let url = this._url;
  9479. if (query) {
  9480. url = `${url}${stringifyQuery(this.queryParameters)}`;
  9481. }
  9482. // Restore the placeholders, which may have been escaped in objectToQuery or elsewhere
  9483. url = url.replace(/%7B/g, "{").replace(/%7D/g, "}");
  9484. const templateValues = this._templateValues;
  9485. if (Object.keys(templateValues).length > 0) {
  9486. url = url.replace(/{(.*?)}/g, function (match, key) {
  9487. const replacement = templateValues[key];
  9488. if (defaultValue.defined(replacement)) {
  9489. // use the replacement value from templateValues if there is one...
  9490. return encodeURIComponent(replacement);
  9491. }
  9492. // otherwise leave it unchanged
  9493. return match;
  9494. });
  9495. }
  9496. if (proxy && defaultValue.defined(this.proxy)) {
  9497. url = this.proxy.getURL(url);
  9498. }
  9499. return url;
  9500. };
  9501. /**
  9502. * Converts a query object into a string.
  9503. *
  9504. * @param {object} queryObject The object with query parameters
  9505. * @returns {string}
  9506. *
  9507. * @private
  9508. */
  9509. function stringifyQuery(queryObject) {
  9510. const keys = Object.keys(queryObject);
  9511. if (keys.length === 0) {
  9512. return "";
  9513. }
  9514. if (keys.length === 1 && !defaultValue.defined(queryObject[keys[0]])) {
  9515. // We have 1 key with an undefined value, so this is just a string, not key/value pairs
  9516. return `?${keys[0]}`;
  9517. }
  9518. return `?${objectToQuery(queryObject)}`;
  9519. }
  9520. /**
  9521. * Combines the specified object and the existing query parameters. This allows you to add many parameters at once,
  9522. * as opposed to adding them one at a time to the queryParameters property. If a value is already set, it will be replaced with the new value.
  9523. *
  9524. * @param {object} params The query parameters
  9525. * @param {boolean} [useAsDefault=false] If true the params will be used as the default values, so they will only be set if they are undefined.
  9526. */
  9527. Resource.prototype.setQueryParameters = function (params, useAsDefault) {
  9528. if (useAsDefault) {
  9529. this._queryParameters = combineQueryParameters(
  9530. this._queryParameters,
  9531. params,
  9532. false
  9533. );
  9534. } else {
  9535. this._queryParameters = combineQueryParameters(
  9536. params,
  9537. this._queryParameters,
  9538. false
  9539. );
  9540. }
  9541. };
  9542. /**
  9543. * Combines the specified object and the existing query parameters. This allows you to add many parameters at once,
  9544. * as opposed to adding them one at a time to the queryParameters property.
  9545. *
  9546. * @param {object} params The query parameters
  9547. */
  9548. Resource.prototype.appendQueryParameters = function (params) {
  9549. this._queryParameters = combineQueryParameters(
  9550. params,
  9551. this._queryParameters,
  9552. true
  9553. );
  9554. };
  9555. /**
  9556. * Combines the specified object and the existing template values. This allows you to add many values at once,
  9557. * as opposed to adding them one at a time to the templateValues property. If a value is already set, it will become an array and the new value will be appended.
  9558. *
  9559. * @param {object} template The template values
  9560. * @param {boolean} [useAsDefault=false] If true the values will be used as the default values, so they will only be set if they are undefined.
  9561. */
  9562. Resource.prototype.setTemplateValues = function (template, useAsDefault) {
  9563. if (useAsDefault) {
  9564. this._templateValues = combine.combine(this._templateValues, template);
  9565. } else {
  9566. this._templateValues = combine.combine(template, this._templateValues);
  9567. }
  9568. };
  9569. /**
  9570. * Returns a resource relative to the current instance. All properties remain the same as the current instance unless overridden in options.
  9571. *
  9572. * @param {object} options An object with the following properties
  9573. * @param {string} [options.url] The url that will be resolved relative to the url of the current instance.
  9574. * @param {object} [options.queryParameters] An object containing query parameters that will be combined with those of the current instance.
  9575. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}). These will be combined with those of the current instance.
  9576. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  9577. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  9578. * @param {Resource.RetryCallback} [options.retryCallback] The function to call when loading the resource fails.
  9579. * @param {number} [options.retryAttempts] The number of times the retryCallback should be called before giving up.
  9580. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  9581. * @param {boolean} [options.preserveQueryParameters=false] If true, this will keep all query parameters from the current resource and derived resource. If false, derived parameters will replace those of the current resource.
  9582. *
  9583. * @returns {Resource} The resource derived from the current one.
  9584. */
  9585. Resource.prototype.getDerivedResource = function (options) {
  9586. const resource = this.clone();
  9587. resource._retryCount = 0;
  9588. if (defaultValue.defined(options.url)) {
  9589. const preserveQuery = defaultValue.defaultValue(options.preserveQueryParameters, false);
  9590. resource.parseUrl(options.url, true, preserveQuery, this._url);
  9591. }
  9592. if (defaultValue.defined(options.queryParameters)) {
  9593. resource._queryParameters = combine.combine(
  9594. options.queryParameters,
  9595. resource.queryParameters
  9596. );
  9597. }
  9598. if (defaultValue.defined(options.templateValues)) {
  9599. resource._templateValues = combine.combine(
  9600. options.templateValues,
  9601. resource.templateValues
  9602. );
  9603. }
  9604. if (defaultValue.defined(options.headers)) {
  9605. resource.headers = combine.combine(options.headers, resource.headers);
  9606. }
  9607. if (defaultValue.defined(options.proxy)) {
  9608. resource.proxy = options.proxy;
  9609. }
  9610. if (defaultValue.defined(options.request)) {
  9611. resource.request = options.request;
  9612. }
  9613. if (defaultValue.defined(options.retryCallback)) {
  9614. resource.retryCallback = options.retryCallback;
  9615. }
  9616. if (defaultValue.defined(options.retryAttempts)) {
  9617. resource.retryAttempts = options.retryAttempts;
  9618. }
  9619. return resource;
  9620. };
  9621. /**
  9622. * Called when a resource fails to load. This will call the retryCallback function if defined until retryAttempts is reached.
  9623. *
  9624. * @param {RequestErrorEvent} [error] The error that was encountered.
  9625. *
  9626. * @returns {Promise<boolean>} A promise to a boolean, that if true will cause the resource request to be retried.
  9627. *
  9628. * @private
  9629. */
  9630. Resource.prototype.retryOnError = function (error) {
  9631. const retryCallback = this.retryCallback;
  9632. if (
  9633. typeof retryCallback !== "function" ||
  9634. this._retryCount >= this.retryAttempts
  9635. ) {
  9636. return Promise.resolve(false);
  9637. }
  9638. const that = this;
  9639. return Promise.resolve(retryCallback(this, error)).then(function (result) {
  9640. ++that._retryCount;
  9641. return result;
  9642. });
  9643. };
  9644. /**
  9645. * Duplicates a Resource instance.
  9646. *
  9647. * @param {Resource} [result] The object onto which to store the result.
  9648. *
  9649. * @returns {Resource} The modified result parameter or a new Resource instance if one was not provided.
  9650. */
  9651. Resource.prototype.clone = function (result) {
  9652. if (!defaultValue.defined(result)) {
  9653. return new Resource({
  9654. url: this._url,
  9655. queryParameters: this.queryParameters,
  9656. templateValues: this.templateValues,
  9657. headers: this.headers,
  9658. proxy: this.proxy,
  9659. retryCallback: this.retryCallback,
  9660. retryAttempts: this.retryAttempts,
  9661. request: this.request.clone(),
  9662. parseUrl: false,
  9663. });
  9664. }
  9665. result._url = this._url;
  9666. result._queryParameters = clone(this._queryParameters);
  9667. result._templateValues = clone(this._templateValues);
  9668. result.headers = clone(this.headers);
  9669. result.proxy = this.proxy;
  9670. result.retryCallback = this.retryCallback;
  9671. result.retryAttempts = this.retryAttempts;
  9672. result._retryCount = 0;
  9673. result.request = this.request.clone();
  9674. return result;
  9675. };
  9676. /**
  9677. * Returns the base path of the Resource.
  9678. *
  9679. * @param {boolean} [includeQuery = false] Whether or not to include the query string and fragment form the uri
  9680. *
  9681. * @returns {string} The base URI of the resource
  9682. */
  9683. Resource.prototype.getBaseUri = function (includeQuery) {
  9684. return getBaseUri(this.getUrlComponent(includeQuery), includeQuery);
  9685. };
  9686. /**
  9687. * Appends a forward slash to the URL.
  9688. */
  9689. Resource.prototype.appendForwardSlash = function () {
  9690. this._url = appendForwardSlash(this._url);
  9691. };
  9692. /**
  9693. * Asynchronously loads the resource as raw binary data. Returns a promise that will resolve to
  9694. * an ArrayBuffer once loaded, or reject if the resource failed to load. The data is loaded
  9695. * using XMLHttpRequest, which means that in order to make requests to another origin,
  9696. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  9697. *
  9698. * @returns {Promise<ArrayBuffer>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  9699. *
  9700. * @example
  9701. * // load a single URL asynchronously
  9702. * resource.fetchArrayBuffer().then(function(arrayBuffer) {
  9703. * // use the data
  9704. * }).catch(function(error) {
  9705. * // an error occurred
  9706. * });
  9707. *
  9708. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  9709. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  9710. */
  9711. Resource.prototype.fetchArrayBuffer = function () {
  9712. return this.fetch({
  9713. responseType: "arraybuffer",
  9714. });
  9715. };
  9716. /**
  9717. * Creates a Resource and calls fetchArrayBuffer() on it.
  9718. *
  9719. * @param {string|object} options A url or an object with the following properties
  9720. * @param {string} options.url The url of the resource.
  9721. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  9722. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  9723. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  9724. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  9725. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  9726. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  9727. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  9728. * @returns {Promise<ArrayBuffer>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  9729. */
  9730. Resource.fetchArrayBuffer = function (options) {
  9731. const resource = new Resource(options);
  9732. return resource.fetchArrayBuffer();
  9733. };
  9734. /**
  9735. * Asynchronously loads the given resource as a blob. Returns a promise that will resolve to
  9736. * a Blob once loaded, or reject if the resource failed to load. The data is loaded
  9737. * using XMLHttpRequest, which means that in order to make requests to another origin,
  9738. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  9739. *
  9740. * @returns {Promise<Blob>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  9741. *
  9742. * @example
  9743. * // load a single URL asynchronously
  9744. * resource.fetchBlob().then(function(blob) {
  9745. * // use the data
  9746. * }).catch(function(error) {
  9747. * // an error occurred
  9748. * });
  9749. *
  9750. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  9751. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  9752. */
  9753. Resource.prototype.fetchBlob = function () {
  9754. return this.fetch({
  9755. responseType: "blob",
  9756. });
  9757. };
  9758. /**
  9759. * Creates a Resource and calls fetchBlob() on it.
  9760. *
  9761. * @param {string|object} options A url or an object with the following properties
  9762. * @param {string} options.url The url of the resource.
  9763. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  9764. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  9765. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  9766. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  9767. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  9768. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  9769. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  9770. * @returns {Promise<Blob>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  9771. */
  9772. Resource.fetchBlob = function (options) {
  9773. const resource = new Resource(options);
  9774. return resource.fetchBlob();
  9775. };
  9776. /**
  9777. * Asynchronously loads the given image resource. Returns a promise that will resolve to
  9778. * an {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap|ImageBitmap} if <code>preferImageBitmap</code> is true and the browser supports <code>createImageBitmap</code> or otherwise an
  9779. * {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement|Image} once loaded, or reject if the image failed to load.
  9780. *
  9781. * @param {object} [options] An object with the following properties.
  9782. * @param {boolean} [options.preferBlob=false] If true, we will load the image via a blob.
  9783. * @param {boolean} [options.preferImageBitmap=false] If true, image will be decoded during fetch and an <code>ImageBitmap</code> is returned.
  9784. * @param {boolean} [options.flipY=false] If true, image will be vertically flipped during decode. Only applies if the browser supports <code>createImageBitmap</code>.
  9785. * @param {boolean} [options.skipColorSpaceConversion=false] If true, any custom gamma or color profiles in the image will be ignored. Only applies if the browser supports <code>createImageBitmap</code>.
  9786. * @returns {Promise<ImageBitmap|HTMLImageElement>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  9787. *
  9788. *
  9789. * @example
  9790. * // load a single image asynchronously
  9791. * resource.fetchImage().then(function(image) {
  9792. * // use the loaded image
  9793. * }).catch(function(error) {
  9794. * // an error occurred
  9795. * });
  9796. *
  9797. * // load several images in parallel
  9798. * Promise.all([resource1.fetchImage(), resource2.fetchImage()]).then(function(images) {
  9799. * // images is an array containing all the loaded images
  9800. * });
  9801. *
  9802. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  9803. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  9804. */
  9805. Resource.prototype.fetchImage = function (options) {
  9806. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  9807. const preferImageBitmap = defaultValue.defaultValue(options.preferImageBitmap, false);
  9808. const preferBlob = defaultValue.defaultValue(options.preferBlob, false);
  9809. const flipY = defaultValue.defaultValue(options.flipY, false);
  9810. const skipColorSpaceConversion = defaultValue.defaultValue(
  9811. options.skipColorSpaceConversion,
  9812. false
  9813. );
  9814. checkAndResetRequest(this.request);
  9815. // We try to load the image normally if
  9816. // 1. Blobs aren't supported
  9817. // 2. It's a data URI
  9818. // 3. It's a blob URI
  9819. // 4. It doesn't have request headers and we preferBlob is false
  9820. if (
  9821. !xhrBlobSupported ||
  9822. this.isDataUri ||
  9823. this.isBlobUri ||
  9824. (!this.hasHeaders && !preferBlob)
  9825. ) {
  9826. return fetchImage({
  9827. resource: this,
  9828. flipY: flipY,
  9829. skipColorSpaceConversion: skipColorSpaceConversion,
  9830. preferImageBitmap: preferImageBitmap,
  9831. });
  9832. }
  9833. const blobPromise = this.fetchBlob();
  9834. if (!defaultValue.defined(blobPromise)) {
  9835. return;
  9836. }
  9837. let supportsImageBitmap;
  9838. let useImageBitmap;
  9839. let generatedBlobResource;
  9840. let generatedBlob;
  9841. return Resource.supportsImageBitmapOptions()
  9842. .then(function (result) {
  9843. supportsImageBitmap = result;
  9844. useImageBitmap = supportsImageBitmap && preferImageBitmap;
  9845. return blobPromise;
  9846. })
  9847. .then(function (blob) {
  9848. if (!defaultValue.defined(blob)) {
  9849. return;
  9850. }
  9851. generatedBlob = blob;
  9852. if (useImageBitmap) {
  9853. return Resource.createImageBitmapFromBlob(blob, {
  9854. flipY: flipY,
  9855. premultiplyAlpha: false,
  9856. skipColorSpaceConversion: skipColorSpaceConversion,
  9857. });
  9858. }
  9859. const blobUrl = window.URL.createObjectURL(blob);
  9860. generatedBlobResource = new Resource({
  9861. url: blobUrl,
  9862. });
  9863. return fetchImage({
  9864. resource: generatedBlobResource,
  9865. flipY: flipY,
  9866. skipColorSpaceConversion: skipColorSpaceConversion,
  9867. preferImageBitmap: false,
  9868. });
  9869. })
  9870. .then(function (image) {
  9871. if (!defaultValue.defined(image)) {
  9872. return;
  9873. }
  9874. // The blob object may be needed for use by a TileDiscardPolicy,
  9875. // so attach it to the image.
  9876. image.blob = generatedBlob;
  9877. if (useImageBitmap) {
  9878. return image;
  9879. }
  9880. window.URL.revokeObjectURL(generatedBlobResource.url);
  9881. return image;
  9882. })
  9883. .catch(function (error) {
  9884. if (defaultValue.defined(generatedBlobResource)) {
  9885. window.URL.revokeObjectURL(generatedBlobResource.url);
  9886. }
  9887. // If the blob load succeeded but the image decode failed, attach the blob
  9888. // to the error object for use by a TileDiscardPolicy.
  9889. // In particular, BingMapsImageryProvider uses this to detect the
  9890. // zero-length response that is returned when a tile is not available.
  9891. error.blob = generatedBlob;
  9892. return Promise.reject(error);
  9893. });
  9894. };
  9895. /**
  9896. * Fetches an image and returns a promise to it.
  9897. *
  9898. * @param {object} [options] An object with the following properties.
  9899. * @param {Resource} [options.resource] Resource object that points to an image to fetch.
  9900. * @param {boolean} [options.preferImageBitmap] If true, image will be decoded during fetch and an <code>ImageBitmap</code> is returned.
  9901. * @param {boolean} [options.flipY] If true, image will be vertically flipped during decode. Only applies if the browser supports <code>createImageBitmap</code>.
  9902. * @param {boolean} [options.skipColorSpaceConversion=false] If true, any custom gamma or color profiles in the image will be ignored. Only applies if the browser supports <code>createImageBitmap</code>.
  9903. * @private
  9904. */
  9905. function fetchImage(options) {
  9906. const resource = options.resource;
  9907. const flipY = options.flipY;
  9908. const skipColorSpaceConversion = options.skipColorSpaceConversion;
  9909. const preferImageBitmap = options.preferImageBitmap;
  9910. const request = resource.request;
  9911. request.url = resource.url;
  9912. request.requestFunction = function () {
  9913. let crossOrigin = false;
  9914. // data URIs can't have crossorigin set.
  9915. if (!resource.isDataUri && !resource.isBlobUri) {
  9916. crossOrigin = resource.isCrossOriginUrl;
  9917. }
  9918. const deferred = defer();
  9919. Resource._Implementations.createImage(
  9920. request,
  9921. crossOrigin,
  9922. deferred,
  9923. flipY,
  9924. skipColorSpaceConversion,
  9925. preferImageBitmap
  9926. );
  9927. return deferred.promise;
  9928. };
  9929. const promise = RequestScheduler.request(request);
  9930. if (!defaultValue.defined(promise)) {
  9931. return;
  9932. }
  9933. return promise.catch(function (e) {
  9934. // Don't retry cancelled or otherwise aborted requests
  9935. if (request.state !== RequestState$1.FAILED) {
  9936. return Promise.reject(e);
  9937. }
  9938. return resource.retryOnError(e).then(function (retry) {
  9939. if (retry) {
  9940. // Reset request so it can try again
  9941. request.state = RequestState$1.UNISSUED;
  9942. request.deferred = undefined;
  9943. return fetchImage({
  9944. resource: resource,
  9945. flipY: flipY,
  9946. skipColorSpaceConversion: skipColorSpaceConversion,
  9947. preferImageBitmap: preferImageBitmap,
  9948. });
  9949. }
  9950. return Promise.reject(e);
  9951. });
  9952. });
  9953. }
  9954. /**
  9955. * Creates a Resource and calls fetchImage() on it.
  9956. *
  9957. * @param {string|object} options A url or an object with the following properties
  9958. * @param {string} options.url The url of the resource.
  9959. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  9960. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  9961. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  9962. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  9963. * @param {boolean} [options.flipY=false] Whether to vertically flip the image during fetch and decode. Only applies when requesting an image and the browser supports <code>createImageBitmap</code>.
  9964. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  9965. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  9966. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  9967. * @param {boolean} [options.preferBlob=false] If true, we will load the image via a blob.
  9968. * @param {boolean} [options.preferImageBitmap=false] If true, image will be decoded during fetch and an <code>ImageBitmap</code> is returned.
  9969. * @param {boolean} [options.skipColorSpaceConversion=false] If true, any custom gamma or color profiles in the image will be ignored. Only applies when requesting an image and the browser supports <code>createImageBitmap</code>.
  9970. * @returns {Promise<ImageBitmap|HTMLImageElement>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  9971. */
  9972. Resource.fetchImage = function (options) {
  9973. const resource = new Resource(options);
  9974. return resource.fetchImage({
  9975. flipY: options.flipY,
  9976. skipColorSpaceConversion: options.skipColorSpaceConversion,
  9977. preferBlob: options.preferBlob,
  9978. preferImageBitmap: options.preferImageBitmap,
  9979. });
  9980. };
  9981. /**
  9982. * Asynchronously loads the given resource as text. Returns a promise that will resolve to
  9983. * a String once loaded, or reject if the resource failed to load. The data is loaded
  9984. * using XMLHttpRequest, which means that in order to make requests to another origin,
  9985. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  9986. *
  9987. * @returns {Promise<string>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  9988. *
  9989. * @example
  9990. * // load text from a URL, setting a custom header
  9991. * const resource = new Resource({
  9992. * url: 'http://someUrl.com/someJson.txt',
  9993. * headers: {
  9994. * 'X-Custom-Header' : 'some value'
  9995. * }
  9996. * });
  9997. * resource.fetchText().then(function(text) {
  9998. * // Do something with the text
  9999. * }).catch(function(error) {
  10000. * // an error occurred
  10001. * });
  10002. *
  10003. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
  10004. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10005. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10006. */
  10007. Resource.prototype.fetchText = function () {
  10008. return this.fetch({
  10009. responseType: "text",
  10010. });
  10011. };
  10012. /**
  10013. * Creates a Resource and calls fetchText() on it.
  10014. *
  10015. * @param {string|object} options A url or an object with the following properties
  10016. * @param {string} options.url The url of the resource.
  10017. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10018. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10019. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10020. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10021. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10022. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10023. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10024. * @returns {Promise<string>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10025. */
  10026. Resource.fetchText = function (options) {
  10027. const resource = new Resource(options);
  10028. return resource.fetchText();
  10029. };
  10030. // note: &#42;&#47;&#42; below is */* but that ends the comment block early
  10031. /**
  10032. * Asynchronously loads the given resource as JSON. Returns a promise that will resolve to
  10033. * a JSON object once loaded, or reject if the resource failed to load. The data is loaded
  10034. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10035. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled. This function
  10036. * adds 'Accept: application/json,&#42;&#47;&#42;;q=0.01' to the request headers, if not
  10037. * already specified.
  10038. *
  10039. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10040. *
  10041. *
  10042. * @example
  10043. * resource.fetchJson().then(function(jsonData) {
  10044. * // Do something with the JSON object
  10045. * }).catch(function(error) {
  10046. * // an error occurred
  10047. * });
  10048. *
  10049. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10050. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10051. */
  10052. Resource.prototype.fetchJson = function () {
  10053. const promise = this.fetch({
  10054. responseType: "text",
  10055. headers: {
  10056. Accept: "application/json,*/*;q=0.01",
  10057. },
  10058. });
  10059. if (!defaultValue.defined(promise)) {
  10060. return undefined;
  10061. }
  10062. return promise.then(function (value) {
  10063. if (!defaultValue.defined(value)) {
  10064. return;
  10065. }
  10066. return JSON.parse(value);
  10067. });
  10068. };
  10069. /**
  10070. * Creates a Resource and calls fetchJson() on it.
  10071. *
  10072. * @param {string|object} options A url or an object with the following properties
  10073. * @param {string} options.url The url of the resource.
  10074. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10075. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10076. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10077. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10078. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10079. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10080. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10081. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10082. */
  10083. Resource.fetchJson = function (options) {
  10084. const resource = new Resource(options);
  10085. return resource.fetchJson();
  10086. };
  10087. /**
  10088. * Asynchronously loads the given resource as XML. Returns a promise that will resolve to
  10089. * an XML Document once loaded, or reject if the resource failed to load. The data is loaded
  10090. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10091. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  10092. *
  10093. * @returns {Promise<XMLDocument>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10094. *
  10095. *
  10096. * @example
  10097. * // load XML from a URL, setting a custom header
  10098. * Cesium.loadXML('http://someUrl.com/someXML.xml', {
  10099. * 'X-Custom-Header' : 'some value'
  10100. * }).then(function(document) {
  10101. * // Do something with the document
  10102. * }).catch(function(error) {
  10103. * // an error occurred
  10104. * });
  10105. *
  10106. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
  10107. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10108. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10109. */
  10110. Resource.prototype.fetchXML = function () {
  10111. return this.fetch({
  10112. responseType: "document",
  10113. overrideMimeType: "text/xml",
  10114. });
  10115. };
  10116. /**
  10117. * Creates a Resource and calls fetchXML() on it.
  10118. *
  10119. * @param {string|object} options A url or an object with the following properties
  10120. * @param {string} options.url The url of the resource.
  10121. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10122. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10123. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10124. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10125. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10126. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10127. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10128. * @returns {Promise<XMLDocument>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10129. */
  10130. Resource.fetchXML = function (options) {
  10131. const resource = new Resource(options);
  10132. return resource.fetchXML();
  10133. };
  10134. /**
  10135. * Requests a resource using JSONP.
  10136. *
  10137. * @param {string} [callbackParameterName='callback'] The callback parameter name that the server expects.
  10138. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10139. *
  10140. *
  10141. * @example
  10142. * // load a data asynchronously
  10143. * resource.fetchJsonp().then(function(data) {
  10144. * // use the loaded data
  10145. * }).catch(function(error) {
  10146. * // an error occurred
  10147. * });
  10148. *
  10149. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10150. */
  10151. Resource.prototype.fetchJsonp = function (callbackParameterName) {
  10152. callbackParameterName = defaultValue.defaultValue(callbackParameterName, "callback");
  10153. checkAndResetRequest(this.request);
  10154. //generate a unique function name
  10155. let functionName;
  10156. do {
  10157. functionName = `loadJsonp${Math$1.CesiumMath.nextRandomNumber()
  10158. .toString()
  10159. .substring(2, 8)}`;
  10160. } while (defaultValue.defined(window[functionName]));
  10161. return fetchJsonp(this, callbackParameterName, functionName);
  10162. };
  10163. function fetchJsonp(resource, callbackParameterName, functionName) {
  10164. const callbackQuery = {};
  10165. callbackQuery[callbackParameterName] = functionName;
  10166. resource.setQueryParameters(callbackQuery);
  10167. const request = resource.request;
  10168. const url = resource.url;
  10169. request.url = url;
  10170. request.requestFunction = function () {
  10171. const deferred = defer();
  10172. //assign a function with that name in the global scope
  10173. window[functionName] = function (data) {
  10174. deferred.resolve(data);
  10175. try {
  10176. delete window[functionName];
  10177. } catch (e) {
  10178. window[functionName] = undefined;
  10179. }
  10180. };
  10181. Resource._Implementations.loadAndExecuteScript(url, functionName, deferred);
  10182. return deferred.promise;
  10183. };
  10184. const promise = RequestScheduler.request(request);
  10185. if (!defaultValue.defined(promise)) {
  10186. return;
  10187. }
  10188. return promise.catch(function (e) {
  10189. if (request.state !== RequestState$1.FAILED) {
  10190. return Promise.reject(e);
  10191. }
  10192. return resource.retryOnError(e).then(function (retry) {
  10193. if (retry) {
  10194. // Reset request so it can try again
  10195. request.state = RequestState$1.UNISSUED;
  10196. request.deferred = undefined;
  10197. return fetchJsonp(resource, callbackParameterName, functionName);
  10198. }
  10199. return Promise.reject(e);
  10200. });
  10201. });
  10202. }
  10203. /**
  10204. * Creates a Resource from a URL and calls fetchJsonp() on it.
  10205. *
  10206. * @param {string|object} options A url or an object with the following properties
  10207. * @param {string} options.url The url of the resource.
  10208. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10209. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10210. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10211. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10212. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10213. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10214. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10215. * @param {string} [options.callbackParameterName='callback'] The callback parameter name that the server expects.
  10216. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10217. */
  10218. Resource.fetchJsonp = function (options) {
  10219. const resource = new Resource(options);
  10220. return resource.fetchJsonp(options.callbackParameterName);
  10221. };
  10222. /**
  10223. * @private
  10224. */
  10225. Resource.prototype._makeRequest = function (options) {
  10226. const resource = this;
  10227. checkAndResetRequest(resource.request);
  10228. const request = resource.request;
  10229. const url = resource.url;
  10230. request.url = url;
  10231. request.requestFunction = function () {
  10232. const responseType = options.responseType;
  10233. const headers = combine.combine(options.headers, resource.headers);
  10234. const overrideMimeType = options.overrideMimeType;
  10235. const method = options.method;
  10236. const data = options.data;
  10237. const deferred = defer();
  10238. const xhr = Resource._Implementations.loadWithXhr(
  10239. url,
  10240. responseType,
  10241. method,
  10242. data,
  10243. headers,
  10244. deferred,
  10245. overrideMimeType
  10246. );
  10247. if (defaultValue.defined(xhr) && defaultValue.defined(xhr.abort)) {
  10248. request.cancelFunction = function () {
  10249. xhr.abort();
  10250. };
  10251. }
  10252. return deferred.promise;
  10253. };
  10254. const promise = RequestScheduler.request(request);
  10255. if (!defaultValue.defined(promise)) {
  10256. return;
  10257. }
  10258. return promise
  10259. .then(function (data) {
  10260. // explicitly set to undefined to ensure GC of request response data. See #8843
  10261. request.cancelFunction = undefined;
  10262. return data;
  10263. })
  10264. .catch(function (e) {
  10265. request.cancelFunction = undefined;
  10266. if (request.state !== RequestState$1.FAILED) {
  10267. return Promise.reject(e);
  10268. }
  10269. return resource.retryOnError(e).then(function (retry) {
  10270. if (retry) {
  10271. // Reset request so it can try again
  10272. request.state = RequestState$1.UNISSUED;
  10273. request.deferred = undefined;
  10274. return resource.fetch(options);
  10275. }
  10276. return Promise.reject(e);
  10277. });
  10278. });
  10279. };
  10280. /**
  10281. * Checks to make sure the Resource isn't already being requested.
  10282. *
  10283. * @param {Request} request The request to check.
  10284. *
  10285. * @private
  10286. */
  10287. function checkAndResetRequest(request) {
  10288. if (
  10289. request.state === RequestState$1.ISSUED ||
  10290. request.state === RequestState$1.ACTIVE
  10291. ) {
  10292. throw new RuntimeError.RuntimeError("The Resource is already being fetched.");
  10293. }
  10294. request.state = RequestState$1.UNISSUED;
  10295. request.deferred = undefined;
  10296. }
  10297. const dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
  10298. function decodeDataUriText(isBase64, data) {
  10299. const result = decodeURIComponent(data);
  10300. if (isBase64) {
  10301. return atob(result);
  10302. }
  10303. return result;
  10304. }
  10305. function decodeDataUriArrayBuffer(isBase64, data) {
  10306. const byteString = decodeDataUriText(isBase64, data);
  10307. const buffer = new ArrayBuffer(byteString.length);
  10308. const view = new Uint8Array(buffer);
  10309. for (let i = 0; i < byteString.length; i++) {
  10310. view[i] = byteString.charCodeAt(i);
  10311. }
  10312. return buffer;
  10313. }
  10314. function decodeDataUri(dataUriRegexResult, responseType) {
  10315. responseType = defaultValue.defaultValue(responseType, "");
  10316. const mimeType = dataUriRegexResult[1];
  10317. const isBase64 = !!dataUriRegexResult[2];
  10318. const data = dataUriRegexResult[3];
  10319. let buffer;
  10320. let parser;
  10321. switch (responseType) {
  10322. case "":
  10323. case "text":
  10324. return decodeDataUriText(isBase64, data);
  10325. case "arraybuffer":
  10326. return decodeDataUriArrayBuffer(isBase64, data);
  10327. case "blob":
  10328. buffer = decodeDataUriArrayBuffer(isBase64, data);
  10329. return new Blob([buffer], {
  10330. type: mimeType,
  10331. });
  10332. case "document":
  10333. parser = new DOMParser();
  10334. return parser.parseFromString(
  10335. decodeDataUriText(isBase64, data),
  10336. mimeType
  10337. );
  10338. case "json":
  10339. return JSON.parse(decodeDataUriText(isBase64, data));
  10340. default:
  10341. //>>includeStart('debug', pragmas.debug);
  10342. throw new Check.DeveloperError(`Unhandled responseType: ${responseType}`);
  10343. //>>includeEnd('debug');
  10344. }
  10345. }
  10346. /**
  10347. * Asynchronously loads the given resource. Returns a promise that will resolve to
  10348. * the result once loaded, or reject if the resource failed to load. The data is loaded
  10349. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10350. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled. It's recommended that you use
  10351. * the more specific functions eg. fetchJson, fetchBlob, etc.
  10352. *
  10353. * @param {object} [options] Object with the following properties:
  10354. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10355. * @param {object} [options.headers] Additional HTTP headers to send with the request, if any.
  10356. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10357. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10358. *
  10359. *
  10360. * @example
  10361. * resource.fetch()
  10362. * .then(function(body) {
  10363. * // use the data
  10364. * }).catch(function(error) {
  10365. * // an error occurred
  10366. * });
  10367. *
  10368. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10369. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10370. */
  10371. Resource.prototype.fetch = function (options) {
  10372. options = defaultClone(options, {});
  10373. options.method = "GET";
  10374. return this._makeRequest(options);
  10375. };
  10376. /**
  10377. * Creates a Resource from a URL and calls fetch() on it.
  10378. *
  10379. * @param {string|object} options A url or an object with the following properties
  10380. * @param {string} options.url The url of the resource.
  10381. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10382. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10383. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10384. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10385. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10386. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10387. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10388. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10389. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10390. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10391. */
  10392. Resource.fetch = function (options) {
  10393. const resource = new Resource(options);
  10394. return resource.fetch({
  10395. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  10396. responseType: options.responseType,
  10397. overrideMimeType: options.overrideMimeType,
  10398. });
  10399. };
  10400. /**
  10401. * Asynchronously deletes the given resource. Returns a promise that will resolve to
  10402. * the result once loaded, or reject if the resource failed to load. The data is loaded
  10403. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10404. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  10405. *
  10406. * @param {object} [options] Object with the following properties:
  10407. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10408. * @param {object} [options.headers] Additional HTTP headers to send with the request, if any.
  10409. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10410. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10411. *
  10412. *
  10413. * @example
  10414. * resource.delete()
  10415. * .then(function(body) {
  10416. * // use the data
  10417. * }).catch(function(error) {
  10418. * // an error occurred
  10419. * });
  10420. *
  10421. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10422. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10423. */
  10424. Resource.prototype.delete = function (options) {
  10425. options = defaultClone(options, {});
  10426. options.method = "DELETE";
  10427. return this._makeRequest(options);
  10428. };
  10429. /**
  10430. * Creates a Resource from a URL and calls delete() on it.
  10431. *
  10432. * @param {string|object} options A url or an object with the following properties
  10433. * @param {string} options.url The url of the resource.
  10434. * @param {object} [options.data] Data that is posted with the resource.
  10435. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10436. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10437. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10438. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10439. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10440. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10441. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10442. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10443. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10444. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10445. */
  10446. Resource.delete = function (options) {
  10447. const resource = new Resource(options);
  10448. return resource.delete({
  10449. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  10450. responseType: options.responseType,
  10451. overrideMimeType: options.overrideMimeType,
  10452. data: options.data,
  10453. });
  10454. };
  10455. /**
  10456. * Asynchronously gets headers the given resource. Returns a promise that will resolve to
  10457. * the result once loaded, or reject if the resource failed to load. The data is loaded
  10458. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10459. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  10460. *
  10461. * @param {object} [options] Object with the following properties:
  10462. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10463. * @param {object} [options.headers] Additional HTTP headers to send with the request, if any.
  10464. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10465. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10466. *
  10467. *
  10468. * @example
  10469. * resource.head()
  10470. * .then(function(headers) {
  10471. * // use the data
  10472. * }).catch(function(error) {
  10473. * // an error occurred
  10474. * });
  10475. *
  10476. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10477. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10478. */
  10479. Resource.prototype.head = function (options) {
  10480. options = defaultClone(options, {});
  10481. options.method = "HEAD";
  10482. return this._makeRequest(options);
  10483. };
  10484. /**
  10485. * Creates a Resource from a URL and calls head() on it.
  10486. *
  10487. * @param {string|object} options A url or an object with the following properties
  10488. * @param {string} options.url The url of the resource.
  10489. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10490. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10491. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10492. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10493. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10494. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10495. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10496. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10497. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10498. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10499. */
  10500. Resource.head = function (options) {
  10501. const resource = new Resource(options);
  10502. return resource.head({
  10503. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  10504. responseType: options.responseType,
  10505. overrideMimeType: options.overrideMimeType,
  10506. });
  10507. };
  10508. /**
  10509. * Asynchronously gets options the given resource. Returns a promise that will resolve to
  10510. * the result once loaded, or reject if the resource failed to load. The data is loaded
  10511. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10512. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  10513. *
  10514. * @param {object} [options] Object with the following properties:
  10515. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10516. * @param {object} [options.headers] Additional HTTP headers to send with the request, if any.
  10517. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10518. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10519. *
  10520. *
  10521. * @example
  10522. * resource.options()
  10523. * .then(function(headers) {
  10524. * // use the data
  10525. * }).catch(function(error) {
  10526. * // an error occurred
  10527. * });
  10528. *
  10529. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10530. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10531. */
  10532. Resource.prototype.options = function (options) {
  10533. options = defaultClone(options, {});
  10534. options.method = "OPTIONS";
  10535. return this._makeRequest(options);
  10536. };
  10537. /**
  10538. * Creates a Resource from a URL and calls options() on it.
  10539. *
  10540. * @param {string|object} options A url or an object with the following properties
  10541. * @param {string} options.url The url of the resource.
  10542. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10543. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10544. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10545. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10546. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10547. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10548. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10549. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10550. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10551. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10552. */
  10553. Resource.options = function (options) {
  10554. const resource = new Resource(options);
  10555. return resource.options({
  10556. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  10557. responseType: options.responseType,
  10558. overrideMimeType: options.overrideMimeType,
  10559. });
  10560. };
  10561. /**
  10562. * Asynchronously posts data to the given resource. Returns a promise that will resolve to
  10563. * the result once loaded, or reject if the resource failed to load. The data is loaded
  10564. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10565. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  10566. *
  10567. * @param {object} data Data that is posted with the resource.
  10568. * @param {object} [options] Object with the following properties:
  10569. * @param {object} [options.data] Data that is posted with the resource.
  10570. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10571. * @param {object} [options.headers] Additional HTTP headers to send with the request, if any.
  10572. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10573. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10574. *
  10575. *
  10576. * @example
  10577. * resource.post(data)
  10578. * .then(function(result) {
  10579. * // use the result
  10580. * }).catch(function(error) {
  10581. * // an error occurred
  10582. * });
  10583. *
  10584. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10585. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10586. */
  10587. Resource.prototype.post = function (data, options) {
  10588. Check.Check.defined("data", data);
  10589. options = defaultClone(options, {});
  10590. options.method = "POST";
  10591. options.data = data;
  10592. return this._makeRequest(options);
  10593. };
  10594. /**
  10595. * Creates a Resource from a URL and calls post() on it.
  10596. *
  10597. * @param {object} options A url or an object with the following properties
  10598. * @param {string} options.url The url of the resource.
  10599. * @param {object} options.data Data that is posted with the resource.
  10600. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10601. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10602. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10603. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10604. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10605. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10606. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10607. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10608. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10609. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10610. */
  10611. Resource.post = function (options) {
  10612. const resource = new Resource(options);
  10613. return resource.post(options.data, {
  10614. // Make copy of just the needed fields because headers can be passed to both the constructor and to post
  10615. responseType: options.responseType,
  10616. overrideMimeType: options.overrideMimeType,
  10617. });
  10618. };
  10619. /**
  10620. * Asynchronously puts data to the given resource. Returns a promise that will resolve to
  10621. * the result once loaded, or reject if the resource failed to load. The data is loaded
  10622. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10623. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  10624. *
  10625. * @param {object} data Data that is posted with the resource.
  10626. * @param {object} [options] Object with the following properties:
  10627. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10628. * @param {object} [options.headers] Additional HTTP headers to send with the request, if any.
  10629. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10630. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10631. *
  10632. *
  10633. * @example
  10634. * resource.put(data)
  10635. * .then(function(result) {
  10636. * // use the result
  10637. * }).catch(function(error) {
  10638. * // an error occurred
  10639. * });
  10640. *
  10641. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10642. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10643. */
  10644. Resource.prototype.put = function (data, options) {
  10645. Check.Check.defined("data", data);
  10646. options = defaultClone(options, {});
  10647. options.method = "PUT";
  10648. options.data = data;
  10649. return this._makeRequest(options);
  10650. };
  10651. /**
  10652. * Creates a Resource from a URL and calls put() on it.
  10653. *
  10654. * @param {object} options A url or an object with the following properties
  10655. * @param {string} options.url The url of the resource.
  10656. * @param {object} options.data Data that is posted with the resource.
  10657. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10658. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10659. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10660. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10661. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10662. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10663. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10664. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10665. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10666. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10667. */
  10668. Resource.put = function (options) {
  10669. const resource = new Resource(options);
  10670. return resource.put(options.data, {
  10671. // Make copy of just the needed fields because headers can be passed to both the constructor and to post
  10672. responseType: options.responseType,
  10673. overrideMimeType: options.overrideMimeType,
  10674. });
  10675. };
  10676. /**
  10677. * Asynchronously patches data to the given resource. Returns a promise that will resolve to
  10678. * the result once loaded, or reject if the resource failed to load. The data is loaded
  10679. * using XMLHttpRequest, which means that in order to make requests to another origin,
  10680. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  10681. *
  10682. * @param {object} data Data that is posted with the resource.
  10683. * @param {object} [options] Object with the following properties:
  10684. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10685. * @param {object} [options.headers] Additional HTTP headers to send with the request, if any.
  10686. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10687. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10688. *
  10689. *
  10690. * @example
  10691. * resource.patch(data)
  10692. * .then(function(result) {
  10693. * // use the result
  10694. * }).catch(function(error) {
  10695. * // an error occurred
  10696. * });
  10697. *
  10698. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  10699. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  10700. */
  10701. Resource.prototype.patch = function (data, options) {
  10702. Check.Check.defined("data", data);
  10703. options = defaultClone(options, {});
  10704. options.method = "PATCH";
  10705. options.data = data;
  10706. return this._makeRequest(options);
  10707. };
  10708. /**
  10709. * Creates a Resource from a URL and calls patch() on it.
  10710. *
  10711. * @param {object} options A url or an object with the following properties
  10712. * @param {string} options.url The url of the resource.
  10713. * @param {object} options.data Data that is posted with the resource.
  10714. * @param {object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  10715. * @param {object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  10716. * @param {object} [options.headers={}] Additional HTTP headers that will be sent.
  10717. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  10718. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  10719. * @param {number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  10720. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  10721. * @param {string} [options.responseType] The type of response. This controls the type of item returned.
  10722. * @param {string} [options.overrideMimeType] Overrides the MIME type returned by the server.
  10723. * @returns {Promise<any>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  10724. */
  10725. Resource.patch = function (options) {
  10726. const resource = new Resource(options);
  10727. return resource.patch(options.data, {
  10728. // Make copy of just the needed fields because headers can be passed to both the constructor and to post
  10729. responseType: options.responseType,
  10730. overrideMimeType: options.overrideMimeType,
  10731. });
  10732. };
  10733. /**
  10734. * Contains implementations of functions that can be replaced for testing
  10735. *
  10736. * @private
  10737. */
  10738. Resource._Implementations = {};
  10739. Resource._Implementations.loadImageElement = function (
  10740. url,
  10741. crossOrigin,
  10742. deferred
  10743. ) {
  10744. const image = new Image();
  10745. image.onload = function () {
  10746. // work-around a known issue with Firefox and dimensionless SVG, see:
  10747. // - https://github.com/whatwg/html/issues/3510
  10748. // - https://bugzilla.mozilla.org/show_bug.cgi?id=700533
  10749. if (
  10750. image.naturalWidth === 0 &&
  10751. image.naturalHeight === 0 &&
  10752. image.width === 0 &&
  10753. image.height === 0
  10754. ) {
  10755. // these values affect rasterization and will likely mar the content
  10756. // until Firefox takes a stance on the issue, marred content is better than no content
  10757. // Chromium uses a more refined heuristic about its choice given nil viewBox, and a better stance and solution is
  10758. // proposed later in the original issue thread:
  10759. // - Chromium behavior: https://github.com/CesiumGS/cesium/issues/9188#issuecomment-704400825
  10760. // - Cesium's stance/solve: https://github.com/CesiumGS/cesium/issues/9188#issuecomment-720645777
  10761. image.width = 300;
  10762. image.height = 150;
  10763. }
  10764. deferred.resolve(image);
  10765. };
  10766. image.onerror = function (e) {
  10767. deferred.reject(e);
  10768. };
  10769. if (crossOrigin) {
  10770. if (TrustedServers$1.contains(url)) {
  10771. image.crossOrigin = "use-credentials";
  10772. } else {
  10773. image.crossOrigin = "";
  10774. }
  10775. }
  10776. image.src = url;
  10777. };
  10778. Resource._Implementations.createImage = function (
  10779. request,
  10780. crossOrigin,
  10781. deferred,
  10782. flipY,
  10783. skipColorSpaceConversion,
  10784. preferImageBitmap
  10785. ) {
  10786. const url = request.url;
  10787. // Passing an Image to createImageBitmap will force it to run on the main thread
  10788. // since DOM elements don't exist on workers. We convert it to a blob so it's non-blocking.
  10789. // See:
  10790. // https://bugzilla.mozilla.org/show_bug.cgi?id=1044102#c38
  10791. // https://bugs.chromium.org/p/chromium/issues/detail?id=580202#c10
  10792. Resource.supportsImageBitmapOptions()
  10793. .then(function (supportsImageBitmap) {
  10794. // We can only use ImageBitmap if we can flip on decode.
  10795. // See: https://github.com/CesiumGS/cesium/pull/7579#issuecomment-466146898
  10796. if (!(supportsImageBitmap && preferImageBitmap)) {
  10797. Resource._Implementations.loadImageElement(url, crossOrigin, deferred);
  10798. return;
  10799. }
  10800. const responseType = "blob";
  10801. const method = "GET";
  10802. const xhrDeferred = defer();
  10803. const xhr = Resource._Implementations.loadWithXhr(
  10804. url,
  10805. responseType,
  10806. method,
  10807. undefined,
  10808. undefined,
  10809. xhrDeferred,
  10810. undefined,
  10811. undefined,
  10812. undefined
  10813. );
  10814. if (defaultValue.defined(xhr) && defaultValue.defined(xhr.abort)) {
  10815. request.cancelFunction = function () {
  10816. xhr.abort();
  10817. };
  10818. }
  10819. return xhrDeferred.promise
  10820. .then(function (blob) {
  10821. if (!defaultValue.defined(blob)) {
  10822. deferred.reject(
  10823. new RuntimeError.RuntimeError(
  10824. `Successfully retrieved ${url} but it contained no content.`
  10825. )
  10826. );
  10827. return;
  10828. }
  10829. return Resource.createImageBitmapFromBlob(blob, {
  10830. flipY: flipY,
  10831. premultiplyAlpha: false,
  10832. skipColorSpaceConversion: skipColorSpaceConversion,
  10833. });
  10834. })
  10835. .then(function (image) {
  10836. deferred.resolve(image);
  10837. });
  10838. })
  10839. .catch(function (e) {
  10840. deferred.reject(e);
  10841. });
  10842. };
  10843. /**
  10844. * Wrapper for createImageBitmap
  10845. *
  10846. * @private
  10847. */
  10848. Resource.createImageBitmapFromBlob = function (blob, options) {
  10849. Check.Check.defined("options", options);
  10850. Check.Check.typeOf.bool("options.flipY", options.flipY);
  10851. Check.Check.typeOf.bool("options.premultiplyAlpha", options.premultiplyAlpha);
  10852. Check.Check.typeOf.bool(
  10853. "options.skipColorSpaceConversion",
  10854. options.skipColorSpaceConversion
  10855. );
  10856. return createImageBitmap(blob, {
  10857. imageOrientation: options.flipY ? "flipY" : "none",
  10858. premultiplyAlpha: options.premultiplyAlpha ? "premultiply" : "none",
  10859. colorSpaceConversion: options.skipColorSpaceConversion ? "none" : "default",
  10860. });
  10861. };
  10862. function decodeResponse(loadWithHttpResponse, responseType) {
  10863. switch (responseType) {
  10864. case "text":
  10865. return loadWithHttpResponse.toString("utf8");
  10866. case "json":
  10867. return JSON.parse(loadWithHttpResponse.toString("utf8"));
  10868. default:
  10869. return new Uint8Array(loadWithHttpResponse).buffer;
  10870. }
  10871. }
  10872. function loadWithHttpRequest(
  10873. url,
  10874. responseType,
  10875. method,
  10876. data,
  10877. headers,
  10878. deferred,
  10879. overrideMimeType
  10880. ) {
  10881. // Note: only the 'json' and 'text' responseTypes transforms the loaded buffer
  10882. let URL;
  10883. let zlib;
  10884. Promise.all([new Promise(function (resolve, reject) { require(['url'], function (m) { resolve(/*#__PURE__*/_interopNamespaceDefault(m)); }, reject); }), new Promise(function (resolve, reject) { require(['zlib'], function (m) { resolve(/*#__PURE__*/_interopNamespaceDefault(m)); }, reject); })])
  10885. .then(([urlImport, zlibImport]) => {
  10886. URL = urlImport.parse(url);
  10887. zlib = zlibImport;
  10888. return URL.protocol === "https:" ? new Promise(function (resolve, reject) { require(['https'], function (m) { resolve(/*#__PURE__*/_interopNamespaceDefault(m)); }, reject); }) : new Promise(function (resolve, reject) { require(['http'], function (m) { resolve(/*#__PURE__*/_interopNamespaceDefault(m)); }, reject); });
  10889. })
  10890. .then((http) => {
  10891. const options = {
  10892. protocol: URL.protocol,
  10893. hostname: URL.hostname,
  10894. port: URL.port,
  10895. path: URL.path,
  10896. query: URL.query,
  10897. method: method,
  10898. headers: headers,
  10899. };
  10900. http
  10901. .request(options)
  10902. .on("response", function (res) {
  10903. if (res.statusCode < 200 || res.statusCode >= 300) {
  10904. deferred.reject(
  10905. new RequestErrorEvent(res.statusCode, res, res.headers)
  10906. );
  10907. return;
  10908. }
  10909. const chunkArray = [];
  10910. res.on("data", function (chunk) {
  10911. chunkArray.push(chunk);
  10912. });
  10913. res.on("end", function () {
  10914. // eslint-disable-next-line no-undef
  10915. const result = Buffer.concat(chunkArray);
  10916. if (res.headers["content-encoding"] === "gzip") {
  10917. zlib.gunzip(result, function (error, resultUnzipped) {
  10918. if (error) {
  10919. deferred.reject(
  10920. new RuntimeError.RuntimeError("Error decompressing response.")
  10921. );
  10922. } else {
  10923. deferred.resolve(
  10924. decodeResponse(resultUnzipped, responseType)
  10925. );
  10926. }
  10927. });
  10928. } else {
  10929. deferred.resolve(decodeResponse(result, responseType));
  10930. }
  10931. });
  10932. })
  10933. .on("error", function (e) {
  10934. deferred.reject(new RequestErrorEvent());
  10935. })
  10936. .end();
  10937. });
  10938. }
  10939. const noXMLHttpRequest = typeof XMLHttpRequest === "undefined";
  10940. Resource._Implementations.loadWithXhr = function (
  10941. url,
  10942. responseType,
  10943. method,
  10944. data,
  10945. headers,
  10946. deferred,
  10947. overrideMimeType
  10948. ) {
  10949. const dataUriRegexResult = dataUriRegex.exec(url);
  10950. if (dataUriRegexResult !== null) {
  10951. deferred.resolve(decodeDataUri(dataUriRegexResult, responseType));
  10952. return;
  10953. }
  10954. if (noXMLHttpRequest) {
  10955. loadWithHttpRequest(
  10956. url,
  10957. responseType,
  10958. method,
  10959. data,
  10960. headers,
  10961. deferred);
  10962. return;
  10963. }
  10964. const xhr = new XMLHttpRequest();
  10965. if (TrustedServers$1.contains(url)) {
  10966. xhr.withCredentials = true;
  10967. }
  10968. xhr.open(method, url, true);
  10969. if (defaultValue.defined(overrideMimeType) && defaultValue.defined(xhr.overrideMimeType)) {
  10970. xhr.overrideMimeType(overrideMimeType);
  10971. }
  10972. if (defaultValue.defined(headers)) {
  10973. for (const key in headers) {
  10974. if (headers.hasOwnProperty(key)) {
  10975. xhr.setRequestHeader(key, headers[key]);
  10976. }
  10977. }
  10978. }
  10979. if (defaultValue.defined(responseType)) {
  10980. xhr.responseType = responseType;
  10981. }
  10982. // While non-standard, file protocol always returns a status of 0 on success
  10983. let localFile = false;
  10984. if (typeof url === "string") {
  10985. localFile =
  10986. url.indexOf("file://") === 0 ||
  10987. (typeof window !== "undefined" && window.location.origin === "file://");
  10988. }
  10989. xhr.onload = function () {
  10990. if (
  10991. (xhr.status < 200 || xhr.status >= 300) &&
  10992. !(localFile && xhr.status === 0)
  10993. ) {
  10994. deferred.reject(
  10995. new RequestErrorEvent(
  10996. xhr.status,
  10997. xhr.response,
  10998. xhr.getAllResponseHeaders()
  10999. )
  11000. );
  11001. return;
  11002. }
  11003. const response = xhr.response;
  11004. const browserResponseType = xhr.responseType;
  11005. if (method === "HEAD" || method === "OPTIONS") {
  11006. const responseHeaderString = xhr.getAllResponseHeaders();
  11007. const splitHeaders = responseHeaderString.trim().split(/[\r\n]+/);
  11008. const responseHeaders = {};
  11009. splitHeaders.forEach(function (line) {
  11010. const parts = line.split(": ");
  11011. const header = parts.shift();
  11012. responseHeaders[header] = parts.join(": ");
  11013. });
  11014. deferred.resolve(responseHeaders);
  11015. return;
  11016. }
  11017. //All modern browsers will go into either the first or second if block or last else block.
  11018. //Other code paths support older browsers that either do not support the supplied responseType
  11019. //or do not support the xhr.response property.
  11020. if (xhr.status === 204) {
  11021. // accept no content
  11022. deferred.resolve();
  11023. } else if (
  11024. defaultValue.defined(response) &&
  11025. (!defaultValue.defined(responseType) || browserResponseType === responseType)
  11026. ) {
  11027. deferred.resolve(response);
  11028. } else if (responseType === "json" && typeof response === "string") {
  11029. try {
  11030. deferred.resolve(JSON.parse(response));
  11031. } catch (e) {
  11032. deferred.reject(e);
  11033. }
  11034. } else if (
  11035. (browserResponseType === "" || browserResponseType === "document") &&
  11036. defaultValue.defined(xhr.responseXML) &&
  11037. xhr.responseXML.hasChildNodes()
  11038. ) {
  11039. deferred.resolve(xhr.responseXML);
  11040. } else if (
  11041. (browserResponseType === "" || browserResponseType === "text") &&
  11042. defaultValue.defined(xhr.responseText)
  11043. ) {
  11044. deferred.resolve(xhr.responseText);
  11045. } else {
  11046. deferred.reject(
  11047. new RuntimeError.RuntimeError("Invalid XMLHttpRequest response type.")
  11048. );
  11049. }
  11050. };
  11051. xhr.onerror = function (e) {
  11052. deferred.reject(new RequestErrorEvent());
  11053. };
  11054. xhr.send(data);
  11055. return xhr;
  11056. };
  11057. Resource._Implementations.loadAndExecuteScript = function (
  11058. url,
  11059. functionName,
  11060. deferred
  11061. ) {
  11062. return loadAndExecuteScript(url).catch(function (e) {
  11063. deferred.reject(e);
  11064. });
  11065. };
  11066. /**
  11067. * The default implementations
  11068. *
  11069. * @private
  11070. */
  11071. Resource._DefaultImplementations = {};
  11072. Resource._DefaultImplementations.createImage =
  11073. Resource._Implementations.createImage;
  11074. Resource._DefaultImplementations.loadWithXhr =
  11075. Resource._Implementations.loadWithXhr;
  11076. Resource._DefaultImplementations.loadAndExecuteScript =
  11077. Resource._Implementations.loadAndExecuteScript;
  11078. /**
  11079. * A resource instance initialized to the current browser location
  11080. *
  11081. * @type {Resource}
  11082. * @constant
  11083. */
  11084. Resource.DEFAULT = Object.freeze(
  11085. new Resource({
  11086. url:
  11087. typeof document === "undefined"
  11088. ? ""
  11089. : document.location.href.split("?")[0],
  11090. })
  11091. );
  11092. /**
  11093. * Specifies Earth polar motion coordinates and the difference between UT1 and UTC.
  11094. * These Earth Orientation Parameters (EOP) are primarily used in the transformation from
  11095. * the International Celestial Reference Frame (ICRF) to the International Terrestrial
  11096. * Reference Frame (ITRF).
  11097. * This object is normally not instantiated directly, use {@link EarthOrientationParameters.fromUrl}.
  11098. *
  11099. * @alias EarthOrientationParameters
  11100. * @constructor
  11101. *
  11102. * @param {object} [options] Object with the following properties:
  11103. * @param {object} [options.data] The actual EOP data. If neither this
  11104. * parameter nor options.data is specified, all EOP values are assumed
  11105. * to be 0.0.
  11106. * @param {boolean} [options.addNewLeapSeconds=true] True if leap seconds that
  11107. * are specified in the EOP data but not in {@link JulianDate.leapSeconds}
  11108. * should be added to {@link JulianDate.leapSeconds}. False if
  11109. * new leap seconds should be handled correctly in the context
  11110. * of the EOP data but otherwise ignored.
  11111. *
  11112. * @private
  11113. */
  11114. function EarthOrientationParameters(options) {
  11115. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  11116. this._dates = undefined;
  11117. this._samples = undefined;
  11118. this._dateColumn = -1;
  11119. this._xPoleWanderRadiansColumn = -1;
  11120. this._yPoleWanderRadiansColumn = -1;
  11121. this._ut1MinusUtcSecondsColumn = -1;
  11122. this._xCelestialPoleOffsetRadiansColumn = -1;
  11123. this._yCelestialPoleOffsetRadiansColumn = -1;
  11124. this._taiMinusUtcSecondsColumn = -1;
  11125. this._columnCount = 0;
  11126. this._lastIndex = -1;
  11127. this._addNewLeapSeconds = defaultValue.defaultValue(options.addNewLeapSeconds, true);
  11128. if (defaultValue.defined(options.data)) {
  11129. // Use supplied EOP data.
  11130. onDataReady(this, options.data);
  11131. } else {
  11132. // Use all zeros for EOP data.
  11133. onDataReady(this, {
  11134. columnNames: [
  11135. "dateIso8601",
  11136. "modifiedJulianDateUtc",
  11137. "xPoleWanderRadians",
  11138. "yPoleWanderRadians",
  11139. "ut1MinusUtcSeconds",
  11140. "lengthOfDayCorrectionSeconds",
  11141. "xCelestialPoleOffsetRadians",
  11142. "yCelestialPoleOffsetRadians",
  11143. "taiMinusUtcSeconds",
  11144. ],
  11145. samples: [],
  11146. });
  11147. }
  11148. }
  11149. /**
  11150. *
  11151. * @param {Resource|string} [url] The URL from which to obtain EOP data. If neither this
  11152. * parameter nor options.data is specified, all EOP values are assumed
  11153. * to be 0.0. If options.data is specified, this parameter is
  11154. * ignored.
  11155. * @param {object} [options] Object with the following properties:
  11156. * @param {boolean} [options.addNewLeapSeconds=true] True if leap seconds that
  11157. * are specified in the EOP data but not in {@link JulianDate.leapSeconds}
  11158. * should be added to {@link JulianDate.leapSeconds}. False if
  11159. * new leap seconds should be handled correctly in the context
  11160. * of the EOP data but otherwise ignored.
  11161. *
  11162. * @example
  11163. * // An example EOP data file, EOP.json:
  11164. * {
  11165. * "columnNames" : ["dateIso8601","modifiedJulianDateUtc","xPoleWanderRadians","yPoleWanderRadians","ut1MinusUtcSeconds","lengthOfDayCorrectionSeconds","xCelestialPoleOffsetRadians","yCelestialPoleOffsetRadians","taiMinusUtcSeconds"],
  11166. * "samples" : [
  11167. * "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,
  11168. * "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,
  11169. * "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
  11170. * ]
  11171. * }
  11172. *
  11173. * @example
  11174. * // Loading the EOP data
  11175. * const eop = await Cesium.EarthOrientationParameters.fromUrl('Data/EOP.json');
  11176. * Cesium.Transforms.earthOrientationParameters = eop;
  11177. */
  11178. EarthOrientationParameters.fromUrl = async function (url, options) {
  11179. //>>includeStart('debug', pragmas.debug);
  11180. Check.Check.defined("url", url);
  11181. //>>includeEnd('debug');
  11182. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  11183. const resource = Resource.createIfNeeded(url);
  11184. // Download EOP data.
  11185. let eopData;
  11186. try {
  11187. eopData = await resource.fetchJson();
  11188. } catch (e) {
  11189. throw new RuntimeError.RuntimeError(
  11190. `An error occurred while retrieving the EOP data from the URL ${resource.url}.`
  11191. );
  11192. }
  11193. return new EarthOrientationParameters({
  11194. addNewLeapSeconds: options.addNewLeapSeconds,
  11195. data: eopData,
  11196. });
  11197. };
  11198. /**
  11199. * A default {@link EarthOrientationParameters} instance that returns zero for all EOP values.
  11200. */
  11201. EarthOrientationParameters.NONE = Object.freeze({
  11202. compute: function (date, result) {
  11203. if (!defaultValue.defined(result)) {
  11204. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  11205. } else {
  11206. result.xPoleWander = 0.0;
  11207. result.yPoleWander = 0.0;
  11208. result.xPoleOffset = 0.0;
  11209. result.yPoleOffset = 0.0;
  11210. result.ut1MinusUtc = 0.0;
  11211. }
  11212. return result;
  11213. },
  11214. });
  11215. /**
  11216. * Computes the Earth Orientation Parameters (EOP) for a given date by interpolating.
  11217. * If the EOP data has not yet been download, this method returns undefined.
  11218. *
  11219. * @param {JulianDate} date The date for each to evaluate the EOP.
  11220. * @param {EarthOrientationParametersSample} [result] The instance to which to copy the result.
  11221. * If this parameter is undefined, a new instance is created and returned.
  11222. * @returns {EarthOrientationParametersSample} The EOP evaluated at the given date, or
  11223. * undefined if the data necessary to evaluate EOP at the date has not yet been
  11224. * downloaded.
  11225. *
  11226. * @exception {RuntimeError} The loaded EOP data has an error and cannot be used.
  11227. *
  11228. * @see EarthOrientationParameters#fromUrl
  11229. */
  11230. EarthOrientationParameters.prototype.compute = function (date, result) {
  11231. // We cannot compute until the samples are available.
  11232. if (!defaultValue.defined(this._samples)) {
  11233. return undefined;
  11234. }
  11235. if (!defaultValue.defined(result)) {
  11236. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  11237. }
  11238. if (this._samples.length === 0) {
  11239. result.xPoleWander = 0.0;
  11240. result.yPoleWander = 0.0;
  11241. result.xPoleOffset = 0.0;
  11242. result.yPoleOffset = 0.0;
  11243. result.ut1MinusUtc = 0.0;
  11244. return result;
  11245. }
  11246. const dates = this._dates;
  11247. const lastIndex = this._lastIndex;
  11248. let before = 0;
  11249. let after = 0;
  11250. if (defaultValue.defined(lastIndex)) {
  11251. const previousIndexDate = dates[lastIndex];
  11252. const nextIndexDate = dates[lastIndex + 1];
  11253. const isAfterPrevious = JulianDate.lessThanOrEquals(
  11254. previousIndexDate,
  11255. date
  11256. );
  11257. const isAfterLastSample = !defaultValue.defined(nextIndexDate);
  11258. const isBeforeNext =
  11259. isAfterLastSample || JulianDate.greaterThanOrEquals(nextIndexDate, date);
  11260. if (isAfterPrevious && isBeforeNext) {
  11261. before = lastIndex;
  11262. if (!isAfterLastSample && nextIndexDate.equals(date)) {
  11263. ++before;
  11264. }
  11265. after = before + 1;
  11266. interpolate(this, dates, this._samples, date, before, after, result);
  11267. return result;
  11268. }
  11269. }
  11270. let index = binarySearch(dates, date, JulianDate.compare, this._dateColumn);
  11271. if (index >= 0) {
  11272. // If the next entry is the same date, use the later entry. This way, if two entries
  11273. // describe the same moment, one before a leap second and the other after, then we will use
  11274. // the post-leap second data.
  11275. if (index < dates.length - 1 && dates[index + 1].equals(date)) {
  11276. ++index;
  11277. }
  11278. before = index;
  11279. after = index;
  11280. } else {
  11281. after = ~index;
  11282. before = after - 1;
  11283. // Use the first entry if the date requested is before the beginning of the data.
  11284. if (before < 0) {
  11285. before = 0;
  11286. }
  11287. }
  11288. this._lastIndex = before;
  11289. interpolate(this, dates, this._samples, date, before, after, result);
  11290. return result;
  11291. };
  11292. function compareLeapSecondDates(leapSecond, dateToFind) {
  11293. return JulianDate.compare(leapSecond.julianDate, dateToFind);
  11294. }
  11295. function onDataReady(eop, eopData) {
  11296. if (!defaultValue.defined(eopData.columnNames)) {
  11297. throw new RuntimeError.RuntimeError(
  11298. "Error in loaded EOP data: The columnNames property is required."
  11299. );
  11300. }
  11301. if (!defaultValue.defined(eopData.samples)) {
  11302. throw new RuntimeError.RuntimeError(
  11303. "Error in loaded EOP data: The samples property is required."
  11304. );
  11305. }
  11306. const dateColumn = eopData.columnNames.indexOf("modifiedJulianDateUtc");
  11307. const xPoleWanderRadiansColumn = eopData.columnNames.indexOf(
  11308. "xPoleWanderRadians"
  11309. );
  11310. const yPoleWanderRadiansColumn = eopData.columnNames.indexOf(
  11311. "yPoleWanderRadians"
  11312. );
  11313. const ut1MinusUtcSecondsColumn = eopData.columnNames.indexOf(
  11314. "ut1MinusUtcSeconds"
  11315. );
  11316. const xCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf(
  11317. "xCelestialPoleOffsetRadians"
  11318. );
  11319. const yCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf(
  11320. "yCelestialPoleOffsetRadians"
  11321. );
  11322. const taiMinusUtcSecondsColumn = eopData.columnNames.indexOf(
  11323. "taiMinusUtcSeconds"
  11324. );
  11325. if (
  11326. dateColumn < 0 ||
  11327. xPoleWanderRadiansColumn < 0 ||
  11328. yPoleWanderRadiansColumn < 0 ||
  11329. ut1MinusUtcSecondsColumn < 0 ||
  11330. xCelestialPoleOffsetRadiansColumn < 0 ||
  11331. yCelestialPoleOffsetRadiansColumn < 0 ||
  11332. taiMinusUtcSecondsColumn < 0
  11333. ) {
  11334. throw new RuntimeError.RuntimeError(
  11335. "Error in loaded EOP data: The columnNames property must include modifiedJulianDateUtc, xPoleWanderRadians, yPoleWanderRadians, ut1MinusUtcSeconds, xCelestialPoleOffsetRadians, yCelestialPoleOffsetRadians, and taiMinusUtcSeconds columns"
  11336. );
  11337. }
  11338. const samples = (eop._samples = eopData.samples);
  11339. const dates = (eop._dates = []);
  11340. eop._dateColumn = dateColumn;
  11341. eop._xPoleWanderRadiansColumn = xPoleWanderRadiansColumn;
  11342. eop._yPoleWanderRadiansColumn = yPoleWanderRadiansColumn;
  11343. eop._ut1MinusUtcSecondsColumn = ut1MinusUtcSecondsColumn;
  11344. eop._xCelestialPoleOffsetRadiansColumn = xCelestialPoleOffsetRadiansColumn;
  11345. eop._yCelestialPoleOffsetRadiansColumn = yCelestialPoleOffsetRadiansColumn;
  11346. eop._taiMinusUtcSecondsColumn = taiMinusUtcSecondsColumn;
  11347. eop._columnCount = eopData.columnNames.length;
  11348. eop._lastIndex = undefined;
  11349. let lastTaiMinusUtc;
  11350. const addNewLeapSeconds = eop._addNewLeapSeconds;
  11351. // Convert the ISO8601 dates to JulianDates.
  11352. for (let i = 0, len = samples.length; i < len; i += eop._columnCount) {
  11353. const mjd = samples[i + dateColumn];
  11354. const taiMinusUtc = samples[i + taiMinusUtcSecondsColumn];
  11355. const day = mjd + TimeConstants$1.MODIFIED_JULIAN_DATE_DIFFERENCE;
  11356. const date = new JulianDate(day, taiMinusUtc, TimeStandard$1.TAI);
  11357. dates.push(date);
  11358. if (addNewLeapSeconds) {
  11359. if (taiMinusUtc !== lastTaiMinusUtc && defaultValue.defined(lastTaiMinusUtc)) {
  11360. // We crossed a leap second boundary, so add the leap second
  11361. // if it does not already exist.
  11362. const leapSeconds = JulianDate.leapSeconds;
  11363. const leapSecondIndex = binarySearch(
  11364. leapSeconds,
  11365. date,
  11366. compareLeapSecondDates
  11367. );
  11368. if (leapSecondIndex < 0) {
  11369. const leapSecond = new LeapSecond(date, taiMinusUtc);
  11370. leapSeconds.splice(~leapSecondIndex, 0, leapSecond);
  11371. }
  11372. }
  11373. lastTaiMinusUtc = taiMinusUtc;
  11374. }
  11375. }
  11376. }
  11377. function fillResultFromIndex(eop, samples, index, columnCount, result) {
  11378. const start = index * columnCount;
  11379. result.xPoleWander = samples[start + eop._xPoleWanderRadiansColumn];
  11380. result.yPoleWander = samples[start + eop._yPoleWanderRadiansColumn];
  11381. result.xPoleOffset = samples[start + eop._xCelestialPoleOffsetRadiansColumn];
  11382. result.yPoleOffset = samples[start + eop._yCelestialPoleOffsetRadiansColumn];
  11383. result.ut1MinusUtc = samples[start + eop._ut1MinusUtcSecondsColumn];
  11384. }
  11385. function linearInterp(dx, y1, y2) {
  11386. return y1 + dx * (y2 - y1);
  11387. }
  11388. function interpolate(eop, dates, samples, date, before, after, result) {
  11389. const columnCount = eop._columnCount;
  11390. // First check the bounds on the EOP data
  11391. // If we are after the bounds of the data, return zeros.
  11392. // The 'before' index should never be less than zero.
  11393. if (after > dates.length - 1) {
  11394. result.xPoleWander = 0;
  11395. result.yPoleWander = 0;
  11396. result.xPoleOffset = 0;
  11397. result.yPoleOffset = 0;
  11398. result.ut1MinusUtc = 0;
  11399. return result;
  11400. }
  11401. const beforeDate = dates[before];
  11402. const afterDate = dates[after];
  11403. if (beforeDate.equals(afterDate) || date.equals(beforeDate)) {
  11404. fillResultFromIndex(eop, samples, before, columnCount, result);
  11405. return result;
  11406. } else if (date.equals(afterDate)) {
  11407. fillResultFromIndex(eop, samples, after, columnCount, result);
  11408. return result;
  11409. }
  11410. const factor =
  11411. JulianDate.secondsDifference(date, beforeDate) /
  11412. JulianDate.secondsDifference(afterDate, beforeDate);
  11413. const startBefore = before * columnCount;
  11414. const startAfter = after * columnCount;
  11415. // Handle UT1 leap second edge case
  11416. let beforeUt1MinusUtc = samples[startBefore + eop._ut1MinusUtcSecondsColumn];
  11417. let afterUt1MinusUtc = samples[startAfter + eop._ut1MinusUtcSecondsColumn];
  11418. const offsetDifference = afterUt1MinusUtc - beforeUt1MinusUtc;
  11419. if (offsetDifference > 0.5 || offsetDifference < -0.5) {
  11420. // The absolute difference between the values is more than 0.5, so we may have
  11421. // crossed a leap second. Check if this is the case and, if so, adjust the
  11422. // afterValue to account for the leap second. This way, our interpolation will
  11423. // produce reasonable results.
  11424. const beforeTaiMinusUtc =
  11425. samples[startBefore + eop._taiMinusUtcSecondsColumn];
  11426. const afterTaiMinusUtc =
  11427. samples[startAfter + eop._taiMinusUtcSecondsColumn];
  11428. if (beforeTaiMinusUtc !== afterTaiMinusUtc) {
  11429. if (afterDate.equals(date)) {
  11430. // If we are at the end of the leap second interval, take the second value
  11431. // Otherwise, the interpolation below will yield the wrong side of the
  11432. // discontinuity
  11433. // At the end of the leap second, we need to start accounting for the jump
  11434. beforeUt1MinusUtc = afterUt1MinusUtc;
  11435. } else {
  11436. // Otherwise, remove the leap second so that the interpolation is correct
  11437. afterUt1MinusUtc -= afterTaiMinusUtc - beforeTaiMinusUtc;
  11438. }
  11439. }
  11440. }
  11441. result.xPoleWander = linearInterp(
  11442. factor,
  11443. samples[startBefore + eop._xPoleWanderRadiansColumn],
  11444. samples[startAfter + eop._xPoleWanderRadiansColumn]
  11445. );
  11446. result.yPoleWander = linearInterp(
  11447. factor,
  11448. samples[startBefore + eop._yPoleWanderRadiansColumn],
  11449. samples[startAfter + eop._yPoleWanderRadiansColumn]
  11450. );
  11451. result.xPoleOffset = linearInterp(
  11452. factor,
  11453. samples[startBefore + eop._xCelestialPoleOffsetRadiansColumn],
  11454. samples[startAfter + eop._xCelestialPoleOffsetRadiansColumn]
  11455. );
  11456. result.yPoleOffset = linearInterp(
  11457. factor,
  11458. samples[startBefore + eop._yCelestialPoleOffsetRadiansColumn],
  11459. samples[startAfter + eop._yCelestialPoleOffsetRadiansColumn]
  11460. );
  11461. result.ut1MinusUtc = linearInterp(
  11462. factor,
  11463. beforeUt1MinusUtc,
  11464. afterUt1MinusUtc
  11465. );
  11466. return result;
  11467. }
  11468. /**
  11469. * A rotation expressed as a heading, pitch, and roll. Heading is the rotation about the
  11470. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  11471. * the positive x axis.
  11472. * @alias HeadingPitchRoll
  11473. * @constructor
  11474. *
  11475. * @param {number} [heading=0.0] The heading component in radians.
  11476. * @param {number} [pitch=0.0] The pitch component in radians.
  11477. * @param {number} [roll=0.0] The roll component in radians.
  11478. */
  11479. function HeadingPitchRoll(heading, pitch, roll) {
  11480. /**
  11481. * Gets or sets the heading.
  11482. * @type {number}
  11483. * @default 0.0
  11484. */
  11485. this.heading = defaultValue.defaultValue(heading, 0.0);
  11486. /**
  11487. * Gets or sets the pitch.
  11488. * @type {number}
  11489. * @default 0.0
  11490. */
  11491. this.pitch = defaultValue.defaultValue(pitch, 0.0);
  11492. /**
  11493. * Gets or sets the roll.
  11494. * @type {number}
  11495. * @default 0.0
  11496. */
  11497. this.roll = defaultValue.defaultValue(roll, 0.0);
  11498. }
  11499. /**
  11500. * Computes the heading, pitch and roll from a quaternion (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  11501. *
  11502. * @param {Quaternion} quaternion The quaternion from which to retrieve heading, pitch, and roll, all expressed in radians.
  11503. * @param {HeadingPitchRoll} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  11504. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  11505. */
  11506. HeadingPitchRoll.fromQuaternion = function (quaternion, result) {
  11507. //>>includeStart('debug', pragmas.debug);
  11508. if (!defaultValue.defined(quaternion)) {
  11509. throw new Check.DeveloperError("quaternion is required");
  11510. }
  11511. //>>includeEnd('debug');
  11512. if (!defaultValue.defined(result)) {
  11513. result = new HeadingPitchRoll();
  11514. }
  11515. const test = 2 * (quaternion.w * quaternion.y - quaternion.z * quaternion.x);
  11516. const denominatorRoll =
  11517. 1 - 2 * (quaternion.x * quaternion.x + quaternion.y * quaternion.y);
  11518. const numeratorRoll =
  11519. 2 * (quaternion.w * quaternion.x + quaternion.y * quaternion.z);
  11520. const denominatorHeading =
  11521. 1 - 2 * (quaternion.y * quaternion.y + quaternion.z * quaternion.z);
  11522. const numeratorHeading =
  11523. 2 * (quaternion.w * quaternion.z + quaternion.x * quaternion.y);
  11524. result.heading = -Math.atan2(numeratorHeading, denominatorHeading);
  11525. result.roll = Math.atan2(numeratorRoll, denominatorRoll);
  11526. result.pitch = -Math$1.CesiumMath.asinClamped(test);
  11527. return result;
  11528. };
  11529. /**
  11530. * Returns a new HeadingPitchRoll instance from angles given in degrees.
  11531. *
  11532. * @param {number} heading the heading in degrees
  11533. * @param {number} pitch the pitch in degrees
  11534. * @param {number} roll the heading in degrees
  11535. * @param {HeadingPitchRoll} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  11536. * @returns {HeadingPitchRoll} A new HeadingPitchRoll instance
  11537. */
  11538. HeadingPitchRoll.fromDegrees = function (heading, pitch, roll, result) {
  11539. //>>includeStart('debug', pragmas.debug);
  11540. if (!defaultValue.defined(heading)) {
  11541. throw new Check.DeveloperError("heading is required");
  11542. }
  11543. if (!defaultValue.defined(pitch)) {
  11544. throw new Check.DeveloperError("pitch is required");
  11545. }
  11546. if (!defaultValue.defined(roll)) {
  11547. throw new Check.DeveloperError("roll is required");
  11548. }
  11549. //>>includeEnd('debug');
  11550. if (!defaultValue.defined(result)) {
  11551. result = new HeadingPitchRoll();
  11552. }
  11553. result.heading = heading * Math$1.CesiumMath.RADIANS_PER_DEGREE;
  11554. result.pitch = pitch * Math$1.CesiumMath.RADIANS_PER_DEGREE;
  11555. result.roll = roll * Math$1.CesiumMath.RADIANS_PER_DEGREE;
  11556. return result;
  11557. };
  11558. /**
  11559. * Duplicates a HeadingPitchRoll instance.
  11560. *
  11561. * @param {HeadingPitchRoll} headingPitchRoll The HeadingPitchRoll to duplicate.
  11562. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  11563. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided. (Returns undefined if headingPitchRoll is undefined)
  11564. */
  11565. HeadingPitchRoll.clone = function (headingPitchRoll, result) {
  11566. if (!defaultValue.defined(headingPitchRoll)) {
  11567. return undefined;
  11568. }
  11569. if (!defaultValue.defined(result)) {
  11570. return new HeadingPitchRoll(
  11571. headingPitchRoll.heading,
  11572. headingPitchRoll.pitch,
  11573. headingPitchRoll.roll
  11574. );
  11575. }
  11576. result.heading = headingPitchRoll.heading;
  11577. result.pitch = headingPitchRoll.pitch;
  11578. result.roll = headingPitchRoll.roll;
  11579. return result;
  11580. };
  11581. /**
  11582. * Compares the provided HeadingPitchRolls componentwise and returns
  11583. * <code>true</code> if they are equal, <code>false</code> otherwise.
  11584. *
  11585. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  11586. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  11587. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  11588. */
  11589. HeadingPitchRoll.equals = function (left, right) {
  11590. return (
  11591. left === right ||
  11592. (defaultValue.defined(left) &&
  11593. defaultValue.defined(right) &&
  11594. left.heading === right.heading &&
  11595. left.pitch === right.pitch &&
  11596. left.roll === right.roll)
  11597. );
  11598. };
  11599. /**
  11600. * Compares the provided HeadingPitchRolls componentwise and returns
  11601. * <code>true</code> if they pass an absolute or relative tolerance test,
  11602. * <code>false</code> otherwise.
  11603. *
  11604. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  11605. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  11606. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  11607. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  11608. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  11609. */
  11610. HeadingPitchRoll.equalsEpsilon = function (
  11611. left,
  11612. right,
  11613. relativeEpsilon,
  11614. absoluteEpsilon
  11615. ) {
  11616. return (
  11617. left === right ||
  11618. (defaultValue.defined(left) &&
  11619. defaultValue.defined(right) &&
  11620. Math$1.CesiumMath.equalsEpsilon(
  11621. left.heading,
  11622. right.heading,
  11623. relativeEpsilon,
  11624. absoluteEpsilon
  11625. ) &&
  11626. Math$1.CesiumMath.equalsEpsilon(
  11627. left.pitch,
  11628. right.pitch,
  11629. relativeEpsilon,
  11630. absoluteEpsilon
  11631. ) &&
  11632. Math$1.CesiumMath.equalsEpsilon(
  11633. left.roll,
  11634. right.roll,
  11635. relativeEpsilon,
  11636. absoluteEpsilon
  11637. ))
  11638. );
  11639. };
  11640. /**
  11641. * Duplicates this HeadingPitchRoll instance.
  11642. *
  11643. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  11644. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  11645. */
  11646. HeadingPitchRoll.prototype.clone = function (result) {
  11647. return HeadingPitchRoll.clone(this, result);
  11648. };
  11649. /**
  11650. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  11651. * <code>true</code> if they are equal, <code>false</code> otherwise.
  11652. *
  11653. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  11654. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  11655. */
  11656. HeadingPitchRoll.prototype.equals = function (right) {
  11657. return HeadingPitchRoll.equals(this, right);
  11658. };
  11659. /**
  11660. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  11661. * <code>true</code> if they pass an absolute or relative tolerance test,
  11662. * <code>false</code> otherwise.
  11663. *
  11664. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  11665. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  11666. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  11667. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  11668. */
  11669. HeadingPitchRoll.prototype.equalsEpsilon = function (
  11670. right,
  11671. relativeEpsilon,
  11672. absoluteEpsilon
  11673. ) {
  11674. return HeadingPitchRoll.equalsEpsilon(
  11675. this,
  11676. right,
  11677. relativeEpsilon,
  11678. absoluteEpsilon
  11679. );
  11680. };
  11681. /**
  11682. * Creates a string representing this HeadingPitchRoll in the format '(heading, pitch, roll)' in radians.
  11683. *
  11684. * @returns {string} A string representing the provided HeadingPitchRoll in the format '(heading, pitch, roll)'.
  11685. */
  11686. HeadingPitchRoll.prototype.toString = function () {
  11687. return `(${this.heading}, ${this.pitch}, ${this.roll})`;
  11688. };
  11689. /*global CESIUM_BASE_URL,define,require*/
  11690. const cesiumScriptRegex = /((?:.*\/)|^)Cesium\.js(?:\?|\#|$)/;
  11691. function getBaseUrlFromCesiumScript() {
  11692. const scripts = document.getElementsByTagName("script");
  11693. for (let i = 0, len = scripts.length; i < len; ++i) {
  11694. const src = scripts[i].getAttribute("src");
  11695. const result = cesiumScriptRegex.exec(src);
  11696. if (result !== null) {
  11697. return result[1];
  11698. }
  11699. }
  11700. return undefined;
  11701. }
  11702. let a;
  11703. function tryMakeAbsolute(url) {
  11704. if (typeof document === "undefined") {
  11705. //Node.js and Web Workers. In both cases, the URL will already be absolute.
  11706. return url;
  11707. }
  11708. if (!defaultValue.defined(a)) {
  11709. a = document.createElement("a");
  11710. }
  11711. a.href = url;
  11712. // IE only absolutizes href on get, not set
  11713. // eslint-disable-next-line no-self-assign
  11714. a.href = a.href;
  11715. return a.href;
  11716. }
  11717. let baseResource;
  11718. function getCesiumBaseUrl() {
  11719. if (defaultValue.defined(baseResource)) {
  11720. return baseResource;
  11721. }
  11722. let baseUrlString;
  11723. if (typeof CESIUM_BASE_URL !== "undefined") {
  11724. baseUrlString = CESIUM_BASE_URL;
  11725. } else if (
  11726. typeof define === "object" &&
  11727. defaultValue.defined(define.amd) &&
  11728. !define.amd.toUrlUndefined &&
  11729. defaultValue.defined(require.toUrl)
  11730. ) {
  11731. baseUrlString = getAbsoluteUri(
  11732. "..",
  11733. buildModuleUrl("Core/buildModuleUrl.js")
  11734. );
  11735. } else {
  11736. baseUrlString = getBaseUrlFromCesiumScript();
  11737. }
  11738. //>>includeStart('debug', pragmas.debug);
  11739. if (!defaultValue.defined(baseUrlString)) {
  11740. throw new Check.DeveloperError(
  11741. "Unable to determine Cesium base URL automatically, try defining a global variable called CESIUM_BASE_URL."
  11742. );
  11743. }
  11744. //>>includeEnd('debug');
  11745. baseResource = new Resource({
  11746. url: tryMakeAbsolute(baseUrlString),
  11747. });
  11748. baseResource.appendForwardSlash();
  11749. return baseResource;
  11750. }
  11751. function buildModuleUrlFromRequireToUrl(moduleID) {
  11752. //moduleID will be non-relative, so require it relative to this module, in Core.
  11753. return tryMakeAbsolute(require.toUrl(`../${moduleID}`));
  11754. }
  11755. function buildModuleUrlFromBaseUrl(moduleID) {
  11756. const resource = getCesiumBaseUrl().getDerivedResource({
  11757. url: moduleID,
  11758. });
  11759. return resource.url;
  11760. }
  11761. let implementation;
  11762. /**
  11763. * Given a relative URL under the Cesium base URL, returns an absolute URL.
  11764. * @function
  11765. *
  11766. * @param {string} relativeUrl The relative path.
  11767. * @returns {string} The absolutely URL representation of the provided path.
  11768. *
  11769. * @example
  11770. * const viewer = new Cesium.Viewer("cesiumContainer", {
  11771. * baseLayer: Cesium.ImageryLayer.fromProviderAsync(
  11772. * Cesium.TileMapServiceImageryProvider.fromUrl(
  11773. * Cesium.buildModuleUrl("Assets/Textures/NaturalEarthII"),
  11774. * )),
  11775. * baseLayerPicker: false,
  11776. * });
  11777. */
  11778. function buildModuleUrl(relativeUrl) {
  11779. if (!defaultValue.defined(implementation)) {
  11780. //select implementation
  11781. if (
  11782. typeof define === "object" &&
  11783. defaultValue.defined(define.amd) &&
  11784. !define.amd.toUrlUndefined &&
  11785. defaultValue.defined(require.toUrl)
  11786. ) {
  11787. implementation = buildModuleUrlFromRequireToUrl;
  11788. } else {
  11789. implementation = buildModuleUrlFromBaseUrl;
  11790. }
  11791. }
  11792. const url = implementation(relativeUrl);
  11793. return url;
  11794. }
  11795. // exposed for testing
  11796. buildModuleUrl._cesiumScriptRegex = cesiumScriptRegex;
  11797. buildModuleUrl._buildModuleUrlFromBaseUrl = buildModuleUrlFromBaseUrl;
  11798. buildModuleUrl._clearBaseResource = function () {
  11799. baseResource = undefined;
  11800. };
  11801. /**
  11802. * Sets the base URL for resolving modules.
  11803. * @param {string} value The new base URL.
  11804. */
  11805. buildModuleUrl.setBaseUrl = function (value) {
  11806. baseResource = Resource.DEFAULT.getDerivedResource({
  11807. url: value,
  11808. });
  11809. };
  11810. /**
  11811. * Gets the base URL for resolving modules.
  11812. *
  11813. * @function
  11814. * @returns {string} The configured base URL
  11815. */
  11816. buildModuleUrl.getCesiumBaseUrl = getCesiumBaseUrl;
  11817. /**
  11818. * An IAU 2006 XYS value sampled at a particular time.
  11819. *
  11820. * @alias Iau2006XysSample
  11821. * @constructor
  11822. *
  11823. * @param {number} x The X value.
  11824. * @param {number} y The Y value.
  11825. * @param {number} s The S value.
  11826. *
  11827. * @private
  11828. */
  11829. function Iau2006XysSample(x, y, s) {
  11830. /**
  11831. * The X value.
  11832. * @type {number}
  11833. */
  11834. this.x = x;
  11835. /**
  11836. * The Y value.
  11837. * @type {number}
  11838. */
  11839. this.y = y;
  11840. /**
  11841. * The S value.
  11842. * @type {number}
  11843. */
  11844. this.s = s;
  11845. }
  11846. /**
  11847. * A set of IAU2006 XYS data that is used to evaluate the transformation between the International
  11848. * Celestial Reference Frame (ICRF) and the International Terrestrial Reference Frame (ITRF).
  11849. *
  11850. * @alias Iau2006XysData
  11851. * @constructor
  11852. *
  11853. * @param {object} [options] Object with the following properties:
  11854. * @param {Resource|string} [options.xysFileUrlTemplate='Assets/IAU2006_XYS/IAU2006_XYS_{0}.json'] A template URL for obtaining the XYS data. In the template,
  11855. * `{0}` will be replaced with the file index.
  11856. * @param {number} [options.interpolationOrder=9] The order of interpolation to perform on the XYS data.
  11857. * @param {number} [options.sampleZeroJulianEphemerisDate=2442396.5] The Julian ephemeris date (JED) of the
  11858. * first XYS sample.
  11859. * @param {number} [options.stepSizeDays=1.0] The step size, in days, between successive XYS samples.
  11860. * @param {number} [options.samplesPerXysFile=1000] The number of samples in each XYS file.
  11861. * @param {number} [options.totalSamples=27426] The total number of samples in all XYS files.
  11862. *
  11863. * @private
  11864. */
  11865. function Iau2006XysData(options) {
  11866. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  11867. this._xysFileUrlTemplate = Resource.createIfNeeded(
  11868. options.xysFileUrlTemplate
  11869. );
  11870. this._interpolationOrder = defaultValue.defaultValue(options.interpolationOrder, 9);
  11871. this._sampleZeroJulianEphemerisDate = defaultValue.defaultValue(
  11872. options.sampleZeroJulianEphemerisDate,
  11873. 2442396.5
  11874. );
  11875. this._sampleZeroDateTT = new JulianDate(
  11876. this._sampleZeroJulianEphemerisDate,
  11877. 0.0,
  11878. TimeStandard$1.TAI
  11879. );
  11880. this._stepSizeDays = defaultValue.defaultValue(options.stepSizeDays, 1.0);
  11881. this._samplesPerXysFile = defaultValue.defaultValue(options.samplesPerXysFile, 1000);
  11882. this._totalSamples = defaultValue.defaultValue(options.totalSamples, 27426);
  11883. this._samples = new Array(this._totalSamples * 3);
  11884. this._chunkDownloadsInProgress = [];
  11885. const order = this._interpolationOrder;
  11886. // Compute denominators and X values for interpolation.
  11887. const denom = (this._denominators = new Array(order + 1));
  11888. const xTable = (this._xTable = new Array(order + 1));
  11889. const stepN = Math.pow(this._stepSizeDays, order);
  11890. for (let i = 0; i <= order; ++i) {
  11891. denom[i] = stepN;
  11892. xTable[i] = i * this._stepSizeDays;
  11893. for (let j = 0; j <= order; ++j) {
  11894. if (j !== i) {
  11895. denom[i] *= i - j;
  11896. }
  11897. }
  11898. denom[i] = 1.0 / denom[i];
  11899. }
  11900. // Allocate scratch arrays for interpolation.
  11901. this._work = new Array(order + 1);
  11902. this._coef = new Array(order + 1);
  11903. }
  11904. const julianDateScratch = new JulianDate(0, 0.0, TimeStandard$1.TAI);
  11905. function getDaysSinceEpoch(xys, dayTT, secondTT) {
  11906. const dateTT = julianDateScratch;
  11907. dateTT.dayNumber = dayTT;
  11908. dateTT.secondsOfDay = secondTT;
  11909. return JulianDate.daysDifference(dateTT, xys._sampleZeroDateTT);
  11910. }
  11911. /**
  11912. * Preloads XYS data for a specified date range.
  11913. *
  11914. * @param {number} startDayTT The Julian day number of the beginning of the interval to preload, expressed in
  11915. * the Terrestrial Time (TT) time standard.
  11916. * @param {number} startSecondTT The seconds past noon of the beginning of the interval to preload, expressed in
  11917. * the Terrestrial Time (TT) time standard.
  11918. * @param {number} stopDayTT The Julian day number of the end of the interval to preload, expressed in
  11919. * the Terrestrial Time (TT) time standard.
  11920. * @param {number} stopSecondTT The seconds past noon of the end of the interval to preload, expressed in
  11921. * the Terrestrial Time (TT) time standard.
  11922. * @returns {Promise<void>} A promise that, when resolved, indicates that the requested interval has been
  11923. * preloaded.
  11924. */
  11925. Iau2006XysData.prototype.preload = function (
  11926. startDayTT,
  11927. startSecondTT,
  11928. stopDayTT,
  11929. stopSecondTT
  11930. ) {
  11931. const startDaysSinceEpoch = getDaysSinceEpoch(
  11932. this,
  11933. startDayTT,
  11934. startSecondTT
  11935. );
  11936. const stopDaysSinceEpoch = getDaysSinceEpoch(this, stopDayTT, stopSecondTT);
  11937. let startIndex =
  11938. (startDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  11939. 0;
  11940. if (startIndex < 0) {
  11941. startIndex = 0;
  11942. }
  11943. let stopIndex =
  11944. (stopDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  11945. (0 + this._interpolationOrder);
  11946. if (stopIndex >= this._totalSamples) {
  11947. stopIndex = this._totalSamples - 1;
  11948. }
  11949. const startChunk = (startIndex / this._samplesPerXysFile) | 0;
  11950. const stopChunk = (stopIndex / this._samplesPerXysFile) | 0;
  11951. const promises = [];
  11952. for (let i = startChunk; i <= stopChunk; ++i) {
  11953. promises.push(requestXysChunk(this, i));
  11954. }
  11955. return Promise.all(promises);
  11956. };
  11957. /**
  11958. * Computes the XYS values for a given date by interpolating. If the required data is not yet downloaded,
  11959. * this method will return undefined.
  11960. *
  11961. * @param {number} dayTT The Julian day number for which to compute the XYS value, expressed in
  11962. * the Terrestrial Time (TT) time standard.
  11963. * @param {number} secondTT The seconds past noon of the date for which to compute the XYS value, expressed in
  11964. * the Terrestrial Time (TT) time standard.
  11965. * @param {Iau2006XysSample} [result] The instance to which to copy the interpolated result. If this parameter
  11966. * is undefined, a new instance is allocated and returned.
  11967. * @returns {Iau2006XysSample} The interpolated XYS values, or undefined if the required data for this
  11968. * computation has not yet been downloaded.
  11969. *
  11970. * @see Iau2006XysData#preload
  11971. */
  11972. Iau2006XysData.prototype.computeXysRadians = function (
  11973. dayTT,
  11974. secondTT,
  11975. result
  11976. ) {
  11977. const daysSinceEpoch = getDaysSinceEpoch(this, dayTT, secondTT);
  11978. if (daysSinceEpoch < 0.0) {
  11979. // Can't evaluate prior to the epoch of the data.
  11980. return undefined;
  11981. }
  11982. const centerIndex = (daysSinceEpoch / this._stepSizeDays) | 0;
  11983. if (centerIndex >= this._totalSamples) {
  11984. // Can't evaluate after the last sample in the data.
  11985. return undefined;
  11986. }
  11987. const degree = this._interpolationOrder;
  11988. let firstIndex = centerIndex - ((degree / 2) | 0);
  11989. if (firstIndex < 0) {
  11990. firstIndex = 0;
  11991. }
  11992. let lastIndex = firstIndex + degree;
  11993. if (lastIndex >= this._totalSamples) {
  11994. lastIndex = this._totalSamples - 1;
  11995. firstIndex = lastIndex - degree;
  11996. if (firstIndex < 0) {
  11997. firstIndex = 0;
  11998. }
  11999. }
  12000. // Are all the samples we need present?
  12001. // We can assume so if the first and last are present
  12002. let isDataMissing = false;
  12003. const samples = this._samples;
  12004. if (!defaultValue.defined(samples[firstIndex * 3])) {
  12005. requestXysChunk(this, (firstIndex / this._samplesPerXysFile) | 0);
  12006. isDataMissing = true;
  12007. }
  12008. if (!defaultValue.defined(samples[lastIndex * 3])) {
  12009. requestXysChunk(this, (lastIndex / this._samplesPerXysFile) | 0);
  12010. isDataMissing = true;
  12011. }
  12012. if (isDataMissing) {
  12013. return undefined;
  12014. }
  12015. if (!defaultValue.defined(result)) {
  12016. result = new Iau2006XysSample(0.0, 0.0, 0.0);
  12017. } else {
  12018. result.x = 0.0;
  12019. result.y = 0.0;
  12020. result.s = 0.0;
  12021. }
  12022. const x = daysSinceEpoch - firstIndex * this._stepSizeDays;
  12023. const work = this._work;
  12024. const denom = this._denominators;
  12025. const coef = this._coef;
  12026. const xTable = this._xTable;
  12027. let i, j;
  12028. for (i = 0; i <= degree; ++i) {
  12029. work[i] = x - xTable[i];
  12030. }
  12031. for (i = 0; i <= degree; ++i) {
  12032. coef[i] = 1.0;
  12033. for (j = 0; j <= degree; ++j) {
  12034. if (j !== i) {
  12035. coef[i] *= work[j];
  12036. }
  12037. }
  12038. coef[i] *= denom[i];
  12039. let sampleIndex = (firstIndex + i) * 3;
  12040. result.x += coef[i] * samples[sampleIndex++];
  12041. result.y += coef[i] * samples[sampleIndex++];
  12042. result.s += coef[i] * samples[sampleIndex];
  12043. }
  12044. return result;
  12045. };
  12046. function requestXysChunk(xysData, chunkIndex) {
  12047. if (xysData._chunkDownloadsInProgress[chunkIndex]) {
  12048. // Chunk has already been requested.
  12049. return xysData._chunkDownloadsInProgress[chunkIndex];
  12050. }
  12051. let chunkUrl;
  12052. const xysFileUrlTemplate = xysData._xysFileUrlTemplate;
  12053. if (defaultValue.defined(xysFileUrlTemplate)) {
  12054. chunkUrl = xysFileUrlTemplate.getDerivedResource({
  12055. templateValues: {
  12056. 0: chunkIndex,
  12057. },
  12058. });
  12059. } else {
  12060. chunkUrl = new Resource({
  12061. url: buildModuleUrl(`Assets/IAU2006_XYS/IAU2006_XYS_${chunkIndex}.json`),
  12062. });
  12063. }
  12064. const promise = chunkUrl.fetchJson().then(function (chunk) {
  12065. xysData._chunkDownloadsInProgress[chunkIndex] = false;
  12066. const samples = xysData._samples;
  12067. const newSamples = chunk.samples;
  12068. const startIndex = chunkIndex * xysData._samplesPerXysFile * 3;
  12069. for (let i = 0, len = newSamples.length; i < len; ++i) {
  12070. samples[startIndex + i] = newSamples[i];
  12071. }
  12072. });
  12073. xysData._chunkDownloadsInProgress[chunkIndex] = promise;
  12074. return promise;
  12075. }
  12076. /**
  12077. * Contains functions for transforming positions to various reference frames.
  12078. *
  12079. * @namespace Transforms
  12080. */
  12081. const Transforms = {};
  12082. const vectorProductLocalFrame = {
  12083. up: {
  12084. south: "east",
  12085. north: "west",
  12086. west: "south",
  12087. east: "north",
  12088. },
  12089. down: {
  12090. south: "west",
  12091. north: "east",
  12092. west: "north",
  12093. east: "south",
  12094. },
  12095. south: {
  12096. up: "west",
  12097. down: "east",
  12098. west: "down",
  12099. east: "up",
  12100. },
  12101. north: {
  12102. up: "east",
  12103. down: "west",
  12104. west: "up",
  12105. east: "down",
  12106. },
  12107. west: {
  12108. up: "north",
  12109. down: "south",
  12110. north: "down",
  12111. south: "up",
  12112. },
  12113. east: {
  12114. up: "south",
  12115. down: "north",
  12116. north: "up",
  12117. south: "down",
  12118. },
  12119. };
  12120. const degeneratePositionLocalFrame = {
  12121. north: [-1, 0, 0],
  12122. east: [0, 1, 0],
  12123. up: [0, 0, 1],
  12124. south: [1, 0, 0],
  12125. west: [0, -1, 0],
  12126. down: [0, 0, -1],
  12127. };
  12128. const localFrameToFixedFrameCache = {};
  12129. const scratchCalculateCartesian = {
  12130. east: new Matrix3.Cartesian3(),
  12131. north: new Matrix3.Cartesian3(),
  12132. up: new Matrix3.Cartesian3(),
  12133. west: new Matrix3.Cartesian3(),
  12134. south: new Matrix3.Cartesian3(),
  12135. down: new Matrix3.Cartesian3(),
  12136. };
  12137. let scratchFirstCartesian = new Matrix3.Cartesian3();
  12138. let scratchSecondCartesian = new Matrix3.Cartesian3();
  12139. let scratchThirdCartesian = new Matrix3.Cartesian3();
  12140. /**
  12141. * Generates a function that computes a 4x4 transformation matrix from a reference frame
  12142. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  12143. * @param {string} firstAxis name of the first axis of the local reference frame. Must be
  12144. * 'east', 'north', 'up', 'west', 'south' or 'down'.
  12145. * @param {string} secondAxis name of the second axis of the local reference frame. Must be
  12146. * 'east', 'north', 'up', 'west', 'south' or 'down'.
  12147. * @return {Transforms.LocalFrameToFixedFrame} The function that will computes a
  12148. * 4x4 transformation matrix from a reference frame, with first axis and second axis compliant with the parameters,
  12149. */
  12150. Transforms.localFrameToFixedFrameGenerator = function (firstAxis, secondAxis) {
  12151. if (
  12152. !vectorProductLocalFrame.hasOwnProperty(firstAxis) ||
  12153. !vectorProductLocalFrame[firstAxis].hasOwnProperty(secondAxis)
  12154. ) {
  12155. throw new Check.DeveloperError(
  12156. "firstAxis and secondAxis must be east, north, up, west, south or down."
  12157. );
  12158. }
  12159. const thirdAxis = vectorProductLocalFrame[firstAxis][secondAxis];
  12160. /**
  12161. * Computes a 4x4 transformation matrix from a reference frame
  12162. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  12163. * @callback Transforms.LocalFrameToFixedFrame
  12164. * @param {Cartesian3} origin The center point of the local reference frame.
  12165. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12166. * @param {Matrix4} [result] The object onto which to store the result.
  12167. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  12168. */
  12169. let resultat;
  12170. const hashAxis = firstAxis + secondAxis;
  12171. if (defaultValue.defined(localFrameToFixedFrameCache[hashAxis])) {
  12172. resultat = localFrameToFixedFrameCache[hashAxis];
  12173. } else {
  12174. resultat = function (origin, ellipsoid, result) {
  12175. //>>includeStart('debug', pragmas.debug);
  12176. if (!defaultValue.defined(origin)) {
  12177. throw new Check.DeveloperError("origin is required.");
  12178. }
  12179. //>>includeEnd('debug');
  12180. if (!defaultValue.defined(result)) {
  12181. result = new Matrix2.Matrix4();
  12182. }
  12183. if (
  12184. Matrix3.Cartesian3.equalsEpsilon(origin, Matrix3.Cartesian3.ZERO, Math$1.CesiumMath.EPSILON14)
  12185. ) {
  12186. // If x, y, and z are zero, use the degenerate local frame, which is a special case
  12187. Matrix3.Cartesian3.unpack(
  12188. degeneratePositionLocalFrame[firstAxis],
  12189. 0,
  12190. scratchFirstCartesian
  12191. );
  12192. Matrix3.Cartesian3.unpack(
  12193. degeneratePositionLocalFrame[secondAxis],
  12194. 0,
  12195. scratchSecondCartesian
  12196. );
  12197. Matrix3.Cartesian3.unpack(
  12198. degeneratePositionLocalFrame[thirdAxis],
  12199. 0,
  12200. scratchThirdCartesian
  12201. );
  12202. } else if (
  12203. Math$1.CesiumMath.equalsEpsilon(origin.x, 0.0, Math$1.CesiumMath.EPSILON14) &&
  12204. Math$1.CesiumMath.equalsEpsilon(origin.y, 0.0, Math$1.CesiumMath.EPSILON14)
  12205. ) {
  12206. // If x and y are zero, assume origin is at a pole, which is a special case.
  12207. const sign = Math$1.CesiumMath.sign(origin.z);
  12208. Matrix3.Cartesian3.unpack(
  12209. degeneratePositionLocalFrame[firstAxis],
  12210. 0,
  12211. scratchFirstCartesian
  12212. );
  12213. if (firstAxis !== "east" && firstAxis !== "west") {
  12214. Matrix3.Cartesian3.multiplyByScalar(
  12215. scratchFirstCartesian,
  12216. sign,
  12217. scratchFirstCartesian
  12218. );
  12219. }
  12220. Matrix3.Cartesian3.unpack(
  12221. degeneratePositionLocalFrame[secondAxis],
  12222. 0,
  12223. scratchSecondCartesian
  12224. );
  12225. if (secondAxis !== "east" && secondAxis !== "west") {
  12226. Matrix3.Cartesian3.multiplyByScalar(
  12227. scratchSecondCartesian,
  12228. sign,
  12229. scratchSecondCartesian
  12230. );
  12231. }
  12232. Matrix3.Cartesian3.unpack(
  12233. degeneratePositionLocalFrame[thirdAxis],
  12234. 0,
  12235. scratchThirdCartesian
  12236. );
  12237. if (thirdAxis !== "east" && thirdAxis !== "west") {
  12238. Matrix3.Cartesian3.multiplyByScalar(
  12239. scratchThirdCartesian,
  12240. sign,
  12241. scratchThirdCartesian
  12242. );
  12243. }
  12244. } else {
  12245. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix3.Ellipsoid.WGS84);
  12246. ellipsoid.geodeticSurfaceNormal(origin, scratchCalculateCartesian.up);
  12247. const up = scratchCalculateCartesian.up;
  12248. const east = scratchCalculateCartesian.east;
  12249. east.x = -origin.y;
  12250. east.y = origin.x;
  12251. east.z = 0.0;
  12252. Matrix3.Cartesian3.normalize(east, scratchCalculateCartesian.east);
  12253. Matrix3.Cartesian3.cross(up, east, scratchCalculateCartesian.north);
  12254. Matrix3.Cartesian3.multiplyByScalar(
  12255. scratchCalculateCartesian.up,
  12256. -1,
  12257. scratchCalculateCartesian.down
  12258. );
  12259. Matrix3.Cartesian3.multiplyByScalar(
  12260. scratchCalculateCartesian.east,
  12261. -1,
  12262. scratchCalculateCartesian.west
  12263. );
  12264. Matrix3.Cartesian3.multiplyByScalar(
  12265. scratchCalculateCartesian.north,
  12266. -1,
  12267. scratchCalculateCartesian.south
  12268. );
  12269. scratchFirstCartesian = scratchCalculateCartesian[firstAxis];
  12270. scratchSecondCartesian = scratchCalculateCartesian[secondAxis];
  12271. scratchThirdCartesian = scratchCalculateCartesian[thirdAxis];
  12272. }
  12273. result[0] = scratchFirstCartesian.x;
  12274. result[1] = scratchFirstCartesian.y;
  12275. result[2] = scratchFirstCartesian.z;
  12276. result[3] = 0.0;
  12277. result[4] = scratchSecondCartesian.x;
  12278. result[5] = scratchSecondCartesian.y;
  12279. result[6] = scratchSecondCartesian.z;
  12280. result[7] = 0.0;
  12281. result[8] = scratchThirdCartesian.x;
  12282. result[9] = scratchThirdCartesian.y;
  12283. result[10] = scratchThirdCartesian.z;
  12284. result[11] = 0.0;
  12285. result[12] = origin.x;
  12286. result[13] = origin.y;
  12287. result[14] = origin.z;
  12288. result[15] = 1.0;
  12289. return result;
  12290. };
  12291. localFrameToFixedFrameCache[hashAxis] = resultat;
  12292. }
  12293. return resultat;
  12294. };
  12295. /**
  12296. * Computes a 4x4 transformation matrix from a reference frame with an east-north-up axes
  12297. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  12298. * The local axes are defined as:
  12299. * <ul>
  12300. * <li>The <code>x</code> axis points in the local east direction.</li>
  12301. * <li>The <code>y</code> axis points in the local north direction.</li>
  12302. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  12303. * </ul>
  12304. *
  12305. * @function
  12306. * @param {Cartesian3} origin The center point of the local reference frame.
  12307. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12308. * @param {Matrix4} [result] The object onto which to store the result.
  12309. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  12310. *
  12311. * @example
  12312. * // Get the transform from local east-north-up at cartographic (0.0, 0.0) to Earth's fixed frame.
  12313. * const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  12314. * const transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);
  12315. */
  12316. Transforms.eastNorthUpToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  12317. "east",
  12318. "north"
  12319. );
  12320. /**
  12321. * Computes a 4x4 transformation matrix from a reference frame with an north-east-down axes
  12322. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  12323. * The local axes are defined as:
  12324. * <ul>
  12325. * <li>The <code>x</code> axis points in the local north direction.</li>
  12326. * <li>The <code>y</code> axis points in the local east direction.</li>
  12327. * <li>The <code>z</code> axis points in the opposite direction of the ellipsoid surface normal which passes through the position.</li>
  12328. * </ul>
  12329. *
  12330. * @function
  12331. * @param {Cartesian3} origin The center point of the local reference frame.
  12332. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12333. * @param {Matrix4} [result] The object onto which to store the result.
  12334. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  12335. *
  12336. * @example
  12337. * // Get the transform from local north-east-down at cartographic (0.0, 0.0) to Earth's fixed frame.
  12338. * const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  12339. * const transform = Cesium.Transforms.northEastDownToFixedFrame(center);
  12340. */
  12341. Transforms.northEastDownToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  12342. "north",
  12343. "east"
  12344. );
  12345. /**
  12346. * Computes a 4x4 transformation matrix from a reference frame with an north-up-east axes
  12347. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  12348. * The local axes are defined as:
  12349. * <ul>
  12350. * <li>The <code>x</code> axis points in the local north direction.</li>
  12351. * <li>The <code>y</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  12352. * <li>The <code>z</code> axis points in the local east direction.</li>
  12353. * </ul>
  12354. *
  12355. * @function
  12356. * @param {Cartesian3} origin The center point of the local reference frame.
  12357. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12358. * @param {Matrix4} [result] The object onto which to store the result.
  12359. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  12360. *
  12361. * @example
  12362. * // Get the transform from local north-up-east at cartographic (0.0, 0.0) to Earth's fixed frame.
  12363. * const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  12364. * const transform = Cesium.Transforms.northUpEastToFixedFrame(center);
  12365. */
  12366. Transforms.northUpEastToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  12367. "north",
  12368. "up"
  12369. );
  12370. /**
  12371. * Computes a 4x4 transformation matrix from a reference frame with an north-west-up axes
  12372. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  12373. * The local axes are defined as:
  12374. * <ul>
  12375. * <li>The <code>x</code> axis points in the local north direction.</li>
  12376. * <li>The <code>y</code> axis points in the local west direction.</li>
  12377. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  12378. * </ul>
  12379. *
  12380. * @function
  12381. * @param {Cartesian3} origin The center point of the local reference frame.
  12382. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12383. * @param {Matrix4} [result] The object onto which to store the result.
  12384. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  12385. *
  12386. * @example
  12387. * // Get the transform from local north-West-Up at cartographic (0.0, 0.0) to Earth's fixed frame.
  12388. * const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  12389. * const transform = Cesium.Transforms.northWestUpToFixedFrame(center);
  12390. */
  12391. Transforms.northWestUpToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  12392. "north",
  12393. "west"
  12394. );
  12395. const scratchHPRQuaternion = new Quaternion();
  12396. const scratchScale = new Matrix3.Cartesian3(1.0, 1.0, 1.0);
  12397. const scratchHPRMatrix4 = new Matrix2.Matrix4();
  12398. /**
  12399. * Computes a 4x4 transformation matrix from a reference frame with axes computed from the heading-pitch-roll angles
  12400. * centered at the provided origin to the provided ellipsoid's fixed reference frame. Heading is the rotation from the local north
  12401. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  12402. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  12403. *
  12404. * @param {Cartesian3} origin The center point of the local reference frame.
  12405. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  12406. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12407. * @param {Transforms.LocalFrameToFixedFrame} [fixedFrameTransform=Transforms.eastNorthUpToFixedFrame] A 4x4 transformation
  12408. * matrix from a reference frame to the provided ellipsoid's fixed reference frame
  12409. * @param {Matrix4} [result] The object onto which to store the result.
  12410. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  12411. *
  12412. * @example
  12413. * // Get the transform from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  12414. * const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  12415. * const heading = -Cesium.Math.PI_OVER_TWO;
  12416. * const pitch = Cesium.Math.PI_OVER_FOUR;
  12417. * const roll = 0.0;
  12418. * const hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  12419. * const transform = Cesium.Transforms.headingPitchRollToFixedFrame(center, hpr);
  12420. */
  12421. Transforms.headingPitchRollToFixedFrame = function (
  12422. origin,
  12423. headingPitchRoll,
  12424. ellipsoid,
  12425. fixedFrameTransform,
  12426. result
  12427. ) {
  12428. //>>includeStart('debug', pragmas.debug);
  12429. Check.Check.typeOf.object("HeadingPitchRoll", headingPitchRoll);
  12430. //>>includeEnd('debug');
  12431. fixedFrameTransform = defaultValue.defaultValue(
  12432. fixedFrameTransform,
  12433. Transforms.eastNorthUpToFixedFrame
  12434. );
  12435. const hprQuaternion = Quaternion.fromHeadingPitchRoll(
  12436. headingPitchRoll,
  12437. scratchHPRQuaternion
  12438. );
  12439. const hprMatrix = Matrix2.Matrix4.fromTranslationQuaternionRotationScale(
  12440. Matrix3.Cartesian3.ZERO,
  12441. hprQuaternion,
  12442. scratchScale,
  12443. scratchHPRMatrix4
  12444. );
  12445. result = fixedFrameTransform(origin, ellipsoid, result);
  12446. return Matrix2.Matrix4.multiply(result, hprMatrix, result);
  12447. };
  12448. const scratchENUMatrix4 = new Matrix2.Matrix4();
  12449. const scratchHPRMatrix3 = new Matrix3.Matrix3();
  12450. /**
  12451. * Computes a quaternion from a reference frame with axes computed from the heading-pitch-roll angles
  12452. * centered at the provided origin. Heading is the rotation from the local north
  12453. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  12454. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  12455. *
  12456. * @param {Cartesian3} origin The center point of the local reference frame.
  12457. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  12458. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12459. * @param {Transforms.LocalFrameToFixedFrame} [fixedFrameTransform=Transforms.eastNorthUpToFixedFrame] A 4x4 transformation
  12460. * matrix from a reference frame to the provided ellipsoid's fixed reference frame
  12461. * @param {Quaternion} [result] The object onto which to store the result.
  12462. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  12463. *
  12464. * @example
  12465. * // Get the quaternion from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  12466. * const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  12467. * const heading = -Cesium.Math.PI_OVER_TWO;
  12468. * const pitch = Cesium.Math.PI_OVER_FOUR;
  12469. * const roll = 0.0;
  12470. * const hpr = new HeadingPitchRoll(heading, pitch, roll);
  12471. * const quaternion = Cesium.Transforms.headingPitchRollQuaternion(center, hpr);
  12472. */
  12473. Transforms.headingPitchRollQuaternion = function (
  12474. origin,
  12475. headingPitchRoll,
  12476. ellipsoid,
  12477. fixedFrameTransform,
  12478. result
  12479. ) {
  12480. //>>includeStart('debug', pragmas.debug);
  12481. Check.Check.typeOf.object("HeadingPitchRoll", headingPitchRoll);
  12482. //>>includeEnd('debug');
  12483. const transform = Transforms.headingPitchRollToFixedFrame(
  12484. origin,
  12485. headingPitchRoll,
  12486. ellipsoid,
  12487. fixedFrameTransform,
  12488. scratchENUMatrix4
  12489. );
  12490. const rotation = Matrix2.Matrix4.getMatrix3(transform, scratchHPRMatrix3);
  12491. return Quaternion.fromRotationMatrix(rotation, result);
  12492. };
  12493. const noScale = new Matrix3.Cartesian3(1.0, 1.0, 1.0);
  12494. const hprCenterScratch = new Matrix3.Cartesian3();
  12495. const ffScratch = new Matrix2.Matrix4();
  12496. const hprTransformScratch = new Matrix2.Matrix4();
  12497. const hprRotationScratch = new Matrix3.Matrix3();
  12498. const hprQuaternionScratch = new Quaternion();
  12499. /**
  12500. * Computes heading-pitch-roll angles from a transform in a particular reference frame. Heading is the rotation from the local north
  12501. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  12502. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  12503. *
  12504. * @param {Matrix4} transform The transform
  12505. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12506. * @param {Transforms.LocalFrameToFixedFrame} [fixedFrameTransform=Transforms.eastNorthUpToFixedFrame] A 4x4 transformation
  12507. * matrix from a reference frame to the provided ellipsoid's fixed reference frame
  12508. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  12509. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if none was provided.
  12510. */
  12511. Transforms.fixedFrameToHeadingPitchRoll = function (
  12512. transform,
  12513. ellipsoid,
  12514. fixedFrameTransform,
  12515. result
  12516. ) {
  12517. //>>includeStart('debug', pragmas.debug);
  12518. Check.Check.defined("transform", transform);
  12519. //>>includeEnd('debug');
  12520. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix3.Ellipsoid.WGS84);
  12521. fixedFrameTransform = defaultValue.defaultValue(
  12522. fixedFrameTransform,
  12523. Transforms.eastNorthUpToFixedFrame
  12524. );
  12525. if (!defaultValue.defined(result)) {
  12526. result = new HeadingPitchRoll();
  12527. }
  12528. const center = Matrix2.Matrix4.getTranslation(transform, hprCenterScratch);
  12529. if (Matrix3.Cartesian3.equals(center, Matrix3.Cartesian3.ZERO)) {
  12530. result.heading = 0;
  12531. result.pitch = 0;
  12532. result.roll = 0;
  12533. return result;
  12534. }
  12535. let toFixedFrame = Matrix2.Matrix4.inverseTransformation(
  12536. fixedFrameTransform(center, ellipsoid, ffScratch),
  12537. ffScratch
  12538. );
  12539. let transformCopy = Matrix2.Matrix4.setScale(transform, noScale, hprTransformScratch);
  12540. transformCopy = Matrix2.Matrix4.setTranslation(
  12541. transformCopy,
  12542. Matrix3.Cartesian3.ZERO,
  12543. transformCopy
  12544. );
  12545. toFixedFrame = Matrix2.Matrix4.multiply(toFixedFrame, transformCopy, toFixedFrame);
  12546. let quaternionRotation = Quaternion.fromRotationMatrix(
  12547. Matrix2.Matrix4.getMatrix3(toFixedFrame, hprRotationScratch),
  12548. hprQuaternionScratch
  12549. );
  12550. quaternionRotation = Quaternion.normalize(
  12551. quaternionRotation,
  12552. quaternionRotation
  12553. );
  12554. return HeadingPitchRoll.fromQuaternion(quaternionRotation, result);
  12555. };
  12556. const gmstConstant0 = 6 * 3600 + 41 * 60 + 50.54841;
  12557. const gmstConstant1 = 8640184.812866;
  12558. const gmstConstant2 = 0.093104;
  12559. const gmstConstant3 = -6.2e-6;
  12560. const rateCoef = 1.1772758384668e-19;
  12561. const wgs84WRPrecessing = 7.2921158553e-5;
  12562. const twoPiOverSecondsInDay = Math$1.CesiumMath.TWO_PI / 86400.0;
  12563. let dateInUtc = new JulianDate();
  12564. /**
  12565. * Computes a rotation matrix to transform a point or vector from True Equator Mean Equinox (TEME) axes to the
  12566. * pseudo-fixed axes at a given time. This method treats the UT1 time standard as equivalent to UTC.
  12567. *
  12568. * @param {JulianDate} date The time at which to compute the rotation matrix.
  12569. * @param {Matrix3} [result] The object onto which to store the result.
  12570. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if none was provided.
  12571. *
  12572. * @example
  12573. * //Set the view to the inertial frame.
  12574. * scene.postUpdate.addEventListener(function(scene, time) {
  12575. * const now = Cesium.JulianDate.now();
  12576. * const offset = Cesium.Matrix4.multiplyByPoint(camera.transform, camera.position, new Cesium.Cartesian3());
  12577. * const transform = Cesium.Matrix4.fromRotationTranslation(Cesium.Transforms.computeTemeToPseudoFixedMatrix(now));
  12578. * const inverseTransform = Cesium.Matrix4.inverseTransformation(transform, new Cesium.Matrix4());
  12579. * Cesium.Matrix4.multiplyByPoint(inverseTransform, offset, offset);
  12580. * camera.lookAtTransform(transform, offset);
  12581. * });
  12582. */
  12583. Transforms.computeTemeToPseudoFixedMatrix = function (date, result) {
  12584. //>>includeStart('debug', pragmas.debug);
  12585. if (!defaultValue.defined(date)) {
  12586. throw new Check.DeveloperError("date is required.");
  12587. }
  12588. //>>includeEnd('debug');
  12589. // GMST is actually computed using UT1. We're using UTC as an approximation of UT1.
  12590. // We do not want to use the function like convertTaiToUtc in JulianDate because
  12591. // we explicitly do not want to fail when inside the leap second.
  12592. dateInUtc = JulianDate.addSeconds(
  12593. date,
  12594. -JulianDate.computeTaiMinusUtc(date),
  12595. dateInUtc
  12596. );
  12597. const utcDayNumber = dateInUtc.dayNumber;
  12598. const utcSecondsIntoDay = dateInUtc.secondsOfDay;
  12599. let t;
  12600. const diffDays = utcDayNumber - 2451545;
  12601. if (utcSecondsIntoDay >= 43200.0) {
  12602. t = (diffDays + 0.5) / TimeConstants$1.DAYS_PER_JULIAN_CENTURY;
  12603. } else {
  12604. t = (diffDays - 0.5) / TimeConstants$1.DAYS_PER_JULIAN_CENTURY;
  12605. }
  12606. const gmst0 =
  12607. gmstConstant0 +
  12608. t * (gmstConstant1 + t * (gmstConstant2 + t * gmstConstant3));
  12609. const angle = (gmst0 * twoPiOverSecondsInDay) % Math$1.CesiumMath.TWO_PI;
  12610. const ratio = wgs84WRPrecessing + rateCoef * (utcDayNumber - 2451545.5);
  12611. const secondsSinceMidnight =
  12612. (utcSecondsIntoDay + TimeConstants$1.SECONDS_PER_DAY * 0.5) %
  12613. TimeConstants$1.SECONDS_PER_DAY;
  12614. const gha = angle + ratio * secondsSinceMidnight;
  12615. const cosGha = Math.cos(gha);
  12616. const sinGha = Math.sin(gha);
  12617. if (!defaultValue.defined(result)) {
  12618. return new Matrix3.Matrix3(
  12619. cosGha,
  12620. sinGha,
  12621. 0.0,
  12622. -sinGha,
  12623. cosGha,
  12624. 0.0,
  12625. 0.0,
  12626. 0.0,
  12627. 1.0
  12628. );
  12629. }
  12630. result[0] = cosGha;
  12631. result[1] = -sinGha;
  12632. result[2] = 0.0;
  12633. result[3] = sinGha;
  12634. result[4] = cosGha;
  12635. result[5] = 0.0;
  12636. result[6] = 0.0;
  12637. result[7] = 0.0;
  12638. result[8] = 1.0;
  12639. return result;
  12640. };
  12641. /**
  12642. * The source of IAU 2006 XYS data, used for computing the transformation between the
  12643. * Fixed and ICRF axes.
  12644. * @type {Iau2006XysData}
  12645. *
  12646. * @see Transforms.computeIcrfToFixedMatrix
  12647. * @see Transforms.computeFixedToIcrfMatrix
  12648. *
  12649. * @private
  12650. */
  12651. Transforms.iau2006XysData = new Iau2006XysData();
  12652. /**
  12653. * The source of Earth Orientation Parameters (EOP) data, used for computing the transformation
  12654. * between the Fixed and ICRF axes. By default, zero values are used for all EOP values,
  12655. * yielding a reasonable but not completely accurate representation of the ICRF axes.
  12656. * @type {EarthOrientationParameters}
  12657. *
  12658. * @see Transforms.computeIcrfToFixedMatrix
  12659. * @see Transforms.computeFixedToIcrfMatrix
  12660. *
  12661. * @private
  12662. */
  12663. Transforms.earthOrientationParameters = EarthOrientationParameters.NONE;
  12664. const ttMinusTai = 32.184;
  12665. const j2000ttDays = 2451545.0;
  12666. /**
  12667. * Preloads the data necessary to transform between the ICRF and Fixed axes, in either
  12668. * direction, over a given interval. This function returns a promise that, when resolved,
  12669. * indicates that the preload has completed.
  12670. *
  12671. * @param {TimeInterval} timeInterval The interval to preload.
  12672. * @returns {Promise<void>} A promise that, when resolved, indicates that the preload has completed
  12673. * and evaluation of the transformation between the fixed and ICRF axes will
  12674. * no longer return undefined for a time inside the interval.
  12675. *
  12676. *
  12677. * @example
  12678. * const interval = new Cesium.TimeInterval(...);
  12679. * await Cesium.Transforms.preloadIcrfFixed(interval));
  12680. * // the data is now loaded
  12681. *
  12682. * @see Transforms.computeIcrfToFixedMatrix
  12683. * @see Transforms.computeFixedToIcrfMatrix
  12684. */
  12685. Transforms.preloadIcrfFixed = function (timeInterval) {
  12686. const startDayTT = timeInterval.start.dayNumber;
  12687. const startSecondTT = timeInterval.start.secondsOfDay + ttMinusTai;
  12688. const stopDayTT = timeInterval.stop.dayNumber;
  12689. const stopSecondTT = timeInterval.stop.secondsOfDay + ttMinusTai;
  12690. return Transforms.iau2006XysData.preload(
  12691. startDayTT,
  12692. startSecondTT,
  12693. stopDayTT,
  12694. stopSecondTT
  12695. );
  12696. };
  12697. /**
  12698. * Computes a rotation matrix to transform a point or vector from the International Celestial
  12699. * Reference Frame (GCRF/ICRF) inertial frame axes to the Earth-Fixed frame axes (ITRF)
  12700. * at a given time. This function may return undefined if the data necessary to
  12701. * do the transformation is not yet loaded.
  12702. *
  12703. * @param {JulianDate} date The time at which to compute the rotation matrix.
  12704. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  12705. * not specified, a new instance is created and returned.
  12706. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  12707. * transformation is not yet loaded.
  12708. *
  12709. *
  12710. * @example
  12711. * scene.postUpdate.addEventListener(function(scene, time) {
  12712. * // View in ICRF.
  12713. * const icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(time);
  12714. * if (Cesium.defined(icrfToFixed)) {
  12715. * const offset = Cesium.Cartesian3.clone(camera.position);
  12716. * const transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);
  12717. * camera.lookAtTransform(transform, offset);
  12718. * }
  12719. * });
  12720. *
  12721. * @see Transforms.preloadIcrfFixed
  12722. */
  12723. Transforms.computeIcrfToFixedMatrix = function (date, result) {
  12724. //>>includeStart('debug', pragmas.debug);
  12725. if (!defaultValue.defined(date)) {
  12726. throw new Check.DeveloperError("date is required.");
  12727. }
  12728. //>>includeEnd('debug');
  12729. if (!defaultValue.defined(result)) {
  12730. result = new Matrix3.Matrix3();
  12731. }
  12732. const fixedToIcrfMtx = Transforms.computeFixedToIcrfMatrix(date, result);
  12733. if (!defaultValue.defined(fixedToIcrfMtx)) {
  12734. return undefined;
  12735. }
  12736. return Matrix3.Matrix3.transpose(fixedToIcrfMtx, result);
  12737. };
  12738. const xysScratch = new Iau2006XysSample(0.0, 0.0, 0.0);
  12739. const eopScratch = new EarthOrientationParametersSample(
  12740. 0.0,
  12741. 0.0,
  12742. 0.0,
  12743. 0.0,
  12744. 0.0);
  12745. const rotation1Scratch = new Matrix3.Matrix3();
  12746. const rotation2Scratch = new Matrix3.Matrix3();
  12747. /**
  12748. * Computes a rotation matrix to transform a point or vector from the Earth-Fixed frame axes (ITRF)
  12749. * to the International Celestial Reference Frame (GCRF/ICRF) inertial frame axes
  12750. * at a given time. This function may return undefined if the data necessary to
  12751. * do the transformation is not yet loaded.
  12752. *
  12753. * @param {JulianDate} date The time at which to compute the rotation matrix.
  12754. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  12755. * not specified, a new instance is created and returned.
  12756. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  12757. * transformation is not yet loaded.
  12758. *
  12759. *
  12760. * @example
  12761. * // Transform a point from the ICRF axes to the Fixed axes.
  12762. * const now = Cesium.JulianDate.now();
  12763. * const pointInFixed = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  12764. * const fixedToIcrf = Cesium.Transforms.computeIcrfToFixedMatrix(now);
  12765. * let pointInInertial = new Cesium.Cartesian3();
  12766. * if (Cesium.defined(fixedToIcrf)) {
  12767. * pointInInertial = Cesium.Matrix3.multiplyByVector(fixedToIcrf, pointInFixed, pointInInertial);
  12768. * }
  12769. *
  12770. * @see Transforms.preloadIcrfFixed
  12771. */
  12772. Transforms.computeFixedToIcrfMatrix = function (date, result) {
  12773. //>>includeStart('debug', pragmas.debug);
  12774. if (!defaultValue.defined(date)) {
  12775. throw new Check.DeveloperError("date is required.");
  12776. }
  12777. //>>includeEnd('debug');
  12778. if (!defaultValue.defined(result)) {
  12779. result = new Matrix3.Matrix3();
  12780. }
  12781. // Compute pole wander
  12782. const eop = Transforms.earthOrientationParameters.compute(date, eopScratch);
  12783. if (!defaultValue.defined(eop)) {
  12784. return undefined;
  12785. }
  12786. // There is no external conversion to Terrestrial Time (TT).
  12787. // So use International Atomic Time (TAI) and convert using offsets.
  12788. // Here we are assuming that dayTT and secondTT are positive
  12789. const dayTT = date.dayNumber;
  12790. // It's possible here that secondTT could roll over 86400
  12791. // This does not seem to affect the precision (unit tests check for this)
  12792. const secondTT = date.secondsOfDay + ttMinusTai;
  12793. const xys = Transforms.iau2006XysData.computeXysRadians(
  12794. dayTT,
  12795. secondTT,
  12796. xysScratch
  12797. );
  12798. if (!defaultValue.defined(xys)) {
  12799. return undefined;
  12800. }
  12801. const x = xys.x + eop.xPoleOffset;
  12802. const y = xys.y + eop.yPoleOffset;
  12803. // Compute XYS rotation
  12804. const a = 1.0 / (1.0 + Math.sqrt(1.0 - x * x - y * y));
  12805. const rotation1 = rotation1Scratch;
  12806. rotation1[0] = 1.0 - a * x * x;
  12807. rotation1[3] = -a * x * y;
  12808. rotation1[6] = x;
  12809. rotation1[1] = -a * x * y;
  12810. rotation1[4] = 1 - a * y * y;
  12811. rotation1[7] = y;
  12812. rotation1[2] = -x;
  12813. rotation1[5] = -y;
  12814. rotation1[8] = 1 - a * (x * x + y * y);
  12815. const rotation2 = Matrix3.Matrix3.fromRotationZ(-xys.s, rotation2Scratch);
  12816. const matrixQ = Matrix3.Matrix3.multiply(rotation1, rotation2, rotation1Scratch);
  12817. // Similar to TT conversions above
  12818. // It's possible here that secondTT could roll over 86400
  12819. // This does not seem to affect the precision (unit tests check for this)
  12820. const dateUt1day = date.dayNumber;
  12821. const dateUt1sec =
  12822. date.secondsOfDay - JulianDate.computeTaiMinusUtc(date) + eop.ut1MinusUtc;
  12823. // Compute Earth rotation angle
  12824. // The IERS standard for era is
  12825. // era = 0.7790572732640 + 1.00273781191135448 * Tu
  12826. // where
  12827. // Tu = JulianDateInUt1 - 2451545.0
  12828. // However, you get much more precision if you make the following simplification
  12829. // era = a + (1 + b) * (JulianDayNumber + FractionOfDay - 2451545)
  12830. // era = a + (JulianDayNumber - 2451545) + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  12831. // era = a + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  12832. // since (JulianDayNumber - 2451545) represents an integer number of revolutions which will be discarded anyway.
  12833. const daysSinceJ2000 = dateUt1day - 2451545;
  12834. const fractionOfDay = dateUt1sec / TimeConstants$1.SECONDS_PER_DAY;
  12835. let era =
  12836. 0.779057273264 +
  12837. fractionOfDay +
  12838. 0.00273781191135448 * (daysSinceJ2000 + fractionOfDay);
  12839. era = (era % 1.0) * Math$1.CesiumMath.TWO_PI;
  12840. const earthRotation = Matrix3.Matrix3.fromRotationZ(era, rotation2Scratch);
  12841. // pseudoFixed to ICRF
  12842. const pfToIcrf = Matrix3.Matrix3.multiply(matrixQ, earthRotation, rotation1Scratch);
  12843. // Compute pole wander matrix
  12844. const cosxp = Math.cos(eop.xPoleWander);
  12845. const cosyp = Math.cos(eop.yPoleWander);
  12846. const sinxp = Math.sin(eop.xPoleWander);
  12847. const sinyp = Math.sin(eop.yPoleWander);
  12848. let ttt = dayTT - j2000ttDays + secondTT / TimeConstants$1.SECONDS_PER_DAY;
  12849. ttt /= 36525.0;
  12850. // approximate sp value in rad
  12851. const sp = (-47.0e-6 * ttt * Math$1.CesiumMath.RADIANS_PER_DEGREE) / 3600.0;
  12852. const cossp = Math.cos(sp);
  12853. const sinsp = Math.sin(sp);
  12854. const fToPfMtx = rotation2Scratch;
  12855. fToPfMtx[0] = cosxp * cossp;
  12856. fToPfMtx[1] = cosxp * sinsp;
  12857. fToPfMtx[2] = sinxp;
  12858. fToPfMtx[3] = -cosyp * sinsp + sinyp * sinxp * cossp;
  12859. fToPfMtx[4] = cosyp * cossp + sinyp * sinxp * sinsp;
  12860. fToPfMtx[5] = -sinyp * cosxp;
  12861. fToPfMtx[6] = -sinyp * sinsp - cosyp * sinxp * cossp;
  12862. fToPfMtx[7] = sinyp * cossp - cosyp * sinxp * sinsp;
  12863. fToPfMtx[8] = cosyp * cosxp;
  12864. return Matrix3.Matrix3.multiply(pfToIcrf, fToPfMtx, result);
  12865. };
  12866. const pointToWindowCoordinatesTemp = new Matrix2.Cartesian4();
  12867. /**
  12868. * Transform a point from model coordinates to window coordinates.
  12869. *
  12870. * @param {Matrix4} modelViewProjectionMatrix The 4x4 model-view-projection matrix.
  12871. * @param {Matrix4} viewportTransformation The 4x4 viewport transformation.
  12872. * @param {Cartesian3} point The point to transform.
  12873. * @param {Cartesian2} [result] The object onto which to store the result.
  12874. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided.
  12875. */
  12876. Transforms.pointToWindowCoordinates = function (
  12877. modelViewProjectionMatrix,
  12878. viewportTransformation,
  12879. point,
  12880. result
  12881. ) {
  12882. result = Transforms.pointToGLWindowCoordinates(
  12883. modelViewProjectionMatrix,
  12884. viewportTransformation,
  12885. point,
  12886. result
  12887. );
  12888. result.y = 2.0 * viewportTransformation[5] - result.y;
  12889. return result;
  12890. };
  12891. /**
  12892. * @private
  12893. */
  12894. Transforms.pointToGLWindowCoordinates = function (
  12895. modelViewProjectionMatrix,
  12896. viewportTransformation,
  12897. point,
  12898. result
  12899. ) {
  12900. //>>includeStart('debug', pragmas.debug);
  12901. if (!defaultValue.defined(modelViewProjectionMatrix)) {
  12902. throw new Check.DeveloperError("modelViewProjectionMatrix is required.");
  12903. }
  12904. if (!defaultValue.defined(viewportTransformation)) {
  12905. throw new Check.DeveloperError("viewportTransformation is required.");
  12906. }
  12907. if (!defaultValue.defined(point)) {
  12908. throw new Check.DeveloperError("point is required.");
  12909. }
  12910. //>>includeEnd('debug');
  12911. if (!defaultValue.defined(result)) {
  12912. result = new Matrix2.Cartesian2();
  12913. }
  12914. const tmp = pointToWindowCoordinatesTemp;
  12915. Matrix2.Matrix4.multiplyByVector(
  12916. modelViewProjectionMatrix,
  12917. Matrix2.Cartesian4.fromElements(point.x, point.y, point.z, 1, tmp),
  12918. tmp
  12919. );
  12920. Matrix2.Cartesian4.multiplyByScalar(tmp, 1.0 / tmp.w, tmp);
  12921. Matrix2.Matrix4.multiplyByVector(viewportTransformation, tmp, tmp);
  12922. return Matrix2.Cartesian2.fromCartesian4(tmp, result);
  12923. };
  12924. const normalScratch = new Matrix3.Cartesian3();
  12925. const rightScratch = new Matrix3.Cartesian3();
  12926. const upScratch = new Matrix3.Cartesian3();
  12927. /**
  12928. * Transform a position and velocity to a rotation matrix.
  12929. *
  12930. * @param {Cartesian3} position The position to transform.
  12931. * @param {Cartesian3} velocity The velocity vector to transform.
  12932. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  12933. * @param {Matrix3} [result] The object onto which to store the result.
  12934. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if none was provided.
  12935. */
  12936. Transforms.rotationMatrixFromPositionVelocity = function (
  12937. position,
  12938. velocity,
  12939. ellipsoid,
  12940. result
  12941. ) {
  12942. //>>includeStart('debug', pragmas.debug);
  12943. if (!defaultValue.defined(position)) {
  12944. throw new Check.DeveloperError("position is required.");
  12945. }
  12946. if (!defaultValue.defined(velocity)) {
  12947. throw new Check.DeveloperError("velocity is required.");
  12948. }
  12949. //>>includeEnd('debug');
  12950. const normal = defaultValue.defaultValue(ellipsoid, Matrix3.Ellipsoid.WGS84).geodeticSurfaceNormal(
  12951. position,
  12952. normalScratch
  12953. );
  12954. let right = Matrix3.Cartesian3.cross(velocity, normal, rightScratch);
  12955. if (Matrix3.Cartesian3.equalsEpsilon(right, Matrix3.Cartesian3.ZERO, Math$1.CesiumMath.EPSILON6)) {
  12956. right = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.UNIT_X, right);
  12957. }
  12958. const up = Matrix3.Cartesian3.cross(right, velocity, upScratch);
  12959. Matrix3.Cartesian3.normalize(up, up);
  12960. Matrix3.Cartesian3.cross(velocity, up, right);
  12961. Matrix3.Cartesian3.negate(right, right);
  12962. Matrix3.Cartesian3.normalize(right, right);
  12963. if (!defaultValue.defined(result)) {
  12964. result = new Matrix3.Matrix3();
  12965. }
  12966. result[0] = velocity.x;
  12967. result[1] = velocity.y;
  12968. result[2] = velocity.z;
  12969. result[3] = right.x;
  12970. result[4] = right.y;
  12971. result[5] = right.z;
  12972. result[6] = up.x;
  12973. result[7] = up.y;
  12974. result[8] = up.z;
  12975. return result;
  12976. };
  12977. const swizzleMatrix = new Matrix2.Matrix4(
  12978. 0.0,
  12979. 0.0,
  12980. 1.0,
  12981. 0.0,
  12982. 1.0,
  12983. 0.0,
  12984. 0.0,
  12985. 0.0,
  12986. 0.0,
  12987. 1.0,
  12988. 0.0,
  12989. 0.0,
  12990. 0.0,
  12991. 0.0,
  12992. 0.0,
  12993. 1.0
  12994. );
  12995. const scratchCartographic = new Matrix3.Cartographic();
  12996. const scratchCartesian3Projection = new Matrix3.Cartesian3();
  12997. const scratchCenter = new Matrix3.Cartesian3();
  12998. const scratchRotation = new Matrix3.Matrix3();
  12999. const scratchFromENU = new Matrix2.Matrix4();
  13000. const scratchToENU = new Matrix2.Matrix4();
  13001. /**
  13002. * @private
  13003. */
  13004. Transforms.basisTo2D = function (projection, matrix, result) {
  13005. //>>includeStart('debug', pragmas.debug);
  13006. if (!defaultValue.defined(projection)) {
  13007. throw new Check.DeveloperError("projection is required.");
  13008. }
  13009. if (!defaultValue.defined(matrix)) {
  13010. throw new Check.DeveloperError("matrix is required.");
  13011. }
  13012. if (!defaultValue.defined(result)) {
  13013. throw new Check.DeveloperError("result is required.");
  13014. }
  13015. //>>includeEnd('debug');
  13016. const rtcCenter = Matrix2.Matrix4.getTranslation(matrix, scratchCenter);
  13017. const ellipsoid = projection.ellipsoid;
  13018. // Get the 2D Center
  13019. const cartographic = ellipsoid.cartesianToCartographic(
  13020. rtcCenter,
  13021. scratchCartographic
  13022. );
  13023. const projectedPosition = projection.project(
  13024. cartographic,
  13025. scratchCartesian3Projection
  13026. );
  13027. Matrix3.Cartesian3.fromElements(
  13028. projectedPosition.z,
  13029. projectedPosition.x,
  13030. projectedPosition.y,
  13031. projectedPosition
  13032. );
  13033. // Assuming the instance are positioned in WGS84, invert the WGS84 transform to get the local transform and then convert to 2D
  13034. const fromENU = Transforms.eastNorthUpToFixedFrame(
  13035. rtcCenter,
  13036. ellipsoid,
  13037. scratchFromENU
  13038. );
  13039. const toENU = Matrix2.Matrix4.inverseTransformation(fromENU, scratchToENU);
  13040. const rotation = Matrix2.Matrix4.getMatrix3(matrix, scratchRotation);
  13041. const local = Matrix2.Matrix4.multiplyByMatrix3(toENU, rotation, result);
  13042. Matrix2.Matrix4.multiply(swizzleMatrix, local, result); // Swap x, y, z for 2D
  13043. Matrix2.Matrix4.setTranslation(result, projectedPosition, result); // Use the projected center
  13044. return result;
  13045. };
  13046. /**
  13047. * @private
  13048. */
  13049. Transforms.wgs84To2DModelMatrix = function (projection, center, result) {
  13050. //>>includeStart('debug', pragmas.debug);
  13051. if (!defaultValue.defined(projection)) {
  13052. throw new Check.DeveloperError("projection is required.");
  13053. }
  13054. if (!defaultValue.defined(center)) {
  13055. throw new Check.DeveloperError("center is required.");
  13056. }
  13057. if (!defaultValue.defined(result)) {
  13058. throw new Check.DeveloperError("result is required.");
  13059. }
  13060. //>>includeEnd('debug');
  13061. const ellipsoid = projection.ellipsoid;
  13062. const fromENU = Transforms.eastNorthUpToFixedFrame(
  13063. center,
  13064. ellipsoid,
  13065. scratchFromENU
  13066. );
  13067. const toENU = Matrix2.Matrix4.inverseTransformation(fromENU, scratchToENU);
  13068. const cartographic = ellipsoid.cartesianToCartographic(
  13069. center,
  13070. scratchCartographic
  13071. );
  13072. const projectedPosition = projection.project(
  13073. cartographic,
  13074. scratchCartesian3Projection
  13075. );
  13076. Matrix3.Cartesian3.fromElements(
  13077. projectedPosition.z,
  13078. projectedPosition.x,
  13079. projectedPosition.y,
  13080. projectedPosition
  13081. );
  13082. const translation = Matrix2.Matrix4.fromTranslation(
  13083. projectedPosition,
  13084. scratchFromENU
  13085. );
  13086. Matrix2.Matrix4.multiply(swizzleMatrix, toENU, result);
  13087. Matrix2.Matrix4.multiply(translation, result, result);
  13088. return result;
  13089. };
  13090. var Transforms$1 = Transforms;
  13091. exports.BoundingSphere = BoundingSphere;
  13092. exports.FeatureDetection = FeatureDetection$1;
  13093. exports.GeographicProjection = GeographicProjection;
  13094. exports.Intersect = Intersect$1;
  13095. exports.Interval = Interval;
  13096. exports.Quaternion = Quaternion;
  13097. exports.Resource = Resource;
  13098. exports.Transforms = Transforms$1;
  13099. exports.buildModuleUrl = buildModuleUrl;
  13100. }));