createRectangleGeometry.js 872 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2016 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. (function () {
  24. /*global define*/
  25. define('Core/defined',[],function() {
  26. 'use strict';
  27. /**
  28. * @exports defined
  29. *
  30. * @param {Object} value The object.
  31. * @returns {Boolean} Returns true if the object is defined, returns false otherwise.
  32. *
  33. * @example
  34. * if (Cesium.defined(positions)) {
  35. * doSomething();
  36. * } else {
  37. * doSomethingElse();
  38. * }
  39. */
  40. function defined(value) {
  41. return value !== undefined && value !== null;
  42. }
  43. return defined;
  44. });
  45. /*global define*/
  46. define('Core/freezeObject',[
  47. './defined'
  48. ], function(
  49. defined) {
  50. 'use strict';
  51. /**
  52. * Freezes an object, using Object.freeze if available, otherwise returns
  53. * the object unchanged. This function should be used in setup code to prevent
  54. * errors from completely halting JavaScript execution in legacy browsers.
  55. *
  56. * @private
  57. *
  58. * @exports freezeObject
  59. */
  60. var freezeObject = Object.freeze;
  61. if (!defined(freezeObject)) {
  62. freezeObject = function(o) {
  63. return o;
  64. };
  65. }
  66. return freezeObject;
  67. });
  68. /*global define*/
  69. define('Core/defaultValue',[
  70. './freezeObject'
  71. ], function(
  72. freezeObject) {
  73. 'use strict';
  74. /**
  75. * Returns the first parameter if not undefined, otherwise the second parameter.
  76. * Useful for setting a default value for a parameter.
  77. *
  78. * @exports defaultValue
  79. *
  80. * @param {*} a
  81. * @param {*} b
  82. * @returns {*} Returns the first parameter if not undefined, otherwise the second parameter.
  83. *
  84. * @example
  85. * param = Cesium.defaultValue(param, 'default');
  86. */
  87. function defaultValue(a, b) {
  88. if (a !== undefined) {
  89. return a;
  90. }
  91. return b;
  92. }
  93. /**
  94. * A frozen empty object that can be used as the default value for options passed as
  95. * an object literal.
  96. */
  97. defaultValue.EMPTY_OBJECT = freezeObject({});
  98. return defaultValue;
  99. });
  100. /*global define*/
  101. define('Core/DeveloperError',[
  102. './defined'
  103. ], function(
  104. defined) {
  105. 'use strict';
  106. /**
  107. * Constructs an exception object that is thrown due to a developer error, e.g., invalid argument,
  108. * argument out of range, etc. This exception should only be thrown during development;
  109. * it usually indicates a bug in the calling code. This exception should never be
  110. * caught; instead the calling code should strive not to generate it.
  111. * <br /><br />
  112. * On the other hand, a {@link RuntimeError} indicates an exception that may
  113. * be thrown at runtime, e.g., out of memory, that the calling code should be prepared
  114. * to catch.
  115. *
  116. * @alias DeveloperError
  117. * @constructor
  118. * @extends Error
  119. *
  120. * @param {String} [message] The error message for this exception.
  121. *
  122. * @see RuntimeError
  123. */
  124. function DeveloperError(message) {
  125. /**
  126. * 'DeveloperError' indicating that this exception was thrown due to a developer error.
  127. * @type {String}
  128. * @readonly
  129. */
  130. this.name = 'DeveloperError';
  131. /**
  132. * The explanation for why this exception was thrown.
  133. * @type {String}
  134. * @readonly
  135. */
  136. this.message = message;
  137. //Browsers such as IE don't have a stack property until you actually throw the error.
  138. var stack;
  139. try {
  140. throw new Error();
  141. } catch (e) {
  142. stack = e.stack;
  143. }
  144. /**
  145. * The stack trace of this exception, if available.
  146. * @type {String}
  147. * @readonly
  148. */
  149. this.stack = stack;
  150. }
  151. if (defined(Object.create)) {
  152. DeveloperError.prototype = Object.create(Error.prototype);
  153. DeveloperError.prototype.constructor = DeveloperError;
  154. }
  155. DeveloperError.prototype.toString = function() {
  156. var str = this.name + ': ' + this.message;
  157. if (defined(this.stack)) {
  158. str += '\n' + this.stack.toString();
  159. }
  160. return str;
  161. };
  162. /**
  163. * @private
  164. */
  165. DeveloperError.throwInstantiationError = function() {
  166. throw new DeveloperError('This function defines an interface and should not be called directly.');
  167. };
  168. return DeveloperError;
  169. });
  170. /*global define*/
  171. define('Core/isArray',[
  172. './defined'
  173. ], function(
  174. defined) {
  175. 'use strict';
  176. /**
  177. * Tests an object to see if it is an array.
  178. * @exports isArray
  179. *
  180. * @param {Object} value The value to test.
  181. * @returns {Boolean} true if the value is an array, false otherwise.
  182. */
  183. var isArray = Array.isArray;
  184. if (!defined(isArray)) {
  185. isArray = function(value) {
  186. return Object.prototype.toString.call(value) === '[object Array]';
  187. };
  188. }
  189. return isArray;
  190. });
  191. /*global define*/
  192. define('Core/Check',[
  193. './defaultValue',
  194. './defined',
  195. './DeveloperError',
  196. './isArray'
  197. ], function(
  198. defaultValue,
  199. defined,
  200. DeveloperError,
  201. isArray) {
  202. 'use strict';
  203. /**
  204. * Contains functions for checking that supplied arguments are of a specified type
  205. * or meet specified conditions
  206. * @private
  207. */
  208. var Check = {};
  209. /**
  210. * Contains type checking functions, all using the typeof operator
  211. */
  212. Check.typeOf = {};
  213. /**
  214. * Contains functions for checking numeric conditions such as minimum and maximum values
  215. */
  216. Check.numeric = {};
  217. function getUndefinedErrorMessage(name) {
  218. return name + ' was required but undefined.';
  219. }
  220. function getFailedTypeErrorMessage(actual, expected, name) {
  221. return 'Expected ' + name + ' to be typeof ' + expected + ', got ' + actual;
  222. }
  223. /**
  224. * Throws if test is not defined
  225. *
  226. * @param {*} test The value that is to be checked
  227. * @param {String} name The name of the variable being tested
  228. * @exception {DeveloperError} test must be defined
  229. */
  230. Check.defined = function (test, name) {
  231. if (!defined(test)) {
  232. throw new DeveloperError(getUndefinedErrorMessage(name));
  233. }
  234. };
  235. /**
  236. * Throws if test is greater than maximum
  237. *
  238. * @param {Number} test The value to test
  239. * @param {Number} maximum The maximum allowed value
  240. * @exception {DeveloperError} test must not be greater than maximum
  241. * @exception {DeveloperError} Both test and maximum must be typeof 'number'
  242. */
  243. Check.numeric.maximum = function (test, maximum) {
  244. Check.typeOf.number(test);
  245. Check.typeOf.number(maximum);
  246. if (test > maximum) {
  247. throw new DeveloperError('Expected ' + test + ' to be at most ' + maximum);
  248. }
  249. };
  250. /**
  251. * Throws if test is less than minimum
  252. *
  253. * @param {Number} test The value to test
  254. * @param {Number} minimum The minimum allowed value
  255. * @exception {DeveloperError} test must not be less than mininum
  256. * @exception {DeveloperError} Both test and maximum must be typeof 'number'
  257. */
  258. Check.numeric.minimum = function (test, minimum) {
  259. Check.typeOf.number(test);
  260. Check.typeOf.number(minimum);
  261. if (test < minimum) {
  262. throw new DeveloperError('Expected ' + test + ' to be at least ' + minimum);
  263. }
  264. };
  265. /**
  266. * Throws if test is not typeof 'function'
  267. *
  268. * @param {*} test The value to test
  269. * @param {String} name The name of the variable being tested
  270. * @exception {DeveloperError} test must be typeof 'function'
  271. */
  272. Check.typeOf.function = function (test, name) {
  273. if (typeof test !== 'function') {
  274. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'function', name));
  275. }
  276. };
  277. /**
  278. * Throws if test is not typeof 'string'
  279. *
  280. * @param {*} test The value to test
  281. * @param {String} name The name of the variable being tested
  282. * @exception {DeveloperError} test must be typeof 'string'
  283. */
  284. Check.typeOf.string = function (test, name) {
  285. if (typeof test !== 'string') {
  286. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'string', name));
  287. }
  288. };
  289. /**
  290. * Throws if test is not typeof 'number'
  291. *
  292. * @param {*} test The value to test
  293. * @param {String} name The name of the variable being tested
  294. * @exception {DeveloperError} test must be typeof 'number'
  295. */
  296. Check.typeOf.number = function (test, name) {
  297. if (typeof test !== 'number') {
  298. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'number', name));
  299. }
  300. };
  301. /**
  302. * Throws if test is not typeof 'object'
  303. *
  304. * @param {*} test The value to test
  305. * @param {String} name The name of the variable being tested
  306. * @exception {DeveloperError} test must be typeof 'object'
  307. */
  308. Check.typeOf.object = function (test, name) {
  309. if (typeof test !== 'object') {
  310. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'object', name));
  311. }
  312. };
  313. /**
  314. * Throws if test is not typeof 'boolean'
  315. *
  316. * @param {*} test The value to test
  317. * @param {String} name The name of the variable being tested
  318. * @exception {DeveloperError} test must be typeof 'boolean'
  319. */
  320. Check.typeOf.boolean = function (test, name) {
  321. if (typeof test !== 'boolean') {
  322. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'boolean', name));
  323. }
  324. };
  325. return Check;
  326. });
  327. /*
  328. I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
  329. so it's better encapsulated. Now you can have multiple random number generators
  330. and they won't stomp all over eachother's state.
  331. If you want to use this as a substitute for Math.random(), use the random()
  332. method like so:
  333. var m = new MersenneTwister();
  334. var randomNumber = m.random();
  335. You can also call the other genrand_{foo}() methods on the instance.
  336. If you want to use a specific seed in order to get a repeatable random
  337. sequence, pass an integer into the constructor:
  338. var m = new MersenneTwister(123);
  339. and that will always produce the same random sequence.
  340. Sean McCullough (banksean@gmail.com)
  341. */
  342. /*
  343. A C-program for MT19937, with initialization improved 2002/1/26.
  344. Coded by Takuji Nishimura and Makoto Matsumoto.
  345. Before using, initialize the state by using init_genrand(seed)
  346. or init_by_array(init_key, key_length).
  347. */
  348. /**
  349. @license
  350. mersenne-twister.js - https://gist.github.com/banksean/300494
  351. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  352. All rights reserved.
  353. Redistribution and use in source and binary forms, with or without
  354. modification, are permitted provided that the following conditions
  355. are met:
  356. 1. Redistributions of source code must retain the above copyright
  357. notice, this list of conditions and the following disclaimer.
  358. 2. Redistributions in binary form must reproduce the above copyright
  359. notice, this list of conditions and the following disclaimer in the
  360. documentation and/or other materials provided with the distribution.
  361. 3. The names of its contributors may not be used to endorse or promote
  362. products derived from this software without specific prior written
  363. permission.
  364. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  365. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  366. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  367. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  368. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  369. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  370. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  371. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  372. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  373. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  374. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  375. */
  376. /*
  377. Any feedback is very welcome.
  378. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  379. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  380. */
  381. define('ThirdParty/mersenne-twister',[],function() {
  382. var MersenneTwister = function(seed) {
  383. if (seed == undefined) {
  384. seed = new Date().getTime();
  385. }
  386. /* Period parameters */
  387. this.N = 624;
  388. this.M = 397;
  389. this.MATRIX_A = 0x9908b0df; /* constant vector a */
  390. this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
  391. this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
  392. this.mt = new Array(this.N); /* the array for the state vector */
  393. this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */
  394. this.init_genrand(seed);
  395. }
  396. /* initializes mt[N] with a seed */
  397. MersenneTwister.prototype.init_genrand = function(s) {
  398. this.mt[0] = s >>> 0;
  399. for (this.mti=1; this.mti<this.N; this.mti++) {
  400. var s = this.mt[this.mti-1] ^ (this.mt[this.mti-1] >>> 30);
  401. this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)
  402. + this.mti;
  403. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  404. /* In the previous versions, MSBs of the seed affect */
  405. /* only MSBs of the array mt[]. */
  406. /* 2002/01/09 modified by Makoto Matsumoto */
  407. this.mt[this.mti] >>>= 0;
  408. /* for >32 bit machines */
  409. }
  410. }
  411. /* initialize by an array with array-length */
  412. /* init_key is the array for initializing keys */
  413. /* key_length is its length */
  414. /* slight change for C++, 2004/2/26 */
  415. //MersenneTwister.prototype.init_by_array = function(init_key, key_length) {
  416. // var i, j, k;
  417. // this.init_genrand(19650218);
  418. // i=1; j=0;
  419. // k = (this.N>key_length ? this.N : key_length);
  420. // for (; k; k--) {
  421. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30)
  422. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525)))
  423. // + init_key[j] + j; /* non linear */
  424. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  425. // i++; j++;
  426. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  427. // if (j>=key_length) j=0;
  428. // }
  429. // for (k=this.N-1; k; k--) {
  430. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
  431. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941))
  432. // - i; /* non linear */
  433. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  434. // i++;
  435. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  436. // }
  437. //
  438. // this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  439. //}
  440. /* generates a random number on [0,0xffffffff]-interval */
  441. MersenneTwister.prototype.genrand_int32 = function() {
  442. var y;
  443. var mag01 = new Array(0x0, this.MATRIX_A);
  444. /* mag01[x] = x * MATRIX_A for x=0,1 */
  445. if (this.mti >= this.N) { /* generate N words at one time */
  446. var kk;
  447. if (this.mti == this.N+1) /* if init_genrand() has not been called, */
  448. this.init_genrand(5489); /* a default initial seed is used */
  449. for (kk=0;kk<this.N-this.M;kk++) {
  450. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  451. this.mt[kk] = this.mt[kk+this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
  452. }
  453. for (;kk<this.N-1;kk++) {
  454. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  455. this.mt[kk] = this.mt[kk+(this.M-this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  456. }
  457. y = (this.mt[this.N-1]&this.UPPER_MASK)|(this.mt[0]&this.LOWER_MASK);
  458. this.mt[this.N-1] = this.mt[this.M-1] ^ (y >>> 1) ^ mag01[y & 0x1];
  459. this.mti = 0;
  460. }
  461. y = this.mt[this.mti++];
  462. /* Tempering */
  463. y ^= (y >>> 11);
  464. y ^= (y << 7) & 0x9d2c5680;
  465. y ^= (y << 15) & 0xefc60000;
  466. y ^= (y >>> 18);
  467. return y >>> 0;
  468. }
  469. /* generates a random number on [0,0x7fffffff]-interval */
  470. //MersenneTwister.prototype.genrand_int31 = function() {
  471. // return (this.genrand_int32()>>>1);
  472. //}
  473. /* generates a random number on [0,1]-real-interval */
  474. //MersenneTwister.prototype.genrand_real1 = function() {
  475. // return this.genrand_int32()*(1.0/4294967295.0);
  476. // /* divided by 2^32-1 */
  477. //}
  478. /* generates a random number on [0,1)-real-interval */
  479. MersenneTwister.prototype.random = function() {
  480. return this.genrand_int32()*(1.0/4294967296.0);
  481. /* divided by 2^32 */
  482. }
  483. /* generates a random number on (0,1)-real-interval */
  484. //MersenneTwister.prototype.genrand_real3 = function() {
  485. // return (this.genrand_int32() + 0.5)*(1.0/4294967296.0);
  486. // /* divided by 2^32 */
  487. //}
  488. /* generates a random number on [0,1) with 53-bit resolution*/
  489. //MersenneTwister.prototype.genrand_res53 = function() {
  490. // var a=this.genrand_int32()>>>5, b=this.genrand_int32()>>>6;
  491. // return(a*67108864.0+b)*(1.0/9007199254740992.0);
  492. //}
  493. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  494. return MersenneTwister;
  495. });
  496. /*global define*/
  497. define('Core/Math',[
  498. '../ThirdParty/mersenne-twister',
  499. './defaultValue',
  500. './defined',
  501. './DeveloperError'
  502. ], function(
  503. MersenneTwister,
  504. defaultValue,
  505. defined,
  506. DeveloperError) {
  507. 'use strict';
  508. /**
  509. * Math functions.
  510. *
  511. * @exports CesiumMath
  512. */
  513. var CesiumMath = {};
  514. /**
  515. * 0.1
  516. * @type {Number}
  517. * @constant
  518. */
  519. CesiumMath.EPSILON1 = 0.1;
  520. /**
  521. * 0.01
  522. * @type {Number}
  523. * @constant
  524. */
  525. CesiumMath.EPSILON2 = 0.01;
  526. /**
  527. * 0.001
  528. * @type {Number}
  529. * @constant
  530. */
  531. CesiumMath.EPSILON3 = 0.001;
  532. /**
  533. * 0.0001
  534. * @type {Number}
  535. * @constant
  536. */
  537. CesiumMath.EPSILON4 = 0.0001;
  538. /**
  539. * 0.00001
  540. * @type {Number}
  541. * @constant
  542. */
  543. CesiumMath.EPSILON5 = 0.00001;
  544. /**
  545. * 0.000001
  546. * @type {Number}
  547. * @constant
  548. */
  549. CesiumMath.EPSILON6 = 0.000001;
  550. /**
  551. * 0.0000001
  552. * @type {Number}
  553. * @constant
  554. */
  555. CesiumMath.EPSILON7 = 0.0000001;
  556. /**
  557. * 0.00000001
  558. * @type {Number}
  559. * @constant
  560. */
  561. CesiumMath.EPSILON8 = 0.00000001;
  562. /**
  563. * 0.000000001
  564. * @type {Number}
  565. * @constant
  566. */
  567. CesiumMath.EPSILON9 = 0.000000001;
  568. /**
  569. * 0.0000000001
  570. * @type {Number}
  571. * @constant
  572. */
  573. CesiumMath.EPSILON10 = 0.0000000001;
  574. /**
  575. * 0.00000000001
  576. * @type {Number}
  577. * @constant
  578. */
  579. CesiumMath.EPSILON11 = 0.00000000001;
  580. /**
  581. * 0.000000000001
  582. * @type {Number}
  583. * @constant
  584. */
  585. CesiumMath.EPSILON12 = 0.000000000001;
  586. /**
  587. * 0.0000000000001
  588. * @type {Number}
  589. * @constant
  590. */
  591. CesiumMath.EPSILON13 = 0.0000000000001;
  592. /**
  593. * 0.00000000000001
  594. * @type {Number}
  595. * @constant
  596. */
  597. CesiumMath.EPSILON14 = 0.00000000000001;
  598. /**
  599. * 0.000000000000001
  600. * @type {Number}
  601. * @constant
  602. */
  603. CesiumMath.EPSILON15 = 0.000000000000001;
  604. /**
  605. * 0.0000000000000001
  606. * @type {Number}
  607. * @constant
  608. */
  609. CesiumMath.EPSILON16 = 0.0000000000000001;
  610. /**
  611. * 0.00000000000000001
  612. * @type {Number}
  613. * @constant
  614. */
  615. CesiumMath.EPSILON17 = 0.00000000000000001;
  616. /**
  617. * 0.000000000000000001
  618. * @type {Number}
  619. * @constant
  620. */
  621. CesiumMath.EPSILON18 = 0.000000000000000001;
  622. /**
  623. * 0.0000000000000000001
  624. * @type {Number}
  625. * @constant
  626. */
  627. CesiumMath.EPSILON19 = 0.0000000000000000001;
  628. /**
  629. * 0.00000000000000000001
  630. * @type {Number}
  631. * @constant
  632. */
  633. CesiumMath.EPSILON20 = 0.00000000000000000001;
  634. /**
  635. * 3.986004418e14
  636. * @type {Number}
  637. * @constant
  638. */
  639. CesiumMath.GRAVITATIONALPARAMETER = 3.986004418e14;
  640. /**
  641. * Radius of the sun in meters: 6.955e8
  642. * @type {Number}
  643. * @constant
  644. */
  645. CesiumMath.SOLAR_RADIUS = 6.955e8;
  646. /**
  647. * The mean radius of the moon, according to the "Report of the IAU/IAG Working Group on
  648. * Cartographic Coordinates and Rotational Elements of the Planets and satellites: 2000",
  649. * Celestial Mechanics 82: 83-110, 2002.
  650. * @type {Number}
  651. * @constant
  652. */
  653. CesiumMath.LUNAR_RADIUS = 1737400.0;
  654. /**
  655. * 64 * 1024
  656. * @type {Number}
  657. * @constant
  658. */
  659. CesiumMath.SIXTY_FOUR_KILOBYTES = 64 * 1024;
  660. /**
  661. * Returns the sign of the value; 1 if the value is positive, -1 if the value is
  662. * negative, or 0 if the value is 0.
  663. *
  664. * @param {Number} value The value to return the sign of.
  665. * @returns {Number} The sign of value.
  666. */
  667. CesiumMath.sign = function(value) {
  668. if (value > 0) {
  669. return 1;
  670. }
  671. if (value < 0) {
  672. return -1;
  673. }
  674. return 0;
  675. };
  676. /**
  677. * Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
  678. * This is similar to {@link CesiumMath#sign} except that returns 1.0 instead of
  679. * 0.0 when the input value is 0.0.
  680. * @param {Number} value The value to return the sign of.
  681. * @returns {Number} The sign of value.
  682. */
  683. CesiumMath.signNotZero = function(value) {
  684. return value < 0.0 ? -1.0 : 1.0;
  685. };
  686. /**
  687. * Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMax]
  688. * @param {Number} value The scalar value in the range [-1.0, 1.0]
  689. * @param {Number} [rangeMax=255] The maximum value in the mapped range, 255 by default.
  690. * @returns {Number} A SNORM value, where 0 maps to -1.0 and rangeMax maps to 1.0.
  691. *
  692. * @see CesiumMath.fromSNorm
  693. */
  694. CesiumMath.toSNorm = function(value, rangeMax) {
  695. rangeMax = defaultValue(rangeMax, 255);
  696. return Math.round((CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMax);
  697. };
  698. /**
  699. * Converts a SNORM value in the range [0, rangeMax] to a scalar in the range [-1.0, 1.0].
  700. * @param {Number} value SNORM value in the range [0, 255]
  701. * @param {Number} [rangeMax=255] The maximum value in the SNORM range, 255 by default.
  702. * @returns {Number} Scalar in the range [-1.0, 1.0].
  703. *
  704. * @see CesiumMath.toSNorm
  705. */
  706. CesiumMath.fromSNorm = function(value, rangeMax) {
  707. rangeMax = defaultValue(rangeMax, 255);
  708. return CesiumMath.clamp(value, 0.0, rangeMax) / rangeMax * 2.0 - 1.0;
  709. };
  710. /**
  711. * Returns the hyperbolic sine of a number.
  712. * The hyperbolic sine of <em>value</em> is defined to be
  713. * (<em>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></em>)/2.0
  714. * where <i>e</i> is Euler's number, approximately 2.71828183.
  715. *
  716. * <p>Special cases:
  717. * <ul>
  718. * <li>If the argument is NaN, then the result is NaN.</li>
  719. *
  720. * <li>If the argument is infinite, then the result is an infinity
  721. * with the same sign as the argument.</li>
  722. *
  723. * <li>If the argument is zero, then the result is a zero with the
  724. * same sign as the argument.</li>
  725. * </ul>
  726. *</p>
  727. *
  728. * @param {Number} value The number whose hyperbolic sine is to be returned.
  729. * @returns {Number} The hyperbolic sine of <code>value</code>.
  730. */
  731. CesiumMath.sinh = function(value) {
  732. var part1 = Math.pow(Math.E, value);
  733. var part2 = Math.pow(Math.E, -1.0 * value);
  734. return (part1 - part2) * 0.5;
  735. };
  736. /**
  737. * Returns the hyperbolic cosine of a number.
  738. * The hyperbolic cosine of <strong>value</strong> is defined to be
  739. * (<em>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></em>)/2.0
  740. * where <i>e</i> is Euler's number, approximately 2.71828183.
  741. *
  742. * <p>Special cases:
  743. * <ul>
  744. * <li>If the argument is NaN, then the result is NaN.</li>
  745. *
  746. * <li>If the argument is infinite, then the result is positive infinity.</li>
  747. *
  748. * <li>If the argument is zero, then the result is 1.0.</li>
  749. * </ul>
  750. *</p>
  751. *
  752. * @param {Number} value The number whose hyperbolic cosine is to be returned.
  753. * @returns {Number} The hyperbolic cosine of <code>value</code>.
  754. */
  755. CesiumMath.cosh = function(value) {
  756. var part1 = Math.pow(Math.E, value);
  757. var part2 = Math.pow(Math.E, -1.0 * value);
  758. return (part1 + part2) * 0.5;
  759. };
  760. /**
  761. * Computes the linear interpolation of two values.
  762. *
  763. * @param {Number} p The start value to interpolate.
  764. * @param {Number} q The end value to interpolate.
  765. * @param {Number} time The time of interpolation generally in the range <code>[0.0, 1.0]</code>.
  766. * @returns {Number} The linearly interpolated value.
  767. *
  768. * @example
  769. * var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0
  770. */
  771. CesiumMath.lerp = function(p, q, time) {
  772. return ((1.0 - time) * p) + (time * q);
  773. };
  774. /**
  775. * pi
  776. *
  777. * @type {Number}
  778. * @constant
  779. */
  780. CesiumMath.PI = Math.PI;
  781. /**
  782. * 1/pi
  783. *
  784. * @type {Number}
  785. * @constant
  786. */
  787. CesiumMath.ONE_OVER_PI = 1.0 / Math.PI;
  788. /**
  789. * pi/2
  790. *
  791. * @type {Number}
  792. * @constant
  793. */
  794. CesiumMath.PI_OVER_TWO = Math.PI * 0.5;
  795. /**
  796. * pi/3
  797. *
  798. * @type {Number}
  799. * @constant
  800. */
  801. CesiumMath.PI_OVER_THREE = Math.PI / 3.0;
  802. /**
  803. * pi/4
  804. *
  805. * @type {Number}
  806. * @constant
  807. */
  808. CesiumMath.PI_OVER_FOUR = Math.PI / 4.0;
  809. /**
  810. * pi/6
  811. *
  812. * @type {Number}
  813. * @constant
  814. */
  815. CesiumMath.PI_OVER_SIX = Math.PI / 6.0;
  816. /**
  817. * 3pi/2
  818. *
  819. * @type {Number}
  820. * @constant
  821. */
  822. CesiumMath.THREE_PI_OVER_TWO = (3.0 * Math.PI) * 0.5;
  823. /**
  824. * 2pi
  825. *
  826. * @type {Number}
  827. * @constant
  828. */
  829. CesiumMath.TWO_PI = 2.0 * Math.PI;
  830. /**
  831. * 1/2pi
  832. *
  833. * @type {Number}
  834. * @constant
  835. */
  836. CesiumMath.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI);
  837. /**
  838. * The number of radians in a degree.
  839. *
  840. * @type {Number}
  841. * @constant
  842. * @default Math.PI / 180.0
  843. */
  844. CesiumMath.RADIANS_PER_DEGREE = Math.PI / 180.0;
  845. /**
  846. * The number of degrees in a radian.
  847. *
  848. * @type {Number}
  849. * @constant
  850. * @default 180.0 / Math.PI
  851. */
  852. CesiumMath.DEGREES_PER_RADIAN = 180.0 / Math.PI;
  853. /**
  854. * The number of radians in an arc second.
  855. *
  856. * @type {Number}
  857. * @constant
  858. * @default {@link CesiumMath.RADIANS_PER_DEGREE} / 3600.0
  859. */
  860. CesiumMath.RADIANS_PER_ARCSECOND = CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  861. /**
  862. * Converts degrees to radians.
  863. * @param {Number} degrees The angle to convert in degrees.
  864. * @returns {Number} The corresponding angle in radians.
  865. */
  866. CesiumMath.toRadians = function(degrees) {
  867. if (!defined(degrees)) {
  868. throw new DeveloperError('degrees is required.');
  869. }
  870. return degrees * CesiumMath.RADIANS_PER_DEGREE;
  871. };
  872. /**
  873. * Converts radians to degrees.
  874. * @param {Number} radians The angle to convert in radians.
  875. * @returns {Number} The corresponding angle in degrees.
  876. */
  877. CesiumMath.toDegrees = function(radians) {
  878. if (!defined(radians)) {
  879. throw new DeveloperError('radians is required.');
  880. }
  881. return radians * CesiumMath.DEGREES_PER_RADIAN;
  882. };
  883. /**
  884. * Converts a longitude value, in radians, to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  885. *
  886. * @param {Number} angle The longitude value, in radians, to convert to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  887. * @returns {Number} The equivalent longitude value in the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  888. *
  889. * @example
  890. * // Convert 270 degrees to -90 degrees longitude
  891. * var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));
  892. */
  893. CesiumMath.convertLongitudeRange = function(angle) {
  894. if (!defined(angle)) {
  895. throw new DeveloperError('angle is required.');
  896. }
  897. var twoPi = CesiumMath.TWO_PI;
  898. var simplified = angle - Math.floor(angle / twoPi) * twoPi;
  899. if (simplified < -Math.PI) {
  900. return simplified + twoPi;
  901. }
  902. if (simplified >= Math.PI) {
  903. return simplified - twoPi;
  904. }
  905. return simplified;
  906. };
  907. /**
  908. * Convenience function that clamps a latitude value, in radians, to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  909. * Useful for sanitizing data before use in objects requiring correct range.
  910. *
  911. * @param {Number} angle The latitude value, in radians, to clamp to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  912. * @returns {Number} The latitude value clamped to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  913. *
  914. * @example
  915. * // Clamp 108 degrees latitude to 90 degrees latitude
  916. * var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));
  917. */
  918. CesiumMath.clampToLatitudeRange = function(angle) {
  919. if (!defined(angle)) {
  920. throw new DeveloperError('angle is required.');
  921. }
  922. return CesiumMath.clamp(angle, -1*CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO);
  923. };
  924. /**
  925. * Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
  926. *
  927. * @param {Number} angle in radians
  928. * @returns {Number} The angle in the range [<code>-CesiumMath.PI</code>, <code>CesiumMath.PI</code>].
  929. */
  930. CesiumMath.negativePiToPi = function(x) {
  931. if (!defined(x)) {
  932. throw new DeveloperError('x is required.');
  933. }
  934. return CesiumMath.zeroToTwoPi(x + CesiumMath.PI) - CesiumMath.PI;
  935. };
  936. /**
  937. * Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
  938. *
  939. * @param {Number} angle in radians
  940. * @returns {Number} The angle in the range [0, <code>CesiumMath.TWO_PI</code>].
  941. */
  942. CesiumMath.zeroToTwoPi = function(x) {
  943. if (!defined(x)) {
  944. throw new DeveloperError('x is required.');
  945. }
  946. var mod = CesiumMath.mod(x, CesiumMath.TWO_PI);
  947. if (Math.abs(mod) < CesiumMath.EPSILON14 && Math.abs(x) > CesiumMath.EPSILON14) {
  948. return CesiumMath.TWO_PI;
  949. }
  950. return mod;
  951. };
  952. /**
  953. * The modulo operation that also works for negative dividends.
  954. *
  955. * @param {Number} m The dividend.
  956. * @param {Number} n The divisor.
  957. * @returns {Number} The remainder.
  958. */
  959. CesiumMath.mod = function(m, n) {
  960. if (!defined(m)) {
  961. throw new DeveloperError('m is required.');
  962. }
  963. if (!defined(n)) {
  964. throw new DeveloperError('n is required.');
  965. }
  966. return ((m % n) + n) % n;
  967. };
  968. /**
  969. * Determines if two values are equal using an absolute or relative tolerance test. This is useful
  970. * to avoid problems due to roundoff error when comparing floating-point values directly. The values are
  971. * first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed.
  972. * Use this test if you are unsure of the magnitudes of left and right.
  973. *
  974. * @param {Number} left The first value to compare.
  975. * @param {Number} right The other value to compare.
  976. * @param {Number} relativeEpsilon The maximum inclusive delta between <code>left</code> and <code>right</code> for the relative tolerance test.
  977. * @param {Number} [absoluteEpsilon=relativeEpsilon] The maximum inclusive delta between <code>left</code> and <code>right</code> for the absolute tolerance test.
  978. * @returns {Boolean} <code>true</code> if the values are equal within the epsilon; otherwise, <code>false</code>.
  979. *
  980. * @example
  981. * var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
  982. * var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2); // false
  983. * var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
  984. * var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false
  985. */
  986. CesiumMath.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  987. if (!defined(left)) {
  988. throw new DeveloperError('left is required.');
  989. }
  990. if (!defined(right)) {
  991. throw new DeveloperError('right is required.');
  992. }
  993. if (!defined(relativeEpsilon)) {
  994. throw new DeveloperError('relativeEpsilon is required.');
  995. }
  996. absoluteEpsilon = defaultValue(absoluteEpsilon, relativeEpsilon);
  997. var absDiff = Math.abs(left - right);
  998. return absDiff <= absoluteEpsilon || absDiff <= relativeEpsilon * Math.max(Math.abs(left), Math.abs(right));
  999. };
  1000. var factorials = [1];
  1001. /**
  1002. * Computes the factorial of the provided number.
  1003. *
  1004. * @param {Number} n The number whose factorial is to be computed.
  1005. * @returns {Number} The factorial of the provided number or undefined if the number is less than 0.
  1006. *
  1007. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  1008. *
  1009. *
  1010. * @example
  1011. * //Compute 7!, which is equal to 5040
  1012. * var computedFactorial = Cesium.Math.factorial(7);
  1013. *
  1014. * @see {@link http://en.wikipedia.org/wiki/Factorial|Factorial on Wikipedia}
  1015. */
  1016. CesiumMath.factorial = function(n) {
  1017. if (typeof n !== 'number' || n < 0) {
  1018. throw new DeveloperError('A number greater than or equal to 0 is required.');
  1019. }
  1020. var length = factorials.length;
  1021. if (n >= length) {
  1022. var sum = factorials[length - 1];
  1023. for (var i = length; i <= n; i++) {
  1024. factorials.push(sum * i);
  1025. }
  1026. }
  1027. return factorials[n];
  1028. };
  1029. /**
  1030. * Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
  1031. *
  1032. * @param {Number} [n] The number to be incremented.
  1033. * @param {Number} [maximumValue] The maximum incremented value before rolling over to the minimum value.
  1034. * @param {Number} [minimumValue=0.0] The number reset to after the maximum value has been exceeded.
  1035. * @returns {Number} The incremented number.
  1036. *
  1037. * @exception {DeveloperError} Maximum value must be greater than minimum value.
  1038. *
  1039. * @example
  1040. * var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
  1041. * var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0
  1042. */
  1043. CesiumMath.incrementWrap = function(n, maximumValue, minimumValue) {
  1044. minimumValue = defaultValue(minimumValue, 0.0);
  1045. if (!defined(n)) {
  1046. throw new DeveloperError('n is required.');
  1047. }
  1048. if (maximumValue <= minimumValue) {
  1049. throw new DeveloperError('maximumValue must be greater than minimumValue.');
  1050. }
  1051. ++n;
  1052. if (n > maximumValue) {
  1053. n = minimumValue;
  1054. }
  1055. return n;
  1056. };
  1057. /**
  1058. * Determines if a positive integer is a power of two.
  1059. *
  1060. * @param {Number} n The positive integer to test.
  1061. * @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
  1062. *
  1063. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  1064. *
  1065. * @example
  1066. * var t = Cesium.Math.isPowerOfTwo(16); // true
  1067. * var f = Cesium.Math.isPowerOfTwo(20); // false
  1068. */
  1069. CesiumMath.isPowerOfTwo = function(n) {
  1070. if (typeof n !== 'number' || n < 0) {
  1071. throw new DeveloperError('A number greater than or equal to 0 is required.');
  1072. }
  1073. return (n !== 0) && ((n & (n - 1)) === 0);
  1074. };
  1075. /**
  1076. * Computes the next power-of-two integer greater than or equal to the provided positive integer.
  1077. *
  1078. * @param {Number} n The positive integer to test.
  1079. * @returns {Number} The next power-of-two integer.
  1080. *
  1081. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  1082. *
  1083. * @example
  1084. * var n = Cesium.Math.nextPowerOfTwo(29); // 32
  1085. * var m = Cesium.Math.nextPowerOfTwo(32); // 32
  1086. */
  1087. CesiumMath.nextPowerOfTwo = function(n) {
  1088. if (typeof n !== 'number' || n < 0) {
  1089. throw new DeveloperError('A number greater than or equal to 0 is required.');
  1090. }
  1091. // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  1092. --n;
  1093. n |= n >> 1;
  1094. n |= n >> 2;
  1095. n |= n >> 4;
  1096. n |= n >> 8;
  1097. n |= n >> 16;
  1098. ++n;
  1099. return n;
  1100. };
  1101. /**
  1102. * Constraint a value to lie between two values.
  1103. *
  1104. * @param {Number} value The value to constrain.
  1105. * @param {Number} min The minimum value.
  1106. * @param {Number} max The maximum value.
  1107. * @returns {Number} The value clamped so that min <= value <= max.
  1108. */
  1109. CesiumMath.clamp = function(value, min, max) {
  1110. if (!defined(value)) {
  1111. throw new DeveloperError('value is required');
  1112. }
  1113. if (!defined(min)) {
  1114. throw new DeveloperError('min is required.');
  1115. }
  1116. if (!defined(max)) {
  1117. throw new DeveloperError('max is required.');
  1118. }
  1119. return value < min ? min : value > max ? max : value;
  1120. };
  1121. var randomNumberGenerator = new MersenneTwister();
  1122. /**
  1123. * Sets the seed used by the random number generator
  1124. * in {@link CesiumMath#nextRandomNumber}.
  1125. *
  1126. * @param {Number} seed An integer used as the seed.
  1127. */
  1128. CesiumMath.setRandomNumberSeed = function(seed) {
  1129. if (!defined(seed)) {
  1130. throw new DeveloperError('seed is required.');
  1131. }
  1132. randomNumberGenerator = new MersenneTwister(seed);
  1133. };
  1134. /**
  1135. * Generates a random number in the range of [0.0, 1.0)
  1136. * using a Mersenne twister.
  1137. *
  1138. * @returns {Number} A random number in the range of [0.0, 1.0).
  1139. *
  1140. * @see CesiumMath.setRandomNumberSeed
  1141. * @see {@link http://en.wikipedia.org/wiki/Mersenne_twister|Mersenne twister on Wikipedia}
  1142. */
  1143. CesiumMath.nextRandomNumber = function() {
  1144. return randomNumberGenerator.random();
  1145. };
  1146. /**
  1147. * Computes <code>Math.acos(value)</acode>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  1148. * so that the function will never return NaN.
  1149. *
  1150. * @param {Number} value The value for which to compute acos.
  1151. * @returns {Number} The acos of the value if the value is in the range [-1.0, 1.0], or the acos of -1.0 or 1.0,
  1152. * whichever is closer, if the value is outside the range.
  1153. */
  1154. CesiumMath.acosClamped = function(value) {
  1155. if (!defined(value)) {
  1156. throw new DeveloperError('value is required.');
  1157. }
  1158. return Math.acos(CesiumMath.clamp(value, -1.0, 1.0));
  1159. };
  1160. /**
  1161. * Computes <code>Math.asin(value)</acode>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  1162. * so that the function will never return NaN.
  1163. *
  1164. * @param {Number} value The value for which to compute asin.
  1165. * @returns {Number} The asin of the value if the value is in the range [-1.0, 1.0], or the asin of -1.0 or 1.0,
  1166. * whichever is closer, if the value is outside the range.
  1167. */
  1168. CesiumMath.asinClamped = function(value) {
  1169. if (!defined(value)) {
  1170. throw new DeveloperError('value is required.');
  1171. }
  1172. return Math.asin(CesiumMath.clamp(value, -1.0, 1.0));
  1173. };
  1174. /**
  1175. * Finds the chord length between two points given the circle's radius and the angle between the points.
  1176. *
  1177. * @param {Number} angle The angle between the two points.
  1178. * @param {Number} radius The radius of the circle.
  1179. * @returns {Number} The chord length.
  1180. */
  1181. CesiumMath.chordLength = function(angle, radius) {
  1182. if (!defined(angle)) {
  1183. throw new DeveloperError('angle is required.');
  1184. }
  1185. if (!defined(radius)) {
  1186. throw new DeveloperError('radius is required.');
  1187. }
  1188. return 2.0 * radius * Math.sin(angle * 0.5);
  1189. };
  1190. /**
  1191. * Finds the logarithm of a number to a base.
  1192. *
  1193. * @param {Number} number The number.
  1194. * @param {Number} base The base.
  1195. * @returns {Number} The result.
  1196. */
  1197. CesiumMath.logBase = function(number, base) {
  1198. if (!defined(number)) {
  1199. throw new DeveloperError('number is required.');
  1200. }
  1201. if (!defined(base)) {
  1202. throw new DeveloperError('base is required.');
  1203. }
  1204. return Math.log(number) / Math.log(base);
  1205. };
  1206. /**
  1207. * @private
  1208. */
  1209. CesiumMath.fog = function(distanceToCamera, density) {
  1210. var scalar = distanceToCamera * density;
  1211. return 1.0 - Math.exp(-(scalar * scalar));
  1212. };
  1213. return CesiumMath;
  1214. });
  1215. /*global define*/
  1216. define('Core/Cartesian3',[
  1217. './Check',
  1218. './defaultValue',
  1219. './defined',
  1220. './DeveloperError',
  1221. './freezeObject',
  1222. './Math'
  1223. ], function(
  1224. Check,
  1225. defaultValue,
  1226. defined,
  1227. DeveloperError,
  1228. freezeObject,
  1229. CesiumMath) {
  1230. 'use strict';
  1231. /**
  1232. * A 3D Cartesian point.
  1233. * @alias Cartesian3
  1234. * @constructor
  1235. *
  1236. * @param {Number} [x=0.0] The X component.
  1237. * @param {Number} [y=0.0] The Y component.
  1238. * @param {Number} [z=0.0] The Z component.
  1239. *
  1240. * @see Cartesian2
  1241. * @see Cartesian4
  1242. * @see Packable
  1243. */
  1244. function Cartesian3(x, y, z) {
  1245. /**
  1246. * The X component.
  1247. * @type {Number}
  1248. * @default 0.0
  1249. */
  1250. this.x = defaultValue(x, 0.0);
  1251. /**
  1252. * The Y component.
  1253. * @type {Number}
  1254. * @default 0.0
  1255. */
  1256. this.y = defaultValue(y, 0.0);
  1257. /**
  1258. * The Z component.
  1259. * @type {Number}
  1260. * @default 0.0
  1261. */
  1262. this.z = defaultValue(z, 0.0);
  1263. }
  1264. /**
  1265. * Converts the provided Spherical into Cartesian3 coordinates.
  1266. *
  1267. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  1268. * @param {Cartesian3} [result] The object onto which to store the result.
  1269. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1270. */
  1271. Cartesian3.fromSpherical = function(spherical, result) {
  1272. Check.typeOf.object(spherical, 'spherical');
  1273. if (!defined(result)) {
  1274. result = new Cartesian3();
  1275. }
  1276. var clock = spherical.clock;
  1277. var cone = spherical.cone;
  1278. var magnitude = defaultValue(spherical.magnitude, 1.0);
  1279. var radial = magnitude * Math.sin(cone);
  1280. result.x = radial * Math.cos(clock);
  1281. result.y = radial * Math.sin(clock);
  1282. result.z = magnitude * Math.cos(cone);
  1283. return result;
  1284. };
  1285. /**
  1286. * Creates a Cartesian3 instance from x, y and z coordinates.
  1287. *
  1288. * @param {Number} x The x coordinate.
  1289. * @param {Number} y The y coordinate.
  1290. * @param {Number} z The z coordinate.
  1291. * @param {Cartesian3} [result] The object onto which to store the result.
  1292. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1293. */
  1294. Cartesian3.fromElements = function(x, y, z, result) {
  1295. if (!defined(result)) {
  1296. return new Cartesian3(x, y, z);
  1297. }
  1298. result.x = x;
  1299. result.y = y;
  1300. result.z = z;
  1301. return result;
  1302. };
  1303. /**
  1304. * Duplicates a Cartesian3 instance.
  1305. *
  1306. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  1307. * @param {Cartesian3} [result] The object onto which to store the result.
  1308. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  1309. */
  1310. Cartesian3.clone = function(cartesian, result) {
  1311. if (!defined(cartesian)) {
  1312. return undefined;
  1313. }
  1314. if (!defined(result)) {
  1315. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  1316. }
  1317. result.x = cartesian.x;
  1318. result.y = cartesian.y;
  1319. result.z = cartesian.z;
  1320. return result;
  1321. };
  1322. /**
  1323. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  1324. * x, y, and z properties of the Cartesian4 and drops w.
  1325. * @function
  1326. *
  1327. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  1328. * @param {Cartesian3} [result] The object onto which to store the result.
  1329. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1330. */
  1331. Cartesian3.fromCartesian4 = Cartesian3.clone;
  1332. /**
  1333. * The number of elements used to pack the object into an array.
  1334. * @type {Number}
  1335. */
  1336. Cartesian3.packedLength = 3;
  1337. /**
  1338. * Stores the provided instance into the provided array.
  1339. *
  1340. * @param {Cartesian3} value The value to pack.
  1341. * @param {Number[]} array The array to pack into.
  1342. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1343. *
  1344. * @returns {Number[]} The array that was packed into
  1345. */
  1346. Cartesian3.pack = function(value, array, startingIndex) {
  1347. Check.typeOf.object(value, 'value');
  1348. Check.defined(array, 'array');
  1349. startingIndex = defaultValue(startingIndex, 0);
  1350. array[startingIndex++] = value.x;
  1351. array[startingIndex++] = value.y;
  1352. array[startingIndex] = value.z;
  1353. return array;
  1354. };
  1355. /**
  1356. * Retrieves an instance from a packed array.
  1357. *
  1358. * @param {Number[]} array The packed array.
  1359. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1360. * @param {Cartesian3} [result] The object into which to store the result.
  1361. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1362. */
  1363. Cartesian3.unpack = function(array, startingIndex, result) {
  1364. Check.defined(array, 'array');
  1365. startingIndex = defaultValue(startingIndex, 0);
  1366. if (!defined(result)) {
  1367. result = new Cartesian3();
  1368. }
  1369. result.x = array[startingIndex++];
  1370. result.y = array[startingIndex++];
  1371. result.z = array[startingIndex];
  1372. return result;
  1373. };
  1374. /**
  1375. * Flattens an array of Cartesian3s into an array of components.
  1376. *
  1377. * @param {Cartesian3[]} array The array of cartesians to pack.
  1378. * @param {Number[]} result The array onto which to store the result.
  1379. * @returns {Number[]} The packed array.
  1380. */
  1381. Cartesian3.packArray = function(array, result) {
  1382. Check.defined(array, 'array');
  1383. var length = array.length;
  1384. if (!defined(result)) {
  1385. result = new Array(length * 3);
  1386. } else {
  1387. result.length = length * 3;
  1388. }
  1389. for (var i = 0; i < length; ++i) {
  1390. Cartesian3.pack(array[i], result, i * 3);
  1391. }
  1392. return result;
  1393. };
  1394. /**
  1395. * Unpacks an array of cartesian components into an array of Cartesian3s.
  1396. *
  1397. * @param {Number[]} array The array of components to unpack.
  1398. * @param {Cartesian3[]} result The array onto which to store the result.
  1399. * @returns {Cartesian3[]} The unpacked array.
  1400. */
  1401. Cartesian3.unpackArray = function(array, result) {
  1402. Check.defined(array, 'array');
  1403. Check.numeric.minimum(array.length, 3);
  1404. if (array.length % 3 !== 0) {
  1405. throw new DeveloperError('array length must be a multiple of 3.');
  1406. }
  1407. var length = array.length;
  1408. if (!defined(result)) {
  1409. result = new Array(length / 3);
  1410. } else {
  1411. result.length = length / 3;
  1412. }
  1413. for (var i = 0; i < length; i += 3) {
  1414. var index = i / 3;
  1415. result[index] = Cartesian3.unpack(array, i, result[index]);
  1416. }
  1417. return result;
  1418. };
  1419. /**
  1420. * Creates a Cartesian3 from three consecutive elements in an array.
  1421. * @function
  1422. *
  1423. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  1424. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  1425. * @param {Cartesian3} [result] The object onto which to store the result.
  1426. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1427. *
  1428. * @example
  1429. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  1430. * var v = [1.0, 2.0, 3.0];
  1431. * var p = Cesium.Cartesian3.fromArray(v);
  1432. *
  1433. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  1434. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  1435. * var p2 = Cesium.Cartesian3.fromArray(v2, 2);
  1436. */
  1437. Cartesian3.fromArray = Cartesian3.unpack;
  1438. /**
  1439. * Computes the value of the maximum component for the supplied Cartesian.
  1440. *
  1441. * @param {Cartesian3} cartesian The cartesian to use.
  1442. * @returns {Number} The value of the maximum component.
  1443. */
  1444. Cartesian3.maximumComponent = function(cartesian) {
  1445. Check.typeOf.object(cartesian, 'cartesian');
  1446. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  1447. };
  1448. /**
  1449. * Computes the value of the minimum component for the supplied Cartesian.
  1450. *
  1451. * @param {Cartesian3} cartesian The cartesian to use.
  1452. * @returns {Number} The value of the minimum component.
  1453. */
  1454. Cartesian3.minimumComponent = function(cartesian) {
  1455. Check.typeOf.object(cartesian, 'cartesian');
  1456. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  1457. };
  1458. /**
  1459. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  1460. *
  1461. * @param {Cartesian3} first A cartesian to compare.
  1462. * @param {Cartesian3} second A cartesian to compare.
  1463. * @param {Cartesian3} result The object into which to store the result.
  1464. * @returns {Cartesian3} A cartesian with the minimum components.
  1465. */
  1466. Cartesian3.minimumByComponent = function(first, second, result) {
  1467. Check.typeOf.object(first, 'first');
  1468. Check.typeOf.object(second, 'second');
  1469. Check.typeOf.object(result, 'result');
  1470. result.x = Math.min(first.x, second.x);
  1471. result.y = Math.min(first.y, second.y);
  1472. result.z = Math.min(first.z, second.z);
  1473. return result;
  1474. };
  1475. /**
  1476. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  1477. *
  1478. * @param {Cartesian3} first A cartesian to compare.
  1479. * @param {Cartesian3} second A cartesian to compare.
  1480. * @param {Cartesian3} result The object into which to store the result.
  1481. * @returns {Cartesian3} A cartesian with the maximum components.
  1482. */
  1483. Cartesian3.maximumByComponent = function(first, second, result) {
  1484. Check.typeOf.object(first, 'first');
  1485. Check.typeOf.object(second, 'second');
  1486. Check.typeOf.object(result, 'result');
  1487. result.x = Math.max(first.x, second.x);
  1488. result.y = Math.max(first.y, second.y);
  1489. result.z = Math.max(first.z, second.z);
  1490. return result;
  1491. };
  1492. /**
  1493. * Computes the provided Cartesian's squared magnitude.
  1494. *
  1495. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  1496. * @returns {Number} The squared magnitude.
  1497. */
  1498. Cartesian3.magnitudeSquared = function(cartesian) {
  1499. Check.typeOf.object(cartesian, 'cartesian');
  1500. return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z;
  1501. };
  1502. /**
  1503. * Computes the Cartesian's magnitude (length).
  1504. *
  1505. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  1506. * @returns {Number} The magnitude.
  1507. */
  1508. Cartesian3.magnitude = function(cartesian) {
  1509. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  1510. };
  1511. var distanceScratch = new Cartesian3();
  1512. /**
  1513. * Computes the distance between two points.
  1514. *
  1515. * @param {Cartesian3} left The first point to compute the distance from.
  1516. * @param {Cartesian3} right The second point to compute the distance to.
  1517. * @returns {Number} The distance between two points.
  1518. *
  1519. * @example
  1520. * // Returns 1.0
  1521. * var d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  1522. */
  1523. Cartesian3.distance = function(left, right) {
  1524. Check.typeOf.object(left, 'left');
  1525. Check.typeOf.object(right, 'right');
  1526. Cartesian3.subtract(left, right, distanceScratch);
  1527. return Cartesian3.magnitude(distanceScratch);
  1528. };
  1529. /**
  1530. * Computes the squared distance between two points. Comparing squared distances
  1531. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  1532. *
  1533. * @param {Cartesian3} left The first point to compute the distance from.
  1534. * @param {Cartesian3} right The second point to compute the distance to.
  1535. * @returns {Number} The distance between two points.
  1536. *
  1537. * @example
  1538. * // Returns 4.0, not 2.0
  1539. * var d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  1540. */
  1541. Cartesian3.distanceSquared = function(left, right) {
  1542. Check.typeOf.object(left, 'left');
  1543. Check.typeOf.object(right, 'right');
  1544. Cartesian3.subtract(left, right, distanceScratch);
  1545. return Cartesian3.magnitudeSquared(distanceScratch);
  1546. };
  1547. /**
  1548. * Computes the normalized form of the supplied Cartesian.
  1549. *
  1550. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  1551. * @param {Cartesian3} result The object onto which to store the result.
  1552. * @returns {Cartesian3} The modified result parameter.
  1553. */
  1554. Cartesian3.normalize = function(cartesian, result) {
  1555. Check.typeOf.object(cartesian, 'cartesian');
  1556. Check.typeOf.object(result, 'result');
  1557. var magnitude = Cartesian3.magnitude(cartesian);
  1558. result.x = cartesian.x / magnitude;
  1559. result.y = cartesian.y / magnitude;
  1560. result.z = cartesian.z / magnitude;
  1561. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  1562. throw new DeveloperError('normalized result is not a number');
  1563. }
  1564. return result;
  1565. };
  1566. /**
  1567. * Computes the dot (scalar) product of two Cartesians.
  1568. *
  1569. * @param {Cartesian3} left The first Cartesian.
  1570. * @param {Cartesian3} right The second Cartesian.
  1571. * @returns {Number} The dot product.
  1572. */
  1573. Cartesian3.dot = function(left, right) {
  1574. Check.typeOf.object(left, 'left');
  1575. Check.typeOf.object(right, 'right');
  1576. return left.x * right.x + left.y * right.y + left.z * right.z;
  1577. };
  1578. /**
  1579. * Computes the componentwise product of two Cartesians.
  1580. *
  1581. * @param {Cartesian3} left The first Cartesian.
  1582. * @param {Cartesian3} right The second Cartesian.
  1583. * @param {Cartesian3} result The object onto which to store the result.
  1584. * @returns {Cartesian3} The modified result parameter.
  1585. */
  1586. Cartesian3.multiplyComponents = function(left, right, result) {
  1587. Check.typeOf.object(left, 'left');
  1588. Check.typeOf.object(right, 'right');
  1589. Check.typeOf.object(result, 'result');
  1590. result.x = left.x * right.x;
  1591. result.y = left.y * right.y;
  1592. result.z = left.z * right.z;
  1593. return result;
  1594. };
  1595. /**
  1596. * Computes the componentwise quotient of two Cartesians.
  1597. *
  1598. * @param {Cartesian3} left The first Cartesian.
  1599. * @param {Cartesian3} right The second Cartesian.
  1600. * @param {Cartesian3} result The object onto which to store the result.
  1601. * @returns {Cartesian3} The modified result parameter.
  1602. */
  1603. Cartesian3.divideComponents = function(left, right, result) {
  1604. if (!defined(left)) {
  1605. throw new DeveloperError('left is required');
  1606. }
  1607. if (!defined(right)) {
  1608. throw new DeveloperError('right is required');
  1609. }
  1610. if (!defined(result)) {
  1611. throw new DeveloperError('result is required');
  1612. }
  1613. result.x = left.x / right.x;
  1614. result.y = left.y / right.y;
  1615. result.z = left.z / right.z;
  1616. return result;
  1617. };
  1618. /**
  1619. * Computes the componentwise sum of two Cartesians.
  1620. *
  1621. * @param {Cartesian3} left The first Cartesian.
  1622. * @param {Cartesian3} right The second Cartesian.
  1623. * @param {Cartesian3} result The object onto which to store the result.
  1624. * @returns {Cartesian3} The modified result parameter.
  1625. */
  1626. Cartesian3.add = function(left, right, result) {
  1627. Check.typeOf.object(left, 'left');
  1628. Check.typeOf.object(right, 'right');
  1629. Check.typeOf.object(result, 'result');
  1630. result.x = left.x + right.x;
  1631. result.y = left.y + right.y;
  1632. result.z = left.z + right.z;
  1633. return result;
  1634. };
  1635. /**
  1636. * Computes the componentwise difference of two Cartesians.
  1637. *
  1638. * @param {Cartesian3} left The first Cartesian.
  1639. * @param {Cartesian3} right The second Cartesian.
  1640. * @param {Cartesian3} result The object onto which to store the result.
  1641. * @returns {Cartesian3} The modified result parameter.
  1642. */
  1643. Cartesian3.subtract = function(left, right, result) {
  1644. Check.typeOf.object(left, 'left');
  1645. Check.typeOf.object(right, 'right');
  1646. Check.typeOf.object(result, 'result');
  1647. result.x = left.x - right.x;
  1648. result.y = left.y - right.y;
  1649. result.z = left.z - right.z;
  1650. return result;
  1651. };
  1652. /**
  1653. * Multiplies the provided Cartesian componentwise by the provided scalar.
  1654. *
  1655. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  1656. * @param {Number} scalar The scalar to multiply with.
  1657. * @param {Cartesian3} result The object onto which to store the result.
  1658. * @returns {Cartesian3} The modified result parameter.
  1659. */
  1660. Cartesian3.multiplyByScalar = function(cartesian, scalar, result) {
  1661. Check.typeOf.object(cartesian, 'cartesian');
  1662. Check.typeOf.number(scalar, 'scalar');
  1663. Check.typeOf.object(result, 'result');
  1664. result.x = cartesian.x * scalar;
  1665. result.y = cartesian.y * scalar;
  1666. result.z = cartesian.z * scalar;
  1667. return result;
  1668. };
  1669. /**
  1670. * Divides the provided Cartesian componentwise by the provided scalar.
  1671. *
  1672. * @param {Cartesian3} cartesian The Cartesian to be divided.
  1673. * @param {Number} scalar The scalar to divide by.
  1674. * @param {Cartesian3} result The object onto which to store the result.
  1675. * @returns {Cartesian3} The modified result parameter.
  1676. */
  1677. Cartesian3.divideByScalar = function(cartesian, scalar, result) {
  1678. Check.typeOf.object(cartesian, 'cartesian');
  1679. Check.typeOf.number(scalar, 'scalar');
  1680. Check.typeOf.object(result, 'result');
  1681. result.x = cartesian.x / scalar;
  1682. result.y = cartesian.y / scalar;
  1683. result.z = cartesian.z / scalar;
  1684. return result;
  1685. };
  1686. /**
  1687. * Negates the provided Cartesian.
  1688. *
  1689. * @param {Cartesian3} cartesian The Cartesian to be negated.
  1690. * @param {Cartesian3} result The object onto which to store the result.
  1691. * @returns {Cartesian3} The modified result parameter.
  1692. */
  1693. Cartesian3.negate = function(cartesian, result) {
  1694. Check.typeOf.object(cartesian, 'cartesian');
  1695. Check.typeOf.object(result, 'result');
  1696. result.x = -cartesian.x;
  1697. result.y = -cartesian.y;
  1698. result.z = -cartesian.z;
  1699. return result;
  1700. };
  1701. /**
  1702. * Computes the absolute value of the provided Cartesian.
  1703. *
  1704. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  1705. * @param {Cartesian3} result The object onto which to store the result.
  1706. * @returns {Cartesian3} The modified result parameter.
  1707. */
  1708. Cartesian3.abs = function(cartesian, result) {
  1709. Check.typeOf.object(cartesian, 'cartesian');
  1710. Check.typeOf.object(result, 'result');
  1711. result.x = Math.abs(cartesian.x);
  1712. result.y = Math.abs(cartesian.y);
  1713. result.z = Math.abs(cartesian.z);
  1714. return result;
  1715. };
  1716. var lerpScratch = new Cartesian3();
  1717. /**
  1718. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  1719. *
  1720. * @param {Cartesian3} start The value corresponding to t at 0.0.
  1721. * @param {Cartesian3} end The value corresponding to t at 1.0.
  1722. * @param {Number} t The point along t at which to interpolate.
  1723. * @param {Cartesian3} result The object onto which to store the result.
  1724. * @returns {Cartesian3} The modified result parameter.
  1725. */
  1726. Cartesian3.lerp = function(start, end, t, result) {
  1727. Check.typeOf.object(start, 'start');
  1728. Check.typeOf.object(end, 'end');
  1729. Check.typeOf.number(t, 't');
  1730. Check.typeOf.object(result, 'result');
  1731. Cartesian3.multiplyByScalar(end, t, lerpScratch);
  1732. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  1733. return Cartesian3.add(lerpScratch, result, result);
  1734. };
  1735. var angleBetweenScratch = new Cartesian3();
  1736. var angleBetweenScratch2 = new Cartesian3();
  1737. /**
  1738. * Returns the angle, in radians, between the provided Cartesians.
  1739. *
  1740. * @param {Cartesian3} left The first Cartesian.
  1741. * @param {Cartesian3} right The second Cartesian.
  1742. * @returns {Number} The angle between the Cartesians.
  1743. */
  1744. Cartesian3.angleBetween = function(left, right) {
  1745. Check.typeOf.object(left, 'left');
  1746. Check.typeOf.object(right, 'right');
  1747. Cartesian3.normalize(left, angleBetweenScratch);
  1748. Cartesian3.normalize(right, angleBetweenScratch2);
  1749. var cosine = Cartesian3.dot(angleBetweenScratch, angleBetweenScratch2);
  1750. var sine = Cartesian3.magnitude(Cartesian3.cross(angleBetweenScratch, angleBetweenScratch2, angleBetweenScratch));
  1751. return Math.atan2(sine, cosine);
  1752. };
  1753. var mostOrthogonalAxisScratch = new Cartesian3();
  1754. /**
  1755. * Returns the axis that is most orthogonal to the provided Cartesian.
  1756. *
  1757. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  1758. * @param {Cartesian3} result The object onto which to store the result.
  1759. * @returns {Cartesian3} The most orthogonal axis.
  1760. */
  1761. Cartesian3.mostOrthogonalAxis = function(cartesian, result) {
  1762. Check.typeOf.object(cartesian, 'cartesian');
  1763. Check.typeOf.object(result, 'result');
  1764. var f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch);
  1765. Cartesian3.abs(f, f);
  1766. if (f.x <= f.y) {
  1767. if (f.x <= f.z) {
  1768. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  1769. } else {
  1770. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  1771. }
  1772. } else {
  1773. if (f.y <= f.z) {
  1774. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  1775. } else {
  1776. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  1777. }
  1778. }
  1779. return result;
  1780. };
  1781. /**
  1782. * Compares the provided Cartesians componentwise and returns
  1783. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1784. *
  1785. * @param {Cartesian3} [left] The first Cartesian.
  1786. * @param {Cartesian3} [right] The second Cartesian.
  1787. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1788. */
  1789. Cartesian3.equals = function(left, right) {
  1790. return (left === right) ||
  1791. ((defined(left)) &&
  1792. (defined(right)) &&
  1793. (left.x === right.x) &&
  1794. (left.y === right.y) &&
  1795. (left.z === right.z));
  1796. };
  1797. /**
  1798. * @private
  1799. */
  1800. Cartesian3.equalsArray = function(cartesian, array, offset) {
  1801. return cartesian.x === array[offset] &&
  1802. cartesian.y === array[offset + 1] &&
  1803. cartesian.z === array[offset + 2];
  1804. };
  1805. /**
  1806. * Compares the provided Cartesians componentwise and returns
  1807. * <code>true</code> if they pass an absolute or relative tolerance test,
  1808. * <code>false</code> otherwise.
  1809. *
  1810. * @param {Cartesian3} [left] The first Cartesian.
  1811. * @param {Cartesian3} [right] The second Cartesian.
  1812. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1813. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1814. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1815. */
  1816. Cartesian3.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  1817. return (left === right) ||
  1818. (defined(left) &&
  1819. defined(right) &&
  1820. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  1821. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon) &&
  1822. CesiumMath.equalsEpsilon(left.z, right.z, relativeEpsilon, absoluteEpsilon));
  1823. };
  1824. /**
  1825. * Computes the cross (outer) product of two Cartesians.
  1826. *
  1827. * @param {Cartesian3} left The first Cartesian.
  1828. * @param {Cartesian3} right The second Cartesian.
  1829. * @param {Cartesian3} result The object onto which to store the result.
  1830. * @returns {Cartesian3} The cross product.
  1831. */
  1832. Cartesian3.cross = function(left, right, result) {
  1833. Check.typeOf.object(left, 'left');
  1834. Check.typeOf.object(right, 'right');
  1835. Check.typeOf.object(result, 'result');
  1836. var leftX = left.x;
  1837. var leftY = left.y;
  1838. var leftZ = left.z;
  1839. var rightX = right.x;
  1840. var rightY = right.y;
  1841. var rightZ = right.z;
  1842. var x = leftY * rightZ - leftZ * rightY;
  1843. var y = leftZ * rightX - leftX * rightZ;
  1844. var z = leftX * rightY - leftY * rightX;
  1845. result.x = x;
  1846. result.y = y;
  1847. result.z = z;
  1848. return result;
  1849. };
  1850. /**
  1851. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  1852. *
  1853. * @param {Number} longitude The longitude, in degrees
  1854. * @param {Number} latitude The latitude, in degrees
  1855. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1856. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1857. * @param {Cartesian3} [result] The object onto which to store the result.
  1858. * @returns {Cartesian3} The position
  1859. *
  1860. * @example
  1861. * var position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  1862. */
  1863. Cartesian3.fromDegrees = function(longitude, latitude, height, ellipsoid, result) {
  1864. Check.typeOf.number(longitude, 'longitude');
  1865. Check.typeOf.number(latitude, 'latitude');
  1866. longitude = CesiumMath.toRadians(longitude);
  1867. latitude = CesiumMath.toRadians(latitude);
  1868. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  1869. };
  1870. var scratchN = new Cartesian3();
  1871. var scratchK = new Cartesian3();
  1872. var wgs84RadiiSquared = new Cartesian3(6378137.0 * 6378137.0, 6378137.0 * 6378137.0, 6356752.3142451793 * 6356752.3142451793);
  1873. /**
  1874. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  1875. *
  1876. * @param {Number} longitude The longitude, in radians
  1877. * @param {Number} latitude The latitude, in radians
  1878. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1879. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1880. * @param {Cartesian3} [result] The object onto which to store the result.
  1881. * @returns {Cartesian3} The position
  1882. *
  1883. * @example
  1884. * var position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  1885. */
  1886. Cartesian3.fromRadians = function(longitude, latitude, height, ellipsoid, result) {
  1887. Check.typeOf.number(longitude, 'longitude');
  1888. Check.typeOf.number(latitude, 'latitude');
  1889. height = defaultValue(height, 0.0);
  1890. var radiiSquared = defined(ellipsoid) ? ellipsoid.radiiSquared : wgs84RadiiSquared;
  1891. var cosLatitude = Math.cos(latitude);
  1892. scratchN.x = cosLatitude * Math.cos(longitude);
  1893. scratchN.y = cosLatitude * Math.sin(longitude);
  1894. scratchN.z = Math.sin(latitude);
  1895. scratchN = Cartesian3.normalize(scratchN, scratchN);
  1896. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  1897. var gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  1898. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  1899. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  1900. if (!defined(result)) {
  1901. result = new Cartesian3();
  1902. }
  1903. return Cartesian3.add(scratchK, scratchN, result);
  1904. };
  1905. /**
  1906. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  1907. *
  1908. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  1909. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  1910. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  1911. * @returns {Cartesian3[]} The array of positions.
  1912. *
  1913. * @example
  1914. * var positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  1915. */
  1916. Cartesian3.fromDegreesArray = function(coordinates, ellipsoid, result) {
  1917. Check.defined(coordinates, 'coordinates');
  1918. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  1919. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  1920. }
  1921. var length = coordinates.length;
  1922. if (!defined(result)) {
  1923. result = new Array(length / 2);
  1924. } else {
  1925. result.length = length / 2;
  1926. }
  1927. for (var i = 0; i < length; i += 2) {
  1928. var longitude = coordinates[i];
  1929. var latitude = coordinates[i + 1];
  1930. var index = i / 2;
  1931. result[index] = Cartesian3.fromDegrees(longitude, latitude, 0, ellipsoid, result[index]);
  1932. }
  1933. return result;
  1934. };
  1935. /**
  1936. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  1937. *
  1938. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  1939. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  1940. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  1941. * @returns {Cartesian3[]} The array of positions.
  1942. *
  1943. * @example
  1944. * var positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  1945. */
  1946. Cartesian3.fromRadiansArray = function(coordinates, ellipsoid, result) {
  1947. Check.defined(coordinates, 'coordinates');
  1948. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  1949. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  1950. }
  1951. var length = coordinates.length;
  1952. if (!defined(result)) {
  1953. result = new Array(length / 2);
  1954. } else {
  1955. result.length = length / 2;
  1956. }
  1957. for (var i = 0; i < length; i += 2) {
  1958. var longitude = coordinates[i];
  1959. var latitude = coordinates[i + 1];
  1960. var index = i / 2;
  1961. result[index] = Cartesian3.fromRadians(longitude, latitude, 0, ellipsoid, result[index]);
  1962. }
  1963. return result;
  1964. };
  1965. /**
  1966. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  1967. *
  1968. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  1969. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1970. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  1971. * @returns {Cartesian3[]} The array of positions.
  1972. *
  1973. * @example
  1974. * var positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  1975. */
  1976. Cartesian3.fromDegreesArrayHeights = function(coordinates, ellipsoid, result) {
  1977. Check.defined(coordinates, 'coordinates');
  1978. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  1979. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  1980. }
  1981. var length = coordinates.length;
  1982. if (!defined(result)) {
  1983. result = new Array(length / 3);
  1984. } else {
  1985. result.length = length / 3;
  1986. }
  1987. for (var i = 0; i < length; i += 3) {
  1988. var longitude = coordinates[i];
  1989. var latitude = coordinates[i + 1];
  1990. var height = coordinates[i + 2];
  1991. var index = i / 3;
  1992. result[index] = Cartesian3.fromDegrees(longitude, latitude, height, ellipsoid, result[index]);
  1993. }
  1994. return result;
  1995. };
  1996. /**
  1997. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  1998. *
  1999. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  2000. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2001. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  2002. * @returns {Cartesian3[]} The array of positions.
  2003. *
  2004. * @example
  2005. * var positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  2006. */
  2007. Cartesian3.fromRadiansArrayHeights = function(coordinates, ellipsoid, result) {
  2008. Check.defined(coordinates, 'coordinates');
  2009. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  2010. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  2011. }
  2012. var length = coordinates.length;
  2013. if (!defined(result)) {
  2014. result = new Array(length / 3);
  2015. } else {
  2016. result.length = length / 3;
  2017. }
  2018. for (var i = 0; i < length; i += 3) {
  2019. var longitude = coordinates[i];
  2020. var latitude = coordinates[i + 1];
  2021. var height = coordinates[i + 2];
  2022. var index = i / 3;
  2023. result[index] = Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result[index]);
  2024. }
  2025. return result;
  2026. };
  2027. /**
  2028. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  2029. *
  2030. * @type {Cartesian3}
  2031. * @constant
  2032. */
  2033. Cartesian3.ZERO = freezeObject(new Cartesian3(0.0, 0.0, 0.0));
  2034. /**
  2035. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  2036. *
  2037. * @type {Cartesian3}
  2038. * @constant
  2039. */
  2040. Cartesian3.UNIT_X = freezeObject(new Cartesian3(1.0, 0.0, 0.0));
  2041. /**
  2042. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  2043. *
  2044. * @type {Cartesian3}
  2045. * @constant
  2046. */
  2047. Cartesian3.UNIT_Y = freezeObject(new Cartesian3(0.0, 1.0, 0.0));
  2048. /**
  2049. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  2050. *
  2051. * @type {Cartesian3}
  2052. * @constant
  2053. */
  2054. Cartesian3.UNIT_Z = freezeObject(new Cartesian3(0.0, 0.0, 1.0));
  2055. /**
  2056. * Duplicates this Cartesian3 instance.
  2057. *
  2058. * @param {Cartesian3} [result] The object onto which to store the result.
  2059. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  2060. */
  2061. Cartesian3.prototype.clone = function(result) {
  2062. return Cartesian3.clone(this, result);
  2063. };
  2064. /**
  2065. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2066. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2067. *
  2068. * @param {Cartesian3} [right] The right hand side Cartesian.
  2069. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2070. */
  2071. Cartesian3.prototype.equals = function(right) {
  2072. return Cartesian3.equals(this, right);
  2073. };
  2074. /**
  2075. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2076. * <code>true</code> if they pass an absolute or relative tolerance test,
  2077. * <code>false</code> otherwise.
  2078. *
  2079. * @param {Cartesian3} [right] The right hand side Cartesian.
  2080. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  2081. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  2082. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  2083. */
  2084. Cartesian3.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  2085. return Cartesian3.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  2086. };
  2087. /**
  2088. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  2089. *
  2090. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  2091. */
  2092. Cartesian3.prototype.toString = function() {
  2093. return '(' + this.x + ', ' + this.y + ', ' + this.z + ')';
  2094. };
  2095. return Cartesian3;
  2096. });
  2097. /*global define*/
  2098. define('Core/scaleToGeodeticSurface',[
  2099. './Cartesian3',
  2100. './defined',
  2101. './DeveloperError',
  2102. './Math'
  2103. ], function(
  2104. Cartesian3,
  2105. defined,
  2106. DeveloperError,
  2107. CesiumMath) {
  2108. 'use strict';
  2109. var scaleToGeodeticSurfaceIntersection = new Cartesian3();
  2110. var scaleToGeodeticSurfaceGradient = new Cartesian3();
  2111. /**
  2112. * Scales the provided Cartesian position along the geodetic surface normal
  2113. * so that it is on the surface of this ellipsoid. If the position is
  2114. * at the center of the ellipsoid, this function returns undefined.
  2115. *
  2116. * @param {Cartesian3} cartesian The Cartesian position to scale.
  2117. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  2118. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  2119. * @param {Number} centerToleranceSquared Tolerance for closeness to the center.
  2120. * @param {Cartesian3} [result] The object onto which to store the result.
  2121. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  2122. *
  2123. * @exports scaleToGeodeticSurface
  2124. *
  2125. * @private
  2126. */
  2127. function scaleToGeodeticSurface(cartesian, oneOverRadii, oneOverRadiiSquared, centerToleranceSquared, result) {
  2128. if (!defined(cartesian)) {
  2129. throw new DeveloperError('cartesian is required.');
  2130. }
  2131. if (!defined(oneOverRadii)) {
  2132. throw new DeveloperError('oneOverRadii is required.');
  2133. }
  2134. if (!defined(oneOverRadiiSquared)) {
  2135. throw new DeveloperError('oneOverRadiiSquared is required.');
  2136. }
  2137. if (!defined(centerToleranceSquared)) {
  2138. throw new DeveloperError('centerToleranceSquared is required.');
  2139. }
  2140. var positionX = cartesian.x;
  2141. var positionY = cartesian.y;
  2142. var positionZ = cartesian.z;
  2143. var oneOverRadiiX = oneOverRadii.x;
  2144. var oneOverRadiiY = oneOverRadii.y;
  2145. var oneOverRadiiZ = oneOverRadii.z;
  2146. var x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  2147. var y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  2148. var z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  2149. // Compute the squared ellipsoid norm.
  2150. var squaredNorm = x2 + y2 + z2;
  2151. var ratio = Math.sqrt(1.0 / squaredNorm);
  2152. // As an initial approximation, assume that the radial intersection is the projection point.
  2153. var intersection = Cartesian3.multiplyByScalar(cartesian, ratio, scaleToGeodeticSurfaceIntersection);
  2154. // If the position is near the center, the iteration will not converge.
  2155. if (squaredNorm < centerToleranceSquared) {
  2156. return !isFinite(ratio) ? undefined : Cartesian3.clone(intersection, result);
  2157. }
  2158. var oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  2159. var oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  2160. var oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  2161. // Use the gradient at the intersection point in place of the true unit normal.
  2162. // The difference in magnitude will be absorbed in the multiplier.
  2163. var gradient = scaleToGeodeticSurfaceGradient;
  2164. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  2165. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  2166. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  2167. // Compute the initial guess at the normal vector multiplier, lambda.
  2168. var lambda = (1.0 - ratio) * Cartesian3.magnitude(cartesian) / (0.5 * Cartesian3.magnitude(gradient));
  2169. var correction = 0.0;
  2170. var func;
  2171. var denominator;
  2172. var xMultiplier;
  2173. var yMultiplier;
  2174. var zMultiplier;
  2175. var xMultiplier2;
  2176. var yMultiplier2;
  2177. var zMultiplier2;
  2178. var xMultiplier3;
  2179. var yMultiplier3;
  2180. var zMultiplier3;
  2181. do {
  2182. lambda -= correction;
  2183. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  2184. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  2185. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  2186. xMultiplier2 = xMultiplier * xMultiplier;
  2187. yMultiplier2 = yMultiplier * yMultiplier;
  2188. zMultiplier2 = zMultiplier * zMultiplier;
  2189. xMultiplier3 = xMultiplier2 * xMultiplier;
  2190. yMultiplier3 = yMultiplier2 * yMultiplier;
  2191. zMultiplier3 = zMultiplier2 * zMultiplier;
  2192. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  2193. // "denominator" here refers to the use of this expression in the velocity and acceleration
  2194. // computations in the sections to follow.
  2195. denominator = x2 * xMultiplier3 * oneOverRadiiSquaredX + y2 * yMultiplier3 * oneOverRadiiSquaredY + z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  2196. var derivative = -2.0 * denominator;
  2197. correction = func / derivative;
  2198. } while (Math.abs(func) > CesiumMath.EPSILON12);
  2199. if (!defined(result)) {
  2200. return new Cartesian3(positionX * xMultiplier, positionY * yMultiplier, positionZ * zMultiplier);
  2201. }
  2202. result.x = positionX * xMultiplier;
  2203. result.y = positionY * yMultiplier;
  2204. result.z = positionZ * zMultiplier;
  2205. return result;
  2206. }
  2207. return scaleToGeodeticSurface;
  2208. });
  2209. /*global define*/
  2210. define('Core/Cartographic',[
  2211. './Cartesian3',
  2212. './defaultValue',
  2213. './defined',
  2214. './DeveloperError',
  2215. './freezeObject',
  2216. './Math',
  2217. './scaleToGeodeticSurface'
  2218. ], function(
  2219. Cartesian3,
  2220. defaultValue,
  2221. defined,
  2222. DeveloperError,
  2223. freezeObject,
  2224. CesiumMath,
  2225. scaleToGeodeticSurface) {
  2226. 'use strict';
  2227. /**
  2228. * A position defined by longitude, latitude, and height.
  2229. * @alias Cartographic
  2230. * @constructor
  2231. *
  2232. * @param {Number} [longitude=0.0] The longitude, in radians.
  2233. * @param {Number} [latitude=0.0] The latitude, in radians.
  2234. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2235. *
  2236. * @see Ellipsoid
  2237. */
  2238. function Cartographic(longitude, latitude, height) {
  2239. /**
  2240. * The longitude, in radians.
  2241. * @type {Number}
  2242. * @default 0.0
  2243. */
  2244. this.longitude = defaultValue(longitude, 0.0);
  2245. /**
  2246. * The latitude, in radians.
  2247. * @type {Number}
  2248. * @default 0.0
  2249. */
  2250. this.latitude = defaultValue(latitude, 0.0);
  2251. /**
  2252. * The height, in meters, above the ellipsoid.
  2253. * @type {Number}
  2254. * @default 0.0
  2255. */
  2256. this.height = defaultValue(height, 0.0);
  2257. }
  2258. /**
  2259. * Creates a new Cartographic instance from longitude and latitude
  2260. * specified in radians.
  2261. *
  2262. * @param {Number} longitude The longitude, in radians.
  2263. * @param {Number} latitude The latitude, in radians.
  2264. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2265. * @param {Cartographic} [result] The object onto which to store the result.
  2266. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  2267. */
  2268. Cartographic.fromRadians = function(longitude, latitude, height, result) {
  2269. if (!defined(longitude)) {
  2270. throw new DeveloperError('longitude is required.');
  2271. }
  2272. if (!defined(latitude)) {
  2273. throw new DeveloperError('latitude is required.');
  2274. }
  2275. height = defaultValue(height, 0.0);
  2276. if (!defined(result)) {
  2277. return new Cartographic(longitude, latitude, height);
  2278. }
  2279. result.longitude = longitude;
  2280. result.latitude = latitude;
  2281. result.height = height;
  2282. return result;
  2283. };
  2284. /**
  2285. * Creates a new Cartographic instance from longitude and latitude
  2286. * specified in degrees. The values in the resulting object will
  2287. * be in radians.
  2288. *
  2289. * @param {Number} longitude The longitude, in degrees.
  2290. * @param {Number} latitude The latitude, in degrees.
  2291. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2292. * @param {Cartographic} [result] The object onto which to store the result.
  2293. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  2294. */
  2295. Cartographic.fromDegrees = function(longitude, latitude, height, result) {
  2296. if (!defined(longitude)) {
  2297. throw new DeveloperError('longitude is required.');
  2298. }
  2299. if (!defined(latitude)) {
  2300. throw new DeveloperError('latitude is required.');
  2301. }
  2302. longitude = CesiumMath.toRadians(longitude);
  2303. latitude = CesiumMath.toRadians(latitude);
  2304. return Cartographic.fromRadians(longitude, latitude, height, result);
  2305. };
  2306. var cartesianToCartographicN = new Cartesian3();
  2307. var cartesianToCartographicP = new Cartesian3();
  2308. var cartesianToCartographicH = new Cartesian3();
  2309. var wgs84OneOverRadii = new Cartesian3(1.0 / 6378137.0, 1.0 / 6378137.0, 1.0 / 6356752.3142451793);
  2310. var wgs84OneOverRadiiSquared = new Cartesian3(1.0 / (6378137.0 * 6378137.0), 1.0 / (6378137.0 * 6378137.0), 1.0 / (6356752.3142451793 * 6356752.3142451793));
  2311. var wgs84CenterToleranceSquared = CesiumMath.EPSILON1;
  2312. /**
  2313. * Creates a new Cartographic instance from a Cartesian position. The values in the
  2314. * resulting object will be in radians.
  2315. *
  2316. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  2317. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2318. * @param {Cartographic} [result] The object onto which to store the result.
  2319. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  2320. */
  2321. Cartographic.fromCartesian = function(cartesian, ellipsoid, result) {
  2322. var oneOverRadii = defined(ellipsoid) ? ellipsoid.oneOverRadii : wgs84OneOverRadii;
  2323. var oneOverRadiiSquared = defined(ellipsoid) ? ellipsoid.oneOverRadiiSquared : wgs84OneOverRadiiSquared;
  2324. var centerToleranceSquared = defined(ellipsoid) ? ellipsoid._centerToleranceSquared : wgs84CenterToleranceSquared;
  2325. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  2326. var p = scaleToGeodeticSurface(cartesian, oneOverRadii, oneOverRadiiSquared, centerToleranceSquared, cartesianToCartographicP);
  2327. if (!defined(p)) {
  2328. return undefined;
  2329. }
  2330. var n = Cartesian3.multiplyComponents(p, oneOverRadiiSquared, cartesianToCartographicN);
  2331. n = Cartesian3.normalize(n, n);
  2332. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  2333. var longitude = Math.atan2(n.y, n.x);
  2334. var latitude = Math.asin(n.z);
  2335. var height = CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  2336. if (!defined(result)) {
  2337. return new Cartographic(longitude, latitude, height);
  2338. }
  2339. result.longitude = longitude;
  2340. result.latitude = latitude;
  2341. result.height = height;
  2342. return result;
  2343. };
  2344. /**
  2345. * Duplicates a Cartographic instance.
  2346. *
  2347. * @param {Cartographic} cartographic The cartographic to duplicate.
  2348. * @param {Cartographic} [result] The object onto which to store the result.
  2349. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  2350. */
  2351. Cartographic.clone = function(cartographic, result) {
  2352. if (!defined(cartographic)) {
  2353. return undefined;
  2354. }
  2355. if (!defined(result)) {
  2356. return new Cartographic(cartographic.longitude, cartographic.latitude, cartographic.height);
  2357. }
  2358. result.longitude = cartographic.longitude;
  2359. result.latitude = cartographic.latitude;
  2360. result.height = cartographic.height;
  2361. return result;
  2362. };
  2363. /**
  2364. * Compares the provided cartographics componentwise and returns
  2365. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2366. *
  2367. * @param {Cartographic} [left] The first cartographic.
  2368. * @param {Cartographic} [right] The second cartographic.
  2369. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  2370. */
  2371. Cartographic.equals = function(left, right) {
  2372. return (left === right) ||
  2373. ((defined(left)) &&
  2374. (defined(right)) &&
  2375. (left.longitude === right.longitude) &&
  2376. (left.latitude === right.latitude) &&
  2377. (left.height === right.height));
  2378. };
  2379. /**
  2380. * Compares the provided cartographics componentwise and returns
  2381. * <code>true</code> if they are within the provided epsilon,
  2382. * <code>false</code> otherwise.
  2383. *
  2384. * @param {Cartographic} [left] The first cartographic.
  2385. * @param {Cartographic} [right] The second cartographic.
  2386. * @param {Number} epsilon The epsilon to use for equality testing.
  2387. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2388. */
  2389. Cartographic.equalsEpsilon = function(left, right, epsilon) {
  2390. if (typeof epsilon !== 'number') {
  2391. throw new DeveloperError('epsilon is required and must be a number.');
  2392. }
  2393. return (left === right) ||
  2394. ((defined(left)) &&
  2395. (defined(right)) &&
  2396. (Math.abs(left.longitude - right.longitude) <= epsilon) &&
  2397. (Math.abs(left.latitude - right.latitude) <= epsilon) &&
  2398. (Math.abs(left.height - right.height) <= epsilon));
  2399. };
  2400. /**
  2401. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  2402. *
  2403. * @type {Cartographic}
  2404. * @constant
  2405. */
  2406. Cartographic.ZERO = freezeObject(new Cartographic(0.0, 0.0, 0.0));
  2407. /**
  2408. * Duplicates this instance.
  2409. *
  2410. * @param {Cartographic} [result] The object onto which to store the result.
  2411. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  2412. */
  2413. Cartographic.prototype.clone = function(result) {
  2414. return Cartographic.clone(this, result);
  2415. };
  2416. /**
  2417. * Compares the provided against this cartographic componentwise and returns
  2418. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2419. *
  2420. * @param {Cartographic} [right] The second cartographic.
  2421. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  2422. */
  2423. Cartographic.prototype.equals = function(right) {
  2424. return Cartographic.equals(this, right);
  2425. };
  2426. /**
  2427. * Compares the provided against this cartographic componentwise and returns
  2428. * <code>true</code> if they are within the provided epsilon,
  2429. * <code>false</code> otherwise.
  2430. *
  2431. * @param {Cartographic} [right] The second cartographic.
  2432. * @param {Number} epsilon The epsilon to use for equality testing.
  2433. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2434. */
  2435. Cartographic.prototype.equalsEpsilon = function(right, epsilon) {
  2436. return Cartographic.equalsEpsilon(this, right, epsilon);
  2437. };
  2438. /**
  2439. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  2440. *
  2441. * @returns {String} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  2442. */
  2443. Cartographic.prototype.toString = function() {
  2444. return '(' + this.longitude + ', ' + this.latitude + ', ' + this.height + ')';
  2445. };
  2446. return Cartographic;
  2447. });
  2448. /*global define*/
  2449. define('Core/defineProperties',[
  2450. './defined'
  2451. ], function(
  2452. defined) {
  2453. 'use strict';
  2454. var definePropertyWorks = (function() {
  2455. try {
  2456. return 'x' in Object.defineProperty({}, 'x', {});
  2457. } catch (e) {
  2458. return false;
  2459. }
  2460. })();
  2461. /**
  2462. * Defines properties on an object, using Object.defineProperties if available,
  2463. * otherwise returns the object unchanged. This function should be used in
  2464. * setup code to prevent errors from completely halting JavaScript execution
  2465. * in legacy browsers.
  2466. *
  2467. * @private
  2468. *
  2469. * @exports defineProperties
  2470. */
  2471. var defineProperties = Object.defineProperties;
  2472. if (!definePropertyWorks || !defined(defineProperties)) {
  2473. defineProperties = function(o) {
  2474. return o;
  2475. };
  2476. }
  2477. return defineProperties;
  2478. });
  2479. /*global define*/
  2480. define('Core/Ellipsoid',[
  2481. './Cartesian3',
  2482. './Cartographic',
  2483. './defaultValue',
  2484. './defined',
  2485. './defineProperties',
  2486. './DeveloperError',
  2487. './freezeObject',
  2488. './Math',
  2489. './scaleToGeodeticSurface'
  2490. ], function(
  2491. Cartesian3,
  2492. Cartographic,
  2493. defaultValue,
  2494. defined,
  2495. defineProperties,
  2496. DeveloperError,
  2497. freezeObject,
  2498. CesiumMath,
  2499. scaleToGeodeticSurface) {
  2500. 'use strict';
  2501. function initialize(ellipsoid, x, y, z) {
  2502. x = defaultValue(x, 0.0);
  2503. y = defaultValue(y, 0.0);
  2504. z = defaultValue(z, 0.0);
  2505. if (x < 0.0 || y < 0.0 || z < 0.0) {
  2506. throw new DeveloperError('All radii components must be greater than or equal to zero.');
  2507. }
  2508. ellipsoid._radii = new Cartesian3(x, y, z);
  2509. ellipsoid._radiiSquared = new Cartesian3(x * x,
  2510. y * y,
  2511. z * z);
  2512. ellipsoid._radiiToTheFourth = new Cartesian3(x * x * x * x,
  2513. y * y * y * y,
  2514. z * z * z * z);
  2515. ellipsoid._oneOverRadii = new Cartesian3(x === 0.0 ? 0.0 : 1.0 / x,
  2516. y === 0.0 ? 0.0 : 1.0 / y,
  2517. z === 0.0 ? 0.0 : 1.0 / z);
  2518. ellipsoid._oneOverRadiiSquared = new Cartesian3(x === 0.0 ? 0.0 : 1.0 / (x * x),
  2519. y === 0.0 ? 0.0 : 1.0 / (y * y),
  2520. z === 0.0 ? 0.0 : 1.0 / (z * z));
  2521. ellipsoid._minimumRadius = Math.min(x, y, z);
  2522. ellipsoid._maximumRadius = Math.max(x, y, z);
  2523. ellipsoid._centerToleranceSquared = CesiumMath.EPSILON1;
  2524. if (ellipsoid._radiiSquared.z !== 0) {
  2525. ellipsoid._sqauredXOverSquaredZ = ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  2526. }
  2527. }
  2528. /**
  2529. * A quadratic surface defined in Cartesian coordinates by the equation
  2530. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  2531. * by Cesium to represent the shape of planetary bodies.
  2532. *
  2533. * Rather than constructing this object directly, one of the provided
  2534. * constants is normally used.
  2535. * @alias Ellipsoid
  2536. * @constructor
  2537. *
  2538. * @param {Number} [x=0] The radius in the x direction.
  2539. * @param {Number} [y=0] The radius in the y direction.
  2540. * @param {Number} [z=0] The radius in the z direction.
  2541. *
  2542. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  2543. *
  2544. * @see Ellipsoid.fromCartesian3
  2545. * @see Ellipsoid.WGS84
  2546. * @see Ellipsoid.UNIT_SPHERE
  2547. */
  2548. function Ellipsoid(x, y, z) {
  2549. this._radii = undefined;
  2550. this._radiiSquared = undefined;
  2551. this._radiiToTheFourth = undefined;
  2552. this._oneOverRadii = undefined;
  2553. this._oneOverRadiiSquared = undefined;
  2554. this._minimumRadius = undefined;
  2555. this._maximumRadius = undefined;
  2556. this._centerToleranceSquared = undefined;
  2557. this._sqauredXOverSquaredZ = undefined;
  2558. initialize(this, x, y, z);
  2559. }
  2560. defineProperties(Ellipsoid.prototype, {
  2561. /**
  2562. * Gets the radii of the ellipsoid.
  2563. * @memberof Ellipsoid.prototype
  2564. * @type {Cartesian3}
  2565. * @readonly
  2566. */
  2567. radii : {
  2568. get: function() {
  2569. return this._radii;
  2570. }
  2571. },
  2572. /**
  2573. * Gets the squared radii of the ellipsoid.
  2574. * @memberof Ellipsoid.prototype
  2575. * @type {Cartesian3}
  2576. * @readonly
  2577. */
  2578. radiiSquared : {
  2579. get : function() {
  2580. return this._radiiSquared;
  2581. }
  2582. },
  2583. /**
  2584. * Gets the radii of the ellipsoid raise to the fourth power.
  2585. * @memberof Ellipsoid.prototype
  2586. * @type {Cartesian3}
  2587. * @readonly
  2588. */
  2589. radiiToTheFourth : {
  2590. get : function() {
  2591. return this._radiiToTheFourth;
  2592. }
  2593. },
  2594. /**
  2595. * Gets one over the radii of the ellipsoid.
  2596. * @memberof Ellipsoid.prototype
  2597. * @type {Cartesian3}
  2598. * @readonly
  2599. */
  2600. oneOverRadii : {
  2601. get : function() {
  2602. return this._oneOverRadii;
  2603. }
  2604. },
  2605. /**
  2606. * Gets one over the squared radii of the ellipsoid.
  2607. * @memberof Ellipsoid.prototype
  2608. * @type {Cartesian3}
  2609. * @readonly
  2610. */
  2611. oneOverRadiiSquared : {
  2612. get : function() {
  2613. return this._oneOverRadiiSquared;
  2614. }
  2615. },
  2616. /**
  2617. * Gets the minimum radius of the ellipsoid.
  2618. * @memberof Ellipsoid.prototype
  2619. * @type {Number}
  2620. * @readonly
  2621. */
  2622. minimumRadius : {
  2623. get : function() {
  2624. return this._minimumRadius;
  2625. }
  2626. },
  2627. /**
  2628. * Gets the maximum radius of the ellipsoid.
  2629. * @memberof Ellipsoid.prototype
  2630. * @type {Number}
  2631. * @readonly
  2632. */
  2633. maximumRadius : {
  2634. get : function() {
  2635. return this._maximumRadius;
  2636. }
  2637. }
  2638. });
  2639. /**
  2640. * Duplicates an Ellipsoid instance.
  2641. *
  2642. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  2643. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  2644. * instance should be created.
  2645. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  2646. */
  2647. Ellipsoid.clone = function(ellipsoid, result) {
  2648. if (!defined(ellipsoid)) {
  2649. return undefined;
  2650. }
  2651. var radii = ellipsoid._radii;
  2652. if (!defined(result)) {
  2653. return new Ellipsoid(radii.x, radii.y, radii.z);
  2654. }
  2655. Cartesian3.clone(radii, result._radii);
  2656. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  2657. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  2658. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  2659. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  2660. result._minimumRadius = ellipsoid._minimumRadius;
  2661. result._maximumRadius = ellipsoid._maximumRadius;
  2662. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  2663. return result;
  2664. };
  2665. /**
  2666. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  2667. *
  2668. * @param {Cartesian3} [radii=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  2669. * @returns {Ellipsoid} A new Ellipsoid instance.
  2670. *
  2671. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  2672. *
  2673. * @see Ellipsoid.WGS84
  2674. * @see Ellipsoid.UNIT_SPHERE
  2675. */
  2676. Ellipsoid.fromCartesian3 = function(cartesian, result) {
  2677. if (!defined(result)) {
  2678. result = new Ellipsoid();
  2679. }
  2680. if (!defined(cartesian)) {
  2681. return result;
  2682. }
  2683. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  2684. return result;
  2685. };
  2686. /**
  2687. * An Ellipsoid instance initialized to the WGS84 standard.
  2688. *
  2689. * @type {Ellipsoid}
  2690. * @constant
  2691. */
  2692. Ellipsoid.WGS84 = freezeObject(new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793));
  2693. /**
  2694. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  2695. *
  2696. * @type {Ellipsoid}
  2697. * @constant
  2698. */
  2699. Ellipsoid.UNIT_SPHERE = freezeObject(new Ellipsoid(1.0, 1.0, 1.0));
  2700. /**
  2701. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  2702. *
  2703. * @type {Ellipsoid}
  2704. * @constant
  2705. */
  2706. Ellipsoid.MOON = freezeObject(new Ellipsoid(CesiumMath.LUNAR_RADIUS, CesiumMath.LUNAR_RADIUS, CesiumMath.LUNAR_RADIUS));
  2707. /**
  2708. * Duplicates an Ellipsoid instance.
  2709. *
  2710. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  2711. * instance should be created.
  2712. * @returns {Ellipsoid} The cloned Ellipsoid.
  2713. */
  2714. Ellipsoid.prototype.clone = function(result) {
  2715. return Ellipsoid.clone(this, result);
  2716. };
  2717. /**
  2718. * The number of elements used to pack the object into an array.
  2719. * @type {Number}
  2720. */
  2721. Ellipsoid.packedLength = Cartesian3.packedLength;
  2722. /**
  2723. * Stores the provided instance into the provided array.
  2724. *
  2725. * @param {Ellipsoid} value The value to pack.
  2726. * @param {Number[]} array The array to pack into.
  2727. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2728. *
  2729. * @returns {Number[]} The array that was packed into
  2730. */
  2731. Ellipsoid.pack = function(value, array, startingIndex) {
  2732. if (!defined(value)) {
  2733. throw new DeveloperError('value is required');
  2734. }
  2735. if (!defined(array)) {
  2736. throw new DeveloperError('array is required');
  2737. }
  2738. startingIndex = defaultValue(startingIndex, 0);
  2739. Cartesian3.pack(value._radii, array, startingIndex);
  2740. return array;
  2741. };
  2742. /**
  2743. * Retrieves an instance from a packed array.
  2744. *
  2745. * @param {Number[]} array The packed array.
  2746. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2747. * @param {Ellipsoid} [result] The object into which to store the result.
  2748. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  2749. */
  2750. Ellipsoid.unpack = function(array, startingIndex, result) {
  2751. if (!defined(array)) {
  2752. throw new DeveloperError('array is required');
  2753. }
  2754. startingIndex = defaultValue(startingIndex, 0);
  2755. var radii = Cartesian3.unpack(array, startingIndex);
  2756. return Ellipsoid.fromCartesian3(radii, result);
  2757. };
  2758. /**
  2759. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  2760. * @function
  2761. *
  2762. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  2763. * @param {Cartesian3} [result] The object onto which to store the result.
  2764. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2765. */
  2766. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  2767. /**
  2768. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  2769. *
  2770. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  2771. * @param {Cartesian3} [result] The object onto which to store the result.
  2772. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2773. */
  2774. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function(cartographic, result) {
  2775. if (!defined(cartographic)) {
  2776. throw new DeveloperError('cartographic is required.');
  2777. }
  2778. var longitude = cartographic.longitude;
  2779. var latitude = cartographic.latitude;
  2780. var cosLatitude = Math.cos(latitude);
  2781. var x = cosLatitude * Math.cos(longitude);
  2782. var y = cosLatitude * Math.sin(longitude);
  2783. var z = Math.sin(latitude);
  2784. if (!defined(result)) {
  2785. result = new Cartesian3();
  2786. }
  2787. result.x = x;
  2788. result.y = y;
  2789. result.z = z;
  2790. return Cartesian3.normalize(result, result);
  2791. };
  2792. /**
  2793. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  2794. *
  2795. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  2796. * @param {Cartesian3} [result] The object onto which to store the result.
  2797. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2798. */
  2799. Ellipsoid.prototype.geodeticSurfaceNormal = function(cartesian, result) {
  2800. if (!defined(result)) {
  2801. result = new Cartesian3();
  2802. }
  2803. result = Cartesian3.multiplyComponents(cartesian, this._oneOverRadiiSquared, result);
  2804. return Cartesian3.normalize(result, result);
  2805. };
  2806. var cartographicToCartesianNormal = new Cartesian3();
  2807. var cartographicToCartesianK = new Cartesian3();
  2808. /**
  2809. * Converts the provided cartographic to Cartesian representation.
  2810. *
  2811. * @param {Cartographic} cartographic The cartographic position.
  2812. * @param {Cartesian3} [result] The object onto which to store the result.
  2813. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2814. *
  2815. * @example
  2816. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  2817. * var position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  2818. * var cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  2819. */
  2820. Ellipsoid.prototype.cartographicToCartesian = function(cartographic, result) {
  2821. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  2822. var n = cartographicToCartesianNormal;
  2823. var k = cartographicToCartesianK;
  2824. this.geodeticSurfaceNormalCartographic(cartographic, n);
  2825. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  2826. var gamma = Math.sqrt(Cartesian3.dot(n, k));
  2827. Cartesian3.divideByScalar(k, gamma, k);
  2828. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  2829. if (!defined(result)) {
  2830. result = new Cartesian3();
  2831. }
  2832. return Cartesian3.add(k, n, result);
  2833. };
  2834. /**
  2835. * Converts the provided array of cartographics to an array of Cartesians.
  2836. *
  2837. * @param {Cartographic[]} cartographics An array of cartographic positions.
  2838. * @param {Cartesian3[]} [result] The object onto which to store the result.
  2839. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  2840. *
  2841. * @example
  2842. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  2843. * var positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  2844. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  2845. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  2846. * var cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  2847. */
  2848. Ellipsoid.prototype.cartographicArrayToCartesianArray = function(cartographics, result) {
  2849. if (!defined(cartographics)) {
  2850. throw new DeveloperError('cartographics is required.');
  2851. }
  2852. var length = cartographics.length;
  2853. if (!defined(result)) {
  2854. result = new Array(length);
  2855. } else {
  2856. result.length = length;
  2857. }
  2858. for ( var i = 0; i < length; i++) {
  2859. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  2860. }
  2861. return result;
  2862. };
  2863. var cartesianToCartographicN = new Cartesian3();
  2864. var cartesianToCartographicP = new Cartesian3();
  2865. var cartesianToCartographicH = new Cartesian3();
  2866. /**
  2867. * Converts the provided cartesian to cartographic representation.
  2868. * The cartesian is undefined at the center of the ellipsoid.
  2869. *
  2870. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  2871. * @param {Cartographic} [result] The object onto which to store the result.
  2872. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  2873. *
  2874. * @example
  2875. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  2876. * var position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  2877. * var cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  2878. */
  2879. Ellipsoid.prototype.cartesianToCartographic = function(cartesian, result) {
  2880. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  2881. var p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  2882. if (!defined(p)) {
  2883. return undefined;
  2884. }
  2885. var n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  2886. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  2887. var longitude = Math.atan2(n.y, n.x);
  2888. var latitude = Math.asin(n.z);
  2889. var height = CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  2890. if (!defined(result)) {
  2891. return new Cartographic(longitude, latitude, height);
  2892. }
  2893. result.longitude = longitude;
  2894. result.latitude = latitude;
  2895. result.height = height;
  2896. return result;
  2897. };
  2898. /**
  2899. * Converts the provided array of cartesians to an array of cartographics.
  2900. *
  2901. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  2902. * @param {Cartographic[]} [result] The object onto which to store the result.
  2903. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  2904. *
  2905. * @example
  2906. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  2907. * var positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  2908. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  2909. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  2910. * var cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  2911. */
  2912. Ellipsoid.prototype.cartesianArrayToCartographicArray = function(cartesians, result) {
  2913. if (!defined(cartesians)) {
  2914. throw new DeveloperError('cartesians is required.');
  2915. }
  2916. var length = cartesians.length;
  2917. if (!defined(result)) {
  2918. result = new Array(length);
  2919. } else {
  2920. result.length = length;
  2921. }
  2922. for ( var i = 0; i < length; ++i) {
  2923. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  2924. }
  2925. return result;
  2926. };
  2927. /**
  2928. * Scales the provided Cartesian position along the geodetic surface normal
  2929. * so that it is on the surface of this ellipsoid. If the position is
  2930. * at the center of the ellipsoid, this function returns undefined.
  2931. *
  2932. * @param {Cartesian3} cartesian The Cartesian position to scale.
  2933. * @param {Cartesian3} [result] The object onto which to store the result.
  2934. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  2935. */
  2936. Ellipsoid.prototype.scaleToGeodeticSurface = function(cartesian, result) {
  2937. return scaleToGeodeticSurface(cartesian, this._oneOverRadii, this._oneOverRadiiSquared, this._centerToleranceSquared, result);
  2938. };
  2939. /**
  2940. * Scales the provided Cartesian position along the geocentric surface normal
  2941. * so that it is on the surface of this ellipsoid.
  2942. *
  2943. * @param {Cartesian3} cartesian The Cartesian position to scale.
  2944. * @param {Cartesian3} [result] The object onto which to store the result.
  2945. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2946. */
  2947. Ellipsoid.prototype.scaleToGeocentricSurface = function(cartesian, result) {
  2948. if (!defined(cartesian)) {
  2949. throw new DeveloperError('cartesian is required.');
  2950. }
  2951. if (!defined(result)) {
  2952. result = new Cartesian3();
  2953. }
  2954. var positionX = cartesian.x;
  2955. var positionY = cartesian.y;
  2956. var positionZ = cartesian.z;
  2957. var oneOverRadiiSquared = this._oneOverRadiiSquared;
  2958. var beta = 1.0 / Math.sqrt((positionX * positionX) * oneOverRadiiSquared.x +
  2959. (positionY * positionY) * oneOverRadiiSquared.y +
  2960. (positionZ * positionZ) * oneOverRadiiSquared.z);
  2961. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  2962. };
  2963. /**
  2964. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  2965. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  2966. *
  2967. * @param {Cartesian3} position The position to transform.
  2968. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2969. * return a new instance.
  2970. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  2971. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2972. */
  2973. Ellipsoid.prototype.transformPositionToScaledSpace = function(position, result) {
  2974. if (!defined(result)) {
  2975. result = new Cartesian3();
  2976. }
  2977. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  2978. };
  2979. /**
  2980. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  2981. * its components by the result of {@link Ellipsoid#radii}.
  2982. *
  2983. * @param {Cartesian3} position The position to transform.
  2984. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2985. * return a new instance.
  2986. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  2987. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2988. */
  2989. Ellipsoid.prototype.transformPositionFromScaledSpace = function(position, result) {
  2990. if (!defined(result)) {
  2991. result = new Cartesian3();
  2992. }
  2993. return Cartesian3.multiplyComponents(position, this._radii, result);
  2994. };
  2995. /**
  2996. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  2997. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2998. *
  2999. * @param {Ellipsoid} [right] The other Ellipsoid.
  3000. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3001. */
  3002. Ellipsoid.prototype.equals = function(right) {
  3003. return (this === right) ||
  3004. (defined(right) &&
  3005. Cartesian3.equals(this._radii, right._radii));
  3006. };
  3007. /**
  3008. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  3009. *
  3010. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  3011. */
  3012. Ellipsoid.prototype.toString = function() {
  3013. return this._radii.toString();
  3014. };
  3015. /**
  3016. * Computes a point which is the intersection of the surface normal with the z-axis.
  3017. *
  3018. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  3019. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  3020. * In earth case, with common earth datums, there is no need for this buffer since the intersection point is always (relatively) very close to the center.
  3021. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  3022. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  3023. * @param {Cartesian} [result] The cartesian to which to copy the result, or undefined to create and
  3024. * return a new instance.
  3025. * @returns {Cartesian | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  3026. *
  3027. * @exception {DeveloperError} position is required.
  3028. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  3029. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  3030. */
  3031. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function(position, buffer, result) {
  3032. if (!defined(position)) {
  3033. throw new DeveloperError('position is required.');
  3034. }
  3035. if (!CesiumMath.equalsEpsilon(this._radii.x, this._radii.y, CesiumMath.EPSILON15)) {
  3036. throw new DeveloperError('Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)');
  3037. }
  3038. if (this._radii.z === 0) {
  3039. throw new DeveloperError('Ellipsoid.radii.z must be greater than 0');
  3040. }
  3041. buffer = defaultValue(buffer, 0.0);
  3042. var sqauredXOverSquaredZ = this._sqauredXOverSquaredZ;
  3043. if (!defined(result)) {
  3044. result = new Cartesian3();
  3045. }
  3046. result.x = 0.0;
  3047. result.y = 0.0;
  3048. result.z = position.z * (1 - sqauredXOverSquaredZ);
  3049. if (Math.abs(result.z) >= this._radii.z - buffer) {
  3050. return undefined;
  3051. }
  3052. return result;
  3053. };
  3054. return Ellipsoid;
  3055. });
  3056. /*global define*/
  3057. define('Core/Rectangle',[
  3058. './Cartographic',
  3059. './defaultValue',
  3060. './defined',
  3061. './defineProperties',
  3062. './DeveloperError',
  3063. './Ellipsoid',
  3064. './freezeObject',
  3065. './Math'
  3066. ], function(
  3067. Cartographic,
  3068. defaultValue,
  3069. defined,
  3070. defineProperties,
  3071. DeveloperError,
  3072. Ellipsoid,
  3073. freezeObject,
  3074. CesiumMath) {
  3075. 'use strict';
  3076. /**
  3077. * A two dimensional region specified as longitude and latitude coordinates.
  3078. *
  3079. * @alias Rectangle
  3080. * @constructor
  3081. *
  3082. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  3083. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  3084. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  3085. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  3086. *
  3087. * @see Packable
  3088. */
  3089. function Rectangle(west, south, east, north) {
  3090. /**
  3091. * The westernmost longitude in radians in the range [-Pi, Pi].
  3092. *
  3093. * @type {Number}
  3094. * @default 0.0
  3095. */
  3096. this.west = defaultValue(west, 0.0);
  3097. /**
  3098. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  3099. *
  3100. * @type {Number}
  3101. * @default 0.0
  3102. */
  3103. this.south = defaultValue(south, 0.0);
  3104. /**
  3105. * The easternmost longitude in radians in the range [-Pi, Pi].
  3106. *
  3107. * @type {Number}
  3108. * @default 0.0
  3109. */
  3110. this.east = defaultValue(east, 0.0);
  3111. /**
  3112. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  3113. *
  3114. * @type {Number}
  3115. * @default 0.0
  3116. */
  3117. this.north = defaultValue(north, 0.0);
  3118. }
  3119. defineProperties(Rectangle.prototype, {
  3120. /**
  3121. * Gets the width of the rectangle in radians.
  3122. * @memberof Rectangle.prototype
  3123. * @type {Number}
  3124. */
  3125. width : {
  3126. get : function() {
  3127. return Rectangle.computeWidth(this);
  3128. }
  3129. },
  3130. /**
  3131. * Gets the height of the rectangle in radians.
  3132. * @memberof Rectangle.prototype
  3133. * @type {Number}
  3134. */
  3135. height : {
  3136. get : function() {
  3137. return Rectangle.computeHeight(this);
  3138. }
  3139. }
  3140. });
  3141. /**
  3142. * The number of elements used to pack the object into an array.
  3143. * @type {Number}
  3144. */
  3145. Rectangle.packedLength = 4;
  3146. /**
  3147. * Stores the provided instance into the provided array.
  3148. *
  3149. * @param {Rectangle} value The value to pack.
  3150. * @param {Number[]} array The array to pack into.
  3151. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  3152. *
  3153. * @returns {Number[]} The array that was packed into
  3154. */
  3155. Rectangle.pack = function(value, array, startingIndex) {
  3156. if (!defined(value)) {
  3157. throw new DeveloperError('value is required');
  3158. }
  3159. if (!defined(array)) {
  3160. throw new DeveloperError('array is required');
  3161. }
  3162. startingIndex = defaultValue(startingIndex, 0);
  3163. array[startingIndex++] = value.west;
  3164. array[startingIndex++] = value.south;
  3165. array[startingIndex++] = value.east;
  3166. array[startingIndex] = value.north;
  3167. return array;
  3168. };
  3169. /**
  3170. * Retrieves an instance from a packed array.
  3171. *
  3172. * @param {Number[]} array The packed array.
  3173. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  3174. * @param {Rectangle} [result] The object into which to store the result.
  3175. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  3176. */
  3177. Rectangle.unpack = function(array, startingIndex, result) {
  3178. if (!defined(array)) {
  3179. throw new DeveloperError('array is required');
  3180. }
  3181. startingIndex = defaultValue(startingIndex, 0);
  3182. if (!defined(result)) {
  3183. result = new Rectangle();
  3184. }
  3185. result.west = array[startingIndex++];
  3186. result.south = array[startingIndex++];
  3187. result.east = array[startingIndex++];
  3188. result.north = array[startingIndex];
  3189. return result;
  3190. };
  3191. /**
  3192. * Computes the width of a rectangle in radians.
  3193. * @param {Rectangle} rectangle The rectangle to compute the width of.
  3194. * @returns {Number} The width.
  3195. */
  3196. Rectangle.computeWidth = function(rectangle) {
  3197. if (!defined(rectangle)) {
  3198. throw new DeveloperError('rectangle is required.');
  3199. }
  3200. var east = rectangle.east;
  3201. var west = rectangle.west;
  3202. if (east < west) {
  3203. east += CesiumMath.TWO_PI;
  3204. }
  3205. return east - west;
  3206. };
  3207. /**
  3208. * Computes the height of a rectangle in radians.
  3209. * @param {Rectangle} rectangle The rectangle to compute the height of.
  3210. * @returns {Number} The height.
  3211. */
  3212. Rectangle.computeHeight = function(rectangle) {
  3213. if (!defined(rectangle)) {
  3214. throw new DeveloperError('rectangle is required.');
  3215. }
  3216. return rectangle.north - rectangle.south;
  3217. };
  3218. /**
  3219. * Creates an rectangle given the boundary longitude and latitude in degrees.
  3220. *
  3221. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  3222. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  3223. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  3224. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  3225. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  3226. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  3227. *
  3228. * @example
  3229. * var rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  3230. */
  3231. Rectangle.fromDegrees = function(west, south, east, north, result) {
  3232. west = CesiumMath.toRadians(defaultValue(west, 0.0));
  3233. south = CesiumMath.toRadians(defaultValue(south, 0.0));
  3234. east = CesiumMath.toRadians(defaultValue(east, 0.0));
  3235. north = CesiumMath.toRadians(defaultValue(north, 0.0));
  3236. if (!defined(result)) {
  3237. return new Rectangle(west, south, east, north);
  3238. }
  3239. result.west = west;
  3240. result.south = south;
  3241. result.east = east;
  3242. result.north = north;
  3243. return result;
  3244. };
  3245. /**
  3246. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  3247. *
  3248. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  3249. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  3250. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  3251. */
  3252. Rectangle.fromCartographicArray = function(cartographics, result) {
  3253. if (!defined(cartographics)) {
  3254. throw new DeveloperError('cartographics is required.');
  3255. }
  3256. var west = Number.MAX_VALUE;
  3257. var east = -Number.MAX_VALUE;
  3258. var westOverIDL = Number.MAX_VALUE;
  3259. var eastOverIDL = -Number.MAX_VALUE;
  3260. var south = Number.MAX_VALUE;
  3261. var north = -Number.MAX_VALUE;
  3262. for ( var i = 0, len = cartographics.length; i < len; i++) {
  3263. var position = cartographics[i];
  3264. west = Math.min(west, position.longitude);
  3265. east = Math.max(east, position.longitude);
  3266. south = Math.min(south, position.latitude);
  3267. north = Math.max(north, position.latitude);
  3268. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + CesiumMath.TWO_PI;
  3269. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  3270. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  3271. }
  3272. if(east - west > eastOverIDL - westOverIDL) {
  3273. west = westOverIDL;
  3274. east = eastOverIDL;
  3275. if (east > CesiumMath.PI) {
  3276. east = east - CesiumMath.TWO_PI;
  3277. }
  3278. if (west > CesiumMath.PI) {
  3279. west = west - CesiumMath.TWO_PI;
  3280. }
  3281. }
  3282. if (!defined(result)) {
  3283. return new Rectangle(west, south, east, north);
  3284. }
  3285. result.west = west;
  3286. result.south = south;
  3287. result.east = east;
  3288. result.north = north;
  3289. return result;
  3290. };
  3291. /**
  3292. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  3293. *
  3294. * @param {Cartesian[]} cartesians The list of Cartesian instances.
  3295. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  3296. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  3297. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  3298. */
  3299. Rectangle.fromCartesianArray = function(cartesians, ellipsoid, result) {
  3300. if (!defined(cartesians)) {
  3301. throw new DeveloperError('cartesians is required.');
  3302. }
  3303. var west = Number.MAX_VALUE;
  3304. var east = -Number.MAX_VALUE;
  3305. var westOverIDL = Number.MAX_VALUE;
  3306. var eastOverIDL = -Number.MAX_VALUE;
  3307. var south = Number.MAX_VALUE;
  3308. var north = -Number.MAX_VALUE;
  3309. for ( var i = 0, len = cartesians.length; i < len; i++) {
  3310. var position = ellipsoid.cartesianToCartographic(cartesians[i]);
  3311. west = Math.min(west, position.longitude);
  3312. east = Math.max(east, position.longitude);
  3313. south = Math.min(south, position.latitude);
  3314. north = Math.max(north, position.latitude);
  3315. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + CesiumMath.TWO_PI;
  3316. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  3317. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  3318. }
  3319. if(east - west > eastOverIDL - westOverIDL) {
  3320. west = westOverIDL;
  3321. east = eastOverIDL;
  3322. if (east > CesiumMath.PI) {
  3323. east = east - CesiumMath.TWO_PI;
  3324. }
  3325. if (west > CesiumMath.PI) {
  3326. west = west - CesiumMath.TWO_PI;
  3327. }
  3328. }
  3329. if (!defined(result)) {
  3330. return new Rectangle(west, south, east, north);
  3331. }
  3332. result.west = west;
  3333. result.south = south;
  3334. result.east = east;
  3335. result.north = north;
  3336. return result;
  3337. };
  3338. /**
  3339. * Duplicates an Rectangle.
  3340. *
  3341. * @param {Rectangle} rectangle The rectangle to clone.
  3342. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  3343. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  3344. */
  3345. Rectangle.clone = function(rectangle, result) {
  3346. if (!defined(rectangle)) {
  3347. return undefined;
  3348. }
  3349. if (!defined(result)) {
  3350. return new Rectangle(rectangle.west, rectangle.south, rectangle.east, rectangle.north);
  3351. }
  3352. result.west = rectangle.west;
  3353. result.south = rectangle.south;
  3354. result.east = rectangle.east;
  3355. result.north = rectangle.north;
  3356. return result;
  3357. };
  3358. /**
  3359. * Duplicates this Rectangle.
  3360. *
  3361. * @param {Rectangle} [result] The object onto which to store the result.
  3362. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  3363. */
  3364. Rectangle.prototype.clone = function(result) {
  3365. return Rectangle.clone(this, result);
  3366. };
  3367. /**
  3368. * Compares the provided Rectangle with this Rectangle componentwise and returns
  3369. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3370. *
  3371. * @param {Rectangle} [other] The Rectangle to compare.
  3372. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  3373. */
  3374. Rectangle.prototype.equals = function(other) {
  3375. return Rectangle.equals(this, other);
  3376. };
  3377. /**
  3378. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  3379. * <code>false</code> otherwise.
  3380. *
  3381. * @param {Rectangle} [left] The first Rectangle.
  3382. * @param {Rectangle} [right] The second Rectangle.
  3383. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  3384. */
  3385. Rectangle.equals = function(left, right) {
  3386. return (left === right) ||
  3387. ((defined(left)) &&
  3388. (defined(right)) &&
  3389. (left.west === right.west) &&
  3390. (left.south === right.south) &&
  3391. (left.east === right.east) &&
  3392. (left.north === right.north));
  3393. };
  3394. /**
  3395. * Compares the provided Rectangle with this Rectangle componentwise and returns
  3396. * <code>true</code> if they are within the provided epsilon,
  3397. * <code>false</code> otherwise.
  3398. *
  3399. * @param {Rectangle} [other] The Rectangle to compare.
  3400. * @param {Number} epsilon The epsilon to use for equality testing.
  3401. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  3402. */
  3403. Rectangle.prototype.equalsEpsilon = function(other, epsilon) {
  3404. if (typeof epsilon !== 'number') {
  3405. throw new DeveloperError('epsilon is required and must be a number.');
  3406. }
  3407. return defined(other) &&
  3408. (Math.abs(this.west - other.west) <= epsilon) &&
  3409. (Math.abs(this.south - other.south) <= epsilon) &&
  3410. (Math.abs(this.east - other.east) <= epsilon) &&
  3411. (Math.abs(this.north - other.north) <= epsilon);
  3412. };
  3413. /**
  3414. * Checks an Rectangle's properties and throws if they are not in valid ranges.
  3415. *
  3416. * @param {Rectangle} rectangle The rectangle to validate
  3417. *
  3418. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  3419. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  3420. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  3421. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  3422. */
  3423. Rectangle.validate = function(rectangle) {
  3424. if (!defined(rectangle)) {
  3425. throw new DeveloperError('rectangle is required');
  3426. }
  3427. var north = rectangle.north;
  3428. if (typeof north !== 'number') {
  3429. throw new DeveloperError('north is required to be a number.');
  3430. }
  3431. if (north < -CesiumMath.PI_OVER_TWO || north > CesiumMath.PI_OVER_TWO) {
  3432. throw new DeveloperError('north must be in the interval [-Pi/2, Pi/2].');
  3433. }
  3434. var south = rectangle.south;
  3435. if (typeof south !== 'number') {
  3436. throw new DeveloperError('south is required to be a number.');
  3437. }
  3438. if (south < -CesiumMath.PI_OVER_TWO || south > CesiumMath.PI_OVER_TWO) {
  3439. throw new DeveloperError('south must be in the interval [-Pi/2, Pi/2].');
  3440. }
  3441. var west = rectangle.west;
  3442. if (typeof west !== 'number') {
  3443. throw new DeveloperError('west is required to be a number.');
  3444. }
  3445. if (west < -Math.PI || west > Math.PI) {
  3446. throw new DeveloperError('west must be in the interval [-Pi, Pi].');
  3447. }
  3448. var east = rectangle.east;
  3449. if (typeof east !== 'number') {
  3450. throw new DeveloperError('east is required to be a number.');
  3451. }
  3452. if (east < -Math.PI || east > Math.PI) {
  3453. throw new DeveloperError('east must be in the interval [-Pi, Pi].');
  3454. }
  3455. };
  3456. /**
  3457. * Computes the southwest corner of an rectangle.
  3458. *
  3459. * @param {Rectangle} rectangle The rectangle for which to find the corner
  3460. * @param {Cartographic} [result] The object onto which to store the result.
  3461. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  3462. */
  3463. Rectangle.southwest = function(rectangle, result) {
  3464. if (!defined(rectangle)) {
  3465. throw new DeveloperError('rectangle is required');
  3466. }
  3467. if (!defined(result)) {
  3468. return new Cartographic(rectangle.west, rectangle.south);
  3469. }
  3470. result.longitude = rectangle.west;
  3471. result.latitude = rectangle.south;
  3472. result.height = 0.0;
  3473. return result;
  3474. };
  3475. /**
  3476. * Computes the northwest corner of an rectangle.
  3477. *
  3478. * @param {Rectangle} rectangle The rectangle for which to find the corner
  3479. * @param {Cartographic} [result] The object onto which to store the result.
  3480. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  3481. */
  3482. Rectangle.northwest = function(rectangle, result) {
  3483. if (!defined(rectangle)) {
  3484. throw new DeveloperError('rectangle is required');
  3485. }
  3486. if (!defined(result)) {
  3487. return new Cartographic(rectangle.west, rectangle.north);
  3488. }
  3489. result.longitude = rectangle.west;
  3490. result.latitude = rectangle.north;
  3491. result.height = 0.0;
  3492. return result;
  3493. };
  3494. /**
  3495. * Computes the northeast corner of an rectangle.
  3496. *
  3497. * @param {Rectangle} rectangle The rectangle for which to find the corner
  3498. * @param {Cartographic} [result] The object onto which to store the result.
  3499. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  3500. */
  3501. Rectangle.northeast = function(rectangle, result) {
  3502. if (!defined(rectangle)) {
  3503. throw new DeveloperError('rectangle is required');
  3504. }
  3505. if (!defined(result)) {
  3506. return new Cartographic(rectangle.east, rectangle.north);
  3507. }
  3508. result.longitude = rectangle.east;
  3509. result.latitude = rectangle.north;
  3510. result.height = 0.0;
  3511. return result;
  3512. };
  3513. /**
  3514. * Computes the southeast corner of an rectangle.
  3515. *
  3516. * @param {Rectangle} rectangle The rectangle for which to find the corner
  3517. * @param {Cartographic} [result] The object onto which to store the result.
  3518. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  3519. */
  3520. Rectangle.southeast = function(rectangle, result) {
  3521. if (!defined(rectangle)) {
  3522. throw new DeveloperError('rectangle is required');
  3523. }
  3524. if (!defined(result)) {
  3525. return new Cartographic(rectangle.east, rectangle.south);
  3526. }
  3527. result.longitude = rectangle.east;
  3528. result.latitude = rectangle.south;
  3529. result.height = 0.0;
  3530. return result;
  3531. };
  3532. /**
  3533. * Computes the center of an rectangle.
  3534. *
  3535. * @param {Rectangle} rectangle The rectangle for which to find the center
  3536. * @param {Cartographic} [result] The object onto which to store the result.
  3537. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  3538. */
  3539. Rectangle.center = function(rectangle, result) {
  3540. if (!defined(rectangle)) {
  3541. throw new DeveloperError('rectangle is required');
  3542. }
  3543. var east = rectangle.east;
  3544. var west = rectangle.west;
  3545. if (east < west) {
  3546. east += CesiumMath.TWO_PI;
  3547. }
  3548. var longitude = CesiumMath.negativePiToPi((west + east) * 0.5);
  3549. var latitude = (rectangle.south + rectangle.north) * 0.5;
  3550. if (!defined(result)) {
  3551. return new Cartographic(longitude, latitude);
  3552. }
  3553. result.longitude = longitude;
  3554. result.latitude = latitude;
  3555. result.height = 0.0;
  3556. return result;
  3557. };
  3558. /**
  3559. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  3560. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  3561. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  3562. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  3563. * coordinates, see {@link Rectangle.simpleIntersection}.
  3564. *
  3565. * @param {Rectangle} rectangle On rectangle to find an intersection
  3566. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  3567. * @param {Rectangle} [result] The object onto which to store the result.
  3568. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  3569. */
  3570. Rectangle.intersection = function(rectangle, otherRectangle, result) {
  3571. if (!defined(rectangle)) {
  3572. throw new DeveloperError('rectangle is required');
  3573. }
  3574. if (!defined(otherRectangle)) {
  3575. throw new DeveloperError('otherRectangle is required.');
  3576. }
  3577. var rectangleEast = rectangle.east;
  3578. var rectangleWest = rectangle.west;
  3579. var otherRectangleEast = otherRectangle.east;
  3580. var otherRectangleWest = otherRectangle.west;
  3581. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  3582. rectangleEast += CesiumMath.TWO_PI;
  3583. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  3584. otherRectangleEast += CesiumMath.TWO_PI;
  3585. }
  3586. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  3587. otherRectangleWest += CesiumMath.TWO_PI;
  3588. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  3589. rectangleWest += CesiumMath.TWO_PI;
  3590. }
  3591. var west = CesiumMath.negativePiToPi(Math.max(rectangleWest, otherRectangleWest));
  3592. var east = CesiumMath.negativePiToPi(Math.min(rectangleEast, otherRectangleEast));
  3593. if ((rectangle.west < rectangle.east || otherRectangle.west < otherRectangle.east) && east <= west) {
  3594. return undefined;
  3595. }
  3596. var south = Math.max(rectangle.south, otherRectangle.south);
  3597. var north = Math.min(rectangle.north, otherRectangle.north);
  3598. if (south >= north) {
  3599. return undefined;
  3600. }
  3601. if (!defined(result)) {
  3602. return new Rectangle(west, south, east, north);
  3603. }
  3604. result.west = west;
  3605. result.south = south;
  3606. result.east = east;
  3607. result.north = north;
  3608. return result;
  3609. };
  3610. /**
  3611. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  3612. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  3613. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  3614. * and longitude (i.e. projected coordinates).
  3615. *
  3616. * @param {Rectangle} rectangle On rectangle to find an intersection
  3617. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  3618. * @param {Rectangle} [result] The object onto which to store the result.
  3619. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  3620. */
  3621. Rectangle.simpleIntersection = function(rectangle, otherRectangle, result) {
  3622. if (!defined(rectangle)) {
  3623. throw new DeveloperError('rectangle is required');
  3624. }
  3625. if (!defined(otherRectangle)) {
  3626. throw new DeveloperError('otherRectangle is required.');
  3627. }
  3628. var west = Math.max(rectangle.west, otherRectangle.west);
  3629. var south = Math.max(rectangle.south, otherRectangle.south);
  3630. var east = Math.min(rectangle.east, otherRectangle.east);
  3631. var north = Math.min(rectangle.north, otherRectangle.north);
  3632. if (south >= north || west >= east) {
  3633. return undefined;
  3634. }
  3635. if (!defined(result)) {
  3636. return new Rectangle(west, south, east, north);
  3637. }
  3638. result.west = west;
  3639. result.south = south;
  3640. result.east = east;
  3641. result.north = north;
  3642. return result;
  3643. };
  3644. /**
  3645. * Computes a rectangle that is the union of two rectangles.
  3646. *
  3647. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  3648. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  3649. * @param {Rectangle} [result] The object onto which to store the result.
  3650. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  3651. */
  3652. Rectangle.union = function(rectangle, otherRectangle, result) {
  3653. if (!defined(rectangle)) {
  3654. throw new DeveloperError('rectangle is required');
  3655. }
  3656. if (!defined(otherRectangle)) {
  3657. throw new DeveloperError('otherRectangle is required.');
  3658. }
  3659. if (!defined(result)) {
  3660. result = new Rectangle();
  3661. }
  3662. var rectangleEast = rectangle.east;
  3663. var rectangleWest = rectangle.west;
  3664. var otherRectangleEast = otherRectangle.east;
  3665. var otherRectangleWest = otherRectangle.west;
  3666. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  3667. rectangleEast += CesiumMath.TWO_PI;
  3668. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  3669. otherRectangleEast += CesiumMath.TWO_PI;
  3670. }
  3671. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  3672. otherRectangleWest += CesiumMath.TWO_PI;
  3673. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  3674. rectangleWest += CesiumMath.TWO_PI;
  3675. }
  3676. var west = CesiumMath.convertLongitudeRange(Math.min(rectangleWest, otherRectangleWest));
  3677. var east = CesiumMath.convertLongitudeRange(Math.max(rectangleEast, otherRectangleEast));
  3678. result.west = west;
  3679. result.south = Math.min(rectangle.south, otherRectangle.south);
  3680. result.east = east;
  3681. result.north = Math.max(rectangle.north, otherRectangle.north);
  3682. return result;
  3683. };
  3684. /**
  3685. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  3686. *
  3687. * @param {Rectangle} rectangle A rectangle to expand.
  3688. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  3689. * @param {Rectangle} [result] The object onto which to store the result.
  3690. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  3691. */
  3692. Rectangle.expand = function(rectangle, cartographic, result) {
  3693. if (!defined(rectangle)) {
  3694. throw new DeveloperError('rectangle is required.');
  3695. }
  3696. if (!defined(cartographic)) {
  3697. throw new DeveloperError('cartographic is required.');
  3698. }
  3699. if (!defined(result)) {
  3700. result = new Rectangle();
  3701. }
  3702. result.west = Math.min(rectangle.west, cartographic.longitude);
  3703. result.south = Math.min(rectangle.south, cartographic.latitude);
  3704. result.east = Math.max(rectangle.east, cartographic.longitude);
  3705. result.north = Math.max(rectangle.north, cartographic.latitude);
  3706. return result;
  3707. };
  3708. /**
  3709. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  3710. *
  3711. * @param {Rectangle} rectangle The rectangle
  3712. * @param {Cartographic} cartographic The cartographic to test.
  3713. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  3714. */
  3715. Rectangle.contains = function(rectangle, cartographic) {
  3716. if (!defined(rectangle)) {
  3717. throw new DeveloperError('rectangle is required');
  3718. }
  3719. if (!defined(cartographic)) {
  3720. throw new DeveloperError('cartographic is required.');
  3721. }
  3722. var longitude = cartographic.longitude;
  3723. var latitude = cartographic.latitude;
  3724. var west = rectangle.west;
  3725. var east = rectangle.east;
  3726. if (east < west) {
  3727. east += CesiumMath.TWO_PI;
  3728. if (longitude < 0.0) {
  3729. longitude += CesiumMath.TWO_PI;
  3730. }
  3731. }
  3732. return (longitude > west || CesiumMath.equalsEpsilon(longitude, west, CesiumMath.EPSILON14)) &&
  3733. (longitude < east || CesiumMath.equalsEpsilon(longitude, east, CesiumMath.EPSILON14)) &&
  3734. latitude >= rectangle.south &&
  3735. latitude <= rectangle.north;
  3736. };
  3737. var subsampleLlaScratch = new Cartographic();
  3738. /**
  3739. * Samples an rectangle so that it includes a list of Cartesian points suitable for passing to
  3740. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  3741. * for rectangles that cover the poles or cross the equator.
  3742. *
  3743. * @param {Rectangle} rectangle The rectangle to subsample.
  3744. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  3745. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  3746. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  3747. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  3748. */
  3749. Rectangle.subsample = function(rectangle, ellipsoid, surfaceHeight, result) {
  3750. if (!defined(rectangle)) {
  3751. throw new DeveloperError('rectangle is required');
  3752. }
  3753. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  3754. surfaceHeight = defaultValue(surfaceHeight, 0.0);
  3755. if (!defined(result)) {
  3756. result = [];
  3757. }
  3758. var length = 0;
  3759. var north = rectangle.north;
  3760. var south = rectangle.south;
  3761. var east = rectangle.east;
  3762. var west = rectangle.west;
  3763. var lla = subsampleLlaScratch;
  3764. lla.height = surfaceHeight;
  3765. lla.longitude = west;
  3766. lla.latitude = north;
  3767. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  3768. length++;
  3769. lla.longitude = east;
  3770. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  3771. length++;
  3772. lla.latitude = south;
  3773. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  3774. length++;
  3775. lla.longitude = west;
  3776. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  3777. length++;
  3778. if (north < 0.0) {
  3779. lla.latitude = north;
  3780. } else if (south > 0.0) {
  3781. lla.latitude = south;
  3782. } else {
  3783. lla.latitude = 0.0;
  3784. }
  3785. for ( var i = 1; i < 8; ++i) {
  3786. lla.longitude = -Math.PI + i * CesiumMath.PI_OVER_TWO;
  3787. if (Rectangle.contains(rectangle, lla)) {
  3788. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  3789. length++;
  3790. }
  3791. }
  3792. if (lla.latitude === 0.0) {
  3793. lla.longitude = west;
  3794. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  3795. length++;
  3796. lla.longitude = east;
  3797. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  3798. length++;
  3799. }
  3800. result.length = length;
  3801. return result;
  3802. };
  3803. /**
  3804. * The largest possible rectangle.
  3805. *
  3806. * @type {Rectangle}
  3807. * @constant
  3808. */
  3809. Rectangle.MAX_VALUE = freezeObject(new Rectangle(-Math.PI, -CesiumMath.PI_OVER_TWO, Math.PI, CesiumMath.PI_OVER_TWO));
  3810. return Rectangle;
  3811. });
  3812. /*global define*/
  3813. define('Core/GeographicProjection',[
  3814. './Cartesian3',
  3815. './Cartographic',
  3816. './defaultValue',
  3817. './defined',
  3818. './defineProperties',
  3819. './DeveloperError',
  3820. './Ellipsoid'
  3821. ], function(
  3822. Cartesian3,
  3823. Cartographic,
  3824. defaultValue,
  3825. defined,
  3826. defineProperties,
  3827. DeveloperError,
  3828. Ellipsoid) {
  3829. 'use strict';
  3830. /**
  3831. * A simple map projection where longitude and latitude are linearly mapped to X and Y by multiplying
  3832. * them by the {@link Ellipsoid#maximumRadius}. This projection
  3833. * is commonly known as geographic, equirectangular, equidistant cylindrical, or plate carrée. It
  3834. * is also known as EPSG:4326.
  3835. *
  3836. * @alias GeographicProjection
  3837. * @constructor
  3838. *
  3839. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  3840. *
  3841. * @see WebMercatorProjection
  3842. */
  3843. function GeographicProjection(ellipsoid) {
  3844. this._ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  3845. this._semimajorAxis = this._ellipsoid.maximumRadius;
  3846. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  3847. }
  3848. defineProperties(GeographicProjection.prototype, {
  3849. /**
  3850. * Gets the {@link Ellipsoid}.
  3851. *
  3852. * @memberof GeographicProjection.prototype
  3853. *
  3854. * @type {Ellipsoid}
  3855. * @readonly
  3856. */
  3857. ellipsoid : {
  3858. get : function() {
  3859. return this._ellipsoid;
  3860. }
  3861. }
  3862. });
  3863. /**
  3864. * Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters.
  3865. * X and Y are the longitude and latitude, respectively, multiplied by the maximum radius of the
  3866. * ellipsoid. Z is the unmodified height.
  3867. *
  3868. * @param {Cartographic} cartographic The coordinates to project.
  3869. * @param {Cartesian3} [result] An instance into which to copy the result. If this parameter is
  3870. * undefined, a new instance is created and returned.
  3871. * @returns {Cartesian3} The projected coordinates. If the result parameter is not undefined, the
  3872. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  3873. * created and returned.
  3874. */
  3875. GeographicProjection.prototype.project = function(cartographic, result) {
  3876. // Actually this is the special case of equidistant cylindrical called the plate carree
  3877. var semimajorAxis = this._semimajorAxis;
  3878. var x = cartographic.longitude * semimajorAxis;
  3879. var y = cartographic.latitude * semimajorAxis;
  3880. var z = cartographic.height;
  3881. if (!defined(result)) {
  3882. return new Cartesian3(x, y, z);
  3883. }
  3884. result.x = x;
  3885. result.y = y;
  3886. result.z = z;
  3887. return result;
  3888. };
  3889. /**
  3890. * Unprojects a set of projected {@link Cartesian3} coordinates, in meters, to {@link Cartographic}
  3891. * coordinates, in radians. Longitude and Latitude are the X and Y coordinates, respectively,
  3892. * divided by the maximum radius of the ellipsoid. Height is the unmodified Z coordinate.
  3893. *
  3894. * @param {Cartesian3} cartesian The Cartesian position to unproject with height (z) in meters.
  3895. * @param {Cartographic} [result] An instance into which to copy the result. If this parameter is
  3896. * undefined, a new instance is created and returned.
  3897. * @returns {Cartographic} The unprojected coordinates. If the result parameter is not undefined, the
  3898. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  3899. * created and returned.
  3900. */
  3901. GeographicProjection.prototype.unproject = function(cartesian, result) {
  3902. if (!defined(cartesian)) {
  3903. throw new DeveloperError('cartesian is required');
  3904. }
  3905. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  3906. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  3907. var latitude = cartesian.y * oneOverEarthSemimajorAxis;
  3908. var height = cartesian.z;
  3909. if (!defined(result)) {
  3910. return new Cartographic(longitude, latitude, height);
  3911. }
  3912. result.longitude = longitude;
  3913. result.latitude = latitude;
  3914. result.height = height;
  3915. return result;
  3916. };
  3917. return GeographicProjection;
  3918. });
  3919. /*global define*/
  3920. define('Core/Intersect',[
  3921. './freezeObject'
  3922. ], function(
  3923. freezeObject) {
  3924. 'use strict';
  3925. /**
  3926. * This enumerated type is used in determining where, relative to the frustum, an
  3927. * object is located. The object can either be fully contained within the frustum (INSIDE),
  3928. * partially inside the frustum and partially outside (INTERSECTING), or somwhere entirely
  3929. * outside of the frustum's 6 planes (OUTSIDE).
  3930. *
  3931. * @exports Intersect
  3932. */
  3933. var Intersect = {
  3934. /**
  3935. * Represents that an object is not contained within the frustum.
  3936. *
  3937. * @type {Number}
  3938. * @constant
  3939. */
  3940. OUTSIDE : -1,
  3941. /**
  3942. * Represents that an object intersects one of the frustum's planes.
  3943. *
  3944. * @type {Number}
  3945. * @constant
  3946. */
  3947. INTERSECTING : 0,
  3948. /**
  3949. * Represents that an object is fully within the frustum.
  3950. *
  3951. * @type {Number}
  3952. * @constant
  3953. */
  3954. INSIDE : 1
  3955. };
  3956. return freezeObject(Intersect);
  3957. });
  3958. /*global define*/
  3959. define('Core/Interval',[
  3960. './defaultValue'
  3961. ], function(
  3962. defaultValue) {
  3963. 'use strict';
  3964. /**
  3965. * Represents the closed interval [start, stop].
  3966. * @alias Interval
  3967. * @constructor
  3968. *
  3969. * @param {Number} [start=0.0] The beginning of the interval.
  3970. * @param {Number} [stop=0.0] The end of the interval.
  3971. */
  3972. function Interval(start, stop) {
  3973. /**
  3974. * The beginning of the interval.
  3975. * @type {Number}
  3976. * @default 0.0
  3977. */
  3978. this.start = defaultValue(start, 0.0);
  3979. /**
  3980. * The end of the interval.
  3981. * @type {Number}
  3982. * @default 0.0
  3983. */
  3984. this.stop = defaultValue(stop, 0.0);
  3985. }
  3986. return Interval;
  3987. });
  3988. /*global define*/
  3989. define('Core/Matrix3',[
  3990. './Cartesian3',
  3991. './defaultValue',
  3992. './defined',
  3993. './defineProperties',
  3994. './DeveloperError',
  3995. './freezeObject',
  3996. './Math'
  3997. ], function(
  3998. Cartesian3,
  3999. defaultValue,
  4000. defined,
  4001. defineProperties,
  4002. DeveloperError,
  4003. freezeObject,
  4004. CesiumMath) {
  4005. 'use strict';
  4006. /**
  4007. * A 3x3 matrix, indexable as a column-major order array.
  4008. * Constructor parameters are in row-major order for code readability.
  4009. * @alias Matrix3
  4010. * @constructor
  4011. *
  4012. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  4013. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  4014. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  4015. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  4016. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  4017. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  4018. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  4019. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  4020. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  4021. *
  4022. * @see Matrix3.fromColumnMajorArray
  4023. * @see Matrix3.fromRowMajorArray
  4024. * @see Matrix3.fromQuaternion
  4025. * @see Matrix3.fromScale
  4026. * @see Matrix3.fromUniformScale
  4027. * @see Matrix2
  4028. * @see Matrix4
  4029. */
  4030. function Matrix3(column0Row0, column1Row0, column2Row0,
  4031. column0Row1, column1Row1, column2Row1,
  4032. column0Row2, column1Row2, column2Row2) {
  4033. this[0] = defaultValue(column0Row0, 0.0);
  4034. this[1] = defaultValue(column0Row1, 0.0);
  4035. this[2] = defaultValue(column0Row2, 0.0);
  4036. this[3] = defaultValue(column1Row0, 0.0);
  4037. this[4] = defaultValue(column1Row1, 0.0);
  4038. this[5] = defaultValue(column1Row2, 0.0);
  4039. this[6] = defaultValue(column2Row0, 0.0);
  4040. this[7] = defaultValue(column2Row1, 0.0);
  4041. this[8] = defaultValue(column2Row2, 0.0);
  4042. }
  4043. /**
  4044. * The number of elements used to pack the object into an array.
  4045. * @type {Number}
  4046. */
  4047. Matrix3.packedLength = 9;
  4048. /**
  4049. * Stores the provided instance into the provided array.
  4050. *
  4051. * @param {Matrix3} value The value to pack.
  4052. * @param {Number[]} array The array to pack into.
  4053. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  4054. *
  4055. * @returns {Number[]} The array that was packed into
  4056. */
  4057. Matrix3.pack = function(value, array, startingIndex) {
  4058. if (!defined(value)) {
  4059. throw new DeveloperError('value is required');
  4060. }
  4061. if (!defined(array)) {
  4062. throw new DeveloperError('array is required');
  4063. }
  4064. startingIndex = defaultValue(startingIndex, 0);
  4065. array[startingIndex++] = value[0];
  4066. array[startingIndex++] = value[1];
  4067. array[startingIndex++] = value[2];
  4068. array[startingIndex++] = value[3];
  4069. array[startingIndex++] = value[4];
  4070. array[startingIndex++] = value[5];
  4071. array[startingIndex++] = value[6];
  4072. array[startingIndex++] = value[7];
  4073. array[startingIndex++] = value[8];
  4074. return array;
  4075. };
  4076. /**
  4077. * Retrieves an instance from a packed array.
  4078. *
  4079. * @param {Number[]} array The packed array.
  4080. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  4081. * @param {Matrix3} [result] The object into which to store the result.
  4082. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  4083. */
  4084. Matrix3.unpack = function(array, startingIndex, result) {
  4085. if (!defined(array)) {
  4086. throw new DeveloperError('array is required');
  4087. }
  4088. startingIndex = defaultValue(startingIndex, 0);
  4089. if (!defined(result)) {
  4090. result = new Matrix3();
  4091. }
  4092. result[0] = array[startingIndex++];
  4093. result[1] = array[startingIndex++];
  4094. result[2] = array[startingIndex++];
  4095. result[3] = array[startingIndex++];
  4096. result[4] = array[startingIndex++];
  4097. result[5] = array[startingIndex++];
  4098. result[6] = array[startingIndex++];
  4099. result[7] = array[startingIndex++];
  4100. result[8] = array[startingIndex++];
  4101. return result;
  4102. };
  4103. /**
  4104. * Duplicates a Matrix3 instance.
  4105. *
  4106. * @param {Matrix3} matrix The matrix to duplicate.
  4107. * @param {Matrix3} [result] The object onto which to store the result.
  4108. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  4109. */
  4110. Matrix3.clone = function(values, result) {
  4111. if (!defined(values)) {
  4112. return undefined;
  4113. }
  4114. if (!defined(result)) {
  4115. return new Matrix3(values[0], values[3], values[6],
  4116. values[1], values[4], values[7],
  4117. values[2], values[5], values[8]);
  4118. }
  4119. result[0] = values[0];
  4120. result[1] = values[1];
  4121. result[2] = values[2];
  4122. result[3] = values[3];
  4123. result[4] = values[4];
  4124. result[5] = values[5];
  4125. result[6] = values[6];
  4126. result[7] = values[7];
  4127. result[8] = values[8];
  4128. return result;
  4129. };
  4130. /**
  4131. * Creates a Matrix3 from 9 consecutive elements in an array.
  4132. *
  4133. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  4134. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  4135. * @param {Matrix3} [result] The object onto which to store the result.
  4136. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  4137. *
  4138. * @example
  4139. * // Create the Matrix3:
  4140. * // [1.0, 2.0, 3.0]
  4141. * // [1.0, 2.0, 3.0]
  4142. * // [1.0, 2.0, 3.0]
  4143. *
  4144. * var v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  4145. * var m = Cesium.Matrix3.fromArray(v);
  4146. *
  4147. * // Create same Matrix3 with using an offset into an array
  4148. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  4149. * var m2 = Cesium.Matrix3.fromArray(v2, 2);
  4150. */
  4151. Matrix3.fromArray = function(array, startingIndex, result) {
  4152. if (!defined(array)) {
  4153. throw new DeveloperError('array is required');
  4154. }
  4155. startingIndex = defaultValue(startingIndex, 0);
  4156. if (!defined(result)) {
  4157. result = new Matrix3();
  4158. }
  4159. result[0] = array[startingIndex];
  4160. result[1] = array[startingIndex + 1];
  4161. result[2] = array[startingIndex + 2];
  4162. result[3] = array[startingIndex + 3];
  4163. result[4] = array[startingIndex + 4];
  4164. result[5] = array[startingIndex + 5];
  4165. result[6] = array[startingIndex + 6];
  4166. result[7] = array[startingIndex + 7];
  4167. result[8] = array[startingIndex + 8];
  4168. return result;
  4169. };
  4170. /**
  4171. * Creates a Matrix3 instance from a column-major order array.
  4172. *
  4173. * @param {Number[]} values The column-major order array.
  4174. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4175. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4176. */
  4177. Matrix3.fromColumnMajorArray = function(values, result) {
  4178. if (!defined(values)) {
  4179. throw new DeveloperError('values parameter is required');
  4180. }
  4181. return Matrix3.clone(values, result);
  4182. };
  4183. /**
  4184. * Creates a Matrix3 instance from a row-major order array.
  4185. * The resulting matrix will be in column-major order.
  4186. *
  4187. * @param {Number[]} values The row-major order array.
  4188. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4189. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4190. */
  4191. Matrix3.fromRowMajorArray = function(values, result) {
  4192. if (!defined(values)) {
  4193. throw new DeveloperError('values is required.');
  4194. }
  4195. if (!defined(result)) {
  4196. return new Matrix3(values[0], values[1], values[2],
  4197. values[3], values[4], values[5],
  4198. values[6], values[7], values[8]);
  4199. }
  4200. result[0] = values[0];
  4201. result[1] = values[3];
  4202. result[2] = values[6];
  4203. result[3] = values[1];
  4204. result[4] = values[4];
  4205. result[5] = values[7];
  4206. result[6] = values[2];
  4207. result[7] = values[5];
  4208. result[8] = values[8];
  4209. return result;
  4210. };
  4211. /**
  4212. * Computes a 3x3 rotation matrix from the provided quaternion.
  4213. *
  4214. * @param {Quaternion} quaternion the quaternion to use.
  4215. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4216. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  4217. */
  4218. Matrix3.fromQuaternion = function(quaternion, result) {
  4219. if (!defined(quaternion)) {
  4220. throw new DeveloperError('quaternion is required');
  4221. }
  4222. var x2 = quaternion.x * quaternion.x;
  4223. var xy = quaternion.x * quaternion.y;
  4224. var xz = quaternion.x * quaternion.z;
  4225. var xw = quaternion.x * quaternion.w;
  4226. var y2 = quaternion.y * quaternion.y;
  4227. var yz = quaternion.y * quaternion.z;
  4228. var yw = quaternion.y * quaternion.w;
  4229. var z2 = quaternion.z * quaternion.z;
  4230. var zw = quaternion.z * quaternion.w;
  4231. var w2 = quaternion.w * quaternion.w;
  4232. var m00 = x2 - y2 - z2 + w2;
  4233. var m01 = 2.0 * (xy - zw);
  4234. var m02 = 2.0 * (xz + yw);
  4235. var m10 = 2.0 * (xy + zw);
  4236. var m11 = -x2 + y2 - z2 + w2;
  4237. var m12 = 2.0 * (yz - xw);
  4238. var m20 = 2.0 * (xz - yw);
  4239. var m21 = 2.0 * (yz + xw);
  4240. var m22 = -x2 - y2 + z2 + w2;
  4241. if (!defined(result)) {
  4242. return new Matrix3(m00, m01, m02,
  4243. m10, m11, m12,
  4244. m20, m21, m22);
  4245. }
  4246. result[0] = m00;
  4247. result[1] = m10;
  4248. result[2] = m20;
  4249. result[3] = m01;
  4250. result[4] = m11;
  4251. result[5] = m21;
  4252. result[6] = m02;
  4253. result[7] = m12;
  4254. result[8] = m22;
  4255. return result;
  4256. };
  4257. /**
  4258. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  4259. *
  4260. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  4261. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4262. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  4263. */
  4264. Matrix3.fromHeadingPitchRoll = function(headingPitchRoll, result) {
  4265. if (!defined(headingPitchRoll)) {
  4266. throw new DeveloperError('headingPitchRoll is required');
  4267. }
  4268. var cosTheta = Math.cos(-headingPitchRoll.pitch);
  4269. var cosPsi = Math.cos(-headingPitchRoll.heading);
  4270. var cosPhi = Math.cos(headingPitchRoll.roll);
  4271. var sinTheta = Math.sin(-headingPitchRoll.pitch);
  4272. var sinPsi = Math.sin(-headingPitchRoll.heading);
  4273. var sinPhi = Math.sin(headingPitchRoll.roll);
  4274. var m00 = cosTheta * cosPsi;
  4275. var m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  4276. var m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  4277. var m10 = cosTheta * sinPsi;
  4278. var m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  4279. var m12 = -sinTheta * cosPhi + cosPhi * sinTheta * sinPsi;
  4280. var m20 = -sinTheta;
  4281. var m21 = sinPhi * cosTheta;
  4282. var m22 = cosPhi * cosTheta;
  4283. if (!defined(result)) {
  4284. return new Matrix3(m00, m01, m02,
  4285. m10, m11, m12,
  4286. m20, m21, m22);
  4287. }
  4288. result[0] = m00;
  4289. result[1] = m10;
  4290. result[2] = m20;
  4291. result[3] = m01;
  4292. result[4] = m11;
  4293. result[5] = m21;
  4294. result[6] = m02;
  4295. result[7] = m12;
  4296. result[8] = m22;
  4297. return result;
  4298. };
  4299. /**
  4300. * Computes a Matrix3 instance representing a non-uniform scale.
  4301. *
  4302. * @param {Cartesian3} scale The x, y, and z scale factors.
  4303. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4304. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4305. *
  4306. * @example
  4307. * // Creates
  4308. * // [7.0, 0.0, 0.0]
  4309. * // [0.0, 8.0, 0.0]
  4310. * // [0.0, 0.0, 9.0]
  4311. * var m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  4312. */
  4313. Matrix3.fromScale = function(scale, result) {
  4314. if (!defined(scale)) {
  4315. throw new DeveloperError('scale is required.');
  4316. }
  4317. if (!defined(result)) {
  4318. return new Matrix3(
  4319. scale.x, 0.0, 0.0,
  4320. 0.0, scale.y, 0.0,
  4321. 0.0, 0.0, scale.z);
  4322. }
  4323. result[0] = scale.x;
  4324. result[1] = 0.0;
  4325. result[2] = 0.0;
  4326. result[3] = 0.0;
  4327. result[4] = scale.y;
  4328. result[5] = 0.0;
  4329. result[6] = 0.0;
  4330. result[7] = 0.0;
  4331. result[8] = scale.z;
  4332. return result;
  4333. };
  4334. /**
  4335. * Computes a Matrix3 instance representing a uniform scale.
  4336. *
  4337. * @param {Number} scale The uniform scale factor.
  4338. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4339. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4340. *
  4341. * @example
  4342. * // Creates
  4343. * // [2.0, 0.0, 0.0]
  4344. * // [0.0, 2.0, 0.0]
  4345. * // [0.0, 0.0, 2.0]
  4346. * var m = Cesium.Matrix3.fromUniformScale(2.0);
  4347. */
  4348. Matrix3.fromUniformScale = function(scale, result) {
  4349. if (typeof scale !== 'number') {
  4350. throw new DeveloperError('scale is required.');
  4351. }
  4352. if (!defined(result)) {
  4353. return new Matrix3(
  4354. scale, 0.0, 0.0,
  4355. 0.0, scale, 0.0,
  4356. 0.0, 0.0, scale);
  4357. }
  4358. result[0] = scale;
  4359. result[1] = 0.0;
  4360. result[2] = 0.0;
  4361. result[3] = 0.0;
  4362. result[4] = scale;
  4363. result[5] = 0.0;
  4364. result[6] = 0.0;
  4365. result[7] = 0.0;
  4366. result[8] = scale;
  4367. return result;
  4368. };
  4369. /**
  4370. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  4371. *
  4372. * @param {Cartesian3} the vector on the left hand side of the cross product operation.
  4373. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4374. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4375. *
  4376. * @example
  4377. * // Creates
  4378. * // [0.0, -9.0, 8.0]
  4379. * // [9.0, 0.0, -7.0]
  4380. * // [-8.0, 7.0, 0.0]
  4381. * var m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  4382. */
  4383. Matrix3.fromCrossProduct = function(vector, result) {
  4384. if (!defined(vector)) {
  4385. throw new DeveloperError('vector is required.');
  4386. }
  4387. if (!defined(result)) {
  4388. return new Matrix3(
  4389. 0.0, -vector.z, vector.y,
  4390. vector.z, 0.0, -vector.x,
  4391. -vector.y, vector.x, 0.0);
  4392. }
  4393. result[0] = 0.0;
  4394. result[1] = vector.z;
  4395. result[2] = -vector.y;
  4396. result[3] = -vector.z;
  4397. result[4] = 0.0;
  4398. result[5] = vector.x;
  4399. result[6] = vector.y;
  4400. result[7] = -vector.x;
  4401. result[8] = 0.0;
  4402. return result;
  4403. };
  4404. /**
  4405. * Creates a rotation matrix around the x-axis.
  4406. *
  4407. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  4408. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4409. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4410. *
  4411. * @example
  4412. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  4413. * var p = new Cesium.Cartesian3(5, 6, 7);
  4414. * var m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  4415. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  4416. */
  4417. Matrix3.fromRotationX = function(angle, result) {
  4418. if (!defined(angle)) {
  4419. throw new DeveloperError('angle is required.');
  4420. }
  4421. var cosAngle = Math.cos(angle);
  4422. var sinAngle = Math.sin(angle);
  4423. if (!defined(result)) {
  4424. return new Matrix3(
  4425. 1.0, 0.0, 0.0,
  4426. 0.0, cosAngle, -sinAngle,
  4427. 0.0, sinAngle, cosAngle);
  4428. }
  4429. result[0] = 1.0;
  4430. result[1] = 0.0;
  4431. result[2] = 0.0;
  4432. result[3] = 0.0;
  4433. result[4] = cosAngle;
  4434. result[5] = sinAngle;
  4435. result[6] = 0.0;
  4436. result[7] = -sinAngle;
  4437. result[8] = cosAngle;
  4438. return result;
  4439. };
  4440. /**
  4441. * Creates a rotation matrix around the y-axis.
  4442. *
  4443. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  4444. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4445. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4446. *
  4447. * @example
  4448. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  4449. * var p = new Cesium.Cartesian3(5, 6, 7);
  4450. * var m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  4451. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  4452. */
  4453. Matrix3.fromRotationY = function(angle, result) {
  4454. if (!defined(angle)) {
  4455. throw new DeveloperError('angle is required.');
  4456. }
  4457. var cosAngle = Math.cos(angle);
  4458. var sinAngle = Math.sin(angle);
  4459. if (!defined(result)) {
  4460. return new Matrix3(
  4461. cosAngle, 0.0, sinAngle,
  4462. 0.0, 1.0, 0.0,
  4463. -sinAngle, 0.0, cosAngle);
  4464. }
  4465. result[0] = cosAngle;
  4466. result[1] = 0.0;
  4467. result[2] = -sinAngle;
  4468. result[3] = 0.0;
  4469. result[4] = 1.0;
  4470. result[5] = 0.0;
  4471. result[6] = sinAngle;
  4472. result[7] = 0.0;
  4473. result[8] = cosAngle;
  4474. return result;
  4475. };
  4476. /**
  4477. * Creates a rotation matrix around the z-axis.
  4478. *
  4479. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  4480. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4481. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4482. *
  4483. * @example
  4484. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  4485. * var p = new Cesium.Cartesian3(5, 6, 7);
  4486. * var m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  4487. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  4488. */
  4489. Matrix3.fromRotationZ = function(angle, result) {
  4490. if (!defined(angle)) {
  4491. throw new DeveloperError('angle is required.');
  4492. }
  4493. var cosAngle = Math.cos(angle);
  4494. var sinAngle = Math.sin(angle);
  4495. if (!defined(result)) {
  4496. return new Matrix3(
  4497. cosAngle, -sinAngle, 0.0,
  4498. sinAngle, cosAngle, 0.0,
  4499. 0.0, 0.0, 1.0);
  4500. }
  4501. result[0] = cosAngle;
  4502. result[1] = sinAngle;
  4503. result[2] = 0.0;
  4504. result[3] = -sinAngle;
  4505. result[4] = cosAngle;
  4506. result[5] = 0.0;
  4507. result[6] = 0.0;
  4508. result[7] = 0.0;
  4509. result[8] = 1.0;
  4510. return result;
  4511. };
  4512. /**
  4513. * Creates an Array from the provided Matrix3 instance.
  4514. * The array will be in column-major order.
  4515. *
  4516. * @param {Matrix3} matrix The matrix to use..
  4517. * @param {Number[]} [result] The Array onto which to store the result.
  4518. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  4519. */
  4520. Matrix3.toArray = function(matrix, result) {
  4521. if (!defined(matrix)) {
  4522. throw new DeveloperError('matrix is required');
  4523. }
  4524. if (!defined(result)) {
  4525. return [matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7], matrix[8]];
  4526. }
  4527. result[0] = matrix[0];
  4528. result[1] = matrix[1];
  4529. result[2] = matrix[2];
  4530. result[3] = matrix[3];
  4531. result[4] = matrix[4];
  4532. result[5] = matrix[5];
  4533. result[6] = matrix[6];
  4534. result[7] = matrix[7];
  4535. result[8] = matrix[8];
  4536. return result;
  4537. };
  4538. /**
  4539. * Computes the array index of the element at the provided row and column.
  4540. *
  4541. * @param {Number} row The zero-based index of the row.
  4542. * @param {Number} column The zero-based index of the column.
  4543. * @returns {Number} The index of the element at the provided row and column.
  4544. *
  4545. * @exception {DeveloperError} row must be 0, 1, or 2.
  4546. * @exception {DeveloperError} column must be 0, 1, or 2.
  4547. *
  4548. * @example
  4549. * var myMatrix = new Cesium.Matrix3();
  4550. * var column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  4551. * var column1Row0 = myMatrix[column1Row0Index]
  4552. * myMatrix[column1Row0Index] = 10.0;
  4553. */
  4554. Matrix3.getElementIndex = function(column, row) {
  4555. if (typeof row !== 'number' || row < 0 || row > 2) {
  4556. throw new DeveloperError('row must be 0, 1, or 2.');
  4557. }
  4558. if (typeof column !== 'number' || column < 0 || column > 2) {
  4559. throw new DeveloperError('column must be 0, 1, or 2.');
  4560. }
  4561. return column * 3 + row;
  4562. };
  4563. /**
  4564. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  4565. *
  4566. * @param {Matrix3} matrix The matrix to use.
  4567. * @param {Number} index The zero-based index of the column to retrieve.
  4568. * @param {Cartesian3} result The object onto which to store the result.
  4569. * @returns {Cartesian3} The modified result parameter.
  4570. *
  4571. * @exception {DeveloperError} index must be 0, 1, or 2.
  4572. */
  4573. Matrix3.getColumn = function(matrix, index, result) {
  4574. if (!defined(matrix)) {
  4575. throw new DeveloperError('matrix is required.');
  4576. }
  4577. if (typeof index !== 'number' || index < 0 || index > 2) {
  4578. throw new DeveloperError('index must be 0, 1, or 2.');
  4579. }
  4580. if (!defined(result)) {
  4581. throw new DeveloperError('result is required');
  4582. }
  4583. var startIndex = index * 3;
  4584. var x = matrix[startIndex];
  4585. var y = matrix[startIndex + 1];
  4586. var z = matrix[startIndex + 2];
  4587. result.x = x;
  4588. result.y = y;
  4589. result.z = z;
  4590. return result;
  4591. };
  4592. /**
  4593. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  4594. *
  4595. * @param {Matrix3} matrix The matrix to use.
  4596. * @param {Number} index The zero-based index of the column to set.
  4597. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  4598. * @param {Matrix3} result The object onto which to store the result.
  4599. * @returns {Matrix3} The modified result parameter.
  4600. *
  4601. * @exception {DeveloperError} index must be 0, 1, or 2.
  4602. */
  4603. Matrix3.setColumn = function(matrix, index, cartesian, result) {
  4604. if (!defined(matrix)) {
  4605. throw new DeveloperError('matrix is required');
  4606. }
  4607. if (!defined(cartesian)) {
  4608. throw new DeveloperError('cartesian is required');
  4609. }
  4610. if (typeof index !== 'number' || index < 0 || index > 2) {
  4611. throw new DeveloperError('index must be 0, 1, or 2.');
  4612. }
  4613. if (!defined(result)) {
  4614. throw new DeveloperError('result is required');
  4615. }
  4616. result = Matrix3.clone(matrix, result);
  4617. var startIndex = index * 3;
  4618. result[startIndex] = cartesian.x;
  4619. result[startIndex + 1] = cartesian.y;
  4620. result[startIndex + 2] = cartesian.z;
  4621. return result;
  4622. };
  4623. /**
  4624. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  4625. *
  4626. * @param {Matrix3} matrix The matrix to use.
  4627. * @param {Number} index The zero-based index of the row to retrieve.
  4628. * @param {Cartesian3} result The object onto which to store the result.
  4629. * @returns {Cartesian3} The modified result parameter.
  4630. *
  4631. * @exception {DeveloperError} index must be 0, 1, or 2.
  4632. */
  4633. Matrix3.getRow = function(matrix, index, result) {
  4634. if (!defined(matrix)) {
  4635. throw new DeveloperError('matrix is required.');
  4636. }
  4637. if (typeof index !== 'number' || index < 0 || index > 2) {
  4638. throw new DeveloperError('index must be 0, 1, or 2.');
  4639. }
  4640. if (!defined(result)) {
  4641. throw new DeveloperError('result is required');
  4642. }
  4643. var x = matrix[index];
  4644. var y = matrix[index + 3];
  4645. var z = matrix[index + 6];
  4646. result.x = x;
  4647. result.y = y;
  4648. result.z = z;
  4649. return result;
  4650. };
  4651. /**
  4652. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  4653. *
  4654. * @param {Matrix3} matrix The matrix to use.
  4655. * @param {Number} index The zero-based index of the row to set.
  4656. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  4657. * @param {Matrix3} result The object onto which to store the result.
  4658. * @returns {Matrix3} The modified result parameter.
  4659. *
  4660. * @exception {DeveloperError} index must be 0, 1, or 2.
  4661. */
  4662. Matrix3.setRow = function(matrix, index, cartesian, result) {
  4663. if (!defined(matrix)) {
  4664. throw new DeveloperError('matrix is required');
  4665. }
  4666. if (!defined(cartesian)) {
  4667. throw new DeveloperError('cartesian is required');
  4668. }
  4669. if (typeof index !== 'number' || index < 0 || index > 2) {
  4670. throw new DeveloperError('index must be 0, 1, or 2.');
  4671. }
  4672. if (!defined(result)) {
  4673. throw new DeveloperError('result is required');
  4674. }
  4675. result = Matrix3.clone(matrix, result);
  4676. result[index] = cartesian.x;
  4677. result[index + 3] = cartesian.y;
  4678. result[index + 6] = cartesian.z;
  4679. return result;
  4680. };
  4681. var scratchColumn = new Cartesian3();
  4682. /**
  4683. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  4684. *
  4685. * @param {Matrix3} matrix The matrix.
  4686. * @param {Cartesian3} result The object onto which to store the result.
  4687. * @returns {Cartesian3} The modified result parameter.
  4688. */
  4689. Matrix3.getScale = function(matrix, result) {
  4690. if (!defined(matrix)) {
  4691. throw new DeveloperError('matrix is required.');
  4692. }
  4693. if (!defined(result)) {
  4694. throw new DeveloperError('result is required');
  4695. }
  4696. result.x = Cartesian3.magnitude(Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  4697. result.y = Cartesian3.magnitude(Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn));
  4698. result.z = Cartesian3.magnitude(Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn));
  4699. return result;
  4700. };
  4701. var scratchScale = new Cartesian3();
  4702. /**
  4703. * Computes the maximum scale assuming the matrix is an affine transformation.
  4704. * The maximum scale is the maximum length of the column vectors.
  4705. *
  4706. * @param {Matrix3} matrix The matrix.
  4707. * @returns {Number} The maximum scale.
  4708. */
  4709. Matrix3.getMaximumScale = function(matrix) {
  4710. Matrix3.getScale(matrix, scratchScale);
  4711. return Cartesian3.maximumComponent(scratchScale);
  4712. };
  4713. /**
  4714. * Computes the product of two matrices.
  4715. *
  4716. * @param {Matrix3} left The first matrix.
  4717. * @param {Matrix3} right The second matrix.
  4718. * @param {Matrix3} result The object onto which to store the result.
  4719. * @returns {Matrix3} The modified result parameter.
  4720. */
  4721. Matrix3.multiply = function(left, right, result) {
  4722. if (!defined(left)) {
  4723. throw new DeveloperError('left is required');
  4724. }
  4725. if (!defined(right)) {
  4726. throw new DeveloperError('right is required');
  4727. }
  4728. if (!defined(result)) {
  4729. throw new DeveloperError('result is required');
  4730. }
  4731. var column0Row0 = left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  4732. var column0Row1 = left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  4733. var column0Row2 = left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  4734. var column1Row0 = left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  4735. var column1Row1 = left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  4736. var column1Row2 = left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  4737. var column2Row0 = left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  4738. var column2Row1 = left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  4739. var column2Row2 = left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  4740. result[0] = column0Row0;
  4741. result[1] = column0Row1;
  4742. result[2] = column0Row2;
  4743. result[3] = column1Row0;
  4744. result[4] = column1Row1;
  4745. result[5] = column1Row2;
  4746. result[6] = column2Row0;
  4747. result[7] = column2Row1;
  4748. result[8] = column2Row2;
  4749. return result;
  4750. };
  4751. /**
  4752. * Computes the sum of two matrices.
  4753. *
  4754. * @param {Matrix3} left The first matrix.
  4755. * @param {Matrix3} right The second matrix.
  4756. * @param {Matrix3} result The object onto which to store the result.
  4757. * @returns {Matrix3} The modified result parameter.
  4758. */
  4759. Matrix3.add = function(left, right, result) {
  4760. if (!defined(left)) {
  4761. throw new DeveloperError('left is required');
  4762. }
  4763. if (!defined(right)) {
  4764. throw new DeveloperError('right is required');
  4765. }
  4766. if (!defined(result)) {
  4767. throw new DeveloperError('result is required');
  4768. }
  4769. result[0] = left[0] + right[0];
  4770. result[1] = left[1] + right[1];
  4771. result[2] = left[2] + right[2];
  4772. result[3] = left[3] + right[3];
  4773. result[4] = left[4] + right[4];
  4774. result[5] = left[5] + right[5];
  4775. result[6] = left[6] + right[6];
  4776. result[7] = left[7] + right[7];
  4777. result[8] = left[8] + right[8];
  4778. return result;
  4779. };
  4780. /**
  4781. * Computes the difference of two matrices.
  4782. *
  4783. * @param {Matrix3} left The first matrix.
  4784. * @param {Matrix3} right The second matrix.
  4785. * @param {Matrix3} result The object onto which to store the result.
  4786. * @returns {Matrix3} The modified result parameter.
  4787. */
  4788. Matrix3.subtract = function(left, right, result) {
  4789. if (!defined(left)) {
  4790. throw new DeveloperError('left is required');
  4791. }
  4792. if (!defined(right)) {
  4793. throw new DeveloperError('right is required');
  4794. }
  4795. if (!defined(result)) {
  4796. throw new DeveloperError('result is required');
  4797. }
  4798. result[0] = left[0] - right[0];
  4799. result[1] = left[1] - right[1];
  4800. result[2] = left[2] - right[2];
  4801. result[3] = left[3] - right[3];
  4802. result[4] = left[4] - right[4];
  4803. result[5] = left[5] - right[5];
  4804. result[6] = left[6] - right[6];
  4805. result[7] = left[7] - right[7];
  4806. result[8] = left[8] - right[8];
  4807. return result;
  4808. };
  4809. /**
  4810. * Computes the product of a matrix and a column vector.
  4811. *
  4812. * @param {Matrix3} matrix The matrix.
  4813. * @param {Cartesian3} cartesian The column.
  4814. * @param {Cartesian3} result The object onto which to store the result.
  4815. * @returns {Cartesian3} The modified result parameter.
  4816. */
  4817. Matrix3.multiplyByVector = function(matrix, cartesian, result) {
  4818. if (!defined(matrix)) {
  4819. throw new DeveloperError('matrix is required');
  4820. }
  4821. if (!defined(cartesian)) {
  4822. throw new DeveloperError('cartesian is required');
  4823. }
  4824. if (!defined(result)) {
  4825. throw new DeveloperError('result is required');
  4826. }
  4827. var vX = cartesian.x;
  4828. var vY = cartesian.y;
  4829. var vZ = cartesian.z;
  4830. var x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  4831. var y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  4832. var z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  4833. result.x = x;
  4834. result.y = y;
  4835. result.z = z;
  4836. return result;
  4837. };
  4838. /**
  4839. * Computes the product of a matrix and a scalar.
  4840. *
  4841. * @param {Matrix3} matrix The matrix.
  4842. * @param {Number} scalar The number to multiply by.
  4843. * @param {Matrix3} result The object onto which to store the result.
  4844. * @returns {Matrix3} The modified result parameter.
  4845. */
  4846. Matrix3.multiplyByScalar = function(matrix, scalar, result) {
  4847. if (!defined(matrix)) {
  4848. throw new DeveloperError('matrix is required');
  4849. }
  4850. if (typeof scalar !== 'number') {
  4851. throw new DeveloperError('scalar must be a number');
  4852. }
  4853. if (!defined(result)) {
  4854. throw new DeveloperError('result is required');
  4855. }
  4856. result[0] = matrix[0] * scalar;
  4857. result[1] = matrix[1] * scalar;
  4858. result[2] = matrix[2] * scalar;
  4859. result[3] = matrix[3] * scalar;
  4860. result[4] = matrix[4] * scalar;
  4861. result[5] = matrix[5] * scalar;
  4862. result[6] = matrix[6] * scalar;
  4863. result[7] = matrix[7] * scalar;
  4864. result[8] = matrix[8] * scalar;
  4865. return result;
  4866. };
  4867. /**
  4868. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  4869. *
  4870. * @param {Matrix3} matrix The matrix on the left-hand side.
  4871. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  4872. * @param {Matrix3} result The object onto which to store the result.
  4873. * @returns {Matrix3} The modified result parameter.
  4874. *
  4875. *
  4876. * @example
  4877. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  4878. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  4879. *
  4880. * @see Matrix3.fromScale
  4881. * @see Matrix3.multiplyByUniformScale
  4882. */
  4883. Matrix3.multiplyByScale = function(matrix, scale, result) {
  4884. if (!defined(matrix)) {
  4885. throw new DeveloperError('matrix is required');
  4886. }
  4887. if (!defined(scale)) {
  4888. throw new DeveloperError('scale is required');
  4889. }
  4890. if (!defined(result)) {
  4891. throw new DeveloperError('result is required');
  4892. }
  4893. result[0] = matrix[0] * scale.x;
  4894. result[1] = matrix[1] * scale.x;
  4895. result[2] = matrix[2] * scale.x;
  4896. result[3] = matrix[3] * scale.y;
  4897. result[4] = matrix[4] * scale.y;
  4898. result[5] = matrix[5] * scale.y;
  4899. result[6] = matrix[6] * scale.z;
  4900. result[7] = matrix[7] * scale.z;
  4901. result[8] = matrix[8] * scale.z;
  4902. return result;
  4903. };
  4904. /**
  4905. * Creates a negated copy of the provided matrix.
  4906. *
  4907. * @param {Matrix3} matrix The matrix to negate.
  4908. * @param {Matrix3} result The object onto which to store the result.
  4909. * @returns {Matrix3} The modified result parameter.
  4910. */
  4911. Matrix3.negate = function(matrix, result) {
  4912. if (!defined(matrix)) {
  4913. throw new DeveloperError('matrix is required');
  4914. }
  4915. if (!defined(result)) {
  4916. throw new DeveloperError('result is required');
  4917. }
  4918. result[0] = -matrix[0];
  4919. result[1] = -matrix[1];
  4920. result[2] = -matrix[2];
  4921. result[3] = -matrix[3];
  4922. result[4] = -matrix[4];
  4923. result[5] = -matrix[5];
  4924. result[6] = -matrix[6];
  4925. result[7] = -matrix[7];
  4926. result[8] = -matrix[8];
  4927. return result;
  4928. };
  4929. /**
  4930. * Computes the transpose of the provided matrix.
  4931. *
  4932. * @param {Matrix3} matrix The matrix to transpose.
  4933. * @param {Matrix3} result The object onto which to store the result.
  4934. * @returns {Matrix3} The modified result parameter.
  4935. */
  4936. Matrix3.transpose = function(matrix, result) {
  4937. if (!defined(matrix)) {
  4938. throw new DeveloperError('matrix is required');
  4939. }
  4940. if (!defined(result)) {
  4941. throw new DeveloperError('result is required');
  4942. }
  4943. var column0Row0 = matrix[0];
  4944. var column0Row1 = matrix[3];
  4945. var column0Row2 = matrix[6];
  4946. var column1Row0 = matrix[1];
  4947. var column1Row1 = matrix[4];
  4948. var column1Row2 = matrix[7];
  4949. var column2Row0 = matrix[2];
  4950. var column2Row1 = matrix[5];
  4951. var column2Row2 = matrix[8];
  4952. result[0] = column0Row0;
  4953. result[1] = column0Row1;
  4954. result[2] = column0Row2;
  4955. result[3] = column1Row0;
  4956. result[4] = column1Row1;
  4957. result[5] = column1Row2;
  4958. result[6] = column2Row0;
  4959. result[7] = column2Row1;
  4960. result[8] = column2Row2;
  4961. return result;
  4962. };
  4963. function computeFrobeniusNorm(matrix) {
  4964. var norm = 0.0;
  4965. for (var i = 0; i < 9; ++i) {
  4966. var temp = matrix[i];
  4967. norm += temp * temp;
  4968. }
  4969. return Math.sqrt(norm);
  4970. }
  4971. var rowVal = [1, 0, 0];
  4972. var colVal = [2, 2, 1];
  4973. function offDiagonalFrobeniusNorm(matrix) {
  4974. // Computes the "off-diagonal" Frobenius norm.
  4975. // Assumes matrix is symmetric.
  4976. var norm = 0.0;
  4977. for (var i = 0; i < 3; ++i) {
  4978. var temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  4979. norm += 2.0 * temp * temp;
  4980. }
  4981. return Math.sqrt(norm);
  4982. }
  4983. function shurDecomposition(matrix, result) {
  4984. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  4985. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  4986. //
  4987. // The routine takes a matrix, which is assumed to be symmetric, and
  4988. // finds the largest off-diagonal term, and then creates
  4989. // a matrix (result) which can be used to help reduce it
  4990. var tolerance = CesiumMath.EPSILON15;
  4991. var maxDiagonal = 0.0;
  4992. var rotAxis = 1;
  4993. // find pivot (rotAxis) based on max diagonal of matrix
  4994. for (var i = 0; i < 3; ++i) {
  4995. var temp = Math.abs(matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]);
  4996. if (temp > maxDiagonal) {
  4997. rotAxis = i;
  4998. maxDiagonal = temp;
  4999. }
  5000. }
  5001. var c = 1.0;
  5002. var s = 0.0;
  5003. var p = rowVal[rotAxis];
  5004. var q = colVal[rotAxis];
  5005. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  5006. var qq = matrix[Matrix3.getElementIndex(q, q)];
  5007. var pp = matrix[Matrix3.getElementIndex(p, p)];
  5008. var qp = matrix[Matrix3.getElementIndex(q, p)];
  5009. var tau = (qq - pp) / 2.0 / qp;
  5010. var t;
  5011. if (tau < 0.0) {
  5012. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  5013. } else {
  5014. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  5015. }
  5016. c = 1.0 / Math.sqrt(1.0 + t * t);
  5017. s = t * c;
  5018. }
  5019. result = Matrix3.clone(Matrix3.IDENTITY, result);
  5020. result[Matrix3.getElementIndex(p, p)] = result[Matrix3.getElementIndex(q, q)] = c;
  5021. result[Matrix3.getElementIndex(q, p)] = s;
  5022. result[Matrix3.getElementIndex(p, q)] = -s;
  5023. return result;
  5024. }
  5025. var jMatrix = new Matrix3();
  5026. var jMatrixTranspose = new Matrix3();
  5027. /**
  5028. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  5029. * <p>
  5030. * Returns a diagonal matrix and unitary matrix such that:
  5031. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  5032. * </p>
  5033. * <p>
  5034. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  5035. * of the unitary matrix are the corresponding eigenvectors.
  5036. * </p>
  5037. *
  5038. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  5039. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  5040. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  5041. *
  5042. * @example
  5043. * var a = //... symetric matrix
  5044. * var result = {
  5045. * unitary : new Cesium.Matrix3(),
  5046. * diagonal : new Cesium.Matrix3()
  5047. * };
  5048. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  5049. *
  5050. * var unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  5051. * var b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  5052. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  5053. *
  5054. * var lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  5055. * var v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  5056. * var c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  5057. */
  5058. Matrix3.computeEigenDecomposition = function(matrix, result) {
  5059. if (!defined(matrix)) {
  5060. throw new DeveloperError('matrix is required.');
  5061. }
  5062. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  5063. // section 8.4.3 The Classical Jacobi Algorithm
  5064. var tolerance = CesiumMath.EPSILON20;
  5065. var maxSweeps = 10;
  5066. var count = 0;
  5067. var sweep = 0;
  5068. if (!defined(result)) {
  5069. result = {};
  5070. }
  5071. var unitaryMatrix = result.unitary = Matrix3.clone(Matrix3.IDENTITY, result.unitary);
  5072. var diagMatrix = result.diagonal = Matrix3.clone(matrix, result.diagonal);
  5073. var epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  5074. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  5075. shurDecomposition(diagMatrix, jMatrix);
  5076. Matrix3.transpose(jMatrix, jMatrixTranspose);
  5077. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  5078. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  5079. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  5080. if (++count > 2) {
  5081. ++sweep;
  5082. count = 0;
  5083. }
  5084. }
  5085. return result;
  5086. };
  5087. /**
  5088. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  5089. *
  5090. * @param {Matrix3} matrix The matrix with signed elements.
  5091. * @param {Matrix3} result The object onto which to store the result.
  5092. * @returns {Matrix3} The modified result parameter.
  5093. */
  5094. Matrix3.abs = function(matrix, result) {
  5095. if (!defined(matrix)) {
  5096. throw new DeveloperError('matrix is required');
  5097. }
  5098. if (!defined(result)) {
  5099. throw new DeveloperError('result is required');
  5100. }
  5101. result[0] = Math.abs(matrix[0]);
  5102. result[1] = Math.abs(matrix[1]);
  5103. result[2] = Math.abs(matrix[2]);
  5104. result[3] = Math.abs(matrix[3]);
  5105. result[4] = Math.abs(matrix[4]);
  5106. result[5] = Math.abs(matrix[5]);
  5107. result[6] = Math.abs(matrix[6]);
  5108. result[7] = Math.abs(matrix[7]);
  5109. result[8] = Math.abs(matrix[8]);
  5110. return result;
  5111. };
  5112. /**
  5113. * Computes the determinant of the provided matrix.
  5114. *
  5115. * @param {Matrix3} matrix The matrix to use.
  5116. * @returns {Number} The value of the determinant of the matrix.
  5117. */
  5118. Matrix3.determinant = function(matrix) {
  5119. if (!defined(matrix)) {
  5120. throw new DeveloperError('matrix is required');
  5121. }
  5122. var m11 = matrix[0];
  5123. var m21 = matrix[3];
  5124. var m31 = matrix[6];
  5125. var m12 = matrix[1];
  5126. var m22 = matrix[4];
  5127. var m32 = matrix[7];
  5128. var m13 = matrix[2];
  5129. var m23 = matrix[5];
  5130. var m33 = matrix[8];
  5131. return m11 * (m22 * m33 - m23 * m32) + m12 * (m23 * m31 - m21 * m33) + m13 * (m21 * m32 - m22 * m31);
  5132. };
  5133. /**
  5134. * Computes the inverse of the provided matrix.
  5135. *
  5136. * @param {Matrix3} matrix The matrix to invert.
  5137. * @param {Matrix3} result The object onto which to store the result.
  5138. * @returns {Matrix3} The modified result parameter.
  5139. *
  5140. * @exception {DeveloperError} matrix is not invertible.
  5141. */
  5142. Matrix3.inverse = function(matrix, result) {
  5143. if (!defined(matrix)) {
  5144. throw new DeveloperError('matrix is required');
  5145. }
  5146. if (!defined(result)) {
  5147. throw new DeveloperError('result is required');
  5148. }
  5149. var m11 = matrix[0];
  5150. var m21 = matrix[1];
  5151. var m31 = matrix[2];
  5152. var m12 = matrix[3];
  5153. var m22 = matrix[4];
  5154. var m32 = matrix[5];
  5155. var m13 = matrix[6];
  5156. var m23 = matrix[7];
  5157. var m33 = matrix[8];
  5158. var determinant = Matrix3.determinant(matrix);
  5159. if (Math.abs(determinant) <= CesiumMath.EPSILON15) {
  5160. throw new DeveloperError('matrix is not invertible');
  5161. }
  5162. result[0] = m22 * m33 - m23 * m32;
  5163. result[1] = m23 * m31 - m21 * m33;
  5164. result[2] = m21 * m32 - m22 * m31;
  5165. result[3] = m13 * m32 - m12 * m33;
  5166. result[4] = m11 * m33 - m13 * m31;
  5167. result[5] = m12 * m31 - m11 * m32;
  5168. result[6] = m12 * m23 - m13 * m22;
  5169. result[7] = m13 * m21 - m11 * m23;
  5170. result[8] = m11 * m22 - m12 * m21;
  5171. var scale = 1.0 / determinant;
  5172. return Matrix3.multiplyByScalar(result, scale, result);
  5173. };
  5174. /**
  5175. * Compares the provided matrices componentwise and returns
  5176. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5177. *
  5178. * @param {Matrix3} [left] The first matrix.
  5179. * @param {Matrix3} [right] The second matrix.
  5180. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  5181. */
  5182. Matrix3.equals = function(left, right) {
  5183. return (left === right) ||
  5184. (defined(left) &&
  5185. defined(right) &&
  5186. left[0] === right[0] &&
  5187. left[1] === right[1] &&
  5188. left[2] === right[2] &&
  5189. left[3] === right[3] &&
  5190. left[4] === right[4] &&
  5191. left[5] === right[5] &&
  5192. left[6] === right[6] &&
  5193. left[7] === right[7] &&
  5194. left[8] === right[8]);
  5195. };
  5196. /**
  5197. * Compares the provided matrices componentwise and returns
  5198. * <code>true</code> if they are within the provided epsilon,
  5199. * <code>false</code> otherwise.
  5200. *
  5201. * @param {Matrix3} [left] The first matrix.
  5202. * @param {Matrix3} [right] The second matrix.
  5203. * @param {Number} epsilon The epsilon to use for equality testing.
  5204. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  5205. */
  5206. Matrix3.equalsEpsilon = function(left, right, epsilon) {
  5207. if (typeof epsilon !== 'number') {
  5208. throw new DeveloperError('epsilon must be a number');
  5209. }
  5210. return (left === right) ||
  5211. (defined(left) &&
  5212. defined(right) &&
  5213. Math.abs(left[0] - right[0]) <= epsilon &&
  5214. Math.abs(left[1] - right[1]) <= epsilon &&
  5215. Math.abs(left[2] - right[2]) <= epsilon &&
  5216. Math.abs(left[3] - right[3]) <= epsilon &&
  5217. Math.abs(left[4] - right[4]) <= epsilon &&
  5218. Math.abs(left[5] - right[5]) <= epsilon &&
  5219. Math.abs(left[6] - right[6]) <= epsilon &&
  5220. Math.abs(left[7] - right[7]) <= epsilon &&
  5221. Math.abs(left[8] - right[8]) <= epsilon);
  5222. };
  5223. /**
  5224. * An immutable Matrix3 instance initialized to the identity matrix.
  5225. *
  5226. * @type {Matrix3}
  5227. * @constant
  5228. */
  5229. Matrix3.IDENTITY = freezeObject(new Matrix3(1.0, 0.0, 0.0,
  5230. 0.0, 1.0, 0.0,
  5231. 0.0, 0.0, 1.0));
  5232. /**
  5233. * An immutable Matrix3 instance initialized to the zero matrix.
  5234. *
  5235. * @type {Matrix3}
  5236. * @constant
  5237. */
  5238. Matrix3.ZERO = freezeObject(new Matrix3(0.0, 0.0, 0.0,
  5239. 0.0, 0.0, 0.0,
  5240. 0.0, 0.0, 0.0));
  5241. /**
  5242. * The index into Matrix3 for column 0, row 0.
  5243. *
  5244. * @type {Number}
  5245. * @constant
  5246. */
  5247. Matrix3.COLUMN0ROW0 = 0;
  5248. /**
  5249. * The index into Matrix3 for column 0, row 1.
  5250. *
  5251. * @type {Number}
  5252. * @constant
  5253. */
  5254. Matrix3.COLUMN0ROW1 = 1;
  5255. /**
  5256. * The index into Matrix3 for column 0, row 2.
  5257. *
  5258. * @type {Number}
  5259. * @constant
  5260. */
  5261. Matrix3.COLUMN0ROW2 = 2;
  5262. /**
  5263. * The index into Matrix3 for column 1, row 0.
  5264. *
  5265. * @type {Number}
  5266. * @constant
  5267. */
  5268. Matrix3.COLUMN1ROW0 = 3;
  5269. /**
  5270. * The index into Matrix3 for column 1, row 1.
  5271. *
  5272. * @type {Number}
  5273. * @constant
  5274. */
  5275. Matrix3.COLUMN1ROW1 = 4;
  5276. /**
  5277. * The index into Matrix3 for column 1, row 2.
  5278. *
  5279. * @type {Number}
  5280. * @constant
  5281. */
  5282. Matrix3.COLUMN1ROW2 = 5;
  5283. /**
  5284. * The index into Matrix3 for column 2, row 0.
  5285. *
  5286. * @type {Number}
  5287. * @constant
  5288. */
  5289. Matrix3.COLUMN2ROW0 = 6;
  5290. /**
  5291. * The index into Matrix3 for column 2, row 1.
  5292. *
  5293. * @type {Number}
  5294. * @constant
  5295. */
  5296. Matrix3.COLUMN2ROW1 = 7;
  5297. /**
  5298. * The index into Matrix3 for column 2, row 2.
  5299. *
  5300. * @type {Number}
  5301. * @constant
  5302. */
  5303. Matrix3.COLUMN2ROW2 = 8;
  5304. defineProperties(Matrix3.prototype, {
  5305. /**
  5306. * Gets the number of items in the collection.
  5307. * @memberof Matrix3.prototype
  5308. *
  5309. * @type {Number}
  5310. */
  5311. length : {
  5312. get : function() {
  5313. return Matrix3.packedLength;
  5314. }
  5315. }
  5316. });
  5317. /**
  5318. * Duplicates the provided Matrix3 instance.
  5319. *
  5320. * @param {Matrix3} [result] The object onto which to store the result.
  5321. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  5322. */
  5323. Matrix3.prototype.clone = function(result) {
  5324. return Matrix3.clone(this, result);
  5325. };
  5326. /**
  5327. * Compares this matrix to the provided matrix componentwise and returns
  5328. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5329. *
  5330. * @param {Matrix3} [right] The right hand side matrix.
  5331. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  5332. */
  5333. Matrix3.prototype.equals = function(right) {
  5334. return Matrix3.equals(this, right);
  5335. };
  5336. /**
  5337. * @private
  5338. */
  5339. Matrix3.equalsArray = function(matrix, array, offset) {
  5340. return matrix[0] === array[offset] &&
  5341. matrix[1] === array[offset + 1] &&
  5342. matrix[2] === array[offset + 2] &&
  5343. matrix[3] === array[offset + 3] &&
  5344. matrix[4] === array[offset + 4] &&
  5345. matrix[5] === array[offset + 5] &&
  5346. matrix[6] === array[offset + 6] &&
  5347. matrix[7] === array[offset + 7] &&
  5348. matrix[8] === array[offset + 8];
  5349. };
  5350. /**
  5351. * Compares this matrix to the provided matrix componentwise and returns
  5352. * <code>true</code> if they are within the provided epsilon,
  5353. * <code>false</code> otherwise.
  5354. *
  5355. * @param {Matrix3} [right] The right hand side matrix.
  5356. * @param {Number} epsilon The epsilon to use for equality testing.
  5357. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  5358. */
  5359. Matrix3.prototype.equalsEpsilon = function(right, epsilon) {
  5360. return Matrix3.equalsEpsilon(this, right, epsilon);
  5361. };
  5362. /**
  5363. * Creates a string representing this Matrix with each row being
  5364. * on a separate line and in the format '(column0, column1, column2)'.
  5365. *
  5366. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  5367. */
  5368. Matrix3.prototype.toString = function() {
  5369. return '(' + this[0] + ', ' + this[3] + ', ' + this[6] + ')\n' +
  5370. '(' + this[1] + ', ' + this[4] + ', ' + this[7] + ')\n' +
  5371. '(' + this[2] + ', ' + this[5] + ', ' + this[8] + ')';
  5372. };
  5373. return Matrix3;
  5374. });
  5375. /*global define*/
  5376. define('Core/Cartesian4',[
  5377. './defaultValue',
  5378. './defined',
  5379. './DeveloperError',
  5380. './freezeObject',
  5381. './Math'
  5382. ], function(
  5383. defaultValue,
  5384. defined,
  5385. DeveloperError,
  5386. freezeObject,
  5387. CesiumMath) {
  5388. 'use strict';
  5389. /**
  5390. * A 4D Cartesian point.
  5391. * @alias Cartesian4
  5392. * @constructor
  5393. *
  5394. * @param {Number} [x=0.0] The X component.
  5395. * @param {Number} [y=0.0] The Y component.
  5396. * @param {Number} [z=0.0] The Z component.
  5397. * @param {Number} [w=0.0] The W component.
  5398. *
  5399. * @see Cartesian2
  5400. * @see Cartesian3
  5401. * @see Packable
  5402. */
  5403. function Cartesian4(x, y, z, w) {
  5404. /**
  5405. * The X component.
  5406. * @type {Number}
  5407. * @default 0.0
  5408. */
  5409. this.x = defaultValue(x, 0.0);
  5410. /**
  5411. * The Y component.
  5412. * @type {Number}
  5413. * @default 0.0
  5414. */
  5415. this.y = defaultValue(y, 0.0);
  5416. /**
  5417. * The Z component.
  5418. * @type {Number}
  5419. * @default 0.0
  5420. */
  5421. this.z = defaultValue(z, 0.0);
  5422. /**
  5423. * The W component.
  5424. * @type {Number}
  5425. * @default 0.0
  5426. */
  5427. this.w = defaultValue(w, 0.0);
  5428. }
  5429. /**
  5430. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  5431. *
  5432. * @param {Number} x The x coordinate.
  5433. * @param {Number} y The y coordinate.
  5434. * @param {Number} z The z coordinate.
  5435. * @param {Number} w The w coordinate.
  5436. * @param {Cartesian4} [result] The object onto which to store the result.
  5437. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5438. */
  5439. Cartesian4.fromElements = function(x, y, z, w, result) {
  5440. if (!defined(result)) {
  5441. return new Cartesian4(x, y, z, w);
  5442. }
  5443. result.x = x;
  5444. result.y = y;
  5445. result.z = z;
  5446. result.w = w;
  5447. return result;
  5448. };
  5449. /**
  5450. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  5451. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  5452. *
  5453. * @param {Color} color The source color.
  5454. * @param {Cartesian4} [result] The object onto which to store the result.
  5455. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5456. */
  5457. Cartesian4.fromColor = function(color, result) {
  5458. if (!defined(color)) {
  5459. throw new DeveloperError('color is required');
  5460. }
  5461. if (!defined(result)) {
  5462. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  5463. }
  5464. result.x = color.red;
  5465. result.y = color.green;
  5466. result.z = color.blue;
  5467. result.w = color.alpha;
  5468. return result;
  5469. };
  5470. /**
  5471. * Duplicates a Cartesian4 instance.
  5472. *
  5473. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  5474. * @param {Cartesian4} [result] The object onto which to store the result.
  5475. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  5476. */
  5477. Cartesian4.clone = function(cartesian, result) {
  5478. if (!defined(cartesian)) {
  5479. return undefined;
  5480. }
  5481. if (!defined(result)) {
  5482. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  5483. }
  5484. result.x = cartesian.x;
  5485. result.y = cartesian.y;
  5486. result.z = cartesian.z;
  5487. result.w = cartesian.w;
  5488. return result;
  5489. };
  5490. /**
  5491. * The number of elements used to pack the object into an array.
  5492. * @type {Number}
  5493. */
  5494. Cartesian4.packedLength = 4;
  5495. /**
  5496. * Stores the provided instance into the provided array.
  5497. *
  5498. * @param {Cartesian4} value The value to pack.
  5499. * @param {Number[]} array The array to pack into.
  5500. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  5501. *
  5502. * @returns {Number[]} The array that was packed into
  5503. */
  5504. Cartesian4.pack = function(value, array, startingIndex) {
  5505. if (!defined(value)) {
  5506. throw new DeveloperError('value is required');
  5507. }
  5508. if (!defined(array)) {
  5509. throw new DeveloperError('array is required');
  5510. }
  5511. startingIndex = defaultValue(startingIndex, 0);
  5512. array[startingIndex++] = value.x;
  5513. array[startingIndex++] = value.y;
  5514. array[startingIndex++] = value.z;
  5515. array[startingIndex] = value.w;
  5516. return array;
  5517. };
  5518. /**
  5519. * Retrieves an instance from a packed array.
  5520. *
  5521. * @param {Number[]} array The packed array.
  5522. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  5523. * @param {Cartesian4} [result] The object into which to store the result.
  5524. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5525. */
  5526. Cartesian4.unpack = function(array, startingIndex, result) {
  5527. if (!defined(array)) {
  5528. throw new DeveloperError('array is required');
  5529. }
  5530. startingIndex = defaultValue(startingIndex, 0);
  5531. if (!defined(result)) {
  5532. result = new Cartesian4();
  5533. }
  5534. result.x = array[startingIndex++];
  5535. result.y = array[startingIndex++];
  5536. result.z = array[startingIndex++];
  5537. result.w = array[startingIndex];
  5538. return result;
  5539. };
  5540. /**
  5541. * Flattens an array of Cartesian4s into and array of components.
  5542. *
  5543. * @param {Cartesian4[]} array The array of cartesians to pack.
  5544. * @param {Number[]} result The array onto which to store the result.
  5545. * @returns {Number[]} The packed array.
  5546. */
  5547. Cartesian4.packArray = function(array, result) {
  5548. if (!defined(array)) {
  5549. throw new DeveloperError('array is required');
  5550. }
  5551. var length = array.length;
  5552. if (!defined(result)) {
  5553. result = new Array(length * 4);
  5554. } else {
  5555. result.length = length * 4;
  5556. }
  5557. for (var i = 0; i < length; ++i) {
  5558. Cartesian4.pack(array[i], result, i * 4);
  5559. }
  5560. return result;
  5561. };
  5562. /**
  5563. * Unpacks an array of cartesian components into and array of Cartesian4s.
  5564. *
  5565. * @param {Number[]} array The array of components to unpack.
  5566. * @param {Cartesian4[]} result The array onto which to store the result.
  5567. * @returns {Cartesian4[]} The unpacked array.
  5568. */
  5569. Cartesian4.unpackArray = function(array, result) {
  5570. if (!defined(array)) {
  5571. throw new DeveloperError('array is required');
  5572. }
  5573. var length = array.length;
  5574. if (!defined(result)) {
  5575. result = new Array(length / 4);
  5576. } else {
  5577. result.length = length / 4;
  5578. }
  5579. for (var i = 0; i < length; i += 4) {
  5580. var index = i / 4;
  5581. result[index] = Cartesian4.unpack(array, i, result[index]);
  5582. }
  5583. return result;
  5584. };
  5585. /**
  5586. * Creates a Cartesian4 from four consecutive elements in an array.
  5587. * @function
  5588. *
  5589. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  5590. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  5591. * @param {Cartesian4} [result] The object onto which to store the result.
  5592. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5593. *
  5594. * @example
  5595. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  5596. * var v = [1.0, 2.0, 3.0, 4.0];
  5597. * var p = Cesium.Cartesian4.fromArray(v);
  5598. *
  5599. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  5600. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  5601. * var p2 = Cesium.Cartesian4.fromArray(v2, 2);
  5602. */
  5603. Cartesian4.fromArray = Cartesian4.unpack;
  5604. /**
  5605. * Computes the value of the maximum component for the supplied Cartesian.
  5606. *
  5607. * @param {Cartesian4} cartesian The cartesian to use.
  5608. * @returns {Number} The value of the maximum component.
  5609. */
  5610. Cartesian4.maximumComponent = function(cartesian) {
  5611. if (!defined(cartesian)) {
  5612. throw new DeveloperError('cartesian is required');
  5613. }
  5614. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  5615. };
  5616. /**
  5617. * Computes the value of the minimum component for the supplied Cartesian.
  5618. *
  5619. * @param {Cartesian4} cartesian The cartesian to use.
  5620. * @returns {Number} The value of the minimum component.
  5621. */
  5622. Cartesian4.minimumComponent = function(cartesian) {
  5623. if (!defined(cartesian)) {
  5624. throw new DeveloperError('cartesian is required');
  5625. }
  5626. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  5627. };
  5628. /**
  5629. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  5630. *
  5631. * @param {Cartesian4} first A cartesian to compare.
  5632. * @param {Cartesian4} second A cartesian to compare.
  5633. * @param {Cartesian4} result The object into which to store the result.
  5634. * @returns {Cartesian4} A cartesian with the minimum components.
  5635. */
  5636. Cartesian4.minimumByComponent = function(first, second, result) {
  5637. if (!defined(first)) {
  5638. throw new DeveloperError('first is required.');
  5639. }
  5640. if (!defined(second)) {
  5641. throw new DeveloperError('second is required.');
  5642. }
  5643. if (!defined(result)) {
  5644. throw new DeveloperError('result is required.');
  5645. }
  5646. result.x = Math.min(first.x, second.x);
  5647. result.y = Math.min(first.y, second.y);
  5648. result.z = Math.min(first.z, second.z);
  5649. result.w = Math.min(first.w, second.w);
  5650. return result;
  5651. };
  5652. /**
  5653. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  5654. *
  5655. * @param {Cartesian4} first A cartesian to compare.
  5656. * @param {Cartesian4} second A cartesian to compare.
  5657. * @param {Cartesian4} result The object into which to store the result.
  5658. * @returns {Cartesian4} A cartesian with the maximum components.
  5659. */
  5660. Cartesian4.maximumByComponent = function(first, second, result) {
  5661. if (!defined(first)) {
  5662. throw new DeveloperError('first is required.');
  5663. }
  5664. if (!defined(second)) {
  5665. throw new DeveloperError('second is required.');
  5666. }
  5667. if (!defined(result)) {
  5668. throw new DeveloperError('result is required.');
  5669. }
  5670. result.x = Math.max(first.x, second.x);
  5671. result.y = Math.max(first.y, second.y);
  5672. result.z = Math.max(first.z, second.z);
  5673. result.w = Math.max(first.w, second.w);
  5674. return result;
  5675. };
  5676. /**
  5677. * Computes the provided Cartesian's squared magnitude.
  5678. *
  5679. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  5680. * @returns {Number} The squared magnitude.
  5681. */
  5682. Cartesian4.magnitudeSquared = function(cartesian) {
  5683. if (!defined(cartesian)) {
  5684. throw new DeveloperError('cartesian is required');
  5685. }
  5686. return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z + cartesian.w * cartesian.w;
  5687. };
  5688. /**
  5689. * Computes the Cartesian's magnitude (length).
  5690. *
  5691. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  5692. * @returns {Number} The magnitude.
  5693. */
  5694. Cartesian4.magnitude = function(cartesian) {
  5695. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  5696. };
  5697. var distanceScratch = new Cartesian4();
  5698. /**
  5699. * Computes the 4-space distance between two points.
  5700. *
  5701. * @param {Cartesian4} left The first point to compute the distance from.
  5702. * @param {Cartesian4} right The second point to compute the distance to.
  5703. * @returns {Number} The distance between two points.
  5704. *
  5705. * @example
  5706. * // Returns 1.0
  5707. * var d = Cesium.Cartesian4.distance(
  5708. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  5709. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  5710. */
  5711. Cartesian4.distance = function(left, right) {
  5712. if (!defined(left) || !defined(right)) {
  5713. throw new DeveloperError('left and right are required.');
  5714. }
  5715. Cartesian4.subtract(left, right, distanceScratch);
  5716. return Cartesian4.magnitude(distanceScratch);
  5717. };
  5718. /**
  5719. * Computes the squared distance between two points. Comparing squared distances
  5720. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  5721. *
  5722. * @param {Cartesian4} left The first point to compute the distance from.
  5723. * @param {Cartesian4} right The second point to compute the distance to.
  5724. * @returns {Number} The distance between two points.
  5725. *
  5726. * @example
  5727. * // Returns 4.0, not 2.0
  5728. * var d = Cesium.Cartesian4.distance(
  5729. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  5730. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  5731. */
  5732. Cartesian4.distanceSquared = function(left, right) {
  5733. if (!defined(left) || !defined(right)) {
  5734. throw new DeveloperError('left and right are required.');
  5735. }
  5736. Cartesian4.subtract(left, right, distanceScratch);
  5737. return Cartesian4.magnitudeSquared(distanceScratch);
  5738. };
  5739. /**
  5740. * Computes the normalized form of the supplied Cartesian.
  5741. *
  5742. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  5743. * @param {Cartesian4} result The object onto which to store the result.
  5744. * @returns {Cartesian4} The modified result parameter.
  5745. */
  5746. Cartesian4.normalize = function(cartesian, result) {
  5747. if (!defined(cartesian)) {
  5748. throw new DeveloperError('cartesian is required');
  5749. }
  5750. if (!defined(result)) {
  5751. throw new DeveloperError('result is required');
  5752. }
  5753. var magnitude = Cartesian4.magnitude(cartesian);
  5754. result.x = cartesian.x / magnitude;
  5755. result.y = cartesian.y / magnitude;
  5756. result.z = cartesian.z / magnitude;
  5757. result.w = cartesian.w / magnitude;
  5758. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z) || isNaN(result.w)) {
  5759. throw new DeveloperError('normalized result is not a number');
  5760. }
  5761. return result;
  5762. };
  5763. /**
  5764. * Computes the dot (scalar) product of two Cartesians.
  5765. *
  5766. * @param {Cartesian4} left The first Cartesian.
  5767. * @param {Cartesian4} right The second Cartesian.
  5768. * @returns {Number} The dot product.
  5769. */
  5770. Cartesian4.dot = function(left, right) {
  5771. if (!defined(left)) {
  5772. throw new DeveloperError('left is required');
  5773. }
  5774. if (!defined(right)) {
  5775. throw new DeveloperError('right is required');
  5776. }
  5777. return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
  5778. };
  5779. /**
  5780. * Computes the componentwise product of two Cartesians.
  5781. *
  5782. * @param {Cartesian4} left The first Cartesian.
  5783. * @param {Cartesian4} right The second Cartesian.
  5784. * @param {Cartesian4} result The object onto which to store the result.
  5785. * @returns {Cartesian4} The modified result parameter.
  5786. */
  5787. Cartesian4.multiplyComponents = function(left, right, result) {
  5788. if (!defined(left)) {
  5789. throw new DeveloperError('left is required');
  5790. }
  5791. if (!defined(right)) {
  5792. throw new DeveloperError('right is required');
  5793. }
  5794. if (!defined(result)) {
  5795. throw new DeveloperError('result is required');
  5796. }
  5797. result.x = left.x * right.x;
  5798. result.y = left.y * right.y;
  5799. result.z = left.z * right.z;
  5800. result.w = left.w * right.w;
  5801. return result;
  5802. };
  5803. /**
  5804. * Computes the componentwise quotient of two Cartesians.
  5805. *
  5806. * @param {Cartesian4} left The first Cartesian.
  5807. * @param {Cartesian4} right The second Cartesian.
  5808. * @param {Cartesian4} result The object onto which to store the result.
  5809. * @returns {Cartesian4} The modified result parameter.
  5810. */
  5811. Cartesian4.divideComponents = function(left, right, result) {
  5812. if (!defined(left)) {
  5813. throw new DeveloperError('left is required');
  5814. }
  5815. if (!defined(right)) {
  5816. throw new DeveloperError('right is required');
  5817. }
  5818. if (!defined(result)) {
  5819. throw new DeveloperError('result is required');
  5820. }
  5821. result.x = left.x / right.x;
  5822. result.y = left.y / right.y;
  5823. result.z = left.z / right.z;
  5824. result.w = left.w / right.w;
  5825. return result;
  5826. };
  5827. /**
  5828. * Computes the componentwise sum of two Cartesians.
  5829. *
  5830. * @param {Cartesian4} left The first Cartesian.
  5831. * @param {Cartesian4} right The second Cartesian.
  5832. * @param {Cartesian4} result The object onto which to store the result.
  5833. * @returns {Cartesian4} The modified result parameter.
  5834. */
  5835. Cartesian4.add = function(left, right, result) {
  5836. if (!defined(left)) {
  5837. throw new DeveloperError('left is required');
  5838. }
  5839. if (!defined(right)) {
  5840. throw new DeveloperError('right is required');
  5841. }
  5842. if (!defined(result)) {
  5843. throw new DeveloperError('result is required');
  5844. }
  5845. result.x = left.x + right.x;
  5846. result.y = left.y + right.y;
  5847. result.z = left.z + right.z;
  5848. result.w = left.w + right.w;
  5849. return result;
  5850. };
  5851. /**
  5852. * Computes the componentwise difference of two Cartesians.
  5853. *
  5854. * @param {Cartesian4} left The first Cartesian.
  5855. * @param {Cartesian4} right The second Cartesian.
  5856. * @param {Cartesian4} result The object onto which to store the result.
  5857. * @returns {Cartesian4} The modified result parameter.
  5858. */
  5859. Cartesian4.subtract = function(left, right, result) {
  5860. if (!defined(left)) {
  5861. throw new DeveloperError('left is required');
  5862. }
  5863. if (!defined(right)) {
  5864. throw new DeveloperError('right is required');
  5865. }
  5866. if (!defined(result)) {
  5867. throw new DeveloperError('result is required');
  5868. }
  5869. result.x = left.x - right.x;
  5870. result.y = left.y - right.y;
  5871. result.z = left.z - right.z;
  5872. result.w = left.w - right.w;
  5873. return result;
  5874. };
  5875. /**
  5876. * Multiplies the provided Cartesian componentwise by the provided scalar.
  5877. *
  5878. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  5879. * @param {Number} scalar The scalar to multiply with.
  5880. * @param {Cartesian4} result The object onto which to store the result.
  5881. * @returns {Cartesian4} The modified result parameter.
  5882. */
  5883. Cartesian4.multiplyByScalar = function(cartesian, scalar, result) {
  5884. if (!defined(cartesian)) {
  5885. throw new DeveloperError('cartesian is required');
  5886. }
  5887. if (typeof scalar !== 'number') {
  5888. throw new DeveloperError('scalar is required and must be a number.');
  5889. }
  5890. if (!defined(result)) {
  5891. throw new DeveloperError('result is required');
  5892. }
  5893. result.x = cartesian.x * scalar;
  5894. result.y = cartesian.y * scalar;
  5895. result.z = cartesian.z * scalar;
  5896. result.w = cartesian.w * scalar;
  5897. return result;
  5898. };
  5899. /**
  5900. * Divides the provided Cartesian componentwise by the provided scalar.
  5901. *
  5902. * @param {Cartesian4} cartesian The Cartesian to be divided.
  5903. * @param {Number} scalar The scalar to divide by.
  5904. * @param {Cartesian4} result The object onto which to store the result.
  5905. * @returns {Cartesian4} The modified result parameter.
  5906. */
  5907. Cartesian4.divideByScalar = function(cartesian, scalar, result) {
  5908. if (!defined(cartesian)) {
  5909. throw new DeveloperError('cartesian is required');
  5910. }
  5911. if (typeof scalar !== 'number') {
  5912. throw new DeveloperError('scalar is required and must be a number.');
  5913. }
  5914. if (!defined(result)) {
  5915. throw new DeveloperError('result is required');
  5916. }
  5917. result.x = cartesian.x / scalar;
  5918. result.y = cartesian.y / scalar;
  5919. result.z = cartesian.z / scalar;
  5920. result.w = cartesian.w / scalar;
  5921. return result;
  5922. };
  5923. /**
  5924. * Negates the provided Cartesian.
  5925. *
  5926. * @param {Cartesian4} cartesian The Cartesian to be negated.
  5927. * @param {Cartesian4} result The object onto which to store the result.
  5928. * @returns {Cartesian4} The modified result parameter.
  5929. */
  5930. Cartesian4.negate = function(cartesian, result) {
  5931. if (!defined(cartesian)) {
  5932. throw new DeveloperError('cartesian is required');
  5933. }
  5934. if (!defined(result)) {
  5935. throw new DeveloperError('result is required');
  5936. }
  5937. result.x = -cartesian.x;
  5938. result.y = -cartesian.y;
  5939. result.z = -cartesian.z;
  5940. result.w = -cartesian.w;
  5941. return result;
  5942. };
  5943. /**
  5944. * Computes the absolute value of the provided Cartesian.
  5945. *
  5946. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  5947. * @param {Cartesian4} result The object onto which to store the result.
  5948. * @returns {Cartesian4} The modified result parameter.
  5949. */
  5950. Cartesian4.abs = function(cartesian, result) {
  5951. if (!defined(cartesian)) {
  5952. throw new DeveloperError('cartesian is required');
  5953. }
  5954. if (!defined(result)) {
  5955. throw new DeveloperError('result is required');
  5956. }
  5957. result.x = Math.abs(cartesian.x);
  5958. result.y = Math.abs(cartesian.y);
  5959. result.z = Math.abs(cartesian.z);
  5960. result.w = Math.abs(cartesian.w);
  5961. return result;
  5962. };
  5963. var lerpScratch = new Cartesian4();
  5964. /**
  5965. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  5966. *
  5967. * @param {Cartesian4} start The value corresponding to t at 0.0.
  5968. * @param {Cartesian4}end The value corresponding to t at 1.0.
  5969. * @param {Number} t The point along t at which to interpolate.
  5970. * @param {Cartesian4} result The object onto which to store the result.
  5971. * @returns {Cartesian4} The modified result parameter.
  5972. */
  5973. Cartesian4.lerp = function(start, end, t, result) {
  5974. if (!defined(start)) {
  5975. throw new DeveloperError('start is required.');
  5976. }
  5977. if (!defined(end)) {
  5978. throw new DeveloperError('end is required.');
  5979. }
  5980. if (typeof t !== 'number') {
  5981. throw new DeveloperError('t is required and must be a number.');
  5982. }
  5983. if (!defined(result)) {
  5984. throw new DeveloperError('result is required.');
  5985. }
  5986. Cartesian4.multiplyByScalar(end, t, lerpScratch);
  5987. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  5988. return Cartesian4.add(lerpScratch, result, result);
  5989. };
  5990. var mostOrthogonalAxisScratch = new Cartesian4();
  5991. /**
  5992. * Returns the axis that is most orthogonal to the provided Cartesian.
  5993. *
  5994. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  5995. * @param {Cartesian4} result The object onto which to store the result.
  5996. * @returns {Cartesian4} The most orthogonal axis.
  5997. */
  5998. Cartesian4.mostOrthogonalAxis = function(cartesian, result) {
  5999. if (!defined(cartesian)) {
  6000. throw new DeveloperError('cartesian is required.');
  6001. }
  6002. if (!defined(result)) {
  6003. throw new DeveloperError('result is required.');
  6004. }
  6005. var f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch);
  6006. Cartesian4.abs(f, f);
  6007. if (f.x <= f.y) {
  6008. if (f.x <= f.z) {
  6009. if (f.x <= f.w) {
  6010. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  6011. } else {
  6012. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6013. }
  6014. } else if (f.z <= f.w) {
  6015. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  6016. } else {
  6017. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6018. }
  6019. } else if (f.y <= f.z) {
  6020. if (f.y <= f.w) {
  6021. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  6022. } else {
  6023. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6024. }
  6025. } else if (f.z <= f.w) {
  6026. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  6027. } else {
  6028. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6029. }
  6030. return result;
  6031. };
  6032. /**
  6033. * Compares the provided Cartesians componentwise and returns
  6034. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6035. *
  6036. * @param {Cartesian4} [left] The first Cartesian.
  6037. * @param {Cartesian4} [right] The second Cartesian.
  6038. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  6039. */
  6040. Cartesian4.equals = function(left, right) {
  6041. return (left === right) ||
  6042. ((defined(left)) &&
  6043. (defined(right)) &&
  6044. (left.x === right.x) &&
  6045. (left.y === right.y) &&
  6046. (left.z === right.z) &&
  6047. (left.w === right.w));
  6048. };
  6049. /**
  6050. * @private
  6051. */
  6052. Cartesian4.equalsArray = function(cartesian, array, offset) {
  6053. return cartesian.x === array[offset] &&
  6054. cartesian.y === array[offset + 1] &&
  6055. cartesian.z === array[offset + 2] &&
  6056. cartesian.w === array[offset + 3];
  6057. };
  6058. /**
  6059. * Compares the provided Cartesians componentwise and returns
  6060. * <code>true</code> if they pass an absolute or relative tolerance test,
  6061. * <code>false</code> otherwise.
  6062. *
  6063. * @param {Cartesian4} [left] The first Cartesian.
  6064. * @param {Cartesian4} [right] The second Cartesian.
  6065. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  6066. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  6067. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  6068. */
  6069. Cartesian4.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  6070. return (left === right) ||
  6071. (defined(left) &&
  6072. defined(right) &&
  6073. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  6074. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon) &&
  6075. CesiumMath.equalsEpsilon(left.z, right.z, relativeEpsilon, absoluteEpsilon) &&
  6076. CesiumMath.equalsEpsilon(left.w, right.w, relativeEpsilon, absoluteEpsilon));
  6077. };
  6078. /**
  6079. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  6080. *
  6081. * @type {Cartesian4}
  6082. * @constant
  6083. */
  6084. Cartesian4.ZERO = freezeObject(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  6085. /**
  6086. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  6087. *
  6088. * @type {Cartesian4}
  6089. * @constant
  6090. */
  6091. Cartesian4.UNIT_X = freezeObject(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  6092. /**
  6093. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  6094. *
  6095. * @type {Cartesian4}
  6096. * @constant
  6097. */
  6098. Cartesian4.UNIT_Y = freezeObject(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  6099. /**
  6100. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  6101. *
  6102. * @type {Cartesian4}
  6103. * @constant
  6104. */
  6105. Cartesian4.UNIT_Z = freezeObject(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  6106. /**
  6107. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  6108. *
  6109. * @type {Cartesian4}
  6110. * @constant
  6111. */
  6112. Cartesian4.UNIT_W = freezeObject(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  6113. /**
  6114. * Duplicates this Cartesian4 instance.
  6115. *
  6116. * @param {Cartesian4} [result] The object onto which to store the result.
  6117. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  6118. */
  6119. Cartesian4.prototype.clone = function(result) {
  6120. return Cartesian4.clone(this, result);
  6121. };
  6122. /**
  6123. * Compares this Cartesian against the provided Cartesian componentwise and returns
  6124. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6125. *
  6126. * @param {Cartesian4} [right] The right hand side Cartesian.
  6127. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  6128. */
  6129. Cartesian4.prototype.equals = function(right) {
  6130. return Cartesian4.equals(this, right);
  6131. };
  6132. /**
  6133. * Compares this Cartesian against the provided Cartesian componentwise and returns
  6134. * <code>true</code> if they pass an absolute or relative tolerance test,
  6135. * <code>false</code> otherwise.
  6136. *
  6137. * @param {Cartesian4} [right] The right hand side Cartesian.
  6138. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  6139. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  6140. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  6141. */
  6142. Cartesian4.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  6143. return Cartesian4.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  6144. };
  6145. /**
  6146. * Creates a string representing this Cartesian in the format '(x, y)'.
  6147. *
  6148. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  6149. */
  6150. Cartesian4.prototype.toString = function() {
  6151. return '(' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
  6152. };
  6153. return Cartesian4;
  6154. });
  6155. /*global define*/
  6156. define('Core/RuntimeError',[
  6157. './defined'
  6158. ], function(
  6159. defined) {
  6160. 'use strict';
  6161. /**
  6162. * Constructs an exception object that is thrown due to an error that can occur at runtime, e.g.,
  6163. * out of memory, could not compile shader, etc. If a function may throw this
  6164. * exception, the calling code should be prepared to catch it.
  6165. * <br /><br />
  6166. * On the other hand, a {@link DeveloperError} indicates an exception due
  6167. * to a developer error, e.g., invalid argument, that usually indicates a bug in the
  6168. * calling code.
  6169. *
  6170. * @alias RuntimeError
  6171. * @constructor
  6172. * @extends Error
  6173. *
  6174. * @param {String} [message] The error message for this exception.
  6175. *
  6176. * @see DeveloperError
  6177. */
  6178. function RuntimeError(message) {
  6179. /**
  6180. * 'RuntimeError' indicating that this exception was thrown due to a runtime error.
  6181. * @type {String}
  6182. * @readonly
  6183. */
  6184. this.name = 'RuntimeError';
  6185. /**
  6186. * The explanation for why this exception was thrown.
  6187. * @type {String}
  6188. * @readonly
  6189. */
  6190. this.message = message;
  6191. //Browsers such as IE don't have a stack property until you actually throw the error.
  6192. var stack;
  6193. try {
  6194. throw new Error();
  6195. } catch (e) {
  6196. stack = e.stack;
  6197. }
  6198. /**
  6199. * The stack trace of this exception, if available.
  6200. * @type {String}
  6201. * @readonly
  6202. */
  6203. this.stack = stack;
  6204. }
  6205. if (defined(Object.create)) {
  6206. RuntimeError.prototype = Object.create(Error.prototype);
  6207. RuntimeError.prototype.constructor = RuntimeError;
  6208. }
  6209. RuntimeError.prototype.toString = function() {
  6210. var str = this.name + ': ' + this.message;
  6211. if (defined(this.stack)) {
  6212. str += '\n' + this.stack.toString();
  6213. }
  6214. return str;
  6215. };
  6216. return RuntimeError;
  6217. });
  6218. /*global define*/
  6219. define('Core/Matrix4',[
  6220. './Cartesian3',
  6221. './Cartesian4',
  6222. './defaultValue',
  6223. './defined',
  6224. './defineProperties',
  6225. './DeveloperError',
  6226. './freezeObject',
  6227. './Math',
  6228. './Matrix3',
  6229. './RuntimeError'
  6230. ], function(
  6231. Cartesian3,
  6232. Cartesian4,
  6233. defaultValue,
  6234. defined,
  6235. defineProperties,
  6236. DeveloperError,
  6237. freezeObject,
  6238. CesiumMath,
  6239. Matrix3,
  6240. RuntimeError) {
  6241. 'use strict';
  6242. /**
  6243. * A 4x4 matrix, indexable as a column-major order array.
  6244. * Constructor parameters are in row-major order for code readability.
  6245. * @alias Matrix4
  6246. * @constructor
  6247. *
  6248. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  6249. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  6250. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  6251. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  6252. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  6253. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  6254. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  6255. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  6256. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  6257. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  6258. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  6259. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  6260. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  6261. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  6262. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  6263. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  6264. *
  6265. * @see Matrix4.fromColumnMajorArray
  6266. * @see Matrix4.fromRowMajorArray
  6267. * @see Matrix4.fromRotationTranslation
  6268. * @see Matrix4.fromTranslationRotationScale
  6269. * @see Matrix4.fromTranslationQuaternionRotationScale
  6270. * @see Matrix4.fromTranslation
  6271. * @see Matrix4.fromScale
  6272. * @see Matrix4.fromUniformScale
  6273. * @see Matrix4.fromCamera
  6274. * @see Matrix4.computePerspectiveFieldOfView
  6275. * @see Matrix4.computeOrthographicOffCenter
  6276. * @see Matrix4.computePerspectiveOffCenter
  6277. * @see Matrix4.computeInfinitePerspectiveOffCenter
  6278. * @see Matrix4.computeViewportTransformation
  6279. * @see Matrix4.computeView
  6280. * @see Matrix2
  6281. * @see Matrix3
  6282. * @see Packable
  6283. */
  6284. function Matrix4(column0Row0, column1Row0, column2Row0, column3Row0,
  6285. column0Row1, column1Row1, column2Row1, column3Row1,
  6286. column0Row2, column1Row2, column2Row2, column3Row2,
  6287. column0Row3, column1Row3, column2Row3, column3Row3) {
  6288. this[0] = defaultValue(column0Row0, 0.0);
  6289. this[1] = defaultValue(column0Row1, 0.0);
  6290. this[2] = defaultValue(column0Row2, 0.0);
  6291. this[3] = defaultValue(column0Row3, 0.0);
  6292. this[4] = defaultValue(column1Row0, 0.0);
  6293. this[5] = defaultValue(column1Row1, 0.0);
  6294. this[6] = defaultValue(column1Row2, 0.0);
  6295. this[7] = defaultValue(column1Row3, 0.0);
  6296. this[8] = defaultValue(column2Row0, 0.0);
  6297. this[9] = defaultValue(column2Row1, 0.0);
  6298. this[10] = defaultValue(column2Row2, 0.0);
  6299. this[11] = defaultValue(column2Row3, 0.0);
  6300. this[12] = defaultValue(column3Row0, 0.0);
  6301. this[13] = defaultValue(column3Row1, 0.0);
  6302. this[14] = defaultValue(column3Row2, 0.0);
  6303. this[15] = defaultValue(column3Row3, 0.0);
  6304. }
  6305. /**
  6306. * The number of elements used to pack the object into an array.
  6307. * @type {Number}
  6308. */
  6309. Matrix4.packedLength = 16;
  6310. /**
  6311. * Stores the provided instance into the provided array.
  6312. *
  6313. * @param {Matrix4} value The value to pack.
  6314. * @param {Number[]} array The array to pack into.
  6315. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  6316. *
  6317. * @returns {Number[]} The array that was packed into
  6318. */
  6319. Matrix4.pack = function(value, array, startingIndex) {
  6320. if (!defined(value)) {
  6321. throw new DeveloperError('value is required');
  6322. }
  6323. if (!defined(array)) {
  6324. throw new DeveloperError('array is required');
  6325. }
  6326. startingIndex = defaultValue(startingIndex, 0);
  6327. array[startingIndex++] = value[0];
  6328. array[startingIndex++] = value[1];
  6329. array[startingIndex++] = value[2];
  6330. array[startingIndex++] = value[3];
  6331. array[startingIndex++] = value[4];
  6332. array[startingIndex++] = value[5];
  6333. array[startingIndex++] = value[6];
  6334. array[startingIndex++] = value[7];
  6335. array[startingIndex++] = value[8];
  6336. array[startingIndex++] = value[9];
  6337. array[startingIndex++] = value[10];
  6338. array[startingIndex++] = value[11];
  6339. array[startingIndex++] = value[12];
  6340. array[startingIndex++] = value[13];
  6341. array[startingIndex++] = value[14];
  6342. array[startingIndex] = value[15];
  6343. return array;
  6344. };
  6345. /**
  6346. * Retrieves an instance from a packed array.
  6347. *
  6348. * @param {Number[]} array The packed array.
  6349. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  6350. * @param {Matrix4} [result] The object into which to store the result.
  6351. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  6352. */
  6353. Matrix4.unpack = function(array, startingIndex, result) {
  6354. if (!defined(array)) {
  6355. throw new DeveloperError('array is required');
  6356. }
  6357. startingIndex = defaultValue(startingIndex, 0);
  6358. if (!defined(result)) {
  6359. result = new Matrix4();
  6360. }
  6361. result[0] = array[startingIndex++];
  6362. result[1] = array[startingIndex++];
  6363. result[2] = array[startingIndex++];
  6364. result[3] = array[startingIndex++];
  6365. result[4] = array[startingIndex++];
  6366. result[5] = array[startingIndex++];
  6367. result[6] = array[startingIndex++];
  6368. result[7] = array[startingIndex++];
  6369. result[8] = array[startingIndex++];
  6370. result[9] = array[startingIndex++];
  6371. result[10] = array[startingIndex++];
  6372. result[11] = array[startingIndex++];
  6373. result[12] = array[startingIndex++];
  6374. result[13] = array[startingIndex++];
  6375. result[14] = array[startingIndex++];
  6376. result[15] = array[startingIndex];
  6377. return result;
  6378. };
  6379. /**
  6380. * Duplicates a Matrix4 instance.
  6381. *
  6382. * @param {Matrix4} matrix The matrix to duplicate.
  6383. * @param {Matrix4} [result] The object onto which to store the result.
  6384. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  6385. */
  6386. Matrix4.clone = function(matrix, result) {
  6387. if (!defined(matrix)) {
  6388. return undefined;
  6389. }
  6390. if (!defined(result)) {
  6391. return new Matrix4(matrix[0], matrix[4], matrix[8], matrix[12],
  6392. matrix[1], matrix[5], matrix[9], matrix[13],
  6393. matrix[2], matrix[6], matrix[10], matrix[14],
  6394. matrix[3], matrix[7], matrix[11], matrix[15]);
  6395. }
  6396. result[0] = matrix[0];
  6397. result[1] = matrix[1];
  6398. result[2] = matrix[2];
  6399. result[3] = matrix[3];
  6400. result[4] = matrix[4];
  6401. result[5] = matrix[5];
  6402. result[6] = matrix[6];
  6403. result[7] = matrix[7];
  6404. result[8] = matrix[8];
  6405. result[9] = matrix[9];
  6406. result[10] = matrix[10];
  6407. result[11] = matrix[11];
  6408. result[12] = matrix[12];
  6409. result[13] = matrix[13];
  6410. result[14] = matrix[14];
  6411. result[15] = matrix[15];
  6412. return result;
  6413. };
  6414. /**
  6415. * Creates a Matrix4 from 16 consecutive elements in an array.
  6416. * @function
  6417. *
  6418. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  6419. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  6420. * @param {Matrix4} [result] The object onto which to store the result.
  6421. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  6422. *
  6423. * @example
  6424. * // Create the Matrix4:
  6425. * // [1.0, 2.0, 3.0, 4.0]
  6426. * // [1.0, 2.0, 3.0, 4.0]
  6427. * // [1.0, 2.0, 3.0, 4.0]
  6428. * // [1.0, 2.0, 3.0, 4.0]
  6429. *
  6430. * var v = [1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  6431. * var m = Cesium.Matrix4.fromArray(v);
  6432. *
  6433. * // Create same Matrix4 with using an offset into an array
  6434. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  6435. * var m2 = Cesium.Matrix4.fromArray(v2, 2);
  6436. */
  6437. Matrix4.fromArray = Matrix4.unpack;
  6438. /**
  6439. * Computes a Matrix4 instance from a column-major order array.
  6440. *
  6441. * @param {Number[]} values The column-major order array.
  6442. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6443. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6444. */
  6445. Matrix4.fromColumnMajorArray = function(values, result) {
  6446. if (!defined(values)) {
  6447. throw new DeveloperError('values is required');
  6448. }
  6449. return Matrix4.clone(values, result);
  6450. };
  6451. /**
  6452. * Computes a Matrix4 instance from a row-major order array.
  6453. * The resulting matrix will be in column-major order.
  6454. *
  6455. * @param {Number[]} values The row-major order array.
  6456. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6457. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6458. */
  6459. Matrix4.fromRowMajorArray = function(values, result) {
  6460. if (!defined(values)) {
  6461. throw new DeveloperError('values is required.');
  6462. }
  6463. if (!defined(result)) {
  6464. return new Matrix4(values[0], values[1], values[2], values[3],
  6465. values[4], values[5], values[6], values[7],
  6466. values[8], values[9], values[10], values[11],
  6467. values[12], values[13], values[14], values[15]);
  6468. }
  6469. result[0] = values[0];
  6470. result[1] = values[4];
  6471. result[2] = values[8];
  6472. result[3] = values[12];
  6473. result[4] = values[1];
  6474. result[5] = values[5];
  6475. result[6] = values[9];
  6476. result[7] = values[13];
  6477. result[8] = values[2];
  6478. result[9] = values[6];
  6479. result[10] = values[10];
  6480. result[11] = values[14];
  6481. result[12] = values[3];
  6482. result[13] = values[7];
  6483. result[14] = values[11];
  6484. result[15] = values[15];
  6485. return result;
  6486. };
  6487. /**
  6488. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  6489. * and a Cartesian3 representing the translation.
  6490. *
  6491. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  6492. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  6493. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6494. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6495. */
  6496. Matrix4.fromRotationTranslation = function(rotation, translation, result) {
  6497. if (!defined(rotation)) {
  6498. throw new DeveloperError('rotation is required.');
  6499. }
  6500. translation = defaultValue(translation, Cartesian3.ZERO);
  6501. if (!defined(result)) {
  6502. return new Matrix4(rotation[0], rotation[3], rotation[6], translation.x,
  6503. rotation[1], rotation[4], rotation[7], translation.y,
  6504. rotation[2], rotation[5], rotation[8], translation.z,
  6505. 0.0, 0.0, 0.0, 1.0);
  6506. }
  6507. result[0] = rotation[0];
  6508. result[1] = rotation[1];
  6509. result[2] = rotation[2];
  6510. result[3] = 0.0;
  6511. result[4] = rotation[3];
  6512. result[5] = rotation[4];
  6513. result[6] = rotation[5];
  6514. result[7] = 0.0;
  6515. result[8] = rotation[6];
  6516. result[9] = rotation[7];
  6517. result[10] = rotation[8];
  6518. result[11] = 0.0;
  6519. result[12] = translation.x;
  6520. result[13] = translation.y;
  6521. result[14] = translation.z;
  6522. result[15] = 1.0;
  6523. return result;
  6524. };
  6525. /**
  6526. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  6527. * representation with the rotation represented as a quaternion.
  6528. *
  6529. * @param {Cartesian3} translation The translation transformation.
  6530. * @param {Quaternion} rotation The rotation transformation.
  6531. * @param {Cartesian3} scale The non-uniform scale transformation.
  6532. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6533. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6534. *
  6535. * @example
  6536. * var result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  6537. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  6538. * Cesium.Quaternion.IDENTITY, // rotation
  6539. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  6540. * result);
  6541. */
  6542. Matrix4.fromTranslationQuaternionRotationScale = function(translation, rotation, scale, result) {
  6543. if (!defined(translation)) {
  6544. throw new DeveloperError('translation is required.');
  6545. }
  6546. if (!defined(rotation)) {
  6547. throw new DeveloperError('rotation is required.');
  6548. }
  6549. if (!defined(scale)) {
  6550. throw new DeveloperError('scale is required.');
  6551. }
  6552. if (!defined(result)) {
  6553. result = new Matrix4();
  6554. }
  6555. var scaleX = scale.x;
  6556. var scaleY = scale.y;
  6557. var scaleZ = scale.z;
  6558. var x2 = rotation.x * rotation.x;
  6559. var xy = rotation.x * rotation.y;
  6560. var xz = rotation.x * rotation.z;
  6561. var xw = rotation.x * rotation.w;
  6562. var y2 = rotation.y * rotation.y;
  6563. var yz = rotation.y * rotation.z;
  6564. var yw = rotation.y * rotation.w;
  6565. var z2 = rotation.z * rotation.z;
  6566. var zw = rotation.z * rotation.w;
  6567. var w2 = rotation.w * rotation.w;
  6568. var m00 = x2 - y2 - z2 + w2;
  6569. var m01 = 2.0 * (xy - zw);
  6570. var m02 = 2.0 * (xz + yw);
  6571. var m10 = 2.0 * (xy + zw);
  6572. var m11 = -x2 + y2 - z2 + w2;
  6573. var m12 = 2.0 * (yz - xw);
  6574. var m20 = 2.0 * (xz - yw);
  6575. var m21 = 2.0 * (yz + xw);
  6576. var m22 = -x2 - y2 + z2 + w2;
  6577. result[0] = m00 * scaleX;
  6578. result[1] = m10 * scaleX;
  6579. result[2] = m20 * scaleX;
  6580. result[3] = 0.0;
  6581. result[4] = m01 * scaleY;
  6582. result[5] = m11 * scaleY;
  6583. result[6] = m21 * scaleY;
  6584. result[7] = 0.0;
  6585. result[8] = m02 * scaleZ;
  6586. result[9] = m12 * scaleZ;
  6587. result[10] = m22 * scaleZ;
  6588. result[11] = 0.0;
  6589. result[12] = translation.x;
  6590. result[13] = translation.y;
  6591. result[14] = translation.z;
  6592. result[15] = 1.0;
  6593. return result;
  6594. };
  6595. /**
  6596. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  6597. *
  6598. * @param {TranslationRotationScale} translationRotationScale The instance.
  6599. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6600. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6601. */
  6602. Matrix4.fromTranslationRotationScale = function(translationRotationScale, result) {
  6603. if (!defined(translationRotationScale)) {
  6604. throw new DeveloperError('translationRotationScale is required.');
  6605. }
  6606. return Matrix4.fromTranslationQuaternionRotationScale(translationRotationScale.translation, translationRotationScale.rotation, translationRotationScale.scale, result);
  6607. };
  6608. /**
  6609. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  6610. *
  6611. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  6612. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6613. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6614. *
  6615. * @see Matrix4.multiplyByTranslation
  6616. */
  6617. Matrix4.fromTranslation = function(translation, result) {
  6618. if (!defined(translation)) {
  6619. throw new DeveloperError('translation is required.');
  6620. }
  6621. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  6622. };
  6623. /**
  6624. * Computes a Matrix4 instance representing a non-uniform scale.
  6625. *
  6626. * @param {Cartesian3} scale The x, y, and z scale factors.
  6627. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6628. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6629. *
  6630. * @example
  6631. * // Creates
  6632. * // [7.0, 0.0, 0.0, 0.0]
  6633. * // [0.0, 8.0, 0.0, 0.0]
  6634. * // [0.0, 0.0, 9.0, 0.0]
  6635. * // [0.0, 0.0, 0.0, 1.0]
  6636. * var m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  6637. */
  6638. Matrix4.fromScale = function(scale, result) {
  6639. if (!defined(scale)) {
  6640. throw new DeveloperError('scale is required.');
  6641. }
  6642. if (!defined(result)) {
  6643. return new Matrix4(
  6644. scale.x, 0.0, 0.0, 0.0,
  6645. 0.0, scale.y, 0.0, 0.0,
  6646. 0.0, 0.0, scale.z, 0.0,
  6647. 0.0, 0.0, 0.0, 1.0);
  6648. }
  6649. result[0] = scale.x;
  6650. result[1] = 0.0;
  6651. result[2] = 0.0;
  6652. result[3] = 0.0;
  6653. result[4] = 0.0;
  6654. result[5] = scale.y;
  6655. result[6] = 0.0;
  6656. result[7] = 0.0;
  6657. result[8] = 0.0;
  6658. result[9] = 0.0;
  6659. result[10] = scale.z;
  6660. result[11] = 0.0;
  6661. result[12] = 0.0;
  6662. result[13] = 0.0;
  6663. result[14] = 0.0;
  6664. result[15] = 1.0;
  6665. return result;
  6666. };
  6667. /**
  6668. * Computes a Matrix4 instance representing a uniform scale.
  6669. *
  6670. * @param {Number} scale The uniform scale factor.
  6671. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6672. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6673. *
  6674. * @example
  6675. * // Creates
  6676. * // [2.0, 0.0, 0.0, 0.0]
  6677. * // [0.0, 2.0, 0.0, 0.0]
  6678. * // [0.0, 0.0, 2.0, 0.0]
  6679. * // [0.0, 0.0, 0.0, 1.0]
  6680. * var m = Cesium.Matrix4.fromUniformScale(2.0);
  6681. */
  6682. Matrix4.fromUniformScale = function(scale, result) {
  6683. if (typeof scale !== 'number') {
  6684. throw new DeveloperError('scale is required.');
  6685. }
  6686. if (!defined(result)) {
  6687. return new Matrix4(scale, 0.0, 0.0, 0.0,
  6688. 0.0, scale, 0.0, 0.0,
  6689. 0.0, 0.0, scale, 0.0,
  6690. 0.0, 0.0, 0.0, 1.0);
  6691. }
  6692. result[0] = scale;
  6693. result[1] = 0.0;
  6694. result[2] = 0.0;
  6695. result[3] = 0.0;
  6696. result[4] = 0.0;
  6697. result[5] = scale;
  6698. result[6] = 0.0;
  6699. result[7] = 0.0;
  6700. result[8] = 0.0;
  6701. result[9] = 0.0;
  6702. result[10] = scale;
  6703. result[11] = 0.0;
  6704. result[12] = 0.0;
  6705. result[13] = 0.0;
  6706. result[14] = 0.0;
  6707. result[15] = 1.0;
  6708. return result;
  6709. };
  6710. var fromCameraF = new Cartesian3();
  6711. var fromCameraR = new Cartesian3();
  6712. var fromCameraU = new Cartesian3();
  6713. /**
  6714. * Computes a Matrix4 instance from a Camera.
  6715. *
  6716. * @param {Camera} camera The camera to use.
  6717. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6718. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6719. */
  6720. Matrix4.fromCamera = function(camera, result) {
  6721. if (!defined(camera)) {
  6722. throw new DeveloperError('camera is required.');
  6723. }
  6724. var position = camera.position;
  6725. var direction = camera.direction;
  6726. var up = camera.up;
  6727. if (!defined(position)) {
  6728. throw new DeveloperError('camera.position is required.');
  6729. }
  6730. if (!defined(direction)) {
  6731. throw new DeveloperError('camera.direction is required.');
  6732. }
  6733. if (!defined(up)) {
  6734. throw new DeveloperError('camera.up is required.');
  6735. }
  6736. Cartesian3.normalize(direction, fromCameraF);
  6737. Cartesian3.normalize(Cartesian3.cross(fromCameraF, up, fromCameraR), fromCameraR);
  6738. Cartesian3.normalize(Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU), fromCameraU);
  6739. var sX = fromCameraR.x;
  6740. var sY = fromCameraR.y;
  6741. var sZ = fromCameraR.z;
  6742. var fX = fromCameraF.x;
  6743. var fY = fromCameraF.y;
  6744. var fZ = fromCameraF.z;
  6745. var uX = fromCameraU.x;
  6746. var uY = fromCameraU.y;
  6747. var uZ = fromCameraU.z;
  6748. var positionX = position.x;
  6749. var positionY = position.y;
  6750. var positionZ = position.z;
  6751. var t0 = sX * -positionX + sY * -positionY+ sZ * -positionZ;
  6752. var t1 = uX * -positionX + uY * -positionY+ uZ * -positionZ;
  6753. var t2 = fX * positionX + fY * positionY + fZ * positionZ;
  6754. // The code below this comment is an optimized
  6755. // version of the commented lines.
  6756. // Rather that create two matrices and then multiply,
  6757. // we just bake in the multiplcation as part of creation.
  6758. // var rotation = new Matrix4(
  6759. // sX, sY, sZ, 0.0,
  6760. // uX, uY, uZ, 0.0,
  6761. // -fX, -fY, -fZ, 0.0,
  6762. // 0.0, 0.0, 0.0, 1.0);
  6763. // var translation = new Matrix4(
  6764. // 1.0, 0.0, 0.0, -position.x,
  6765. // 0.0, 1.0, 0.0, -position.y,
  6766. // 0.0, 0.0, 1.0, -position.z,
  6767. // 0.0, 0.0, 0.0, 1.0);
  6768. // return rotation.multiply(translation);
  6769. if (!defined(result)) {
  6770. return new Matrix4(
  6771. sX, sY, sZ, t0,
  6772. uX, uY, uZ, t1,
  6773. -fX, -fY, -fZ, t2,
  6774. 0.0, 0.0, 0.0, 1.0);
  6775. }
  6776. result[0] = sX;
  6777. result[1] = uX;
  6778. result[2] = -fX;
  6779. result[3] = 0.0;
  6780. result[4] = sY;
  6781. result[5] = uY;
  6782. result[6] = -fY;
  6783. result[7] = 0.0;
  6784. result[8] = sZ;
  6785. result[9] = uZ;
  6786. result[10] = -fZ;
  6787. result[11] = 0.0;
  6788. result[12] = t0;
  6789. result[13] = t1;
  6790. result[14] = t2;
  6791. result[15] = 1.0;
  6792. return result;
  6793. };
  6794. /**
  6795. * Computes a Matrix4 instance representing a perspective transformation matrix.
  6796. *
  6797. * @param {Number} fovY The field of view along the Y axis in radians.
  6798. * @param {Number} aspectRatio The aspect ratio.
  6799. * @param {Number} near The distance to the near plane in meters.
  6800. * @param {Number} far The distance to the far plane in meters.
  6801. * @param {Matrix4} result The object in which the result will be stored.
  6802. * @returns {Matrix4} The modified result parameter.
  6803. *
  6804. * @exception {DeveloperError} fovY must be in (0, PI].
  6805. * @exception {DeveloperError} aspectRatio must be greater than zero.
  6806. * @exception {DeveloperError} near must be greater than zero.
  6807. * @exception {DeveloperError} far must be greater than zero.
  6808. */
  6809. Matrix4.computePerspectiveFieldOfView = function(fovY, aspectRatio, near, far, result) {
  6810. if (fovY <= 0.0 || fovY > Math.PI) {
  6811. throw new DeveloperError('fovY must be in (0, PI].');
  6812. }
  6813. if (aspectRatio <= 0.0) {
  6814. throw new DeveloperError('aspectRatio must be greater than zero.');
  6815. }
  6816. if (near <= 0.0) {
  6817. throw new DeveloperError('near must be greater than zero.');
  6818. }
  6819. if (far <= 0.0) {
  6820. throw new DeveloperError('far must be greater than zero.');
  6821. }
  6822. if (!defined(result)) {
  6823. throw new DeveloperError('result is required');
  6824. }
  6825. var bottom = Math.tan(fovY * 0.5);
  6826. var column1Row1 = 1.0 / bottom;
  6827. var column0Row0 = column1Row1 / aspectRatio;
  6828. var column2Row2 = (far + near) / (near - far);
  6829. var column3Row2 = (2.0 * far * near) / (near - far);
  6830. result[0] = column0Row0;
  6831. result[1] = 0.0;
  6832. result[2] = 0.0;
  6833. result[3] = 0.0;
  6834. result[4] = 0.0;
  6835. result[5] = column1Row1;
  6836. result[6] = 0.0;
  6837. result[7] = 0.0;
  6838. result[8] = 0.0;
  6839. result[9] = 0.0;
  6840. result[10] = column2Row2;
  6841. result[11] = -1.0;
  6842. result[12] = 0.0;
  6843. result[13] = 0.0;
  6844. result[14] = column3Row2;
  6845. result[15] = 0.0;
  6846. return result;
  6847. };
  6848. /**
  6849. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  6850. *
  6851. * @param {Number} left The number of meters to the left of the camera that will be in view.
  6852. * @param {Number} right The number of meters to the right of the camera that will be in view.
  6853. * @param {Number} bottom The number of meters below of the camera that will be in view.
  6854. * @param {Number} top The number of meters above of the camera that will be in view.
  6855. * @param {Number} near The distance to the near plane in meters.
  6856. * @param {Number} far The distance to the far plane in meters.
  6857. * @param {Matrix4} result The object in which the result will be stored.
  6858. * @returns {Matrix4} The modified result parameter.
  6859. */
  6860. Matrix4.computeOrthographicOffCenter = function(left, right, bottom, top, near, far, result) {
  6861. if (!defined(left)) {
  6862. throw new DeveloperError('left is required.');
  6863. }
  6864. if (!defined(right)) {
  6865. throw new DeveloperError('right is required.');
  6866. }
  6867. if (!defined(bottom)) {
  6868. throw new DeveloperError('bottom is required.');
  6869. }
  6870. if (!defined(top)) {
  6871. throw new DeveloperError('top is required.');
  6872. }
  6873. if (!defined(near)) {
  6874. throw new DeveloperError('near is required.');
  6875. }
  6876. if (!defined(far)) {
  6877. throw new DeveloperError('far is required.');
  6878. }
  6879. if (!defined(result)) {
  6880. throw new DeveloperError('result is required');
  6881. }
  6882. var a = 1.0 / (right - left);
  6883. var b = 1.0 / (top - bottom);
  6884. var c = 1.0 / (far - near);
  6885. var tx = -(right + left) * a;
  6886. var ty = -(top + bottom) * b;
  6887. var tz = -(far + near) * c;
  6888. a *= 2.0;
  6889. b *= 2.0;
  6890. c *= -2.0;
  6891. result[0] = a;
  6892. result[1] = 0.0;
  6893. result[2] = 0.0;
  6894. result[3] = 0.0;
  6895. result[4] = 0.0;
  6896. result[5] = b;
  6897. result[6] = 0.0;
  6898. result[7] = 0.0;
  6899. result[8] = 0.0;
  6900. result[9] = 0.0;
  6901. result[10] = c;
  6902. result[11] = 0.0;
  6903. result[12] = tx;
  6904. result[13] = ty;
  6905. result[14] = tz;
  6906. result[15] = 1.0;
  6907. return result;
  6908. };
  6909. /**
  6910. * Computes a Matrix4 instance representing an off center perspective transformation.
  6911. *
  6912. * @param {Number} left The number of meters to the left of the camera that will be in view.
  6913. * @param {Number} right The number of meters to the right of the camera that will be in view.
  6914. * @param {Number} bottom The number of meters below of the camera that will be in view.
  6915. * @param {Number} top The number of meters above of the camera that will be in view.
  6916. * @param {Number} near The distance to the near plane in meters.
  6917. * @param {Number} far The distance to the far plane in meters.
  6918. * @param {Matrix4} result The object in which the result will be stored.
  6919. * @returns {Matrix4} The modified result parameter.
  6920. */
  6921. Matrix4.computePerspectiveOffCenter = function(left, right, bottom, top, near, far, result) {
  6922. if (!defined(left)) {
  6923. throw new DeveloperError('left is required.');
  6924. }
  6925. if (!defined(right)) {
  6926. throw new DeveloperError('right is required.');
  6927. }
  6928. if (!defined(bottom)) {
  6929. throw new DeveloperError('bottom is required.');
  6930. }
  6931. if (!defined(top)) {
  6932. throw new DeveloperError('top is required.');
  6933. }
  6934. if (!defined(near)) {
  6935. throw new DeveloperError('near is required.');
  6936. }
  6937. if (!defined(far)) {
  6938. throw new DeveloperError('far is required.');
  6939. }
  6940. if (!defined(result)) {
  6941. throw new DeveloperError('result is required');
  6942. }
  6943. var column0Row0 = 2.0 * near / (right - left);
  6944. var column1Row1 = 2.0 * near / (top - bottom);
  6945. var column2Row0 = (right + left) / (right - left);
  6946. var column2Row1 = (top + bottom) / (top - bottom);
  6947. var column2Row2 = -(far + near) / (far - near);
  6948. var column2Row3 = -1.0;
  6949. var column3Row2 = -2.0 * far * near / (far - near);
  6950. result[0] = column0Row0;
  6951. result[1] = 0.0;
  6952. result[2] = 0.0;
  6953. result[3] = 0.0;
  6954. result[4] = 0.0;
  6955. result[5] = column1Row1;
  6956. result[6] = 0.0;
  6957. result[7] = 0.0;
  6958. result[8] = column2Row0;
  6959. result[9] = column2Row1;
  6960. result[10] = column2Row2;
  6961. result[11] = column2Row3;
  6962. result[12] = 0.0;
  6963. result[13] = 0.0;
  6964. result[14] = column3Row2;
  6965. result[15] = 0.0;
  6966. return result;
  6967. };
  6968. /**
  6969. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  6970. *
  6971. * @param {Number} left The number of meters to the left of the camera that will be in view.
  6972. * @param {Number} right The number of meters to the right of the camera that will be in view.
  6973. * @param {Number} bottom The number of meters below of the camera that will be in view.
  6974. * @param {Number} top The number of meters above of the camera that will be in view.
  6975. * @param {Number} near The distance to the near plane in meters.
  6976. * @param {Matrix4} result The object in which the result will be stored.
  6977. * @returns {Matrix4} The modified result parameter.
  6978. */
  6979. Matrix4.computeInfinitePerspectiveOffCenter = function(left, right, bottom, top, near, result) {
  6980. if (!defined(left)) {
  6981. throw new DeveloperError('left is required.');
  6982. }
  6983. if (!defined(right)) {
  6984. throw new DeveloperError('right is required.');
  6985. }
  6986. if (!defined(bottom)) {
  6987. throw new DeveloperError('bottom is required.');
  6988. }
  6989. if (!defined(top)) {
  6990. throw new DeveloperError('top is required.');
  6991. }
  6992. if (!defined(near)) {
  6993. throw new DeveloperError('near is required.');
  6994. }
  6995. if (!defined(result)) {
  6996. throw new DeveloperError('result is required');
  6997. }
  6998. var column0Row0 = 2.0 * near / (right - left);
  6999. var column1Row1 = 2.0 * near / (top - bottom);
  7000. var column2Row0 = (right + left) / (right - left);
  7001. var column2Row1 = (top + bottom) / (top - bottom);
  7002. var column2Row2 = -1.0;
  7003. var column2Row3 = -1.0;
  7004. var column3Row2 = -2.0 * near;
  7005. result[0] = column0Row0;
  7006. result[1] = 0.0;
  7007. result[2] = 0.0;
  7008. result[3] = 0.0;
  7009. result[4] = 0.0;
  7010. result[5] = column1Row1;
  7011. result[6] = 0.0;
  7012. result[7] = 0.0;
  7013. result[8] = column2Row0;
  7014. result[9] = column2Row1;
  7015. result[10] = column2Row2;
  7016. result[11] = column2Row3;
  7017. result[12] = 0.0;
  7018. result[13] = 0.0;
  7019. result[14] = column3Row2;
  7020. result[15] = 0.0;
  7021. return result;
  7022. };
  7023. /**
  7024. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  7025. *
  7026. * @param {Object}[viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  7027. * @param {Number}[nearDepthRange=0.0] The near plane distance in window coordinates.
  7028. * @param {Number}[farDepthRange=1.0] The far plane distance in window coordinates.
  7029. * @param {Matrix4} result The object in which the result will be stored.
  7030. * @returns {Matrix4} The modified result parameter.
  7031. *
  7032. * @example
  7033. * // Create viewport transformation using an explicit viewport and depth range.
  7034. * var m = Cesium.Matrix4.computeViewportTransformation({
  7035. * x : 0.0,
  7036. * y : 0.0,
  7037. * width : 1024.0,
  7038. * height : 768.0
  7039. * }, 0.0, 1.0, new Cesium.Matrix4());
  7040. */
  7041. Matrix4.computeViewportTransformation = function(viewport, nearDepthRange, farDepthRange, result) {
  7042. if (!defined(result)) {
  7043. throw new DeveloperError('result is required');
  7044. }
  7045. viewport = defaultValue(viewport, defaultValue.EMPTY_OBJECT);
  7046. var x = defaultValue(viewport.x, 0.0);
  7047. var y = defaultValue(viewport.y, 0.0);
  7048. var width = defaultValue(viewport.width, 0.0);
  7049. var height = defaultValue(viewport.height, 0.0);
  7050. nearDepthRange = defaultValue(nearDepthRange, 0.0);
  7051. farDepthRange = defaultValue(farDepthRange, 1.0);
  7052. var halfWidth = width * 0.5;
  7053. var halfHeight = height * 0.5;
  7054. var halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  7055. var column0Row0 = halfWidth;
  7056. var column1Row1 = halfHeight;
  7057. var column2Row2 = halfDepth;
  7058. var column3Row0 = x + halfWidth;
  7059. var column3Row1 = y + halfHeight;
  7060. var column3Row2 = nearDepthRange + halfDepth;
  7061. var column3Row3 = 1.0;
  7062. result[0] = column0Row0;
  7063. result[1] = 0.0;
  7064. result[2] = 0.0;
  7065. result[3] = 0.0;
  7066. result[4] = 0.0;
  7067. result[5] = column1Row1;
  7068. result[6] = 0.0;
  7069. result[7] = 0.0;
  7070. result[8] = 0.0;
  7071. result[9] = 0.0;
  7072. result[10] = column2Row2;
  7073. result[11] = 0.0;
  7074. result[12] = column3Row0;
  7075. result[13] = column3Row1;
  7076. result[14] = column3Row2;
  7077. result[15] = column3Row3;
  7078. return result;
  7079. };
  7080. /**
  7081. * Computes a Matrix4 instance that transforms from world space to view space.
  7082. *
  7083. * @param {Cartesian3} position The position of the camera.
  7084. * @param {Cartesian3} direction The forward direction.
  7085. * @param {Cartesian3} up The up direction.
  7086. * @param {Cartesian3} right The right direction.
  7087. * @param {Matrix4} result The object in which the result will be stored.
  7088. * @returns {Matrix4} The modified result parameter.
  7089. */
  7090. Matrix4.computeView = function(position, direction, up, right, result) {
  7091. if (!defined(position)) {
  7092. throw new DeveloperError('position is required');
  7093. }
  7094. if (!defined(direction)) {
  7095. throw new DeveloperError('direction is required');
  7096. }
  7097. if (!defined(up)) {
  7098. throw new DeveloperError('up is required');
  7099. }
  7100. if (!defined(right)) {
  7101. throw new DeveloperError('right is required');
  7102. }
  7103. if (!defined(result)) {
  7104. throw new DeveloperError('result is required');
  7105. }
  7106. result[0] = right.x;
  7107. result[1] = up.x;
  7108. result[2] = -direction.x;
  7109. result[3] = 0.0;
  7110. result[4] = right.y;
  7111. result[5] = up.y;
  7112. result[6] = -direction.y;
  7113. result[7] = 0.0;
  7114. result[8] = right.z;
  7115. result[9] = up.z;
  7116. result[10] = -direction.z;
  7117. result[11] = 0.0;
  7118. result[12] = -Cartesian3.dot(right, position);
  7119. result[13] = -Cartesian3.dot(up, position);
  7120. result[14] = Cartesian3.dot(direction, position);
  7121. result[15] = 1.0;
  7122. return result;
  7123. };
  7124. /**
  7125. * Computes an Array from the provided Matrix4 instance.
  7126. * The array will be in column-major order.
  7127. *
  7128. * @param {Matrix4} matrix The matrix to use..
  7129. * @param {Number[]} [result] The Array onto which to store the result.
  7130. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  7131. *
  7132. * @example
  7133. * //create an array from an instance of Matrix4
  7134. * // m = [10.0, 14.0, 18.0, 22.0]
  7135. * // [11.0, 15.0, 19.0, 23.0]
  7136. * // [12.0, 16.0, 20.0, 24.0]
  7137. * // [13.0, 17.0, 21.0, 25.0]
  7138. * var a = Cesium.Matrix4.toArray(m);
  7139. *
  7140. * // m remains the same
  7141. * //creates a = [10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0]
  7142. */
  7143. Matrix4.toArray = function(matrix, result) {
  7144. if (!defined(matrix)) {
  7145. throw new DeveloperError('matrix is required');
  7146. }
  7147. if (!defined(result)) {
  7148. return [matrix[0], matrix[1], matrix[2], matrix[3],
  7149. matrix[4], matrix[5], matrix[6], matrix[7],
  7150. matrix[8], matrix[9], matrix[10], matrix[11],
  7151. matrix[12], matrix[13], matrix[14], matrix[15]];
  7152. }
  7153. result[0] = matrix[0];
  7154. result[1] = matrix[1];
  7155. result[2] = matrix[2];
  7156. result[3] = matrix[3];
  7157. result[4] = matrix[4];
  7158. result[5] = matrix[5];
  7159. result[6] = matrix[6];
  7160. result[7] = matrix[7];
  7161. result[8] = matrix[8];
  7162. result[9] = matrix[9];
  7163. result[10] = matrix[10];
  7164. result[11] = matrix[11];
  7165. result[12] = matrix[12];
  7166. result[13] = matrix[13];
  7167. result[14] = matrix[14];
  7168. result[15] = matrix[15];
  7169. return result;
  7170. };
  7171. /**
  7172. * Computes the array index of the element at the provided row and column.
  7173. *
  7174. * @param {Number} row The zero-based index of the row.
  7175. * @param {Number} column The zero-based index of the column.
  7176. * @returns {Number} The index of the element at the provided row and column.
  7177. *
  7178. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  7179. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  7180. *
  7181. * @example
  7182. * var myMatrix = new Cesium.Matrix4();
  7183. * var column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  7184. * var column1Row0 = myMatrix[column1Row0Index];
  7185. * myMatrix[column1Row0Index] = 10.0;
  7186. */
  7187. Matrix4.getElementIndex = function(column, row) {
  7188. if (typeof row !== 'number' || row < 0 || row > 3) {
  7189. throw new DeveloperError('row must be 0, 1, 2, or 3.');
  7190. }
  7191. if (typeof column !== 'number' || column < 0 || column > 3) {
  7192. throw new DeveloperError('column must be 0, 1, 2, or 3.');
  7193. }
  7194. return column * 4 + row;
  7195. };
  7196. /**
  7197. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  7198. *
  7199. * @param {Matrix4} matrix The matrix to use.
  7200. * @param {Number} index The zero-based index of the column to retrieve.
  7201. * @param {Cartesian4} result The object onto which to store the result.
  7202. * @returns {Cartesian4} The modified result parameter.
  7203. *
  7204. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7205. *
  7206. * @example
  7207. * //returns a Cartesian4 instance with values from the specified column
  7208. * // m = [10.0, 11.0, 12.0, 13.0]
  7209. * // [14.0, 15.0, 16.0, 17.0]
  7210. * // [18.0, 19.0, 20.0, 21.0]
  7211. * // [22.0, 23.0, 24.0, 25.0]
  7212. *
  7213. * //Example 1: Creates an instance of Cartesian
  7214. * var a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  7215. *
  7216. * @example
  7217. * //Example 2: Sets values for Cartesian instance
  7218. * var a = new Cesium.Cartesian4();
  7219. * Cesium.Matrix4.getColumn(m, 2, a);
  7220. *
  7221. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  7222. */
  7223. Matrix4.getColumn = function(matrix, index, result) {
  7224. if (!defined(matrix)) {
  7225. throw new DeveloperError('matrix is required.');
  7226. }
  7227. if (typeof index !== 'number' || index < 0 || index > 3) {
  7228. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7229. }
  7230. if (!defined(result)) {
  7231. throw new DeveloperError('result is required');
  7232. }
  7233. var startIndex = index * 4;
  7234. var x = matrix[startIndex];
  7235. var y = matrix[startIndex + 1];
  7236. var z = matrix[startIndex + 2];
  7237. var w = matrix[startIndex + 3];
  7238. result.x = x;
  7239. result.y = y;
  7240. result.z = z;
  7241. result.w = w;
  7242. return result;
  7243. };
  7244. /**
  7245. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  7246. *
  7247. * @param {Matrix4} matrix The matrix to use.
  7248. * @param {Number} index The zero-based index of the column to set.
  7249. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  7250. * @param {Matrix4} result The object onto which to store the result.
  7251. * @returns {Matrix4} The modified result parameter.
  7252. *
  7253. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7254. *
  7255. * @example
  7256. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  7257. * // m = [10.0, 11.0, 12.0, 13.0]
  7258. * // [14.0, 15.0, 16.0, 17.0]
  7259. * // [18.0, 19.0, 20.0, 21.0]
  7260. * // [22.0, 23.0, 24.0, 25.0]
  7261. *
  7262. * var a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  7263. *
  7264. * // m remains the same
  7265. * // a = [10.0, 11.0, 99.0, 13.0]
  7266. * // [14.0, 15.0, 98.0, 17.0]
  7267. * // [18.0, 19.0, 97.0, 21.0]
  7268. * // [22.0, 23.0, 96.0, 25.0]
  7269. */
  7270. Matrix4.setColumn = function(matrix, index, cartesian, result) {
  7271. if (!defined(matrix)) {
  7272. throw new DeveloperError('matrix is required');
  7273. }
  7274. if (!defined(cartesian)) {
  7275. throw new DeveloperError('cartesian is required');
  7276. }
  7277. if (typeof index !== 'number' || index < 0 || index > 3) {
  7278. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7279. }
  7280. if (!defined(result)) {
  7281. throw new DeveloperError('result is required');
  7282. }
  7283. result = Matrix4.clone(matrix, result);
  7284. var startIndex = index * 4;
  7285. result[startIndex] = cartesian.x;
  7286. result[startIndex + 1] = cartesian.y;
  7287. result[startIndex + 2] = cartesian.z;
  7288. result[startIndex + 3] = cartesian.w;
  7289. return result;
  7290. };
  7291. /**
  7292. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  7293. * matrix with the provided translation. This assumes the matrix is an affine transformation
  7294. *
  7295. * @param {Matrix4} matrix The matrix to use.
  7296. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  7297. * @param {Cartesian4} result The object onto which to store the result.
  7298. * @returns {Matrix4} The modified result parameter.
  7299. */
  7300. Matrix4.setTranslation = function(matrix, translation, result) {
  7301. if (!defined(matrix)) {
  7302. throw new DeveloperError('matrix is required');
  7303. }
  7304. if (!defined(translation)) {
  7305. throw new DeveloperError('translation is required');
  7306. }
  7307. if (!defined(result)) {
  7308. throw new DeveloperError('result is required');
  7309. }
  7310. result[0] = matrix[0];
  7311. result[1] = matrix[1];
  7312. result[2] = matrix[2];
  7313. result[3] = matrix[3];
  7314. result[4] = matrix[4];
  7315. result[5] = matrix[5];
  7316. result[6] = matrix[6];
  7317. result[7] = matrix[7];
  7318. result[8] = matrix[8];
  7319. result[9] = matrix[9];
  7320. result[10] = matrix[10];
  7321. result[11] = matrix[11];
  7322. result[12] = translation.x;
  7323. result[13] = translation.y;
  7324. result[14] = translation.z;
  7325. result[15] = matrix[15];
  7326. return result;
  7327. };
  7328. /**
  7329. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  7330. *
  7331. * @param {Matrix4} matrix The matrix to use.
  7332. * @param {Number} index The zero-based index of the row to retrieve.
  7333. * @param {Cartesian4} result The object onto which to store the result.
  7334. * @returns {Cartesian4} The modified result parameter.
  7335. *
  7336. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7337. *
  7338. * @example
  7339. * //returns a Cartesian4 instance with values from the specified column
  7340. * // m = [10.0, 11.0, 12.0, 13.0]
  7341. * // [14.0, 15.0, 16.0, 17.0]
  7342. * // [18.0, 19.0, 20.0, 21.0]
  7343. * // [22.0, 23.0, 24.0, 25.0]
  7344. *
  7345. * //Example 1: Returns an instance of Cartesian
  7346. * var a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  7347. *
  7348. * @example
  7349. * //Example 2: Sets values for a Cartesian instance
  7350. * var a = new Cesium.Cartesian4();
  7351. * Cesium.Matrix4.getRow(m, 2, a);
  7352. *
  7353. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  7354. */
  7355. Matrix4.getRow = function(matrix, index, result) {
  7356. if (!defined(matrix)) {
  7357. throw new DeveloperError('matrix is required.');
  7358. }
  7359. if (typeof index !== 'number' || index < 0 || index > 3) {
  7360. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7361. }
  7362. if (!defined(result)) {
  7363. throw new DeveloperError('result is required');
  7364. }
  7365. var x = matrix[index];
  7366. var y = matrix[index + 4];
  7367. var z = matrix[index + 8];
  7368. var w = matrix[index + 12];
  7369. result.x = x;
  7370. result.y = y;
  7371. result.z = z;
  7372. result.w = w;
  7373. return result;
  7374. };
  7375. /**
  7376. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  7377. *
  7378. * @param {Matrix4} matrix The matrix to use.
  7379. * @param {Number} index The zero-based index of the row to set.
  7380. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  7381. * @param {Matrix4} result The object onto which to store the result.
  7382. * @returns {Matrix4} The modified result parameter.
  7383. *
  7384. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7385. *
  7386. * @example
  7387. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  7388. * // m = [10.0, 11.0, 12.0, 13.0]
  7389. * // [14.0, 15.0, 16.0, 17.0]
  7390. * // [18.0, 19.0, 20.0, 21.0]
  7391. * // [22.0, 23.0, 24.0, 25.0]
  7392. *
  7393. * var a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  7394. *
  7395. * // m remains the same
  7396. * // a = [10.0, 11.0, 12.0, 13.0]
  7397. * // [14.0, 15.0, 16.0, 17.0]
  7398. * // [99.0, 98.0, 97.0, 96.0]
  7399. * // [22.0, 23.0, 24.0, 25.0]
  7400. */
  7401. Matrix4.setRow = function(matrix, index, cartesian, result) {
  7402. if (!defined(matrix)) {
  7403. throw new DeveloperError('matrix is required');
  7404. }
  7405. if (!defined(cartesian)) {
  7406. throw new DeveloperError('cartesian is required');
  7407. }
  7408. if (typeof index !== 'number' || index < 0 || index > 3) {
  7409. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7410. }
  7411. if (!defined(result)) {
  7412. throw new DeveloperError('result is required');
  7413. }
  7414. result = Matrix4.clone(matrix, result);
  7415. result[index] = cartesian.x;
  7416. result[index + 4] = cartesian.y;
  7417. result[index + 8] = cartesian.z;
  7418. result[index + 12] = cartesian.w;
  7419. return result;
  7420. };
  7421. var scratchColumn = new Cartesian3();
  7422. /**
  7423. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  7424. *
  7425. * @param {Matrix4} matrix The matrix.
  7426. * @param {Cartesian3} result The object onto which to store the result.
  7427. * @returns {Cartesian3} The modified result parameter
  7428. */
  7429. Matrix4.getScale = function(matrix, result) {
  7430. if (!defined(matrix)) {
  7431. throw new DeveloperError('matrix is required.');
  7432. }
  7433. if (!defined(result)) {
  7434. throw new DeveloperError('result is required');
  7435. }
  7436. result.x = Cartesian3.magnitude(Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  7437. result.y = Cartesian3.magnitude(Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn));
  7438. result.z = Cartesian3.magnitude(Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn));
  7439. return result;
  7440. };
  7441. var scratchScale = new Cartesian3();
  7442. /**
  7443. * Computes the maximum scale assuming the matrix is an affine transformation.
  7444. * The maximum scale is the maximum length of the column vectors in the upper-left
  7445. * 3x3 matrix.
  7446. *
  7447. * @param {Matrix4} matrix The matrix.
  7448. * @returns {Number} The maximum scale.
  7449. */
  7450. Matrix4.getMaximumScale = function(matrix) {
  7451. Matrix4.getScale(matrix, scratchScale);
  7452. return Cartesian3.maximumComponent(scratchScale);
  7453. };
  7454. /**
  7455. * Computes the product of two matrices.
  7456. *
  7457. * @param {Matrix4} left The first matrix.
  7458. * @param {Matrix4} right The second matrix.
  7459. * @param {Matrix4} result The object onto which to store the result.
  7460. * @returns {Matrix4} The modified result parameter.
  7461. */
  7462. Matrix4.multiply = function(left, right, result) {
  7463. if (!defined(left)) {
  7464. throw new DeveloperError('left is required');
  7465. }
  7466. if (!defined(right)) {
  7467. throw new DeveloperError('right is required');
  7468. }
  7469. if (!defined(result)) {
  7470. throw new DeveloperError('result is required');
  7471. }
  7472. var left0 = left[0];
  7473. var left1 = left[1];
  7474. var left2 = left[2];
  7475. var left3 = left[3];
  7476. var left4 = left[4];
  7477. var left5 = left[5];
  7478. var left6 = left[6];
  7479. var left7 = left[7];
  7480. var left8 = left[8];
  7481. var left9 = left[9];
  7482. var left10 = left[10];
  7483. var left11 = left[11];
  7484. var left12 = left[12];
  7485. var left13 = left[13];
  7486. var left14 = left[14];
  7487. var left15 = left[15];
  7488. var right0 = right[0];
  7489. var right1 = right[1];
  7490. var right2 = right[2];
  7491. var right3 = right[3];
  7492. var right4 = right[4];
  7493. var right5 = right[5];
  7494. var right6 = right[6];
  7495. var right7 = right[7];
  7496. var right8 = right[8];
  7497. var right9 = right[9];
  7498. var right10 = right[10];
  7499. var right11 = right[11];
  7500. var right12 = right[12];
  7501. var right13 = right[13];
  7502. var right14 = right[14];
  7503. var right15 = right[15];
  7504. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  7505. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  7506. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  7507. var column0Row3 = left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  7508. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  7509. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  7510. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  7511. var column1Row3 = left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  7512. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  7513. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  7514. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  7515. var column2Row3 = left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  7516. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  7517. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  7518. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  7519. var column3Row3 = left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  7520. result[0] = column0Row0;
  7521. result[1] = column0Row1;
  7522. result[2] = column0Row2;
  7523. result[3] = column0Row3;
  7524. result[4] = column1Row0;
  7525. result[5] = column1Row1;
  7526. result[6] = column1Row2;
  7527. result[7] = column1Row3;
  7528. result[8] = column2Row0;
  7529. result[9] = column2Row1;
  7530. result[10] = column2Row2;
  7531. result[11] = column2Row3;
  7532. result[12] = column3Row0;
  7533. result[13] = column3Row1;
  7534. result[14] = column3Row2;
  7535. result[15] = column3Row3;
  7536. return result;
  7537. };
  7538. /**
  7539. * Computes the sum of two matrices.
  7540. *
  7541. * @param {Matrix4} left The first matrix.
  7542. * @param {Matrix4} right The second matrix.
  7543. * @param {Matrix4} result The object onto which to store the result.
  7544. * @returns {Matrix4} The modified result parameter.
  7545. */
  7546. Matrix4.add = function(left, right, result) {
  7547. if (!defined(left)) {
  7548. throw new DeveloperError('left is required');
  7549. }
  7550. if (!defined(right)) {
  7551. throw new DeveloperError('right is required');
  7552. }
  7553. if (!defined(result)) {
  7554. throw new DeveloperError('result is required');
  7555. }
  7556. result[0] = left[0] + right[0];
  7557. result[1] = left[1] + right[1];
  7558. result[2] = left[2] + right[2];
  7559. result[3] = left[3] + right[3];
  7560. result[4] = left[4] + right[4];
  7561. result[5] = left[5] + right[5];
  7562. result[6] = left[6] + right[6];
  7563. result[7] = left[7] + right[7];
  7564. result[8] = left[8] + right[8];
  7565. result[9] = left[9] + right[9];
  7566. result[10] = left[10] + right[10];
  7567. result[11] = left[11] + right[11];
  7568. result[12] = left[12] + right[12];
  7569. result[13] = left[13] + right[13];
  7570. result[14] = left[14] + right[14];
  7571. result[15] = left[15] + right[15];
  7572. return result;
  7573. };
  7574. /**
  7575. * Computes the difference of two matrices.
  7576. *
  7577. * @param {Matrix4} left The first matrix.
  7578. * @param {Matrix4} right The second matrix.
  7579. * @param {Matrix4} result The object onto which to store the result.
  7580. * @returns {Matrix4} The modified result parameter.
  7581. */
  7582. Matrix4.subtract = function(left, right, result) {
  7583. if (!defined(left)) {
  7584. throw new DeveloperError('left is required');
  7585. }
  7586. if (!defined(right)) {
  7587. throw new DeveloperError('right is required');
  7588. }
  7589. if (!defined(result)) {
  7590. throw new DeveloperError('result is required');
  7591. }
  7592. result[0] = left[0] - right[0];
  7593. result[1] = left[1] - right[1];
  7594. result[2] = left[2] - right[2];
  7595. result[3] = left[3] - right[3];
  7596. result[4] = left[4] - right[4];
  7597. result[5] = left[5] - right[5];
  7598. result[6] = left[6] - right[6];
  7599. result[7] = left[7] - right[7];
  7600. result[8] = left[8] - right[8];
  7601. result[9] = left[9] - right[9];
  7602. result[10] = left[10] - right[10];
  7603. result[11] = left[11] - right[11];
  7604. result[12] = left[12] - right[12];
  7605. result[13] = left[13] - right[13];
  7606. result[14] = left[14] - right[14];
  7607. result[15] = left[15] - right[15];
  7608. return result;
  7609. };
  7610. /**
  7611. * Computes the product of two matrices assuming the matrices are
  7612. * affine transformation matrices, where the upper left 3x3 elements
  7613. * are a rotation matrix, and the upper three elements in the fourth
  7614. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  7615. * The matrix is not verified to be in the proper form.
  7616. * This method is faster than computing the product for general 4x4
  7617. * matrices using {@link Matrix4.multiply}.
  7618. *
  7619. * @param {Matrix4} left The first matrix.
  7620. * @param {Matrix4} right The second matrix.
  7621. * @param {Matrix4} result The object onto which to store the result.
  7622. * @returns {Matrix4} The modified result parameter.
  7623. *
  7624. * @example
  7625. * var m1 = new Cesium.Matrix4(1.0, 6.0, 7.0, 0.0, 2.0, 5.0, 8.0, 0.0, 3.0, 4.0, 9.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  7626. * var m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  7627. * var m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  7628. */
  7629. Matrix4.multiplyTransformation = function(left, right, result) {
  7630. if (!defined(left)) {
  7631. throw new DeveloperError('left is required');
  7632. }
  7633. if (!defined(right)) {
  7634. throw new DeveloperError('right is required');
  7635. }
  7636. if (!defined(result)) {
  7637. throw new DeveloperError('result is required');
  7638. }
  7639. var left0 = left[0];
  7640. var left1 = left[1];
  7641. var left2 = left[2];
  7642. var left4 = left[4];
  7643. var left5 = left[5];
  7644. var left6 = left[6];
  7645. var left8 = left[8];
  7646. var left9 = left[9];
  7647. var left10 = left[10];
  7648. var left12 = left[12];
  7649. var left13 = left[13];
  7650. var left14 = left[14];
  7651. var right0 = right[0];
  7652. var right1 = right[1];
  7653. var right2 = right[2];
  7654. var right4 = right[4];
  7655. var right5 = right[5];
  7656. var right6 = right[6];
  7657. var right8 = right[8];
  7658. var right9 = right[9];
  7659. var right10 = right[10];
  7660. var right12 = right[12];
  7661. var right13 = right[13];
  7662. var right14 = right[14];
  7663. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  7664. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  7665. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  7666. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  7667. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  7668. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  7669. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  7670. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  7671. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  7672. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12;
  7673. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13;
  7674. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14;
  7675. result[0] = column0Row0;
  7676. result[1] = column0Row1;
  7677. result[2] = column0Row2;
  7678. result[3] = 0.0;
  7679. result[4] = column1Row0;
  7680. result[5] = column1Row1;
  7681. result[6] = column1Row2;
  7682. result[7] = 0.0;
  7683. result[8] = column2Row0;
  7684. result[9] = column2Row1;
  7685. result[10] = column2Row2;
  7686. result[11] = 0.0;
  7687. result[12] = column3Row0;
  7688. result[13] = column3Row1;
  7689. result[14] = column3Row2;
  7690. result[15] = 1.0;
  7691. return result;
  7692. };
  7693. /**
  7694. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7695. * by a 3x3 rotation matrix. This is an optimization
  7696. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  7697. *
  7698. * @param {Matrix4} matrix The matrix on the left-hand side.
  7699. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  7700. * @param {Matrix4} result The object onto which to store the result.
  7701. * @returns {Matrix4} The modified result parameter.
  7702. *
  7703. * @example
  7704. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  7705. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  7706. */
  7707. Matrix4.multiplyByMatrix3 = function(matrix, rotation, result) {
  7708. if (!defined(matrix)) {
  7709. throw new DeveloperError('matrix is required');
  7710. }
  7711. if (!defined(rotation)) {
  7712. throw new DeveloperError('rotation is required');
  7713. }
  7714. if (!defined(result)) {
  7715. throw new DeveloperError('result is required');
  7716. }
  7717. var left0 = matrix[0];
  7718. var left1 = matrix[1];
  7719. var left2 = matrix[2];
  7720. var left4 = matrix[4];
  7721. var left5 = matrix[5];
  7722. var left6 = matrix[6];
  7723. var left8 = matrix[8];
  7724. var left9 = matrix[9];
  7725. var left10 = matrix[10];
  7726. var right0 = rotation[0];
  7727. var right1 = rotation[1];
  7728. var right2 = rotation[2];
  7729. var right4 = rotation[3];
  7730. var right5 = rotation[4];
  7731. var right6 = rotation[5];
  7732. var right8 = rotation[6];
  7733. var right9 = rotation[7];
  7734. var right10 = rotation[8];
  7735. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  7736. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  7737. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  7738. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  7739. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  7740. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  7741. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  7742. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  7743. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  7744. result[0] = column0Row0;
  7745. result[1] = column0Row1;
  7746. result[2] = column0Row2;
  7747. result[3] = 0.0;
  7748. result[4] = column1Row0;
  7749. result[5] = column1Row1;
  7750. result[6] = column1Row2;
  7751. result[7] = 0.0;
  7752. result[8] = column2Row0;
  7753. result[9] = column2Row1;
  7754. result[10] = column2Row2;
  7755. result[11] = 0.0;
  7756. result[12] = matrix[12];
  7757. result[13] = matrix[13];
  7758. result[14] = matrix[14];
  7759. result[15] = matrix[15];
  7760. return result;
  7761. };
  7762. /**
  7763. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7764. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  7765. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  7766. *
  7767. * @param {Matrix4} matrix The matrix on the left-hand side.
  7768. * @param {Cartesian3} translation The translation on the right-hand side.
  7769. * @param {Matrix4} result The object onto which to store the result.
  7770. * @returns {Matrix4} The modified result parameter.
  7771. *
  7772. * @example
  7773. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  7774. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  7775. */
  7776. Matrix4.multiplyByTranslation = function(matrix, translation, result) {
  7777. if (!defined(matrix)) {
  7778. throw new DeveloperError('matrix is required');
  7779. }
  7780. if (!defined(translation)) {
  7781. throw new DeveloperError('translation is required');
  7782. }
  7783. if (!defined(result)) {
  7784. throw new DeveloperError('result is required');
  7785. }
  7786. var x = translation.x;
  7787. var y = translation.y;
  7788. var z = translation.z;
  7789. var tx = (x * matrix[0]) + (y * matrix[4]) + (z * matrix[8]) + matrix[12];
  7790. var ty = (x * matrix[1]) + (y * matrix[5]) + (z * matrix[9]) + matrix[13];
  7791. var tz = (x * matrix[2]) + (y * matrix[6]) + (z * matrix[10]) + matrix[14];
  7792. result[0] = matrix[0];
  7793. result[1] = matrix[1];
  7794. result[2] = matrix[2];
  7795. result[3] = matrix[3];
  7796. result[4] = matrix[4];
  7797. result[5] = matrix[5];
  7798. result[6] = matrix[6];
  7799. result[7] = matrix[7];
  7800. result[8] = matrix[8];
  7801. result[9] = matrix[9];
  7802. result[10] = matrix[10];
  7803. result[11] = matrix[11];
  7804. result[12] = tx;
  7805. result[13] = ty;
  7806. result[14] = tz;
  7807. result[15] = matrix[15];
  7808. return result;
  7809. };
  7810. var uniformScaleScratch = new Cartesian3();
  7811. /**
  7812. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7813. * by an implicit uniform scale matrix. This is an optimization
  7814. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  7815. * <code>m</code> must be an affine matrix.
  7816. * This function performs fewer allocations and arithmetic operations.
  7817. *
  7818. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  7819. * @param {Number} scale The uniform scale on the right-hand side.
  7820. * @param {Matrix4} result The object onto which to store the result.
  7821. * @returns {Matrix4} The modified result parameter.
  7822. *
  7823. *
  7824. * @example
  7825. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  7826. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  7827. *
  7828. * @see Matrix4.fromUniformScale
  7829. * @see Matrix4.multiplyByScale
  7830. */
  7831. Matrix4.multiplyByUniformScale = function(matrix, scale, result) {
  7832. if (!defined(matrix)) {
  7833. throw new DeveloperError('matrix is required');
  7834. }
  7835. if (typeof scale !== 'number') {
  7836. throw new DeveloperError('scale is required');
  7837. }
  7838. if (!defined(result)) {
  7839. throw new DeveloperError('result is required');
  7840. }
  7841. uniformScaleScratch.x = scale;
  7842. uniformScaleScratch.y = scale;
  7843. uniformScaleScratch.z = scale;
  7844. return Matrix4.multiplyByScale(matrix, uniformScaleScratch, result);
  7845. };
  7846. /**
  7847. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7848. * by an implicit non-uniform scale matrix. This is an optimization
  7849. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  7850. * <code>m</code> must be an affine matrix.
  7851. * This function performs fewer allocations and arithmetic operations.
  7852. *
  7853. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  7854. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  7855. * @param {Matrix4} result The object onto which to store the result.
  7856. * @returns {Matrix4} The modified result parameter.
  7857. *
  7858. *
  7859. * @example
  7860. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  7861. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  7862. *
  7863. * @see Matrix4.fromScale
  7864. * @see Matrix4.multiplyByUniformScale
  7865. */
  7866. Matrix4.multiplyByScale = function(matrix, scale, result) {
  7867. if (!defined(matrix)) {
  7868. throw new DeveloperError('matrix is required');
  7869. }
  7870. if (!defined(scale)) {
  7871. throw new DeveloperError('scale is required');
  7872. }
  7873. if (!defined(result)) {
  7874. throw new DeveloperError('result is required');
  7875. }
  7876. var scaleX = scale.x;
  7877. var scaleY = scale.y;
  7878. var scaleZ = scale.z;
  7879. // Faster than Cartesian3.equals
  7880. if ((scaleX === 1.0) && (scaleY === 1.0) && (scaleZ === 1.0)) {
  7881. return Matrix4.clone(matrix, result);
  7882. }
  7883. result[0] = scaleX * matrix[0];
  7884. result[1] = scaleX * matrix[1];
  7885. result[2] = scaleX * matrix[2];
  7886. result[3] = 0.0;
  7887. result[4] = scaleY * matrix[4];
  7888. result[5] = scaleY * matrix[5];
  7889. result[6] = scaleY * matrix[6];
  7890. result[7] = 0.0;
  7891. result[8] = scaleZ * matrix[8];
  7892. result[9] = scaleZ * matrix[9];
  7893. result[10] = scaleZ * matrix[10];
  7894. result[11] = 0.0;
  7895. result[12] = matrix[12];
  7896. result[13] = matrix[13];
  7897. result[14] = matrix[14];
  7898. result[15] = 1.0;
  7899. return result;
  7900. };
  7901. /**
  7902. * Computes the product of a matrix and a column vector.
  7903. *
  7904. * @param {Matrix4} matrix The matrix.
  7905. * @param {Cartesian4} cartesian The vector.
  7906. * @param {Cartesian4} result The object onto which to store the result.
  7907. * @returns {Cartesian4} The modified result parameter.
  7908. */
  7909. Matrix4.multiplyByVector = function(matrix, cartesian, result) {
  7910. if (!defined(matrix)) {
  7911. throw new DeveloperError('matrix is required');
  7912. }
  7913. if (!defined(cartesian)) {
  7914. throw new DeveloperError('cartesian is required');
  7915. }
  7916. if (!defined(result)) {
  7917. throw new DeveloperError('result is required');
  7918. }
  7919. var vX = cartesian.x;
  7920. var vY = cartesian.y;
  7921. var vZ = cartesian.z;
  7922. var vW = cartesian.w;
  7923. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  7924. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  7925. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  7926. var w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  7927. result.x = x;
  7928. result.y = y;
  7929. result.z = z;
  7930. result.w = w;
  7931. return result;
  7932. };
  7933. /**
  7934. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  7935. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  7936. *
  7937. * @param {Matrix4} matrix The matrix.
  7938. * @param {Cartesian3} cartesian The point.
  7939. * @param {Cartesian3} result The object onto which to store the result.
  7940. * @returns {Cartesian3} The modified result parameter.
  7941. *
  7942. * @example
  7943. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  7944. * var result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  7945. * // A shortcut for
  7946. * // Cartesian3 p = ...
  7947. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  7948. */
  7949. Matrix4.multiplyByPointAsVector = function(matrix, cartesian, result) {
  7950. if (!defined(matrix)) {
  7951. throw new DeveloperError('matrix is required');
  7952. }
  7953. if (!defined(cartesian)) {
  7954. throw new DeveloperError('cartesian is required');
  7955. }
  7956. if (!defined(result)) {
  7957. throw new DeveloperError('result is required');
  7958. }
  7959. var vX = cartesian.x;
  7960. var vY = cartesian.y;
  7961. var vZ = cartesian.z;
  7962. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  7963. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  7964. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  7965. result.x = x;
  7966. result.y = y;
  7967. result.z = z;
  7968. return result;
  7969. };
  7970. /**
  7971. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  7972. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  7973. *
  7974. * @param {Matrix4} matrix The matrix.
  7975. * @param {Cartesian3} cartesian The point.
  7976. * @param {Cartesian3} result The object onto which to store the result.
  7977. * @returns {Cartesian3} The modified result parameter.
  7978. *
  7979. * @example
  7980. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  7981. * var result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  7982. */
  7983. Matrix4.multiplyByPoint = function(matrix, cartesian, result) {
  7984. if (!defined(matrix)) {
  7985. throw new DeveloperError('matrix is required');
  7986. }
  7987. if (!defined(cartesian)) {
  7988. throw new DeveloperError('cartesian is required');
  7989. }
  7990. if (!defined(result)) {
  7991. throw new DeveloperError('result is required');
  7992. }
  7993. var vX = cartesian.x;
  7994. var vY = cartesian.y;
  7995. var vZ = cartesian.z;
  7996. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  7997. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  7998. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  7999. result.x = x;
  8000. result.y = y;
  8001. result.z = z;
  8002. return result;
  8003. };
  8004. /**
  8005. * Computes the product of a matrix and a scalar.
  8006. *
  8007. * @param {Matrix4} matrix The matrix.
  8008. * @param {Number} scalar The number to multiply by.
  8009. * @param {Matrix4} result The object onto which to store the result.
  8010. * @returns {Matrix4} The modified result parameter.
  8011. *
  8012. * @example
  8013. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  8014. * // m = [10.0, 11.0, 12.0, 13.0]
  8015. * // [14.0, 15.0, 16.0, 17.0]
  8016. * // [18.0, 19.0, 20.0, 21.0]
  8017. * // [22.0, 23.0, 24.0, 25.0]
  8018. *
  8019. * var a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  8020. *
  8021. * // m remains the same
  8022. * // a = [-20.0, -22.0, -24.0, -26.0]
  8023. * // [-28.0, -30.0, -32.0, -34.0]
  8024. * // [-36.0, -38.0, -40.0, -42.0]
  8025. * // [-44.0, -46.0, -48.0, -50.0]
  8026. */
  8027. Matrix4.multiplyByScalar = function(matrix, scalar, result) {
  8028. if (!defined(matrix)) {
  8029. throw new DeveloperError('matrix is required');
  8030. }
  8031. if (typeof scalar !== 'number') {
  8032. throw new DeveloperError('scalar must be a number');
  8033. }
  8034. if (!defined(result)) {
  8035. throw new DeveloperError('result is required');
  8036. }
  8037. result[0] = matrix[0] * scalar;
  8038. result[1] = matrix[1] * scalar;
  8039. result[2] = matrix[2] * scalar;
  8040. result[3] = matrix[3] * scalar;
  8041. result[4] = matrix[4] * scalar;
  8042. result[5] = matrix[5] * scalar;
  8043. result[6] = matrix[6] * scalar;
  8044. result[7] = matrix[7] * scalar;
  8045. result[8] = matrix[8] * scalar;
  8046. result[9] = matrix[9] * scalar;
  8047. result[10] = matrix[10] * scalar;
  8048. result[11] = matrix[11] * scalar;
  8049. result[12] = matrix[12] * scalar;
  8050. result[13] = matrix[13] * scalar;
  8051. result[14] = matrix[14] * scalar;
  8052. result[15] = matrix[15] * scalar;
  8053. return result;
  8054. };
  8055. /**
  8056. * Computes a negated copy of the provided matrix.
  8057. *
  8058. * @param {Matrix4} matrix The matrix to negate.
  8059. * @param {Matrix4} result The object onto which to store the result.
  8060. * @returns {Matrix4} The modified result parameter.
  8061. *
  8062. * @example
  8063. * //create a new Matrix4 instance which is a negation of a Matrix4
  8064. * // m = [10.0, 11.0, 12.0, 13.0]
  8065. * // [14.0, 15.0, 16.0, 17.0]
  8066. * // [18.0, 19.0, 20.0, 21.0]
  8067. * // [22.0, 23.0, 24.0, 25.0]
  8068. *
  8069. * var a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  8070. *
  8071. * // m remains the same
  8072. * // a = [-10.0, -11.0, -12.0, -13.0]
  8073. * // [-14.0, -15.0, -16.0, -17.0]
  8074. * // [-18.0, -19.0, -20.0, -21.0]
  8075. * // [-22.0, -23.0, -24.0, -25.0]
  8076. */
  8077. Matrix4.negate = function(matrix, result) {
  8078. if (!defined(matrix)) {
  8079. throw new DeveloperError('matrix is required');
  8080. }
  8081. if (!defined(result)) {
  8082. throw new DeveloperError('result is required');
  8083. }
  8084. result[0] = -matrix[0];
  8085. result[1] = -matrix[1];
  8086. result[2] = -matrix[2];
  8087. result[3] = -matrix[3];
  8088. result[4] = -matrix[4];
  8089. result[5] = -matrix[5];
  8090. result[6] = -matrix[6];
  8091. result[7] = -matrix[7];
  8092. result[8] = -matrix[8];
  8093. result[9] = -matrix[9];
  8094. result[10] = -matrix[10];
  8095. result[11] = -matrix[11];
  8096. result[12] = -matrix[12];
  8097. result[13] = -matrix[13];
  8098. result[14] = -matrix[14];
  8099. result[15] = -matrix[15];
  8100. return result;
  8101. };
  8102. /**
  8103. * Computes the transpose of the provided matrix.
  8104. *
  8105. * @param {Matrix4} matrix The matrix to transpose.
  8106. * @param {Matrix4} result The object onto which to store the result.
  8107. * @returns {Matrix4} The modified result parameter.
  8108. *
  8109. * @example
  8110. * //returns transpose of a Matrix4
  8111. * // m = [10.0, 11.0, 12.0, 13.0]
  8112. * // [14.0, 15.0, 16.0, 17.0]
  8113. * // [18.0, 19.0, 20.0, 21.0]
  8114. * // [22.0, 23.0, 24.0, 25.0]
  8115. *
  8116. * var a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  8117. *
  8118. * // m remains the same
  8119. * // a = [10.0, 14.0, 18.0, 22.0]
  8120. * // [11.0, 15.0, 19.0, 23.0]
  8121. * // [12.0, 16.0, 20.0, 24.0]
  8122. * // [13.0, 17.0, 21.0, 25.0]
  8123. */
  8124. Matrix4.transpose = function(matrix, result) {
  8125. if (!defined(matrix)) {
  8126. throw new DeveloperError('matrix is required');
  8127. }
  8128. if (!defined(result)) {
  8129. throw new DeveloperError('result is required');
  8130. }
  8131. var matrix1 = matrix[1];
  8132. var matrix2 = matrix[2];
  8133. var matrix3 = matrix[3];
  8134. var matrix6 = matrix[6];
  8135. var matrix7 = matrix[7];
  8136. var matrix11 = matrix[11];
  8137. result[0] = matrix[0];
  8138. result[1] = matrix[4];
  8139. result[2] = matrix[8];
  8140. result[3] = matrix[12];
  8141. result[4] = matrix1;
  8142. result[5] = matrix[5];
  8143. result[6] = matrix[9];
  8144. result[7] = matrix[13];
  8145. result[8] = matrix2;
  8146. result[9] = matrix6;
  8147. result[10] = matrix[10];
  8148. result[11] = matrix[14];
  8149. result[12] = matrix3;
  8150. result[13] = matrix7;
  8151. result[14] = matrix11;
  8152. result[15] = matrix[15];
  8153. return result;
  8154. };
  8155. /**
  8156. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  8157. *
  8158. * @param {Matrix4} matrix The matrix with signed elements.
  8159. * @param {Matrix4} result The object onto which to store the result.
  8160. * @returns {Matrix4} The modified result parameter.
  8161. */
  8162. Matrix4.abs = function(matrix, result) {
  8163. if (!defined(matrix)) {
  8164. throw new DeveloperError('matrix is required');
  8165. }
  8166. if (!defined(result)) {
  8167. throw new DeveloperError('result is required');
  8168. }
  8169. result[0] = Math.abs(matrix[0]);
  8170. result[1] = Math.abs(matrix[1]);
  8171. result[2] = Math.abs(matrix[2]);
  8172. result[3] = Math.abs(matrix[3]);
  8173. result[4] = Math.abs(matrix[4]);
  8174. result[5] = Math.abs(matrix[5]);
  8175. result[6] = Math.abs(matrix[6]);
  8176. result[7] = Math.abs(matrix[7]);
  8177. result[8] = Math.abs(matrix[8]);
  8178. result[9] = Math.abs(matrix[9]);
  8179. result[10] = Math.abs(matrix[10]);
  8180. result[11] = Math.abs(matrix[11]);
  8181. result[12] = Math.abs(matrix[12]);
  8182. result[13] = Math.abs(matrix[13]);
  8183. result[14] = Math.abs(matrix[14]);
  8184. result[15] = Math.abs(matrix[15]);
  8185. return result;
  8186. };
  8187. /**
  8188. * Compares the provided matrices componentwise and returns
  8189. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8190. *
  8191. * @param {Matrix4} [left] The first matrix.
  8192. * @param {Matrix4} [right] The second matrix.
  8193. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  8194. *
  8195. * @example
  8196. * //compares two Matrix4 instances
  8197. *
  8198. * // a = [10.0, 14.0, 18.0, 22.0]
  8199. * // [11.0, 15.0, 19.0, 23.0]
  8200. * // [12.0, 16.0, 20.0, 24.0]
  8201. * // [13.0, 17.0, 21.0, 25.0]
  8202. *
  8203. * // b = [10.0, 14.0, 18.0, 22.0]
  8204. * // [11.0, 15.0, 19.0, 23.0]
  8205. * // [12.0, 16.0, 20.0, 24.0]
  8206. * // [13.0, 17.0, 21.0, 25.0]
  8207. *
  8208. * if(Cesium.Matrix4.equals(a,b)) {
  8209. * console.log("Both matrices are equal");
  8210. * } else {
  8211. * console.log("They are not equal");
  8212. * }
  8213. *
  8214. * //Prints "Both matrices are equal" on the console
  8215. */
  8216. Matrix4.equals = function(left, right) {
  8217. // Given that most matrices will be transformation matrices, the elements
  8218. // are tested in order such that the test is likely to fail as early
  8219. // as possible. I _think_ this is just as friendly to the L1 cache
  8220. // as testing in index order. It is certainty faster in practice.
  8221. return (left === right) ||
  8222. (defined(left) &&
  8223. defined(right) &&
  8224. // Translation
  8225. left[12] === right[12] &&
  8226. left[13] === right[13] &&
  8227. left[14] === right[14] &&
  8228. // Rotation/scale
  8229. left[0] === right[0] &&
  8230. left[1] === right[1] &&
  8231. left[2] === right[2] &&
  8232. left[4] === right[4] &&
  8233. left[5] === right[5] &&
  8234. left[6] === right[6] &&
  8235. left[8] === right[8] &&
  8236. left[9] === right[9] &&
  8237. left[10] === right[10] &&
  8238. // Bottom row
  8239. left[3] === right[3] &&
  8240. left[7] === right[7] &&
  8241. left[11] === right[11] &&
  8242. left[15] === right[15]);
  8243. };
  8244. /**
  8245. * Compares the provided matrices componentwise and returns
  8246. * <code>true</code> if they are within the provided epsilon,
  8247. * <code>false</code> otherwise.
  8248. *
  8249. * @param {Matrix4} [left] The first matrix.
  8250. * @param {Matrix4} [right] The second matrix.
  8251. * @param {Number} epsilon The epsilon to use for equality testing.
  8252. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  8253. *
  8254. * @example
  8255. * //compares two Matrix4 instances
  8256. *
  8257. * // a = [10.5, 14.5, 18.5, 22.5]
  8258. * // [11.5, 15.5, 19.5, 23.5]
  8259. * // [12.5, 16.5, 20.5, 24.5]
  8260. * // [13.5, 17.5, 21.5, 25.5]
  8261. *
  8262. * // b = [10.0, 14.0, 18.0, 22.0]
  8263. * // [11.0, 15.0, 19.0, 23.0]
  8264. * // [12.0, 16.0, 20.0, 24.0]
  8265. * // [13.0, 17.0, 21.0, 25.0]
  8266. *
  8267. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  8268. * console.log("Difference between both the matrices is less than 0.1");
  8269. * } else {
  8270. * console.log("Difference between both the matrices is not less than 0.1");
  8271. * }
  8272. *
  8273. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  8274. */
  8275. Matrix4.equalsEpsilon = function(left, right, epsilon) {
  8276. if (typeof epsilon !== 'number') {
  8277. throw new DeveloperError('epsilon must be a number');
  8278. }
  8279. return (left === right) ||
  8280. (defined(left) &&
  8281. defined(right) &&
  8282. Math.abs(left[0] - right[0]) <= epsilon &&
  8283. Math.abs(left[1] - right[1]) <= epsilon &&
  8284. Math.abs(left[2] - right[2]) <= epsilon &&
  8285. Math.abs(left[3] - right[3]) <= epsilon &&
  8286. Math.abs(left[4] - right[4]) <= epsilon &&
  8287. Math.abs(left[5] - right[5]) <= epsilon &&
  8288. Math.abs(left[6] - right[6]) <= epsilon &&
  8289. Math.abs(left[7] - right[7]) <= epsilon &&
  8290. Math.abs(left[8] - right[8]) <= epsilon &&
  8291. Math.abs(left[9] - right[9]) <= epsilon &&
  8292. Math.abs(left[10] - right[10]) <= epsilon &&
  8293. Math.abs(left[11] - right[11]) <= epsilon &&
  8294. Math.abs(left[12] - right[12]) <= epsilon &&
  8295. Math.abs(left[13] - right[13]) <= epsilon &&
  8296. Math.abs(left[14] - right[14]) <= epsilon &&
  8297. Math.abs(left[15] - right[15]) <= epsilon);
  8298. };
  8299. /**
  8300. * Gets the translation portion of the provided matrix, assuming the matrix is a affine transformation matrix.
  8301. *
  8302. * @param {Matrix4} matrix The matrix to use.
  8303. * @param {Cartesian3} result The object onto which to store the result.
  8304. * @returns {Cartesian3} The modified result parameter.
  8305. */
  8306. Matrix4.getTranslation = function(matrix, result) {
  8307. if (!defined(matrix)) {
  8308. throw new DeveloperError('matrix is required');
  8309. }
  8310. if (!defined(result)) {
  8311. throw new DeveloperError('result is required');
  8312. }
  8313. result.x = matrix[12];
  8314. result.y = matrix[13];
  8315. result.z = matrix[14];
  8316. return result;
  8317. };
  8318. /**
  8319. * Gets the upper left 3x3 rotation matrix of the provided matrix, assuming the matrix is a affine transformation matrix.
  8320. *
  8321. * @param {Matrix4} matrix The matrix to use.
  8322. * @param {Matrix3} result The object onto which to store the result.
  8323. * @returns {Matrix3} The modified result parameter.
  8324. *
  8325. * @example
  8326. * // returns a Matrix3 instance from a Matrix4 instance
  8327. *
  8328. * // m = [10.0, 14.0, 18.0, 22.0]
  8329. * // [11.0, 15.0, 19.0, 23.0]
  8330. * // [12.0, 16.0, 20.0, 24.0]
  8331. * // [13.0, 17.0, 21.0, 25.0]
  8332. *
  8333. * var b = new Cesium.Matrix3();
  8334. * Cesium.Matrix4.getRotation(m,b);
  8335. *
  8336. * // b = [10.0, 14.0, 18.0]
  8337. * // [11.0, 15.0, 19.0]
  8338. * // [12.0, 16.0, 20.0]
  8339. */
  8340. Matrix4.getRotation = function(matrix, result) {
  8341. if (!defined(matrix)) {
  8342. throw new DeveloperError('matrix is required');
  8343. }
  8344. if (!defined(result)) {
  8345. throw new DeveloperError('result is required');
  8346. }
  8347. result[0] = matrix[0];
  8348. result[1] = matrix[1];
  8349. result[2] = matrix[2];
  8350. result[3] = matrix[4];
  8351. result[4] = matrix[5];
  8352. result[5] = matrix[6];
  8353. result[6] = matrix[8];
  8354. result[7] = matrix[9];
  8355. result[8] = matrix[10];
  8356. return result;
  8357. };
  8358. var scratchInverseRotation = new Matrix3();
  8359. var scratchMatrix3Zero = new Matrix3();
  8360. var scratchBottomRow = new Cartesian4();
  8361. var scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  8362. /**
  8363. * Computes the inverse of the provided matrix using Cramers Rule.
  8364. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  8365. * If the matrix is an affine transformation matrix, it is more efficient
  8366. * to invert it with {@link Matrix4.inverseTransformation}.
  8367. *
  8368. * @param {Matrix4} matrix The matrix to invert.
  8369. * @param {Matrix4} result The object onto which to store the result.
  8370. * @returns {Matrix4} The modified result parameter.
  8371. *
  8372. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  8373. */
  8374. Matrix4.inverse = function(matrix, result) {
  8375. if (!defined(matrix)) {
  8376. throw new DeveloperError('matrix is required');
  8377. }
  8378. if (!defined(result)) {
  8379. throw new DeveloperError('result is required');
  8380. }
  8381. // Special case for a zero scale matrix that can occur, for example,
  8382. // when a model's node has a [0, 0, 0] scale.
  8383. if (Matrix3.equalsEpsilon(Matrix4.getRotation(matrix, scratchInverseRotation), scratchMatrix3Zero, CesiumMath.EPSILON7) &&
  8384. Cartesian4.equals(Matrix4.getRow(matrix, 3, scratchBottomRow), scratchExpectedBottomRow)) {
  8385. result[0] = 0.0;
  8386. result[1] = 0.0;
  8387. result[2] = 0.0;
  8388. result[3] = 0.0;
  8389. result[4] = 0.0;
  8390. result[5] = 0.0;
  8391. result[6] = 0.0;
  8392. result[7] = 0.0;
  8393. result[8] = 0.0;
  8394. result[9] = 0.0;
  8395. result[10] = 0.0;
  8396. result[11] = 0.0;
  8397. result[12] = -matrix[12];
  8398. result[13] = -matrix[13];
  8399. result[14] = -matrix[14];
  8400. result[15] = 1.0;
  8401. return result;
  8402. }
  8403. //
  8404. // Ported from:
  8405. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  8406. //
  8407. var src0 = matrix[0];
  8408. var src1 = matrix[4];
  8409. var src2 = matrix[8];
  8410. var src3 = matrix[12];
  8411. var src4 = matrix[1];
  8412. var src5 = matrix[5];
  8413. var src6 = matrix[9];
  8414. var src7 = matrix[13];
  8415. var src8 = matrix[2];
  8416. var src9 = matrix[6];
  8417. var src10 = matrix[10];
  8418. var src11 = matrix[14];
  8419. var src12 = matrix[3];
  8420. var src13 = matrix[7];
  8421. var src14 = matrix[11];
  8422. var src15 = matrix[15];
  8423. // calculate pairs for first 8 elements (cofactors)
  8424. var tmp0 = src10 * src15;
  8425. var tmp1 = src11 * src14;
  8426. var tmp2 = src9 * src15;
  8427. var tmp3 = src11 * src13;
  8428. var tmp4 = src9 * src14;
  8429. var tmp5 = src10 * src13;
  8430. var tmp6 = src8 * src15;
  8431. var tmp7 = src11 * src12;
  8432. var tmp8 = src8 * src14;
  8433. var tmp9 = src10 * src12;
  8434. var tmp10 = src8 * src13;
  8435. var tmp11 = src9 * src12;
  8436. // calculate first 8 elements (cofactors)
  8437. var dst0 = (tmp0 * src5 + tmp3 * src6 + tmp4 * src7) - (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  8438. var dst1 = (tmp1 * src4 + tmp6 * src6 + tmp9 * src7) - (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  8439. var dst2 = (tmp2 * src4 + tmp7 * src5 + tmp10 * src7) - (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  8440. var dst3 = (tmp5 * src4 + tmp8 * src5 + tmp11 * src6) - (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  8441. var dst4 = (tmp1 * src1 + tmp2 * src2 + tmp5 * src3) - (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  8442. var dst5 = (tmp0 * src0 + tmp7 * src2 + tmp8 * src3) - (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  8443. var dst6 = (tmp3 * src0 + tmp6 * src1 + tmp11 * src3) - (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  8444. var dst7 = (tmp4 * src0 + tmp9 * src1 + tmp10 * src2) - (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  8445. // calculate pairs for second 8 elements (cofactors)
  8446. tmp0 = src2 * src7;
  8447. tmp1 = src3 * src6;
  8448. tmp2 = src1 * src7;
  8449. tmp3 = src3 * src5;
  8450. tmp4 = src1 * src6;
  8451. tmp5 = src2 * src5;
  8452. tmp6 = src0 * src7;
  8453. tmp7 = src3 * src4;
  8454. tmp8 = src0 * src6;
  8455. tmp9 = src2 * src4;
  8456. tmp10 = src0 * src5;
  8457. tmp11 = src1 * src4;
  8458. // calculate second 8 elements (cofactors)
  8459. var dst8 = (tmp0 * src13 + tmp3 * src14 + tmp4 * src15) - (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  8460. var dst9 = (tmp1 * src12 + tmp6 * src14 + tmp9 * src15) - (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  8461. var dst10 = (tmp2 * src12 + tmp7 * src13 + tmp10 * src15) - (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  8462. var dst11 = (tmp5 * src12 + tmp8 * src13 + tmp11 * src14) - (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  8463. var dst12 = (tmp2 * src10 + tmp5 * src11 + tmp1 * src9) - (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  8464. var dst13 = (tmp8 * src11 + tmp0 * src8 + tmp7 * src10) - (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  8465. var dst14 = (tmp6 * src9 + tmp11 * src11 + tmp3 * src8) - (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  8466. var dst15 = (tmp10 * src10 + tmp4 * src8 + tmp9 * src9) - (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  8467. // calculate determinant
  8468. var det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  8469. if (Math.abs(det) < CesiumMath.EPSILON20) {
  8470. throw new RuntimeError('matrix is not invertible because its determinate is zero.');
  8471. }
  8472. // calculate matrix inverse
  8473. det = 1.0 / det;
  8474. result[0] = dst0 * det;
  8475. result[1] = dst1 * det;
  8476. result[2] = dst2 * det;
  8477. result[3] = dst3 * det;
  8478. result[4] = dst4 * det;
  8479. result[5] = dst5 * det;
  8480. result[6] = dst6 * det;
  8481. result[7] = dst7 * det;
  8482. result[8] = dst8 * det;
  8483. result[9] = dst9 * det;
  8484. result[10] = dst10 * det;
  8485. result[11] = dst11 * det;
  8486. result[12] = dst12 * det;
  8487. result[13] = dst13 * det;
  8488. result[14] = dst14 * det;
  8489. result[15] = dst15 * det;
  8490. return result;
  8491. };
  8492. /**
  8493. * Computes the inverse of the provided matrix assuming it is
  8494. * an affine transformation matrix, where the upper left 3x3 elements
  8495. * are a rotation matrix, and the upper three elements in the fourth
  8496. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  8497. * The matrix is not verified to be in the proper form.
  8498. * This method is faster than computing the inverse for a general 4x4
  8499. * matrix using {@link Matrix4.inverse}.
  8500. *
  8501. * @param {Matrix4} matrix The matrix to invert.
  8502. * @param {Matrix4} result The object onto which to store the result.
  8503. * @returns {Matrix4} The modified result parameter.
  8504. */
  8505. Matrix4.inverseTransformation = function(matrix, result) {
  8506. if (!defined(matrix)) {
  8507. throw new DeveloperError('matrix is required');
  8508. }
  8509. if (!defined(result)) {
  8510. throw new DeveloperError('result is required');
  8511. }
  8512. //This function is an optimized version of the below 4 lines.
  8513. //var rT = Matrix3.transpose(Matrix4.getRotation(matrix));
  8514. //var rTN = Matrix3.negate(rT);
  8515. //var rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  8516. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  8517. var matrix0 = matrix[0];
  8518. var matrix1 = matrix[1];
  8519. var matrix2 = matrix[2];
  8520. var matrix4 = matrix[4];
  8521. var matrix5 = matrix[5];
  8522. var matrix6 = matrix[6];
  8523. var matrix8 = matrix[8];
  8524. var matrix9 = matrix[9];
  8525. var matrix10 = matrix[10];
  8526. var vX = matrix[12];
  8527. var vY = matrix[13];
  8528. var vZ = matrix[14];
  8529. var x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  8530. var y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  8531. var z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  8532. result[0] = matrix0;
  8533. result[1] = matrix4;
  8534. result[2] = matrix8;
  8535. result[3] = 0.0;
  8536. result[4] = matrix1;
  8537. result[5] = matrix5;
  8538. result[6] = matrix9;
  8539. result[7] = 0.0;
  8540. result[8] = matrix2;
  8541. result[9] = matrix6;
  8542. result[10] = matrix10;
  8543. result[11] = 0.0;
  8544. result[12] = x;
  8545. result[13] = y;
  8546. result[14] = z;
  8547. result[15] = 1.0;
  8548. return result;
  8549. };
  8550. /**
  8551. * An immutable Matrix4 instance initialized to the identity matrix.
  8552. *
  8553. * @type {Matrix4}
  8554. * @constant
  8555. */
  8556. Matrix4.IDENTITY = freezeObject(new Matrix4(1.0, 0.0, 0.0, 0.0,
  8557. 0.0, 1.0, 0.0, 0.0,
  8558. 0.0, 0.0, 1.0, 0.0,
  8559. 0.0, 0.0, 0.0, 1.0));
  8560. /**
  8561. * An immutable Matrix4 instance initialized to the zero matrix.
  8562. *
  8563. * @type {Matrix4}
  8564. * @constant
  8565. */
  8566. Matrix4.ZERO = freezeObject(new Matrix4(0.0, 0.0, 0.0, 0.0,
  8567. 0.0, 0.0, 0.0, 0.0,
  8568. 0.0, 0.0, 0.0, 0.0,
  8569. 0.0, 0.0, 0.0, 0.0));
  8570. /**
  8571. * The index into Matrix4 for column 0, row 0.
  8572. *
  8573. * @type {Number}
  8574. * @constant
  8575. */
  8576. Matrix4.COLUMN0ROW0 = 0;
  8577. /**
  8578. * The index into Matrix4 for column 0, row 1.
  8579. *
  8580. * @type {Number}
  8581. * @constant
  8582. */
  8583. Matrix4.COLUMN0ROW1 = 1;
  8584. /**
  8585. * The index into Matrix4 for column 0, row 2.
  8586. *
  8587. * @type {Number}
  8588. * @constant
  8589. */
  8590. Matrix4.COLUMN0ROW2 = 2;
  8591. /**
  8592. * The index into Matrix4 for column 0, row 3.
  8593. *
  8594. * @type {Number}
  8595. * @constant
  8596. */
  8597. Matrix4.COLUMN0ROW3 = 3;
  8598. /**
  8599. * The index into Matrix4 for column 1, row 0.
  8600. *
  8601. * @type {Number}
  8602. * @constant
  8603. */
  8604. Matrix4.COLUMN1ROW0 = 4;
  8605. /**
  8606. * The index into Matrix4 for column 1, row 1.
  8607. *
  8608. * @type {Number}
  8609. * @constant
  8610. */
  8611. Matrix4.COLUMN1ROW1 = 5;
  8612. /**
  8613. * The index into Matrix4 for column 1, row 2.
  8614. *
  8615. * @type {Number}
  8616. * @constant
  8617. */
  8618. Matrix4.COLUMN1ROW2 = 6;
  8619. /**
  8620. * The index into Matrix4 for column 1, row 3.
  8621. *
  8622. * @type {Number}
  8623. * @constant
  8624. */
  8625. Matrix4.COLUMN1ROW3 = 7;
  8626. /**
  8627. * The index into Matrix4 for column 2, row 0.
  8628. *
  8629. * @type {Number}
  8630. * @constant
  8631. */
  8632. Matrix4.COLUMN2ROW0 = 8;
  8633. /**
  8634. * The index into Matrix4 for column 2, row 1.
  8635. *
  8636. * @type {Number}
  8637. * @constant
  8638. */
  8639. Matrix4.COLUMN2ROW1 = 9;
  8640. /**
  8641. * The index into Matrix4 for column 2, row 2.
  8642. *
  8643. * @type {Number}
  8644. * @constant
  8645. */
  8646. Matrix4.COLUMN2ROW2 = 10;
  8647. /**
  8648. * The index into Matrix4 for column 2, row 3.
  8649. *
  8650. * @type {Number}
  8651. * @constant
  8652. */
  8653. Matrix4.COLUMN2ROW3 = 11;
  8654. /**
  8655. * The index into Matrix4 for column 3, row 0.
  8656. *
  8657. * @type {Number}
  8658. * @constant
  8659. */
  8660. Matrix4.COLUMN3ROW0 = 12;
  8661. /**
  8662. * The index into Matrix4 for column 3, row 1.
  8663. *
  8664. * @type {Number}
  8665. * @constant
  8666. */
  8667. Matrix4.COLUMN3ROW1 = 13;
  8668. /**
  8669. * The index into Matrix4 for column 3, row 2.
  8670. *
  8671. * @type {Number}
  8672. * @constant
  8673. */
  8674. Matrix4.COLUMN3ROW2 = 14;
  8675. /**
  8676. * The index into Matrix4 for column 3, row 3.
  8677. *
  8678. * @type {Number}
  8679. * @constant
  8680. */
  8681. Matrix4.COLUMN3ROW3 = 15;
  8682. defineProperties(Matrix4.prototype, {
  8683. /**
  8684. * Gets the number of items in the collection.
  8685. * @memberof Matrix4.prototype
  8686. *
  8687. * @type {Number}
  8688. */
  8689. length : {
  8690. get : function() {
  8691. return Matrix4.packedLength;
  8692. }
  8693. }
  8694. });
  8695. /**
  8696. * Duplicates the provided Matrix4 instance.
  8697. *
  8698. * @param {Matrix4} [result] The object onto which to store the result.
  8699. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  8700. */
  8701. Matrix4.prototype.clone = function(result) {
  8702. return Matrix4.clone(this, result);
  8703. };
  8704. /**
  8705. * Compares this matrix to the provided matrix componentwise and returns
  8706. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8707. *
  8708. * @param {Matrix4} [right] The right hand side matrix.
  8709. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  8710. */
  8711. Matrix4.prototype.equals = function(right) {
  8712. return Matrix4.equals(this, right);
  8713. };
  8714. /**
  8715. * @private
  8716. */
  8717. Matrix4.equalsArray = function(matrix, array, offset) {
  8718. return matrix[0] === array[offset] &&
  8719. matrix[1] === array[offset + 1] &&
  8720. matrix[2] === array[offset + 2] &&
  8721. matrix[3] === array[offset + 3] &&
  8722. matrix[4] === array[offset + 4] &&
  8723. matrix[5] === array[offset + 5] &&
  8724. matrix[6] === array[offset + 6] &&
  8725. matrix[7] === array[offset + 7] &&
  8726. matrix[8] === array[offset + 8] &&
  8727. matrix[9] === array[offset + 9] &&
  8728. matrix[10] === array[offset + 10] &&
  8729. matrix[11] === array[offset + 11] &&
  8730. matrix[12] === array[offset + 12] &&
  8731. matrix[13] === array[offset + 13] &&
  8732. matrix[14] === array[offset + 14] &&
  8733. matrix[15] === array[offset + 15];
  8734. };
  8735. /**
  8736. * Compares this matrix to the provided matrix componentwise and returns
  8737. * <code>true</code> if they are within the provided epsilon,
  8738. * <code>false</code> otherwise.
  8739. *
  8740. * @param {Matrix4} [right] The right hand side matrix.
  8741. * @param {Number} epsilon The epsilon to use for equality testing.
  8742. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  8743. */
  8744. Matrix4.prototype.equalsEpsilon = function(right, epsilon) {
  8745. return Matrix4.equalsEpsilon(this, right, epsilon);
  8746. };
  8747. /**
  8748. * Computes a string representing this Matrix with each row being
  8749. * on a separate line and in the format '(column0, column1, column2, column3)'.
  8750. *
  8751. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  8752. */
  8753. Matrix4.prototype.toString = function() {
  8754. return '(' + this[0] + ', ' + this[4] + ', ' + this[8] + ', ' + this[12] +')\n' +
  8755. '(' + this[1] + ', ' + this[5] + ', ' + this[9] + ', ' + this[13] +')\n' +
  8756. '(' + this[2] + ', ' + this[6] + ', ' + this[10] + ', ' + this[14] +')\n' +
  8757. '(' + this[3] + ', ' + this[7] + ', ' + this[11] + ', ' + this[15] +')';
  8758. };
  8759. return Matrix4;
  8760. });
  8761. /*global define*/
  8762. define('Core/BoundingSphere',[
  8763. './Cartesian3',
  8764. './Cartographic',
  8765. './defaultValue',
  8766. './defined',
  8767. './DeveloperError',
  8768. './Ellipsoid',
  8769. './GeographicProjection',
  8770. './Intersect',
  8771. './Interval',
  8772. './Matrix3',
  8773. './Matrix4',
  8774. './Rectangle'
  8775. ], function(
  8776. Cartesian3,
  8777. Cartographic,
  8778. defaultValue,
  8779. defined,
  8780. DeveloperError,
  8781. Ellipsoid,
  8782. GeographicProjection,
  8783. Intersect,
  8784. Interval,
  8785. Matrix3,
  8786. Matrix4,
  8787. Rectangle) {
  8788. 'use strict';
  8789. /**
  8790. * A bounding sphere with a center and a radius.
  8791. * @alias BoundingSphere
  8792. * @constructor
  8793. *
  8794. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  8795. * @param {Number} [radius=0.0] The radius of the bounding sphere.
  8796. *
  8797. * @see AxisAlignedBoundingBox
  8798. * @see BoundingRectangle
  8799. * @see Packable
  8800. */
  8801. function BoundingSphere(center, radius) {
  8802. /**
  8803. * The center point of the sphere.
  8804. * @type {Cartesian3}
  8805. * @default {@link Cartesian3.ZERO}
  8806. */
  8807. this.center = Cartesian3.clone(defaultValue(center, Cartesian3.ZERO));
  8808. /**
  8809. * The radius of the sphere.
  8810. * @type {Number}
  8811. * @default 0.0
  8812. */
  8813. this.radius = defaultValue(radius, 0.0);
  8814. }
  8815. var fromPointsXMin = new Cartesian3();
  8816. var fromPointsYMin = new Cartesian3();
  8817. var fromPointsZMin = new Cartesian3();
  8818. var fromPointsXMax = new Cartesian3();
  8819. var fromPointsYMax = new Cartesian3();
  8820. var fromPointsZMax = new Cartesian3();
  8821. var fromPointsCurrentPos = new Cartesian3();
  8822. var fromPointsScratch = new Cartesian3();
  8823. var fromPointsRitterCenter = new Cartesian3();
  8824. var fromPointsMinBoxPt = new Cartesian3();
  8825. var fromPointsMaxBoxPt = new Cartesian3();
  8826. var fromPointsNaiveCenterScratch = new Cartesian3();
  8827. /**
  8828. * Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
  8829. * The bounding sphere is computed by running two algorithms, a naive algorithm and
  8830. * Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
  8831. *
  8832. * @param {Cartesian3[]} positions An array of points that the bounding sphere will enclose. Each point must have <code>x</code>, <code>y</code>, and <code>z</code> properties.
  8833. * @param {BoundingSphere} [result] The object onto which to store the result.
  8834. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  8835. *
  8836. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  8837. */
  8838. BoundingSphere.fromPoints = function(positions, result) {
  8839. if (!defined(result)) {
  8840. result = new BoundingSphere();
  8841. }
  8842. if (!defined(positions) || positions.length === 0) {
  8843. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  8844. result.radius = 0.0;
  8845. return result;
  8846. }
  8847. var currentPos = Cartesian3.clone(positions[0], fromPointsCurrentPos);
  8848. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  8849. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  8850. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  8851. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  8852. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  8853. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  8854. var numPositions = positions.length;
  8855. for (var i = 1; i < numPositions; i++) {
  8856. Cartesian3.clone(positions[i], currentPos);
  8857. var x = currentPos.x;
  8858. var y = currentPos.y;
  8859. var z = currentPos.z;
  8860. // Store points containing the the smallest and largest components
  8861. if (x < xMin.x) {
  8862. Cartesian3.clone(currentPos, xMin);
  8863. }
  8864. if (x > xMax.x) {
  8865. Cartesian3.clone(currentPos, xMax);
  8866. }
  8867. if (y < yMin.y) {
  8868. Cartesian3.clone(currentPos, yMin);
  8869. }
  8870. if (y > yMax.y) {
  8871. Cartesian3.clone(currentPos, yMax);
  8872. }
  8873. if (z < zMin.z) {
  8874. Cartesian3.clone(currentPos, zMin);
  8875. }
  8876. if (z > zMax.z) {
  8877. Cartesian3.clone(currentPos, zMax);
  8878. }
  8879. }
  8880. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  8881. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  8882. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  8883. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  8884. // Set the diameter endpoints to the largest span.
  8885. var diameter1 = xMin;
  8886. var diameter2 = xMax;
  8887. var maxSpan = xSpan;
  8888. if (ySpan > maxSpan) {
  8889. maxSpan = ySpan;
  8890. diameter1 = yMin;
  8891. diameter2 = yMax;
  8892. }
  8893. if (zSpan > maxSpan) {
  8894. maxSpan = zSpan;
  8895. diameter1 = zMin;
  8896. diameter2 = zMax;
  8897. }
  8898. // Calculate the center of the initial sphere found by Ritter's algorithm
  8899. var ritterCenter = fromPointsRitterCenter;
  8900. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  8901. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  8902. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  8903. // Calculate the radius of the initial sphere found by Ritter's algorithm
  8904. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  8905. var ritterRadius = Math.sqrt(radiusSquared);
  8906. // Find the center of the sphere found using the Naive method.
  8907. var minBoxPt = fromPointsMinBoxPt;
  8908. minBoxPt.x = xMin.x;
  8909. minBoxPt.y = yMin.y;
  8910. minBoxPt.z = zMin.z;
  8911. var maxBoxPt = fromPointsMaxBoxPt;
  8912. maxBoxPt.x = xMax.x;
  8913. maxBoxPt.y = yMax.y;
  8914. maxBoxPt.z = zMax.z;
  8915. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  8916. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  8917. var naiveRadius = 0;
  8918. for (i = 0; i < numPositions; i++) {
  8919. Cartesian3.clone(positions[i], currentPos);
  8920. // Find the furthest point from the naive center to calculate the naive radius.
  8921. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  8922. if (r > naiveRadius) {
  8923. naiveRadius = r;
  8924. }
  8925. // Make adjustments to the Ritter Sphere to include all points.
  8926. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  8927. if (oldCenterToPointSquared > radiusSquared) {
  8928. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  8929. // Calculate new radius to include the point that lies outside
  8930. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  8931. radiusSquared = ritterRadius * ritterRadius;
  8932. // Calculate center of new Ritter sphere
  8933. var oldToNew = oldCenterToPoint - ritterRadius;
  8934. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  8935. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  8936. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  8937. }
  8938. }
  8939. if (ritterRadius < naiveRadius) {
  8940. Cartesian3.clone(ritterCenter, result.center);
  8941. result.radius = ritterRadius;
  8942. } else {
  8943. Cartesian3.clone(naiveCenter, result.center);
  8944. result.radius = naiveRadius;
  8945. }
  8946. return result;
  8947. };
  8948. var defaultProjection = new GeographicProjection();
  8949. var fromRectangle2DLowerLeft = new Cartesian3();
  8950. var fromRectangle2DUpperRight = new Cartesian3();
  8951. var fromRectangle2DSouthwest = new Cartographic();
  8952. var fromRectangle2DNortheast = new Cartographic();
  8953. /**
  8954. * Computes a bounding sphere from an rectangle projected in 2D.
  8955. *
  8956. * @param {Rectangle} rectangle The rectangle around which to create a bounding sphere.
  8957. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  8958. * @param {BoundingSphere} [result] The object onto which to store the result.
  8959. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  8960. */
  8961. BoundingSphere.fromRectangle2D = function(rectangle, projection, result) {
  8962. return BoundingSphere.fromRectangleWithHeights2D(rectangle, projection, 0.0, 0.0, result);
  8963. };
  8964. /**
  8965. * Computes a bounding sphere from an rectangle projected in 2D. The bounding sphere accounts for the
  8966. * object's minimum and maximum heights over the rectangle.
  8967. *
  8968. * @param {Rectangle} rectangle The rectangle around which to create a bounding sphere.
  8969. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  8970. * @param {Number} [minimumHeight=0.0] The minimum height over the rectangle.
  8971. * @param {Number} [maximumHeight=0.0] The maximum height over the rectangle.
  8972. * @param {BoundingSphere} [result] The object onto which to store the result.
  8973. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  8974. */
  8975. BoundingSphere.fromRectangleWithHeights2D = function(rectangle, projection, minimumHeight, maximumHeight, result) {
  8976. if (!defined(result)) {
  8977. result = new BoundingSphere();
  8978. }
  8979. if (!defined(rectangle)) {
  8980. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  8981. result.radius = 0.0;
  8982. return result;
  8983. }
  8984. projection = defaultValue(projection, defaultProjection);
  8985. Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
  8986. fromRectangle2DSouthwest.height = minimumHeight;
  8987. Rectangle.northeast(rectangle, fromRectangle2DNortheast);
  8988. fromRectangle2DNortheast.height = maximumHeight;
  8989. var lowerLeft = projection.project(fromRectangle2DSouthwest, fromRectangle2DLowerLeft);
  8990. var upperRight = projection.project(fromRectangle2DNortheast, fromRectangle2DUpperRight);
  8991. var width = upperRight.x - lowerLeft.x;
  8992. var height = upperRight.y - lowerLeft.y;
  8993. var elevation = upperRight.z - lowerLeft.z;
  8994. result.radius = Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
  8995. var center = result.center;
  8996. center.x = lowerLeft.x + width * 0.5;
  8997. center.y = lowerLeft.y + height * 0.5;
  8998. center.z = lowerLeft.z + elevation * 0.5;
  8999. return result;
  9000. };
  9001. var fromRectangle3DScratch = [];
  9002. /**
  9003. * Computes a bounding sphere from an rectangle in 3D. The bounding sphere is created using a subsample of points
  9004. * on the ellipsoid and contained in the rectangle. It may not be accurate for all rectangles on all types of ellipsoids.
  9005. *
  9006. * @param {Rectangle} rectangle The valid rectangle used to create a bounding sphere.
  9007. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine positions of the rectangle.
  9008. * @param {Number} [surfaceHeight=0.0] The height above the surface of the ellipsoid.
  9009. * @param {BoundingSphere} [result] The object onto which to store the result.
  9010. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9011. */
  9012. BoundingSphere.fromRectangle3D = function(rectangle, ellipsoid, surfaceHeight, result) {
  9013. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  9014. surfaceHeight = defaultValue(surfaceHeight, 0.0);
  9015. var positions;
  9016. if (defined(rectangle)) {
  9017. positions = Rectangle.subsample(rectangle, ellipsoid, surfaceHeight, fromRectangle3DScratch);
  9018. }
  9019. return BoundingSphere.fromPoints(positions, result);
  9020. };
  9021. /**
  9022. * Computes a tight-fitting bounding sphere enclosing a list of 3D points, where the points are
  9023. * stored in a flat array in X, Y, Z, order. The bounding sphere is computed by running two
  9024. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  9025. * ensure a tight fit.
  9026. *
  9027. * @param {Number[]} positions An array of points that the bounding sphere will enclose. Each point
  9028. * is formed from three elements in the array in the order X, Y, Z.
  9029. * @param {Cartesian3} [center=Cartesian3.ZERO] The position to which the positions are relative, which need not be the
  9030. * origin of the coordinate system. This is useful when the positions are to be used for
  9031. * relative-to-center (RTC) rendering.
  9032. * @param {Number} [stride=3] The number of array elements per vertex. It must be at least 3, but it may
  9033. * be higher. Regardless of the value of this parameter, the X coordinate of the first position
  9034. * is at array index 0, the Y coordinate is at array index 1, and the Z coordinate is at array index
  9035. * 2. When stride is 3, the X coordinate of the next position then begins at array index 3. If
  9036. * the stride is 5, however, two array elements are skipped and the next position begins at array
  9037. * index 5.
  9038. * @param {BoundingSphere} [result] The object onto which to store the result.
  9039. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  9040. *
  9041. * @example
  9042. * // Compute the bounding sphere from 3 positions, each specified relative to a center.
  9043. * // In addition to the X, Y, and Z coordinates, the points array contains two additional
  9044. * // elements per point which are ignored for the purpose of computing the bounding sphere.
  9045. * var center = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  9046. * var points = [1.0, 2.0, 3.0, 0.1, 0.2,
  9047. * 4.0, 5.0, 6.0, 0.1, 0.2,
  9048. * 7.0, 8.0, 9.0, 0.1, 0.2];
  9049. * var sphere = Cesium.BoundingSphere.fromVertices(points, center, 5);
  9050. *
  9051. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  9052. */
  9053. BoundingSphere.fromVertices = function(positions, center, stride, result) {
  9054. if (!defined(result)) {
  9055. result = new BoundingSphere();
  9056. }
  9057. if (!defined(positions) || positions.length === 0) {
  9058. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9059. result.radius = 0.0;
  9060. return result;
  9061. }
  9062. center = defaultValue(center, Cartesian3.ZERO);
  9063. stride = defaultValue(stride, 3);
  9064. if (stride < 3) {
  9065. throw new DeveloperError('stride must be 3 or greater.');
  9066. }
  9067. var currentPos = fromPointsCurrentPos;
  9068. currentPos.x = positions[0] + center.x;
  9069. currentPos.y = positions[1] + center.y;
  9070. currentPos.z = positions[2] + center.z;
  9071. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  9072. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  9073. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  9074. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  9075. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  9076. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  9077. var numElements = positions.length;
  9078. for (var i = 0; i < numElements; i += stride) {
  9079. var x = positions[i] + center.x;
  9080. var y = positions[i + 1] + center.y;
  9081. var z = positions[i + 2] + center.z;
  9082. currentPos.x = x;
  9083. currentPos.y = y;
  9084. currentPos.z = z;
  9085. // Store points containing the the smallest and largest components
  9086. if (x < xMin.x) {
  9087. Cartesian3.clone(currentPos, xMin);
  9088. }
  9089. if (x > xMax.x) {
  9090. Cartesian3.clone(currentPos, xMax);
  9091. }
  9092. if (y < yMin.y) {
  9093. Cartesian3.clone(currentPos, yMin);
  9094. }
  9095. if (y > yMax.y) {
  9096. Cartesian3.clone(currentPos, yMax);
  9097. }
  9098. if (z < zMin.z) {
  9099. Cartesian3.clone(currentPos, zMin);
  9100. }
  9101. if (z > zMax.z) {
  9102. Cartesian3.clone(currentPos, zMax);
  9103. }
  9104. }
  9105. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  9106. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  9107. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  9108. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  9109. // Set the diameter endpoints to the largest span.
  9110. var diameter1 = xMin;
  9111. var diameter2 = xMax;
  9112. var maxSpan = xSpan;
  9113. if (ySpan > maxSpan) {
  9114. maxSpan = ySpan;
  9115. diameter1 = yMin;
  9116. diameter2 = yMax;
  9117. }
  9118. if (zSpan > maxSpan) {
  9119. maxSpan = zSpan;
  9120. diameter1 = zMin;
  9121. diameter2 = zMax;
  9122. }
  9123. // Calculate the center of the initial sphere found by Ritter's algorithm
  9124. var ritterCenter = fromPointsRitterCenter;
  9125. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  9126. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  9127. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  9128. // Calculate the radius of the initial sphere found by Ritter's algorithm
  9129. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  9130. var ritterRadius = Math.sqrt(radiusSquared);
  9131. // Find the center of the sphere found using the Naive method.
  9132. var minBoxPt = fromPointsMinBoxPt;
  9133. minBoxPt.x = xMin.x;
  9134. minBoxPt.y = yMin.y;
  9135. minBoxPt.z = zMin.z;
  9136. var maxBoxPt = fromPointsMaxBoxPt;
  9137. maxBoxPt.x = xMax.x;
  9138. maxBoxPt.y = yMax.y;
  9139. maxBoxPt.z = zMax.z;
  9140. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  9141. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  9142. var naiveRadius = 0;
  9143. for (i = 0; i < numElements; i += stride) {
  9144. currentPos.x = positions[i] + center.x;
  9145. currentPos.y = positions[i + 1] + center.y;
  9146. currentPos.z = positions[i + 2] + center.z;
  9147. // Find the furthest point from the naive center to calculate the naive radius.
  9148. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  9149. if (r > naiveRadius) {
  9150. naiveRadius = r;
  9151. }
  9152. // Make adjustments to the Ritter Sphere to include all points.
  9153. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  9154. if (oldCenterToPointSquared > radiusSquared) {
  9155. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  9156. // Calculate new radius to include the point that lies outside
  9157. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  9158. radiusSquared = ritterRadius * ritterRadius;
  9159. // Calculate center of new Ritter sphere
  9160. var oldToNew = oldCenterToPoint - ritterRadius;
  9161. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  9162. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  9163. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  9164. }
  9165. }
  9166. if (ritterRadius < naiveRadius) {
  9167. Cartesian3.clone(ritterCenter, result.center);
  9168. result.radius = ritterRadius;
  9169. } else {
  9170. Cartesian3.clone(naiveCenter, result.center);
  9171. result.radius = naiveRadius;
  9172. }
  9173. return result;
  9174. };
  9175. /**
  9176. * Computes a tight-fitting bounding sphere enclosing a list of {@link EncodedCartesian3}s, where the points are
  9177. * stored in parallel flat arrays in X, Y, Z, order. The bounding sphere is computed by running two
  9178. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  9179. * ensure a tight fit.
  9180. *
  9181. * @param {Number[]} positionsHigh An array of high bits of the encoded cartesians that the bounding sphere will enclose. Each point
  9182. * is formed from three elements in the array in the order X, Y, Z.
  9183. * @param {Number[]} positionsLow An array of low bits of the encoded cartesians that the bounding sphere will enclose. Each point
  9184. * is formed from three elements in the array in the order X, Y, Z.
  9185. * @param {BoundingSphere} [result] The object onto which to store the result.
  9186. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  9187. *
  9188. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  9189. */
  9190. BoundingSphere.fromEncodedCartesianVertices = function(positionsHigh, positionsLow, result) {
  9191. if (!defined(result)) {
  9192. result = new BoundingSphere();
  9193. }
  9194. if (!defined(positionsHigh) || !defined(positionsLow) || positionsHigh.length !== positionsLow.length || positionsHigh.length === 0) {
  9195. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9196. result.radius = 0.0;
  9197. return result;
  9198. }
  9199. var currentPos = fromPointsCurrentPos;
  9200. currentPos.x = positionsHigh[0] + positionsLow[0];
  9201. currentPos.y = positionsHigh[1] + positionsLow[1];
  9202. currentPos.z = positionsHigh[2] + positionsLow[2];
  9203. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  9204. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  9205. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  9206. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  9207. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  9208. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  9209. var numElements = positionsHigh.length;
  9210. for (var i = 0; i < numElements; i += 3) {
  9211. var x = positionsHigh[i] + positionsLow[i];
  9212. var y = positionsHigh[i + 1] + positionsLow[i + 1];
  9213. var z = positionsHigh[i + 2] + positionsLow[i + 2];
  9214. currentPos.x = x;
  9215. currentPos.y = y;
  9216. currentPos.z = z;
  9217. // Store points containing the the smallest and largest components
  9218. if (x < xMin.x) {
  9219. Cartesian3.clone(currentPos, xMin);
  9220. }
  9221. if (x > xMax.x) {
  9222. Cartesian3.clone(currentPos, xMax);
  9223. }
  9224. if (y < yMin.y) {
  9225. Cartesian3.clone(currentPos, yMin);
  9226. }
  9227. if (y > yMax.y) {
  9228. Cartesian3.clone(currentPos, yMax);
  9229. }
  9230. if (z < zMin.z) {
  9231. Cartesian3.clone(currentPos, zMin);
  9232. }
  9233. if (z > zMax.z) {
  9234. Cartesian3.clone(currentPos, zMax);
  9235. }
  9236. }
  9237. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  9238. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  9239. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  9240. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  9241. // Set the diameter endpoints to the largest span.
  9242. var diameter1 = xMin;
  9243. var diameter2 = xMax;
  9244. var maxSpan = xSpan;
  9245. if (ySpan > maxSpan) {
  9246. maxSpan = ySpan;
  9247. diameter1 = yMin;
  9248. diameter2 = yMax;
  9249. }
  9250. if (zSpan > maxSpan) {
  9251. maxSpan = zSpan;
  9252. diameter1 = zMin;
  9253. diameter2 = zMax;
  9254. }
  9255. // Calculate the center of the initial sphere found by Ritter's algorithm
  9256. var ritterCenter = fromPointsRitterCenter;
  9257. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  9258. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  9259. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  9260. // Calculate the radius of the initial sphere found by Ritter's algorithm
  9261. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  9262. var ritterRadius = Math.sqrt(radiusSquared);
  9263. // Find the center of the sphere found using the Naive method.
  9264. var minBoxPt = fromPointsMinBoxPt;
  9265. minBoxPt.x = xMin.x;
  9266. minBoxPt.y = yMin.y;
  9267. minBoxPt.z = zMin.z;
  9268. var maxBoxPt = fromPointsMaxBoxPt;
  9269. maxBoxPt.x = xMax.x;
  9270. maxBoxPt.y = yMax.y;
  9271. maxBoxPt.z = zMax.z;
  9272. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  9273. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  9274. var naiveRadius = 0;
  9275. for (i = 0; i < numElements; i += 3) {
  9276. currentPos.x = positionsHigh[i] + positionsLow[i];
  9277. currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
  9278. currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
  9279. // Find the furthest point from the naive center to calculate the naive radius.
  9280. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  9281. if (r > naiveRadius) {
  9282. naiveRadius = r;
  9283. }
  9284. // Make adjustments to the Ritter Sphere to include all points.
  9285. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  9286. if (oldCenterToPointSquared > radiusSquared) {
  9287. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  9288. // Calculate new radius to include the point that lies outside
  9289. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  9290. radiusSquared = ritterRadius * ritterRadius;
  9291. // Calculate center of new Ritter sphere
  9292. var oldToNew = oldCenterToPoint - ritterRadius;
  9293. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  9294. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  9295. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  9296. }
  9297. }
  9298. if (ritterRadius < naiveRadius) {
  9299. Cartesian3.clone(ritterCenter, result.center);
  9300. result.radius = ritterRadius;
  9301. } else {
  9302. Cartesian3.clone(naiveCenter, result.center);
  9303. result.radius = naiveRadius;
  9304. }
  9305. return result;
  9306. };
  9307. /**
  9308. * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
  9309. * tighly and fully encompases the box.
  9310. *
  9311. * @param {Cartesian3} [corner] The minimum height over the rectangle.
  9312. * @param {Cartesian3} [oppositeCorner] The maximum height over the rectangle.
  9313. * @param {BoundingSphere} [result] The object onto which to store the result.
  9314. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9315. *
  9316. * @example
  9317. * // Create a bounding sphere around the unit cube
  9318. * var sphere = Cesium.BoundingSphere.fromCornerPoints(new Cesium.Cartesian3(-0.5, -0.5, -0.5), new Cesium.Cartesian3(0.5, 0.5, 0.5));
  9319. */
  9320. BoundingSphere.fromCornerPoints = function(corner, oppositeCorner, result) {
  9321. if (!defined(corner) || !defined(oppositeCorner)) {
  9322. throw new DeveloperError('corner and oppositeCorner are required.');
  9323. }
  9324. if (!defined(result)) {
  9325. result = new BoundingSphere();
  9326. }
  9327. var center = result.center;
  9328. Cartesian3.add(corner, oppositeCorner, center);
  9329. Cartesian3.multiplyByScalar(center, 0.5, center);
  9330. result.radius = Cartesian3.distance(center, oppositeCorner);
  9331. return result;
  9332. };
  9333. /**
  9334. * Creates a bounding sphere encompassing an ellipsoid.
  9335. *
  9336. * @param {Ellipsoid} ellipsoid The ellipsoid around which to create a bounding sphere.
  9337. * @param {BoundingSphere} [result] The object onto which to store the result.
  9338. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9339. *
  9340. * @example
  9341. * var boundingSphere = Cesium.BoundingSphere.fromEllipsoid(ellipsoid);
  9342. */
  9343. BoundingSphere.fromEllipsoid = function(ellipsoid, result) {
  9344. if (!defined(ellipsoid)) {
  9345. throw new DeveloperError('ellipsoid is required.');
  9346. }
  9347. if (!defined(result)) {
  9348. result = new BoundingSphere();
  9349. }
  9350. Cartesian3.clone(Cartesian3.ZERO, result.center);
  9351. result.radius = ellipsoid.maximumRadius;
  9352. return result;
  9353. };
  9354. var fromBoundingSpheresScratch = new Cartesian3();
  9355. /**
  9356. * Computes a tight-fitting bounding sphere enclosing the provided array of bounding spheres.
  9357. *
  9358. * @param {BoundingSphere[]} boundingSpheres The array of bounding spheres.
  9359. * @param {BoundingSphere} [result] The object onto which to store the result.
  9360. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9361. */
  9362. BoundingSphere.fromBoundingSpheres = function(boundingSpheres, result) {
  9363. if (!defined(result)) {
  9364. result = new BoundingSphere();
  9365. }
  9366. if (!defined(boundingSpheres) || boundingSpheres.length === 0) {
  9367. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9368. result.radius = 0.0;
  9369. return result;
  9370. }
  9371. var length = boundingSpheres.length;
  9372. if (length === 1) {
  9373. return BoundingSphere.clone(boundingSpheres[0], result);
  9374. }
  9375. if (length === 2) {
  9376. return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
  9377. }
  9378. var positions = [];
  9379. for (var i = 0; i < length; i++) {
  9380. positions.push(boundingSpheres[i].center);
  9381. }
  9382. result = BoundingSphere.fromPoints(positions, result);
  9383. var center = result.center;
  9384. var radius = result.radius;
  9385. for (i = 0; i < length; i++) {
  9386. var tmp = boundingSpheres[i];
  9387. radius = Math.max(radius, Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) + tmp.radius);
  9388. }
  9389. result.radius = radius;
  9390. return result;
  9391. };
  9392. var fromOrientedBoundingBoxScratchU = new Cartesian3();
  9393. var fromOrientedBoundingBoxScratchV = new Cartesian3();
  9394. var fromOrientedBoundingBoxScratchW = new Cartesian3();
  9395. /**
  9396. * Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
  9397. *
  9398. * @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
  9399. * @param {BoundingSphere} [result] The object onto which to store the result.
  9400. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9401. */
  9402. BoundingSphere.fromOrientedBoundingBox = function(orientedBoundingBox, result) {
  9403. if (!defined(result)) {
  9404. result = new BoundingSphere();
  9405. }
  9406. var halfAxes = orientedBoundingBox.halfAxes;
  9407. var u = Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
  9408. var v = Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
  9409. var w = Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
  9410. var uHalf = Cartesian3.magnitude(u);
  9411. var vHalf = Cartesian3.magnitude(v);
  9412. var wHalf = Cartesian3.magnitude(w);
  9413. result.center = Cartesian3.clone(orientedBoundingBox.center, result.center);
  9414. result.radius = Math.max(uHalf, vHalf, wHalf);
  9415. return result;
  9416. };
  9417. /**
  9418. * Duplicates a BoundingSphere instance.
  9419. *
  9420. * @param {BoundingSphere} sphere The bounding sphere to duplicate.
  9421. * @param {BoundingSphere} [result] The object onto which to store the result.
  9422. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided. (Returns undefined if sphere is undefined)
  9423. */
  9424. BoundingSphere.clone = function(sphere, result) {
  9425. if (!defined(sphere)) {
  9426. return undefined;
  9427. }
  9428. if (!defined(result)) {
  9429. return new BoundingSphere(sphere.center, sphere.radius);
  9430. }
  9431. result.center = Cartesian3.clone(sphere.center, result.center);
  9432. result.radius = sphere.radius;
  9433. return result;
  9434. };
  9435. /**
  9436. * The number of elements used to pack the object into an array.
  9437. * @type {Number}
  9438. */
  9439. BoundingSphere.packedLength = 4;
  9440. /**
  9441. * Stores the provided instance into the provided array.
  9442. *
  9443. * @param {BoundingSphere} value The value to pack.
  9444. * @param {Number[]} array The array to pack into.
  9445. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  9446. *
  9447. * @returns {Number[]} The array that was packed into
  9448. */
  9449. BoundingSphere.pack = function(value, array, startingIndex) {
  9450. if (!defined(value)) {
  9451. throw new DeveloperError('value is required');
  9452. }
  9453. if (!defined(array)) {
  9454. throw new DeveloperError('array is required');
  9455. }
  9456. startingIndex = defaultValue(startingIndex, 0);
  9457. var center = value.center;
  9458. array[startingIndex++] = center.x;
  9459. array[startingIndex++] = center.y;
  9460. array[startingIndex++] = center.z;
  9461. array[startingIndex] = value.radius;
  9462. return array;
  9463. };
  9464. /**
  9465. * Retrieves an instance from a packed array.
  9466. *
  9467. * @param {Number[]} array The packed array.
  9468. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  9469. * @param {BoundingSphere} [result] The object into which to store the result.
  9470. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  9471. */
  9472. BoundingSphere.unpack = function(array, startingIndex, result) {
  9473. if (!defined(array)) {
  9474. throw new DeveloperError('array is required');
  9475. }
  9476. startingIndex = defaultValue(startingIndex, 0);
  9477. if (!defined(result)) {
  9478. result = new BoundingSphere();
  9479. }
  9480. var center = result.center;
  9481. center.x = array[startingIndex++];
  9482. center.y = array[startingIndex++];
  9483. center.z = array[startingIndex++];
  9484. result.radius = array[startingIndex];
  9485. return result;
  9486. };
  9487. var unionScratch = new Cartesian3();
  9488. var unionScratchCenter = new Cartesian3();
  9489. /**
  9490. * Computes a bounding sphere that contains both the left and right bounding spheres.
  9491. *
  9492. * @param {BoundingSphere} left A sphere to enclose in a bounding sphere.
  9493. * @param {BoundingSphere} right A sphere to enclose in a bounding sphere.
  9494. * @param {BoundingSphere} [result] The object onto which to store the result.
  9495. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9496. */
  9497. BoundingSphere.union = function(left, right, result) {
  9498. if (!defined(left)) {
  9499. throw new DeveloperError('left is required.');
  9500. }
  9501. if (!defined(right)) {
  9502. throw new DeveloperError('right is required.');
  9503. }
  9504. if (!defined(result)) {
  9505. result = new BoundingSphere();
  9506. }
  9507. var leftCenter = left.center;
  9508. var leftRadius = left.radius;
  9509. var rightCenter = right.center;
  9510. var rightRadius = right.radius;
  9511. var toRightCenter = Cartesian3.subtract(rightCenter, leftCenter, unionScratch);
  9512. var centerSeparation = Cartesian3.magnitude(toRightCenter);
  9513. if (leftRadius >= (centerSeparation + rightRadius)) {
  9514. // Left sphere wins.
  9515. left.clone(result);
  9516. return result;
  9517. }
  9518. if (rightRadius >= (centerSeparation + leftRadius)) {
  9519. // Right sphere wins.
  9520. right.clone(result);
  9521. return result;
  9522. }
  9523. // There are two tangent points, one on far side of each sphere.
  9524. var halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5;
  9525. // Compute the center point halfway between the two tangent points.
  9526. var center = Cartesian3.multiplyByScalar(toRightCenter,
  9527. (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation, unionScratchCenter);
  9528. Cartesian3.add(center, leftCenter, center);
  9529. Cartesian3.clone(center, result.center);
  9530. result.radius = halfDistanceBetweenTangentPoints;
  9531. return result;
  9532. };
  9533. var expandScratch = new Cartesian3();
  9534. /**
  9535. * Computes a bounding sphere by enlarging the provided sphere to contain the provided point.
  9536. *
  9537. * @param {BoundingSphere} sphere A sphere to expand.
  9538. * @param {Cartesian3} point A point to enclose in a bounding sphere.
  9539. * @param {BoundingSphere} [result] The object onto which to store the result.
  9540. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9541. */
  9542. BoundingSphere.expand = function(sphere, point, result) {
  9543. if (!defined(sphere)) {
  9544. throw new DeveloperError('sphere is required.');
  9545. }
  9546. if (!defined(point)) {
  9547. throw new DeveloperError('point is required.');
  9548. }
  9549. result = BoundingSphere.clone(sphere, result);
  9550. var radius = Cartesian3.magnitude(Cartesian3.subtract(point, result.center, expandScratch));
  9551. if (radius > result.radius) {
  9552. result.radius = radius;
  9553. }
  9554. return result;
  9555. };
  9556. /**
  9557. * Determines which side of a plane a sphere is located.
  9558. *
  9559. * @param {BoundingSphere} sphere The bounding sphere to test.
  9560. * @param {Plane} plane The plane to test against.
  9561. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  9562. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  9563. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  9564. * intersects the plane.
  9565. */
  9566. BoundingSphere.intersectPlane = function(sphere, plane) {
  9567. if (!defined(sphere)) {
  9568. throw new DeveloperError('sphere is required.');
  9569. }
  9570. if (!defined(plane)) {
  9571. throw new DeveloperError('plane is required.');
  9572. }
  9573. var center = sphere.center;
  9574. var radius = sphere.radius;
  9575. var normal = plane.normal;
  9576. var distanceToPlane = Cartesian3.dot(normal, center) + plane.distance;
  9577. if (distanceToPlane < -radius) {
  9578. // The center point is negative side of the plane normal
  9579. return Intersect.OUTSIDE;
  9580. } else if (distanceToPlane < radius) {
  9581. // The center point is positive side of the plane, but radius extends beyond it; partial overlap
  9582. return Intersect.INTERSECTING;
  9583. }
  9584. return Intersect.INSIDE;
  9585. };
  9586. /**
  9587. * Applies a 4x4 affine transformation matrix to a bounding sphere.
  9588. *
  9589. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  9590. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  9591. * @param {BoundingSphere} [result] The object onto which to store the result.
  9592. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9593. */
  9594. BoundingSphere.transform = function(sphere, transform, result) {
  9595. if (!defined(sphere)) {
  9596. throw new DeveloperError('sphere is required.');
  9597. }
  9598. if (!defined(transform)) {
  9599. throw new DeveloperError('transform is required.');
  9600. }
  9601. if (!defined(result)) {
  9602. result = new BoundingSphere();
  9603. }
  9604. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  9605. result.radius = Matrix4.getMaximumScale(transform) * sphere.radius;
  9606. return result;
  9607. };
  9608. var distanceSquaredToScratch = new Cartesian3();
  9609. /**
  9610. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  9611. *
  9612. * @param {BoundingSphere} sphere The sphere.
  9613. * @param {Cartesian3} cartesian The point
  9614. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  9615. *
  9616. * @example
  9617. * // Sort bounding spheres from back to front
  9618. * spheres.sort(function(a, b) {
  9619. * return Cesium.BoundingSphere.distanceSquaredTo(b, camera.positionWC) - Cesium.BoundingSphere.distanceSquaredTo(a, camera.positionWC);
  9620. * });
  9621. */
  9622. BoundingSphere.distanceSquaredTo = function(sphere, cartesian) {
  9623. if (!defined(sphere)) {
  9624. throw new DeveloperError('sphere is required.');
  9625. }
  9626. if (!defined(cartesian)) {
  9627. throw new DeveloperError('cartesian is required.');
  9628. }
  9629. var diff = Cartesian3.subtract(sphere.center, cartesian, distanceSquaredToScratch);
  9630. return Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius;
  9631. };
  9632. /**
  9633. * Applies a 4x4 affine transformation matrix to a bounding sphere where there is no scale
  9634. * The transformation matrix is not verified to have a uniform scale of 1.
  9635. * This method is faster than computing the general bounding sphere transform using {@link BoundingSphere.transform}.
  9636. *
  9637. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  9638. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  9639. * @param {BoundingSphere} [result] The object onto which to store the result.
  9640. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9641. *
  9642. * @example
  9643. * var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid);
  9644. * var boundingSphere = new Cesium.BoundingSphere();
  9645. * var newBoundingSphere = Cesium.BoundingSphere.transformWithoutScale(boundingSphere, modelMatrix);
  9646. */
  9647. BoundingSphere.transformWithoutScale = function(sphere, transform, result) {
  9648. if (!defined(sphere)) {
  9649. throw new DeveloperError('sphere is required.');
  9650. }
  9651. if (!defined(transform)) {
  9652. throw new DeveloperError('transform is required.');
  9653. }
  9654. if (!defined(result)) {
  9655. result = new BoundingSphere();
  9656. }
  9657. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  9658. result.radius = sphere.radius;
  9659. return result;
  9660. };
  9661. var scratchCartesian3 = new Cartesian3();
  9662. /**
  9663. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  9664. * plus/minus the radius of the bounding sphere.
  9665. * <br>
  9666. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  9667. * closest and farthest planes from position that intersect the bounding sphere.
  9668. *
  9669. * @param {BoundingSphere} sphere The bounding sphere to calculate the distance to.
  9670. * @param {Cartesian3} position The position to calculate the distance from.
  9671. * @param {Cartesian3} direction The direction from position.
  9672. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  9673. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  9674. */
  9675. BoundingSphere.computePlaneDistances = function(sphere, position, direction, result) {
  9676. if (!defined(sphere)) {
  9677. throw new DeveloperError('sphere is required.');
  9678. }
  9679. if (!defined(position)) {
  9680. throw new DeveloperError('position is required.');
  9681. }
  9682. if (!defined(direction)) {
  9683. throw new DeveloperError('direction is required.');
  9684. }
  9685. if (!defined(result)) {
  9686. result = new Interval();
  9687. }
  9688. var toCenter = Cartesian3.subtract(sphere.center, position, scratchCartesian3);
  9689. var mag = Cartesian3.dot(direction, toCenter);
  9690. result.start = mag - sphere.radius;
  9691. result.stop = mag + sphere.radius;
  9692. return result;
  9693. };
  9694. var projectTo2DNormalScratch = new Cartesian3();
  9695. var projectTo2DEastScratch = new Cartesian3();
  9696. var projectTo2DNorthScratch = new Cartesian3();
  9697. var projectTo2DWestScratch = new Cartesian3();
  9698. var projectTo2DSouthScratch = new Cartesian3();
  9699. var projectTo2DCartographicScratch = new Cartographic();
  9700. var projectTo2DPositionsScratch = new Array(8);
  9701. for (var n = 0; n < 8; ++n) {
  9702. projectTo2DPositionsScratch[n] = new Cartesian3();
  9703. }
  9704. var projectTo2DProjection = new GeographicProjection();
  9705. /**
  9706. * Creates a bounding sphere in 2D from a bounding sphere in 3D world coordinates.
  9707. *
  9708. * @param {BoundingSphere} sphere The bounding sphere to transform to 2D.
  9709. * @param {Object} [projection=GeographicProjection] The projection to 2D.
  9710. * @param {BoundingSphere} [result] The object onto which to store the result.
  9711. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9712. */
  9713. BoundingSphere.projectTo2D = function(sphere, projection, result) {
  9714. if (!defined(sphere)) {
  9715. throw new DeveloperError('sphere is required.');
  9716. }
  9717. projection = defaultValue(projection, projectTo2DProjection);
  9718. var ellipsoid = projection.ellipsoid;
  9719. var center = sphere.center;
  9720. var radius = sphere.radius;
  9721. var normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
  9722. var east = Cartesian3.cross(Cartesian3.UNIT_Z, normal, projectTo2DEastScratch);
  9723. Cartesian3.normalize(east, east);
  9724. var north = Cartesian3.cross(normal, east, projectTo2DNorthScratch);
  9725. Cartesian3.normalize(north, north);
  9726. Cartesian3.multiplyByScalar(normal, radius, normal);
  9727. Cartesian3.multiplyByScalar(north, radius, north);
  9728. Cartesian3.multiplyByScalar(east, radius, east);
  9729. var south = Cartesian3.negate(north, projectTo2DSouthScratch);
  9730. var west = Cartesian3.negate(east, projectTo2DWestScratch);
  9731. var positions = projectTo2DPositionsScratch;
  9732. // top NE corner
  9733. var corner = positions[0];
  9734. Cartesian3.add(normal, north, corner);
  9735. Cartesian3.add(corner, east, corner);
  9736. // top NW corner
  9737. corner = positions[1];
  9738. Cartesian3.add(normal, north, corner);
  9739. Cartesian3.add(corner, west, corner);
  9740. // top SW corner
  9741. corner = positions[2];
  9742. Cartesian3.add(normal, south, corner);
  9743. Cartesian3.add(corner, west, corner);
  9744. // top SE corner
  9745. corner = positions[3];
  9746. Cartesian3.add(normal, south, corner);
  9747. Cartesian3.add(corner, east, corner);
  9748. Cartesian3.negate(normal, normal);
  9749. // bottom NE corner
  9750. corner = positions[4];
  9751. Cartesian3.add(normal, north, corner);
  9752. Cartesian3.add(corner, east, corner);
  9753. // bottom NW corner
  9754. corner = positions[5];
  9755. Cartesian3.add(normal, north, corner);
  9756. Cartesian3.add(corner, west, corner);
  9757. // bottom SW corner
  9758. corner = positions[6];
  9759. Cartesian3.add(normal, south, corner);
  9760. Cartesian3.add(corner, west, corner);
  9761. // bottom SE corner
  9762. corner = positions[7];
  9763. Cartesian3.add(normal, south, corner);
  9764. Cartesian3.add(corner, east, corner);
  9765. var length = positions.length;
  9766. for (var i = 0; i < length; ++i) {
  9767. var position = positions[i];
  9768. Cartesian3.add(center, position, position);
  9769. var cartographic = ellipsoid.cartesianToCartographic(position, projectTo2DCartographicScratch);
  9770. projection.project(cartographic, position);
  9771. }
  9772. result = BoundingSphere.fromPoints(positions, result);
  9773. // swizzle center components
  9774. center = result.center;
  9775. var x = center.x;
  9776. var y = center.y;
  9777. var z = center.z;
  9778. center.x = z;
  9779. center.y = x;
  9780. center.z = y;
  9781. return result;
  9782. };
  9783. /**
  9784. * Determines whether or not a sphere is hidden from view by the occluder.
  9785. *
  9786. * @param {BoundingSphere} sphere The bounding sphere surrounding the occludee object.
  9787. * @param {Occluder} occluder The occluder.
  9788. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  9789. */
  9790. BoundingSphere.isOccluded = function(sphere, occluder) {
  9791. if (!defined(sphere)) {
  9792. throw new DeveloperError('sphere is required.');
  9793. }
  9794. if (!defined(occluder)) {
  9795. throw new DeveloperError('occluder is required.');
  9796. }
  9797. return !occluder.isBoundingSphereVisible(sphere);
  9798. };
  9799. /**
  9800. * Compares the provided BoundingSphere componentwise and returns
  9801. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9802. *
  9803. * @param {BoundingSphere} [left] The first BoundingSphere.
  9804. * @param {BoundingSphere} [right] The second BoundingSphere.
  9805. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  9806. */
  9807. BoundingSphere.equals = function(left, right) {
  9808. return (left === right) ||
  9809. ((defined(left)) &&
  9810. (defined(right)) &&
  9811. Cartesian3.equals(left.center, right.center) &&
  9812. left.radius === right.radius);
  9813. };
  9814. /**
  9815. * Determines which side of a plane the sphere is located.
  9816. *
  9817. * @param {Plane} plane The plane to test against.
  9818. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  9819. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  9820. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  9821. * intersects the plane.
  9822. */
  9823. BoundingSphere.prototype.intersectPlane = function(plane) {
  9824. return BoundingSphere.intersectPlane(this, plane);
  9825. };
  9826. /**
  9827. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  9828. *
  9829. * @param {Cartesian3} cartesian The point
  9830. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  9831. *
  9832. * @example
  9833. * // Sort bounding spheres from back to front
  9834. * spheres.sort(function(a, b) {
  9835. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  9836. * });
  9837. */
  9838. BoundingSphere.prototype.distanceSquaredTo = function(cartesian) {
  9839. return BoundingSphere.distanceSquaredTo(this, cartesian);
  9840. };
  9841. /**
  9842. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  9843. * plus/minus the radius of the bounding sphere.
  9844. * <br>
  9845. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  9846. * closest and farthest planes from position that intersect the bounding sphere.
  9847. *
  9848. * @param {Cartesian3} position The position to calculate the distance from.
  9849. * @param {Cartesian3} direction The direction from position.
  9850. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  9851. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  9852. */
  9853. BoundingSphere.prototype.computePlaneDistances = function(position, direction, result) {
  9854. return BoundingSphere.computePlaneDistances(this, position, direction, result);
  9855. };
  9856. /**
  9857. * Determines whether or not a sphere is hidden from view by the occluder.
  9858. *
  9859. * @param {Occluder} occluder The occluder.
  9860. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  9861. */
  9862. BoundingSphere.prototype.isOccluded = function(occluder) {
  9863. return BoundingSphere.isOccluded(this, occluder);
  9864. };
  9865. /**
  9866. * Compares this BoundingSphere against the provided BoundingSphere componentwise and returns
  9867. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9868. *
  9869. * @param {BoundingSphere} [right] The right hand side BoundingSphere.
  9870. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  9871. */
  9872. BoundingSphere.prototype.equals = function(right) {
  9873. return BoundingSphere.equals(this, right);
  9874. };
  9875. /**
  9876. * Duplicates this BoundingSphere instance.
  9877. *
  9878. * @param {BoundingSphere} [result] The object onto which to store the result.
  9879. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9880. */
  9881. BoundingSphere.prototype.clone = function(result) {
  9882. return BoundingSphere.clone(this, result);
  9883. };
  9884. return BoundingSphere;
  9885. });
  9886. /*global define*/
  9887. define('Core/Cartesian2',[
  9888. './defaultValue',
  9889. './defined',
  9890. './DeveloperError',
  9891. './freezeObject',
  9892. './Math'
  9893. ], function(
  9894. defaultValue,
  9895. defined,
  9896. DeveloperError,
  9897. freezeObject,
  9898. CesiumMath) {
  9899. 'use strict';
  9900. /**
  9901. * A 2D Cartesian point.
  9902. * @alias Cartesian2
  9903. * @constructor
  9904. *
  9905. * @param {Number} [x=0.0] The X component.
  9906. * @param {Number} [y=0.0] The Y component.
  9907. *
  9908. * @see Cartesian3
  9909. * @see Cartesian4
  9910. * @see Packable
  9911. */
  9912. function Cartesian2(x, y) {
  9913. /**
  9914. * The X component.
  9915. * @type {Number}
  9916. * @default 0.0
  9917. */
  9918. this.x = defaultValue(x, 0.0);
  9919. /**
  9920. * The Y component.
  9921. * @type {Number}
  9922. * @default 0.0
  9923. */
  9924. this.y = defaultValue(y, 0.0);
  9925. }
  9926. /**
  9927. * Creates a Cartesian2 instance from x and y coordinates.
  9928. *
  9929. * @param {Number} x The x coordinate.
  9930. * @param {Number} y The y coordinate.
  9931. * @param {Cartesian2} [result] The object onto which to store the result.
  9932. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  9933. */
  9934. Cartesian2.fromElements = function(x, y, result) {
  9935. if (!defined(result)) {
  9936. return new Cartesian2(x, y);
  9937. }
  9938. result.x = x;
  9939. result.y = y;
  9940. return result;
  9941. };
  9942. /**
  9943. * Duplicates a Cartesian2 instance.
  9944. *
  9945. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  9946. * @param {Cartesian2} [result] The object onto which to store the result.
  9947. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  9948. */
  9949. Cartesian2.clone = function(cartesian, result) {
  9950. if (!defined(cartesian)) {
  9951. return undefined;
  9952. }
  9953. if (!defined(result)) {
  9954. return new Cartesian2(cartesian.x, cartesian.y);
  9955. }
  9956. result.x = cartesian.x;
  9957. result.y = cartesian.y;
  9958. return result;
  9959. };
  9960. /**
  9961. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  9962. * x and y properties of the Cartesian3 and drops z.
  9963. * @function
  9964. *
  9965. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  9966. * @param {Cartesian2} [result] The object onto which to store the result.
  9967. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  9968. */
  9969. Cartesian2.fromCartesian3 = Cartesian2.clone;
  9970. /**
  9971. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  9972. * x and y properties of the Cartesian4 and drops z and w.
  9973. * @function
  9974. *
  9975. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  9976. * @param {Cartesian2} [result] The object onto which to store the result.
  9977. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  9978. */
  9979. Cartesian2.fromCartesian4 = Cartesian2.clone;
  9980. /**
  9981. * The number of elements used to pack the object into an array.
  9982. * @type {Number}
  9983. */
  9984. Cartesian2.packedLength = 2;
  9985. /**
  9986. * Stores the provided instance into the provided array.
  9987. *
  9988. * @param {Cartesian2} value The value to pack.
  9989. * @param {Number[]} array The array to pack into.
  9990. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  9991. *
  9992. * @returns {Number[]} The array that was packed into
  9993. */
  9994. Cartesian2.pack = function(value, array, startingIndex) {
  9995. if (!defined(value)) {
  9996. throw new DeveloperError('value is required');
  9997. }
  9998. if (!defined(array)) {
  9999. throw new DeveloperError('array is required');
  10000. }
  10001. startingIndex = defaultValue(startingIndex, 0);
  10002. array[startingIndex++] = value.x;
  10003. array[startingIndex] = value.y;
  10004. return array;
  10005. };
  10006. /**
  10007. * Retrieves an instance from a packed array.
  10008. *
  10009. * @param {Number[]} array The packed array.
  10010. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  10011. * @param {Cartesian2} [result] The object into which to store the result.
  10012. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  10013. */
  10014. Cartesian2.unpack = function(array, startingIndex, result) {
  10015. if (!defined(array)) {
  10016. throw new DeveloperError('array is required');
  10017. }
  10018. startingIndex = defaultValue(startingIndex, 0);
  10019. if (!defined(result)) {
  10020. result = new Cartesian2();
  10021. }
  10022. result.x = array[startingIndex++];
  10023. result.y = array[startingIndex];
  10024. return result;
  10025. };
  10026. /**
  10027. * Flattens an array of Cartesian2s into and array of components.
  10028. *
  10029. * @param {Cartesian2[]} array The array of cartesians to pack.
  10030. * @param {Number[]} result The array onto which to store the result.
  10031. * @returns {Number[]} The packed array.
  10032. */
  10033. Cartesian2.packArray = function(array, result) {
  10034. if (!defined(array)) {
  10035. throw new DeveloperError('array is required');
  10036. }
  10037. var length = array.length;
  10038. if (!defined(result)) {
  10039. result = new Array(length * 2);
  10040. } else {
  10041. result.length = length * 2;
  10042. }
  10043. for (var i = 0; i < length; ++i) {
  10044. Cartesian2.pack(array[i], result, i * 2);
  10045. }
  10046. return result;
  10047. };
  10048. /**
  10049. * Unpacks an array of cartesian components into and array of Cartesian2s.
  10050. *
  10051. * @param {Number[]} array The array of components to unpack.
  10052. * @param {Cartesian2[]} result The array onto which to store the result.
  10053. * @returns {Cartesian2[]} The unpacked array.
  10054. */
  10055. Cartesian2.unpackArray = function(array, result) {
  10056. if (!defined(array)) {
  10057. throw new DeveloperError('array is required');
  10058. }
  10059. var length = array.length;
  10060. if (!defined(result)) {
  10061. result = new Array(length / 2);
  10062. } else {
  10063. result.length = length / 2;
  10064. }
  10065. for (var i = 0; i < length; i += 2) {
  10066. var index = i / 2;
  10067. result[index] = Cartesian2.unpack(array, i, result[index]);
  10068. }
  10069. return result;
  10070. };
  10071. /**
  10072. * Creates a Cartesian2 from two consecutive elements in an array.
  10073. * @function
  10074. *
  10075. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  10076. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  10077. * @param {Cartesian2} [result] The object onto which to store the result.
  10078. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  10079. *
  10080. * @example
  10081. * // Create a Cartesian2 with (1.0, 2.0)
  10082. * var v = [1.0, 2.0];
  10083. * var p = Cesium.Cartesian2.fromArray(v);
  10084. *
  10085. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  10086. * var v2 = [0.0, 0.0, 1.0, 2.0];
  10087. * var p2 = Cesium.Cartesian2.fromArray(v2, 2);
  10088. */
  10089. Cartesian2.fromArray = Cartesian2.unpack;
  10090. /**
  10091. * Computes the value of the maximum component for the supplied Cartesian.
  10092. *
  10093. * @param {Cartesian2} cartesian The cartesian to use.
  10094. * @returns {Number} The value of the maximum component.
  10095. */
  10096. Cartesian2.maximumComponent = function(cartesian) {
  10097. if (!defined(cartesian)) {
  10098. throw new DeveloperError('cartesian is required');
  10099. }
  10100. return Math.max(cartesian.x, cartesian.y);
  10101. };
  10102. /**
  10103. * Computes the value of the minimum component for the supplied Cartesian.
  10104. *
  10105. * @param {Cartesian2} cartesian The cartesian to use.
  10106. * @returns {Number} The value of the minimum component.
  10107. */
  10108. Cartesian2.minimumComponent = function(cartesian) {
  10109. if (!defined(cartesian)) {
  10110. throw new DeveloperError('cartesian is required');
  10111. }
  10112. return Math.min(cartesian.x, cartesian.y);
  10113. };
  10114. /**
  10115. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  10116. *
  10117. * @param {Cartesian2} first A cartesian to compare.
  10118. * @param {Cartesian2} second A cartesian to compare.
  10119. * @param {Cartesian2} result The object into which to store the result.
  10120. * @returns {Cartesian2} A cartesian with the minimum components.
  10121. */
  10122. Cartesian2.minimumByComponent = function(first, second, result) {
  10123. if (!defined(first)) {
  10124. throw new DeveloperError('first is required.');
  10125. }
  10126. if (!defined(second)) {
  10127. throw new DeveloperError('second is required.');
  10128. }
  10129. if (!defined(result)) {
  10130. throw new DeveloperError('result is required.');
  10131. }
  10132. result.x = Math.min(first.x, second.x);
  10133. result.y = Math.min(first.y, second.y);
  10134. return result;
  10135. };
  10136. /**
  10137. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  10138. *
  10139. * @param {Cartesian2} first A cartesian to compare.
  10140. * @param {Cartesian2} second A cartesian to compare.
  10141. * @param {Cartesian2} result The object into which to store the result.
  10142. * @returns {Cartesian2} A cartesian with the maximum components.
  10143. */
  10144. Cartesian2.maximumByComponent = function(first, second, result) {
  10145. if (!defined(first)) {
  10146. throw new DeveloperError('first is required.');
  10147. }
  10148. if (!defined(second)) {
  10149. throw new DeveloperError('second is required.');
  10150. }
  10151. if (!defined(result)) {
  10152. throw new DeveloperError('result is required.');
  10153. }
  10154. result.x = Math.max(first.x, second.x);
  10155. result.y = Math.max(first.y, second.y);
  10156. return result;
  10157. };
  10158. /**
  10159. * Computes the provided Cartesian's squared magnitude.
  10160. *
  10161. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  10162. * @returns {Number} The squared magnitude.
  10163. */
  10164. Cartesian2.magnitudeSquared = function(cartesian) {
  10165. if (!defined(cartesian)) {
  10166. throw new DeveloperError('cartesian is required');
  10167. }
  10168. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  10169. };
  10170. /**
  10171. * Computes the Cartesian's magnitude (length).
  10172. *
  10173. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  10174. * @returns {Number} The magnitude.
  10175. */
  10176. Cartesian2.magnitude = function(cartesian) {
  10177. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  10178. };
  10179. var distanceScratch = new Cartesian2();
  10180. /**
  10181. * Computes the distance between two points.
  10182. *
  10183. * @param {Cartesian2} left The first point to compute the distance from.
  10184. * @param {Cartesian2} right The second point to compute the distance to.
  10185. * @returns {Number} The distance between two points.
  10186. *
  10187. * @example
  10188. * // Returns 1.0
  10189. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  10190. */
  10191. Cartesian2.distance = function(left, right) {
  10192. if (!defined(left) || !defined(right)) {
  10193. throw new DeveloperError('left and right are required.');
  10194. }
  10195. Cartesian2.subtract(left, right, distanceScratch);
  10196. return Cartesian2.magnitude(distanceScratch);
  10197. };
  10198. /**
  10199. * Computes the squared distance between two points. Comparing squared distances
  10200. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  10201. *
  10202. * @param {Cartesian2} left The first point to compute the distance from.
  10203. * @param {Cartesian2} right The second point to compute the distance to.
  10204. * @returns {Number} The distance between two points.
  10205. *
  10206. * @example
  10207. * // Returns 4.0, not 2.0
  10208. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  10209. */
  10210. Cartesian2.distanceSquared = function(left, right) {
  10211. if (!defined(left) || !defined(right)) {
  10212. throw new DeveloperError('left and right are required.');
  10213. }
  10214. Cartesian2.subtract(left, right, distanceScratch);
  10215. return Cartesian2.magnitudeSquared(distanceScratch);
  10216. };
  10217. /**
  10218. * Computes the normalized form of the supplied Cartesian.
  10219. *
  10220. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  10221. * @param {Cartesian2} result The object onto which to store the result.
  10222. * @returns {Cartesian2} The modified result parameter.
  10223. */
  10224. Cartesian2.normalize = function(cartesian, result) {
  10225. if (!defined(cartesian)) {
  10226. throw new DeveloperError('cartesian is required');
  10227. }
  10228. if (!defined(result)) {
  10229. throw new DeveloperError('result is required');
  10230. }
  10231. var magnitude = Cartesian2.magnitude(cartesian);
  10232. result.x = cartesian.x / magnitude;
  10233. result.y = cartesian.y / magnitude;
  10234. if (isNaN(result.x) || isNaN(result.y)) {
  10235. throw new DeveloperError('normalized result is not a number');
  10236. }
  10237. return result;
  10238. };
  10239. /**
  10240. * Computes the dot (scalar) product of two Cartesians.
  10241. *
  10242. * @param {Cartesian2} left The first Cartesian.
  10243. * @param {Cartesian2} right The second Cartesian.
  10244. * @returns {Number} The dot product.
  10245. */
  10246. Cartesian2.dot = function(left, right) {
  10247. if (!defined(left)) {
  10248. throw new DeveloperError('left is required');
  10249. }
  10250. if (!defined(right)) {
  10251. throw new DeveloperError('right is required');
  10252. }
  10253. return left.x * right.x + left.y * right.y;
  10254. };
  10255. /**
  10256. * Computes the componentwise product of two Cartesians.
  10257. *
  10258. * @param {Cartesian2} left The first Cartesian.
  10259. * @param {Cartesian2} right The second Cartesian.
  10260. * @param {Cartesian2} result The object onto which to store the result.
  10261. * @returns {Cartesian2} The modified result parameter.
  10262. */
  10263. Cartesian2.multiplyComponents = function(left, right, result) {
  10264. if (!defined(left)) {
  10265. throw new DeveloperError('left is required');
  10266. }
  10267. if (!defined(right)) {
  10268. throw new DeveloperError('right is required');
  10269. }
  10270. if (!defined(result)) {
  10271. throw new DeveloperError('result is required');
  10272. }
  10273. result.x = left.x * right.x;
  10274. result.y = left.y * right.y;
  10275. return result;
  10276. };
  10277. /**
  10278. * Computes the componentwise quotient of two Cartesians.
  10279. *
  10280. * @param {Cartesian2} left The first Cartesian.
  10281. * @param {Cartesian2} right The second Cartesian.
  10282. * @param {Cartesian2} result The object onto which to store the result.
  10283. * @returns {Cartesian2} The modified result parameter.
  10284. */
  10285. Cartesian2.divideComponents = function(left, right, result) {
  10286. if (!defined(left)) {
  10287. throw new DeveloperError('left is required');
  10288. }
  10289. if (!defined(right)) {
  10290. throw new DeveloperError('right is required');
  10291. }
  10292. if (!defined(result)) {
  10293. throw new DeveloperError('result is required');
  10294. }
  10295. result.x = left.x / right.x;
  10296. result.y = left.y / right.y;
  10297. return result;
  10298. };
  10299. /**
  10300. * Computes the componentwise sum of two Cartesians.
  10301. *
  10302. * @param {Cartesian2} left The first Cartesian.
  10303. * @param {Cartesian2} right The second Cartesian.
  10304. * @param {Cartesian2} result The object onto which to store the result.
  10305. * @returns {Cartesian2} The modified result parameter.
  10306. */
  10307. Cartesian2.add = function(left, right, result) {
  10308. if (!defined(left)) {
  10309. throw new DeveloperError('left is required');
  10310. }
  10311. if (!defined(right)) {
  10312. throw new DeveloperError('right is required');
  10313. }
  10314. if (!defined(result)) {
  10315. throw new DeveloperError('result is required');
  10316. }
  10317. result.x = left.x + right.x;
  10318. result.y = left.y + right.y;
  10319. return result;
  10320. };
  10321. /**
  10322. * Computes the componentwise difference of two Cartesians.
  10323. *
  10324. * @param {Cartesian2} left The first Cartesian.
  10325. * @param {Cartesian2} right The second Cartesian.
  10326. * @param {Cartesian2} result The object onto which to store the result.
  10327. * @returns {Cartesian2} The modified result parameter.
  10328. */
  10329. Cartesian2.subtract = function(left, right, result) {
  10330. if (!defined(left)) {
  10331. throw new DeveloperError('left is required');
  10332. }
  10333. if (!defined(right)) {
  10334. throw new DeveloperError('right is required');
  10335. }
  10336. if (!defined(result)) {
  10337. throw new DeveloperError('result is required');
  10338. }
  10339. result.x = left.x - right.x;
  10340. result.y = left.y - right.y;
  10341. return result;
  10342. };
  10343. /**
  10344. * Multiplies the provided Cartesian componentwise by the provided scalar.
  10345. *
  10346. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  10347. * @param {Number} scalar The scalar to multiply with.
  10348. * @param {Cartesian2} result The object onto which to store the result.
  10349. * @returns {Cartesian2} The modified result parameter.
  10350. */
  10351. Cartesian2.multiplyByScalar = function(cartesian, scalar, result) {
  10352. if (!defined(cartesian)) {
  10353. throw new DeveloperError('cartesian is required');
  10354. }
  10355. if (typeof scalar !== 'number') {
  10356. throw new DeveloperError('scalar is required and must be a number.');
  10357. }
  10358. if (!defined(result)) {
  10359. throw new DeveloperError('result is required');
  10360. }
  10361. result.x = cartesian.x * scalar;
  10362. result.y = cartesian.y * scalar;
  10363. return result;
  10364. };
  10365. /**
  10366. * Divides the provided Cartesian componentwise by the provided scalar.
  10367. *
  10368. * @param {Cartesian2} cartesian The Cartesian to be divided.
  10369. * @param {Number} scalar The scalar to divide by.
  10370. * @param {Cartesian2} result The object onto which to store the result.
  10371. * @returns {Cartesian2} The modified result parameter.
  10372. */
  10373. Cartesian2.divideByScalar = function(cartesian, scalar, result) {
  10374. if (!defined(cartesian)) {
  10375. throw new DeveloperError('cartesian is required');
  10376. }
  10377. if (typeof scalar !== 'number') {
  10378. throw new DeveloperError('scalar is required and must be a number.');
  10379. }
  10380. if (!defined(result)) {
  10381. throw new DeveloperError('result is required');
  10382. }
  10383. result.x = cartesian.x / scalar;
  10384. result.y = cartesian.y / scalar;
  10385. return result;
  10386. };
  10387. /**
  10388. * Negates the provided Cartesian.
  10389. *
  10390. * @param {Cartesian2} cartesian The Cartesian to be negated.
  10391. * @param {Cartesian2} result The object onto which to store the result.
  10392. * @returns {Cartesian2} The modified result parameter.
  10393. */
  10394. Cartesian2.negate = function(cartesian, result) {
  10395. if (!defined(cartesian)) {
  10396. throw new DeveloperError('cartesian is required');
  10397. }
  10398. if (!defined(result)) {
  10399. throw new DeveloperError('result is required');
  10400. }
  10401. result.x = -cartesian.x;
  10402. result.y = -cartesian.y;
  10403. return result;
  10404. };
  10405. /**
  10406. * Computes the absolute value of the provided Cartesian.
  10407. *
  10408. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  10409. * @param {Cartesian2} result The object onto which to store the result.
  10410. * @returns {Cartesian2} The modified result parameter.
  10411. */
  10412. Cartesian2.abs = function(cartesian, result) {
  10413. if (!defined(cartesian)) {
  10414. throw new DeveloperError('cartesian is required');
  10415. }
  10416. if (!defined(result)) {
  10417. throw new DeveloperError('result is required');
  10418. }
  10419. result.x = Math.abs(cartesian.x);
  10420. result.y = Math.abs(cartesian.y);
  10421. return result;
  10422. };
  10423. var lerpScratch = new Cartesian2();
  10424. /**
  10425. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  10426. *
  10427. * @param {Cartesian2} start The value corresponding to t at 0.0.
  10428. * @param {Cartesian2} end The value corresponding to t at 1.0.
  10429. * @param {Number} t The point along t at which to interpolate.
  10430. * @param {Cartesian2} result The object onto which to store the result.
  10431. * @returns {Cartesian2} The modified result parameter.
  10432. */
  10433. Cartesian2.lerp = function(start, end, t, result) {
  10434. if (!defined(start)) {
  10435. throw new DeveloperError('start is required.');
  10436. }
  10437. if (!defined(end)) {
  10438. throw new DeveloperError('end is required.');
  10439. }
  10440. if (typeof t !== 'number') {
  10441. throw new DeveloperError('t is required and must be a number.');
  10442. }
  10443. if (!defined(result)) {
  10444. throw new DeveloperError('result is required.');
  10445. }
  10446. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  10447. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  10448. return Cartesian2.add(lerpScratch, result, result);
  10449. };
  10450. var angleBetweenScratch = new Cartesian2();
  10451. var angleBetweenScratch2 = new Cartesian2();
  10452. /**
  10453. * Returns the angle, in radians, between the provided Cartesians.
  10454. *
  10455. * @param {Cartesian2} left The first Cartesian.
  10456. * @param {Cartesian2} right The second Cartesian.
  10457. * @returns {Number} The angle between the Cartesians.
  10458. */
  10459. Cartesian2.angleBetween = function(left, right) {
  10460. if (!defined(left)) {
  10461. throw new DeveloperError('left is required');
  10462. }
  10463. if (!defined(right)) {
  10464. throw new DeveloperError('right is required');
  10465. }
  10466. Cartesian2.normalize(left, angleBetweenScratch);
  10467. Cartesian2.normalize(right, angleBetweenScratch2);
  10468. return CesiumMath.acosClamped(Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2));
  10469. };
  10470. var mostOrthogonalAxisScratch = new Cartesian2();
  10471. /**
  10472. * Returns the axis that is most orthogonal to the provided Cartesian.
  10473. *
  10474. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  10475. * @param {Cartesian2} result The object onto which to store the result.
  10476. * @returns {Cartesian2} The most orthogonal axis.
  10477. */
  10478. Cartesian2.mostOrthogonalAxis = function(cartesian, result) {
  10479. if (!defined(cartesian)) {
  10480. throw new DeveloperError('cartesian is required.');
  10481. }
  10482. if (!defined(result)) {
  10483. throw new DeveloperError('result is required.');
  10484. }
  10485. var f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  10486. Cartesian2.abs(f, f);
  10487. if (f.x <= f.y) {
  10488. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  10489. } else {
  10490. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  10491. }
  10492. return result;
  10493. };
  10494. /**
  10495. * Compares the provided Cartesians componentwise and returns
  10496. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10497. *
  10498. * @param {Cartesian2} [left] The first Cartesian.
  10499. * @param {Cartesian2} [right] The second Cartesian.
  10500. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  10501. */
  10502. Cartesian2.equals = function(left, right) {
  10503. return (left === right) ||
  10504. ((defined(left)) &&
  10505. (defined(right)) &&
  10506. (left.x === right.x) &&
  10507. (left.y === right.y));
  10508. };
  10509. /**
  10510. * @private
  10511. */
  10512. Cartesian2.equalsArray = function(cartesian, array, offset) {
  10513. return cartesian.x === array[offset] &&
  10514. cartesian.y === array[offset + 1];
  10515. };
  10516. /**
  10517. * Compares the provided Cartesians componentwise and returns
  10518. * <code>true</code> if they pass an absolute or relative tolerance test,
  10519. * <code>false</code> otherwise.
  10520. *
  10521. * @param {Cartesian2} [left] The first Cartesian.
  10522. * @param {Cartesian2} [right] The second Cartesian.
  10523. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  10524. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  10525. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  10526. */
  10527. Cartesian2.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  10528. return (left === right) ||
  10529. (defined(left) &&
  10530. defined(right) &&
  10531. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  10532. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon));
  10533. };
  10534. /**
  10535. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  10536. *
  10537. * @type {Cartesian2}
  10538. * @constant
  10539. */
  10540. Cartesian2.ZERO = freezeObject(new Cartesian2(0.0, 0.0));
  10541. /**
  10542. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  10543. *
  10544. * @type {Cartesian2}
  10545. * @constant
  10546. */
  10547. Cartesian2.UNIT_X = freezeObject(new Cartesian2(1.0, 0.0));
  10548. /**
  10549. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  10550. *
  10551. * @type {Cartesian2}
  10552. * @constant
  10553. */
  10554. Cartesian2.UNIT_Y = freezeObject(new Cartesian2(0.0, 1.0));
  10555. /**
  10556. * Duplicates this Cartesian2 instance.
  10557. *
  10558. * @param {Cartesian2} [result] The object onto which to store the result.
  10559. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  10560. */
  10561. Cartesian2.prototype.clone = function(result) {
  10562. return Cartesian2.clone(this, result);
  10563. };
  10564. /**
  10565. * Compares this Cartesian against the provided Cartesian componentwise and returns
  10566. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10567. *
  10568. * @param {Cartesian2} [right] The right hand side Cartesian.
  10569. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  10570. */
  10571. Cartesian2.prototype.equals = function(right) {
  10572. return Cartesian2.equals(this, right);
  10573. };
  10574. /**
  10575. * Compares this Cartesian against the provided Cartesian componentwise and returns
  10576. * <code>true</code> if they pass an absolute or relative tolerance test,
  10577. * <code>false</code> otherwise.
  10578. *
  10579. * @param {Cartesian2} [right] The right hand side Cartesian.
  10580. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  10581. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  10582. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  10583. */
  10584. Cartesian2.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  10585. return Cartesian2.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  10586. };
  10587. /**
  10588. * Creates a string representing this Cartesian in the format '(x, y)'.
  10589. *
  10590. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  10591. */
  10592. Cartesian2.prototype.toString = function() {
  10593. return '(' + this.x + ', ' + this.y + ')';
  10594. };
  10595. return Cartesian2;
  10596. });
  10597. /*global define*/
  10598. define('Core/Fullscreen',[
  10599. './defined',
  10600. './defineProperties'
  10601. ], function(
  10602. defined,
  10603. defineProperties) {
  10604. 'use strict';
  10605. var _supportsFullscreen;
  10606. var _names = {
  10607. requestFullscreen : undefined,
  10608. exitFullscreen : undefined,
  10609. fullscreenEnabled : undefined,
  10610. fullscreenElement : undefined,
  10611. fullscreenchange : undefined,
  10612. fullscreenerror : undefined
  10613. };
  10614. /**
  10615. * Browser-independent functions for working with the standard fullscreen API.
  10616. *
  10617. * @exports Fullscreen
  10618. *
  10619. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  10620. */
  10621. var Fullscreen = {};
  10622. defineProperties(Fullscreen, {
  10623. /**
  10624. * The element that is currently fullscreen, if any. To simply check if the
  10625. * browser is in fullscreen mode or not, use {@link Fullscreen#fullscreen}.
  10626. * @memberof Fullscreen
  10627. * @type {Object}
  10628. * @readonly
  10629. */
  10630. element : {
  10631. get : function() {
  10632. if (!Fullscreen.supportsFullscreen()) {
  10633. return undefined;
  10634. }
  10635. return document[_names.fullscreenElement];
  10636. }
  10637. },
  10638. /**
  10639. * The name of the event on the document that is fired when fullscreen is
  10640. * entered or exited. This event name is intended for use with addEventListener.
  10641. * In your event handler, to determine if the browser is in fullscreen mode or not,
  10642. * use {@link Fullscreen#fullscreen}.
  10643. * @memberof Fullscreen
  10644. * @type {String}
  10645. * @readonly
  10646. */
  10647. changeEventName : {
  10648. get : function() {
  10649. if (!Fullscreen.supportsFullscreen()) {
  10650. return undefined;
  10651. }
  10652. return _names.fullscreenchange;
  10653. }
  10654. },
  10655. /**
  10656. * The name of the event that is fired when a fullscreen error
  10657. * occurs. This event name is intended for use with addEventListener.
  10658. * @memberof Fullscreen
  10659. * @type {String}
  10660. * @readonly
  10661. */
  10662. errorEventName : {
  10663. get : function() {
  10664. if (!Fullscreen.supportsFullscreen()) {
  10665. return undefined;
  10666. }
  10667. return _names.fullscreenerror;
  10668. }
  10669. },
  10670. /**
  10671. * Determine whether the browser will allow an element to be made fullscreen, or not.
  10672. * For example, by default, iframes cannot go fullscreen unless the containing page
  10673. * adds an "allowfullscreen" attribute (or prefixed equivalent).
  10674. * @memberof Fullscreen
  10675. * @type {Boolean}
  10676. * @readonly
  10677. */
  10678. enabled : {
  10679. get : function() {
  10680. if (!Fullscreen.supportsFullscreen()) {
  10681. return undefined;
  10682. }
  10683. return document[_names.fullscreenEnabled];
  10684. }
  10685. },
  10686. /**
  10687. * Determines if the browser is currently in fullscreen mode.
  10688. * @memberof Fullscreen
  10689. * @type {Boolean}
  10690. * @readonly
  10691. */
  10692. fullscreen : {
  10693. get : function() {
  10694. if (!Fullscreen.supportsFullscreen()) {
  10695. return undefined;
  10696. }
  10697. return Fullscreen.element !== null;
  10698. }
  10699. }
  10700. });
  10701. /**
  10702. * Detects whether the browser supports the standard fullscreen API.
  10703. *
  10704. * @returns {Boolean} <code>true</code> if the browser supports the standard fullscreen API,
  10705. * <code>false</code> otherwise.
  10706. */
  10707. Fullscreen.supportsFullscreen = function() {
  10708. if (defined(_supportsFullscreen)) {
  10709. return _supportsFullscreen;
  10710. }
  10711. _supportsFullscreen = false;
  10712. var body = document.body;
  10713. if (typeof body.requestFullscreen === 'function') {
  10714. // go with the unprefixed, standard set of names
  10715. _names.requestFullscreen = 'requestFullscreen';
  10716. _names.exitFullscreen = 'exitFullscreen';
  10717. _names.fullscreenEnabled = 'fullscreenEnabled';
  10718. _names.fullscreenElement = 'fullscreenElement';
  10719. _names.fullscreenchange = 'fullscreenchange';
  10720. _names.fullscreenerror = 'fullscreenerror';
  10721. _supportsFullscreen = true;
  10722. return _supportsFullscreen;
  10723. }
  10724. //check for the correct combination of prefix plus the various names that browsers use
  10725. var prefixes = ['webkit', 'moz', 'o', 'ms', 'khtml'];
  10726. var name;
  10727. for (var i = 0, len = prefixes.length; i < len; ++i) {
  10728. var prefix = prefixes[i];
  10729. // casing of Fullscreen differs across browsers
  10730. name = prefix + 'RequestFullscreen';
  10731. if (typeof body[name] === 'function') {
  10732. _names.requestFullscreen = name;
  10733. _supportsFullscreen = true;
  10734. } else {
  10735. name = prefix + 'RequestFullScreen';
  10736. if (typeof body[name] === 'function') {
  10737. _names.requestFullscreen = name;
  10738. _supportsFullscreen = true;
  10739. }
  10740. }
  10741. // disagreement about whether it's "exit" as per spec, or "cancel"
  10742. name = prefix + 'ExitFullscreen';
  10743. if (typeof document[name] === 'function') {
  10744. _names.exitFullscreen = name;
  10745. } else {
  10746. name = prefix + 'CancelFullScreen';
  10747. if (typeof document[name] === 'function') {
  10748. _names.exitFullscreen = name;
  10749. }
  10750. }
  10751. // casing of Fullscreen differs across browsers
  10752. name = prefix + 'FullscreenEnabled';
  10753. if (document[name] !== undefined) {
  10754. _names.fullscreenEnabled = name;
  10755. } else {
  10756. name = prefix + 'FullScreenEnabled';
  10757. if (document[name] !== undefined) {
  10758. _names.fullscreenEnabled = name;
  10759. }
  10760. }
  10761. // casing of Fullscreen differs across browsers
  10762. name = prefix + 'FullscreenElement';
  10763. if (document[name] !== undefined) {
  10764. _names.fullscreenElement = name;
  10765. } else {
  10766. name = prefix + 'FullScreenElement';
  10767. if (document[name] !== undefined) {
  10768. _names.fullscreenElement = name;
  10769. }
  10770. }
  10771. // thankfully, event names are all lowercase per spec
  10772. name = prefix + 'fullscreenchange';
  10773. // event names do not have 'on' in the front, but the property on the document does
  10774. if (document['on' + name] !== undefined) {
  10775. //except on IE
  10776. if (prefix === 'ms') {
  10777. name = 'MSFullscreenChange';
  10778. }
  10779. _names.fullscreenchange = name;
  10780. }
  10781. name = prefix + 'fullscreenerror';
  10782. if (document['on' + name] !== undefined) {
  10783. //except on IE
  10784. if (prefix === 'ms') {
  10785. name = 'MSFullscreenError';
  10786. }
  10787. _names.fullscreenerror = name;
  10788. }
  10789. }
  10790. return _supportsFullscreen;
  10791. };
  10792. /**
  10793. * Asynchronously requests the browser to enter fullscreen mode on the given element.
  10794. * If fullscreen mode is not supported by the browser, does nothing.
  10795. *
  10796. * @param {Object} element The HTML element which will be placed into fullscreen mode.
  10797. * @param {HMDVRDevice} [vrDevice] The VR device.
  10798. *
  10799. * @example
  10800. * // Put the entire page into fullscreen.
  10801. * Cesium.Fullscreen.requestFullscreen(document.body)
  10802. *
  10803. * // Place only the Cesium canvas into fullscreen.
  10804. * Cesium.Fullscreen.requestFullscreen(scene.canvas)
  10805. */
  10806. Fullscreen.requestFullscreen = function(element, vrDevice) {
  10807. if (!Fullscreen.supportsFullscreen()) {
  10808. return;
  10809. }
  10810. element[_names.requestFullscreen]({ vrDisplay: vrDevice });
  10811. };
  10812. /**
  10813. * Asynchronously exits fullscreen mode. If the browser is not currently
  10814. * in fullscreen, or if fullscreen mode is not supported by the browser, does nothing.
  10815. */
  10816. Fullscreen.exitFullscreen = function() {
  10817. if (!Fullscreen.supportsFullscreen()) {
  10818. return;
  10819. }
  10820. document[_names.exitFullscreen]();
  10821. };
  10822. return Fullscreen;
  10823. });
  10824. /*global define*/
  10825. define('Core/FeatureDetection',[
  10826. './defaultValue',
  10827. './defined',
  10828. './Fullscreen'
  10829. ], function(
  10830. defaultValue,
  10831. defined,
  10832. Fullscreen) {
  10833. 'use strict';
  10834. var theNavigator;
  10835. if (typeof navigator !== 'undefined') {
  10836. theNavigator = navigator;
  10837. } else {
  10838. theNavigator = {};
  10839. }
  10840. function extractVersion(versionString) {
  10841. var parts = versionString.split('.');
  10842. for (var i = 0, len = parts.length; i < len; ++i) {
  10843. parts[i] = parseInt(parts[i], 10);
  10844. }
  10845. return parts;
  10846. }
  10847. var isChromeResult;
  10848. var chromeVersionResult;
  10849. function isChrome() {
  10850. if (!defined(isChromeResult)) {
  10851. isChromeResult = false;
  10852. // Edge contains Chrome in the user agent too
  10853. if (!isEdge()) {
  10854. var fields = (/ Chrome\/([\.0-9]+)/).exec(theNavigator.userAgent);
  10855. if (fields !== null) {
  10856. isChromeResult = true;
  10857. chromeVersionResult = extractVersion(fields[1]);
  10858. }
  10859. }
  10860. }
  10861. return isChromeResult;
  10862. }
  10863. function chromeVersion() {
  10864. return isChrome() && chromeVersionResult;
  10865. }
  10866. var isSafariResult;
  10867. var safariVersionResult;
  10868. function isSafari() {
  10869. if (!defined(isSafariResult)) {
  10870. isSafariResult = false;
  10871. // Chrome and Edge contain Safari in the user agent too
  10872. if (!isChrome() && !isEdge() && (/ Safari\/[\.0-9]+/).test(theNavigator.userAgent)) {
  10873. var fields = (/ Version\/([\.0-9]+)/).exec(theNavigator.userAgent);
  10874. if (fields !== null) {
  10875. isSafariResult = true;
  10876. safariVersionResult = extractVersion(fields[1]);
  10877. }
  10878. }
  10879. }
  10880. return isSafariResult;
  10881. }
  10882. function safariVersion() {
  10883. return isSafari() && safariVersionResult;
  10884. }
  10885. var isWebkitResult;
  10886. var webkitVersionResult;
  10887. function isWebkit() {
  10888. if (!defined(isWebkitResult)) {
  10889. isWebkitResult = false;
  10890. var fields = (/ AppleWebKit\/([\.0-9]+)(\+?)/).exec(theNavigator.userAgent);
  10891. if (fields !== null) {
  10892. isWebkitResult = true;
  10893. webkitVersionResult = extractVersion(fields[1]);
  10894. webkitVersionResult.isNightly = !!fields[2];
  10895. }
  10896. }
  10897. return isWebkitResult;
  10898. }
  10899. function webkitVersion() {
  10900. return isWebkit() && webkitVersionResult;
  10901. }
  10902. var isInternetExplorerResult;
  10903. var internetExplorerVersionResult;
  10904. function isInternetExplorer() {
  10905. if (!defined(isInternetExplorerResult)) {
  10906. isInternetExplorerResult = false;
  10907. var fields;
  10908. if (theNavigator.appName === 'Microsoft Internet Explorer') {
  10909. fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  10910. if (fields !== null) {
  10911. isInternetExplorerResult = true;
  10912. internetExplorerVersionResult = extractVersion(fields[1]);
  10913. }
  10914. } else if (theNavigator.appName === 'Netscape') {
  10915. fields = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  10916. if (fields !== null) {
  10917. isInternetExplorerResult = true;
  10918. internetExplorerVersionResult = extractVersion(fields[1]);
  10919. }
  10920. }
  10921. }
  10922. return isInternetExplorerResult;
  10923. }
  10924. function internetExplorerVersion() {
  10925. return isInternetExplorer() && internetExplorerVersionResult;
  10926. }
  10927. var isEdgeResult;
  10928. var edgeVersionResult;
  10929. function isEdge() {
  10930. if (!defined(isEdgeResult)) {
  10931. isEdgeResult = false;
  10932. var fields = (/ Edge\/([\.0-9]+)/).exec(theNavigator.userAgent);
  10933. if (fields !== null) {
  10934. isEdgeResult = true;
  10935. edgeVersionResult = extractVersion(fields[1]);
  10936. }
  10937. }
  10938. return isEdgeResult;
  10939. }
  10940. function edgeVersion() {
  10941. return isEdge() && edgeVersionResult;
  10942. }
  10943. var isFirefoxResult;
  10944. var firefoxVersionResult;
  10945. function isFirefox() {
  10946. if (!defined(isFirefoxResult)) {
  10947. isFirefoxResult = false;
  10948. var fields = /Firefox\/([\.0-9]+)/.exec(theNavigator.userAgent);
  10949. if (fields !== null) {
  10950. isFirefoxResult = true;
  10951. firefoxVersionResult = extractVersion(fields[1]);
  10952. }
  10953. }
  10954. return isFirefoxResult;
  10955. }
  10956. var isWindowsResult;
  10957. function isWindows() {
  10958. if (!defined(isWindowsResult)) {
  10959. isWindowsResult = /Windows/i.test(theNavigator.appVersion);
  10960. }
  10961. return isWindowsResult;
  10962. }
  10963. function firefoxVersion() {
  10964. return isFirefox() && firefoxVersionResult;
  10965. }
  10966. var hasPointerEvents;
  10967. function supportsPointerEvents() {
  10968. if (!defined(hasPointerEvents)) {
  10969. //While navigator.pointerEnabled is deprecated in the W3C specification
  10970. //we still need to use it if it exists in order to support browsers
  10971. //that rely on it, such as the Windows WebBrowser control which defines
  10972. //PointerEvent but sets navigator.pointerEnabled to false.
  10973. hasPointerEvents = typeof PointerEvent !== 'undefined' && (!defined(theNavigator.pointerEnabled) || theNavigator.pointerEnabled);
  10974. }
  10975. return hasPointerEvents;
  10976. }
  10977. var imageRenderingValueResult;
  10978. var supportsImageRenderingPixelatedResult;
  10979. function supportsImageRenderingPixelated() {
  10980. if (!defined(supportsImageRenderingPixelatedResult)) {
  10981. var canvas = document.createElement('canvas');
  10982. canvas.setAttribute('style',
  10983. 'image-rendering: -moz-crisp-edges;' +
  10984. 'image-rendering: pixelated;');
  10985. //canvas.style.imageRendering will be undefined, null or an empty string on unsupported browsers.
  10986. var tmp = canvas.style.imageRendering;
  10987. supportsImageRenderingPixelatedResult = defined(tmp) && tmp !== '';
  10988. if (supportsImageRenderingPixelatedResult) {
  10989. imageRenderingValueResult = tmp;
  10990. }
  10991. }
  10992. return supportsImageRenderingPixelatedResult;
  10993. }
  10994. function imageRenderingValue() {
  10995. return supportsImageRenderingPixelated() ? imageRenderingValueResult : undefined;
  10996. }
  10997. /**
  10998. * A set of functions to detect whether the current browser supports
  10999. * various features.
  11000. *
  11001. * @exports FeatureDetection
  11002. */
  11003. var FeatureDetection = {
  11004. isChrome : isChrome,
  11005. chromeVersion : chromeVersion,
  11006. isSafari : isSafari,
  11007. safariVersion : safariVersion,
  11008. isWebkit : isWebkit,
  11009. webkitVersion : webkitVersion,
  11010. isInternetExplorer : isInternetExplorer,
  11011. internetExplorerVersion : internetExplorerVersion,
  11012. isEdge : isEdge,
  11013. edgeVersion : edgeVersion,
  11014. isFirefox : isFirefox,
  11015. firefoxVersion : firefoxVersion,
  11016. isWindows : isWindows,
  11017. hardwareConcurrency : defaultValue(theNavigator.hardwareConcurrency, 3),
  11018. supportsPointerEvents : supportsPointerEvents,
  11019. supportsImageRenderingPixelated: supportsImageRenderingPixelated,
  11020. imageRenderingValue: imageRenderingValue
  11021. };
  11022. /**
  11023. * Detects whether the current browser supports the full screen standard.
  11024. *
  11025. * @returns {Boolean} true if the browser supports the full screen standard, false if not.
  11026. *
  11027. * @see Fullscreen
  11028. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  11029. */
  11030. FeatureDetection.supportsFullscreen = function() {
  11031. return Fullscreen.supportsFullscreen();
  11032. };
  11033. /**
  11034. * Detects whether the current browser supports typed arrays.
  11035. *
  11036. * @returns {Boolean} true if the browser supports typed arrays, false if not.
  11037. *
  11038. * @see {@link http://www.khronos.org/registry/typedarray/specs/latest/|Typed Array Specification}
  11039. */
  11040. FeatureDetection.supportsTypedArrays = function() {
  11041. return typeof ArrayBuffer !== 'undefined';
  11042. };
  11043. /**
  11044. * Detects whether the current browser supports Web Workers.
  11045. *
  11046. * @returns {Boolean} true if the browsers supports Web Workers, false if not.
  11047. *
  11048. * @see {@link http://www.w3.org/TR/workers/}
  11049. */
  11050. FeatureDetection.supportsWebWorkers = function() {
  11051. return typeof Worker !== 'undefined';
  11052. };
  11053. return FeatureDetection;
  11054. });
  11055. /*global define*/
  11056. define('Core/WebGLConstants',[
  11057. './freezeObject'
  11058. ], function(
  11059. freezeObject) {
  11060. 'use strict';
  11061. /**
  11062. * Enum containing WebGL Constant values by name.
  11063. * for use without an active WebGL context, or in cases where certain constants are unavailable using the WebGL context
  11064. * (For example, in [Safari 9]{@link https://github.com/AnalyticalGraphicsInc/cesium/issues/2989}).
  11065. *
  11066. * These match the constants from the [WebGL 1.0]{@link https://www.khronos.org/registry/webgl/specs/latest/1.0/}
  11067. * and [WebGL 2.0]{@link https://www.khronos.org/registry/webgl/specs/latest/2.0/}
  11068. * specifications.
  11069. *
  11070. * @exports WebGLConstants
  11071. */
  11072. var WebGLConstants = {
  11073. DEPTH_BUFFER_BIT : 0x00000100,
  11074. STENCIL_BUFFER_BIT : 0x00000400,
  11075. COLOR_BUFFER_BIT : 0x00004000,
  11076. POINTS : 0x0000,
  11077. LINES : 0x0001,
  11078. LINE_LOOP : 0x0002,
  11079. LINE_STRIP : 0x0003,
  11080. TRIANGLES : 0x0004,
  11081. TRIANGLE_STRIP : 0x0005,
  11082. TRIANGLE_FAN : 0x0006,
  11083. ZERO : 0,
  11084. ONE : 1,
  11085. SRC_COLOR : 0x0300,
  11086. ONE_MINUS_SRC_COLOR : 0x0301,
  11087. SRC_ALPHA : 0x0302,
  11088. ONE_MINUS_SRC_ALPHA : 0x0303,
  11089. DST_ALPHA : 0x0304,
  11090. ONE_MINUS_DST_ALPHA : 0x0305,
  11091. DST_COLOR : 0x0306,
  11092. ONE_MINUS_DST_COLOR : 0x0307,
  11093. SRC_ALPHA_SATURATE : 0x0308,
  11094. FUNC_ADD : 0x8006,
  11095. BLEND_EQUATION : 0x8009,
  11096. BLEND_EQUATION_RGB : 0x8009, // same as BLEND_EQUATION
  11097. BLEND_EQUATION_ALPHA : 0x883D,
  11098. FUNC_SUBTRACT : 0x800A,
  11099. FUNC_REVERSE_SUBTRACT : 0x800B,
  11100. BLEND_DST_RGB : 0x80C8,
  11101. BLEND_SRC_RGB : 0x80C9,
  11102. BLEND_DST_ALPHA : 0x80CA,
  11103. BLEND_SRC_ALPHA : 0x80CB,
  11104. CONSTANT_COLOR : 0x8001,
  11105. ONE_MINUS_CONSTANT_COLOR : 0x8002,
  11106. CONSTANT_ALPHA : 0x8003,
  11107. ONE_MINUS_CONSTANT_ALPHA : 0x8004,
  11108. BLEND_COLOR : 0x8005,
  11109. ARRAY_BUFFER : 0x8892,
  11110. ELEMENT_ARRAY_BUFFER : 0x8893,
  11111. ARRAY_BUFFER_BINDING : 0x8894,
  11112. ELEMENT_ARRAY_BUFFER_BINDING : 0x8895,
  11113. STREAM_DRAW : 0x88E0,
  11114. STATIC_DRAW : 0x88E4,
  11115. DYNAMIC_DRAW : 0x88E8,
  11116. BUFFER_SIZE : 0x8764,
  11117. BUFFER_USAGE : 0x8765,
  11118. CURRENT_VERTEX_ATTRIB : 0x8626,
  11119. FRONT : 0x0404,
  11120. BACK : 0x0405,
  11121. FRONT_AND_BACK : 0x0408,
  11122. CULL_FACE : 0x0B44,
  11123. BLEND : 0x0BE2,
  11124. DITHER : 0x0BD0,
  11125. STENCIL_TEST : 0x0B90,
  11126. DEPTH_TEST : 0x0B71,
  11127. SCISSOR_TEST : 0x0C11,
  11128. POLYGON_OFFSET_FILL : 0x8037,
  11129. SAMPLE_ALPHA_TO_COVERAGE : 0x809E,
  11130. SAMPLE_COVERAGE : 0x80A0,
  11131. NO_ERROR : 0,
  11132. INVALID_ENUM : 0x0500,
  11133. INVALID_VALUE : 0x0501,
  11134. INVALID_OPERATION : 0x0502,
  11135. OUT_OF_MEMORY : 0x0505,
  11136. CW : 0x0900,
  11137. CCW : 0x0901,
  11138. LINE_WIDTH : 0x0B21,
  11139. ALIASED_POINT_SIZE_RANGE : 0x846D,
  11140. ALIASED_LINE_WIDTH_RANGE : 0x846E,
  11141. CULL_FACE_MODE : 0x0B45,
  11142. FRONT_FACE : 0x0B46,
  11143. DEPTH_RANGE : 0x0B70,
  11144. DEPTH_WRITEMASK : 0x0B72,
  11145. DEPTH_CLEAR_VALUE : 0x0B73,
  11146. DEPTH_FUNC : 0x0B74,
  11147. STENCIL_CLEAR_VALUE : 0x0B91,
  11148. STENCIL_FUNC : 0x0B92,
  11149. STENCIL_FAIL : 0x0B94,
  11150. STENCIL_PASS_DEPTH_FAIL : 0x0B95,
  11151. STENCIL_PASS_DEPTH_PASS : 0x0B96,
  11152. STENCIL_REF : 0x0B97,
  11153. STENCIL_VALUE_MASK : 0x0B93,
  11154. STENCIL_WRITEMASK : 0x0B98,
  11155. STENCIL_BACK_FUNC : 0x8800,
  11156. STENCIL_BACK_FAIL : 0x8801,
  11157. STENCIL_BACK_PASS_DEPTH_FAIL : 0x8802,
  11158. STENCIL_BACK_PASS_DEPTH_PASS : 0x8803,
  11159. STENCIL_BACK_REF : 0x8CA3,
  11160. STENCIL_BACK_VALUE_MASK : 0x8CA4,
  11161. STENCIL_BACK_WRITEMASK : 0x8CA5,
  11162. VIEWPORT : 0x0BA2,
  11163. SCISSOR_BOX : 0x0C10,
  11164. COLOR_CLEAR_VALUE : 0x0C22,
  11165. COLOR_WRITEMASK : 0x0C23,
  11166. UNPACK_ALIGNMENT : 0x0CF5,
  11167. PACK_ALIGNMENT : 0x0D05,
  11168. MAX_TEXTURE_SIZE : 0x0D33,
  11169. MAX_VIEWPORT_DIMS : 0x0D3A,
  11170. SUBPIXEL_BITS : 0x0D50,
  11171. RED_BITS : 0x0D52,
  11172. GREEN_BITS : 0x0D53,
  11173. BLUE_BITS : 0x0D54,
  11174. ALPHA_BITS : 0x0D55,
  11175. DEPTH_BITS : 0x0D56,
  11176. STENCIL_BITS : 0x0D57,
  11177. POLYGON_OFFSET_UNITS : 0x2A00,
  11178. POLYGON_OFFSET_FACTOR : 0x8038,
  11179. TEXTURE_BINDING_2D : 0x8069,
  11180. SAMPLE_BUFFERS : 0x80A8,
  11181. SAMPLES : 0x80A9,
  11182. SAMPLE_COVERAGE_VALUE : 0x80AA,
  11183. SAMPLE_COVERAGE_INVERT : 0x80AB,
  11184. COMPRESSED_TEXTURE_FORMATS : 0x86A3,
  11185. DONT_CARE : 0x1100,
  11186. FASTEST : 0x1101,
  11187. NICEST : 0x1102,
  11188. GENERATE_MIPMAP_HINT : 0x8192,
  11189. BYTE : 0x1400,
  11190. UNSIGNED_BYTE : 0x1401,
  11191. SHORT : 0x1402,
  11192. UNSIGNED_SHORT : 0x1403,
  11193. INT : 0x1404,
  11194. UNSIGNED_INT : 0x1405,
  11195. FLOAT : 0x1406,
  11196. DEPTH_COMPONENT : 0x1902,
  11197. ALPHA : 0x1906,
  11198. RGB : 0x1907,
  11199. RGBA : 0x1908,
  11200. LUMINANCE : 0x1909,
  11201. LUMINANCE_ALPHA : 0x190A,
  11202. UNSIGNED_SHORT_4_4_4_4 : 0x8033,
  11203. UNSIGNED_SHORT_5_5_5_1 : 0x8034,
  11204. UNSIGNED_SHORT_5_6_5 : 0x8363,
  11205. FRAGMENT_SHADER : 0x8B30,
  11206. VERTEX_SHADER : 0x8B31,
  11207. MAX_VERTEX_ATTRIBS : 0x8869,
  11208. MAX_VERTEX_UNIFORM_VECTORS : 0x8DFB,
  11209. MAX_VARYING_VECTORS : 0x8DFC,
  11210. MAX_COMBINED_TEXTURE_IMAGE_UNITS : 0x8B4D,
  11211. MAX_VERTEX_TEXTURE_IMAGE_UNITS : 0x8B4C,
  11212. MAX_TEXTURE_IMAGE_UNITS : 0x8872,
  11213. MAX_FRAGMENT_UNIFORM_VECTORS : 0x8DFD,
  11214. SHADER_TYPE : 0x8B4F,
  11215. DELETE_STATUS : 0x8B80,
  11216. LINK_STATUS : 0x8B82,
  11217. VALIDATE_STATUS : 0x8B83,
  11218. ATTACHED_SHADERS : 0x8B85,
  11219. ACTIVE_UNIFORMS : 0x8B86,
  11220. ACTIVE_ATTRIBUTES : 0x8B89,
  11221. SHADING_LANGUAGE_VERSION : 0x8B8C,
  11222. CURRENT_PROGRAM : 0x8B8D,
  11223. NEVER : 0x0200,
  11224. LESS : 0x0201,
  11225. EQUAL : 0x0202,
  11226. LEQUAL : 0x0203,
  11227. GREATER : 0x0204,
  11228. NOTEQUAL : 0x0205,
  11229. GEQUAL : 0x0206,
  11230. ALWAYS : 0x0207,
  11231. KEEP : 0x1E00,
  11232. REPLACE : 0x1E01,
  11233. INCR : 0x1E02,
  11234. DECR : 0x1E03,
  11235. INVERT : 0x150A,
  11236. INCR_WRAP : 0x8507,
  11237. DECR_WRAP : 0x8508,
  11238. VENDOR : 0x1F00,
  11239. RENDERER : 0x1F01,
  11240. VERSION : 0x1F02,
  11241. NEAREST : 0x2600,
  11242. LINEAR : 0x2601,
  11243. NEAREST_MIPMAP_NEAREST : 0x2700,
  11244. LINEAR_MIPMAP_NEAREST : 0x2701,
  11245. NEAREST_MIPMAP_LINEAR : 0x2702,
  11246. LINEAR_MIPMAP_LINEAR : 0x2703,
  11247. TEXTURE_MAG_FILTER : 0x2800,
  11248. TEXTURE_MIN_FILTER : 0x2801,
  11249. TEXTURE_WRAP_S : 0x2802,
  11250. TEXTURE_WRAP_T : 0x2803,
  11251. TEXTURE_2D : 0x0DE1,
  11252. TEXTURE : 0x1702,
  11253. TEXTURE_CUBE_MAP : 0x8513,
  11254. TEXTURE_BINDING_CUBE_MAP : 0x8514,
  11255. TEXTURE_CUBE_MAP_POSITIVE_X : 0x8515,
  11256. TEXTURE_CUBE_MAP_NEGATIVE_X : 0x8516,
  11257. TEXTURE_CUBE_MAP_POSITIVE_Y : 0x8517,
  11258. TEXTURE_CUBE_MAP_NEGATIVE_Y : 0x8518,
  11259. TEXTURE_CUBE_MAP_POSITIVE_Z : 0x8519,
  11260. TEXTURE_CUBE_MAP_NEGATIVE_Z : 0x851A,
  11261. MAX_CUBE_MAP_TEXTURE_SIZE : 0x851C,
  11262. TEXTURE0 : 0x84C0,
  11263. TEXTURE1 : 0x84C1,
  11264. TEXTURE2 : 0x84C2,
  11265. TEXTURE3 : 0x84C3,
  11266. TEXTURE4 : 0x84C4,
  11267. TEXTURE5 : 0x84C5,
  11268. TEXTURE6 : 0x84C6,
  11269. TEXTURE7 : 0x84C7,
  11270. TEXTURE8 : 0x84C8,
  11271. TEXTURE9 : 0x84C9,
  11272. TEXTURE10 : 0x84CA,
  11273. TEXTURE11 : 0x84CB,
  11274. TEXTURE12 : 0x84CC,
  11275. TEXTURE13 : 0x84CD,
  11276. TEXTURE14 : 0x84CE,
  11277. TEXTURE15 : 0x84CF,
  11278. TEXTURE16 : 0x84D0,
  11279. TEXTURE17 : 0x84D1,
  11280. TEXTURE18 : 0x84D2,
  11281. TEXTURE19 : 0x84D3,
  11282. TEXTURE20 : 0x84D4,
  11283. TEXTURE21 : 0x84D5,
  11284. TEXTURE22 : 0x84D6,
  11285. TEXTURE23 : 0x84D7,
  11286. TEXTURE24 : 0x84D8,
  11287. TEXTURE25 : 0x84D9,
  11288. TEXTURE26 : 0x84DA,
  11289. TEXTURE27 : 0x84DB,
  11290. TEXTURE28 : 0x84DC,
  11291. TEXTURE29 : 0x84DD,
  11292. TEXTURE30 : 0x84DE,
  11293. TEXTURE31 : 0x84DF,
  11294. ACTIVE_TEXTURE : 0x84E0,
  11295. REPEAT : 0x2901,
  11296. CLAMP_TO_EDGE : 0x812F,
  11297. MIRRORED_REPEAT : 0x8370,
  11298. FLOAT_VEC2 : 0x8B50,
  11299. FLOAT_VEC3 : 0x8B51,
  11300. FLOAT_VEC4 : 0x8B52,
  11301. INT_VEC2 : 0x8B53,
  11302. INT_VEC3 : 0x8B54,
  11303. INT_VEC4 : 0x8B55,
  11304. BOOL : 0x8B56,
  11305. BOOL_VEC2 : 0x8B57,
  11306. BOOL_VEC3 : 0x8B58,
  11307. BOOL_VEC4 : 0x8B59,
  11308. FLOAT_MAT2 : 0x8B5A,
  11309. FLOAT_MAT3 : 0x8B5B,
  11310. FLOAT_MAT4 : 0x8B5C,
  11311. SAMPLER_2D : 0x8B5E,
  11312. SAMPLER_CUBE : 0x8B60,
  11313. VERTEX_ATTRIB_ARRAY_ENABLED : 0x8622,
  11314. VERTEX_ATTRIB_ARRAY_SIZE : 0x8623,
  11315. VERTEX_ATTRIB_ARRAY_STRIDE : 0x8624,
  11316. VERTEX_ATTRIB_ARRAY_TYPE : 0x8625,
  11317. VERTEX_ATTRIB_ARRAY_NORMALIZED : 0x886A,
  11318. VERTEX_ATTRIB_ARRAY_POINTER : 0x8645,
  11319. VERTEX_ATTRIB_ARRAY_BUFFER_BINDING : 0x889F,
  11320. IMPLEMENTATION_COLOR_READ_TYPE : 0x8B9A,
  11321. IMPLEMENTATION_COLOR_READ_FORMAT : 0x8B9B,
  11322. COMPILE_STATUS : 0x8B81,
  11323. LOW_FLOAT : 0x8DF0,
  11324. MEDIUM_FLOAT : 0x8DF1,
  11325. HIGH_FLOAT : 0x8DF2,
  11326. LOW_INT : 0x8DF3,
  11327. MEDIUM_INT : 0x8DF4,
  11328. HIGH_INT : 0x8DF5,
  11329. FRAMEBUFFER : 0x8D40,
  11330. RENDERBUFFER : 0x8D41,
  11331. RGBA4 : 0x8056,
  11332. RGB5_A1 : 0x8057,
  11333. RGB565 : 0x8D62,
  11334. DEPTH_COMPONENT16 : 0x81A5,
  11335. STENCIL_INDEX : 0x1901,
  11336. STENCIL_INDEX8 : 0x8D48,
  11337. DEPTH_STENCIL : 0x84F9,
  11338. RENDERBUFFER_WIDTH : 0x8D42,
  11339. RENDERBUFFER_HEIGHT : 0x8D43,
  11340. RENDERBUFFER_INTERNAL_FORMAT : 0x8D44,
  11341. RENDERBUFFER_RED_SIZE : 0x8D50,
  11342. RENDERBUFFER_GREEN_SIZE : 0x8D51,
  11343. RENDERBUFFER_BLUE_SIZE : 0x8D52,
  11344. RENDERBUFFER_ALPHA_SIZE : 0x8D53,
  11345. RENDERBUFFER_DEPTH_SIZE : 0x8D54,
  11346. RENDERBUFFER_STENCIL_SIZE : 0x8D55,
  11347. FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE : 0x8CD0,
  11348. FRAMEBUFFER_ATTACHMENT_OBJECT_NAME : 0x8CD1,
  11349. FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL : 0x8CD2,
  11350. FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE : 0x8CD3,
  11351. COLOR_ATTACHMENT0 : 0x8CE0,
  11352. DEPTH_ATTACHMENT : 0x8D00,
  11353. STENCIL_ATTACHMENT : 0x8D20,
  11354. DEPTH_STENCIL_ATTACHMENT : 0x821A,
  11355. NONE : 0,
  11356. FRAMEBUFFER_COMPLETE : 0x8CD5,
  11357. FRAMEBUFFER_INCOMPLETE_ATTACHMENT : 0x8CD6,
  11358. FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : 0x8CD7,
  11359. FRAMEBUFFER_INCOMPLETE_DIMENSIONS : 0x8CD9,
  11360. FRAMEBUFFER_UNSUPPORTED : 0x8CDD,
  11361. FRAMEBUFFER_BINDING : 0x8CA6,
  11362. RENDERBUFFER_BINDING : 0x8CA7,
  11363. MAX_RENDERBUFFER_SIZE : 0x84E8,
  11364. INVALID_FRAMEBUFFER_OPERATION : 0x0506,
  11365. UNPACK_FLIP_Y_WEBGL : 0x9240,
  11366. UNPACK_PREMULTIPLY_ALPHA_WEBGL : 0x9241,
  11367. CONTEXT_LOST_WEBGL : 0x9242,
  11368. UNPACK_COLORSPACE_CONVERSION_WEBGL : 0x9243,
  11369. BROWSER_DEFAULT_WEBGL : 0x9244,
  11370. // Desktop OpenGL
  11371. DOUBLE : 0x140A,
  11372. // WebGL 2
  11373. READ_BUFFER : 0x0C02,
  11374. UNPACK_ROW_LENGTH : 0x0CF2,
  11375. UNPACK_SKIP_ROWS : 0x0CF3,
  11376. UNPACK_SKIP_PIXELS : 0x0CF4,
  11377. PACK_ROW_LENGTH : 0x0D02,
  11378. PACK_SKIP_ROWS : 0x0D03,
  11379. PACK_SKIP_PIXELS : 0x0D04,
  11380. COLOR : 0x1800,
  11381. DEPTH : 0x1801,
  11382. STENCIL : 0x1802,
  11383. RED : 0x1903,
  11384. RGB8 : 0x8051,
  11385. RGBA8 : 0x8058,
  11386. RGB10_A2 : 0x8059,
  11387. TEXTURE_BINDING_3D : 0x806A,
  11388. UNPACK_SKIP_IMAGES : 0x806D,
  11389. UNPACK_IMAGE_HEIGHT : 0x806E,
  11390. TEXTURE_3D : 0x806F,
  11391. TEXTURE_WRAP_R : 0x8072,
  11392. MAX_3D_TEXTURE_SIZE : 0x8073,
  11393. UNSIGNED_INT_2_10_10_10_REV : 0x8368,
  11394. MAX_ELEMENTS_VERTICES : 0x80E8,
  11395. MAX_ELEMENTS_INDICES : 0x80E9,
  11396. TEXTURE_MIN_LOD : 0x813A,
  11397. TEXTURE_MAX_LOD : 0x813B,
  11398. TEXTURE_BASE_LEVEL : 0x813C,
  11399. TEXTURE_MAX_LEVEL : 0x813D,
  11400. MIN : 0x8007,
  11401. MAX : 0x8008,
  11402. DEPTH_COMPONENT24 : 0x81A6,
  11403. MAX_TEXTURE_LOD_BIAS : 0x84FD,
  11404. TEXTURE_COMPARE_MODE : 0x884C,
  11405. TEXTURE_COMPARE_FUNC : 0x884D,
  11406. CURRENT_QUERY : 0x8865,
  11407. QUERY_RESULT : 0x8866,
  11408. QUERY_RESULT_AVAILABLE : 0x8867,
  11409. STREAM_READ : 0x88E1,
  11410. STREAM_COPY : 0x88E2,
  11411. STATIC_READ : 0x88E5,
  11412. STATIC_COPY : 0x88E6,
  11413. DYNAMIC_READ : 0x88E9,
  11414. DYNAMIC_COPY : 0x88EA,
  11415. MAX_DRAW_BUFFERS : 0x8824,
  11416. DRAW_BUFFER0 : 0x8825,
  11417. DRAW_BUFFER1 : 0x8826,
  11418. DRAW_BUFFER2 : 0x8827,
  11419. DRAW_BUFFER3 : 0x8828,
  11420. DRAW_BUFFER4 : 0x8829,
  11421. DRAW_BUFFER5 : 0x882A,
  11422. DRAW_BUFFER6 : 0x882B,
  11423. DRAW_BUFFER7 : 0x882C,
  11424. DRAW_BUFFER8 : 0x882D,
  11425. DRAW_BUFFER9 : 0x882E,
  11426. DRAW_BUFFER10 : 0x882F,
  11427. DRAW_BUFFER11 : 0x8830,
  11428. DRAW_BUFFER12 : 0x8831,
  11429. DRAW_BUFFER13 : 0x8832,
  11430. DRAW_BUFFER14 : 0x8833,
  11431. DRAW_BUFFER15 : 0x8834,
  11432. MAX_FRAGMENT_UNIFORM_COMPONENTS : 0x8B49,
  11433. MAX_VERTEX_UNIFORM_COMPONENTS : 0x8B4A,
  11434. SAMPLER_3D : 0x8B5F,
  11435. SAMPLER_2D_SHADOW : 0x8B62,
  11436. FRAGMENT_SHADER_DERIVATIVE_HINT : 0x8B8B,
  11437. PIXEL_PACK_BUFFER : 0x88EB,
  11438. PIXEL_UNPACK_BUFFER : 0x88EC,
  11439. PIXEL_PACK_BUFFER_BINDING : 0x88ED,
  11440. PIXEL_UNPACK_BUFFER_BINDING : 0x88EF,
  11441. FLOAT_MAT2x3 : 0x8B65,
  11442. FLOAT_MAT2x4 : 0x8B66,
  11443. FLOAT_MAT3x2 : 0x8B67,
  11444. FLOAT_MAT3x4 : 0x8B68,
  11445. FLOAT_MAT4x2 : 0x8B69,
  11446. FLOAT_MAT4x3 : 0x8B6A,
  11447. SRGB : 0x8C40,
  11448. SRGB8 : 0x8C41,
  11449. SRGB8_ALPHA8 : 0x8C43,
  11450. COMPARE_REF_TO_TEXTURE : 0x884E,
  11451. RGBA32F : 0x8814,
  11452. RGB32F : 0x8815,
  11453. RGBA16F : 0x881A,
  11454. RGB16F : 0x881B,
  11455. VERTEX_ATTRIB_ARRAY_INTEGER : 0x88FD,
  11456. MAX_ARRAY_TEXTURE_LAYERS : 0x88FF,
  11457. MIN_PROGRAM_TEXEL_OFFSET : 0x8904,
  11458. MAX_PROGRAM_TEXEL_OFFSET : 0x8905,
  11459. MAX_VARYING_COMPONENTS : 0x8B4B,
  11460. TEXTURE_2D_ARRAY : 0x8C1A,
  11461. TEXTURE_BINDING_2D_ARRAY : 0x8C1D,
  11462. R11F_G11F_B10F : 0x8C3A,
  11463. UNSIGNED_INT_10F_11F_11F_REV : 0x8C3B,
  11464. RGB9_E5 : 0x8C3D,
  11465. UNSIGNED_INT_5_9_9_9_REV : 0x8C3E,
  11466. TRANSFORM_FEEDBACK_BUFFER_MODE : 0x8C7F,
  11467. MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS : 0x8C80,
  11468. TRANSFORM_FEEDBACK_VARYINGS : 0x8C83,
  11469. TRANSFORM_FEEDBACK_BUFFER_START : 0x8C84,
  11470. TRANSFORM_FEEDBACK_BUFFER_SIZE : 0x8C85,
  11471. TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN : 0x8C88,
  11472. RASTERIZER_DISCARD : 0x8C89,
  11473. MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS : 0x8C8A,
  11474. MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS : 0x8C8B,
  11475. INTERLEAVED_ATTRIBS : 0x8C8C,
  11476. SEPARATE_ATTRIBS : 0x8C8D,
  11477. TRANSFORM_FEEDBACK_BUFFER : 0x8C8E,
  11478. TRANSFORM_FEEDBACK_BUFFER_BINDING : 0x8C8F,
  11479. RGBA32UI : 0x8D70,
  11480. RGB32UI : 0x8D71,
  11481. RGBA16UI : 0x8D76,
  11482. RGB16UI : 0x8D77,
  11483. RGBA8UI : 0x8D7C,
  11484. RGB8UI : 0x8D7D,
  11485. RGBA32I : 0x8D82,
  11486. RGB32I : 0x8D83,
  11487. RGBA16I : 0x8D88,
  11488. RGB16I : 0x8D89,
  11489. RGBA8I : 0x8D8E,
  11490. RGB8I : 0x8D8F,
  11491. RED_INTEGER : 0x8D94,
  11492. RGB_INTEGER : 0x8D98,
  11493. RGBA_INTEGER : 0x8D99,
  11494. SAMPLER_2D_ARRAY : 0x8DC1,
  11495. SAMPLER_2D_ARRAY_SHADOW : 0x8DC4,
  11496. SAMPLER_CUBE_SHADOW : 0x8DC5,
  11497. UNSIGNED_INT_VEC2 : 0x8DC6,
  11498. UNSIGNED_INT_VEC3 : 0x8DC7,
  11499. UNSIGNED_INT_VEC4 : 0x8DC8,
  11500. INT_SAMPLER_2D : 0x8DCA,
  11501. INT_SAMPLER_3D : 0x8DCB,
  11502. INT_SAMPLER_CUBE : 0x8DCC,
  11503. INT_SAMPLER_2D_ARRAY : 0x8DCF,
  11504. UNSIGNED_INT_SAMPLER_2D : 0x8DD2,
  11505. UNSIGNED_INT_SAMPLER_3D : 0x8DD3,
  11506. UNSIGNED_INT_SAMPLER_CUBE : 0x8DD4,
  11507. UNSIGNED_INT_SAMPLER_2D_ARRAY : 0x8DD7,
  11508. DEPTH_COMPONENT32F : 0x8CAC,
  11509. DEPTH32F_STENCIL8 : 0x8CAD,
  11510. FLOAT_32_UNSIGNED_INT_24_8_REV : 0x8DAD,
  11511. FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING : 0x8210,
  11512. FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE : 0x8211,
  11513. FRAMEBUFFER_ATTACHMENT_RED_SIZE : 0x8212,
  11514. FRAMEBUFFER_ATTACHMENT_GREEN_SIZE : 0x8213,
  11515. FRAMEBUFFER_ATTACHMENT_BLUE_SIZE : 0x8214,
  11516. FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE : 0x8215,
  11517. FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE : 0x8216,
  11518. FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE : 0x8217,
  11519. FRAMEBUFFER_DEFAULT : 0x8218,
  11520. UNSIGNED_INT_24_8 : 0x84FA,
  11521. DEPTH24_STENCIL8 : 0x88F0,
  11522. UNSIGNED_NORMALIZED : 0x8C17,
  11523. DRAW_FRAMEBUFFER_BINDING : 0x8CA6, // Same as FRAMEBUFFER_BINDING
  11524. READ_FRAMEBUFFER : 0x8CA8,
  11525. DRAW_FRAMEBUFFER : 0x8CA9,
  11526. READ_FRAMEBUFFER_BINDING : 0x8CAA,
  11527. RENDERBUFFER_SAMPLES : 0x8CAB,
  11528. FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER : 0x8CD4,
  11529. MAX_COLOR_ATTACHMENTS : 0x8CDF,
  11530. COLOR_ATTACHMENT1 : 0x8CE1,
  11531. COLOR_ATTACHMENT2 : 0x8CE2,
  11532. COLOR_ATTACHMENT3 : 0x8CE3,
  11533. COLOR_ATTACHMENT4 : 0x8CE4,
  11534. COLOR_ATTACHMENT5 : 0x8CE5,
  11535. COLOR_ATTACHMENT6 : 0x8CE6,
  11536. COLOR_ATTACHMENT7 : 0x8CE7,
  11537. COLOR_ATTACHMENT8 : 0x8CE8,
  11538. COLOR_ATTACHMENT9 : 0x8CE9,
  11539. COLOR_ATTACHMENT10 : 0x8CEA,
  11540. COLOR_ATTACHMENT11 : 0x8CEB,
  11541. COLOR_ATTACHMENT12 : 0x8CEC,
  11542. COLOR_ATTACHMENT13 : 0x8CED,
  11543. COLOR_ATTACHMENT14 : 0x8CEE,
  11544. COLOR_ATTACHMENT15 : 0x8CEF,
  11545. FRAMEBUFFER_INCOMPLETE_MULTISAMPLE : 0x8D56,
  11546. MAX_SAMPLES : 0x8D57,
  11547. HALF_FLOAT : 0x140B,
  11548. RG : 0x8227,
  11549. RG_INTEGER : 0x8228,
  11550. R8 : 0x8229,
  11551. RG8 : 0x822B,
  11552. R16F : 0x822D,
  11553. R32F : 0x822E,
  11554. RG16F : 0x822F,
  11555. RG32F : 0x8230,
  11556. R8I : 0x8231,
  11557. R8UI : 0x8232,
  11558. R16I : 0x8233,
  11559. R16UI : 0x8234,
  11560. R32I : 0x8235,
  11561. R32UI : 0x8236,
  11562. RG8I : 0x8237,
  11563. RG8UI : 0x8238,
  11564. RG16I : 0x8239,
  11565. RG16UI : 0x823A,
  11566. RG32I : 0x823B,
  11567. RG32UI : 0x823C,
  11568. VERTEX_ARRAY_BINDING : 0x85B5,
  11569. R8_SNORM : 0x8F94,
  11570. RG8_SNORM : 0x8F95,
  11571. RGB8_SNORM : 0x8F96,
  11572. RGBA8_SNORM : 0x8F97,
  11573. SIGNED_NORMALIZED : 0x8F9C,
  11574. COPY_READ_BUFFER : 0x8F36,
  11575. COPY_WRITE_BUFFER : 0x8F37,
  11576. COPY_READ_BUFFER_BINDING : 0x8F36, // Same as COPY_READ_BUFFER
  11577. COPY_WRITE_BUFFER_BINDING : 0x8F37, // Same as COPY_WRITE_BUFFER
  11578. UNIFORM_BUFFER : 0x8A11,
  11579. UNIFORM_BUFFER_BINDING : 0x8A28,
  11580. UNIFORM_BUFFER_START : 0x8A29,
  11581. UNIFORM_BUFFER_SIZE : 0x8A2A,
  11582. MAX_VERTEX_UNIFORM_BLOCKS : 0x8A2B,
  11583. MAX_FRAGMENT_UNIFORM_BLOCKS : 0x8A2D,
  11584. MAX_COMBINED_UNIFORM_BLOCKS : 0x8A2E,
  11585. MAX_UNIFORM_BUFFER_BINDINGS : 0x8A2F,
  11586. MAX_UNIFORM_BLOCK_SIZE : 0x8A30,
  11587. MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS : 0x8A31,
  11588. MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS : 0x8A33,
  11589. UNIFORM_BUFFER_OFFSET_ALIGNMENT : 0x8A34,
  11590. ACTIVE_UNIFORM_BLOCKS : 0x8A36,
  11591. UNIFORM_TYPE : 0x8A37,
  11592. UNIFORM_SIZE : 0x8A38,
  11593. UNIFORM_BLOCK_INDEX : 0x8A3A,
  11594. UNIFORM_OFFSET : 0x8A3B,
  11595. UNIFORM_ARRAY_STRIDE : 0x8A3C,
  11596. UNIFORM_MATRIX_STRIDE : 0x8A3D,
  11597. UNIFORM_IS_ROW_MAJOR : 0x8A3E,
  11598. UNIFORM_BLOCK_BINDING : 0x8A3F,
  11599. UNIFORM_BLOCK_DATA_SIZE : 0x8A40,
  11600. UNIFORM_BLOCK_ACTIVE_UNIFORMS : 0x8A42,
  11601. UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES : 0x8A43,
  11602. UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER : 0x8A44,
  11603. UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER : 0x8A46,
  11604. INVALID_INDEX : 0xFFFFFFFF,
  11605. MAX_VERTEX_OUTPUT_COMPONENTS : 0x9122,
  11606. MAX_FRAGMENT_INPUT_COMPONENTS : 0x9125,
  11607. MAX_SERVER_WAIT_TIMEOUT : 0x9111,
  11608. OBJECT_TYPE : 0x9112,
  11609. SYNC_CONDITION : 0x9113,
  11610. SYNC_STATUS : 0x9114,
  11611. SYNC_FLAGS : 0x9115,
  11612. SYNC_FENCE : 0x9116,
  11613. SYNC_GPU_COMMANDS_COMPLETE : 0x9117,
  11614. UNSIGNALED : 0x9118,
  11615. SIGNALED : 0x9119,
  11616. ALREADY_SIGNALED : 0x911A,
  11617. TIMEOUT_EXPIRED : 0x911B,
  11618. CONDITION_SATISFIED : 0x911C,
  11619. WAIT_FAILED : 0x911D,
  11620. SYNC_FLUSH_COMMANDS_BIT : 0x00000001,
  11621. VERTEX_ATTRIB_ARRAY_DIVISOR : 0x88FE,
  11622. ANY_SAMPLES_PASSED : 0x8C2F,
  11623. ANY_SAMPLES_PASSED_CONSERVATIVE : 0x8D6A,
  11624. SAMPLER_BINDING : 0x8919,
  11625. RGB10_A2UI : 0x906F,
  11626. INT_2_10_10_10_REV : 0x8D9F,
  11627. TRANSFORM_FEEDBACK : 0x8E22,
  11628. TRANSFORM_FEEDBACK_PAUSED : 0x8E23,
  11629. TRANSFORM_FEEDBACK_ACTIVE : 0x8E24,
  11630. TRANSFORM_FEEDBACK_BINDING : 0x8E25,
  11631. COMPRESSED_R11_EAC : 0x9270,
  11632. COMPRESSED_SIGNED_R11_EAC : 0x9271,
  11633. COMPRESSED_RG11_EAC : 0x9272,
  11634. COMPRESSED_SIGNED_RG11_EAC : 0x9273,
  11635. COMPRESSED_RGB8_ETC2 : 0x9274,
  11636. COMPRESSED_SRGB8_ETC2 : 0x9275,
  11637. COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 : 0x9276,
  11638. COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 : 0x9277,
  11639. COMPRESSED_RGBA8_ETC2_EAC : 0x9278,
  11640. COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : 0x9279,
  11641. TEXTURE_IMMUTABLE_FORMAT : 0x912F,
  11642. MAX_ELEMENT_INDEX : 0x8D6B,
  11643. TEXTURE_IMMUTABLE_LEVELS : 0x82DF
  11644. };
  11645. return freezeObject(WebGLConstants);
  11646. });
  11647. /*global define*/
  11648. define('Core/ComponentDatatype',[
  11649. './defaultValue',
  11650. './defined',
  11651. './DeveloperError',
  11652. './FeatureDetection',
  11653. './freezeObject',
  11654. './WebGLConstants'
  11655. ], function(
  11656. defaultValue,
  11657. defined,
  11658. DeveloperError,
  11659. FeatureDetection,
  11660. freezeObject,
  11661. WebGLConstants) {
  11662. 'use strict';
  11663. // Bail out if the browser doesn't support typed arrays, to prevent the setup function
  11664. // from failing, since we won't be able to create a WebGL context anyway.
  11665. if (!FeatureDetection.supportsTypedArrays()) {
  11666. return {};
  11667. }
  11668. /**
  11669. * WebGL component datatypes. Components are intrinsics,
  11670. * which form attributes, which form vertices.
  11671. *
  11672. * @exports ComponentDatatype
  11673. */
  11674. var ComponentDatatype = {
  11675. /**
  11676. * 8-bit signed byte corresponding to <code>gl.BYTE</code> and the type
  11677. * of an element in <code>Int8Array</code>.
  11678. *
  11679. * @type {Number}
  11680. * @constant
  11681. */
  11682. BYTE : WebGLConstants.BYTE,
  11683. /**
  11684. * 8-bit unsigned byte corresponding to <code>UNSIGNED_BYTE</code> and the type
  11685. * of an element in <code>Uint8Array</code>.
  11686. *
  11687. * @type {Number}
  11688. * @constant
  11689. */
  11690. UNSIGNED_BYTE : WebGLConstants.UNSIGNED_BYTE,
  11691. /**
  11692. * 16-bit signed short corresponding to <code>SHORT</code> and the type
  11693. * of an element in <code>Int16Array</code>.
  11694. *
  11695. * @type {Number}
  11696. * @constant
  11697. */
  11698. SHORT : WebGLConstants.SHORT,
  11699. /**
  11700. * 16-bit unsigned short corresponding to <code>UNSIGNED_SHORT</code> and the type
  11701. * of an element in <code>Uint16Array</code>.
  11702. *
  11703. * @type {Number}
  11704. * @constant
  11705. */
  11706. UNSIGNED_SHORT : WebGLConstants.UNSIGNED_SHORT,
  11707. /**
  11708. * 32-bit signed int corresponding to <code>INT</code> and the type
  11709. * of an element in <code>Int32Array</code>.
  11710. *
  11711. * @memberOf ComponentDatatype
  11712. *
  11713. * @type {Number}
  11714. * @constant
  11715. */
  11716. INT : WebGLConstants.INT,
  11717. /**
  11718. * 32-bit unsigned int corresponding to <code>UNSIGNED_INT</code> and the type
  11719. * of an element in <code>Uint32Array</code>.
  11720. *
  11721. * @memberOf ComponentDatatype
  11722. *
  11723. * @type {Number}
  11724. * @constant
  11725. */
  11726. UNSIGNED_INT : WebGLConstants.UNSIGNED_INT,
  11727. /**
  11728. * 32-bit floating-point corresponding to <code>FLOAT</code> and the type
  11729. * of an element in <code>Float32Array</code>.
  11730. *
  11731. * @type {Number}
  11732. * @constant
  11733. */
  11734. FLOAT : WebGLConstants.FLOAT,
  11735. /**
  11736. * 64-bit floating-point corresponding to <code>gl.DOUBLE</code> (in Desktop OpenGL;
  11737. * this is not supported in WebGL, and is emulated in Cesium via {@link GeometryPipeline.encodeAttribute})
  11738. * and the type of an element in <code>Float64Array</code>.
  11739. *
  11740. * @memberOf ComponentDatatype
  11741. *
  11742. * @type {Number}
  11743. * @constant
  11744. * @default 0x140A
  11745. */
  11746. DOUBLE : WebGLConstants.DOUBLE
  11747. };
  11748. /**
  11749. * Returns the size, in bytes, of the corresponding datatype.
  11750. *
  11751. * @param {ComponentDatatype} componentDatatype The component datatype to get the size of.
  11752. * @returns {Number} The size in bytes.
  11753. *
  11754. * @exception {DeveloperError} componentDatatype is not a valid value.
  11755. *
  11756. * @example
  11757. * // Returns Int8Array.BYTES_PER_ELEMENT
  11758. * var size = Cesium.ComponentDatatype.getSizeInBytes(Cesium.ComponentDatatype.BYTE);
  11759. */
  11760. ComponentDatatype.getSizeInBytes = function(componentDatatype){
  11761. if (!defined(componentDatatype)) {
  11762. throw new DeveloperError('value is required.');
  11763. }
  11764. switch (componentDatatype) {
  11765. case ComponentDatatype.BYTE:
  11766. return Int8Array.BYTES_PER_ELEMENT;
  11767. case ComponentDatatype.UNSIGNED_BYTE:
  11768. return Uint8Array.BYTES_PER_ELEMENT;
  11769. case ComponentDatatype.SHORT:
  11770. return Int16Array.BYTES_PER_ELEMENT;
  11771. case ComponentDatatype.UNSIGNED_SHORT:
  11772. return Uint16Array.BYTES_PER_ELEMENT;
  11773. case ComponentDatatype.INT:
  11774. return Int32Array.BYTES_PER_ELEMENT;
  11775. case ComponentDatatype.UNSIGNED_INT:
  11776. return Uint32Array.BYTES_PER_ELEMENT;
  11777. case ComponentDatatype.FLOAT:
  11778. return Float32Array.BYTES_PER_ELEMENT;
  11779. case ComponentDatatype.DOUBLE:
  11780. return Float64Array.BYTES_PER_ELEMENT;
  11781. default:
  11782. throw new DeveloperError('componentDatatype is not a valid value.');
  11783. }
  11784. };
  11785. /**
  11786. * Gets the {@link ComponentDatatype} for the provided TypedArray instance.
  11787. *
  11788. * @param {TypedArray} array The typed array.
  11789. * @returns {ComponentDatatype} The ComponentDatatype for the provided array, or undefined if the array is not a TypedArray.
  11790. */
  11791. ComponentDatatype.fromTypedArray = function(array) {
  11792. if (array instanceof Int8Array) {
  11793. return ComponentDatatype.BYTE;
  11794. }
  11795. if (array instanceof Uint8Array) {
  11796. return ComponentDatatype.UNSIGNED_BYTE;
  11797. }
  11798. if (array instanceof Int16Array) {
  11799. return ComponentDatatype.SHORT;
  11800. }
  11801. if (array instanceof Uint16Array) {
  11802. return ComponentDatatype.UNSIGNED_SHORT;
  11803. }
  11804. if (array instanceof Int32Array) {
  11805. return ComponentDatatype.INT;
  11806. }
  11807. if (array instanceof Uint32Array) {
  11808. return ComponentDatatype.UNSIGNED_INT;
  11809. }
  11810. if (array instanceof Float32Array) {
  11811. return ComponentDatatype.FLOAT;
  11812. }
  11813. if (array instanceof Float64Array) {
  11814. return ComponentDatatype.DOUBLE;
  11815. }
  11816. };
  11817. /**
  11818. * Validates that the provided component datatype is a valid {@link ComponentDatatype}
  11819. *
  11820. * @param {ComponentDatatype} componentDatatype The component datatype to validate.
  11821. * @returns {Boolean} <code>true</code> if the provided component datatype is a valid value; otherwise, <code>false</code>.
  11822. *
  11823. * @example
  11824. * if (!Cesium.ComponentDatatype.validate(componentDatatype)) {
  11825. * throw new Cesium.DeveloperError('componentDatatype must be a valid value.');
  11826. * }
  11827. */
  11828. ComponentDatatype.validate = function(componentDatatype) {
  11829. return defined(componentDatatype) &&
  11830. (componentDatatype === ComponentDatatype.BYTE ||
  11831. componentDatatype === ComponentDatatype.UNSIGNED_BYTE ||
  11832. componentDatatype === ComponentDatatype.SHORT ||
  11833. componentDatatype === ComponentDatatype.UNSIGNED_SHORT ||
  11834. componentDatatype === ComponentDatatype.INT ||
  11835. componentDatatype === ComponentDatatype.UNSIGNED_INT ||
  11836. componentDatatype === ComponentDatatype.FLOAT ||
  11837. componentDatatype === ComponentDatatype.DOUBLE);
  11838. };
  11839. /**
  11840. * Creates a typed array corresponding to component data type.
  11841. *
  11842. * @param {ComponentDatatype} componentDatatype The component data type.
  11843. * @param {Number|Array} valuesOrLength The length of the array to create or an array.
  11844. * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array.
  11845. *
  11846. * @exception {DeveloperError} componentDatatype is not a valid value.
  11847. *
  11848. * @example
  11849. * // creates a Float32Array with length of 100
  11850. * var typedArray = Cesium.ComponentDatatype.createTypedArray(Cesium.ComponentDatatype.FLOAT, 100);
  11851. */
  11852. ComponentDatatype.createTypedArray = function(componentDatatype, valuesOrLength) {
  11853. if (!defined(componentDatatype)) {
  11854. throw new DeveloperError('componentDatatype is required.');
  11855. }
  11856. if (!defined(valuesOrLength)) {
  11857. throw new DeveloperError('valuesOrLength is required.');
  11858. }
  11859. switch (componentDatatype) {
  11860. case ComponentDatatype.BYTE:
  11861. return new Int8Array(valuesOrLength);
  11862. case ComponentDatatype.UNSIGNED_BYTE:
  11863. return new Uint8Array(valuesOrLength);
  11864. case ComponentDatatype.SHORT:
  11865. return new Int16Array(valuesOrLength);
  11866. case ComponentDatatype.UNSIGNED_SHORT:
  11867. return new Uint16Array(valuesOrLength);
  11868. case ComponentDatatype.INT:
  11869. return new Int32Array(valuesOrLength);
  11870. case ComponentDatatype.UNSIGNED_INT:
  11871. return new Uint32Array(valuesOrLength);
  11872. case ComponentDatatype.FLOAT:
  11873. return new Float32Array(valuesOrLength);
  11874. case ComponentDatatype.DOUBLE:
  11875. return new Float64Array(valuesOrLength);
  11876. default:
  11877. throw new DeveloperError('componentDatatype is not a valid value.');
  11878. }
  11879. };
  11880. /**
  11881. * Creates a typed view of an array of bytes.
  11882. *
  11883. * @param {ComponentDatatype} componentDatatype The type of the view to create.
  11884. * @param {ArrayBuffer} buffer The buffer storage to use for the view.
  11885. * @param {Number} [byteOffset] The offset, in bytes, to the first element in the view.
  11886. * @param {Number} [length] The number of elements in the view.
  11887. * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array view of the buffer.
  11888. *
  11889. * @exception {DeveloperError} componentDatatype is not a valid value.
  11890. */
  11891. ComponentDatatype.createArrayBufferView = function(componentDatatype, buffer, byteOffset, length) {
  11892. if (!defined(componentDatatype)) {
  11893. throw new DeveloperError('componentDatatype is required.');
  11894. }
  11895. if (!defined(buffer)) {
  11896. throw new DeveloperError('buffer is required.');
  11897. }
  11898. byteOffset = defaultValue(byteOffset, 0);
  11899. length = defaultValue(length, (buffer.byteLength - byteOffset) / ComponentDatatype.getSizeInBytes(componentDatatype));
  11900. switch (componentDatatype) {
  11901. case ComponentDatatype.BYTE:
  11902. return new Int8Array(buffer, byteOffset, length);
  11903. case ComponentDatatype.UNSIGNED_BYTE:
  11904. return new Uint8Array(buffer, byteOffset, length);
  11905. case ComponentDatatype.SHORT:
  11906. return new Int16Array(buffer, byteOffset, length);
  11907. case ComponentDatatype.UNSIGNED_SHORT:
  11908. return new Uint16Array(buffer, byteOffset, length);
  11909. case ComponentDatatype.INT:
  11910. return new Int32Array(buffer, byteOffset, length);
  11911. case ComponentDatatype.UNSIGNED_INT:
  11912. return new Uint32Array(buffer, byteOffset, length);
  11913. case ComponentDatatype.FLOAT:
  11914. return new Float32Array(buffer, byteOffset, length);
  11915. case ComponentDatatype.DOUBLE:
  11916. return new Float64Array(buffer, byteOffset, length);
  11917. default:
  11918. throw new DeveloperError('componentDatatype is not a valid value.');
  11919. }
  11920. };
  11921. /**
  11922. * Get the ComponentDatatype from its name.
  11923. *
  11924. * @param {String} name The name of the ComponentDatatype.
  11925. * @returns {ComponentDatatype} The ComponentDatatype.
  11926. *
  11927. * @exception {DeveloperError} name is not a valid value.
  11928. */
  11929. ComponentDatatype.fromName = function(name) {
  11930. switch (name) {
  11931. case 'BYTE':
  11932. return ComponentDatatype.BYTE;
  11933. case 'UNSIGNED_BYTE':
  11934. return ComponentDatatype.UNSIGNED_BYTE;
  11935. case 'SHORT':
  11936. return ComponentDatatype.SHORT;
  11937. case 'UNSIGNED_SHORT':
  11938. return ComponentDatatype.UNSIGNED_SHORT;
  11939. case 'INT':
  11940. return ComponentDatatype.INT;
  11941. case 'UNSIGNED_INT':
  11942. return ComponentDatatype.UNSIGNED_INT;
  11943. case 'FLOAT':
  11944. return ComponentDatatype.FLOAT;
  11945. case 'DOUBLE':
  11946. return ComponentDatatype.DOUBLE;
  11947. default:
  11948. throw new DeveloperError('name is not a valid value.');
  11949. }
  11950. };
  11951. return freezeObject(ComponentDatatype);
  11952. });
  11953. /*global define*/
  11954. define('Core/GeometryType',[
  11955. './freezeObject'
  11956. ], function(
  11957. freezeObject) {
  11958. 'use strict';
  11959. /**
  11960. * @private
  11961. */
  11962. var GeometryType = {
  11963. NONE : 0,
  11964. TRIANGLES : 1,
  11965. LINES : 2,
  11966. POLYLINES : 3
  11967. };
  11968. return freezeObject(GeometryType);
  11969. });
  11970. /*global define*/
  11971. define('Core/PrimitiveType',[
  11972. './freezeObject',
  11973. './WebGLConstants'
  11974. ], function(
  11975. freezeObject,
  11976. WebGLConstants) {
  11977. 'use strict';
  11978. /**
  11979. * The type of a geometric primitive, i.e., points, lines, and triangles.
  11980. *
  11981. * @exports PrimitiveType
  11982. */
  11983. var PrimitiveType = {
  11984. /**
  11985. * Points primitive where each vertex (or index) is a separate point.
  11986. *
  11987. * @type {Number}
  11988. * @constant
  11989. */
  11990. POINTS : WebGLConstants.POINTS,
  11991. /**
  11992. * Lines primitive where each two vertices (or indices) is a line segment. Line segments are not necessarily connected.
  11993. *
  11994. * @type {Number}
  11995. * @constant
  11996. */
  11997. LINES : WebGLConstants.LINES,
  11998. /**
  11999. * Line loop primitive where each vertex (or index) after the first connects a line to
  12000. * the previous vertex, and the last vertex implicitly connects to the first.
  12001. *
  12002. * @type {Number}
  12003. * @constant
  12004. */
  12005. LINE_LOOP : WebGLConstants.LINE_LOOP,
  12006. /**
  12007. * Line strip primitive where each vertex (or index) after the first connects a line to the previous vertex.
  12008. *
  12009. * @type {Number}
  12010. * @constant
  12011. */
  12012. LINE_STRIP : WebGLConstants.LINE_STRIP,
  12013. /**
  12014. * Triangles primitive where each three vertices (or indices) is a triangle. Triangles do not necessarily share edges.
  12015. *
  12016. * @type {Number}
  12017. * @constant
  12018. */
  12019. TRIANGLES : WebGLConstants.TRIANGLES,
  12020. /**
  12021. * Triangle strip primitive where each vertex (or index) after the first two connect to
  12022. * the previous two vertices forming a triangle. For example, this can be used to model a wall.
  12023. *
  12024. * @type {Number}
  12025. * @constant
  12026. */
  12027. TRIANGLE_STRIP : WebGLConstants.TRIANGLE_STRIP,
  12028. /**
  12029. * Triangle fan primitive where each vertex (or index) after the first two connect to
  12030. * the previous vertex and the first vertex forming a triangle. For example, this can be used
  12031. * to model a cone or circle.
  12032. *
  12033. * @type {Number}
  12034. * @constant
  12035. */
  12036. TRIANGLE_FAN : WebGLConstants.TRIANGLE_FAN,
  12037. /**
  12038. * @private
  12039. */
  12040. validate : function(primitiveType) {
  12041. return primitiveType === PrimitiveType.POINTS ||
  12042. primitiveType === PrimitiveType.LINES ||
  12043. primitiveType === PrimitiveType.LINE_LOOP ||
  12044. primitiveType === PrimitiveType.LINE_STRIP ||
  12045. primitiveType === PrimitiveType.TRIANGLES ||
  12046. primitiveType === PrimitiveType.TRIANGLE_STRIP ||
  12047. primitiveType === PrimitiveType.TRIANGLE_FAN;
  12048. }
  12049. };
  12050. return freezeObject(PrimitiveType);
  12051. });
  12052. /*global define*/
  12053. define('Core/Geometry',[
  12054. './defaultValue',
  12055. './defined',
  12056. './DeveloperError',
  12057. './GeometryType',
  12058. './PrimitiveType'
  12059. ], function(
  12060. defaultValue,
  12061. defined,
  12062. DeveloperError,
  12063. GeometryType,
  12064. PrimitiveType) {
  12065. 'use strict';
  12066. /**
  12067. * A geometry representation with attributes forming vertices and optional index data
  12068. * defining primitives. Geometries and an {@link Appearance}, which describes the shading,
  12069. * can be assigned to a {@link Primitive} for visualization. A <code>Primitive</code> can
  12070. * be created from many heterogeneous - in many cases - geometries for performance.
  12071. * <p>
  12072. * Geometries can be transformed and optimized using functions in {@link GeometryPipeline}.
  12073. * </p>
  12074. *
  12075. * @alias Geometry
  12076. * @constructor
  12077. *
  12078. * @param {Object} options Object with the following properties:
  12079. * @param {GeometryAttributes} options.attributes Attributes, which make up the geometry's vertices.
  12080. * @param {PrimitiveType} [options.primitiveType=PrimitiveType.TRIANGLES] The type of primitives in the geometry.
  12081. * @param {Uint16Array|Uint32Array} [options.indices] Optional index data that determines the primitives in the geometry.
  12082. * @param {BoundingSphere} [options.boundingSphere] An optional bounding sphere that fully enclosed the geometry.
  12083. *
  12084. * @see PolygonGeometry
  12085. * @see RectangleGeometry
  12086. * @see EllipseGeometry
  12087. * @see CircleGeometry
  12088. * @see WallGeometry
  12089. * @see SimplePolylineGeometry
  12090. * @see BoxGeometry
  12091. * @see EllipsoidGeometry
  12092. *
  12093. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Geometry%20and%20Appearances.html|Geometry and Appearances Demo}
  12094. *
  12095. * @example
  12096. * // Create geometry with a position attribute and indexed lines.
  12097. * var positions = new Float64Array([
  12098. * 0.0, 0.0, 0.0,
  12099. * 7500000.0, 0.0, 0.0,
  12100. * 0.0, 7500000.0, 0.0
  12101. * ]);
  12102. *
  12103. * var geometry = new Cesium.Geometry({
  12104. * attributes : {
  12105. * position : new Cesium.GeometryAttribute({
  12106. * componentDatatype : Cesium.ComponentDatatype.DOUBLE,
  12107. * componentsPerAttribute : 3,
  12108. * values : positions
  12109. * })
  12110. * },
  12111. * indices : new Uint16Array([0, 1, 1, 2, 2, 0]),
  12112. * primitiveType : Cesium.PrimitiveType.LINES,
  12113. * boundingSphere : Cesium.BoundingSphere.fromVertices(positions)
  12114. * });
  12115. */
  12116. function Geometry(options) {
  12117. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  12118. if (!defined(options.attributes)) {
  12119. throw new DeveloperError('options.attributes is required.');
  12120. }
  12121. /**
  12122. * Attributes, which make up the geometry's vertices. Each property in this object corresponds to a
  12123. * {@link GeometryAttribute} containing the attribute's data.
  12124. * <p>
  12125. * Attributes are always stored non-interleaved in a Geometry.
  12126. * </p>
  12127. * <p>
  12128. * There are reserved attribute names with well-known semantics. The following attributes
  12129. * are created by a Geometry (depending on the provided {@link VertexFormat}.
  12130. * <ul>
  12131. * <li><code>position</code> - 3D vertex position. 64-bit floating-point (for precision). 3 components per attribute. See {@link VertexFormat#position}.</li>
  12132. * <li><code>normal</code> - Normal (normalized), commonly used for lighting. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#normal}.</li>
  12133. * <li><code>st</code> - 2D texture coordinate. 32-bit floating-point. 2 components per attribute. See {@link VertexFormat#st}.</li>
  12134. * <li><code>binormal</code> - Binormal (normalized), used for tangent-space effects like bump mapping. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#binormal}.</li>
  12135. * <li><code>tangent</code> - Tangent (normalized), used for tangent-space effects like bump mapping. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#tangent}.</li>
  12136. * </ul>
  12137. * </p>
  12138. * <p>
  12139. * The following attribute names are generally not created by a Geometry, but are added
  12140. * to a Geometry by a {@link Primitive} or {@link GeometryPipeline} functions to prepare
  12141. * the geometry for rendering.
  12142. * <ul>
  12143. * <li><code>position3DHigh</code> - High 32 bits for encoded 64-bit position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  12144. * <li><code>position3DLow</code> - Low 32 bits for encoded 64-bit position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  12145. * <li><code>position3DHigh</code> - High 32 bits for encoded 64-bit 2D (Columbus view) position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  12146. * <li><code>position2DLow</code> - Low 32 bits for encoded 64-bit 2D (Columbus view) position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  12147. * <li><code>color</code> - RGBA color (normalized) usually from {@link GeometryInstance#color}. 32-bit floating-point. 4 components per attribute.</li>
  12148. * <li><code>pickColor</code> - RGBA color used for picking. 32-bit floating-point. 4 components per attribute.</li>
  12149. * </ul>
  12150. * </p>
  12151. *
  12152. * @type GeometryAttributes
  12153. *
  12154. * @default undefined
  12155. *
  12156. *
  12157. * @example
  12158. * geometry.attributes.position = new Cesium.GeometryAttribute({
  12159. * componentDatatype : Cesium.ComponentDatatype.FLOAT,
  12160. * componentsPerAttribute : 3,
  12161. * values : new Float32Array(0)
  12162. * });
  12163. *
  12164. * @see GeometryAttribute
  12165. * @see VertexFormat
  12166. */
  12167. this.attributes = options.attributes;
  12168. /**
  12169. * Optional index data that - along with {@link Geometry#primitiveType} -
  12170. * determines the primitives in the geometry.
  12171. *
  12172. * @type Array
  12173. *
  12174. * @default undefined
  12175. */
  12176. this.indices = options.indices;
  12177. /**
  12178. * The type of primitives in the geometry. This is most often {@link PrimitiveType.TRIANGLES},
  12179. * but can varying based on the specific geometry.
  12180. *
  12181. * @type PrimitiveType
  12182. *
  12183. * @default undefined
  12184. */
  12185. this.primitiveType = defaultValue(options.primitiveType, PrimitiveType.TRIANGLES);
  12186. /**
  12187. * An optional bounding sphere that fully encloses the geometry. This is
  12188. * commonly used for culling.
  12189. *
  12190. * @type BoundingSphere
  12191. *
  12192. * @default undefined
  12193. */
  12194. this.boundingSphere = options.boundingSphere;
  12195. /**
  12196. * @private
  12197. */
  12198. this.geometryType = defaultValue(options.geometryType, GeometryType.NONE);
  12199. /**
  12200. * @private
  12201. */
  12202. this.boundingSphereCV = options.boundingSphereCV;
  12203. }
  12204. /**
  12205. * Computes the number of vertices in a geometry. The runtime is linear with
  12206. * respect to the number of attributes in a vertex, not the number of vertices.
  12207. *
  12208. * @param {Geometry} geometry The geometry.
  12209. * @returns {Number} The number of vertices in the geometry.
  12210. *
  12211. * @example
  12212. * var numVertices = Cesium.Geometry.computeNumberOfVertices(geometry);
  12213. */
  12214. Geometry.computeNumberOfVertices = function(geometry) {
  12215. if (!defined(geometry)) {
  12216. throw new DeveloperError('geometry is required.');
  12217. }
  12218. var numberOfVertices = -1;
  12219. for ( var property in geometry.attributes) {
  12220. if (geometry.attributes.hasOwnProperty(property) &&
  12221. defined(geometry.attributes[property]) &&
  12222. defined(geometry.attributes[property].values)) {
  12223. var attribute = geometry.attributes[property];
  12224. var num = attribute.values.length / attribute.componentsPerAttribute;
  12225. if ((numberOfVertices !== num) && (numberOfVertices !== -1)) {
  12226. throw new DeveloperError('All attribute lists must have the same number of attributes.');
  12227. }
  12228. numberOfVertices = num;
  12229. }
  12230. }
  12231. return numberOfVertices;
  12232. };
  12233. return Geometry;
  12234. });
  12235. /*global define*/
  12236. define('Core/GeometryAttribute',[
  12237. './defaultValue',
  12238. './defined',
  12239. './DeveloperError'
  12240. ], function(
  12241. defaultValue,
  12242. defined,
  12243. DeveloperError) {
  12244. 'use strict';
  12245. /**
  12246. * Values and type information for geometry attributes. A {@link Geometry}
  12247. * generally contains one or more attributes. All attributes together form
  12248. * the geometry's vertices.
  12249. *
  12250. * @alias GeometryAttribute
  12251. * @constructor
  12252. *
  12253. * @param {Object} [options] Object with the following properties:
  12254. * @param {ComponentDatatype} [options.componentDatatype] The datatype of each component in the attribute, e.g., individual elements in values.
  12255. * @param {Number} [options.componentsPerAttribute] A number between 1 and 4 that defines the number of components in an attributes.
  12256. * @param {Boolean} [options.normalize=false] When <code>true</code> and <code>componentDatatype</code> is an integer format, indicate that the components should be mapped to the range [0, 1] (unsigned) or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  12257. * @param {TypedArray} [options.values] The values for the attributes stored in a typed array.
  12258. *
  12259. * @exception {DeveloperError} options.componentsPerAttribute must be between 1 and 4.
  12260. *
  12261. *
  12262. * @example
  12263. * var geometry = new Cesium.Geometry({
  12264. * attributes : {
  12265. * position : new Cesium.GeometryAttribute({
  12266. * componentDatatype : Cesium.ComponentDatatype.FLOAT,
  12267. * componentsPerAttribute : 3,
  12268. * values : new Float32Array([
  12269. * 0.0, 0.0, 0.0,
  12270. * 7500000.0, 0.0, 0.0,
  12271. * 0.0, 7500000.0, 0.0
  12272. * ])
  12273. * })
  12274. * },
  12275. * primitiveType : Cesium.PrimitiveType.LINE_LOOP
  12276. * });
  12277. *
  12278. * @see Geometry
  12279. */
  12280. function GeometryAttribute(options) {
  12281. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  12282. if (!defined(options.componentDatatype)) {
  12283. throw new DeveloperError('options.componentDatatype is required.');
  12284. }
  12285. if (!defined(options.componentsPerAttribute)) {
  12286. throw new DeveloperError('options.componentsPerAttribute is required.');
  12287. }
  12288. if (options.componentsPerAttribute < 1 || options.componentsPerAttribute > 4) {
  12289. throw new DeveloperError('options.componentsPerAttribute must be between 1 and 4.');
  12290. }
  12291. if (!defined(options.values)) {
  12292. throw new DeveloperError('options.values is required.');
  12293. }
  12294. /**
  12295. * The datatype of each component in the attribute, e.g., individual elements in
  12296. * {@link GeometryAttribute#values}.
  12297. *
  12298. * @type ComponentDatatype
  12299. *
  12300. * @default undefined
  12301. */
  12302. this.componentDatatype = options.componentDatatype;
  12303. /**
  12304. * A number between 1 and 4 that defines the number of components in an attributes.
  12305. * For example, a position attribute with x, y, and z components would have 3 as
  12306. * shown in the code example.
  12307. *
  12308. * @type Number
  12309. *
  12310. * @default undefined
  12311. *
  12312. * @example
  12313. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  12314. * attribute.componentsPerAttribute = 3;
  12315. * attribute.values = new Float32Array([
  12316. * 0.0, 0.0, 0.0,
  12317. * 7500000.0, 0.0, 0.0,
  12318. * 0.0, 7500000.0, 0.0
  12319. * ]);
  12320. */
  12321. this.componentsPerAttribute = options.componentsPerAttribute;
  12322. /**
  12323. * When <code>true</code> and <code>componentDatatype</code> is an integer format,
  12324. * indicate that the components should be mapped to the range [0, 1] (unsigned)
  12325. * or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  12326. * <p>
  12327. * This is commonly used when storing colors using {@link ComponentDatatype.UNSIGNED_BYTE}.
  12328. * </p>
  12329. *
  12330. * @type Boolean
  12331. *
  12332. * @default false
  12333. *
  12334. * @example
  12335. * attribute.componentDatatype = Cesium.ComponentDatatype.UNSIGNED_BYTE;
  12336. * attribute.componentsPerAttribute = 4;
  12337. * attribute.normalize = true;
  12338. * attribute.values = new Uint8Array([
  12339. * Cesium.Color.floatToByte(color.red),
  12340. * Cesium.Color.floatToByte(color.green),
  12341. * Cesium.Color.floatToByte(color.blue),
  12342. * Cesium.Color.floatToByte(color.alpha)
  12343. * ]);
  12344. */
  12345. this.normalize = defaultValue(options.normalize, false);
  12346. /**
  12347. * The values for the attributes stored in a typed array. In the code example,
  12348. * every three elements in <code>values</code> defines one attributes since
  12349. * <code>componentsPerAttribute</code> is 3.
  12350. *
  12351. * @type TypedArray
  12352. *
  12353. * @default undefined
  12354. *
  12355. * @example
  12356. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  12357. * attribute.componentsPerAttribute = 3;
  12358. * attribute.values = new Float32Array([
  12359. * 0.0, 0.0, 0.0,
  12360. * 7500000.0, 0.0, 0.0,
  12361. * 0.0, 7500000.0, 0.0
  12362. * ]);
  12363. */
  12364. this.values = options.values;
  12365. }
  12366. return GeometryAttribute;
  12367. });
  12368. /*global define*/
  12369. define('Core/GeometryAttributes',[
  12370. './defaultValue'
  12371. ], function(
  12372. defaultValue) {
  12373. 'use strict';
  12374. /**
  12375. * Attributes, which make up a geometry's vertices. Each property in this object corresponds to a
  12376. * {@link GeometryAttribute} containing the attribute's data.
  12377. * <p>
  12378. * Attributes are always stored non-interleaved in a Geometry.
  12379. * </p>
  12380. *
  12381. * @alias GeometryAttributes
  12382. * @constructor
  12383. */
  12384. function GeometryAttributes(options) {
  12385. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  12386. /**
  12387. * The 3D position attribute.
  12388. * <p>
  12389. * 64-bit floating-point (for precision). 3 components per attribute.
  12390. * </p>
  12391. *
  12392. * @type GeometryAttribute
  12393. *
  12394. * @default undefined
  12395. */
  12396. this.position = options.position;
  12397. /**
  12398. * The normal attribute (normalized), which is commonly used for lighting.
  12399. * <p>
  12400. * 32-bit floating-point. 3 components per attribute.
  12401. * </p>
  12402. *
  12403. * @type GeometryAttribute
  12404. *
  12405. * @default undefined
  12406. */
  12407. this.normal = options.normal;
  12408. /**
  12409. * The 2D texture coordinate attribute.
  12410. * <p>
  12411. * 32-bit floating-point. 2 components per attribute
  12412. * </p>
  12413. *
  12414. * @type GeometryAttribute
  12415. *
  12416. * @default undefined
  12417. */
  12418. this.st = options.st;
  12419. /**
  12420. * The binormal attribute (normalized), which is used for tangent-space effects like bump mapping.
  12421. * <p>
  12422. * 32-bit floating-point. 3 components per attribute.
  12423. * </p>
  12424. *
  12425. * @type GeometryAttribute
  12426. *
  12427. * @default undefined
  12428. */
  12429. this.binormal = options.binormal;
  12430. /**
  12431. * The tangent attribute (normalized), which is used for tangent-space effects like bump mapping.
  12432. * <p>
  12433. * 32-bit floating-point. 3 components per attribute.
  12434. * </p>
  12435. *
  12436. * @type GeometryAttribute
  12437. *
  12438. * @default undefined
  12439. */
  12440. this.tangent = options.tangent;
  12441. /**
  12442. * The color attribute.
  12443. * <p>
  12444. * 8-bit unsigned integer. 4 components per attribute.
  12445. * </p>
  12446. *
  12447. * @type GeometryAttribute
  12448. *
  12449. * @default undefined
  12450. */
  12451. this.color = options.color;
  12452. }
  12453. return GeometryAttributes;
  12454. });
  12455. /*global define*/
  12456. define('Core/GeometryInstance',[
  12457. './defaultValue',
  12458. './defined',
  12459. './DeveloperError',
  12460. './Matrix4'
  12461. ], function(
  12462. defaultValue,
  12463. defined,
  12464. DeveloperError,
  12465. Matrix4) {
  12466. 'use strict';
  12467. /**
  12468. * Geometry instancing allows one {@link Geometry} object to be positions in several
  12469. * different locations and colored uniquely. For example, one {@link BoxGeometry} can
  12470. * be instanced several times, each with a different <code>modelMatrix</code> to change
  12471. * its position, rotation, and scale.
  12472. *
  12473. * @alias GeometryInstance
  12474. * @constructor
  12475. *
  12476. * @param {Object} options Object with the following properties:
  12477. * @param {Geometry} options.geometry The geometry to instance.
  12478. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The model matrix that transforms to transform the geometry from model to world coordinates.
  12479. * @param {Object} [options.id] A user-defined object to return when the instance is picked with {@link Scene#pick} or get/set per-instance attributes with {@link Primitive#getGeometryInstanceAttributes}.
  12480. * @param {Object} [options.attributes] Per-instance attributes like a show or color attribute shown in the example below.
  12481. *
  12482. *
  12483. * @example
  12484. * // Create geometry for a box, and two instances that refer to it.
  12485. * // One instance positions the box on the bottom and colored aqua.
  12486. * // The other instance positions the box on the top and color white.
  12487. * var geometry = Cesium.BoxGeometry.fromDimensions({
  12488. * vertexFormat : Cesium.VertexFormat.POSITION_AND_NORMAL,
  12489. * dimensions : new Cesium.Cartesian3(1000000.0, 1000000.0, 500000.0)
  12490. * });
  12491. * var instanceBottom = new Cesium.GeometryInstance({
  12492. * geometry : geometry,
  12493. * modelMatrix : Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
  12494. * Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883)), new Cesium.Cartesian3(0.0, 0.0, 1000000.0), new Cesium.Matrix4()),
  12495. * attributes : {
  12496. * color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.AQUA)
  12497. * },
  12498. * id : 'bottom'
  12499. * });
  12500. * var instanceTop = new Cesium.GeometryInstance({
  12501. * geometry : geometry,
  12502. * modelMatrix : Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
  12503. * Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883)), new Cesium.Cartesian3(0.0, 0.0, 3000000.0), new Cesium.Matrix4()),
  12504. * attributes : {
  12505. * color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.AQUA)
  12506. * },
  12507. * id : 'top'
  12508. * });
  12509. *
  12510. * @see Geometry
  12511. */
  12512. function GeometryInstance(options) {
  12513. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  12514. if (!defined(options.geometry)) {
  12515. throw new DeveloperError('options.geometry is required.');
  12516. }
  12517. /**
  12518. * The geometry being instanced.
  12519. *
  12520. * @type Geometry
  12521. *
  12522. * @default undefined
  12523. */
  12524. this.geometry = options.geometry;
  12525. /**
  12526. * The 4x4 transformation matrix that transforms the geometry from model to world coordinates.
  12527. * When this is the identity matrix, the geometry is drawn in world coordinates, i.e., Earth's WGS84 coordinates.
  12528. * Local reference frames can be used by providing a different transformation matrix, like that returned
  12529. * by {@link Transforms.eastNorthUpToFixedFrame}.
  12530. *
  12531. * @type Matrix4
  12532. *
  12533. * @default Matrix4.IDENTITY
  12534. */
  12535. this.modelMatrix = Matrix4.clone(defaultValue(options.modelMatrix, Matrix4.IDENTITY));
  12536. /**
  12537. * User-defined object returned when the instance is picked or used to get/set per-instance attributes.
  12538. *
  12539. * @type Object
  12540. *
  12541. * @default undefined
  12542. *
  12543. * @see Scene#pick
  12544. * @see Primitive#getGeometryInstanceAttributes
  12545. */
  12546. this.id = options.id;
  12547. /**
  12548. * Used for picking primitives that wrap geometry instances.
  12549. *
  12550. * @private
  12551. */
  12552. this.pickPrimitive = options.pickPrimitive;
  12553. /**
  12554. * Per-instance attributes like {@link ColorGeometryInstanceAttribute} or {@link ShowGeometryInstanceAttribute}.
  12555. * {@link Geometry} attributes varying per vertex; these attributes are constant for the entire instance.
  12556. *
  12557. * @type Object
  12558. *
  12559. * @default undefined
  12560. */
  12561. this.attributes = defaultValue(options.attributes, {});
  12562. /**
  12563. * @private
  12564. */
  12565. this.westHemisphereGeometry = undefined;
  12566. /**
  12567. * @private
  12568. */
  12569. this.eastHemisphereGeometry = undefined;
  12570. }
  12571. return GeometryInstance;
  12572. });
  12573. /*global define*/
  12574. define('Core/AttributeCompression',[
  12575. './Cartesian2',
  12576. './Cartesian3',
  12577. './defined',
  12578. './DeveloperError',
  12579. './Math'
  12580. ], function(
  12581. Cartesian2,
  12582. Cartesian3,
  12583. defined,
  12584. DeveloperError,
  12585. CesiumMath) {
  12586. 'use strict';
  12587. /**
  12588. * Attribute compression and decompression functions.
  12589. *
  12590. * @exports AttributeCompression
  12591. *
  12592. * @private
  12593. */
  12594. var AttributeCompression = {};
  12595. /**
  12596. * Encodes a normalized vector into 2 SNORM values in the range of [0-rangeMax] following the 'oct' encoding.
  12597. *
  12598. * Oct encoding is a compact representation of unit length vectors.
  12599. * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors",
  12600. * Cigolle et al 2014: {@link http://jcgt.org/published/0003/02/01/}
  12601. *
  12602. * @param {Cartesian3} vector The normalized vector to be compressed into 2 component 'oct' encoding.
  12603. * @param {Cartesian2} result The 2 component oct-encoded unit length vector.
  12604. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  12605. * @returns {Cartesian2} The 2 component oct-encoded unit length vector.
  12606. *
  12607. * @exception {DeveloperError} vector must be normalized.
  12608. *
  12609. * @see AttributeCompression.octDecodeInRange
  12610. */
  12611. AttributeCompression.octEncodeInRange = function(vector, rangeMax, result) {
  12612. if (!defined(vector)) {
  12613. throw new DeveloperError('vector is required.');
  12614. }
  12615. if (!defined(result)) {
  12616. throw new DeveloperError('result is required.');
  12617. }
  12618. var magSquared = Cartesian3.magnitudeSquared(vector);
  12619. if (Math.abs(magSquared - 1.0) > CesiumMath.EPSILON6) {
  12620. throw new DeveloperError('vector must be normalized.');
  12621. }
  12622. result.x = vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  12623. result.y = vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  12624. if (vector.z < 0) {
  12625. var x = result.x;
  12626. var y = result.y;
  12627. result.x = (1.0 - Math.abs(y)) * CesiumMath.signNotZero(x);
  12628. result.y = (1.0 - Math.abs(x)) * CesiumMath.signNotZero(y);
  12629. }
  12630. result.x = CesiumMath.toSNorm(result.x, rangeMax);
  12631. result.y = CesiumMath.toSNorm(result.y, rangeMax);
  12632. return result;
  12633. };
  12634. /**
  12635. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding.
  12636. *
  12637. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  12638. * @param {Cartesian2} result The 2 byte oct-encoded unit length vector.
  12639. * @returns {Cartesian2} The 2 byte oct-encoded unit length vector.
  12640. *
  12641. * @exception {DeveloperError} vector must be normalized.
  12642. *
  12643. * @see AttributeCompression.octEncodeInRange
  12644. * @see AttributeCompression.octDecode
  12645. */
  12646. AttributeCompression.octEncode = function(vector, result) {
  12647. return AttributeCompression.octEncodeInRange(vector, 255, result);
  12648. };
  12649. /**
  12650. * Decodes a unit-length vector in 'oct' encoding to a normalized 3-component vector.
  12651. *
  12652. * @param {Number} x The x component of the oct-encoded unit length vector.
  12653. * @param {Number} y The y component of the oct-encoded unit length vector.
  12654. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  12655. * @param {Cartesian3} result The decoded and normalized vector
  12656. * @returns {Cartesian3} The decoded and normalized vector.
  12657. *
  12658. * @exception {DeveloperError} x and y must be an unsigned normalized integer between 0 and rangeMax.
  12659. *
  12660. * @see AttributeCompression.octEncodeInRange
  12661. */
  12662. AttributeCompression.octDecodeInRange = function(x, y, rangeMax, result) {
  12663. if (!defined(result)) {
  12664. throw new DeveloperError('result is required.');
  12665. }
  12666. if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) {
  12667. throw new DeveloperError('x and y must be a signed normalized integer between 0 and ' + rangeMax);
  12668. }
  12669. result.x = CesiumMath.fromSNorm(x, rangeMax);
  12670. result.y = CesiumMath.fromSNorm(y, rangeMax);
  12671. result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y));
  12672. if (result.z < 0.0)
  12673. {
  12674. var oldVX = result.x;
  12675. result.x = (1.0 - Math.abs(result.y)) * CesiumMath.signNotZero(oldVX);
  12676. result.y = (1.0 - Math.abs(oldVX)) * CesiumMath.signNotZero(result.y);
  12677. }
  12678. return Cartesian3.normalize(result, result);
  12679. };
  12680. /**
  12681. * Decodes a unit-length vector in 2 byte 'oct' encoding to a normalized 3-component vector.
  12682. *
  12683. * @param {Number} x The x component of the oct-encoded unit length vector.
  12684. * @param {Number} y The y component of the oct-encoded unit length vector.
  12685. * @param {Cartesian3} result The decoded and normalized vector.
  12686. * @returns {Cartesian3} The decoded and normalized vector.
  12687. *
  12688. * @exception {DeveloperError} x and y must be an unsigned normalized integer between 0 and 255.
  12689. *
  12690. * @see AttributeCompression.octDecodeInRange
  12691. */
  12692. AttributeCompression.octDecode = function(x, y, result) {
  12693. return AttributeCompression.octDecodeInRange(x, y, 255, result);
  12694. };
  12695. /**
  12696. * Packs an oct encoded vector into a single floating-point number.
  12697. *
  12698. * @param {Cartesian2} encoded The oct encoded vector.
  12699. * @returns {Number} The oct encoded vector packed into a single float.
  12700. *
  12701. */
  12702. AttributeCompression.octPackFloat = function(encoded) {
  12703. if (!defined(encoded)) {
  12704. throw new DeveloperError('encoded is required.');
  12705. }
  12706. return 256.0 * encoded.x + encoded.y;
  12707. };
  12708. var scratchEncodeCart2 = new Cartesian2();
  12709. /**
  12710. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding and
  12711. * stores those values in a single float-point number.
  12712. *
  12713. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  12714. * @returns {Number} The 2 byte oct-encoded unit length vector.
  12715. *
  12716. * @exception {DeveloperError} vector must be normalized.
  12717. */
  12718. AttributeCompression.octEncodeFloat = function(vector) {
  12719. AttributeCompression.octEncode(vector, scratchEncodeCart2);
  12720. return AttributeCompression.octPackFloat(scratchEncodeCart2);
  12721. };
  12722. /**
  12723. * Decodes a unit-length vector in 'oct' encoding packed in a floating-point number to a normalized 3-component vector.
  12724. *
  12725. * @param {Number} value The oct-encoded unit length vector stored as a single floating-point number.
  12726. * @param {Cartesian3} result The decoded and normalized vector
  12727. * @returns {Cartesian3} The decoded and normalized vector.
  12728. *
  12729. */
  12730. AttributeCompression.octDecodeFloat = function(value, result) {
  12731. if (!defined(value)) {
  12732. throw new DeveloperError('value is required.');
  12733. }
  12734. var temp = value / 256.0;
  12735. var x = Math.floor(temp);
  12736. var y = (temp - x) * 256.0;
  12737. return AttributeCompression.octDecode(x, y, result);
  12738. };
  12739. /**
  12740. * Encodes three normalized vectors into 6 SNORM values in the range of [0-255] following the 'oct' encoding and
  12741. * packs those into two floating-point numbers.
  12742. *
  12743. * @param {Cartesian3} v1 A normalized vector to be compressed.
  12744. * @param {Cartesian3} v2 A normalized vector to be compressed.
  12745. * @param {Cartesian3} v3 A normalized vector to be compressed.
  12746. * @param {Cartesian2} result The 'oct' encoded vectors packed into two floating-point numbers.
  12747. * @returns {Cartesian2} The 'oct' encoded vectors packed into two floating-point numbers.
  12748. *
  12749. */
  12750. AttributeCompression.octPack = function(v1, v2, v3, result) {
  12751. if (!defined(v1)) {
  12752. throw new DeveloperError('v1 is required.');
  12753. }
  12754. if (!defined(v2)) {
  12755. throw new DeveloperError('v2 is required.');
  12756. }
  12757. if (!defined(v3)) {
  12758. throw new DeveloperError('v3 is required.');
  12759. }
  12760. if (!defined(result)) {
  12761. throw new DeveloperError('result is required.');
  12762. }
  12763. var encoded1 = AttributeCompression.octEncodeFloat(v1);
  12764. var encoded2 = AttributeCompression.octEncodeFloat(v2);
  12765. var encoded3 = AttributeCompression.octEncode(v3, scratchEncodeCart2);
  12766. result.x = 65536.0 * encoded3.x + encoded1;
  12767. result.y = 65536.0 * encoded3.y + encoded2;
  12768. return result;
  12769. };
  12770. /**
  12771. * Decodes three unit-length vectors in 'oct' encoding packed into a floating-point number to a normalized 3-component vector.
  12772. *
  12773. * @param {Cartesian2} packed The three oct-encoded unit length vectors stored as two floating-point number.
  12774. * @param {Cartesian3} v1 One decoded and normalized vector.
  12775. * @param {Cartesian3} v2 One decoded and normalized vector.
  12776. * @param {Cartesian3} v3 One decoded and normalized vector.
  12777. */
  12778. AttributeCompression.octUnpack = function(packed, v1, v2, v3) {
  12779. if (!defined(packed)) {
  12780. throw new DeveloperError('packed is required.');
  12781. }
  12782. if (!defined(v1)) {
  12783. throw new DeveloperError('v1 is required.');
  12784. }
  12785. if (!defined(v2)) {
  12786. throw new DeveloperError('v2 is required.');
  12787. }
  12788. if (!defined(v3)) {
  12789. throw new DeveloperError('v3 is required.');
  12790. }
  12791. var temp = packed.x / 65536.0;
  12792. var x = Math.floor(temp);
  12793. var encodedFloat1 = (temp - x) * 65536.0;
  12794. temp = packed.y / 65536.0;
  12795. var y = Math.floor(temp);
  12796. var encodedFloat2 = (temp - y) * 65536.0;
  12797. AttributeCompression.octDecodeFloat(encodedFloat1, v1);
  12798. AttributeCompression.octDecodeFloat(encodedFloat2, v2);
  12799. AttributeCompression.octDecode(x, y, v3);
  12800. };
  12801. /**
  12802. * Pack texture coordinates into a single float. The texture coordinates will only preserve 12 bits of precision.
  12803. *
  12804. * @param {Cartesian2} textureCoordinates The texture coordinates to compress. Both coordinates must be in the range 0.0-1.0.
  12805. * @returns {Number} The packed texture coordinates.
  12806. *
  12807. */
  12808. AttributeCompression.compressTextureCoordinates = function(textureCoordinates) {
  12809. if (!defined(textureCoordinates)) {
  12810. throw new DeveloperError('textureCoordinates is required.');
  12811. }
  12812. // Move x and y to the range 0-4095;
  12813. var x = (textureCoordinates.x * 4095.0) | 0;
  12814. var y = (textureCoordinates.y * 4095.0) | 0;
  12815. return 4096.0 * x + y;
  12816. };
  12817. /**
  12818. * Decompresses texture coordinates that were packed into a single float.
  12819. *
  12820. * @param {Number} compressed The compressed texture coordinates.
  12821. * @param {Cartesian2} result The decompressed texture coordinates.
  12822. * @returns {Cartesian2} The modified result parameter.
  12823. *
  12824. */
  12825. AttributeCompression.decompressTextureCoordinates = function(compressed, result) {
  12826. if (!defined(compressed)) {
  12827. throw new DeveloperError('compressed is required.');
  12828. }
  12829. if (!defined(result)) {
  12830. throw new DeveloperError('result is required.');
  12831. }
  12832. var temp = compressed / 4096.0;
  12833. var xZeroTo4095 = Math.floor(temp);
  12834. result.x = xZeroTo4095 / 4095.0;
  12835. result.y = (compressed - xZeroTo4095 * 4096) / 4095;
  12836. return result;
  12837. };
  12838. return AttributeCompression;
  12839. });
  12840. /*global define*/
  12841. define('Core/barycentricCoordinates',[
  12842. './Cartesian2',
  12843. './Cartesian3',
  12844. './defined',
  12845. './DeveloperError'
  12846. ], function(
  12847. Cartesian2,
  12848. Cartesian3,
  12849. defined,
  12850. DeveloperError) {
  12851. 'use strict';
  12852. var scratchCartesian1 = new Cartesian3();
  12853. var scratchCartesian2 = new Cartesian3();
  12854. var scratchCartesian3 = new Cartesian3();
  12855. /**
  12856. * Computes the barycentric coordinates for a point with respect to a triangle.
  12857. *
  12858. * @exports barycentricCoordinates
  12859. *
  12860. * @param {Cartesian2|Cartesian3} point The point to test.
  12861. * @param {Cartesian2|Cartesian3} p0 The first point of the triangle, corresponding to the barycentric x-axis.
  12862. * @param {Cartesian2|Cartesian3} p1 The second point of the triangle, corresponding to the barycentric y-axis.
  12863. * @param {Cartesian2|Cartesian3} p2 The third point of the triangle, corresponding to the barycentric z-axis.
  12864. * @param {Cartesian3} [result] The object onto which to store the result.
  12865. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  12866. *
  12867. * @example
  12868. * // Returns Cartesian3.UNIT_X
  12869. * var p = new Cesium.Cartesian3(-1.0, 0.0, 0.0);
  12870. * var b = Cesium.barycentricCoordinates(p,
  12871. * new Cesium.Cartesian3(-1.0, 0.0, 0.0),
  12872. * new Cesium.Cartesian3( 1.0, 0.0, 0.0),
  12873. * new Cesium.Cartesian3( 0.0, 1.0, 1.0));
  12874. */
  12875. function barycentricCoordinates(point, p0, p1, p2, result) {
  12876. if (!defined(point) || !defined(p0) || !defined(p1) || !defined(p2)) {
  12877. throw new DeveloperError('point, p0, p1, and p2 are required.');
  12878. }
  12879. if (!defined(result)) {
  12880. result = new Cartesian3();
  12881. }
  12882. // Implementation based on http://www.blackpawn.com/texts/pointinpoly/default.html.
  12883. var v0, v1, v2;
  12884. var dot00, dot01, dot02, dot11, dot12;
  12885. if(!defined(p0.z)) {
  12886. v0 = Cartesian2.subtract(p1, p0, scratchCartesian1);
  12887. v1 = Cartesian2.subtract(p2, p0, scratchCartesian2);
  12888. v2 = Cartesian2.subtract(point, p0, scratchCartesian3);
  12889. dot00 = Cartesian2.dot(v0, v0);
  12890. dot01 = Cartesian2.dot(v0, v1);
  12891. dot02 = Cartesian2.dot(v0, v2);
  12892. dot11 = Cartesian2.dot(v1, v1);
  12893. dot12 = Cartesian2.dot(v1, v2);
  12894. } else {
  12895. v0 = Cartesian3.subtract(p1, p0, scratchCartesian1);
  12896. v1 = Cartesian3.subtract(p2, p0, scratchCartesian2);
  12897. v2 = Cartesian3.subtract(point, p0, scratchCartesian3);
  12898. dot00 = Cartesian3.dot(v0, v0);
  12899. dot01 = Cartesian3.dot(v0, v1);
  12900. dot02 = Cartesian3.dot(v0, v2);
  12901. dot11 = Cartesian3.dot(v1, v1);
  12902. dot12 = Cartesian3.dot(v1, v2);
  12903. }
  12904. var q = 1.0 / (dot00 * dot11 - dot01 * dot01);
  12905. result.y = (dot11 * dot02 - dot01 * dot12) * q;
  12906. result.z = (dot00 * dot12 - dot01 * dot02) * q;
  12907. result.x = 1.0 - result.y - result.z;
  12908. return result;
  12909. }
  12910. return barycentricCoordinates;
  12911. });
  12912. /*global define*/
  12913. define('Core/EncodedCartesian3',[
  12914. './Cartesian3',
  12915. './defined',
  12916. './DeveloperError'
  12917. ], function(
  12918. Cartesian3,
  12919. defined,
  12920. DeveloperError) {
  12921. 'use strict';
  12922. /**
  12923. * A fixed-point encoding of a {@link Cartesian3} with 64-bit floating-point components, as two {@link Cartesian3}
  12924. * values that, when converted to 32-bit floating-point and added, approximate the original input.
  12925. * <p>
  12926. * This is used to encode positions in vertex buffers for rendering without jittering artifacts
  12927. * as described in {@link http://blogs.agi.com/insight3d/index.php/2008/09/03/precisions-precisions/|Precisions, Precisions}.
  12928. * </p>
  12929. *
  12930. * @alias EncodedCartesian3
  12931. * @constructor
  12932. *
  12933. * @private
  12934. */
  12935. function EncodedCartesian3() {
  12936. /**
  12937. * The high bits for each component. Bits 0 to 22 store the whole value. Bits 23 to 31 are not used.
  12938. *
  12939. * @type {Cartesian3}
  12940. * @default {@link Cartesian3.ZERO}
  12941. */
  12942. this.high = Cartesian3.clone(Cartesian3.ZERO);
  12943. /**
  12944. * The low bits for each component. Bits 7 to 22 store the whole value, and bits 0 to 6 store the fraction. Bits 23 to 31 are not used.
  12945. *
  12946. * @type {Cartesian3}
  12947. * @default {@link Cartesian3.ZERO}
  12948. */
  12949. this.low = Cartesian3.clone(Cartesian3.ZERO);
  12950. }
  12951. /**
  12952. * Encodes a 64-bit floating-point value as two floating-point values that, when converted to
  12953. * 32-bit floating-point and added, approximate the original input. The returned object
  12954. * has <code>high</code> and <code>low</code> properties for the high and low bits, respectively.
  12955. * <p>
  12956. * The fixed-point encoding follows {@link http://blogs.agi.com/insight3d/index.php/2008/09/03/precisions-precisions/|Precisions, Precisions}.
  12957. * </p>
  12958. *
  12959. * @param {Number} value The floating-point value to encode.
  12960. * @param {Object} [result] The object onto which to store the result.
  12961. * @returns {Object} The modified result parameter or a new instance if one was not provided.
  12962. *
  12963. * @example
  12964. * var value = 1234567.1234567;
  12965. * var splitValue = Cesium.EncodedCartesian3.encode(value);
  12966. */
  12967. EncodedCartesian3.encode = function(value, result) {
  12968. if (!defined(value)) {
  12969. throw new DeveloperError('value is required');
  12970. }
  12971. if (!defined(result)) {
  12972. result = {
  12973. high : 0.0,
  12974. low : 0.0
  12975. };
  12976. }
  12977. var doubleHigh;
  12978. if (value >= 0.0) {
  12979. doubleHigh = Math.floor(value / 65536.0) * 65536.0;
  12980. result.high = doubleHigh;
  12981. result.low = value - doubleHigh;
  12982. } else {
  12983. doubleHigh = Math.floor(-value / 65536.0) * 65536.0;
  12984. result.high = -doubleHigh;
  12985. result.low = value + doubleHigh;
  12986. }
  12987. return result;
  12988. };
  12989. var scratchEncode = {
  12990. high : 0.0,
  12991. low : 0.0
  12992. };
  12993. /**
  12994. * Encodes a {@link Cartesian3} with 64-bit floating-point components as two {@link Cartesian3}
  12995. * values that, when converted to 32-bit floating-point and added, approximate the original input.
  12996. * <p>
  12997. * The fixed-point encoding follows {@link http://blogs.agi.com/insight3d/index.php/2008/09/03/precisions-precisions/|Precisions, Precisions}.
  12998. * </p>
  12999. *
  13000. * @param {Cartesian3} cartesian The cartesian to encode.
  13001. * @param {EncodedCartesian3} [result] The object onto which to store the result.
  13002. * @returns {EncodedCartesian3} The modified result parameter or a new EncodedCartesian3 instance if one was not provided.
  13003. *
  13004. * @example
  13005. * var cart = new Cesium.Cartesian3(-10000000.0, 0.0, 10000000.0);
  13006. * var encoded = Cesium.EncodedCartesian3.fromCartesian(cart);
  13007. */
  13008. EncodedCartesian3.fromCartesian = function(cartesian, result) {
  13009. if (!defined(cartesian)) {
  13010. throw new DeveloperError('cartesian is required');
  13011. }
  13012. if (!defined(result)) {
  13013. result = new EncodedCartesian3();
  13014. }
  13015. var high = result.high;
  13016. var low = result.low;
  13017. EncodedCartesian3.encode(cartesian.x, scratchEncode);
  13018. high.x = scratchEncode.high;
  13019. low.x = scratchEncode.low;
  13020. EncodedCartesian3.encode(cartesian.y, scratchEncode);
  13021. high.y = scratchEncode.high;
  13022. low.y = scratchEncode.low;
  13023. EncodedCartesian3.encode(cartesian.z, scratchEncode);
  13024. high.z = scratchEncode.high;
  13025. low.z = scratchEncode.low;
  13026. return result;
  13027. };
  13028. var encodedP = new EncodedCartesian3();
  13029. /**
  13030. * Encodes the provided <code>cartesian</code>, and writes it to an array with <code>high</code>
  13031. * components followed by <code>low</code> components, i.e. <code>[high.x, high.y, high.z, low.x, low.y, low.z]</code>.
  13032. * <p>
  13033. * This is used to create interleaved high-precision position vertex attributes.
  13034. * </p>
  13035. *
  13036. * @param {Cartesian3} cartesian The cartesian to encode.
  13037. * @param {Number[]} cartesianArray The array to write to.
  13038. * @param {Number} index The index into the array to start writing. Six elements will be written.
  13039. *
  13040. * @exception {DeveloperError} index must be a number greater than or equal to 0.
  13041. *
  13042. * @example
  13043. * var positions = [
  13044. * new Cesium.Cartesian3(),
  13045. * // ...
  13046. * ];
  13047. * var encodedPositions = new Float32Array(2 * 3 * positions.length);
  13048. * var j = 0;
  13049. * for (var i = 0; i < positions.length; ++i) {
  13050. * Cesium.EncodedCartesian3.writeElement(positions[i], encodedPositions, j);
  13051. * j += 6;
  13052. * }
  13053. */
  13054. EncodedCartesian3.writeElements = function(cartesian, cartesianArray, index) {
  13055. if (!defined(cartesian)) {
  13056. throw new DeveloperError('cartesian is required');
  13057. }
  13058. if (!defined(cartesianArray)) {
  13059. throw new DeveloperError('cartesianArray is required');
  13060. }
  13061. if (typeof index !== 'number' || index < 0) {
  13062. throw new DeveloperError('index must be a number greater than or equal to 0.');
  13063. }
  13064. EncodedCartesian3.fromCartesian(cartesian, encodedP);
  13065. var high = encodedP.high;
  13066. var low = encodedP.low;
  13067. cartesianArray[index] = high.x;
  13068. cartesianArray[index + 1] = high.y;
  13069. cartesianArray[index + 2] = high.z;
  13070. cartesianArray[index + 3] = low.x;
  13071. cartesianArray[index + 4] = low.y;
  13072. cartesianArray[index + 5] = low.z;
  13073. };
  13074. return EncodedCartesian3;
  13075. });
  13076. /*global define*/
  13077. define('Core/IndexDatatype',[
  13078. './defined',
  13079. './DeveloperError',
  13080. './freezeObject',
  13081. './Math',
  13082. './WebGLConstants'
  13083. ], function(
  13084. defined,
  13085. DeveloperError,
  13086. freezeObject,
  13087. CesiumMath,
  13088. WebGLConstants) {
  13089. 'use strict';
  13090. /**
  13091. * Constants for WebGL index datatypes. These corresponds to the
  13092. * <code>type</code> parameter of {@link http://www.khronos.org/opengles/sdk/docs/man/xhtml/glDrawElements.xml|drawElements}.
  13093. *
  13094. * @exports IndexDatatype
  13095. */
  13096. var IndexDatatype = {
  13097. /**
  13098. * 8-bit unsigned byte corresponding to <code>UNSIGNED_BYTE</code> and the type
  13099. * of an element in <code>Uint8Array</code>.
  13100. *
  13101. * @type {Number}
  13102. * @constant
  13103. */
  13104. UNSIGNED_BYTE : WebGLConstants.UNSIGNED_BYTE,
  13105. /**
  13106. * 16-bit unsigned short corresponding to <code>UNSIGNED_SHORT</code> and the type
  13107. * of an element in <code>Uint16Array</code>.
  13108. *
  13109. * @type {Number}
  13110. * @constant
  13111. */
  13112. UNSIGNED_SHORT : WebGLConstants.UNSIGNED_SHORT,
  13113. /**
  13114. * 32-bit unsigned int corresponding to <code>UNSIGNED_INT</code> and the type
  13115. * of an element in <code>Uint32Array</code>.
  13116. *
  13117. * @type {Number}
  13118. * @constant
  13119. */
  13120. UNSIGNED_INT : WebGLConstants.UNSIGNED_INT
  13121. };
  13122. /**
  13123. * Returns the size, in bytes, of the corresponding datatype.
  13124. *
  13125. * @param {IndexDatatype} indexDatatype The index datatype to get the size of.
  13126. * @returns {Number} The size in bytes.
  13127. *
  13128. * @example
  13129. * // Returns 2
  13130. * var size = Cesium.IndexDatatype.getSizeInBytes(Cesium.IndexDatatype.UNSIGNED_SHORT);
  13131. */
  13132. IndexDatatype.getSizeInBytes = function(indexDatatype) {
  13133. switch(indexDatatype) {
  13134. case IndexDatatype.UNSIGNED_BYTE:
  13135. return Uint8Array.BYTES_PER_ELEMENT;
  13136. case IndexDatatype.UNSIGNED_SHORT:
  13137. return Uint16Array.BYTES_PER_ELEMENT;
  13138. case IndexDatatype.UNSIGNED_INT:
  13139. return Uint32Array.BYTES_PER_ELEMENT;
  13140. }
  13141. throw new DeveloperError('indexDatatype is required and must be a valid IndexDatatype constant.');
  13142. };
  13143. /**
  13144. * Validates that the provided index datatype is a valid {@link IndexDatatype}.
  13145. *
  13146. * @param {IndexDatatype} indexDatatype The index datatype to validate.
  13147. * @returns {Boolean} <code>true</code> if the provided index datatype is a valid value; otherwise, <code>false</code>.
  13148. *
  13149. * @example
  13150. * if (!Cesium.IndexDatatype.validate(indexDatatype)) {
  13151. * throw new Cesium.DeveloperError('indexDatatype must be a valid value.');
  13152. * }
  13153. */
  13154. IndexDatatype.validate = function(indexDatatype) {
  13155. return defined(indexDatatype) &&
  13156. (indexDatatype === IndexDatatype.UNSIGNED_BYTE ||
  13157. indexDatatype === IndexDatatype.UNSIGNED_SHORT ||
  13158. indexDatatype === IndexDatatype.UNSIGNED_INT);
  13159. };
  13160. /**
  13161. * Creates a typed array that will store indices, using either <code><Uint16Array</code>
  13162. * or <code>Uint32Array</code> depending on the number of vertices.
  13163. *
  13164. * @param {Number} numberOfVertices Number of vertices that the indices will reference.
  13165. * @param {Any} indicesLengthOrArray Passed through to the typed array constructor.
  13166. * @returns {Uint16Array|Uint32Array} A <code>Uint16Array</code> or <code>Uint32Array</code> constructed with <code>indicesLengthOrArray</code>.
  13167. *
  13168. * @example
  13169. * this.indices = Cesium.IndexDatatype.createTypedArray(positions.length / 3, numberOfIndices);
  13170. */
  13171. IndexDatatype.createTypedArray = function(numberOfVertices, indicesLengthOrArray) {
  13172. if (!defined(numberOfVertices)) {
  13173. throw new DeveloperError('numberOfVertices is required.');
  13174. }
  13175. if (numberOfVertices >= CesiumMath.SIXTY_FOUR_KILOBYTES) {
  13176. return new Uint32Array(indicesLengthOrArray);
  13177. }
  13178. return new Uint16Array(indicesLengthOrArray);
  13179. };
  13180. /**
  13181. * Creates a typed array from a source array buffer. The resulting typed array will store indices, using either <code><Uint16Array</code>
  13182. * or <code>Uint32Array</code> depending on the number of vertices.
  13183. *
  13184. * @param {Number} numberOfVertices Number of vertices that the indices will reference.
  13185. * @param {ArrayBuffer} sourceArray Passed through to the typed array constructor.
  13186. * @param {Number} byteOffset Passed through to the typed array constructor.
  13187. * @param {Number} length Passed through to the typed array constructor.
  13188. * @returns {Uint16Array|Uint32Array} A <code>Uint16Array</code> or <code>Uint32Array</code> constructed with <code>sourceArray</code>, <code>byteOffset</code>, and <code>length</code>.
  13189. *
  13190. */
  13191. IndexDatatype.createTypedArrayFromArrayBuffer = function(numberOfVertices, sourceArray, byteOffset, length) {
  13192. if (!defined(numberOfVertices)) {
  13193. throw new DeveloperError('numberOfVertices is required.');
  13194. }
  13195. if (!defined(sourceArray)) {
  13196. throw new DeveloperError('sourceArray is required.');
  13197. }
  13198. if (!defined(byteOffset)) {
  13199. throw new DeveloperError('byteOffset is required.');
  13200. }
  13201. if (numberOfVertices >= CesiumMath.SIXTY_FOUR_KILOBYTES) {
  13202. return new Uint32Array(sourceArray, byteOffset, length);
  13203. }
  13204. return new Uint16Array(sourceArray, byteOffset, length);
  13205. };
  13206. return freezeObject(IndexDatatype);
  13207. });
  13208. /*global define*/
  13209. define('Core/QuadraticRealPolynomial',[
  13210. './DeveloperError',
  13211. './Math'
  13212. ], function(
  13213. DeveloperError,
  13214. CesiumMath) {
  13215. 'use strict';
  13216. /**
  13217. * Defines functions for 2nd order polynomial functions of one variable with only real coefficients.
  13218. *
  13219. * @exports QuadraticRealPolynomial
  13220. */
  13221. var QuadraticRealPolynomial = {};
  13222. /**
  13223. * Provides the discriminant of the quadratic equation from the supplied coefficients.
  13224. *
  13225. * @param {Number} a The coefficient of the 2nd order monomial.
  13226. * @param {Number} b The coefficient of the 1st order monomial.
  13227. * @param {Number} c The coefficient of the 0th order monomial.
  13228. * @returns {Number} The value of the discriminant.
  13229. */
  13230. QuadraticRealPolynomial.computeDiscriminant = function(a, b, c) {
  13231. if (typeof a !== 'number') {
  13232. throw new DeveloperError('a is a required number.');
  13233. }
  13234. if (typeof b !== 'number') {
  13235. throw new DeveloperError('b is a required number.');
  13236. }
  13237. if (typeof c !== 'number') {
  13238. throw new DeveloperError('c is a required number.');
  13239. }
  13240. var discriminant = b * b - 4.0 * a * c;
  13241. return discriminant;
  13242. };
  13243. function addWithCancellationCheck(left, right, tolerance) {
  13244. var difference = left + right;
  13245. if ((CesiumMath.sign(left) !== CesiumMath.sign(right)) &&
  13246. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance) {
  13247. return 0.0;
  13248. }
  13249. return difference;
  13250. }
  13251. /**
  13252. * Provides the real valued roots of the quadratic polynomial with the provided coefficients.
  13253. *
  13254. * @param {Number} a The coefficient of the 2nd order monomial.
  13255. * @param {Number} b The coefficient of the 1st order monomial.
  13256. * @param {Number} c The coefficient of the 0th order monomial.
  13257. * @returns {Number[]} The real valued roots.
  13258. */
  13259. QuadraticRealPolynomial.computeRealRoots = function(a, b, c) {
  13260. if (typeof a !== 'number') {
  13261. throw new DeveloperError('a is a required number.');
  13262. }
  13263. if (typeof b !== 'number') {
  13264. throw new DeveloperError('b is a required number.');
  13265. }
  13266. if (typeof c !== 'number') {
  13267. throw new DeveloperError('c is a required number.');
  13268. }
  13269. var ratio;
  13270. if (a === 0.0) {
  13271. if (b === 0.0) {
  13272. // Constant function: c = 0.
  13273. return [];
  13274. }
  13275. // Linear function: b * x + c = 0.
  13276. return [-c / b];
  13277. } else if (b === 0.0) {
  13278. if (c === 0.0) {
  13279. // 2nd order monomial: a * x^2 = 0.
  13280. return [0.0, 0.0];
  13281. }
  13282. var cMagnitude = Math.abs(c);
  13283. var aMagnitude = Math.abs(a);
  13284. if ((cMagnitude < aMagnitude) && (cMagnitude / aMagnitude < CesiumMath.EPSILON14)) { // c ~= 0.0.
  13285. // 2nd order monomial: a * x^2 = 0.
  13286. return [0.0, 0.0];
  13287. } else if ((cMagnitude > aMagnitude) && (aMagnitude / cMagnitude < CesiumMath.EPSILON14)) { // a ~= 0.0.
  13288. // Constant function: c = 0.
  13289. return [];
  13290. }
  13291. // a * x^2 + c = 0
  13292. ratio = -c / a;
  13293. if (ratio < 0.0) {
  13294. // Both roots are complex.
  13295. return [];
  13296. }
  13297. // Both roots are real.
  13298. var root = Math.sqrt(ratio);
  13299. return [-root, root];
  13300. } else if (c === 0.0) {
  13301. // a * x^2 + b * x = 0
  13302. ratio = -b / a;
  13303. if (ratio < 0.0) {
  13304. return [ratio, 0.0];
  13305. }
  13306. return [0.0, ratio];
  13307. }
  13308. // a * x^2 + b * x + c = 0
  13309. var b2 = b * b;
  13310. var four_ac = 4.0 * a * c;
  13311. var radicand = addWithCancellationCheck(b2, -four_ac, CesiumMath.EPSILON14);
  13312. if (radicand < 0.0) {
  13313. // Both roots are complex.
  13314. return [];
  13315. }
  13316. var q = -0.5 * addWithCancellationCheck(b, CesiumMath.sign(b) * Math.sqrt(radicand), CesiumMath.EPSILON14);
  13317. if (b > 0.0) {
  13318. return [q / a, c / q];
  13319. }
  13320. return [c / q, q / a];
  13321. };
  13322. return QuadraticRealPolynomial;
  13323. });
  13324. /*global define*/
  13325. define('Core/CubicRealPolynomial',[
  13326. './DeveloperError',
  13327. './QuadraticRealPolynomial'
  13328. ], function(
  13329. DeveloperError,
  13330. QuadraticRealPolynomial) {
  13331. 'use strict';
  13332. /**
  13333. * Defines functions for 3rd order polynomial functions of one variable with only real coefficients.
  13334. *
  13335. * @exports CubicRealPolynomial
  13336. */
  13337. var CubicRealPolynomial = {};
  13338. /**
  13339. * Provides the discriminant of the cubic equation from the supplied coefficients.
  13340. *
  13341. * @param {Number} a The coefficient of the 3rd order monomial.
  13342. * @param {Number} b The coefficient of the 2nd order monomial.
  13343. * @param {Number} c The coefficient of the 1st order monomial.
  13344. * @param {Number} d The coefficient of the 0th order monomial.
  13345. * @returns {Number} The value of the discriminant.
  13346. */
  13347. CubicRealPolynomial.computeDiscriminant = function(a, b, c, d) {
  13348. if (typeof a !== 'number') {
  13349. throw new DeveloperError('a is a required number.');
  13350. }
  13351. if (typeof b !== 'number') {
  13352. throw new DeveloperError('b is a required number.');
  13353. }
  13354. if (typeof c !== 'number') {
  13355. throw new DeveloperError('c is a required number.');
  13356. }
  13357. if (typeof d !== 'number') {
  13358. throw new DeveloperError('d is a required number.');
  13359. }
  13360. var a2 = a * a;
  13361. var b2 = b * b;
  13362. var c2 = c * c;
  13363. var d2 = d * d;
  13364. var discriminant = 18.0 * a * b * c * d + b2 * c2 - 27.0 * a2 * d2 - 4.0 * (a * c2 * c + b2 * b * d);
  13365. return discriminant;
  13366. };
  13367. function computeRealRoots(a, b, c, d) {
  13368. var A = a;
  13369. var B = b / 3.0;
  13370. var C = c / 3.0;
  13371. var D = d;
  13372. var AC = A * C;
  13373. var BD = B * D;
  13374. var B2 = B * B;
  13375. var C2 = C * C;
  13376. var delta1 = A * C - B2;
  13377. var delta2 = A * D - B * C;
  13378. var delta3 = B * D - C2;
  13379. var discriminant = 4.0 * delta1 * delta3 - delta2 * delta2;
  13380. var temp;
  13381. var temp1;
  13382. if (discriminant < 0.0) {
  13383. var ABar;
  13384. var CBar;
  13385. var DBar;
  13386. if (B2 * BD >= AC * C2) {
  13387. ABar = A;
  13388. CBar = delta1;
  13389. DBar = -2.0 * B * delta1 + A * delta2;
  13390. } else {
  13391. ABar = D;
  13392. CBar = delta3;
  13393. DBar = -D * delta2 + 2.0 * C * delta3;
  13394. }
  13395. var s = (DBar < 0.0) ? -1.0 : 1.0; // This is not Math.Sign()!
  13396. var temp0 = -s * Math.abs(ABar) * Math.sqrt(-discriminant);
  13397. temp1 = -DBar + temp0;
  13398. var x = temp1 / 2.0;
  13399. var p = x < 0.0 ? -Math.pow(-x, 1.0 / 3.0) : Math.pow(x, 1.0 / 3.0);
  13400. var q = (temp1 === temp0) ? -p : -CBar / p;
  13401. temp = (CBar <= 0.0) ? p + q : -DBar / (p * p + q * q + CBar);
  13402. if (B2 * BD >= AC * C2) {
  13403. return [(temp - B) / A];
  13404. }
  13405. return [-D / (temp + C)];
  13406. }
  13407. var CBarA = delta1;
  13408. var DBarA = -2.0 * B * delta1 + A * delta2;
  13409. var CBarD = delta3;
  13410. var DBarD = -D * delta2 + 2.0 * C * delta3;
  13411. var squareRootOfDiscriminant = Math.sqrt(discriminant);
  13412. var halfSquareRootOf3 = Math.sqrt(3.0) / 2.0;
  13413. var theta = Math.abs(Math.atan2(A * squareRootOfDiscriminant, -DBarA) / 3.0);
  13414. temp = 2.0 * Math.sqrt(-CBarA);
  13415. var cosine = Math.cos(theta);
  13416. temp1 = temp * cosine;
  13417. var temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  13418. var numeratorLarge = (temp1 + temp3 > 2.0 * B) ? temp1 - B : temp3 - B;
  13419. var denominatorLarge = A;
  13420. var root1 = numeratorLarge / denominatorLarge;
  13421. theta = Math.abs(Math.atan2(D * squareRootOfDiscriminant, -DBarD) / 3.0);
  13422. temp = 2.0 * Math.sqrt(-CBarD);
  13423. cosine = Math.cos(theta);
  13424. temp1 = temp * cosine;
  13425. temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  13426. var numeratorSmall = -D;
  13427. var denominatorSmall = (temp1 + temp3 < 2.0 * C) ? temp1 + C : temp3 + C;
  13428. var root3 = numeratorSmall / denominatorSmall;
  13429. var E = denominatorLarge * denominatorSmall;
  13430. var F = -numeratorLarge * denominatorSmall - denominatorLarge * numeratorSmall;
  13431. var G = numeratorLarge * numeratorSmall;
  13432. var root2 = (C * F - B * G) / (-B * F + C * E);
  13433. if (root1 <= root2) {
  13434. if (root1 <= root3) {
  13435. if (root2 <= root3) {
  13436. return [root1, root2, root3];
  13437. }
  13438. return [root1, root3, root2];
  13439. }
  13440. return [root3, root1, root2];
  13441. }
  13442. if (root1 <= root3) {
  13443. return [root2, root1, root3];
  13444. }
  13445. if (root2 <= root3) {
  13446. return [root2, root3, root1];
  13447. }
  13448. return [root3, root2, root1];
  13449. }
  13450. /**
  13451. * Provides the real valued roots of the cubic polynomial with the provided coefficients.
  13452. *
  13453. * @param {Number} a The coefficient of the 3rd order monomial.
  13454. * @param {Number} b The coefficient of the 2nd order monomial.
  13455. * @param {Number} c The coefficient of the 1st order monomial.
  13456. * @param {Number} d The coefficient of the 0th order monomial.
  13457. * @returns {Number[]} The real valued roots.
  13458. */
  13459. CubicRealPolynomial.computeRealRoots = function(a, b, c, d) {
  13460. if (typeof a !== 'number') {
  13461. throw new DeveloperError('a is a required number.');
  13462. }
  13463. if (typeof b !== 'number') {
  13464. throw new DeveloperError('b is a required number.');
  13465. }
  13466. if (typeof c !== 'number') {
  13467. throw new DeveloperError('c is a required number.');
  13468. }
  13469. if (typeof d !== 'number') {
  13470. throw new DeveloperError('d is a required number.');
  13471. }
  13472. var roots;
  13473. var ratio;
  13474. if (a === 0.0) {
  13475. // Quadratic function: b * x^2 + c * x + d = 0.
  13476. return QuadraticRealPolynomial.computeRealRoots(b, c, d);
  13477. } else if (b === 0.0) {
  13478. if (c === 0.0) {
  13479. if (d === 0.0) {
  13480. // 3rd order monomial: a * x^3 = 0.
  13481. return [0.0, 0.0, 0.0];
  13482. }
  13483. // a * x^3 + d = 0
  13484. ratio = -d / a;
  13485. var root = (ratio < 0.0) ? -Math.pow(-ratio, 1.0 / 3.0) : Math.pow(ratio, 1.0 / 3.0);
  13486. return [root, root, root];
  13487. } else if (d === 0.0) {
  13488. // x * (a * x^2 + c) = 0.
  13489. roots = QuadraticRealPolynomial.computeRealRoots(a, 0, c);
  13490. // Return the roots in ascending order.
  13491. if (roots.Length === 0) {
  13492. return [0.0];
  13493. }
  13494. return [roots[0], 0.0, roots[1]];
  13495. }
  13496. // Deflated cubic polynomial: a * x^3 + c * x + d= 0.
  13497. return computeRealRoots(a, 0, c, d);
  13498. } else if (c === 0.0) {
  13499. if (d === 0.0) {
  13500. // x^2 * (a * x + b) = 0.
  13501. ratio = -b / a;
  13502. if (ratio < 0.0) {
  13503. return [ratio, 0.0, 0.0];
  13504. }
  13505. return [0.0, 0.0, ratio];
  13506. }
  13507. // a * x^3 + b * x^2 + d = 0.
  13508. return computeRealRoots(a, b, 0, d);
  13509. } else if (d === 0.0) {
  13510. // x * (a * x^2 + b * x + c) = 0
  13511. roots = QuadraticRealPolynomial.computeRealRoots(a, b, c);
  13512. // Return the roots in ascending order.
  13513. if (roots.length === 0) {
  13514. return [0.0];
  13515. } else if (roots[1] <= 0.0) {
  13516. return [roots[0], roots[1], 0.0];
  13517. } else if (roots[0] >= 0.0) {
  13518. return [0.0, roots[0], roots[1]];
  13519. }
  13520. return [roots[0], 0.0, roots[1]];
  13521. }
  13522. return computeRealRoots(a, b, c, d);
  13523. };
  13524. return CubicRealPolynomial;
  13525. });
  13526. /*global define*/
  13527. define('Core/QuarticRealPolynomial',[
  13528. './CubicRealPolynomial',
  13529. './DeveloperError',
  13530. './Math',
  13531. './QuadraticRealPolynomial'
  13532. ], function(
  13533. CubicRealPolynomial,
  13534. DeveloperError,
  13535. CesiumMath,
  13536. QuadraticRealPolynomial) {
  13537. 'use strict';
  13538. /**
  13539. * Defines functions for 4th order polynomial functions of one variable with only real coefficients.
  13540. *
  13541. * @exports QuarticRealPolynomial
  13542. */
  13543. var QuarticRealPolynomial = {};
  13544. /**
  13545. * Provides the discriminant of the quartic equation from the supplied coefficients.
  13546. *
  13547. * @param {Number} a The coefficient of the 4th order monomial.
  13548. * @param {Number} b The coefficient of the 3rd order monomial.
  13549. * @param {Number} c The coefficient of the 2nd order monomial.
  13550. * @param {Number} d The coefficient of the 1st order monomial.
  13551. * @param {Number} e The coefficient of the 0th order monomial.
  13552. * @returns {Number} The value of the discriminant.
  13553. */
  13554. QuarticRealPolynomial.computeDiscriminant = function(a, b, c, d, e) {
  13555. if (typeof a !== 'number') {
  13556. throw new DeveloperError('a is a required number.');
  13557. }
  13558. if (typeof b !== 'number') {
  13559. throw new DeveloperError('b is a required number.');
  13560. }
  13561. if (typeof c !== 'number') {
  13562. throw new DeveloperError('c is a required number.');
  13563. }
  13564. if (typeof d !== 'number') {
  13565. throw new DeveloperError('d is a required number.');
  13566. }
  13567. if (typeof e !== 'number') {
  13568. throw new DeveloperError('e is a required number.');
  13569. }
  13570. var a2 = a * a;
  13571. var a3 = a2 * a;
  13572. var b2 = b * b;
  13573. var b3 = b2 * b;
  13574. var c2 = c * c;
  13575. var c3 = c2 * c;
  13576. var d2 = d * d;
  13577. var d3 = d2 * d;
  13578. var e2 = e * e;
  13579. var e3 = e2 * e;
  13580. var discriminant = (b2 * c2 * d2 - 4.0 * b3 * d3 - 4.0 * a * c3 * d2 + 18 * a * b * c * d3 - 27.0 * a2 * d2 * d2 + 256.0 * a3 * e3) +
  13581. e * (18.0 * b3 * c * d - 4.0 * b2 * c3 + 16.0 * a * c2 * c2 - 80.0 * a * b * c2 * d - 6.0 * a * b2 * d2 + 144.0 * a2 * c * d2) +
  13582. e2 * (144.0 * a * b2 * c - 27.0 * b2 * b2 - 128.0 * a2 * c2 - 192.0 * a2 * b * d);
  13583. return discriminant;
  13584. };
  13585. function original(a3, a2, a1, a0) {
  13586. var a3Squared = a3 * a3;
  13587. var p = a2 - 3.0 * a3Squared / 8.0;
  13588. var q = a1 - a2 * a3 / 2.0 + a3Squared * a3 / 8.0;
  13589. var r = a0 - a1 * a3 / 4.0 + a2 * a3Squared / 16.0 - 3.0 * a3Squared * a3Squared / 256.0;
  13590. // Find the roots of the cubic equations: h^6 + 2 p h^4 + (p^2 - 4 r) h^2 - q^2 = 0.
  13591. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, 2.0 * p, p * p - 4.0 * r, -q * q);
  13592. if (cubicRoots.length > 0) {
  13593. var temp = -a3 / 4.0;
  13594. // Use the largest positive root.
  13595. var hSquared = cubicRoots[cubicRoots.length - 1];
  13596. if (Math.abs(hSquared) < CesiumMath.EPSILON14) {
  13597. // y^4 + p y^2 + r = 0.
  13598. var roots = QuadraticRealPolynomial.computeRealRoots(1.0, p, r);
  13599. if (roots.length === 2) {
  13600. var root0 = roots[0];
  13601. var root1 = roots[1];
  13602. var y;
  13603. if (root0 >= 0.0 && root1 >= 0.0) {
  13604. var y0 = Math.sqrt(root0);
  13605. var y1 = Math.sqrt(root1);
  13606. return [temp - y1, temp - y0, temp + y0, temp + y1];
  13607. } else if (root0 >= 0.0 && root1 < 0.0) {
  13608. y = Math.sqrt(root0);
  13609. return [temp - y, temp + y];
  13610. } else if (root0 < 0.0 && root1 >= 0.0) {
  13611. y = Math.sqrt(root1);
  13612. return [temp - y, temp + y];
  13613. }
  13614. }
  13615. return [];
  13616. } else if (hSquared > 0.0) {
  13617. var h = Math.sqrt(hSquared);
  13618. var m = (p + hSquared - q / h) / 2.0;
  13619. var n = (p + hSquared + q / h) / 2.0;
  13620. // Now solve the two quadratic factors: (y^2 + h y + m)(y^2 - h y + n);
  13621. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, h, m);
  13622. var roots2 = QuadraticRealPolynomial.computeRealRoots(1.0, -h, n);
  13623. if (roots1.length !== 0) {
  13624. roots1[0] += temp;
  13625. roots1[1] += temp;
  13626. if (roots2.length !== 0) {
  13627. roots2[0] += temp;
  13628. roots2[1] += temp;
  13629. if (roots1[1] <= roots2[0]) {
  13630. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  13631. } else if (roots2[1] <= roots1[0]) {
  13632. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  13633. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  13634. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  13635. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  13636. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  13637. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  13638. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  13639. }
  13640. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  13641. }
  13642. return roots1;
  13643. }
  13644. if (roots2.length !== 0) {
  13645. roots2[0] += temp;
  13646. roots2[1] += temp;
  13647. return roots2;
  13648. }
  13649. return [];
  13650. }
  13651. }
  13652. return [];
  13653. }
  13654. function neumark(a3, a2, a1, a0) {
  13655. var a1Squared = a1 * a1;
  13656. var a2Squared = a2 * a2;
  13657. var a3Squared = a3 * a3;
  13658. var p = -2.0 * a2;
  13659. var q = a1 * a3 + a2Squared - 4.0 * a0;
  13660. var r = a3Squared * a0 - a1 * a2 * a3 + a1Squared;
  13661. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, p, q, r);
  13662. if (cubicRoots.length > 0) {
  13663. // Use the most positive root
  13664. var y = cubicRoots[0];
  13665. var temp = (a2 - y);
  13666. var tempSquared = temp * temp;
  13667. var g1 = a3 / 2.0;
  13668. var h1 = temp / 2.0;
  13669. var m = tempSquared - 4.0 * a0;
  13670. var mError = tempSquared + 4.0 * Math.abs(a0);
  13671. var n = a3Squared - 4.0 * y;
  13672. var nError = a3Squared + 4.0 * Math.abs(y);
  13673. var g2;
  13674. var h2;
  13675. if (y < 0.0 || (m * nError < n * mError)) {
  13676. var squareRootOfN = Math.sqrt(n);
  13677. g2 = squareRootOfN / 2.0;
  13678. h2 = squareRootOfN === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfN;
  13679. } else {
  13680. var squareRootOfM = Math.sqrt(m);
  13681. g2 = squareRootOfM === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfM;
  13682. h2 = squareRootOfM / 2.0;
  13683. }
  13684. var G;
  13685. var g;
  13686. if (g1 === 0.0 && g2 === 0.0) {
  13687. G = 0.0;
  13688. g = 0.0;
  13689. } else if (CesiumMath.sign(g1) === CesiumMath.sign(g2)) {
  13690. G = g1 + g2;
  13691. g = y / G;
  13692. } else {
  13693. g = g1 - g2;
  13694. G = y / g;
  13695. }
  13696. var H;
  13697. var h;
  13698. if (h1 === 0.0 && h2 === 0.0) {
  13699. H = 0.0;
  13700. h = 0.0;
  13701. } else if (CesiumMath.sign(h1) === CesiumMath.sign(h2)) {
  13702. H = h1 + h2;
  13703. h = a0 / H;
  13704. } else {
  13705. h = h1 - h2;
  13706. H = a0 / h;
  13707. }
  13708. // Now solve the two quadratic factors: (y^2 + G y + H)(y^2 + g y + h);
  13709. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, G, H);
  13710. var roots2 = QuadraticRealPolynomial.computeRealRoots(1.0, g, h);
  13711. if (roots1.length !== 0) {
  13712. if (roots2.length !== 0) {
  13713. if (roots1[1] <= roots2[0]) {
  13714. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  13715. } else if (roots2[1] <= roots1[0]) {
  13716. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  13717. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  13718. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  13719. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  13720. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  13721. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  13722. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  13723. } else {
  13724. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  13725. }
  13726. }
  13727. return roots1;
  13728. }
  13729. if (roots2.length !== 0) {
  13730. return roots2;
  13731. }
  13732. }
  13733. return [];
  13734. }
  13735. /**
  13736. * Provides the real valued roots of the quartic polynomial with the provided coefficients.
  13737. *
  13738. * @param {Number} a The coefficient of the 4th order monomial.
  13739. * @param {Number} b The coefficient of the 3rd order monomial.
  13740. * @param {Number} c The coefficient of the 2nd order monomial.
  13741. * @param {Number} d The coefficient of the 1st order monomial.
  13742. * @param {Number} e The coefficient of the 0th order monomial.
  13743. * @returns {Number[]} The real valued roots.
  13744. */
  13745. QuarticRealPolynomial.computeRealRoots = function(a, b, c, d, e) {
  13746. if (typeof a !== 'number') {
  13747. throw new DeveloperError('a is a required number.');
  13748. }
  13749. if (typeof b !== 'number') {
  13750. throw new DeveloperError('b is a required number.');
  13751. }
  13752. if (typeof c !== 'number') {
  13753. throw new DeveloperError('c is a required number.');
  13754. }
  13755. if (typeof d !== 'number') {
  13756. throw new DeveloperError('d is a required number.');
  13757. }
  13758. if (typeof e !== 'number') {
  13759. throw new DeveloperError('e is a required number.');
  13760. }
  13761. if (Math.abs(a) < CesiumMath.EPSILON15) {
  13762. return CubicRealPolynomial.computeRealRoots(b, c, d, e);
  13763. }
  13764. var a3 = b / a;
  13765. var a2 = c / a;
  13766. var a1 = d / a;
  13767. var a0 = e / a;
  13768. var k = (a3 < 0.0) ? 1 : 0;
  13769. k += (a2 < 0.0) ? k + 1 : k;
  13770. k += (a1 < 0.0) ? k + 1 : k;
  13771. k += (a0 < 0.0) ? k + 1 : k;
  13772. switch (k) {
  13773. case 0:
  13774. return original(a3, a2, a1, a0);
  13775. case 1:
  13776. return neumark(a3, a2, a1, a0);
  13777. case 2:
  13778. return neumark(a3, a2, a1, a0);
  13779. case 3:
  13780. return original(a3, a2, a1, a0);
  13781. case 4:
  13782. return original(a3, a2, a1, a0);
  13783. case 5:
  13784. return neumark(a3, a2, a1, a0);
  13785. case 6:
  13786. return original(a3, a2, a1, a0);
  13787. case 7:
  13788. return original(a3, a2, a1, a0);
  13789. case 8:
  13790. return neumark(a3, a2, a1, a0);
  13791. case 9:
  13792. return original(a3, a2, a1, a0);
  13793. case 10:
  13794. return original(a3, a2, a1, a0);
  13795. case 11:
  13796. return neumark(a3, a2, a1, a0);
  13797. case 12:
  13798. return original(a3, a2, a1, a0);
  13799. case 13:
  13800. return original(a3, a2, a1, a0);
  13801. case 14:
  13802. return original(a3, a2, a1, a0);
  13803. case 15:
  13804. return original(a3, a2, a1, a0);
  13805. default:
  13806. return undefined;
  13807. }
  13808. };
  13809. return QuarticRealPolynomial;
  13810. });
  13811. /*global define*/
  13812. define('Core/Ray',[
  13813. './Cartesian3',
  13814. './defaultValue',
  13815. './defined',
  13816. './DeveloperError'
  13817. ], function(
  13818. Cartesian3,
  13819. defaultValue,
  13820. defined,
  13821. DeveloperError) {
  13822. 'use strict';
  13823. /**
  13824. * Represents a ray that extends infinitely from the provided origin in the provided direction.
  13825. * @alias Ray
  13826. * @constructor
  13827. *
  13828. * @param {Cartesian3} [origin=Cartesian3.ZERO] The origin of the ray.
  13829. * @param {Cartesian3} [direction=Cartesian3.ZERO] The direction of the ray.
  13830. */
  13831. function Ray(origin, direction) {
  13832. direction = Cartesian3.clone(defaultValue(direction, Cartesian3.ZERO));
  13833. if (!Cartesian3.equals(direction, Cartesian3.ZERO)) {
  13834. Cartesian3.normalize(direction, direction);
  13835. }
  13836. /**
  13837. * The origin of the ray.
  13838. * @type {Cartesian3}
  13839. * @default {@link Cartesian3.ZERO}
  13840. */
  13841. this.origin = Cartesian3.clone(defaultValue(origin, Cartesian3.ZERO));
  13842. /**
  13843. * The direction of the ray.
  13844. * @type {Cartesian3}
  13845. */
  13846. this.direction = direction;
  13847. }
  13848. /**
  13849. * Computes the point along the ray given by r(t) = o + t*d,
  13850. * where o is the origin of the ray and d is the direction.
  13851. *
  13852. * @param {Ray} ray The ray.
  13853. * @param {Number} t A scalar value.
  13854. * @param {Cartesian3} [result] The object in which the result will be stored.
  13855. * @returns {Cartesian3} The modified result parameter, or a new instance if none was provided.
  13856. *
  13857. * @example
  13858. * //Get the first intersection point of a ray and an ellipsoid.
  13859. * var intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
  13860. * var point = Cesium.Ray.getPoint(ray, intersection.start);
  13861. */
  13862. Ray.getPoint = function(ray, t, result) {
  13863. if (!defined(ray)){
  13864. throw new DeveloperError('ray is requred');
  13865. }
  13866. if (typeof t !== 'number') {
  13867. throw new DeveloperError('t is a required number');
  13868. }
  13869. if (!defined(result)) {
  13870. result = new Cartesian3();
  13871. }
  13872. result = Cartesian3.multiplyByScalar(ray.direction, t, result);
  13873. return Cartesian3.add(ray.origin, result, result);
  13874. };
  13875. return Ray;
  13876. });
  13877. /*global define*/
  13878. define('Core/IntersectionTests',[
  13879. './Cartesian3',
  13880. './Cartographic',
  13881. './defaultValue',
  13882. './defined',
  13883. './DeveloperError',
  13884. './Math',
  13885. './Matrix3',
  13886. './QuadraticRealPolynomial',
  13887. './QuarticRealPolynomial',
  13888. './Ray'
  13889. ], function(
  13890. Cartesian3,
  13891. Cartographic,
  13892. defaultValue,
  13893. defined,
  13894. DeveloperError,
  13895. CesiumMath,
  13896. Matrix3,
  13897. QuadraticRealPolynomial,
  13898. QuarticRealPolynomial,
  13899. Ray) {
  13900. 'use strict';
  13901. /**
  13902. * Functions for computing the intersection between geometries such as rays, planes, triangles, and ellipsoids.
  13903. *
  13904. * @exports IntersectionTests
  13905. */
  13906. var IntersectionTests = {};
  13907. /**
  13908. * Computes the intersection of a ray and a plane.
  13909. *
  13910. * @param {Ray} ray The ray.
  13911. * @param {Plane} plane The plane.
  13912. * @param {Cartesian3} [result] The object onto which to store the result.
  13913. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  13914. */
  13915. IntersectionTests.rayPlane = function(ray, plane, result) {
  13916. if (!defined(ray)) {
  13917. throw new DeveloperError('ray is required.');
  13918. }
  13919. if (!defined(plane)) {
  13920. throw new DeveloperError('plane is required.');
  13921. }
  13922. if (!defined(result)) {
  13923. result = new Cartesian3();
  13924. }
  13925. var origin = ray.origin;
  13926. var direction = ray.direction;
  13927. var normal = plane.normal;
  13928. var denominator = Cartesian3.dot(normal, direction);
  13929. if (Math.abs(denominator) < CesiumMath.EPSILON15) {
  13930. // Ray is parallel to plane. The ray may be in the polygon's plane.
  13931. return undefined;
  13932. }
  13933. var t = (-plane.distance - Cartesian3.dot(normal, origin)) / denominator;
  13934. if (t < 0) {
  13935. return undefined;
  13936. }
  13937. result = Cartesian3.multiplyByScalar(direction, t, result);
  13938. return Cartesian3.add(origin, result, result);
  13939. };
  13940. var scratchEdge0 = new Cartesian3();
  13941. var scratchEdge1 = new Cartesian3();
  13942. var scratchPVec = new Cartesian3();
  13943. var scratchTVec = new Cartesian3();
  13944. var scratchQVec = new Cartesian3();
  13945. /**
  13946. * Computes the intersection of a ray and a triangle as a parametric distance along the input ray.
  13947. *
  13948. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  13949. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  13950. *
  13951. * @memberof IntersectionTests
  13952. *
  13953. * @param {Ray} ray The ray.
  13954. * @param {Cartesian3} p0 The first vertex of the triangle.
  13955. * @param {Cartesian3} p1 The second vertex of the triangle.
  13956. * @param {Cartesian3} p2 The third vertex of the triangle.
  13957. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  13958. * and return undefined for intersections with the back face.
  13959. * @returns {Number} The intersection as a parametric distance along the ray, or undefined if there is no intersection.
  13960. */
  13961. IntersectionTests.rayTriangleParametric = function(ray, p0, p1, p2, cullBackFaces) {
  13962. if (!defined(ray)) {
  13963. throw new DeveloperError('ray is required.');
  13964. }
  13965. if (!defined(p0)) {
  13966. throw new DeveloperError('p0 is required.');
  13967. }
  13968. if (!defined(p1)) {
  13969. throw new DeveloperError('p1 is required.');
  13970. }
  13971. if (!defined(p2)) {
  13972. throw new DeveloperError('p2 is required.');
  13973. }
  13974. cullBackFaces = defaultValue(cullBackFaces, false);
  13975. var origin = ray.origin;
  13976. var direction = ray.direction;
  13977. var edge0 = Cartesian3.subtract(p1, p0, scratchEdge0);
  13978. var edge1 = Cartesian3.subtract(p2, p0, scratchEdge1);
  13979. var p = Cartesian3.cross(direction, edge1, scratchPVec);
  13980. var det = Cartesian3.dot(edge0, p);
  13981. var tvec;
  13982. var q;
  13983. var u;
  13984. var v;
  13985. var t;
  13986. if (cullBackFaces) {
  13987. if (det < CesiumMath.EPSILON6) {
  13988. return undefined;
  13989. }
  13990. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  13991. u = Cartesian3.dot(tvec, p);
  13992. if (u < 0.0 || u > det) {
  13993. return undefined;
  13994. }
  13995. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  13996. v = Cartesian3.dot(direction, q);
  13997. if (v < 0.0 || u + v > det) {
  13998. return undefined;
  13999. }
  14000. t = Cartesian3.dot(edge1, q) / det;
  14001. } else {
  14002. if (Math.abs(det) < CesiumMath.EPSILON6) {
  14003. return undefined;
  14004. }
  14005. var invDet = 1.0 / det;
  14006. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  14007. u = Cartesian3.dot(tvec, p) * invDet;
  14008. if (u < 0.0 || u > 1.0) {
  14009. return undefined;
  14010. }
  14011. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  14012. v = Cartesian3.dot(direction, q) * invDet;
  14013. if (v < 0.0 || u + v > 1.0) {
  14014. return undefined;
  14015. }
  14016. t = Cartesian3.dot(edge1, q) * invDet;
  14017. }
  14018. return t;
  14019. };
  14020. /**
  14021. * Computes the intersection of a ray and a triangle as a Cartesian3 coordinate.
  14022. *
  14023. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  14024. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  14025. *
  14026. * @memberof IntersectionTests
  14027. *
  14028. * @param {Ray} ray The ray.
  14029. * @param {Cartesian3} p0 The first vertex of the triangle.
  14030. * @param {Cartesian3} p1 The second vertex of the triangle.
  14031. * @param {Cartesian3} p2 The third vertex of the triangle.
  14032. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  14033. * and return undefined for intersections with the back face.
  14034. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  14035. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  14036. */
  14037. IntersectionTests.rayTriangle = function(ray, p0, p1, p2, cullBackFaces, result) {
  14038. var t = IntersectionTests.rayTriangleParametric(ray, p0, p1, p2, cullBackFaces);
  14039. if (!defined(t) || t < 0.0) {
  14040. return undefined;
  14041. }
  14042. if (!defined(result)) {
  14043. result = new Cartesian3();
  14044. }
  14045. Cartesian3.multiplyByScalar(ray.direction, t, result);
  14046. return Cartesian3.add(ray.origin, result, result);
  14047. };
  14048. var scratchLineSegmentTriangleRay = new Ray();
  14049. /**
  14050. * Computes the intersection of a line segment and a triangle.
  14051. * @memberof IntersectionTests
  14052. *
  14053. * @param {Cartesian3} v0 The an end point of the line segment.
  14054. * @param {Cartesian3} v1 The other end point of the line segment.
  14055. * @param {Cartesian3} p0 The first vertex of the triangle.
  14056. * @param {Cartesian3} p1 The second vertex of the triangle.
  14057. * @param {Cartesian3} p2 The third vertex of the triangle.
  14058. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  14059. * and return undefined for intersections with the back face.
  14060. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  14061. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  14062. */
  14063. IntersectionTests.lineSegmentTriangle = function(v0, v1, p0, p1, p2, cullBackFaces, result) {
  14064. if (!defined(v0)) {
  14065. throw new DeveloperError('v0 is required.');
  14066. }
  14067. if (!defined(v1)) {
  14068. throw new DeveloperError('v1 is required.');
  14069. }
  14070. if (!defined(p0)) {
  14071. throw new DeveloperError('p0 is required.');
  14072. }
  14073. if (!defined(p1)) {
  14074. throw new DeveloperError('p1 is required.');
  14075. }
  14076. if (!defined(p2)) {
  14077. throw new DeveloperError('p2 is required.');
  14078. }
  14079. var ray = scratchLineSegmentTriangleRay;
  14080. Cartesian3.clone(v0, ray.origin);
  14081. Cartesian3.subtract(v1, v0, ray.direction);
  14082. Cartesian3.normalize(ray.direction, ray.direction);
  14083. var t = IntersectionTests.rayTriangleParametric(ray, p0, p1, p2, cullBackFaces);
  14084. if (!defined(t) || t < 0.0 || t > Cartesian3.distance(v0, v1)) {
  14085. return undefined;
  14086. }
  14087. if (!defined(result)) {
  14088. result = new Cartesian3();
  14089. }
  14090. Cartesian3.multiplyByScalar(ray.direction, t, result);
  14091. return Cartesian3.add(ray.origin, result, result);
  14092. };
  14093. function solveQuadratic(a, b, c, result) {
  14094. var det = b * b - 4.0 * a * c;
  14095. if (det < 0.0) {
  14096. return undefined;
  14097. } else if (det > 0.0) {
  14098. var denom = 1.0 / (2.0 * a);
  14099. var disc = Math.sqrt(det);
  14100. var root0 = (-b + disc) * denom;
  14101. var root1 = (-b - disc) * denom;
  14102. if (root0 < root1) {
  14103. result.root0 = root0;
  14104. result.root1 = root1;
  14105. } else {
  14106. result.root0 = root1;
  14107. result.root1 = root0;
  14108. }
  14109. return result;
  14110. }
  14111. var root = -b / (2.0 * a);
  14112. if (root === 0.0) {
  14113. return undefined;
  14114. }
  14115. result.root0 = result.root1 = root;
  14116. return result;
  14117. }
  14118. var raySphereRoots = {
  14119. root0 : 0.0,
  14120. root1 : 0.0
  14121. };
  14122. function raySphere(ray, sphere, result) {
  14123. if (!defined(result)) {
  14124. result = {};
  14125. }
  14126. var origin = ray.origin;
  14127. var direction = ray.direction;
  14128. var center = sphere.center;
  14129. var radiusSquared = sphere.radius * sphere.radius;
  14130. var diff = Cartesian3.subtract(origin, center, scratchPVec);
  14131. var a = Cartesian3.dot(direction, direction);
  14132. var b = 2.0 * Cartesian3.dot(direction, diff);
  14133. var c = Cartesian3.magnitudeSquared(diff) - radiusSquared;
  14134. var roots = solveQuadratic(a, b, c, raySphereRoots);
  14135. if (!defined(roots)) {
  14136. return undefined;
  14137. }
  14138. result.start = roots.root0;
  14139. result.stop = roots.root1;
  14140. return result;
  14141. }
  14142. /**
  14143. * Computes the intersection points of a ray with a sphere.
  14144. * @memberof IntersectionTests
  14145. *
  14146. * @param {Ray} ray The ray.
  14147. * @param {BoundingSphere} sphere The sphere.
  14148. * @param {Object} [result] The result onto which to store the result.
  14149. * @returns {Object} An object with the first (<code>start</code>) and the second (<code>stop</code>) intersection scalars for points along the ray or undefined if there are no intersections.
  14150. */
  14151. IntersectionTests.raySphere = function(ray, sphere, result) {
  14152. if (!defined(ray)) {
  14153. throw new DeveloperError('ray is required.');
  14154. }
  14155. if (!defined(sphere)) {
  14156. throw new DeveloperError('sphere is required.');
  14157. }
  14158. result = raySphere(ray, sphere, result);
  14159. if (!defined(result) || result.stop < 0.0) {
  14160. return undefined;
  14161. }
  14162. result.start = Math.max(result.start, 0.0);
  14163. return result;
  14164. };
  14165. var scratchLineSegmentRay = new Ray();
  14166. /**
  14167. * Computes the intersection points of a line segment with a sphere.
  14168. * @memberof IntersectionTests
  14169. *
  14170. * @param {Cartesian3} p0 An end point of the line segment.
  14171. * @param {Cartesian3} p1 The other end point of the line segment.
  14172. * @param {BoundingSphere} sphere The sphere.
  14173. * @param {Object} [result] The result onto which to store the result.
  14174. * @returns {Object} An object with the first (<code>start</code>) and the second (<code>stop</code>) intersection scalars for points along the line segment or undefined if there are no intersections.
  14175. */
  14176. IntersectionTests.lineSegmentSphere = function(p0, p1, sphere, result) {
  14177. if (!defined(p0)) {
  14178. throw new DeveloperError('p0 is required.');
  14179. }
  14180. if (!defined(p1)) {
  14181. throw new DeveloperError('p1 is required.');
  14182. }
  14183. if (!defined(sphere)) {
  14184. throw new DeveloperError('sphere is required.');
  14185. }
  14186. var ray = scratchLineSegmentRay;
  14187. Cartesian3.clone(p0, ray.origin);
  14188. var direction = Cartesian3.subtract(p1, p0, ray.direction);
  14189. var maxT = Cartesian3.magnitude(direction);
  14190. Cartesian3.normalize(direction, direction);
  14191. result = raySphere(ray, sphere, result);
  14192. if (!defined(result) || result.stop < 0.0 || result.start > maxT) {
  14193. return undefined;
  14194. }
  14195. result.start = Math.max(result.start, 0.0);
  14196. result.stop = Math.min(result.stop, maxT);
  14197. return result;
  14198. };
  14199. var scratchQ = new Cartesian3();
  14200. var scratchW = new Cartesian3();
  14201. /**
  14202. * Computes the intersection points of a ray with an ellipsoid.
  14203. *
  14204. * @param {Ray} ray The ray.
  14205. * @param {Ellipsoid} ellipsoid The ellipsoid.
  14206. * @returns {Object} An object with the first (<code>start</code>) and the second (<code>stop</code>) intersection scalars for points along the ray or undefined if there are no intersections.
  14207. */
  14208. IntersectionTests.rayEllipsoid = function(ray, ellipsoid) {
  14209. if (!defined(ray)) {
  14210. throw new DeveloperError('ray is required.');
  14211. }
  14212. if (!defined(ellipsoid)) {
  14213. throw new DeveloperError('ellipsoid is required.');
  14214. }
  14215. var inverseRadii = ellipsoid.oneOverRadii;
  14216. var q = Cartesian3.multiplyComponents(inverseRadii, ray.origin, scratchQ);
  14217. var w = Cartesian3.multiplyComponents(inverseRadii, ray.direction, scratchW);
  14218. var q2 = Cartesian3.magnitudeSquared(q);
  14219. var qw = Cartesian3.dot(q, w);
  14220. var difference, w2, product, discriminant, temp;
  14221. if (q2 > 1.0) {
  14222. // Outside ellipsoid.
  14223. if (qw >= 0.0) {
  14224. // Looking outward or tangent (0 intersections).
  14225. return undefined;
  14226. }
  14227. // qw < 0.0.
  14228. var qw2 = qw * qw;
  14229. difference = q2 - 1.0; // Positively valued.
  14230. w2 = Cartesian3.magnitudeSquared(w);
  14231. product = w2 * difference;
  14232. if (qw2 < product) {
  14233. // Imaginary roots (0 intersections).
  14234. return undefined;
  14235. } else if (qw2 > product) {
  14236. // Distinct roots (2 intersections).
  14237. discriminant = qw * qw - product;
  14238. temp = -qw + Math.sqrt(discriminant); // Avoid cancellation.
  14239. var root0 = temp / w2;
  14240. var root1 = difference / temp;
  14241. if (root0 < root1) {
  14242. return {
  14243. start : root0,
  14244. stop : root1
  14245. };
  14246. }
  14247. return {
  14248. start : root1,
  14249. stop : root0
  14250. };
  14251. } else {
  14252. // qw2 == product. Repeated roots (2 intersections).
  14253. var root = Math.sqrt(difference / w2);
  14254. return {
  14255. start : root,
  14256. stop : root
  14257. };
  14258. }
  14259. } else if (q2 < 1.0) {
  14260. // Inside ellipsoid (2 intersections).
  14261. difference = q2 - 1.0; // Negatively valued.
  14262. w2 = Cartesian3.magnitudeSquared(w);
  14263. product = w2 * difference; // Negatively valued.
  14264. discriminant = qw * qw - product;
  14265. temp = -qw + Math.sqrt(discriminant); // Positively valued.
  14266. return {
  14267. start : 0.0,
  14268. stop : temp / w2
  14269. };
  14270. } else {
  14271. // q2 == 1.0. On ellipsoid.
  14272. if (qw < 0.0) {
  14273. // Looking inward.
  14274. w2 = Cartesian3.magnitudeSquared(w);
  14275. return {
  14276. start : 0.0,
  14277. stop : -qw / w2
  14278. };
  14279. }
  14280. // qw >= 0.0. Looking outward or tangent.
  14281. return undefined;
  14282. }
  14283. };
  14284. function addWithCancellationCheck(left, right, tolerance) {
  14285. var difference = left + right;
  14286. if ((CesiumMath.sign(left) !== CesiumMath.sign(right)) &&
  14287. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance) {
  14288. return 0.0;
  14289. }
  14290. return difference;
  14291. }
  14292. function quadraticVectorExpression(A, b, c, x, w) {
  14293. var xSquared = x * x;
  14294. var wSquared = w * w;
  14295. var l2 = (A[Matrix3.COLUMN1ROW1] - A[Matrix3.COLUMN2ROW2]) * wSquared;
  14296. var l1 = w * (x * addWithCancellationCheck(A[Matrix3.COLUMN1ROW0], A[Matrix3.COLUMN0ROW1], CesiumMath.EPSILON15) + b.y);
  14297. var l0 = (A[Matrix3.COLUMN0ROW0] * xSquared + A[Matrix3.COLUMN2ROW2] * wSquared) + x * b.x + c;
  14298. var r1 = wSquared * addWithCancellationCheck(A[Matrix3.COLUMN2ROW1], A[Matrix3.COLUMN1ROW2], CesiumMath.EPSILON15);
  14299. var r0 = w * (x * addWithCancellationCheck(A[Matrix3.COLUMN2ROW0], A[Matrix3.COLUMN0ROW2]) + b.z);
  14300. var cosines;
  14301. var solutions = [];
  14302. if (r0 === 0.0 && r1 === 0.0) {
  14303. cosines = QuadraticRealPolynomial.computeRealRoots(l2, l1, l0);
  14304. if (cosines.length === 0) {
  14305. return solutions;
  14306. }
  14307. var cosine0 = cosines[0];
  14308. var sine0 = Math.sqrt(Math.max(1.0 - cosine0 * cosine0, 0.0));
  14309. solutions.push(new Cartesian3(x, w * cosine0, w * -sine0));
  14310. solutions.push(new Cartesian3(x, w * cosine0, w * sine0));
  14311. if (cosines.length === 2) {
  14312. var cosine1 = cosines[1];
  14313. var sine1 = Math.sqrt(Math.max(1.0 - cosine1 * cosine1, 0.0));
  14314. solutions.push(new Cartesian3(x, w * cosine1, w * -sine1));
  14315. solutions.push(new Cartesian3(x, w * cosine1, w * sine1));
  14316. }
  14317. return solutions;
  14318. }
  14319. var r0Squared = r0 * r0;
  14320. var r1Squared = r1 * r1;
  14321. var l2Squared = l2 * l2;
  14322. var r0r1 = r0 * r1;
  14323. var c4 = l2Squared + r1Squared;
  14324. var c3 = 2.0 * (l1 * l2 + r0r1);
  14325. var c2 = 2.0 * l0 * l2 + l1 * l1 - r1Squared + r0Squared;
  14326. var c1 = 2.0 * (l0 * l1 - r0r1);
  14327. var c0 = l0 * l0 - r0Squared;
  14328. if (c4 === 0.0 && c3 === 0.0 && c2 === 0.0 && c1 === 0.0) {
  14329. return solutions;
  14330. }
  14331. cosines = QuarticRealPolynomial.computeRealRoots(c4, c3, c2, c1, c0);
  14332. var length = cosines.length;
  14333. if (length === 0) {
  14334. return solutions;
  14335. }
  14336. for ( var i = 0; i < length; ++i) {
  14337. var cosine = cosines[i];
  14338. var cosineSquared = cosine * cosine;
  14339. var sineSquared = Math.max(1.0 - cosineSquared, 0.0);
  14340. var sine = Math.sqrt(sineSquared);
  14341. //var left = l2 * cosineSquared + l1 * cosine + l0;
  14342. var left;
  14343. if (CesiumMath.sign(l2) === CesiumMath.sign(l0)) {
  14344. left = addWithCancellationCheck(l2 * cosineSquared + l0, l1 * cosine, CesiumMath.EPSILON12);
  14345. } else if (CesiumMath.sign(l0) === CesiumMath.sign(l1 * cosine)) {
  14346. left = addWithCancellationCheck(l2 * cosineSquared, l1 * cosine + l0, CesiumMath.EPSILON12);
  14347. } else {
  14348. left = addWithCancellationCheck(l2 * cosineSquared + l1 * cosine, l0, CesiumMath.EPSILON12);
  14349. }
  14350. var right = addWithCancellationCheck(r1 * cosine, r0, CesiumMath.EPSILON15);
  14351. var product = left * right;
  14352. if (product < 0.0) {
  14353. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  14354. } else if (product > 0.0) {
  14355. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  14356. } else if (sine !== 0.0) {
  14357. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  14358. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  14359. ++i;
  14360. } else {
  14361. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  14362. }
  14363. }
  14364. return solutions;
  14365. }
  14366. var firstAxisScratch = new Cartesian3();
  14367. var secondAxisScratch = new Cartesian3();
  14368. var thirdAxisScratch = new Cartesian3();
  14369. var referenceScratch = new Cartesian3();
  14370. var bCart = new Cartesian3();
  14371. var bScratch = new Matrix3();
  14372. var btScratch = new Matrix3();
  14373. var diScratch = new Matrix3();
  14374. var dScratch = new Matrix3();
  14375. var cScratch = new Matrix3();
  14376. var tempMatrix = new Matrix3();
  14377. var aScratch = new Matrix3();
  14378. var sScratch = new Cartesian3();
  14379. var closestScratch = new Cartesian3();
  14380. var surfPointScratch = new Cartographic();
  14381. /**
  14382. * Provides the point along the ray which is nearest to the ellipsoid.
  14383. *
  14384. * @param {Ray} ray The ray.
  14385. * @param {Ellipsoid} ellipsoid The ellipsoid.
  14386. * @returns {Cartesian3} The nearest planetodetic point on the ray.
  14387. */
  14388. IntersectionTests.grazingAltitudeLocation = function(ray, ellipsoid) {
  14389. if (!defined(ray)) {
  14390. throw new DeveloperError('ray is required.');
  14391. }
  14392. if (!defined(ellipsoid)) {
  14393. throw new DeveloperError('ellipsoid is required.');
  14394. }
  14395. var position = ray.origin;
  14396. var direction = ray.direction;
  14397. if (!Cartesian3.equals(position, Cartesian3.ZERO)) {
  14398. var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
  14399. if (Cartesian3.dot(direction, normal) >= 0.0) { // The location provided is the closest point in altitude
  14400. return position;
  14401. }
  14402. }
  14403. var intersects = defined(this.rayEllipsoid(ray, ellipsoid));
  14404. // Compute the scaled direction vector.
  14405. var f = ellipsoid.transformPositionToScaledSpace(direction, firstAxisScratch);
  14406. // Constructs a basis from the unit scaled direction vector. Construct its rotation and transpose.
  14407. var firstAxis = Cartesian3.normalize(f, f);
  14408. var reference = Cartesian3.mostOrthogonalAxis(f, referenceScratch);
  14409. var secondAxis = Cartesian3.normalize(Cartesian3.cross(reference, firstAxis, secondAxisScratch), secondAxisScratch);
  14410. var thirdAxis = Cartesian3.normalize(Cartesian3.cross(firstAxis, secondAxis, thirdAxisScratch), thirdAxisScratch);
  14411. var B = bScratch;
  14412. B[0] = firstAxis.x;
  14413. B[1] = firstAxis.y;
  14414. B[2] = firstAxis.z;
  14415. B[3] = secondAxis.x;
  14416. B[4] = secondAxis.y;
  14417. B[5] = secondAxis.z;
  14418. B[6] = thirdAxis.x;
  14419. B[7] = thirdAxis.y;
  14420. B[8] = thirdAxis.z;
  14421. var B_T = Matrix3.transpose(B, btScratch);
  14422. // Get the scaling matrix and its inverse.
  14423. var D_I = Matrix3.fromScale(ellipsoid.radii, diScratch);
  14424. var D = Matrix3.fromScale(ellipsoid.oneOverRadii, dScratch);
  14425. var C = cScratch;
  14426. C[0] = 0.0;
  14427. C[1] = -direction.z;
  14428. C[2] = direction.y;
  14429. C[3] = direction.z;
  14430. C[4] = 0.0;
  14431. C[5] = -direction.x;
  14432. C[6] = -direction.y;
  14433. C[7] = direction.x;
  14434. C[8] = 0.0;
  14435. var temp = Matrix3.multiply(Matrix3.multiply(B_T, D, tempMatrix), C, tempMatrix);
  14436. var A = Matrix3.multiply(Matrix3.multiply(temp, D_I, aScratch), B, aScratch);
  14437. var b = Matrix3.multiplyByVector(temp, position, bCart);
  14438. // Solve for the solutions to the expression in standard form:
  14439. var solutions = quadraticVectorExpression(A, Cartesian3.negate(b, firstAxisScratch), 0.0, 0.0, 1.0);
  14440. var s;
  14441. var altitude;
  14442. var length = solutions.length;
  14443. if (length > 0) {
  14444. var closest = Cartesian3.clone(Cartesian3.ZERO, closestScratch);
  14445. var maximumValue = Number.NEGATIVE_INFINITY;
  14446. for ( var i = 0; i < length; ++i) {
  14447. s = Matrix3.multiplyByVector(D_I, Matrix3.multiplyByVector(B, solutions[i], sScratch), sScratch);
  14448. var v = Cartesian3.normalize(Cartesian3.subtract(s, position, referenceScratch), referenceScratch);
  14449. var dotProduct = Cartesian3.dot(v, direction);
  14450. if (dotProduct > maximumValue) {
  14451. maximumValue = dotProduct;
  14452. closest = Cartesian3.clone(s, closest);
  14453. }
  14454. }
  14455. var surfacePoint = ellipsoid.cartesianToCartographic(closest, surfPointScratch);
  14456. maximumValue = CesiumMath.clamp(maximumValue, 0.0, 1.0);
  14457. altitude = Cartesian3.magnitude(Cartesian3.subtract(closest, position, referenceScratch)) * Math.sqrt(1.0 - maximumValue * maximumValue);
  14458. altitude = intersects ? -altitude : altitude;
  14459. surfacePoint.height = altitude;
  14460. return ellipsoid.cartographicToCartesian(surfacePoint, new Cartesian3());
  14461. }
  14462. return undefined;
  14463. };
  14464. var lineSegmentPlaneDifference = new Cartesian3();
  14465. /**
  14466. * Computes the intersection of a line segment and a plane.
  14467. *
  14468. * @param {Cartesian3} endPoint0 An end point of the line segment.
  14469. * @param {Cartesian3} endPoint1 The other end point of the line segment.
  14470. * @param {Plane} plane The plane.
  14471. * @param {Cartesian3} [result] The object onto which to store the result.
  14472. * @returns {Cartesian3} The intersection point or undefined if there is no intersection.
  14473. *
  14474. * @example
  14475. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  14476. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  14477. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  14478. *
  14479. * var p0 = new Cesium.Cartesian3(...);
  14480. * var p1 = new Cesium.Cartesian3(...);
  14481. *
  14482. * // find the intersection of the line segment from p0 to p1 and the tangent plane at origin.
  14483. * var intersection = Cesium.IntersectionTests.lineSegmentPlane(p0, p1, plane);
  14484. */
  14485. IntersectionTests.lineSegmentPlane = function(endPoint0, endPoint1, plane, result) {
  14486. if (!defined(endPoint0)) {
  14487. throw new DeveloperError('endPoint0 is required.');
  14488. }
  14489. if (!defined(endPoint1)) {
  14490. throw new DeveloperError('endPoint1 is required.');
  14491. }
  14492. if (!defined(plane)) {
  14493. throw new DeveloperError('plane is required.');
  14494. }
  14495. if (!defined(result)) {
  14496. result = new Cartesian3();
  14497. }
  14498. var difference = Cartesian3.subtract(endPoint1, endPoint0, lineSegmentPlaneDifference);
  14499. var normal = plane.normal;
  14500. var nDotDiff = Cartesian3.dot(normal, difference);
  14501. // check if the segment and plane are parallel
  14502. if (Math.abs(nDotDiff) < CesiumMath.EPSILON6) {
  14503. return undefined;
  14504. }
  14505. var nDotP0 = Cartesian3.dot(normal, endPoint0);
  14506. var t = -(plane.distance + nDotP0) / nDotDiff;
  14507. // intersection only if t is in [0, 1]
  14508. if (t < 0.0 || t > 1.0) {
  14509. return undefined;
  14510. }
  14511. // intersection is endPoint0 + t * (endPoint1 - endPoint0)
  14512. Cartesian3.multiplyByScalar(difference, t, result);
  14513. Cartesian3.add(endPoint0, result, result);
  14514. return result;
  14515. };
  14516. /**
  14517. * Computes the intersection of a triangle and a plane
  14518. *
  14519. * @param {Cartesian3} p0 First point of the triangle
  14520. * @param {Cartesian3} p1 Second point of the triangle
  14521. * @param {Cartesian3} p2 Third point of the triangle
  14522. * @param {Plane} plane Intersection plane
  14523. * @returns {Object} An object with properties <code>positions</code> and <code>indices</code>, which are arrays that represent three triangles that do not cross the plane. (Undefined if no intersection exists)
  14524. *
  14525. * @example
  14526. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  14527. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  14528. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  14529. *
  14530. * var p0 = new Cesium.Cartesian3(...);
  14531. * var p1 = new Cesium.Cartesian3(...);
  14532. * var p2 = new Cesium.Cartesian3(...);
  14533. *
  14534. * // convert the triangle composed of points (p0, p1, p2) to three triangles that don't cross the plane
  14535. * var triangles = Cesium.IntersectionTests.trianglePlaneIntersection(p0, p1, p2, plane);
  14536. */
  14537. IntersectionTests.trianglePlaneIntersection = function(p0, p1, p2, plane) {
  14538. if ((!defined(p0)) ||
  14539. (!defined(p1)) ||
  14540. (!defined(p2)) ||
  14541. (!defined(plane))) {
  14542. throw new DeveloperError('p0, p1, p2, and plane are required.');
  14543. }
  14544. var planeNormal = plane.normal;
  14545. var planeD = plane.distance;
  14546. var p0Behind = (Cartesian3.dot(planeNormal, p0) + planeD) < 0.0;
  14547. var p1Behind = (Cartesian3.dot(planeNormal, p1) + planeD) < 0.0;
  14548. var p2Behind = (Cartesian3.dot(planeNormal, p2) + planeD) < 0.0;
  14549. // Given these dots products, the calls to lineSegmentPlaneIntersection
  14550. // always have defined results.
  14551. var numBehind = 0;
  14552. numBehind += p0Behind ? 1 : 0;
  14553. numBehind += p1Behind ? 1 : 0;
  14554. numBehind += p2Behind ? 1 : 0;
  14555. var u1, u2;
  14556. if (numBehind === 1 || numBehind === 2) {
  14557. u1 = new Cartesian3();
  14558. u2 = new Cartesian3();
  14559. }
  14560. if (numBehind === 1) {
  14561. if (p0Behind) {
  14562. IntersectionTests.lineSegmentPlane(p0, p1, plane, u1);
  14563. IntersectionTests.lineSegmentPlane(p0, p2, plane, u2);
  14564. return {
  14565. positions : [p0, p1, p2, u1, u2 ],
  14566. indices : [
  14567. // Behind
  14568. 0, 3, 4,
  14569. // In front
  14570. 1, 2, 4,
  14571. 1, 4, 3
  14572. ]
  14573. };
  14574. } else if (p1Behind) {
  14575. IntersectionTests.lineSegmentPlane(p1, p2, plane, u1);
  14576. IntersectionTests.lineSegmentPlane(p1, p0, plane, u2);
  14577. return {
  14578. positions : [p0, p1, p2, u1, u2 ],
  14579. indices : [
  14580. // Behind
  14581. 1, 3, 4,
  14582. // In front
  14583. 2, 0, 4,
  14584. 2, 4, 3
  14585. ]
  14586. };
  14587. } else if (p2Behind) {
  14588. IntersectionTests.lineSegmentPlane(p2, p0, plane, u1);
  14589. IntersectionTests.lineSegmentPlane(p2, p1, plane, u2);
  14590. return {
  14591. positions : [p0, p1, p2, u1, u2 ],
  14592. indices : [
  14593. // Behind
  14594. 2, 3, 4,
  14595. // In front
  14596. 0, 1, 4,
  14597. 0, 4, 3
  14598. ]
  14599. };
  14600. }
  14601. } else if (numBehind === 2) {
  14602. if (!p0Behind) {
  14603. IntersectionTests.lineSegmentPlane(p1, p0, plane, u1);
  14604. IntersectionTests.lineSegmentPlane(p2, p0, plane, u2);
  14605. return {
  14606. positions : [p0, p1, p2, u1, u2 ],
  14607. indices : [
  14608. // Behind
  14609. 1, 2, 4,
  14610. 1, 4, 3,
  14611. // In front
  14612. 0, 3, 4
  14613. ]
  14614. };
  14615. } else if (!p1Behind) {
  14616. IntersectionTests.lineSegmentPlane(p2, p1, plane, u1);
  14617. IntersectionTests.lineSegmentPlane(p0, p1, plane, u2);
  14618. return {
  14619. positions : [p0, p1, p2, u1, u2 ],
  14620. indices : [
  14621. // Behind
  14622. 2, 0, 4,
  14623. 2, 4, 3,
  14624. // In front
  14625. 1, 3, 4
  14626. ]
  14627. };
  14628. } else if (!p2Behind) {
  14629. IntersectionTests.lineSegmentPlane(p0, p2, plane, u1);
  14630. IntersectionTests.lineSegmentPlane(p1, p2, plane, u2);
  14631. return {
  14632. positions : [p0, p1, p2, u1, u2 ],
  14633. indices : [
  14634. // Behind
  14635. 0, 1, 4,
  14636. 0, 4, 3,
  14637. // In front
  14638. 2, 3, 4
  14639. ]
  14640. };
  14641. }
  14642. }
  14643. // if numBehind is 3, the triangle is completely behind the plane;
  14644. // otherwise, it is completely in front (numBehind is 0).
  14645. return undefined;
  14646. };
  14647. return IntersectionTests;
  14648. });
  14649. /*global define*/
  14650. define('Core/Plane',[
  14651. './Cartesian3',
  14652. './defined',
  14653. './DeveloperError',
  14654. './freezeObject'
  14655. ], function(
  14656. Cartesian3,
  14657. defined,
  14658. DeveloperError,
  14659. freezeObject) {
  14660. 'use strict';
  14661. /**
  14662. * A plane in Hessian Normal Form defined by
  14663. * <pre>
  14664. * ax + by + cz + d = 0
  14665. * </pre>
  14666. * where (a, b, c) is the plane's <code>normal</code>, d is the signed
  14667. * <code>distance</code> to the plane, and (x, y, z) is any point on
  14668. * the plane.
  14669. *
  14670. * @alias Plane
  14671. * @constructor
  14672. *
  14673. * @param {Cartesian3} normal The plane's normal (normalized).
  14674. * @param {Number} distance The shortest distance from the origin to the plane. The sign of
  14675. * <code>distance</code> determines which side of the plane the origin
  14676. * is on. If <code>distance</code> is positive, the origin is in the half-space
  14677. * in the direction of the normal; if negative, the origin is in the half-space
  14678. * opposite to the normal; if zero, the plane passes through the origin.
  14679. *
  14680. * @example
  14681. * // The plane x=0
  14682. * var plane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X, 0.0);
  14683. */
  14684. function Plane(normal, distance) {
  14685. if (!defined(normal)) {
  14686. throw new DeveloperError('normal is required.');
  14687. }
  14688. if (!defined(distance)) {
  14689. throw new DeveloperError('distance is required.');
  14690. }
  14691. /**
  14692. * The plane's normal.
  14693. *
  14694. * @type {Cartesian3}
  14695. */
  14696. this.normal = Cartesian3.clone(normal);
  14697. /**
  14698. * The shortest distance from the origin to the plane. The sign of
  14699. * <code>distance</code> determines which side of the plane the origin
  14700. * is on. If <code>distance</code> is positive, the origin is in the half-space
  14701. * in the direction of the normal; if negative, the origin is in the half-space
  14702. * opposite to the normal; if zero, the plane passes through the origin.
  14703. *
  14704. * @type {Number}
  14705. */
  14706. this.distance = distance;
  14707. }
  14708. /**
  14709. * Creates a plane from a normal and a point on the plane.
  14710. *
  14711. * @param {Cartesian3} point The point on the plane.
  14712. * @param {Cartesian3} normal The plane's normal (normalized).
  14713. * @param {Plane} [result] The object onto which to store the result.
  14714. * @returns {Plane} A new plane instance or the modified result parameter.
  14715. *
  14716. * @example
  14717. * var point = Cesium.Cartesian3.fromDegrees(-72.0, 40.0);
  14718. * var normal = ellipsoid.geodeticSurfaceNormal(point);
  14719. * var tangentPlane = Cesium.Plane.fromPointNormal(point, normal);
  14720. */
  14721. Plane.fromPointNormal = function(point, normal, result) {
  14722. if (!defined(point)) {
  14723. throw new DeveloperError('point is required.');
  14724. }
  14725. if (!defined(normal)) {
  14726. throw new DeveloperError('normal is required.');
  14727. }
  14728. var distance = -Cartesian3.dot(normal, point);
  14729. if (!defined(result)) {
  14730. return new Plane(normal, distance);
  14731. }
  14732. Cartesian3.clone(normal, result.normal);
  14733. result.distance = distance;
  14734. return result;
  14735. };
  14736. var scratchNormal = new Cartesian3();
  14737. /**
  14738. * Creates a plane from the general equation
  14739. *
  14740. * @param {Cartesian4} coefficients The plane's normal (normalized).
  14741. * @param {Plane} [result] The object onto which to store the result.
  14742. * @returns {Plane} A new plane instance or the modified result parameter.
  14743. */
  14744. Plane.fromCartesian4 = function(coefficients, result) {
  14745. if (!defined(coefficients)) {
  14746. throw new DeveloperError('coefficients is required.');
  14747. }
  14748. var normal = Cartesian3.fromCartesian4(coefficients, scratchNormal);
  14749. var distance = coefficients.w;
  14750. if (!defined(result)) {
  14751. return new Plane(normal, distance);
  14752. } else {
  14753. Cartesian3.clone(normal, result.normal);
  14754. result.distance = distance;
  14755. return result;
  14756. }
  14757. };
  14758. /**
  14759. * Computes the signed shortest distance of a point to a plane.
  14760. * The sign of the distance determines which side of the plane the point
  14761. * is on. If the distance is positive, the point is in the half-space
  14762. * in the direction of the normal; if negative, the point is in the half-space
  14763. * opposite to the normal; if zero, the plane passes through the point.
  14764. *
  14765. * @param {Plane} plane The plane.
  14766. * @param {Cartesian3} point The point.
  14767. * @returns {Number} The signed shortest distance of the point to the plane.
  14768. */
  14769. Plane.getPointDistance = function(plane, point) {
  14770. if (!defined(plane)) {
  14771. throw new DeveloperError('plane is required.');
  14772. }
  14773. if (!defined(point)) {
  14774. throw new DeveloperError('point is required.');
  14775. }
  14776. return Cartesian3.dot(plane.normal, point) + plane.distance;
  14777. };
  14778. /**
  14779. * A constant initialized to the XY plane passing through the origin, with normal in positive Z.
  14780. *
  14781. * @type {Plane}
  14782. * @constant
  14783. */
  14784. Plane.ORIGIN_XY_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Z, 0.0));
  14785. /**
  14786. * A constant initialized to the YZ plane passing through the origin, with normal in positive X.
  14787. *
  14788. * @type {Plane}
  14789. * @constant
  14790. */
  14791. Plane.ORIGIN_YZ_PLANE = freezeObject(new Plane(Cartesian3.UNIT_X, 0.0));
  14792. /**
  14793. * A constant initialized to the ZX plane passing through the origin, with normal in positive Y.
  14794. *
  14795. * @type {Plane}
  14796. * @constant
  14797. */
  14798. Plane.ORIGIN_ZX_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Y, 0.0));
  14799. return Plane;
  14800. });
  14801. /*global define*/
  14802. define('Core/Tipsify',[
  14803. './defaultValue',
  14804. './defined',
  14805. './DeveloperError'
  14806. ], function(
  14807. defaultValue,
  14808. defined,
  14809. DeveloperError) {
  14810. 'use strict';
  14811. /**
  14812. * Encapsulates an algorithm to optimize triangles for the post
  14813. * vertex-shader cache. This is based on the 2007 SIGGRAPH paper
  14814. * 'Fast Triangle Reordering for Vertex Locality and Reduced Overdraw.'
  14815. * The runtime is linear but several passes are made.
  14816. *
  14817. * @exports Tipsify
  14818. *
  14819. * @see <a href='http://gfx.cs.princeton.edu/pubs/Sander_2007_%3ETR/tipsy.pdf'>
  14820. * Fast Triangle Reordering for Vertex Locality and Reduced Overdraw</a>
  14821. * by Sander, Nehab, and Barczak
  14822. *
  14823. * @private
  14824. */
  14825. var Tipsify = {};
  14826. /**
  14827. * Calculates the average cache miss ratio (ACMR) for a given set of indices.
  14828. *
  14829. * @param {Object} options Object with the following properties:
  14830. * @param {Number[]} options.indices Lists triads of numbers corresponding to the indices of the vertices
  14831. * in the vertex buffer that define the geometry's triangles.
  14832. * @param {Number} [options.maximumIndex] The maximum value of the elements in <code>args.indices</code>.
  14833. * If not supplied, this value will be computed.
  14834. * @param {Number} [options.cacheSize=24] The number of vertices that can be stored in the cache at any one time.
  14835. * @returns {Number} The average cache miss ratio (ACMR).
  14836. *
  14837. * @exception {DeveloperError} indices length must be a multiple of three.
  14838. * @exception {DeveloperError} cacheSize must be greater than two.
  14839. *
  14840. * @example
  14841. * var indices = [0, 1, 2, 3, 4, 5];
  14842. * var maxIndex = 5;
  14843. * var cacheSize = 3;
  14844. * var acmr = Cesium.Tipsify.calculateACMR({indices : indices, maxIndex : maxIndex, cacheSize : cacheSize});
  14845. */
  14846. Tipsify.calculateACMR = function(options) {
  14847. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  14848. var indices = options.indices;
  14849. var maximumIndex = options.maximumIndex;
  14850. var cacheSize = defaultValue(options.cacheSize, 24);
  14851. if (!defined(indices)) {
  14852. throw new DeveloperError('indices is required.');
  14853. }
  14854. var numIndices = indices.length;
  14855. if (numIndices < 3 || numIndices % 3 !== 0) {
  14856. throw new DeveloperError('indices length must be a multiple of three.');
  14857. }
  14858. if (maximumIndex <= 0) {
  14859. throw new DeveloperError('maximumIndex must be greater than zero.');
  14860. }
  14861. if (cacheSize < 3) {
  14862. throw new DeveloperError('cacheSize must be greater than two.');
  14863. }
  14864. // Compute the maximumIndex if not given
  14865. if (!defined(maximumIndex)) {
  14866. maximumIndex = 0;
  14867. var currentIndex = 0;
  14868. var intoIndices = indices[currentIndex];
  14869. while (currentIndex < numIndices) {
  14870. if (intoIndices > maximumIndex) {
  14871. maximumIndex = intoIndices;
  14872. }
  14873. ++currentIndex;
  14874. intoIndices = indices[currentIndex];
  14875. }
  14876. }
  14877. // Vertex time stamps
  14878. var vertexTimeStamps = [];
  14879. for ( var i = 0; i < maximumIndex + 1; i++) {
  14880. vertexTimeStamps[i] = 0;
  14881. }
  14882. // Cache processing
  14883. var s = cacheSize + 1;
  14884. for ( var j = 0; j < numIndices; ++j) {
  14885. if ((s - vertexTimeStamps[indices[j]]) > cacheSize) {
  14886. vertexTimeStamps[indices[j]] = s;
  14887. ++s;
  14888. }
  14889. }
  14890. return (s - cacheSize + 1) / (numIndices / 3);
  14891. };
  14892. /**
  14893. * Optimizes triangles for the post-vertex shader cache.
  14894. *
  14895. * @param {Number[]} options.indices Lists triads of numbers corresponding to the indices of the vertices
  14896. * in the vertex buffer that define the geometry's triangles.
  14897. * @param {Number} [options.maximumIndex] The maximum value of the elements in <code>args.indices</code>.
  14898. * If not supplied, this value will be computed.
  14899. * @param {Number} [options.cacheSize=24] The number of vertices that can be stored in the cache at any one time.
  14900. * @returns {Number[]} A list of the input indices in an optimized order.
  14901. *
  14902. * @exception {DeveloperError} indices length must be a multiple of three.
  14903. * @exception {DeveloperError} cacheSize must be greater than two.
  14904. *
  14905. * @example
  14906. * var indices = [0, 1, 2, 3, 4, 5];
  14907. * var maxIndex = 5;
  14908. * var cacheSize = 3;
  14909. * var reorderedIndices = Cesium.Tipsify.tipsify({indices : indices, maxIndex : maxIndex, cacheSize : cacheSize});
  14910. */
  14911. Tipsify.tipsify = function(options) {
  14912. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  14913. var indices = options.indices;
  14914. var maximumIndex = options.maximumIndex;
  14915. var cacheSize = defaultValue(options.cacheSize, 24);
  14916. var cursor;
  14917. function skipDeadEnd(vertices, deadEnd, indices, maximumIndexPlusOne) {
  14918. while (deadEnd.length >= 1) {
  14919. // while the stack is not empty
  14920. var d = deadEnd[deadEnd.length - 1]; // top of the stack
  14921. deadEnd.splice(deadEnd.length - 1, 1); // pop the stack
  14922. if (vertices[d].numLiveTriangles > 0) {
  14923. return d;
  14924. }
  14925. }
  14926. while (cursor < maximumIndexPlusOne) {
  14927. if (vertices[cursor].numLiveTriangles > 0) {
  14928. ++cursor;
  14929. return cursor - 1;
  14930. }
  14931. ++cursor;
  14932. }
  14933. return -1;
  14934. }
  14935. function getNextVertex(indices, cacheSize, oneRing, vertices, s, deadEnd, maximumIndexPlusOne) {
  14936. var n = -1;
  14937. var p;
  14938. var m = -1;
  14939. var itOneRing = 0;
  14940. while (itOneRing < oneRing.length) {
  14941. var index = oneRing[itOneRing];
  14942. if (vertices[index].numLiveTriangles) {
  14943. p = 0;
  14944. if ((s - vertices[index].timeStamp + (2 * vertices[index].numLiveTriangles)) <= cacheSize) {
  14945. p = s - vertices[index].timeStamp;
  14946. }
  14947. if ((p > m) || (m === -1)) {
  14948. m = p;
  14949. n = index;
  14950. }
  14951. }
  14952. ++itOneRing;
  14953. }
  14954. if (n === -1) {
  14955. return skipDeadEnd(vertices, deadEnd, indices, maximumIndexPlusOne);
  14956. }
  14957. return n;
  14958. }
  14959. if (!defined(indices)) {
  14960. throw new DeveloperError('indices is required.');
  14961. }
  14962. var numIndices = indices.length;
  14963. if (numIndices < 3 || numIndices % 3 !== 0) {
  14964. throw new DeveloperError('indices length must be a multiple of three.');
  14965. }
  14966. if (maximumIndex <= 0) {
  14967. throw new DeveloperError('maximumIndex must be greater than zero.');
  14968. }
  14969. if (cacheSize < 3) {
  14970. throw new DeveloperError('cacheSize must be greater than two.');
  14971. }
  14972. // Determine maximum index
  14973. var maximumIndexPlusOne = 0;
  14974. var currentIndex = 0;
  14975. var intoIndices = indices[currentIndex];
  14976. var endIndex = numIndices;
  14977. if (defined(maximumIndex)) {
  14978. maximumIndexPlusOne = maximumIndex + 1;
  14979. } else {
  14980. while (currentIndex < endIndex) {
  14981. if (intoIndices > maximumIndexPlusOne) {
  14982. maximumIndexPlusOne = intoIndices;
  14983. }
  14984. ++currentIndex;
  14985. intoIndices = indices[currentIndex];
  14986. }
  14987. if (maximumIndexPlusOne === -1) {
  14988. return 0;
  14989. }
  14990. ++maximumIndexPlusOne;
  14991. }
  14992. // Vertices
  14993. var vertices = [];
  14994. for ( var i = 0; i < maximumIndexPlusOne; i++) {
  14995. vertices[i] = {
  14996. numLiveTriangles : 0,
  14997. timeStamp : 0,
  14998. vertexTriangles : []
  14999. };
  15000. }
  15001. currentIndex = 0;
  15002. var triangle = 0;
  15003. while (currentIndex < endIndex) {
  15004. vertices[indices[currentIndex]].vertexTriangles.push(triangle);
  15005. ++(vertices[indices[currentIndex]]).numLiveTriangles;
  15006. vertices[indices[currentIndex + 1]].vertexTriangles.push(triangle);
  15007. ++(vertices[indices[currentIndex + 1]]).numLiveTriangles;
  15008. vertices[indices[currentIndex + 2]].vertexTriangles.push(triangle);
  15009. ++(vertices[indices[currentIndex + 2]]).numLiveTriangles;
  15010. ++triangle;
  15011. currentIndex += 3;
  15012. }
  15013. // Starting index
  15014. var f = 0;
  15015. // Time Stamp
  15016. var s = cacheSize + 1;
  15017. cursor = 1;
  15018. // Process
  15019. var oneRing = [];
  15020. var deadEnd = []; //Stack
  15021. var vertex;
  15022. var intoVertices;
  15023. var currentOutputIndex = 0;
  15024. var outputIndices = [];
  15025. var numTriangles = numIndices / 3;
  15026. var triangleEmitted = [];
  15027. for (i = 0; i < numTriangles; i++) {
  15028. triangleEmitted[i] = false;
  15029. }
  15030. var index;
  15031. var limit;
  15032. while (f !== -1) {
  15033. oneRing = [];
  15034. intoVertices = vertices[f];
  15035. limit = intoVertices.vertexTriangles.length;
  15036. for ( var k = 0; k < limit; ++k) {
  15037. triangle = intoVertices.vertexTriangles[k];
  15038. if (!triangleEmitted[triangle]) {
  15039. triangleEmitted[triangle] = true;
  15040. currentIndex = triangle + triangle + triangle;
  15041. for ( var j = 0; j < 3; ++j) {
  15042. // Set this index as a possible next index
  15043. index = indices[currentIndex];
  15044. oneRing.push(index);
  15045. deadEnd.push(index);
  15046. // Output index
  15047. outputIndices[currentOutputIndex] = index;
  15048. ++currentOutputIndex;
  15049. // Cache processing
  15050. vertex = vertices[index];
  15051. --vertex.numLiveTriangles;
  15052. if ((s - vertex.timeStamp) > cacheSize) {
  15053. vertex.timeStamp = s;
  15054. ++s;
  15055. }
  15056. ++currentIndex;
  15057. }
  15058. }
  15059. }
  15060. f = getNextVertex(indices, cacheSize, oneRing, vertices, s, deadEnd, maximumIndexPlusOne);
  15061. }
  15062. return outputIndices;
  15063. };
  15064. return Tipsify;
  15065. });
  15066. /*global define*/
  15067. define('Core/GeometryPipeline',[
  15068. './AttributeCompression',
  15069. './barycentricCoordinates',
  15070. './BoundingSphere',
  15071. './Cartesian2',
  15072. './Cartesian3',
  15073. './Cartesian4',
  15074. './Cartographic',
  15075. './ComponentDatatype',
  15076. './defaultValue',
  15077. './defined',
  15078. './DeveloperError',
  15079. './EncodedCartesian3',
  15080. './GeographicProjection',
  15081. './Geometry',
  15082. './GeometryAttribute',
  15083. './GeometryType',
  15084. './IndexDatatype',
  15085. './Intersect',
  15086. './IntersectionTests',
  15087. './Math',
  15088. './Matrix3',
  15089. './Matrix4',
  15090. './Plane',
  15091. './PrimitiveType',
  15092. './Tipsify'
  15093. ], function(
  15094. AttributeCompression,
  15095. barycentricCoordinates,
  15096. BoundingSphere,
  15097. Cartesian2,
  15098. Cartesian3,
  15099. Cartesian4,
  15100. Cartographic,
  15101. ComponentDatatype,
  15102. defaultValue,
  15103. defined,
  15104. DeveloperError,
  15105. EncodedCartesian3,
  15106. GeographicProjection,
  15107. Geometry,
  15108. GeometryAttribute,
  15109. GeometryType,
  15110. IndexDatatype,
  15111. Intersect,
  15112. IntersectionTests,
  15113. CesiumMath,
  15114. Matrix3,
  15115. Matrix4,
  15116. Plane,
  15117. PrimitiveType,
  15118. Tipsify) {
  15119. 'use strict';
  15120. /**
  15121. * Content pipeline functions for geometries.
  15122. *
  15123. * @exports GeometryPipeline
  15124. *
  15125. * @see Geometry
  15126. */
  15127. var GeometryPipeline = {};
  15128. function addTriangle(lines, index, i0, i1, i2) {
  15129. lines[index++] = i0;
  15130. lines[index++] = i1;
  15131. lines[index++] = i1;
  15132. lines[index++] = i2;
  15133. lines[index++] = i2;
  15134. lines[index] = i0;
  15135. }
  15136. function trianglesToLines(triangles) {
  15137. var count = triangles.length;
  15138. var size = (count / 3) * 6;
  15139. var lines = IndexDatatype.createTypedArray(count, size);
  15140. var index = 0;
  15141. for ( var i = 0; i < count; i += 3, index += 6) {
  15142. addTriangle(lines, index, triangles[i], triangles[i + 1], triangles[i + 2]);
  15143. }
  15144. return lines;
  15145. }
  15146. function triangleStripToLines(triangles) {
  15147. var count = triangles.length;
  15148. if (count >= 3) {
  15149. var size = (count - 2) * 6;
  15150. var lines = IndexDatatype.createTypedArray(count, size);
  15151. addTriangle(lines, 0, triangles[0], triangles[1], triangles[2]);
  15152. var index = 6;
  15153. for ( var i = 3; i < count; ++i, index += 6) {
  15154. addTriangle(lines, index, triangles[i - 1], triangles[i], triangles[i - 2]);
  15155. }
  15156. return lines;
  15157. }
  15158. return new Uint16Array();
  15159. }
  15160. function triangleFanToLines(triangles) {
  15161. if (triangles.length > 0) {
  15162. var count = triangles.length - 1;
  15163. var size = (count - 1) * 6;
  15164. var lines = IndexDatatype.createTypedArray(count, size);
  15165. var base = triangles[0];
  15166. var index = 0;
  15167. for ( var i = 1; i < count; ++i, index += 6) {
  15168. addTriangle(lines, index, base, triangles[i], triangles[i + 1]);
  15169. }
  15170. return lines;
  15171. }
  15172. return new Uint16Array();
  15173. }
  15174. /**
  15175. * Converts a geometry's triangle indices to line indices. If the geometry has an <code>indices</code>
  15176. * and its <code>primitiveType</code> is <code>TRIANGLES</code>, <code>TRIANGLE_STRIP</code>,
  15177. * <code>TRIANGLE_FAN</code>, it is converted to <code>LINES</code>; otherwise, the geometry is not changed.
  15178. * <p>
  15179. * This is commonly used to create a wireframe geometry for visual debugging.
  15180. * </p>
  15181. *
  15182. * @param {Geometry} geometry The geometry to modify.
  15183. * @returns {Geometry} The modified <code>geometry</code> argument, with its triangle indices converted to lines.
  15184. *
  15185. * @exception {DeveloperError} geometry.primitiveType must be TRIANGLES, TRIANGLE_STRIP, or TRIANGLE_FAN.
  15186. *
  15187. * @example
  15188. * geometry = Cesium.GeometryPipeline.toWireframe(geometry);
  15189. */
  15190. GeometryPipeline.toWireframe = function(geometry) {
  15191. if (!defined(geometry)) {
  15192. throw new DeveloperError('geometry is required.');
  15193. }
  15194. var indices = geometry.indices;
  15195. if (defined(indices)) {
  15196. switch (geometry.primitiveType) {
  15197. case PrimitiveType.TRIANGLES:
  15198. geometry.indices = trianglesToLines(indices);
  15199. break;
  15200. case PrimitiveType.TRIANGLE_STRIP:
  15201. geometry.indices = triangleStripToLines(indices);
  15202. break;
  15203. case PrimitiveType.TRIANGLE_FAN:
  15204. geometry.indices = triangleFanToLines(indices);
  15205. break;
  15206. default:
  15207. throw new DeveloperError('geometry.primitiveType must be TRIANGLES, TRIANGLE_STRIP, or TRIANGLE_FAN.');
  15208. }
  15209. geometry.primitiveType = PrimitiveType.LINES;
  15210. }
  15211. return geometry;
  15212. };
  15213. /**
  15214. * Creates a new {@link Geometry} with <code>LINES</code> representing the provided
  15215. * attribute (<code>attributeName</code>) for the provided geometry. This is used to
  15216. * visualize vector attributes like normals, binormals, and tangents.
  15217. *
  15218. * @param {Geometry} geometry The <code>Geometry</code> instance with the attribute.
  15219. * @param {String} [attributeName='normal'] The name of the attribute.
  15220. * @param {Number} [length=10000.0] The length of each line segment in meters. This can be negative to point the vector in the opposite direction.
  15221. * @returns {Geometry} A new <code>Geometry</code> instance with line segments for the vector.
  15222. *
  15223. * @exception {DeveloperError} geometry.attributes must have an attribute with the same name as the attributeName parameter.
  15224. *
  15225. * @example
  15226. * var geometry = Cesium.GeometryPipeline.createLineSegmentsForVectors(instance.geometry, 'binormal', 100000.0);
  15227. */
  15228. GeometryPipeline.createLineSegmentsForVectors = function(geometry, attributeName, length) {
  15229. attributeName = defaultValue(attributeName, 'normal');
  15230. if (!defined(geometry)) {
  15231. throw new DeveloperError('geometry is required.');
  15232. }
  15233. if (!defined(geometry.attributes.position)) {
  15234. throw new DeveloperError('geometry.attributes.position is required.');
  15235. }
  15236. if (!defined(geometry.attributes[attributeName])) {
  15237. throw new DeveloperError('geometry.attributes must have an attribute with the same name as the attributeName parameter, ' + attributeName + '.');
  15238. }
  15239. length = defaultValue(length, 10000.0);
  15240. var positions = geometry.attributes.position.values;
  15241. var vectors = geometry.attributes[attributeName].values;
  15242. var positionsLength = positions.length;
  15243. var newPositions = new Float64Array(2 * positionsLength);
  15244. var j = 0;
  15245. for (var i = 0; i < positionsLength; i += 3) {
  15246. newPositions[j++] = positions[i];
  15247. newPositions[j++] = positions[i + 1];
  15248. newPositions[j++] = positions[i + 2];
  15249. newPositions[j++] = positions[i] + (vectors[i] * length);
  15250. newPositions[j++] = positions[i + 1] + (vectors[i + 1] * length);
  15251. newPositions[j++] = positions[i + 2] + (vectors[i + 2] * length);
  15252. }
  15253. var newBoundingSphere;
  15254. var bs = geometry.boundingSphere;
  15255. if (defined(bs)) {
  15256. newBoundingSphere = new BoundingSphere(bs.center, bs.radius + length);
  15257. }
  15258. return new Geometry({
  15259. attributes : {
  15260. position : new GeometryAttribute({
  15261. componentDatatype : ComponentDatatype.DOUBLE,
  15262. componentsPerAttribute : 3,
  15263. values : newPositions
  15264. })
  15265. },
  15266. primitiveType : PrimitiveType.LINES,
  15267. boundingSphere : newBoundingSphere
  15268. });
  15269. };
  15270. /**
  15271. * Creates an object that maps attribute names to unique locations (indices)
  15272. * for matching vertex attributes and shader programs.
  15273. *
  15274. * @param {Geometry} geometry The geometry, which is not modified, to create the object for.
  15275. * @returns {Object} An object with attribute name / index pairs.
  15276. *
  15277. * @example
  15278. * var attributeLocations = Cesium.GeometryPipeline.createAttributeLocations(geometry);
  15279. * // Example output
  15280. * // {
  15281. * // 'position' : 0,
  15282. * // 'normal' : 1
  15283. * // }
  15284. */
  15285. GeometryPipeline.createAttributeLocations = function(geometry) {
  15286. if (!defined(geometry)) {
  15287. throw new DeveloperError('geometry is required.');
  15288. }
  15289. // There can be a WebGL performance hit when attribute 0 is disabled, so
  15290. // assign attribute locations to well-known attributes.
  15291. var semantics = [
  15292. 'position',
  15293. 'positionHigh',
  15294. 'positionLow',
  15295. // From VertexFormat.position - after 2D projection and high-precision encoding
  15296. 'position3DHigh',
  15297. 'position3DLow',
  15298. 'position2DHigh',
  15299. 'position2DLow',
  15300. // From Primitive
  15301. 'pickColor',
  15302. // From VertexFormat
  15303. 'normal',
  15304. 'st',
  15305. 'binormal',
  15306. 'tangent',
  15307. // From compressing texture coordinates and normals
  15308. 'compressedAttributes'
  15309. ];
  15310. var attributes = geometry.attributes;
  15311. var indices = {};
  15312. var j = 0;
  15313. var i;
  15314. var len = semantics.length;
  15315. // Attribute locations for well-known attributes
  15316. for (i = 0; i < len; ++i) {
  15317. var semantic = semantics[i];
  15318. if (defined(attributes[semantic])) {
  15319. indices[semantic] = j++;
  15320. }
  15321. }
  15322. // Locations for custom attributes
  15323. for (var name in attributes) {
  15324. if (attributes.hasOwnProperty(name) && (!defined(indices[name]))) {
  15325. indices[name] = j++;
  15326. }
  15327. }
  15328. return indices;
  15329. };
  15330. /**
  15331. * Reorders a geometry's attributes and <code>indices</code> to achieve better performance from the GPU's pre-vertex-shader cache.
  15332. *
  15333. * @param {Geometry} geometry The geometry to modify.
  15334. * @returns {Geometry} The modified <code>geometry</code> argument, with its attributes and indices reordered for the GPU's pre-vertex-shader cache.
  15335. *
  15336. * @exception {DeveloperError} Each attribute array in geometry.attributes must have the same number of attributes.
  15337. *
  15338. *
  15339. * @example
  15340. * geometry = Cesium.GeometryPipeline.reorderForPreVertexCache(geometry);
  15341. *
  15342. * @see GeometryPipeline.reorderForPostVertexCache
  15343. */
  15344. GeometryPipeline.reorderForPreVertexCache = function(geometry) {
  15345. if (!defined(geometry)) {
  15346. throw new DeveloperError('geometry is required.');
  15347. }
  15348. var numVertices = Geometry.computeNumberOfVertices(geometry);
  15349. var indices = geometry.indices;
  15350. if (defined(indices)) {
  15351. var indexCrossReferenceOldToNew = new Int32Array(numVertices);
  15352. for ( var i = 0; i < numVertices; i++) {
  15353. indexCrossReferenceOldToNew[i] = -1;
  15354. }
  15355. // Construct cross reference and reorder indices
  15356. var indicesIn = indices;
  15357. var numIndices = indicesIn.length;
  15358. var indicesOut = IndexDatatype.createTypedArray(numVertices, numIndices);
  15359. var intoIndicesIn = 0;
  15360. var intoIndicesOut = 0;
  15361. var nextIndex = 0;
  15362. var tempIndex;
  15363. while (intoIndicesIn < numIndices) {
  15364. tempIndex = indexCrossReferenceOldToNew[indicesIn[intoIndicesIn]];
  15365. if (tempIndex !== -1) {
  15366. indicesOut[intoIndicesOut] = tempIndex;
  15367. } else {
  15368. tempIndex = indicesIn[intoIndicesIn];
  15369. indexCrossReferenceOldToNew[tempIndex] = nextIndex;
  15370. indicesOut[intoIndicesOut] = nextIndex;
  15371. ++nextIndex;
  15372. }
  15373. ++intoIndicesIn;
  15374. ++intoIndicesOut;
  15375. }
  15376. geometry.indices = indicesOut;
  15377. // Reorder attributes
  15378. var attributes = geometry.attributes;
  15379. for ( var property in attributes) {
  15380. if (attributes.hasOwnProperty(property) &&
  15381. defined(attributes[property]) &&
  15382. defined(attributes[property].values)) {
  15383. var attribute = attributes[property];
  15384. var elementsIn = attribute.values;
  15385. var intoElementsIn = 0;
  15386. var numComponents = attribute.componentsPerAttribute;
  15387. var elementsOut = ComponentDatatype.createTypedArray(attribute.componentDatatype, nextIndex * numComponents);
  15388. while (intoElementsIn < numVertices) {
  15389. var temp = indexCrossReferenceOldToNew[intoElementsIn];
  15390. if (temp !== -1) {
  15391. for (i = 0; i < numComponents; i++) {
  15392. elementsOut[numComponents * temp + i] = elementsIn[numComponents * intoElementsIn + i];
  15393. }
  15394. }
  15395. ++intoElementsIn;
  15396. }
  15397. attribute.values = elementsOut;
  15398. }
  15399. }
  15400. }
  15401. return geometry;
  15402. };
  15403. /**
  15404. * Reorders a geometry's <code>indices</code> to achieve better performance from the GPU's
  15405. * post vertex-shader cache by using the Tipsify algorithm. If the geometry <code>primitiveType</code>
  15406. * is not <code>TRIANGLES</code> or the geometry does not have an <code>indices</code>, this function has no effect.
  15407. *
  15408. * @param {Geometry} geometry The geometry to modify.
  15409. * @param {Number} [cacheCapacity=24] The number of vertices that can be held in the GPU's vertex cache.
  15410. * @returns {Geometry} The modified <code>geometry</code> argument, with its indices reordered for the post-vertex-shader cache.
  15411. *
  15412. * @exception {DeveloperError} cacheCapacity must be greater than two.
  15413. *
  15414. *
  15415. * @example
  15416. * geometry = Cesium.GeometryPipeline.reorderForPostVertexCache(geometry);
  15417. *
  15418. * @see GeometryPipeline.reorderForPreVertexCache
  15419. * @see {@link http://gfx.cs.princ0eton.edu/pubs/Sander_2007_%3ETR/tipsy.pdf|Fast Triangle Reordering for Vertex Locality and Reduced Overdraw}
  15420. * by Sander, Nehab, and Barczak
  15421. */
  15422. GeometryPipeline.reorderForPostVertexCache = function(geometry, cacheCapacity) {
  15423. if (!defined(geometry)) {
  15424. throw new DeveloperError('geometry is required.');
  15425. }
  15426. var indices = geometry.indices;
  15427. if ((geometry.primitiveType === PrimitiveType.TRIANGLES) && (defined(indices))) {
  15428. var numIndices = indices.length;
  15429. var maximumIndex = 0;
  15430. for ( var j = 0; j < numIndices; j++) {
  15431. if (indices[j] > maximumIndex) {
  15432. maximumIndex = indices[j];
  15433. }
  15434. }
  15435. geometry.indices = Tipsify.tipsify({
  15436. indices : indices,
  15437. maximumIndex : maximumIndex,
  15438. cacheSize : cacheCapacity
  15439. });
  15440. }
  15441. return geometry;
  15442. };
  15443. function copyAttributesDescriptions(attributes) {
  15444. var newAttributes = {};
  15445. for ( var attribute in attributes) {
  15446. if (attributes.hasOwnProperty(attribute) &&
  15447. defined(attributes[attribute]) &&
  15448. defined(attributes[attribute].values)) {
  15449. var attr = attributes[attribute];
  15450. newAttributes[attribute] = new GeometryAttribute({
  15451. componentDatatype : attr.componentDatatype,
  15452. componentsPerAttribute : attr.componentsPerAttribute,
  15453. normalize : attr.normalize,
  15454. values : []
  15455. });
  15456. }
  15457. }
  15458. return newAttributes;
  15459. }
  15460. function copyVertex(destinationAttributes, sourceAttributes, index) {
  15461. for ( var attribute in sourceAttributes) {
  15462. if (sourceAttributes.hasOwnProperty(attribute) &&
  15463. defined(sourceAttributes[attribute]) &&
  15464. defined(sourceAttributes[attribute].values)) {
  15465. var attr = sourceAttributes[attribute];
  15466. for ( var k = 0; k < attr.componentsPerAttribute; ++k) {
  15467. destinationAttributes[attribute].values.push(attr.values[(index * attr.componentsPerAttribute) + k]);
  15468. }
  15469. }
  15470. }
  15471. }
  15472. /**
  15473. * Splits a geometry into multiple geometries, if necessary, to ensure that indices in the
  15474. * <code>indices</code> fit into unsigned shorts. This is used to meet the WebGL requirements
  15475. * when unsigned int indices are not supported.
  15476. * <p>
  15477. * If the geometry does not have any <code>indices</code>, this function has no effect.
  15478. * </p>
  15479. *
  15480. * @param {Geometry} geometry The geometry to be split into multiple geometries.
  15481. * @returns {Geometry[]} An array of geometries, each with indices that fit into unsigned shorts.
  15482. *
  15483. * @exception {DeveloperError} geometry.primitiveType must equal to PrimitiveType.TRIANGLES, PrimitiveType.LINES, or PrimitiveType.POINTS
  15484. * @exception {DeveloperError} All geometry attribute lists must have the same number of attributes.
  15485. *
  15486. * @example
  15487. * var geometries = Cesium.GeometryPipeline.fitToUnsignedShortIndices(geometry);
  15488. */
  15489. GeometryPipeline.fitToUnsignedShortIndices = function(geometry) {
  15490. if (!defined(geometry)) {
  15491. throw new DeveloperError('geometry is required.');
  15492. }
  15493. if ((defined(geometry.indices)) &&
  15494. ((geometry.primitiveType !== PrimitiveType.TRIANGLES) &&
  15495. (geometry.primitiveType !== PrimitiveType.LINES) &&
  15496. (geometry.primitiveType !== PrimitiveType.POINTS))) {
  15497. throw new DeveloperError('geometry.primitiveType must equal to PrimitiveType.TRIANGLES, PrimitiveType.LINES, or PrimitiveType.POINTS.');
  15498. }
  15499. var geometries = [];
  15500. // If there's an index list and more than 64K attributes, it is possible that
  15501. // some indices are outside the range of unsigned short [0, 64K - 1]
  15502. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  15503. if (defined(geometry.indices) && (numberOfVertices >= CesiumMath.SIXTY_FOUR_KILOBYTES)) {
  15504. var oldToNewIndex = [];
  15505. var newIndices = [];
  15506. var currentIndex = 0;
  15507. var newAttributes = copyAttributesDescriptions(geometry.attributes);
  15508. var originalIndices = geometry.indices;
  15509. var numberOfIndices = originalIndices.length;
  15510. var indicesPerPrimitive;
  15511. if (geometry.primitiveType === PrimitiveType.TRIANGLES) {
  15512. indicesPerPrimitive = 3;
  15513. } else if (geometry.primitiveType === PrimitiveType.LINES) {
  15514. indicesPerPrimitive = 2;
  15515. } else if (geometry.primitiveType === PrimitiveType.POINTS) {
  15516. indicesPerPrimitive = 1;
  15517. }
  15518. for ( var j = 0; j < numberOfIndices; j += indicesPerPrimitive) {
  15519. for (var k = 0; k < indicesPerPrimitive; ++k) {
  15520. var x = originalIndices[j + k];
  15521. var i = oldToNewIndex[x];
  15522. if (!defined(i)) {
  15523. i = currentIndex++;
  15524. oldToNewIndex[x] = i;
  15525. copyVertex(newAttributes, geometry.attributes, x);
  15526. }
  15527. newIndices.push(i);
  15528. }
  15529. if (currentIndex + indicesPerPrimitive >= CesiumMath.SIXTY_FOUR_KILOBYTES) {
  15530. geometries.push(new Geometry({
  15531. attributes : newAttributes,
  15532. indices : newIndices,
  15533. primitiveType : geometry.primitiveType,
  15534. boundingSphere : geometry.boundingSphere,
  15535. boundingSphereCV : geometry.boundingSphereCV
  15536. }));
  15537. // Reset for next vertex-array
  15538. oldToNewIndex = [];
  15539. newIndices = [];
  15540. currentIndex = 0;
  15541. newAttributes = copyAttributesDescriptions(geometry.attributes);
  15542. }
  15543. }
  15544. if (newIndices.length !== 0) {
  15545. geometries.push(new Geometry({
  15546. attributes : newAttributes,
  15547. indices : newIndices,
  15548. primitiveType : geometry.primitiveType,
  15549. boundingSphere : geometry.boundingSphere,
  15550. boundingSphereCV : geometry.boundingSphereCV
  15551. }));
  15552. }
  15553. } else {
  15554. // No need to split into multiple geometries
  15555. geometries.push(geometry);
  15556. }
  15557. return geometries;
  15558. };
  15559. var scratchProjectTo2DCartesian3 = new Cartesian3();
  15560. var scratchProjectTo2DCartographic = new Cartographic();
  15561. /**
  15562. * Projects a geometry's 3D <code>position</code> attribute to 2D, replacing the <code>position</code>
  15563. * attribute with separate <code>position3D</code> and <code>position2D</code> attributes.
  15564. * <p>
  15565. * If the geometry does not have a <code>position</code>, this function has no effect.
  15566. * </p>
  15567. *
  15568. * @param {Geometry} geometry The geometry to modify.
  15569. * @param {String} attributeName The name of the attribute.
  15570. * @param {String} attributeName3D The name of the attribute in 3D.
  15571. * @param {String} attributeName2D The name of the attribute in 2D.
  15572. * @param {Object} [projection=new GeographicProjection()] The projection to use.
  15573. * @returns {Geometry} The modified <code>geometry</code> argument with <code>position3D</code> and <code>position2D</code> attributes.
  15574. *
  15575. * @exception {DeveloperError} geometry must have attribute matching the attributeName argument.
  15576. * @exception {DeveloperError} The attribute componentDatatype must be ComponentDatatype.DOUBLE.
  15577. * @exception {DeveloperError} Could not project a point to 2D.
  15578. *
  15579. * @example
  15580. * geometry = Cesium.GeometryPipeline.projectTo2D(geometry, 'position', 'position3D', 'position2D');
  15581. */
  15582. GeometryPipeline.projectTo2D = function(geometry, attributeName, attributeName3D, attributeName2D, projection) {
  15583. if (!defined(geometry)) {
  15584. throw new DeveloperError('geometry is required.');
  15585. }
  15586. if (!defined(attributeName)) {
  15587. throw new DeveloperError('attributeName is required.');
  15588. }
  15589. if (!defined(attributeName3D)) {
  15590. throw new DeveloperError('attributeName3D is required.');
  15591. }
  15592. if (!defined(attributeName2D)) {
  15593. throw new DeveloperError('attributeName2D is required.');
  15594. }
  15595. if (!defined(geometry.attributes[attributeName])) {
  15596. throw new DeveloperError('geometry must have attribute matching the attributeName argument: ' + attributeName + '.');
  15597. }
  15598. if (geometry.attributes[attributeName].componentDatatype !== ComponentDatatype.DOUBLE) {
  15599. throw new DeveloperError('The attribute componentDatatype must be ComponentDatatype.DOUBLE.');
  15600. }
  15601. var attribute = geometry.attributes[attributeName];
  15602. projection = (defined(projection)) ? projection : new GeographicProjection();
  15603. var ellipsoid = projection.ellipsoid;
  15604. // Project original values to 2D.
  15605. var values3D = attribute.values;
  15606. var projectedValues = new Float64Array(values3D.length);
  15607. var index = 0;
  15608. for ( var i = 0; i < values3D.length; i += 3) {
  15609. var value = Cartesian3.fromArray(values3D, i, scratchProjectTo2DCartesian3);
  15610. var lonLat = ellipsoid.cartesianToCartographic(value, scratchProjectTo2DCartographic);
  15611. if (!defined(lonLat)) {
  15612. throw new DeveloperError('Could not project point (' + value.x + ', ' + value.y + ', ' + value.z + ') to 2D.');
  15613. }
  15614. var projectedLonLat = projection.project(lonLat, scratchProjectTo2DCartesian3);
  15615. projectedValues[index++] = projectedLonLat.x;
  15616. projectedValues[index++] = projectedLonLat.y;
  15617. projectedValues[index++] = projectedLonLat.z;
  15618. }
  15619. // Rename original cartesians to WGS84 cartesians.
  15620. geometry.attributes[attributeName3D] = attribute;
  15621. // Replace original cartesians with 2D projected cartesians
  15622. geometry.attributes[attributeName2D] = new GeometryAttribute({
  15623. componentDatatype : ComponentDatatype.DOUBLE,
  15624. componentsPerAttribute : 3,
  15625. values : projectedValues
  15626. });
  15627. delete geometry.attributes[attributeName];
  15628. return geometry;
  15629. };
  15630. var encodedResult = {
  15631. high : 0.0,
  15632. low : 0.0
  15633. };
  15634. /**
  15635. * Encodes floating-point geometry attribute values as two separate attributes to improve
  15636. * rendering precision.
  15637. * <p>
  15638. * This is commonly used to create high-precision position vertex attributes.
  15639. * </p>
  15640. *
  15641. * @param {Geometry} geometry The geometry to modify.
  15642. * @param {String} attributeName The name of the attribute.
  15643. * @param {String} attributeHighName The name of the attribute for the encoded high bits.
  15644. * @param {String} attributeLowName The name of the attribute for the encoded low bits.
  15645. * @returns {Geometry} The modified <code>geometry</code> argument, with its encoded attribute.
  15646. *
  15647. * @exception {DeveloperError} geometry must have attribute matching the attributeName argument.
  15648. * @exception {DeveloperError} The attribute componentDatatype must be ComponentDatatype.DOUBLE.
  15649. *
  15650. * @example
  15651. * geometry = Cesium.GeometryPipeline.encodeAttribute(geometry, 'position3D', 'position3DHigh', 'position3DLow');
  15652. */
  15653. GeometryPipeline.encodeAttribute = function(geometry, attributeName, attributeHighName, attributeLowName) {
  15654. if (!defined(geometry)) {
  15655. throw new DeveloperError('geometry is required.');
  15656. }
  15657. if (!defined(attributeName)) {
  15658. throw new DeveloperError('attributeName is required.');
  15659. }
  15660. if (!defined(attributeHighName)) {
  15661. throw new DeveloperError('attributeHighName is required.');
  15662. }
  15663. if (!defined(attributeLowName)) {
  15664. throw new DeveloperError('attributeLowName is required.');
  15665. }
  15666. if (!defined(geometry.attributes[attributeName])) {
  15667. throw new DeveloperError('geometry must have attribute matching the attributeName argument: ' + attributeName + '.');
  15668. }
  15669. if (geometry.attributes[attributeName].componentDatatype !== ComponentDatatype.DOUBLE) {
  15670. throw new DeveloperError('The attribute componentDatatype must be ComponentDatatype.DOUBLE.');
  15671. }
  15672. var attribute = geometry.attributes[attributeName];
  15673. var values = attribute.values;
  15674. var length = values.length;
  15675. var highValues = new Float32Array(length);
  15676. var lowValues = new Float32Array(length);
  15677. for (var i = 0; i < length; ++i) {
  15678. EncodedCartesian3.encode(values[i], encodedResult);
  15679. highValues[i] = encodedResult.high;
  15680. lowValues[i] = encodedResult.low;
  15681. }
  15682. var componentsPerAttribute = attribute.componentsPerAttribute;
  15683. geometry.attributes[attributeHighName] = new GeometryAttribute({
  15684. componentDatatype : ComponentDatatype.FLOAT,
  15685. componentsPerAttribute : componentsPerAttribute,
  15686. values : highValues
  15687. });
  15688. geometry.attributes[attributeLowName] = new GeometryAttribute({
  15689. componentDatatype : ComponentDatatype.FLOAT,
  15690. componentsPerAttribute : componentsPerAttribute,
  15691. values : lowValues
  15692. });
  15693. delete geometry.attributes[attributeName];
  15694. return geometry;
  15695. };
  15696. var scratchCartesian3 = new Cartesian3();
  15697. function transformPoint(matrix, attribute) {
  15698. if (defined(attribute)) {
  15699. var values = attribute.values;
  15700. var length = values.length;
  15701. for (var i = 0; i < length; i += 3) {
  15702. Cartesian3.unpack(values, i, scratchCartesian3);
  15703. Matrix4.multiplyByPoint(matrix, scratchCartesian3, scratchCartesian3);
  15704. Cartesian3.pack(scratchCartesian3, values, i);
  15705. }
  15706. }
  15707. }
  15708. function transformVector(matrix, attribute) {
  15709. if (defined(attribute)) {
  15710. var values = attribute.values;
  15711. var length = values.length;
  15712. for (var i = 0; i < length; i += 3) {
  15713. Cartesian3.unpack(values, i, scratchCartesian3);
  15714. Matrix3.multiplyByVector(matrix, scratchCartesian3, scratchCartesian3);
  15715. scratchCartesian3 = Cartesian3.normalize(scratchCartesian3, scratchCartesian3);
  15716. Cartesian3.pack(scratchCartesian3, values, i);
  15717. }
  15718. }
  15719. }
  15720. var inverseTranspose = new Matrix4();
  15721. var normalMatrix = new Matrix3();
  15722. /**
  15723. * Transforms a geometry instance to world coordinates. This changes
  15724. * the instance's <code>modelMatrix</code> to {@link Matrix4.IDENTITY} and transforms the
  15725. * following attributes if they are present: <code>position</code>, <code>normal</code>,
  15726. * <code>binormal</code>, and <code>tangent</code>.
  15727. *
  15728. * @param {GeometryInstance} instance The geometry instance to modify.
  15729. * @returns {GeometryInstance} The modified <code>instance</code> argument, with its attributes transforms to world coordinates.
  15730. *
  15731. * @example
  15732. * Cesium.GeometryPipeline.transformToWorldCoordinates(instance);
  15733. */
  15734. GeometryPipeline.transformToWorldCoordinates = function(instance) {
  15735. if (!defined(instance)) {
  15736. throw new DeveloperError('instance is required.');
  15737. }
  15738. var modelMatrix = instance.modelMatrix;
  15739. if (Matrix4.equals(modelMatrix, Matrix4.IDENTITY)) {
  15740. // Already in world coordinates
  15741. return instance;
  15742. }
  15743. var attributes = instance.geometry.attributes;
  15744. // Transform attributes in known vertex formats
  15745. transformPoint(modelMatrix, attributes.position);
  15746. transformPoint(modelMatrix, attributes.prevPosition);
  15747. transformPoint(modelMatrix, attributes.nextPosition);
  15748. if ((defined(attributes.normal)) ||
  15749. (defined(attributes.binormal)) ||
  15750. (defined(attributes.tangent))) {
  15751. Matrix4.inverse(modelMatrix, inverseTranspose);
  15752. Matrix4.transpose(inverseTranspose, inverseTranspose);
  15753. Matrix4.getRotation(inverseTranspose, normalMatrix);
  15754. transformVector(normalMatrix, attributes.normal);
  15755. transformVector(normalMatrix, attributes.binormal);
  15756. transformVector(normalMatrix, attributes.tangent);
  15757. }
  15758. var boundingSphere = instance.geometry.boundingSphere;
  15759. if (defined(boundingSphere)) {
  15760. instance.geometry.boundingSphere = BoundingSphere.transform(boundingSphere, modelMatrix, boundingSphere);
  15761. }
  15762. instance.modelMatrix = Matrix4.clone(Matrix4.IDENTITY);
  15763. return instance;
  15764. };
  15765. function findAttributesInAllGeometries(instances, propertyName) {
  15766. var length = instances.length;
  15767. var attributesInAllGeometries = {};
  15768. var attributes0 = instances[0][propertyName].attributes;
  15769. var name;
  15770. for (name in attributes0) {
  15771. if (attributes0.hasOwnProperty(name) &&
  15772. defined(attributes0[name]) &&
  15773. defined(attributes0[name].values)) {
  15774. var attribute = attributes0[name];
  15775. var numberOfComponents = attribute.values.length;
  15776. var inAllGeometries = true;
  15777. // Does this same attribute exist in all geometries?
  15778. for (var i = 1; i < length; ++i) {
  15779. var otherAttribute = instances[i][propertyName].attributes[name];
  15780. if ((!defined(otherAttribute)) ||
  15781. (attribute.componentDatatype !== otherAttribute.componentDatatype) ||
  15782. (attribute.componentsPerAttribute !== otherAttribute.componentsPerAttribute) ||
  15783. (attribute.normalize !== otherAttribute.normalize)) {
  15784. inAllGeometries = false;
  15785. break;
  15786. }
  15787. numberOfComponents += otherAttribute.values.length;
  15788. }
  15789. if (inAllGeometries) {
  15790. attributesInAllGeometries[name] = new GeometryAttribute({
  15791. componentDatatype : attribute.componentDatatype,
  15792. componentsPerAttribute : attribute.componentsPerAttribute,
  15793. normalize : attribute.normalize,
  15794. values : ComponentDatatype.createTypedArray(attribute.componentDatatype, numberOfComponents)
  15795. });
  15796. }
  15797. }
  15798. }
  15799. return attributesInAllGeometries;
  15800. }
  15801. var tempScratch = new Cartesian3();
  15802. function combineGeometries(instances, propertyName) {
  15803. var length = instances.length;
  15804. var name;
  15805. var i;
  15806. var j;
  15807. var k;
  15808. var m = instances[0].modelMatrix;
  15809. var haveIndices = (defined(instances[0][propertyName].indices));
  15810. var primitiveType = instances[0][propertyName].primitiveType;
  15811. for (i = 1; i < length; ++i) {
  15812. if (!Matrix4.equals(instances[i].modelMatrix, m)) {
  15813. throw new DeveloperError('All instances must have the same modelMatrix.');
  15814. }
  15815. if ((defined(instances[i][propertyName].indices)) !== haveIndices) {
  15816. throw new DeveloperError('All instance geometries must have an indices or not have one.');
  15817. }
  15818. if (instances[i][propertyName].primitiveType !== primitiveType) {
  15819. throw new DeveloperError('All instance geometries must have the same primitiveType.');
  15820. }
  15821. }
  15822. // Find subset of attributes in all geometries
  15823. var attributes = findAttributesInAllGeometries(instances, propertyName);
  15824. var values;
  15825. var sourceValues;
  15826. var sourceValuesLength;
  15827. // Combine attributes from each geometry into a single typed array
  15828. for (name in attributes) {
  15829. if (attributes.hasOwnProperty(name)) {
  15830. values = attributes[name].values;
  15831. k = 0;
  15832. for (i = 0; i < length; ++i) {
  15833. sourceValues = instances[i][propertyName].attributes[name].values;
  15834. sourceValuesLength = sourceValues.length;
  15835. for (j = 0; j < sourceValuesLength; ++j) {
  15836. values[k++] = sourceValues[j];
  15837. }
  15838. }
  15839. }
  15840. }
  15841. // Combine index lists
  15842. var indices;
  15843. if (haveIndices) {
  15844. var numberOfIndices = 0;
  15845. for (i = 0; i < length; ++i) {
  15846. numberOfIndices += instances[i][propertyName].indices.length;
  15847. }
  15848. var numberOfVertices = Geometry.computeNumberOfVertices(new Geometry({
  15849. attributes : attributes,
  15850. primitiveType : PrimitiveType.POINTS
  15851. }));
  15852. var destIndices = IndexDatatype.createTypedArray(numberOfVertices, numberOfIndices);
  15853. var destOffset = 0;
  15854. var offset = 0;
  15855. for (i = 0; i < length; ++i) {
  15856. var sourceIndices = instances[i][propertyName].indices;
  15857. var sourceIndicesLen = sourceIndices.length;
  15858. for (k = 0; k < sourceIndicesLen; ++k) {
  15859. destIndices[destOffset++] = offset + sourceIndices[k];
  15860. }
  15861. offset += Geometry.computeNumberOfVertices(instances[i][propertyName]);
  15862. }
  15863. indices = destIndices;
  15864. }
  15865. // Create bounding sphere that includes all instances
  15866. var center = new Cartesian3();
  15867. var radius = 0.0;
  15868. var bs;
  15869. for (i = 0; i < length; ++i) {
  15870. bs = instances[i][propertyName].boundingSphere;
  15871. if (!defined(bs)) {
  15872. // If any geometries have an undefined bounding sphere, then so does the combined geometry
  15873. center = undefined;
  15874. break;
  15875. }
  15876. Cartesian3.add(bs.center, center, center);
  15877. }
  15878. if (defined(center)) {
  15879. Cartesian3.divideByScalar(center, length, center);
  15880. for (i = 0; i < length; ++i) {
  15881. bs = instances[i][propertyName].boundingSphere;
  15882. var tempRadius = Cartesian3.magnitude(Cartesian3.subtract(bs.center, center, tempScratch)) + bs.radius;
  15883. if (tempRadius > radius) {
  15884. radius = tempRadius;
  15885. }
  15886. }
  15887. }
  15888. return new Geometry({
  15889. attributes : attributes,
  15890. indices : indices,
  15891. primitiveType : primitiveType,
  15892. boundingSphere : (defined(center)) ? new BoundingSphere(center, radius) : undefined
  15893. });
  15894. }
  15895. /**
  15896. * Combines geometry from several {@link GeometryInstance} objects into one geometry.
  15897. * This concatenates the attributes, concatenates and adjusts the indices, and creates
  15898. * a bounding sphere encompassing all instances.
  15899. * <p>
  15900. * If the instances do not have the same attributes, a subset of attributes common
  15901. * to all instances is used, and the others are ignored.
  15902. * </p>
  15903. * <p>
  15904. * This is used by {@link Primitive} to efficiently render a large amount of static data.
  15905. * </p>
  15906. *
  15907. * @private
  15908. *
  15909. * @param {GeometryInstance[]} [instances] The array of {@link GeometryInstance} objects whose geometry will be combined.
  15910. * @returns {Geometry} A single geometry created from the provided geometry instances.
  15911. *
  15912. * @exception {DeveloperError} All instances must have the same modelMatrix.
  15913. * @exception {DeveloperError} All instance geometries must have an indices or not have one.
  15914. * @exception {DeveloperError} All instance geometries must have the same primitiveType.
  15915. *
  15916. *
  15917. * @example
  15918. * for (var i = 0; i < instances.length; ++i) {
  15919. * Cesium.GeometryPipeline.transformToWorldCoordinates(instances[i]);
  15920. * }
  15921. * var geometries = Cesium.GeometryPipeline.combineInstances(instances);
  15922. *
  15923. * @see GeometryPipeline.transformToWorldCoordinates
  15924. */
  15925. GeometryPipeline.combineInstances = function(instances) {
  15926. if ((!defined(instances)) || (instances.length < 1)) {
  15927. throw new DeveloperError('instances is required and must have length greater than zero.');
  15928. }
  15929. var instanceGeometry = [];
  15930. var instanceSplitGeometry = [];
  15931. var length = instances.length;
  15932. for (var i = 0; i < length; ++i) {
  15933. var instance = instances[i];
  15934. if (defined(instance.geometry)) {
  15935. instanceGeometry.push(instance);
  15936. } else if (defined(instance.westHemisphereGeometry) && defined(instance.eastHemisphereGeometry)) {
  15937. instanceSplitGeometry.push(instance);
  15938. }
  15939. }
  15940. var geometries = [];
  15941. if (instanceGeometry.length > 0) {
  15942. geometries.push(combineGeometries(instanceGeometry, 'geometry'));
  15943. }
  15944. if (instanceSplitGeometry.length > 0) {
  15945. geometries.push(combineGeometries(instanceSplitGeometry, 'westHemisphereGeometry'));
  15946. geometries.push(combineGeometries(instanceSplitGeometry, 'eastHemisphereGeometry'));
  15947. }
  15948. return geometries;
  15949. };
  15950. var normal = new Cartesian3();
  15951. var v0 = new Cartesian3();
  15952. var v1 = new Cartesian3();
  15953. var v2 = new Cartesian3();
  15954. /**
  15955. * Computes per-vertex normals for a geometry containing <code>TRIANGLES</code> by averaging the normals of
  15956. * all triangles incident to the vertex. The result is a new <code>normal</code> attribute added to the geometry.
  15957. * This assumes a counter-clockwise winding order.
  15958. *
  15959. * @param {Geometry} geometry The geometry to modify.
  15960. * @returns {Geometry} The modified <code>geometry</code> argument with the computed <code>normal</code> attribute.
  15961. *
  15962. * @exception {DeveloperError} geometry.indices length must be greater than 0 and be a multiple of 3.
  15963. * @exception {DeveloperError} geometry.primitiveType must be {@link PrimitiveType.TRIANGLES}.
  15964. *
  15965. * @example
  15966. * Cesium.GeometryPipeline.computeNormal(geometry);
  15967. */
  15968. GeometryPipeline.computeNormal = function(geometry) {
  15969. if (!defined(geometry)) {
  15970. throw new DeveloperError('geometry is required.');
  15971. }
  15972. if (!defined(geometry.attributes.position) || !defined(geometry.attributes.position.values)) {
  15973. throw new DeveloperError('geometry.attributes.position.values is required.');
  15974. }
  15975. if (!defined(geometry.indices)) {
  15976. throw new DeveloperError('geometry.indices is required.');
  15977. }
  15978. if (geometry.indices.length < 2 || geometry.indices.length % 3 !== 0) {
  15979. throw new DeveloperError('geometry.indices length must be greater than 0 and be a multiple of 3.');
  15980. }
  15981. if (geometry.primitiveType !== PrimitiveType.TRIANGLES) {
  15982. throw new DeveloperError('geometry.primitiveType must be PrimitiveType.TRIANGLES.');
  15983. }
  15984. var indices = geometry.indices;
  15985. var attributes = geometry.attributes;
  15986. var vertices = attributes.position.values;
  15987. var numVertices = attributes.position.values.length / 3;
  15988. var numIndices = indices.length;
  15989. var normalsPerVertex = new Array(numVertices);
  15990. var normalsPerTriangle = new Array(numIndices / 3);
  15991. var normalIndices = new Array(numIndices);
  15992. for ( var i = 0; i < numVertices; i++) {
  15993. normalsPerVertex[i] = {
  15994. indexOffset : 0,
  15995. count : 0,
  15996. currentCount : 0
  15997. };
  15998. }
  15999. var j = 0;
  16000. for (i = 0; i < numIndices; i += 3) {
  16001. var i0 = indices[i];
  16002. var i1 = indices[i + 1];
  16003. var i2 = indices[i + 2];
  16004. var i03 = i0 * 3;
  16005. var i13 = i1 * 3;
  16006. var i23 = i2 * 3;
  16007. v0.x = vertices[i03];
  16008. v0.y = vertices[i03 + 1];
  16009. v0.z = vertices[i03 + 2];
  16010. v1.x = vertices[i13];
  16011. v1.y = vertices[i13 + 1];
  16012. v1.z = vertices[i13 + 2];
  16013. v2.x = vertices[i23];
  16014. v2.y = vertices[i23 + 1];
  16015. v2.z = vertices[i23 + 2];
  16016. normalsPerVertex[i0].count++;
  16017. normalsPerVertex[i1].count++;
  16018. normalsPerVertex[i2].count++;
  16019. Cartesian3.subtract(v1, v0, v1);
  16020. Cartesian3.subtract(v2, v0, v2);
  16021. normalsPerTriangle[j] = Cartesian3.cross(v1, v2, new Cartesian3());
  16022. j++;
  16023. }
  16024. var indexOffset = 0;
  16025. for (i = 0; i < numVertices; i++) {
  16026. normalsPerVertex[i].indexOffset += indexOffset;
  16027. indexOffset += normalsPerVertex[i].count;
  16028. }
  16029. j = 0;
  16030. var vertexNormalData;
  16031. for (i = 0; i < numIndices; i += 3) {
  16032. vertexNormalData = normalsPerVertex[indices[i]];
  16033. var index = vertexNormalData.indexOffset + vertexNormalData.currentCount;
  16034. normalIndices[index] = j;
  16035. vertexNormalData.currentCount++;
  16036. vertexNormalData = normalsPerVertex[indices[i + 1]];
  16037. index = vertexNormalData.indexOffset + vertexNormalData.currentCount;
  16038. normalIndices[index] = j;
  16039. vertexNormalData.currentCount++;
  16040. vertexNormalData = normalsPerVertex[indices[i + 2]];
  16041. index = vertexNormalData.indexOffset + vertexNormalData.currentCount;
  16042. normalIndices[index] = j;
  16043. vertexNormalData.currentCount++;
  16044. j++;
  16045. }
  16046. var normalValues = new Float32Array(numVertices * 3);
  16047. for (i = 0; i < numVertices; i++) {
  16048. var i3 = i * 3;
  16049. vertexNormalData = normalsPerVertex[i];
  16050. if (vertexNormalData.count > 0) {
  16051. Cartesian3.clone(Cartesian3.ZERO, normal);
  16052. for (j = 0; j < vertexNormalData.count; j++) {
  16053. Cartesian3.add(normal, normalsPerTriangle[normalIndices[vertexNormalData.indexOffset + j]], normal);
  16054. }
  16055. Cartesian3.normalize(normal, normal);
  16056. normalValues[i3] = normal.x;
  16057. normalValues[i3 + 1] = normal.y;
  16058. normalValues[i3 + 2] = normal.z;
  16059. } else {
  16060. normalValues[i3] = 0.0;
  16061. normalValues[i3 + 1] = 0.0;
  16062. normalValues[i3 + 2] = 1.0;
  16063. }
  16064. }
  16065. geometry.attributes.normal = new GeometryAttribute({
  16066. componentDatatype : ComponentDatatype.FLOAT,
  16067. componentsPerAttribute : 3,
  16068. values : normalValues
  16069. });
  16070. return geometry;
  16071. };
  16072. var normalScratch = new Cartesian3();
  16073. var normalScale = new Cartesian3();
  16074. var tScratch = new Cartesian3();
  16075. /**
  16076. * Computes per-vertex binormals and tangents for a geometry containing <code>TRIANGLES</code>.
  16077. * The result is new <code>binormal</code> and <code>tangent</code> attributes added to the geometry.
  16078. * This assumes a counter-clockwise winding order.
  16079. * <p>
  16080. * Based on <a href="http://www.terathon.com/code/tangent.html">Computing Tangent Space Basis Vectors
  16081. * for an Arbitrary Mesh</a> by Eric Lengyel.
  16082. * </p>
  16083. *
  16084. * @param {Geometry} geometry The geometry to modify.
  16085. * @returns {Geometry} The modified <code>geometry</code> argument with the computed <code>binormal</code> and <code>tangent</code> attributes.
  16086. *
  16087. * @exception {DeveloperError} geometry.indices length must be greater than 0 and be a multiple of 3.
  16088. * @exception {DeveloperError} geometry.primitiveType must be {@link PrimitiveType.TRIANGLES}.
  16089. *
  16090. * @example
  16091. * Cesium.GeometryPipeline.computeBinormalAndTangent(geometry);
  16092. */
  16093. GeometryPipeline.computeBinormalAndTangent = function(geometry) {
  16094. if (!defined(geometry)) {
  16095. throw new DeveloperError('geometry is required.');
  16096. }
  16097. var attributes = geometry.attributes;
  16098. var indices = geometry.indices;
  16099. if (!defined(attributes.position) || !defined(attributes.position.values)) {
  16100. throw new DeveloperError('geometry.attributes.position.values is required.');
  16101. }
  16102. if (!defined(attributes.normal) || !defined(attributes.normal.values)) {
  16103. throw new DeveloperError('geometry.attributes.normal.values is required.');
  16104. }
  16105. if (!defined(attributes.st) || !defined(attributes.st.values)) {
  16106. throw new DeveloperError('geometry.attributes.st.values is required.');
  16107. }
  16108. if (!defined(indices)) {
  16109. throw new DeveloperError('geometry.indices is required.');
  16110. }
  16111. if (indices.length < 2 || indices.length % 3 !== 0) {
  16112. throw new DeveloperError('geometry.indices length must be greater than 0 and be a multiple of 3.');
  16113. }
  16114. if (geometry.primitiveType !== PrimitiveType.TRIANGLES) {
  16115. throw new DeveloperError('geometry.primitiveType must be PrimitiveType.TRIANGLES.');
  16116. }
  16117. var vertices = geometry.attributes.position.values;
  16118. var normals = geometry.attributes.normal.values;
  16119. var st = geometry.attributes.st.values;
  16120. var numVertices = geometry.attributes.position.values.length / 3;
  16121. var numIndices = indices.length;
  16122. var tan1 = new Array(numVertices * 3);
  16123. for ( var i = 0; i < tan1.length; i++) {
  16124. tan1[i] = 0;
  16125. }
  16126. var i03;
  16127. var i13;
  16128. var i23;
  16129. for (i = 0; i < numIndices; i += 3) {
  16130. var i0 = indices[i];
  16131. var i1 = indices[i + 1];
  16132. var i2 = indices[i + 2];
  16133. i03 = i0 * 3;
  16134. i13 = i1 * 3;
  16135. i23 = i2 * 3;
  16136. var i02 = i0 * 2;
  16137. var i12 = i1 * 2;
  16138. var i22 = i2 * 2;
  16139. var ux = vertices[i03];
  16140. var uy = vertices[i03 + 1];
  16141. var uz = vertices[i03 + 2];
  16142. var wx = st[i02];
  16143. var wy = st[i02 + 1];
  16144. var t1 = st[i12 + 1] - wy;
  16145. var t2 = st[i22 + 1] - wy;
  16146. var r = 1.0 / ((st[i12] - wx) * t2 - (st[i22] - wx) * t1);
  16147. var sdirx = (t2 * (vertices[i13] - ux) - t1 * (vertices[i23] - ux)) * r;
  16148. var sdiry = (t2 * (vertices[i13 + 1] - uy) - t1 * (vertices[i23 + 1] - uy)) * r;
  16149. var sdirz = (t2 * (vertices[i13 + 2] - uz) - t1 * (vertices[i23 + 2] - uz)) * r;
  16150. tan1[i03] += sdirx;
  16151. tan1[i03 + 1] += sdiry;
  16152. tan1[i03 + 2] += sdirz;
  16153. tan1[i13] += sdirx;
  16154. tan1[i13 + 1] += sdiry;
  16155. tan1[i13 + 2] += sdirz;
  16156. tan1[i23] += sdirx;
  16157. tan1[i23 + 1] += sdiry;
  16158. tan1[i23 + 2] += sdirz;
  16159. }
  16160. var binormalValues = new Float32Array(numVertices * 3);
  16161. var tangentValues = new Float32Array(numVertices * 3);
  16162. for (i = 0; i < numVertices; i++) {
  16163. i03 = i * 3;
  16164. i13 = i03 + 1;
  16165. i23 = i03 + 2;
  16166. var n = Cartesian3.fromArray(normals, i03, normalScratch);
  16167. var t = Cartesian3.fromArray(tan1, i03, tScratch);
  16168. var scalar = Cartesian3.dot(n, t);
  16169. Cartesian3.multiplyByScalar(n, scalar, normalScale);
  16170. Cartesian3.normalize(Cartesian3.subtract(t, normalScale, t), t);
  16171. tangentValues[i03] = t.x;
  16172. tangentValues[i13] = t.y;
  16173. tangentValues[i23] = t.z;
  16174. Cartesian3.normalize(Cartesian3.cross(n, t, t), t);
  16175. binormalValues[i03] = t.x;
  16176. binormalValues[i13] = t.y;
  16177. binormalValues[i23] = t.z;
  16178. }
  16179. geometry.attributes.tangent = new GeometryAttribute({
  16180. componentDatatype : ComponentDatatype.FLOAT,
  16181. componentsPerAttribute : 3,
  16182. values : tangentValues
  16183. });
  16184. geometry.attributes.binormal = new GeometryAttribute({
  16185. componentDatatype : ComponentDatatype.FLOAT,
  16186. componentsPerAttribute : 3,
  16187. values : binormalValues
  16188. });
  16189. return geometry;
  16190. };
  16191. var scratchCartesian2 = new Cartesian2();
  16192. var toEncode1 = new Cartesian3();
  16193. var toEncode2 = new Cartesian3();
  16194. var toEncode3 = new Cartesian3();
  16195. /**
  16196. * Compresses and packs geometry normal attribute values to save memory.
  16197. *
  16198. * @param {Geometry} geometry The geometry to modify.
  16199. * @returns {Geometry} The modified <code>geometry</code> argument, with its normals compressed and packed.
  16200. *
  16201. * @example
  16202. * geometry = Cesium.GeometryPipeline.compressVertices(geometry);
  16203. */
  16204. GeometryPipeline.compressVertices = function(geometry) {
  16205. if (!defined(geometry)) {
  16206. throw new DeveloperError('geometry is required.');
  16207. }
  16208. var normalAttribute = geometry.attributes.normal;
  16209. var stAttribute = geometry.attributes.st;
  16210. if (!defined(normalAttribute) && !defined(stAttribute)) {
  16211. return geometry;
  16212. }
  16213. var tangentAttribute = geometry.attributes.tangent;
  16214. var binormalAttribute = geometry.attributes.binormal;
  16215. var normals;
  16216. var st;
  16217. var tangents;
  16218. var binormals;
  16219. if (defined(normalAttribute)) {
  16220. normals = normalAttribute.values;
  16221. }
  16222. if (defined(stAttribute)) {
  16223. st = stAttribute.values;
  16224. }
  16225. if (defined(tangentAttribute)) {
  16226. tangents = tangentAttribute.values;
  16227. }
  16228. if (binormalAttribute) {
  16229. binormals = binormalAttribute.values;
  16230. }
  16231. var length = defined(normals) ? normals.length : st.length;
  16232. var numComponents = defined(normals) ? 3.0 : 2.0;
  16233. var numVertices = length / numComponents;
  16234. var compressedLength = numVertices;
  16235. var numCompressedComponents = defined(st) && defined(normals) ? 2.0 : 1.0;
  16236. numCompressedComponents += defined(tangents) || defined(binormals) ? 1.0 : 0.0;
  16237. compressedLength *= numCompressedComponents;
  16238. var compressedAttributes = new Float32Array(compressedLength);
  16239. var normalIndex = 0;
  16240. for (var i = 0; i < numVertices; ++i) {
  16241. if (defined(st)) {
  16242. Cartesian2.fromArray(st, i * 2.0, scratchCartesian2);
  16243. compressedAttributes[normalIndex++] = AttributeCompression.compressTextureCoordinates(scratchCartesian2);
  16244. }
  16245. var index = i * 3.0;
  16246. if (defined(normals) && defined(tangents) && defined(binormals)) {
  16247. Cartesian3.fromArray(normals, index, toEncode1);
  16248. Cartesian3.fromArray(tangents, index, toEncode2);
  16249. Cartesian3.fromArray(binormals, index, toEncode3);
  16250. AttributeCompression.octPack(toEncode1, toEncode2, toEncode3, scratchCartesian2);
  16251. compressedAttributes[normalIndex++] = scratchCartesian2.x;
  16252. compressedAttributes[normalIndex++] = scratchCartesian2.y;
  16253. } else {
  16254. if (defined(normals)) {
  16255. Cartesian3.fromArray(normals, index, toEncode1);
  16256. compressedAttributes[normalIndex++] = AttributeCompression.octEncodeFloat(toEncode1);
  16257. }
  16258. if (defined(tangents)) {
  16259. Cartesian3.fromArray(tangents, index, toEncode1);
  16260. compressedAttributes[normalIndex++] = AttributeCompression.octEncodeFloat(toEncode1);
  16261. }
  16262. if (defined(binormals)) {
  16263. Cartesian3.fromArray(binormals, index, toEncode1);
  16264. compressedAttributes[normalIndex++] = AttributeCompression.octEncodeFloat(toEncode1);
  16265. }
  16266. }
  16267. }
  16268. geometry.attributes.compressedAttributes = new GeometryAttribute({
  16269. componentDatatype : ComponentDatatype.FLOAT,
  16270. componentsPerAttribute : numCompressedComponents,
  16271. values : compressedAttributes
  16272. });
  16273. if (defined(normals)) {
  16274. delete geometry.attributes.normal;
  16275. }
  16276. if (defined(st)) {
  16277. delete geometry.attributes.st;
  16278. }
  16279. if (defined(tangents)) {
  16280. delete geometry.attributes.tangent;
  16281. }
  16282. if (defined(binormals)) {
  16283. delete geometry.attributes.binormal;
  16284. }
  16285. return geometry;
  16286. };
  16287. function indexTriangles(geometry) {
  16288. if (defined(geometry.indices)) {
  16289. return geometry;
  16290. }
  16291. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  16292. if (numberOfVertices < 3) {
  16293. throw new DeveloperError('The number of vertices must be at least three.');
  16294. }
  16295. if (numberOfVertices % 3 !== 0) {
  16296. throw new DeveloperError('The number of vertices must be a multiple of three.');
  16297. }
  16298. var indices = IndexDatatype.createTypedArray(numberOfVertices, numberOfVertices);
  16299. for (var i = 0; i < numberOfVertices; ++i) {
  16300. indices[i] = i;
  16301. }
  16302. geometry.indices = indices;
  16303. return geometry;
  16304. }
  16305. function indexTriangleFan(geometry) {
  16306. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  16307. if (numberOfVertices < 3) {
  16308. throw new DeveloperError('The number of vertices must be at least three.');
  16309. }
  16310. var indices = IndexDatatype.createTypedArray(numberOfVertices, (numberOfVertices - 2) * 3);
  16311. indices[0] = 1;
  16312. indices[1] = 0;
  16313. indices[2] = 2;
  16314. var indicesIndex = 3;
  16315. for (var i = 3; i < numberOfVertices; ++i) {
  16316. indices[indicesIndex++] = i - 1;
  16317. indices[indicesIndex++] = 0;
  16318. indices[indicesIndex++] = i;
  16319. }
  16320. geometry.indices = indices;
  16321. geometry.primitiveType = PrimitiveType.TRIANGLES;
  16322. return geometry;
  16323. }
  16324. function indexTriangleStrip(geometry) {
  16325. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  16326. if (numberOfVertices < 3) {
  16327. throw new DeveloperError('The number of vertices must be at least 3.');
  16328. }
  16329. var indices = IndexDatatype.createTypedArray(numberOfVertices, (numberOfVertices - 2) * 3);
  16330. indices[0] = 0;
  16331. indices[1] = 1;
  16332. indices[2] = 2;
  16333. if (numberOfVertices > 3) {
  16334. indices[3] = 0;
  16335. indices[4] = 2;
  16336. indices[5] = 3;
  16337. }
  16338. var indicesIndex = 6;
  16339. for (var i = 3; i < numberOfVertices - 1; i += 2) {
  16340. indices[indicesIndex++] = i;
  16341. indices[indicesIndex++] = i - 1;
  16342. indices[indicesIndex++] = i + 1;
  16343. if (i + 2 < numberOfVertices) {
  16344. indices[indicesIndex++] = i;
  16345. indices[indicesIndex++] = i + 1;
  16346. indices[indicesIndex++] = i + 2;
  16347. }
  16348. }
  16349. geometry.indices = indices;
  16350. geometry.primitiveType = PrimitiveType.TRIANGLES;
  16351. return geometry;
  16352. }
  16353. function indexLines(geometry) {
  16354. if (defined(geometry.indices)) {
  16355. return geometry;
  16356. }
  16357. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  16358. if (numberOfVertices < 2) {
  16359. throw new DeveloperError('The number of vertices must be at least two.');
  16360. }
  16361. if (numberOfVertices % 2 !== 0) {
  16362. throw new DeveloperError('The number of vertices must be a multiple of 2.');
  16363. }
  16364. var indices = IndexDatatype.createTypedArray(numberOfVertices, numberOfVertices);
  16365. for (var i = 0; i < numberOfVertices; ++i) {
  16366. indices[i] = i;
  16367. }
  16368. geometry.indices = indices;
  16369. return geometry;
  16370. }
  16371. function indexLineStrip(geometry) {
  16372. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  16373. if (numberOfVertices < 2) {
  16374. throw new DeveloperError('The number of vertices must be at least two.');
  16375. }
  16376. var indices = IndexDatatype.createTypedArray(numberOfVertices, (numberOfVertices - 1) * 2);
  16377. indices[0] = 0;
  16378. indices[1] = 1;
  16379. var indicesIndex = 2;
  16380. for (var i = 2; i < numberOfVertices; ++i) {
  16381. indices[indicesIndex++] = i - 1;
  16382. indices[indicesIndex++] = i;
  16383. }
  16384. geometry.indices = indices;
  16385. geometry.primitiveType = PrimitiveType.LINES;
  16386. return geometry;
  16387. }
  16388. function indexLineLoop(geometry) {
  16389. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  16390. if (numberOfVertices < 2) {
  16391. throw new DeveloperError('The number of vertices must be at least two.');
  16392. }
  16393. var indices = IndexDatatype.createTypedArray(numberOfVertices, numberOfVertices * 2);
  16394. indices[0] = 0;
  16395. indices[1] = 1;
  16396. var indicesIndex = 2;
  16397. for (var i = 2; i < numberOfVertices; ++i) {
  16398. indices[indicesIndex++] = i - 1;
  16399. indices[indicesIndex++] = i;
  16400. }
  16401. indices[indicesIndex++] = numberOfVertices - 1;
  16402. indices[indicesIndex] = 0;
  16403. geometry.indices = indices;
  16404. geometry.primitiveType = PrimitiveType.LINES;
  16405. return geometry;
  16406. }
  16407. function indexPrimitive(geometry) {
  16408. switch (geometry.primitiveType) {
  16409. case PrimitiveType.TRIANGLE_FAN:
  16410. return indexTriangleFan(geometry);
  16411. case PrimitiveType.TRIANGLE_STRIP:
  16412. return indexTriangleStrip(geometry);
  16413. case PrimitiveType.TRIANGLES:
  16414. return indexTriangles(geometry);
  16415. case PrimitiveType.LINE_STRIP:
  16416. return indexLineStrip(geometry);
  16417. case PrimitiveType.LINE_LOOP:
  16418. return indexLineLoop(geometry);
  16419. case PrimitiveType.LINES:
  16420. return indexLines(geometry);
  16421. }
  16422. return geometry;
  16423. }
  16424. function offsetPointFromXZPlane(p, isBehind) {
  16425. if (Math.abs(p.y) < CesiumMath.EPSILON6){
  16426. if (isBehind) {
  16427. p.y = -CesiumMath.EPSILON6;
  16428. } else {
  16429. p.y = CesiumMath.EPSILON6;
  16430. }
  16431. }
  16432. }
  16433. function offsetTriangleFromXZPlane(p0, p1, p2) {
  16434. if (p0.y !== 0.0 && p1.y !== 0.0 && p2.y !== 0.0) {
  16435. offsetPointFromXZPlane(p0, p0.y < 0.0);
  16436. offsetPointFromXZPlane(p1, p1.y < 0.0);
  16437. offsetPointFromXZPlane(p2, p2.y < 0.0);
  16438. return;
  16439. }
  16440. var p0y = Math.abs(p0.y);
  16441. var p1y = Math.abs(p1.y);
  16442. var p2y = Math.abs(p2.y);
  16443. var sign;
  16444. if (p0y > p1y) {
  16445. if (p0y > p2y) {
  16446. sign = CesiumMath.sign(p0.y);
  16447. } else {
  16448. sign = CesiumMath.sign(p2.y);
  16449. }
  16450. } else if (p1y > p2y) {
  16451. sign = CesiumMath.sign(p1.y);
  16452. } else {
  16453. sign = CesiumMath.sign(p2.y);
  16454. }
  16455. var isBehind = sign < 0.0;
  16456. offsetPointFromXZPlane(p0, isBehind);
  16457. offsetPointFromXZPlane(p1, isBehind);
  16458. offsetPointFromXZPlane(p2, isBehind);
  16459. }
  16460. var c3 = new Cartesian3();
  16461. function getXZIntersectionOffsetPoints(p, p1, u1, v1) {
  16462. Cartesian3.add(p, Cartesian3.multiplyByScalar(Cartesian3.subtract(p1, p, c3), p.y/(p.y-p1.y), c3), u1);
  16463. Cartesian3.clone(u1, v1);
  16464. offsetPointFromXZPlane(u1, true);
  16465. offsetPointFromXZPlane(v1, false);
  16466. }
  16467. var u1 = new Cartesian3();
  16468. var u2 = new Cartesian3();
  16469. var q1 = new Cartesian3();
  16470. var q2 = new Cartesian3();
  16471. var splitTriangleResult = {
  16472. positions : new Array(7),
  16473. indices : new Array(3 * 3)
  16474. };
  16475. function splitTriangle(p0, p1, p2) {
  16476. // In WGS84 coordinates, for a triangle approximately on the
  16477. // ellipsoid to cross the IDL, first it needs to be on the
  16478. // negative side of the plane x = 0.
  16479. if ((p0.x >= 0.0) || (p1.x >= 0.0) || (p2.x >= 0.0)) {
  16480. return undefined;
  16481. }
  16482. offsetTriangleFromXZPlane(p0, p1, p2);
  16483. var p0Behind = p0.y < 0.0;
  16484. var p1Behind = p1.y < 0.0;
  16485. var p2Behind = p2.y < 0.0;
  16486. var numBehind = 0;
  16487. numBehind += p0Behind ? 1 : 0;
  16488. numBehind += p1Behind ? 1 : 0;
  16489. numBehind += p2Behind ? 1 : 0;
  16490. var indices = splitTriangleResult.indices;
  16491. if (numBehind === 1) {
  16492. indices[1] = 3;
  16493. indices[2] = 4;
  16494. indices[5] = 6;
  16495. indices[7] = 6;
  16496. indices[8] = 5;
  16497. if (p0Behind) {
  16498. getXZIntersectionOffsetPoints(p0, p1, u1, q1);
  16499. getXZIntersectionOffsetPoints(p0, p2, u2, q2);
  16500. indices[0] = 0;
  16501. indices[3] = 1;
  16502. indices[4] = 2;
  16503. indices[6] = 1;
  16504. } else if (p1Behind) {
  16505. getXZIntersectionOffsetPoints(p1, p2, u1, q1);
  16506. getXZIntersectionOffsetPoints(p1, p0, u2, q2);
  16507. indices[0] = 1;
  16508. indices[3] = 2;
  16509. indices[4] = 0;
  16510. indices[6] = 2;
  16511. } else if (p2Behind) {
  16512. getXZIntersectionOffsetPoints(p2, p0, u1, q1);
  16513. getXZIntersectionOffsetPoints(p2, p1, u2, q2);
  16514. indices[0] = 2;
  16515. indices[3] = 0;
  16516. indices[4] = 1;
  16517. indices[6] = 0;
  16518. }
  16519. } else if (numBehind === 2) {
  16520. indices[2] = 4;
  16521. indices[4] = 4;
  16522. indices[5] = 3;
  16523. indices[7] = 5;
  16524. indices[8] = 6;
  16525. if (!p0Behind) {
  16526. getXZIntersectionOffsetPoints(p0, p1, u1, q1);
  16527. getXZIntersectionOffsetPoints(p0, p2, u2, q2);
  16528. indices[0] = 1;
  16529. indices[1] = 2;
  16530. indices[3] = 1;
  16531. indices[6] = 0;
  16532. } else if (!p1Behind) {
  16533. getXZIntersectionOffsetPoints(p1, p2, u1, q1);
  16534. getXZIntersectionOffsetPoints(p1, p0, u2, q2);
  16535. indices[0] = 2;
  16536. indices[1] = 0;
  16537. indices[3] = 2;
  16538. indices[6] = 1;
  16539. } else if (!p2Behind) {
  16540. getXZIntersectionOffsetPoints(p2, p0, u1, q1);
  16541. getXZIntersectionOffsetPoints(p2, p1, u2, q2);
  16542. indices[0] = 0;
  16543. indices[1] = 1;
  16544. indices[3] = 0;
  16545. indices[6] = 2;
  16546. }
  16547. }
  16548. var positions = splitTriangleResult.positions;
  16549. positions[0] = p0;
  16550. positions[1] = p1;
  16551. positions[2] = p2;
  16552. positions.length = 3;
  16553. if (numBehind === 1 || numBehind === 2) {
  16554. positions[3] = u1;
  16555. positions[4] = u2;
  16556. positions[5] = q1;
  16557. positions[6] = q2;
  16558. positions.length = 7;
  16559. }
  16560. return splitTriangleResult;
  16561. }
  16562. function updateGeometryAfterSplit(geometry, computeBoundingSphere) {
  16563. var attributes = geometry.attributes;
  16564. if (attributes.position.values.length === 0) {
  16565. return undefined;
  16566. }
  16567. for (var property in attributes) {
  16568. if (attributes.hasOwnProperty(property) &&
  16569. defined(attributes[property]) &&
  16570. defined(attributes[property].values)) {
  16571. var attribute = attributes[property];
  16572. attribute.values = ComponentDatatype.createTypedArray(attribute.componentDatatype, attribute.values);
  16573. }
  16574. }
  16575. var numberOfVertices = Geometry.computeNumberOfVertices(geometry);
  16576. geometry.indices = IndexDatatype.createTypedArray(numberOfVertices, geometry.indices);
  16577. if (computeBoundingSphere) {
  16578. geometry.boundingSphere = BoundingSphere.fromVertices(attributes.position.values);
  16579. }
  16580. return geometry;
  16581. }
  16582. function copyGeometryForSplit(geometry) {
  16583. var attributes = geometry.attributes;
  16584. var copiedAttributes = {};
  16585. for (var property in attributes) {
  16586. if (attributes.hasOwnProperty(property) &&
  16587. defined(attributes[property]) &&
  16588. defined(attributes[property].values)) {
  16589. var attribute = attributes[property];
  16590. copiedAttributes[property] = new GeometryAttribute({
  16591. componentDatatype : attribute.componentDatatype,
  16592. componentsPerAttribute : attribute.componentsPerAttribute,
  16593. normalize : attribute.normalize,
  16594. values : []
  16595. });
  16596. }
  16597. }
  16598. return new Geometry({
  16599. attributes : copiedAttributes,
  16600. indices : [],
  16601. primitiveType : geometry.primitiveType
  16602. });
  16603. }
  16604. function updateInstanceAfterSplit(instance, westGeometry, eastGeometry) {
  16605. var computeBoundingSphere = defined(instance.geometry.boundingSphere);
  16606. westGeometry = updateGeometryAfterSplit(westGeometry, computeBoundingSphere);
  16607. eastGeometry = updateGeometryAfterSplit(eastGeometry, computeBoundingSphere);
  16608. if (defined(eastGeometry) && !defined(westGeometry)) {
  16609. instance.geometry = eastGeometry;
  16610. } else if (!defined(eastGeometry) && defined(westGeometry)) {
  16611. instance.geometry = westGeometry;
  16612. } else {
  16613. instance.westHemisphereGeometry = westGeometry;
  16614. instance.eastHemisphereGeometry = eastGeometry;
  16615. instance.geometry = undefined;
  16616. }
  16617. }
  16618. var p0Scratch = new Cartesian3();
  16619. var p1Scratch = new Cartesian3();
  16620. var p2Scratch = new Cartesian3();
  16621. var barycentricScratch = new Cartesian3();
  16622. var s0Scratch = new Cartesian2();
  16623. var s1Scratch = new Cartesian2();
  16624. var s2Scratch = new Cartesian2();
  16625. function computeTriangleAttributes(i0, i1, i2, point, positions, normals, binormals, tangents, texCoords, currentAttributes, insertedIndex) {
  16626. if (!defined(normals) && !defined(binormals) && !defined(tangents) && !defined(texCoords)) {
  16627. return;
  16628. }
  16629. var p0 = Cartesian3.fromArray(positions, i0 * 3, p0Scratch);
  16630. var p1 = Cartesian3.fromArray(positions, i1 * 3, p1Scratch);
  16631. var p2 = Cartesian3.fromArray(positions, i2 * 3, p2Scratch);
  16632. var coords = barycentricCoordinates(point, p0, p1, p2, barycentricScratch);
  16633. if (defined(normals)) {
  16634. var n0 = Cartesian3.fromArray(normals, i0 * 3, p0Scratch);
  16635. var n1 = Cartesian3.fromArray(normals, i1 * 3, p1Scratch);
  16636. var n2 = Cartesian3.fromArray(normals, i2 * 3, p2Scratch);
  16637. Cartesian3.multiplyByScalar(n0, coords.x, n0);
  16638. Cartesian3.multiplyByScalar(n1, coords.y, n1);
  16639. Cartesian3.multiplyByScalar(n2, coords.z, n2);
  16640. var normal = Cartesian3.add(n0, n1, n0);
  16641. Cartesian3.add(normal, n2, normal);
  16642. Cartesian3.normalize(normal, normal);
  16643. Cartesian3.pack(normal, currentAttributes.normal.values, insertedIndex * 3);
  16644. }
  16645. if (defined(binormals)) {
  16646. var b0 = Cartesian3.fromArray(binormals, i0 * 3, p0Scratch);
  16647. var b1 = Cartesian3.fromArray(binormals, i1 * 3, p1Scratch);
  16648. var b2 = Cartesian3.fromArray(binormals, i2 * 3, p2Scratch);
  16649. Cartesian3.multiplyByScalar(b0, coords.x, b0);
  16650. Cartesian3.multiplyByScalar(b1, coords.y, b1);
  16651. Cartesian3.multiplyByScalar(b2, coords.z, b2);
  16652. var binormal = Cartesian3.add(b0, b1, b0);
  16653. Cartesian3.add(binormal, b2, binormal);
  16654. Cartesian3.normalize(binormal, binormal);
  16655. Cartesian3.pack(binormal, currentAttributes.binormal.values, insertedIndex * 3);
  16656. }
  16657. if (defined(tangents)) {
  16658. var t0 = Cartesian3.fromArray(tangents, i0 * 3, p0Scratch);
  16659. var t1 = Cartesian3.fromArray(tangents, i1 * 3, p1Scratch);
  16660. var t2 = Cartesian3.fromArray(tangents, i2 * 3, p2Scratch);
  16661. Cartesian3.multiplyByScalar(t0, coords.x, t0);
  16662. Cartesian3.multiplyByScalar(t1, coords.y, t1);
  16663. Cartesian3.multiplyByScalar(t2, coords.z, t2);
  16664. var tangent = Cartesian3.add(t0, t1, t0);
  16665. Cartesian3.add(tangent, t2, tangent);
  16666. Cartesian3.normalize(tangent, tangent);
  16667. Cartesian3.pack(tangent, currentAttributes.tangent.values, insertedIndex * 3);
  16668. }
  16669. if (defined(texCoords)) {
  16670. var s0 = Cartesian2.fromArray(texCoords, i0 * 2, s0Scratch);
  16671. var s1 = Cartesian2.fromArray(texCoords, i1 * 2, s1Scratch);
  16672. var s2 = Cartesian2.fromArray(texCoords, i2 * 2, s2Scratch);
  16673. Cartesian2.multiplyByScalar(s0, coords.x, s0);
  16674. Cartesian2.multiplyByScalar(s1, coords.y, s1);
  16675. Cartesian2.multiplyByScalar(s2, coords.z, s2);
  16676. var texCoord = Cartesian2.add(s0, s1, s0);
  16677. Cartesian2.add(texCoord, s2, texCoord);
  16678. Cartesian2.pack(texCoord, currentAttributes.st.values, insertedIndex * 2);
  16679. }
  16680. }
  16681. function insertSplitPoint(currentAttributes, currentIndices, currentIndexMap, indices, currentIndex, point) {
  16682. var insertIndex = currentAttributes.position.values.length / 3;
  16683. if (currentIndex !== -1) {
  16684. var prevIndex = indices[currentIndex];
  16685. var newIndex = currentIndexMap[prevIndex];
  16686. if (newIndex === -1) {
  16687. currentIndexMap[prevIndex] = insertIndex;
  16688. currentAttributes.position.values.push(point.x, point.y, point.z);
  16689. currentIndices.push(insertIndex);
  16690. return insertIndex;
  16691. }
  16692. currentIndices.push(newIndex);
  16693. return newIndex;
  16694. }
  16695. currentAttributes.position.values.push(point.x, point.y, point.z);
  16696. currentIndices.push(insertIndex);
  16697. return insertIndex;
  16698. }
  16699. function splitLongitudeTriangles(instance) {
  16700. var geometry = instance.geometry;
  16701. var attributes = geometry.attributes;
  16702. var positions = attributes.position.values;
  16703. var normals = (defined(attributes.normal)) ? attributes.normal.values : undefined;
  16704. var binormals = (defined(attributes.binormal)) ? attributes.binormal.values : undefined;
  16705. var tangents = (defined(attributes.tangent)) ? attributes.tangent.values : undefined;
  16706. var texCoords = (defined(attributes.st)) ? attributes.st.values : undefined;
  16707. var indices = geometry.indices;
  16708. var eastGeometry = copyGeometryForSplit(geometry);
  16709. var westGeometry = copyGeometryForSplit(geometry);
  16710. var currentAttributes;
  16711. var currentIndices;
  16712. var currentIndexMap;
  16713. var insertedIndex;
  16714. var i;
  16715. var westGeometryIndexMap = [];
  16716. westGeometryIndexMap.length = positions.length / 3;
  16717. var eastGeometryIndexMap = [];
  16718. eastGeometryIndexMap.length = positions.length / 3;
  16719. for (i = 0; i < westGeometryIndexMap.length; ++i) {
  16720. westGeometryIndexMap[i] = -1;
  16721. eastGeometryIndexMap[i] = -1;
  16722. }
  16723. var len = indices.length;
  16724. for (i = 0; i < len; i += 3) {
  16725. var i0 = indices[i];
  16726. var i1 = indices[i + 1];
  16727. var i2 = indices[i + 2];
  16728. var p0 = Cartesian3.fromArray(positions, i0 * 3);
  16729. var p1 = Cartesian3.fromArray(positions, i1 * 3);
  16730. var p2 = Cartesian3.fromArray(positions, i2 * 3);
  16731. var result = splitTriangle(p0, p1, p2);
  16732. if (defined(result) && result.positions.length > 3) {
  16733. var resultPositions = result.positions;
  16734. var resultIndices = result.indices;
  16735. var resultLength = resultIndices.length;
  16736. for (var j = 0; j < resultLength; ++j) {
  16737. var resultIndex = resultIndices[j];
  16738. var point = resultPositions[resultIndex];
  16739. if (point.y < 0.0) {
  16740. currentAttributes = westGeometry.attributes;
  16741. currentIndices = westGeometry.indices;
  16742. currentIndexMap = westGeometryIndexMap;
  16743. } else {
  16744. currentAttributes = eastGeometry.attributes;
  16745. currentIndices = eastGeometry.indices;
  16746. currentIndexMap = eastGeometryIndexMap;
  16747. }
  16748. insertedIndex = insertSplitPoint(currentAttributes, currentIndices, currentIndexMap, indices, resultIndex < 3 ? i + resultIndex : -1, point);
  16749. computeTriangleAttributes(i0, i1, i2, point, positions, normals, binormals, tangents, texCoords, currentAttributes, insertedIndex);
  16750. }
  16751. } else {
  16752. if (defined(result)) {
  16753. p0 = result.positions[0];
  16754. p1 = result.positions[1];
  16755. p2 = result.positions[2];
  16756. }
  16757. if (p0.y < 0.0) {
  16758. currentAttributes = westGeometry.attributes;
  16759. currentIndices = westGeometry.indices;
  16760. currentIndexMap = westGeometryIndexMap;
  16761. } else {
  16762. currentAttributes = eastGeometry.attributes;
  16763. currentIndices = eastGeometry.indices;
  16764. currentIndexMap = eastGeometryIndexMap;
  16765. }
  16766. insertedIndex = insertSplitPoint(currentAttributes, currentIndices, currentIndexMap, indices, i, p0);
  16767. computeTriangleAttributes(i0, i1, i2, p0, positions, normals, binormals, tangents, texCoords, currentAttributes, insertedIndex);
  16768. insertedIndex = insertSplitPoint(currentAttributes, currentIndices, currentIndexMap, indices, i + 1, p1);
  16769. computeTriangleAttributes(i0, i1, i2, p1, positions, normals, binormals, tangents, texCoords, currentAttributes, insertedIndex);
  16770. insertedIndex = insertSplitPoint(currentAttributes, currentIndices, currentIndexMap, indices, i + 2, p2);
  16771. computeTriangleAttributes(i0, i1, i2, p2, positions, normals, binormals, tangents, texCoords, currentAttributes, insertedIndex);
  16772. }
  16773. }
  16774. updateInstanceAfterSplit(instance, westGeometry, eastGeometry);
  16775. }
  16776. var xzPlane = Plane.fromPointNormal(Cartesian3.ZERO, Cartesian3.UNIT_Y);
  16777. var offsetScratch = new Cartesian3();
  16778. var offsetPointScratch = new Cartesian3();
  16779. function splitLongitudeLines(instance) {
  16780. var geometry = instance.geometry;
  16781. var attributes = geometry.attributes;
  16782. var positions = attributes.position.values;
  16783. var indices = geometry.indices;
  16784. var eastGeometry = copyGeometryForSplit(geometry);
  16785. var westGeometry = copyGeometryForSplit(geometry);
  16786. var i;
  16787. var length = indices.length;
  16788. var westGeometryIndexMap = [];
  16789. westGeometryIndexMap.length = positions.length / 3;
  16790. var eastGeometryIndexMap = [];
  16791. eastGeometryIndexMap.length = positions.length / 3;
  16792. for (i = 0; i < westGeometryIndexMap.length; ++i) {
  16793. westGeometryIndexMap[i] = -1;
  16794. eastGeometryIndexMap[i] = -1;
  16795. }
  16796. for (i = 0; i < length; i += 2) {
  16797. var i0 = indices[i];
  16798. var i1 = indices[i + 1];
  16799. var p0 = Cartesian3.fromArray(positions, i0 * 3, p0Scratch);
  16800. var p1 = Cartesian3.fromArray(positions, i1 * 3, p1Scratch);
  16801. if (Math.abs(p0.y) < CesiumMath.EPSILON6){
  16802. if (p0.y < 0.0) {
  16803. p0.y = -CesiumMath.EPSILON6;
  16804. } else {
  16805. p0.y = CesiumMath.EPSILON6;
  16806. }
  16807. }
  16808. if (Math.abs(p1.y) < CesiumMath.EPSILON6){
  16809. if (p1.y < 0.0) {
  16810. p1.y = -CesiumMath.EPSILON6;
  16811. } else {
  16812. p1.y = CesiumMath.EPSILON6;
  16813. }
  16814. }
  16815. var p0Attributes = eastGeometry.attributes;
  16816. var p0Indices = eastGeometry.indices;
  16817. var p0IndexMap = eastGeometryIndexMap;
  16818. var p1Attributes = westGeometry.attributes;
  16819. var p1Indices = westGeometry.indices;
  16820. var p1IndexMap = westGeometryIndexMap;
  16821. var intersection = IntersectionTests.lineSegmentPlane(p0, p1, xzPlane, p2Scratch);
  16822. if (defined(intersection)) {
  16823. // move point on the xz-plane slightly away from the plane
  16824. var offset = Cartesian3.multiplyByScalar(Cartesian3.UNIT_Y, 5.0 * CesiumMath.EPSILON9, offsetScratch);
  16825. if (p0.y < 0.0) {
  16826. Cartesian3.negate(offset, offset);
  16827. p0Attributes = westGeometry.attributes;
  16828. p0Indices = westGeometry.indices;
  16829. p0IndexMap = westGeometryIndexMap;
  16830. p1Attributes = eastGeometry.attributes;
  16831. p1Indices = eastGeometry.indices;
  16832. p1IndexMap = eastGeometryIndexMap;
  16833. }
  16834. var offsetPoint = Cartesian3.add(intersection, offset, offsetPointScratch);
  16835. insertSplitPoint(p0Attributes, p0Indices, p0IndexMap, indices, i, p0);
  16836. insertSplitPoint(p0Attributes, p0Indices, p0IndexMap, indices, -1, offsetPoint);
  16837. Cartesian3.negate(offset, offset);
  16838. Cartesian3.add(intersection, offset, offsetPoint);
  16839. insertSplitPoint(p1Attributes, p1Indices, p1IndexMap, indices, -1, offsetPoint);
  16840. insertSplitPoint(p1Attributes, p1Indices, p1IndexMap, indices, i + 1, p1);
  16841. } else {
  16842. var currentAttributes;
  16843. var currentIndices;
  16844. var currentIndexMap;
  16845. if (p0.y < 0.0) {
  16846. currentAttributes = westGeometry.attributes;
  16847. currentIndices = westGeometry.indices;
  16848. currentIndexMap = westGeometryIndexMap;
  16849. } else {
  16850. currentAttributes = eastGeometry.attributes;
  16851. currentIndices = eastGeometry.indices;
  16852. currentIndexMap = eastGeometryIndexMap;
  16853. }
  16854. insertSplitPoint(currentAttributes, currentIndices, currentIndexMap, indices, i, p0);
  16855. insertSplitPoint(currentAttributes, currentIndices, currentIndexMap, indices, i + 1, p1);
  16856. }
  16857. }
  16858. updateInstanceAfterSplit(instance, westGeometry, eastGeometry);
  16859. }
  16860. var cartesian2Scratch0 = new Cartesian2();
  16861. var cartesian2Scratch1 = new Cartesian2();
  16862. var cartesian3Scratch0 = new Cartesian3();
  16863. var cartesian3Scratch2 = new Cartesian3();
  16864. var cartesian3Scratch3 = new Cartesian3();
  16865. var cartesian3Scratch4 = new Cartesian3();
  16866. var cartesian3Scratch5 = new Cartesian3();
  16867. var cartesian3Scratch6 = new Cartesian3();
  16868. var cartesian4Scratch0 = new Cartesian4();
  16869. function updateAdjacencyAfterSplit(geometry) {
  16870. var attributes = geometry.attributes;
  16871. var positions = attributes.position.values;
  16872. var prevPositions = attributes.prevPosition.values;
  16873. var nextPositions = attributes.nextPosition.values;
  16874. var length = positions.length;
  16875. for (var j = 0; j < length; j += 3) {
  16876. var position = Cartesian3.unpack(positions, j, cartesian3Scratch0);
  16877. if (position.x > 0.0) {
  16878. continue;
  16879. }
  16880. var prevPosition = Cartesian3.unpack(prevPositions, j, cartesian3Scratch2);
  16881. if ((position.y < 0.0 && prevPosition.y > 0.0) || (position.y > 0.0 && prevPosition.y < 0.0)) {
  16882. if (j - 3 > 0) {
  16883. prevPositions[j] = positions[j - 3];
  16884. prevPositions[j + 1] = positions[j - 2];
  16885. prevPositions[j + 2] = positions[j - 1];
  16886. } else {
  16887. Cartesian3.pack(position, prevPositions, j);
  16888. }
  16889. }
  16890. var nextPosition = Cartesian3.unpack(nextPositions, j, cartesian3Scratch3);
  16891. if ((position.y < 0.0 && nextPosition.y > 0.0) || (position.y > 0.0 && nextPosition.y < 0.0)) {
  16892. if (j + 3 < length) {
  16893. nextPositions[j] = positions[j + 3];
  16894. nextPositions[j + 1] = positions[j + 4];
  16895. nextPositions[j + 2] = positions[j + 5];
  16896. } else {
  16897. Cartesian3.pack(position, nextPositions, j);
  16898. }
  16899. }
  16900. }
  16901. }
  16902. var offsetScalar = 5.0 * CesiumMath.EPSILON9;
  16903. var coplanarOffset = CesiumMath.EPSILON6;
  16904. function splitLongitudePolyline(instance) {
  16905. var geometry = instance.geometry;
  16906. var attributes = geometry.attributes;
  16907. var positions = attributes.position.values;
  16908. var prevPositions = attributes.prevPosition.values;
  16909. var nextPositions = attributes.nextPosition.values;
  16910. var expandAndWidths = attributes.expandAndWidth.values;
  16911. var texCoords = (defined(attributes.st)) ? attributes.st.values : undefined;
  16912. var colors = (defined(attributes.color)) ? attributes.color.values : undefined;
  16913. var eastGeometry = copyGeometryForSplit(geometry);
  16914. var westGeometry = copyGeometryForSplit(geometry);
  16915. var i;
  16916. var j;
  16917. var index;
  16918. var intersectionFound = false;
  16919. var length = positions.length / 3;
  16920. for (i = 0; i < length; i += 4) {
  16921. var i0 = i;
  16922. var i2 = i + 2;
  16923. var p0 = Cartesian3.fromArray(positions, i0 * 3, cartesian3Scratch0);
  16924. var p2 = Cartesian3.fromArray(positions, i2 * 3, cartesian3Scratch2);
  16925. // Offset points that are close to the 180 longitude and change the previous/next point
  16926. // to be the same offset point so it can be projected to 2D. There is special handling in the
  16927. // shader for when position == prevPosition || position == nextPosition.
  16928. if (Math.abs(p0.y) < coplanarOffset) {
  16929. p0.y = coplanarOffset * (p2.y < 0.0 ? -1.0 : 1.0);
  16930. positions[i * 3 + 1] = p0.y;
  16931. positions[(i + 1) * 3 + 1] = p0.y;
  16932. for (j = i0 * 3; j < i0 * 3 + 4 * 3; j += 3) {
  16933. prevPositions[j] = positions[i * 3];
  16934. prevPositions[j + 1] = positions[i * 3 + 1];
  16935. prevPositions[j + 2] = positions[i * 3 + 2];
  16936. }
  16937. }
  16938. // Do the same but for when the line crosses 180 longitude in the opposite direction.
  16939. if (Math.abs(p2.y) < coplanarOffset) {
  16940. p2.y = coplanarOffset * (p0.y < 0.0 ? -1.0 : 1.0);
  16941. positions[(i + 2) * 3 + 1] = p2.y;
  16942. positions[(i + 3) * 3 + 1] = p2.y;
  16943. for (j = i0 * 3; j < i0 * 3 + 4 * 3; j += 3) {
  16944. nextPositions[j] = positions[(i + 2) * 3];
  16945. nextPositions[j + 1] = positions[(i + 2) * 3 + 1];
  16946. nextPositions[j + 2] = positions[(i + 2) * 3 + 2];
  16947. }
  16948. }
  16949. var p0Attributes = eastGeometry.attributes;
  16950. var p0Indices = eastGeometry.indices;
  16951. var p2Attributes = westGeometry.attributes;
  16952. var p2Indices = westGeometry.indices;
  16953. var intersection = IntersectionTests.lineSegmentPlane(p0, p2, xzPlane, cartesian3Scratch4);
  16954. if (defined(intersection)) {
  16955. intersectionFound = true;
  16956. // move point on the xz-plane slightly away from the plane
  16957. var offset = Cartesian3.multiplyByScalar(Cartesian3.UNIT_Y, offsetScalar, cartesian3Scratch5);
  16958. if (p0.y < 0.0) {
  16959. Cartesian3.negate(offset, offset);
  16960. p0Attributes = westGeometry.attributes;
  16961. p0Indices = westGeometry.indices;
  16962. p2Attributes = eastGeometry.attributes;
  16963. p2Indices = eastGeometry.indices;
  16964. }
  16965. var offsetPoint = Cartesian3.add(intersection, offset, cartesian3Scratch6);
  16966. p0Attributes.position.values.push(p0.x, p0.y, p0.z, p0.x, p0.y, p0.z);
  16967. p0Attributes.position.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16968. p0Attributes.position.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16969. p0Attributes.prevPosition.values.push(prevPositions[i0 * 3], prevPositions[i0 * 3 + 1], prevPositions[i0 * 3 + 2]);
  16970. p0Attributes.prevPosition.values.push(prevPositions[i0 * 3 + 3], prevPositions[i0 * 3 + 4], prevPositions[i0 * 3 + 5]);
  16971. p0Attributes.prevPosition.values.push(p0.x, p0.y, p0.z, p0.x, p0.y, p0.z);
  16972. p0Attributes.nextPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16973. p0Attributes.nextPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16974. p0Attributes.nextPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16975. p0Attributes.nextPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16976. Cartesian3.negate(offset, offset);
  16977. Cartesian3.add(intersection, offset, offsetPoint);
  16978. p2Attributes.position.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16979. p2Attributes.position.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16980. p2Attributes.position.values.push(p2.x, p2.y, p2.z, p2.x, p2.y, p2.z);
  16981. p2Attributes.prevPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16982. p2Attributes.prevPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16983. p2Attributes.prevPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16984. p2Attributes.prevPosition.values.push(offsetPoint.x, offsetPoint.y, offsetPoint.z);
  16985. p2Attributes.nextPosition.values.push(p2.x, p2.y, p2.z, p2.x, p2.y, p2.z);
  16986. p2Attributes.nextPosition.values.push(nextPositions[i2 * 3], nextPositions[i2 * 3 + 1], nextPositions[i2 * 3 + 2]);
  16987. p2Attributes.nextPosition.values.push(nextPositions[i2 * 3 + 3], nextPositions[i2 * 3 + 4], nextPositions[i2 * 3 + 5]);
  16988. var ew0 = Cartesian2.fromArray(expandAndWidths, i0 * 2, cartesian2Scratch0);
  16989. var width = Math.abs(ew0.y);
  16990. p0Attributes.expandAndWidth.values.push(-1, width, 1, width);
  16991. p0Attributes.expandAndWidth.values.push(-1, -width, 1, -width);
  16992. p2Attributes.expandAndWidth.values.push(-1, width, 1, width);
  16993. p2Attributes.expandAndWidth.values.push(-1, -width, 1, -width);
  16994. var t = Cartesian3.magnitudeSquared(Cartesian3.subtract(intersection, p0, cartesian3Scratch3));
  16995. t /= Cartesian3.magnitudeSquared(Cartesian3.subtract(p2, p0, cartesian3Scratch3));
  16996. if (defined(colors)) {
  16997. var c0 = Cartesian4.fromArray(colors, i0 * 4, cartesian4Scratch0);
  16998. var c2 = Cartesian4.fromArray(colors, i2 * 4, cartesian4Scratch0);
  16999. var r = CesiumMath.lerp(c0.x, c2.x, t);
  17000. var g = CesiumMath.lerp(c0.y, c2.y, t);
  17001. var b = CesiumMath.lerp(c0.z, c2.z, t);
  17002. var a = CesiumMath.lerp(c0.w, c2.w, t);
  17003. for (j = i0 * 4; j < i0 * 4 + 2 * 4; ++j) {
  17004. p0Attributes.color.values.push(colors[j]);
  17005. }
  17006. p0Attributes.color.values.push(r, g, b, a);
  17007. p0Attributes.color.values.push(r, g, b, a);
  17008. p2Attributes.color.values.push(r, g, b, a);
  17009. p2Attributes.color.values.push(r, g, b, a);
  17010. for (j = i2 * 4; j < i2 * 4 + 2 * 4; ++j) {
  17011. p2Attributes.color.values.push(colors[j]);
  17012. }
  17013. }
  17014. if (defined(texCoords)) {
  17015. var s0 = Cartesian2.fromArray(texCoords, i0 * 2, cartesian2Scratch0);
  17016. var s3 = Cartesian2.fromArray(texCoords, (i + 3) * 2, cartesian2Scratch1);
  17017. var sx = CesiumMath.lerp(s0.x, s3.x, t);
  17018. for (j = i0 * 2; j < i0 * 2 + 2 * 2; ++j) {
  17019. p0Attributes.st.values.push(texCoords[j]);
  17020. }
  17021. p0Attributes.st.values.push(sx, s0.y);
  17022. p0Attributes.st.values.push(sx, s3.y);
  17023. p2Attributes.st.values.push(sx, s0.y);
  17024. p2Attributes.st.values.push(sx, s3.y);
  17025. for (j = i2 * 2; j < i2 * 2 + 2 * 2; ++j) {
  17026. p2Attributes.st.values.push(texCoords[j]);
  17027. }
  17028. }
  17029. index = p0Attributes.position.values.length / 3 - 4;
  17030. p0Indices.push(index, index + 2, index + 1);
  17031. p0Indices.push(index + 1, index + 2, index + 3);
  17032. index = p2Attributes.position.values.length / 3 - 4;
  17033. p2Indices.push(index, index + 2, index + 1);
  17034. p2Indices.push(index + 1, index + 2, index + 3);
  17035. } else {
  17036. var currentAttributes;
  17037. var currentIndices;
  17038. if (p0.y < 0.0) {
  17039. currentAttributes = westGeometry.attributes;
  17040. currentIndices = westGeometry.indices;
  17041. } else {
  17042. currentAttributes = eastGeometry.attributes;
  17043. currentIndices = eastGeometry.indices;
  17044. }
  17045. currentAttributes.position.values.push(p0.x, p0.y, p0.z);
  17046. currentAttributes.position.values.push(p0.x, p0.y, p0.z);
  17047. currentAttributes.position.values.push(p2.x, p2.y, p2.z);
  17048. currentAttributes.position.values.push(p2.x, p2.y, p2.z);
  17049. for (j = i * 3; j < i * 3 + 4 * 3; ++j) {
  17050. currentAttributes.prevPosition.values.push(prevPositions[j]);
  17051. currentAttributes.nextPosition.values.push(nextPositions[j]);
  17052. }
  17053. for (j = i * 2; j < i * 2 + 4 * 2; ++j) {
  17054. currentAttributes.expandAndWidth.values.push(expandAndWidths[j]);
  17055. if (defined(texCoords)) {
  17056. currentAttributes.st.values.push(texCoords[j]);
  17057. }
  17058. }
  17059. if (defined(colors)) {
  17060. for (j = i * 4; j < i * 4 + 4 * 4; ++j) {
  17061. currentAttributes.color.values.push(colors[j]);
  17062. }
  17063. }
  17064. index = currentAttributes.position.values.length / 3 - 4;
  17065. currentIndices.push(index, index + 2, index + 1);
  17066. currentIndices.push(index + 1, index + 2, index + 3);
  17067. }
  17068. }
  17069. if (intersectionFound) {
  17070. updateAdjacencyAfterSplit(westGeometry);
  17071. updateAdjacencyAfterSplit(eastGeometry);
  17072. }
  17073. updateInstanceAfterSplit(instance, westGeometry, eastGeometry);
  17074. }
  17075. /**
  17076. * Splits the instances's geometry, by introducing new vertices and indices,that
  17077. * intersect the International Date Line and Prime Meridian so that no primitives cross longitude
  17078. * -180/180 degrees. This is not required for 3D drawing, but is required for
  17079. * correcting drawing in 2D and Columbus view.
  17080. *
  17081. * @private
  17082. *
  17083. * @param {GeometryInstance} instance The instance to modify.
  17084. * @returns {GeometryInstance} The modified <code>instance</code> argument, with it's geometry split at the International Date Line.
  17085. *
  17086. * @example
  17087. * instance = Cesium.GeometryPipeline.splitLongitude(instance);
  17088. */
  17089. GeometryPipeline.splitLongitude = function(instance) {
  17090. if (!defined(instance)) {
  17091. throw new DeveloperError('instance is required.');
  17092. }
  17093. var geometry = instance.geometry;
  17094. var boundingSphere = geometry.boundingSphere;
  17095. if (defined(boundingSphere)) {
  17096. var minX = boundingSphere.center.x - boundingSphere.radius;
  17097. if (minX > 0 || BoundingSphere.intersectPlane(boundingSphere, Plane.ORIGIN_ZX_PLANE) !== Intersect.INTERSECTING) {
  17098. return instance;
  17099. }
  17100. }
  17101. if (geometry.geometryType !== GeometryType.NONE) {
  17102. switch (geometry.geometryType) {
  17103. case GeometryType.POLYLINES:
  17104. splitLongitudePolyline(instance);
  17105. break;
  17106. case GeometryType.TRIANGLES:
  17107. splitLongitudeTriangles(instance);
  17108. break;
  17109. case GeometryType.LINES:
  17110. splitLongitudeLines(instance);
  17111. break;
  17112. }
  17113. } else {
  17114. indexPrimitive(geometry);
  17115. if (geometry.primitiveType === PrimitiveType.TRIANGLES) {
  17116. splitLongitudeTriangles(instance);
  17117. } else if (geometry.primitiveType === PrimitiveType.LINES) {
  17118. splitLongitudeLines(instance);
  17119. }
  17120. }
  17121. return instance;
  17122. };
  17123. return GeometryPipeline;
  17124. });
  17125. /*global define*/
  17126. define('Core/Matrix2',[
  17127. './Cartesian2',
  17128. './defaultValue',
  17129. './defined',
  17130. './defineProperties',
  17131. './DeveloperError',
  17132. './freezeObject'
  17133. ], function(
  17134. Cartesian2,
  17135. defaultValue,
  17136. defined,
  17137. defineProperties,
  17138. DeveloperError,
  17139. freezeObject) {
  17140. 'use strict';
  17141. /**
  17142. * A 2x2 matrix, indexable as a column-major order array.
  17143. * Constructor parameters are in row-major order for code readability.
  17144. * @alias Matrix2
  17145. * @constructor
  17146. *
  17147. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  17148. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  17149. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  17150. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  17151. *
  17152. * @see Matrix2.fromColumnMajorArray
  17153. * @see Matrix2.fromRowMajorArray
  17154. * @see Matrix2.fromScale
  17155. * @see Matrix2.fromUniformScale
  17156. * @see Matrix3
  17157. * @see Matrix4
  17158. */
  17159. function Matrix2(column0Row0, column1Row0, column0Row1, column1Row1) {
  17160. this[0] = defaultValue(column0Row0, 0.0);
  17161. this[1] = defaultValue(column0Row1, 0.0);
  17162. this[2] = defaultValue(column1Row0, 0.0);
  17163. this[3] = defaultValue(column1Row1, 0.0);
  17164. }
  17165. /**
  17166. * The number of elements used to pack the object into an array.
  17167. * @type {Number}
  17168. */
  17169. Matrix2.packedLength = 4;
  17170. /**
  17171. * Stores the provided instance into the provided array.
  17172. *
  17173. * @param {Matrix2} value The value to pack.
  17174. * @param {Number[]} array The array to pack into.
  17175. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  17176. *
  17177. * @returns {Number[]} The array that was packed into
  17178. */
  17179. Matrix2.pack = function(value, array, startingIndex) {
  17180. if (!defined(value)) {
  17181. throw new DeveloperError('value is required');
  17182. }
  17183. if (!defined(array)) {
  17184. throw new DeveloperError('array is required');
  17185. }
  17186. startingIndex = defaultValue(startingIndex, 0);
  17187. array[startingIndex++] = value[0];
  17188. array[startingIndex++] = value[1];
  17189. array[startingIndex++] = value[2];
  17190. array[startingIndex++] = value[3];
  17191. return array;
  17192. };
  17193. /**
  17194. * Retrieves an instance from a packed array.
  17195. *
  17196. * @param {Number[]} array The packed array.
  17197. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  17198. * @param {Matrix2} [result] The object into which to store the result.
  17199. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  17200. */
  17201. Matrix2.unpack = function(array, startingIndex, result) {
  17202. if (!defined(array)) {
  17203. throw new DeveloperError('array is required');
  17204. }
  17205. startingIndex = defaultValue(startingIndex, 0);
  17206. if (!defined(result)) {
  17207. result = new Matrix2();
  17208. }
  17209. result[0] = array[startingIndex++];
  17210. result[1] = array[startingIndex++];
  17211. result[2] = array[startingIndex++];
  17212. result[3] = array[startingIndex++];
  17213. return result;
  17214. };
  17215. /**
  17216. * Duplicates a Matrix2 instance.
  17217. *
  17218. * @param {Matrix2} matrix The matrix to duplicate.
  17219. * @param {Matrix2} [result] The object onto which to store the result.
  17220. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided. (Returns undefined if matrix is undefined)
  17221. */
  17222. Matrix2.clone = function(values, result) {
  17223. if (!defined(values)) {
  17224. return undefined;
  17225. }
  17226. if (!defined(result)) {
  17227. return new Matrix2(values[0], values[2],
  17228. values[1], values[3]);
  17229. }
  17230. result[0] = values[0];
  17231. result[1] = values[1];
  17232. result[2] = values[2];
  17233. result[3] = values[3];
  17234. return result;
  17235. };
  17236. /**
  17237. * Creates a Matrix2 from 4 consecutive elements in an array.
  17238. *
  17239. * @param {Number[]} array The array whose 4 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  17240. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  17241. * @param {Matrix2} [result] The object onto which to store the result.
  17242. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  17243. *
  17244. * @example
  17245. * // Create the Matrix2:
  17246. * // [1.0, 2.0]
  17247. * // [1.0, 2.0]
  17248. *
  17249. * var v = [1.0, 1.0, 2.0, 2.0];
  17250. * var m = Cesium.Matrix2.fromArray(v);
  17251. *
  17252. * // Create same Matrix2 with using an offset into an array
  17253. * var v2 = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0];
  17254. * var m2 = Cesium.Matrix2.fromArray(v2, 2);
  17255. */
  17256. Matrix2.fromArray = function(array, startingIndex, result) {
  17257. if (!defined(array)) {
  17258. throw new DeveloperError('array is required');
  17259. }
  17260. startingIndex = defaultValue(startingIndex, 0);
  17261. if (!defined(result)) {
  17262. result = new Matrix2();
  17263. }
  17264. result[0] = array[startingIndex];
  17265. result[1] = array[startingIndex + 1];
  17266. result[2] = array[startingIndex + 2];
  17267. result[3] = array[startingIndex + 3];
  17268. return result;
  17269. };
  17270. /**
  17271. * Creates a Matrix2 instance from a column-major order array.
  17272. *
  17273. * @param {Number[]} values The column-major order array.
  17274. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  17275. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  17276. */
  17277. Matrix2.fromColumnMajorArray = function(values, result) {
  17278. if (!defined(values)) {
  17279. throw new DeveloperError('values parameter is required');
  17280. }
  17281. return Matrix2.clone(values, result);
  17282. };
  17283. /**
  17284. * Creates a Matrix2 instance from a row-major order array.
  17285. * The resulting matrix will be in column-major order.
  17286. *
  17287. * @param {Number[]} values The row-major order array.
  17288. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  17289. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  17290. */
  17291. Matrix2.fromRowMajorArray = function(values, result) {
  17292. if (!defined(values)) {
  17293. throw new DeveloperError('values is required.');
  17294. }
  17295. if (!defined(result)) {
  17296. return new Matrix2(values[0], values[1],
  17297. values[2], values[3]);
  17298. }
  17299. result[0] = values[0];
  17300. result[1] = values[2];
  17301. result[2] = values[1];
  17302. result[3] = values[3];
  17303. return result;
  17304. };
  17305. /**
  17306. * Computes a Matrix2 instance representing a non-uniform scale.
  17307. *
  17308. * @param {Cartesian2} scale The x and y scale factors.
  17309. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  17310. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  17311. *
  17312. * @example
  17313. * // Creates
  17314. * // [7.0, 0.0]
  17315. * // [0.0, 8.0]
  17316. * var m = Cesium.Matrix2.fromScale(new Cesium.Cartesian2(7.0, 8.0));
  17317. */
  17318. Matrix2.fromScale = function(scale, result) {
  17319. if (!defined(scale)) {
  17320. throw new DeveloperError('scale is required.');
  17321. }
  17322. if (!defined(result)) {
  17323. return new Matrix2(
  17324. scale.x, 0.0,
  17325. 0.0, scale.y);
  17326. }
  17327. result[0] = scale.x;
  17328. result[1] = 0.0;
  17329. result[2] = 0.0;
  17330. result[3] = scale.y;
  17331. return result;
  17332. };
  17333. /**
  17334. * Computes a Matrix2 instance representing a uniform scale.
  17335. *
  17336. * @param {Number} scale The uniform scale factor.
  17337. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  17338. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  17339. *
  17340. * @example
  17341. * // Creates
  17342. * // [2.0, 0.0]
  17343. * // [0.0, 2.0]
  17344. * var m = Cesium.Matrix2.fromUniformScale(2.0);
  17345. */
  17346. Matrix2.fromUniformScale = function(scale, result) {
  17347. if (typeof scale !== 'number') {
  17348. throw new DeveloperError('scale is required.');
  17349. }
  17350. if (!defined(result)) {
  17351. return new Matrix2(
  17352. scale, 0.0,
  17353. 0.0, scale);
  17354. }
  17355. result[0] = scale;
  17356. result[1] = 0.0;
  17357. result[2] = 0.0;
  17358. result[3] = scale;
  17359. return result;
  17360. };
  17361. /**
  17362. * Creates a rotation matrix.
  17363. *
  17364. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  17365. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  17366. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  17367. *
  17368. * @example
  17369. * // Rotate a point 45 degrees counterclockwise.
  17370. * var p = new Cesium.Cartesian2(5, 6);
  17371. * var m = Cesium.Matrix2.fromRotation(Cesium.Math.toRadians(45.0));
  17372. * var rotated = Cesium.Matrix2.multiplyByVector(m, p, new Cesium.Cartesian2());
  17373. */
  17374. Matrix2.fromRotation = function(angle, result) {
  17375. if (!defined(angle)) {
  17376. throw new DeveloperError('angle is required.');
  17377. }
  17378. var cosAngle = Math.cos(angle);
  17379. var sinAngle = Math.sin(angle);
  17380. if (!defined(result)) {
  17381. return new Matrix2(
  17382. cosAngle, -sinAngle,
  17383. sinAngle, cosAngle);
  17384. }
  17385. result[0] = cosAngle;
  17386. result[1] = sinAngle;
  17387. result[2] = -sinAngle;
  17388. result[3] = cosAngle;
  17389. return result;
  17390. };
  17391. /**
  17392. * Creates an Array from the provided Matrix2 instance.
  17393. * The array will be in column-major order.
  17394. *
  17395. * @param {Matrix2} matrix The matrix to use..
  17396. * @param {Number[]} [result] The Array onto which to store the result.
  17397. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  17398. */
  17399. Matrix2.toArray = function(matrix, result) {
  17400. if (!defined(matrix)) {
  17401. throw new DeveloperError('matrix is required');
  17402. }
  17403. if (!defined(result)) {
  17404. return [matrix[0], matrix[1], matrix[2], matrix[3]];
  17405. }
  17406. result[0] = matrix[0];
  17407. result[1] = matrix[1];
  17408. result[2] = matrix[2];
  17409. result[3] = matrix[3];
  17410. return result;
  17411. };
  17412. /**
  17413. * Computes the array index of the element at the provided row and column.
  17414. *
  17415. * @param {Number} row The zero-based index of the row.
  17416. * @param {Number} column The zero-based index of the column.
  17417. * @returns {Number} The index of the element at the provided row and column.
  17418. *
  17419. * @exception {DeveloperError} row must be 0 or 1.
  17420. * @exception {DeveloperError} column must be 0 or 1.
  17421. *
  17422. * @example
  17423. * var myMatrix = new Cesium.Matrix2();
  17424. * var column1Row0Index = Cesium.Matrix2.getElementIndex(1, 0);
  17425. * var column1Row0 = myMatrix[column1Row0Index]
  17426. * myMatrix[column1Row0Index] = 10.0;
  17427. */
  17428. Matrix2.getElementIndex = function(column, row) {
  17429. if (typeof row !== 'number' || row < 0 || row > 1) {
  17430. throw new DeveloperError('row must be 0 or 1.');
  17431. }
  17432. if (typeof column !== 'number' || column < 0 || column > 1) {
  17433. throw new DeveloperError('column must be 0 or 1.');
  17434. }
  17435. return column * 2 + row;
  17436. };
  17437. /**
  17438. * Retrieves a copy of the matrix column at the provided index as a Cartesian2 instance.
  17439. *
  17440. * @param {Matrix2} matrix The matrix to use.
  17441. * @param {Number} index The zero-based index of the column to retrieve.
  17442. * @param {Cartesian2} result The object onto which to store the result.
  17443. * @returns {Cartesian2} The modified result parameter.
  17444. *
  17445. * @exception {DeveloperError} index must be 0 or 1.
  17446. */
  17447. Matrix2.getColumn = function(matrix, index, result) {
  17448. if (!defined(matrix)) {
  17449. throw new DeveloperError('matrix is required.');
  17450. }
  17451. if (typeof index !== 'number' || index < 0 || index > 1) {
  17452. throw new DeveloperError('index must be 0 or 1.');
  17453. }
  17454. if (!defined(result)) {
  17455. throw new DeveloperError('result is required');
  17456. }
  17457. var startIndex = index * 2;
  17458. var x = matrix[startIndex];
  17459. var y = matrix[startIndex + 1];
  17460. result.x = x;
  17461. result.y = y;
  17462. return result;
  17463. };
  17464. /**
  17465. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian2 instance.
  17466. *
  17467. * @param {Matrix2} matrix The matrix to use.
  17468. * @param {Number} index The zero-based index of the column to set.
  17469. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified column.
  17470. * @param {Cartesian2} result The object onto which to store the result.
  17471. * @returns {Matrix2} The modified result parameter.
  17472. *
  17473. * @exception {DeveloperError} index must be 0 or 1.
  17474. */
  17475. Matrix2.setColumn = function(matrix, index, cartesian, result) {
  17476. if (!defined(matrix)) {
  17477. throw new DeveloperError('matrix is required');
  17478. }
  17479. if (!defined(cartesian)) {
  17480. throw new DeveloperError('cartesian is required');
  17481. }
  17482. if (typeof index !== 'number' || index < 0 || index > 1) {
  17483. throw new DeveloperError('index must be 0 or 1.');
  17484. }
  17485. if (!defined(result)) {
  17486. throw new DeveloperError('result is required');
  17487. }
  17488. result = Matrix2.clone(matrix, result);
  17489. var startIndex = index * 2;
  17490. result[startIndex] = cartesian.x;
  17491. result[startIndex + 1] = cartesian.y;
  17492. return result;
  17493. };
  17494. /**
  17495. * Retrieves a copy of the matrix row at the provided index as a Cartesian2 instance.
  17496. *
  17497. * @param {Matrix2} matrix The matrix to use.
  17498. * @param {Number} index The zero-based index of the row to retrieve.
  17499. * @param {Cartesian2} result The object onto which to store the result.
  17500. * @returns {Cartesian2} The modified result parameter.
  17501. *
  17502. * @exception {DeveloperError} index must be 0 or 1.
  17503. */
  17504. Matrix2.getRow = function(matrix, index, result) {
  17505. if (!defined(matrix)) {
  17506. throw new DeveloperError('matrix is required.');
  17507. }
  17508. if (typeof index !== 'number' || index < 0 || index > 1) {
  17509. throw new DeveloperError('index must be 0 or 1.');
  17510. }
  17511. if (!defined(result)) {
  17512. throw new DeveloperError('result is required');
  17513. }
  17514. var x = matrix[index];
  17515. var y = matrix[index + 2];
  17516. result.x = x;
  17517. result.y = y;
  17518. return result;
  17519. };
  17520. /**
  17521. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian2 instance.
  17522. *
  17523. * @param {Matrix2} matrix The matrix to use.
  17524. * @param {Number} index The zero-based index of the row to set.
  17525. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified row.
  17526. * @param {Matrix2} result The object onto which to store the result.
  17527. * @returns {Matrix2} The modified result parameter.
  17528. *
  17529. * @exception {DeveloperError} index must be 0 or 1.
  17530. */
  17531. Matrix2.setRow = function(matrix, index, cartesian, result) {
  17532. if (!defined(matrix)) {
  17533. throw new DeveloperError('matrix is required');
  17534. }
  17535. if (!defined(cartesian)) {
  17536. throw new DeveloperError('cartesian is required');
  17537. }
  17538. if (typeof index !== 'number' || index < 0 || index > 1) {
  17539. throw new DeveloperError('index must be 0 or 1.');
  17540. }
  17541. if (!defined(result)) {
  17542. throw new DeveloperError('result is required');
  17543. }
  17544. result = Matrix2.clone(matrix, result);
  17545. result[index] = cartesian.x;
  17546. result[index + 2] = cartesian.y;
  17547. return result;
  17548. };
  17549. var scratchColumn = new Cartesian2();
  17550. /**
  17551. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  17552. *
  17553. * @param {Matrix2} matrix The matrix.
  17554. * @param {Cartesian2} result The object onto which to store the result.
  17555. * @returns {Cartesian2} The modified result parameter.
  17556. */
  17557. Matrix2.getScale = function(matrix, result) {
  17558. if (!defined(matrix)) {
  17559. throw new DeveloperError('matrix is required.');
  17560. }
  17561. if (!defined(result)) {
  17562. throw new DeveloperError('result is required');
  17563. }
  17564. result.x = Cartesian2.magnitude(Cartesian2.fromElements(matrix[0], matrix[1], scratchColumn));
  17565. result.y = Cartesian2.magnitude(Cartesian2.fromElements(matrix[2], matrix[3], scratchColumn));
  17566. return result;
  17567. };
  17568. var scratchScale = new Cartesian2();
  17569. /**
  17570. * Computes the maximum scale assuming the matrix is an affine transformation.
  17571. * The maximum scale is the maximum length of the column vectors.
  17572. *
  17573. * @param {Matrix2} matrix The matrix.
  17574. * @returns {Number} The maximum scale.
  17575. */
  17576. Matrix2.getMaximumScale = function(matrix) {
  17577. Matrix2.getScale(matrix, scratchScale);
  17578. return Cartesian2.maximumComponent(scratchScale);
  17579. };
  17580. /**
  17581. * Computes the product of two matrices.
  17582. *
  17583. * @param {Matrix2} left The first matrix.
  17584. * @param {Matrix2} right The second matrix.
  17585. * @param {Matrix2} result The object onto which to store the result.
  17586. * @returns {Matrix2} The modified result parameter.
  17587. */
  17588. Matrix2.multiply = function(left, right, result) {
  17589. if (!defined(left)) {
  17590. throw new DeveloperError('left is required');
  17591. }
  17592. if (!defined(right)) {
  17593. throw new DeveloperError('right is required');
  17594. }
  17595. if (!defined(result)) {
  17596. throw new DeveloperError('result is required');
  17597. }
  17598. var column0Row0 = left[0] * right[0] + left[2] * right[1];
  17599. var column1Row0 = left[0] * right[2] + left[2] * right[3];
  17600. var column0Row1 = left[1] * right[0] + left[3] * right[1];
  17601. var column1Row1 = left[1] * right[2] + left[3] * right[3];
  17602. result[0] = column0Row0;
  17603. result[1] = column0Row1;
  17604. result[2] = column1Row0;
  17605. result[3] = column1Row1;
  17606. return result;
  17607. };
  17608. /**
  17609. * Computes the sum of two matrices.
  17610. *
  17611. * @param {Matrix2} left The first matrix.
  17612. * @param {Matrix2} right The second matrix.
  17613. * @param {Matrix2} result The object onto which to store the result.
  17614. * @returns {Matrix2} The modified result parameter.
  17615. */
  17616. Matrix2.add = function(left, right, result) {
  17617. if (!defined(left)) {
  17618. throw new DeveloperError('left is required');
  17619. }
  17620. if (!defined(right)) {
  17621. throw new DeveloperError('right is required');
  17622. }
  17623. if (!defined(result)) {
  17624. throw new DeveloperError('result is required');
  17625. }
  17626. result[0] = left[0] + right[0];
  17627. result[1] = left[1] + right[1];
  17628. result[2] = left[2] + right[2];
  17629. result[3] = left[3] + right[3];
  17630. return result;
  17631. };
  17632. /**
  17633. * Computes the difference of two matrices.
  17634. *
  17635. * @param {Matrix2} left The first matrix.
  17636. * @param {Matrix2} right The second matrix.
  17637. * @param {Matrix2} result The object onto which to store the result.
  17638. * @returns {Matrix2} The modified result parameter.
  17639. */
  17640. Matrix2.subtract = function(left, right, result) {
  17641. if (!defined(left)) {
  17642. throw new DeveloperError('left is required');
  17643. }
  17644. if (!defined(right)) {
  17645. throw new DeveloperError('right is required');
  17646. }
  17647. if (!defined(result)) {
  17648. throw new DeveloperError('result is required');
  17649. }
  17650. result[0] = left[0] - right[0];
  17651. result[1] = left[1] - right[1];
  17652. result[2] = left[2] - right[2];
  17653. result[3] = left[3] - right[3];
  17654. return result;
  17655. };
  17656. /**
  17657. * Computes the product of a matrix and a column vector.
  17658. *
  17659. * @param {Matrix2} matrix The matrix.
  17660. * @param {Cartesian2} cartesian The column.
  17661. * @param {Cartesian2} result The object onto which to store the result.
  17662. * @returns {Cartesian2} The modified result parameter.
  17663. */
  17664. Matrix2.multiplyByVector = function(matrix, cartesian, result) {
  17665. if (!defined(matrix)) {
  17666. throw new DeveloperError('matrix is required');
  17667. }
  17668. if (!defined(cartesian)) {
  17669. throw new DeveloperError('cartesian is required');
  17670. }
  17671. if (!defined(result)) {
  17672. throw new DeveloperError('result is required');
  17673. }
  17674. var x = matrix[0] * cartesian.x + matrix[2] * cartesian.y;
  17675. var y = matrix[1] * cartesian.x + matrix[3] * cartesian.y;
  17676. result.x = x;
  17677. result.y = y;
  17678. return result;
  17679. };
  17680. /**
  17681. * Computes the product of a matrix and a scalar.
  17682. *
  17683. * @param {Matrix2} matrix The matrix.
  17684. * @param {Number} scalar The number to multiply by.
  17685. * @param {Matrix2} result The object onto which to store the result.
  17686. * @returns {Matrix2} The modified result parameter.
  17687. */
  17688. Matrix2.multiplyByScalar = function(matrix, scalar, result) {
  17689. if (!defined(matrix)) {
  17690. throw new DeveloperError('matrix is required');
  17691. }
  17692. if (typeof scalar !== 'number') {
  17693. throw new DeveloperError('scalar is required and must be a number');
  17694. }
  17695. if (!defined(result)) {
  17696. throw new DeveloperError('result is required');
  17697. }
  17698. result[0] = matrix[0] * scalar;
  17699. result[1] = matrix[1] * scalar;
  17700. result[2] = matrix[2] * scalar;
  17701. result[3] = matrix[3] * scalar;
  17702. return result;
  17703. };
  17704. /**
  17705. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  17706. *
  17707. * @param {Matrix2} matrix The matrix on the left-hand side.
  17708. * @param {Cartesian2} scale The non-uniform scale on the right-hand side.
  17709. * @param {Matrix2} result The object onto which to store the result.
  17710. * @returns {Matrix2} The modified result parameter.
  17711. *
  17712. *
  17713. * @example
  17714. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
  17715. * Cesium.Matrix2.multiplyByScale(m, scale, m);
  17716. *
  17717. * @see Matrix2.fromScale
  17718. * @see Matrix2.multiplyByUniformScale
  17719. */
  17720. Matrix2.multiplyByScale = function(matrix, scale, result) {
  17721. if (!defined(matrix)) {
  17722. throw new DeveloperError('matrix is required');
  17723. }
  17724. if (!defined(scale)) {
  17725. throw new DeveloperError('scale is required');
  17726. }
  17727. if (!defined(result)) {
  17728. throw new DeveloperError('result is required');
  17729. }
  17730. result[0] = matrix[0] * scale.x;
  17731. result[1] = matrix[1] * scale.x;
  17732. result[2] = matrix[2] * scale.y;
  17733. result[3] = matrix[3] * scale.y;
  17734. return result;
  17735. };
  17736. /**
  17737. * Creates a negated copy of the provided matrix.
  17738. *
  17739. * @param {Matrix2} matrix The matrix to negate.
  17740. * @param {Matrix2} result The object onto which to store the result.
  17741. * @returns {Matrix2} The modified result parameter.
  17742. */
  17743. Matrix2.negate = function(matrix, result) {
  17744. if (!defined(matrix)) {
  17745. throw new DeveloperError('matrix is required');
  17746. }
  17747. if (!defined(result)) {
  17748. throw new DeveloperError('result is required');
  17749. }
  17750. result[0] = -matrix[0];
  17751. result[1] = -matrix[1];
  17752. result[2] = -matrix[2];
  17753. result[3] = -matrix[3];
  17754. return result;
  17755. };
  17756. /**
  17757. * Computes the transpose of the provided matrix.
  17758. *
  17759. * @param {Matrix2} matrix The matrix to transpose.
  17760. * @param {Matrix2} result The object onto which to store the result.
  17761. * @returns {Matrix2} The modified result parameter.
  17762. */
  17763. Matrix2.transpose = function(matrix, result) {
  17764. if (!defined(matrix)) {
  17765. throw new DeveloperError('matrix is required');
  17766. }
  17767. if (!defined(result)) {
  17768. throw new DeveloperError('result is required');
  17769. }
  17770. var column0Row0 = matrix[0];
  17771. var column0Row1 = matrix[2];
  17772. var column1Row0 = matrix[1];
  17773. var column1Row1 = matrix[3];
  17774. result[0] = column0Row0;
  17775. result[1] = column0Row1;
  17776. result[2] = column1Row0;
  17777. result[3] = column1Row1;
  17778. return result;
  17779. };
  17780. /**
  17781. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  17782. *
  17783. * @param {Matrix2} matrix The matrix with signed elements.
  17784. * @param {Matrix2} result The object onto which to store the result.
  17785. * @returns {Matrix2} The modified result parameter.
  17786. */
  17787. Matrix2.abs = function(matrix, result) {
  17788. if (!defined(matrix)) {
  17789. throw new DeveloperError('matrix is required');
  17790. }
  17791. if (!defined(result)) {
  17792. throw new DeveloperError('result is required');
  17793. }
  17794. result[0] = Math.abs(matrix[0]);
  17795. result[1] = Math.abs(matrix[1]);
  17796. result[2] = Math.abs(matrix[2]);
  17797. result[3] = Math.abs(matrix[3]);
  17798. return result;
  17799. };
  17800. /**
  17801. * Compares the provided matrices componentwise and returns
  17802. * <code>true</code> if they are equal, <code>false</code> otherwise.
  17803. *
  17804. * @param {Matrix2} [left] The first matrix.
  17805. * @param {Matrix2} [right] The second matrix.
  17806. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  17807. */
  17808. Matrix2.equals = function(left, right) {
  17809. return (left === right) ||
  17810. (defined(left) &&
  17811. defined(right) &&
  17812. left[0] === right[0] &&
  17813. left[1] === right[1] &&
  17814. left[2] === right[2] &&
  17815. left[3] === right[3]);
  17816. };
  17817. /**
  17818. * @private
  17819. */
  17820. Matrix2.equalsArray = function(matrix, array, offset) {
  17821. return matrix[0] === array[offset] &&
  17822. matrix[1] === array[offset + 1] &&
  17823. matrix[2] === array[offset + 2] &&
  17824. matrix[3] === array[offset + 3];
  17825. };
  17826. /**
  17827. * Compares the provided matrices componentwise and returns
  17828. * <code>true</code> if they are within the provided epsilon,
  17829. * <code>false</code> otherwise.
  17830. *
  17831. * @param {Matrix2} [left] The first matrix.
  17832. * @param {Matrix2} [right] The second matrix.
  17833. * @param {Number} epsilon The epsilon to use for equality testing.
  17834. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  17835. */
  17836. Matrix2.equalsEpsilon = function(left, right, epsilon) {
  17837. if (typeof epsilon !== 'number') {
  17838. throw new DeveloperError('epsilon must be a number');
  17839. }
  17840. return (left === right) ||
  17841. (defined(left) &&
  17842. defined(right) &&
  17843. Math.abs(left[0] - right[0]) <= epsilon &&
  17844. Math.abs(left[1] - right[1]) <= epsilon &&
  17845. Math.abs(left[2] - right[2]) <= epsilon &&
  17846. Math.abs(left[3] - right[3]) <= epsilon);
  17847. };
  17848. /**
  17849. * An immutable Matrix2 instance initialized to the identity matrix.
  17850. *
  17851. * @type {Matrix2}
  17852. * @constant
  17853. */
  17854. Matrix2.IDENTITY = freezeObject(new Matrix2(1.0, 0.0,
  17855. 0.0, 1.0));
  17856. /**
  17857. * An immutable Matrix2 instance initialized to the zero matrix.
  17858. *
  17859. * @type {Matrix2}
  17860. * @constant
  17861. */
  17862. Matrix2.ZERO = freezeObject(new Matrix2(0.0, 0.0,
  17863. 0.0, 0.0));
  17864. /**
  17865. * The index into Matrix2 for column 0, row 0.
  17866. *
  17867. * @type {Number}
  17868. * @constant
  17869. *
  17870. * @example
  17871. * var matrix = new Cesium.Matrix2();
  17872. * matrix[Cesium.Matrix2.COLUMN0ROW0] = 5.0; // set column 0, row 0 to 5.0
  17873. */
  17874. Matrix2.COLUMN0ROW0 = 0;
  17875. /**
  17876. * The index into Matrix2 for column 0, row 1.
  17877. *
  17878. * @type {Number}
  17879. * @constant
  17880. *
  17881. * @example
  17882. * var matrix = new Cesium.Matrix2();
  17883. * matrix[Cesium.Matrix2.COLUMN0ROW1] = 5.0; // set column 0, row 1 to 5.0
  17884. */
  17885. Matrix2.COLUMN0ROW1 = 1;
  17886. /**
  17887. * The index into Matrix2 for column 1, row 0.
  17888. *
  17889. * @type {Number}
  17890. * @constant
  17891. *
  17892. * @example
  17893. * var matrix = new Cesium.Matrix2();
  17894. * matrix[Cesium.Matrix2.COLUMN1ROW0] = 5.0; // set column 1, row 0 to 5.0
  17895. */
  17896. Matrix2.COLUMN1ROW0 = 2;
  17897. /**
  17898. * The index into Matrix2 for column 1, row 1.
  17899. *
  17900. * @type {Number}
  17901. * @constant
  17902. *
  17903. * @example
  17904. * var matrix = new Cesium.Matrix2();
  17905. * matrix[Cesium.Matrix2.COLUMN1ROW1] = 5.0; // set column 1, row 1 to 5.0
  17906. */
  17907. Matrix2.COLUMN1ROW1 = 3;
  17908. defineProperties(Matrix2.prototype, {
  17909. /**
  17910. * Gets the number of items in the collection.
  17911. * @memberof Matrix2.prototype
  17912. *
  17913. * @type {Number}
  17914. */
  17915. length : {
  17916. get : function() {
  17917. return Matrix2.packedLength;
  17918. }
  17919. }
  17920. });
  17921. /**
  17922. * Duplicates the provided Matrix2 instance.
  17923. *
  17924. * @param {Matrix2} [result] The object onto which to store the result.
  17925. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  17926. */
  17927. Matrix2.prototype.clone = function(result) {
  17928. return Matrix2.clone(this, result);
  17929. };
  17930. /**
  17931. * Compares this matrix to the provided matrix componentwise and returns
  17932. * <code>true</code> if they are equal, <code>false</code> otherwise.
  17933. *
  17934. * @param {Matrix2} [right] The right hand side matrix.
  17935. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  17936. */
  17937. Matrix2.prototype.equals = function(right) {
  17938. return Matrix2.equals(this, right);
  17939. };
  17940. /**
  17941. * Compares this matrix to the provided matrix componentwise and returns
  17942. * <code>true</code> if they are within the provided epsilon,
  17943. * <code>false</code> otherwise.
  17944. *
  17945. * @param {Matrix2} [right] The right hand side matrix.
  17946. * @param {Number} epsilon The epsilon to use for equality testing.
  17947. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  17948. */
  17949. Matrix2.prototype.equalsEpsilon = function(right, epsilon) {
  17950. return Matrix2.equalsEpsilon(this, right, epsilon);
  17951. };
  17952. /**
  17953. * Creates a string representing this Matrix with each row being
  17954. * on a separate line and in the format '(column0, column1)'.
  17955. *
  17956. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1)'.
  17957. */
  17958. Matrix2.prototype.toString = function() {
  17959. return '(' + this[0] + ', ' + this[2] + ')\n' +
  17960. '(' + this[1] + ', ' + this[3] + ')';
  17961. };
  17962. return Matrix2;
  17963. });
  17964. /*global define*/
  17965. define('ThirdParty/earcut-2.1.1',[], function() {
  17966. 'use strict';
  17967. function earcut(data, holeIndices, dim) {
  17968. dim = dim || 2;
  17969. var hasHoles = holeIndices && holeIndices.length,
  17970. outerLen = hasHoles ? holeIndices[0] * dim : data.length,
  17971. outerNode = linkedList(data, 0, outerLen, dim, true),
  17972. triangles = [];
  17973. if (!outerNode) return triangles;
  17974. var minX, minY, maxX, maxY, x, y, size;
  17975. if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
  17976. // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
  17977. if (data.length > 80 * dim) {
  17978. minX = maxX = data[0];
  17979. minY = maxY = data[1];
  17980. for (var i = dim; i < outerLen; i += dim) {
  17981. x = data[i];
  17982. y = data[i + 1];
  17983. if (x < minX) minX = x;
  17984. if (y < minY) minY = y;
  17985. if (x > maxX) maxX = x;
  17986. if (y > maxY) maxY = y;
  17987. }
  17988. // minX, minY and size are later used to transform coords into integers for z-order calculation
  17989. size = Math.max(maxX - minX, maxY - minY);
  17990. }
  17991. earcutLinked(outerNode, triangles, dim, minX, minY, size);
  17992. return triangles;
  17993. }
  17994. // create a circular doubly linked list from polygon points in the specified winding order
  17995. function linkedList(data, start, end, dim, clockwise) {
  17996. var i, last;
  17997. if (clockwise === (signedArea(data, start, end, dim) > 0)) {
  17998. for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
  17999. } else {
  18000. for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
  18001. }
  18002. if (last && equals(last, last.next)) {
  18003. removeNode(last);
  18004. last = last.next;
  18005. }
  18006. return last;
  18007. }
  18008. // eliminate colinear or duplicate points
  18009. function filterPoints(start, end) {
  18010. if (!start) return start;
  18011. if (!end) end = start;
  18012. var p = start,
  18013. again;
  18014. do {
  18015. again = false;
  18016. if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
  18017. removeNode(p);
  18018. p = end = p.prev;
  18019. if (p === p.next) return null;
  18020. again = true;
  18021. } else {
  18022. p = p.next;
  18023. }
  18024. } while (again || p !== end);
  18025. return end;
  18026. }
  18027. // main ear slicing loop which triangulates a polygon (given as a linked list)
  18028. function earcutLinked(ear, triangles, dim, minX, minY, size, pass) {
  18029. if (!ear) return;
  18030. // interlink polygon nodes in z-order
  18031. if (!pass && size) indexCurve(ear, minX, minY, size);
  18032. var stop = ear,
  18033. prev, next;
  18034. // iterate through ears, slicing them one by one
  18035. while (ear.prev !== ear.next) {
  18036. prev = ear.prev;
  18037. next = ear.next;
  18038. if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) {
  18039. // cut off the triangle
  18040. triangles.push(prev.i / dim);
  18041. triangles.push(ear.i / dim);
  18042. triangles.push(next.i / dim);
  18043. removeNode(ear);
  18044. // skipping the next vertice leads to less sliver triangles
  18045. ear = next.next;
  18046. stop = next.next;
  18047. continue;
  18048. }
  18049. ear = next;
  18050. // if we looped through the whole remaining polygon and can't find any more ears
  18051. if (ear === stop) {
  18052. // try filtering points and slicing again
  18053. if (!pass) {
  18054. earcutLinked(filterPoints(ear), triangles, dim, minX, minY, size, 1);
  18055. // if this didn't work, try curing all small self-intersections locally
  18056. } else if (pass === 1) {
  18057. ear = cureLocalIntersections(ear, triangles, dim);
  18058. earcutLinked(ear, triangles, dim, minX, minY, size, 2);
  18059. // as a last resort, try splitting the remaining polygon into two
  18060. } else if (pass === 2) {
  18061. splitEarcut(ear, triangles, dim, minX, minY, size);
  18062. }
  18063. break;
  18064. }
  18065. }
  18066. }
  18067. // check whether a polygon node forms a valid ear with adjacent nodes
  18068. function isEar(ear) {
  18069. var a = ear.prev,
  18070. b = ear,
  18071. c = ear.next;
  18072. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  18073. // now make sure we don't have other points inside the potential ear
  18074. var p = ear.next.next;
  18075. while (p !== ear.prev) {
  18076. if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  18077. area(p.prev, p, p.next) >= 0) return false;
  18078. p = p.next;
  18079. }
  18080. return true;
  18081. }
  18082. function isEarHashed(ear, minX, minY, size) {
  18083. var a = ear.prev,
  18084. b = ear,
  18085. c = ear.next;
  18086. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  18087. // triangle bbox; min & max are calculated like this for speed
  18088. var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x),
  18089. minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y),
  18090. maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x),
  18091. maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y);
  18092. // z-order range for the current triangle bbox;
  18093. var minZ = zOrder(minTX, minTY, minX, minY, size),
  18094. maxZ = zOrder(maxTX, maxTY, minX, minY, size);
  18095. // first look for points inside the triangle in increasing z-order
  18096. var p = ear.nextZ;
  18097. while (p && p.z <= maxZ) {
  18098. if (p !== ear.prev && p !== ear.next &&
  18099. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  18100. area(p.prev, p, p.next) >= 0) return false;
  18101. p = p.nextZ;
  18102. }
  18103. // then look for points in decreasing z-order
  18104. p = ear.prevZ;
  18105. while (p && p.z >= minZ) {
  18106. if (p !== ear.prev && p !== ear.next &&
  18107. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  18108. area(p.prev, p, p.next) >= 0) return false;
  18109. p = p.prevZ;
  18110. }
  18111. return true;
  18112. }
  18113. // go through all polygon nodes and cure small local self-intersections
  18114. function cureLocalIntersections(start, triangles, dim) {
  18115. var p = start;
  18116. do {
  18117. var a = p.prev,
  18118. b = p.next.next;
  18119. if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
  18120. triangles.push(a.i / dim);
  18121. triangles.push(p.i / dim);
  18122. triangles.push(b.i / dim);
  18123. // remove two nodes involved
  18124. removeNode(p);
  18125. removeNode(p.next);
  18126. p = start = b;
  18127. }
  18128. p = p.next;
  18129. } while (p !== start);
  18130. return p;
  18131. }
  18132. // try splitting polygon into two and triangulate them independently
  18133. function splitEarcut(start, triangles, dim, minX, minY, size) {
  18134. // look for a valid diagonal that divides the polygon into two
  18135. var a = start;
  18136. do {
  18137. var b = a.next.next;
  18138. while (b !== a.prev) {
  18139. if (a.i !== b.i && isValidDiagonal(a, b)) {
  18140. // split the polygon in two by the diagonal
  18141. var c = splitPolygon(a, b);
  18142. // filter colinear points around the cuts
  18143. a = filterPoints(a, a.next);
  18144. c = filterPoints(c, c.next);
  18145. // run earcut on each half
  18146. earcutLinked(a, triangles, dim, minX, minY, size);
  18147. earcutLinked(c, triangles, dim, minX, minY, size);
  18148. return;
  18149. }
  18150. b = b.next;
  18151. }
  18152. a = a.next;
  18153. } while (a !== start);
  18154. }
  18155. // link every hole into the outer loop, producing a single-ring polygon without holes
  18156. function eliminateHoles(data, holeIndices, outerNode, dim) {
  18157. var queue = [],
  18158. i, len, start, end, list;
  18159. for (i = 0, len = holeIndices.length; i < len; i++) {
  18160. start = holeIndices[i] * dim;
  18161. end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  18162. list = linkedList(data, start, end, dim, false);
  18163. if (list === list.next) list.steiner = true;
  18164. queue.push(getLeftmost(list));
  18165. }
  18166. queue.sort(compareX);
  18167. // process holes from left to right
  18168. for (i = 0; i < queue.length; i++) {
  18169. eliminateHole(queue[i], outerNode);
  18170. outerNode = filterPoints(outerNode, outerNode.next);
  18171. }
  18172. return outerNode;
  18173. }
  18174. function compareX(a, b) {
  18175. return a.x - b.x;
  18176. }
  18177. // find a bridge between vertices that connects hole with an outer ring and and link it
  18178. function eliminateHole(hole, outerNode) {
  18179. outerNode = findHoleBridge(hole, outerNode);
  18180. if (outerNode) {
  18181. var b = splitPolygon(outerNode, hole);
  18182. filterPoints(b, b.next);
  18183. }
  18184. }
  18185. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  18186. function findHoleBridge(hole, outerNode) {
  18187. var p = outerNode,
  18188. hx = hole.x,
  18189. hy = hole.y,
  18190. qx = -Infinity,
  18191. m;
  18192. // find a segment intersected by a ray from the hole's leftmost point to the left;
  18193. // segment's endpoint with lesser x will be potential connection point
  18194. do {
  18195. if (hy <= p.y && hy >= p.next.y) {
  18196. var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  18197. if (x <= hx && x > qx) {
  18198. qx = x;
  18199. if (x === hx) {
  18200. if (hy === p.y) return p;
  18201. if (hy === p.next.y) return p.next;
  18202. }
  18203. m = p.x < p.next.x ? p : p.next;
  18204. }
  18205. }
  18206. p = p.next;
  18207. } while (p !== outerNode);
  18208. if (!m) return null;
  18209. if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint
  18210. // look for points inside the triangle of hole point, segment intersection and endpoint;
  18211. // if there are no points found, we have a valid connection;
  18212. // otherwise choose the point of the minimum angle with the ray as connection point
  18213. var stop = m,
  18214. mx = m.x,
  18215. my = m.y,
  18216. tanMin = Infinity,
  18217. tan;
  18218. p = m.next;
  18219. while (p !== stop) {
  18220. if (hx >= p.x && p.x >= mx &&
  18221. pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  18222. tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
  18223. if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) {
  18224. m = p;
  18225. tanMin = tan;
  18226. }
  18227. }
  18228. p = p.next;
  18229. }
  18230. return m;
  18231. }
  18232. // interlink polygon nodes in z-order
  18233. function indexCurve(start, minX, minY, size) {
  18234. var p = start;
  18235. do {
  18236. if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, size);
  18237. p.prevZ = p.prev;
  18238. p.nextZ = p.next;
  18239. p = p.next;
  18240. } while (p !== start);
  18241. p.prevZ.nextZ = null;
  18242. p.prevZ = null;
  18243. sortLinked(p);
  18244. }
  18245. // Simon Tatham's linked list merge sort algorithm
  18246. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  18247. function sortLinked(list) {
  18248. var i, p, q, e, tail, numMerges, pSize, qSize,
  18249. inSize = 1;
  18250. do {
  18251. p = list;
  18252. list = null;
  18253. tail = null;
  18254. numMerges = 0;
  18255. while (p) {
  18256. numMerges++;
  18257. q = p;
  18258. pSize = 0;
  18259. for (i = 0; i < inSize; i++) {
  18260. pSize++;
  18261. q = q.nextZ;
  18262. if (!q) break;
  18263. }
  18264. qSize = inSize;
  18265. while (pSize > 0 || (qSize > 0 && q)) {
  18266. if (pSize === 0) {
  18267. e = q;
  18268. q = q.nextZ;
  18269. qSize--;
  18270. } else if (qSize === 0 || !q) {
  18271. e = p;
  18272. p = p.nextZ;
  18273. pSize--;
  18274. } else if (p.z <= q.z) {
  18275. e = p;
  18276. p = p.nextZ;
  18277. pSize--;
  18278. } else {
  18279. e = q;
  18280. q = q.nextZ;
  18281. qSize--;
  18282. }
  18283. if (tail) tail.nextZ = e;
  18284. else list = e;
  18285. e.prevZ = tail;
  18286. tail = e;
  18287. }
  18288. p = q;
  18289. }
  18290. tail.nextZ = null;
  18291. inSize *= 2;
  18292. } while (numMerges > 1);
  18293. return list;
  18294. }
  18295. // z-order of a point given coords and size of the data bounding box
  18296. function zOrder(x, y, minX, minY, size) {
  18297. // coords are transformed into non-negative 15-bit integer range
  18298. x = 32767 * (x - minX) / size;
  18299. y = 32767 * (y - minY) / size;
  18300. x = (x | (x << 8)) & 0x00FF00FF;
  18301. x = (x | (x << 4)) & 0x0F0F0F0F;
  18302. x = (x | (x << 2)) & 0x33333333;
  18303. x = (x | (x << 1)) & 0x55555555;
  18304. y = (y | (y << 8)) & 0x00FF00FF;
  18305. y = (y | (y << 4)) & 0x0F0F0F0F;
  18306. y = (y | (y << 2)) & 0x33333333;
  18307. y = (y | (y << 1)) & 0x55555555;
  18308. return x | (y << 1);
  18309. }
  18310. // find the leftmost node of a polygon ring
  18311. function getLeftmost(start) {
  18312. var p = start,
  18313. leftmost = start;
  18314. do {
  18315. if (p.x < leftmost.x) leftmost = p;
  18316. p = p.next;
  18317. } while (p !== start);
  18318. return leftmost;
  18319. }
  18320. // check if a point lies within a convex triangle
  18321. function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
  18322. return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
  18323. (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
  18324. (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
  18325. }
  18326. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  18327. function isValidDiagonal(a, b) {
  18328. return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
  18329. locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b);
  18330. }
  18331. // signed area of a triangle
  18332. function area(p, q, r) {
  18333. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  18334. }
  18335. // check if two points are equal
  18336. function equals(p1, p2) {
  18337. return p1.x === p2.x && p1.y === p2.y;
  18338. }
  18339. // check if two segments intersect
  18340. function intersects(p1, q1, p2, q2) {
  18341. if ((equals(p1, q1) && equals(p2, q2)) ||
  18342. (equals(p1, q2) && equals(p2, q1))) return true;
  18343. return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 &&
  18344. area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0;
  18345. }
  18346. // check if a polygon diagonal intersects any polygon segments
  18347. function intersectsPolygon(a, b) {
  18348. var p = a;
  18349. do {
  18350. if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  18351. intersects(p, p.next, a, b)) return true;
  18352. p = p.next;
  18353. } while (p !== a);
  18354. return false;
  18355. }
  18356. // check if a polygon diagonal is locally inside the polygon
  18357. function locallyInside(a, b) {
  18358. return area(a.prev, a, a.next) < 0 ?
  18359. area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
  18360. area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
  18361. }
  18362. // check if the middle point of a polygon diagonal is inside the polygon
  18363. function middleInside(a, b) {
  18364. var p = a,
  18365. inside = false,
  18366. px = (a.x + b.x) / 2,
  18367. py = (a.y + b.y) / 2;
  18368. do {
  18369. if (((p.y > py) !== (p.next.y > py)) && (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  18370. inside = !inside;
  18371. p = p.next;
  18372. } while (p !== a);
  18373. return inside;
  18374. }
  18375. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  18376. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  18377. function splitPolygon(a, b) {
  18378. var a2 = new Node(a.i, a.x, a.y),
  18379. b2 = new Node(b.i, b.x, b.y),
  18380. an = a.next,
  18381. bp = b.prev;
  18382. a.next = b;
  18383. b.prev = a;
  18384. a2.next = an;
  18385. an.prev = a2;
  18386. b2.next = a2;
  18387. a2.prev = b2;
  18388. bp.next = b2;
  18389. b2.prev = bp;
  18390. return b2;
  18391. }
  18392. // create a node and optionally link it with previous one (in a circular doubly linked list)
  18393. function insertNode(i, x, y, last) {
  18394. var p = new Node(i, x, y);
  18395. if (!last) {
  18396. p.prev = p;
  18397. p.next = p;
  18398. } else {
  18399. p.next = last.next;
  18400. p.prev = last;
  18401. last.next.prev = p;
  18402. last.next = p;
  18403. }
  18404. return p;
  18405. }
  18406. function removeNode(p) {
  18407. p.next.prev = p.prev;
  18408. p.prev.next = p.next;
  18409. if (p.prevZ) p.prevZ.nextZ = p.nextZ;
  18410. if (p.nextZ) p.nextZ.prevZ = p.prevZ;
  18411. }
  18412. function Node(i, x, y) {
  18413. // vertice index in coordinates array
  18414. this.i = i;
  18415. // vertex coordinates
  18416. this.x = x;
  18417. this.y = y;
  18418. // previous and next vertice nodes in a polygon ring
  18419. this.prev = null;
  18420. this.next = null;
  18421. // z-order curve value
  18422. this.z = null;
  18423. // previous and next nodes in z-order
  18424. this.prevZ = null;
  18425. this.nextZ = null;
  18426. // indicates whether this is a steiner point
  18427. this.steiner = false;
  18428. }
  18429. // return a percentage difference between the polygon area and its triangulation area;
  18430. // used to verify correctness of triangulation
  18431. earcut.deviation = function (data, holeIndices, dim, triangles) {
  18432. var hasHoles = holeIndices && holeIndices.length;
  18433. var outerLen = hasHoles ? holeIndices[0] * dim : data.length;
  18434. var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
  18435. if (hasHoles) {
  18436. for (var i = 0, len = holeIndices.length; i < len; i++) {
  18437. var start = holeIndices[i] * dim;
  18438. var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  18439. polygonArea -= Math.abs(signedArea(data, start, end, dim));
  18440. }
  18441. }
  18442. var trianglesArea = 0;
  18443. for (i = 0; i < triangles.length; i += 3) {
  18444. var a = triangles[i] * dim;
  18445. var b = triangles[i + 1] * dim;
  18446. var c = triangles[i + 2] * dim;
  18447. trianglesArea += Math.abs(
  18448. (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
  18449. (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
  18450. }
  18451. return polygonArea === 0 && trianglesArea === 0 ? 0 :
  18452. Math.abs((trianglesArea - polygonArea) / polygonArea);
  18453. };
  18454. function signedArea(data, start, end, dim) {
  18455. var sum = 0;
  18456. for (var i = start, j = end - dim; i < end; i += dim) {
  18457. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  18458. j = i;
  18459. }
  18460. return sum;
  18461. }
  18462. // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
  18463. earcut.flatten = function (data) {
  18464. var dim = data[0][0].length,
  18465. result = {vertices: [], holes: [], dimensions: dim},
  18466. holeIndex = 0;
  18467. for (var i = 0; i < data.length; i++) {
  18468. for (var j = 0; j < data[i].length; j++) {
  18469. for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
  18470. }
  18471. if (i > 0) {
  18472. holeIndex += data[i - 1].length;
  18473. result.holes.push(holeIndex);
  18474. }
  18475. }
  18476. return result;
  18477. };
  18478. return earcut;
  18479. });
  18480. /*global define*/
  18481. define('Core/WindingOrder',[
  18482. './freezeObject',
  18483. './WebGLConstants'
  18484. ], function(
  18485. freezeObject,
  18486. WebGLConstants) {
  18487. 'use strict';
  18488. /**
  18489. * Winding order defines the order of vertices for a triangle to be considered front-facing.
  18490. *
  18491. * @exports WindingOrder
  18492. */
  18493. var WindingOrder = {
  18494. /**
  18495. * Vertices are in clockwise order.
  18496. *
  18497. * @type {Number}
  18498. * @constant
  18499. */
  18500. CLOCKWISE : WebGLConstants.CW,
  18501. /**
  18502. * Vertices are in counter-clockwise order.
  18503. *
  18504. * @type {Number}
  18505. * @constant
  18506. */
  18507. COUNTER_CLOCKWISE : WebGLConstants.CCW,
  18508. /**
  18509. * @private
  18510. */
  18511. validate : function(windingOrder) {
  18512. return windingOrder === WindingOrder.CLOCKWISE ||
  18513. windingOrder === WindingOrder.COUNTER_CLOCKWISE;
  18514. }
  18515. };
  18516. return freezeObject(WindingOrder);
  18517. });
  18518. /*global define*/
  18519. define('Core/PolygonPipeline',[
  18520. '../ThirdParty/earcut-2.1.1',
  18521. './Cartesian2',
  18522. './Cartesian3',
  18523. './ComponentDatatype',
  18524. './defaultValue',
  18525. './defined',
  18526. './DeveloperError',
  18527. './Ellipsoid',
  18528. './Geometry',
  18529. './GeometryAttribute',
  18530. './Math',
  18531. './PrimitiveType',
  18532. './WindingOrder'
  18533. ], function(
  18534. earcut,
  18535. Cartesian2,
  18536. Cartesian3,
  18537. ComponentDatatype,
  18538. defaultValue,
  18539. defined,
  18540. DeveloperError,
  18541. Ellipsoid,
  18542. Geometry,
  18543. GeometryAttribute,
  18544. CesiumMath,
  18545. PrimitiveType,
  18546. WindingOrder) {
  18547. 'use strict';
  18548. var scaleToGeodeticHeightN = new Cartesian3();
  18549. var scaleToGeodeticHeightP = new Cartesian3();
  18550. /**
  18551. * @private
  18552. */
  18553. var PolygonPipeline = {};
  18554. /**
  18555. * @exception {DeveloperError} At least three positions are required.
  18556. */
  18557. PolygonPipeline.computeArea2D = function(positions) {
  18558. if (!defined(positions)) {
  18559. throw new DeveloperError('positions is required.');
  18560. }
  18561. if (positions.length < 3) {
  18562. throw new DeveloperError('At least three positions are required.');
  18563. }
  18564. var length = positions.length;
  18565. var area = 0.0;
  18566. for ( var i0 = length - 1, i1 = 0; i1 < length; i0 = i1++) {
  18567. var v0 = positions[i0];
  18568. var v1 = positions[i1];
  18569. area += (v0.x * v1.y) - (v1.x * v0.y);
  18570. }
  18571. return area * 0.5;
  18572. };
  18573. /**
  18574. * @returns {WindingOrder} The winding order.
  18575. *
  18576. * @exception {DeveloperError} At least three positions are required.
  18577. */
  18578. PolygonPipeline.computeWindingOrder2D = function(positions) {
  18579. var area = PolygonPipeline.computeArea2D(positions);
  18580. return (area > 0.0) ? WindingOrder.COUNTER_CLOCKWISE : WindingOrder.CLOCKWISE;
  18581. };
  18582. /**
  18583. * Triangulate a polygon.
  18584. *
  18585. * @param {Cartesian2[]} positions Cartesian2 array containing the vertices of the polygon
  18586. * @param {Number[]} [holes] An array of the staring indices of the holes.
  18587. * @returns {Number[]} Index array representing triangles that fill the polygon
  18588. */
  18589. PolygonPipeline.triangulate = function(positions, holes) {
  18590. if (!defined(positions)) {
  18591. throw new DeveloperError('positions is required.');
  18592. }
  18593. var flattenedPositions = Cartesian2.packArray(positions);
  18594. return earcut(flattenedPositions, holes, 2);
  18595. };
  18596. var subdivisionV0Scratch = new Cartesian3();
  18597. var subdivisionV1Scratch = new Cartesian3();
  18598. var subdivisionV2Scratch = new Cartesian3();
  18599. var subdivisionS0Scratch = new Cartesian3();
  18600. var subdivisionS1Scratch = new Cartesian3();
  18601. var subdivisionS2Scratch = new Cartesian3();
  18602. var subdivisionMidScratch = new Cartesian3();
  18603. /**
  18604. * Subdivides positions and raises points to the surface of the ellipsoid.
  18605. *
  18606. * @param {Ellipsoid} ellipsoid The ellipsoid the polygon in on.
  18607. * @param {Cartesian3[]} positions An array of {@link Cartesian3} positions of the polygon.
  18608. * @param {Number[]} indices An array of indices that determines the triangles in the polygon.
  18609. * @param {Number} [granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  18610. *
  18611. * @exception {DeveloperError} At least three indices are required.
  18612. * @exception {DeveloperError} The number of indices must be divisable by three.
  18613. * @exception {DeveloperError} Granularity must be greater than zero.
  18614. */
  18615. PolygonPipeline.computeSubdivision = function(ellipsoid, positions, indices, granularity) {
  18616. granularity = defaultValue(granularity, CesiumMath.RADIANS_PER_DEGREE);
  18617. if (!defined(ellipsoid)) {
  18618. throw new DeveloperError('ellipsoid is required.');
  18619. }
  18620. if (!defined(positions)) {
  18621. throw new DeveloperError('positions is required.');
  18622. }
  18623. if (!defined(indices)) {
  18624. throw new DeveloperError('indices is required.');
  18625. }
  18626. if (indices.length < 3) {
  18627. throw new DeveloperError('At least three indices are required.');
  18628. }
  18629. if (indices.length % 3 !== 0) {
  18630. throw new DeveloperError('The number of indices must be divisable by three.');
  18631. }
  18632. if (granularity <= 0.0) {
  18633. throw new DeveloperError('granularity must be greater than zero.');
  18634. }
  18635. // triangles that need (or might need) to be subdivided.
  18636. var triangles = indices.slice(0);
  18637. // New positions due to edge splits are appended to the positions list.
  18638. var i;
  18639. var length = positions.length;
  18640. var subdividedPositions = new Array(length * 3);
  18641. var q = 0;
  18642. for (i = 0; i < length; i++) {
  18643. var item = positions[i];
  18644. subdividedPositions[q++] = item.x;
  18645. subdividedPositions[q++] = item.y;
  18646. subdividedPositions[q++] = item.z;
  18647. }
  18648. var subdividedIndices = [];
  18649. // Used to make sure shared edges are not split more than once.
  18650. var edges = {};
  18651. var radius = ellipsoid.maximumRadius;
  18652. var minDistance = CesiumMath.chordLength(granularity, radius);
  18653. var minDistanceSqrd = minDistance * minDistance;
  18654. while (triangles.length > 0) {
  18655. var i2 = triangles.pop();
  18656. var i1 = triangles.pop();
  18657. var i0 = triangles.pop();
  18658. var v0 = Cartesian3.fromArray(subdividedPositions, i0 * 3, subdivisionV0Scratch);
  18659. var v1 = Cartesian3.fromArray(subdividedPositions, i1 * 3, subdivisionV1Scratch);
  18660. var v2 = Cartesian3.fromArray(subdividedPositions, i2 * 3, subdivisionV2Scratch);
  18661. var s0 = Cartesian3.multiplyByScalar(Cartesian3.normalize(v0, subdivisionS0Scratch), radius, subdivisionS0Scratch);
  18662. var s1 = Cartesian3.multiplyByScalar(Cartesian3.normalize(v1, subdivisionS1Scratch), radius, subdivisionS1Scratch);
  18663. var s2 = Cartesian3.multiplyByScalar(Cartesian3.normalize(v2, subdivisionS2Scratch), radius, subdivisionS2Scratch);
  18664. var g0 = Cartesian3.magnitudeSquared(Cartesian3.subtract(s0, s1, subdivisionMidScratch));
  18665. var g1 = Cartesian3.magnitudeSquared(Cartesian3.subtract(s1, s2, subdivisionMidScratch));
  18666. var g2 = Cartesian3.magnitudeSquared(Cartesian3.subtract(s2, s0, subdivisionMidScratch));
  18667. var max = Math.max(g0, g1, g2);
  18668. var edge;
  18669. var mid;
  18670. // if the max length squared of a triangle edge is greater than the chord length of squared
  18671. // of the granularity, subdivide the triangle
  18672. if (max > minDistanceSqrd) {
  18673. if (g0 === max) {
  18674. edge = Math.min(i0, i1) + ' ' + Math.max(i0, i1);
  18675. i = edges[edge];
  18676. if (!defined(i)) {
  18677. mid = Cartesian3.add(v0, v1, subdivisionMidScratch);
  18678. Cartesian3.multiplyByScalar(mid, 0.5, mid);
  18679. subdividedPositions.push(mid.x, mid.y, mid.z);
  18680. i = subdividedPositions.length / 3 - 1;
  18681. edges[edge] = i;
  18682. }
  18683. triangles.push(i0, i, i2);
  18684. triangles.push(i, i1, i2);
  18685. } else if (g1 === max) {
  18686. edge = Math.min(i1, i2) + ' ' + Math.max(i1, i2);
  18687. i = edges[edge];
  18688. if (!defined(i)) {
  18689. mid = Cartesian3.add(v1, v2, subdivisionMidScratch);
  18690. Cartesian3.multiplyByScalar(mid, 0.5, mid);
  18691. subdividedPositions.push(mid.x, mid.y, mid.z);
  18692. i = subdividedPositions.length / 3 - 1;
  18693. edges[edge] = i;
  18694. }
  18695. triangles.push(i1, i, i0);
  18696. triangles.push(i, i2, i0);
  18697. } else if (g2 === max) {
  18698. edge = Math.min(i2, i0) + ' ' + Math.max(i2, i0);
  18699. i = edges[edge];
  18700. if (!defined(i)) {
  18701. mid = Cartesian3.add(v2, v0, subdivisionMidScratch);
  18702. Cartesian3.multiplyByScalar(mid, 0.5, mid);
  18703. subdividedPositions.push(mid.x, mid.y, mid.z);
  18704. i = subdividedPositions.length / 3 - 1;
  18705. edges[edge] = i;
  18706. }
  18707. triangles.push(i2, i, i1);
  18708. triangles.push(i, i0, i1);
  18709. }
  18710. } else {
  18711. subdividedIndices.push(i0);
  18712. subdividedIndices.push(i1);
  18713. subdividedIndices.push(i2);
  18714. }
  18715. }
  18716. return new Geometry({
  18717. attributes : {
  18718. position : new GeometryAttribute({
  18719. componentDatatype : ComponentDatatype.DOUBLE,
  18720. componentsPerAttribute : 3,
  18721. values : subdividedPositions
  18722. })
  18723. },
  18724. indices : subdividedIndices,
  18725. primitiveType : PrimitiveType.TRIANGLES
  18726. });
  18727. };
  18728. /**
  18729. * Scales each position of a geometry's position attribute to a height, in place.
  18730. *
  18731. * @param {Number[]} positions The array of numbers representing the positions to be scaled
  18732. * @param {Number} [height=0.0] The desired height to add to the positions
  18733. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the positions lie.
  18734. * @param {Boolean} [scaleToSurface=true] <code>true</code> if the positions need to be scaled to the surface before the height is added.
  18735. * @returns {Number[]} The input array of positions, scaled to height
  18736. */
  18737. PolygonPipeline.scaleToGeodeticHeight = function(positions, height, ellipsoid, scaleToSurface) {
  18738. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  18739. var n = scaleToGeodeticHeightN;
  18740. var p = scaleToGeodeticHeightP;
  18741. height = defaultValue(height, 0.0);
  18742. scaleToSurface = defaultValue(scaleToSurface, true);
  18743. if (defined(positions)) {
  18744. var length = positions.length;
  18745. for ( var i = 0; i < length; i += 3) {
  18746. Cartesian3.fromArray(positions, i, p);
  18747. if (scaleToSurface) {
  18748. p = ellipsoid.scaleToGeodeticSurface(p, p);
  18749. }
  18750. if (height !== 0) {
  18751. n = ellipsoid.geodeticSurfaceNormal(p, n);
  18752. Cartesian3.multiplyByScalar(n, height, n);
  18753. Cartesian3.add(p, n, p);
  18754. }
  18755. positions[i] = p.x;
  18756. positions[i + 1] = p.y;
  18757. positions[i + 2] = p.z;
  18758. }
  18759. }
  18760. return positions;
  18761. };
  18762. return PolygonPipeline;
  18763. });
  18764. /*global define*/
  18765. define('Core/Quaternion',[
  18766. './Cartesian3',
  18767. './defaultValue',
  18768. './defined',
  18769. './DeveloperError',
  18770. './FeatureDetection',
  18771. './freezeObject',
  18772. './Math',
  18773. './Matrix3'
  18774. ], function(
  18775. Cartesian3,
  18776. defaultValue,
  18777. defined,
  18778. DeveloperError,
  18779. FeatureDetection,
  18780. freezeObject,
  18781. CesiumMath,
  18782. Matrix3) {
  18783. 'use strict';
  18784. /**
  18785. * A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
  18786. * @alias Quaternion
  18787. * @constructor
  18788. *
  18789. * @param {Number} [x=0.0] The X component.
  18790. * @param {Number} [y=0.0] The Y component.
  18791. * @param {Number} [z=0.0] The Z component.
  18792. * @param {Number} [w=0.0] The W component.
  18793. *
  18794. * @see PackableForInterpolation
  18795. */
  18796. function Quaternion(x, y, z, w) {
  18797. /**
  18798. * The X component.
  18799. * @type {Number}
  18800. * @default 0.0
  18801. */
  18802. this.x = defaultValue(x, 0.0);
  18803. /**
  18804. * The Y component.
  18805. * @type {Number}
  18806. * @default 0.0
  18807. */
  18808. this.y = defaultValue(y, 0.0);
  18809. /**
  18810. * The Z component.
  18811. * @type {Number}
  18812. * @default 0.0
  18813. */
  18814. this.z = defaultValue(z, 0.0);
  18815. /**
  18816. * The W component.
  18817. * @type {Number}
  18818. * @default 0.0
  18819. */
  18820. this.w = defaultValue(w, 0.0);
  18821. }
  18822. var fromAxisAngleScratch = new Cartesian3();
  18823. /**
  18824. * Computes a quaternion representing a rotation around an axis.
  18825. *
  18826. * @param {Cartesian3} axis The axis of rotation.
  18827. * @param {Number} angle The angle in radians to rotate around the axis.
  18828. * @param {Quaternion} [result] The object onto which to store the result.
  18829. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18830. */
  18831. Quaternion.fromAxisAngle = function(axis, angle, result) {
  18832. if (!defined(axis)) {
  18833. throw new DeveloperError('axis is required.');
  18834. }
  18835. if (typeof angle !== 'number') {
  18836. throw new DeveloperError('angle is required and must be a number.');
  18837. }
  18838. var halfAngle = angle / 2.0;
  18839. var s = Math.sin(halfAngle);
  18840. fromAxisAngleScratch = Cartesian3.normalize(axis, fromAxisAngleScratch);
  18841. var x = fromAxisAngleScratch.x * s;
  18842. var y = fromAxisAngleScratch.y * s;
  18843. var z = fromAxisAngleScratch.z * s;
  18844. var w = Math.cos(halfAngle);
  18845. if (!defined(result)) {
  18846. return new Quaternion(x, y, z, w);
  18847. }
  18848. result.x = x;
  18849. result.y = y;
  18850. result.z = z;
  18851. result.w = w;
  18852. return result;
  18853. };
  18854. var fromRotationMatrixNext = [1, 2, 0];
  18855. var fromRotationMatrixQuat = new Array(3);
  18856. /**
  18857. * Computes a Quaternion from the provided Matrix3 instance.
  18858. *
  18859. * @param {Matrix3} matrix The rotation matrix.
  18860. * @param {Quaternion} [result] The object onto which to store the result.
  18861. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18862. *
  18863. * @see Matrix3.fromQuaternion
  18864. */
  18865. Quaternion.fromRotationMatrix = function(matrix, result) {
  18866. if (!defined(matrix)) {
  18867. throw new DeveloperError('matrix is required.');
  18868. }
  18869. var root;
  18870. var x;
  18871. var y;
  18872. var z;
  18873. var w;
  18874. var m00 = matrix[Matrix3.COLUMN0ROW0];
  18875. var m11 = matrix[Matrix3.COLUMN1ROW1];
  18876. var m22 = matrix[Matrix3.COLUMN2ROW2];
  18877. var trace = m00 + m11 + m22;
  18878. if (trace > 0.0) {
  18879. // |w| > 1/2, may as well choose w > 1/2
  18880. root = Math.sqrt(trace + 1.0); // 2w
  18881. w = 0.5 * root;
  18882. root = 0.5 / root; // 1/(4w)
  18883. x = (matrix[Matrix3.COLUMN1ROW2] - matrix[Matrix3.COLUMN2ROW1]) * root;
  18884. y = (matrix[Matrix3.COLUMN2ROW0] - matrix[Matrix3.COLUMN0ROW2]) * root;
  18885. z = (matrix[Matrix3.COLUMN0ROW1] - matrix[Matrix3.COLUMN1ROW0]) * root;
  18886. } else {
  18887. // |w| <= 1/2
  18888. var next = fromRotationMatrixNext;
  18889. var i = 0;
  18890. if (m11 > m00) {
  18891. i = 1;
  18892. }
  18893. if (m22 > m00 && m22 > m11) {
  18894. i = 2;
  18895. }
  18896. var j = next[i];
  18897. var k = next[j];
  18898. root = Math.sqrt(matrix[Matrix3.getElementIndex(i, i)] - matrix[Matrix3.getElementIndex(j, j)] - matrix[Matrix3.getElementIndex(k, k)] + 1.0);
  18899. var quat = fromRotationMatrixQuat;
  18900. quat[i] = 0.5 * root;
  18901. root = 0.5 / root;
  18902. w = (matrix[Matrix3.getElementIndex(k, j)] - matrix[Matrix3.getElementIndex(j, k)]) * root;
  18903. quat[j] = (matrix[Matrix3.getElementIndex(j, i)] + matrix[Matrix3.getElementIndex(i, j)]) * root;
  18904. quat[k] = (matrix[Matrix3.getElementIndex(k, i)] + matrix[Matrix3.getElementIndex(i, k)]) * root;
  18905. x = -quat[0];
  18906. y = -quat[1];
  18907. z = -quat[2];
  18908. }
  18909. if (!defined(result)) {
  18910. return new Quaternion(x, y, z, w);
  18911. }
  18912. result.x = x;
  18913. result.y = y;
  18914. result.z = z;
  18915. result.w = w;
  18916. return result;
  18917. };
  18918. var scratchHPRQuaternion = new Quaternion();
  18919. /**
  18920. * Computes a rotation from the given heading, pitch and roll angles. Heading is the rotation about the
  18921. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  18922. * the positive x axis.
  18923. *
  18924. * @param {Number} heading The heading angle in radians.
  18925. * @param {Number} pitch The pitch angle in radians.
  18926. * @param {Number} roll The roll angle in radians.
  18927. * @param {Quaternion} [result] The object onto which to store the result.
  18928. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  18929. */
  18930. Quaternion.fromHeadingPitchRoll = function(heading, pitch, roll, result) {
  18931. if (!defined(heading)) {
  18932. throw new DeveloperError('heading is required.');
  18933. }
  18934. if (!defined(pitch)) {
  18935. throw new DeveloperError('pitch is required.');
  18936. }
  18937. if (!defined(roll)) {
  18938. throw new DeveloperError('roll is required.');
  18939. }
  18940. var rollQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_X, roll, scratchHPRQuaternion);
  18941. var pitchQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_Y, -pitch, result);
  18942. result = Quaternion.multiply(pitchQuaternion, rollQuaternion, pitchQuaternion);
  18943. var headingQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_Z, -heading, scratchHPRQuaternion);
  18944. return Quaternion.multiply(headingQuaternion, result, result);
  18945. };
  18946. var sampledQuaternionAxis = new Cartesian3();
  18947. var sampledQuaternionRotation = new Cartesian3();
  18948. var sampledQuaternionTempQuaternion = new Quaternion();
  18949. var sampledQuaternionQuaternion0 = new Quaternion();
  18950. var sampledQuaternionQuaternion0Conjugate = new Quaternion();
  18951. /**
  18952. * The number of elements used to pack the object into an array.
  18953. * @type {Number}
  18954. */
  18955. Quaternion.packedLength = 4;
  18956. /**
  18957. * Stores the provided instance into the provided array.
  18958. *
  18959. * @param {Quaternion} value The value to pack.
  18960. * @param {Number[]} array The array to pack into.
  18961. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  18962. *
  18963. * @returns {Number[]} The array that was packed into
  18964. */
  18965. Quaternion.pack = function(value, array, startingIndex) {
  18966. if (!defined(value)) {
  18967. throw new DeveloperError('value is required');
  18968. }
  18969. if (!defined(array)) {
  18970. throw new DeveloperError('array is required');
  18971. }
  18972. startingIndex = defaultValue(startingIndex, 0);
  18973. array[startingIndex++] = value.x;
  18974. array[startingIndex++] = value.y;
  18975. array[startingIndex++] = value.z;
  18976. array[startingIndex] = value.w;
  18977. return array;
  18978. };
  18979. /**
  18980. * Retrieves an instance from a packed array.
  18981. *
  18982. * @param {Number[]} array The packed array.
  18983. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  18984. * @param {Quaternion} [result] The object into which to store the result.
  18985. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18986. */
  18987. Quaternion.unpack = function(array, startingIndex, result) {
  18988. if (!defined(array)) {
  18989. throw new DeveloperError('array is required');
  18990. }
  18991. startingIndex = defaultValue(startingIndex, 0);
  18992. if (!defined(result)) {
  18993. result = new Quaternion();
  18994. }
  18995. result.x = array[startingIndex];
  18996. result.y = array[startingIndex + 1];
  18997. result.z = array[startingIndex + 2];
  18998. result.w = array[startingIndex + 3];
  18999. return result;
  19000. };
  19001. /**
  19002. * The number of elements used to store the object into an array in its interpolatable form.
  19003. * @type {Number}
  19004. */
  19005. Quaternion.packedInterpolationLength = 3;
  19006. /**
  19007. * Converts a packed array into a form suitable for interpolation.
  19008. *
  19009. * @param {Number[]} packedArray The packed array.
  19010. * @param {Number} [startingIndex=0] The index of the first element to be converted.
  19011. * @param {Number} [lastIndex=packedArray.length] The index of the last element to be converted.
  19012. * @param {Number[]} result The object into which to store the result.
  19013. */
  19014. Quaternion.convertPackedArrayForInterpolation = function(packedArray, startingIndex, lastIndex, result) {
  19015. Quaternion.unpack(packedArray, lastIndex * 4, sampledQuaternionQuaternion0Conjugate);
  19016. Quaternion.conjugate(sampledQuaternionQuaternion0Conjugate, sampledQuaternionQuaternion0Conjugate);
  19017. for (var i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
  19018. var offset = i * 3;
  19019. Quaternion.unpack(packedArray, (startingIndex + i) * 4, sampledQuaternionTempQuaternion);
  19020. Quaternion.multiply(sampledQuaternionTempQuaternion, sampledQuaternionQuaternion0Conjugate, sampledQuaternionTempQuaternion);
  19021. if (sampledQuaternionTempQuaternion.w < 0) {
  19022. Quaternion.negate(sampledQuaternionTempQuaternion, sampledQuaternionTempQuaternion);
  19023. }
  19024. Quaternion.computeAxis(sampledQuaternionTempQuaternion, sampledQuaternionAxis);
  19025. var angle = Quaternion.computeAngle(sampledQuaternionTempQuaternion);
  19026. result[offset] = sampledQuaternionAxis.x * angle;
  19027. result[offset + 1] = sampledQuaternionAxis.y * angle;
  19028. result[offset + 2] = sampledQuaternionAxis.z * angle;
  19029. }
  19030. };
  19031. /**
  19032. * Retrieves an instance from a packed array converted with {@link convertPackedArrayForInterpolation}.
  19033. *
  19034. * @param {Number[]} array The array previously packed for interpolation.
  19035. * @param {Number[]} sourceArray The original packed array.
  19036. * @param {Number} [startingIndex=0] The startingIndex used to convert the array.
  19037. * @param {Number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
  19038. * @param {Quaternion} [result] The object into which to store the result.
  19039. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  19040. */
  19041. Quaternion.unpackInterpolationResult = function(array, sourceArray, firstIndex, lastIndex, result) {
  19042. if (!defined(result)) {
  19043. result = new Quaternion();
  19044. }
  19045. Cartesian3.fromArray(array, 0, sampledQuaternionRotation);
  19046. var magnitude = Cartesian3.magnitude(sampledQuaternionRotation);
  19047. Quaternion.unpack(sourceArray, lastIndex * 4, sampledQuaternionQuaternion0);
  19048. if (magnitude === 0) {
  19049. Quaternion.clone(Quaternion.IDENTITY, sampledQuaternionTempQuaternion);
  19050. } else {
  19051. Quaternion.fromAxisAngle(sampledQuaternionRotation, magnitude, sampledQuaternionTempQuaternion);
  19052. }
  19053. return Quaternion.multiply(sampledQuaternionTempQuaternion, sampledQuaternionQuaternion0, result);
  19054. };
  19055. /**
  19056. * Duplicates a Quaternion instance.
  19057. *
  19058. * @param {Quaternion} quaternion The quaternion to duplicate.
  19059. * @param {Quaternion} [result] The object onto which to store the result.
  19060. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided. (Returns undefined if quaternion is undefined)
  19061. */
  19062. Quaternion.clone = function(quaternion, result) {
  19063. if (!defined(quaternion)) {
  19064. return undefined;
  19065. }
  19066. if (!defined(result)) {
  19067. return new Quaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
  19068. }
  19069. result.x = quaternion.x;
  19070. result.y = quaternion.y;
  19071. result.z = quaternion.z;
  19072. result.w = quaternion.w;
  19073. return result;
  19074. };
  19075. /**
  19076. * Computes the conjugate of the provided quaternion.
  19077. *
  19078. * @param {Quaternion} quaternion The quaternion to conjugate.
  19079. * @param {Quaternion} result The object onto which to store the result.
  19080. * @returns {Quaternion} The modified result parameter.
  19081. */
  19082. Quaternion.conjugate = function(quaternion, result) {
  19083. if (!defined(quaternion)) {
  19084. throw new DeveloperError('quaternion is required');
  19085. }
  19086. if (!defined(result)) {
  19087. throw new DeveloperError('result is required');
  19088. }
  19089. result.x = -quaternion.x;
  19090. result.y = -quaternion.y;
  19091. result.z = -quaternion.z;
  19092. result.w = quaternion.w;
  19093. return result;
  19094. };
  19095. /**
  19096. * Computes magnitude squared for the provided quaternion.
  19097. *
  19098. * @param {Quaternion} quaternion The quaternion to conjugate.
  19099. * @returns {Number} The magnitude squared.
  19100. */
  19101. Quaternion.magnitudeSquared = function(quaternion) {
  19102. if (!defined(quaternion)) {
  19103. throw new DeveloperError('quaternion is required');
  19104. }
  19105. return quaternion.x * quaternion.x + quaternion.y * quaternion.y + quaternion.z * quaternion.z + quaternion.w * quaternion.w;
  19106. };
  19107. /**
  19108. * Computes magnitude for the provided quaternion.
  19109. *
  19110. * @param {Quaternion} quaternion The quaternion to conjugate.
  19111. * @returns {Number} The magnitude.
  19112. */
  19113. Quaternion.magnitude = function(quaternion) {
  19114. return Math.sqrt(Quaternion.magnitudeSquared(quaternion));
  19115. };
  19116. /**
  19117. * Computes the normalized form of the provided quaternion.
  19118. *
  19119. * @param {Quaternion} quaternion The quaternion to normalize.
  19120. * @param {Quaternion} result The object onto which to store the result.
  19121. * @returns {Quaternion} The modified result parameter.
  19122. */
  19123. Quaternion.normalize = function(quaternion, result) {
  19124. if (!defined(result)) {
  19125. throw new DeveloperError('result is required');
  19126. }
  19127. var inverseMagnitude = 1.0 / Quaternion.magnitude(quaternion);
  19128. var x = quaternion.x * inverseMagnitude;
  19129. var y = quaternion.y * inverseMagnitude;
  19130. var z = quaternion.z * inverseMagnitude;
  19131. var w = quaternion.w * inverseMagnitude;
  19132. result.x = x;
  19133. result.y = y;
  19134. result.z = z;
  19135. result.w = w;
  19136. return result;
  19137. };
  19138. /**
  19139. * Computes the inverse of the provided quaternion.
  19140. *
  19141. * @param {Quaternion} quaternion The quaternion to normalize.
  19142. * @param {Quaternion} result The object onto which to store the result.
  19143. * @returns {Quaternion} The modified result parameter.
  19144. */
  19145. Quaternion.inverse = function(quaternion, result) {
  19146. if (!defined(result)) {
  19147. throw new DeveloperError('result is required');
  19148. }
  19149. var magnitudeSquared = Quaternion.magnitudeSquared(quaternion);
  19150. result = Quaternion.conjugate(quaternion, result);
  19151. return Quaternion.multiplyByScalar(result, 1.0 / magnitudeSquared, result);
  19152. };
  19153. /**
  19154. * Computes the componentwise sum of two quaternions.
  19155. *
  19156. * @param {Quaternion} left The first quaternion.
  19157. * @param {Quaternion} right The second quaternion.
  19158. * @param {Quaternion} result The object onto which to store the result.
  19159. * @returns {Quaternion} The modified result parameter.
  19160. */
  19161. Quaternion.add = function(left, right, result) {
  19162. if (!defined(left)) {
  19163. throw new DeveloperError('left is required');
  19164. }
  19165. if (!defined(right)) {
  19166. throw new DeveloperError('right is required');
  19167. }
  19168. if (!defined(result)) {
  19169. throw new DeveloperError('result is required');
  19170. }
  19171. result.x = left.x + right.x;
  19172. result.y = left.y + right.y;
  19173. result.z = left.z + right.z;
  19174. result.w = left.w + right.w;
  19175. return result;
  19176. };
  19177. /**
  19178. * Computes the componentwise difference of two quaternions.
  19179. *
  19180. * @param {Quaternion} left The first quaternion.
  19181. * @param {Quaternion} right The second quaternion.
  19182. * @param {Quaternion} result The object onto which to store the result.
  19183. * @returns {Quaternion} The modified result parameter.
  19184. */
  19185. Quaternion.subtract = function(left, right, result) {
  19186. if (!defined(left)) {
  19187. throw new DeveloperError('left is required');
  19188. }
  19189. if (!defined(right)) {
  19190. throw new DeveloperError('right is required');
  19191. }
  19192. if (!defined(result)) {
  19193. throw new DeveloperError('result is required');
  19194. }
  19195. result.x = left.x - right.x;
  19196. result.y = left.y - right.y;
  19197. result.z = left.z - right.z;
  19198. result.w = left.w - right.w;
  19199. return result;
  19200. };
  19201. /**
  19202. * Negates the provided quaternion.
  19203. *
  19204. * @param {Quaternion} quaternion The quaternion to be negated.
  19205. * @param {Quaternion} result The object onto which to store the result.
  19206. * @returns {Quaternion} The modified result parameter.
  19207. */
  19208. Quaternion.negate = function(quaternion, result) {
  19209. if (!defined(quaternion)) {
  19210. throw new DeveloperError('quaternion is required');
  19211. }
  19212. if (!defined(result)) {
  19213. throw new DeveloperError('result is required');
  19214. }
  19215. result.x = -quaternion.x;
  19216. result.y = -quaternion.y;
  19217. result.z = -quaternion.z;
  19218. result.w = -quaternion.w;
  19219. return result;
  19220. };
  19221. /**
  19222. * Computes the dot (scalar) product of two quaternions.
  19223. *
  19224. * @param {Quaternion} left The first quaternion.
  19225. * @param {Quaternion} right The second quaternion.
  19226. * @returns {Number} The dot product.
  19227. */
  19228. Quaternion.dot = function(left, right) {
  19229. if (!defined(left)) {
  19230. throw new DeveloperError('left is required');
  19231. }
  19232. if (!defined(right)) {
  19233. throw new DeveloperError('right is required');
  19234. }
  19235. return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
  19236. };
  19237. /**
  19238. * Computes the product of two quaternions.
  19239. *
  19240. * @param {Quaternion} left The first quaternion.
  19241. * @param {Quaternion} right The second quaternion.
  19242. * @param {Quaternion} result The object onto which to store the result.
  19243. * @returns {Quaternion} The modified result parameter.
  19244. */
  19245. Quaternion.multiply = function(left, right, result) {
  19246. if (!defined(left)) {
  19247. throw new DeveloperError('left is required');
  19248. }
  19249. if (!defined(right)) {
  19250. throw new DeveloperError('right is required');
  19251. }
  19252. if (!defined(result)) {
  19253. throw new DeveloperError('result is required');
  19254. }
  19255. var leftX = left.x;
  19256. var leftY = left.y;
  19257. var leftZ = left.z;
  19258. var leftW = left.w;
  19259. var rightX = right.x;
  19260. var rightY = right.y;
  19261. var rightZ = right.z;
  19262. var rightW = right.w;
  19263. var x = leftW * rightX + leftX * rightW + leftY * rightZ - leftZ * rightY;
  19264. var y = leftW * rightY - leftX * rightZ + leftY * rightW + leftZ * rightX;
  19265. var z = leftW * rightZ + leftX * rightY - leftY * rightX + leftZ * rightW;
  19266. var w = leftW * rightW - leftX * rightX - leftY * rightY - leftZ * rightZ;
  19267. result.x = x;
  19268. result.y = y;
  19269. result.z = z;
  19270. result.w = w;
  19271. return result;
  19272. };
  19273. /**
  19274. * Multiplies the provided quaternion componentwise by the provided scalar.
  19275. *
  19276. * @param {Quaternion} quaternion The quaternion to be scaled.
  19277. * @param {Number} scalar The scalar to multiply with.
  19278. * @param {Quaternion} result The object onto which to store the result.
  19279. * @returns {Quaternion} The modified result parameter.
  19280. */
  19281. Quaternion.multiplyByScalar = function(quaternion, scalar, result) {
  19282. if (!defined(quaternion)) {
  19283. throw new DeveloperError('quaternion is required');
  19284. }
  19285. if (typeof scalar !== 'number') {
  19286. throw new DeveloperError('scalar is required and must be a number.');
  19287. }
  19288. if (!defined(result)) {
  19289. throw new DeveloperError('result is required');
  19290. }
  19291. result.x = quaternion.x * scalar;
  19292. result.y = quaternion.y * scalar;
  19293. result.z = quaternion.z * scalar;
  19294. result.w = quaternion.w * scalar;
  19295. return result;
  19296. };
  19297. /**
  19298. * Divides the provided quaternion componentwise by the provided scalar.
  19299. *
  19300. * @param {Quaternion} quaternion The quaternion to be divided.
  19301. * @param {Number} scalar The scalar to divide by.
  19302. * @param {Quaternion} result The object onto which to store the result.
  19303. * @returns {Quaternion} The modified result parameter.
  19304. */
  19305. Quaternion.divideByScalar = function(quaternion, scalar, result) {
  19306. if (!defined(quaternion)) {
  19307. throw new DeveloperError('quaternion is required');
  19308. }
  19309. if (typeof scalar !== 'number') {
  19310. throw new DeveloperError('scalar is required and must be a number.');
  19311. }
  19312. if (!defined(result)) {
  19313. throw new DeveloperError('result is required');
  19314. }
  19315. result.x = quaternion.x / scalar;
  19316. result.y = quaternion.y / scalar;
  19317. result.z = quaternion.z / scalar;
  19318. result.w = quaternion.w / scalar;
  19319. return result;
  19320. };
  19321. /**
  19322. * Computes the axis of rotation of the provided quaternion.
  19323. *
  19324. * @param {Quaternion} quaternion The quaternion to use.
  19325. * @param {Cartesian3} result The object onto which to store the result.
  19326. * @returns {Cartesian3} The modified result parameter.
  19327. */
  19328. Quaternion.computeAxis = function(quaternion, result) {
  19329. if (!defined(quaternion)) {
  19330. throw new DeveloperError('quaternion is required');
  19331. }
  19332. if (!defined(result)) {
  19333. throw new DeveloperError('result is required');
  19334. }
  19335. var w = quaternion.w;
  19336. if (Math.abs(w - 1.0) < CesiumMath.EPSILON6) {
  19337. result.x = result.y = result.z = 0;
  19338. return result;
  19339. }
  19340. var scalar = 1.0 / Math.sqrt(1.0 - (w * w));
  19341. result.x = quaternion.x * scalar;
  19342. result.y = quaternion.y * scalar;
  19343. result.z = quaternion.z * scalar;
  19344. return result;
  19345. };
  19346. /**
  19347. * Computes the angle of rotation of the provided quaternion.
  19348. *
  19349. * @param {Quaternion} quaternion The quaternion to use.
  19350. * @returns {Number} The angle of rotation.
  19351. */
  19352. Quaternion.computeAngle = function(quaternion) {
  19353. if (!defined(quaternion)) {
  19354. throw new DeveloperError('quaternion is required');
  19355. }
  19356. if (Math.abs(quaternion.w - 1.0) < CesiumMath.EPSILON6) {
  19357. return 0.0;
  19358. }
  19359. return 2.0 * Math.acos(quaternion.w);
  19360. };
  19361. var lerpScratch = new Quaternion();
  19362. /**
  19363. * Computes the linear interpolation or extrapolation at t using the provided quaternions.
  19364. *
  19365. * @param {Quaternion} start The value corresponding to t at 0.0.
  19366. * @param {Quaternion} end The value corresponding to t at 1.0.
  19367. * @param {Number} t The point along t at which to interpolate.
  19368. * @param {Quaternion} result The object onto which to store the result.
  19369. * @returns {Quaternion} The modified result parameter.
  19370. */
  19371. Quaternion.lerp = function(start, end, t, result) {
  19372. if (!defined(start)) {
  19373. throw new DeveloperError('start is required.');
  19374. }
  19375. if (!defined(end)) {
  19376. throw new DeveloperError('end is required.');
  19377. }
  19378. if (typeof t !== 'number') {
  19379. throw new DeveloperError('t is required and must be a number.');
  19380. }
  19381. if (!defined(result)) {
  19382. throw new DeveloperError('result is required');
  19383. }
  19384. lerpScratch = Quaternion.multiplyByScalar(end, t, lerpScratch);
  19385. result = Quaternion.multiplyByScalar(start, 1.0 - t, result);
  19386. return Quaternion.add(lerpScratch, result, result);
  19387. };
  19388. var slerpEndNegated = new Quaternion();
  19389. var slerpScaledP = new Quaternion();
  19390. var slerpScaledR = new Quaternion();
  19391. /**
  19392. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  19393. *
  19394. * @param {Quaternion} start The value corresponding to t at 0.0.
  19395. * @param {Quaternion} end The value corresponding to t at 1.0.
  19396. * @param {Number} t The point along t at which to interpolate.
  19397. * @param {Quaternion} result The object onto which to store the result.
  19398. * @returns {Quaternion} The modified result parameter.
  19399. *
  19400. * @see Quaternion#fastSlerp
  19401. */
  19402. Quaternion.slerp = function(start, end, t, result) {
  19403. if (!defined(start)) {
  19404. throw new DeveloperError('start is required.');
  19405. }
  19406. if (!defined(end)) {
  19407. throw new DeveloperError('end is required.');
  19408. }
  19409. if (typeof t !== 'number') {
  19410. throw new DeveloperError('t is required and must be a number.');
  19411. }
  19412. if (!defined(result)) {
  19413. throw new DeveloperError('result is required');
  19414. }
  19415. var dot = Quaternion.dot(start, end);
  19416. // The angle between start must be acute. Since q and -q represent
  19417. // the same rotation, negate q to get the acute angle.
  19418. var r = end;
  19419. if (dot < 0.0) {
  19420. dot = -dot;
  19421. r = slerpEndNegated = Quaternion.negate(end, slerpEndNegated);
  19422. }
  19423. // dot > 0, as the dot product approaches 1, the angle between the
  19424. // quaternions vanishes. use linear interpolation.
  19425. if (1.0 - dot < CesiumMath.EPSILON6) {
  19426. return Quaternion.lerp(start, r, t, result);
  19427. }
  19428. var theta = Math.acos(dot);
  19429. slerpScaledP = Quaternion.multiplyByScalar(start, Math.sin((1 - t) * theta), slerpScaledP);
  19430. slerpScaledR = Quaternion.multiplyByScalar(r, Math.sin(t * theta), slerpScaledR);
  19431. result = Quaternion.add(slerpScaledP, slerpScaledR, result);
  19432. return Quaternion.multiplyByScalar(result, 1.0 / Math.sin(theta), result);
  19433. };
  19434. /**
  19435. * The logarithmic quaternion function.
  19436. *
  19437. * @param {Quaternion} quaternion The unit quaternion.
  19438. * @param {Cartesian3} result The object onto which to store the result.
  19439. * @returns {Cartesian3} The modified result parameter.
  19440. */
  19441. Quaternion.log = function(quaternion, result) {
  19442. if (!defined(quaternion)) {
  19443. throw new DeveloperError('quaternion is required.');
  19444. }
  19445. if (!defined(result)) {
  19446. throw new DeveloperError('result is required');
  19447. }
  19448. var theta = CesiumMath.acosClamped(quaternion.w);
  19449. var thetaOverSinTheta = 0.0;
  19450. if (theta !== 0.0) {
  19451. thetaOverSinTheta = theta / Math.sin(theta);
  19452. }
  19453. return Cartesian3.multiplyByScalar(quaternion, thetaOverSinTheta, result);
  19454. };
  19455. /**
  19456. * The exponential quaternion function.
  19457. *
  19458. * @param {Cartesian3} cartesian The cartesian.
  19459. * @param {Quaternion} result The object onto which to store the result.
  19460. * @returns {Quaternion} The modified result parameter.
  19461. */
  19462. Quaternion.exp = function(cartesian, result) {
  19463. if (!defined(cartesian)) {
  19464. throw new DeveloperError('cartesian is required.');
  19465. }
  19466. if (!defined(result)) {
  19467. throw new DeveloperError('result is required');
  19468. }
  19469. var theta = Cartesian3.magnitude(cartesian);
  19470. var sinThetaOverTheta = 0.0;
  19471. if (theta !== 0.0) {
  19472. sinThetaOverTheta = Math.sin(theta) / theta;
  19473. }
  19474. result.x = cartesian.x * sinThetaOverTheta;
  19475. result.y = cartesian.y * sinThetaOverTheta;
  19476. result.z = cartesian.z * sinThetaOverTheta;
  19477. result.w = Math.cos(theta);
  19478. return result;
  19479. };
  19480. var squadScratchCartesian0 = new Cartesian3();
  19481. var squadScratchCartesian1 = new Cartesian3();
  19482. var squadScratchQuaternion0 = new Quaternion();
  19483. var squadScratchQuaternion1 = new Quaternion();
  19484. /**
  19485. * Computes an inner quadrangle point.
  19486. * <p>This will compute quaternions that ensure a squad curve is C<sup>1</sup>.</p>
  19487. *
  19488. * @param {Quaternion} q0 The first quaternion.
  19489. * @param {Quaternion} q1 The second quaternion.
  19490. * @param {Quaternion} q2 The third quaternion.
  19491. * @param {Quaternion} result The object onto which to store the result.
  19492. * @returns {Quaternion} The modified result parameter.
  19493. *
  19494. * @see Quaternion#squad
  19495. */
  19496. Quaternion.computeInnerQuadrangle = function(q0, q1, q2, result) {
  19497. if (!defined(q0) || !defined(q1) || !defined(q2)) {
  19498. throw new DeveloperError('q0, q1, and q2 are required.');
  19499. }
  19500. if (!defined(result)) {
  19501. throw new DeveloperError('result is required');
  19502. }
  19503. var qInv = Quaternion.conjugate(q1, squadScratchQuaternion0);
  19504. Quaternion.multiply(qInv, q2, squadScratchQuaternion1);
  19505. var cart0 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian0);
  19506. Quaternion.multiply(qInv, q0, squadScratchQuaternion1);
  19507. var cart1 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian1);
  19508. Cartesian3.add(cart0, cart1, cart0);
  19509. Cartesian3.multiplyByScalar(cart0, 0.25, cart0);
  19510. Cartesian3.negate(cart0, cart0);
  19511. Quaternion.exp(cart0, squadScratchQuaternion0);
  19512. return Quaternion.multiply(q1, squadScratchQuaternion0, result);
  19513. };
  19514. /**
  19515. * Computes the spherical quadrangle interpolation between quaternions.
  19516. *
  19517. * @param {Quaternion} q0 The first quaternion.
  19518. * @param {Quaternion} q1 The second quaternion.
  19519. * @param {Quaternion} s0 The first inner quadrangle.
  19520. * @param {Quaternion} s1 The second inner quadrangle.
  19521. * @param {Number} t The time in [0,1] used to interpolate.
  19522. * @param {Quaternion} result The object onto which to store the result.
  19523. * @returns {Quaternion} The modified result parameter.
  19524. *
  19525. *
  19526. * @example
  19527. * // 1. compute the squad interpolation between two quaternions on a curve
  19528. * var s0 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i - 1], quaternions[i], quaternions[i + 1], new Cesium.Quaternion());
  19529. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i], quaternions[i + 1], quaternions[i + 2], new Cesium.Quaternion());
  19530. * var q = Cesium.Quaternion.squad(quaternions[i], quaternions[i + 1], s0, s1, t, new Cesium.Quaternion());
  19531. *
  19532. * // 2. compute the squad interpolation as above but where the first quaternion is a end point.
  19533. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[0], quaternions[1], quaternions[2], new Cesium.Quaternion());
  19534. * var q = Cesium.Quaternion.squad(quaternions[0], quaternions[1], quaternions[0], s1, t, new Cesium.Quaternion());
  19535. *
  19536. * @see Quaternion#computeInnerQuadrangle
  19537. */
  19538. Quaternion.squad = function(q0, q1, s0, s1, t, result) {
  19539. if (!defined(q0) || !defined(q1) || !defined(s0) || !defined(s1)) {
  19540. throw new DeveloperError('q0, q1, s0, and s1 are required.');
  19541. }
  19542. if (typeof t !== 'number') {
  19543. throw new DeveloperError('t is required and must be a number.');
  19544. }
  19545. if (!defined(result)) {
  19546. throw new DeveloperError('result is required');
  19547. }
  19548. var slerp0 = Quaternion.slerp(q0, q1, t, squadScratchQuaternion0);
  19549. var slerp1 = Quaternion.slerp(s0, s1, t, squadScratchQuaternion1);
  19550. return Quaternion.slerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  19551. };
  19552. var fastSlerpScratchQuaternion = new Quaternion();
  19553. var opmu = 1.90110745351730037;
  19554. var u = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19555. var v = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19556. var bT = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19557. var bD = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19558. for (var i = 0; i < 7; ++i) {
  19559. var s = i + 1.0;
  19560. var t = 2.0 * s + 1.0;
  19561. u[i] = 1.0 / (s * t);
  19562. v[i] = s / t;
  19563. }
  19564. u[7] = opmu / (8.0 * 17.0);
  19565. v[7] = opmu * 8.0 / 17.0;
  19566. /**
  19567. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  19568. * This implementation is faster than {@link Quaternion#slerp}, but is only accurate up to 10<sup>-6</sup>.
  19569. *
  19570. * @param {Quaternion} start The value corresponding to t at 0.0.
  19571. * @param {Quaternion} end The value corresponding to t at 1.0.
  19572. * @param {Number} t The point along t at which to interpolate.
  19573. * @param {Quaternion} result The object onto which to store the result.
  19574. * @returns {Quaternion} The modified result parameter.
  19575. *
  19576. * @see Quaternion#slerp
  19577. */
  19578. Quaternion.fastSlerp = function(start, end, t, result) {
  19579. if (!defined(start)) {
  19580. throw new DeveloperError('start is required.');
  19581. }
  19582. if (!defined(end)) {
  19583. throw new DeveloperError('end is required.');
  19584. }
  19585. if (typeof t !== 'number') {
  19586. throw new DeveloperError('t is required and must be a number.');
  19587. }
  19588. if (!defined(result)) {
  19589. throw new DeveloperError('result is required');
  19590. }
  19591. var x = Quaternion.dot(start, end);
  19592. var sign;
  19593. if (x >= 0) {
  19594. sign = 1.0;
  19595. } else {
  19596. sign = -1.0;
  19597. x = -x;
  19598. }
  19599. var xm1 = x - 1.0;
  19600. var d = 1.0 - t;
  19601. var sqrT = t * t;
  19602. var sqrD = d * d;
  19603. for (var i = 7; i >= 0; --i) {
  19604. bT[i] = (u[i] * sqrT - v[i]) * xm1;
  19605. bD[i] = (u[i] * sqrD - v[i]) * xm1;
  19606. }
  19607. var cT = sign * t * (
  19608. 1.0 + bT[0] * (1.0 + bT[1] * (1.0 + bT[2] * (1.0 + bT[3] * (
  19609. 1.0 + bT[4] * (1.0 + bT[5] * (1.0 + bT[6] * (1.0 + bT[7]))))))));
  19610. var cD = d * (
  19611. 1.0 + bD[0] * (1.0 + bD[1] * (1.0 + bD[2] * (1.0 + bD[3] * (
  19612. 1.0 + bD[4] * (1.0 + bD[5] * (1.0 + bD[6] * (1.0 + bD[7]))))))));
  19613. var temp = Quaternion.multiplyByScalar(start, cD, fastSlerpScratchQuaternion);
  19614. Quaternion.multiplyByScalar(end, cT, result);
  19615. return Quaternion.add(temp, result, result);
  19616. };
  19617. /**
  19618. * Computes the spherical quadrangle interpolation between quaternions.
  19619. * An implementation that is faster than {@link Quaternion#squad}, but less accurate.
  19620. *
  19621. * @param {Quaternion} q0 The first quaternion.
  19622. * @param {Quaternion} q1 The second quaternion.
  19623. * @param {Quaternion} s0 The first inner quadrangle.
  19624. * @param {Quaternion} s1 The second inner quadrangle.
  19625. * @param {Number} t The time in [0,1] used to interpolate.
  19626. * @param {Quaternion} result The object onto which to store the result.
  19627. * @returns {Quaternion} The modified result parameter or a new instance if none was provided.
  19628. *
  19629. * @see Quaternion#squad
  19630. */
  19631. Quaternion.fastSquad = function(q0, q1, s0, s1, t, result) {
  19632. if (!defined(q0) || !defined(q1) || !defined(s0) || !defined(s1)) {
  19633. throw new DeveloperError('q0, q1, s0, and s1 are required.');
  19634. }
  19635. if (typeof t !== 'number') {
  19636. throw new DeveloperError('t is required and must be a number.');
  19637. }
  19638. if (!defined(result)) {
  19639. throw new DeveloperError('result is required');
  19640. }
  19641. var slerp0 = Quaternion.fastSlerp(q0, q1, t, squadScratchQuaternion0);
  19642. var slerp1 = Quaternion.fastSlerp(s0, s1, t, squadScratchQuaternion1);
  19643. return Quaternion.fastSlerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  19644. };
  19645. /**
  19646. * Compares the provided quaternions componentwise and returns
  19647. * <code>true</code> if they are equal, <code>false</code> otherwise.
  19648. *
  19649. * @param {Quaternion} [left] The first quaternion.
  19650. * @param {Quaternion} [right] The second quaternion.
  19651. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  19652. */
  19653. Quaternion.equals = function(left, right) {
  19654. return (left === right) ||
  19655. ((defined(left)) &&
  19656. (defined(right)) &&
  19657. (left.x === right.x) &&
  19658. (left.y === right.y) &&
  19659. (left.z === right.z) &&
  19660. (left.w === right.w));
  19661. };
  19662. /**
  19663. * Compares the provided quaternions componentwise and returns
  19664. * <code>true</code> if they are within the provided epsilon,
  19665. * <code>false</code> otherwise.
  19666. *
  19667. * @param {Quaternion} [left] The first quaternion.
  19668. * @param {Quaternion} [right] The second quaternion.
  19669. * @param {Number} epsilon The epsilon to use for equality testing.
  19670. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  19671. */
  19672. Quaternion.equalsEpsilon = function(left, right, epsilon) {
  19673. if (typeof epsilon !== 'number') {
  19674. throw new DeveloperError('epsilon is required and must be a number.');
  19675. }
  19676. return (left === right) ||
  19677. ((defined(left)) &&
  19678. (defined(right)) &&
  19679. (Math.abs(left.x - right.x) <= epsilon) &&
  19680. (Math.abs(left.y - right.y) <= epsilon) &&
  19681. (Math.abs(left.z - right.z) <= epsilon) &&
  19682. (Math.abs(left.w - right.w) <= epsilon));
  19683. };
  19684. /**
  19685. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 0.0).
  19686. *
  19687. * @type {Quaternion}
  19688. * @constant
  19689. */
  19690. Quaternion.ZERO = freezeObject(new Quaternion(0.0, 0.0, 0.0, 0.0));
  19691. /**
  19692. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 1.0).
  19693. *
  19694. * @type {Quaternion}
  19695. * @constant
  19696. */
  19697. Quaternion.IDENTITY = freezeObject(new Quaternion(0.0, 0.0, 0.0, 1.0));
  19698. /**
  19699. * Duplicates this Quaternion instance.
  19700. *
  19701. * @param {Quaternion} [result] The object onto which to store the result.
  19702. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  19703. */
  19704. Quaternion.prototype.clone = function(result) {
  19705. return Quaternion.clone(this, result);
  19706. };
  19707. /**
  19708. * Compares this and the provided quaternion componentwise and returns
  19709. * <code>true</code> if they are equal, <code>false</code> otherwise.
  19710. *
  19711. * @param {Quaternion} [right] The right hand side quaternion.
  19712. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  19713. */
  19714. Quaternion.prototype.equals = function(right) {
  19715. return Quaternion.equals(this, right);
  19716. };
  19717. /**
  19718. * Compares this and the provided quaternion componentwise and returns
  19719. * <code>true</code> if they are within the provided epsilon,
  19720. * <code>false</code> otherwise.
  19721. *
  19722. * @param {Quaternion} [right] The right hand side quaternion.
  19723. * @param {Number} epsilon The epsilon to use for equality testing.
  19724. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  19725. */
  19726. Quaternion.prototype.equalsEpsilon = function(right, epsilon) {
  19727. return Quaternion.equalsEpsilon(this, right, epsilon);
  19728. };
  19729. /**
  19730. * Returns a string representing this quaternion in the format (x, y, z, w).
  19731. *
  19732. * @returns {String} A string representing this Quaternion.
  19733. */
  19734. Quaternion.prototype.toString = function() {
  19735. return '(' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
  19736. };
  19737. return Quaternion;
  19738. });
  19739. /*global define*/
  19740. define('Core/RectangleGeometryLibrary',[
  19741. './Cartesian3',
  19742. './Cartographic',
  19743. './defined',
  19744. './DeveloperError',
  19745. './GeographicProjection',
  19746. './Math',
  19747. './Matrix2',
  19748. './Rectangle'
  19749. ], function(
  19750. Cartesian3,
  19751. Cartographic,
  19752. defined,
  19753. DeveloperError,
  19754. GeographicProjection,
  19755. CesiumMath,
  19756. Matrix2,
  19757. Rectangle) {
  19758. 'use strict';
  19759. var cos = Math.cos;
  19760. var sin = Math.sin;
  19761. var sqrt = Math.sqrt;
  19762. /**
  19763. * @private
  19764. */
  19765. var RectangleGeometryLibrary = {};
  19766. /**
  19767. * @private
  19768. */
  19769. RectangleGeometryLibrary.computePosition = function(options, row, col, position, st) {
  19770. var radiiSquared = options.ellipsoid.radiiSquared;
  19771. var nwCorner = options.nwCorner;
  19772. var rectangle = options.rectangle;
  19773. var stLatitude = nwCorner.latitude - options.granYCos * row + col * options.granXSin;
  19774. var cosLatitude = cos(stLatitude);
  19775. var nZ = sin(stLatitude);
  19776. var kZ = radiiSquared.z * nZ;
  19777. var stLongitude = nwCorner.longitude + row * options.granYSin + col * options.granXCos;
  19778. var nX = cosLatitude * cos(stLongitude);
  19779. var nY = cosLatitude * sin(stLongitude);
  19780. var kX = radiiSquared.x * nX;
  19781. var kY = radiiSquared.y * nY;
  19782. var gamma = sqrt((kX * nX) + (kY * nY) + (kZ * nZ));
  19783. position.x = kX / gamma;
  19784. position.y = kY / gamma;
  19785. position.z = kZ / gamma;
  19786. if (defined(options.vertexFormat) && options.vertexFormat.st) {
  19787. var stNwCorner = options.stNwCorner;
  19788. if (defined(stNwCorner)) {
  19789. stLatitude = stNwCorner.latitude - options.stGranYCos * row + col * options.stGranXSin;
  19790. stLongitude = stNwCorner.longitude + row * options.stGranYSin + col * options.stGranXCos;
  19791. st.x = (stLongitude - options.stWest) * options.lonScalar;
  19792. st.y = (stLatitude - options.stSouth) * options.latScalar;
  19793. } else {
  19794. st.x = (stLongitude - rectangle.west) * options.lonScalar;
  19795. st.y = (stLatitude - rectangle.south) * options.latScalar;
  19796. }
  19797. }
  19798. };
  19799. var rotationMatrixScratch = new Matrix2();
  19800. var nwCartesian = new Cartesian3();
  19801. var centerScratch = new Cartographic();
  19802. var centerCartesian = new Cartesian3();
  19803. var proj = new GeographicProjection();
  19804. function getRotationOptions(nwCorner, rotation, granularityX, granularityY, center, width, height) {
  19805. var cosRotation = Math.cos(rotation);
  19806. var granYCos = granularityY * cosRotation;
  19807. var granXCos = granularityX * cosRotation;
  19808. var sinRotation = Math.sin(rotation);
  19809. var granYSin = granularityY * sinRotation;
  19810. var granXSin = granularityX * sinRotation;
  19811. nwCartesian = proj.project(nwCorner, nwCartesian);
  19812. nwCartesian = Cartesian3.subtract(nwCartesian, centerCartesian, nwCartesian);
  19813. var rotationMatrix = Matrix2.fromRotation(rotation, rotationMatrixScratch);
  19814. nwCartesian = Matrix2.multiplyByVector(rotationMatrix, nwCartesian, nwCartesian);
  19815. nwCartesian = Cartesian3.add(nwCartesian, centerCartesian, nwCartesian);
  19816. nwCorner = proj.unproject(nwCartesian, nwCorner);
  19817. width -= 1;
  19818. height -= 1;
  19819. var latitude = nwCorner.latitude;
  19820. var latitude0 = latitude + width * granXSin;
  19821. var latitude1 = latitude - granYCos * height;
  19822. var latitude2 = latitude - granYCos * height + width * granXSin;
  19823. var north = Math.max(latitude, latitude0, latitude1, latitude2);
  19824. var south = Math.min(latitude, latitude0, latitude1, latitude2);
  19825. var longitude = nwCorner.longitude;
  19826. var longitude0 = longitude + width * granXCos;
  19827. var longitude1 = longitude + height * granYSin;
  19828. var longitude2 = longitude + height * granYSin + width * granXCos;
  19829. var east = Math.max(longitude, longitude0, longitude1, longitude2);
  19830. var west = Math.min(longitude, longitude0, longitude1, longitude2);
  19831. return {
  19832. north: north,
  19833. south: south,
  19834. east: east,
  19835. west: west,
  19836. granYCos : granYCos,
  19837. granYSin : granYSin,
  19838. granXCos : granXCos,
  19839. granXSin : granXSin,
  19840. nwCorner : nwCorner
  19841. };
  19842. }
  19843. /**
  19844. * @private
  19845. */
  19846. RectangleGeometryLibrary.computeOptions = function(geometry, rectangle, nwCorner, stNwCorner) {
  19847. var granularity = geometry._granularity;
  19848. var ellipsoid = geometry._ellipsoid;
  19849. var surfaceHeight = geometry._surfaceHeight;
  19850. var rotation = geometry._rotation;
  19851. var stRotation = geometry._stRotation;
  19852. var extrudedHeight = geometry._extrudedHeight;
  19853. var east = rectangle.east;
  19854. var west = rectangle.west;
  19855. var north = rectangle.north;
  19856. var south = rectangle.south;
  19857. var width;
  19858. var height;
  19859. var granularityX;
  19860. var granularityY;
  19861. var dx;
  19862. var dy = north - south;
  19863. if (west > east) {
  19864. dx = (CesiumMath.TWO_PI - west + east);
  19865. width = Math.ceil(dx / granularity) + 1;
  19866. height = Math.ceil(dy / granularity) + 1;
  19867. granularityX = dx / (width - 1);
  19868. granularityY = dy / (height - 1);
  19869. } else {
  19870. dx = east - west;
  19871. width = Math.ceil(dx / granularity) + 1;
  19872. height = Math.ceil(dy / granularity) + 1;
  19873. granularityX = dx / (width - 1);
  19874. granularityY = dy / (height - 1);
  19875. }
  19876. nwCorner = Rectangle.northwest(rectangle, nwCorner);
  19877. var center = Rectangle.center(rectangle, centerScratch);
  19878. if (rotation !== 0 || stRotation !== 0) {
  19879. if (center.longitude < nwCorner.longitude) {
  19880. center.longitude += CesiumMath.TWO_PI;
  19881. }
  19882. centerCartesian = proj.project(center, centerCartesian);
  19883. }
  19884. var granYCos = granularityY;
  19885. var granXCos = granularityX;
  19886. var granYSin = 0.0;
  19887. var granXSin = 0.0;
  19888. var options = {
  19889. granYCos : granYCos,
  19890. granYSin : granYSin,
  19891. granXCos : granXCos,
  19892. granXSin : granXSin,
  19893. ellipsoid : ellipsoid,
  19894. surfaceHeight : surfaceHeight,
  19895. extrudedHeight : extrudedHeight,
  19896. nwCorner : nwCorner,
  19897. rectangle : rectangle,
  19898. width: width,
  19899. height: height
  19900. };
  19901. if (rotation !== 0) {
  19902. var rotationOptions = getRotationOptions(nwCorner, rotation, granularityX, granularityY, center, width, height);
  19903. north = rotationOptions.north;
  19904. south = rotationOptions.south;
  19905. east = rotationOptions.east;
  19906. west = rotationOptions.west;
  19907. if (north < -CesiumMath.PI_OVER_TWO || north > CesiumMath.PI_OVER_TWO ||
  19908. south < -CesiumMath.PI_OVER_TWO || south > CesiumMath.PI_OVER_TWO) {
  19909. throw new DeveloperError('Rotated rectangle is invalid. It crosses over either the north or south pole.');
  19910. }
  19911. options.granYCos = rotationOptions.granYCos;
  19912. options.granYSin = rotationOptions.granYSin;
  19913. options.granXCos = rotationOptions.granXCos;
  19914. options.granXSin = rotationOptions.granXSin;
  19915. rectangle.north = north;
  19916. rectangle.south = south;
  19917. rectangle.east = east;
  19918. rectangle.west = west;
  19919. }
  19920. if (stRotation !== 0) {
  19921. rotation = rotation - stRotation;
  19922. stNwCorner = Rectangle.northwest(rectangle, stNwCorner);
  19923. var stRotationOptions = getRotationOptions(stNwCorner, rotation, granularityX, granularityY, center, width, height);
  19924. options.stGranYCos = stRotationOptions.granYCos;
  19925. options.stGranXCos = stRotationOptions.granXCos;
  19926. options.stGranYSin = stRotationOptions.granYSin;
  19927. options.stGranXSin = stRotationOptions.granXSin;
  19928. options.stNwCorner = stNwCorner;
  19929. options.stWest = stRotationOptions.west;
  19930. options.stSouth = stRotationOptions.south;
  19931. }
  19932. return options;
  19933. };
  19934. return RectangleGeometryLibrary;
  19935. });
  19936. /*global define*/
  19937. define('Core/VertexFormat',[
  19938. './defaultValue',
  19939. './defined',
  19940. './DeveloperError',
  19941. './freezeObject'
  19942. ], function(
  19943. defaultValue,
  19944. defined,
  19945. DeveloperError,
  19946. freezeObject) {
  19947. 'use strict';
  19948. /**
  19949. * A vertex format defines what attributes make up a vertex. A VertexFormat can be provided
  19950. * to a {@link Geometry} to request that certain properties be computed, e.g., just position,
  19951. * position and normal, etc.
  19952. *
  19953. * @param {Object} [options] An object with boolean properties corresponding to VertexFormat properties as shown in the code example.
  19954. *
  19955. * @alias VertexFormat
  19956. * @constructor
  19957. *
  19958. * @example
  19959. * // Create a vertex format with position and 2D texture coordinate attributes.
  19960. * var format = new Cesium.VertexFormat({
  19961. * position : true,
  19962. * st : true
  19963. * });
  19964. *
  19965. * @see Geometry#attributes
  19966. * @see Packable
  19967. */
  19968. function VertexFormat(options) {
  19969. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  19970. /**
  19971. * When <code>true</code>, the vertex has a 3D position attribute.
  19972. * <p>
  19973. * 64-bit floating-point (for precision). 3 components per attribute.
  19974. * </p>
  19975. *
  19976. * @type Boolean
  19977. *
  19978. * @default false
  19979. */
  19980. this.position = defaultValue(options.position, false);
  19981. /**
  19982. * When <code>true</code>, the vertex has a normal attribute (normalized), which is commonly used for lighting.
  19983. * <p>
  19984. * 32-bit floating-point. 3 components per attribute.
  19985. * </p>
  19986. *
  19987. * @type Boolean
  19988. *
  19989. * @default false
  19990. */
  19991. this.normal = defaultValue(options.normal, false);
  19992. /**
  19993. * When <code>true</code>, the vertex has a 2D texture coordinate attribute.
  19994. * <p>
  19995. * 32-bit floating-point. 2 components per attribute
  19996. * </p>
  19997. *
  19998. * @type Boolean
  19999. *
  20000. * @default false
  20001. */
  20002. this.st = defaultValue(options.st, false);
  20003. /**
  20004. * When <code>true</code>, the vertex has a binormal attribute (normalized), which is used for tangent-space effects like bump mapping.
  20005. * <p>
  20006. * 32-bit floating-point. 3 components per attribute.
  20007. * </p>
  20008. *
  20009. * @type Boolean
  20010. *
  20011. * @default false
  20012. */
  20013. this.binormal = defaultValue(options.binormal, false);
  20014. /**
  20015. * When <code>true</code>, the vertex has a tangent attribute (normalized), which is used for tangent-space effects like bump mapping.
  20016. * <p>
  20017. * 32-bit floating-point. 3 components per attribute.
  20018. * </p>
  20019. *
  20020. * @type Boolean
  20021. *
  20022. * @default false
  20023. */
  20024. this.tangent = defaultValue(options.tangent, false);
  20025. /**
  20026. * When <code>true</code>, the vertex has an RGB color attribute.
  20027. * <p>
  20028. * 8-bit unsigned byte. 3 components per attribute.
  20029. * </p>
  20030. *
  20031. * @type Boolean
  20032. *
  20033. * @default false
  20034. */
  20035. this.color = defaultValue(options.color, false);
  20036. }
  20037. /**
  20038. * An immutable vertex format with only a position attribute.
  20039. *
  20040. * @type {VertexFormat}
  20041. * @constant
  20042. *
  20043. * @see VertexFormat#position
  20044. */
  20045. VertexFormat.POSITION_ONLY = freezeObject(new VertexFormat({
  20046. position : true
  20047. }));
  20048. /**
  20049. * An immutable vertex format with position and normal attributes.
  20050. * This is compatible with per-instance color appearances like {@link PerInstanceColorAppearance}.
  20051. *
  20052. * @type {VertexFormat}
  20053. * @constant
  20054. *
  20055. * @see VertexFormat#position
  20056. * @see VertexFormat#normal
  20057. */
  20058. VertexFormat.POSITION_AND_NORMAL = freezeObject(new VertexFormat({
  20059. position : true,
  20060. normal : true
  20061. }));
  20062. /**
  20063. * An immutable vertex format with position, normal, and st attributes.
  20064. * This is compatible with {@link MaterialAppearance} when {@link MaterialAppearance#materialSupport}
  20065. * is <code>TEXTURED/code>.
  20066. *
  20067. * @type {VertexFormat}
  20068. * @constant
  20069. *
  20070. * @see VertexFormat#position
  20071. * @see VertexFormat#normal
  20072. * @see VertexFormat#st
  20073. */
  20074. VertexFormat.POSITION_NORMAL_AND_ST = freezeObject(new VertexFormat({
  20075. position : true,
  20076. normal : true,
  20077. st : true
  20078. }));
  20079. /**
  20080. * An immutable vertex format with position and st attributes.
  20081. * This is compatible with {@link EllipsoidSurfaceAppearance}.
  20082. *
  20083. * @type {VertexFormat}
  20084. * @constant
  20085. *
  20086. * @see VertexFormat#position
  20087. * @see VertexFormat#st
  20088. */
  20089. VertexFormat.POSITION_AND_ST = freezeObject(new VertexFormat({
  20090. position : true,
  20091. st : true
  20092. }));
  20093. /**
  20094. * An immutable vertex format with position and color attributes.
  20095. *
  20096. * @type {VertexFormat}
  20097. * @constant
  20098. *
  20099. * @see VertexFormat#position
  20100. * @see VertexFormat#color
  20101. */
  20102. VertexFormat.POSITION_AND_COLOR = freezeObject(new VertexFormat({
  20103. position : true,
  20104. color : true
  20105. }));
  20106. /**
  20107. * An immutable vertex format with well-known attributes: position, normal, st, binormal, and tangent.
  20108. *
  20109. * @type {VertexFormat}
  20110. * @constant
  20111. *
  20112. * @see VertexFormat#position
  20113. * @see VertexFormat#normal
  20114. * @see VertexFormat#st
  20115. * @see VertexFormat#binormal
  20116. * @see VertexFormat#tangent
  20117. */
  20118. VertexFormat.ALL = freezeObject(new VertexFormat({
  20119. position : true,
  20120. normal : true,
  20121. st : true,
  20122. binormal : true,
  20123. tangent : true
  20124. }));
  20125. /**
  20126. * An immutable vertex format with position, normal, and st attributes.
  20127. * This is compatible with most appearances and materials; however
  20128. * normal and st attributes are not always required. When this is
  20129. * known in advance, another <code>VertexFormat</code> should be used.
  20130. *
  20131. * @type {VertexFormat}
  20132. * @constant
  20133. *
  20134. * @see VertexFormat#position
  20135. * @see VertexFormat#normal
  20136. */
  20137. VertexFormat.DEFAULT = VertexFormat.POSITION_NORMAL_AND_ST;
  20138. /**
  20139. * The number of elements used to pack the object into an array.
  20140. * @type {Number}
  20141. */
  20142. VertexFormat.packedLength = 6;
  20143. /**
  20144. * Stores the provided instance into the provided array.
  20145. *
  20146. * @param {VertexFormat} value The value to pack.
  20147. * @param {Number[]} array The array to pack into.
  20148. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  20149. *
  20150. * @returns {Number[]} The array that was packed into
  20151. */
  20152. VertexFormat.pack = function(value, array, startingIndex) {
  20153. if (!defined(value)) {
  20154. throw new DeveloperError('value is required');
  20155. }
  20156. if (!defined(array)) {
  20157. throw new DeveloperError('array is required');
  20158. }
  20159. startingIndex = defaultValue(startingIndex, 0);
  20160. array[startingIndex++] = value.position ? 1.0 : 0.0;
  20161. array[startingIndex++] = value.normal ? 1.0 : 0.0;
  20162. array[startingIndex++] = value.st ? 1.0 : 0.0;
  20163. array[startingIndex++] = value.binormal ? 1.0 : 0.0;
  20164. array[startingIndex++] = value.tangent ? 1.0 : 0.0;
  20165. array[startingIndex++] = value.color ? 1.0 : 0.0;
  20166. return array;
  20167. };
  20168. /**
  20169. * Retrieves an instance from a packed array.
  20170. *
  20171. * @param {Number[]} array The packed array.
  20172. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  20173. * @param {VertexFormat} [result] The object into which to store the result.
  20174. * @returns {VertexFormat} The modified result parameter or a new VertexFormat instance if one was not provided.
  20175. */
  20176. VertexFormat.unpack = function(array, startingIndex, result) {
  20177. if (!defined(array)) {
  20178. throw new DeveloperError('array is required');
  20179. }
  20180. startingIndex = defaultValue(startingIndex, 0);
  20181. if (!defined(result)) {
  20182. result = new VertexFormat();
  20183. }
  20184. result.position = array[startingIndex++] === 1.0;
  20185. result.normal = array[startingIndex++] === 1.0;
  20186. result.st = array[startingIndex++] === 1.0;
  20187. result.binormal = array[startingIndex++] === 1.0;
  20188. result.tangent = array[startingIndex++] === 1.0;
  20189. result.color = array[startingIndex++] === 1.0;
  20190. return result;
  20191. };
  20192. /**
  20193. * Duplicates a VertexFormat instance.
  20194. *
  20195. * @param {VertexFormat} cartesian The vertex format to duplicate.
  20196. * @param {VertexFormat} [result] The object onto which to store the result.
  20197. * @returns {VertexFormat} The modified result parameter or a new VertexFormat instance if one was not provided. (Returns undefined if vertexFormat is undefined)
  20198. */
  20199. VertexFormat.clone = function(vertexFormat, result) {
  20200. if (!defined(vertexFormat)) {
  20201. return undefined;
  20202. }
  20203. if (!defined(result)) {
  20204. result = new VertexFormat();
  20205. }
  20206. result.position = vertexFormat.position;
  20207. result.normal = vertexFormat.normal;
  20208. result.st = vertexFormat.st;
  20209. result.binormal = vertexFormat.binormal;
  20210. result.tangent = vertexFormat.tangent;
  20211. result.color = vertexFormat.color;
  20212. return result;
  20213. };
  20214. return VertexFormat;
  20215. });
  20216. /*global define*/
  20217. define('Core/RectangleGeometry',[
  20218. './BoundingSphere',
  20219. './Cartesian2',
  20220. './Cartesian3',
  20221. './Cartographic',
  20222. './ComponentDatatype',
  20223. './defaultValue',
  20224. './defined',
  20225. './defineProperties',
  20226. './DeveloperError',
  20227. './Ellipsoid',
  20228. './Geometry',
  20229. './GeometryAttribute',
  20230. './GeometryAttributes',
  20231. './GeometryInstance',
  20232. './GeometryPipeline',
  20233. './IndexDatatype',
  20234. './Math',
  20235. './Matrix2',
  20236. './Matrix3',
  20237. './PolygonPipeline',
  20238. './PrimitiveType',
  20239. './Quaternion',
  20240. './Rectangle',
  20241. './RectangleGeometryLibrary',
  20242. './VertexFormat'
  20243. ], function(
  20244. BoundingSphere,
  20245. Cartesian2,
  20246. Cartesian3,
  20247. Cartographic,
  20248. ComponentDatatype,
  20249. defaultValue,
  20250. defined,
  20251. defineProperties,
  20252. DeveloperError,
  20253. Ellipsoid,
  20254. Geometry,
  20255. GeometryAttribute,
  20256. GeometryAttributes,
  20257. GeometryInstance,
  20258. GeometryPipeline,
  20259. IndexDatatype,
  20260. CesiumMath,
  20261. Matrix2,
  20262. Matrix3,
  20263. PolygonPipeline,
  20264. PrimitiveType,
  20265. Quaternion,
  20266. Rectangle,
  20267. RectangleGeometryLibrary,
  20268. VertexFormat) {
  20269. 'use strict';
  20270. var positionScratch = new Cartesian3();
  20271. var normalScratch = new Cartesian3();
  20272. var tangentScratch = new Cartesian3();
  20273. var binormalScratch = new Cartesian3();
  20274. var rectangleScratch = new Rectangle();
  20275. var stScratch = new Cartesian2();
  20276. var bottomBoundingSphere = new BoundingSphere();
  20277. var topBoundingSphere = new BoundingSphere();
  20278. function createAttributes(vertexFormat, attributes) {
  20279. var geo = new Geometry({
  20280. attributes : new GeometryAttributes(),
  20281. primitiveType : PrimitiveType.TRIANGLES
  20282. });
  20283. geo.attributes.position = new GeometryAttribute({
  20284. componentDatatype : ComponentDatatype.DOUBLE,
  20285. componentsPerAttribute : 3,
  20286. values : attributes.positions
  20287. });
  20288. if (vertexFormat.normal) {
  20289. geo.attributes.normal = new GeometryAttribute({
  20290. componentDatatype : ComponentDatatype.FLOAT,
  20291. componentsPerAttribute : 3,
  20292. values : attributes.normals
  20293. });
  20294. }
  20295. if (vertexFormat.tangent) {
  20296. geo.attributes.tangent = new GeometryAttribute({
  20297. componentDatatype : ComponentDatatype.FLOAT,
  20298. componentsPerAttribute : 3,
  20299. values : attributes.tangents
  20300. });
  20301. }
  20302. if (vertexFormat.binormal) {
  20303. geo.attributes.binormal = new GeometryAttribute({
  20304. componentDatatype : ComponentDatatype.FLOAT,
  20305. componentsPerAttribute : 3,
  20306. values : attributes.binormals
  20307. });
  20308. }
  20309. return geo;
  20310. }
  20311. function calculateAttributes(positions, vertexFormat, ellipsoid, tangentRotationMatrix) {
  20312. var length = positions.length;
  20313. var normals = (vertexFormat.normal) ? new Float32Array(length) : undefined;
  20314. var tangents = (vertexFormat.tangent) ? new Float32Array(length) : undefined;
  20315. var binormals = (vertexFormat.binormal) ? new Float32Array(length) : undefined;
  20316. var attrIndex = 0;
  20317. var binormal = binormalScratch;
  20318. var tangent = tangentScratch;
  20319. var normal = normalScratch;
  20320. for (var i = 0; i < length; i += 3) {
  20321. var p = Cartesian3.fromArray(positions, i, positionScratch);
  20322. var attrIndex1 = attrIndex + 1;
  20323. var attrIndex2 = attrIndex + 2;
  20324. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.binormal) {
  20325. normal = ellipsoid.geodeticSurfaceNormal(p, normal);
  20326. if (vertexFormat.tangent || vertexFormat.binormal) {
  20327. Cartesian3.cross(Cartesian3.UNIT_Z, normal, tangent);
  20328. Matrix3.multiplyByVector(tangentRotationMatrix, tangent, tangent);
  20329. Cartesian3.normalize(tangent, tangent);
  20330. if (vertexFormat.binormal) {
  20331. Cartesian3.normalize(Cartesian3.cross(normal, tangent, binormal), binormal);
  20332. }
  20333. }
  20334. if (vertexFormat.normal) {
  20335. normals[attrIndex] = normal.x;
  20336. normals[attrIndex1] = normal.y;
  20337. normals[attrIndex2] = normal.z;
  20338. }
  20339. if (vertexFormat.tangent) {
  20340. tangents[attrIndex] = tangent.x;
  20341. tangents[attrIndex1] = tangent.y;
  20342. tangents[attrIndex2] = tangent.z;
  20343. }
  20344. if (vertexFormat.binormal) {
  20345. binormals[attrIndex] = binormal.x;
  20346. binormals[attrIndex1] = binormal.y;
  20347. binormals[attrIndex2] = binormal.z;
  20348. }
  20349. }
  20350. attrIndex += 3;
  20351. }
  20352. return createAttributes(vertexFormat, {
  20353. positions : positions,
  20354. normals : normals,
  20355. tangents : tangents,
  20356. binormals : binormals
  20357. });
  20358. }
  20359. var v1Scratch = new Cartesian3();
  20360. var v2Scratch = new Cartesian3();
  20361. function calculateAttributesWall(positions, vertexFormat, ellipsoid) {
  20362. var length = positions.length;
  20363. var normals = (vertexFormat.normal) ? new Float32Array(length) : undefined;
  20364. var tangents = (vertexFormat.tangent) ? new Float32Array(length) : undefined;
  20365. var binormals = (vertexFormat.binormal) ? new Float32Array(length) : undefined;
  20366. var normalIndex = 0;
  20367. var tangentIndex = 0;
  20368. var binormalIndex = 0;
  20369. var recomputeNormal = true;
  20370. var binormal = binormalScratch;
  20371. var tangent = tangentScratch;
  20372. var normal = normalScratch;
  20373. for (var i = 0; i < length; i += 6) {
  20374. var p = Cartesian3.fromArray(positions, i, positionScratch);
  20375. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.binormal) {
  20376. var p1 = Cartesian3.fromArray(positions, (i + 6) % length, v1Scratch);
  20377. if (recomputeNormal) {
  20378. var p2 = Cartesian3.fromArray(positions, (i + 3) % length, v2Scratch);
  20379. Cartesian3.subtract(p1, p, p1);
  20380. Cartesian3.subtract(p2, p, p2);
  20381. normal = Cartesian3.normalize(Cartesian3.cross(p2, p1, normal), normal);
  20382. recomputeNormal = false;
  20383. }
  20384. if (Cartesian3.equalsEpsilon(p1, p, CesiumMath.EPSILON10)) { // if we've reached a corner
  20385. recomputeNormal = true;
  20386. }
  20387. if (vertexFormat.tangent || vertexFormat.binormal) {
  20388. binormal = ellipsoid.geodeticSurfaceNormal(p, binormal);
  20389. if (vertexFormat.tangent) {
  20390. tangent = Cartesian3.normalize(Cartesian3.cross(binormal, normal, tangent), tangent);
  20391. }
  20392. }
  20393. if (vertexFormat.normal) {
  20394. normals[normalIndex++] = normal.x;
  20395. normals[normalIndex++] = normal.y;
  20396. normals[normalIndex++] = normal.z;
  20397. normals[normalIndex++] = normal.x;
  20398. normals[normalIndex++] = normal.y;
  20399. normals[normalIndex++] = normal.z;
  20400. }
  20401. if (vertexFormat.tangent) {
  20402. tangents[tangentIndex++] = tangent.x;
  20403. tangents[tangentIndex++] = tangent.y;
  20404. tangents[tangentIndex++] = tangent.z;
  20405. tangents[tangentIndex++] = tangent.x;
  20406. tangents[tangentIndex++] = tangent.y;
  20407. tangents[tangentIndex++] = tangent.z;
  20408. }
  20409. if (vertexFormat.binormal) {
  20410. binormals[binormalIndex++] = binormal.x;
  20411. binormals[binormalIndex++] = binormal.y;
  20412. binormals[binormalIndex++] = binormal.z;
  20413. binormals[binormalIndex++] = binormal.x;
  20414. binormals[binormalIndex++] = binormal.y;
  20415. binormals[binormalIndex++] = binormal.z;
  20416. }
  20417. }
  20418. }
  20419. return createAttributes(vertexFormat, {
  20420. positions : positions,
  20421. normals : normals,
  20422. tangents : tangents,
  20423. binormals : binormals
  20424. });
  20425. }
  20426. function constructRectangle(options) {
  20427. var vertexFormat = options.vertexFormat;
  20428. var ellipsoid = options.ellipsoid;
  20429. var size = options.size;
  20430. var height = options.height;
  20431. var width = options.width;
  20432. var positions = (vertexFormat.position) ? new Float64Array(size * 3) : undefined;
  20433. var textureCoordinates = (vertexFormat.st) ? new Float32Array(size * 2) : undefined;
  20434. var posIndex = 0;
  20435. var stIndex = 0;
  20436. var position = positionScratch;
  20437. var st = stScratch;
  20438. var minX = Number.MAX_VALUE;
  20439. var minY = Number.MAX_VALUE;
  20440. var maxX = -Number.MAX_VALUE;
  20441. var maxY = -Number.MAX_VALUE;
  20442. for (var row = 0; row < height; ++row) {
  20443. for (var col = 0; col < width; ++col) {
  20444. RectangleGeometryLibrary.computePosition(options, row, col, position, st);
  20445. positions[posIndex++] = position.x;
  20446. positions[posIndex++] = position.y;
  20447. positions[posIndex++] = position.z;
  20448. if (vertexFormat.st) {
  20449. textureCoordinates[stIndex++] = st.x;
  20450. textureCoordinates[stIndex++] = st.y;
  20451. minX = Math.min(minX, st.x);
  20452. minY = Math.min(minY, st.y);
  20453. maxX = Math.max(maxX, st.x);
  20454. maxY = Math.max(maxY, st.y);
  20455. }
  20456. }
  20457. }
  20458. if (vertexFormat.st && (minX < 0.0 || minY < 0.0 || maxX > 1.0 || maxY > 1.0)) {
  20459. for (var k = 0; k < textureCoordinates.length; k += 2) {
  20460. textureCoordinates[k] = (textureCoordinates[k] - minX) / (maxX - minX);
  20461. textureCoordinates[k + 1] = (textureCoordinates[k + 1] - minY) / (maxY - minY);
  20462. }
  20463. }
  20464. var geo = calculateAttributes(positions, vertexFormat, ellipsoid, options.tangentRotationMatrix);
  20465. var indicesSize = 6 * (width - 1) * (height - 1);
  20466. var indices = IndexDatatype.createTypedArray(size, indicesSize);
  20467. var index = 0;
  20468. var indicesIndex = 0;
  20469. for (var i = 0; i < height - 1; ++i) {
  20470. for (var j = 0; j < width - 1; ++j) {
  20471. var upperLeft = index;
  20472. var lowerLeft = upperLeft + width;
  20473. var lowerRight = lowerLeft + 1;
  20474. var upperRight = upperLeft + 1;
  20475. indices[indicesIndex++] = upperLeft;
  20476. indices[indicesIndex++] = lowerLeft;
  20477. indices[indicesIndex++] = upperRight;
  20478. indices[indicesIndex++] = upperRight;
  20479. indices[indicesIndex++] = lowerLeft;
  20480. indices[indicesIndex++] = lowerRight;
  20481. ++index;
  20482. }
  20483. ++index;
  20484. }
  20485. geo.indices = indices;
  20486. if (vertexFormat.st) {
  20487. geo.attributes.st = new GeometryAttribute({
  20488. componentDatatype : ComponentDatatype.FLOAT,
  20489. componentsPerAttribute : 2,
  20490. values : textureCoordinates
  20491. });
  20492. }
  20493. return geo;
  20494. }
  20495. function addWallPositions(wallPositions, posIndex, i, topPositions, bottomPositions) {
  20496. wallPositions[posIndex++] = topPositions[i];
  20497. wallPositions[posIndex++] = topPositions[i + 1];
  20498. wallPositions[posIndex++] = topPositions[i + 2];
  20499. wallPositions[posIndex++] = bottomPositions[i];
  20500. wallPositions[posIndex++] = bottomPositions[i + 1];
  20501. wallPositions[posIndex++] = bottomPositions[i + 2];
  20502. return wallPositions;
  20503. }
  20504. function addWallTextureCoordinates(wallTextures, stIndex, i, st) {
  20505. wallTextures[stIndex++] = st[i];
  20506. wallTextures[stIndex++] = st[i + 1];
  20507. wallTextures[stIndex++] = st[i];
  20508. wallTextures[stIndex++] = st[i + 1];
  20509. return wallTextures;
  20510. }
  20511. function constructExtrudedRectangle(options) {
  20512. var vertexFormat = options.vertexFormat;
  20513. var surfaceHeight = options.surfaceHeight;
  20514. var extrudedHeight = options.extrudedHeight;
  20515. var minHeight = Math.min(extrudedHeight, surfaceHeight);
  20516. var maxHeight = Math.max(extrudedHeight, surfaceHeight);
  20517. var height = options.height;
  20518. var width = options.width;
  20519. var ellipsoid = options.ellipsoid;
  20520. var i;
  20521. var topBottomGeo = constructRectangle(options);
  20522. if (CesiumMath.equalsEpsilon(minHeight, maxHeight, CesiumMath.EPSILON10)) {
  20523. return topBottomGeo;
  20524. }
  20525. var topPositions = PolygonPipeline.scaleToGeodeticHeight(topBottomGeo.attributes.position.values, maxHeight, ellipsoid, false);
  20526. topPositions = new Float64Array(topPositions);
  20527. var length = topPositions.length;
  20528. var newLength = length*2;
  20529. var positions = new Float64Array(newLength);
  20530. positions.set(topPositions);
  20531. var bottomPositions = PolygonPipeline.scaleToGeodeticHeight(topBottomGeo.attributes.position.values, minHeight, ellipsoid);
  20532. positions.set(bottomPositions, length);
  20533. topBottomGeo.attributes.position.values = positions;
  20534. var normals = (vertexFormat.normal) ? new Float32Array(newLength) : undefined;
  20535. var tangents = (vertexFormat.tangent) ? new Float32Array(newLength) : undefined;
  20536. var binormals = (vertexFormat.binormal) ? new Float32Array(newLength) : undefined;
  20537. var textures = (vertexFormat.st) ? new Float32Array(newLength/3*2) : undefined;
  20538. var topSt;
  20539. if (vertexFormat.normal) {
  20540. var topNormals = topBottomGeo.attributes.normal.values;
  20541. normals.set(topNormals);
  20542. for (i = 0; i < length; i ++) {
  20543. topNormals[i] = -topNormals[i];
  20544. }
  20545. normals.set(topNormals, length);
  20546. topBottomGeo.attributes.normal.values = normals;
  20547. }
  20548. if (vertexFormat.tangent) {
  20549. var topTangents = topBottomGeo.attributes.tangent.values;
  20550. tangents.set(topTangents);
  20551. for (i = 0; i < length; i ++) {
  20552. topTangents[i] = -topTangents[i];
  20553. }
  20554. tangents.set(topTangents, length);
  20555. topBottomGeo.attributes.tangent.values = tangents;
  20556. }
  20557. if (vertexFormat.binormal) {
  20558. var topBinormals = topBottomGeo.attributes.binormal.values;
  20559. binormals.set(topBinormals);
  20560. binormals.set(topBinormals, length);
  20561. topBottomGeo.attributes.binormal.values = binormals;
  20562. }
  20563. if (vertexFormat.st) {
  20564. topSt = topBottomGeo.attributes.st.values;
  20565. textures.set(topSt);
  20566. textures.set(topSt, length/3*2);
  20567. topBottomGeo.attributes.st.values = textures;
  20568. }
  20569. var indices = topBottomGeo.indices;
  20570. var indicesLength = indices.length;
  20571. var posLength = length / 3;
  20572. var newIndices = IndexDatatype.createTypedArray(newLength/3, indicesLength*2);
  20573. newIndices.set(indices);
  20574. for (i = 0; i < indicesLength; i += 3) {
  20575. newIndices[i + indicesLength] = indices[i + 2] + posLength;
  20576. newIndices[i + 1 + indicesLength] = indices[i + 1] + posLength;
  20577. newIndices[i + 2 + indicesLength] = indices[i] + posLength;
  20578. }
  20579. topBottomGeo.indices = newIndices;
  20580. var perimeterPositions = 2 * width + 2 * height - 4;
  20581. var wallCount = (perimeterPositions + 4) * 2;
  20582. var wallPositions = new Float64Array(wallCount * 3);
  20583. var wallTextures = (vertexFormat.st) ? new Float32Array(wallCount * 2) : undefined;
  20584. var posIndex = 0;
  20585. var stIndex = 0;
  20586. var area = width * height;
  20587. for (i = 0; i < area; i+=width) {
  20588. wallPositions = addWallPositions(wallPositions, posIndex, i*3, topPositions, bottomPositions);
  20589. posIndex += 6;
  20590. if (vertexFormat.st) {
  20591. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
  20592. stIndex += 4;
  20593. }
  20594. }
  20595. for (i = area-width; i < area; i++) {
  20596. wallPositions = addWallPositions(wallPositions, posIndex, i*3, topPositions, bottomPositions);
  20597. posIndex += 6;
  20598. if (vertexFormat.st) {
  20599. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
  20600. stIndex += 4;
  20601. }
  20602. }
  20603. for (i = area-1; i > 0; i-=width) {
  20604. wallPositions = addWallPositions(wallPositions, posIndex, i*3, topPositions, bottomPositions);
  20605. posIndex += 6;
  20606. if (vertexFormat.st) {
  20607. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
  20608. stIndex += 4;
  20609. }
  20610. }
  20611. for (i = width-1; i >= 0; i--) {
  20612. wallPositions = addWallPositions(wallPositions, posIndex, i*3, topPositions, bottomPositions);
  20613. posIndex += 6;
  20614. if (vertexFormat.st) {
  20615. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
  20616. stIndex += 4;
  20617. }
  20618. }
  20619. var geo = calculateAttributesWall(wallPositions, vertexFormat, ellipsoid);
  20620. if (vertexFormat.st) {
  20621. geo.attributes.st = new GeometryAttribute({
  20622. componentDatatype : ComponentDatatype.FLOAT,
  20623. componentsPerAttribute : 2,
  20624. values : wallTextures
  20625. });
  20626. }
  20627. var wallIndices = IndexDatatype.createTypedArray(wallCount, perimeterPositions * 6);
  20628. var upperLeft;
  20629. var lowerLeft;
  20630. var lowerRight;
  20631. var upperRight;
  20632. length = wallPositions.length / 3;
  20633. var index = 0;
  20634. for (i = 0; i < length - 1; i+=2) {
  20635. upperLeft = i;
  20636. upperRight = (upperLeft + 2) % length;
  20637. var p1 = Cartesian3.fromArray(wallPositions, upperLeft * 3, v1Scratch);
  20638. var p2 = Cartesian3.fromArray(wallPositions, upperRight * 3, v2Scratch);
  20639. if (Cartesian3.equalsEpsilon(p1, p2, CesiumMath.EPSILON10)) {
  20640. continue;
  20641. }
  20642. lowerLeft = (upperLeft + 1) % length;
  20643. lowerRight = (lowerLeft + 2) % length;
  20644. wallIndices[index++] = upperLeft;
  20645. wallIndices[index++] = lowerLeft;
  20646. wallIndices[index++] = upperRight;
  20647. wallIndices[index++] = upperRight;
  20648. wallIndices[index++] = lowerLeft;
  20649. wallIndices[index++] = lowerRight;
  20650. }
  20651. geo.indices = wallIndices;
  20652. geo = GeometryPipeline.combineInstances([
  20653. new GeometryInstance({
  20654. geometry : topBottomGeo
  20655. }),
  20656. new GeometryInstance({
  20657. geometry : geo
  20658. })
  20659. ]);
  20660. return geo[0];
  20661. }
  20662. var scratchRotationMatrix = new Matrix3();
  20663. var scratchCartesian3 = new Cartesian3();
  20664. var scratchQuaternion = new Quaternion();
  20665. var scratchRectanglePoints = [new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3()];
  20666. var scratchCartographicPoints = [new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic()];
  20667. function computeRectangle(rectangle, ellipsoid, rotation) {
  20668. if (rotation === 0.0) {
  20669. return Rectangle.clone(rectangle);
  20670. }
  20671. Rectangle.northeast(rectangle, scratchCartographicPoints[0]);
  20672. Rectangle.northwest(rectangle, scratchCartographicPoints[1]);
  20673. Rectangle.southeast(rectangle, scratchCartographicPoints[2]);
  20674. Rectangle.southwest(rectangle, scratchCartographicPoints[3]);
  20675. ellipsoid.cartographicArrayToCartesianArray(scratchCartographicPoints, scratchRectanglePoints);
  20676. var surfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(Rectangle.center(rectangle, scratchCartesian3));
  20677. Quaternion.fromAxisAngle(surfaceNormal, rotation, scratchQuaternion);
  20678. Matrix3.fromQuaternion(scratchQuaternion, scratchRotationMatrix);
  20679. for (var i = 0; i < 4; ++i) {
  20680. // Apply the rotation
  20681. Matrix3.multiplyByVector(scratchRotationMatrix, scratchRectanglePoints[i], scratchRectanglePoints[i]);
  20682. }
  20683. ellipsoid.cartesianArrayToCartographicArray(scratchRectanglePoints, scratchCartographicPoints);
  20684. return Rectangle.fromCartographicArray(scratchCartographicPoints);
  20685. }
  20686. /**
  20687. * A description of a cartographic rectangle on an ellipsoid centered at the origin. Rectangle geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  20688. *
  20689. * @alias RectangleGeometry
  20690. * @constructor
  20691. *
  20692. * @param {Object} options Object with the following properties:
  20693. * @param {Rectangle} options.rectangle A cartographic rectangle with north, south, east and west properties in radians.
  20694. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  20695. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle lies.
  20696. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  20697. * @param {Number} [options.height=0.0] The distance in meters between the rectangle and the ellipsoid surface.
  20698. * @param {Number} [options.rotation=0.0] The rotation of the rectangle, in radians. A positive rotation is counter-clockwise.
  20699. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  20700. * @param {Number} [options.extrudedHeight] The distance in meters between the rectangle's extruded face and the ellipsoid surface.
  20701. * @param {Boolean} [options.closeTop=true] Specifies whether the rectangle has a top cover when extruded.
  20702. * @param {Boolean} [options.closeBottom=true] Specifies whether the rectangle has a bottom cover when extruded.
  20703. *
  20704. * @exception {DeveloperError} <code>options.rectangle.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  20705. * @exception {DeveloperError} <code>options.rectangle.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  20706. * @exception {DeveloperError} <code>options.rectangle.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  20707. * @exception {DeveloperError} <code>options.rectangle.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  20708. * @exception {DeveloperError} <code>options.rectangle.north</code> must be greater than <code>options.rectangle.south</code>.
  20709. *
  20710. * @see RectangleGeometry#createGeometry
  20711. *
  20712. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Rectangle.html|Cesium Sandcastle Rectangle Demo}
  20713. *
  20714. * @example
  20715. * // 1. create an rectangle
  20716. * var rectangle = new Cesium.RectangleGeometry({
  20717. * ellipsoid : Cesium.Ellipsoid.WGS84,
  20718. * rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
  20719. * height : 10000.0
  20720. * });
  20721. * var geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
  20722. *
  20723. * // 2. create an extruded rectangle without a top
  20724. * var rectangle = new Cesium.RectangleGeometry({
  20725. * ellipsoid : Cesium.Ellipsoid.WGS84,
  20726. * rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
  20727. * height : 10000.0,
  20728. * extrudedHeight: 300000,
  20729. * closeTop: false
  20730. * });
  20731. * var geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
  20732. */
  20733. function RectangleGeometry(options) {
  20734. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  20735. var rectangle = options.rectangle;
  20736. var granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  20737. var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  20738. var surfaceHeight = defaultValue(options.height, 0.0);
  20739. var rotation = defaultValue(options.rotation, 0.0);
  20740. var stRotation = defaultValue(options.stRotation, 0.0);
  20741. var vertexFormat = defaultValue(options.vertexFormat, VertexFormat.DEFAULT);
  20742. var extrudedHeight = options.extrudedHeight;
  20743. var extrude = defined(extrudedHeight);
  20744. var closeTop = defaultValue(options.closeTop, true);
  20745. var closeBottom = defaultValue(options.closeBottom, true);
  20746. if (!defined(rectangle)) {
  20747. throw new DeveloperError('rectangle is required.');
  20748. }
  20749. Rectangle.validate(rectangle);
  20750. if (rectangle.north < rectangle.south) {
  20751. throw new DeveloperError('options.rectangle.north must be greater than options.rectangle.south');
  20752. }
  20753. this._rectangle = rectangle;
  20754. this._granularity = granularity;
  20755. this._ellipsoid = Ellipsoid.clone(ellipsoid);
  20756. this._surfaceHeight = surfaceHeight;
  20757. this._rotation = rotation;
  20758. this._stRotation = stRotation;
  20759. this._vertexFormat = VertexFormat.clone(vertexFormat);
  20760. this._extrudedHeight = defaultValue(extrudedHeight, 0.0);
  20761. this._extrude = extrude;
  20762. this._closeTop = closeTop;
  20763. this._closeBottom = closeBottom;
  20764. this._workerName = 'createRectangleGeometry';
  20765. this._rotatedRectangle = computeRectangle(this._rectangle, this._ellipsoid, rotation);
  20766. }
  20767. /**
  20768. * The number of elements used to pack the object into an array.
  20769. * @type {Number}
  20770. */
  20771. RectangleGeometry.packedLength = Rectangle.packedLength + Ellipsoid.packedLength + VertexFormat.packedLength + Rectangle.packedLength + 8;
  20772. /**
  20773. * Stores the provided instance into the provided array.
  20774. *
  20775. * @param {RectangleGeometry} value The value to pack.
  20776. * @param {Number[]} array The array to pack into.
  20777. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  20778. *
  20779. * @returns {Number[]} The array that was packed into
  20780. */
  20781. RectangleGeometry.pack = function(value, array, startingIndex) {
  20782. if (!defined(value)) {
  20783. throw new DeveloperError('value is required');
  20784. }
  20785. if (!defined(array)) {
  20786. throw new DeveloperError('array is required');
  20787. }
  20788. startingIndex = defaultValue(startingIndex, 0);
  20789. Rectangle.pack(value._rectangle, array, startingIndex);
  20790. startingIndex += Rectangle.packedLength;
  20791. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  20792. startingIndex += Ellipsoid.packedLength;
  20793. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  20794. startingIndex += VertexFormat.packedLength;
  20795. Rectangle.pack(value._rotatedRectangle, array, startingIndex);
  20796. startingIndex += Rectangle.packedLength;
  20797. array[startingIndex++] = value._granularity;
  20798. array[startingIndex++] = value._surfaceHeight;
  20799. array[startingIndex++] = value._rotation;
  20800. array[startingIndex++] = value._stRotation;
  20801. array[startingIndex++] = value._extrudedHeight;
  20802. array[startingIndex++] = value._extrude ? 1.0 : 0.0;
  20803. array[startingIndex++] = value._closeTop ? 1.0 : 0.0;
  20804. array[startingIndex] = value._closeBottom ? 1.0 : 0.0;
  20805. return array;
  20806. };
  20807. var scratchRectangle = new Rectangle();
  20808. var scratchRotatedRectangle = new Rectangle();
  20809. var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  20810. var scratchVertexFormat = new VertexFormat();
  20811. var scratchOptions = {
  20812. rectangle : scratchRectangle,
  20813. ellipsoid : scratchEllipsoid,
  20814. vertexFormat : scratchVertexFormat,
  20815. granularity : undefined,
  20816. height : undefined,
  20817. rotation : undefined,
  20818. stRotation : undefined,
  20819. extrudedHeight : undefined,
  20820. closeTop : undefined,
  20821. closeBottom : undefined
  20822. };
  20823. /**
  20824. * Retrieves an instance from a packed array.
  20825. *
  20826. * @param {Number[]} array The packed array.
  20827. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  20828. * @param {RectangleGeometry} [result] The object into which to store the result.
  20829. * @returns {RectangleGeometry} The modified result parameter or a new RectangleGeometry instance if one was not provided.
  20830. */
  20831. RectangleGeometry.unpack = function(array, startingIndex, result) {
  20832. if (!defined(array)) {
  20833. throw new DeveloperError('array is required');
  20834. }
  20835. startingIndex = defaultValue(startingIndex, 0);
  20836. var rectangle = Rectangle.unpack(array, startingIndex, scratchRectangle);
  20837. startingIndex += Rectangle.packedLength;
  20838. var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  20839. startingIndex += Ellipsoid.packedLength;
  20840. var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  20841. startingIndex += VertexFormat.packedLength;
  20842. var rotatedRectangle = Rectangle.unpack(array, startingIndex, scratchRotatedRectangle);
  20843. startingIndex += Rectangle.packedLength;
  20844. var granularity = array[startingIndex++];
  20845. var surfaceHeight = array[startingIndex++];
  20846. var rotation = array[startingIndex++];
  20847. var stRotation = array[startingIndex++];
  20848. var extrudedHeight = array[startingIndex++];
  20849. var extrude = array[startingIndex++] === 1.0;
  20850. var closeTop = array[startingIndex++] === 1.0;
  20851. var closeBottom = array[startingIndex] === 1.0;
  20852. if (!defined(result)) {
  20853. scratchOptions.granularity = granularity;
  20854. scratchOptions.height = surfaceHeight;
  20855. scratchOptions.rotation = rotation;
  20856. scratchOptions.stRotation = stRotation;
  20857. scratchOptions.extrudedHeight = extrude ? extrudedHeight : undefined;
  20858. scratchOptions.closeTop = closeTop;
  20859. scratchOptions.closeBottom = closeBottom;
  20860. return new RectangleGeometry(scratchOptions);
  20861. }
  20862. result._rectangle = Rectangle.clone(rectangle, result._rectangle);
  20863. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  20864. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  20865. result._granularity = granularity;
  20866. result._surfaceHeight = surfaceHeight;
  20867. result._rotation = rotation;
  20868. result._stRotation = stRotation;
  20869. result._extrudedHeight = extrude ? extrudedHeight : undefined;
  20870. result._extrude = extrude;
  20871. result._closeTop = closeTop;
  20872. result._closeBottom = closeBottom;
  20873. result._rotatedRectangle = rotatedRectangle;
  20874. return result;
  20875. };
  20876. var tangentRotationMatrixScratch = new Matrix3();
  20877. var nwScratch = new Cartographic();
  20878. var stNwScratch = new Cartographic();
  20879. var quaternionScratch = new Quaternion();
  20880. var centerScratch = new Cartographic();
  20881. /**
  20882. * Computes the geometric representation of an rectangle, including its vertices, indices, and a bounding sphere.
  20883. *
  20884. * @param {RectangleGeometry} rectangleGeometry A description of the rectangle.
  20885. * @returns {Geometry|undefined} The computed vertices and indices.
  20886. *
  20887. * @exception {DeveloperError} Rotated rectangle is invalid.
  20888. */
  20889. RectangleGeometry.createGeometry = function(rectangleGeometry) {
  20890. if ((CesiumMath.equalsEpsilon(rectangleGeometry._rectangle.north, rectangleGeometry._rectangle.south, CesiumMath.EPSILON10) ||
  20891. (CesiumMath.equalsEpsilon(rectangleGeometry._rectangle.east, rectangleGeometry._rectangle.west, CesiumMath.EPSILON10)))) {
  20892. return undefined;
  20893. }
  20894. var rectangle = Rectangle.clone(rectangleGeometry._rectangle, rectangleScratch);
  20895. var ellipsoid = rectangleGeometry._ellipsoid;
  20896. var surfaceHeight = rectangleGeometry._surfaceHeight;
  20897. var extrude = rectangleGeometry._extrude;
  20898. var extrudedHeight = rectangleGeometry._extrudedHeight;
  20899. var rotation = rectangleGeometry._rotation;
  20900. var stRotation = rectangleGeometry._stRotation;
  20901. var vertexFormat = rectangleGeometry._vertexFormat;
  20902. var options = RectangleGeometryLibrary.computeOptions(rectangleGeometry, rectangle, nwScratch, stNwScratch);
  20903. var tangentRotationMatrix = tangentRotationMatrixScratch;
  20904. if (stRotation !== 0 || rotation !== 0) {
  20905. var center = Rectangle.center(rectangle, centerScratch);
  20906. var axis = ellipsoid.geodeticSurfaceNormalCartographic(center, v1Scratch);
  20907. Quaternion.fromAxisAngle(axis, -stRotation, quaternionScratch);
  20908. Matrix3.fromQuaternion(quaternionScratch, tangentRotationMatrix);
  20909. } else {
  20910. Matrix3.clone(Matrix3.IDENTITY, tangentRotationMatrix);
  20911. }
  20912. options.lonScalar = 1.0 / rectangleGeometry._rectangle.width;
  20913. options.latScalar = 1.0 / rectangleGeometry._rectangle.height;
  20914. options.vertexFormat = vertexFormat;
  20915. options.rotation = rotation;
  20916. options.stRotation = stRotation;
  20917. options.tangentRotationMatrix = tangentRotationMatrix;
  20918. options.size = options.width * options.height;
  20919. var geometry;
  20920. var boundingSphere;
  20921. rectangle = rectangleGeometry._rectangle;
  20922. if (extrude) {
  20923. geometry = constructExtrudedRectangle(options);
  20924. var topBS = BoundingSphere.fromRectangle3D(rectangle, ellipsoid, surfaceHeight, topBoundingSphere);
  20925. var bottomBS = BoundingSphere.fromRectangle3D(rectangle, ellipsoid, extrudedHeight, bottomBoundingSphere);
  20926. boundingSphere = BoundingSphere.union(topBS, bottomBS);
  20927. } else {
  20928. geometry = constructRectangle(options);
  20929. geometry.attributes.position.values = PolygonPipeline.scaleToGeodeticHeight(geometry.attributes.position.values, surfaceHeight, ellipsoid, false);
  20930. boundingSphere = BoundingSphere.fromRectangle3D(rectangle, ellipsoid, surfaceHeight);
  20931. }
  20932. if (!vertexFormat.position) {
  20933. delete geometry.attributes.position;
  20934. }
  20935. return new Geometry({
  20936. attributes : new GeometryAttributes(geometry.attributes),
  20937. indices : geometry.indices,
  20938. primitiveType : geometry.primitiveType,
  20939. boundingSphere : boundingSphere
  20940. });
  20941. };
  20942. /**
  20943. * @private
  20944. */
  20945. RectangleGeometry.createShadowVolume = function(rectangleGeometry, minHeightFunc, maxHeightFunc) {
  20946. var granularity = rectangleGeometry._granularity;
  20947. var ellipsoid = rectangleGeometry._ellipsoid;
  20948. var minHeight = minHeightFunc(granularity, ellipsoid);
  20949. var maxHeight = maxHeightFunc(granularity, ellipsoid);
  20950. // TODO: stRotation
  20951. return new RectangleGeometry({
  20952. rectangle : rectangleGeometry._rectangle,
  20953. rotation : rectangleGeometry._rotation,
  20954. ellipsoid : ellipsoid,
  20955. stRotation : rectangleGeometry._stRotation,
  20956. granularity : granularity,
  20957. extrudedHeight : maxHeight,
  20958. height : minHeight,
  20959. closeTop : true,
  20960. closeBottom : true,
  20961. vertexFormat : VertexFormat.POSITION_ONLY
  20962. });
  20963. };
  20964. defineProperties(RectangleGeometry.prototype, {
  20965. /**
  20966. * @private
  20967. */
  20968. rectangle : {
  20969. get : function() {
  20970. return this._rotatedRectangle;
  20971. }
  20972. }
  20973. });
  20974. return RectangleGeometry;
  20975. });
  20976. /*global define*/
  20977. define('Workers/createRectangleGeometry',[
  20978. '../Core/defined',
  20979. '../Core/Ellipsoid',
  20980. '../Core/Rectangle',
  20981. '../Core/RectangleGeometry'
  20982. ], function(
  20983. defined,
  20984. Ellipsoid,
  20985. Rectangle,
  20986. RectangleGeometry) {
  20987. 'use strict';
  20988. function createRectangleGeometry(rectangleGeometry, offset) {
  20989. if (defined(offset)) {
  20990. rectangleGeometry = RectangleGeometry.unpack(rectangleGeometry, offset);
  20991. }
  20992. rectangleGeometry._ellipsoid = Ellipsoid.clone(rectangleGeometry._ellipsoid);
  20993. rectangleGeometry._rectangle = Rectangle.clone(rectangleGeometry._rectangle);
  20994. return RectangleGeometry.createGeometry(rectangleGeometry);
  20995. }
  20996. return createRectangleGeometry;
  20997. });
  20998. }());