Matrix2-d35cf4b5.js 379 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './RuntimeError-8952249c', './defaultValue-81eec7ed', './ComponentDatatype-9e86ac8f'], (function (exports, RuntimeError, defaultValue, ComponentDatatype) { 'use strict';
  24. /**
  25. * A 3D Cartesian point.
  26. * @alias Cartesian3
  27. * @constructor
  28. *
  29. * @param {Number} [x=0.0] The X component.
  30. * @param {Number} [y=0.0] The Y component.
  31. * @param {Number} [z=0.0] The Z component.
  32. *
  33. * @see Cartesian2
  34. * @see Cartesian4
  35. * @see Packable
  36. */
  37. function Cartesian3(x, y, z) {
  38. /**
  39. * The X component.
  40. * @type {Number}
  41. * @default 0.0
  42. */
  43. this.x = defaultValue.defaultValue(x, 0.0);
  44. /**
  45. * The Y component.
  46. * @type {Number}
  47. * @default 0.0
  48. */
  49. this.y = defaultValue.defaultValue(y, 0.0);
  50. /**
  51. * The Z component.
  52. * @type {Number}
  53. * @default 0.0
  54. */
  55. this.z = defaultValue.defaultValue(z, 0.0);
  56. }
  57. /**
  58. * Converts the provided Spherical into Cartesian3 coordinates.
  59. *
  60. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  61. * @param {Cartesian3} [result] The object onto which to store the result.
  62. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  63. */
  64. Cartesian3.fromSpherical = function (spherical, result) {
  65. //>>includeStart('debug', pragmas.debug);
  66. RuntimeError.Check.typeOf.object("spherical", spherical);
  67. //>>includeEnd('debug');
  68. if (!defaultValue.defined(result)) {
  69. result = new Cartesian3();
  70. }
  71. const clock = spherical.clock;
  72. const cone = spherical.cone;
  73. const magnitude = defaultValue.defaultValue(spherical.magnitude, 1.0);
  74. const radial = magnitude * Math.sin(cone);
  75. result.x = radial * Math.cos(clock);
  76. result.y = radial * Math.sin(clock);
  77. result.z = magnitude * Math.cos(cone);
  78. return result;
  79. };
  80. /**
  81. * Creates a Cartesian3 instance from x, y and z coordinates.
  82. *
  83. * @param {Number} x The x coordinate.
  84. * @param {Number} y The y coordinate.
  85. * @param {Number} z The z coordinate.
  86. * @param {Cartesian3} [result] The object onto which to store the result.
  87. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  88. */
  89. Cartesian3.fromElements = function (x, y, z, result) {
  90. if (!defaultValue.defined(result)) {
  91. return new Cartesian3(x, y, z);
  92. }
  93. result.x = x;
  94. result.y = y;
  95. result.z = z;
  96. return result;
  97. };
  98. /**
  99. * Duplicates a Cartesian3 instance.
  100. *
  101. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  102. * @param {Cartesian3} [result] The object onto which to store the result.
  103. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  104. */
  105. Cartesian3.clone = function (cartesian, result) {
  106. if (!defaultValue.defined(cartesian)) {
  107. return undefined;
  108. }
  109. if (!defaultValue.defined(result)) {
  110. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  111. }
  112. result.x = cartesian.x;
  113. result.y = cartesian.y;
  114. result.z = cartesian.z;
  115. return result;
  116. };
  117. /**
  118. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  119. * x, y, and z properties of the Cartesian4 and drops w.
  120. * @function
  121. *
  122. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  123. * @param {Cartesian3} [result] The object onto which to store the result.
  124. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  125. */
  126. Cartesian3.fromCartesian4 = Cartesian3.clone;
  127. /**
  128. * The number of elements used to pack the object into an array.
  129. * @type {Number}
  130. */
  131. Cartesian3.packedLength = 3;
  132. /**
  133. * Stores the provided instance into the provided array.
  134. *
  135. * @param {Cartesian3} value The value to pack.
  136. * @param {Number[]} array The array to pack into.
  137. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  138. *
  139. * @returns {Number[]} The array that was packed into
  140. */
  141. Cartesian3.pack = function (value, array, startingIndex) {
  142. //>>includeStart('debug', pragmas.debug);
  143. RuntimeError.Check.typeOf.object("value", value);
  144. RuntimeError.Check.defined("array", array);
  145. //>>includeEnd('debug');
  146. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  147. array[startingIndex++] = value.x;
  148. array[startingIndex++] = value.y;
  149. array[startingIndex] = value.z;
  150. return array;
  151. };
  152. /**
  153. * Retrieves an instance from a packed array.
  154. *
  155. * @param {Number[]} array The packed array.
  156. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  157. * @param {Cartesian3} [result] The object into which to store the result.
  158. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  159. */
  160. Cartesian3.unpack = function (array, startingIndex, result) {
  161. //>>includeStart('debug', pragmas.debug);
  162. RuntimeError.Check.defined("array", array);
  163. //>>includeEnd('debug');
  164. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  165. if (!defaultValue.defined(result)) {
  166. result = new Cartesian3();
  167. }
  168. result.x = array[startingIndex++];
  169. result.y = array[startingIndex++];
  170. result.z = array[startingIndex];
  171. return result;
  172. };
  173. /**
  174. * Flattens an array of Cartesian3s into an array of components.
  175. *
  176. * @param {Cartesian3[]} array The array of cartesians to pack.
  177. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 3 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 3) elements.
  178. * @returns {Number[]} The packed array.
  179. */
  180. Cartesian3.packArray = function (array, result) {
  181. //>>includeStart('debug', pragmas.debug);
  182. RuntimeError.Check.defined("array", array);
  183. //>>includeEnd('debug');
  184. const length = array.length;
  185. const resultLength = length * 3;
  186. if (!defaultValue.defined(result)) {
  187. result = new Array(resultLength);
  188. } else if (!Array.isArray(result) && result.length !== resultLength) {
  189. //>>includeStart('debug', pragmas.debug);
  190. throw new RuntimeError.DeveloperError(
  191. "If result is a typed array, it must have exactly array.length * 3 elements"
  192. );
  193. //>>includeEnd('debug');
  194. } else if (result.length !== resultLength) {
  195. result.length = resultLength;
  196. }
  197. for (let i = 0; i < length; ++i) {
  198. Cartesian3.pack(array[i], result, i * 3);
  199. }
  200. return result;
  201. };
  202. /**
  203. * Unpacks an array of cartesian components into an array of Cartesian3s.
  204. *
  205. * @param {Number[]} array The array of components to unpack.
  206. * @param {Cartesian3[]} [result] The array onto which to store the result.
  207. * @returns {Cartesian3[]} The unpacked array.
  208. */
  209. Cartesian3.unpackArray = function (array, result) {
  210. //>>includeStart('debug', pragmas.debug);
  211. RuntimeError.Check.defined("array", array);
  212. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 3);
  213. if (array.length % 3 !== 0) {
  214. throw new RuntimeError.DeveloperError("array length must be a multiple of 3.");
  215. }
  216. //>>includeEnd('debug');
  217. const length = array.length;
  218. if (!defaultValue.defined(result)) {
  219. result = new Array(length / 3);
  220. } else {
  221. result.length = length / 3;
  222. }
  223. for (let i = 0; i < length; i += 3) {
  224. const index = i / 3;
  225. result[index] = Cartesian3.unpack(array, i, result[index]);
  226. }
  227. return result;
  228. };
  229. /**
  230. * Creates a Cartesian3 from three consecutive elements in an array.
  231. * @function
  232. *
  233. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  234. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  235. * @param {Cartesian3} [result] The object onto which to store the result.
  236. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  237. *
  238. * @example
  239. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  240. * const v = [1.0, 2.0, 3.0];
  241. * const p = Cesium.Cartesian3.fromArray(v);
  242. *
  243. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  244. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  245. * const p2 = Cesium.Cartesian3.fromArray(v2, 2);
  246. */
  247. Cartesian3.fromArray = Cartesian3.unpack;
  248. /**
  249. * Computes the value of the maximum component for the supplied Cartesian.
  250. *
  251. * @param {Cartesian3} cartesian The cartesian to use.
  252. * @returns {Number} The value of the maximum component.
  253. */
  254. Cartesian3.maximumComponent = function (cartesian) {
  255. //>>includeStart('debug', pragmas.debug);
  256. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  257. //>>includeEnd('debug');
  258. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  259. };
  260. /**
  261. * Computes the value of the minimum component for the supplied Cartesian.
  262. *
  263. * @param {Cartesian3} cartesian The cartesian to use.
  264. * @returns {Number} The value of the minimum component.
  265. */
  266. Cartesian3.minimumComponent = function (cartesian) {
  267. //>>includeStart('debug', pragmas.debug);
  268. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  269. //>>includeEnd('debug');
  270. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  271. };
  272. /**
  273. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  274. *
  275. * @param {Cartesian3} first A cartesian to compare.
  276. * @param {Cartesian3} second A cartesian to compare.
  277. * @param {Cartesian3} result The object into which to store the result.
  278. * @returns {Cartesian3} A cartesian with the minimum components.
  279. */
  280. Cartesian3.minimumByComponent = function (first, second, result) {
  281. //>>includeStart('debug', pragmas.debug);
  282. RuntimeError.Check.typeOf.object("first", first);
  283. RuntimeError.Check.typeOf.object("second", second);
  284. RuntimeError.Check.typeOf.object("result", result);
  285. //>>includeEnd('debug');
  286. result.x = Math.min(first.x, second.x);
  287. result.y = Math.min(first.y, second.y);
  288. result.z = Math.min(first.z, second.z);
  289. return result;
  290. };
  291. /**
  292. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  293. *
  294. * @param {Cartesian3} first A cartesian to compare.
  295. * @param {Cartesian3} second A cartesian to compare.
  296. * @param {Cartesian3} result The object into which to store the result.
  297. * @returns {Cartesian3} A cartesian with the maximum components.
  298. */
  299. Cartesian3.maximumByComponent = function (first, second, result) {
  300. //>>includeStart('debug', pragmas.debug);
  301. RuntimeError.Check.typeOf.object("first", first);
  302. RuntimeError.Check.typeOf.object("second", second);
  303. RuntimeError.Check.typeOf.object("result", result);
  304. //>>includeEnd('debug');
  305. result.x = Math.max(first.x, second.x);
  306. result.y = Math.max(first.y, second.y);
  307. result.z = Math.max(first.z, second.z);
  308. return result;
  309. };
  310. /**
  311. * Constrain a value to lie between two values.
  312. *
  313. * @param {Cartesian3} cartesian The value to clamp.
  314. * @param {Cartesian3} min The minimum bound.
  315. * @param {Cartesian3} max The maximum bound.
  316. * @param {Cartesian3} result The object into which to store the result.
  317. * @returns {Cartesian3} The clamped value such that min <= value <= max.
  318. */
  319. Cartesian3.clamp = function (value, min, max, result) {
  320. //>>includeStart('debug', pragmas.debug);
  321. RuntimeError.Check.typeOf.object("value", value);
  322. RuntimeError.Check.typeOf.object("min", min);
  323. RuntimeError.Check.typeOf.object("max", max);
  324. RuntimeError.Check.typeOf.object("result", result);
  325. //>>includeEnd('debug');
  326. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  327. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  328. const z = ComponentDatatype.CesiumMath.clamp(value.z, min.z, max.z);
  329. result.x = x;
  330. result.y = y;
  331. result.z = z;
  332. return result;
  333. };
  334. /**
  335. * Computes the provided Cartesian's squared magnitude.
  336. *
  337. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  338. * @returns {Number} The squared magnitude.
  339. */
  340. Cartesian3.magnitudeSquared = function (cartesian) {
  341. //>>includeStart('debug', pragmas.debug);
  342. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  343. //>>includeEnd('debug');
  344. return (
  345. cartesian.x * cartesian.x +
  346. cartesian.y * cartesian.y +
  347. cartesian.z * cartesian.z
  348. );
  349. };
  350. /**
  351. * Computes the Cartesian's magnitude (length).
  352. *
  353. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  354. * @returns {Number} The magnitude.
  355. */
  356. Cartesian3.magnitude = function (cartesian) {
  357. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  358. };
  359. const distanceScratch$2 = new Cartesian3();
  360. /**
  361. * Computes the distance between two points.
  362. *
  363. * @param {Cartesian3} left The first point to compute the distance from.
  364. * @param {Cartesian3} right The second point to compute the distance to.
  365. * @returns {Number} The distance between two points.
  366. *
  367. * @example
  368. * // Returns 1.0
  369. * const d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  370. */
  371. Cartesian3.distance = function (left, right) {
  372. //>>includeStart('debug', pragmas.debug);
  373. RuntimeError.Check.typeOf.object("left", left);
  374. RuntimeError.Check.typeOf.object("right", right);
  375. //>>includeEnd('debug');
  376. Cartesian3.subtract(left, right, distanceScratch$2);
  377. return Cartesian3.magnitude(distanceScratch$2);
  378. };
  379. /**
  380. * Computes the squared distance between two points. Comparing squared distances
  381. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  382. *
  383. * @param {Cartesian3} left The first point to compute the distance from.
  384. * @param {Cartesian3} right The second point to compute the distance to.
  385. * @returns {Number} The distance between two points.
  386. *
  387. * @example
  388. * // Returns 4.0, not 2.0
  389. * const d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  390. */
  391. Cartesian3.distanceSquared = function (left, right) {
  392. //>>includeStart('debug', pragmas.debug);
  393. RuntimeError.Check.typeOf.object("left", left);
  394. RuntimeError.Check.typeOf.object("right", right);
  395. //>>includeEnd('debug');
  396. Cartesian3.subtract(left, right, distanceScratch$2);
  397. return Cartesian3.magnitudeSquared(distanceScratch$2);
  398. };
  399. /**
  400. * Computes the normalized form of the supplied Cartesian.
  401. *
  402. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  403. * @param {Cartesian3} result The object onto which to store the result.
  404. * @returns {Cartesian3} The modified result parameter.
  405. */
  406. Cartesian3.normalize = function (cartesian, result) {
  407. //>>includeStart('debug', pragmas.debug);
  408. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  409. RuntimeError.Check.typeOf.object("result", result);
  410. //>>includeEnd('debug');
  411. const magnitude = Cartesian3.magnitude(cartesian);
  412. result.x = cartesian.x / magnitude;
  413. result.y = cartesian.y / magnitude;
  414. result.z = cartesian.z / magnitude;
  415. //>>includeStart('debug', pragmas.debug);
  416. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  417. throw new RuntimeError.DeveloperError("normalized result is not a number");
  418. }
  419. //>>includeEnd('debug');
  420. return result;
  421. };
  422. /**
  423. * Computes the dot (scalar) product of two Cartesians.
  424. *
  425. * @param {Cartesian3} left The first Cartesian.
  426. * @param {Cartesian3} right The second Cartesian.
  427. * @returns {Number} The dot product.
  428. */
  429. Cartesian3.dot = function (left, right) {
  430. //>>includeStart('debug', pragmas.debug);
  431. RuntimeError.Check.typeOf.object("left", left);
  432. RuntimeError.Check.typeOf.object("right", right);
  433. //>>includeEnd('debug');
  434. return left.x * right.x + left.y * right.y + left.z * right.z;
  435. };
  436. /**
  437. * Computes the componentwise product of two Cartesians.
  438. *
  439. * @param {Cartesian3} left The first Cartesian.
  440. * @param {Cartesian3} right The second Cartesian.
  441. * @param {Cartesian3} result The object onto which to store the result.
  442. * @returns {Cartesian3} The modified result parameter.
  443. */
  444. Cartesian3.multiplyComponents = function (left, right, result) {
  445. //>>includeStart('debug', pragmas.debug);
  446. RuntimeError.Check.typeOf.object("left", left);
  447. RuntimeError.Check.typeOf.object("right", right);
  448. RuntimeError.Check.typeOf.object("result", result);
  449. //>>includeEnd('debug');
  450. result.x = left.x * right.x;
  451. result.y = left.y * right.y;
  452. result.z = left.z * right.z;
  453. return result;
  454. };
  455. /**
  456. * Computes the componentwise quotient of two Cartesians.
  457. *
  458. * @param {Cartesian3} left The first Cartesian.
  459. * @param {Cartesian3} right The second Cartesian.
  460. * @param {Cartesian3} result The object onto which to store the result.
  461. * @returns {Cartesian3} The modified result parameter.
  462. */
  463. Cartesian3.divideComponents = function (left, right, result) {
  464. //>>includeStart('debug', pragmas.debug);
  465. RuntimeError.Check.typeOf.object("left", left);
  466. RuntimeError.Check.typeOf.object("right", right);
  467. RuntimeError.Check.typeOf.object("result", result);
  468. //>>includeEnd('debug');
  469. result.x = left.x / right.x;
  470. result.y = left.y / right.y;
  471. result.z = left.z / right.z;
  472. return result;
  473. };
  474. /**
  475. * Computes the componentwise sum of two Cartesians.
  476. *
  477. * @param {Cartesian3} left The first Cartesian.
  478. * @param {Cartesian3} right The second Cartesian.
  479. * @param {Cartesian3} result The object onto which to store the result.
  480. * @returns {Cartesian3} The modified result parameter.
  481. */
  482. Cartesian3.add = function (left, right, result) {
  483. //>>includeStart('debug', pragmas.debug);
  484. RuntimeError.Check.typeOf.object("left", left);
  485. RuntimeError.Check.typeOf.object("right", right);
  486. RuntimeError.Check.typeOf.object("result", result);
  487. //>>includeEnd('debug');
  488. result.x = left.x + right.x;
  489. result.y = left.y + right.y;
  490. result.z = left.z + right.z;
  491. return result;
  492. };
  493. /**
  494. * Computes the componentwise difference of two Cartesians.
  495. *
  496. * @param {Cartesian3} left The first Cartesian.
  497. * @param {Cartesian3} right The second Cartesian.
  498. * @param {Cartesian3} result The object onto which to store the result.
  499. * @returns {Cartesian3} The modified result parameter.
  500. */
  501. Cartesian3.subtract = function (left, right, result) {
  502. //>>includeStart('debug', pragmas.debug);
  503. RuntimeError.Check.typeOf.object("left", left);
  504. RuntimeError.Check.typeOf.object("right", right);
  505. RuntimeError.Check.typeOf.object("result", result);
  506. //>>includeEnd('debug');
  507. result.x = left.x - right.x;
  508. result.y = left.y - right.y;
  509. result.z = left.z - right.z;
  510. return result;
  511. };
  512. /**
  513. * Multiplies the provided Cartesian componentwise by the provided scalar.
  514. *
  515. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  516. * @param {Number} scalar The scalar to multiply with.
  517. * @param {Cartesian3} result The object onto which to store the result.
  518. * @returns {Cartesian3} The modified result parameter.
  519. */
  520. Cartesian3.multiplyByScalar = function (cartesian, scalar, result) {
  521. //>>includeStart('debug', pragmas.debug);
  522. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  523. RuntimeError.Check.typeOf.number("scalar", scalar);
  524. RuntimeError.Check.typeOf.object("result", result);
  525. //>>includeEnd('debug');
  526. result.x = cartesian.x * scalar;
  527. result.y = cartesian.y * scalar;
  528. result.z = cartesian.z * scalar;
  529. return result;
  530. };
  531. /**
  532. * Divides the provided Cartesian componentwise by the provided scalar.
  533. *
  534. * @param {Cartesian3} cartesian The Cartesian to be divided.
  535. * @param {Number} scalar The scalar to divide by.
  536. * @param {Cartesian3} result The object onto which to store the result.
  537. * @returns {Cartesian3} The modified result parameter.
  538. */
  539. Cartesian3.divideByScalar = function (cartesian, scalar, result) {
  540. //>>includeStart('debug', pragmas.debug);
  541. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  542. RuntimeError.Check.typeOf.number("scalar", scalar);
  543. RuntimeError.Check.typeOf.object("result", result);
  544. //>>includeEnd('debug');
  545. result.x = cartesian.x / scalar;
  546. result.y = cartesian.y / scalar;
  547. result.z = cartesian.z / scalar;
  548. return result;
  549. };
  550. /**
  551. * Negates the provided Cartesian.
  552. *
  553. * @param {Cartesian3} cartesian The Cartesian to be negated.
  554. * @param {Cartesian3} result The object onto which to store the result.
  555. * @returns {Cartesian3} The modified result parameter.
  556. */
  557. Cartesian3.negate = function (cartesian, result) {
  558. //>>includeStart('debug', pragmas.debug);
  559. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  560. RuntimeError.Check.typeOf.object("result", result);
  561. //>>includeEnd('debug');
  562. result.x = -cartesian.x;
  563. result.y = -cartesian.y;
  564. result.z = -cartesian.z;
  565. return result;
  566. };
  567. /**
  568. * Computes the absolute value of the provided Cartesian.
  569. *
  570. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  571. * @param {Cartesian3} result The object onto which to store the result.
  572. * @returns {Cartesian3} The modified result parameter.
  573. */
  574. Cartesian3.abs = function (cartesian, result) {
  575. //>>includeStart('debug', pragmas.debug);
  576. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  577. RuntimeError.Check.typeOf.object("result", result);
  578. //>>includeEnd('debug');
  579. result.x = Math.abs(cartesian.x);
  580. result.y = Math.abs(cartesian.y);
  581. result.z = Math.abs(cartesian.z);
  582. return result;
  583. };
  584. const lerpScratch$2 = new Cartesian3();
  585. /**
  586. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  587. *
  588. * @param {Cartesian3} start The value corresponding to t at 0.0.
  589. * @param {Cartesian3} end The value corresponding to t at 1.0.
  590. * @param {Number} t The point along t at which to interpolate.
  591. * @param {Cartesian3} result The object onto which to store the result.
  592. * @returns {Cartesian3} The modified result parameter.
  593. */
  594. Cartesian3.lerp = function (start, end, t, result) {
  595. //>>includeStart('debug', pragmas.debug);
  596. RuntimeError.Check.typeOf.object("start", start);
  597. RuntimeError.Check.typeOf.object("end", end);
  598. RuntimeError.Check.typeOf.number("t", t);
  599. RuntimeError.Check.typeOf.object("result", result);
  600. //>>includeEnd('debug');
  601. Cartesian3.multiplyByScalar(end, t, lerpScratch$2);
  602. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  603. return Cartesian3.add(lerpScratch$2, result, result);
  604. };
  605. const angleBetweenScratch$1 = new Cartesian3();
  606. const angleBetweenScratch2$1 = new Cartesian3();
  607. /**
  608. * Returns the angle, in radians, between the provided Cartesians.
  609. *
  610. * @param {Cartesian3} left The first Cartesian.
  611. * @param {Cartesian3} right The second Cartesian.
  612. * @returns {Number} The angle between the Cartesians.
  613. */
  614. Cartesian3.angleBetween = function (left, right) {
  615. //>>includeStart('debug', pragmas.debug);
  616. RuntimeError.Check.typeOf.object("left", left);
  617. RuntimeError.Check.typeOf.object("right", right);
  618. //>>includeEnd('debug');
  619. Cartesian3.normalize(left, angleBetweenScratch$1);
  620. Cartesian3.normalize(right, angleBetweenScratch2$1);
  621. const cosine = Cartesian3.dot(angleBetweenScratch$1, angleBetweenScratch2$1);
  622. const sine = Cartesian3.magnitude(
  623. Cartesian3.cross(
  624. angleBetweenScratch$1,
  625. angleBetweenScratch2$1,
  626. angleBetweenScratch$1
  627. )
  628. );
  629. return Math.atan2(sine, cosine);
  630. };
  631. const mostOrthogonalAxisScratch$2 = new Cartesian3();
  632. /**
  633. * Returns the axis that is most orthogonal to the provided Cartesian.
  634. *
  635. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  636. * @param {Cartesian3} result The object onto which to store the result.
  637. * @returns {Cartesian3} The most orthogonal axis.
  638. */
  639. Cartesian3.mostOrthogonalAxis = function (cartesian, result) {
  640. //>>includeStart('debug', pragmas.debug);
  641. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  642. RuntimeError.Check.typeOf.object("result", result);
  643. //>>includeEnd('debug');
  644. const f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch$2);
  645. Cartesian3.abs(f, f);
  646. if (f.x <= f.y) {
  647. if (f.x <= f.z) {
  648. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  649. } else {
  650. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  651. }
  652. } else if (f.y <= f.z) {
  653. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  654. } else {
  655. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  656. }
  657. return result;
  658. };
  659. /**
  660. * Projects vector a onto vector b
  661. * @param {Cartesian3} a The vector that needs projecting
  662. * @param {Cartesian3} b The vector to project onto
  663. * @param {Cartesian3} result The result cartesian
  664. * @returns {Cartesian3} The modified result parameter
  665. */
  666. Cartesian3.projectVector = function (a, b, result) {
  667. //>>includeStart('debug', pragmas.debug);
  668. RuntimeError.Check.defined("a", a);
  669. RuntimeError.Check.defined("b", b);
  670. RuntimeError.Check.defined("result", result);
  671. //>>includeEnd('debug');
  672. const scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
  673. return Cartesian3.multiplyByScalar(b, scalar, result);
  674. };
  675. /**
  676. * Compares the provided Cartesians componentwise and returns
  677. * <code>true</code> if they are equal, <code>false</code> otherwise.
  678. *
  679. * @param {Cartesian3} [left] The first Cartesian.
  680. * @param {Cartesian3} [right] The second Cartesian.
  681. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  682. */
  683. Cartesian3.equals = function (left, right) {
  684. return (
  685. left === right ||
  686. (defaultValue.defined(left) &&
  687. defaultValue.defined(right) &&
  688. left.x === right.x &&
  689. left.y === right.y &&
  690. left.z === right.z)
  691. );
  692. };
  693. /**
  694. * @private
  695. */
  696. Cartesian3.equalsArray = function (cartesian, array, offset) {
  697. return (
  698. cartesian.x === array[offset] &&
  699. cartesian.y === array[offset + 1] &&
  700. cartesian.z === array[offset + 2]
  701. );
  702. };
  703. /**
  704. * Compares the provided Cartesians componentwise and returns
  705. * <code>true</code> if they pass an absolute or relative tolerance test,
  706. * <code>false</code> otherwise.
  707. *
  708. * @param {Cartesian3} [left] The first Cartesian.
  709. * @param {Cartesian3} [right] The second Cartesian.
  710. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  711. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  712. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  713. */
  714. Cartesian3.equalsEpsilon = function (
  715. left,
  716. right,
  717. relativeEpsilon,
  718. absoluteEpsilon
  719. ) {
  720. return (
  721. left === right ||
  722. (defaultValue.defined(left) &&
  723. defaultValue.defined(right) &&
  724. ComponentDatatype.CesiumMath.equalsEpsilon(
  725. left.x,
  726. right.x,
  727. relativeEpsilon,
  728. absoluteEpsilon
  729. ) &&
  730. ComponentDatatype.CesiumMath.equalsEpsilon(
  731. left.y,
  732. right.y,
  733. relativeEpsilon,
  734. absoluteEpsilon
  735. ) &&
  736. ComponentDatatype.CesiumMath.equalsEpsilon(
  737. left.z,
  738. right.z,
  739. relativeEpsilon,
  740. absoluteEpsilon
  741. ))
  742. );
  743. };
  744. /**
  745. * Computes the cross (outer) product of two Cartesians.
  746. *
  747. * @param {Cartesian3} left The first Cartesian.
  748. * @param {Cartesian3} right The second Cartesian.
  749. * @param {Cartesian3} result The object onto which to store the result.
  750. * @returns {Cartesian3} The cross product.
  751. */
  752. Cartesian3.cross = function (left, right, result) {
  753. //>>includeStart('debug', pragmas.debug);
  754. RuntimeError.Check.typeOf.object("left", left);
  755. RuntimeError.Check.typeOf.object("right", right);
  756. RuntimeError.Check.typeOf.object("result", result);
  757. //>>includeEnd('debug');
  758. const leftX = left.x;
  759. const leftY = left.y;
  760. const leftZ = left.z;
  761. const rightX = right.x;
  762. const rightY = right.y;
  763. const rightZ = right.z;
  764. const x = leftY * rightZ - leftZ * rightY;
  765. const y = leftZ * rightX - leftX * rightZ;
  766. const z = leftX * rightY - leftY * rightX;
  767. result.x = x;
  768. result.y = y;
  769. result.z = z;
  770. return result;
  771. };
  772. /**
  773. * Computes the midpoint between the right and left Cartesian.
  774. * @param {Cartesian3} left The first Cartesian.
  775. * @param {Cartesian3} right The second Cartesian.
  776. * @param {Cartesian3} result The object onto which to store the result.
  777. * @returns {Cartesian3} The midpoint.
  778. */
  779. Cartesian3.midpoint = function (left, right, result) {
  780. //>>includeStart('debug', pragmas.debug);
  781. RuntimeError.Check.typeOf.object("left", left);
  782. RuntimeError.Check.typeOf.object("right", right);
  783. RuntimeError.Check.typeOf.object("result", result);
  784. //>>includeEnd('debug');
  785. result.x = (left.x + right.x) * 0.5;
  786. result.y = (left.y + right.y) * 0.5;
  787. result.z = (left.z + right.z) * 0.5;
  788. return result;
  789. };
  790. /**
  791. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  792. *
  793. * @param {Number} longitude The longitude, in degrees
  794. * @param {Number} latitude The latitude, in degrees
  795. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  796. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  797. * @param {Cartesian3} [result] The object onto which to store the result.
  798. * @returns {Cartesian3} The position
  799. *
  800. * @example
  801. * const position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  802. */
  803. Cartesian3.fromDegrees = function (
  804. longitude,
  805. latitude,
  806. height,
  807. ellipsoid,
  808. result
  809. ) {
  810. //>>includeStart('debug', pragmas.debug);
  811. RuntimeError.Check.typeOf.number("longitude", longitude);
  812. RuntimeError.Check.typeOf.number("latitude", latitude);
  813. //>>includeEnd('debug');
  814. longitude = ComponentDatatype.CesiumMath.toRadians(longitude);
  815. latitude = ComponentDatatype.CesiumMath.toRadians(latitude);
  816. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  817. };
  818. let scratchN = new Cartesian3();
  819. let scratchK = new Cartesian3();
  820. const wgs84RadiiSquared = new Cartesian3(
  821. 6378137.0 * 6378137.0,
  822. 6378137.0 * 6378137.0,
  823. 6356752.3142451793 * 6356752.3142451793
  824. );
  825. /**
  826. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  827. *
  828. * @param {Number} longitude The longitude, in radians
  829. * @param {Number} latitude The latitude, in radians
  830. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  831. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  832. * @param {Cartesian3} [result] The object onto which to store the result.
  833. * @returns {Cartesian3} The position
  834. *
  835. * @example
  836. * const position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  837. */
  838. Cartesian3.fromRadians = function (
  839. longitude,
  840. latitude,
  841. height,
  842. ellipsoid,
  843. result
  844. ) {
  845. //>>includeStart('debug', pragmas.debug);
  846. RuntimeError.Check.typeOf.number("longitude", longitude);
  847. RuntimeError.Check.typeOf.number("latitude", latitude);
  848. //>>includeEnd('debug');
  849. height = defaultValue.defaultValue(height, 0.0);
  850. const radiiSquared = defaultValue.defined(ellipsoid)
  851. ? ellipsoid.radiiSquared
  852. : wgs84RadiiSquared;
  853. const cosLatitude = Math.cos(latitude);
  854. scratchN.x = cosLatitude * Math.cos(longitude);
  855. scratchN.y = cosLatitude * Math.sin(longitude);
  856. scratchN.z = Math.sin(latitude);
  857. scratchN = Cartesian3.normalize(scratchN, scratchN);
  858. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  859. const gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  860. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  861. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  862. if (!defaultValue.defined(result)) {
  863. result = new Cartesian3();
  864. }
  865. return Cartesian3.add(scratchK, scratchN, result);
  866. };
  867. /**
  868. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  869. *
  870. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  871. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  872. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  873. * @returns {Cartesian3[]} The array of positions.
  874. *
  875. * @example
  876. * const positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  877. */
  878. Cartesian3.fromDegreesArray = function (coordinates, ellipsoid, result) {
  879. //>>includeStart('debug', pragmas.debug);
  880. RuntimeError.Check.defined("coordinates", coordinates);
  881. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  882. throw new RuntimeError.DeveloperError(
  883. "the number of coordinates must be a multiple of 2 and at least 2"
  884. );
  885. }
  886. //>>includeEnd('debug');
  887. const length = coordinates.length;
  888. if (!defaultValue.defined(result)) {
  889. result = new Array(length / 2);
  890. } else {
  891. result.length = length / 2;
  892. }
  893. for (let i = 0; i < length; i += 2) {
  894. const longitude = coordinates[i];
  895. const latitude = coordinates[i + 1];
  896. const index = i / 2;
  897. result[index] = Cartesian3.fromDegrees(
  898. longitude,
  899. latitude,
  900. 0,
  901. ellipsoid,
  902. result[index]
  903. );
  904. }
  905. return result;
  906. };
  907. /**
  908. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  909. *
  910. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  911. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  912. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  913. * @returns {Cartesian3[]} The array of positions.
  914. *
  915. * @example
  916. * const positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  917. */
  918. Cartesian3.fromRadiansArray = function (coordinates, ellipsoid, result) {
  919. //>>includeStart('debug', pragmas.debug);
  920. RuntimeError.Check.defined("coordinates", coordinates);
  921. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  922. throw new RuntimeError.DeveloperError(
  923. "the number of coordinates must be a multiple of 2 and at least 2"
  924. );
  925. }
  926. //>>includeEnd('debug');
  927. const length = coordinates.length;
  928. if (!defaultValue.defined(result)) {
  929. result = new Array(length / 2);
  930. } else {
  931. result.length = length / 2;
  932. }
  933. for (let i = 0; i < length; i += 2) {
  934. const longitude = coordinates[i];
  935. const latitude = coordinates[i + 1];
  936. const index = i / 2;
  937. result[index] = Cartesian3.fromRadians(
  938. longitude,
  939. latitude,
  940. 0,
  941. ellipsoid,
  942. result[index]
  943. );
  944. }
  945. return result;
  946. };
  947. /**
  948. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  949. *
  950. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  951. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  952. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  953. * @returns {Cartesian3[]} The array of positions.
  954. *
  955. * @example
  956. * const positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  957. */
  958. Cartesian3.fromDegreesArrayHeights = function (coordinates, ellipsoid, result) {
  959. //>>includeStart('debug', pragmas.debug);
  960. RuntimeError.Check.defined("coordinates", coordinates);
  961. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  962. throw new RuntimeError.DeveloperError(
  963. "the number of coordinates must be a multiple of 3 and at least 3"
  964. );
  965. }
  966. //>>includeEnd('debug');
  967. const length = coordinates.length;
  968. if (!defaultValue.defined(result)) {
  969. result = new Array(length / 3);
  970. } else {
  971. result.length = length / 3;
  972. }
  973. for (let i = 0; i < length; i += 3) {
  974. const longitude = coordinates[i];
  975. const latitude = coordinates[i + 1];
  976. const height = coordinates[i + 2];
  977. const index = i / 3;
  978. result[index] = Cartesian3.fromDegrees(
  979. longitude,
  980. latitude,
  981. height,
  982. ellipsoid,
  983. result[index]
  984. );
  985. }
  986. return result;
  987. };
  988. /**
  989. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  990. *
  991. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  992. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  993. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  994. * @returns {Cartesian3[]} The array of positions.
  995. *
  996. * @example
  997. * const positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  998. */
  999. Cartesian3.fromRadiansArrayHeights = function (coordinates, ellipsoid, result) {
  1000. //>>includeStart('debug', pragmas.debug);
  1001. RuntimeError.Check.defined("coordinates", coordinates);
  1002. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  1003. throw new RuntimeError.DeveloperError(
  1004. "the number of coordinates must be a multiple of 3 and at least 3"
  1005. );
  1006. }
  1007. //>>includeEnd('debug');
  1008. const length = coordinates.length;
  1009. if (!defaultValue.defined(result)) {
  1010. result = new Array(length / 3);
  1011. } else {
  1012. result.length = length / 3;
  1013. }
  1014. for (let i = 0; i < length; i += 3) {
  1015. const longitude = coordinates[i];
  1016. const latitude = coordinates[i + 1];
  1017. const height = coordinates[i + 2];
  1018. const index = i / 3;
  1019. result[index] = Cartesian3.fromRadians(
  1020. longitude,
  1021. latitude,
  1022. height,
  1023. ellipsoid,
  1024. result[index]
  1025. );
  1026. }
  1027. return result;
  1028. };
  1029. /**
  1030. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  1031. *
  1032. * @type {Cartesian3}
  1033. * @constant
  1034. */
  1035. Cartesian3.ZERO = Object.freeze(new Cartesian3(0.0, 0.0, 0.0));
  1036. /**
  1037. * An immutable Cartesian3 instance initialized to (1.0, 1.0, 1.0).
  1038. *
  1039. * @type {Cartesian3}
  1040. * @constant
  1041. */
  1042. Cartesian3.ONE = Object.freeze(new Cartesian3(1.0, 1.0, 1.0));
  1043. /**
  1044. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  1045. *
  1046. * @type {Cartesian3}
  1047. * @constant
  1048. */
  1049. Cartesian3.UNIT_X = Object.freeze(new Cartesian3(1.0, 0.0, 0.0));
  1050. /**
  1051. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  1052. *
  1053. * @type {Cartesian3}
  1054. * @constant
  1055. */
  1056. Cartesian3.UNIT_Y = Object.freeze(new Cartesian3(0.0, 1.0, 0.0));
  1057. /**
  1058. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  1059. *
  1060. * @type {Cartesian3}
  1061. * @constant
  1062. */
  1063. Cartesian3.UNIT_Z = Object.freeze(new Cartesian3(0.0, 0.0, 1.0));
  1064. /**
  1065. * Duplicates this Cartesian3 instance.
  1066. *
  1067. * @param {Cartesian3} [result] The object onto which to store the result.
  1068. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1069. */
  1070. Cartesian3.prototype.clone = function (result) {
  1071. return Cartesian3.clone(this, result);
  1072. };
  1073. /**
  1074. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1075. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1076. *
  1077. * @param {Cartesian3} [right] The right hand side Cartesian.
  1078. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1079. */
  1080. Cartesian3.prototype.equals = function (right) {
  1081. return Cartesian3.equals(this, right);
  1082. };
  1083. /**
  1084. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1085. * <code>true</code> if they pass an absolute or relative tolerance test,
  1086. * <code>false</code> otherwise.
  1087. *
  1088. * @param {Cartesian3} [right] The right hand side Cartesian.
  1089. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  1090. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1091. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1092. */
  1093. Cartesian3.prototype.equalsEpsilon = function (
  1094. right,
  1095. relativeEpsilon,
  1096. absoluteEpsilon
  1097. ) {
  1098. return Cartesian3.equalsEpsilon(
  1099. this,
  1100. right,
  1101. relativeEpsilon,
  1102. absoluteEpsilon
  1103. );
  1104. };
  1105. /**
  1106. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  1107. *
  1108. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  1109. */
  1110. Cartesian3.prototype.toString = function () {
  1111. return `(${this.x}, ${this.y}, ${this.z})`;
  1112. };
  1113. const scaleToGeodeticSurfaceIntersection = new Cartesian3();
  1114. const scaleToGeodeticSurfaceGradient = new Cartesian3();
  1115. /**
  1116. * Scales the provided Cartesian position along the geodetic surface normal
  1117. * so that it is on the surface of this ellipsoid. If the position is
  1118. * at the center of the ellipsoid, this function returns undefined.
  1119. *
  1120. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1121. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  1122. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  1123. * @param {Number} centerToleranceSquared Tolerance for closeness to the center.
  1124. * @param {Cartesian3} [result] The object onto which to store the result.
  1125. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1126. *
  1127. * @function scaleToGeodeticSurface
  1128. *
  1129. * @private
  1130. */
  1131. function scaleToGeodeticSurface(
  1132. cartesian,
  1133. oneOverRadii,
  1134. oneOverRadiiSquared,
  1135. centerToleranceSquared,
  1136. result
  1137. ) {
  1138. //>>includeStart('debug', pragmas.debug);
  1139. if (!defaultValue.defined(cartesian)) {
  1140. throw new RuntimeError.DeveloperError("cartesian is required.");
  1141. }
  1142. if (!defaultValue.defined(oneOverRadii)) {
  1143. throw new RuntimeError.DeveloperError("oneOverRadii is required.");
  1144. }
  1145. if (!defaultValue.defined(oneOverRadiiSquared)) {
  1146. throw new RuntimeError.DeveloperError("oneOverRadiiSquared is required.");
  1147. }
  1148. if (!defaultValue.defined(centerToleranceSquared)) {
  1149. throw new RuntimeError.DeveloperError("centerToleranceSquared is required.");
  1150. }
  1151. //>>includeEnd('debug');
  1152. const positionX = cartesian.x;
  1153. const positionY = cartesian.y;
  1154. const positionZ = cartesian.z;
  1155. const oneOverRadiiX = oneOverRadii.x;
  1156. const oneOverRadiiY = oneOverRadii.y;
  1157. const oneOverRadiiZ = oneOverRadii.z;
  1158. const x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  1159. const y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  1160. const z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  1161. // Compute the squared ellipsoid norm.
  1162. const squaredNorm = x2 + y2 + z2;
  1163. const ratio = Math.sqrt(1.0 / squaredNorm);
  1164. // As an initial approximation, assume that the radial intersection is the projection point.
  1165. const intersection = Cartesian3.multiplyByScalar(
  1166. cartesian,
  1167. ratio,
  1168. scaleToGeodeticSurfaceIntersection
  1169. );
  1170. // If the position is near the center, the iteration will not converge.
  1171. if (squaredNorm < centerToleranceSquared) {
  1172. return !isFinite(ratio)
  1173. ? undefined
  1174. : Cartesian3.clone(intersection, result);
  1175. }
  1176. const oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  1177. const oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  1178. const oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  1179. // Use the gradient at the intersection point in place of the true unit normal.
  1180. // The difference in magnitude will be absorbed in the multiplier.
  1181. const gradient = scaleToGeodeticSurfaceGradient;
  1182. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  1183. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  1184. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  1185. // Compute the initial guess at the normal vector multiplier, lambda.
  1186. let lambda =
  1187. ((1.0 - ratio) * Cartesian3.magnitude(cartesian)) /
  1188. (0.5 * Cartesian3.magnitude(gradient));
  1189. let correction = 0.0;
  1190. let func;
  1191. let denominator;
  1192. let xMultiplier;
  1193. let yMultiplier;
  1194. let zMultiplier;
  1195. let xMultiplier2;
  1196. let yMultiplier2;
  1197. let zMultiplier2;
  1198. let xMultiplier3;
  1199. let yMultiplier3;
  1200. let zMultiplier3;
  1201. do {
  1202. lambda -= correction;
  1203. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  1204. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  1205. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  1206. xMultiplier2 = xMultiplier * xMultiplier;
  1207. yMultiplier2 = yMultiplier * yMultiplier;
  1208. zMultiplier2 = zMultiplier * zMultiplier;
  1209. xMultiplier3 = xMultiplier2 * xMultiplier;
  1210. yMultiplier3 = yMultiplier2 * yMultiplier;
  1211. zMultiplier3 = zMultiplier2 * zMultiplier;
  1212. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  1213. // "denominator" here refers to the use of this expression in the velocity and acceleration
  1214. // computations in the sections to follow.
  1215. denominator =
  1216. x2 * xMultiplier3 * oneOverRadiiSquaredX +
  1217. y2 * yMultiplier3 * oneOverRadiiSquaredY +
  1218. z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  1219. const derivative = -2.0 * denominator;
  1220. correction = func / derivative;
  1221. } while (Math.abs(func) > ComponentDatatype.CesiumMath.EPSILON12);
  1222. if (!defaultValue.defined(result)) {
  1223. return new Cartesian3(
  1224. positionX * xMultiplier,
  1225. positionY * yMultiplier,
  1226. positionZ * zMultiplier
  1227. );
  1228. }
  1229. result.x = positionX * xMultiplier;
  1230. result.y = positionY * yMultiplier;
  1231. result.z = positionZ * zMultiplier;
  1232. return result;
  1233. }
  1234. /**
  1235. * A position defined by longitude, latitude, and height.
  1236. * @alias Cartographic
  1237. * @constructor
  1238. *
  1239. * @param {Number} [longitude=0.0] The longitude, in radians.
  1240. * @param {Number} [latitude=0.0] The latitude, in radians.
  1241. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1242. *
  1243. * @see Ellipsoid
  1244. */
  1245. function Cartographic(longitude, latitude, height) {
  1246. /**
  1247. * The longitude, in radians.
  1248. * @type {Number}
  1249. * @default 0.0
  1250. */
  1251. this.longitude = defaultValue.defaultValue(longitude, 0.0);
  1252. /**
  1253. * The latitude, in radians.
  1254. * @type {Number}
  1255. * @default 0.0
  1256. */
  1257. this.latitude = defaultValue.defaultValue(latitude, 0.0);
  1258. /**
  1259. * The height, in meters, above the ellipsoid.
  1260. * @type {Number}
  1261. * @default 0.0
  1262. */
  1263. this.height = defaultValue.defaultValue(height, 0.0);
  1264. }
  1265. /**
  1266. * Creates a new Cartographic instance from longitude and latitude
  1267. * specified in radians.
  1268. *
  1269. * @param {Number} longitude The longitude, in radians.
  1270. * @param {Number} latitude The latitude, in radians.
  1271. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1272. * @param {Cartographic} [result] The object onto which to store the result.
  1273. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1274. */
  1275. Cartographic.fromRadians = function (longitude, latitude, height, result) {
  1276. //>>includeStart('debug', pragmas.debug);
  1277. RuntimeError.Check.typeOf.number("longitude", longitude);
  1278. RuntimeError.Check.typeOf.number("latitude", latitude);
  1279. //>>includeEnd('debug');
  1280. height = defaultValue.defaultValue(height, 0.0);
  1281. if (!defaultValue.defined(result)) {
  1282. return new Cartographic(longitude, latitude, height);
  1283. }
  1284. result.longitude = longitude;
  1285. result.latitude = latitude;
  1286. result.height = height;
  1287. return result;
  1288. };
  1289. /**
  1290. * Creates a new Cartographic instance from longitude and latitude
  1291. * specified in degrees. The values in the resulting object will
  1292. * be in radians.
  1293. *
  1294. * @param {Number} longitude The longitude, in degrees.
  1295. * @param {Number} latitude The latitude, in degrees.
  1296. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1297. * @param {Cartographic} [result] The object onto which to store the result.
  1298. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1299. */
  1300. Cartographic.fromDegrees = function (longitude, latitude, height, result) {
  1301. //>>includeStart('debug', pragmas.debug);
  1302. RuntimeError.Check.typeOf.number("longitude", longitude);
  1303. RuntimeError.Check.typeOf.number("latitude", latitude);
  1304. //>>includeEnd('debug');
  1305. longitude = ComponentDatatype.CesiumMath.toRadians(longitude);
  1306. latitude = ComponentDatatype.CesiumMath.toRadians(latitude);
  1307. return Cartographic.fromRadians(longitude, latitude, height, result);
  1308. };
  1309. const cartesianToCartographicN$1 = new Cartesian3();
  1310. const cartesianToCartographicP$1 = new Cartesian3();
  1311. const cartesianToCartographicH$1 = new Cartesian3();
  1312. const wgs84OneOverRadii = new Cartesian3(
  1313. 1.0 / 6378137.0,
  1314. 1.0 / 6378137.0,
  1315. 1.0 / 6356752.3142451793
  1316. );
  1317. const wgs84OneOverRadiiSquared = new Cartesian3(
  1318. 1.0 / (6378137.0 * 6378137.0),
  1319. 1.0 / (6378137.0 * 6378137.0),
  1320. 1.0 / (6356752.3142451793 * 6356752.3142451793)
  1321. );
  1322. const wgs84CenterToleranceSquared = ComponentDatatype.CesiumMath.EPSILON1;
  1323. /**
  1324. * Creates a new Cartographic instance from a Cartesian position. The values in the
  1325. * resulting object will be in radians.
  1326. *
  1327. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1328. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1329. * @param {Cartographic} [result] The object onto which to store the result.
  1330. * @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.
  1331. */
  1332. Cartographic.fromCartesian = function (cartesian, ellipsoid, result) {
  1333. const oneOverRadii = defaultValue.defined(ellipsoid)
  1334. ? ellipsoid.oneOverRadii
  1335. : wgs84OneOverRadii;
  1336. const oneOverRadiiSquared = defaultValue.defined(ellipsoid)
  1337. ? ellipsoid.oneOverRadiiSquared
  1338. : wgs84OneOverRadiiSquared;
  1339. const centerToleranceSquared = defaultValue.defined(ellipsoid)
  1340. ? ellipsoid._centerToleranceSquared
  1341. : wgs84CenterToleranceSquared;
  1342. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1343. const p = scaleToGeodeticSurface(
  1344. cartesian,
  1345. oneOverRadii,
  1346. oneOverRadiiSquared,
  1347. centerToleranceSquared,
  1348. cartesianToCartographicP$1
  1349. );
  1350. if (!defaultValue.defined(p)) {
  1351. return undefined;
  1352. }
  1353. let n = Cartesian3.multiplyComponents(
  1354. p,
  1355. oneOverRadiiSquared,
  1356. cartesianToCartographicN$1
  1357. );
  1358. n = Cartesian3.normalize(n, n);
  1359. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH$1);
  1360. const longitude = Math.atan2(n.y, n.x);
  1361. const latitude = Math.asin(n.z);
  1362. const height =
  1363. ComponentDatatype.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1364. if (!defaultValue.defined(result)) {
  1365. return new Cartographic(longitude, latitude, height);
  1366. }
  1367. result.longitude = longitude;
  1368. result.latitude = latitude;
  1369. result.height = height;
  1370. return result;
  1371. };
  1372. /**
  1373. * Creates a new Cartesian3 instance from a Cartographic input. The values in the inputted
  1374. * object should be in radians.
  1375. *
  1376. * @param {Cartographic} cartographic Input to be converted into a Cartesian3 output.
  1377. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1378. * @param {Cartesian3} [result] The object onto which to store the result.
  1379. * @returns {Cartesian3} The position
  1380. */
  1381. Cartographic.toCartesian = function (cartographic, ellipsoid, result) {
  1382. //>>includeStart('debug', pragmas.debug);
  1383. RuntimeError.Check.defined("cartographic", cartographic);
  1384. //>>includeEnd('debug');
  1385. return Cartesian3.fromRadians(
  1386. cartographic.longitude,
  1387. cartographic.latitude,
  1388. cartographic.height,
  1389. ellipsoid,
  1390. result
  1391. );
  1392. };
  1393. /**
  1394. * Duplicates a Cartographic instance.
  1395. *
  1396. * @param {Cartographic} cartographic The cartographic to duplicate.
  1397. * @param {Cartographic} [result] The object onto which to store the result.
  1398. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  1399. */
  1400. Cartographic.clone = function (cartographic, result) {
  1401. if (!defaultValue.defined(cartographic)) {
  1402. return undefined;
  1403. }
  1404. if (!defaultValue.defined(result)) {
  1405. return new Cartographic(
  1406. cartographic.longitude,
  1407. cartographic.latitude,
  1408. cartographic.height
  1409. );
  1410. }
  1411. result.longitude = cartographic.longitude;
  1412. result.latitude = cartographic.latitude;
  1413. result.height = cartographic.height;
  1414. return result;
  1415. };
  1416. /**
  1417. * Compares the provided cartographics componentwise and returns
  1418. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1419. *
  1420. * @param {Cartographic} [left] The first cartographic.
  1421. * @param {Cartographic} [right] The second cartographic.
  1422. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1423. */
  1424. Cartographic.equals = function (left, right) {
  1425. return (
  1426. left === right ||
  1427. (defaultValue.defined(left) &&
  1428. defaultValue.defined(right) &&
  1429. left.longitude === right.longitude &&
  1430. left.latitude === right.latitude &&
  1431. left.height === right.height)
  1432. );
  1433. };
  1434. /**
  1435. * Compares the provided cartographics componentwise and returns
  1436. * <code>true</code> if they are within the provided epsilon,
  1437. * <code>false</code> otherwise.
  1438. *
  1439. * @param {Cartographic} [left] The first cartographic.
  1440. * @param {Cartographic} [right] The second cartographic.
  1441. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1442. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1443. */
  1444. Cartographic.equalsEpsilon = function (left, right, epsilon) {
  1445. epsilon = defaultValue.defaultValue(epsilon, 0);
  1446. return (
  1447. left === right ||
  1448. (defaultValue.defined(left) &&
  1449. defaultValue.defined(right) &&
  1450. Math.abs(left.longitude - right.longitude) <= epsilon &&
  1451. Math.abs(left.latitude - right.latitude) <= epsilon &&
  1452. Math.abs(left.height - right.height) <= epsilon)
  1453. );
  1454. };
  1455. /**
  1456. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  1457. *
  1458. * @type {Cartographic}
  1459. * @constant
  1460. */
  1461. Cartographic.ZERO = Object.freeze(new Cartographic(0.0, 0.0, 0.0));
  1462. /**
  1463. * Duplicates this instance.
  1464. *
  1465. * @param {Cartographic} [result] The object onto which to store the result.
  1466. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1467. */
  1468. Cartographic.prototype.clone = function (result) {
  1469. return Cartographic.clone(this, result);
  1470. };
  1471. /**
  1472. * Compares the provided against this cartographic componentwise and returns
  1473. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1474. *
  1475. * @param {Cartographic} [right] The second cartographic.
  1476. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1477. */
  1478. Cartographic.prototype.equals = function (right) {
  1479. return Cartographic.equals(this, right);
  1480. };
  1481. /**
  1482. * Compares the provided against this cartographic componentwise and returns
  1483. * <code>true</code> if they are within the provided epsilon,
  1484. * <code>false</code> otherwise.
  1485. *
  1486. * @param {Cartographic} [right] The second cartographic.
  1487. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1488. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1489. */
  1490. Cartographic.prototype.equalsEpsilon = function (right, epsilon) {
  1491. return Cartographic.equalsEpsilon(this, right, epsilon);
  1492. };
  1493. /**
  1494. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  1495. *
  1496. * @returns {String} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  1497. */
  1498. Cartographic.prototype.toString = function () {
  1499. return `(${this.longitude}, ${this.latitude}, ${this.height})`;
  1500. };
  1501. function initialize(ellipsoid, x, y, z) {
  1502. x = defaultValue.defaultValue(x, 0.0);
  1503. y = defaultValue.defaultValue(y, 0.0);
  1504. z = defaultValue.defaultValue(z, 0.0);
  1505. //>>includeStart('debug', pragmas.debug);
  1506. RuntimeError.Check.typeOf.number.greaterThanOrEquals("x", x, 0.0);
  1507. RuntimeError.Check.typeOf.number.greaterThanOrEquals("y", y, 0.0);
  1508. RuntimeError.Check.typeOf.number.greaterThanOrEquals("z", z, 0.0);
  1509. //>>includeEnd('debug');
  1510. ellipsoid._radii = new Cartesian3(x, y, z);
  1511. ellipsoid._radiiSquared = new Cartesian3(x * x, y * y, z * z);
  1512. ellipsoid._radiiToTheFourth = new Cartesian3(
  1513. x * x * x * x,
  1514. y * y * y * y,
  1515. z * z * z * z
  1516. );
  1517. ellipsoid._oneOverRadii = new Cartesian3(
  1518. x === 0.0 ? 0.0 : 1.0 / x,
  1519. y === 0.0 ? 0.0 : 1.0 / y,
  1520. z === 0.0 ? 0.0 : 1.0 / z
  1521. );
  1522. ellipsoid._oneOverRadiiSquared = new Cartesian3(
  1523. x === 0.0 ? 0.0 : 1.0 / (x * x),
  1524. y === 0.0 ? 0.0 : 1.0 / (y * y),
  1525. z === 0.0 ? 0.0 : 1.0 / (z * z)
  1526. );
  1527. ellipsoid._minimumRadius = Math.min(x, y, z);
  1528. ellipsoid._maximumRadius = Math.max(x, y, z);
  1529. ellipsoid._centerToleranceSquared = ComponentDatatype.CesiumMath.EPSILON1;
  1530. if (ellipsoid._radiiSquared.z !== 0) {
  1531. ellipsoid._squaredXOverSquaredZ =
  1532. ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  1533. }
  1534. }
  1535. /**
  1536. * A quadratic surface defined in Cartesian coordinates by the equation
  1537. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  1538. * by Cesium to represent the shape of planetary bodies.
  1539. *
  1540. * Rather than constructing this object directly, one of the provided
  1541. * constants is normally used.
  1542. * @alias Ellipsoid
  1543. * @constructor
  1544. *
  1545. * @param {Number} [x=0] The radius in the x direction.
  1546. * @param {Number} [y=0] The radius in the y direction.
  1547. * @param {Number} [z=0] The radius in the z direction.
  1548. *
  1549. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1550. *
  1551. * @see Ellipsoid.fromCartesian3
  1552. * @see Ellipsoid.WGS84
  1553. * @see Ellipsoid.UNIT_SPHERE
  1554. */
  1555. function Ellipsoid(x, y, z) {
  1556. this._radii = undefined;
  1557. this._radiiSquared = undefined;
  1558. this._radiiToTheFourth = undefined;
  1559. this._oneOverRadii = undefined;
  1560. this._oneOverRadiiSquared = undefined;
  1561. this._minimumRadius = undefined;
  1562. this._maximumRadius = undefined;
  1563. this._centerToleranceSquared = undefined;
  1564. this._squaredXOverSquaredZ = undefined;
  1565. initialize(this, x, y, z);
  1566. }
  1567. Object.defineProperties(Ellipsoid.prototype, {
  1568. /**
  1569. * Gets the radii of the ellipsoid.
  1570. * @memberof Ellipsoid.prototype
  1571. * @type {Cartesian3}
  1572. * @readonly
  1573. */
  1574. radii: {
  1575. get: function () {
  1576. return this._radii;
  1577. },
  1578. },
  1579. /**
  1580. * Gets the squared radii of the ellipsoid.
  1581. * @memberof Ellipsoid.prototype
  1582. * @type {Cartesian3}
  1583. * @readonly
  1584. */
  1585. radiiSquared: {
  1586. get: function () {
  1587. return this._radiiSquared;
  1588. },
  1589. },
  1590. /**
  1591. * Gets the radii of the ellipsoid raise to the fourth power.
  1592. * @memberof Ellipsoid.prototype
  1593. * @type {Cartesian3}
  1594. * @readonly
  1595. */
  1596. radiiToTheFourth: {
  1597. get: function () {
  1598. return this._radiiToTheFourth;
  1599. },
  1600. },
  1601. /**
  1602. * Gets one over the radii of the ellipsoid.
  1603. * @memberof Ellipsoid.prototype
  1604. * @type {Cartesian3}
  1605. * @readonly
  1606. */
  1607. oneOverRadii: {
  1608. get: function () {
  1609. return this._oneOverRadii;
  1610. },
  1611. },
  1612. /**
  1613. * Gets one over the squared radii of the ellipsoid.
  1614. * @memberof Ellipsoid.prototype
  1615. * @type {Cartesian3}
  1616. * @readonly
  1617. */
  1618. oneOverRadiiSquared: {
  1619. get: function () {
  1620. return this._oneOverRadiiSquared;
  1621. },
  1622. },
  1623. /**
  1624. * Gets the minimum radius of the ellipsoid.
  1625. * @memberof Ellipsoid.prototype
  1626. * @type {Number}
  1627. * @readonly
  1628. */
  1629. minimumRadius: {
  1630. get: function () {
  1631. return this._minimumRadius;
  1632. },
  1633. },
  1634. /**
  1635. * Gets the maximum radius of the ellipsoid.
  1636. * @memberof Ellipsoid.prototype
  1637. * @type {Number}
  1638. * @readonly
  1639. */
  1640. maximumRadius: {
  1641. get: function () {
  1642. return this._maximumRadius;
  1643. },
  1644. },
  1645. });
  1646. /**
  1647. * Duplicates an Ellipsoid instance.
  1648. *
  1649. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  1650. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1651. * instance should be created.
  1652. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  1653. */
  1654. Ellipsoid.clone = function (ellipsoid, result) {
  1655. if (!defaultValue.defined(ellipsoid)) {
  1656. return undefined;
  1657. }
  1658. const radii = ellipsoid._radii;
  1659. if (!defaultValue.defined(result)) {
  1660. return new Ellipsoid(radii.x, radii.y, radii.z);
  1661. }
  1662. Cartesian3.clone(radii, result._radii);
  1663. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  1664. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  1665. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  1666. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  1667. result._minimumRadius = ellipsoid._minimumRadius;
  1668. result._maximumRadius = ellipsoid._maximumRadius;
  1669. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  1670. return result;
  1671. };
  1672. /**
  1673. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  1674. *
  1675. * @param {Cartesian3} [cartesian=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  1676. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1677. * instance should be created.
  1678. * @returns {Ellipsoid} A new Ellipsoid instance.
  1679. *
  1680. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1681. *
  1682. * @see Ellipsoid.WGS84
  1683. * @see Ellipsoid.UNIT_SPHERE
  1684. */
  1685. Ellipsoid.fromCartesian3 = function (cartesian, result) {
  1686. if (!defaultValue.defined(result)) {
  1687. result = new Ellipsoid();
  1688. }
  1689. if (!defaultValue.defined(cartesian)) {
  1690. return result;
  1691. }
  1692. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  1693. return result;
  1694. };
  1695. /**
  1696. * An Ellipsoid instance initialized to the WGS84 standard.
  1697. *
  1698. * @type {Ellipsoid}
  1699. * @constant
  1700. */
  1701. Ellipsoid.WGS84 = Object.freeze(
  1702. new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793)
  1703. );
  1704. /**
  1705. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  1706. *
  1707. * @type {Ellipsoid}
  1708. * @constant
  1709. */
  1710. Ellipsoid.UNIT_SPHERE = Object.freeze(new Ellipsoid(1.0, 1.0, 1.0));
  1711. /**
  1712. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  1713. *
  1714. * @type {Ellipsoid}
  1715. * @constant
  1716. */
  1717. Ellipsoid.MOON = Object.freeze(
  1718. new Ellipsoid(
  1719. ComponentDatatype.CesiumMath.LUNAR_RADIUS,
  1720. ComponentDatatype.CesiumMath.LUNAR_RADIUS,
  1721. ComponentDatatype.CesiumMath.LUNAR_RADIUS
  1722. )
  1723. );
  1724. /**
  1725. * Duplicates an Ellipsoid instance.
  1726. *
  1727. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1728. * instance should be created.
  1729. * @returns {Ellipsoid} The cloned Ellipsoid.
  1730. */
  1731. Ellipsoid.prototype.clone = function (result) {
  1732. return Ellipsoid.clone(this, result);
  1733. };
  1734. /**
  1735. * The number of elements used to pack the object into an array.
  1736. * @type {Number}
  1737. */
  1738. Ellipsoid.packedLength = Cartesian3.packedLength;
  1739. /**
  1740. * Stores the provided instance into the provided array.
  1741. *
  1742. * @param {Ellipsoid} value The value to pack.
  1743. * @param {Number[]} array The array to pack into.
  1744. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1745. *
  1746. * @returns {Number[]} The array that was packed into
  1747. */
  1748. Ellipsoid.pack = function (value, array, startingIndex) {
  1749. //>>includeStart('debug', pragmas.debug);
  1750. RuntimeError.Check.typeOf.object("value", value);
  1751. RuntimeError.Check.defined("array", array);
  1752. //>>includeEnd('debug');
  1753. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1754. Cartesian3.pack(value._radii, array, startingIndex);
  1755. return array;
  1756. };
  1757. /**
  1758. * Retrieves an instance from a packed array.
  1759. *
  1760. * @param {Number[]} array The packed array.
  1761. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1762. * @param {Ellipsoid} [result] The object into which to store the result.
  1763. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  1764. */
  1765. Ellipsoid.unpack = function (array, startingIndex, result) {
  1766. //>>includeStart('debug', pragmas.debug);
  1767. RuntimeError.Check.defined("array", array);
  1768. //>>includeEnd('debug');
  1769. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1770. const radii = Cartesian3.unpack(array, startingIndex);
  1771. return Ellipsoid.fromCartesian3(radii, result);
  1772. };
  1773. /**
  1774. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  1775. * @function
  1776. *
  1777. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  1778. * @param {Cartesian3} [result] The object onto which to store the result.
  1779. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1780. */
  1781. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  1782. /**
  1783. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1784. *
  1785. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  1786. * @param {Cartesian3} [result] The object onto which to store the result.
  1787. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1788. */
  1789. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function (
  1790. cartographic,
  1791. result
  1792. ) {
  1793. //>>includeStart('debug', pragmas.debug);
  1794. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  1795. //>>includeEnd('debug');
  1796. const longitude = cartographic.longitude;
  1797. const latitude = cartographic.latitude;
  1798. const cosLatitude = Math.cos(latitude);
  1799. const x = cosLatitude * Math.cos(longitude);
  1800. const y = cosLatitude * Math.sin(longitude);
  1801. const z = Math.sin(latitude);
  1802. if (!defaultValue.defined(result)) {
  1803. result = new Cartesian3();
  1804. }
  1805. result.x = x;
  1806. result.y = y;
  1807. result.z = z;
  1808. return Cartesian3.normalize(result, result);
  1809. };
  1810. /**
  1811. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1812. *
  1813. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  1814. * @param {Cartesian3} [result] The object onto which to store the result.
  1815. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided, or undefined if a normal cannot be found.
  1816. */
  1817. Ellipsoid.prototype.geodeticSurfaceNormal = function (cartesian, result) {
  1818. if (
  1819. Cartesian3.equalsEpsilon(cartesian, Cartesian3.ZERO, ComponentDatatype.CesiumMath.EPSILON14)
  1820. ) {
  1821. return undefined;
  1822. }
  1823. if (!defaultValue.defined(result)) {
  1824. result = new Cartesian3();
  1825. }
  1826. result = Cartesian3.multiplyComponents(
  1827. cartesian,
  1828. this._oneOverRadiiSquared,
  1829. result
  1830. );
  1831. return Cartesian3.normalize(result, result);
  1832. };
  1833. const cartographicToCartesianNormal = new Cartesian3();
  1834. const cartographicToCartesianK = new Cartesian3();
  1835. /**
  1836. * Converts the provided cartographic to Cartesian representation.
  1837. *
  1838. * @param {Cartographic} cartographic The cartographic position.
  1839. * @param {Cartesian3} [result] The object onto which to store the result.
  1840. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1841. *
  1842. * @example
  1843. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  1844. * const position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  1845. * const cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  1846. */
  1847. Ellipsoid.prototype.cartographicToCartesian = function (cartographic, result) {
  1848. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  1849. const n = cartographicToCartesianNormal;
  1850. const k = cartographicToCartesianK;
  1851. this.geodeticSurfaceNormalCartographic(cartographic, n);
  1852. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  1853. const gamma = Math.sqrt(Cartesian3.dot(n, k));
  1854. Cartesian3.divideByScalar(k, gamma, k);
  1855. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  1856. if (!defaultValue.defined(result)) {
  1857. result = new Cartesian3();
  1858. }
  1859. return Cartesian3.add(k, n, result);
  1860. };
  1861. /**
  1862. * Converts the provided array of cartographics to an array of Cartesians.
  1863. *
  1864. * @param {Cartographic[]} cartographics An array of cartographic positions.
  1865. * @param {Cartesian3[]} [result] The object onto which to store the result.
  1866. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  1867. *
  1868. * @example
  1869. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  1870. * const positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  1871. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  1872. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  1873. * const cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  1874. */
  1875. Ellipsoid.prototype.cartographicArrayToCartesianArray = function (
  1876. cartographics,
  1877. result
  1878. ) {
  1879. //>>includeStart('debug', pragmas.debug);
  1880. RuntimeError.Check.defined("cartographics", cartographics);
  1881. //>>includeEnd('debug')
  1882. const length = cartographics.length;
  1883. if (!defaultValue.defined(result)) {
  1884. result = new Array(length);
  1885. } else {
  1886. result.length = length;
  1887. }
  1888. for (let i = 0; i < length; i++) {
  1889. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  1890. }
  1891. return result;
  1892. };
  1893. const cartesianToCartographicN = new Cartesian3();
  1894. const cartesianToCartographicP = new Cartesian3();
  1895. const cartesianToCartographicH = new Cartesian3();
  1896. /**
  1897. * Converts the provided cartesian to cartographic representation.
  1898. * The cartesian is undefined at the center of the ellipsoid.
  1899. *
  1900. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1901. * @param {Cartographic} [result] The object onto which to store the result.
  1902. * @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.
  1903. *
  1904. * @example
  1905. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  1906. * const position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  1907. * const cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  1908. */
  1909. Ellipsoid.prototype.cartesianToCartographic = function (cartesian, result) {
  1910. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1911. const p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  1912. if (!defaultValue.defined(p)) {
  1913. return undefined;
  1914. }
  1915. const n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  1916. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  1917. const longitude = Math.atan2(n.y, n.x);
  1918. const latitude = Math.asin(n.z);
  1919. const height =
  1920. ComponentDatatype.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1921. if (!defaultValue.defined(result)) {
  1922. return new Cartographic(longitude, latitude, height);
  1923. }
  1924. result.longitude = longitude;
  1925. result.latitude = latitude;
  1926. result.height = height;
  1927. return result;
  1928. };
  1929. /**
  1930. * Converts the provided array of cartesians to an array of cartographics.
  1931. *
  1932. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  1933. * @param {Cartographic[]} [result] The object onto which to store the result.
  1934. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  1935. *
  1936. * @example
  1937. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  1938. * const positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  1939. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  1940. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  1941. * const cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  1942. */
  1943. Ellipsoid.prototype.cartesianArrayToCartographicArray = function (
  1944. cartesians,
  1945. result
  1946. ) {
  1947. //>>includeStart('debug', pragmas.debug);
  1948. RuntimeError.Check.defined("cartesians", cartesians);
  1949. //>>includeEnd('debug');
  1950. const length = cartesians.length;
  1951. if (!defaultValue.defined(result)) {
  1952. result = new Array(length);
  1953. } else {
  1954. result.length = length;
  1955. }
  1956. for (let i = 0; i < length; ++i) {
  1957. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  1958. }
  1959. return result;
  1960. };
  1961. /**
  1962. * Scales the provided Cartesian position along the geodetic surface normal
  1963. * so that it is on the surface of this ellipsoid. If the position is
  1964. * at the center of the ellipsoid, this function returns undefined.
  1965. *
  1966. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1967. * @param {Cartesian3} [result] The object onto which to store the result.
  1968. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1969. */
  1970. Ellipsoid.prototype.scaleToGeodeticSurface = function (cartesian, result) {
  1971. return scaleToGeodeticSurface(
  1972. cartesian,
  1973. this._oneOverRadii,
  1974. this._oneOverRadiiSquared,
  1975. this._centerToleranceSquared,
  1976. result
  1977. );
  1978. };
  1979. /**
  1980. * Scales the provided Cartesian position along the geocentric surface normal
  1981. * so that it is on the surface of this ellipsoid.
  1982. *
  1983. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1984. * @param {Cartesian3} [result] The object onto which to store the result.
  1985. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1986. */
  1987. Ellipsoid.prototype.scaleToGeocentricSurface = function (cartesian, result) {
  1988. //>>includeStart('debug', pragmas.debug);
  1989. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  1990. //>>includeEnd('debug');
  1991. if (!defaultValue.defined(result)) {
  1992. result = new Cartesian3();
  1993. }
  1994. const positionX = cartesian.x;
  1995. const positionY = cartesian.y;
  1996. const positionZ = cartesian.z;
  1997. const oneOverRadiiSquared = this._oneOverRadiiSquared;
  1998. const beta =
  1999. 1.0 /
  2000. Math.sqrt(
  2001. positionX * positionX * oneOverRadiiSquared.x +
  2002. positionY * positionY * oneOverRadiiSquared.y +
  2003. positionZ * positionZ * oneOverRadiiSquared.z
  2004. );
  2005. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  2006. };
  2007. /**
  2008. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  2009. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  2010. *
  2011. * @param {Cartesian3} position The position to transform.
  2012. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2013. * return a new instance.
  2014. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  2015. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2016. */
  2017. Ellipsoid.prototype.transformPositionToScaledSpace = function (
  2018. position,
  2019. result
  2020. ) {
  2021. if (!defaultValue.defined(result)) {
  2022. result = new Cartesian3();
  2023. }
  2024. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  2025. };
  2026. /**
  2027. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  2028. * its components by the result of {@link Ellipsoid#radii}.
  2029. *
  2030. * @param {Cartesian3} position The position to transform.
  2031. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2032. * return a new instance.
  2033. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  2034. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2035. */
  2036. Ellipsoid.prototype.transformPositionFromScaledSpace = function (
  2037. position,
  2038. result
  2039. ) {
  2040. if (!defaultValue.defined(result)) {
  2041. result = new Cartesian3();
  2042. }
  2043. return Cartesian3.multiplyComponents(position, this._radii, result);
  2044. };
  2045. /**
  2046. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  2047. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2048. *
  2049. * @param {Ellipsoid} [right] The other Ellipsoid.
  2050. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2051. */
  2052. Ellipsoid.prototype.equals = function (right) {
  2053. return (
  2054. this === right ||
  2055. (defaultValue.defined(right) && Cartesian3.equals(this._radii, right._radii))
  2056. );
  2057. };
  2058. /**
  2059. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2060. *
  2061. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2062. */
  2063. Ellipsoid.prototype.toString = function () {
  2064. return this._radii.toString();
  2065. };
  2066. /**
  2067. * Computes a point which is the intersection of the surface normal with the z-axis.
  2068. *
  2069. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  2070. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  2071. * 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.
  2072. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  2073. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  2074. * @param {Cartesian3} [result] The cartesian to which to copy the result, or undefined to create and
  2075. * return a new instance.
  2076. * @returns {Cartesian3 | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  2077. *
  2078. * @exception {DeveloperError} position is required.
  2079. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  2080. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  2081. */
  2082. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function (
  2083. position,
  2084. buffer,
  2085. result
  2086. ) {
  2087. //>>includeStart('debug', pragmas.debug);
  2088. RuntimeError.Check.typeOf.object("position", position);
  2089. if (
  2090. !ComponentDatatype.CesiumMath.equalsEpsilon(
  2091. this._radii.x,
  2092. this._radii.y,
  2093. ComponentDatatype.CesiumMath.EPSILON15
  2094. )
  2095. ) {
  2096. throw new RuntimeError.DeveloperError(
  2097. "Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)"
  2098. );
  2099. }
  2100. RuntimeError.Check.typeOf.number.greaterThan("Ellipsoid.radii.z", this._radii.z, 0);
  2101. //>>includeEnd('debug');
  2102. buffer = defaultValue.defaultValue(buffer, 0.0);
  2103. const squaredXOverSquaredZ = this._squaredXOverSquaredZ;
  2104. if (!defaultValue.defined(result)) {
  2105. result = new Cartesian3();
  2106. }
  2107. result.x = 0.0;
  2108. result.y = 0.0;
  2109. result.z = position.z * (1 - squaredXOverSquaredZ);
  2110. if (Math.abs(result.z) >= this._radii.z - buffer) {
  2111. return undefined;
  2112. }
  2113. return result;
  2114. };
  2115. const abscissas = [
  2116. 0.14887433898163,
  2117. 0.43339539412925,
  2118. 0.67940956829902,
  2119. 0.86506336668898,
  2120. 0.97390652851717,
  2121. 0.0,
  2122. ];
  2123. const weights = [
  2124. 0.29552422471475,
  2125. 0.26926671930999,
  2126. 0.21908636251598,
  2127. 0.14945134915058,
  2128. 0.066671344308684,
  2129. 0.0,
  2130. ];
  2131. /**
  2132. * Compute the 10th order Gauss-Legendre Quadrature of the given definite integral.
  2133. *
  2134. * @param {Number} a The lower bound for the integration.
  2135. * @param {Number} b The upper bound for the integration.
  2136. * @param {Ellipsoid~RealValuedScalarFunction} func The function to integrate.
  2137. * @returns {Number} The value of the integral of the given function over the given domain.
  2138. *
  2139. * @private
  2140. */
  2141. function gaussLegendreQuadrature(a, b, func) {
  2142. //>>includeStart('debug', pragmas.debug);
  2143. RuntimeError.Check.typeOf.number("a", a);
  2144. RuntimeError.Check.typeOf.number("b", b);
  2145. RuntimeError.Check.typeOf.func("func", func);
  2146. //>>includeEnd('debug');
  2147. // The range is half of the normal range since the five weights add to one (ten weights add to two).
  2148. // The values of the abscissas are multiplied by two to account for this.
  2149. const xMean = 0.5 * (b + a);
  2150. const xRange = 0.5 * (b - a);
  2151. let sum = 0.0;
  2152. for (let i = 0; i < 5; i++) {
  2153. const dx = xRange * abscissas[i];
  2154. sum += weights[i] * (func(xMean + dx) + func(xMean - dx));
  2155. }
  2156. // Scale the sum to the range of x.
  2157. sum *= xRange;
  2158. return sum;
  2159. }
  2160. /**
  2161. * A real valued scalar function.
  2162. * @callback Ellipsoid~RealValuedScalarFunction
  2163. *
  2164. * @param {Number} x The value used to evaluate the function.
  2165. * @returns {Number} The value of the function at x.
  2166. *
  2167. * @private
  2168. */
  2169. /**
  2170. * Computes an approximation of the surface area of a rectangle on the surface of an ellipsoid using
  2171. * Gauss-Legendre 10th order quadrature.
  2172. *
  2173. * @param {Rectangle} rectangle The rectangle used for computing the surface area.
  2174. * @returns {Number} The approximate area of the rectangle on the surface of this ellipsoid.
  2175. */
  2176. Ellipsoid.prototype.surfaceArea = function (rectangle) {
  2177. //>>includeStart('debug', pragmas.debug);
  2178. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  2179. //>>includeEnd('debug');
  2180. const minLongitude = rectangle.west;
  2181. let maxLongitude = rectangle.east;
  2182. const minLatitude = rectangle.south;
  2183. const maxLatitude = rectangle.north;
  2184. while (maxLongitude < minLongitude) {
  2185. maxLongitude += ComponentDatatype.CesiumMath.TWO_PI;
  2186. }
  2187. const radiiSquared = this._radiiSquared;
  2188. const a2 = radiiSquared.x;
  2189. const b2 = radiiSquared.y;
  2190. const c2 = radiiSquared.z;
  2191. const a2b2 = a2 * b2;
  2192. return gaussLegendreQuadrature(minLatitude, maxLatitude, function (lat) {
  2193. // phi represents the angle measured from the north pole
  2194. // sin(phi) = sin(pi / 2 - lat) = cos(lat), cos(phi) is similar
  2195. const sinPhi = Math.cos(lat);
  2196. const cosPhi = Math.sin(lat);
  2197. return (
  2198. Math.cos(lat) *
  2199. gaussLegendreQuadrature(minLongitude, maxLongitude, function (lon) {
  2200. const cosTheta = Math.cos(lon);
  2201. const sinTheta = Math.sin(lon);
  2202. return Math.sqrt(
  2203. a2b2 * cosPhi * cosPhi +
  2204. c2 *
  2205. (b2 * cosTheta * cosTheta + a2 * sinTheta * sinTheta) *
  2206. sinPhi *
  2207. sinPhi
  2208. );
  2209. })
  2210. );
  2211. });
  2212. };
  2213. /**
  2214. * A 3x3 matrix, indexable as a column-major order array.
  2215. * Constructor parameters are in row-major order for code readability.
  2216. * @alias Matrix3
  2217. * @constructor
  2218. * @implements {ArrayLike<number>}
  2219. *
  2220. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  2221. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  2222. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  2223. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  2224. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  2225. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  2226. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  2227. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  2228. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  2229. *
  2230. * @see Matrix3.fromArray
  2231. * @see Matrix3.fromColumnMajorArray
  2232. * @see Matrix3.fromRowMajorArray
  2233. * @see Matrix3.fromQuaternion
  2234. * @see Matrix3.fromHeadingPitchRoll
  2235. * @see Matrix3.fromScale
  2236. * @see Matrix3.fromUniformScale
  2237. * @see Matrix3.fromCrossProduct
  2238. * @see Matrix3.fromRotationX
  2239. * @see Matrix3.fromRotationY
  2240. * @see Matrix3.fromRotationZ
  2241. * @see Matrix2
  2242. * @see Matrix4
  2243. */
  2244. function Matrix3(
  2245. column0Row0,
  2246. column1Row0,
  2247. column2Row0,
  2248. column0Row1,
  2249. column1Row1,
  2250. column2Row1,
  2251. column0Row2,
  2252. column1Row2,
  2253. column2Row2
  2254. ) {
  2255. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  2256. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  2257. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  2258. this[3] = defaultValue.defaultValue(column1Row0, 0.0);
  2259. this[4] = defaultValue.defaultValue(column1Row1, 0.0);
  2260. this[5] = defaultValue.defaultValue(column1Row2, 0.0);
  2261. this[6] = defaultValue.defaultValue(column2Row0, 0.0);
  2262. this[7] = defaultValue.defaultValue(column2Row1, 0.0);
  2263. this[8] = defaultValue.defaultValue(column2Row2, 0.0);
  2264. }
  2265. /**
  2266. * The number of elements used to pack the object into an array.
  2267. * @type {Number}
  2268. */
  2269. Matrix3.packedLength = 9;
  2270. /**
  2271. * Stores the provided instance into the provided array.
  2272. *
  2273. * @param {Matrix3} value The value to pack.
  2274. * @param {Number[]} array The array to pack into.
  2275. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2276. *
  2277. * @returns {Number[]} The array that was packed into
  2278. */
  2279. Matrix3.pack = function (value, array, startingIndex) {
  2280. //>>includeStart('debug', pragmas.debug);
  2281. RuntimeError.Check.typeOf.object("value", value);
  2282. RuntimeError.Check.defined("array", array);
  2283. //>>includeEnd('debug');
  2284. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2285. array[startingIndex++] = value[0];
  2286. array[startingIndex++] = value[1];
  2287. array[startingIndex++] = value[2];
  2288. array[startingIndex++] = value[3];
  2289. array[startingIndex++] = value[4];
  2290. array[startingIndex++] = value[5];
  2291. array[startingIndex++] = value[6];
  2292. array[startingIndex++] = value[7];
  2293. array[startingIndex++] = value[8];
  2294. return array;
  2295. };
  2296. /**
  2297. * Retrieves an instance from a packed array.
  2298. *
  2299. * @param {Number[]} array The packed array.
  2300. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2301. * @param {Matrix3} [result] The object into which to store the result.
  2302. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2303. */
  2304. Matrix3.unpack = function (array, startingIndex, result) {
  2305. //>>includeStart('debug', pragmas.debug);
  2306. RuntimeError.Check.defined("array", array);
  2307. //>>includeEnd('debug');
  2308. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2309. if (!defaultValue.defined(result)) {
  2310. result = new Matrix3();
  2311. }
  2312. result[0] = array[startingIndex++];
  2313. result[1] = array[startingIndex++];
  2314. result[2] = array[startingIndex++];
  2315. result[3] = array[startingIndex++];
  2316. result[4] = array[startingIndex++];
  2317. result[5] = array[startingIndex++];
  2318. result[6] = array[startingIndex++];
  2319. result[7] = array[startingIndex++];
  2320. result[8] = array[startingIndex++];
  2321. return result;
  2322. };
  2323. /**
  2324. * Flattens an array of Matrix3s into an array of components. The components
  2325. * are stored in column-major order.
  2326. *
  2327. * @param {Matrix3[]} array The array of matrices to pack.
  2328. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 9 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 9) elements.
  2329. * @returns {Number[]} The packed array.
  2330. */
  2331. Matrix3.packArray = function (array, result) {
  2332. //>>includeStart('debug', pragmas.debug);
  2333. RuntimeError.Check.defined("array", array);
  2334. //>>includeEnd('debug');
  2335. const length = array.length;
  2336. const resultLength = length * 9;
  2337. if (!defaultValue.defined(result)) {
  2338. result = new Array(resultLength);
  2339. } else if (!Array.isArray(result) && result.length !== resultLength) {
  2340. //>>includeStart('debug', pragmas.debug);
  2341. throw new RuntimeError.DeveloperError(
  2342. "If result is a typed array, it must have exactly array.length * 9 elements"
  2343. );
  2344. //>>includeEnd('debug');
  2345. } else if (result.length !== resultLength) {
  2346. result.length = resultLength;
  2347. }
  2348. for (let i = 0; i < length; ++i) {
  2349. Matrix3.pack(array[i], result, i * 9);
  2350. }
  2351. return result;
  2352. };
  2353. /**
  2354. * Unpacks an array of column-major matrix components into an array of Matrix3s.
  2355. *
  2356. * @param {Number[]} array The array of components to unpack.
  2357. * @param {Matrix3[]} [result] The array onto which to store the result.
  2358. * @returns {Matrix3[]} The unpacked array.
  2359. */
  2360. Matrix3.unpackArray = function (array, result) {
  2361. //>>includeStart('debug', pragmas.debug);
  2362. RuntimeError.Check.defined("array", array);
  2363. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 9);
  2364. if (array.length % 9 !== 0) {
  2365. throw new RuntimeError.DeveloperError("array length must be a multiple of 9.");
  2366. }
  2367. //>>includeEnd('debug');
  2368. const length = array.length;
  2369. if (!defaultValue.defined(result)) {
  2370. result = new Array(length / 9);
  2371. } else {
  2372. result.length = length / 9;
  2373. }
  2374. for (let i = 0; i < length; i += 9) {
  2375. const index = i / 9;
  2376. result[index] = Matrix3.unpack(array, i, result[index]);
  2377. }
  2378. return result;
  2379. };
  2380. /**
  2381. * Duplicates a Matrix3 instance.
  2382. *
  2383. * @param {Matrix3} matrix The matrix to duplicate.
  2384. * @param {Matrix3} [result] The object onto which to store the result.
  2385. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  2386. */
  2387. Matrix3.clone = function (matrix, result) {
  2388. if (!defaultValue.defined(matrix)) {
  2389. return undefined;
  2390. }
  2391. if (!defaultValue.defined(result)) {
  2392. return new Matrix3(
  2393. matrix[0],
  2394. matrix[3],
  2395. matrix[6],
  2396. matrix[1],
  2397. matrix[4],
  2398. matrix[7],
  2399. matrix[2],
  2400. matrix[5],
  2401. matrix[8]
  2402. );
  2403. }
  2404. result[0] = matrix[0];
  2405. result[1] = matrix[1];
  2406. result[2] = matrix[2];
  2407. result[3] = matrix[3];
  2408. result[4] = matrix[4];
  2409. result[5] = matrix[5];
  2410. result[6] = matrix[6];
  2411. result[7] = matrix[7];
  2412. result[8] = matrix[8];
  2413. return result;
  2414. };
  2415. /**
  2416. * Creates a Matrix3 from 9 consecutive elements in an array.
  2417. *
  2418. * @function
  2419. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  2420. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  2421. * @param {Matrix3} [result] The object onto which to store the result.
  2422. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2423. *
  2424. * @example
  2425. * // Create the Matrix3:
  2426. * // [1.0, 2.0, 3.0]
  2427. * // [1.0, 2.0, 3.0]
  2428. * // [1.0, 2.0, 3.0]
  2429. *
  2430. * const v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2431. * const m = Cesium.Matrix3.fromArray(v);
  2432. *
  2433. * // Create same Matrix3 with using an offset into an array
  2434. * const v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2435. * const m2 = Cesium.Matrix3.fromArray(v2, 2);
  2436. */
  2437. Matrix3.fromArray = Matrix3.unpack;
  2438. /**
  2439. * Creates a Matrix3 instance from a column-major order array.
  2440. *
  2441. * @param {Number[]} values The column-major order array.
  2442. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2443. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2444. */
  2445. Matrix3.fromColumnMajorArray = function (values, result) {
  2446. //>>includeStart('debug', pragmas.debug);
  2447. RuntimeError.Check.defined("values", values);
  2448. //>>includeEnd('debug');
  2449. return Matrix3.clone(values, result);
  2450. };
  2451. /**
  2452. * Creates a Matrix3 instance from a row-major order array.
  2453. * The resulting matrix will be in column-major order.
  2454. *
  2455. * @param {Number[]} values The row-major order array.
  2456. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2457. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2458. */
  2459. Matrix3.fromRowMajorArray = function (values, result) {
  2460. //>>includeStart('debug', pragmas.debug);
  2461. RuntimeError.Check.defined("values", values);
  2462. //>>includeEnd('debug');
  2463. if (!defaultValue.defined(result)) {
  2464. return new Matrix3(
  2465. values[0],
  2466. values[1],
  2467. values[2],
  2468. values[3],
  2469. values[4],
  2470. values[5],
  2471. values[6],
  2472. values[7],
  2473. values[8]
  2474. );
  2475. }
  2476. result[0] = values[0];
  2477. result[1] = values[3];
  2478. result[2] = values[6];
  2479. result[3] = values[1];
  2480. result[4] = values[4];
  2481. result[5] = values[7];
  2482. result[6] = values[2];
  2483. result[7] = values[5];
  2484. result[8] = values[8];
  2485. return result;
  2486. };
  2487. /**
  2488. * Computes a 3x3 rotation matrix from the provided quaternion.
  2489. *
  2490. * @param {Quaternion} quaternion the quaternion to use.
  2491. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2492. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  2493. */
  2494. Matrix3.fromQuaternion = function (quaternion, result) {
  2495. //>>includeStart('debug', pragmas.debug);
  2496. RuntimeError.Check.typeOf.object("quaternion", quaternion);
  2497. //>>includeEnd('debug');
  2498. const x2 = quaternion.x * quaternion.x;
  2499. const xy = quaternion.x * quaternion.y;
  2500. const xz = quaternion.x * quaternion.z;
  2501. const xw = quaternion.x * quaternion.w;
  2502. const y2 = quaternion.y * quaternion.y;
  2503. const yz = quaternion.y * quaternion.z;
  2504. const yw = quaternion.y * quaternion.w;
  2505. const z2 = quaternion.z * quaternion.z;
  2506. const zw = quaternion.z * quaternion.w;
  2507. const w2 = quaternion.w * quaternion.w;
  2508. const m00 = x2 - y2 - z2 + w2;
  2509. const m01 = 2.0 * (xy - zw);
  2510. const m02 = 2.0 * (xz + yw);
  2511. const m10 = 2.0 * (xy + zw);
  2512. const m11 = -x2 + y2 - z2 + w2;
  2513. const m12 = 2.0 * (yz - xw);
  2514. const m20 = 2.0 * (xz - yw);
  2515. const m21 = 2.0 * (yz + xw);
  2516. const m22 = -x2 - y2 + z2 + w2;
  2517. if (!defaultValue.defined(result)) {
  2518. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2519. }
  2520. result[0] = m00;
  2521. result[1] = m10;
  2522. result[2] = m20;
  2523. result[3] = m01;
  2524. result[4] = m11;
  2525. result[5] = m21;
  2526. result[6] = m02;
  2527. result[7] = m12;
  2528. result[8] = m22;
  2529. return result;
  2530. };
  2531. /**
  2532. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  2533. *
  2534. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  2535. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2536. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  2537. */
  2538. Matrix3.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  2539. //>>includeStart('debug', pragmas.debug);
  2540. RuntimeError.Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  2541. //>>includeEnd('debug');
  2542. const cosTheta = Math.cos(-headingPitchRoll.pitch);
  2543. const cosPsi = Math.cos(-headingPitchRoll.heading);
  2544. const cosPhi = Math.cos(headingPitchRoll.roll);
  2545. const sinTheta = Math.sin(-headingPitchRoll.pitch);
  2546. const sinPsi = Math.sin(-headingPitchRoll.heading);
  2547. const sinPhi = Math.sin(headingPitchRoll.roll);
  2548. const m00 = cosTheta * cosPsi;
  2549. const m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  2550. const m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  2551. const m10 = cosTheta * sinPsi;
  2552. const m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  2553. const m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  2554. const m20 = -sinTheta;
  2555. const m21 = sinPhi * cosTheta;
  2556. const m22 = cosPhi * cosTheta;
  2557. if (!defaultValue.defined(result)) {
  2558. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2559. }
  2560. result[0] = m00;
  2561. result[1] = m10;
  2562. result[2] = m20;
  2563. result[3] = m01;
  2564. result[4] = m11;
  2565. result[5] = m21;
  2566. result[6] = m02;
  2567. result[7] = m12;
  2568. result[8] = m22;
  2569. return result;
  2570. };
  2571. /**
  2572. * Computes a Matrix3 instance representing a non-uniform scale.
  2573. *
  2574. * @param {Cartesian3} scale The x, y, and z scale factors.
  2575. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2576. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2577. *
  2578. * @example
  2579. * // Creates
  2580. * // [7.0, 0.0, 0.0]
  2581. * // [0.0, 8.0, 0.0]
  2582. * // [0.0, 0.0, 9.0]
  2583. * const m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2584. */
  2585. Matrix3.fromScale = function (scale, result) {
  2586. //>>includeStart('debug', pragmas.debug);
  2587. RuntimeError.Check.typeOf.object("scale", scale);
  2588. //>>includeEnd('debug');
  2589. if (!defaultValue.defined(result)) {
  2590. return new Matrix3(scale.x, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, scale.z);
  2591. }
  2592. result[0] = scale.x;
  2593. result[1] = 0.0;
  2594. result[2] = 0.0;
  2595. result[3] = 0.0;
  2596. result[4] = scale.y;
  2597. result[5] = 0.0;
  2598. result[6] = 0.0;
  2599. result[7] = 0.0;
  2600. result[8] = scale.z;
  2601. return result;
  2602. };
  2603. /**
  2604. * Computes a Matrix3 instance representing a uniform scale.
  2605. *
  2606. * @param {Number} scale The uniform scale factor.
  2607. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2608. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2609. *
  2610. * @example
  2611. * // Creates
  2612. * // [2.0, 0.0, 0.0]
  2613. * // [0.0, 2.0, 0.0]
  2614. * // [0.0, 0.0, 2.0]
  2615. * const m = Cesium.Matrix3.fromUniformScale(2.0);
  2616. */
  2617. Matrix3.fromUniformScale = function (scale, result) {
  2618. //>>includeStart('debug', pragmas.debug);
  2619. RuntimeError.Check.typeOf.number("scale", scale);
  2620. //>>includeEnd('debug');
  2621. if (!defaultValue.defined(result)) {
  2622. return new Matrix3(scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, scale);
  2623. }
  2624. result[0] = scale;
  2625. result[1] = 0.0;
  2626. result[2] = 0.0;
  2627. result[3] = 0.0;
  2628. result[4] = scale;
  2629. result[5] = 0.0;
  2630. result[6] = 0.0;
  2631. result[7] = 0.0;
  2632. result[8] = scale;
  2633. return result;
  2634. };
  2635. /**
  2636. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  2637. *
  2638. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  2639. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2640. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2641. *
  2642. * @example
  2643. * // Creates
  2644. * // [0.0, -9.0, 8.0]
  2645. * // [9.0, 0.0, -7.0]
  2646. * // [-8.0, 7.0, 0.0]
  2647. * const m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2648. */
  2649. Matrix3.fromCrossProduct = function (vector, result) {
  2650. //>>includeStart('debug', pragmas.debug);
  2651. RuntimeError.Check.typeOf.object("vector", vector);
  2652. //>>includeEnd('debug');
  2653. if (!defaultValue.defined(result)) {
  2654. return new Matrix3(
  2655. 0.0,
  2656. -vector.z,
  2657. vector.y,
  2658. vector.z,
  2659. 0.0,
  2660. -vector.x,
  2661. -vector.y,
  2662. vector.x,
  2663. 0.0
  2664. );
  2665. }
  2666. result[0] = 0.0;
  2667. result[1] = vector.z;
  2668. result[2] = -vector.y;
  2669. result[3] = -vector.z;
  2670. result[4] = 0.0;
  2671. result[5] = vector.x;
  2672. result[6] = vector.y;
  2673. result[7] = -vector.x;
  2674. result[8] = 0.0;
  2675. return result;
  2676. };
  2677. /**
  2678. * Creates a rotation matrix around the x-axis.
  2679. *
  2680. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2681. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2682. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2683. *
  2684. * @example
  2685. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  2686. * const p = new Cesium.Cartesian3(5, 6, 7);
  2687. * const m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  2688. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2689. */
  2690. Matrix3.fromRotationX = function (angle, result) {
  2691. //>>includeStart('debug', pragmas.debug);
  2692. RuntimeError.Check.typeOf.number("angle", angle);
  2693. //>>includeEnd('debug');
  2694. const cosAngle = Math.cos(angle);
  2695. const sinAngle = Math.sin(angle);
  2696. if (!defaultValue.defined(result)) {
  2697. return new Matrix3(
  2698. 1.0,
  2699. 0.0,
  2700. 0.0,
  2701. 0.0,
  2702. cosAngle,
  2703. -sinAngle,
  2704. 0.0,
  2705. sinAngle,
  2706. cosAngle
  2707. );
  2708. }
  2709. result[0] = 1.0;
  2710. result[1] = 0.0;
  2711. result[2] = 0.0;
  2712. result[3] = 0.0;
  2713. result[4] = cosAngle;
  2714. result[5] = sinAngle;
  2715. result[6] = 0.0;
  2716. result[7] = -sinAngle;
  2717. result[8] = cosAngle;
  2718. return result;
  2719. };
  2720. /**
  2721. * Creates a rotation matrix around the y-axis.
  2722. *
  2723. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2724. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2725. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2726. *
  2727. * @example
  2728. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  2729. * const p = new Cesium.Cartesian3(5, 6, 7);
  2730. * const m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  2731. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2732. */
  2733. Matrix3.fromRotationY = function (angle, result) {
  2734. //>>includeStart('debug', pragmas.debug);
  2735. RuntimeError.Check.typeOf.number("angle", angle);
  2736. //>>includeEnd('debug');
  2737. const cosAngle = Math.cos(angle);
  2738. const sinAngle = Math.sin(angle);
  2739. if (!defaultValue.defined(result)) {
  2740. return new Matrix3(
  2741. cosAngle,
  2742. 0.0,
  2743. sinAngle,
  2744. 0.0,
  2745. 1.0,
  2746. 0.0,
  2747. -sinAngle,
  2748. 0.0,
  2749. cosAngle
  2750. );
  2751. }
  2752. result[0] = cosAngle;
  2753. result[1] = 0.0;
  2754. result[2] = -sinAngle;
  2755. result[3] = 0.0;
  2756. result[4] = 1.0;
  2757. result[5] = 0.0;
  2758. result[6] = sinAngle;
  2759. result[7] = 0.0;
  2760. result[8] = cosAngle;
  2761. return result;
  2762. };
  2763. /**
  2764. * Creates a rotation matrix around the z-axis.
  2765. *
  2766. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2767. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2768. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2769. *
  2770. * @example
  2771. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  2772. * const p = new Cesium.Cartesian3(5, 6, 7);
  2773. * const m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  2774. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2775. */
  2776. Matrix3.fromRotationZ = function (angle, result) {
  2777. //>>includeStart('debug', pragmas.debug);
  2778. RuntimeError.Check.typeOf.number("angle", angle);
  2779. //>>includeEnd('debug');
  2780. const cosAngle = Math.cos(angle);
  2781. const sinAngle = Math.sin(angle);
  2782. if (!defaultValue.defined(result)) {
  2783. return new Matrix3(
  2784. cosAngle,
  2785. -sinAngle,
  2786. 0.0,
  2787. sinAngle,
  2788. cosAngle,
  2789. 0.0,
  2790. 0.0,
  2791. 0.0,
  2792. 1.0
  2793. );
  2794. }
  2795. result[0] = cosAngle;
  2796. result[1] = sinAngle;
  2797. result[2] = 0.0;
  2798. result[3] = -sinAngle;
  2799. result[4] = cosAngle;
  2800. result[5] = 0.0;
  2801. result[6] = 0.0;
  2802. result[7] = 0.0;
  2803. result[8] = 1.0;
  2804. return result;
  2805. };
  2806. /**
  2807. * Creates an Array from the provided Matrix3 instance.
  2808. * The array will be in column-major order.
  2809. *
  2810. * @param {Matrix3} matrix The matrix to use..
  2811. * @param {Number[]} [result] The Array onto which to store the result.
  2812. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  2813. */
  2814. Matrix3.toArray = function (matrix, result) {
  2815. //>>includeStart('debug', pragmas.debug);
  2816. RuntimeError.Check.typeOf.object("matrix", matrix);
  2817. //>>includeEnd('debug');
  2818. if (!defaultValue.defined(result)) {
  2819. return [
  2820. matrix[0],
  2821. matrix[1],
  2822. matrix[2],
  2823. matrix[3],
  2824. matrix[4],
  2825. matrix[5],
  2826. matrix[6],
  2827. matrix[7],
  2828. matrix[8],
  2829. ];
  2830. }
  2831. result[0] = matrix[0];
  2832. result[1] = matrix[1];
  2833. result[2] = matrix[2];
  2834. result[3] = matrix[3];
  2835. result[4] = matrix[4];
  2836. result[5] = matrix[5];
  2837. result[6] = matrix[6];
  2838. result[7] = matrix[7];
  2839. result[8] = matrix[8];
  2840. return result;
  2841. };
  2842. /**
  2843. * Computes the array index of the element at the provided row and column.
  2844. *
  2845. * @param {Number} column The zero-based index of the column.
  2846. * @param {Number} row The zero-based index of the row.
  2847. * @returns {Number} The index of the element at the provided row and column.
  2848. *
  2849. * @exception {DeveloperError} row must be 0, 1, or 2.
  2850. * @exception {DeveloperError} column must be 0, 1, or 2.
  2851. *
  2852. * @example
  2853. * const myMatrix = new Cesium.Matrix3();
  2854. * const column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  2855. * const column1Row0 = myMatrix[column1Row0Index]
  2856. * myMatrix[column1Row0Index] = 10.0;
  2857. */
  2858. Matrix3.getElementIndex = function (column, row) {
  2859. //>>includeStart('debug', pragmas.debug);
  2860. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  2861. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 2);
  2862. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  2863. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 2);
  2864. //>>includeEnd('debug');
  2865. return column * 3 + row;
  2866. };
  2867. /**
  2868. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  2869. *
  2870. * @param {Matrix3} matrix The matrix to use.
  2871. * @param {Number} index The zero-based index of the column to retrieve.
  2872. * @param {Cartesian3} result The object onto which to store the result.
  2873. * @returns {Cartesian3} The modified result parameter.
  2874. *
  2875. * @exception {DeveloperError} index must be 0, 1, or 2.
  2876. */
  2877. Matrix3.getColumn = function (matrix, index, result) {
  2878. //>>includeStart('debug', pragmas.debug);
  2879. RuntimeError.Check.typeOf.object("matrix", matrix);
  2880. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2881. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2882. RuntimeError.Check.typeOf.object("result", result);
  2883. //>>includeEnd('debug');
  2884. const startIndex = index * 3;
  2885. const x = matrix[startIndex];
  2886. const y = matrix[startIndex + 1];
  2887. const z = matrix[startIndex + 2];
  2888. result.x = x;
  2889. result.y = y;
  2890. result.z = z;
  2891. return result;
  2892. };
  2893. /**
  2894. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  2895. *
  2896. * @param {Matrix3} matrix The matrix to use.
  2897. * @param {Number} index The zero-based index of the column to set.
  2898. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  2899. * @param {Matrix3} result The object onto which to store the result.
  2900. * @returns {Matrix3} The modified result parameter.
  2901. *
  2902. * @exception {DeveloperError} index must be 0, 1, or 2.
  2903. */
  2904. Matrix3.setColumn = function (matrix, index, cartesian, result) {
  2905. //>>includeStart('debug', pragmas.debug);
  2906. RuntimeError.Check.typeOf.object("matrix", matrix);
  2907. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2908. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2909. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  2910. RuntimeError.Check.typeOf.object("result", result);
  2911. //>>includeEnd('debug');
  2912. result = Matrix3.clone(matrix, result);
  2913. const startIndex = index * 3;
  2914. result[startIndex] = cartesian.x;
  2915. result[startIndex + 1] = cartesian.y;
  2916. result[startIndex + 2] = cartesian.z;
  2917. return result;
  2918. };
  2919. /**
  2920. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  2921. *
  2922. * @param {Matrix3} matrix The matrix to use.
  2923. * @param {Number} index The zero-based index of the row to retrieve.
  2924. * @param {Cartesian3} result The object onto which to store the result.
  2925. * @returns {Cartesian3} The modified result parameter.
  2926. *
  2927. * @exception {DeveloperError} index must be 0, 1, or 2.
  2928. */
  2929. Matrix3.getRow = function (matrix, index, result) {
  2930. //>>includeStart('debug', pragmas.debug);
  2931. RuntimeError.Check.typeOf.object("matrix", matrix);
  2932. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2933. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2934. RuntimeError.Check.typeOf.object("result", result);
  2935. //>>includeEnd('debug');
  2936. const x = matrix[index];
  2937. const y = matrix[index + 3];
  2938. const z = matrix[index + 6];
  2939. result.x = x;
  2940. result.y = y;
  2941. result.z = z;
  2942. return result;
  2943. };
  2944. /**
  2945. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  2946. *
  2947. * @param {Matrix3} matrix The matrix to use.
  2948. * @param {Number} index The zero-based index of the row to set.
  2949. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  2950. * @param {Matrix3} result The object onto which to store the result.
  2951. * @returns {Matrix3} The modified result parameter.
  2952. *
  2953. * @exception {DeveloperError} index must be 0, 1, or 2.
  2954. */
  2955. Matrix3.setRow = function (matrix, index, cartesian, result) {
  2956. //>>includeStart('debug', pragmas.debug);
  2957. RuntimeError.Check.typeOf.object("matrix", matrix);
  2958. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2959. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2960. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  2961. RuntimeError.Check.typeOf.object("result", result);
  2962. //>>includeEnd('debug');
  2963. result = Matrix3.clone(matrix, result);
  2964. result[index] = cartesian.x;
  2965. result[index + 3] = cartesian.y;
  2966. result[index + 6] = cartesian.z;
  2967. return result;
  2968. };
  2969. const scaleScratch1$2 = new Cartesian3();
  2970. /**
  2971. * Computes a new matrix that replaces the scale with the provided scale.
  2972. * This assumes the matrix is an affine transformation.
  2973. *
  2974. * @param {Matrix3} matrix The matrix to use.
  2975. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  2976. * @param {Matrix3} result The object onto which to store the result.
  2977. * @returns {Matrix3} The modified result parameter.
  2978. *
  2979. * @see Matrix3.setUniformScale
  2980. * @see Matrix3.fromScale
  2981. * @see Matrix3.fromUniformScale
  2982. * @see Matrix3.multiplyByScale
  2983. * @see Matrix3.multiplyByUniformScale
  2984. * @see Matrix3.getScale
  2985. */
  2986. Matrix3.setScale = function (matrix, scale, result) {
  2987. //>>includeStart('debug', pragmas.debug);
  2988. RuntimeError.Check.typeOf.object("matrix", matrix);
  2989. RuntimeError.Check.typeOf.object("scale", scale);
  2990. RuntimeError.Check.typeOf.object("result", result);
  2991. //>>includeEnd('debug');
  2992. const existingScale = Matrix3.getScale(matrix, scaleScratch1$2);
  2993. const scaleRatioX = scale.x / existingScale.x;
  2994. const scaleRatioY = scale.y / existingScale.y;
  2995. const scaleRatioZ = scale.z / existingScale.z;
  2996. result[0] = matrix[0] * scaleRatioX;
  2997. result[1] = matrix[1] * scaleRatioX;
  2998. result[2] = matrix[2] * scaleRatioX;
  2999. result[3] = matrix[3] * scaleRatioY;
  3000. result[4] = matrix[4] * scaleRatioY;
  3001. result[5] = matrix[5] * scaleRatioY;
  3002. result[6] = matrix[6] * scaleRatioZ;
  3003. result[7] = matrix[7] * scaleRatioZ;
  3004. result[8] = matrix[8] * scaleRatioZ;
  3005. return result;
  3006. };
  3007. const scaleScratch2$2 = new Cartesian3();
  3008. /**
  3009. * Computes a new matrix that replaces the scale with the provided uniform scale.
  3010. * This assumes the matrix is an affine transformation.
  3011. *
  3012. * @param {Matrix3} matrix The matrix to use.
  3013. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  3014. * @param {Matrix3} result The object onto which to store the result.
  3015. * @returns {Matrix3} The modified result parameter.
  3016. *
  3017. * @see Matrix3.setScale
  3018. * @see Matrix3.fromScale
  3019. * @see Matrix3.fromUniformScale
  3020. * @see Matrix3.multiplyByScale
  3021. * @see Matrix3.multiplyByUniformScale
  3022. * @see Matrix3.getScale
  3023. */
  3024. Matrix3.setUniformScale = function (matrix, scale, result) {
  3025. //>>includeStart('debug', pragmas.debug);
  3026. RuntimeError.Check.typeOf.object("matrix", matrix);
  3027. RuntimeError.Check.typeOf.number("scale", scale);
  3028. RuntimeError.Check.typeOf.object("result", result);
  3029. //>>includeEnd('debug');
  3030. const existingScale = Matrix3.getScale(matrix, scaleScratch2$2);
  3031. const scaleRatioX = scale / existingScale.x;
  3032. const scaleRatioY = scale / existingScale.y;
  3033. const scaleRatioZ = scale / existingScale.z;
  3034. result[0] = matrix[0] * scaleRatioX;
  3035. result[1] = matrix[1] * scaleRatioX;
  3036. result[2] = matrix[2] * scaleRatioX;
  3037. result[3] = matrix[3] * scaleRatioY;
  3038. result[4] = matrix[4] * scaleRatioY;
  3039. result[5] = matrix[5] * scaleRatioY;
  3040. result[6] = matrix[6] * scaleRatioZ;
  3041. result[7] = matrix[7] * scaleRatioZ;
  3042. result[8] = matrix[8] * scaleRatioZ;
  3043. return result;
  3044. };
  3045. const scratchColumn$2 = new Cartesian3();
  3046. /**
  3047. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  3048. *
  3049. * @param {Matrix3} matrix The matrix.
  3050. * @param {Cartesian3} result The object onto which to store the result.
  3051. * @returns {Cartesian3} The modified result parameter.
  3052. *
  3053. * @see Matrix3.multiplyByScale
  3054. * @see Matrix3.multiplyByUniformScale
  3055. * @see Matrix3.fromScale
  3056. * @see Matrix3.fromUniformScale
  3057. * @see Matrix3.setScale
  3058. * @see Matrix3.setUniformScale
  3059. */
  3060. Matrix3.getScale = function (matrix, result) {
  3061. //>>includeStart('debug', pragmas.debug);
  3062. RuntimeError.Check.typeOf.object("matrix", matrix);
  3063. RuntimeError.Check.typeOf.object("result", result);
  3064. //>>includeEnd('debug');
  3065. result.x = Cartesian3.magnitude(
  3066. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$2)
  3067. );
  3068. result.y = Cartesian3.magnitude(
  3069. Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn$2)
  3070. );
  3071. result.z = Cartesian3.magnitude(
  3072. Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn$2)
  3073. );
  3074. return result;
  3075. };
  3076. const scaleScratch3$2 = new Cartesian3();
  3077. /**
  3078. * Computes the maximum scale assuming the matrix is an affine transformation.
  3079. * The maximum scale is the maximum length of the column vectors.
  3080. *
  3081. * @param {Matrix3} matrix The matrix.
  3082. * @returns {Number} The maximum scale.
  3083. */
  3084. Matrix3.getMaximumScale = function (matrix) {
  3085. Matrix3.getScale(matrix, scaleScratch3$2);
  3086. return Cartesian3.maximumComponent(scaleScratch3$2);
  3087. };
  3088. const scaleScratch4$2 = new Cartesian3();
  3089. /**
  3090. * Sets the rotation assuming the matrix is an affine transformation.
  3091. *
  3092. * @param {Matrix3} matrix The matrix.
  3093. * @param {Matrix3} rotation The rotation matrix.
  3094. * @returns {Matrix3} The modified result parameter.
  3095. *
  3096. * @see Matrix3.getRotation
  3097. */
  3098. Matrix3.setRotation = function (matrix, rotation, result) {
  3099. //>>includeStart('debug', pragmas.debug);
  3100. RuntimeError.Check.typeOf.object("matrix", matrix);
  3101. RuntimeError.Check.typeOf.object("result", result);
  3102. //>>includeEnd('debug');
  3103. const scale = Matrix3.getScale(matrix, scaleScratch4$2);
  3104. result[0] = rotation[0] * scale.x;
  3105. result[1] = rotation[1] * scale.x;
  3106. result[2] = rotation[2] * scale.x;
  3107. result[3] = rotation[3] * scale.y;
  3108. result[4] = rotation[4] * scale.y;
  3109. result[5] = rotation[5] * scale.y;
  3110. result[6] = rotation[6] * scale.z;
  3111. result[7] = rotation[7] * scale.z;
  3112. result[8] = rotation[8] * scale.z;
  3113. return result;
  3114. };
  3115. const scaleScratch5$2 = new Cartesian3();
  3116. /**
  3117. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  3118. *
  3119. * @param {Matrix3} matrix The matrix.
  3120. * @param {Matrix3} result The object onto which to store the result.
  3121. * @returns {Matrix3} The modified result parameter.
  3122. *
  3123. * @see Matrix3.setRotation
  3124. */
  3125. Matrix3.getRotation = function (matrix, result) {
  3126. //>>includeStart('debug', pragmas.debug);
  3127. RuntimeError.Check.typeOf.object("matrix", matrix);
  3128. RuntimeError.Check.typeOf.object("result", result);
  3129. //>>includeEnd('debug');
  3130. const scale = Matrix3.getScale(matrix, scaleScratch5$2);
  3131. result[0] = matrix[0] / scale.x;
  3132. result[1] = matrix[1] / scale.x;
  3133. result[2] = matrix[2] / scale.x;
  3134. result[3] = matrix[3] / scale.y;
  3135. result[4] = matrix[4] / scale.y;
  3136. result[5] = matrix[5] / scale.y;
  3137. result[6] = matrix[6] / scale.z;
  3138. result[7] = matrix[7] / scale.z;
  3139. result[8] = matrix[8] / scale.z;
  3140. return result;
  3141. };
  3142. /**
  3143. * Computes the product of two matrices.
  3144. *
  3145. * @param {Matrix3} left The first matrix.
  3146. * @param {Matrix3} right The second matrix.
  3147. * @param {Matrix3} result The object onto which to store the result.
  3148. * @returns {Matrix3} The modified result parameter.
  3149. */
  3150. Matrix3.multiply = function (left, right, result) {
  3151. //>>includeStart('debug', pragmas.debug);
  3152. RuntimeError.Check.typeOf.object("left", left);
  3153. RuntimeError.Check.typeOf.object("right", right);
  3154. RuntimeError.Check.typeOf.object("result", result);
  3155. //>>includeEnd('debug');
  3156. const column0Row0 =
  3157. left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  3158. const column0Row1 =
  3159. left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  3160. const column0Row2 =
  3161. left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  3162. const column1Row0 =
  3163. left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  3164. const column1Row1 =
  3165. left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  3166. const column1Row2 =
  3167. left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  3168. const column2Row0 =
  3169. left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  3170. const column2Row1 =
  3171. left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  3172. const column2Row2 =
  3173. left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  3174. result[0] = column0Row0;
  3175. result[1] = column0Row1;
  3176. result[2] = column0Row2;
  3177. result[3] = column1Row0;
  3178. result[4] = column1Row1;
  3179. result[5] = column1Row2;
  3180. result[6] = column2Row0;
  3181. result[7] = column2Row1;
  3182. result[8] = column2Row2;
  3183. return result;
  3184. };
  3185. /**
  3186. * Computes the sum of two matrices.
  3187. *
  3188. * @param {Matrix3} left The first matrix.
  3189. * @param {Matrix3} right The second matrix.
  3190. * @param {Matrix3} result The object onto which to store the result.
  3191. * @returns {Matrix3} The modified result parameter.
  3192. */
  3193. Matrix3.add = function (left, right, result) {
  3194. //>>includeStart('debug', pragmas.debug);
  3195. RuntimeError.Check.typeOf.object("left", left);
  3196. RuntimeError.Check.typeOf.object("right", right);
  3197. RuntimeError.Check.typeOf.object("result", result);
  3198. //>>includeEnd('debug');
  3199. result[0] = left[0] + right[0];
  3200. result[1] = left[1] + right[1];
  3201. result[2] = left[2] + right[2];
  3202. result[3] = left[3] + right[3];
  3203. result[4] = left[4] + right[4];
  3204. result[5] = left[5] + right[5];
  3205. result[6] = left[6] + right[6];
  3206. result[7] = left[7] + right[7];
  3207. result[8] = left[8] + right[8];
  3208. return result;
  3209. };
  3210. /**
  3211. * Computes the difference of two matrices.
  3212. *
  3213. * @param {Matrix3} left The first matrix.
  3214. * @param {Matrix3} right The second matrix.
  3215. * @param {Matrix3} result The object onto which to store the result.
  3216. * @returns {Matrix3} The modified result parameter.
  3217. */
  3218. Matrix3.subtract = function (left, right, result) {
  3219. //>>includeStart('debug', pragmas.debug);
  3220. RuntimeError.Check.typeOf.object("left", left);
  3221. RuntimeError.Check.typeOf.object("right", right);
  3222. RuntimeError.Check.typeOf.object("result", result);
  3223. //>>includeEnd('debug');
  3224. result[0] = left[0] - right[0];
  3225. result[1] = left[1] - right[1];
  3226. result[2] = left[2] - right[2];
  3227. result[3] = left[3] - right[3];
  3228. result[4] = left[4] - right[4];
  3229. result[5] = left[5] - right[5];
  3230. result[6] = left[6] - right[6];
  3231. result[7] = left[7] - right[7];
  3232. result[8] = left[8] - right[8];
  3233. return result;
  3234. };
  3235. /**
  3236. * Computes the product of a matrix and a column vector.
  3237. *
  3238. * @param {Matrix3} matrix The matrix.
  3239. * @param {Cartesian3} cartesian The column.
  3240. * @param {Cartesian3} result The object onto which to store the result.
  3241. * @returns {Cartesian3} The modified result parameter.
  3242. */
  3243. Matrix3.multiplyByVector = function (matrix, cartesian, result) {
  3244. //>>includeStart('debug', pragmas.debug);
  3245. RuntimeError.Check.typeOf.object("matrix", matrix);
  3246. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  3247. RuntimeError.Check.typeOf.object("result", result);
  3248. //>>includeEnd('debug');
  3249. const vX = cartesian.x;
  3250. const vY = cartesian.y;
  3251. const vZ = cartesian.z;
  3252. const x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  3253. const y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  3254. const z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  3255. result.x = x;
  3256. result.y = y;
  3257. result.z = z;
  3258. return result;
  3259. };
  3260. /**
  3261. * Computes the product of a matrix and a scalar.
  3262. *
  3263. * @param {Matrix3} matrix The matrix.
  3264. * @param {Number} scalar The number to multiply by.
  3265. * @param {Matrix3} result The object onto which to store the result.
  3266. * @returns {Matrix3} The modified result parameter.
  3267. */
  3268. Matrix3.multiplyByScalar = function (matrix, scalar, result) {
  3269. //>>includeStart('debug', pragmas.debug);
  3270. RuntimeError.Check.typeOf.object("matrix", matrix);
  3271. RuntimeError.Check.typeOf.number("scalar", scalar);
  3272. RuntimeError.Check.typeOf.object("result", result);
  3273. //>>includeEnd('debug');
  3274. result[0] = matrix[0] * scalar;
  3275. result[1] = matrix[1] * scalar;
  3276. result[2] = matrix[2] * scalar;
  3277. result[3] = matrix[3] * scalar;
  3278. result[4] = matrix[4] * scalar;
  3279. result[5] = matrix[5] * scalar;
  3280. result[6] = matrix[6] * scalar;
  3281. result[7] = matrix[7] * scalar;
  3282. result[8] = matrix[8] * scalar;
  3283. return result;
  3284. };
  3285. /**
  3286. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  3287. *
  3288. * @param {Matrix3} matrix The matrix on the left-hand side.
  3289. * @param {Number} scale The non-uniform scale on the right-hand side.
  3290. * @param {Matrix3} result The object onto which to store the result.
  3291. * @returns {Matrix3} The modified result parameter.
  3292. *
  3293. *
  3294. * @example
  3295. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  3296. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  3297. *
  3298. * @see Matrix3.multiplyByUniformScale
  3299. * @see Matrix3.fromScale
  3300. * @see Matrix3.fromUniformScale
  3301. * @see Matrix3.setScale
  3302. * @see Matrix3.setUniformScale
  3303. * @see Matrix3.getScale
  3304. */
  3305. Matrix3.multiplyByScale = function (matrix, scale, result) {
  3306. //>>includeStart('debug', pragmas.debug);
  3307. RuntimeError.Check.typeOf.object("matrix", matrix);
  3308. RuntimeError.Check.typeOf.object("scale", scale);
  3309. RuntimeError.Check.typeOf.object("result", result);
  3310. //>>includeEnd('debug');
  3311. result[0] = matrix[0] * scale.x;
  3312. result[1] = matrix[1] * scale.x;
  3313. result[2] = matrix[2] * scale.x;
  3314. result[3] = matrix[3] * scale.y;
  3315. result[4] = matrix[4] * scale.y;
  3316. result[5] = matrix[5] * scale.y;
  3317. result[6] = matrix[6] * scale.z;
  3318. result[7] = matrix[7] * scale.z;
  3319. result[8] = matrix[8] * scale.z;
  3320. return result;
  3321. };
  3322. /**
  3323. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  3324. *
  3325. * @param {Matrix3} matrix The matrix on the left-hand side.
  3326. * @param {Number} scale The uniform scale on the right-hand side.
  3327. * @param {Matrix3} result The object onto which to store the result.
  3328. * @returns {Matrix3} The modified result parameter.
  3329. *
  3330. * @example
  3331. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromUniformScale(scale), m);
  3332. * Cesium.Matrix3.multiplyByUniformScale(m, scale, m);
  3333. *
  3334. * @see Matrix3.multiplyByScale
  3335. * @see Matrix3.fromScale
  3336. * @see Matrix3.fromUniformScale
  3337. * @see Matrix3.setScale
  3338. * @see Matrix3.setUniformScale
  3339. * @see Matrix3.getScale
  3340. */
  3341. Matrix3.multiplyByUniformScale = function (matrix, scale, result) {
  3342. //>>includeStart('debug', pragmas.debug);
  3343. RuntimeError.Check.typeOf.object("matrix", matrix);
  3344. RuntimeError.Check.typeOf.number("scale", scale);
  3345. RuntimeError.Check.typeOf.object("result", result);
  3346. //>>includeEnd('debug');
  3347. result[0] = matrix[0] * scale;
  3348. result[1] = matrix[1] * scale;
  3349. result[2] = matrix[2] * scale;
  3350. result[3] = matrix[3] * scale;
  3351. result[4] = matrix[4] * scale;
  3352. result[5] = matrix[5] * scale;
  3353. result[6] = matrix[6] * scale;
  3354. result[7] = matrix[7] * scale;
  3355. result[8] = matrix[8] * scale;
  3356. return result;
  3357. };
  3358. /**
  3359. * Creates a negated copy of the provided matrix.
  3360. *
  3361. * @param {Matrix3} matrix The matrix to negate.
  3362. * @param {Matrix3} result The object onto which to store the result.
  3363. * @returns {Matrix3} The modified result parameter.
  3364. */
  3365. Matrix3.negate = function (matrix, result) {
  3366. //>>includeStart('debug', pragmas.debug);
  3367. RuntimeError.Check.typeOf.object("matrix", matrix);
  3368. RuntimeError.Check.typeOf.object("result", result);
  3369. //>>includeEnd('debug');
  3370. result[0] = -matrix[0];
  3371. result[1] = -matrix[1];
  3372. result[2] = -matrix[2];
  3373. result[3] = -matrix[3];
  3374. result[4] = -matrix[4];
  3375. result[5] = -matrix[5];
  3376. result[6] = -matrix[6];
  3377. result[7] = -matrix[7];
  3378. result[8] = -matrix[8];
  3379. return result;
  3380. };
  3381. /**
  3382. * Computes the transpose of the provided matrix.
  3383. *
  3384. * @param {Matrix3} matrix The matrix to transpose.
  3385. * @param {Matrix3} result The object onto which to store the result.
  3386. * @returns {Matrix3} The modified result parameter.
  3387. */
  3388. Matrix3.transpose = function (matrix, result) {
  3389. //>>includeStart('debug', pragmas.debug);
  3390. RuntimeError.Check.typeOf.object("matrix", matrix);
  3391. RuntimeError.Check.typeOf.object("result", result);
  3392. //>>includeEnd('debug');
  3393. const column0Row0 = matrix[0];
  3394. const column0Row1 = matrix[3];
  3395. const column0Row2 = matrix[6];
  3396. const column1Row0 = matrix[1];
  3397. const column1Row1 = matrix[4];
  3398. const column1Row2 = matrix[7];
  3399. const column2Row0 = matrix[2];
  3400. const column2Row1 = matrix[5];
  3401. const column2Row2 = matrix[8];
  3402. result[0] = column0Row0;
  3403. result[1] = column0Row1;
  3404. result[2] = column0Row2;
  3405. result[3] = column1Row0;
  3406. result[4] = column1Row1;
  3407. result[5] = column1Row2;
  3408. result[6] = column2Row0;
  3409. result[7] = column2Row1;
  3410. result[8] = column2Row2;
  3411. return result;
  3412. };
  3413. function computeFrobeniusNorm(matrix) {
  3414. let norm = 0.0;
  3415. for (let i = 0; i < 9; ++i) {
  3416. const temp = matrix[i];
  3417. norm += temp * temp;
  3418. }
  3419. return Math.sqrt(norm);
  3420. }
  3421. const rowVal = [1, 0, 0];
  3422. const colVal = [2, 2, 1];
  3423. function offDiagonalFrobeniusNorm(matrix) {
  3424. // Computes the "off-diagonal" Frobenius norm.
  3425. // Assumes matrix is symmetric.
  3426. let norm = 0.0;
  3427. for (let i = 0; i < 3; ++i) {
  3428. const temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  3429. norm += 2.0 * temp * temp;
  3430. }
  3431. return Math.sqrt(norm);
  3432. }
  3433. function shurDecomposition(matrix, result) {
  3434. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  3435. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  3436. //
  3437. // The routine takes a matrix, which is assumed to be symmetric, and
  3438. // finds the largest off-diagonal term, and then creates
  3439. // a matrix (result) which can be used to help reduce it
  3440. const tolerance = ComponentDatatype.CesiumMath.EPSILON15;
  3441. let maxDiagonal = 0.0;
  3442. let rotAxis = 1;
  3443. // find pivot (rotAxis) based on max diagonal of matrix
  3444. for (let i = 0; i < 3; ++i) {
  3445. const temp = Math.abs(
  3446. matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]
  3447. );
  3448. if (temp > maxDiagonal) {
  3449. rotAxis = i;
  3450. maxDiagonal = temp;
  3451. }
  3452. }
  3453. let c = 1.0;
  3454. let s = 0.0;
  3455. const p = rowVal[rotAxis];
  3456. const q = colVal[rotAxis];
  3457. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  3458. const qq = matrix[Matrix3.getElementIndex(q, q)];
  3459. const pp = matrix[Matrix3.getElementIndex(p, p)];
  3460. const qp = matrix[Matrix3.getElementIndex(q, p)];
  3461. const tau = (qq - pp) / 2.0 / qp;
  3462. let t;
  3463. if (tau < 0.0) {
  3464. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  3465. } else {
  3466. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  3467. }
  3468. c = 1.0 / Math.sqrt(1.0 + t * t);
  3469. s = t * c;
  3470. }
  3471. result = Matrix3.clone(Matrix3.IDENTITY, result);
  3472. result[Matrix3.getElementIndex(p, p)] = result[
  3473. Matrix3.getElementIndex(q, q)
  3474. ] = c;
  3475. result[Matrix3.getElementIndex(q, p)] = s;
  3476. result[Matrix3.getElementIndex(p, q)] = -s;
  3477. return result;
  3478. }
  3479. const jMatrix = new Matrix3();
  3480. const jMatrixTranspose = new Matrix3();
  3481. /**
  3482. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  3483. * <p>
  3484. * Returns a diagonal matrix and unitary matrix such that:
  3485. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  3486. * </p>
  3487. * <p>
  3488. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  3489. * of the unitary matrix are the corresponding eigenvectors.
  3490. * </p>
  3491. *
  3492. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  3493. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  3494. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  3495. *
  3496. * @example
  3497. * const a = //... symetric matrix
  3498. * const result = {
  3499. * unitary : new Cesium.Matrix3(),
  3500. * diagonal : new Cesium.Matrix3()
  3501. * };
  3502. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  3503. *
  3504. * const unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  3505. * const b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  3506. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  3507. *
  3508. * const lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  3509. * const v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  3510. * const c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  3511. */
  3512. Matrix3.computeEigenDecomposition = function (matrix, result) {
  3513. //>>includeStart('debug', pragmas.debug);
  3514. RuntimeError.Check.typeOf.object("matrix", matrix);
  3515. //>>includeEnd('debug');
  3516. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  3517. // section 8.4.3 The Classical Jacobi Algorithm
  3518. const tolerance = ComponentDatatype.CesiumMath.EPSILON20;
  3519. const maxSweeps = 10;
  3520. let count = 0;
  3521. let sweep = 0;
  3522. if (!defaultValue.defined(result)) {
  3523. result = {};
  3524. }
  3525. const unitaryMatrix = (result.unitary = Matrix3.clone(
  3526. Matrix3.IDENTITY,
  3527. result.unitary
  3528. ));
  3529. const diagMatrix = (result.diagonal = Matrix3.clone(matrix, result.diagonal));
  3530. const epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  3531. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  3532. shurDecomposition(diagMatrix, jMatrix);
  3533. Matrix3.transpose(jMatrix, jMatrixTranspose);
  3534. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  3535. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  3536. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  3537. if (++count > 2) {
  3538. ++sweep;
  3539. count = 0;
  3540. }
  3541. }
  3542. return result;
  3543. };
  3544. /**
  3545. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  3546. *
  3547. * @param {Matrix3} matrix The matrix with signed elements.
  3548. * @param {Matrix3} result The object onto which to store the result.
  3549. * @returns {Matrix3} The modified result parameter.
  3550. */
  3551. Matrix3.abs = function (matrix, result) {
  3552. //>>includeStart('debug', pragmas.debug);
  3553. RuntimeError.Check.typeOf.object("matrix", matrix);
  3554. RuntimeError.Check.typeOf.object("result", result);
  3555. //>>includeEnd('debug');
  3556. result[0] = Math.abs(matrix[0]);
  3557. result[1] = Math.abs(matrix[1]);
  3558. result[2] = Math.abs(matrix[2]);
  3559. result[3] = Math.abs(matrix[3]);
  3560. result[4] = Math.abs(matrix[4]);
  3561. result[5] = Math.abs(matrix[5]);
  3562. result[6] = Math.abs(matrix[6]);
  3563. result[7] = Math.abs(matrix[7]);
  3564. result[8] = Math.abs(matrix[8]);
  3565. return result;
  3566. };
  3567. /**
  3568. * Computes the determinant of the provided matrix.
  3569. *
  3570. * @param {Matrix3} matrix The matrix to use.
  3571. * @returns {Number} The value of the determinant of the matrix.
  3572. */
  3573. Matrix3.determinant = function (matrix) {
  3574. //>>includeStart('debug', pragmas.debug);
  3575. RuntimeError.Check.typeOf.object("matrix", matrix);
  3576. //>>includeEnd('debug');
  3577. const m11 = matrix[0];
  3578. const m21 = matrix[3];
  3579. const m31 = matrix[6];
  3580. const m12 = matrix[1];
  3581. const m22 = matrix[4];
  3582. const m32 = matrix[7];
  3583. const m13 = matrix[2];
  3584. const m23 = matrix[5];
  3585. const m33 = matrix[8];
  3586. return (
  3587. m11 * (m22 * m33 - m23 * m32) +
  3588. m12 * (m23 * m31 - m21 * m33) +
  3589. m13 * (m21 * m32 - m22 * m31)
  3590. );
  3591. };
  3592. /**
  3593. * Computes the inverse of the provided matrix.
  3594. *
  3595. * @param {Matrix3} matrix The matrix to invert.
  3596. * @param {Matrix3} result The object onto which to store the result.
  3597. * @returns {Matrix3} The modified result parameter.
  3598. *
  3599. * @exception {DeveloperError} matrix is not invertible.
  3600. */
  3601. Matrix3.inverse = function (matrix, result) {
  3602. //>>includeStart('debug', pragmas.debug);
  3603. RuntimeError.Check.typeOf.object("matrix", matrix);
  3604. RuntimeError.Check.typeOf.object("result", result);
  3605. //>>includeEnd('debug');
  3606. const m11 = matrix[0];
  3607. const m21 = matrix[1];
  3608. const m31 = matrix[2];
  3609. const m12 = matrix[3];
  3610. const m22 = matrix[4];
  3611. const m32 = matrix[5];
  3612. const m13 = matrix[6];
  3613. const m23 = matrix[7];
  3614. const m33 = matrix[8];
  3615. const determinant = Matrix3.determinant(matrix);
  3616. //>>includeStart('debug', pragmas.debug);
  3617. if (Math.abs(determinant) <= ComponentDatatype.CesiumMath.EPSILON15) {
  3618. throw new RuntimeError.DeveloperError("matrix is not invertible");
  3619. }
  3620. //>>includeEnd('debug');
  3621. result[0] = m22 * m33 - m23 * m32;
  3622. result[1] = m23 * m31 - m21 * m33;
  3623. result[2] = m21 * m32 - m22 * m31;
  3624. result[3] = m13 * m32 - m12 * m33;
  3625. result[4] = m11 * m33 - m13 * m31;
  3626. result[5] = m12 * m31 - m11 * m32;
  3627. result[6] = m12 * m23 - m13 * m22;
  3628. result[7] = m13 * m21 - m11 * m23;
  3629. result[8] = m11 * m22 - m12 * m21;
  3630. const scale = 1.0 / determinant;
  3631. return Matrix3.multiplyByScalar(result, scale, result);
  3632. };
  3633. const scratchTransposeMatrix$1 = new Matrix3();
  3634. /**
  3635. * Computes the inverse transpose of a matrix.
  3636. *
  3637. * @param {Matrix3} matrix The matrix to transpose and invert.
  3638. * @param {Matrix3} result The object onto which to store the result.
  3639. * @returns {Matrix3} The modified result parameter.
  3640. */
  3641. Matrix3.inverseTranspose = function (matrix, result) {
  3642. //>>includeStart('debug', pragmas.debug);
  3643. RuntimeError.Check.typeOf.object("matrix", matrix);
  3644. RuntimeError.Check.typeOf.object("result", result);
  3645. //>>includeEnd('debug');
  3646. return Matrix3.inverse(
  3647. Matrix3.transpose(matrix, scratchTransposeMatrix$1),
  3648. result
  3649. );
  3650. };
  3651. /**
  3652. * Compares the provided matrices componentwise and returns
  3653. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3654. *
  3655. * @param {Matrix3} [left] The first matrix.
  3656. * @param {Matrix3} [right] The second matrix.
  3657. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3658. */
  3659. Matrix3.equals = function (left, right) {
  3660. return (
  3661. left === right ||
  3662. (defaultValue.defined(left) &&
  3663. defaultValue.defined(right) &&
  3664. left[0] === right[0] &&
  3665. left[1] === right[1] &&
  3666. left[2] === right[2] &&
  3667. left[3] === right[3] &&
  3668. left[4] === right[4] &&
  3669. left[5] === right[5] &&
  3670. left[6] === right[6] &&
  3671. left[7] === right[7] &&
  3672. left[8] === right[8])
  3673. );
  3674. };
  3675. /**
  3676. * Compares the provided matrices componentwise and returns
  3677. * <code>true</code> if they are within the provided epsilon,
  3678. * <code>false</code> otherwise.
  3679. *
  3680. * @param {Matrix3} [left] The first matrix.
  3681. * @param {Matrix3} [right] The second matrix.
  3682. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  3683. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3684. */
  3685. Matrix3.equalsEpsilon = function (left, right, epsilon) {
  3686. epsilon = defaultValue.defaultValue(epsilon, 0);
  3687. return (
  3688. left === right ||
  3689. (defaultValue.defined(left) &&
  3690. defaultValue.defined(right) &&
  3691. Math.abs(left[0] - right[0]) <= epsilon &&
  3692. Math.abs(left[1] - right[1]) <= epsilon &&
  3693. Math.abs(left[2] - right[2]) <= epsilon &&
  3694. Math.abs(left[3] - right[3]) <= epsilon &&
  3695. Math.abs(left[4] - right[4]) <= epsilon &&
  3696. Math.abs(left[5] - right[5]) <= epsilon &&
  3697. Math.abs(left[6] - right[6]) <= epsilon &&
  3698. Math.abs(left[7] - right[7]) <= epsilon &&
  3699. Math.abs(left[8] - right[8]) <= epsilon)
  3700. );
  3701. };
  3702. /**
  3703. * An immutable Matrix3 instance initialized to the identity matrix.
  3704. *
  3705. * @type {Matrix3}
  3706. * @constant
  3707. */
  3708. Matrix3.IDENTITY = Object.freeze(
  3709. new Matrix3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  3710. );
  3711. /**
  3712. * An immutable Matrix3 instance initialized to the zero matrix.
  3713. *
  3714. * @type {Matrix3}
  3715. * @constant
  3716. */
  3717. Matrix3.ZERO = Object.freeze(
  3718. new Matrix3(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  3719. );
  3720. /**
  3721. * The index into Matrix3 for column 0, row 0.
  3722. *
  3723. * @type {Number}
  3724. * @constant
  3725. */
  3726. Matrix3.COLUMN0ROW0 = 0;
  3727. /**
  3728. * The index into Matrix3 for column 0, row 1.
  3729. *
  3730. * @type {Number}
  3731. * @constant
  3732. */
  3733. Matrix3.COLUMN0ROW1 = 1;
  3734. /**
  3735. * The index into Matrix3 for column 0, row 2.
  3736. *
  3737. * @type {Number}
  3738. * @constant
  3739. */
  3740. Matrix3.COLUMN0ROW2 = 2;
  3741. /**
  3742. * The index into Matrix3 for column 1, row 0.
  3743. *
  3744. * @type {Number}
  3745. * @constant
  3746. */
  3747. Matrix3.COLUMN1ROW0 = 3;
  3748. /**
  3749. * The index into Matrix3 for column 1, row 1.
  3750. *
  3751. * @type {Number}
  3752. * @constant
  3753. */
  3754. Matrix3.COLUMN1ROW1 = 4;
  3755. /**
  3756. * The index into Matrix3 for column 1, row 2.
  3757. *
  3758. * @type {Number}
  3759. * @constant
  3760. */
  3761. Matrix3.COLUMN1ROW2 = 5;
  3762. /**
  3763. * The index into Matrix3 for column 2, row 0.
  3764. *
  3765. * @type {Number}
  3766. * @constant
  3767. */
  3768. Matrix3.COLUMN2ROW0 = 6;
  3769. /**
  3770. * The index into Matrix3 for column 2, row 1.
  3771. *
  3772. * @type {Number}
  3773. * @constant
  3774. */
  3775. Matrix3.COLUMN2ROW1 = 7;
  3776. /**
  3777. * The index into Matrix3 for column 2, row 2.
  3778. *
  3779. * @type {Number}
  3780. * @constant
  3781. */
  3782. Matrix3.COLUMN2ROW2 = 8;
  3783. Object.defineProperties(Matrix3.prototype, {
  3784. /**
  3785. * Gets the number of items in the collection.
  3786. * @memberof Matrix3.prototype
  3787. *
  3788. * @type {Number}
  3789. */
  3790. length: {
  3791. get: function () {
  3792. return Matrix3.packedLength;
  3793. },
  3794. },
  3795. });
  3796. /**
  3797. * Duplicates the provided Matrix3 instance.
  3798. *
  3799. * @param {Matrix3} [result] The object onto which to store the result.
  3800. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  3801. */
  3802. Matrix3.prototype.clone = function (result) {
  3803. return Matrix3.clone(this, result);
  3804. };
  3805. /**
  3806. * Compares this matrix to the provided matrix componentwise and returns
  3807. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3808. *
  3809. * @param {Matrix3} [right] The right hand side matrix.
  3810. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3811. */
  3812. Matrix3.prototype.equals = function (right) {
  3813. return Matrix3.equals(this, right);
  3814. };
  3815. /**
  3816. * @private
  3817. */
  3818. Matrix3.equalsArray = function (matrix, array, offset) {
  3819. return (
  3820. matrix[0] === array[offset] &&
  3821. matrix[1] === array[offset + 1] &&
  3822. matrix[2] === array[offset + 2] &&
  3823. matrix[3] === array[offset + 3] &&
  3824. matrix[4] === array[offset + 4] &&
  3825. matrix[5] === array[offset + 5] &&
  3826. matrix[6] === array[offset + 6] &&
  3827. matrix[7] === array[offset + 7] &&
  3828. matrix[8] === array[offset + 8]
  3829. );
  3830. };
  3831. /**
  3832. * Compares this matrix to the provided matrix componentwise and returns
  3833. * <code>true</code> if they are within the provided epsilon,
  3834. * <code>false</code> otherwise.
  3835. *
  3836. * @param {Matrix3} [right] The right hand side matrix.
  3837. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  3838. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  3839. */
  3840. Matrix3.prototype.equalsEpsilon = function (right, epsilon) {
  3841. return Matrix3.equalsEpsilon(this, right, epsilon);
  3842. };
  3843. /**
  3844. * Creates a string representing this Matrix with each row being
  3845. * on a separate line and in the format '(column0, column1, column2)'.
  3846. *
  3847. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  3848. */
  3849. Matrix3.prototype.toString = function () {
  3850. return (
  3851. `(${this[0]}, ${this[3]}, ${this[6]})\n` +
  3852. `(${this[1]}, ${this[4]}, ${this[7]})\n` +
  3853. `(${this[2]}, ${this[5]}, ${this[8]})`
  3854. );
  3855. };
  3856. /**
  3857. * A 4D Cartesian point.
  3858. * @alias Cartesian4
  3859. * @constructor
  3860. *
  3861. * @param {Number} [x=0.0] The X component.
  3862. * @param {Number} [y=0.0] The Y component.
  3863. * @param {Number} [z=0.0] The Z component.
  3864. * @param {Number} [w=0.0] The W component.
  3865. *
  3866. * @see Cartesian2
  3867. * @see Cartesian3
  3868. * @see Packable
  3869. */
  3870. function Cartesian4(x, y, z, w) {
  3871. /**
  3872. * The X component.
  3873. * @type {Number}
  3874. * @default 0.0
  3875. */
  3876. this.x = defaultValue.defaultValue(x, 0.0);
  3877. /**
  3878. * The Y component.
  3879. * @type {Number}
  3880. * @default 0.0
  3881. */
  3882. this.y = defaultValue.defaultValue(y, 0.0);
  3883. /**
  3884. * The Z component.
  3885. * @type {Number}
  3886. * @default 0.0
  3887. */
  3888. this.z = defaultValue.defaultValue(z, 0.0);
  3889. /**
  3890. * The W component.
  3891. * @type {Number}
  3892. * @default 0.0
  3893. */
  3894. this.w = defaultValue.defaultValue(w, 0.0);
  3895. }
  3896. /**
  3897. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  3898. *
  3899. * @param {Number} x The x coordinate.
  3900. * @param {Number} y The y coordinate.
  3901. * @param {Number} z The z coordinate.
  3902. * @param {Number} w The w coordinate.
  3903. * @param {Cartesian4} [result] The object onto which to store the result.
  3904. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3905. */
  3906. Cartesian4.fromElements = function (x, y, z, w, result) {
  3907. if (!defaultValue.defined(result)) {
  3908. return new Cartesian4(x, y, z, w);
  3909. }
  3910. result.x = x;
  3911. result.y = y;
  3912. result.z = z;
  3913. result.w = w;
  3914. return result;
  3915. };
  3916. /**
  3917. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  3918. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  3919. *
  3920. * @param {Color} color The source color.
  3921. * @param {Cartesian4} [result] The object onto which to store the result.
  3922. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3923. */
  3924. Cartesian4.fromColor = function (color, result) {
  3925. //>>includeStart('debug', pragmas.debug);
  3926. RuntimeError.Check.typeOf.object("color", color);
  3927. //>>includeEnd('debug');
  3928. if (!defaultValue.defined(result)) {
  3929. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  3930. }
  3931. result.x = color.red;
  3932. result.y = color.green;
  3933. result.z = color.blue;
  3934. result.w = color.alpha;
  3935. return result;
  3936. };
  3937. /**
  3938. * Duplicates a Cartesian4 instance.
  3939. *
  3940. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  3941. * @param {Cartesian4} [result] The object onto which to store the result.
  3942. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  3943. */
  3944. Cartesian4.clone = function (cartesian, result) {
  3945. if (!defaultValue.defined(cartesian)) {
  3946. return undefined;
  3947. }
  3948. if (!defaultValue.defined(result)) {
  3949. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  3950. }
  3951. result.x = cartesian.x;
  3952. result.y = cartesian.y;
  3953. result.z = cartesian.z;
  3954. result.w = cartesian.w;
  3955. return result;
  3956. };
  3957. /**
  3958. * The number of elements used to pack the object into an array.
  3959. * @type {Number}
  3960. */
  3961. Cartesian4.packedLength = 4;
  3962. /**
  3963. * Stores the provided instance into the provided array.
  3964. *
  3965. * @param {Cartesian4} value The value to pack.
  3966. * @param {Number[]} array The array to pack into.
  3967. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  3968. *
  3969. * @returns {Number[]} The array that was packed into
  3970. */
  3971. Cartesian4.pack = function (value, array, startingIndex) {
  3972. //>>includeStart('debug', pragmas.debug);
  3973. RuntimeError.Check.typeOf.object("value", value);
  3974. RuntimeError.Check.defined("array", array);
  3975. //>>includeEnd('debug');
  3976. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3977. array[startingIndex++] = value.x;
  3978. array[startingIndex++] = value.y;
  3979. array[startingIndex++] = value.z;
  3980. array[startingIndex] = value.w;
  3981. return array;
  3982. };
  3983. /**
  3984. * Retrieves an instance from a packed array.
  3985. *
  3986. * @param {Number[]} array The packed array.
  3987. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  3988. * @param {Cartesian4} [result] The object into which to store the result.
  3989. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3990. */
  3991. Cartesian4.unpack = function (array, startingIndex, result) {
  3992. //>>includeStart('debug', pragmas.debug);
  3993. RuntimeError.Check.defined("array", array);
  3994. //>>includeEnd('debug');
  3995. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3996. if (!defaultValue.defined(result)) {
  3997. result = new Cartesian4();
  3998. }
  3999. result.x = array[startingIndex++];
  4000. result.y = array[startingIndex++];
  4001. result.z = array[startingIndex++];
  4002. result.w = array[startingIndex];
  4003. return result;
  4004. };
  4005. /**
  4006. * Flattens an array of Cartesian4s into an array of components.
  4007. *
  4008. * @param {Cartesian4[]} array The array of cartesians to pack.
  4009. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 4 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 4) elements.
  4010. * @returns {Number[]} The packed array.
  4011. */
  4012. Cartesian4.packArray = function (array, result) {
  4013. //>>includeStart('debug', pragmas.debug);
  4014. RuntimeError.Check.defined("array", array);
  4015. //>>includeEnd('debug');
  4016. const length = array.length;
  4017. const resultLength = length * 4;
  4018. if (!defaultValue.defined(result)) {
  4019. result = new Array(resultLength);
  4020. } else if (!Array.isArray(result) && result.length !== resultLength) {
  4021. //>>includeStart('debug', pragmas.debug);
  4022. throw new RuntimeError.DeveloperError(
  4023. "If result is a typed array, it must have exactly array.length * 4 elements"
  4024. );
  4025. //>>includeEnd('debug');
  4026. } else if (result.length !== resultLength) {
  4027. result.length = resultLength;
  4028. }
  4029. for (let i = 0; i < length; ++i) {
  4030. Cartesian4.pack(array[i], result, i * 4);
  4031. }
  4032. return result;
  4033. };
  4034. /**
  4035. * Unpacks an array of cartesian components into an array of Cartesian4s.
  4036. *
  4037. * @param {Number[]} array The array of components to unpack.
  4038. * @param {Cartesian4[]} [result] The array onto which to store the result.
  4039. * @returns {Cartesian4[]} The unpacked array.
  4040. */
  4041. Cartesian4.unpackArray = function (array, result) {
  4042. //>>includeStart('debug', pragmas.debug);
  4043. RuntimeError.Check.defined("array", array);
  4044. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  4045. if (array.length % 4 !== 0) {
  4046. throw new RuntimeError.DeveloperError("array length must be a multiple of 4.");
  4047. }
  4048. //>>includeEnd('debug');
  4049. const length = array.length;
  4050. if (!defaultValue.defined(result)) {
  4051. result = new Array(length / 4);
  4052. } else {
  4053. result.length = length / 4;
  4054. }
  4055. for (let i = 0; i < length; i += 4) {
  4056. const index = i / 4;
  4057. result[index] = Cartesian4.unpack(array, i, result[index]);
  4058. }
  4059. return result;
  4060. };
  4061. /**
  4062. * Creates a Cartesian4 from four consecutive elements in an array.
  4063. * @function
  4064. *
  4065. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  4066. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  4067. * @param {Cartesian4} [result] The object onto which to store the result.
  4068. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4069. *
  4070. * @example
  4071. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  4072. * const v = [1.0, 2.0, 3.0, 4.0];
  4073. * const p = Cesium.Cartesian4.fromArray(v);
  4074. *
  4075. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  4076. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  4077. * const p2 = Cesium.Cartesian4.fromArray(v2, 2);
  4078. */
  4079. Cartesian4.fromArray = Cartesian4.unpack;
  4080. /**
  4081. * Computes the value of the maximum component for the supplied Cartesian.
  4082. *
  4083. * @param {Cartesian4} cartesian The cartesian to use.
  4084. * @returns {Number} The value of the maximum component.
  4085. */
  4086. Cartesian4.maximumComponent = function (cartesian) {
  4087. //>>includeStart('debug', pragmas.debug);
  4088. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4089. //>>includeEnd('debug');
  4090. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4091. };
  4092. /**
  4093. * Computes the value of the minimum component for the supplied Cartesian.
  4094. *
  4095. * @param {Cartesian4} cartesian The cartesian to use.
  4096. * @returns {Number} The value of the minimum component.
  4097. */
  4098. Cartesian4.minimumComponent = function (cartesian) {
  4099. //>>includeStart('debug', pragmas.debug);
  4100. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4101. //>>includeEnd('debug');
  4102. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4103. };
  4104. /**
  4105. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  4106. *
  4107. * @param {Cartesian4} first A cartesian to compare.
  4108. * @param {Cartesian4} second A cartesian to compare.
  4109. * @param {Cartesian4} result The object into which to store the result.
  4110. * @returns {Cartesian4} A cartesian with the minimum components.
  4111. */
  4112. Cartesian4.minimumByComponent = function (first, second, result) {
  4113. //>>includeStart('debug', pragmas.debug);
  4114. RuntimeError.Check.typeOf.object("first", first);
  4115. RuntimeError.Check.typeOf.object("second", second);
  4116. RuntimeError.Check.typeOf.object("result", result);
  4117. //>>includeEnd('debug');
  4118. result.x = Math.min(first.x, second.x);
  4119. result.y = Math.min(first.y, second.y);
  4120. result.z = Math.min(first.z, second.z);
  4121. result.w = Math.min(first.w, second.w);
  4122. return result;
  4123. };
  4124. /**
  4125. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  4126. *
  4127. * @param {Cartesian4} first A cartesian to compare.
  4128. * @param {Cartesian4} second A cartesian to compare.
  4129. * @param {Cartesian4} result The object into which to store the result.
  4130. * @returns {Cartesian4} A cartesian with the maximum components.
  4131. */
  4132. Cartesian4.maximumByComponent = function (first, second, result) {
  4133. //>>includeStart('debug', pragmas.debug);
  4134. RuntimeError.Check.typeOf.object("first", first);
  4135. RuntimeError.Check.typeOf.object("second", second);
  4136. RuntimeError.Check.typeOf.object("result", result);
  4137. //>>includeEnd('debug');
  4138. result.x = Math.max(first.x, second.x);
  4139. result.y = Math.max(first.y, second.y);
  4140. result.z = Math.max(first.z, second.z);
  4141. result.w = Math.max(first.w, second.w);
  4142. return result;
  4143. };
  4144. /**
  4145. * Constrain a value to lie between two values.
  4146. *
  4147. * @param {Cartesian4} value The value to clamp.
  4148. * @param {Cartesian4} min The minimum bound.
  4149. * @param {Cartesian4} max The maximum bound.
  4150. * @param {Cartesian4} result The object into which to store the result.
  4151. * @returns {Cartesian4} The clamped value such that min <= result <= max.
  4152. */
  4153. Cartesian4.clamp = function (value, min, max, result) {
  4154. //>>includeStart('debug', pragmas.debug);
  4155. RuntimeError.Check.typeOf.object("value", value);
  4156. RuntimeError.Check.typeOf.object("min", min);
  4157. RuntimeError.Check.typeOf.object("max", max);
  4158. RuntimeError.Check.typeOf.object("result", result);
  4159. //>>includeEnd('debug');
  4160. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  4161. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  4162. const z = ComponentDatatype.CesiumMath.clamp(value.z, min.z, max.z);
  4163. const w = ComponentDatatype.CesiumMath.clamp(value.w, min.w, max.w);
  4164. result.x = x;
  4165. result.y = y;
  4166. result.z = z;
  4167. result.w = w;
  4168. return result;
  4169. };
  4170. /**
  4171. * Computes the provided Cartesian's squared magnitude.
  4172. *
  4173. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  4174. * @returns {Number} The squared magnitude.
  4175. */
  4176. Cartesian4.magnitudeSquared = function (cartesian) {
  4177. //>>includeStart('debug', pragmas.debug);
  4178. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4179. //>>includeEnd('debug');
  4180. return (
  4181. cartesian.x * cartesian.x +
  4182. cartesian.y * cartesian.y +
  4183. cartesian.z * cartesian.z +
  4184. cartesian.w * cartesian.w
  4185. );
  4186. };
  4187. /**
  4188. * Computes the Cartesian's magnitude (length).
  4189. *
  4190. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  4191. * @returns {Number} The magnitude.
  4192. */
  4193. Cartesian4.magnitude = function (cartesian) {
  4194. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  4195. };
  4196. const distanceScratch$1 = new Cartesian4();
  4197. /**
  4198. * Computes the 4-space distance between two points.
  4199. *
  4200. * @param {Cartesian4} left The first point to compute the distance from.
  4201. * @param {Cartesian4} right The second point to compute the distance to.
  4202. * @returns {Number} The distance between two points.
  4203. *
  4204. * @example
  4205. * // Returns 1.0
  4206. * const d = Cesium.Cartesian4.distance(
  4207. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4208. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  4209. */
  4210. Cartesian4.distance = function (left, right) {
  4211. //>>includeStart('debug', pragmas.debug);
  4212. RuntimeError.Check.typeOf.object("left", left);
  4213. RuntimeError.Check.typeOf.object("right", right);
  4214. //>>includeEnd('debug');
  4215. Cartesian4.subtract(left, right, distanceScratch$1);
  4216. return Cartesian4.magnitude(distanceScratch$1);
  4217. };
  4218. /**
  4219. * Computes the squared distance between two points. Comparing squared distances
  4220. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  4221. *
  4222. * @param {Cartesian4} left The first point to compute the distance from.
  4223. * @param {Cartesian4} right The second point to compute the distance to.
  4224. * @returns {Number} The distance between two points.
  4225. *
  4226. * @example
  4227. * // Returns 4.0, not 2.0
  4228. * const d = Cesium.Cartesian4.distance(
  4229. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4230. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  4231. */
  4232. Cartesian4.distanceSquared = function (left, right) {
  4233. //>>includeStart('debug', pragmas.debug);
  4234. RuntimeError.Check.typeOf.object("left", left);
  4235. RuntimeError.Check.typeOf.object("right", right);
  4236. //>>includeEnd('debug');
  4237. Cartesian4.subtract(left, right, distanceScratch$1);
  4238. return Cartesian4.magnitudeSquared(distanceScratch$1);
  4239. };
  4240. /**
  4241. * Computes the normalized form of the supplied Cartesian.
  4242. *
  4243. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  4244. * @param {Cartesian4} result The object onto which to store the result.
  4245. * @returns {Cartesian4} The modified result parameter.
  4246. */
  4247. Cartesian4.normalize = function (cartesian, result) {
  4248. //>>includeStart('debug', pragmas.debug);
  4249. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4250. RuntimeError.Check.typeOf.object("result", result);
  4251. //>>includeEnd('debug');
  4252. const magnitude = Cartesian4.magnitude(cartesian);
  4253. result.x = cartesian.x / magnitude;
  4254. result.y = cartesian.y / magnitude;
  4255. result.z = cartesian.z / magnitude;
  4256. result.w = cartesian.w / magnitude;
  4257. //>>includeStart('debug', pragmas.debug);
  4258. if (
  4259. isNaN(result.x) ||
  4260. isNaN(result.y) ||
  4261. isNaN(result.z) ||
  4262. isNaN(result.w)
  4263. ) {
  4264. throw new RuntimeError.DeveloperError("normalized result is not a number");
  4265. }
  4266. //>>includeEnd('debug');
  4267. return result;
  4268. };
  4269. /**
  4270. * Computes the dot (scalar) product of two Cartesians.
  4271. *
  4272. * @param {Cartesian4} left The first Cartesian.
  4273. * @param {Cartesian4} right The second Cartesian.
  4274. * @returns {Number} The dot product.
  4275. */
  4276. Cartesian4.dot = function (left, right) {
  4277. //>>includeStart('debug', pragmas.debug);
  4278. RuntimeError.Check.typeOf.object("left", left);
  4279. RuntimeError.Check.typeOf.object("right", right);
  4280. //>>includeEnd('debug');
  4281. return (
  4282. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  4283. );
  4284. };
  4285. /**
  4286. * Computes the componentwise product of two Cartesians.
  4287. *
  4288. * @param {Cartesian4} left The first Cartesian.
  4289. * @param {Cartesian4} right The second Cartesian.
  4290. * @param {Cartesian4} result The object onto which to store the result.
  4291. * @returns {Cartesian4} The modified result parameter.
  4292. */
  4293. Cartesian4.multiplyComponents = function (left, right, result) {
  4294. //>>includeStart('debug', pragmas.debug);
  4295. RuntimeError.Check.typeOf.object("left", left);
  4296. RuntimeError.Check.typeOf.object("right", right);
  4297. RuntimeError.Check.typeOf.object("result", result);
  4298. //>>includeEnd('debug');
  4299. result.x = left.x * right.x;
  4300. result.y = left.y * right.y;
  4301. result.z = left.z * right.z;
  4302. result.w = left.w * right.w;
  4303. return result;
  4304. };
  4305. /**
  4306. * Computes the componentwise quotient of two Cartesians.
  4307. *
  4308. * @param {Cartesian4} left The first Cartesian.
  4309. * @param {Cartesian4} right The second Cartesian.
  4310. * @param {Cartesian4} result The object onto which to store the result.
  4311. * @returns {Cartesian4} The modified result parameter.
  4312. */
  4313. Cartesian4.divideComponents = function (left, right, result) {
  4314. //>>includeStart('debug', pragmas.debug);
  4315. RuntimeError.Check.typeOf.object("left", left);
  4316. RuntimeError.Check.typeOf.object("right", right);
  4317. RuntimeError.Check.typeOf.object("result", result);
  4318. //>>includeEnd('debug');
  4319. result.x = left.x / right.x;
  4320. result.y = left.y / right.y;
  4321. result.z = left.z / right.z;
  4322. result.w = left.w / right.w;
  4323. return result;
  4324. };
  4325. /**
  4326. * Computes the componentwise sum of two Cartesians.
  4327. *
  4328. * @param {Cartesian4} left The first Cartesian.
  4329. * @param {Cartesian4} right The second Cartesian.
  4330. * @param {Cartesian4} result The object onto which to store the result.
  4331. * @returns {Cartesian4} The modified result parameter.
  4332. */
  4333. Cartesian4.add = function (left, right, result) {
  4334. //>>includeStart('debug', pragmas.debug);
  4335. RuntimeError.Check.typeOf.object("left", left);
  4336. RuntimeError.Check.typeOf.object("right", right);
  4337. RuntimeError.Check.typeOf.object("result", result);
  4338. //>>includeEnd('debug');
  4339. result.x = left.x + right.x;
  4340. result.y = left.y + right.y;
  4341. result.z = left.z + right.z;
  4342. result.w = left.w + right.w;
  4343. return result;
  4344. };
  4345. /**
  4346. * Computes the componentwise difference of two Cartesians.
  4347. *
  4348. * @param {Cartesian4} left The first Cartesian.
  4349. * @param {Cartesian4} right The second Cartesian.
  4350. * @param {Cartesian4} result The object onto which to store the result.
  4351. * @returns {Cartesian4} The modified result parameter.
  4352. */
  4353. Cartesian4.subtract = function (left, right, result) {
  4354. //>>includeStart('debug', pragmas.debug);
  4355. RuntimeError.Check.typeOf.object("left", left);
  4356. RuntimeError.Check.typeOf.object("right", right);
  4357. RuntimeError.Check.typeOf.object("result", result);
  4358. //>>includeEnd('debug');
  4359. result.x = left.x - right.x;
  4360. result.y = left.y - right.y;
  4361. result.z = left.z - right.z;
  4362. result.w = left.w - right.w;
  4363. return result;
  4364. };
  4365. /**
  4366. * Multiplies the provided Cartesian componentwise by the provided scalar.
  4367. *
  4368. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  4369. * @param {Number} scalar The scalar to multiply with.
  4370. * @param {Cartesian4} result The object onto which to store the result.
  4371. * @returns {Cartesian4} The modified result parameter.
  4372. */
  4373. Cartesian4.multiplyByScalar = function (cartesian, scalar, result) {
  4374. //>>includeStart('debug', pragmas.debug);
  4375. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4376. RuntimeError.Check.typeOf.number("scalar", scalar);
  4377. RuntimeError.Check.typeOf.object("result", result);
  4378. //>>includeEnd('debug');
  4379. result.x = cartesian.x * scalar;
  4380. result.y = cartesian.y * scalar;
  4381. result.z = cartesian.z * scalar;
  4382. result.w = cartesian.w * scalar;
  4383. return result;
  4384. };
  4385. /**
  4386. * Divides the provided Cartesian componentwise by the provided scalar.
  4387. *
  4388. * @param {Cartesian4} cartesian The Cartesian to be divided.
  4389. * @param {Number} scalar The scalar to divide by.
  4390. * @param {Cartesian4} result The object onto which to store the result.
  4391. * @returns {Cartesian4} The modified result parameter.
  4392. */
  4393. Cartesian4.divideByScalar = function (cartesian, scalar, result) {
  4394. //>>includeStart('debug', pragmas.debug);
  4395. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4396. RuntimeError.Check.typeOf.number("scalar", scalar);
  4397. RuntimeError.Check.typeOf.object("result", result);
  4398. //>>includeEnd('debug');
  4399. result.x = cartesian.x / scalar;
  4400. result.y = cartesian.y / scalar;
  4401. result.z = cartesian.z / scalar;
  4402. result.w = cartesian.w / scalar;
  4403. return result;
  4404. };
  4405. /**
  4406. * Negates the provided Cartesian.
  4407. *
  4408. * @param {Cartesian4} cartesian The Cartesian to be negated.
  4409. * @param {Cartesian4} result The object onto which to store the result.
  4410. * @returns {Cartesian4} The modified result parameter.
  4411. */
  4412. Cartesian4.negate = function (cartesian, result) {
  4413. //>>includeStart('debug', pragmas.debug);
  4414. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4415. RuntimeError.Check.typeOf.object("result", result);
  4416. //>>includeEnd('debug');
  4417. result.x = -cartesian.x;
  4418. result.y = -cartesian.y;
  4419. result.z = -cartesian.z;
  4420. result.w = -cartesian.w;
  4421. return result;
  4422. };
  4423. /**
  4424. * Computes the absolute value of the provided Cartesian.
  4425. *
  4426. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  4427. * @param {Cartesian4} result The object onto which to store the result.
  4428. * @returns {Cartesian4} The modified result parameter.
  4429. */
  4430. Cartesian4.abs = function (cartesian, result) {
  4431. //>>includeStart('debug', pragmas.debug);
  4432. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4433. RuntimeError.Check.typeOf.object("result", result);
  4434. //>>includeEnd('debug');
  4435. result.x = Math.abs(cartesian.x);
  4436. result.y = Math.abs(cartesian.y);
  4437. result.z = Math.abs(cartesian.z);
  4438. result.w = Math.abs(cartesian.w);
  4439. return result;
  4440. };
  4441. const lerpScratch$1 = new Cartesian4();
  4442. /**
  4443. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  4444. *
  4445. * @param {Cartesian4} start The value corresponding to t at 0.0.
  4446. * @param {Cartesian4}end The value corresponding to t at 1.0.
  4447. * @param {Number} t The point along t at which to interpolate.
  4448. * @param {Cartesian4} result The object onto which to store the result.
  4449. * @returns {Cartesian4} The modified result parameter.
  4450. */
  4451. Cartesian4.lerp = function (start, end, t, result) {
  4452. //>>includeStart('debug', pragmas.debug);
  4453. RuntimeError.Check.typeOf.object("start", start);
  4454. RuntimeError.Check.typeOf.object("end", end);
  4455. RuntimeError.Check.typeOf.number("t", t);
  4456. RuntimeError.Check.typeOf.object("result", result);
  4457. //>>includeEnd('debug');
  4458. Cartesian4.multiplyByScalar(end, t, lerpScratch$1);
  4459. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  4460. return Cartesian4.add(lerpScratch$1, result, result);
  4461. };
  4462. const mostOrthogonalAxisScratch$1 = new Cartesian4();
  4463. /**
  4464. * Returns the axis that is most orthogonal to the provided Cartesian.
  4465. *
  4466. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  4467. * @param {Cartesian4} result The object onto which to store the result.
  4468. * @returns {Cartesian4} The most orthogonal axis.
  4469. */
  4470. Cartesian4.mostOrthogonalAxis = function (cartesian, result) {
  4471. //>>includeStart('debug', pragmas.debug);
  4472. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4473. RuntimeError.Check.typeOf.object("result", result);
  4474. //>>includeEnd('debug');
  4475. const f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch$1);
  4476. Cartesian4.abs(f, f);
  4477. if (f.x <= f.y) {
  4478. if (f.x <= f.z) {
  4479. if (f.x <= f.w) {
  4480. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  4481. } else {
  4482. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4483. }
  4484. } else if (f.z <= f.w) {
  4485. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  4486. } else {
  4487. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4488. }
  4489. } else if (f.y <= f.z) {
  4490. if (f.y <= f.w) {
  4491. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  4492. } else {
  4493. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4494. }
  4495. } else if (f.z <= f.w) {
  4496. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  4497. } else {
  4498. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4499. }
  4500. return result;
  4501. };
  4502. /**
  4503. * Compares the provided Cartesians componentwise and returns
  4504. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4505. *
  4506. * @param {Cartesian4} [left] The first Cartesian.
  4507. * @param {Cartesian4} [right] The second Cartesian.
  4508. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  4509. */
  4510. Cartesian4.equals = function (left, right) {
  4511. return (
  4512. left === right ||
  4513. (defaultValue.defined(left) &&
  4514. defaultValue.defined(right) &&
  4515. left.x === right.x &&
  4516. left.y === right.y &&
  4517. left.z === right.z &&
  4518. left.w === right.w)
  4519. );
  4520. };
  4521. /**
  4522. * @private
  4523. */
  4524. Cartesian4.equalsArray = function (cartesian, array, offset) {
  4525. return (
  4526. cartesian.x === array[offset] &&
  4527. cartesian.y === array[offset + 1] &&
  4528. cartesian.z === array[offset + 2] &&
  4529. cartesian.w === array[offset + 3]
  4530. );
  4531. };
  4532. /**
  4533. * Compares the provided Cartesians componentwise and returns
  4534. * <code>true</code> if they pass an absolute or relative tolerance test,
  4535. * <code>false</code> otherwise.
  4536. *
  4537. * @param {Cartesian4} [left] The first Cartesian.
  4538. * @param {Cartesian4} [right] The second Cartesian.
  4539. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  4540. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  4541. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  4542. */
  4543. Cartesian4.equalsEpsilon = function (
  4544. left,
  4545. right,
  4546. relativeEpsilon,
  4547. absoluteEpsilon
  4548. ) {
  4549. return (
  4550. left === right ||
  4551. (defaultValue.defined(left) &&
  4552. defaultValue.defined(right) &&
  4553. ComponentDatatype.CesiumMath.equalsEpsilon(
  4554. left.x,
  4555. right.x,
  4556. relativeEpsilon,
  4557. absoluteEpsilon
  4558. ) &&
  4559. ComponentDatatype.CesiumMath.equalsEpsilon(
  4560. left.y,
  4561. right.y,
  4562. relativeEpsilon,
  4563. absoluteEpsilon
  4564. ) &&
  4565. ComponentDatatype.CesiumMath.equalsEpsilon(
  4566. left.z,
  4567. right.z,
  4568. relativeEpsilon,
  4569. absoluteEpsilon
  4570. ) &&
  4571. ComponentDatatype.CesiumMath.equalsEpsilon(
  4572. left.w,
  4573. right.w,
  4574. relativeEpsilon,
  4575. absoluteEpsilon
  4576. ))
  4577. );
  4578. };
  4579. /**
  4580. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  4581. *
  4582. * @type {Cartesian4}
  4583. * @constant
  4584. */
  4585. Cartesian4.ZERO = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  4586. /**
  4587. * An immutable Cartesian4 instance initialized to (1.0, 1.0, 1.0, 1.0).
  4588. *
  4589. * @type {Cartesian4}
  4590. * @constant
  4591. */
  4592. Cartesian4.ONE = Object.freeze(new Cartesian4(1.0, 1.0, 1.0, 1.0));
  4593. /**
  4594. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  4595. *
  4596. * @type {Cartesian4}
  4597. * @constant
  4598. */
  4599. Cartesian4.UNIT_X = Object.freeze(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  4600. /**
  4601. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  4602. *
  4603. * @type {Cartesian4}
  4604. * @constant
  4605. */
  4606. Cartesian4.UNIT_Y = Object.freeze(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  4607. /**
  4608. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  4609. *
  4610. * @type {Cartesian4}
  4611. * @constant
  4612. */
  4613. Cartesian4.UNIT_Z = Object.freeze(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  4614. /**
  4615. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  4616. *
  4617. * @type {Cartesian4}
  4618. * @constant
  4619. */
  4620. Cartesian4.UNIT_W = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  4621. /**
  4622. * Duplicates this Cartesian4 instance.
  4623. *
  4624. * @param {Cartesian4} [result] The object onto which to store the result.
  4625. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4626. */
  4627. Cartesian4.prototype.clone = function (result) {
  4628. return Cartesian4.clone(this, result);
  4629. };
  4630. /**
  4631. * Compares this Cartesian against the provided Cartesian componentwise and returns
  4632. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4633. *
  4634. * @param {Cartesian4} [right] The right hand side Cartesian.
  4635. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  4636. */
  4637. Cartesian4.prototype.equals = function (right) {
  4638. return Cartesian4.equals(this, right);
  4639. };
  4640. /**
  4641. * Compares this Cartesian against the provided Cartesian componentwise and returns
  4642. * <code>true</code> if they pass an absolute or relative tolerance test,
  4643. * <code>false</code> otherwise.
  4644. *
  4645. * @param {Cartesian4} [right] The right hand side Cartesian.
  4646. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  4647. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  4648. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  4649. */
  4650. Cartesian4.prototype.equalsEpsilon = function (
  4651. right,
  4652. relativeEpsilon,
  4653. absoluteEpsilon
  4654. ) {
  4655. return Cartesian4.equalsEpsilon(
  4656. this,
  4657. right,
  4658. relativeEpsilon,
  4659. absoluteEpsilon
  4660. );
  4661. };
  4662. /**
  4663. * Creates a string representing this Cartesian in the format '(x, y, z, w)'.
  4664. *
  4665. * @returns {String} A string representing the provided Cartesian in the format '(x, y, z, w)'.
  4666. */
  4667. Cartesian4.prototype.toString = function () {
  4668. return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`;
  4669. };
  4670. // scratchU8Array and scratchF32Array are views into the same buffer
  4671. const scratchF32Array = new Float32Array(1);
  4672. const scratchU8Array = new Uint8Array(scratchF32Array.buffer);
  4673. const testU32 = new Uint32Array([0x11223344]);
  4674. const testU8 = new Uint8Array(testU32.buffer);
  4675. const littleEndian = testU8[0] === 0x44;
  4676. /**
  4677. * Packs an arbitrary floating point value to 4 values representable using uint8.
  4678. *
  4679. * @param {Number} value A floating point number.
  4680. * @param {Cartesian4} [result] The Cartesian4 that will contain the packed float.
  4681. * @returns {Cartesian4} A Cartesian4 representing the float packed to values in x, y, z, and w.
  4682. */
  4683. Cartesian4.packFloat = function (value, result) {
  4684. //>>includeStart('debug', pragmas.debug);
  4685. RuntimeError.Check.typeOf.number("value", value);
  4686. //>>includeEnd('debug');
  4687. if (!defaultValue.defined(result)) {
  4688. result = new Cartesian4();
  4689. }
  4690. // scratchU8Array and scratchF32Array are views into the same buffer
  4691. scratchF32Array[0] = value;
  4692. if (littleEndian) {
  4693. result.x = scratchU8Array[0];
  4694. result.y = scratchU8Array[1];
  4695. result.z = scratchU8Array[2];
  4696. result.w = scratchU8Array[3];
  4697. } else {
  4698. // convert from big-endian to little-endian
  4699. result.x = scratchU8Array[3];
  4700. result.y = scratchU8Array[2];
  4701. result.z = scratchU8Array[1];
  4702. result.w = scratchU8Array[0];
  4703. }
  4704. return result;
  4705. };
  4706. /**
  4707. * Unpacks a float packed using Cartesian4.packFloat.
  4708. *
  4709. * @param {Cartesian4} packedFloat A Cartesian4 containing a float packed to 4 values representable using uint8.
  4710. * @returns {Number} The unpacked float.
  4711. * @private
  4712. */
  4713. Cartesian4.unpackFloat = function (packedFloat) {
  4714. //>>includeStart('debug', pragmas.debug);
  4715. RuntimeError.Check.typeOf.object("packedFloat", packedFloat);
  4716. //>>includeEnd('debug');
  4717. // scratchU8Array and scratchF32Array are views into the same buffer
  4718. if (littleEndian) {
  4719. scratchU8Array[0] = packedFloat.x;
  4720. scratchU8Array[1] = packedFloat.y;
  4721. scratchU8Array[2] = packedFloat.z;
  4722. scratchU8Array[3] = packedFloat.w;
  4723. } else {
  4724. // convert from little-endian to big-endian
  4725. scratchU8Array[0] = packedFloat.w;
  4726. scratchU8Array[1] = packedFloat.z;
  4727. scratchU8Array[2] = packedFloat.y;
  4728. scratchU8Array[3] = packedFloat.x;
  4729. }
  4730. return scratchF32Array[0];
  4731. };
  4732. /**
  4733. * A 4x4 matrix, indexable as a column-major order array.
  4734. * Constructor parameters are in row-major order for code readability.
  4735. * @alias Matrix4
  4736. * @constructor
  4737. * @implements {ArrayLike<number>}
  4738. *
  4739. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  4740. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  4741. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  4742. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  4743. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  4744. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  4745. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  4746. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  4747. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  4748. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  4749. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  4750. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  4751. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  4752. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  4753. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  4754. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  4755. *
  4756. * @see Matrix4.fromArray
  4757. * @see Matrix4.fromColumnMajorArray
  4758. * @see Matrix4.fromRowMajorArray
  4759. * @see Matrix4.fromRotationTranslation
  4760. * @see Matrix4.fromTranslationQuaternionRotationScale
  4761. * @see Matrix4.fromTranslationRotationScale
  4762. * @see Matrix4.fromTranslation
  4763. * @see Matrix4.fromScale
  4764. * @see Matrix4.fromUniformScale
  4765. * @see Matrix4.fromRotation
  4766. * @see Matrix4.fromCamera
  4767. * @see Matrix4.computePerspectiveFieldOfView
  4768. * @see Matrix4.computeOrthographicOffCenter
  4769. * @see Matrix4.computePerspectiveOffCenter
  4770. * @see Matrix4.computeInfinitePerspectiveOffCenter
  4771. * @see Matrix4.computeViewportTransformation
  4772. * @see Matrix4.computeView
  4773. * @see Matrix2
  4774. * @see Matrix3
  4775. * @see Packable
  4776. */
  4777. function Matrix4(
  4778. column0Row0,
  4779. column1Row0,
  4780. column2Row0,
  4781. column3Row0,
  4782. column0Row1,
  4783. column1Row1,
  4784. column2Row1,
  4785. column3Row1,
  4786. column0Row2,
  4787. column1Row2,
  4788. column2Row2,
  4789. column3Row2,
  4790. column0Row3,
  4791. column1Row3,
  4792. column2Row3,
  4793. column3Row3
  4794. ) {
  4795. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  4796. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  4797. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  4798. this[3] = defaultValue.defaultValue(column0Row3, 0.0);
  4799. this[4] = defaultValue.defaultValue(column1Row0, 0.0);
  4800. this[5] = defaultValue.defaultValue(column1Row1, 0.0);
  4801. this[6] = defaultValue.defaultValue(column1Row2, 0.0);
  4802. this[7] = defaultValue.defaultValue(column1Row3, 0.0);
  4803. this[8] = defaultValue.defaultValue(column2Row0, 0.0);
  4804. this[9] = defaultValue.defaultValue(column2Row1, 0.0);
  4805. this[10] = defaultValue.defaultValue(column2Row2, 0.0);
  4806. this[11] = defaultValue.defaultValue(column2Row3, 0.0);
  4807. this[12] = defaultValue.defaultValue(column3Row0, 0.0);
  4808. this[13] = defaultValue.defaultValue(column3Row1, 0.0);
  4809. this[14] = defaultValue.defaultValue(column3Row2, 0.0);
  4810. this[15] = defaultValue.defaultValue(column3Row3, 0.0);
  4811. }
  4812. /**
  4813. * The number of elements used to pack the object into an array.
  4814. * @type {Number}
  4815. */
  4816. Matrix4.packedLength = 16;
  4817. /**
  4818. * Stores the provided instance into the provided array.
  4819. *
  4820. * @param {Matrix4} value The value to pack.
  4821. * @param {Number[]} array The array to pack into.
  4822. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  4823. *
  4824. * @returns {Number[]} The array that was packed into
  4825. */
  4826. Matrix4.pack = function (value, array, startingIndex) {
  4827. //>>includeStart('debug', pragmas.debug);
  4828. RuntimeError.Check.typeOf.object("value", value);
  4829. RuntimeError.Check.defined("array", array);
  4830. //>>includeEnd('debug');
  4831. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4832. array[startingIndex++] = value[0];
  4833. array[startingIndex++] = value[1];
  4834. array[startingIndex++] = value[2];
  4835. array[startingIndex++] = value[3];
  4836. array[startingIndex++] = value[4];
  4837. array[startingIndex++] = value[5];
  4838. array[startingIndex++] = value[6];
  4839. array[startingIndex++] = value[7];
  4840. array[startingIndex++] = value[8];
  4841. array[startingIndex++] = value[9];
  4842. array[startingIndex++] = value[10];
  4843. array[startingIndex++] = value[11];
  4844. array[startingIndex++] = value[12];
  4845. array[startingIndex++] = value[13];
  4846. array[startingIndex++] = value[14];
  4847. array[startingIndex] = value[15];
  4848. return array;
  4849. };
  4850. /**
  4851. * Retrieves an instance from a packed array.
  4852. *
  4853. * @param {Number[]} array The packed array.
  4854. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  4855. * @param {Matrix4} [result] The object into which to store the result.
  4856. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  4857. */
  4858. Matrix4.unpack = function (array, startingIndex, result) {
  4859. //>>includeStart('debug', pragmas.debug);
  4860. RuntimeError.Check.defined("array", array);
  4861. //>>includeEnd('debug');
  4862. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4863. if (!defaultValue.defined(result)) {
  4864. result = new Matrix4();
  4865. }
  4866. result[0] = array[startingIndex++];
  4867. result[1] = array[startingIndex++];
  4868. result[2] = array[startingIndex++];
  4869. result[3] = array[startingIndex++];
  4870. result[4] = array[startingIndex++];
  4871. result[5] = array[startingIndex++];
  4872. result[6] = array[startingIndex++];
  4873. result[7] = array[startingIndex++];
  4874. result[8] = array[startingIndex++];
  4875. result[9] = array[startingIndex++];
  4876. result[10] = array[startingIndex++];
  4877. result[11] = array[startingIndex++];
  4878. result[12] = array[startingIndex++];
  4879. result[13] = array[startingIndex++];
  4880. result[14] = array[startingIndex++];
  4881. result[15] = array[startingIndex];
  4882. return result;
  4883. };
  4884. /**
  4885. * Flattens an array of Matrix4s into an array of components. The components
  4886. * are stored in column-major order.
  4887. *
  4888. * @param {Matrix4[]} array The array of matrices to pack.
  4889. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 16 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 16) elements.
  4890. * @returns {Number[]} The packed array.
  4891. */
  4892. Matrix4.packArray = function (array, result) {
  4893. //>>includeStart('debug', pragmas.debug);
  4894. RuntimeError.Check.defined("array", array);
  4895. //>>includeEnd('debug');
  4896. const length = array.length;
  4897. const resultLength = length * 16;
  4898. if (!defaultValue.defined(result)) {
  4899. result = new Array(resultLength);
  4900. } else if (!Array.isArray(result) && result.length !== resultLength) {
  4901. //>>includeStart('debug', pragmas.debug);
  4902. throw new RuntimeError.DeveloperError(
  4903. "If result is a typed array, it must have exactly array.length * 16 elements"
  4904. );
  4905. //>>includeEnd('debug');
  4906. } else if (result.length !== resultLength) {
  4907. result.length = resultLength;
  4908. }
  4909. for (let i = 0; i < length; ++i) {
  4910. Matrix4.pack(array[i], result, i * 16);
  4911. }
  4912. return result;
  4913. };
  4914. /**
  4915. * Unpacks an array of column-major matrix components into an array of Matrix4s.
  4916. *
  4917. * @param {Number[]} array The array of components to unpack.
  4918. * @param {Matrix4[]} [result] The array onto which to store the result.
  4919. * @returns {Matrix4[]} The unpacked array.
  4920. */
  4921. Matrix4.unpackArray = function (array, result) {
  4922. //>>includeStart('debug', pragmas.debug);
  4923. RuntimeError.Check.defined("array", array);
  4924. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 16);
  4925. if (array.length % 16 !== 0) {
  4926. throw new RuntimeError.DeveloperError("array length must be a multiple of 16.");
  4927. }
  4928. //>>includeEnd('debug');
  4929. const length = array.length;
  4930. if (!defaultValue.defined(result)) {
  4931. result = new Array(length / 16);
  4932. } else {
  4933. result.length = length / 16;
  4934. }
  4935. for (let i = 0; i < length; i += 16) {
  4936. const index = i / 16;
  4937. result[index] = Matrix4.unpack(array, i, result[index]);
  4938. }
  4939. return result;
  4940. };
  4941. /**
  4942. * Duplicates a Matrix4 instance.
  4943. *
  4944. * @param {Matrix4} matrix The matrix to duplicate.
  4945. * @param {Matrix4} [result] The object onto which to store the result.
  4946. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  4947. */
  4948. Matrix4.clone = function (matrix, result) {
  4949. if (!defaultValue.defined(matrix)) {
  4950. return undefined;
  4951. }
  4952. if (!defaultValue.defined(result)) {
  4953. return new Matrix4(
  4954. matrix[0],
  4955. matrix[4],
  4956. matrix[8],
  4957. matrix[12],
  4958. matrix[1],
  4959. matrix[5],
  4960. matrix[9],
  4961. matrix[13],
  4962. matrix[2],
  4963. matrix[6],
  4964. matrix[10],
  4965. matrix[14],
  4966. matrix[3],
  4967. matrix[7],
  4968. matrix[11],
  4969. matrix[15]
  4970. );
  4971. }
  4972. result[0] = matrix[0];
  4973. result[1] = matrix[1];
  4974. result[2] = matrix[2];
  4975. result[3] = matrix[3];
  4976. result[4] = matrix[4];
  4977. result[5] = matrix[5];
  4978. result[6] = matrix[6];
  4979. result[7] = matrix[7];
  4980. result[8] = matrix[8];
  4981. result[9] = matrix[9];
  4982. result[10] = matrix[10];
  4983. result[11] = matrix[11];
  4984. result[12] = matrix[12];
  4985. result[13] = matrix[13];
  4986. result[14] = matrix[14];
  4987. result[15] = matrix[15];
  4988. return result;
  4989. };
  4990. /**
  4991. * Creates a Matrix4 from 16 consecutive elements in an array.
  4992. * @function
  4993. *
  4994. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  4995. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  4996. * @param {Matrix4} [result] The object onto which to store the result.
  4997. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  4998. *
  4999. * @example
  5000. * // Create the Matrix4:
  5001. * // [1.0, 2.0, 3.0, 4.0]
  5002. * // [1.0, 2.0, 3.0, 4.0]
  5003. * // [1.0, 2.0, 3.0, 4.0]
  5004. * // [1.0, 2.0, 3.0, 4.0]
  5005. *
  5006. * const 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];
  5007. * const m = Cesium.Matrix4.fromArray(v);
  5008. *
  5009. * // Create same Matrix4 with using an offset into an array
  5010. * const 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];
  5011. * const m2 = Cesium.Matrix4.fromArray(v2, 2);
  5012. */
  5013. Matrix4.fromArray = Matrix4.unpack;
  5014. /**
  5015. * Computes a Matrix4 instance from a column-major order array.
  5016. *
  5017. * @param {Number[]} values The column-major order array.
  5018. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5019. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5020. */
  5021. Matrix4.fromColumnMajorArray = function (values, result) {
  5022. //>>includeStart('debug', pragmas.debug);
  5023. RuntimeError.Check.defined("values", values);
  5024. //>>includeEnd('debug');
  5025. return Matrix4.clone(values, result);
  5026. };
  5027. /**
  5028. * Computes a Matrix4 instance from a row-major order array.
  5029. * The resulting matrix will be in column-major order.
  5030. *
  5031. * @param {Number[]} values The row-major order array.
  5032. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5033. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5034. */
  5035. Matrix4.fromRowMajorArray = function (values, result) {
  5036. //>>includeStart('debug', pragmas.debug);
  5037. RuntimeError.Check.defined("values", values);
  5038. //>>includeEnd('debug');
  5039. if (!defaultValue.defined(result)) {
  5040. return new Matrix4(
  5041. values[0],
  5042. values[1],
  5043. values[2],
  5044. values[3],
  5045. values[4],
  5046. values[5],
  5047. values[6],
  5048. values[7],
  5049. values[8],
  5050. values[9],
  5051. values[10],
  5052. values[11],
  5053. values[12],
  5054. values[13],
  5055. values[14],
  5056. values[15]
  5057. );
  5058. }
  5059. result[0] = values[0];
  5060. result[1] = values[4];
  5061. result[2] = values[8];
  5062. result[3] = values[12];
  5063. result[4] = values[1];
  5064. result[5] = values[5];
  5065. result[6] = values[9];
  5066. result[7] = values[13];
  5067. result[8] = values[2];
  5068. result[9] = values[6];
  5069. result[10] = values[10];
  5070. result[11] = values[14];
  5071. result[12] = values[3];
  5072. result[13] = values[7];
  5073. result[14] = values[11];
  5074. result[15] = values[15];
  5075. return result;
  5076. };
  5077. /**
  5078. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  5079. * and a Cartesian3 representing the translation.
  5080. *
  5081. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  5082. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  5083. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5084. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5085. */
  5086. Matrix4.fromRotationTranslation = function (rotation, translation, result) {
  5087. //>>includeStart('debug', pragmas.debug);
  5088. RuntimeError.Check.typeOf.object("rotation", rotation);
  5089. //>>includeEnd('debug');
  5090. translation = defaultValue.defaultValue(translation, Cartesian3.ZERO);
  5091. if (!defaultValue.defined(result)) {
  5092. return new Matrix4(
  5093. rotation[0],
  5094. rotation[3],
  5095. rotation[6],
  5096. translation.x,
  5097. rotation[1],
  5098. rotation[4],
  5099. rotation[7],
  5100. translation.y,
  5101. rotation[2],
  5102. rotation[5],
  5103. rotation[8],
  5104. translation.z,
  5105. 0.0,
  5106. 0.0,
  5107. 0.0,
  5108. 1.0
  5109. );
  5110. }
  5111. result[0] = rotation[0];
  5112. result[1] = rotation[1];
  5113. result[2] = rotation[2];
  5114. result[3] = 0.0;
  5115. result[4] = rotation[3];
  5116. result[5] = rotation[4];
  5117. result[6] = rotation[5];
  5118. result[7] = 0.0;
  5119. result[8] = rotation[6];
  5120. result[9] = rotation[7];
  5121. result[10] = rotation[8];
  5122. result[11] = 0.0;
  5123. result[12] = translation.x;
  5124. result[13] = translation.y;
  5125. result[14] = translation.z;
  5126. result[15] = 1.0;
  5127. return result;
  5128. };
  5129. /**
  5130. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  5131. * representation with the rotation represented as a quaternion.
  5132. *
  5133. * @param {Cartesian3} translation The translation transformation.
  5134. * @param {Quaternion} rotation The rotation transformation.
  5135. * @param {Cartesian3} scale The non-uniform scale transformation.
  5136. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5137. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5138. *
  5139. * @example
  5140. * const result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  5141. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  5142. * Cesium.Quaternion.IDENTITY, // rotation
  5143. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  5144. * result);
  5145. */
  5146. Matrix4.fromTranslationQuaternionRotationScale = function (
  5147. translation,
  5148. rotation,
  5149. scale,
  5150. result
  5151. ) {
  5152. //>>includeStart('debug', pragmas.debug);
  5153. RuntimeError.Check.typeOf.object("translation", translation);
  5154. RuntimeError.Check.typeOf.object("rotation", rotation);
  5155. RuntimeError.Check.typeOf.object("scale", scale);
  5156. //>>includeEnd('debug');
  5157. if (!defaultValue.defined(result)) {
  5158. result = new Matrix4();
  5159. }
  5160. const scaleX = scale.x;
  5161. const scaleY = scale.y;
  5162. const scaleZ = scale.z;
  5163. const x2 = rotation.x * rotation.x;
  5164. const xy = rotation.x * rotation.y;
  5165. const xz = rotation.x * rotation.z;
  5166. const xw = rotation.x * rotation.w;
  5167. const y2 = rotation.y * rotation.y;
  5168. const yz = rotation.y * rotation.z;
  5169. const yw = rotation.y * rotation.w;
  5170. const z2 = rotation.z * rotation.z;
  5171. const zw = rotation.z * rotation.w;
  5172. const w2 = rotation.w * rotation.w;
  5173. const m00 = x2 - y2 - z2 + w2;
  5174. const m01 = 2.0 * (xy - zw);
  5175. const m02 = 2.0 * (xz + yw);
  5176. const m10 = 2.0 * (xy + zw);
  5177. const m11 = -x2 + y2 - z2 + w2;
  5178. const m12 = 2.0 * (yz - xw);
  5179. const m20 = 2.0 * (xz - yw);
  5180. const m21 = 2.0 * (yz + xw);
  5181. const m22 = -x2 - y2 + z2 + w2;
  5182. result[0] = m00 * scaleX;
  5183. result[1] = m10 * scaleX;
  5184. result[2] = m20 * scaleX;
  5185. result[3] = 0.0;
  5186. result[4] = m01 * scaleY;
  5187. result[5] = m11 * scaleY;
  5188. result[6] = m21 * scaleY;
  5189. result[7] = 0.0;
  5190. result[8] = m02 * scaleZ;
  5191. result[9] = m12 * scaleZ;
  5192. result[10] = m22 * scaleZ;
  5193. result[11] = 0.0;
  5194. result[12] = translation.x;
  5195. result[13] = translation.y;
  5196. result[14] = translation.z;
  5197. result[15] = 1.0;
  5198. return result;
  5199. };
  5200. /**
  5201. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  5202. *
  5203. * @param {TranslationRotationScale} translationRotationScale The instance.
  5204. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5205. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5206. */
  5207. Matrix4.fromTranslationRotationScale = function (
  5208. translationRotationScale,
  5209. result
  5210. ) {
  5211. //>>includeStart('debug', pragmas.debug);
  5212. RuntimeError.Check.typeOf.object("translationRotationScale", translationRotationScale);
  5213. //>>includeEnd('debug');
  5214. return Matrix4.fromTranslationQuaternionRotationScale(
  5215. translationRotationScale.translation,
  5216. translationRotationScale.rotation,
  5217. translationRotationScale.scale,
  5218. result
  5219. );
  5220. };
  5221. /**
  5222. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  5223. *
  5224. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  5225. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5226. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5227. *
  5228. * @see Matrix4.multiplyByTranslation
  5229. */
  5230. Matrix4.fromTranslation = function (translation, result) {
  5231. //>>includeStart('debug', pragmas.debug);
  5232. RuntimeError.Check.typeOf.object("translation", translation);
  5233. //>>includeEnd('debug');
  5234. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  5235. };
  5236. /**
  5237. * Computes a Matrix4 instance representing a non-uniform scale.
  5238. *
  5239. * @param {Cartesian3} scale The x, y, and z scale factors.
  5240. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5241. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5242. *
  5243. * @example
  5244. * // Creates
  5245. * // [7.0, 0.0, 0.0, 0.0]
  5246. * // [0.0, 8.0, 0.0, 0.0]
  5247. * // [0.0, 0.0, 9.0, 0.0]
  5248. * // [0.0, 0.0, 0.0, 1.0]
  5249. * const m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  5250. */
  5251. Matrix4.fromScale = function (scale, result) {
  5252. //>>includeStart('debug', pragmas.debug);
  5253. RuntimeError.Check.typeOf.object("scale", scale);
  5254. //>>includeEnd('debug');
  5255. if (!defaultValue.defined(result)) {
  5256. return new Matrix4(
  5257. scale.x,
  5258. 0.0,
  5259. 0.0,
  5260. 0.0,
  5261. 0.0,
  5262. scale.y,
  5263. 0.0,
  5264. 0.0,
  5265. 0.0,
  5266. 0.0,
  5267. scale.z,
  5268. 0.0,
  5269. 0.0,
  5270. 0.0,
  5271. 0.0,
  5272. 1.0
  5273. );
  5274. }
  5275. result[0] = scale.x;
  5276. result[1] = 0.0;
  5277. result[2] = 0.0;
  5278. result[3] = 0.0;
  5279. result[4] = 0.0;
  5280. result[5] = scale.y;
  5281. result[6] = 0.0;
  5282. result[7] = 0.0;
  5283. result[8] = 0.0;
  5284. result[9] = 0.0;
  5285. result[10] = scale.z;
  5286. result[11] = 0.0;
  5287. result[12] = 0.0;
  5288. result[13] = 0.0;
  5289. result[14] = 0.0;
  5290. result[15] = 1.0;
  5291. return result;
  5292. };
  5293. /**
  5294. * Computes a Matrix4 instance representing a uniform scale.
  5295. *
  5296. * @param {Number} scale The uniform scale factor.
  5297. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5298. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5299. *
  5300. * @example
  5301. * // Creates
  5302. * // [2.0, 0.0, 0.0, 0.0]
  5303. * // [0.0, 2.0, 0.0, 0.0]
  5304. * // [0.0, 0.0, 2.0, 0.0]
  5305. * // [0.0, 0.0, 0.0, 1.0]
  5306. * const m = Cesium.Matrix4.fromUniformScale(2.0);
  5307. */
  5308. Matrix4.fromUniformScale = function (scale, result) {
  5309. //>>includeStart('debug', pragmas.debug);
  5310. RuntimeError.Check.typeOf.number("scale", scale);
  5311. //>>includeEnd('debug');
  5312. if (!defaultValue.defined(result)) {
  5313. return new Matrix4(
  5314. scale,
  5315. 0.0,
  5316. 0.0,
  5317. 0.0,
  5318. 0.0,
  5319. scale,
  5320. 0.0,
  5321. 0.0,
  5322. 0.0,
  5323. 0.0,
  5324. scale,
  5325. 0.0,
  5326. 0.0,
  5327. 0.0,
  5328. 0.0,
  5329. 1.0
  5330. );
  5331. }
  5332. result[0] = scale;
  5333. result[1] = 0.0;
  5334. result[2] = 0.0;
  5335. result[3] = 0.0;
  5336. result[4] = 0.0;
  5337. result[5] = scale;
  5338. result[6] = 0.0;
  5339. result[7] = 0.0;
  5340. result[8] = 0.0;
  5341. result[9] = 0.0;
  5342. result[10] = scale;
  5343. result[11] = 0.0;
  5344. result[12] = 0.0;
  5345. result[13] = 0.0;
  5346. result[14] = 0.0;
  5347. result[15] = 1.0;
  5348. return result;
  5349. };
  5350. /**
  5351. * Creates a rotation matrix.
  5352. *
  5353. * @param {Matrix3} rotation The rotation matrix.
  5354. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5355. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5356. */
  5357. Matrix4.fromRotation = function (rotation, result) {
  5358. //>>includeStart('debug', pragmas.debug);
  5359. RuntimeError.Check.typeOf.object("rotation", rotation);
  5360. //>>includeEnd('debug');
  5361. if (!defaultValue.defined(result)) {
  5362. result = new Matrix4();
  5363. }
  5364. result[0] = rotation[0];
  5365. result[1] = rotation[1];
  5366. result[2] = rotation[2];
  5367. result[3] = 0.0;
  5368. result[4] = rotation[3];
  5369. result[5] = rotation[4];
  5370. result[6] = rotation[5];
  5371. result[7] = 0.0;
  5372. result[8] = rotation[6];
  5373. result[9] = rotation[7];
  5374. result[10] = rotation[8];
  5375. result[11] = 0.0;
  5376. result[12] = 0.0;
  5377. result[13] = 0.0;
  5378. result[14] = 0.0;
  5379. result[15] = 1.0;
  5380. return result;
  5381. };
  5382. const fromCameraF = new Cartesian3();
  5383. const fromCameraR = new Cartesian3();
  5384. const fromCameraU = new Cartesian3();
  5385. /**
  5386. * Computes a Matrix4 instance from a Camera.
  5387. *
  5388. * @param {Camera} camera The camera to use.
  5389. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5390. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5391. */
  5392. Matrix4.fromCamera = function (camera, result) {
  5393. //>>includeStart('debug', pragmas.debug);
  5394. RuntimeError.Check.typeOf.object("camera", camera);
  5395. //>>includeEnd('debug');
  5396. const position = camera.position;
  5397. const direction = camera.direction;
  5398. const up = camera.up;
  5399. //>>includeStart('debug', pragmas.debug);
  5400. RuntimeError.Check.typeOf.object("camera.position", position);
  5401. RuntimeError.Check.typeOf.object("camera.direction", direction);
  5402. RuntimeError.Check.typeOf.object("camera.up", up);
  5403. //>>includeEnd('debug');
  5404. Cartesian3.normalize(direction, fromCameraF);
  5405. Cartesian3.normalize(
  5406. Cartesian3.cross(fromCameraF, up, fromCameraR),
  5407. fromCameraR
  5408. );
  5409. Cartesian3.normalize(
  5410. Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU),
  5411. fromCameraU
  5412. );
  5413. const sX = fromCameraR.x;
  5414. const sY = fromCameraR.y;
  5415. const sZ = fromCameraR.z;
  5416. const fX = fromCameraF.x;
  5417. const fY = fromCameraF.y;
  5418. const fZ = fromCameraF.z;
  5419. const uX = fromCameraU.x;
  5420. const uY = fromCameraU.y;
  5421. const uZ = fromCameraU.z;
  5422. const positionX = position.x;
  5423. const positionY = position.y;
  5424. const positionZ = position.z;
  5425. const t0 = sX * -positionX + sY * -positionY + sZ * -positionZ;
  5426. const t1 = uX * -positionX + uY * -positionY + uZ * -positionZ;
  5427. const t2 = fX * positionX + fY * positionY + fZ * positionZ;
  5428. // The code below this comment is an optimized
  5429. // version of the commented lines.
  5430. // Rather that create two matrices and then multiply,
  5431. // we just bake in the multiplcation as part of creation.
  5432. // const rotation = new Matrix4(
  5433. // sX, sY, sZ, 0.0,
  5434. // uX, uY, uZ, 0.0,
  5435. // -fX, -fY, -fZ, 0.0,
  5436. // 0.0, 0.0, 0.0, 1.0);
  5437. // const translation = new Matrix4(
  5438. // 1.0, 0.0, 0.0, -position.x,
  5439. // 0.0, 1.0, 0.0, -position.y,
  5440. // 0.0, 0.0, 1.0, -position.z,
  5441. // 0.0, 0.0, 0.0, 1.0);
  5442. // return rotation.multiply(translation);
  5443. if (!defaultValue.defined(result)) {
  5444. return new Matrix4(
  5445. sX,
  5446. sY,
  5447. sZ,
  5448. t0,
  5449. uX,
  5450. uY,
  5451. uZ,
  5452. t1,
  5453. -fX,
  5454. -fY,
  5455. -fZ,
  5456. t2,
  5457. 0.0,
  5458. 0.0,
  5459. 0.0,
  5460. 1.0
  5461. );
  5462. }
  5463. result[0] = sX;
  5464. result[1] = uX;
  5465. result[2] = -fX;
  5466. result[3] = 0.0;
  5467. result[4] = sY;
  5468. result[5] = uY;
  5469. result[6] = -fY;
  5470. result[7] = 0.0;
  5471. result[8] = sZ;
  5472. result[9] = uZ;
  5473. result[10] = -fZ;
  5474. result[11] = 0.0;
  5475. result[12] = t0;
  5476. result[13] = t1;
  5477. result[14] = t2;
  5478. result[15] = 1.0;
  5479. return result;
  5480. };
  5481. /**
  5482. * Computes a Matrix4 instance representing a perspective transformation matrix.
  5483. *
  5484. * @param {Number} fovY The field of view along the Y axis in radians.
  5485. * @param {Number} aspectRatio The aspect ratio.
  5486. * @param {Number} near The distance to the near plane in meters.
  5487. * @param {Number} far The distance to the far plane in meters.
  5488. * @param {Matrix4} result The object in which the result will be stored.
  5489. * @returns {Matrix4} The modified result parameter.
  5490. *
  5491. * @exception {DeveloperError} fovY must be in (0, PI].
  5492. * @exception {DeveloperError} aspectRatio must be greater than zero.
  5493. * @exception {DeveloperError} near must be greater than zero.
  5494. * @exception {DeveloperError} far must be greater than zero.
  5495. */
  5496. Matrix4.computePerspectiveFieldOfView = function (
  5497. fovY,
  5498. aspectRatio,
  5499. near,
  5500. far,
  5501. result
  5502. ) {
  5503. //>>includeStart('debug', pragmas.debug);
  5504. RuntimeError.Check.typeOf.number.greaterThan("fovY", fovY, 0.0);
  5505. RuntimeError.Check.typeOf.number.lessThan("fovY", fovY, Math.PI);
  5506. RuntimeError.Check.typeOf.number.greaterThan("near", near, 0.0);
  5507. RuntimeError.Check.typeOf.number.greaterThan("far", far, 0.0);
  5508. RuntimeError.Check.typeOf.object("result", result);
  5509. //>>includeEnd('debug');
  5510. const bottom = Math.tan(fovY * 0.5);
  5511. const column1Row1 = 1.0 / bottom;
  5512. const column0Row0 = column1Row1 / aspectRatio;
  5513. const column2Row2 = (far + near) / (near - far);
  5514. const column3Row2 = (2.0 * far * near) / (near - far);
  5515. result[0] = column0Row0;
  5516. result[1] = 0.0;
  5517. result[2] = 0.0;
  5518. result[3] = 0.0;
  5519. result[4] = 0.0;
  5520. result[5] = column1Row1;
  5521. result[6] = 0.0;
  5522. result[7] = 0.0;
  5523. result[8] = 0.0;
  5524. result[9] = 0.0;
  5525. result[10] = column2Row2;
  5526. result[11] = -1.0;
  5527. result[12] = 0.0;
  5528. result[13] = 0.0;
  5529. result[14] = column3Row2;
  5530. result[15] = 0.0;
  5531. return result;
  5532. };
  5533. /**
  5534. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  5535. *
  5536. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5537. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5538. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5539. * @param {Number} top The number of meters above of the camera that will be in view.
  5540. * @param {Number} near The distance to the near plane in meters.
  5541. * @param {Number} far The distance to the far plane in meters.
  5542. * @param {Matrix4} result The object in which the result will be stored.
  5543. * @returns {Matrix4} The modified result parameter.
  5544. */
  5545. Matrix4.computeOrthographicOffCenter = function (
  5546. left,
  5547. right,
  5548. bottom,
  5549. top,
  5550. near,
  5551. far,
  5552. result
  5553. ) {
  5554. //>>includeStart('debug', pragmas.debug);
  5555. RuntimeError.Check.typeOf.number("left", left);
  5556. RuntimeError.Check.typeOf.number("right", right);
  5557. RuntimeError.Check.typeOf.number("bottom", bottom);
  5558. RuntimeError.Check.typeOf.number("top", top);
  5559. RuntimeError.Check.typeOf.number("near", near);
  5560. RuntimeError.Check.typeOf.number("far", far);
  5561. RuntimeError.Check.typeOf.object("result", result);
  5562. //>>includeEnd('debug');
  5563. let a = 1.0 / (right - left);
  5564. let b = 1.0 / (top - bottom);
  5565. let c = 1.0 / (far - near);
  5566. const tx = -(right + left) * a;
  5567. const ty = -(top + bottom) * b;
  5568. const tz = -(far + near) * c;
  5569. a *= 2.0;
  5570. b *= 2.0;
  5571. c *= -2.0;
  5572. result[0] = a;
  5573. result[1] = 0.0;
  5574. result[2] = 0.0;
  5575. result[3] = 0.0;
  5576. result[4] = 0.0;
  5577. result[5] = b;
  5578. result[6] = 0.0;
  5579. result[7] = 0.0;
  5580. result[8] = 0.0;
  5581. result[9] = 0.0;
  5582. result[10] = c;
  5583. result[11] = 0.0;
  5584. result[12] = tx;
  5585. result[13] = ty;
  5586. result[14] = tz;
  5587. result[15] = 1.0;
  5588. return result;
  5589. };
  5590. /**
  5591. * Computes a Matrix4 instance representing an off center perspective transformation.
  5592. *
  5593. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5594. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5595. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5596. * @param {Number} top The number of meters above of the camera that will be in view.
  5597. * @param {Number} near The distance to the near plane in meters.
  5598. * @param {Number} far The distance to the far plane in meters.
  5599. * @param {Matrix4} result The object in which the result will be stored.
  5600. * @returns {Matrix4} The modified result parameter.
  5601. */
  5602. Matrix4.computePerspectiveOffCenter = function (
  5603. left,
  5604. right,
  5605. bottom,
  5606. top,
  5607. near,
  5608. far,
  5609. result
  5610. ) {
  5611. //>>includeStart('debug', pragmas.debug);
  5612. RuntimeError.Check.typeOf.number("left", left);
  5613. RuntimeError.Check.typeOf.number("right", right);
  5614. RuntimeError.Check.typeOf.number("bottom", bottom);
  5615. RuntimeError.Check.typeOf.number("top", top);
  5616. RuntimeError.Check.typeOf.number("near", near);
  5617. RuntimeError.Check.typeOf.number("far", far);
  5618. RuntimeError.Check.typeOf.object("result", result);
  5619. //>>includeEnd('debug');
  5620. const column0Row0 = (2.0 * near) / (right - left);
  5621. const column1Row1 = (2.0 * near) / (top - bottom);
  5622. const column2Row0 = (right + left) / (right - left);
  5623. const column2Row1 = (top + bottom) / (top - bottom);
  5624. const column2Row2 = -(far + near) / (far - near);
  5625. const column2Row3 = -1.0;
  5626. const column3Row2 = (-2.0 * far * near) / (far - near);
  5627. result[0] = column0Row0;
  5628. result[1] = 0.0;
  5629. result[2] = 0.0;
  5630. result[3] = 0.0;
  5631. result[4] = 0.0;
  5632. result[5] = column1Row1;
  5633. result[6] = 0.0;
  5634. result[7] = 0.0;
  5635. result[8] = column2Row0;
  5636. result[9] = column2Row1;
  5637. result[10] = column2Row2;
  5638. result[11] = column2Row3;
  5639. result[12] = 0.0;
  5640. result[13] = 0.0;
  5641. result[14] = column3Row2;
  5642. result[15] = 0.0;
  5643. return result;
  5644. };
  5645. /**
  5646. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  5647. *
  5648. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5649. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5650. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5651. * @param {Number} top The number of meters above of the camera that will be in view.
  5652. * @param {Number} near The distance to the near plane in meters.
  5653. * @param {Matrix4} result The object in which the result will be stored.
  5654. * @returns {Matrix4} The modified result parameter.
  5655. */
  5656. Matrix4.computeInfinitePerspectiveOffCenter = function (
  5657. left,
  5658. right,
  5659. bottom,
  5660. top,
  5661. near,
  5662. result
  5663. ) {
  5664. //>>includeStart('debug', pragmas.debug);
  5665. RuntimeError.Check.typeOf.number("left", left);
  5666. RuntimeError.Check.typeOf.number("right", right);
  5667. RuntimeError.Check.typeOf.number("bottom", bottom);
  5668. RuntimeError.Check.typeOf.number("top", top);
  5669. RuntimeError.Check.typeOf.number("near", near);
  5670. RuntimeError.Check.typeOf.object("result", result);
  5671. //>>includeEnd('debug');
  5672. const column0Row0 = (2.0 * near) / (right - left);
  5673. const column1Row1 = (2.0 * near) / (top - bottom);
  5674. const column2Row0 = (right + left) / (right - left);
  5675. const column2Row1 = (top + bottom) / (top - bottom);
  5676. const column2Row2 = -1.0;
  5677. const column2Row3 = -1.0;
  5678. const column3Row2 = -2.0 * near;
  5679. result[0] = column0Row0;
  5680. result[1] = 0.0;
  5681. result[2] = 0.0;
  5682. result[3] = 0.0;
  5683. result[4] = 0.0;
  5684. result[5] = column1Row1;
  5685. result[6] = 0.0;
  5686. result[7] = 0.0;
  5687. result[8] = column2Row0;
  5688. result[9] = column2Row1;
  5689. result[10] = column2Row2;
  5690. result[11] = column2Row3;
  5691. result[12] = 0.0;
  5692. result[13] = 0.0;
  5693. result[14] = column3Row2;
  5694. result[15] = 0.0;
  5695. return result;
  5696. };
  5697. /**
  5698. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  5699. *
  5700. * @param {Object} [viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  5701. * @param {Number} [nearDepthRange=0.0] The near plane distance in window coordinates.
  5702. * @param {Number} [farDepthRange=1.0] The far plane distance in window coordinates.
  5703. * @param {Matrix4} [result] The object in which the result will be stored.
  5704. * @returns {Matrix4} The modified result parameter.
  5705. *
  5706. * @example
  5707. * // Create viewport transformation using an explicit viewport and depth range.
  5708. * const m = Cesium.Matrix4.computeViewportTransformation({
  5709. * x : 0.0,
  5710. * y : 0.0,
  5711. * width : 1024.0,
  5712. * height : 768.0
  5713. * }, 0.0, 1.0, new Cesium.Matrix4());
  5714. */
  5715. Matrix4.computeViewportTransformation = function (
  5716. viewport,
  5717. nearDepthRange,
  5718. farDepthRange,
  5719. result
  5720. ) {
  5721. if (!defaultValue.defined(result)) {
  5722. result = new Matrix4();
  5723. }
  5724. viewport = defaultValue.defaultValue(viewport, defaultValue.defaultValue.EMPTY_OBJECT);
  5725. const x = defaultValue.defaultValue(viewport.x, 0.0);
  5726. const y = defaultValue.defaultValue(viewport.y, 0.0);
  5727. const width = defaultValue.defaultValue(viewport.width, 0.0);
  5728. const height = defaultValue.defaultValue(viewport.height, 0.0);
  5729. nearDepthRange = defaultValue.defaultValue(nearDepthRange, 0.0);
  5730. farDepthRange = defaultValue.defaultValue(farDepthRange, 1.0);
  5731. const halfWidth = width * 0.5;
  5732. const halfHeight = height * 0.5;
  5733. const halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  5734. const column0Row0 = halfWidth;
  5735. const column1Row1 = halfHeight;
  5736. const column2Row2 = halfDepth;
  5737. const column3Row0 = x + halfWidth;
  5738. const column3Row1 = y + halfHeight;
  5739. const column3Row2 = nearDepthRange + halfDepth;
  5740. const column3Row3 = 1.0;
  5741. result[0] = column0Row0;
  5742. result[1] = 0.0;
  5743. result[2] = 0.0;
  5744. result[3] = 0.0;
  5745. result[4] = 0.0;
  5746. result[5] = column1Row1;
  5747. result[6] = 0.0;
  5748. result[7] = 0.0;
  5749. result[8] = 0.0;
  5750. result[9] = 0.0;
  5751. result[10] = column2Row2;
  5752. result[11] = 0.0;
  5753. result[12] = column3Row0;
  5754. result[13] = column3Row1;
  5755. result[14] = column3Row2;
  5756. result[15] = column3Row3;
  5757. return result;
  5758. };
  5759. /**
  5760. * Computes a Matrix4 instance that transforms from world space to view space.
  5761. *
  5762. * @param {Cartesian3} position The position of the camera.
  5763. * @param {Cartesian3} direction The forward direction.
  5764. * @param {Cartesian3} up The up direction.
  5765. * @param {Cartesian3} right The right direction.
  5766. * @param {Matrix4} result The object in which the result will be stored.
  5767. * @returns {Matrix4} The modified result parameter.
  5768. */
  5769. Matrix4.computeView = function (position, direction, up, right, result) {
  5770. //>>includeStart('debug', pragmas.debug);
  5771. RuntimeError.Check.typeOf.object("position", position);
  5772. RuntimeError.Check.typeOf.object("direction", direction);
  5773. RuntimeError.Check.typeOf.object("up", up);
  5774. RuntimeError.Check.typeOf.object("right", right);
  5775. RuntimeError.Check.typeOf.object("result", result);
  5776. //>>includeEnd('debug');
  5777. result[0] = right.x;
  5778. result[1] = up.x;
  5779. result[2] = -direction.x;
  5780. result[3] = 0.0;
  5781. result[4] = right.y;
  5782. result[5] = up.y;
  5783. result[6] = -direction.y;
  5784. result[7] = 0.0;
  5785. result[8] = right.z;
  5786. result[9] = up.z;
  5787. result[10] = -direction.z;
  5788. result[11] = 0.0;
  5789. result[12] = -Cartesian3.dot(right, position);
  5790. result[13] = -Cartesian3.dot(up, position);
  5791. result[14] = Cartesian3.dot(direction, position);
  5792. result[15] = 1.0;
  5793. return result;
  5794. };
  5795. /**
  5796. * Computes an Array from the provided Matrix4 instance.
  5797. * The array will be in column-major order.
  5798. *
  5799. * @param {Matrix4} matrix The matrix to use..
  5800. * @param {Number[]} [result] The Array onto which to store the result.
  5801. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  5802. *
  5803. * @example
  5804. * //create an array from an instance of Matrix4
  5805. * // m = [10.0, 14.0, 18.0, 22.0]
  5806. * // [11.0, 15.0, 19.0, 23.0]
  5807. * // [12.0, 16.0, 20.0, 24.0]
  5808. * // [13.0, 17.0, 21.0, 25.0]
  5809. * const a = Cesium.Matrix4.toArray(m);
  5810. *
  5811. * // m remains the same
  5812. * //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]
  5813. */
  5814. Matrix4.toArray = function (matrix, result) {
  5815. //>>includeStart('debug', pragmas.debug);
  5816. RuntimeError.Check.typeOf.object("matrix", matrix);
  5817. //>>includeEnd('debug');
  5818. if (!defaultValue.defined(result)) {
  5819. return [
  5820. matrix[0],
  5821. matrix[1],
  5822. matrix[2],
  5823. matrix[3],
  5824. matrix[4],
  5825. matrix[5],
  5826. matrix[6],
  5827. matrix[7],
  5828. matrix[8],
  5829. matrix[9],
  5830. matrix[10],
  5831. matrix[11],
  5832. matrix[12],
  5833. matrix[13],
  5834. matrix[14],
  5835. matrix[15],
  5836. ];
  5837. }
  5838. result[0] = matrix[0];
  5839. result[1] = matrix[1];
  5840. result[2] = matrix[2];
  5841. result[3] = matrix[3];
  5842. result[4] = matrix[4];
  5843. result[5] = matrix[5];
  5844. result[6] = matrix[6];
  5845. result[7] = matrix[7];
  5846. result[8] = matrix[8];
  5847. result[9] = matrix[9];
  5848. result[10] = matrix[10];
  5849. result[11] = matrix[11];
  5850. result[12] = matrix[12];
  5851. result[13] = matrix[13];
  5852. result[14] = matrix[14];
  5853. result[15] = matrix[15];
  5854. return result;
  5855. };
  5856. /**
  5857. * Computes the array index of the element at the provided row and column.
  5858. *
  5859. * @param {Number} row The zero-based index of the row.
  5860. * @param {Number} column The zero-based index of the column.
  5861. * @returns {Number} The index of the element at the provided row and column.
  5862. *
  5863. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  5864. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  5865. *
  5866. * @example
  5867. * const myMatrix = new Cesium.Matrix4();
  5868. * const column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  5869. * const column1Row0 = myMatrix[column1Row0Index];
  5870. * myMatrix[column1Row0Index] = 10.0;
  5871. */
  5872. Matrix4.getElementIndex = function (column, row) {
  5873. //>>includeStart('debug', pragmas.debug);
  5874. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  5875. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 3);
  5876. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  5877. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 3);
  5878. //>>includeEnd('debug');
  5879. return column * 4 + row;
  5880. };
  5881. /**
  5882. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  5883. *
  5884. * @param {Matrix4} matrix The matrix to use.
  5885. * @param {Number} index The zero-based index of the column to retrieve.
  5886. * @param {Cartesian4} result The object onto which to store the result.
  5887. * @returns {Cartesian4} The modified result parameter.
  5888. *
  5889. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5890. *
  5891. * @example
  5892. * //returns a Cartesian4 instance with values from the specified column
  5893. * // m = [10.0, 11.0, 12.0, 13.0]
  5894. * // [14.0, 15.0, 16.0, 17.0]
  5895. * // [18.0, 19.0, 20.0, 21.0]
  5896. * // [22.0, 23.0, 24.0, 25.0]
  5897. *
  5898. * //Example 1: Creates an instance of Cartesian
  5899. * const a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  5900. *
  5901. * @example
  5902. * //Example 2: Sets values for Cartesian instance
  5903. * const a = new Cesium.Cartesian4();
  5904. * Cesium.Matrix4.getColumn(m, 2, a);
  5905. *
  5906. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  5907. */
  5908. Matrix4.getColumn = function (matrix, index, result) {
  5909. //>>includeStart('debug', pragmas.debug);
  5910. RuntimeError.Check.typeOf.object("matrix", matrix);
  5911. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5912. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  5913. RuntimeError.Check.typeOf.object("result", result);
  5914. //>>includeEnd('debug');
  5915. const startIndex = index * 4;
  5916. const x = matrix[startIndex];
  5917. const y = matrix[startIndex + 1];
  5918. const z = matrix[startIndex + 2];
  5919. const w = matrix[startIndex + 3];
  5920. result.x = x;
  5921. result.y = y;
  5922. result.z = z;
  5923. result.w = w;
  5924. return result;
  5925. };
  5926. /**
  5927. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  5928. *
  5929. * @param {Matrix4} matrix The matrix to use.
  5930. * @param {Number} index The zero-based index of the column to set.
  5931. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  5932. * @param {Matrix4} result The object onto which to store the result.
  5933. * @returns {Matrix4} The modified result parameter.
  5934. *
  5935. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5936. *
  5937. * @example
  5938. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  5939. * // m = [10.0, 11.0, 12.0, 13.0]
  5940. * // [14.0, 15.0, 16.0, 17.0]
  5941. * // [18.0, 19.0, 20.0, 21.0]
  5942. * // [22.0, 23.0, 24.0, 25.0]
  5943. *
  5944. * const a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  5945. *
  5946. * // m remains the same
  5947. * // a = [10.0, 11.0, 99.0, 13.0]
  5948. * // [14.0, 15.0, 98.0, 17.0]
  5949. * // [18.0, 19.0, 97.0, 21.0]
  5950. * // [22.0, 23.0, 96.0, 25.0]
  5951. */
  5952. Matrix4.setColumn = function (matrix, index, cartesian, result) {
  5953. //>>includeStart('debug', pragmas.debug);
  5954. RuntimeError.Check.typeOf.object("matrix", matrix);
  5955. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5956. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  5957. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  5958. RuntimeError.Check.typeOf.object("result", result);
  5959. //>>includeEnd('debug');
  5960. result = Matrix4.clone(matrix, result);
  5961. const startIndex = index * 4;
  5962. result[startIndex] = cartesian.x;
  5963. result[startIndex + 1] = cartesian.y;
  5964. result[startIndex + 2] = cartesian.z;
  5965. result[startIndex + 3] = cartesian.w;
  5966. return result;
  5967. };
  5968. /**
  5969. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  5970. *
  5971. * @param {Matrix4} matrix The matrix to use.
  5972. * @param {Number} index The zero-based index of the row to retrieve.
  5973. * @param {Cartesian4} result The object onto which to store the result.
  5974. * @returns {Cartesian4} The modified result parameter.
  5975. *
  5976. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5977. *
  5978. * @example
  5979. * //returns a Cartesian4 instance with values from the specified column
  5980. * // m = [10.0, 11.0, 12.0, 13.0]
  5981. * // [14.0, 15.0, 16.0, 17.0]
  5982. * // [18.0, 19.0, 20.0, 21.0]
  5983. * // [22.0, 23.0, 24.0, 25.0]
  5984. *
  5985. * //Example 1: Returns an instance of Cartesian
  5986. * const a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  5987. *
  5988. * @example
  5989. * //Example 2: Sets values for a Cartesian instance
  5990. * const a = new Cesium.Cartesian4();
  5991. * Cesium.Matrix4.getRow(m, 2, a);
  5992. *
  5993. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  5994. */
  5995. Matrix4.getRow = function (matrix, index, result) {
  5996. //>>includeStart('debug', pragmas.debug);
  5997. RuntimeError.Check.typeOf.object("matrix", matrix);
  5998. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5999. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  6000. RuntimeError.Check.typeOf.object("result", result);
  6001. //>>includeEnd('debug');
  6002. const x = matrix[index];
  6003. const y = matrix[index + 4];
  6004. const z = matrix[index + 8];
  6005. const w = matrix[index + 12];
  6006. result.x = x;
  6007. result.y = y;
  6008. result.z = z;
  6009. result.w = w;
  6010. return result;
  6011. };
  6012. /**
  6013. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  6014. *
  6015. * @param {Matrix4} matrix The matrix to use.
  6016. * @param {Number} index The zero-based index of the row to set.
  6017. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  6018. * @param {Matrix4} result The object onto which to store the result.
  6019. * @returns {Matrix4} The modified result parameter.
  6020. *
  6021. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  6022. *
  6023. * @example
  6024. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  6025. * // m = [10.0, 11.0, 12.0, 13.0]
  6026. * // [14.0, 15.0, 16.0, 17.0]
  6027. * // [18.0, 19.0, 20.0, 21.0]
  6028. * // [22.0, 23.0, 24.0, 25.0]
  6029. *
  6030. * const a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  6031. *
  6032. * // m remains the same
  6033. * // a = [10.0, 11.0, 12.0, 13.0]
  6034. * // [14.0, 15.0, 16.0, 17.0]
  6035. * // [99.0, 98.0, 97.0, 96.0]
  6036. * // [22.0, 23.0, 24.0, 25.0]
  6037. */
  6038. Matrix4.setRow = function (matrix, index, cartesian, result) {
  6039. //>>includeStart('debug', pragmas.debug);
  6040. RuntimeError.Check.typeOf.object("matrix", matrix);
  6041. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  6042. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  6043. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6044. RuntimeError.Check.typeOf.object("result", result);
  6045. //>>includeEnd('debug');
  6046. result = Matrix4.clone(matrix, result);
  6047. result[index] = cartesian.x;
  6048. result[index + 4] = cartesian.y;
  6049. result[index + 8] = cartesian.z;
  6050. result[index + 12] = cartesian.w;
  6051. return result;
  6052. };
  6053. /**
  6054. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  6055. * matrix with the provided translation. This assumes the matrix is an affine transformation.
  6056. *
  6057. * @param {Matrix4} matrix The matrix to use.
  6058. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  6059. * @param {Matrix4} result The object onto which to store the result.
  6060. * @returns {Matrix4} The modified result parameter.
  6061. */
  6062. Matrix4.setTranslation = function (matrix, translation, result) {
  6063. //>>includeStart('debug', pragmas.debug);
  6064. RuntimeError.Check.typeOf.object("matrix", matrix);
  6065. RuntimeError.Check.typeOf.object("translation", translation);
  6066. RuntimeError.Check.typeOf.object("result", result);
  6067. //>>includeEnd('debug');
  6068. result[0] = matrix[0];
  6069. result[1] = matrix[1];
  6070. result[2] = matrix[2];
  6071. result[3] = matrix[3];
  6072. result[4] = matrix[4];
  6073. result[5] = matrix[5];
  6074. result[6] = matrix[6];
  6075. result[7] = matrix[7];
  6076. result[8] = matrix[8];
  6077. result[9] = matrix[9];
  6078. result[10] = matrix[10];
  6079. result[11] = matrix[11];
  6080. result[12] = translation.x;
  6081. result[13] = translation.y;
  6082. result[14] = translation.z;
  6083. result[15] = matrix[15];
  6084. return result;
  6085. };
  6086. const scaleScratch1$1 = new Cartesian3();
  6087. /**
  6088. * Computes a new matrix that replaces the scale with the provided scale.
  6089. * This assumes the matrix is an affine transformation.
  6090. *
  6091. * @param {Matrix4} matrix The matrix to use.
  6092. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  6093. * @param {Matrix4} result The object onto which to store the result.
  6094. * @returns {Matrix4} The modified result parameter.
  6095. *
  6096. * @see Matrix4.setUniformScale
  6097. * @see Matrix4.fromScale
  6098. * @see Matrix4.fromUniformScale
  6099. * @see Matrix4.multiplyByScale
  6100. * @see Matrix4.multiplyByUniformScale
  6101. * @see Matrix4.getScale
  6102. */
  6103. Matrix4.setScale = function (matrix, scale, result) {
  6104. //>>includeStart('debug', pragmas.debug);
  6105. RuntimeError.Check.typeOf.object("matrix", matrix);
  6106. RuntimeError.Check.typeOf.object("scale", scale);
  6107. RuntimeError.Check.typeOf.object("result", result);
  6108. //>>includeEnd('debug');
  6109. const existingScale = Matrix4.getScale(matrix, scaleScratch1$1);
  6110. const scaleRatioX = scale.x / existingScale.x;
  6111. const scaleRatioY = scale.y / existingScale.y;
  6112. const scaleRatioZ = scale.z / existingScale.z;
  6113. result[0] = matrix[0] * scaleRatioX;
  6114. result[1] = matrix[1] * scaleRatioX;
  6115. result[2] = matrix[2] * scaleRatioX;
  6116. result[3] = matrix[3];
  6117. result[4] = matrix[4] * scaleRatioY;
  6118. result[5] = matrix[5] * scaleRatioY;
  6119. result[6] = matrix[6] * scaleRatioY;
  6120. result[7] = matrix[7];
  6121. result[8] = matrix[8] * scaleRatioZ;
  6122. result[9] = matrix[9] * scaleRatioZ;
  6123. result[10] = matrix[10] * scaleRatioZ;
  6124. result[11] = matrix[11];
  6125. result[12] = matrix[12];
  6126. result[13] = matrix[13];
  6127. result[14] = matrix[14];
  6128. result[15] = matrix[15];
  6129. return result;
  6130. };
  6131. const scaleScratch2$1 = new Cartesian3();
  6132. /**
  6133. * Computes a new matrix that replaces the scale with the provided uniform scale.
  6134. * This assumes the matrix is an affine transformation.
  6135. *
  6136. * @param {Matrix4} matrix The matrix to use.
  6137. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  6138. * @param {Matrix4} result The object onto which to store the result.
  6139. * @returns {Matrix4} The modified result parameter.
  6140. *
  6141. * @see Matrix4.setScale
  6142. * @see Matrix4.fromScale
  6143. * @see Matrix4.fromUniformScale
  6144. * @see Matrix4.multiplyByScale
  6145. * @see Matrix4.multiplyByUniformScale
  6146. * @see Matrix4.getScale
  6147. */
  6148. Matrix4.setUniformScale = function (matrix, scale, result) {
  6149. //>>includeStart('debug', pragmas.debug);
  6150. RuntimeError.Check.typeOf.object("matrix", matrix);
  6151. RuntimeError.Check.typeOf.number("scale", scale);
  6152. RuntimeError.Check.typeOf.object("result", result);
  6153. //>>includeEnd('debug');
  6154. const existingScale = Matrix4.getScale(matrix, scaleScratch2$1);
  6155. const scaleRatioX = scale / existingScale.x;
  6156. const scaleRatioY = scale / existingScale.y;
  6157. const scaleRatioZ = scale / existingScale.z;
  6158. result[0] = matrix[0] * scaleRatioX;
  6159. result[1] = matrix[1] * scaleRatioX;
  6160. result[2] = matrix[2] * scaleRatioX;
  6161. result[3] = matrix[3];
  6162. result[4] = matrix[4] * scaleRatioY;
  6163. result[5] = matrix[5] * scaleRatioY;
  6164. result[6] = matrix[6] * scaleRatioY;
  6165. result[7] = matrix[7];
  6166. result[8] = matrix[8] * scaleRatioZ;
  6167. result[9] = matrix[9] * scaleRatioZ;
  6168. result[10] = matrix[10] * scaleRatioZ;
  6169. result[11] = matrix[11];
  6170. result[12] = matrix[12];
  6171. result[13] = matrix[13];
  6172. result[14] = matrix[14];
  6173. result[15] = matrix[15];
  6174. return result;
  6175. };
  6176. const scratchColumn$1 = new Cartesian3();
  6177. /**
  6178. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  6179. *
  6180. * @param {Matrix4} matrix The matrix.
  6181. * @param {Cartesian3} result The object onto which to store the result.
  6182. * @returns {Cartesian3} The modified result parameter
  6183. *
  6184. * @see Matrix4.multiplyByScale
  6185. * @see Matrix4.multiplyByUniformScale
  6186. * @see Matrix4.fromScale
  6187. * @see Matrix4.fromUniformScale
  6188. * @see Matrix4.setScale
  6189. * @see Matrix4.setUniformScale
  6190. */
  6191. Matrix4.getScale = function (matrix, result) {
  6192. //>>includeStart('debug', pragmas.debug);
  6193. RuntimeError.Check.typeOf.object("matrix", matrix);
  6194. RuntimeError.Check.typeOf.object("result", result);
  6195. //>>includeEnd('debug');
  6196. result.x = Cartesian3.magnitude(
  6197. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$1)
  6198. );
  6199. result.y = Cartesian3.magnitude(
  6200. Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn$1)
  6201. );
  6202. result.z = Cartesian3.magnitude(
  6203. Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn$1)
  6204. );
  6205. return result;
  6206. };
  6207. const scaleScratch3$1 = new Cartesian3();
  6208. /**
  6209. * Computes the maximum scale assuming the matrix is an affine transformation.
  6210. * The maximum scale is the maximum length of the column vectors in the upper-left
  6211. * 3x3 matrix.
  6212. *
  6213. * @param {Matrix4} matrix The matrix.
  6214. * @returns {Number} The maximum scale.
  6215. */
  6216. Matrix4.getMaximumScale = function (matrix) {
  6217. Matrix4.getScale(matrix, scaleScratch3$1);
  6218. return Cartesian3.maximumComponent(scaleScratch3$1);
  6219. };
  6220. const scaleScratch4$1 = new Cartesian3();
  6221. /**
  6222. * Sets the rotation assuming the matrix is an affine transformation.
  6223. *
  6224. * @param {Matrix4} matrix The matrix.
  6225. * @param {Matrix4} rotation The rotation matrix.
  6226. * @returns {Matrix4} The modified result parameter.
  6227. *
  6228. * @see Matrix4.fromRotation
  6229. * @see Matrix4.getRotation
  6230. */
  6231. Matrix4.setRotation = function (matrix, rotation, result) {
  6232. //>>includeStart('debug', pragmas.debug);
  6233. RuntimeError.Check.typeOf.object("matrix", matrix);
  6234. RuntimeError.Check.typeOf.object("result", result);
  6235. //>>includeEnd('debug');
  6236. const scale = Matrix4.getScale(matrix, scaleScratch4$1);
  6237. result[0] = rotation[0] * scale.x;
  6238. result[1] = rotation[1] * scale.x;
  6239. result[2] = rotation[2] * scale.x;
  6240. result[3] = matrix[3];
  6241. result[4] = rotation[3] * scale.y;
  6242. result[5] = rotation[4] * scale.y;
  6243. result[6] = rotation[5] * scale.y;
  6244. result[7] = matrix[7];
  6245. result[8] = rotation[6] * scale.z;
  6246. result[9] = rotation[7] * scale.z;
  6247. result[10] = rotation[8] * scale.z;
  6248. result[11] = matrix[11];
  6249. result[12] = matrix[12];
  6250. result[13] = matrix[13];
  6251. result[14] = matrix[14];
  6252. result[15] = matrix[15];
  6253. return result;
  6254. };
  6255. const scaleScratch5$1 = new Cartesian3();
  6256. /**
  6257. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  6258. *
  6259. * @param {Matrix4} matrix The matrix.
  6260. * @param {Matrix4} result The object onto which to store the result.
  6261. * @returns {Matrix4} The modified result parameter.
  6262. *
  6263. * @see Matrix4.setRotation
  6264. * @see Matrix4.fromRotation
  6265. */
  6266. Matrix4.getRotation = function (matrix, result) {
  6267. //>>includeStart('debug', pragmas.debug);
  6268. RuntimeError.Check.typeOf.object("matrix", matrix);
  6269. RuntimeError.Check.typeOf.object("result", result);
  6270. //>>includeEnd('debug');
  6271. const scale = Matrix4.getScale(matrix, scaleScratch5$1);
  6272. result[0] = matrix[0] / scale.x;
  6273. result[1] = matrix[1] / scale.x;
  6274. result[2] = matrix[2] / scale.x;
  6275. result[3] = matrix[4] / scale.y;
  6276. result[4] = matrix[5] / scale.y;
  6277. result[5] = matrix[6] / scale.y;
  6278. result[6] = matrix[8] / scale.z;
  6279. result[7] = matrix[9] / scale.z;
  6280. result[8] = matrix[10] / scale.z;
  6281. return result;
  6282. };
  6283. /**
  6284. * Computes the product of two matrices.
  6285. *
  6286. * @param {Matrix4} left The first matrix.
  6287. * @param {Matrix4} right The second matrix.
  6288. * @param {Matrix4} result The object onto which to store the result.
  6289. * @returns {Matrix4} The modified result parameter.
  6290. */
  6291. Matrix4.multiply = function (left, right, result) {
  6292. //>>includeStart('debug', pragmas.debug);
  6293. RuntimeError.Check.typeOf.object("left", left);
  6294. RuntimeError.Check.typeOf.object("right", right);
  6295. RuntimeError.Check.typeOf.object("result", result);
  6296. //>>includeEnd('debug');
  6297. const left0 = left[0];
  6298. const left1 = left[1];
  6299. const left2 = left[2];
  6300. const left3 = left[3];
  6301. const left4 = left[4];
  6302. const left5 = left[5];
  6303. const left6 = left[6];
  6304. const left7 = left[7];
  6305. const left8 = left[8];
  6306. const left9 = left[9];
  6307. const left10 = left[10];
  6308. const left11 = left[11];
  6309. const left12 = left[12];
  6310. const left13 = left[13];
  6311. const left14 = left[14];
  6312. const left15 = left[15];
  6313. const right0 = right[0];
  6314. const right1 = right[1];
  6315. const right2 = right[2];
  6316. const right3 = right[3];
  6317. const right4 = right[4];
  6318. const right5 = right[5];
  6319. const right6 = right[6];
  6320. const right7 = right[7];
  6321. const right8 = right[8];
  6322. const right9 = right[9];
  6323. const right10 = right[10];
  6324. const right11 = right[11];
  6325. const right12 = right[12];
  6326. const right13 = right[13];
  6327. const right14 = right[14];
  6328. const right15 = right[15];
  6329. const column0Row0 =
  6330. left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  6331. const column0Row1 =
  6332. left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  6333. const column0Row2 =
  6334. left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  6335. const column0Row3 =
  6336. left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  6337. const column1Row0 =
  6338. left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  6339. const column1Row1 =
  6340. left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  6341. const column1Row2 =
  6342. left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  6343. const column1Row3 =
  6344. left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  6345. const column2Row0 =
  6346. left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  6347. const column2Row1 =
  6348. left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  6349. const column2Row2 =
  6350. left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  6351. const column2Row3 =
  6352. left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  6353. const column3Row0 =
  6354. left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  6355. const column3Row1 =
  6356. left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  6357. const column3Row2 =
  6358. left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  6359. const column3Row3 =
  6360. left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  6361. result[0] = column0Row0;
  6362. result[1] = column0Row1;
  6363. result[2] = column0Row2;
  6364. result[3] = column0Row3;
  6365. result[4] = column1Row0;
  6366. result[5] = column1Row1;
  6367. result[6] = column1Row2;
  6368. result[7] = column1Row3;
  6369. result[8] = column2Row0;
  6370. result[9] = column2Row1;
  6371. result[10] = column2Row2;
  6372. result[11] = column2Row3;
  6373. result[12] = column3Row0;
  6374. result[13] = column3Row1;
  6375. result[14] = column3Row2;
  6376. result[15] = column3Row3;
  6377. return result;
  6378. };
  6379. /**
  6380. * Computes the sum of two matrices.
  6381. *
  6382. * @param {Matrix4} left The first matrix.
  6383. * @param {Matrix4} right The second matrix.
  6384. * @param {Matrix4} result The object onto which to store the result.
  6385. * @returns {Matrix4} The modified result parameter.
  6386. */
  6387. Matrix4.add = function (left, right, result) {
  6388. //>>includeStart('debug', pragmas.debug);
  6389. RuntimeError.Check.typeOf.object("left", left);
  6390. RuntimeError.Check.typeOf.object("right", right);
  6391. RuntimeError.Check.typeOf.object("result", result);
  6392. //>>includeEnd('debug');
  6393. result[0] = left[0] + right[0];
  6394. result[1] = left[1] + right[1];
  6395. result[2] = left[2] + right[2];
  6396. result[3] = left[3] + right[3];
  6397. result[4] = left[4] + right[4];
  6398. result[5] = left[5] + right[5];
  6399. result[6] = left[6] + right[6];
  6400. result[7] = left[7] + right[7];
  6401. result[8] = left[8] + right[8];
  6402. result[9] = left[9] + right[9];
  6403. result[10] = left[10] + right[10];
  6404. result[11] = left[11] + right[11];
  6405. result[12] = left[12] + right[12];
  6406. result[13] = left[13] + right[13];
  6407. result[14] = left[14] + right[14];
  6408. result[15] = left[15] + right[15];
  6409. return result;
  6410. };
  6411. /**
  6412. * Computes the difference of two matrices.
  6413. *
  6414. * @param {Matrix4} left The first matrix.
  6415. * @param {Matrix4} right The second matrix.
  6416. * @param {Matrix4} result The object onto which to store the result.
  6417. * @returns {Matrix4} The modified result parameter.
  6418. */
  6419. Matrix4.subtract = function (left, right, result) {
  6420. //>>includeStart('debug', pragmas.debug);
  6421. RuntimeError.Check.typeOf.object("left", left);
  6422. RuntimeError.Check.typeOf.object("right", right);
  6423. RuntimeError.Check.typeOf.object("result", result);
  6424. //>>includeEnd('debug');
  6425. result[0] = left[0] - right[0];
  6426. result[1] = left[1] - right[1];
  6427. result[2] = left[2] - right[2];
  6428. result[3] = left[3] - right[3];
  6429. result[4] = left[4] - right[4];
  6430. result[5] = left[5] - right[5];
  6431. result[6] = left[6] - right[6];
  6432. result[7] = left[7] - right[7];
  6433. result[8] = left[8] - right[8];
  6434. result[9] = left[9] - right[9];
  6435. result[10] = left[10] - right[10];
  6436. result[11] = left[11] - right[11];
  6437. result[12] = left[12] - right[12];
  6438. result[13] = left[13] - right[13];
  6439. result[14] = left[14] - right[14];
  6440. result[15] = left[15] - right[15];
  6441. return result;
  6442. };
  6443. /**
  6444. * Computes the product of two matrices assuming the matrices are affine transformation matrices,
  6445. * where the upper left 3x3 elements are any matrix, and
  6446. * the upper three elements in the fourth column are the translation.
  6447. * The bottom row is assumed to be [0, 0, 0, 1].
  6448. * The matrix is not verified to be in the proper form.
  6449. * This method is faster than computing the product for general 4x4
  6450. * matrices using {@link Matrix4.multiply}.
  6451. *
  6452. * @param {Matrix4} left The first matrix.
  6453. * @param {Matrix4} right The second matrix.
  6454. * @param {Matrix4} result The object onto which to store the result.
  6455. * @returns {Matrix4} The modified result parameter.
  6456. *
  6457. * @example
  6458. * const 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);
  6459. * const m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  6460. * const m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  6461. */
  6462. Matrix4.multiplyTransformation = function (left, right, result) {
  6463. //>>includeStart('debug', pragmas.debug);
  6464. RuntimeError.Check.typeOf.object("left", left);
  6465. RuntimeError.Check.typeOf.object("right", right);
  6466. RuntimeError.Check.typeOf.object("result", result);
  6467. //>>includeEnd('debug');
  6468. const left0 = left[0];
  6469. const left1 = left[1];
  6470. const left2 = left[2];
  6471. const left4 = left[4];
  6472. const left5 = left[5];
  6473. const left6 = left[6];
  6474. const left8 = left[8];
  6475. const left9 = left[9];
  6476. const left10 = left[10];
  6477. const left12 = left[12];
  6478. const left13 = left[13];
  6479. const left14 = left[14];
  6480. const right0 = right[0];
  6481. const right1 = right[1];
  6482. const right2 = right[2];
  6483. const right4 = right[4];
  6484. const right5 = right[5];
  6485. const right6 = right[6];
  6486. const right8 = right[8];
  6487. const right9 = right[9];
  6488. const right10 = right[10];
  6489. const right12 = right[12];
  6490. const right13 = right[13];
  6491. const right14 = right[14];
  6492. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6493. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6494. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6495. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6496. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6497. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6498. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6499. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6500. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6501. const column3Row0 =
  6502. left0 * right12 + left4 * right13 + left8 * right14 + left12;
  6503. const column3Row1 =
  6504. left1 * right12 + left5 * right13 + left9 * right14 + left13;
  6505. const column3Row2 =
  6506. left2 * right12 + left6 * right13 + left10 * right14 + left14;
  6507. result[0] = column0Row0;
  6508. result[1] = column0Row1;
  6509. result[2] = column0Row2;
  6510. result[3] = 0.0;
  6511. result[4] = column1Row0;
  6512. result[5] = column1Row1;
  6513. result[6] = column1Row2;
  6514. result[7] = 0.0;
  6515. result[8] = column2Row0;
  6516. result[9] = column2Row1;
  6517. result[10] = column2Row2;
  6518. result[11] = 0.0;
  6519. result[12] = column3Row0;
  6520. result[13] = column3Row1;
  6521. result[14] = column3Row2;
  6522. result[15] = 1.0;
  6523. return result;
  6524. };
  6525. /**
  6526. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6527. * by a 3x3 rotation matrix. This is an optimization
  6528. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  6529. *
  6530. * @param {Matrix4} matrix The matrix on the left-hand side.
  6531. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  6532. * @param {Matrix4} result The object onto which to store the result.
  6533. * @returns {Matrix4} The modified result parameter.
  6534. *
  6535. * @example
  6536. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  6537. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  6538. */
  6539. Matrix4.multiplyByMatrix3 = function (matrix, rotation, result) {
  6540. //>>includeStart('debug', pragmas.debug);
  6541. RuntimeError.Check.typeOf.object("matrix", matrix);
  6542. RuntimeError.Check.typeOf.object("rotation", rotation);
  6543. RuntimeError.Check.typeOf.object("result", result);
  6544. //>>includeEnd('debug');
  6545. const left0 = matrix[0];
  6546. const left1 = matrix[1];
  6547. const left2 = matrix[2];
  6548. const left4 = matrix[4];
  6549. const left5 = matrix[5];
  6550. const left6 = matrix[6];
  6551. const left8 = matrix[8];
  6552. const left9 = matrix[9];
  6553. const left10 = matrix[10];
  6554. const right0 = rotation[0];
  6555. const right1 = rotation[1];
  6556. const right2 = rotation[2];
  6557. const right4 = rotation[3];
  6558. const right5 = rotation[4];
  6559. const right6 = rotation[5];
  6560. const right8 = rotation[6];
  6561. const right9 = rotation[7];
  6562. const right10 = rotation[8];
  6563. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6564. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6565. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6566. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6567. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6568. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6569. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6570. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6571. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6572. result[0] = column0Row0;
  6573. result[1] = column0Row1;
  6574. result[2] = column0Row2;
  6575. result[3] = 0.0;
  6576. result[4] = column1Row0;
  6577. result[5] = column1Row1;
  6578. result[6] = column1Row2;
  6579. result[7] = 0.0;
  6580. result[8] = column2Row0;
  6581. result[9] = column2Row1;
  6582. result[10] = column2Row2;
  6583. result[11] = 0.0;
  6584. result[12] = matrix[12];
  6585. result[13] = matrix[13];
  6586. result[14] = matrix[14];
  6587. result[15] = matrix[15];
  6588. return result;
  6589. };
  6590. /**
  6591. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6592. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  6593. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  6594. *
  6595. * @param {Matrix4} matrix The matrix on the left-hand side.
  6596. * @param {Cartesian3} translation The translation on the right-hand side.
  6597. * @param {Matrix4} result The object onto which to store the result.
  6598. * @returns {Matrix4} The modified result parameter.
  6599. *
  6600. * @example
  6601. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  6602. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  6603. */
  6604. Matrix4.multiplyByTranslation = function (matrix, translation, result) {
  6605. //>>includeStart('debug', pragmas.debug);
  6606. RuntimeError.Check.typeOf.object("matrix", matrix);
  6607. RuntimeError.Check.typeOf.object("translation", translation);
  6608. RuntimeError.Check.typeOf.object("result", result);
  6609. //>>includeEnd('debug');
  6610. const x = translation.x;
  6611. const y = translation.y;
  6612. const z = translation.z;
  6613. const tx = x * matrix[0] + y * matrix[4] + z * matrix[8] + matrix[12];
  6614. const ty = x * matrix[1] + y * matrix[5] + z * matrix[9] + matrix[13];
  6615. const tz = x * matrix[2] + y * matrix[6] + z * matrix[10] + matrix[14];
  6616. result[0] = matrix[0];
  6617. result[1] = matrix[1];
  6618. result[2] = matrix[2];
  6619. result[3] = matrix[3];
  6620. result[4] = matrix[4];
  6621. result[5] = matrix[5];
  6622. result[6] = matrix[6];
  6623. result[7] = matrix[7];
  6624. result[8] = matrix[8];
  6625. result[9] = matrix[9];
  6626. result[10] = matrix[10];
  6627. result[11] = matrix[11];
  6628. result[12] = tx;
  6629. result[13] = ty;
  6630. result[14] = tz;
  6631. result[15] = matrix[15];
  6632. return result;
  6633. };
  6634. /**
  6635. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6636. * by an implicit non-uniform scale matrix. This is an optimization
  6637. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  6638. * <code>m</code> must be an affine matrix.
  6639. * This function performs fewer allocations and arithmetic operations.
  6640. *
  6641. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  6642. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  6643. * @param {Matrix4} result The object onto which to store the result.
  6644. * @returns {Matrix4} The modified result parameter.
  6645. *
  6646. *
  6647. * @example
  6648. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  6649. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  6650. *
  6651. * @see Matrix4.multiplyByUniformScale
  6652. * @see Matrix4.fromScale
  6653. * @see Matrix4.fromUniformScale
  6654. * @see Matrix4.setScale
  6655. * @see Matrix4.setUniformScale
  6656. * @see Matrix4.getScale
  6657. */
  6658. Matrix4.multiplyByScale = function (matrix, scale, result) {
  6659. //>>includeStart('debug', pragmas.debug);
  6660. RuntimeError.Check.typeOf.object("matrix", matrix);
  6661. RuntimeError.Check.typeOf.object("scale", scale);
  6662. RuntimeError.Check.typeOf.object("result", result);
  6663. //>>includeEnd('debug');
  6664. const scaleX = scale.x;
  6665. const scaleY = scale.y;
  6666. const scaleZ = scale.z;
  6667. // Faster than Cartesian3.equals
  6668. if (scaleX === 1.0 && scaleY === 1.0 && scaleZ === 1.0) {
  6669. return Matrix4.clone(matrix, result);
  6670. }
  6671. result[0] = scaleX * matrix[0];
  6672. result[1] = scaleX * matrix[1];
  6673. result[2] = scaleX * matrix[2];
  6674. result[3] = matrix[3];
  6675. result[4] = scaleY * matrix[4];
  6676. result[5] = scaleY * matrix[5];
  6677. result[6] = scaleY * matrix[6];
  6678. result[7] = matrix[7];
  6679. result[8] = scaleZ * matrix[8];
  6680. result[9] = scaleZ * matrix[9];
  6681. result[10] = scaleZ * matrix[10];
  6682. result[11] = matrix[11];
  6683. result[12] = matrix[12];
  6684. result[13] = matrix[13];
  6685. result[14] = matrix[14];
  6686. result[15] = matrix[15];
  6687. return result;
  6688. };
  6689. /**
  6690. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  6691. *
  6692. * @param {Matrix4} matrix The matrix on the left-hand side.
  6693. * @param {Number} scale The uniform scale on the right-hand side.
  6694. * @param {Matrix4} result The object onto which to store the result.
  6695. * @returns {Matrix4} The modified result parameter.
  6696. *
  6697. * @example
  6698. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  6699. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  6700. *
  6701. * @see Matrix4.multiplyByScale
  6702. * @see Matrix4.fromScale
  6703. * @see Matrix4.fromUniformScale
  6704. * @see Matrix4.setScale
  6705. * @see Matrix4.setUniformScale
  6706. * @see Matrix4.getScale
  6707. */
  6708. Matrix4.multiplyByUniformScale = function (matrix, scale, result) {
  6709. //>>includeStart('debug', pragmas.debug);
  6710. RuntimeError.Check.typeOf.object("matrix", matrix);
  6711. RuntimeError.Check.typeOf.number("scale", scale);
  6712. RuntimeError.Check.typeOf.object("result", result);
  6713. //>>includeEnd('debug');
  6714. result[0] = matrix[0] * scale;
  6715. result[1] = matrix[1] * scale;
  6716. result[2] = matrix[2] * scale;
  6717. result[3] = matrix[3];
  6718. result[4] = matrix[4] * scale;
  6719. result[5] = matrix[5] * scale;
  6720. result[6] = matrix[6] * scale;
  6721. result[7] = matrix[7];
  6722. result[8] = matrix[8] * scale;
  6723. result[9] = matrix[9] * scale;
  6724. result[10] = matrix[10] * scale;
  6725. result[11] = matrix[11];
  6726. result[12] = matrix[12];
  6727. result[13] = matrix[13];
  6728. result[14] = matrix[14];
  6729. result[15] = matrix[15];
  6730. return result;
  6731. };
  6732. /**
  6733. * Computes the product of a matrix and a column vector.
  6734. *
  6735. * @param {Matrix4} matrix The matrix.
  6736. * @param {Cartesian4} cartesian The vector.
  6737. * @param {Cartesian4} result The object onto which to store the result.
  6738. * @returns {Cartesian4} The modified result parameter.
  6739. */
  6740. Matrix4.multiplyByVector = function (matrix, cartesian, result) {
  6741. //>>includeStart('debug', pragmas.debug);
  6742. RuntimeError.Check.typeOf.object("matrix", matrix);
  6743. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6744. RuntimeError.Check.typeOf.object("result", result);
  6745. //>>includeEnd('debug');
  6746. const vX = cartesian.x;
  6747. const vY = cartesian.y;
  6748. const vZ = cartesian.z;
  6749. const vW = cartesian.w;
  6750. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  6751. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  6752. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  6753. const w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  6754. result.x = x;
  6755. result.y = y;
  6756. result.z = z;
  6757. result.w = w;
  6758. return result;
  6759. };
  6760. /**
  6761. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  6762. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  6763. *
  6764. * @param {Matrix4} matrix The matrix.
  6765. * @param {Cartesian3} cartesian The point.
  6766. * @param {Cartesian3} result The object onto which to store the result.
  6767. * @returns {Cartesian3} The modified result parameter.
  6768. *
  6769. * @example
  6770. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  6771. * const result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  6772. * // A shortcut for
  6773. * // Cartesian3 p = ...
  6774. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  6775. */
  6776. Matrix4.multiplyByPointAsVector = function (matrix, cartesian, result) {
  6777. //>>includeStart('debug', pragmas.debug);
  6778. RuntimeError.Check.typeOf.object("matrix", matrix);
  6779. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6780. RuntimeError.Check.typeOf.object("result", result);
  6781. //>>includeEnd('debug');
  6782. const vX = cartesian.x;
  6783. const vY = cartesian.y;
  6784. const vZ = cartesian.z;
  6785. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  6786. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  6787. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  6788. result.x = x;
  6789. result.y = y;
  6790. result.z = z;
  6791. return result;
  6792. };
  6793. /**
  6794. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  6795. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  6796. *
  6797. * @param {Matrix4} matrix The matrix.
  6798. * @param {Cartesian3} cartesian The point.
  6799. * @param {Cartesian3} result The object onto which to store the result.
  6800. * @returns {Cartesian3} The modified result parameter.
  6801. *
  6802. * @example
  6803. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  6804. * const result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  6805. */
  6806. Matrix4.multiplyByPoint = function (matrix, cartesian, result) {
  6807. //>>includeStart('debug', pragmas.debug);
  6808. RuntimeError.Check.typeOf.object("matrix", matrix);
  6809. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6810. RuntimeError.Check.typeOf.object("result", result);
  6811. //>>includeEnd('debug');
  6812. const vX = cartesian.x;
  6813. const vY = cartesian.y;
  6814. const vZ = cartesian.z;
  6815. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  6816. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  6817. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  6818. result.x = x;
  6819. result.y = y;
  6820. result.z = z;
  6821. return result;
  6822. };
  6823. /**
  6824. * Computes the product of a matrix and a scalar.
  6825. *
  6826. * @param {Matrix4} matrix The matrix.
  6827. * @param {Number} scalar The number to multiply by.
  6828. * @param {Matrix4} result The object onto which to store the result.
  6829. * @returns {Matrix4} The modified result parameter.
  6830. *
  6831. * @example
  6832. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  6833. * // m = [10.0, 11.0, 12.0, 13.0]
  6834. * // [14.0, 15.0, 16.0, 17.0]
  6835. * // [18.0, 19.0, 20.0, 21.0]
  6836. * // [22.0, 23.0, 24.0, 25.0]
  6837. *
  6838. * const a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  6839. *
  6840. * // m remains the same
  6841. * // a = [-20.0, -22.0, -24.0, -26.0]
  6842. * // [-28.0, -30.0, -32.0, -34.0]
  6843. * // [-36.0, -38.0, -40.0, -42.0]
  6844. * // [-44.0, -46.0, -48.0, -50.0]
  6845. */
  6846. Matrix4.multiplyByScalar = function (matrix, scalar, result) {
  6847. //>>includeStart('debug', pragmas.debug);
  6848. RuntimeError.Check.typeOf.object("matrix", matrix);
  6849. RuntimeError.Check.typeOf.number("scalar", scalar);
  6850. RuntimeError.Check.typeOf.object("result", result);
  6851. //>>includeEnd('debug');
  6852. result[0] = matrix[0] * scalar;
  6853. result[1] = matrix[1] * scalar;
  6854. result[2] = matrix[2] * scalar;
  6855. result[3] = matrix[3] * scalar;
  6856. result[4] = matrix[4] * scalar;
  6857. result[5] = matrix[5] * scalar;
  6858. result[6] = matrix[6] * scalar;
  6859. result[7] = matrix[7] * scalar;
  6860. result[8] = matrix[8] * scalar;
  6861. result[9] = matrix[9] * scalar;
  6862. result[10] = matrix[10] * scalar;
  6863. result[11] = matrix[11] * scalar;
  6864. result[12] = matrix[12] * scalar;
  6865. result[13] = matrix[13] * scalar;
  6866. result[14] = matrix[14] * scalar;
  6867. result[15] = matrix[15] * scalar;
  6868. return result;
  6869. };
  6870. /**
  6871. * Computes a negated copy of the provided matrix.
  6872. *
  6873. * @param {Matrix4} matrix The matrix to negate.
  6874. * @param {Matrix4} result The object onto which to store the result.
  6875. * @returns {Matrix4} The modified result parameter.
  6876. *
  6877. * @example
  6878. * //create a new Matrix4 instance which is a negation of a Matrix4
  6879. * // m = [10.0, 11.0, 12.0, 13.0]
  6880. * // [14.0, 15.0, 16.0, 17.0]
  6881. * // [18.0, 19.0, 20.0, 21.0]
  6882. * // [22.0, 23.0, 24.0, 25.0]
  6883. *
  6884. * const a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  6885. *
  6886. * // m remains the same
  6887. * // a = [-10.0, -11.0, -12.0, -13.0]
  6888. * // [-14.0, -15.0, -16.0, -17.0]
  6889. * // [-18.0, -19.0, -20.0, -21.0]
  6890. * // [-22.0, -23.0, -24.0, -25.0]
  6891. */
  6892. Matrix4.negate = function (matrix, result) {
  6893. //>>includeStart('debug', pragmas.debug);
  6894. RuntimeError.Check.typeOf.object("matrix", matrix);
  6895. RuntimeError.Check.typeOf.object("result", result);
  6896. //>>includeEnd('debug');
  6897. result[0] = -matrix[0];
  6898. result[1] = -matrix[1];
  6899. result[2] = -matrix[2];
  6900. result[3] = -matrix[3];
  6901. result[4] = -matrix[4];
  6902. result[5] = -matrix[5];
  6903. result[6] = -matrix[6];
  6904. result[7] = -matrix[7];
  6905. result[8] = -matrix[8];
  6906. result[9] = -matrix[9];
  6907. result[10] = -matrix[10];
  6908. result[11] = -matrix[11];
  6909. result[12] = -matrix[12];
  6910. result[13] = -matrix[13];
  6911. result[14] = -matrix[14];
  6912. result[15] = -matrix[15];
  6913. return result;
  6914. };
  6915. /**
  6916. * Computes the transpose of the provided matrix.
  6917. *
  6918. * @param {Matrix4} matrix The matrix to transpose.
  6919. * @param {Matrix4} result The object onto which to store the result.
  6920. * @returns {Matrix4} The modified result parameter.
  6921. *
  6922. * @example
  6923. * //returns transpose of a Matrix4
  6924. * // m = [10.0, 11.0, 12.0, 13.0]
  6925. * // [14.0, 15.0, 16.0, 17.0]
  6926. * // [18.0, 19.0, 20.0, 21.0]
  6927. * // [22.0, 23.0, 24.0, 25.0]
  6928. *
  6929. * const a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  6930. *
  6931. * // m remains the same
  6932. * // a = [10.0, 14.0, 18.0, 22.0]
  6933. * // [11.0, 15.0, 19.0, 23.0]
  6934. * // [12.0, 16.0, 20.0, 24.0]
  6935. * // [13.0, 17.0, 21.0, 25.0]
  6936. */
  6937. Matrix4.transpose = function (matrix, result) {
  6938. //>>includeStart('debug', pragmas.debug);
  6939. RuntimeError.Check.typeOf.object("matrix", matrix);
  6940. RuntimeError.Check.typeOf.object("result", result);
  6941. //>>includeEnd('debug');
  6942. const matrix1 = matrix[1];
  6943. const matrix2 = matrix[2];
  6944. const matrix3 = matrix[3];
  6945. const matrix6 = matrix[6];
  6946. const matrix7 = matrix[7];
  6947. const matrix11 = matrix[11];
  6948. result[0] = matrix[0];
  6949. result[1] = matrix[4];
  6950. result[2] = matrix[8];
  6951. result[3] = matrix[12];
  6952. result[4] = matrix1;
  6953. result[5] = matrix[5];
  6954. result[6] = matrix[9];
  6955. result[7] = matrix[13];
  6956. result[8] = matrix2;
  6957. result[9] = matrix6;
  6958. result[10] = matrix[10];
  6959. result[11] = matrix[14];
  6960. result[12] = matrix3;
  6961. result[13] = matrix7;
  6962. result[14] = matrix11;
  6963. result[15] = matrix[15];
  6964. return result;
  6965. };
  6966. /**
  6967. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  6968. *
  6969. * @param {Matrix4} matrix The matrix with signed elements.
  6970. * @param {Matrix4} result The object onto which to store the result.
  6971. * @returns {Matrix4} The modified result parameter.
  6972. */
  6973. Matrix4.abs = function (matrix, result) {
  6974. //>>includeStart('debug', pragmas.debug);
  6975. RuntimeError.Check.typeOf.object("matrix", matrix);
  6976. RuntimeError.Check.typeOf.object("result", result);
  6977. //>>includeEnd('debug');
  6978. result[0] = Math.abs(matrix[0]);
  6979. result[1] = Math.abs(matrix[1]);
  6980. result[2] = Math.abs(matrix[2]);
  6981. result[3] = Math.abs(matrix[3]);
  6982. result[4] = Math.abs(matrix[4]);
  6983. result[5] = Math.abs(matrix[5]);
  6984. result[6] = Math.abs(matrix[6]);
  6985. result[7] = Math.abs(matrix[7]);
  6986. result[8] = Math.abs(matrix[8]);
  6987. result[9] = Math.abs(matrix[9]);
  6988. result[10] = Math.abs(matrix[10]);
  6989. result[11] = Math.abs(matrix[11]);
  6990. result[12] = Math.abs(matrix[12]);
  6991. result[13] = Math.abs(matrix[13]);
  6992. result[14] = Math.abs(matrix[14]);
  6993. result[15] = Math.abs(matrix[15]);
  6994. return result;
  6995. };
  6996. /**
  6997. * Compares the provided matrices componentwise and returns
  6998. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6999. *
  7000. * @param {Matrix4} [left] The first matrix.
  7001. * @param {Matrix4} [right] The second matrix.
  7002. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  7003. *
  7004. * @example
  7005. * //compares two Matrix4 instances
  7006. *
  7007. * // a = [10.0, 14.0, 18.0, 22.0]
  7008. * // [11.0, 15.0, 19.0, 23.0]
  7009. * // [12.0, 16.0, 20.0, 24.0]
  7010. * // [13.0, 17.0, 21.0, 25.0]
  7011. *
  7012. * // b = [10.0, 14.0, 18.0, 22.0]
  7013. * // [11.0, 15.0, 19.0, 23.0]
  7014. * // [12.0, 16.0, 20.0, 24.0]
  7015. * // [13.0, 17.0, 21.0, 25.0]
  7016. *
  7017. * if(Cesium.Matrix4.equals(a,b)) {
  7018. * console.log("Both matrices are equal");
  7019. * } else {
  7020. * console.log("They are not equal");
  7021. * }
  7022. *
  7023. * //Prints "Both matrices are equal" on the console
  7024. */
  7025. Matrix4.equals = function (left, right) {
  7026. // Given that most matrices will be transformation matrices, the elements
  7027. // are tested in order such that the test is likely to fail as early
  7028. // as possible. I _think_ this is just as friendly to the L1 cache
  7029. // as testing in index order. It is certainty faster in practice.
  7030. return (
  7031. left === right ||
  7032. (defaultValue.defined(left) &&
  7033. defaultValue.defined(right) &&
  7034. // Translation
  7035. left[12] === right[12] &&
  7036. left[13] === right[13] &&
  7037. left[14] === right[14] &&
  7038. // Rotation/scale
  7039. left[0] === right[0] &&
  7040. left[1] === right[1] &&
  7041. left[2] === right[2] &&
  7042. left[4] === right[4] &&
  7043. left[5] === right[5] &&
  7044. left[6] === right[6] &&
  7045. left[8] === right[8] &&
  7046. left[9] === right[9] &&
  7047. left[10] === right[10] &&
  7048. // Bottom row
  7049. left[3] === right[3] &&
  7050. left[7] === right[7] &&
  7051. left[11] === right[11] &&
  7052. left[15] === right[15])
  7053. );
  7054. };
  7055. /**
  7056. * Compares the provided matrices componentwise and returns
  7057. * <code>true</code> if they are within the provided epsilon,
  7058. * <code>false</code> otherwise.
  7059. *
  7060. * @param {Matrix4} [left] The first matrix.
  7061. * @param {Matrix4} [right] The second matrix.
  7062. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7063. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  7064. *
  7065. * @example
  7066. * //compares two Matrix4 instances
  7067. *
  7068. * // a = [10.5, 14.5, 18.5, 22.5]
  7069. * // [11.5, 15.5, 19.5, 23.5]
  7070. * // [12.5, 16.5, 20.5, 24.5]
  7071. * // [13.5, 17.5, 21.5, 25.5]
  7072. *
  7073. * // b = [10.0, 14.0, 18.0, 22.0]
  7074. * // [11.0, 15.0, 19.0, 23.0]
  7075. * // [12.0, 16.0, 20.0, 24.0]
  7076. * // [13.0, 17.0, 21.0, 25.0]
  7077. *
  7078. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  7079. * console.log("Difference between both the matrices is less than 0.1");
  7080. * } else {
  7081. * console.log("Difference between both the matrices is not less than 0.1");
  7082. * }
  7083. *
  7084. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  7085. */
  7086. Matrix4.equalsEpsilon = function (left, right, epsilon) {
  7087. epsilon = defaultValue.defaultValue(epsilon, 0);
  7088. return (
  7089. left === right ||
  7090. (defaultValue.defined(left) &&
  7091. defaultValue.defined(right) &&
  7092. Math.abs(left[0] - right[0]) <= epsilon &&
  7093. Math.abs(left[1] - right[1]) <= epsilon &&
  7094. Math.abs(left[2] - right[2]) <= epsilon &&
  7095. Math.abs(left[3] - right[3]) <= epsilon &&
  7096. Math.abs(left[4] - right[4]) <= epsilon &&
  7097. Math.abs(left[5] - right[5]) <= epsilon &&
  7098. Math.abs(left[6] - right[6]) <= epsilon &&
  7099. Math.abs(left[7] - right[7]) <= epsilon &&
  7100. Math.abs(left[8] - right[8]) <= epsilon &&
  7101. Math.abs(left[9] - right[9]) <= epsilon &&
  7102. Math.abs(left[10] - right[10]) <= epsilon &&
  7103. Math.abs(left[11] - right[11]) <= epsilon &&
  7104. Math.abs(left[12] - right[12]) <= epsilon &&
  7105. Math.abs(left[13] - right[13]) <= epsilon &&
  7106. Math.abs(left[14] - right[14]) <= epsilon &&
  7107. Math.abs(left[15] - right[15]) <= epsilon)
  7108. );
  7109. };
  7110. /**
  7111. * Gets the translation portion of the provided matrix, assuming the matrix is an affine transformation matrix.
  7112. *
  7113. * @param {Matrix4} matrix The matrix to use.
  7114. * @param {Cartesian3} result The object onto which to store the result.
  7115. * @returns {Cartesian3} The modified result parameter.
  7116. */
  7117. Matrix4.getTranslation = function (matrix, result) {
  7118. //>>includeStart('debug', pragmas.debug);
  7119. RuntimeError.Check.typeOf.object("matrix", matrix);
  7120. RuntimeError.Check.typeOf.object("result", result);
  7121. //>>includeEnd('debug');
  7122. result.x = matrix[12];
  7123. result.y = matrix[13];
  7124. result.z = matrix[14];
  7125. return result;
  7126. };
  7127. /**
  7128. * Gets the upper left 3x3 matrix of the provided matrix.
  7129. *
  7130. * @param {Matrix4} matrix The matrix to use.
  7131. * @param {Matrix3} result The object onto which to store the result.
  7132. * @returns {Matrix3} The modified result parameter.
  7133. *
  7134. * @example
  7135. * // returns a Matrix3 instance from a Matrix4 instance
  7136. *
  7137. * // m = [10.0, 14.0, 18.0, 22.0]
  7138. * // [11.0, 15.0, 19.0, 23.0]
  7139. * // [12.0, 16.0, 20.0, 24.0]
  7140. * // [13.0, 17.0, 21.0, 25.0]
  7141. *
  7142. * const b = new Cesium.Matrix3();
  7143. * Cesium.Matrix4.getMatrix3(m,b);
  7144. *
  7145. * // b = [10.0, 14.0, 18.0]
  7146. * // [11.0, 15.0, 19.0]
  7147. * // [12.0, 16.0, 20.0]
  7148. */
  7149. Matrix4.getMatrix3 = function (matrix, result) {
  7150. //>>includeStart('debug', pragmas.debug);
  7151. RuntimeError.Check.typeOf.object("matrix", matrix);
  7152. RuntimeError.Check.typeOf.object("result", result);
  7153. //>>includeEnd('debug');
  7154. result[0] = matrix[0];
  7155. result[1] = matrix[1];
  7156. result[2] = matrix[2];
  7157. result[3] = matrix[4];
  7158. result[4] = matrix[5];
  7159. result[5] = matrix[6];
  7160. result[6] = matrix[8];
  7161. result[7] = matrix[9];
  7162. result[8] = matrix[10];
  7163. return result;
  7164. };
  7165. const scratchInverseRotation = new Matrix3();
  7166. const scratchMatrix3Zero = new Matrix3();
  7167. const scratchBottomRow = new Cartesian4();
  7168. const scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  7169. /**
  7170. * Computes the inverse of the provided matrix using Cramers Rule.
  7171. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  7172. * If the matrix is a proper rigid transformation, it is more efficient
  7173. * to invert it with {@link Matrix4.inverseTransformation}.
  7174. *
  7175. * @param {Matrix4} matrix The matrix to invert.
  7176. * @param {Matrix4} result The object onto which to store the result.
  7177. * @returns {Matrix4} The modified result parameter.
  7178. *
  7179. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  7180. */
  7181. Matrix4.inverse = function (matrix, result) {
  7182. //>>includeStart('debug', pragmas.debug);
  7183. RuntimeError.Check.typeOf.object("matrix", matrix);
  7184. RuntimeError.Check.typeOf.object("result", result);
  7185. //>>includeEnd('debug');
  7186. //
  7187. // Ported from:
  7188. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  7189. //
  7190. const src0 = matrix[0];
  7191. const src1 = matrix[4];
  7192. const src2 = matrix[8];
  7193. const src3 = matrix[12];
  7194. const src4 = matrix[1];
  7195. const src5 = matrix[5];
  7196. const src6 = matrix[9];
  7197. const src7 = matrix[13];
  7198. const src8 = matrix[2];
  7199. const src9 = matrix[6];
  7200. const src10 = matrix[10];
  7201. const src11 = matrix[14];
  7202. const src12 = matrix[3];
  7203. const src13 = matrix[7];
  7204. const src14 = matrix[11];
  7205. const src15 = matrix[15];
  7206. // calculate pairs for first 8 elements (cofactors)
  7207. let tmp0 = src10 * src15;
  7208. let tmp1 = src11 * src14;
  7209. let tmp2 = src9 * src15;
  7210. let tmp3 = src11 * src13;
  7211. let tmp4 = src9 * src14;
  7212. let tmp5 = src10 * src13;
  7213. let tmp6 = src8 * src15;
  7214. let tmp7 = src11 * src12;
  7215. let tmp8 = src8 * src14;
  7216. let tmp9 = src10 * src12;
  7217. let tmp10 = src8 * src13;
  7218. let tmp11 = src9 * src12;
  7219. // calculate first 8 elements (cofactors)
  7220. const dst0 =
  7221. tmp0 * src5 +
  7222. tmp3 * src6 +
  7223. tmp4 * src7 -
  7224. (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  7225. const dst1 =
  7226. tmp1 * src4 +
  7227. tmp6 * src6 +
  7228. tmp9 * src7 -
  7229. (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  7230. const dst2 =
  7231. tmp2 * src4 +
  7232. tmp7 * src5 +
  7233. tmp10 * src7 -
  7234. (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  7235. const dst3 =
  7236. tmp5 * src4 +
  7237. tmp8 * src5 +
  7238. tmp11 * src6 -
  7239. (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  7240. const dst4 =
  7241. tmp1 * src1 +
  7242. tmp2 * src2 +
  7243. tmp5 * src3 -
  7244. (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  7245. const dst5 =
  7246. tmp0 * src0 +
  7247. tmp7 * src2 +
  7248. tmp8 * src3 -
  7249. (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  7250. const dst6 =
  7251. tmp3 * src0 +
  7252. tmp6 * src1 +
  7253. tmp11 * src3 -
  7254. (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  7255. const dst7 =
  7256. tmp4 * src0 +
  7257. tmp9 * src1 +
  7258. tmp10 * src2 -
  7259. (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  7260. // calculate pairs for second 8 elements (cofactors)
  7261. tmp0 = src2 * src7;
  7262. tmp1 = src3 * src6;
  7263. tmp2 = src1 * src7;
  7264. tmp3 = src3 * src5;
  7265. tmp4 = src1 * src6;
  7266. tmp5 = src2 * src5;
  7267. tmp6 = src0 * src7;
  7268. tmp7 = src3 * src4;
  7269. tmp8 = src0 * src6;
  7270. tmp9 = src2 * src4;
  7271. tmp10 = src0 * src5;
  7272. tmp11 = src1 * src4;
  7273. // calculate second 8 elements (cofactors)
  7274. const dst8 =
  7275. tmp0 * src13 +
  7276. tmp3 * src14 +
  7277. tmp4 * src15 -
  7278. (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  7279. const dst9 =
  7280. tmp1 * src12 +
  7281. tmp6 * src14 +
  7282. tmp9 * src15 -
  7283. (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  7284. const dst10 =
  7285. tmp2 * src12 +
  7286. tmp7 * src13 +
  7287. tmp10 * src15 -
  7288. (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  7289. const dst11 =
  7290. tmp5 * src12 +
  7291. tmp8 * src13 +
  7292. tmp11 * src14 -
  7293. (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  7294. const dst12 =
  7295. tmp2 * src10 +
  7296. tmp5 * src11 +
  7297. tmp1 * src9 -
  7298. (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  7299. const dst13 =
  7300. tmp8 * src11 +
  7301. tmp0 * src8 +
  7302. tmp7 * src10 -
  7303. (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  7304. const dst14 =
  7305. tmp6 * src9 +
  7306. tmp11 * src11 +
  7307. tmp3 * src8 -
  7308. (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  7309. const dst15 =
  7310. tmp10 * src10 +
  7311. tmp4 * src8 +
  7312. tmp9 * src9 -
  7313. (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  7314. // calculate determinant
  7315. let det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  7316. if (Math.abs(det) < ComponentDatatype.CesiumMath.EPSILON21) {
  7317. // Special case for a zero scale matrix that can occur, for example,
  7318. // when a model's node has a [0, 0, 0] scale.
  7319. if (
  7320. Matrix3.equalsEpsilon(
  7321. Matrix4.getMatrix3(matrix, scratchInverseRotation),
  7322. scratchMatrix3Zero,
  7323. ComponentDatatype.CesiumMath.EPSILON7
  7324. ) &&
  7325. Cartesian4.equals(
  7326. Matrix4.getRow(matrix, 3, scratchBottomRow),
  7327. scratchExpectedBottomRow
  7328. )
  7329. ) {
  7330. result[0] = 0.0;
  7331. result[1] = 0.0;
  7332. result[2] = 0.0;
  7333. result[3] = 0.0;
  7334. result[4] = 0.0;
  7335. result[5] = 0.0;
  7336. result[6] = 0.0;
  7337. result[7] = 0.0;
  7338. result[8] = 0.0;
  7339. result[9] = 0.0;
  7340. result[10] = 0.0;
  7341. result[11] = 0.0;
  7342. result[12] = -matrix[12];
  7343. result[13] = -matrix[13];
  7344. result[14] = -matrix[14];
  7345. result[15] = 1.0;
  7346. return result;
  7347. }
  7348. throw new RuntimeError.RuntimeError(
  7349. "matrix is not invertible because its determinate is zero."
  7350. );
  7351. }
  7352. // calculate matrix inverse
  7353. det = 1.0 / det;
  7354. result[0] = dst0 * det;
  7355. result[1] = dst1 * det;
  7356. result[2] = dst2 * det;
  7357. result[3] = dst3 * det;
  7358. result[4] = dst4 * det;
  7359. result[5] = dst5 * det;
  7360. result[6] = dst6 * det;
  7361. result[7] = dst7 * det;
  7362. result[8] = dst8 * det;
  7363. result[9] = dst9 * det;
  7364. result[10] = dst10 * det;
  7365. result[11] = dst11 * det;
  7366. result[12] = dst12 * det;
  7367. result[13] = dst13 * det;
  7368. result[14] = dst14 * det;
  7369. result[15] = dst15 * det;
  7370. return result;
  7371. };
  7372. /**
  7373. * Computes the inverse of the provided matrix assuming it is a proper rigid matrix,
  7374. * where the upper left 3x3 elements are a rotation matrix,
  7375. * and the upper three elements in the fourth column are the translation.
  7376. * The bottom row is assumed to be [0, 0, 0, 1].
  7377. * The matrix is not verified to be in the proper form.
  7378. * This method is faster than computing the inverse for a general 4x4
  7379. * matrix using {@link Matrix4.inverse}.
  7380. *
  7381. * @param {Matrix4} matrix The matrix to invert.
  7382. * @param {Matrix4} result The object onto which to store the result.
  7383. * @returns {Matrix4} The modified result parameter.
  7384. */
  7385. Matrix4.inverseTransformation = function (matrix, result) {
  7386. //>>includeStart('debug', pragmas.debug);
  7387. RuntimeError.Check.typeOf.object("matrix", matrix);
  7388. RuntimeError.Check.typeOf.object("result", result);
  7389. //>>includeEnd('debug');
  7390. //This function is an optimized version of the below 4 lines.
  7391. //const rT = Matrix3.transpose(Matrix4.getMatrix3(matrix));
  7392. //const rTN = Matrix3.negate(rT);
  7393. //const rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  7394. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  7395. const matrix0 = matrix[0];
  7396. const matrix1 = matrix[1];
  7397. const matrix2 = matrix[2];
  7398. const matrix4 = matrix[4];
  7399. const matrix5 = matrix[5];
  7400. const matrix6 = matrix[6];
  7401. const matrix8 = matrix[8];
  7402. const matrix9 = matrix[9];
  7403. const matrix10 = matrix[10];
  7404. const vX = matrix[12];
  7405. const vY = matrix[13];
  7406. const vZ = matrix[14];
  7407. const x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  7408. const y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  7409. const z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  7410. result[0] = matrix0;
  7411. result[1] = matrix4;
  7412. result[2] = matrix8;
  7413. result[3] = 0.0;
  7414. result[4] = matrix1;
  7415. result[5] = matrix5;
  7416. result[6] = matrix9;
  7417. result[7] = 0.0;
  7418. result[8] = matrix2;
  7419. result[9] = matrix6;
  7420. result[10] = matrix10;
  7421. result[11] = 0.0;
  7422. result[12] = x;
  7423. result[13] = y;
  7424. result[14] = z;
  7425. result[15] = 1.0;
  7426. return result;
  7427. };
  7428. const scratchTransposeMatrix = new Matrix4();
  7429. /**
  7430. * Computes the inverse transpose of a matrix.
  7431. *
  7432. * @param {Matrix4} matrix The matrix to transpose and invert.
  7433. * @param {Matrix4} result The object onto which to store the result.
  7434. * @returns {Matrix4} The modified result parameter.
  7435. */
  7436. Matrix4.inverseTranspose = function (matrix, result) {
  7437. //>>includeStart('debug', pragmas.debug);
  7438. RuntimeError.Check.typeOf.object("matrix", matrix);
  7439. RuntimeError.Check.typeOf.object("result", result);
  7440. //>>includeEnd('debug');
  7441. return Matrix4.inverse(
  7442. Matrix4.transpose(matrix, scratchTransposeMatrix),
  7443. result
  7444. );
  7445. };
  7446. /**
  7447. * An immutable Matrix4 instance initialized to the identity matrix.
  7448. *
  7449. * @type {Matrix4}
  7450. * @constant
  7451. */
  7452. Matrix4.IDENTITY = Object.freeze(
  7453. new Matrix4(
  7454. 1.0,
  7455. 0.0,
  7456. 0.0,
  7457. 0.0,
  7458. 0.0,
  7459. 1.0,
  7460. 0.0,
  7461. 0.0,
  7462. 0.0,
  7463. 0.0,
  7464. 1.0,
  7465. 0.0,
  7466. 0.0,
  7467. 0.0,
  7468. 0.0,
  7469. 1.0
  7470. )
  7471. );
  7472. /**
  7473. * An immutable Matrix4 instance initialized to the zero matrix.
  7474. *
  7475. * @type {Matrix4}
  7476. * @constant
  7477. */
  7478. Matrix4.ZERO = Object.freeze(
  7479. new Matrix4(
  7480. 0.0,
  7481. 0.0,
  7482. 0.0,
  7483. 0.0,
  7484. 0.0,
  7485. 0.0,
  7486. 0.0,
  7487. 0.0,
  7488. 0.0,
  7489. 0.0,
  7490. 0.0,
  7491. 0.0,
  7492. 0.0,
  7493. 0.0,
  7494. 0.0,
  7495. 0.0
  7496. )
  7497. );
  7498. /**
  7499. * The index into Matrix4 for column 0, row 0.
  7500. *
  7501. * @type {Number}
  7502. * @constant
  7503. */
  7504. Matrix4.COLUMN0ROW0 = 0;
  7505. /**
  7506. * The index into Matrix4 for column 0, row 1.
  7507. *
  7508. * @type {Number}
  7509. * @constant
  7510. */
  7511. Matrix4.COLUMN0ROW1 = 1;
  7512. /**
  7513. * The index into Matrix4 for column 0, row 2.
  7514. *
  7515. * @type {Number}
  7516. * @constant
  7517. */
  7518. Matrix4.COLUMN0ROW2 = 2;
  7519. /**
  7520. * The index into Matrix4 for column 0, row 3.
  7521. *
  7522. * @type {Number}
  7523. * @constant
  7524. */
  7525. Matrix4.COLUMN0ROW3 = 3;
  7526. /**
  7527. * The index into Matrix4 for column 1, row 0.
  7528. *
  7529. * @type {Number}
  7530. * @constant
  7531. */
  7532. Matrix4.COLUMN1ROW0 = 4;
  7533. /**
  7534. * The index into Matrix4 for column 1, row 1.
  7535. *
  7536. * @type {Number}
  7537. * @constant
  7538. */
  7539. Matrix4.COLUMN1ROW1 = 5;
  7540. /**
  7541. * The index into Matrix4 for column 1, row 2.
  7542. *
  7543. * @type {Number}
  7544. * @constant
  7545. */
  7546. Matrix4.COLUMN1ROW2 = 6;
  7547. /**
  7548. * The index into Matrix4 for column 1, row 3.
  7549. *
  7550. * @type {Number}
  7551. * @constant
  7552. */
  7553. Matrix4.COLUMN1ROW3 = 7;
  7554. /**
  7555. * The index into Matrix4 for column 2, row 0.
  7556. *
  7557. * @type {Number}
  7558. * @constant
  7559. */
  7560. Matrix4.COLUMN2ROW0 = 8;
  7561. /**
  7562. * The index into Matrix4 for column 2, row 1.
  7563. *
  7564. * @type {Number}
  7565. * @constant
  7566. */
  7567. Matrix4.COLUMN2ROW1 = 9;
  7568. /**
  7569. * The index into Matrix4 for column 2, row 2.
  7570. *
  7571. * @type {Number}
  7572. * @constant
  7573. */
  7574. Matrix4.COLUMN2ROW2 = 10;
  7575. /**
  7576. * The index into Matrix4 for column 2, row 3.
  7577. *
  7578. * @type {Number}
  7579. * @constant
  7580. */
  7581. Matrix4.COLUMN2ROW3 = 11;
  7582. /**
  7583. * The index into Matrix4 for column 3, row 0.
  7584. *
  7585. * @type {Number}
  7586. * @constant
  7587. */
  7588. Matrix4.COLUMN3ROW0 = 12;
  7589. /**
  7590. * The index into Matrix4 for column 3, row 1.
  7591. *
  7592. * @type {Number}
  7593. * @constant
  7594. */
  7595. Matrix4.COLUMN3ROW1 = 13;
  7596. /**
  7597. * The index into Matrix4 for column 3, row 2.
  7598. *
  7599. * @type {Number}
  7600. * @constant
  7601. */
  7602. Matrix4.COLUMN3ROW2 = 14;
  7603. /**
  7604. * The index into Matrix4 for column 3, row 3.
  7605. *
  7606. * @type {Number}
  7607. * @constant
  7608. */
  7609. Matrix4.COLUMN3ROW3 = 15;
  7610. Object.defineProperties(Matrix4.prototype, {
  7611. /**
  7612. * Gets the number of items in the collection.
  7613. * @memberof Matrix4.prototype
  7614. *
  7615. * @type {Number}
  7616. */
  7617. length: {
  7618. get: function () {
  7619. return Matrix4.packedLength;
  7620. },
  7621. },
  7622. });
  7623. /**
  7624. * Duplicates the provided Matrix4 instance.
  7625. *
  7626. * @param {Matrix4} [result] The object onto which to store the result.
  7627. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  7628. */
  7629. Matrix4.prototype.clone = function (result) {
  7630. return Matrix4.clone(this, result);
  7631. };
  7632. /**
  7633. * Compares this matrix to the provided matrix componentwise and returns
  7634. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7635. *
  7636. * @param {Matrix4} [right] The right hand side matrix.
  7637. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  7638. */
  7639. Matrix4.prototype.equals = function (right) {
  7640. return Matrix4.equals(this, right);
  7641. };
  7642. /**
  7643. * @private
  7644. */
  7645. Matrix4.equalsArray = function (matrix, array, offset) {
  7646. return (
  7647. matrix[0] === array[offset] &&
  7648. matrix[1] === array[offset + 1] &&
  7649. matrix[2] === array[offset + 2] &&
  7650. matrix[3] === array[offset + 3] &&
  7651. matrix[4] === array[offset + 4] &&
  7652. matrix[5] === array[offset + 5] &&
  7653. matrix[6] === array[offset + 6] &&
  7654. matrix[7] === array[offset + 7] &&
  7655. matrix[8] === array[offset + 8] &&
  7656. matrix[9] === array[offset + 9] &&
  7657. matrix[10] === array[offset + 10] &&
  7658. matrix[11] === array[offset + 11] &&
  7659. matrix[12] === array[offset + 12] &&
  7660. matrix[13] === array[offset + 13] &&
  7661. matrix[14] === array[offset + 14] &&
  7662. matrix[15] === array[offset + 15]
  7663. );
  7664. };
  7665. /**
  7666. * Compares this matrix to the provided matrix componentwise and returns
  7667. * <code>true</code> if they are within the provided epsilon,
  7668. * <code>false</code> otherwise.
  7669. *
  7670. * @param {Matrix4} [right] The right hand side matrix.
  7671. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7672. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  7673. */
  7674. Matrix4.prototype.equalsEpsilon = function (right, epsilon) {
  7675. return Matrix4.equalsEpsilon(this, right, epsilon);
  7676. };
  7677. /**
  7678. * Computes a string representing this Matrix with each row being
  7679. * on a separate line and in the format '(column0, column1, column2, column3)'.
  7680. *
  7681. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  7682. */
  7683. Matrix4.prototype.toString = function () {
  7684. return (
  7685. `(${this[0]}, ${this[4]}, ${this[8]}, ${this[12]})\n` +
  7686. `(${this[1]}, ${this[5]}, ${this[9]}, ${this[13]})\n` +
  7687. `(${this[2]}, ${this[6]}, ${this[10]}, ${this[14]})\n` +
  7688. `(${this[3]}, ${this[7]}, ${this[11]}, ${this[15]})`
  7689. );
  7690. };
  7691. /**
  7692. * A two dimensional region specified as longitude and latitude coordinates.
  7693. *
  7694. * @alias Rectangle
  7695. * @constructor
  7696. *
  7697. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  7698. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  7699. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  7700. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  7701. *
  7702. * @see Packable
  7703. */
  7704. function Rectangle(west, south, east, north) {
  7705. /**
  7706. * The westernmost longitude in radians in the range [-Pi, Pi].
  7707. *
  7708. * @type {Number}
  7709. * @default 0.0
  7710. */
  7711. this.west = defaultValue.defaultValue(west, 0.0);
  7712. /**
  7713. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  7714. *
  7715. * @type {Number}
  7716. * @default 0.0
  7717. */
  7718. this.south = defaultValue.defaultValue(south, 0.0);
  7719. /**
  7720. * The easternmost longitude in radians in the range [-Pi, Pi].
  7721. *
  7722. * @type {Number}
  7723. * @default 0.0
  7724. */
  7725. this.east = defaultValue.defaultValue(east, 0.0);
  7726. /**
  7727. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  7728. *
  7729. * @type {Number}
  7730. * @default 0.0
  7731. */
  7732. this.north = defaultValue.defaultValue(north, 0.0);
  7733. }
  7734. Object.defineProperties(Rectangle.prototype, {
  7735. /**
  7736. * Gets the width of the rectangle in radians.
  7737. * @memberof Rectangle.prototype
  7738. * @type {Number}
  7739. * @readonly
  7740. */
  7741. width: {
  7742. get: function () {
  7743. return Rectangle.computeWidth(this);
  7744. },
  7745. },
  7746. /**
  7747. * Gets the height of the rectangle in radians.
  7748. * @memberof Rectangle.prototype
  7749. * @type {Number}
  7750. * @readonly
  7751. */
  7752. height: {
  7753. get: function () {
  7754. return Rectangle.computeHeight(this);
  7755. },
  7756. },
  7757. });
  7758. /**
  7759. * The number of elements used to pack the object into an array.
  7760. * @type {Number}
  7761. */
  7762. Rectangle.packedLength = 4;
  7763. /**
  7764. * Stores the provided instance into the provided array.
  7765. *
  7766. * @param {Rectangle} value The value to pack.
  7767. * @param {Number[]} array The array to pack into.
  7768. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  7769. *
  7770. * @returns {Number[]} The array that was packed into
  7771. */
  7772. Rectangle.pack = function (value, array, startingIndex) {
  7773. //>>includeStart('debug', pragmas.debug);
  7774. RuntimeError.Check.typeOf.object("value", value);
  7775. RuntimeError.Check.defined("array", array);
  7776. //>>includeEnd('debug');
  7777. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  7778. array[startingIndex++] = value.west;
  7779. array[startingIndex++] = value.south;
  7780. array[startingIndex++] = value.east;
  7781. array[startingIndex] = value.north;
  7782. return array;
  7783. };
  7784. /**
  7785. * Retrieves an instance from a packed array.
  7786. *
  7787. * @param {Number[]} array The packed array.
  7788. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  7789. * @param {Rectangle} [result] The object into which to store the result.
  7790. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  7791. */
  7792. Rectangle.unpack = function (array, startingIndex, result) {
  7793. //>>includeStart('debug', pragmas.debug);
  7794. RuntimeError.Check.defined("array", array);
  7795. //>>includeEnd('debug');
  7796. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  7797. if (!defaultValue.defined(result)) {
  7798. result = new Rectangle();
  7799. }
  7800. result.west = array[startingIndex++];
  7801. result.south = array[startingIndex++];
  7802. result.east = array[startingIndex++];
  7803. result.north = array[startingIndex];
  7804. return result;
  7805. };
  7806. /**
  7807. * Computes the width of a rectangle in radians.
  7808. * @param {Rectangle} rectangle The rectangle to compute the width of.
  7809. * @returns {Number} The width.
  7810. */
  7811. Rectangle.computeWidth = function (rectangle) {
  7812. //>>includeStart('debug', pragmas.debug);
  7813. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  7814. //>>includeEnd('debug');
  7815. let east = rectangle.east;
  7816. const west = rectangle.west;
  7817. if (east < west) {
  7818. east += ComponentDatatype.CesiumMath.TWO_PI;
  7819. }
  7820. return east - west;
  7821. };
  7822. /**
  7823. * Computes the height of a rectangle in radians.
  7824. * @param {Rectangle} rectangle The rectangle to compute the height of.
  7825. * @returns {Number} The height.
  7826. */
  7827. Rectangle.computeHeight = function (rectangle) {
  7828. //>>includeStart('debug', pragmas.debug);
  7829. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  7830. //>>includeEnd('debug');
  7831. return rectangle.north - rectangle.south;
  7832. };
  7833. /**
  7834. * Creates a rectangle given the boundary longitude and latitude in degrees.
  7835. *
  7836. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  7837. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  7838. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  7839. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  7840. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7841. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7842. *
  7843. * @example
  7844. * const rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  7845. */
  7846. Rectangle.fromDegrees = function (west, south, east, north, result) {
  7847. west = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(west, 0.0));
  7848. south = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(south, 0.0));
  7849. east = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(east, 0.0));
  7850. north = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(north, 0.0));
  7851. if (!defaultValue.defined(result)) {
  7852. return new Rectangle(west, south, east, north);
  7853. }
  7854. result.west = west;
  7855. result.south = south;
  7856. result.east = east;
  7857. result.north = north;
  7858. return result;
  7859. };
  7860. /**
  7861. * Creates a rectangle given the boundary longitude and latitude in radians.
  7862. *
  7863. * @param {Number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
  7864. * @param {Number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  7865. * @param {Number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
  7866. * @param {Number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  7867. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7868. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7869. *
  7870. * @example
  7871. * const rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
  7872. */
  7873. Rectangle.fromRadians = function (west, south, east, north, result) {
  7874. if (!defaultValue.defined(result)) {
  7875. return new Rectangle(west, south, east, north);
  7876. }
  7877. result.west = defaultValue.defaultValue(west, 0.0);
  7878. result.south = defaultValue.defaultValue(south, 0.0);
  7879. result.east = defaultValue.defaultValue(east, 0.0);
  7880. result.north = defaultValue.defaultValue(north, 0.0);
  7881. return result;
  7882. };
  7883. /**
  7884. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  7885. *
  7886. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  7887. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7888. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7889. */
  7890. Rectangle.fromCartographicArray = function (cartographics, result) {
  7891. //>>includeStart('debug', pragmas.debug);
  7892. RuntimeError.Check.defined("cartographics", cartographics);
  7893. //>>includeEnd('debug');
  7894. let west = Number.MAX_VALUE;
  7895. let east = -Number.MAX_VALUE;
  7896. let westOverIDL = Number.MAX_VALUE;
  7897. let eastOverIDL = -Number.MAX_VALUE;
  7898. let south = Number.MAX_VALUE;
  7899. let north = -Number.MAX_VALUE;
  7900. for (let i = 0, len = cartographics.length; i < len; i++) {
  7901. const position = cartographics[i];
  7902. west = Math.min(west, position.longitude);
  7903. east = Math.max(east, position.longitude);
  7904. south = Math.min(south, position.latitude);
  7905. north = Math.max(north, position.latitude);
  7906. const lonAdjusted =
  7907. position.longitude >= 0
  7908. ? position.longitude
  7909. : position.longitude + ComponentDatatype.CesiumMath.TWO_PI;
  7910. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  7911. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  7912. }
  7913. if (east - west > eastOverIDL - westOverIDL) {
  7914. west = westOverIDL;
  7915. east = eastOverIDL;
  7916. if (east > ComponentDatatype.CesiumMath.PI) {
  7917. east = east - ComponentDatatype.CesiumMath.TWO_PI;
  7918. }
  7919. if (west > ComponentDatatype.CesiumMath.PI) {
  7920. west = west - ComponentDatatype.CesiumMath.TWO_PI;
  7921. }
  7922. }
  7923. if (!defaultValue.defined(result)) {
  7924. return new Rectangle(west, south, east, north);
  7925. }
  7926. result.west = west;
  7927. result.south = south;
  7928. result.east = east;
  7929. result.north = north;
  7930. return result;
  7931. };
  7932. /**
  7933. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  7934. *
  7935. * @param {Cartesian3[]} cartesians The list of Cartesian instances.
  7936. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  7937. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7938. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7939. */
  7940. Rectangle.fromCartesianArray = function (cartesians, ellipsoid, result) {
  7941. //>>includeStart('debug', pragmas.debug);
  7942. RuntimeError.Check.defined("cartesians", cartesians);
  7943. //>>includeEnd('debug');
  7944. ellipsoid = defaultValue.defaultValue(ellipsoid, Ellipsoid.WGS84);
  7945. let west = Number.MAX_VALUE;
  7946. let east = -Number.MAX_VALUE;
  7947. let westOverIDL = Number.MAX_VALUE;
  7948. let eastOverIDL = -Number.MAX_VALUE;
  7949. let south = Number.MAX_VALUE;
  7950. let north = -Number.MAX_VALUE;
  7951. for (let i = 0, len = cartesians.length; i < len; i++) {
  7952. const position = ellipsoid.cartesianToCartographic(cartesians[i]);
  7953. west = Math.min(west, position.longitude);
  7954. east = Math.max(east, position.longitude);
  7955. south = Math.min(south, position.latitude);
  7956. north = Math.max(north, position.latitude);
  7957. const lonAdjusted =
  7958. position.longitude >= 0
  7959. ? position.longitude
  7960. : position.longitude + ComponentDatatype.CesiumMath.TWO_PI;
  7961. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  7962. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  7963. }
  7964. if (east - west > eastOverIDL - westOverIDL) {
  7965. west = westOverIDL;
  7966. east = eastOverIDL;
  7967. if (east > ComponentDatatype.CesiumMath.PI) {
  7968. east = east - ComponentDatatype.CesiumMath.TWO_PI;
  7969. }
  7970. if (west > ComponentDatatype.CesiumMath.PI) {
  7971. west = west - ComponentDatatype.CesiumMath.TWO_PI;
  7972. }
  7973. }
  7974. if (!defaultValue.defined(result)) {
  7975. return new Rectangle(west, south, east, north);
  7976. }
  7977. result.west = west;
  7978. result.south = south;
  7979. result.east = east;
  7980. result.north = north;
  7981. return result;
  7982. };
  7983. /**
  7984. * Duplicates a Rectangle.
  7985. *
  7986. * @param {Rectangle} rectangle The rectangle to clone.
  7987. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7988. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  7989. */
  7990. Rectangle.clone = function (rectangle, result) {
  7991. if (!defaultValue.defined(rectangle)) {
  7992. return undefined;
  7993. }
  7994. if (!defaultValue.defined(result)) {
  7995. return new Rectangle(
  7996. rectangle.west,
  7997. rectangle.south,
  7998. rectangle.east,
  7999. rectangle.north
  8000. );
  8001. }
  8002. result.west = rectangle.west;
  8003. result.south = rectangle.south;
  8004. result.east = rectangle.east;
  8005. result.north = rectangle.north;
  8006. return result;
  8007. };
  8008. /**
  8009. * Compares the provided Rectangles componentwise and returns
  8010. * <code>true</code> if they pass an absolute or relative tolerance test,
  8011. * <code>false</code> otherwise.
  8012. *
  8013. * @param {Rectangle} [left] The first Rectangle.
  8014. * @param {Rectangle} [right] The second Rectangle.
  8015. * @param {Number} [absoluteEpsilon=0] The absolute epsilon tolerance to use for equality testing.
  8016. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  8017. */
  8018. Rectangle.equalsEpsilon = function (left, right, absoluteEpsilon) {
  8019. absoluteEpsilon = defaultValue.defaultValue(absoluteEpsilon, 0);
  8020. return (
  8021. left === right ||
  8022. (defaultValue.defined(left) &&
  8023. defaultValue.defined(right) &&
  8024. Math.abs(left.west - right.west) <= absoluteEpsilon &&
  8025. Math.abs(left.south - right.south) <= absoluteEpsilon &&
  8026. Math.abs(left.east - right.east) <= absoluteEpsilon &&
  8027. Math.abs(left.north - right.north) <= absoluteEpsilon)
  8028. );
  8029. };
  8030. /**
  8031. * Duplicates this Rectangle.
  8032. *
  8033. * @param {Rectangle} [result] The object onto which to store the result.
  8034. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8035. */
  8036. Rectangle.prototype.clone = function (result) {
  8037. return Rectangle.clone(this, result);
  8038. };
  8039. /**
  8040. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8041. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8042. *
  8043. * @param {Rectangle} [other] The Rectangle to compare.
  8044. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  8045. */
  8046. Rectangle.prototype.equals = function (other) {
  8047. return Rectangle.equals(this, other);
  8048. };
  8049. /**
  8050. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  8051. * <code>false</code> otherwise.
  8052. *
  8053. * @param {Rectangle} [left] The first Rectangle.
  8054. * @param {Rectangle} [right] The second Rectangle.
  8055. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  8056. */
  8057. Rectangle.equals = function (left, right) {
  8058. return (
  8059. left === right ||
  8060. (defaultValue.defined(left) &&
  8061. defaultValue.defined(right) &&
  8062. left.west === right.west &&
  8063. left.south === right.south &&
  8064. left.east === right.east &&
  8065. left.north === right.north)
  8066. );
  8067. };
  8068. /**
  8069. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8070. * <code>true</code> if they are within the provided epsilon,
  8071. * <code>false</code> otherwise.
  8072. *
  8073. * @param {Rectangle} [other] The Rectangle to compare.
  8074. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  8075. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  8076. */
  8077. Rectangle.prototype.equalsEpsilon = function (other, epsilon) {
  8078. return Rectangle.equalsEpsilon(this, other, epsilon);
  8079. };
  8080. /**
  8081. * Checks a Rectangle's properties and throws if they are not in valid ranges.
  8082. *
  8083. * @param {Rectangle} rectangle The rectangle to validate
  8084. *
  8085. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8086. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8087. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8088. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8089. */
  8090. Rectangle.validate = function (rectangle) {
  8091. //>>includeStart('debug', pragmas.debug);
  8092. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8093. const north = rectangle.north;
  8094. RuntimeError.Check.typeOf.number.greaterThanOrEquals(
  8095. "north",
  8096. north,
  8097. -ComponentDatatype.CesiumMath.PI_OVER_TWO
  8098. );
  8099. RuntimeError.Check.typeOf.number.lessThanOrEquals("north", north, ComponentDatatype.CesiumMath.PI_OVER_TWO);
  8100. const south = rectangle.south;
  8101. RuntimeError.Check.typeOf.number.greaterThanOrEquals(
  8102. "south",
  8103. south,
  8104. -ComponentDatatype.CesiumMath.PI_OVER_TWO
  8105. );
  8106. RuntimeError.Check.typeOf.number.lessThanOrEquals("south", south, ComponentDatatype.CesiumMath.PI_OVER_TWO);
  8107. const west = rectangle.west;
  8108. RuntimeError.Check.typeOf.number.greaterThanOrEquals("west", west, -Math.PI);
  8109. RuntimeError.Check.typeOf.number.lessThanOrEquals("west", west, Math.PI);
  8110. const east = rectangle.east;
  8111. RuntimeError.Check.typeOf.number.greaterThanOrEquals("east", east, -Math.PI);
  8112. RuntimeError.Check.typeOf.number.lessThanOrEquals("east", east, Math.PI);
  8113. //>>includeEnd('debug');
  8114. };
  8115. /**
  8116. * Computes the southwest corner of a rectangle.
  8117. *
  8118. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8119. * @param {Cartographic} [result] The object onto which to store the result.
  8120. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8121. */
  8122. Rectangle.southwest = function (rectangle, result) {
  8123. //>>includeStart('debug', pragmas.debug);
  8124. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8125. //>>includeEnd('debug');
  8126. if (!defaultValue.defined(result)) {
  8127. return new Cartographic(rectangle.west, rectangle.south);
  8128. }
  8129. result.longitude = rectangle.west;
  8130. result.latitude = rectangle.south;
  8131. result.height = 0.0;
  8132. return result;
  8133. };
  8134. /**
  8135. * Computes the northwest corner of a rectangle.
  8136. *
  8137. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8138. * @param {Cartographic} [result] The object onto which to store the result.
  8139. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8140. */
  8141. Rectangle.northwest = function (rectangle, result) {
  8142. //>>includeStart('debug', pragmas.debug);
  8143. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8144. //>>includeEnd('debug');
  8145. if (!defaultValue.defined(result)) {
  8146. return new Cartographic(rectangle.west, rectangle.north);
  8147. }
  8148. result.longitude = rectangle.west;
  8149. result.latitude = rectangle.north;
  8150. result.height = 0.0;
  8151. return result;
  8152. };
  8153. /**
  8154. * Computes the northeast corner of a rectangle.
  8155. *
  8156. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8157. * @param {Cartographic} [result] The object onto which to store the result.
  8158. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8159. */
  8160. Rectangle.northeast = function (rectangle, result) {
  8161. //>>includeStart('debug', pragmas.debug);
  8162. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8163. //>>includeEnd('debug');
  8164. if (!defaultValue.defined(result)) {
  8165. return new Cartographic(rectangle.east, rectangle.north);
  8166. }
  8167. result.longitude = rectangle.east;
  8168. result.latitude = rectangle.north;
  8169. result.height = 0.0;
  8170. return result;
  8171. };
  8172. /**
  8173. * Computes the southeast corner of a rectangle.
  8174. *
  8175. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8176. * @param {Cartographic} [result] The object onto which to store the result.
  8177. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8178. */
  8179. Rectangle.southeast = function (rectangle, result) {
  8180. //>>includeStart('debug', pragmas.debug);
  8181. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8182. //>>includeEnd('debug');
  8183. if (!defaultValue.defined(result)) {
  8184. return new Cartographic(rectangle.east, rectangle.south);
  8185. }
  8186. result.longitude = rectangle.east;
  8187. result.latitude = rectangle.south;
  8188. result.height = 0.0;
  8189. return result;
  8190. };
  8191. /**
  8192. * Computes the center of a rectangle.
  8193. *
  8194. * @param {Rectangle} rectangle The rectangle for which to find the center
  8195. * @param {Cartographic} [result] The object onto which to store the result.
  8196. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8197. */
  8198. Rectangle.center = function (rectangle, result) {
  8199. //>>includeStart('debug', pragmas.debug);
  8200. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8201. //>>includeEnd('debug');
  8202. let east = rectangle.east;
  8203. const west = rectangle.west;
  8204. if (east < west) {
  8205. east += ComponentDatatype.CesiumMath.TWO_PI;
  8206. }
  8207. const longitude = ComponentDatatype.CesiumMath.negativePiToPi((west + east) * 0.5);
  8208. const latitude = (rectangle.south + rectangle.north) * 0.5;
  8209. if (!defaultValue.defined(result)) {
  8210. return new Cartographic(longitude, latitude);
  8211. }
  8212. result.longitude = longitude;
  8213. result.latitude = latitude;
  8214. result.height = 0.0;
  8215. return result;
  8216. };
  8217. /**
  8218. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  8219. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  8220. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  8221. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  8222. * coordinates, see {@link Rectangle.simpleIntersection}.
  8223. *
  8224. * @param {Rectangle} rectangle On rectangle to find an intersection
  8225. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8226. * @param {Rectangle} [result] The object onto which to store the result.
  8227. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8228. */
  8229. Rectangle.intersection = function (rectangle, otherRectangle, result) {
  8230. //>>includeStart('debug', pragmas.debug);
  8231. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8232. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8233. //>>includeEnd('debug');
  8234. let rectangleEast = rectangle.east;
  8235. let rectangleWest = rectangle.west;
  8236. let otherRectangleEast = otherRectangle.east;
  8237. let otherRectangleWest = otherRectangle.west;
  8238. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8239. rectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8240. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8241. otherRectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8242. }
  8243. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8244. otherRectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8245. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8246. rectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8247. }
  8248. const west = ComponentDatatype.CesiumMath.negativePiToPi(
  8249. Math.max(rectangleWest, otherRectangleWest)
  8250. );
  8251. const east = ComponentDatatype.CesiumMath.negativePiToPi(
  8252. Math.min(rectangleEast, otherRectangleEast)
  8253. );
  8254. if (
  8255. (rectangle.west < rectangle.east ||
  8256. otherRectangle.west < otherRectangle.east) &&
  8257. east <= west
  8258. ) {
  8259. return undefined;
  8260. }
  8261. const south = Math.max(rectangle.south, otherRectangle.south);
  8262. const north = Math.min(rectangle.north, otherRectangle.north);
  8263. if (south >= north) {
  8264. return undefined;
  8265. }
  8266. if (!defaultValue.defined(result)) {
  8267. return new Rectangle(west, south, east, north);
  8268. }
  8269. result.west = west;
  8270. result.south = south;
  8271. result.east = east;
  8272. result.north = north;
  8273. return result;
  8274. };
  8275. /**
  8276. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  8277. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  8278. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  8279. * and longitude (i.e. projected coordinates).
  8280. *
  8281. * @param {Rectangle} rectangle On rectangle to find an intersection
  8282. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8283. * @param {Rectangle} [result] The object onto which to store the result.
  8284. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8285. */
  8286. Rectangle.simpleIntersection = function (rectangle, otherRectangle, result) {
  8287. //>>includeStart('debug', pragmas.debug);
  8288. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8289. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8290. //>>includeEnd('debug');
  8291. const west = Math.max(rectangle.west, otherRectangle.west);
  8292. const south = Math.max(rectangle.south, otherRectangle.south);
  8293. const east = Math.min(rectangle.east, otherRectangle.east);
  8294. const north = Math.min(rectangle.north, otherRectangle.north);
  8295. if (south >= north || west >= east) {
  8296. return undefined;
  8297. }
  8298. if (!defaultValue.defined(result)) {
  8299. return new Rectangle(west, south, east, north);
  8300. }
  8301. result.west = west;
  8302. result.south = south;
  8303. result.east = east;
  8304. result.north = north;
  8305. return result;
  8306. };
  8307. /**
  8308. * Computes a rectangle that is the union of two rectangles.
  8309. *
  8310. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  8311. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  8312. * @param {Rectangle} [result] The object onto which to store the result.
  8313. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8314. */
  8315. Rectangle.union = function (rectangle, otherRectangle, result) {
  8316. //>>includeStart('debug', pragmas.debug);
  8317. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8318. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8319. //>>includeEnd('debug');
  8320. if (!defaultValue.defined(result)) {
  8321. result = new Rectangle();
  8322. }
  8323. let rectangleEast = rectangle.east;
  8324. let rectangleWest = rectangle.west;
  8325. let otherRectangleEast = otherRectangle.east;
  8326. let otherRectangleWest = otherRectangle.west;
  8327. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8328. rectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8329. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8330. otherRectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8331. }
  8332. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8333. otherRectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8334. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8335. rectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8336. }
  8337. const west = ComponentDatatype.CesiumMath.negativePiToPi(
  8338. Math.min(rectangleWest, otherRectangleWest)
  8339. );
  8340. const east = ComponentDatatype.CesiumMath.negativePiToPi(
  8341. Math.max(rectangleEast, otherRectangleEast)
  8342. );
  8343. result.west = west;
  8344. result.south = Math.min(rectangle.south, otherRectangle.south);
  8345. result.east = east;
  8346. result.north = Math.max(rectangle.north, otherRectangle.north);
  8347. return result;
  8348. };
  8349. /**
  8350. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  8351. *
  8352. * @param {Rectangle} rectangle A rectangle to expand.
  8353. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  8354. * @param {Rectangle} [result] The object onto which to store the result.
  8355. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  8356. */
  8357. Rectangle.expand = function (rectangle, cartographic, result) {
  8358. //>>includeStart('debug', pragmas.debug);
  8359. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8360. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  8361. //>>includeEnd('debug');
  8362. if (!defaultValue.defined(result)) {
  8363. result = new Rectangle();
  8364. }
  8365. result.west = Math.min(rectangle.west, cartographic.longitude);
  8366. result.south = Math.min(rectangle.south, cartographic.latitude);
  8367. result.east = Math.max(rectangle.east, cartographic.longitude);
  8368. result.north = Math.max(rectangle.north, cartographic.latitude);
  8369. return result;
  8370. };
  8371. /**
  8372. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  8373. *
  8374. * @param {Rectangle} rectangle The rectangle
  8375. * @param {Cartographic} cartographic The cartographic to test.
  8376. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  8377. */
  8378. Rectangle.contains = function (rectangle, cartographic) {
  8379. //>>includeStart('debug', pragmas.debug);
  8380. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8381. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  8382. //>>includeEnd('debug');
  8383. let longitude = cartographic.longitude;
  8384. const latitude = cartographic.latitude;
  8385. const west = rectangle.west;
  8386. let east = rectangle.east;
  8387. if (east < west) {
  8388. east += ComponentDatatype.CesiumMath.TWO_PI;
  8389. if (longitude < 0.0) {
  8390. longitude += ComponentDatatype.CesiumMath.TWO_PI;
  8391. }
  8392. }
  8393. return (
  8394. (longitude > west ||
  8395. ComponentDatatype.CesiumMath.equalsEpsilon(longitude, west, ComponentDatatype.CesiumMath.EPSILON14)) &&
  8396. (longitude < east ||
  8397. ComponentDatatype.CesiumMath.equalsEpsilon(longitude, east, ComponentDatatype.CesiumMath.EPSILON14)) &&
  8398. latitude >= rectangle.south &&
  8399. latitude <= rectangle.north
  8400. );
  8401. };
  8402. const subsampleLlaScratch = new Cartographic();
  8403. /**
  8404. * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
  8405. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  8406. * for rectangles that cover the poles or cross the equator.
  8407. *
  8408. * @param {Rectangle} rectangle The rectangle to subsample.
  8409. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  8410. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  8411. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  8412. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  8413. */
  8414. Rectangle.subsample = function (rectangle, ellipsoid, surfaceHeight, result) {
  8415. //>>includeStart('debug', pragmas.debug);
  8416. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8417. //>>includeEnd('debug');
  8418. ellipsoid = defaultValue.defaultValue(ellipsoid, Ellipsoid.WGS84);
  8419. surfaceHeight = defaultValue.defaultValue(surfaceHeight, 0.0);
  8420. if (!defaultValue.defined(result)) {
  8421. result = [];
  8422. }
  8423. let length = 0;
  8424. const north = rectangle.north;
  8425. const south = rectangle.south;
  8426. const east = rectangle.east;
  8427. const west = rectangle.west;
  8428. const lla = subsampleLlaScratch;
  8429. lla.height = surfaceHeight;
  8430. lla.longitude = west;
  8431. lla.latitude = north;
  8432. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8433. length++;
  8434. lla.longitude = east;
  8435. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8436. length++;
  8437. lla.latitude = south;
  8438. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8439. length++;
  8440. lla.longitude = west;
  8441. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8442. length++;
  8443. if (north < 0.0) {
  8444. lla.latitude = north;
  8445. } else if (south > 0.0) {
  8446. lla.latitude = south;
  8447. } else {
  8448. lla.latitude = 0.0;
  8449. }
  8450. for (let i = 1; i < 8; ++i) {
  8451. lla.longitude = -Math.PI + i * ComponentDatatype.CesiumMath.PI_OVER_TWO;
  8452. if (Rectangle.contains(rectangle, lla)) {
  8453. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8454. length++;
  8455. }
  8456. }
  8457. if (lla.latitude === 0.0) {
  8458. lla.longitude = west;
  8459. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8460. length++;
  8461. lla.longitude = east;
  8462. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8463. length++;
  8464. }
  8465. result.length = length;
  8466. return result;
  8467. };
  8468. /**
  8469. * Computes a subsection of a rectangle from normalized coordinates in the range [0.0, 1.0].
  8470. *
  8471. * @param {Rectangle} rectangle The rectangle to subsection.
  8472. * @param {Number} westLerp The west interpolation factor in the range [0.0, 1.0]. Must be less than or equal to eastLerp.
  8473. * @param {Number} southLerp The south interpolation factor in the range [0.0, 1.0]. Must be less than or equal to northLerp.
  8474. * @param {Number} eastLerp The east interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to westLerp.
  8475. * @param {Number} northLerp The north interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to southLerp.
  8476. * @param {Rectangle} [result] The object onto which to store the result.
  8477. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8478. */
  8479. Rectangle.subsection = function (
  8480. rectangle,
  8481. westLerp,
  8482. southLerp,
  8483. eastLerp,
  8484. northLerp,
  8485. result
  8486. ) {
  8487. //>>includeStart('debug', pragmas.debug);
  8488. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8489. RuntimeError.Check.typeOf.number.greaterThanOrEquals("westLerp", westLerp, 0.0);
  8490. RuntimeError.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, 1.0);
  8491. RuntimeError.Check.typeOf.number.greaterThanOrEquals("southLerp", southLerp, 0.0);
  8492. RuntimeError.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, 1.0);
  8493. RuntimeError.Check.typeOf.number.greaterThanOrEquals("eastLerp", eastLerp, 0.0);
  8494. RuntimeError.Check.typeOf.number.lessThanOrEquals("eastLerp", eastLerp, 1.0);
  8495. RuntimeError.Check.typeOf.number.greaterThanOrEquals("northLerp", northLerp, 0.0);
  8496. RuntimeError.Check.typeOf.number.lessThanOrEquals("northLerp", northLerp, 1.0);
  8497. RuntimeError.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, eastLerp);
  8498. RuntimeError.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, northLerp);
  8499. //>>includeEnd('debug');
  8500. if (!defaultValue.defined(result)) {
  8501. result = new Rectangle();
  8502. }
  8503. // This function doesn't use CesiumMath.lerp because it has floating point precision problems
  8504. // when the start and end values are the same but the t changes.
  8505. if (rectangle.west <= rectangle.east) {
  8506. const width = rectangle.east - rectangle.west;
  8507. result.west = rectangle.west + westLerp * width;
  8508. result.east = rectangle.west + eastLerp * width;
  8509. } else {
  8510. const width = ComponentDatatype.CesiumMath.TWO_PI + rectangle.east - rectangle.west;
  8511. result.west = ComponentDatatype.CesiumMath.negativePiToPi(rectangle.west + westLerp * width);
  8512. result.east = ComponentDatatype.CesiumMath.negativePiToPi(rectangle.west + eastLerp * width);
  8513. }
  8514. const height = rectangle.north - rectangle.south;
  8515. result.south = rectangle.south + southLerp * height;
  8516. result.north = rectangle.south + northLerp * height;
  8517. // Fix floating point precision problems when t = 1
  8518. if (westLerp === 1.0) {
  8519. result.west = rectangle.east;
  8520. }
  8521. if (eastLerp === 1.0) {
  8522. result.east = rectangle.east;
  8523. }
  8524. if (southLerp === 1.0) {
  8525. result.south = rectangle.north;
  8526. }
  8527. if (northLerp === 1.0) {
  8528. result.north = rectangle.north;
  8529. }
  8530. return result;
  8531. };
  8532. /**
  8533. * The largest possible rectangle.
  8534. *
  8535. * @type {Rectangle}
  8536. * @constant
  8537. */
  8538. Rectangle.MAX_VALUE = Object.freeze(
  8539. new Rectangle(
  8540. -Math.PI,
  8541. -ComponentDatatype.CesiumMath.PI_OVER_TWO,
  8542. Math.PI,
  8543. ComponentDatatype.CesiumMath.PI_OVER_TWO
  8544. )
  8545. );
  8546. /**
  8547. * A 2D Cartesian point.
  8548. * @alias Cartesian2
  8549. * @constructor
  8550. *
  8551. * @param {Number} [x=0.0] The X component.
  8552. * @param {Number} [y=0.0] The Y component.
  8553. *
  8554. * @see Cartesian3
  8555. * @see Cartesian4
  8556. * @see Packable
  8557. */
  8558. function Cartesian2(x, y) {
  8559. /**
  8560. * The X component.
  8561. * @type {Number}
  8562. * @default 0.0
  8563. */
  8564. this.x = defaultValue.defaultValue(x, 0.0);
  8565. /**
  8566. * The Y component.
  8567. * @type {Number}
  8568. * @default 0.0
  8569. */
  8570. this.y = defaultValue.defaultValue(y, 0.0);
  8571. }
  8572. /**
  8573. * Creates a Cartesian2 instance from x and y coordinates.
  8574. *
  8575. * @param {Number} x The x coordinate.
  8576. * @param {Number} y The y coordinate.
  8577. * @param {Cartesian2} [result] The object onto which to store the result.
  8578. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8579. */
  8580. Cartesian2.fromElements = function (x, y, result) {
  8581. if (!defaultValue.defined(result)) {
  8582. return new Cartesian2(x, y);
  8583. }
  8584. result.x = x;
  8585. result.y = y;
  8586. return result;
  8587. };
  8588. /**
  8589. * Duplicates a Cartesian2 instance.
  8590. *
  8591. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  8592. * @param {Cartesian2} [result] The object onto which to store the result.
  8593. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  8594. */
  8595. Cartesian2.clone = function (cartesian, result) {
  8596. if (!defaultValue.defined(cartesian)) {
  8597. return undefined;
  8598. }
  8599. if (!defaultValue.defined(result)) {
  8600. return new Cartesian2(cartesian.x, cartesian.y);
  8601. }
  8602. result.x = cartesian.x;
  8603. result.y = cartesian.y;
  8604. return result;
  8605. };
  8606. /**
  8607. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  8608. * x and y properties of the Cartesian3 and drops z.
  8609. * @function
  8610. *
  8611. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  8612. * @param {Cartesian2} [result] The object onto which to store the result.
  8613. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8614. */
  8615. Cartesian2.fromCartesian3 = Cartesian2.clone;
  8616. /**
  8617. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  8618. * x and y properties of the Cartesian4 and drops z and w.
  8619. * @function
  8620. *
  8621. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  8622. * @param {Cartesian2} [result] The object onto which to store the result.
  8623. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8624. */
  8625. Cartesian2.fromCartesian4 = Cartesian2.clone;
  8626. /**
  8627. * The number of elements used to pack the object into an array.
  8628. * @type {Number}
  8629. */
  8630. Cartesian2.packedLength = 2;
  8631. /**
  8632. * Stores the provided instance into the provided array.
  8633. *
  8634. * @param {Cartesian2} value The value to pack.
  8635. * @param {Number[]} array The array to pack into.
  8636. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  8637. *
  8638. * @returns {Number[]} The array that was packed into
  8639. */
  8640. Cartesian2.pack = function (value, array, startingIndex) {
  8641. //>>includeStart('debug', pragmas.debug);
  8642. RuntimeError.Check.typeOf.object("value", value);
  8643. RuntimeError.Check.defined("array", array);
  8644. //>>includeEnd('debug');
  8645. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  8646. array[startingIndex++] = value.x;
  8647. array[startingIndex] = value.y;
  8648. return array;
  8649. };
  8650. /**
  8651. * Retrieves an instance from a packed array.
  8652. *
  8653. * @param {Number[]} array The packed array.
  8654. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  8655. * @param {Cartesian2} [result] The object into which to store the result.
  8656. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8657. */
  8658. Cartesian2.unpack = function (array, startingIndex, result) {
  8659. //>>includeStart('debug', pragmas.debug);
  8660. RuntimeError.Check.defined("array", array);
  8661. //>>includeEnd('debug');
  8662. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  8663. if (!defaultValue.defined(result)) {
  8664. result = new Cartesian2();
  8665. }
  8666. result.x = array[startingIndex++];
  8667. result.y = array[startingIndex];
  8668. return result;
  8669. };
  8670. /**
  8671. * Flattens an array of Cartesian2s into an array of components.
  8672. *
  8673. * @param {Cartesian2[]} array The array of cartesians to pack.
  8674. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 2 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 2) elements.
  8675. * @returns {Number[]} The packed array.
  8676. */
  8677. Cartesian2.packArray = function (array, result) {
  8678. //>>includeStart('debug', pragmas.debug);
  8679. RuntimeError.Check.defined("array", array);
  8680. //>>includeEnd('debug');
  8681. const length = array.length;
  8682. const resultLength = length * 2;
  8683. if (!defaultValue.defined(result)) {
  8684. result = new Array(resultLength);
  8685. } else if (!Array.isArray(result) && result.length !== resultLength) {
  8686. //>>includeStart('debug', pragmas.debug);
  8687. throw new RuntimeError.DeveloperError(
  8688. "If result is a typed array, it must have exactly array.length * 2 elements"
  8689. );
  8690. //>>includeEnd('debug');
  8691. } else if (result.length !== resultLength) {
  8692. result.length = resultLength;
  8693. }
  8694. for (let i = 0; i < length; ++i) {
  8695. Cartesian2.pack(array[i], result, i * 2);
  8696. }
  8697. return result;
  8698. };
  8699. /**
  8700. * Unpacks an array of cartesian components into an array of Cartesian2s.
  8701. *
  8702. * @param {Number[]} array The array of components to unpack.
  8703. * @param {Cartesian2[]} [result] The array onto which to store the result.
  8704. * @returns {Cartesian2[]} The unpacked array.
  8705. */
  8706. Cartesian2.unpackArray = function (array, result) {
  8707. //>>includeStart('debug', pragmas.debug);
  8708. RuntimeError.Check.defined("array", array);
  8709. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 2);
  8710. if (array.length % 2 !== 0) {
  8711. throw new RuntimeError.DeveloperError("array length must be a multiple of 2.");
  8712. }
  8713. //>>includeEnd('debug');
  8714. const length = array.length;
  8715. if (!defaultValue.defined(result)) {
  8716. result = new Array(length / 2);
  8717. } else {
  8718. result.length = length / 2;
  8719. }
  8720. for (let i = 0; i < length; i += 2) {
  8721. const index = i / 2;
  8722. result[index] = Cartesian2.unpack(array, i, result[index]);
  8723. }
  8724. return result;
  8725. };
  8726. /**
  8727. * Creates a Cartesian2 from two consecutive elements in an array.
  8728. * @function
  8729. *
  8730. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  8731. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  8732. * @param {Cartesian2} [result] The object onto which to store the result.
  8733. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8734. *
  8735. * @example
  8736. * // Create a Cartesian2 with (1.0, 2.0)
  8737. * const v = [1.0, 2.0];
  8738. * const p = Cesium.Cartesian2.fromArray(v);
  8739. *
  8740. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  8741. * const v2 = [0.0, 0.0, 1.0, 2.0];
  8742. * const p2 = Cesium.Cartesian2.fromArray(v2, 2);
  8743. */
  8744. Cartesian2.fromArray = Cartesian2.unpack;
  8745. /**
  8746. * Computes the value of the maximum component for the supplied Cartesian.
  8747. *
  8748. * @param {Cartesian2} cartesian The cartesian to use.
  8749. * @returns {Number} The value of the maximum component.
  8750. */
  8751. Cartesian2.maximumComponent = function (cartesian) {
  8752. //>>includeStart('debug', pragmas.debug);
  8753. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8754. //>>includeEnd('debug');
  8755. return Math.max(cartesian.x, cartesian.y);
  8756. };
  8757. /**
  8758. * Computes the value of the minimum component for the supplied Cartesian.
  8759. *
  8760. * @param {Cartesian2} cartesian The cartesian to use.
  8761. * @returns {Number} The value of the minimum component.
  8762. */
  8763. Cartesian2.minimumComponent = function (cartesian) {
  8764. //>>includeStart('debug', pragmas.debug);
  8765. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8766. //>>includeEnd('debug');
  8767. return Math.min(cartesian.x, cartesian.y);
  8768. };
  8769. /**
  8770. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  8771. *
  8772. * @param {Cartesian2} first A cartesian to compare.
  8773. * @param {Cartesian2} second A cartesian to compare.
  8774. * @param {Cartesian2} result The object into which to store the result.
  8775. * @returns {Cartesian2} A cartesian with the minimum components.
  8776. */
  8777. Cartesian2.minimumByComponent = function (first, second, result) {
  8778. //>>includeStart('debug', pragmas.debug);
  8779. RuntimeError.Check.typeOf.object("first", first);
  8780. RuntimeError.Check.typeOf.object("second", second);
  8781. RuntimeError.Check.typeOf.object("result", result);
  8782. //>>includeEnd('debug');
  8783. result.x = Math.min(first.x, second.x);
  8784. result.y = Math.min(first.y, second.y);
  8785. return result;
  8786. };
  8787. /**
  8788. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  8789. *
  8790. * @param {Cartesian2} first A cartesian to compare.
  8791. * @param {Cartesian2} second A cartesian to compare.
  8792. * @param {Cartesian2} result The object into which to store the result.
  8793. * @returns {Cartesian2} A cartesian with the maximum components.
  8794. */
  8795. Cartesian2.maximumByComponent = function (first, second, result) {
  8796. //>>includeStart('debug', pragmas.debug);
  8797. RuntimeError.Check.typeOf.object("first", first);
  8798. RuntimeError.Check.typeOf.object("second", second);
  8799. RuntimeError.Check.typeOf.object("result", result);
  8800. //>>includeEnd('debug');
  8801. result.x = Math.max(first.x, second.x);
  8802. result.y = Math.max(first.y, second.y);
  8803. return result;
  8804. };
  8805. /**
  8806. * Constrain a value to lie between two values.
  8807. *
  8808. * @param {Cartesian2} value The value to clamp.
  8809. * @param {Cartesian2} min The minimum bound.
  8810. * @param {Cartesian2} max The maximum bound.
  8811. * @param {Cartesian2} result The object into which to store the result.
  8812. * @returns {Cartesian2} The clamped value such that min <= result <= max.
  8813. */
  8814. Cartesian2.clamp = function (value, min, max, result) {
  8815. //>>includeStart('debug', pragmas.debug);
  8816. RuntimeError.Check.typeOf.object("value", value);
  8817. RuntimeError.Check.typeOf.object("min", min);
  8818. RuntimeError.Check.typeOf.object("max", max);
  8819. RuntimeError.Check.typeOf.object("result", result);
  8820. //>>includeEnd('debug');
  8821. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  8822. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  8823. result.x = x;
  8824. result.y = y;
  8825. return result;
  8826. };
  8827. /**
  8828. * Computes the provided Cartesian's squared magnitude.
  8829. *
  8830. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  8831. * @returns {Number} The squared magnitude.
  8832. */
  8833. Cartesian2.magnitudeSquared = function (cartesian) {
  8834. //>>includeStart('debug', pragmas.debug);
  8835. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8836. //>>includeEnd('debug');
  8837. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  8838. };
  8839. /**
  8840. * Computes the Cartesian's magnitude (length).
  8841. *
  8842. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  8843. * @returns {Number} The magnitude.
  8844. */
  8845. Cartesian2.magnitude = function (cartesian) {
  8846. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  8847. };
  8848. const distanceScratch = new Cartesian2();
  8849. /**
  8850. * Computes the distance between two points.
  8851. *
  8852. * @param {Cartesian2} left The first point to compute the distance from.
  8853. * @param {Cartesian2} right The second point to compute the distance to.
  8854. * @returns {Number} The distance between two points.
  8855. *
  8856. * @example
  8857. * // Returns 1.0
  8858. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  8859. */
  8860. Cartesian2.distance = function (left, right) {
  8861. //>>includeStart('debug', pragmas.debug);
  8862. RuntimeError.Check.typeOf.object("left", left);
  8863. RuntimeError.Check.typeOf.object("right", right);
  8864. //>>includeEnd('debug');
  8865. Cartesian2.subtract(left, right, distanceScratch);
  8866. return Cartesian2.magnitude(distanceScratch);
  8867. };
  8868. /**
  8869. * Computes the squared distance between two points. Comparing squared distances
  8870. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  8871. *
  8872. * @param {Cartesian2} left The first point to compute the distance from.
  8873. * @param {Cartesian2} right The second point to compute the distance to.
  8874. * @returns {Number} The distance between two points.
  8875. *
  8876. * @example
  8877. * // Returns 4.0, not 2.0
  8878. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  8879. */
  8880. Cartesian2.distanceSquared = function (left, right) {
  8881. //>>includeStart('debug', pragmas.debug);
  8882. RuntimeError.Check.typeOf.object("left", left);
  8883. RuntimeError.Check.typeOf.object("right", right);
  8884. //>>includeEnd('debug');
  8885. Cartesian2.subtract(left, right, distanceScratch);
  8886. return Cartesian2.magnitudeSquared(distanceScratch);
  8887. };
  8888. /**
  8889. * Computes the normalized form of the supplied Cartesian.
  8890. *
  8891. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  8892. * @param {Cartesian2} result The object onto which to store the result.
  8893. * @returns {Cartesian2} The modified result parameter.
  8894. */
  8895. Cartesian2.normalize = function (cartesian, result) {
  8896. //>>includeStart('debug', pragmas.debug);
  8897. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8898. RuntimeError.Check.typeOf.object("result", result);
  8899. //>>includeEnd('debug');
  8900. const magnitude = Cartesian2.magnitude(cartesian);
  8901. result.x = cartesian.x / magnitude;
  8902. result.y = cartesian.y / magnitude;
  8903. //>>includeStart('debug', pragmas.debug);
  8904. if (isNaN(result.x) || isNaN(result.y)) {
  8905. throw new RuntimeError.DeveloperError("normalized result is not a number");
  8906. }
  8907. //>>includeEnd('debug');
  8908. return result;
  8909. };
  8910. /**
  8911. * Computes the dot (scalar) product of two Cartesians.
  8912. *
  8913. * @param {Cartesian2} left The first Cartesian.
  8914. * @param {Cartesian2} right The second Cartesian.
  8915. * @returns {Number} The dot product.
  8916. */
  8917. Cartesian2.dot = function (left, right) {
  8918. //>>includeStart('debug', pragmas.debug);
  8919. RuntimeError.Check.typeOf.object("left", left);
  8920. RuntimeError.Check.typeOf.object("right", right);
  8921. //>>includeEnd('debug');
  8922. return left.x * right.x + left.y * right.y;
  8923. };
  8924. /**
  8925. * Computes the magnitude of the cross product that would result from implicitly setting the Z coordinate of the input vectors to 0
  8926. *
  8927. * @param {Cartesian2} left The first Cartesian.
  8928. * @param {Cartesian2} right The second Cartesian.
  8929. * @returns {Number} The cross product.
  8930. */
  8931. Cartesian2.cross = function (left, right) {
  8932. //>>includeStart('debug', pragmas.debug);
  8933. RuntimeError.Check.typeOf.object("left", left);
  8934. RuntimeError.Check.typeOf.object("right", right);
  8935. //>>includeEnd('debug');
  8936. return left.x * right.y - left.y * right.x;
  8937. };
  8938. /**
  8939. * Computes the componentwise product of two Cartesians.
  8940. *
  8941. * @param {Cartesian2} left The first Cartesian.
  8942. * @param {Cartesian2} right The second Cartesian.
  8943. * @param {Cartesian2} result The object onto which to store the result.
  8944. * @returns {Cartesian2} The modified result parameter.
  8945. */
  8946. Cartesian2.multiplyComponents = function (left, right, result) {
  8947. //>>includeStart('debug', pragmas.debug);
  8948. RuntimeError.Check.typeOf.object("left", left);
  8949. RuntimeError.Check.typeOf.object("right", right);
  8950. RuntimeError.Check.typeOf.object("result", result);
  8951. //>>includeEnd('debug');
  8952. result.x = left.x * right.x;
  8953. result.y = left.y * right.y;
  8954. return result;
  8955. };
  8956. /**
  8957. * Computes the componentwise quotient of two Cartesians.
  8958. *
  8959. * @param {Cartesian2} left The first Cartesian.
  8960. * @param {Cartesian2} right The second Cartesian.
  8961. * @param {Cartesian2} result The object onto which to store the result.
  8962. * @returns {Cartesian2} The modified result parameter.
  8963. */
  8964. Cartesian2.divideComponents = function (left, right, result) {
  8965. //>>includeStart('debug', pragmas.debug);
  8966. RuntimeError.Check.typeOf.object("left", left);
  8967. RuntimeError.Check.typeOf.object("right", right);
  8968. RuntimeError.Check.typeOf.object("result", result);
  8969. //>>includeEnd('debug');
  8970. result.x = left.x / right.x;
  8971. result.y = left.y / right.y;
  8972. return result;
  8973. };
  8974. /**
  8975. * Computes the componentwise sum of two Cartesians.
  8976. *
  8977. * @param {Cartesian2} left The first Cartesian.
  8978. * @param {Cartesian2} right The second Cartesian.
  8979. * @param {Cartesian2} result The object onto which to store the result.
  8980. * @returns {Cartesian2} The modified result parameter.
  8981. */
  8982. Cartesian2.add = function (left, right, result) {
  8983. //>>includeStart('debug', pragmas.debug);
  8984. RuntimeError.Check.typeOf.object("left", left);
  8985. RuntimeError.Check.typeOf.object("right", right);
  8986. RuntimeError.Check.typeOf.object("result", result);
  8987. //>>includeEnd('debug');
  8988. result.x = left.x + right.x;
  8989. result.y = left.y + right.y;
  8990. return result;
  8991. };
  8992. /**
  8993. * Computes the componentwise difference of two Cartesians.
  8994. *
  8995. * @param {Cartesian2} left The first Cartesian.
  8996. * @param {Cartesian2} right The second Cartesian.
  8997. * @param {Cartesian2} result The object onto which to store the result.
  8998. * @returns {Cartesian2} The modified result parameter.
  8999. */
  9000. Cartesian2.subtract = function (left, right, result) {
  9001. //>>includeStart('debug', pragmas.debug);
  9002. RuntimeError.Check.typeOf.object("left", left);
  9003. RuntimeError.Check.typeOf.object("right", right);
  9004. RuntimeError.Check.typeOf.object("result", result);
  9005. //>>includeEnd('debug');
  9006. result.x = left.x - right.x;
  9007. result.y = left.y - right.y;
  9008. return result;
  9009. };
  9010. /**
  9011. * Multiplies the provided Cartesian componentwise by the provided scalar.
  9012. *
  9013. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  9014. * @param {Number} scalar The scalar to multiply with.
  9015. * @param {Cartesian2} result The object onto which to store the result.
  9016. * @returns {Cartesian2} The modified result parameter.
  9017. */
  9018. Cartesian2.multiplyByScalar = function (cartesian, scalar, result) {
  9019. //>>includeStart('debug', pragmas.debug);
  9020. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9021. RuntimeError.Check.typeOf.number("scalar", scalar);
  9022. RuntimeError.Check.typeOf.object("result", result);
  9023. //>>includeEnd('debug');
  9024. result.x = cartesian.x * scalar;
  9025. result.y = cartesian.y * scalar;
  9026. return result;
  9027. };
  9028. /**
  9029. * Divides the provided Cartesian componentwise by the provided scalar.
  9030. *
  9031. * @param {Cartesian2} cartesian The Cartesian to be divided.
  9032. * @param {Number} scalar The scalar to divide by.
  9033. * @param {Cartesian2} result The object onto which to store the result.
  9034. * @returns {Cartesian2} The modified result parameter.
  9035. */
  9036. Cartesian2.divideByScalar = function (cartesian, scalar, result) {
  9037. //>>includeStart('debug', pragmas.debug);
  9038. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9039. RuntimeError.Check.typeOf.number("scalar", scalar);
  9040. RuntimeError.Check.typeOf.object("result", result);
  9041. //>>includeEnd('debug');
  9042. result.x = cartesian.x / scalar;
  9043. result.y = cartesian.y / scalar;
  9044. return result;
  9045. };
  9046. /**
  9047. * Negates the provided Cartesian.
  9048. *
  9049. * @param {Cartesian2} cartesian The Cartesian to be negated.
  9050. * @param {Cartesian2} result The object onto which to store the result.
  9051. * @returns {Cartesian2} The modified result parameter.
  9052. */
  9053. Cartesian2.negate = function (cartesian, result) {
  9054. //>>includeStart('debug', pragmas.debug);
  9055. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9056. RuntimeError.Check.typeOf.object("result", result);
  9057. //>>includeEnd('debug');
  9058. result.x = -cartesian.x;
  9059. result.y = -cartesian.y;
  9060. return result;
  9061. };
  9062. /**
  9063. * Computes the absolute value of the provided Cartesian.
  9064. *
  9065. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  9066. * @param {Cartesian2} result The object onto which to store the result.
  9067. * @returns {Cartesian2} The modified result parameter.
  9068. */
  9069. Cartesian2.abs = function (cartesian, result) {
  9070. //>>includeStart('debug', pragmas.debug);
  9071. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9072. RuntimeError.Check.typeOf.object("result", result);
  9073. //>>includeEnd('debug');
  9074. result.x = Math.abs(cartesian.x);
  9075. result.y = Math.abs(cartesian.y);
  9076. return result;
  9077. };
  9078. const lerpScratch = new Cartesian2();
  9079. /**
  9080. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  9081. *
  9082. * @param {Cartesian2} start The value corresponding to t at 0.0.
  9083. * @param {Cartesian2} end The value corresponding to t at 1.0.
  9084. * @param {Number} t The point along t at which to interpolate.
  9085. * @param {Cartesian2} result The object onto which to store the result.
  9086. * @returns {Cartesian2} The modified result parameter.
  9087. */
  9088. Cartesian2.lerp = function (start, end, t, result) {
  9089. //>>includeStart('debug', pragmas.debug);
  9090. RuntimeError.Check.typeOf.object("start", start);
  9091. RuntimeError.Check.typeOf.object("end", end);
  9092. RuntimeError.Check.typeOf.number("t", t);
  9093. RuntimeError.Check.typeOf.object("result", result);
  9094. //>>includeEnd('debug');
  9095. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  9096. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  9097. return Cartesian2.add(lerpScratch, result, result);
  9098. };
  9099. const angleBetweenScratch = new Cartesian2();
  9100. const angleBetweenScratch2 = new Cartesian2();
  9101. /**
  9102. * Returns the angle, in radians, between the provided Cartesians.
  9103. *
  9104. * @param {Cartesian2} left The first Cartesian.
  9105. * @param {Cartesian2} right The second Cartesian.
  9106. * @returns {Number} The angle between the Cartesians.
  9107. */
  9108. Cartesian2.angleBetween = function (left, right) {
  9109. //>>includeStart('debug', pragmas.debug);
  9110. RuntimeError.Check.typeOf.object("left", left);
  9111. RuntimeError.Check.typeOf.object("right", right);
  9112. //>>includeEnd('debug');
  9113. Cartesian2.normalize(left, angleBetweenScratch);
  9114. Cartesian2.normalize(right, angleBetweenScratch2);
  9115. return ComponentDatatype.CesiumMath.acosClamped(
  9116. Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2)
  9117. );
  9118. };
  9119. const mostOrthogonalAxisScratch = new Cartesian2();
  9120. /**
  9121. * Returns the axis that is most orthogonal to the provided Cartesian.
  9122. *
  9123. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  9124. * @param {Cartesian2} result The object onto which to store the result.
  9125. * @returns {Cartesian2} The most orthogonal axis.
  9126. */
  9127. Cartesian2.mostOrthogonalAxis = function (cartesian, result) {
  9128. //>>includeStart('debug', pragmas.debug);
  9129. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9130. RuntimeError.Check.typeOf.object("result", result);
  9131. //>>includeEnd('debug');
  9132. const f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  9133. Cartesian2.abs(f, f);
  9134. if (f.x <= f.y) {
  9135. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  9136. } else {
  9137. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  9138. }
  9139. return result;
  9140. };
  9141. /**
  9142. * Compares the provided Cartesians componentwise and returns
  9143. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9144. *
  9145. * @param {Cartesian2} [left] The first Cartesian.
  9146. * @param {Cartesian2} [right] The second Cartesian.
  9147. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  9148. */
  9149. Cartesian2.equals = function (left, right) {
  9150. return (
  9151. left === right ||
  9152. (defaultValue.defined(left) &&
  9153. defaultValue.defined(right) &&
  9154. left.x === right.x &&
  9155. left.y === right.y)
  9156. );
  9157. };
  9158. /**
  9159. * @private
  9160. */
  9161. Cartesian2.equalsArray = function (cartesian, array, offset) {
  9162. return cartesian.x === array[offset] && cartesian.y === array[offset + 1];
  9163. };
  9164. /**
  9165. * Compares the provided Cartesians componentwise and returns
  9166. * <code>true</code> if they pass an absolute or relative tolerance test,
  9167. * <code>false</code> otherwise.
  9168. *
  9169. * @param {Cartesian2} [left] The first Cartesian.
  9170. * @param {Cartesian2} [right] The second Cartesian.
  9171. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  9172. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  9173. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  9174. */
  9175. Cartesian2.equalsEpsilon = function (
  9176. left,
  9177. right,
  9178. relativeEpsilon,
  9179. absoluteEpsilon
  9180. ) {
  9181. return (
  9182. left === right ||
  9183. (defaultValue.defined(left) &&
  9184. defaultValue.defined(right) &&
  9185. ComponentDatatype.CesiumMath.equalsEpsilon(
  9186. left.x,
  9187. right.x,
  9188. relativeEpsilon,
  9189. absoluteEpsilon
  9190. ) &&
  9191. ComponentDatatype.CesiumMath.equalsEpsilon(
  9192. left.y,
  9193. right.y,
  9194. relativeEpsilon,
  9195. absoluteEpsilon
  9196. ))
  9197. );
  9198. };
  9199. /**
  9200. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  9201. *
  9202. * @type {Cartesian2}
  9203. * @constant
  9204. */
  9205. Cartesian2.ZERO = Object.freeze(new Cartesian2(0.0, 0.0));
  9206. /**
  9207. * An immutable Cartesian2 instance initialized to (1.0, 1.0).
  9208. *
  9209. * @type {Cartesian2}
  9210. * @constant
  9211. */
  9212. Cartesian2.ONE = Object.freeze(new Cartesian2(1.0, 1.0));
  9213. /**
  9214. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  9215. *
  9216. * @type {Cartesian2}
  9217. * @constant
  9218. */
  9219. Cartesian2.UNIT_X = Object.freeze(new Cartesian2(1.0, 0.0));
  9220. /**
  9221. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  9222. *
  9223. * @type {Cartesian2}
  9224. * @constant
  9225. */
  9226. Cartesian2.UNIT_Y = Object.freeze(new Cartesian2(0.0, 1.0));
  9227. /**
  9228. * Duplicates this Cartesian2 instance.
  9229. *
  9230. * @param {Cartesian2} [result] The object onto which to store the result.
  9231. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  9232. */
  9233. Cartesian2.prototype.clone = function (result) {
  9234. return Cartesian2.clone(this, result);
  9235. };
  9236. /**
  9237. * Compares this Cartesian against the provided Cartesian componentwise and returns
  9238. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9239. *
  9240. * @param {Cartesian2} [right] The right hand side Cartesian.
  9241. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  9242. */
  9243. Cartesian2.prototype.equals = function (right) {
  9244. return Cartesian2.equals(this, right);
  9245. };
  9246. /**
  9247. * Compares this Cartesian against the provided Cartesian componentwise and returns
  9248. * <code>true</code> if they pass an absolute or relative tolerance test,
  9249. * <code>false</code> otherwise.
  9250. *
  9251. * @param {Cartesian2} [right] The right hand side Cartesian.
  9252. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  9253. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  9254. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  9255. */
  9256. Cartesian2.prototype.equalsEpsilon = function (
  9257. right,
  9258. relativeEpsilon,
  9259. absoluteEpsilon
  9260. ) {
  9261. return Cartesian2.equalsEpsilon(
  9262. this,
  9263. right,
  9264. relativeEpsilon,
  9265. absoluteEpsilon
  9266. );
  9267. };
  9268. /**
  9269. * Creates a string representing this Cartesian in the format '(x, y)'.
  9270. *
  9271. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  9272. */
  9273. Cartesian2.prototype.toString = function () {
  9274. return `(${this.x}, ${this.y})`;
  9275. };
  9276. /**
  9277. * A 2x2 matrix, indexable as a column-major order array.
  9278. * Constructor parameters are in row-major order for code readability.
  9279. * @alias Matrix2
  9280. * @constructor
  9281. * @implements {ArrayLike<number>}
  9282. *
  9283. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  9284. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  9285. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  9286. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  9287. *
  9288. * @see Matrix2.fromArray
  9289. * @see Matrix2.fromColumnMajorArray
  9290. * @see Matrix2.fromRowMajorArray
  9291. * @see Matrix2.fromScale
  9292. * @see Matrix2.fromUniformScale
  9293. * @see Matrix2.fromRotation
  9294. * @see Matrix3
  9295. * @see Matrix4
  9296. */
  9297. function Matrix2(column0Row0, column1Row0, column0Row1, column1Row1) {
  9298. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  9299. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  9300. this[2] = defaultValue.defaultValue(column1Row0, 0.0);
  9301. this[3] = defaultValue.defaultValue(column1Row1, 0.0);
  9302. }
  9303. /**
  9304. * The number of elements used to pack the object into an array.
  9305. * @type {Number}
  9306. */
  9307. Matrix2.packedLength = 4;
  9308. /**
  9309. * Stores the provided instance into the provided array.
  9310. *
  9311. * @param {Matrix2} value The value to pack.
  9312. * @param {Number[]} array The array to pack into.
  9313. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  9314. *
  9315. * @returns {Number[]} The array that was packed into
  9316. */
  9317. Matrix2.pack = function (value, array, startingIndex) {
  9318. //>>includeStart('debug', pragmas.debug);
  9319. RuntimeError.Check.typeOf.object("value", value);
  9320. RuntimeError.Check.defined("array", array);
  9321. //>>includeEnd('debug');
  9322. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  9323. array[startingIndex++] = value[0];
  9324. array[startingIndex++] = value[1];
  9325. array[startingIndex++] = value[2];
  9326. array[startingIndex++] = value[3];
  9327. return array;
  9328. };
  9329. /**
  9330. * Retrieves an instance from a packed array.
  9331. *
  9332. * @param {Number[]} array The packed array.
  9333. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  9334. * @param {Matrix2} [result] The object into which to store the result.
  9335. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  9336. */
  9337. Matrix2.unpack = function (array, startingIndex, result) {
  9338. //>>includeStart('debug', pragmas.debug);
  9339. RuntimeError.Check.defined("array", array);
  9340. //>>includeEnd('debug');
  9341. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  9342. if (!defaultValue.defined(result)) {
  9343. result = new Matrix2();
  9344. }
  9345. result[0] = array[startingIndex++];
  9346. result[1] = array[startingIndex++];
  9347. result[2] = array[startingIndex++];
  9348. result[3] = array[startingIndex++];
  9349. return result;
  9350. };
  9351. /**
  9352. * Flattens an array of Matrix2s into an array of components. The components
  9353. * are stored in column-major order.
  9354. *
  9355. * @param {Matrix2[]} array The array of matrices to pack.
  9356. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 4 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 4) elements.
  9357. * @returns {Number[]} The packed array.
  9358. */
  9359. Matrix2.packArray = function (array, result) {
  9360. //>>includeStart('debug', pragmas.debug);
  9361. RuntimeError.Check.defined("array", array);
  9362. //>>includeEnd('debug');
  9363. const length = array.length;
  9364. const resultLength = length * 4;
  9365. if (!defaultValue.defined(result)) {
  9366. result = new Array(resultLength);
  9367. } else if (!Array.isArray(result) && result.length !== resultLength) {
  9368. //>>includeStart('debug', pragmas.debug);
  9369. throw new RuntimeError.DeveloperError(
  9370. "If result is a typed array, it must have exactly array.length * 4 elements"
  9371. );
  9372. //>>includeEnd('debug');
  9373. } else if (result.length !== resultLength) {
  9374. result.length = resultLength;
  9375. }
  9376. for (let i = 0; i < length; ++i) {
  9377. Matrix2.pack(array[i], result, i * 4);
  9378. }
  9379. return result;
  9380. };
  9381. /**
  9382. * Unpacks an array of column-major matrix components into an array of Matrix2s.
  9383. *
  9384. * @param {Number[]} array The array of components to unpack.
  9385. * @param {Matrix2[]} [result] The array onto which to store the result.
  9386. * @returns {Matrix2[]} The unpacked array.
  9387. */
  9388. Matrix2.unpackArray = function (array, result) {
  9389. //>>includeStart('debug', pragmas.debug);
  9390. RuntimeError.Check.defined("array", array);
  9391. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  9392. if (array.length % 4 !== 0) {
  9393. throw new RuntimeError.DeveloperError("array length must be a multiple of 4.");
  9394. }
  9395. //>>includeEnd('debug');
  9396. const length = array.length;
  9397. if (!defaultValue.defined(result)) {
  9398. result = new Array(length / 4);
  9399. } else {
  9400. result.length = length / 4;
  9401. }
  9402. for (let i = 0; i < length; i += 4) {
  9403. const index = i / 4;
  9404. result[index] = Matrix2.unpack(array, i, result[index]);
  9405. }
  9406. return result;
  9407. };
  9408. /**
  9409. * Duplicates a Matrix2 instance.
  9410. *
  9411. * @param {Matrix2} matrix The matrix to duplicate.
  9412. * @param {Matrix2} [result] The object onto which to store the result.
  9413. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided. (Returns undefined if matrix is undefined)
  9414. */
  9415. Matrix2.clone = function (matrix, result) {
  9416. if (!defaultValue.defined(matrix)) {
  9417. return undefined;
  9418. }
  9419. if (!defaultValue.defined(result)) {
  9420. return new Matrix2(matrix[0], matrix[2], matrix[1], matrix[3]);
  9421. }
  9422. result[0] = matrix[0];
  9423. result[1] = matrix[1];
  9424. result[2] = matrix[2];
  9425. result[3] = matrix[3];
  9426. return result;
  9427. };
  9428. /**
  9429. * Creates a Matrix2 from 4 consecutive elements in an array.
  9430. *
  9431. * @function
  9432. * @param {Number[]} array The array whose 4 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  9433. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  9434. * @param {Matrix2} [result] The object onto which to store the result.
  9435. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  9436. *
  9437. * @example
  9438. * // Create the Matrix2:
  9439. * // [1.0, 2.0]
  9440. * // [1.0, 2.0]
  9441. *
  9442. * const v = [1.0, 1.0, 2.0, 2.0];
  9443. * const m = Cesium.Matrix2.fromArray(v);
  9444. *
  9445. * // Create same Matrix2 with using an offset into an array
  9446. * const v2 = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0];
  9447. * const m2 = Cesium.Matrix2.fromArray(v2, 2);
  9448. */
  9449. Matrix2.fromArray = Matrix2.unpack;
  9450. /**
  9451. * Creates a Matrix2 instance from a column-major order array.
  9452. *
  9453. * @param {Number[]} values The column-major order array.
  9454. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9455. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9456. */
  9457. Matrix2.fromColumnMajorArray = function (values, result) {
  9458. //>>includeStart('debug', pragmas.debug);
  9459. RuntimeError.Check.defined("values", values);
  9460. //>>includeEnd('debug');
  9461. return Matrix2.clone(values, result);
  9462. };
  9463. /**
  9464. * Creates a Matrix2 instance from a row-major order array.
  9465. * The resulting matrix will be in column-major order.
  9466. *
  9467. * @param {Number[]} values The row-major order array.
  9468. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9469. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9470. */
  9471. Matrix2.fromRowMajorArray = function (values, result) {
  9472. //>>includeStart('debug', pragmas.debug);
  9473. RuntimeError.Check.defined("values", values);
  9474. //>>includeEnd('debug');
  9475. if (!defaultValue.defined(result)) {
  9476. return new Matrix2(values[0], values[1], values[2], values[3]);
  9477. }
  9478. result[0] = values[0];
  9479. result[1] = values[2];
  9480. result[2] = values[1];
  9481. result[3] = values[3];
  9482. return result;
  9483. };
  9484. /**
  9485. * Computes a Matrix2 instance representing a non-uniform scale.
  9486. *
  9487. * @param {Cartesian2} scale The x and y scale factors.
  9488. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9489. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9490. *
  9491. * @example
  9492. * // Creates
  9493. * // [7.0, 0.0]
  9494. * // [0.0, 8.0]
  9495. * const m = Cesium.Matrix2.fromScale(new Cesium.Cartesian2(7.0, 8.0));
  9496. */
  9497. Matrix2.fromScale = function (scale, result) {
  9498. //>>includeStart('debug', pragmas.debug);
  9499. RuntimeError.Check.typeOf.object("scale", scale);
  9500. //>>includeEnd('debug');
  9501. if (!defaultValue.defined(result)) {
  9502. return new Matrix2(scale.x, 0.0, 0.0, scale.y);
  9503. }
  9504. result[0] = scale.x;
  9505. result[1] = 0.0;
  9506. result[2] = 0.0;
  9507. result[3] = scale.y;
  9508. return result;
  9509. };
  9510. /**
  9511. * Computes a Matrix2 instance representing a uniform scale.
  9512. *
  9513. * @param {Number} scale The uniform scale factor.
  9514. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9515. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9516. *
  9517. * @example
  9518. * // Creates
  9519. * // [2.0, 0.0]
  9520. * // [0.0, 2.0]
  9521. * const m = Cesium.Matrix2.fromUniformScale(2.0);
  9522. */
  9523. Matrix2.fromUniformScale = function (scale, result) {
  9524. //>>includeStart('debug', pragmas.debug);
  9525. RuntimeError.Check.typeOf.number("scale", scale);
  9526. //>>includeEnd('debug');
  9527. if (!defaultValue.defined(result)) {
  9528. return new Matrix2(scale, 0.0, 0.0, scale);
  9529. }
  9530. result[0] = scale;
  9531. result[1] = 0.0;
  9532. result[2] = 0.0;
  9533. result[3] = scale;
  9534. return result;
  9535. };
  9536. /**
  9537. * Creates a rotation matrix.
  9538. *
  9539. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  9540. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9541. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9542. *
  9543. * @example
  9544. * // Rotate a point 45 degrees counterclockwise.
  9545. * const p = new Cesium.Cartesian2(5, 6);
  9546. * const m = Cesium.Matrix2.fromRotation(Cesium.Math.toRadians(45.0));
  9547. * const rotated = Cesium.Matrix2.multiplyByVector(m, p, new Cesium.Cartesian2());
  9548. */
  9549. Matrix2.fromRotation = function (angle, result) {
  9550. //>>includeStart('debug', pragmas.debug);
  9551. RuntimeError.Check.typeOf.number("angle", angle);
  9552. //>>includeEnd('debug');
  9553. const cosAngle = Math.cos(angle);
  9554. const sinAngle = Math.sin(angle);
  9555. if (!defaultValue.defined(result)) {
  9556. return new Matrix2(cosAngle, -sinAngle, sinAngle, cosAngle);
  9557. }
  9558. result[0] = cosAngle;
  9559. result[1] = sinAngle;
  9560. result[2] = -sinAngle;
  9561. result[3] = cosAngle;
  9562. return result;
  9563. };
  9564. /**
  9565. * Creates an Array from the provided Matrix2 instance.
  9566. * The array will be in column-major order.
  9567. *
  9568. * @param {Matrix2} matrix The matrix to use..
  9569. * @param {Number[]} [result] The Array onto which to store the result.
  9570. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  9571. */
  9572. Matrix2.toArray = function (matrix, result) {
  9573. //>>includeStart('debug', pragmas.debug);
  9574. RuntimeError.Check.typeOf.object("matrix", matrix);
  9575. //>>includeEnd('debug');
  9576. if (!defaultValue.defined(result)) {
  9577. return [matrix[0], matrix[1], matrix[2], matrix[3]];
  9578. }
  9579. result[0] = matrix[0];
  9580. result[1] = matrix[1];
  9581. result[2] = matrix[2];
  9582. result[3] = matrix[3];
  9583. return result;
  9584. };
  9585. /**
  9586. * Computes the array index of the element at the provided row and column.
  9587. *
  9588. * @param {Number} row The zero-based index of the row.
  9589. * @param {Number} column The zero-based index of the column.
  9590. * @returns {Number} The index of the element at the provided row and column.
  9591. *
  9592. * @exception {DeveloperError} row must be 0 or 1.
  9593. * @exception {DeveloperError} column must be 0 or 1.
  9594. *
  9595. * @example
  9596. * const myMatrix = new Cesium.Matrix2();
  9597. * const column1Row0Index = Cesium.Matrix2.getElementIndex(1, 0);
  9598. * const column1Row0 = myMatrix[column1Row0Index]
  9599. * myMatrix[column1Row0Index] = 10.0;
  9600. */
  9601. Matrix2.getElementIndex = function (column, row) {
  9602. //>>includeStart('debug', pragmas.debug);
  9603. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  9604. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 1);
  9605. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  9606. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 1);
  9607. //>>includeEnd('debug');
  9608. return column * 2 + row;
  9609. };
  9610. /**
  9611. * Retrieves a copy of the matrix column at the provided index as a Cartesian2 instance.
  9612. *
  9613. * @param {Matrix2} matrix The matrix to use.
  9614. * @param {Number} index The zero-based index of the column to retrieve.
  9615. * @param {Cartesian2} result The object onto which to store the result.
  9616. * @returns {Cartesian2} The modified result parameter.
  9617. *
  9618. * @exception {DeveloperError} index must be 0 or 1.
  9619. */
  9620. Matrix2.getColumn = function (matrix, index, result) {
  9621. //>>includeStart('debug', pragmas.debug);
  9622. RuntimeError.Check.typeOf.object("matrix", matrix);
  9623. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9624. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9625. RuntimeError.Check.typeOf.object("result", result);
  9626. //>>includeEnd('debug');
  9627. const startIndex = index * 2;
  9628. const x = matrix[startIndex];
  9629. const y = matrix[startIndex + 1];
  9630. result.x = x;
  9631. result.y = y;
  9632. return result;
  9633. };
  9634. /**
  9635. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian2 instance.
  9636. *
  9637. * @param {Matrix2} matrix The matrix to use.
  9638. * @param {Number} index The zero-based index of the column to set.
  9639. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified column.
  9640. * @param {Cartesian2} result The object onto which to store the result.
  9641. * @returns {Matrix2} The modified result parameter.
  9642. *
  9643. * @exception {DeveloperError} index must be 0 or 1.
  9644. */
  9645. Matrix2.setColumn = function (matrix, index, cartesian, result) {
  9646. //>>includeStart('debug', pragmas.debug);
  9647. RuntimeError.Check.typeOf.object("matrix", matrix);
  9648. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9649. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9650. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9651. RuntimeError.Check.typeOf.object("result", result);
  9652. //>>includeEnd('debug');
  9653. result = Matrix2.clone(matrix, result);
  9654. const startIndex = index * 2;
  9655. result[startIndex] = cartesian.x;
  9656. result[startIndex + 1] = cartesian.y;
  9657. return result;
  9658. };
  9659. /**
  9660. * Retrieves a copy of the matrix row at the provided index as a Cartesian2 instance.
  9661. *
  9662. * @param {Matrix2} matrix The matrix to use.
  9663. * @param {Number} index The zero-based index of the row to retrieve.
  9664. * @param {Cartesian2} result The object onto which to store the result.
  9665. * @returns {Cartesian2} The modified result parameter.
  9666. *
  9667. * @exception {DeveloperError} index must be 0 or 1.
  9668. */
  9669. Matrix2.getRow = function (matrix, index, result) {
  9670. //>>includeStart('debug', pragmas.debug);
  9671. RuntimeError.Check.typeOf.object("matrix", matrix);
  9672. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9673. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9674. RuntimeError.Check.typeOf.object("result", result);
  9675. //>>includeEnd('debug');
  9676. const x = matrix[index];
  9677. const y = matrix[index + 2];
  9678. result.x = x;
  9679. result.y = y;
  9680. return result;
  9681. };
  9682. /**
  9683. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian2 instance.
  9684. *
  9685. * @param {Matrix2} matrix The matrix to use.
  9686. * @param {Number} index The zero-based index of the row to set.
  9687. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified row.
  9688. * @param {Matrix2} result The object onto which to store the result.
  9689. * @returns {Matrix2} The modified result parameter.
  9690. *
  9691. * @exception {DeveloperError} index must be 0 or 1.
  9692. */
  9693. Matrix2.setRow = function (matrix, index, cartesian, result) {
  9694. //>>includeStart('debug', pragmas.debug);
  9695. RuntimeError.Check.typeOf.object("matrix", matrix);
  9696. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9697. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9698. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9699. RuntimeError.Check.typeOf.object("result", result);
  9700. //>>includeEnd('debug');
  9701. result = Matrix2.clone(matrix, result);
  9702. result[index] = cartesian.x;
  9703. result[index + 2] = cartesian.y;
  9704. return result;
  9705. };
  9706. const scaleScratch1 = new Cartesian2();
  9707. /**
  9708. * Computes a new matrix that replaces the scale with the provided scale.
  9709. * This assumes the matrix is an affine transformation.
  9710. *
  9711. * @param {Matrix2} matrix The matrix to use.
  9712. * @param {Cartesian2} scale The scale that replaces the scale of the provided matrix.
  9713. * @param {Matrix2} result The object onto which to store the result.
  9714. * @returns {Matrix2} The modified result parameter.
  9715. *
  9716. * @see Matrix2.setUniformScale
  9717. * @see Matrix2.fromScale
  9718. * @see Matrix2.fromUniformScale
  9719. * @see Matrix2.multiplyByScale
  9720. * @see Matrix2.multiplyByUniformScale
  9721. * @see Matrix2.getScale
  9722. */
  9723. Matrix2.setScale = function (matrix, scale, result) {
  9724. //>>includeStart('debug', pragmas.debug);
  9725. RuntimeError.Check.typeOf.object("matrix", matrix);
  9726. RuntimeError.Check.typeOf.object("scale", scale);
  9727. RuntimeError.Check.typeOf.object("result", result);
  9728. //>>includeEnd('debug');
  9729. const existingScale = Matrix2.getScale(matrix, scaleScratch1);
  9730. const scaleRatioX = scale.x / existingScale.x;
  9731. const scaleRatioY = scale.y / existingScale.y;
  9732. result[0] = matrix[0] * scaleRatioX;
  9733. result[1] = matrix[1] * scaleRatioX;
  9734. result[2] = matrix[2] * scaleRatioY;
  9735. result[3] = matrix[3] * scaleRatioY;
  9736. return result;
  9737. };
  9738. const scaleScratch2 = new Cartesian2();
  9739. /**
  9740. * Computes a new matrix that replaces the scale with the provided uniform scale.
  9741. * This assumes the matrix is an affine transformation.
  9742. *
  9743. * @param {Matrix2} matrix The matrix to use.
  9744. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  9745. * @param {Matrix2} result The object onto which to store the result.
  9746. * @returns {Matrix2} The modified result parameter.
  9747. *
  9748. * @see Matrix2.setScale
  9749. * @see Matrix2.fromScale
  9750. * @see Matrix2.fromUniformScale
  9751. * @see Matrix2.multiplyByScale
  9752. * @see Matrix2.multiplyByUniformScale
  9753. * @see Matrix2.getScale
  9754. */
  9755. Matrix2.setUniformScale = function (matrix, scale, result) {
  9756. //>>includeStart('debug', pragmas.debug);
  9757. RuntimeError.Check.typeOf.object("matrix", matrix);
  9758. RuntimeError.Check.typeOf.number("scale", scale);
  9759. RuntimeError.Check.typeOf.object("result", result);
  9760. //>>includeEnd('debug');
  9761. const existingScale = Matrix2.getScale(matrix, scaleScratch2);
  9762. const scaleRatioX = scale / existingScale.x;
  9763. const scaleRatioY = scale / existingScale.y;
  9764. result[0] = matrix[0] * scaleRatioX;
  9765. result[1] = matrix[1] * scaleRatioX;
  9766. result[2] = matrix[2] * scaleRatioY;
  9767. result[3] = matrix[3] * scaleRatioY;
  9768. return result;
  9769. };
  9770. const scratchColumn = new Cartesian2();
  9771. /**
  9772. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  9773. *
  9774. * @param {Matrix2} matrix The matrix.
  9775. * @param {Cartesian2} result The object onto which to store the result.
  9776. * @returns {Cartesian2} The modified result parameter.
  9777. *
  9778. * @see Matrix2.multiplyByScale
  9779. * @see Matrix2.multiplyByUniformScale
  9780. * @see Matrix2.fromScale
  9781. * @see Matrix2.fromUniformScale
  9782. * @see Matrix2.setScale
  9783. * @see Matrix2.setUniformScale
  9784. */
  9785. Matrix2.getScale = function (matrix, result) {
  9786. //>>includeStart('debug', pragmas.debug);
  9787. RuntimeError.Check.typeOf.object("matrix", matrix);
  9788. RuntimeError.Check.typeOf.object("result", result);
  9789. //>>includeEnd('debug');
  9790. result.x = Cartesian2.magnitude(
  9791. Cartesian2.fromElements(matrix[0], matrix[1], scratchColumn)
  9792. );
  9793. result.y = Cartesian2.magnitude(
  9794. Cartesian2.fromElements(matrix[2], matrix[3], scratchColumn)
  9795. );
  9796. return result;
  9797. };
  9798. const scaleScratch3 = new Cartesian2();
  9799. /**
  9800. * Computes the maximum scale assuming the matrix is an affine transformation.
  9801. * The maximum scale is the maximum length of the column vectors.
  9802. *
  9803. * @param {Matrix2} matrix The matrix.
  9804. * @returns {Number} The maximum scale.
  9805. */
  9806. Matrix2.getMaximumScale = function (matrix) {
  9807. Matrix2.getScale(matrix, scaleScratch3);
  9808. return Cartesian2.maximumComponent(scaleScratch3);
  9809. };
  9810. const scaleScratch4 = new Cartesian2();
  9811. /**
  9812. * Sets the rotation assuming the matrix is an affine transformation.
  9813. *
  9814. * @param {Matrix2} matrix The matrix.
  9815. * @param {Matrix2} rotation The rotation matrix.
  9816. * @returns {Matrix2} The modified result parameter.
  9817. *
  9818. * @see Matrix2.fromRotation
  9819. * @see Matrix2.getRotation
  9820. */
  9821. Matrix2.setRotation = function (matrix, rotation, result) {
  9822. //>>includeStart('debug', pragmas.debug);
  9823. RuntimeError.Check.typeOf.object("matrix", matrix);
  9824. RuntimeError.Check.typeOf.object("result", result);
  9825. //>>includeEnd('debug');
  9826. const scale = Matrix2.getScale(matrix, scaleScratch4);
  9827. result[0] = rotation[0] * scale.x;
  9828. result[1] = rotation[1] * scale.x;
  9829. result[2] = rotation[2] * scale.y;
  9830. result[3] = rotation[3] * scale.y;
  9831. return result;
  9832. };
  9833. const scaleScratch5 = new Cartesian2();
  9834. /**
  9835. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  9836. *
  9837. * @param {Matrix2} matrix The matrix.
  9838. * @param {Matrix2} result The object onto which to store the result.
  9839. * @returns {Matrix2} The modified result parameter.
  9840. *
  9841. * @see Matrix2.setRotation
  9842. * @see Matrix2.fromRotation
  9843. */
  9844. Matrix2.getRotation = function (matrix, result) {
  9845. //>>includeStart('debug', pragmas.debug);
  9846. RuntimeError.Check.typeOf.object("matrix", matrix);
  9847. RuntimeError.Check.typeOf.object("result", result);
  9848. //>>includeEnd('debug');
  9849. const scale = Matrix2.getScale(matrix, scaleScratch5);
  9850. result[0] = matrix[0] / scale.x;
  9851. result[1] = matrix[1] / scale.x;
  9852. result[2] = matrix[2] / scale.y;
  9853. result[3] = matrix[3] / scale.y;
  9854. return result;
  9855. };
  9856. /**
  9857. * Computes the product of two matrices.
  9858. *
  9859. * @param {Matrix2} left The first matrix.
  9860. * @param {Matrix2} right The second matrix.
  9861. * @param {Matrix2} result The object onto which to store the result.
  9862. * @returns {Matrix2} The modified result parameter.
  9863. */
  9864. Matrix2.multiply = function (left, right, result) {
  9865. //>>includeStart('debug', pragmas.debug);
  9866. RuntimeError.Check.typeOf.object("left", left);
  9867. RuntimeError.Check.typeOf.object("right", right);
  9868. RuntimeError.Check.typeOf.object("result", result);
  9869. //>>includeEnd('debug');
  9870. const column0Row0 = left[0] * right[0] + left[2] * right[1];
  9871. const column1Row0 = left[0] * right[2] + left[2] * right[3];
  9872. const column0Row1 = left[1] * right[0] + left[3] * right[1];
  9873. const column1Row1 = left[1] * right[2] + left[3] * right[3];
  9874. result[0] = column0Row0;
  9875. result[1] = column0Row1;
  9876. result[2] = column1Row0;
  9877. result[3] = column1Row1;
  9878. return result;
  9879. };
  9880. /**
  9881. * Computes the sum of two matrices.
  9882. *
  9883. * @param {Matrix2} left The first matrix.
  9884. * @param {Matrix2} right The second matrix.
  9885. * @param {Matrix2} result The object onto which to store the result.
  9886. * @returns {Matrix2} The modified result parameter.
  9887. */
  9888. Matrix2.add = function (left, right, result) {
  9889. //>>includeStart('debug', pragmas.debug);
  9890. RuntimeError.Check.typeOf.object("left", left);
  9891. RuntimeError.Check.typeOf.object("right", right);
  9892. RuntimeError.Check.typeOf.object("result", result);
  9893. //>>includeEnd('debug');
  9894. result[0] = left[0] + right[0];
  9895. result[1] = left[1] + right[1];
  9896. result[2] = left[2] + right[2];
  9897. result[3] = left[3] + right[3];
  9898. return result;
  9899. };
  9900. /**
  9901. * Computes the difference of two matrices.
  9902. *
  9903. * @param {Matrix2} left The first matrix.
  9904. * @param {Matrix2} right The second matrix.
  9905. * @param {Matrix2} result The object onto which to store the result.
  9906. * @returns {Matrix2} The modified result parameter.
  9907. */
  9908. Matrix2.subtract = function (left, right, result) {
  9909. //>>includeStart('debug', pragmas.debug);
  9910. RuntimeError.Check.typeOf.object("left", left);
  9911. RuntimeError.Check.typeOf.object("right", right);
  9912. RuntimeError.Check.typeOf.object("result", result);
  9913. //>>includeEnd('debug');
  9914. result[0] = left[0] - right[0];
  9915. result[1] = left[1] - right[1];
  9916. result[2] = left[2] - right[2];
  9917. result[3] = left[3] - right[3];
  9918. return result;
  9919. };
  9920. /**
  9921. * Computes the product of a matrix and a column vector.
  9922. *
  9923. * @param {Matrix2} matrix The matrix.
  9924. * @param {Cartesian2} cartesian The column.
  9925. * @param {Cartesian2} result The object onto which to store the result.
  9926. * @returns {Cartesian2} The modified result parameter.
  9927. */
  9928. Matrix2.multiplyByVector = function (matrix, cartesian, result) {
  9929. //>>includeStart('debug', pragmas.debug);
  9930. RuntimeError.Check.typeOf.object("matrix", matrix);
  9931. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9932. RuntimeError.Check.typeOf.object("result", result);
  9933. //>>includeEnd('debug');
  9934. const x = matrix[0] * cartesian.x + matrix[2] * cartesian.y;
  9935. const y = matrix[1] * cartesian.x + matrix[3] * cartesian.y;
  9936. result.x = x;
  9937. result.y = y;
  9938. return result;
  9939. };
  9940. /**
  9941. * Computes the product of a matrix and a scalar.
  9942. *
  9943. * @param {Matrix2} matrix The matrix.
  9944. * @param {Number} scalar The number to multiply by.
  9945. * @param {Matrix2} result The object onto which to store the result.
  9946. * @returns {Matrix2} The modified result parameter.
  9947. */
  9948. Matrix2.multiplyByScalar = function (matrix, scalar, result) {
  9949. //>>includeStart('debug', pragmas.debug);
  9950. RuntimeError.Check.typeOf.object("matrix", matrix);
  9951. RuntimeError.Check.typeOf.number("scalar", scalar);
  9952. RuntimeError.Check.typeOf.object("result", result);
  9953. //>>includeEnd('debug');
  9954. result[0] = matrix[0] * scalar;
  9955. result[1] = matrix[1] * scalar;
  9956. result[2] = matrix[2] * scalar;
  9957. result[3] = matrix[3] * scalar;
  9958. return result;
  9959. };
  9960. /**
  9961. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  9962. *
  9963. * @param {Matrix2} matrix The matrix on the left-hand side.
  9964. * @param {Number} scale The non-uniform scale on the right-hand side.
  9965. * @param {Matrix2} result The object onto which to store the result.
  9966. * @returns {Matrix2} The modified result parameter.
  9967. *
  9968. *
  9969. * @example
  9970. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
  9971. * Cesium.Matrix2.multiplyByScale(m, scale, m);
  9972. *
  9973. * @see Matrix2.multiplyByUniformScale
  9974. * @see Matrix2.fromScale
  9975. * @see Matrix2.fromUniformScale
  9976. * @see Matrix2.setScale
  9977. * @see Matrix2.setUniformScale
  9978. * @see Matrix2.getScale
  9979. */
  9980. Matrix2.multiplyByScale = function (matrix, scale, result) {
  9981. //>>includeStart('debug', pragmas.debug);
  9982. RuntimeError.Check.typeOf.object("matrix", matrix);
  9983. RuntimeError.Check.typeOf.object("scale", scale);
  9984. RuntimeError.Check.typeOf.object("result", result);
  9985. //>>includeEnd('debug');
  9986. result[0] = matrix[0] * scale.x;
  9987. result[1] = matrix[1] * scale.x;
  9988. result[2] = matrix[2] * scale.y;
  9989. result[3] = matrix[3] * scale.y;
  9990. return result;
  9991. };
  9992. /**
  9993. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  9994. *
  9995. * @param {Matrix2} matrix The matrix on the left-hand side.
  9996. * @param {Number} scale The uniform scale on the right-hand side.
  9997. * @param {Matrix2} result The object onto which to store the result.
  9998. * @returns {Matrix2} The modified result parameter.
  9999. *
  10000. * @example
  10001. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromUniformScale(scale), m);
  10002. * Cesium.Matrix2.multiplyByUniformScale(m, scale, m);
  10003. *
  10004. * @see Matrix2.multiplyByScale
  10005. * @see Matrix2.fromScale
  10006. * @see Matrix2.fromUniformScale
  10007. * @see Matrix2.setScale
  10008. * @see Matrix2.setUniformScale
  10009. * @see Matrix2.getScale
  10010. */
  10011. Matrix2.multiplyByUniformScale = function (matrix, scale, result) {
  10012. //>>includeStart('debug', pragmas.debug);
  10013. RuntimeError.Check.typeOf.object("matrix", matrix);
  10014. RuntimeError.Check.typeOf.number("scale", scale);
  10015. RuntimeError.Check.typeOf.object("result", result);
  10016. //>>includeEnd('debug');
  10017. result[0] = matrix[0] * scale;
  10018. result[1] = matrix[1] * scale;
  10019. result[2] = matrix[2] * scale;
  10020. result[3] = matrix[3] * scale;
  10021. return result;
  10022. };
  10023. /**
  10024. * Creates a negated copy of the provided matrix.
  10025. *
  10026. * @param {Matrix2} matrix The matrix to negate.
  10027. * @param {Matrix2} result The object onto which to store the result.
  10028. * @returns {Matrix2} The modified result parameter.
  10029. */
  10030. Matrix2.negate = function (matrix, result) {
  10031. //>>includeStart('debug', pragmas.debug);
  10032. RuntimeError.Check.typeOf.object("matrix", matrix);
  10033. RuntimeError.Check.typeOf.object("result", result);
  10034. //>>includeEnd('debug');
  10035. result[0] = -matrix[0];
  10036. result[1] = -matrix[1];
  10037. result[2] = -matrix[2];
  10038. result[3] = -matrix[3];
  10039. return result;
  10040. };
  10041. /**
  10042. * Computes the transpose of the provided matrix.
  10043. *
  10044. * @param {Matrix2} matrix The matrix to transpose.
  10045. * @param {Matrix2} result The object onto which to store the result.
  10046. * @returns {Matrix2} The modified result parameter.
  10047. */
  10048. Matrix2.transpose = function (matrix, result) {
  10049. //>>includeStart('debug', pragmas.debug);
  10050. RuntimeError.Check.typeOf.object("matrix", matrix);
  10051. RuntimeError.Check.typeOf.object("result", result);
  10052. //>>includeEnd('debug');
  10053. const column0Row0 = matrix[0];
  10054. const column0Row1 = matrix[2];
  10055. const column1Row0 = matrix[1];
  10056. const column1Row1 = matrix[3];
  10057. result[0] = column0Row0;
  10058. result[1] = column0Row1;
  10059. result[2] = column1Row0;
  10060. result[3] = column1Row1;
  10061. return result;
  10062. };
  10063. /**
  10064. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  10065. *
  10066. * @param {Matrix2} matrix The matrix with signed elements.
  10067. * @param {Matrix2} result The object onto which to store the result.
  10068. * @returns {Matrix2} The modified result parameter.
  10069. */
  10070. Matrix2.abs = function (matrix, result) {
  10071. //>>includeStart('debug', pragmas.debug);
  10072. RuntimeError.Check.typeOf.object("matrix", matrix);
  10073. RuntimeError.Check.typeOf.object("result", result);
  10074. //>>includeEnd('debug');
  10075. result[0] = Math.abs(matrix[0]);
  10076. result[1] = Math.abs(matrix[1]);
  10077. result[2] = Math.abs(matrix[2]);
  10078. result[3] = Math.abs(matrix[3]);
  10079. return result;
  10080. };
  10081. /**
  10082. * Compares the provided matrices componentwise and returns
  10083. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10084. *
  10085. * @param {Matrix2} [left] The first matrix.
  10086. * @param {Matrix2} [right] The second matrix.
  10087. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  10088. */
  10089. Matrix2.equals = function (left, right) {
  10090. return (
  10091. left === right ||
  10092. (defaultValue.defined(left) &&
  10093. defaultValue.defined(right) &&
  10094. left[0] === right[0] &&
  10095. left[1] === right[1] &&
  10096. left[2] === right[2] &&
  10097. left[3] === right[3])
  10098. );
  10099. };
  10100. /**
  10101. * @private
  10102. */
  10103. Matrix2.equalsArray = function (matrix, array, offset) {
  10104. return (
  10105. matrix[0] === array[offset] &&
  10106. matrix[1] === array[offset + 1] &&
  10107. matrix[2] === array[offset + 2] &&
  10108. matrix[3] === array[offset + 3]
  10109. );
  10110. };
  10111. /**
  10112. * Compares the provided matrices componentwise and returns
  10113. * <code>true</code> if they are within the provided epsilon,
  10114. * <code>false</code> otherwise.
  10115. *
  10116. * @param {Matrix2} [left] The first matrix.
  10117. * @param {Matrix2} [right] The second matrix.
  10118. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  10119. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  10120. */
  10121. Matrix2.equalsEpsilon = function (left, right, epsilon) {
  10122. epsilon = defaultValue.defaultValue(epsilon, 0);
  10123. return (
  10124. left === right ||
  10125. (defaultValue.defined(left) &&
  10126. defaultValue.defined(right) &&
  10127. Math.abs(left[0] - right[0]) <= epsilon &&
  10128. Math.abs(left[1] - right[1]) <= epsilon &&
  10129. Math.abs(left[2] - right[2]) <= epsilon &&
  10130. Math.abs(left[3] - right[3]) <= epsilon)
  10131. );
  10132. };
  10133. /**
  10134. * An immutable Matrix2 instance initialized to the identity matrix.
  10135. *
  10136. * @type {Matrix2}
  10137. * @constant
  10138. */
  10139. Matrix2.IDENTITY = Object.freeze(new Matrix2(1.0, 0.0, 0.0, 1.0));
  10140. /**
  10141. * An immutable Matrix2 instance initialized to the zero matrix.
  10142. *
  10143. * @type {Matrix2}
  10144. * @constant
  10145. */
  10146. Matrix2.ZERO = Object.freeze(new Matrix2(0.0, 0.0, 0.0, 0.0));
  10147. /**
  10148. * The index into Matrix2 for column 0, row 0.
  10149. *
  10150. * @type {Number}
  10151. * @constant
  10152. *
  10153. * @example
  10154. * const matrix = new Cesium.Matrix2();
  10155. * matrix[Cesium.Matrix2.COLUMN0ROW0] = 5.0; // set column 0, row 0 to 5.0
  10156. */
  10157. Matrix2.COLUMN0ROW0 = 0;
  10158. /**
  10159. * The index into Matrix2 for column 0, row 1.
  10160. *
  10161. * @type {Number}
  10162. * @constant
  10163. *
  10164. * @example
  10165. * const matrix = new Cesium.Matrix2();
  10166. * matrix[Cesium.Matrix2.COLUMN0ROW1] = 5.0; // set column 0, row 1 to 5.0
  10167. */
  10168. Matrix2.COLUMN0ROW1 = 1;
  10169. /**
  10170. * The index into Matrix2 for column 1, row 0.
  10171. *
  10172. * @type {Number}
  10173. * @constant
  10174. *
  10175. * @example
  10176. * const matrix = new Cesium.Matrix2();
  10177. * matrix[Cesium.Matrix2.COLUMN1ROW0] = 5.0; // set column 1, row 0 to 5.0
  10178. */
  10179. Matrix2.COLUMN1ROW0 = 2;
  10180. /**
  10181. * The index into Matrix2 for column 1, row 1.
  10182. *
  10183. * @type {Number}
  10184. * @constant
  10185. *
  10186. * @example
  10187. * const matrix = new Cesium.Matrix2();
  10188. * matrix[Cesium.Matrix2.COLUMN1ROW1] = 5.0; // set column 1, row 1 to 5.0
  10189. */
  10190. Matrix2.COLUMN1ROW1 = 3;
  10191. Object.defineProperties(Matrix2.prototype, {
  10192. /**
  10193. * Gets the number of items in the collection.
  10194. * @memberof Matrix2.prototype
  10195. *
  10196. * @type {Number}
  10197. */
  10198. length: {
  10199. get: function () {
  10200. return Matrix2.packedLength;
  10201. },
  10202. },
  10203. });
  10204. /**
  10205. * Duplicates the provided Matrix2 instance.
  10206. *
  10207. * @param {Matrix2} [result] The object onto which to store the result.
  10208. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  10209. */
  10210. Matrix2.prototype.clone = function (result) {
  10211. return Matrix2.clone(this, result);
  10212. };
  10213. /**
  10214. * Compares this matrix to the provided matrix componentwise and returns
  10215. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10216. *
  10217. * @param {Matrix2} [right] The right hand side matrix.
  10218. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  10219. */
  10220. Matrix2.prototype.equals = function (right) {
  10221. return Matrix2.equals(this, right);
  10222. };
  10223. /**
  10224. * Compares this matrix to the provided matrix componentwise and returns
  10225. * <code>true</code> if they are within the provided epsilon,
  10226. * <code>false</code> otherwise.
  10227. *
  10228. * @param {Matrix2} [right] The right hand side matrix.
  10229. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  10230. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  10231. */
  10232. Matrix2.prototype.equalsEpsilon = function (right, epsilon) {
  10233. return Matrix2.equalsEpsilon(this, right, epsilon);
  10234. };
  10235. /**
  10236. * Creates a string representing this Matrix with each row being
  10237. * on a separate line and in the format '(column0, column1)'.
  10238. *
  10239. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1)'.
  10240. */
  10241. Matrix2.prototype.toString = function () {
  10242. return `(${this[0]}, ${this[2]})\n` + `(${this[1]}, ${this[3]})`;
  10243. };
  10244. exports.Cartesian2 = Cartesian2;
  10245. exports.Cartesian3 = Cartesian3;
  10246. exports.Cartesian4 = Cartesian4;
  10247. exports.Cartographic = Cartographic;
  10248. exports.Ellipsoid = Ellipsoid;
  10249. exports.Matrix2 = Matrix2;
  10250. exports.Matrix3 = Matrix3;
  10251. exports.Matrix4 = Matrix4;
  10252. exports.Rectangle = Rectangle;
  10253. }));
  10254. //# sourceMappingURL=Matrix2-d35cf4b5.js.map