Matrix2-fc7e9822.js 378 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './RuntimeError-c581ca93', './defaultValue-94c3e563', './ComponentDatatype-4a60b8d6'], (function (exports, RuntimeError, defaultValue, ComponentDatatype) { 'use strict';
  3. /**
  4. * A 3D Cartesian point.
  5. * @alias Cartesian3
  6. * @constructor
  7. *
  8. * @param {Number} [x=0.0] The X component.
  9. * @param {Number} [y=0.0] The Y component.
  10. * @param {Number} [z=0.0] The Z component.
  11. *
  12. * @see Cartesian2
  13. * @see Cartesian4
  14. * @see Packable
  15. */
  16. function Cartesian3(x, y, z) {
  17. /**
  18. * The X component.
  19. * @type {Number}
  20. * @default 0.0
  21. */
  22. this.x = defaultValue.defaultValue(x, 0.0);
  23. /**
  24. * The Y component.
  25. * @type {Number}
  26. * @default 0.0
  27. */
  28. this.y = defaultValue.defaultValue(y, 0.0);
  29. /**
  30. * The Z component.
  31. * @type {Number}
  32. * @default 0.0
  33. */
  34. this.z = defaultValue.defaultValue(z, 0.0);
  35. }
  36. /**
  37. * Converts the provided Spherical into Cartesian3 coordinates.
  38. *
  39. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  40. * @param {Cartesian3} [result] The object onto which to store the result.
  41. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  42. */
  43. Cartesian3.fromSpherical = function (spherical, result) {
  44. //>>includeStart('debug', pragmas.debug);
  45. RuntimeError.Check.typeOf.object("spherical", spherical);
  46. //>>includeEnd('debug');
  47. if (!defaultValue.defined(result)) {
  48. result = new Cartesian3();
  49. }
  50. const clock = spherical.clock;
  51. const cone = spherical.cone;
  52. const magnitude = defaultValue.defaultValue(spherical.magnitude, 1.0);
  53. const radial = magnitude * Math.sin(cone);
  54. result.x = radial * Math.cos(clock);
  55. result.y = radial * Math.sin(clock);
  56. result.z = magnitude * Math.cos(cone);
  57. return result;
  58. };
  59. /**
  60. * Creates a Cartesian3 instance from x, y and z coordinates.
  61. *
  62. * @param {Number} x The x coordinate.
  63. * @param {Number} y The y coordinate.
  64. * @param {Number} z The z coordinate.
  65. * @param {Cartesian3} [result] The object onto which to store the result.
  66. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  67. */
  68. Cartesian3.fromElements = function (x, y, z, result) {
  69. if (!defaultValue.defined(result)) {
  70. return new Cartesian3(x, y, z);
  71. }
  72. result.x = x;
  73. result.y = y;
  74. result.z = z;
  75. return result;
  76. };
  77. /**
  78. * Duplicates a Cartesian3 instance.
  79. *
  80. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  81. * @param {Cartesian3} [result] The object onto which to store the result.
  82. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  83. */
  84. Cartesian3.clone = function (cartesian, result) {
  85. if (!defaultValue.defined(cartesian)) {
  86. return undefined;
  87. }
  88. if (!defaultValue.defined(result)) {
  89. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  90. }
  91. result.x = cartesian.x;
  92. result.y = cartesian.y;
  93. result.z = cartesian.z;
  94. return result;
  95. };
  96. /**
  97. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  98. * x, y, and z properties of the Cartesian4 and drops w.
  99. * @function
  100. *
  101. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  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.
  104. */
  105. Cartesian3.fromCartesian4 = Cartesian3.clone;
  106. /**
  107. * The number of elements used to pack the object into an array.
  108. * @type {Number}
  109. */
  110. Cartesian3.packedLength = 3;
  111. /**
  112. * Stores the provided instance into the provided array.
  113. *
  114. * @param {Cartesian3} value The value to pack.
  115. * @param {Number[]} array The array to pack into.
  116. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  117. *
  118. * @returns {Number[]} The array that was packed into
  119. */
  120. Cartesian3.pack = function (value, array, startingIndex) {
  121. //>>includeStart('debug', pragmas.debug);
  122. RuntimeError.Check.typeOf.object("value", value);
  123. RuntimeError.Check.defined("array", array);
  124. //>>includeEnd('debug');
  125. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  126. array[startingIndex++] = value.x;
  127. array[startingIndex++] = value.y;
  128. array[startingIndex] = value.z;
  129. return array;
  130. };
  131. /**
  132. * Retrieves an instance from a packed array.
  133. *
  134. * @param {Number[]} array The packed array.
  135. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  136. * @param {Cartesian3} [result] The object into which to store the result.
  137. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  138. */
  139. Cartesian3.unpack = function (array, startingIndex, result) {
  140. //>>includeStart('debug', pragmas.debug);
  141. RuntimeError.Check.defined("array", array);
  142. //>>includeEnd('debug');
  143. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  144. if (!defaultValue.defined(result)) {
  145. result = new Cartesian3();
  146. }
  147. result.x = array[startingIndex++];
  148. result.y = array[startingIndex++];
  149. result.z = array[startingIndex];
  150. return result;
  151. };
  152. /**
  153. * Flattens an array of Cartesian3s into an array of components.
  154. *
  155. * @param {Cartesian3[]} array The array of cartesians to pack.
  156. * @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.
  157. * @returns {Number[]} The packed array.
  158. */
  159. Cartesian3.packArray = function (array, result) {
  160. //>>includeStart('debug', pragmas.debug);
  161. RuntimeError.Check.defined("array", array);
  162. //>>includeEnd('debug');
  163. const length = array.length;
  164. const resultLength = length * 3;
  165. if (!defaultValue.defined(result)) {
  166. result = new Array(resultLength);
  167. } else if (!Array.isArray(result) && result.length !== resultLength) {
  168. //>>includeStart('debug', pragmas.debug);
  169. throw new RuntimeError.DeveloperError(
  170. "If result is a typed array, it must have exactly array.length * 3 elements"
  171. );
  172. //>>includeEnd('debug');
  173. } else if (result.length !== resultLength) {
  174. result.length = resultLength;
  175. }
  176. for (let i = 0; i < length; ++i) {
  177. Cartesian3.pack(array[i], result, i * 3);
  178. }
  179. return result;
  180. };
  181. /**
  182. * Unpacks an array of cartesian components into an array of Cartesian3s.
  183. *
  184. * @param {Number[]} array The array of components to unpack.
  185. * @param {Cartesian3[]} [result] The array onto which to store the result.
  186. * @returns {Cartesian3[]} The unpacked array.
  187. */
  188. Cartesian3.unpackArray = function (array, result) {
  189. //>>includeStart('debug', pragmas.debug);
  190. RuntimeError.Check.defined("array", array);
  191. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 3);
  192. if (array.length % 3 !== 0) {
  193. throw new RuntimeError.DeveloperError("array length must be a multiple of 3.");
  194. }
  195. //>>includeEnd('debug');
  196. const length = array.length;
  197. if (!defaultValue.defined(result)) {
  198. result = new Array(length / 3);
  199. } else {
  200. result.length = length / 3;
  201. }
  202. for (let i = 0; i < length; i += 3) {
  203. const index = i / 3;
  204. result[index] = Cartesian3.unpack(array, i, result[index]);
  205. }
  206. return result;
  207. };
  208. /**
  209. * Creates a Cartesian3 from three consecutive elements in an array.
  210. * @function
  211. *
  212. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  213. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  214. * @param {Cartesian3} [result] The object onto which to store the result.
  215. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  216. *
  217. * @example
  218. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  219. * const v = [1.0, 2.0, 3.0];
  220. * const p = Cesium.Cartesian3.fromArray(v);
  221. *
  222. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  223. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  224. * const p2 = Cesium.Cartesian3.fromArray(v2, 2);
  225. */
  226. Cartesian3.fromArray = Cartesian3.unpack;
  227. /**
  228. * Computes the value of the maximum component for the supplied Cartesian.
  229. *
  230. * @param {Cartesian3} cartesian The cartesian to use.
  231. * @returns {Number} The value of the maximum component.
  232. */
  233. Cartesian3.maximumComponent = function (cartesian) {
  234. //>>includeStart('debug', pragmas.debug);
  235. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  236. //>>includeEnd('debug');
  237. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  238. };
  239. /**
  240. * Computes the value of the minimum component for the supplied Cartesian.
  241. *
  242. * @param {Cartesian3} cartesian The cartesian to use.
  243. * @returns {Number} The value of the minimum component.
  244. */
  245. Cartesian3.minimumComponent = function (cartesian) {
  246. //>>includeStart('debug', pragmas.debug);
  247. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  248. //>>includeEnd('debug');
  249. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  250. };
  251. /**
  252. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  253. *
  254. * @param {Cartesian3} first A cartesian to compare.
  255. * @param {Cartesian3} second A cartesian to compare.
  256. * @param {Cartesian3} result The object into which to store the result.
  257. * @returns {Cartesian3} A cartesian with the minimum components.
  258. */
  259. Cartesian3.minimumByComponent = function (first, second, result) {
  260. //>>includeStart('debug', pragmas.debug);
  261. RuntimeError.Check.typeOf.object("first", first);
  262. RuntimeError.Check.typeOf.object("second", second);
  263. RuntimeError.Check.typeOf.object("result", result);
  264. //>>includeEnd('debug');
  265. result.x = Math.min(first.x, second.x);
  266. result.y = Math.min(first.y, second.y);
  267. result.z = Math.min(first.z, second.z);
  268. return result;
  269. };
  270. /**
  271. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  272. *
  273. * @param {Cartesian3} first A cartesian to compare.
  274. * @param {Cartesian3} second A cartesian to compare.
  275. * @param {Cartesian3} result The object into which to store the result.
  276. * @returns {Cartesian3} A cartesian with the maximum components.
  277. */
  278. Cartesian3.maximumByComponent = function (first, second, result) {
  279. //>>includeStart('debug', pragmas.debug);
  280. RuntimeError.Check.typeOf.object("first", first);
  281. RuntimeError.Check.typeOf.object("second", second);
  282. RuntimeError.Check.typeOf.object("result", result);
  283. //>>includeEnd('debug');
  284. result.x = Math.max(first.x, second.x);
  285. result.y = Math.max(first.y, second.y);
  286. result.z = Math.max(first.z, second.z);
  287. return result;
  288. };
  289. /**
  290. * Constrain a value to lie between two values.
  291. *
  292. * @param {Cartesian3} cartesian The value to clamp.
  293. * @param {Cartesian3} min The minimum bound.
  294. * @param {Cartesian3} max The maximum bound.
  295. * @param {Cartesian3} result The object into which to store the result.
  296. * @returns {Cartesian3} The clamped value such that min <= value <= max.
  297. */
  298. Cartesian3.clamp = function (value, min, max, result) {
  299. //>>includeStart('debug', pragmas.debug);
  300. RuntimeError.Check.typeOf.object("value", value);
  301. RuntimeError.Check.typeOf.object("min", min);
  302. RuntimeError.Check.typeOf.object("max", max);
  303. RuntimeError.Check.typeOf.object("result", result);
  304. //>>includeEnd('debug');
  305. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  306. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  307. const z = ComponentDatatype.CesiumMath.clamp(value.z, min.z, max.z);
  308. result.x = x;
  309. result.y = y;
  310. result.z = z;
  311. return result;
  312. };
  313. /**
  314. * Computes the provided Cartesian's squared magnitude.
  315. *
  316. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  317. * @returns {Number} The squared magnitude.
  318. */
  319. Cartesian3.magnitudeSquared = function (cartesian) {
  320. //>>includeStart('debug', pragmas.debug);
  321. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  322. //>>includeEnd('debug');
  323. return (
  324. cartesian.x * cartesian.x +
  325. cartesian.y * cartesian.y +
  326. cartesian.z * cartesian.z
  327. );
  328. };
  329. /**
  330. * Computes the Cartesian's magnitude (length).
  331. *
  332. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  333. * @returns {Number} The magnitude.
  334. */
  335. Cartesian3.magnitude = function (cartesian) {
  336. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  337. };
  338. const distanceScratch$2 = new Cartesian3();
  339. /**
  340. * Computes the distance between two points.
  341. *
  342. * @param {Cartesian3} left The first point to compute the distance from.
  343. * @param {Cartesian3} right The second point to compute the distance to.
  344. * @returns {Number} The distance between two points.
  345. *
  346. * @example
  347. * // Returns 1.0
  348. * const d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  349. */
  350. Cartesian3.distance = function (left, right) {
  351. //>>includeStart('debug', pragmas.debug);
  352. RuntimeError.Check.typeOf.object("left", left);
  353. RuntimeError.Check.typeOf.object("right", right);
  354. //>>includeEnd('debug');
  355. Cartesian3.subtract(left, right, distanceScratch$2);
  356. return Cartesian3.magnitude(distanceScratch$2);
  357. };
  358. /**
  359. * Computes the squared distance between two points. Comparing squared distances
  360. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  361. *
  362. * @param {Cartesian3} left The first point to compute the distance from.
  363. * @param {Cartesian3} right The second point to compute the distance to.
  364. * @returns {Number} The distance between two points.
  365. *
  366. * @example
  367. * // Returns 4.0, not 2.0
  368. * const d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  369. */
  370. Cartesian3.distanceSquared = function (left, right) {
  371. //>>includeStart('debug', pragmas.debug);
  372. RuntimeError.Check.typeOf.object("left", left);
  373. RuntimeError.Check.typeOf.object("right", right);
  374. //>>includeEnd('debug');
  375. Cartesian3.subtract(left, right, distanceScratch$2);
  376. return Cartesian3.magnitudeSquared(distanceScratch$2);
  377. };
  378. /**
  379. * Computes the normalized form of the supplied Cartesian.
  380. *
  381. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  382. * @param {Cartesian3} result The object onto which to store the result.
  383. * @returns {Cartesian3} The modified result parameter.
  384. */
  385. Cartesian3.normalize = function (cartesian, result) {
  386. //>>includeStart('debug', pragmas.debug);
  387. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  388. RuntimeError.Check.typeOf.object("result", result);
  389. //>>includeEnd('debug');
  390. const magnitude = Cartesian3.magnitude(cartesian);
  391. result.x = cartesian.x / magnitude;
  392. result.y = cartesian.y / magnitude;
  393. result.z = cartesian.z / magnitude;
  394. //>>includeStart('debug', pragmas.debug);
  395. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  396. throw new RuntimeError.DeveloperError("normalized result is not a number");
  397. }
  398. //>>includeEnd('debug');
  399. return result;
  400. };
  401. /**
  402. * Computes the dot (scalar) product of two Cartesians.
  403. *
  404. * @param {Cartesian3} left The first Cartesian.
  405. * @param {Cartesian3} right The second Cartesian.
  406. * @returns {Number} The dot product.
  407. */
  408. Cartesian3.dot = function (left, right) {
  409. //>>includeStart('debug', pragmas.debug);
  410. RuntimeError.Check.typeOf.object("left", left);
  411. RuntimeError.Check.typeOf.object("right", right);
  412. //>>includeEnd('debug');
  413. return left.x * right.x + left.y * right.y + left.z * right.z;
  414. };
  415. /**
  416. * Computes the componentwise product of two Cartesians.
  417. *
  418. * @param {Cartesian3} left The first Cartesian.
  419. * @param {Cartesian3} right The second Cartesian.
  420. * @param {Cartesian3} result The object onto which to store the result.
  421. * @returns {Cartesian3} The modified result parameter.
  422. */
  423. Cartesian3.multiplyComponents = function (left, right, result) {
  424. //>>includeStart('debug', pragmas.debug);
  425. RuntimeError.Check.typeOf.object("left", left);
  426. RuntimeError.Check.typeOf.object("right", right);
  427. RuntimeError.Check.typeOf.object("result", result);
  428. //>>includeEnd('debug');
  429. result.x = left.x * right.x;
  430. result.y = left.y * right.y;
  431. result.z = left.z * right.z;
  432. return result;
  433. };
  434. /**
  435. * Computes the componentwise quotient of two Cartesians.
  436. *
  437. * @param {Cartesian3} left The first Cartesian.
  438. * @param {Cartesian3} right The second Cartesian.
  439. * @param {Cartesian3} result The object onto which to store the result.
  440. * @returns {Cartesian3} The modified result parameter.
  441. */
  442. Cartesian3.divideComponents = function (left, right, result) {
  443. //>>includeStart('debug', pragmas.debug);
  444. RuntimeError.Check.typeOf.object("left", left);
  445. RuntimeError.Check.typeOf.object("right", right);
  446. RuntimeError.Check.typeOf.object("result", result);
  447. //>>includeEnd('debug');
  448. result.x = left.x / right.x;
  449. result.y = left.y / right.y;
  450. result.z = left.z / right.z;
  451. return result;
  452. };
  453. /**
  454. * Computes the componentwise sum of two Cartesians.
  455. *
  456. * @param {Cartesian3} left The first Cartesian.
  457. * @param {Cartesian3} right The second Cartesian.
  458. * @param {Cartesian3} result The object onto which to store the result.
  459. * @returns {Cartesian3} The modified result parameter.
  460. */
  461. Cartesian3.add = function (left, right, result) {
  462. //>>includeStart('debug', pragmas.debug);
  463. RuntimeError.Check.typeOf.object("left", left);
  464. RuntimeError.Check.typeOf.object("right", right);
  465. RuntimeError.Check.typeOf.object("result", result);
  466. //>>includeEnd('debug');
  467. result.x = left.x + right.x;
  468. result.y = left.y + right.y;
  469. result.z = left.z + right.z;
  470. return result;
  471. };
  472. /**
  473. * Computes the componentwise difference of two Cartesians.
  474. *
  475. * @param {Cartesian3} left The first Cartesian.
  476. * @param {Cartesian3} right The second Cartesian.
  477. * @param {Cartesian3} result The object onto which to store the result.
  478. * @returns {Cartesian3} The modified result parameter.
  479. */
  480. Cartesian3.subtract = function (left, right, result) {
  481. //>>includeStart('debug', pragmas.debug);
  482. RuntimeError.Check.typeOf.object("left", left);
  483. RuntimeError.Check.typeOf.object("right", right);
  484. RuntimeError.Check.typeOf.object("result", result);
  485. //>>includeEnd('debug');
  486. result.x = left.x - right.x;
  487. result.y = left.y - right.y;
  488. result.z = left.z - right.z;
  489. return result;
  490. };
  491. /**
  492. * Multiplies the provided Cartesian componentwise by the provided scalar.
  493. *
  494. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  495. * @param {Number} scalar The scalar to multiply with.
  496. * @param {Cartesian3} result The object onto which to store the result.
  497. * @returns {Cartesian3} The modified result parameter.
  498. */
  499. Cartesian3.multiplyByScalar = function (cartesian, scalar, result) {
  500. //>>includeStart('debug', pragmas.debug);
  501. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  502. RuntimeError.Check.typeOf.number("scalar", scalar);
  503. RuntimeError.Check.typeOf.object("result", result);
  504. //>>includeEnd('debug');
  505. result.x = cartesian.x * scalar;
  506. result.y = cartesian.y * scalar;
  507. result.z = cartesian.z * scalar;
  508. return result;
  509. };
  510. /**
  511. * Divides the provided Cartesian componentwise by the provided scalar.
  512. *
  513. * @param {Cartesian3} cartesian The Cartesian to be divided.
  514. * @param {Number} scalar The scalar to divide by.
  515. * @param {Cartesian3} result The object onto which to store the result.
  516. * @returns {Cartesian3} The modified result parameter.
  517. */
  518. Cartesian3.divideByScalar = function (cartesian, scalar, result) {
  519. //>>includeStart('debug', pragmas.debug);
  520. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  521. RuntimeError.Check.typeOf.number("scalar", scalar);
  522. RuntimeError.Check.typeOf.object("result", result);
  523. //>>includeEnd('debug');
  524. result.x = cartesian.x / scalar;
  525. result.y = cartesian.y / scalar;
  526. result.z = cartesian.z / scalar;
  527. return result;
  528. };
  529. /**
  530. * Negates the provided Cartesian.
  531. *
  532. * @param {Cartesian3} cartesian The Cartesian to be negated.
  533. * @param {Cartesian3} result The object onto which to store the result.
  534. * @returns {Cartesian3} The modified result parameter.
  535. */
  536. Cartesian3.negate = function (cartesian, result) {
  537. //>>includeStart('debug', pragmas.debug);
  538. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  539. RuntimeError.Check.typeOf.object("result", result);
  540. //>>includeEnd('debug');
  541. result.x = -cartesian.x;
  542. result.y = -cartesian.y;
  543. result.z = -cartesian.z;
  544. return result;
  545. };
  546. /**
  547. * Computes the absolute value of the provided Cartesian.
  548. *
  549. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  550. * @param {Cartesian3} result The object onto which to store the result.
  551. * @returns {Cartesian3} The modified result parameter.
  552. */
  553. Cartesian3.abs = function (cartesian, result) {
  554. //>>includeStart('debug', pragmas.debug);
  555. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  556. RuntimeError.Check.typeOf.object("result", result);
  557. //>>includeEnd('debug');
  558. result.x = Math.abs(cartesian.x);
  559. result.y = Math.abs(cartesian.y);
  560. result.z = Math.abs(cartesian.z);
  561. return result;
  562. };
  563. const lerpScratch$2 = new Cartesian3();
  564. /**
  565. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  566. *
  567. * @param {Cartesian3} start The value corresponding to t at 0.0.
  568. * @param {Cartesian3} end The value corresponding to t at 1.0.
  569. * @param {Number} t The point along t at which to interpolate.
  570. * @param {Cartesian3} result The object onto which to store the result.
  571. * @returns {Cartesian3} The modified result parameter.
  572. */
  573. Cartesian3.lerp = function (start, end, t, result) {
  574. //>>includeStart('debug', pragmas.debug);
  575. RuntimeError.Check.typeOf.object("start", start);
  576. RuntimeError.Check.typeOf.object("end", end);
  577. RuntimeError.Check.typeOf.number("t", t);
  578. RuntimeError.Check.typeOf.object("result", result);
  579. //>>includeEnd('debug');
  580. Cartesian3.multiplyByScalar(end, t, lerpScratch$2);
  581. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  582. return Cartesian3.add(lerpScratch$2, result, result);
  583. };
  584. const angleBetweenScratch$1 = new Cartesian3();
  585. const angleBetweenScratch2$1 = new Cartesian3();
  586. /**
  587. * Returns the angle, in radians, between the provided Cartesians.
  588. *
  589. * @param {Cartesian3} left The first Cartesian.
  590. * @param {Cartesian3} right The second Cartesian.
  591. * @returns {Number} The angle between the Cartesians.
  592. */
  593. Cartesian3.angleBetween = function (left, right) {
  594. //>>includeStart('debug', pragmas.debug);
  595. RuntimeError.Check.typeOf.object("left", left);
  596. RuntimeError.Check.typeOf.object("right", right);
  597. //>>includeEnd('debug');
  598. Cartesian3.normalize(left, angleBetweenScratch$1);
  599. Cartesian3.normalize(right, angleBetweenScratch2$1);
  600. const cosine = Cartesian3.dot(angleBetweenScratch$1, angleBetweenScratch2$1);
  601. const sine = Cartesian3.magnitude(
  602. Cartesian3.cross(
  603. angleBetweenScratch$1,
  604. angleBetweenScratch2$1,
  605. angleBetweenScratch$1
  606. )
  607. );
  608. return Math.atan2(sine, cosine);
  609. };
  610. const mostOrthogonalAxisScratch$2 = new Cartesian3();
  611. /**
  612. * Returns the axis that is most orthogonal to the provided Cartesian.
  613. *
  614. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  615. * @param {Cartesian3} result The object onto which to store the result.
  616. * @returns {Cartesian3} The most orthogonal axis.
  617. */
  618. Cartesian3.mostOrthogonalAxis = function (cartesian, result) {
  619. //>>includeStart('debug', pragmas.debug);
  620. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  621. RuntimeError.Check.typeOf.object("result", result);
  622. //>>includeEnd('debug');
  623. const f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch$2);
  624. Cartesian3.abs(f, f);
  625. if (f.x <= f.y) {
  626. if (f.x <= f.z) {
  627. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  628. } else {
  629. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  630. }
  631. } else if (f.y <= f.z) {
  632. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  633. } else {
  634. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  635. }
  636. return result;
  637. };
  638. /**
  639. * Projects vector a onto vector b
  640. * @param {Cartesian3} a The vector that needs projecting
  641. * @param {Cartesian3} b The vector to project onto
  642. * @param {Cartesian3} result The result cartesian
  643. * @returns {Cartesian3} The modified result parameter
  644. */
  645. Cartesian3.projectVector = function (a, b, result) {
  646. //>>includeStart('debug', pragmas.debug);
  647. RuntimeError.Check.defined("a", a);
  648. RuntimeError.Check.defined("b", b);
  649. RuntimeError.Check.defined("result", result);
  650. //>>includeEnd('debug');
  651. const scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
  652. return Cartesian3.multiplyByScalar(b, scalar, result);
  653. };
  654. /**
  655. * Compares the provided Cartesians componentwise and returns
  656. * <code>true</code> if they are equal, <code>false</code> otherwise.
  657. *
  658. * @param {Cartesian3} [left] The first Cartesian.
  659. * @param {Cartesian3} [right] The second Cartesian.
  660. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  661. */
  662. Cartesian3.equals = function (left, right) {
  663. return (
  664. left === right ||
  665. (defaultValue.defined(left) &&
  666. defaultValue.defined(right) &&
  667. left.x === right.x &&
  668. left.y === right.y &&
  669. left.z === right.z)
  670. );
  671. };
  672. /**
  673. * @private
  674. */
  675. Cartesian3.equalsArray = function (cartesian, array, offset) {
  676. return (
  677. cartesian.x === array[offset] &&
  678. cartesian.y === array[offset + 1] &&
  679. cartesian.z === array[offset + 2]
  680. );
  681. };
  682. /**
  683. * Compares the provided Cartesians componentwise and returns
  684. * <code>true</code> if they pass an absolute or relative tolerance test,
  685. * <code>false</code> otherwise.
  686. *
  687. * @param {Cartesian3} [left] The first Cartesian.
  688. * @param {Cartesian3} [right] The second Cartesian.
  689. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  690. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  691. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  692. */
  693. Cartesian3.equalsEpsilon = function (
  694. left,
  695. right,
  696. relativeEpsilon,
  697. absoluteEpsilon
  698. ) {
  699. return (
  700. left === right ||
  701. (defaultValue.defined(left) &&
  702. defaultValue.defined(right) &&
  703. ComponentDatatype.CesiumMath.equalsEpsilon(
  704. left.x,
  705. right.x,
  706. relativeEpsilon,
  707. absoluteEpsilon
  708. ) &&
  709. ComponentDatatype.CesiumMath.equalsEpsilon(
  710. left.y,
  711. right.y,
  712. relativeEpsilon,
  713. absoluteEpsilon
  714. ) &&
  715. ComponentDatatype.CesiumMath.equalsEpsilon(
  716. left.z,
  717. right.z,
  718. relativeEpsilon,
  719. absoluteEpsilon
  720. ))
  721. );
  722. };
  723. /**
  724. * Computes the cross (outer) product of two Cartesians.
  725. *
  726. * @param {Cartesian3} left The first Cartesian.
  727. * @param {Cartesian3} right The second Cartesian.
  728. * @param {Cartesian3} result The object onto which to store the result.
  729. * @returns {Cartesian3} The cross product.
  730. */
  731. Cartesian3.cross = function (left, right, result) {
  732. //>>includeStart('debug', pragmas.debug);
  733. RuntimeError.Check.typeOf.object("left", left);
  734. RuntimeError.Check.typeOf.object("right", right);
  735. RuntimeError.Check.typeOf.object("result", result);
  736. //>>includeEnd('debug');
  737. const leftX = left.x;
  738. const leftY = left.y;
  739. const leftZ = left.z;
  740. const rightX = right.x;
  741. const rightY = right.y;
  742. const rightZ = right.z;
  743. const x = leftY * rightZ - leftZ * rightY;
  744. const y = leftZ * rightX - leftX * rightZ;
  745. const z = leftX * rightY - leftY * rightX;
  746. result.x = x;
  747. result.y = y;
  748. result.z = z;
  749. return result;
  750. };
  751. /**
  752. * Computes the midpoint between the right and left Cartesian.
  753. * @param {Cartesian3} left The first Cartesian.
  754. * @param {Cartesian3} right The second Cartesian.
  755. * @param {Cartesian3} result The object onto which to store the result.
  756. * @returns {Cartesian3} The midpoint.
  757. */
  758. Cartesian3.midpoint = function (left, right, result) {
  759. //>>includeStart('debug', pragmas.debug);
  760. RuntimeError.Check.typeOf.object("left", left);
  761. RuntimeError.Check.typeOf.object("right", right);
  762. RuntimeError.Check.typeOf.object("result", result);
  763. //>>includeEnd('debug');
  764. result.x = (left.x + right.x) * 0.5;
  765. result.y = (left.y + right.y) * 0.5;
  766. result.z = (left.z + right.z) * 0.5;
  767. return result;
  768. };
  769. /**
  770. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  771. *
  772. * @param {Number} longitude The longitude, in degrees
  773. * @param {Number} latitude The latitude, in degrees
  774. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  775. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  776. * @param {Cartesian3} [result] The object onto which to store the result.
  777. * @returns {Cartesian3} The position
  778. *
  779. * @example
  780. * const position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  781. */
  782. Cartesian3.fromDegrees = function (
  783. longitude,
  784. latitude,
  785. height,
  786. ellipsoid,
  787. result
  788. ) {
  789. //>>includeStart('debug', pragmas.debug);
  790. RuntimeError.Check.typeOf.number("longitude", longitude);
  791. RuntimeError.Check.typeOf.number("latitude", latitude);
  792. //>>includeEnd('debug');
  793. longitude = ComponentDatatype.CesiumMath.toRadians(longitude);
  794. latitude = ComponentDatatype.CesiumMath.toRadians(latitude);
  795. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  796. };
  797. let scratchN = new Cartesian3();
  798. let scratchK = new Cartesian3();
  799. const wgs84RadiiSquared = new Cartesian3(
  800. 6378137.0 * 6378137.0,
  801. 6378137.0 * 6378137.0,
  802. 6356752.3142451793 * 6356752.3142451793
  803. );
  804. /**
  805. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  806. *
  807. * @param {Number} longitude The longitude, in radians
  808. * @param {Number} latitude The latitude, in radians
  809. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  810. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  811. * @param {Cartesian3} [result] The object onto which to store the result.
  812. * @returns {Cartesian3} The position
  813. *
  814. * @example
  815. * const position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  816. */
  817. Cartesian3.fromRadians = function (
  818. longitude,
  819. latitude,
  820. height,
  821. ellipsoid,
  822. result
  823. ) {
  824. //>>includeStart('debug', pragmas.debug);
  825. RuntimeError.Check.typeOf.number("longitude", longitude);
  826. RuntimeError.Check.typeOf.number("latitude", latitude);
  827. //>>includeEnd('debug');
  828. height = defaultValue.defaultValue(height, 0.0);
  829. const radiiSquared = defaultValue.defined(ellipsoid)
  830. ? ellipsoid.radiiSquared
  831. : wgs84RadiiSquared;
  832. const cosLatitude = Math.cos(latitude);
  833. scratchN.x = cosLatitude * Math.cos(longitude);
  834. scratchN.y = cosLatitude * Math.sin(longitude);
  835. scratchN.z = Math.sin(latitude);
  836. scratchN = Cartesian3.normalize(scratchN, scratchN);
  837. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  838. const gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  839. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  840. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  841. if (!defaultValue.defined(result)) {
  842. result = new Cartesian3();
  843. }
  844. return Cartesian3.add(scratchK, scratchN, result);
  845. };
  846. /**
  847. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  848. *
  849. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  850. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  851. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  852. * @returns {Cartesian3[]} The array of positions.
  853. *
  854. * @example
  855. * const positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  856. */
  857. Cartesian3.fromDegreesArray = function (coordinates, ellipsoid, result) {
  858. //>>includeStart('debug', pragmas.debug);
  859. RuntimeError.Check.defined("coordinates", coordinates);
  860. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  861. throw new RuntimeError.DeveloperError(
  862. "the number of coordinates must be a multiple of 2 and at least 2"
  863. );
  864. }
  865. //>>includeEnd('debug');
  866. const length = coordinates.length;
  867. if (!defaultValue.defined(result)) {
  868. result = new Array(length / 2);
  869. } else {
  870. result.length = length / 2;
  871. }
  872. for (let i = 0; i < length; i += 2) {
  873. const longitude = coordinates[i];
  874. const latitude = coordinates[i + 1];
  875. const index = i / 2;
  876. result[index] = Cartesian3.fromDegrees(
  877. longitude,
  878. latitude,
  879. 0,
  880. ellipsoid,
  881. result[index]
  882. );
  883. }
  884. return result;
  885. };
  886. /**
  887. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  888. *
  889. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  890. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  891. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  892. * @returns {Cartesian3[]} The array of positions.
  893. *
  894. * @example
  895. * const positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  896. */
  897. Cartesian3.fromRadiansArray = function (coordinates, ellipsoid, result) {
  898. //>>includeStart('debug', pragmas.debug);
  899. RuntimeError.Check.defined("coordinates", coordinates);
  900. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  901. throw new RuntimeError.DeveloperError(
  902. "the number of coordinates must be a multiple of 2 and at least 2"
  903. );
  904. }
  905. //>>includeEnd('debug');
  906. const length = coordinates.length;
  907. if (!defaultValue.defined(result)) {
  908. result = new Array(length / 2);
  909. } else {
  910. result.length = length / 2;
  911. }
  912. for (let i = 0; i < length; i += 2) {
  913. const longitude = coordinates[i];
  914. const latitude = coordinates[i + 1];
  915. const index = i / 2;
  916. result[index] = Cartesian3.fromRadians(
  917. longitude,
  918. latitude,
  919. 0,
  920. ellipsoid,
  921. result[index]
  922. );
  923. }
  924. return result;
  925. };
  926. /**
  927. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  928. *
  929. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  930. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  931. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  932. * @returns {Cartesian3[]} The array of positions.
  933. *
  934. * @example
  935. * const positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  936. */
  937. Cartesian3.fromDegreesArrayHeights = function (coordinates, ellipsoid, result) {
  938. //>>includeStart('debug', pragmas.debug);
  939. RuntimeError.Check.defined("coordinates", coordinates);
  940. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  941. throw new RuntimeError.DeveloperError(
  942. "the number of coordinates must be a multiple of 3 and at least 3"
  943. );
  944. }
  945. //>>includeEnd('debug');
  946. const length = coordinates.length;
  947. if (!defaultValue.defined(result)) {
  948. result = new Array(length / 3);
  949. } else {
  950. result.length = length / 3;
  951. }
  952. for (let i = 0; i < length; i += 3) {
  953. const longitude = coordinates[i];
  954. const latitude = coordinates[i + 1];
  955. const height = coordinates[i + 2];
  956. const index = i / 3;
  957. result[index] = Cartesian3.fromDegrees(
  958. longitude,
  959. latitude,
  960. height,
  961. ellipsoid,
  962. result[index]
  963. );
  964. }
  965. return result;
  966. };
  967. /**
  968. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  969. *
  970. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  971. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  972. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  973. * @returns {Cartesian3[]} The array of positions.
  974. *
  975. * @example
  976. * const positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  977. */
  978. Cartesian3.fromRadiansArrayHeights = function (coordinates, ellipsoid, result) {
  979. //>>includeStart('debug', pragmas.debug);
  980. RuntimeError.Check.defined("coordinates", coordinates);
  981. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  982. throw new RuntimeError.DeveloperError(
  983. "the number of coordinates must be a multiple of 3 and at least 3"
  984. );
  985. }
  986. //>>includeEnd('debug');
  987. const length = coordinates.length;
  988. if (!defaultValue.defined(result)) {
  989. result = new Array(length / 3);
  990. } else {
  991. result.length = length / 3;
  992. }
  993. for (let i = 0; i < length; i += 3) {
  994. const longitude = coordinates[i];
  995. const latitude = coordinates[i + 1];
  996. const height = coordinates[i + 2];
  997. const index = i / 3;
  998. result[index] = Cartesian3.fromRadians(
  999. longitude,
  1000. latitude,
  1001. height,
  1002. ellipsoid,
  1003. result[index]
  1004. );
  1005. }
  1006. return result;
  1007. };
  1008. /**
  1009. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  1010. *
  1011. * @type {Cartesian3}
  1012. * @constant
  1013. */
  1014. Cartesian3.ZERO = Object.freeze(new Cartesian3(0.0, 0.0, 0.0));
  1015. /**
  1016. * An immutable Cartesian3 instance initialized to (1.0, 1.0, 1.0).
  1017. *
  1018. * @type {Cartesian3}
  1019. * @constant
  1020. */
  1021. Cartesian3.ONE = Object.freeze(new Cartesian3(1.0, 1.0, 1.0));
  1022. /**
  1023. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  1024. *
  1025. * @type {Cartesian3}
  1026. * @constant
  1027. */
  1028. Cartesian3.UNIT_X = Object.freeze(new Cartesian3(1.0, 0.0, 0.0));
  1029. /**
  1030. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  1031. *
  1032. * @type {Cartesian3}
  1033. * @constant
  1034. */
  1035. Cartesian3.UNIT_Y = Object.freeze(new Cartesian3(0.0, 1.0, 0.0));
  1036. /**
  1037. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  1038. *
  1039. * @type {Cartesian3}
  1040. * @constant
  1041. */
  1042. Cartesian3.UNIT_Z = Object.freeze(new Cartesian3(0.0, 0.0, 1.0));
  1043. /**
  1044. * Duplicates this Cartesian3 instance.
  1045. *
  1046. * @param {Cartesian3} [result] The object onto which to store the result.
  1047. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1048. */
  1049. Cartesian3.prototype.clone = function (result) {
  1050. return Cartesian3.clone(this, result);
  1051. };
  1052. /**
  1053. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1054. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1055. *
  1056. * @param {Cartesian3} [right] The right hand side Cartesian.
  1057. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1058. */
  1059. Cartesian3.prototype.equals = function (right) {
  1060. return Cartesian3.equals(this, right);
  1061. };
  1062. /**
  1063. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1064. * <code>true</code> if they pass an absolute or relative tolerance test,
  1065. * <code>false</code> otherwise.
  1066. *
  1067. * @param {Cartesian3} [right] The right hand side Cartesian.
  1068. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  1069. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1070. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1071. */
  1072. Cartesian3.prototype.equalsEpsilon = function (
  1073. right,
  1074. relativeEpsilon,
  1075. absoluteEpsilon
  1076. ) {
  1077. return Cartesian3.equalsEpsilon(
  1078. this,
  1079. right,
  1080. relativeEpsilon,
  1081. absoluteEpsilon
  1082. );
  1083. };
  1084. /**
  1085. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  1086. *
  1087. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  1088. */
  1089. Cartesian3.prototype.toString = function () {
  1090. return `(${this.x}, ${this.y}, ${this.z})`;
  1091. };
  1092. const scaleToGeodeticSurfaceIntersection = new Cartesian3();
  1093. const scaleToGeodeticSurfaceGradient = new Cartesian3();
  1094. /**
  1095. * Scales the provided Cartesian position along the geodetic surface normal
  1096. * so that it is on the surface of this ellipsoid. If the position is
  1097. * at the center of the ellipsoid, this function returns undefined.
  1098. *
  1099. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1100. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  1101. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  1102. * @param {Number} centerToleranceSquared Tolerance for closeness to the center.
  1103. * @param {Cartesian3} [result] The object onto which to store the result.
  1104. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1105. *
  1106. * @function scaleToGeodeticSurface
  1107. *
  1108. * @private
  1109. */
  1110. function scaleToGeodeticSurface(
  1111. cartesian,
  1112. oneOverRadii,
  1113. oneOverRadiiSquared,
  1114. centerToleranceSquared,
  1115. result
  1116. ) {
  1117. //>>includeStart('debug', pragmas.debug);
  1118. if (!defaultValue.defined(cartesian)) {
  1119. throw new RuntimeError.DeveloperError("cartesian is required.");
  1120. }
  1121. if (!defaultValue.defined(oneOverRadii)) {
  1122. throw new RuntimeError.DeveloperError("oneOverRadii is required.");
  1123. }
  1124. if (!defaultValue.defined(oneOverRadiiSquared)) {
  1125. throw new RuntimeError.DeveloperError("oneOverRadiiSquared is required.");
  1126. }
  1127. if (!defaultValue.defined(centerToleranceSquared)) {
  1128. throw new RuntimeError.DeveloperError("centerToleranceSquared is required.");
  1129. }
  1130. //>>includeEnd('debug');
  1131. const positionX = cartesian.x;
  1132. const positionY = cartesian.y;
  1133. const positionZ = cartesian.z;
  1134. const oneOverRadiiX = oneOverRadii.x;
  1135. const oneOverRadiiY = oneOverRadii.y;
  1136. const oneOverRadiiZ = oneOverRadii.z;
  1137. const x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  1138. const y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  1139. const z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  1140. // Compute the squared ellipsoid norm.
  1141. const squaredNorm = x2 + y2 + z2;
  1142. const ratio = Math.sqrt(1.0 / squaredNorm);
  1143. // As an initial approximation, assume that the radial intersection is the projection point.
  1144. const intersection = Cartesian3.multiplyByScalar(
  1145. cartesian,
  1146. ratio,
  1147. scaleToGeodeticSurfaceIntersection
  1148. );
  1149. // If the position is near the center, the iteration will not converge.
  1150. if (squaredNorm < centerToleranceSquared) {
  1151. return !isFinite(ratio)
  1152. ? undefined
  1153. : Cartesian3.clone(intersection, result);
  1154. }
  1155. const oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  1156. const oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  1157. const oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  1158. // Use the gradient at the intersection point in place of the true unit normal.
  1159. // The difference in magnitude will be absorbed in the multiplier.
  1160. const gradient = scaleToGeodeticSurfaceGradient;
  1161. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  1162. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  1163. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  1164. // Compute the initial guess at the normal vector multiplier, lambda.
  1165. let lambda =
  1166. ((1.0 - ratio) * Cartesian3.magnitude(cartesian)) /
  1167. (0.5 * Cartesian3.magnitude(gradient));
  1168. let correction = 0.0;
  1169. let func;
  1170. let denominator;
  1171. let xMultiplier;
  1172. let yMultiplier;
  1173. let zMultiplier;
  1174. let xMultiplier2;
  1175. let yMultiplier2;
  1176. let zMultiplier2;
  1177. let xMultiplier3;
  1178. let yMultiplier3;
  1179. let zMultiplier3;
  1180. do {
  1181. lambda -= correction;
  1182. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  1183. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  1184. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  1185. xMultiplier2 = xMultiplier * xMultiplier;
  1186. yMultiplier2 = yMultiplier * yMultiplier;
  1187. zMultiplier2 = zMultiplier * zMultiplier;
  1188. xMultiplier3 = xMultiplier2 * xMultiplier;
  1189. yMultiplier3 = yMultiplier2 * yMultiplier;
  1190. zMultiplier3 = zMultiplier2 * zMultiplier;
  1191. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  1192. // "denominator" here refers to the use of this expression in the velocity and acceleration
  1193. // computations in the sections to follow.
  1194. denominator =
  1195. x2 * xMultiplier3 * oneOverRadiiSquaredX +
  1196. y2 * yMultiplier3 * oneOverRadiiSquaredY +
  1197. z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  1198. const derivative = -2.0 * denominator;
  1199. correction = func / derivative;
  1200. } while (Math.abs(func) > ComponentDatatype.CesiumMath.EPSILON12);
  1201. if (!defaultValue.defined(result)) {
  1202. return new Cartesian3(
  1203. positionX * xMultiplier,
  1204. positionY * yMultiplier,
  1205. positionZ * zMultiplier
  1206. );
  1207. }
  1208. result.x = positionX * xMultiplier;
  1209. result.y = positionY * yMultiplier;
  1210. result.z = positionZ * zMultiplier;
  1211. return result;
  1212. }
  1213. /**
  1214. * A position defined by longitude, latitude, and height.
  1215. * @alias Cartographic
  1216. * @constructor
  1217. *
  1218. * @param {Number} [longitude=0.0] The longitude, in radians.
  1219. * @param {Number} [latitude=0.0] The latitude, in radians.
  1220. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1221. *
  1222. * @see Ellipsoid
  1223. */
  1224. function Cartographic(longitude, latitude, height) {
  1225. /**
  1226. * The longitude, in radians.
  1227. * @type {Number}
  1228. * @default 0.0
  1229. */
  1230. this.longitude = defaultValue.defaultValue(longitude, 0.0);
  1231. /**
  1232. * The latitude, in radians.
  1233. * @type {Number}
  1234. * @default 0.0
  1235. */
  1236. this.latitude = defaultValue.defaultValue(latitude, 0.0);
  1237. /**
  1238. * The height, in meters, above the ellipsoid.
  1239. * @type {Number}
  1240. * @default 0.0
  1241. */
  1242. this.height = defaultValue.defaultValue(height, 0.0);
  1243. }
  1244. /**
  1245. * Creates a new Cartographic instance from longitude and latitude
  1246. * specified in radians.
  1247. *
  1248. * @param {Number} longitude The longitude, in radians.
  1249. * @param {Number} latitude The latitude, in radians.
  1250. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1251. * @param {Cartographic} [result] The object onto which to store the result.
  1252. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1253. */
  1254. Cartographic.fromRadians = function (longitude, latitude, height, result) {
  1255. //>>includeStart('debug', pragmas.debug);
  1256. RuntimeError.Check.typeOf.number("longitude", longitude);
  1257. RuntimeError.Check.typeOf.number("latitude", latitude);
  1258. //>>includeEnd('debug');
  1259. height = defaultValue.defaultValue(height, 0.0);
  1260. if (!defaultValue.defined(result)) {
  1261. return new Cartographic(longitude, latitude, height);
  1262. }
  1263. result.longitude = longitude;
  1264. result.latitude = latitude;
  1265. result.height = height;
  1266. return result;
  1267. };
  1268. /**
  1269. * Creates a new Cartographic instance from longitude and latitude
  1270. * specified in degrees. The values in the resulting object will
  1271. * be in radians.
  1272. *
  1273. * @param {Number} longitude The longitude, in degrees.
  1274. * @param {Number} latitude The latitude, in degrees.
  1275. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1276. * @param {Cartographic} [result] The object onto which to store the result.
  1277. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1278. */
  1279. Cartographic.fromDegrees = function (longitude, latitude, height, result) {
  1280. //>>includeStart('debug', pragmas.debug);
  1281. RuntimeError.Check.typeOf.number("longitude", longitude);
  1282. RuntimeError.Check.typeOf.number("latitude", latitude);
  1283. //>>includeEnd('debug');
  1284. longitude = ComponentDatatype.CesiumMath.toRadians(longitude);
  1285. latitude = ComponentDatatype.CesiumMath.toRadians(latitude);
  1286. return Cartographic.fromRadians(longitude, latitude, height, result);
  1287. };
  1288. const cartesianToCartographicN$1 = new Cartesian3();
  1289. const cartesianToCartographicP$1 = new Cartesian3();
  1290. const cartesianToCartographicH$1 = new Cartesian3();
  1291. const wgs84OneOverRadii = new Cartesian3(
  1292. 1.0 / 6378137.0,
  1293. 1.0 / 6378137.0,
  1294. 1.0 / 6356752.3142451793
  1295. );
  1296. const wgs84OneOverRadiiSquared = new Cartesian3(
  1297. 1.0 / (6378137.0 * 6378137.0),
  1298. 1.0 / (6378137.0 * 6378137.0),
  1299. 1.0 / (6356752.3142451793 * 6356752.3142451793)
  1300. );
  1301. const wgs84CenterToleranceSquared = ComponentDatatype.CesiumMath.EPSILON1;
  1302. /**
  1303. * Creates a new Cartographic instance from a Cartesian position. The values in the
  1304. * resulting object will be in radians.
  1305. *
  1306. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1307. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1308. * @param {Cartographic} [result] The object onto which to store the result.
  1309. * @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.
  1310. */
  1311. Cartographic.fromCartesian = function (cartesian, ellipsoid, result) {
  1312. const oneOverRadii = defaultValue.defined(ellipsoid)
  1313. ? ellipsoid.oneOverRadii
  1314. : wgs84OneOverRadii;
  1315. const oneOverRadiiSquared = defaultValue.defined(ellipsoid)
  1316. ? ellipsoid.oneOverRadiiSquared
  1317. : wgs84OneOverRadiiSquared;
  1318. const centerToleranceSquared = defaultValue.defined(ellipsoid)
  1319. ? ellipsoid._centerToleranceSquared
  1320. : wgs84CenterToleranceSquared;
  1321. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1322. const p = scaleToGeodeticSurface(
  1323. cartesian,
  1324. oneOverRadii,
  1325. oneOverRadiiSquared,
  1326. centerToleranceSquared,
  1327. cartesianToCartographicP$1
  1328. );
  1329. if (!defaultValue.defined(p)) {
  1330. return undefined;
  1331. }
  1332. let n = Cartesian3.multiplyComponents(
  1333. p,
  1334. oneOverRadiiSquared,
  1335. cartesianToCartographicN$1
  1336. );
  1337. n = Cartesian3.normalize(n, n);
  1338. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH$1);
  1339. const longitude = Math.atan2(n.y, n.x);
  1340. const latitude = Math.asin(n.z);
  1341. const height =
  1342. ComponentDatatype.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1343. if (!defaultValue.defined(result)) {
  1344. return new Cartographic(longitude, latitude, height);
  1345. }
  1346. result.longitude = longitude;
  1347. result.latitude = latitude;
  1348. result.height = height;
  1349. return result;
  1350. };
  1351. /**
  1352. * Creates a new Cartesian3 instance from a Cartographic input. The values in the inputted
  1353. * object should be in radians.
  1354. *
  1355. * @param {Cartographic} cartographic Input to be converted into a Cartesian3 output.
  1356. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1357. * @param {Cartesian3} [result] The object onto which to store the result.
  1358. * @returns {Cartesian3} The position
  1359. */
  1360. Cartographic.toCartesian = function (cartographic, ellipsoid, result) {
  1361. //>>includeStart('debug', pragmas.debug);
  1362. RuntimeError.Check.defined("cartographic", cartographic);
  1363. //>>includeEnd('debug');
  1364. return Cartesian3.fromRadians(
  1365. cartographic.longitude,
  1366. cartographic.latitude,
  1367. cartographic.height,
  1368. ellipsoid,
  1369. result
  1370. );
  1371. };
  1372. /**
  1373. * Duplicates a Cartographic instance.
  1374. *
  1375. * @param {Cartographic} cartographic The cartographic to duplicate.
  1376. * @param {Cartographic} [result] The object onto which to store the result.
  1377. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  1378. */
  1379. Cartographic.clone = function (cartographic, result) {
  1380. if (!defaultValue.defined(cartographic)) {
  1381. return undefined;
  1382. }
  1383. if (!defaultValue.defined(result)) {
  1384. return new Cartographic(
  1385. cartographic.longitude,
  1386. cartographic.latitude,
  1387. cartographic.height
  1388. );
  1389. }
  1390. result.longitude = cartographic.longitude;
  1391. result.latitude = cartographic.latitude;
  1392. result.height = cartographic.height;
  1393. return result;
  1394. };
  1395. /**
  1396. * Compares the provided cartographics componentwise and returns
  1397. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1398. *
  1399. * @param {Cartographic} [left] The first cartographic.
  1400. * @param {Cartographic} [right] The second cartographic.
  1401. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1402. */
  1403. Cartographic.equals = function (left, right) {
  1404. return (
  1405. left === right ||
  1406. (defaultValue.defined(left) &&
  1407. defaultValue.defined(right) &&
  1408. left.longitude === right.longitude &&
  1409. left.latitude === right.latitude &&
  1410. left.height === right.height)
  1411. );
  1412. };
  1413. /**
  1414. * Compares the provided cartographics componentwise and returns
  1415. * <code>true</code> if they are within the provided epsilon,
  1416. * <code>false</code> otherwise.
  1417. *
  1418. * @param {Cartographic} [left] The first cartographic.
  1419. * @param {Cartographic} [right] The second cartographic.
  1420. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1421. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1422. */
  1423. Cartographic.equalsEpsilon = function (left, right, epsilon) {
  1424. epsilon = defaultValue.defaultValue(epsilon, 0);
  1425. return (
  1426. left === right ||
  1427. (defaultValue.defined(left) &&
  1428. defaultValue.defined(right) &&
  1429. Math.abs(left.longitude - right.longitude) <= epsilon &&
  1430. Math.abs(left.latitude - right.latitude) <= epsilon &&
  1431. Math.abs(left.height - right.height) <= epsilon)
  1432. );
  1433. };
  1434. /**
  1435. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  1436. *
  1437. * @type {Cartographic}
  1438. * @constant
  1439. */
  1440. Cartographic.ZERO = Object.freeze(new Cartographic(0.0, 0.0, 0.0));
  1441. /**
  1442. * Duplicates this instance.
  1443. *
  1444. * @param {Cartographic} [result] The object onto which to store the result.
  1445. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1446. */
  1447. Cartographic.prototype.clone = function (result) {
  1448. return Cartographic.clone(this, result);
  1449. };
  1450. /**
  1451. * Compares the provided against this cartographic componentwise and returns
  1452. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1453. *
  1454. * @param {Cartographic} [right] The second cartographic.
  1455. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1456. */
  1457. Cartographic.prototype.equals = function (right) {
  1458. return Cartographic.equals(this, right);
  1459. };
  1460. /**
  1461. * Compares the provided against this cartographic componentwise and returns
  1462. * <code>true</code> if they are within the provided epsilon,
  1463. * <code>false</code> otherwise.
  1464. *
  1465. * @param {Cartographic} [right] The second cartographic.
  1466. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1467. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1468. */
  1469. Cartographic.prototype.equalsEpsilon = function (right, epsilon) {
  1470. return Cartographic.equalsEpsilon(this, right, epsilon);
  1471. };
  1472. /**
  1473. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  1474. *
  1475. * @returns {String} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  1476. */
  1477. Cartographic.prototype.toString = function () {
  1478. return `(${this.longitude}, ${this.latitude}, ${this.height})`;
  1479. };
  1480. function initialize(ellipsoid, x, y, z) {
  1481. x = defaultValue.defaultValue(x, 0.0);
  1482. y = defaultValue.defaultValue(y, 0.0);
  1483. z = defaultValue.defaultValue(z, 0.0);
  1484. //>>includeStart('debug', pragmas.debug);
  1485. RuntimeError.Check.typeOf.number.greaterThanOrEquals("x", x, 0.0);
  1486. RuntimeError.Check.typeOf.number.greaterThanOrEquals("y", y, 0.0);
  1487. RuntimeError.Check.typeOf.number.greaterThanOrEquals("z", z, 0.0);
  1488. //>>includeEnd('debug');
  1489. ellipsoid._radii = new Cartesian3(x, y, z);
  1490. ellipsoid._radiiSquared = new Cartesian3(x * x, y * y, z * z);
  1491. ellipsoid._radiiToTheFourth = new Cartesian3(
  1492. x * x * x * x,
  1493. y * y * y * y,
  1494. z * z * z * z
  1495. );
  1496. ellipsoid._oneOverRadii = new Cartesian3(
  1497. x === 0.0 ? 0.0 : 1.0 / x,
  1498. y === 0.0 ? 0.0 : 1.0 / y,
  1499. z === 0.0 ? 0.0 : 1.0 / z
  1500. );
  1501. ellipsoid._oneOverRadiiSquared = new Cartesian3(
  1502. x === 0.0 ? 0.0 : 1.0 / (x * x),
  1503. y === 0.0 ? 0.0 : 1.0 / (y * y),
  1504. z === 0.0 ? 0.0 : 1.0 / (z * z)
  1505. );
  1506. ellipsoid._minimumRadius = Math.min(x, y, z);
  1507. ellipsoid._maximumRadius = Math.max(x, y, z);
  1508. ellipsoid._centerToleranceSquared = ComponentDatatype.CesiumMath.EPSILON1;
  1509. if (ellipsoid._radiiSquared.z !== 0) {
  1510. ellipsoid._squaredXOverSquaredZ =
  1511. ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  1512. }
  1513. }
  1514. /**
  1515. * A quadratic surface defined in Cartesian coordinates by the equation
  1516. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  1517. * by Cesium to represent the shape of planetary bodies.
  1518. *
  1519. * Rather than constructing this object directly, one of the provided
  1520. * constants is normally used.
  1521. * @alias Ellipsoid
  1522. * @constructor
  1523. *
  1524. * @param {Number} [x=0] The radius in the x direction.
  1525. * @param {Number} [y=0] The radius in the y direction.
  1526. * @param {Number} [z=0] The radius in the z direction.
  1527. *
  1528. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1529. *
  1530. * @see Ellipsoid.fromCartesian3
  1531. * @see Ellipsoid.WGS84
  1532. * @see Ellipsoid.UNIT_SPHERE
  1533. */
  1534. function Ellipsoid(x, y, z) {
  1535. this._radii = undefined;
  1536. this._radiiSquared = undefined;
  1537. this._radiiToTheFourth = undefined;
  1538. this._oneOverRadii = undefined;
  1539. this._oneOverRadiiSquared = undefined;
  1540. this._minimumRadius = undefined;
  1541. this._maximumRadius = undefined;
  1542. this._centerToleranceSquared = undefined;
  1543. this._squaredXOverSquaredZ = undefined;
  1544. initialize(this, x, y, z);
  1545. }
  1546. Object.defineProperties(Ellipsoid.prototype, {
  1547. /**
  1548. * Gets the radii of the ellipsoid.
  1549. * @memberof Ellipsoid.prototype
  1550. * @type {Cartesian3}
  1551. * @readonly
  1552. */
  1553. radii: {
  1554. get: function () {
  1555. return this._radii;
  1556. },
  1557. },
  1558. /**
  1559. * Gets the squared radii of the ellipsoid.
  1560. * @memberof Ellipsoid.prototype
  1561. * @type {Cartesian3}
  1562. * @readonly
  1563. */
  1564. radiiSquared: {
  1565. get: function () {
  1566. return this._radiiSquared;
  1567. },
  1568. },
  1569. /**
  1570. * Gets the radii of the ellipsoid raise to the fourth power.
  1571. * @memberof Ellipsoid.prototype
  1572. * @type {Cartesian3}
  1573. * @readonly
  1574. */
  1575. radiiToTheFourth: {
  1576. get: function () {
  1577. return this._radiiToTheFourth;
  1578. },
  1579. },
  1580. /**
  1581. * Gets one over the radii of the ellipsoid.
  1582. * @memberof Ellipsoid.prototype
  1583. * @type {Cartesian3}
  1584. * @readonly
  1585. */
  1586. oneOverRadii: {
  1587. get: function () {
  1588. return this._oneOverRadii;
  1589. },
  1590. },
  1591. /**
  1592. * Gets one over the squared radii of the ellipsoid.
  1593. * @memberof Ellipsoid.prototype
  1594. * @type {Cartesian3}
  1595. * @readonly
  1596. */
  1597. oneOverRadiiSquared: {
  1598. get: function () {
  1599. return this._oneOverRadiiSquared;
  1600. },
  1601. },
  1602. /**
  1603. * Gets the minimum radius of the ellipsoid.
  1604. * @memberof Ellipsoid.prototype
  1605. * @type {Number}
  1606. * @readonly
  1607. */
  1608. minimumRadius: {
  1609. get: function () {
  1610. return this._minimumRadius;
  1611. },
  1612. },
  1613. /**
  1614. * Gets the maximum radius of the ellipsoid.
  1615. * @memberof Ellipsoid.prototype
  1616. * @type {Number}
  1617. * @readonly
  1618. */
  1619. maximumRadius: {
  1620. get: function () {
  1621. return this._maximumRadius;
  1622. },
  1623. },
  1624. });
  1625. /**
  1626. * Duplicates an Ellipsoid instance.
  1627. *
  1628. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  1629. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1630. * instance should be created.
  1631. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  1632. */
  1633. Ellipsoid.clone = function (ellipsoid, result) {
  1634. if (!defaultValue.defined(ellipsoid)) {
  1635. return undefined;
  1636. }
  1637. const radii = ellipsoid._radii;
  1638. if (!defaultValue.defined(result)) {
  1639. return new Ellipsoid(radii.x, radii.y, radii.z);
  1640. }
  1641. Cartesian3.clone(radii, result._radii);
  1642. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  1643. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  1644. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  1645. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  1646. result._minimumRadius = ellipsoid._minimumRadius;
  1647. result._maximumRadius = ellipsoid._maximumRadius;
  1648. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  1649. return result;
  1650. };
  1651. /**
  1652. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  1653. *
  1654. * @param {Cartesian3} [cartesian=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  1655. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1656. * instance should be created.
  1657. * @returns {Ellipsoid} A new Ellipsoid instance.
  1658. *
  1659. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1660. *
  1661. * @see Ellipsoid.WGS84
  1662. * @see Ellipsoid.UNIT_SPHERE
  1663. */
  1664. Ellipsoid.fromCartesian3 = function (cartesian, result) {
  1665. if (!defaultValue.defined(result)) {
  1666. result = new Ellipsoid();
  1667. }
  1668. if (!defaultValue.defined(cartesian)) {
  1669. return result;
  1670. }
  1671. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  1672. return result;
  1673. };
  1674. /**
  1675. * An Ellipsoid instance initialized to the WGS84 standard.
  1676. *
  1677. * @type {Ellipsoid}
  1678. * @constant
  1679. */
  1680. Ellipsoid.WGS84 = Object.freeze(
  1681. new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793)
  1682. );
  1683. /**
  1684. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  1685. *
  1686. * @type {Ellipsoid}
  1687. * @constant
  1688. */
  1689. Ellipsoid.UNIT_SPHERE = Object.freeze(new Ellipsoid(1.0, 1.0, 1.0));
  1690. /**
  1691. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  1692. *
  1693. * @type {Ellipsoid}
  1694. * @constant
  1695. */
  1696. Ellipsoid.MOON = Object.freeze(
  1697. new Ellipsoid(
  1698. ComponentDatatype.CesiumMath.LUNAR_RADIUS,
  1699. ComponentDatatype.CesiumMath.LUNAR_RADIUS,
  1700. ComponentDatatype.CesiumMath.LUNAR_RADIUS
  1701. )
  1702. );
  1703. /**
  1704. * Duplicates an Ellipsoid instance.
  1705. *
  1706. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1707. * instance should be created.
  1708. * @returns {Ellipsoid} The cloned Ellipsoid.
  1709. */
  1710. Ellipsoid.prototype.clone = function (result) {
  1711. return Ellipsoid.clone(this, result);
  1712. };
  1713. /**
  1714. * The number of elements used to pack the object into an array.
  1715. * @type {Number}
  1716. */
  1717. Ellipsoid.packedLength = Cartesian3.packedLength;
  1718. /**
  1719. * Stores the provided instance into the provided array.
  1720. *
  1721. * @param {Ellipsoid} value The value to pack.
  1722. * @param {Number[]} array The array to pack into.
  1723. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1724. *
  1725. * @returns {Number[]} The array that was packed into
  1726. */
  1727. Ellipsoid.pack = function (value, array, startingIndex) {
  1728. //>>includeStart('debug', pragmas.debug);
  1729. RuntimeError.Check.typeOf.object("value", value);
  1730. RuntimeError.Check.defined("array", array);
  1731. //>>includeEnd('debug');
  1732. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1733. Cartesian3.pack(value._radii, array, startingIndex);
  1734. return array;
  1735. };
  1736. /**
  1737. * Retrieves an instance from a packed array.
  1738. *
  1739. * @param {Number[]} array The packed array.
  1740. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1741. * @param {Ellipsoid} [result] The object into which to store the result.
  1742. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  1743. */
  1744. Ellipsoid.unpack = function (array, startingIndex, result) {
  1745. //>>includeStart('debug', pragmas.debug);
  1746. RuntimeError.Check.defined("array", array);
  1747. //>>includeEnd('debug');
  1748. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1749. const radii = Cartesian3.unpack(array, startingIndex);
  1750. return Ellipsoid.fromCartesian3(radii, result);
  1751. };
  1752. /**
  1753. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  1754. * @function
  1755. *
  1756. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  1757. * @param {Cartesian3} [result] The object onto which to store the result.
  1758. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1759. */
  1760. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  1761. /**
  1762. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1763. *
  1764. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  1765. * @param {Cartesian3} [result] The object onto which to store the result.
  1766. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1767. */
  1768. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function (
  1769. cartographic,
  1770. result
  1771. ) {
  1772. //>>includeStart('debug', pragmas.debug);
  1773. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  1774. //>>includeEnd('debug');
  1775. const longitude = cartographic.longitude;
  1776. const latitude = cartographic.latitude;
  1777. const cosLatitude = Math.cos(latitude);
  1778. const x = cosLatitude * Math.cos(longitude);
  1779. const y = cosLatitude * Math.sin(longitude);
  1780. const z = Math.sin(latitude);
  1781. if (!defaultValue.defined(result)) {
  1782. result = new Cartesian3();
  1783. }
  1784. result.x = x;
  1785. result.y = y;
  1786. result.z = z;
  1787. return Cartesian3.normalize(result, result);
  1788. };
  1789. /**
  1790. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1791. *
  1792. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  1793. * @param {Cartesian3} [result] The object onto which to store the result.
  1794. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided, or undefined if a normal cannot be found.
  1795. */
  1796. Ellipsoid.prototype.geodeticSurfaceNormal = function (cartesian, result) {
  1797. if (
  1798. Cartesian3.equalsEpsilon(cartesian, Cartesian3.ZERO, ComponentDatatype.CesiumMath.EPSILON14)
  1799. ) {
  1800. return undefined;
  1801. }
  1802. if (!defaultValue.defined(result)) {
  1803. result = new Cartesian3();
  1804. }
  1805. result = Cartesian3.multiplyComponents(
  1806. cartesian,
  1807. this._oneOverRadiiSquared,
  1808. result
  1809. );
  1810. return Cartesian3.normalize(result, result);
  1811. };
  1812. const cartographicToCartesianNormal = new Cartesian3();
  1813. const cartographicToCartesianK = new Cartesian3();
  1814. /**
  1815. * Converts the provided cartographic to Cartesian representation.
  1816. *
  1817. * @param {Cartographic} cartographic The cartographic position.
  1818. * @param {Cartesian3} [result] The object onto which to store the result.
  1819. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1820. *
  1821. * @example
  1822. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  1823. * const position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  1824. * const cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  1825. */
  1826. Ellipsoid.prototype.cartographicToCartesian = function (cartographic, result) {
  1827. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  1828. const n = cartographicToCartesianNormal;
  1829. const k = cartographicToCartesianK;
  1830. this.geodeticSurfaceNormalCartographic(cartographic, n);
  1831. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  1832. const gamma = Math.sqrt(Cartesian3.dot(n, k));
  1833. Cartesian3.divideByScalar(k, gamma, k);
  1834. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  1835. if (!defaultValue.defined(result)) {
  1836. result = new Cartesian3();
  1837. }
  1838. return Cartesian3.add(k, n, result);
  1839. };
  1840. /**
  1841. * Converts the provided array of cartographics to an array of Cartesians.
  1842. *
  1843. * @param {Cartographic[]} cartographics An array of cartographic positions.
  1844. * @param {Cartesian3[]} [result] The object onto which to store the result.
  1845. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  1846. *
  1847. * @example
  1848. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  1849. * const positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  1850. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  1851. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  1852. * const cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  1853. */
  1854. Ellipsoid.prototype.cartographicArrayToCartesianArray = function (
  1855. cartographics,
  1856. result
  1857. ) {
  1858. //>>includeStart('debug', pragmas.debug);
  1859. RuntimeError.Check.defined("cartographics", cartographics);
  1860. //>>includeEnd('debug')
  1861. const length = cartographics.length;
  1862. if (!defaultValue.defined(result)) {
  1863. result = new Array(length);
  1864. } else {
  1865. result.length = length;
  1866. }
  1867. for (let i = 0; i < length; i++) {
  1868. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  1869. }
  1870. return result;
  1871. };
  1872. const cartesianToCartographicN = new Cartesian3();
  1873. const cartesianToCartographicP = new Cartesian3();
  1874. const cartesianToCartographicH = new Cartesian3();
  1875. /**
  1876. * Converts the provided cartesian to cartographic representation.
  1877. * The cartesian is undefined at the center of the ellipsoid.
  1878. *
  1879. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1880. * @param {Cartographic} [result] The object onto which to store the result.
  1881. * @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.
  1882. *
  1883. * @example
  1884. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  1885. * const position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  1886. * const cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  1887. */
  1888. Ellipsoid.prototype.cartesianToCartographic = function (cartesian, result) {
  1889. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1890. const p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  1891. if (!defaultValue.defined(p)) {
  1892. return undefined;
  1893. }
  1894. const n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  1895. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  1896. const longitude = Math.atan2(n.y, n.x);
  1897. const latitude = Math.asin(n.z);
  1898. const height =
  1899. ComponentDatatype.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1900. if (!defaultValue.defined(result)) {
  1901. return new Cartographic(longitude, latitude, height);
  1902. }
  1903. result.longitude = longitude;
  1904. result.latitude = latitude;
  1905. result.height = height;
  1906. return result;
  1907. };
  1908. /**
  1909. * Converts the provided array of cartesians to an array of cartographics.
  1910. *
  1911. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  1912. * @param {Cartographic[]} [result] The object onto which to store the result.
  1913. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  1914. *
  1915. * @example
  1916. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  1917. * const positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  1918. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  1919. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  1920. * const cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  1921. */
  1922. Ellipsoid.prototype.cartesianArrayToCartographicArray = function (
  1923. cartesians,
  1924. result
  1925. ) {
  1926. //>>includeStart('debug', pragmas.debug);
  1927. RuntimeError.Check.defined("cartesians", cartesians);
  1928. //>>includeEnd('debug');
  1929. const length = cartesians.length;
  1930. if (!defaultValue.defined(result)) {
  1931. result = new Array(length);
  1932. } else {
  1933. result.length = length;
  1934. }
  1935. for (let i = 0; i < length; ++i) {
  1936. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  1937. }
  1938. return result;
  1939. };
  1940. /**
  1941. * Scales the provided Cartesian position along the geodetic surface normal
  1942. * so that it is on the surface of this ellipsoid. If the position is
  1943. * at the center of the ellipsoid, this function returns undefined.
  1944. *
  1945. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1946. * @param {Cartesian3} [result] The object onto which to store the result.
  1947. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1948. */
  1949. Ellipsoid.prototype.scaleToGeodeticSurface = function (cartesian, result) {
  1950. return scaleToGeodeticSurface(
  1951. cartesian,
  1952. this._oneOverRadii,
  1953. this._oneOverRadiiSquared,
  1954. this._centerToleranceSquared,
  1955. result
  1956. );
  1957. };
  1958. /**
  1959. * Scales the provided Cartesian position along the geocentric surface normal
  1960. * so that it is on the surface of this ellipsoid.
  1961. *
  1962. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1963. * @param {Cartesian3} [result] The object onto which to store the result.
  1964. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1965. */
  1966. Ellipsoid.prototype.scaleToGeocentricSurface = function (cartesian, result) {
  1967. //>>includeStart('debug', pragmas.debug);
  1968. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  1969. //>>includeEnd('debug');
  1970. if (!defaultValue.defined(result)) {
  1971. result = new Cartesian3();
  1972. }
  1973. const positionX = cartesian.x;
  1974. const positionY = cartesian.y;
  1975. const positionZ = cartesian.z;
  1976. const oneOverRadiiSquared = this._oneOverRadiiSquared;
  1977. const beta =
  1978. 1.0 /
  1979. Math.sqrt(
  1980. positionX * positionX * oneOverRadiiSquared.x +
  1981. positionY * positionY * oneOverRadiiSquared.y +
  1982. positionZ * positionZ * oneOverRadiiSquared.z
  1983. );
  1984. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  1985. };
  1986. /**
  1987. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  1988. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  1989. *
  1990. * @param {Cartesian3} position The position to transform.
  1991. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  1992. * return a new instance.
  1993. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  1994. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  1995. */
  1996. Ellipsoid.prototype.transformPositionToScaledSpace = function (
  1997. position,
  1998. result
  1999. ) {
  2000. if (!defaultValue.defined(result)) {
  2001. result = new Cartesian3();
  2002. }
  2003. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  2004. };
  2005. /**
  2006. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  2007. * its components by the result of {@link Ellipsoid#radii}.
  2008. *
  2009. * @param {Cartesian3} position The position to transform.
  2010. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2011. * return a new instance.
  2012. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  2013. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2014. */
  2015. Ellipsoid.prototype.transformPositionFromScaledSpace = function (
  2016. position,
  2017. result
  2018. ) {
  2019. if (!defaultValue.defined(result)) {
  2020. result = new Cartesian3();
  2021. }
  2022. return Cartesian3.multiplyComponents(position, this._radii, result);
  2023. };
  2024. /**
  2025. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  2026. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2027. *
  2028. * @param {Ellipsoid} [right] The other Ellipsoid.
  2029. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2030. */
  2031. Ellipsoid.prototype.equals = function (right) {
  2032. return (
  2033. this === right ||
  2034. (defaultValue.defined(right) && Cartesian3.equals(this._radii, right._radii))
  2035. );
  2036. };
  2037. /**
  2038. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2039. *
  2040. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2041. */
  2042. Ellipsoid.prototype.toString = function () {
  2043. return this._radii.toString();
  2044. };
  2045. /**
  2046. * Computes a point which is the intersection of the surface normal with the z-axis.
  2047. *
  2048. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  2049. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  2050. * 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.
  2051. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  2052. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  2053. * @param {Cartesian3} [result] The cartesian to which to copy the result, or undefined to create and
  2054. * return a new instance.
  2055. * @returns {Cartesian3 | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  2056. *
  2057. * @exception {DeveloperError} position is required.
  2058. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  2059. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  2060. */
  2061. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function (
  2062. position,
  2063. buffer,
  2064. result
  2065. ) {
  2066. //>>includeStart('debug', pragmas.debug);
  2067. RuntimeError.Check.typeOf.object("position", position);
  2068. if (
  2069. !ComponentDatatype.CesiumMath.equalsEpsilon(
  2070. this._radii.x,
  2071. this._radii.y,
  2072. ComponentDatatype.CesiumMath.EPSILON15
  2073. )
  2074. ) {
  2075. throw new RuntimeError.DeveloperError(
  2076. "Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)"
  2077. );
  2078. }
  2079. RuntimeError.Check.typeOf.number.greaterThan("Ellipsoid.radii.z", this._radii.z, 0);
  2080. //>>includeEnd('debug');
  2081. buffer = defaultValue.defaultValue(buffer, 0.0);
  2082. const squaredXOverSquaredZ = this._squaredXOverSquaredZ;
  2083. if (!defaultValue.defined(result)) {
  2084. result = new Cartesian3();
  2085. }
  2086. result.x = 0.0;
  2087. result.y = 0.0;
  2088. result.z = position.z * (1 - squaredXOverSquaredZ);
  2089. if (Math.abs(result.z) >= this._radii.z - buffer) {
  2090. return undefined;
  2091. }
  2092. return result;
  2093. };
  2094. const abscissas = [
  2095. 0.14887433898163,
  2096. 0.43339539412925,
  2097. 0.67940956829902,
  2098. 0.86506336668898,
  2099. 0.97390652851717,
  2100. 0.0,
  2101. ];
  2102. const weights = [
  2103. 0.29552422471475,
  2104. 0.26926671930999,
  2105. 0.21908636251598,
  2106. 0.14945134915058,
  2107. 0.066671344308684,
  2108. 0.0,
  2109. ];
  2110. /**
  2111. * Compute the 10th order Gauss-Legendre Quadrature of the given definite integral.
  2112. *
  2113. * @param {Number} a The lower bound for the integration.
  2114. * @param {Number} b The upper bound for the integration.
  2115. * @param {Ellipsoid~RealValuedScalarFunction} func The function to integrate.
  2116. * @returns {Number} The value of the integral of the given function over the given domain.
  2117. *
  2118. * @private
  2119. */
  2120. function gaussLegendreQuadrature(a, b, func) {
  2121. //>>includeStart('debug', pragmas.debug);
  2122. RuntimeError.Check.typeOf.number("a", a);
  2123. RuntimeError.Check.typeOf.number("b", b);
  2124. RuntimeError.Check.typeOf.func("func", func);
  2125. //>>includeEnd('debug');
  2126. // The range is half of the normal range since the five weights add to one (ten weights add to two).
  2127. // The values of the abscissas are multiplied by two to account for this.
  2128. const xMean = 0.5 * (b + a);
  2129. const xRange = 0.5 * (b - a);
  2130. let sum = 0.0;
  2131. for (let i = 0; i < 5; i++) {
  2132. const dx = xRange * abscissas[i];
  2133. sum += weights[i] * (func(xMean + dx) + func(xMean - dx));
  2134. }
  2135. // Scale the sum to the range of x.
  2136. sum *= xRange;
  2137. return sum;
  2138. }
  2139. /**
  2140. * A real valued scalar function.
  2141. * @callback Ellipsoid~RealValuedScalarFunction
  2142. *
  2143. * @param {Number} x The value used to evaluate the function.
  2144. * @returns {Number} The value of the function at x.
  2145. *
  2146. * @private
  2147. */
  2148. /**
  2149. * Computes an approximation of the surface area of a rectangle on the surface of an ellipsoid using
  2150. * Gauss-Legendre 10th order quadrature.
  2151. *
  2152. * @param {Rectangle} rectangle The rectangle used for computing the surface area.
  2153. * @returns {Number} The approximate area of the rectangle on the surface of this ellipsoid.
  2154. */
  2155. Ellipsoid.prototype.surfaceArea = function (rectangle) {
  2156. //>>includeStart('debug', pragmas.debug);
  2157. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  2158. //>>includeEnd('debug');
  2159. const minLongitude = rectangle.west;
  2160. let maxLongitude = rectangle.east;
  2161. const minLatitude = rectangle.south;
  2162. const maxLatitude = rectangle.north;
  2163. while (maxLongitude < minLongitude) {
  2164. maxLongitude += ComponentDatatype.CesiumMath.TWO_PI;
  2165. }
  2166. const radiiSquared = this._radiiSquared;
  2167. const a2 = radiiSquared.x;
  2168. const b2 = radiiSquared.y;
  2169. const c2 = radiiSquared.z;
  2170. const a2b2 = a2 * b2;
  2171. return gaussLegendreQuadrature(minLatitude, maxLatitude, function (lat) {
  2172. // phi represents the angle measured from the north pole
  2173. // sin(phi) = sin(pi / 2 - lat) = cos(lat), cos(phi) is similar
  2174. const sinPhi = Math.cos(lat);
  2175. const cosPhi = Math.sin(lat);
  2176. return (
  2177. Math.cos(lat) *
  2178. gaussLegendreQuadrature(minLongitude, maxLongitude, function (lon) {
  2179. const cosTheta = Math.cos(lon);
  2180. const sinTheta = Math.sin(lon);
  2181. return Math.sqrt(
  2182. a2b2 * cosPhi * cosPhi +
  2183. c2 *
  2184. (b2 * cosTheta * cosTheta + a2 * sinTheta * sinTheta) *
  2185. sinPhi *
  2186. sinPhi
  2187. );
  2188. })
  2189. );
  2190. });
  2191. };
  2192. /**
  2193. * A 3x3 matrix, indexable as a column-major order array.
  2194. * Constructor parameters are in row-major order for code readability.
  2195. * @alias Matrix3
  2196. * @constructor
  2197. * @implements {ArrayLike<number>}
  2198. *
  2199. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  2200. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  2201. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  2202. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  2203. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  2204. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  2205. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  2206. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  2207. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  2208. *
  2209. * @see Matrix3.fromArray
  2210. * @see Matrix3.fromColumnMajorArray
  2211. * @see Matrix3.fromRowMajorArray
  2212. * @see Matrix3.fromQuaternion
  2213. * @see Matrix3.fromHeadingPitchRoll
  2214. * @see Matrix3.fromScale
  2215. * @see Matrix3.fromUniformScale
  2216. * @see Matrix3.fromCrossProduct
  2217. * @see Matrix3.fromRotationX
  2218. * @see Matrix3.fromRotationY
  2219. * @see Matrix3.fromRotationZ
  2220. * @see Matrix2
  2221. * @see Matrix4
  2222. */
  2223. function Matrix3(
  2224. column0Row0,
  2225. column1Row0,
  2226. column2Row0,
  2227. column0Row1,
  2228. column1Row1,
  2229. column2Row1,
  2230. column0Row2,
  2231. column1Row2,
  2232. column2Row2
  2233. ) {
  2234. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  2235. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  2236. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  2237. this[3] = defaultValue.defaultValue(column1Row0, 0.0);
  2238. this[4] = defaultValue.defaultValue(column1Row1, 0.0);
  2239. this[5] = defaultValue.defaultValue(column1Row2, 0.0);
  2240. this[6] = defaultValue.defaultValue(column2Row0, 0.0);
  2241. this[7] = defaultValue.defaultValue(column2Row1, 0.0);
  2242. this[8] = defaultValue.defaultValue(column2Row2, 0.0);
  2243. }
  2244. /**
  2245. * The number of elements used to pack the object into an array.
  2246. * @type {Number}
  2247. */
  2248. Matrix3.packedLength = 9;
  2249. /**
  2250. * Stores the provided instance into the provided array.
  2251. *
  2252. * @param {Matrix3} value The value to pack.
  2253. * @param {Number[]} array The array to pack into.
  2254. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2255. *
  2256. * @returns {Number[]} The array that was packed into
  2257. */
  2258. Matrix3.pack = function (value, array, startingIndex) {
  2259. //>>includeStart('debug', pragmas.debug);
  2260. RuntimeError.Check.typeOf.object("value", value);
  2261. RuntimeError.Check.defined("array", array);
  2262. //>>includeEnd('debug');
  2263. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2264. array[startingIndex++] = value[0];
  2265. array[startingIndex++] = value[1];
  2266. array[startingIndex++] = value[2];
  2267. array[startingIndex++] = value[3];
  2268. array[startingIndex++] = value[4];
  2269. array[startingIndex++] = value[5];
  2270. array[startingIndex++] = value[6];
  2271. array[startingIndex++] = value[7];
  2272. array[startingIndex++] = value[8];
  2273. return array;
  2274. };
  2275. /**
  2276. * Retrieves an instance from a packed array.
  2277. *
  2278. * @param {Number[]} array The packed array.
  2279. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2280. * @param {Matrix3} [result] The object into which to store the result.
  2281. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2282. */
  2283. Matrix3.unpack = function (array, startingIndex, result) {
  2284. //>>includeStart('debug', pragmas.debug);
  2285. RuntimeError.Check.defined("array", array);
  2286. //>>includeEnd('debug');
  2287. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2288. if (!defaultValue.defined(result)) {
  2289. result = new Matrix3();
  2290. }
  2291. result[0] = array[startingIndex++];
  2292. result[1] = array[startingIndex++];
  2293. result[2] = array[startingIndex++];
  2294. result[3] = array[startingIndex++];
  2295. result[4] = array[startingIndex++];
  2296. result[5] = array[startingIndex++];
  2297. result[6] = array[startingIndex++];
  2298. result[7] = array[startingIndex++];
  2299. result[8] = array[startingIndex++];
  2300. return result;
  2301. };
  2302. /**
  2303. * Flattens an array of Matrix3s into an array of components. The components
  2304. * are stored in column-major order.
  2305. *
  2306. * @param {Matrix3[]} array The array of matrices to pack.
  2307. * @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.
  2308. * @returns {Number[]} The packed array.
  2309. */
  2310. Matrix3.packArray = function (array, result) {
  2311. //>>includeStart('debug', pragmas.debug);
  2312. RuntimeError.Check.defined("array", array);
  2313. //>>includeEnd('debug');
  2314. const length = array.length;
  2315. const resultLength = length * 9;
  2316. if (!defaultValue.defined(result)) {
  2317. result = new Array(resultLength);
  2318. } else if (!Array.isArray(result) && result.length !== resultLength) {
  2319. //>>includeStart('debug', pragmas.debug);
  2320. throw new RuntimeError.DeveloperError(
  2321. "If result is a typed array, it must have exactly array.length * 9 elements"
  2322. );
  2323. //>>includeEnd('debug');
  2324. } else if (result.length !== resultLength) {
  2325. result.length = resultLength;
  2326. }
  2327. for (let i = 0; i < length; ++i) {
  2328. Matrix3.pack(array[i], result, i * 9);
  2329. }
  2330. return result;
  2331. };
  2332. /**
  2333. * Unpacks an array of column-major matrix components into an array of Matrix3s.
  2334. *
  2335. * @param {Number[]} array The array of components to unpack.
  2336. * @param {Matrix3[]} [result] The array onto which to store the result.
  2337. * @returns {Matrix3[]} The unpacked array.
  2338. */
  2339. Matrix3.unpackArray = function (array, result) {
  2340. //>>includeStart('debug', pragmas.debug);
  2341. RuntimeError.Check.defined("array", array);
  2342. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 9);
  2343. if (array.length % 9 !== 0) {
  2344. throw new RuntimeError.DeveloperError("array length must be a multiple of 9.");
  2345. }
  2346. //>>includeEnd('debug');
  2347. const length = array.length;
  2348. if (!defaultValue.defined(result)) {
  2349. result = new Array(length / 9);
  2350. } else {
  2351. result.length = length / 9;
  2352. }
  2353. for (let i = 0; i < length; i += 9) {
  2354. const index = i / 9;
  2355. result[index] = Matrix3.unpack(array, i, result[index]);
  2356. }
  2357. return result;
  2358. };
  2359. /**
  2360. * Duplicates a Matrix3 instance.
  2361. *
  2362. * @param {Matrix3} matrix The matrix to duplicate.
  2363. * @param {Matrix3} [result] The object onto which to store the result.
  2364. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  2365. */
  2366. Matrix3.clone = function (matrix, result) {
  2367. if (!defaultValue.defined(matrix)) {
  2368. return undefined;
  2369. }
  2370. if (!defaultValue.defined(result)) {
  2371. return new Matrix3(
  2372. matrix[0],
  2373. matrix[3],
  2374. matrix[6],
  2375. matrix[1],
  2376. matrix[4],
  2377. matrix[7],
  2378. matrix[2],
  2379. matrix[5],
  2380. matrix[8]
  2381. );
  2382. }
  2383. result[0] = matrix[0];
  2384. result[1] = matrix[1];
  2385. result[2] = matrix[2];
  2386. result[3] = matrix[3];
  2387. result[4] = matrix[4];
  2388. result[5] = matrix[5];
  2389. result[6] = matrix[6];
  2390. result[7] = matrix[7];
  2391. result[8] = matrix[8];
  2392. return result;
  2393. };
  2394. /**
  2395. * Creates a Matrix3 from 9 consecutive elements in an array.
  2396. *
  2397. * @function
  2398. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  2399. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  2400. * @param {Matrix3} [result] The object onto which to store the result.
  2401. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2402. *
  2403. * @example
  2404. * // Create the Matrix3:
  2405. * // [1.0, 2.0, 3.0]
  2406. * // [1.0, 2.0, 3.0]
  2407. * // [1.0, 2.0, 3.0]
  2408. *
  2409. * const v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2410. * const m = Cesium.Matrix3.fromArray(v);
  2411. *
  2412. * // Create same Matrix3 with using an offset into an array
  2413. * const v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2414. * const m2 = Cesium.Matrix3.fromArray(v2, 2);
  2415. */
  2416. Matrix3.fromArray = Matrix3.unpack;
  2417. /**
  2418. * Creates a Matrix3 instance from a column-major order array.
  2419. *
  2420. * @param {Number[]} values The column-major order array.
  2421. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2422. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2423. */
  2424. Matrix3.fromColumnMajorArray = function (values, result) {
  2425. //>>includeStart('debug', pragmas.debug);
  2426. RuntimeError.Check.defined("values", values);
  2427. //>>includeEnd('debug');
  2428. return Matrix3.clone(values, result);
  2429. };
  2430. /**
  2431. * Creates a Matrix3 instance from a row-major order array.
  2432. * The resulting matrix will be in column-major order.
  2433. *
  2434. * @param {Number[]} values The row-major order array.
  2435. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2436. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2437. */
  2438. Matrix3.fromRowMajorArray = function (values, result) {
  2439. //>>includeStart('debug', pragmas.debug);
  2440. RuntimeError.Check.defined("values", values);
  2441. //>>includeEnd('debug');
  2442. if (!defaultValue.defined(result)) {
  2443. return new Matrix3(
  2444. values[0],
  2445. values[1],
  2446. values[2],
  2447. values[3],
  2448. values[4],
  2449. values[5],
  2450. values[6],
  2451. values[7],
  2452. values[8]
  2453. );
  2454. }
  2455. result[0] = values[0];
  2456. result[1] = values[3];
  2457. result[2] = values[6];
  2458. result[3] = values[1];
  2459. result[4] = values[4];
  2460. result[5] = values[7];
  2461. result[6] = values[2];
  2462. result[7] = values[5];
  2463. result[8] = values[8];
  2464. return result;
  2465. };
  2466. /**
  2467. * Computes a 3x3 rotation matrix from the provided quaternion.
  2468. *
  2469. * @param {Quaternion} quaternion the quaternion to use.
  2470. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2471. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  2472. */
  2473. Matrix3.fromQuaternion = function (quaternion, result) {
  2474. //>>includeStart('debug', pragmas.debug);
  2475. RuntimeError.Check.typeOf.object("quaternion", quaternion);
  2476. //>>includeEnd('debug');
  2477. const x2 = quaternion.x * quaternion.x;
  2478. const xy = quaternion.x * quaternion.y;
  2479. const xz = quaternion.x * quaternion.z;
  2480. const xw = quaternion.x * quaternion.w;
  2481. const y2 = quaternion.y * quaternion.y;
  2482. const yz = quaternion.y * quaternion.z;
  2483. const yw = quaternion.y * quaternion.w;
  2484. const z2 = quaternion.z * quaternion.z;
  2485. const zw = quaternion.z * quaternion.w;
  2486. const w2 = quaternion.w * quaternion.w;
  2487. const m00 = x2 - y2 - z2 + w2;
  2488. const m01 = 2.0 * (xy - zw);
  2489. const m02 = 2.0 * (xz + yw);
  2490. const m10 = 2.0 * (xy + zw);
  2491. const m11 = -x2 + y2 - z2 + w2;
  2492. const m12 = 2.0 * (yz - xw);
  2493. const m20 = 2.0 * (xz - yw);
  2494. const m21 = 2.0 * (yz + xw);
  2495. const m22 = -x2 - y2 + z2 + w2;
  2496. if (!defaultValue.defined(result)) {
  2497. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2498. }
  2499. result[0] = m00;
  2500. result[1] = m10;
  2501. result[2] = m20;
  2502. result[3] = m01;
  2503. result[4] = m11;
  2504. result[5] = m21;
  2505. result[6] = m02;
  2506. result[7] = m12;
  2507. result[8] = m22;
  2508. return result;
  2509. };
  2510. /**
  2511. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  2512. *
  2513. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  2514. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2515. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  2516. */
  2517. Matrix3.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  2518. //>>includeStart('debug', pragmas.debug);
  2519. RuntimeError.Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  2520. //>>includeEnd('debug');
  2521. const cosTheta = Math.cos(-headingPitchRoll.pitch);
  2522. const cosPsi = Math.cos(-headingPitchRoll.heading);
  2523. const cosPhi = Math.cos(headingPitchRoll.roll);
  2524. const sinTheta = Math.sin(-headingPitchRoll.pitch);
  2525. const sinPsi = Math.sin(-headingPitchRoll.heading);
  2526. const sinPhi = Math.sin(headingPitchRoll.roll);
  2527. const m00 = cosTheta * cosPsi;
  2528. const m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  2529. const m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  2530. const m10 = cosTheta * sinPsi;
  2531. const m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  2532. const m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  2533. const m20 = -sinTheta;
  2534. const m21 = sinPhi * cosTheta;
  2535. const m22 = cosPhi * cosTheta;
  2536. if (!defaultValue.defined(result)) {
  2537. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2538. }
  2539. result[0] = m00;
  2540. result[1] = m10;
  2541. result[2] = m20;
  2542. result[3] = m01;
  2543. result[4] = m11;
  2544. result[5] = m21;
  2545. result[6] = m02;
  2546. result[7] = m12;
  2547. result[8] = m22;
  2548. return result;
  2549. };
  2550. /**
  2551. * Computes a Matrix3 instance representing a non-uniform scale.
  2552. *
  2553. * @param {Cartesian3} scale The x, y, and z scale factors.
  2554. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2555. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2556. *
  2557. * @example
  2558. * // Creates
  2559. * // [7.0, 0.0, 0.0]
  2560. * // [0.0, 8.0, 0.0]
  2561. * // [0.0, 0.0, 9.0]
  2562. * const m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2563. */
  2564. Matrix3.fromScale = function (scale, result) {
  2565. //>>includeStart('debug', pragmas.debug);
  2566. RuntimeError.Check.typeOf.object("scale", scale);
  2567. //>>includeEnd('debug');
  2568. if (!defaultValue.defined(result)) {
  2569. return new Matrix3(scale.x, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, scale.z);
  2570. }
  2571. result[0] = scale.x;
  2572. result[1] = 0.0;
  2573. result[2] = 0.0;
  2574. result[3] = 0.0;
  2575. result[4] = scale.y;
  2576. result[5] = 0.0;
  2577. result[6] = 0.0;
  2578. result[7] = 0.0;
  2579. result[8] = scale.z;
  2580. return result;
  2581. };
  2582. /**
  2583. * Computes a Matrix3 instance representing a uniform scale.
  2584. *
  2585. * @param {Number} scale The uniform scale factor.
  2586. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2587. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2588. *
  2589. * @example
  2590. * // Creates
  2591. * // [2.0, 0.0, 0.0]
  2592. * // [0.0, 2.0, 0.0]
  2593. * // [0.0, 0.0, 2.0]
  2594. * const m = Cesium.Matrix3.fromUniformScale(2.0);
  2595. */
  2596. Matrix3.fromUniformScale = function (scale, result) {
  2597. //>>includeStart('debug', pragmas.debug);
  2598. RuntimeError.Check.typeOf.number("scale", scale);
  2599. //>>includeEnd('debug');
  2600. if (!defaultValue.defined(result)) {
  2601. return new Matrix3(scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, scale);
  2602. }
  2603. result[0] = scale;
  2604. result[1] = 0.0;
  2605. result[2] = 0.0;
  2606. result[3] = 0.0;
  2607. result[4] = scale;
  2608. result[5] = 0.0;
  2609. result[6] = 0.0;
  2610. result[7] = 0.0;
  2611. result[8] = scale;
  2612. return result;
  2613. };
  2614. /**
  2615. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  2616. *
  2617. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  2618. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2619. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2620. *
  2621. * @example
  2622. * // Creates
  2623. * // [0.0, -9.0, 8.0]
  2624. * // [9.0, 0.0, -7.0]
  2625. * // [-8.0, 7.0, 0.0]
  2626. * const m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2627. */
  2628. Matrix3.fromCrossProduct = function (vector, result) {
  2629. //>>includeStart('debug', pragmas.debug);
  2630. RuntimeError.Check.typeOf.object("vector", vector);
  2631. //>>includeEnd('debug');
  2632. if (!defaultValue.defined(result)) {
  2633. return new Matrix3(
  2634. 0.0,
  2635. -vector.z,
  2636. vector.y,
  2637. vector.z,
  2638. 0.0,
  2639. -vector.x,
  2640. -vector.y,
  2641. vector.x,
  2642. 0.0
  2643. );
  2644. }
  2645. result[0] = 0.0;
  2646. result[1] = vector.z;
  2647. result[2] = -vector.y;
  2648. result[3] = -vector.z;
  2649. result[4] = 0.0;
  2650. result[5] = vector.x;
  2651. result[6] = vector.y;
  2652. result[7] = -vector.x;
  2653. result[8] = 0.0;
  2654. return result;
  2655. };
  2656. /**
  2657. * Creates a rotation matrix around the x-axis.
  2658. *
  2659. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2660. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2661. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2662. *
  2663. * @example
  2664. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  2665. * const p = new Cesium.Cartesian3(5, 6, 7);
  2666. * const m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  2667. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2668. */
  2669. Matrix3.fromRotationX = function (angle, result) {
  2670. //>>includeStart('debug', pragmas.debug);
  2671. RuntimeError.Check.typeOf.number("angle", angle);
  2672. //>>includeEnd('debug');
  2673. const cosAngle = Math.cos(angle);
  2674. const sinAngle = Math.sin(angle);
  2675. if (!defaultValue.defined(result)) {
  2676. return new Matrix3(
  2677. 1.0,
  2678. 0.0,
  2679. 0.0,
  2680. 0.0,
  2681. cosAngle,
  2682. -sinAngle,
  2683. 0.0,
  2684. sinAngle,
  2685. cosAngle
  2686. );
  2687. }
  2688. result[0] = 1.0;
  2689. result[1] = 0.0;
  2690. result[2] = 0.0;
  2691. result[3] = 0.0;
  2692. result[4] = cosAngle;
  2693. result[5] = sinAngle;
  2694. result[6] = 0.0;
  2695. result[7] = -sinAngle;
  2696. result[8] = cosAngle;
  2697. return result;
  2698. };
  2699. /**
  2700. * Creates a rotation matrix around the y-axis.
  2701. *
  2702. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2703. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2704. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2705. *
  2706. * @example
  2707. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  2708. * const p = new Cesium.Cartesian3(5, 6, 7);
  2709. * const m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  2710. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2711. */
  2712. Matrix3.fromRotationY = function (angle, result) {
  2713. //>>includeStart('debug', pragmas.debug);
  2714. RuntimeError.Check.typeOf.number("angle", angle);
  2715. //>>includeEnd('debug');
  2716. const cosAngle = Math.cos(angle);
  2717. const sinAngle = Math.sin(angle);
  2718. if (!defaultValue.defined(result)) {
  2719. return new Matrix3(
  2720. cosAngle,
  2721. 0.0,
  2722. sinAngle,
  2723. 0.0,
  2724. 1.0,
  2725. 0.0,
  2726. -sinAngle,
  2727. 0.0,
  2728. cosAngle
  2729. );
  2730. }
  2731. result[0] = cosAngle;
  2732. result[1] = 0.0;
  2733. result[2] = -sinAngle;
  2734. result[3] = 0.0;
  2735. result[4] = 1.0;
  2736. result[5] = 0.0;
  2737. result[6] = sinAngle;
  2738. result[7] = 0.0;
  2739. result[8] = cosAngle;
  2740. return result;
  2741. };
  2742. /**
  2743. * Creates a rotation matrix around the z-axis.
  2744. *
  2745. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2746. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2747. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2748. *
  2749. * @example
  2750. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  2751. * const p = new Cesium.Cartesian3(5, 6, 7);
  2752. * const m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  2753. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2754. */
  2755. Matrix3.fromRotationZ = function (angle, result) {
  2756. //>>includeStart('debug', pragmas.debug);
  2757. RuntimeError.Check.typeOf.number("angle", angle);
  2758. //>>includeEnd('debug');
  2759. const cosAngle = Math.cos(angle);
  2760. const sinAngle = Math.sin(angle);
  2761. if (!defaultValue.defined(result)) {
  2762. return new Matrix3(
  2763. cosAngle,
  2764. -sinAngle,
  2765. 0.0,
  2766. sinAngle,
  2767. cosAngle,
  2768. 0.0,
  2769. 0.0,
  2770. 0.0,
  2771. 1.0
  2772. );
  2773. }
  2774. result[0] = cosAngle;
  2775. result[1] = sinAngle;
  2776. result[2] = 0.0;
  2777. result[3] = -sinAngle;
  2778. result[4] = cosAngle;
  2779. result[5] = 0.0;
  2780. result[6] = 0.0;
  2781. result[7] = 0.0;
  2782. result[8] = 1.0;
  2783. return result;
  2784. };
  2785. /**
  2786. * Creates an Array from the provided Matrix3 instance.
  2787. * The array will be in column-major order.
  2788. *
  2789. * @param {Matrix3} matrix The matrix to use..
  2790. * @param {Number[]} [result] The Array onto which to store the result.
  2791. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  2792. */
  2793. Matrix3.toArray = function (matrix, result) {
  2794. //>>includeStart('debug', pragmas.debug);
  2795. RuntimeError.Check.typeOf.object("matrix", matrix);
  2796. //>>includeEnd('debug');
  2797. if (!defaultValue.defined(result)) {
  2798. return [
  2799. matrix[0],
  2800. matrix[1],
  2801. matrix[2],
  2802. matrix[3],
  2803. matrix[4],
  2804. matrix[5],
  2805. matrix[6],
  2806. matrix[7],
  2807. matrix[8],
  2808. ];
  2809. }
  2810. result[0] = matrix[0];
  2811. result[1] = matrix[1];
  2812. result[2] = matrix[2];
  2813. result[3] = matrix[3];
  2814. result[4] = matrix[4];
  2815. result[5] = matrix[5];
  2816. result[6] = matrix[6];
  2817. result[7] = matrix[7];
  2818. result[8] = matrix[8];
  2819. return result;
  2820. };
  2821. /**
  2822. * Computes the array index of the element at the provided row and column.
  2823. *
  2824. * @param {Number} column The zero-based index of the column.
  2825. * @param {Number} row The zero-based index of the row.
  2826. * @returns {Number} The index of the element at the provided row and column.
  2827. *
  2828. * @exception {DeveloperError} row must be 0, 1, or 2.
  2829. * @exception {DeveloperError} column must be 0, 1, or 2.
  2830. *
  2831. * @example
  2832. * const myMatrix = new Cesium.Matrix3();
  2833. * const column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  2834. * const column1Row0 = myMatrix[column1Row0Index]
  2835. * myMatrix[column1Row0Index] = 10.0;
  2836. */
  2837. Matrix3.getElementIndex = function (column, row) {
  2838. //>>includeStart('debug', pragmas.debug);
  2839. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  2840. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 2);
  2841. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  2842. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 2);
  2843. //>>includeEnd('debug');
  2844. return column * 3 + row;
  2845. };
  2846. /**
  2847. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  2848. *
  2849. * @param {Matrix3} matrix The matrix to use.
  2850. * @param {Number} index The zero-based index of the column to retrieve.
  2851. * @param {Cartesian3} result The object onto which to store the result.
  2852. * @returns {Cartesian3} The modified result parameter.
  2853. *
  2854. * @exception {DeveloperError} index must be 0, 1, or 2.
  2855. */
  2856. Matrix3.getColumn = function (matrix, index, result) {
  2857. //>>includeStart('debug', pragmas.debug);
  2858. RuntimeError.Check.typeOf.object("matrix", matrix);
  2859. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2860. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2861. RuntimeError.Check.typeOf.object("result", result);
  2862. //>>includeEnd('debug');
  2863. const startIndex = index * 3;
  2864. const x = matrix[startIndex];
  2865. const y = matrix[startIndex + 1];
  2866. const z = matrix[startIndex + 2];
  2867. result.x = x;
  2868. result.y = y;
  2869. result.z = z;
  2870. return result;
  2871. };
  2872. /**
  2873. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  2874. *
  2875. * @param {Matrix3} matrix The matrix to use.
  2876. * @param {Number} index The zero-based index of the column to set.
  2877. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  2878. * @param {Matrix3} result The object onto which to store the result.
  2879. * @returns {Matrix3} The modified result parameter.
  2880. *
  2881. * @exception {DeveloperError} index must be 0, 1, or 2.
  2882. */
  2883. Matrix3.setColumn = function (matrix, index, cartesian, result) {
  2884. //>>includeStart('debug', pragmas.debug);
  2885. RuntimeError.Check.typeOf.object("matrix", matrix);
  2886. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2887. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2888. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  2889. RuntimeError.Check.typeOf.object("result", result);
  2890. //>>includeEnd('debug');
  2891. result = Matrix3.clone(matrix, result);
  2892. const startIndex = index * 3;
  2893. result[startIndex] = cartesian.x;
  2894. result[startIndex + 1] = cartesian.y;
  2895. result[startIndex + 2] = cartesian.z;
  2896. return result;
  2897. };
  2898. /**
  2899. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  2900. *
  2901. * @param {Matrix3} matrix The matrix to use.
  2902. * @param {Number} index The zero-based index of the row to retrieve.
  2903. * @param {Cartesian3} result The object onto which to store the result.
  2904. * @returns {Cartesian3} The modified result parameter.
  2905. *
  2906. * @exception {DeveloperError} index must be 0, 1, or 2.
  2907. */
  2908. Matrix3.getRow = function (matrix, index, result) {
  2909. //>>includeStart('debug', pragmas.debug);
  2910. RuntimeError.Check.typeOf.object("matrix", matrix);
  2911. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2912. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2913. RuntimeError.Check.typeOf.object("result", result);
  2914. //>>includeEnd('debug');
  2915. const x = matrix[index];
  2916. const y = matrix[index + 3];
  2917. const z = matrix[index + 6];
  2918. result.x = x;
  2919. result.y = y;
  2920. result.z = z;
  2921. return result;
  2922. };
  2923. /**
  2924. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  2925. *
  2926. * @param {Matrix3} matrix The matrix to use.
  2927. * @param {Number} index The zero-based index of the row to set.
  2928. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  2929. * @param {Matrix3} result The object onto which to store the result.
  2930. * @returns {Matrix3} The modified result parameter.
  2931. *
  2932. * @exception {DeveloperError} index must be 0, 1, or 2.
  2933. */
  2934. Matrix3.setRow = function (matrix, index, cartesian, result) {
  2935. //>>includeStart('debug', pragmas.debug);
  2936. RuntimeError.Check.typeOf.object("matrix", matrix);
  2937. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2938. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2939. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  2940. RuntimeError.Check.typeOf.object("result", result);
  2941. //>>includeEnd('debug');
  2942. result = Matrix3.clone(matrix, result);
  2943. result[index] = cartesian.x;
  2944. result[index + 3] = cartesian.y;
  2945. result[index + 6] = cartesian.z;
  2946. return result;
  2947. };
  2948. const scaleScratch1$2 = new Cartesian3();
  2949. /**
  2950. * Computes a new matrix that replaces the scale with the provided scale.
  2951. * This assumes the matrix is an affine transformation.
  2952. *
  2953. * @param {Matrix3} matrix The matrix to use.
  2954. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  2955. * @param {Matrix3} result The object onto which to store the result.
  2956. * @returns {Matrix3} The modified result parameter.
  2957. *
  2958. * @see Matrix3.setUniformScale
  2959. * @see Matrix3.fromScale
  2960. * @see Matrix3.fromUniformScale
  2961. * @see Matrix3.multiplyByScale
  2962. * @see Matrix3.multiplyByUniformScale
  2963. * @see Matrix3.getScale
  2964. */
  2965. Matrix3.setScale = function (matrix, scale, result) {
  2966. //>>includeStart('debug', pragmas.debug);
  2967. RuntimeError.Check.typeOf.object("matrix", matrix);
  2968. RuntimeError.Check.typeOf.object("scale", scale);
  2969. RuntimeError.Check.typeOf.object("result", result);
  2970. //>>includeEnd('debug');
  2971. const existingScale = Matrix3.getScale(matrix, scaleScratch1$2);
  2972. const scaleRatioX = scale.x / existingScale.x;
  2973. const scaleRatioY = scale.y / existingScale.y;
  2974. const scaleRatioZ = scale.z / existingScale.z;
  2975. result[0] = matrix[0] * scaleRatioX;
  2976. result[1] = matrix[1] * scaleRatioX;
  2977. result[2] = matrix[2] * scaleRatioX;
  2978. result[3] = matrix[3] * scaleRatioY;
  2979. result[4] = matrix[4] * scaleRatioY;
  2980. result[5] = matrix[5] * scaleRatioY;
  2981. result[6] = matrix[6] * scaleRatioZ;
  2982. result[7] = matrix[7] * scaleRatioZ;
  2983. result[8] = matrix[8] * scaleRatioZ;
  2984. return result;
  2985. };
  2986. const scaleScratch2$2 = new Cartesian3();
  2987. /**
  2988. * Computes a new matrix that replaces the scale with the provided uniform scale.
  2989. * This assumes the matrix is an affine transformation.
  2990. *
  2991. * @param {Matrix3} matrix The matrix to use.
  2992. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  2993. * @param {Matrix3} result The object onto which to store the result.
  2994. * @returns {Matrix3} The modified result parameter.
  2995. *
  2996. * @see Matrix3.setScale
  2997. * @see Matrix3.fromScale
  2998. * @see Matrix3.fromUniformScale
  2999. * @see Matrix3.multiplyByScale
  3000. * @see Matrix3.multiplyByUniformScale
  3001. * @see Matrix3.getScale
  3002. */
  3003. Matrix3.setUniformScale = function (matrix, scale, result) {
  3004. //>>includeStart('debug', pragmas.debug);
  3005. RuntimeError.Check.typeOf.object("matrix", matrix);
  3006. RuntimeError.Check.typeOf.number("scale", scale);
  3007. RuntimeError.Check.typeOf.object("result", result);
  3008. //>>includeEnd('debug');
  3009. const existingScale = Matrix3.getScale(matrix, scaleScratch2$2);
  3010. const scaleRatioX = scale / existingScale.x;
  3011. const scaleRatioY = scale / existingScale.y;
  3012. const scaleRatioZ = scale / existingScale.z;
  3013. result[0] = matrix[0] * scaleRatioX;
  3014. result[1] = matrix[1] * scaleRatioX;
  3015. result[2] = matrix[2] * scaleRatioX;
  3016. result[3] = matrix[3] * scaleRatioY;
  3017. result[4] = matrix[4] * scaleRatioY;
  3018. result[5] = matrix[5] * scaleRatioY;
  3019. result[6] = matrix[6] * scaleRatioZ;
  3020. result[7] = matrix[7] * scaleRatioZ;
  3021. result[8] = matrix[8] * scaleRatioZ;
  3022. return result;
  3023. };
  3024. const scratchColumn$2 = new Cartesian3();
  3025. /**
  3026. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  3027. *
  3028. * @param {Matrix3} matrix The matrix.
  3029. * @param {Cartesian3} result The object onto which to store the result.
  3030. * @returns {Cartesian3} The modified result parameter.
  3031. *
  3032. * @see Matrix3.multiplyByScale
  3033. * @see Matrix3.multiplyByUniformScale
  3034. * @see Matrix3.fromScale
  3035. * @see Matrix3.fromUniformScale
  3036. * @see Matrix3.setScale
  3037. * @see Matrix3.setUniformScale
  3038. */
  3039. Matrix3.getScale = function (matrix, result) {
  3040. //>>includeStart('debug', pragmas.debug);
  3041. RuntimeError.Check.typeOf.object("matrix", matrix);
  3042. RuntimeError.Check.typeOf.object("result", result);
  3043. //>>includeEnd('debug');
  3044. result.x = Cartesian3.magnitude(
  3045. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$2)
  3046. );
  3047. result.y = Cartesian3.magnitude(
  3048. Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn$2)
  3049. );
  3050. result.z = Cartesian3.magnitude(
  3051. Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn$2)
  3052. );
  3053. return result;
  3054. };
  3055. const scaleScratch3$2 = new Cartesian3();
  3056. /**
  3057. * Computes the maximum scale assuming the matrix is an affine transformation.
  3058. * The maximum scale is the maximum length of the column vectors.
  3059. *
  3060. * @param {Matrix3} matrix The matrix.
  3061. * @returns {Number} The maximum scale.
  3062. */
  3063. Matrix3.getMaximumScale = function (matrix) {
  3064. Matrix3.getScale(matrix, scaleScratch3$2);
  3065. return Cartesian3.maximumComponent(scaleScratch3$2);
  3066. };
  3067. const scaleScratch4$2 = new Cartesian3();
  3068. /**
  3069. * Sets the rotation assuming the matrix is an affine transformation.
  3070. *
  3071. * @param {Matrix3} matrix The matrix.
  3072. * @param {Matrix3} rotation The rotation matrix.
  3073. * @returns {Matrix3} The modified result parameter.
  3074. *
  3075. * @see Matrix3.getRotation
  3076. */
  3077. Matrix3.setRotation = function (matrix, rotation, result) {
  3078. //>>includeStart('debug', pragmas.debug);
  3079. RuntimeError.Check.typeOf.object("matrix", matrix);
  3080. RuntimeError.Check.typeOf.object("result", result);
  3081. //>>includeEnd('debug');
  3082. const scale = Matrix3.getScale(matrix, scaleScratch4$2);
  3083. result[0] = rotation[0] * scale.x;
  3084. result[1] = rotation[1] * scale.x;
  3085. result[2] = rotation[2] * scale.x;
  3086. result[3] = rotation[3] * scale.y;
  3087. result[4] = rotation[4] * scale.y;
  3088. result[5] = rotation[5] * scale.y;
  3089. result[6] = rotation[6] * scale.z;
  3090. result[7] = rotation[7] * scale.z;
  3091. result[8] = rotation[8] * scale.z;
  3092. return result;
  3093. };
  3094. const scaleScratch5$2 = new Cartesian3();
  3095. /**
  3096. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  3097. *
  3098. * @param {Matrix3} matrix The matrix.
  3099. * @param {Matrix3} result The object onto which to store the result.
  3100. * @returns {Matrix3} The modified result parameter.
  3101. *
  3102. * @see Matrix3.setRotation
  3103. */
  3104. Matrix3.getRotation = function (matrix, result) {
  3105. //>>includeStart('debug', pragmas.debug);
  3106. RuntimeError.Check.typeOf.object("matrix", matrix);
  3107. RuntimeError.Check.typeOf.object("result", result);
  3108. //>>includeEnd('debug');
  3109. const scale = Matrix3.getScale(matrix, scaleScratch5$2);
  3110. result[0] = matrix[0] / scale.x;
  3111. result[1] = matrix[1] / scale.x;
  3112. result[2] = matrix[2] / scale.x;
  3113. result[3] = matrix[3] / scale.y;
  3114. result[4] = matrix[4] / scale.y;
  3115. result[5] = matrix[5] / scale.y;
  3116. result[6] = matrix[6] / scale.z;
  3117. result[7] = matrix[7] / scale.z;
  3118. result[8] = matrix[8] / scale.z;
  3119. return result;
  3120. };
  3121. /**
  3122. * Computes the product of two matrices.
  3123. *
  3124. * @param {Matrix3} left The first matrix.
  3125. * @param {Matrix3} right The second matrix.
  3126. * @param {Matrix3} result The object onto which to store the result.
  3127. * @returns {Matrix3} The modified result parameter.
  3128. */
  3129. Matrix3.multiply = function (left, right, result) {
  3130. //>>includeStart('debug', pragmas.debug);
  3131. RuntimeError.Check.typeOf.object("left", left);
  3132. RuntimeError.Check.typeOf.object("right", right);
  3133. RuntimeError.Check.typeOf.object("result", result);
  3134. //>>includeEnd('debug');
  3135. const column0Row0 =
  3136. left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  3137. const column0Row1 =
  3138. left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  3139. const column0Row2 =
  3140. left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  3141. const column1Row0 =
  3142. left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  3143. const column1Row1 =
  3144. left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  3145. const column1Row2 =
  3146. left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  3147. const column2Row0 =
  3148. left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  3149. const column2Row1 =
  3150. left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  3151. const column2Row2 =
  3152. left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  3153. result[0] = column0Row0;
  3154. result[1] = column0Row1;
  3155. result[2] = column0Row2;
  3156. result[3] = column1Row0;
  3157. result[4] = column1Row1;
  3158. result[5] = column1Row2;
  3159. result[6] = column2Row0;
  3160. result[7] = column2Row1;
  3161. result[8] = column2Row2;
  3162. return result;
  3163. };
  3164. /**
  3165. * Computes the sum of two matrices.
  3166. *
  3167. * @param {Matrix3} left The first matrix.
  3168. * @param {Matrix3} right The second matrix.
  3169. * @param {Matrix3} result The object onto which to store the result.
  3170. * @returns {Matrix3} The modified result parameter.
  3171. */
  3172. Matrix3.add = function (left, right, result) {
  3173. //>>includeStart('debug', pragmas.debug);
  3174. RuntimeError.Check.typeOf.object("left", left);
  3175. RuntimeError.Check.typeOf.object("right", right);
  3176. RuntimeError.Check.typeOf.object("result", result);
  3177. //>>includeEnd('debug');
  3178. result[0] = left[0] + right[0];
  3179. result[1] = left[1] + right[1];
  3180. result[2] = left[2] + right[2];
  3181. result[3] = left[3] + right[3];
  3182. result[4] = left[4] + right[4];
  3183. result[5] = left[5] + right[5];
  3184. result[6] = left[6] + right[6];
  3185. result[7] = left[7] + right[7];
  3186. result[8] = left[8] + right[8];
  3187. return result;
  3188. };
  3189. /**
  3190. * Computes the difference of two matrices.
  3191. *
  3192. * @param {Matrix3} left The first matrix.
  3193. * @param {Matrix3} right The second matrix.
  3194. * @param {Matrix3} result The object onto which to store the result.
  3195. * @returns {Matrix3} The modified result parameter.
  3196. */
  3197. Matrix3.subtract = function (left, right, result) {
  3198. //>>includeStart('debug', pragmas.debug);
  3199. RuntimeError.Check.typeOf.object("left", left);
  3200. RuntimeError.Check.typeOf.object("right", right);
  3201. RuntimeError.Check.typeOf.object("result", result);
  3202. //>>includeEnd('debug');
  3203. result[0] = left[0] - right[0];
  3204. result[1] = left[1] - right[1];
  3205. result[2] = left[2] - right[2];
  3206. result[3] = left[3] - right[3];
  3207. result[4] = left[4] - right[4];
  3208. result[5] = left[5] - right[5];
  3209. result[6] = left[6] - right[6];
  3210. result[7] = left[7] - right[7];
  3211. result[8] = left[8] - right[8];
  3212. return result;
  3213. };
  3214. /**
  3215. * Computes the product of a matrix and a column vector.
  3216. *
  3217. * @param {Matrix3} matrix The matrix.
  3218. * @param {Cartesian3} cartesian The column.
  3219. * @param {Cartesian3} result The object onto which to store the result.
  3220. * @returns {Cartesian3} The modified result parameter.
  3221. */
  3222. Matrix3.multiplyByVector = function (matrix, cartesian, result) {
  3223. //>>includeStart('debug', pragmas.debug);
  3224. RuntimeError.Check.typeOf.object("matrix", matrix);
  3225. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  3226. RuntimeError.Check.typeOf.object("result", result);
  3227. //>>includeEnd('debug');
  3228. const vX = cartesian.x;
  3229. const vY = cartesian.y;
  3230. const vZ = cartesian.z;
  3231. const x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  3232. const y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  3233. const z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  3234. result.x = x;
  3235. result.y = y;
  3236. result.z = z;
  3237. return result;
  3238. };
  3239. /**
  3240. * Computes the product of a matrix and a scalar.
  3241. *
  3242. * @param {Matrix3} matrix The matrix.
  3243. * @param {Number} scalar The number to multiply by.
  3244. * @param {Matrix3} result The object onto which to store the result.
  3245. * @returns {Matrix3} The modified result parameter.
  3246. */
  3247. Matrix3.multiplyByScalar = function (matrix, scalar, result) {
  3248. //>>includeStart('debug', pragmas.debug);
  3249. RuntimeError.Check.typeOf.object("matrix", matrix);
  3250. RuntimeError.Check.typeOf.number("scalar", scalar);
  3251. RuntimeError.Check.typeOf.object("result", result);
  3252. //>>includeEnd('debug');
  3253. result[0] = matrix[0] * scalar;
  3254. result[1] = matrix[1] * scalar;
  3255. result[2] = matrix[2] * scalar;
  3256. result[3] = matrix[3] * scalar;
  3257. result[4] = matrix[4] * scalar;
  3258. result[5] = matrix[5] * scalar;
  3259. result[6] = matrix[6] * scalar;
  3260. result[7] = matrix[7] * scalar;
  3261. result[8] = matrix[8] * scalar;
  3262. return result;
  3263. };
  3264. /**
  3265. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  3266. *
  3267. * @param {Matrix3} matrix The matrix on the left-hand side.
  3268. * @param {Number} scale The non-uniform scale on the right-hand side.
  3269. * @param {Matrix3} result The object onto which to store the result.
  3270. * @returns {Matrix3} The modified result parameter.
  3271. *
  3272. *
  3273. * @example
  3274. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  3275. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  3276. *
  3277. * @see Matrix3.multiplyByUniformScale
  3278. * @see Matrix3.fromScale
  3279. * @see Matrix3.fromUniformScale
  3280. * @see Matrix3.setScale
  3281. * @see Matrix3.setUniformScale
  3282. * @see Matrix3.getScale
  3283. */
  3284. Matrix3.multiplyByScale = function (matrix, scale, result) {
  3285. //>>includeStart('debug', pragmas.debug);
  3286. RuntimeError.Check.typeOf.object("matrix", matrix);
  3287. RuntimeError.Check.typeOf.object("scale", scale);
  3288. RuntimeError.Check.typeOf.object("result", result);
  3289. //>>includeEnd('debug');
  3290. result[0] = matrix[0] * scale.x;
  3291. result[1] = matrix[1] * scale.x;
  3292. result[2] = matrix[2] * scale.x;
  3293. result[3] = matrix[3] * scale.y;
  3294. result[4] = matrix[4] * scale.y;
  3295. result[5] = matrix[5] * scale.y;
  3296. result[6] = matrix[6] * scale.z;
  3297. result[7] = matrix[7] * scale.z;
  3298. result[8] = matrix[8] * scale.z;
  3299. return result;
  3300. };
  3301. /**
  3302. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  3303. *
  3304. * @param {Matrix3} matrix The matrix on the left-hand side.
  3305. * @param {Number} scale The uniform scale on the right-hand side.
  3306. * @param {Matrix3} result The object onto which to store the result.
  3307. * @returns {Matrix3} The modified result parameter.
  3308. *
  3309. * @example
  3310. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromUniformScale(scale), m);
  3311. * Cesium.Matrix3.multiplyByUniformScale(m, scale, m);
  3312. *
  3313. * @see Matrix3.multiplyByScale
  3314. * @see Matrix3.fromScale
  3315. * @see Matrix3.fromUniformScale
  3316. * @see Matrix3.setScale
  3317. * @see Matrix3.setUniformScale
  3318. * @see Matrix3.getScale
  3319. */
  3320. Matrix3.multiplyByUniformScale = function (matrix, scale, result) {
  3321. //>>includeStart('debug', pragmas.debug);
  3322. RuntimeError.Check.typeOf.object("matrix", matrix);
  3323. RuntimeError.Check.typeOf.number("scale", scale);
  3324. RuntimeError.Check.typeOf.object("result", result);
  3325. //>>includeEnd('debug');
  3326. result[0] = matrix[0] * scale;
  3327. result[1] = matrix[1] * scale;
  3328. result[2] = matrix[2] * scale;
  3329. result[3] = matrix[3] * scale;
  3330. result[4] = matrix[4] * scale;
  3331. result[5] = matrix[5] * scale;
  3332. result[6] = matrix[6] * scale;
  3333. result[7] = matrix[7] * scale;
  3334. result[8] = matrix[8] * scale;
  3335. return result;
  3336. };
  3337. /**
  3338. * Creates a negated copy of the provided matrix.
  3339. *
  3340. * @param {Matrix3} matrix The matrix to negate.
  3341. * @param {Matrix3} result The object onto which to store the result.
  3342. * @returns {Matrix3} The modified result parameter.
  3343. */
  3344. Matrix3.negate = function (matrix, result) {
  3345. //>>includeStart('debug', pragmas.debug);
  3346. RuntimeError.Check.typeOf.object("matrix", matrix);
  3347. RuntimeError.Check.typeOf.object("result", result);
  3348. //>>includeEnd('debug');
  3349. result[0] = -matrix[0];
  3350. result[1] = -matrix[1];
  3351. result[2] = -matrix[2];
  3352. result[3] = -matrix[3];
  3353. result[4] = -matrix[4];
  3354. result[5] = -matrix[5];
  3355. result[6] = -matrix[6];
  3356. result[7] = -matrix[7];
  3357. result[8] = -matrix[8];
  3358. return result;
  3359. };
  3360. /**
  3361. * Computes the transpose of the provided matrix.
  3362. *
  3363. * @param {Matrix3} matrix The matrix to transpose.
  3364. * @param {Matrix3} result The object onto which to store the result.
  3365. * @returns {Matrix3} The modified result parameter.
  3366. */
  3367. Matrix3.transpose = function (matrix, result) {
  3368. //>>includeStart('debug', pragmas.debug);
  3369. RuntimeError.Check.typeOf.object("matrix", matrix);
  3370. RuntimeError.Check.typeOf.object("result", result);
  3371. //>>includeEnd('debug');
  3372. const column0Row0 = matrix[0];
  3373. const column0Row1 = matrix[3];
  3374. const column0Row2 = matrix[6];
  3375. const column1Row0 = matrix[1];
  3376. const column1Row1 = matrix[4];
  3377. const column1Row2 = matrix[7];
  3378. const column2Row0 = matrix[2];
  3379. const column2Row1 = matrix[5];
  3380. const column2Row2 = matrix[8];
  3381. result[0] = column0Row0;
  3382. result[1] = column0Row1;
  3383. result[2] = column0Row2;
  3384. result[3] = column1Row0;
  3385. result[4] = column1Row1;
  3386. result[5] = column1Row2;
  3387. result[6] = column2Row0;
  3388. result[7] = column2Row1;
  3389. result[8] = column2Row2;
  3390. return result;
  3391. };
  3392. function computeFrobeniusNorm(matrix) {
  3393. let norm = 0.0;
  3394. for (let i = 0; i < 9; ++i) {
  3395. const temp = matrix[i];
  3396. norm += temp * temp;
  3397. }
  3398. return Math.sqrt(norm);
  3399. }
  3400. const rowVal = [1, 0, 0];
  3401. const colVal = [2, 2, 1];
  3402. function offDiagonalFrobeniusNorm(matrix) {
  3403. // Computes the "off-diagonal" Frobenius norm.
  3404. // Assumes matrix is symmetric.
  3405. let norm = 0.0;
  3406. for (let i = 0; i < 3; ++i) {
  3407. const temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  3408. norm += 2.0 * temp * temp;
  3409. }
  3410. return Math.sqrt(norm);
  3411. }
  3412. function shurDecomposition(matrix, result) {
  3413. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  3414. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  3415. //
  3416. // The routine takes a matrix, which is assumed to be symmetric, and
  3417. // finds the largest off-diagonal term, and then creates
  3418. // a matrix (result) which can be used to help reduce it
  3419. const tolerance = ComponentDatatype.CesiumMath.EPSILON15;
  3420. let maxDiagonal = 0.0;
  3421. let rotAxis = 1;
  3422. // find pivot (rotAxis) based on max diagonal of matrix
  3423. for (let i = 0; i < 3; ++i) {
  3424. const temp = Math.abs(
  3425. matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]
  3426. );
  3427. if (temp > maxDiagonal) {
  3428. rotAxis = i;
  3429. maxDiagonal = temp;
  3430. }
  3431. }
  3432. let c = 1.0;
  3433. let s = 0.0;
  3434. const p = rowVal[rotAxis];
  3435. const q = colVal[rotAxis];
  3436. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  3437. const qq = matrix[Matrix3.getElementIndex(q, q)];
  3438. const pp = matrix[Matrix3.getElementIndex(p, p)];
  3439. const qp = matrix[Matrix3.getElementIndex(q, p)];
  3440. const tau = (qq - pp) / 2.0 / qp;
  3441. let t;
  3442. if (tau < 0.0) {
  3443. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  3444. } else {
  3445. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  3446. }
  3447. c = 1.0 / Math.sqrt(1.0 + t * t);
  3448. s = t * c;
  3449. }
  3450. result = Matrix3.clone(Matrix3.IDENTITY, result);
  3451. result[Matrix3.getElementIndex(p, p)] = result[
  3452. Matrix3.getElementIndex(q, q)
  3453. ] = c;
  3454. result[Matrix3.getElementIndex(q, p)] = s;
  3455. result[Matrix3.getElementIndex(p, q)] = -s;
  3456. return result;
  3457. }
  3458. const jMatrix = new Matrix3();
  3459. const jMatrixTranspose = new Matrix3();
  3460. /**
  3461. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  3462. * <p>
  3463. * Returns a diagonal matrix and unitary matrix such that:
  3464. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  3465. * </p>
  3466. * <p>
  3467. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  3468. * of the unitary matrix are the corresponding eigenvectors.
  3469. * </p>
  3470. *
  3471. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  3472. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  3473. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  3474. *
  3475. * @example
  3476. * const a = //... symetric matrix
  3477. * const result = {
  3478. * unitary : new Cesium.Matrix3(),
  3479. * diagonal : new Cesium.Matrix3()
  3480. * };
  3481. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  3482. *
  3483. * const unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  3484. * const b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  3485. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  3486. *
  3487. * const lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  3488. * const v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  3489. * const c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  3490. */
  3491. Matrix3.computeEigenDecomposition = function (matrix, result) {
  3492. //>>includeStart('debug', pragmas.debug);
  3493. RuntimeError.Check.typeOf.object("matrix", matrix);
  3494. //>>includeEnd('debug');
  3495. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  3496. // section 8.4.3 The Classical Jacobi Algorithm
  3497. const tolerance = ComponentDatatype.CesiumMath.EPSILON20;
  3498. const maxSweeps = 10;
  3499. let count = 0;
  3500. let sweep = 0;
  3501. if (!defaultValue.defined(result)) {
  3502. result = {};
  3503. }
  3504. const unitaryMatrix = (result.unitary = Matrix3.clone(
  3505. Matrix3.IDENTITY,
  3506. result.unitary
  3507. ));
  3508. const diagMatrix = (result.diagonal = Matrix3.clone(matrix, result.diagonal));
  3509. const epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  3510. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  3511. shurDecomposition(diagMatrix, jMatrix);
  3512. Matrix3.transpose(jMatrix, jMatrixTranspose);
  3513. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  3514. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  3515. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  3516. if (++count > 2) {
  3517. ++sweep;
  3518. count = 0;
  3519. }
  3520. }
  3521. return result;
  3522. };
  3523. /**
  3524. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  3525. *
  3526. * @param {Matrix3} matrix The matrix with signed elements.
  3527. * @param {Matrix3} result The object onto which to store the result.
  3528. * @returns {Matrix3} The modified result parameter.
  3529. */
  3530. Matrix3.abs = function (matrix, result) {
  3531. //>>includeStart('debug', pragmas.debug);
  3532. RuntimeError.Check.typeOf.object("matrix", matrix);
  3533. RuntimeError.Check.typeOf.object("result", result);
  3534. //>>includeEnd('debug');
  3535. result[0] = Math.abs(matrix[0]);
  3536. result[1] = Math.abs(matrix[1]);
  3537. result[2] = Math.abs(matrix[2]);
  3538. result[3] = Math.abs(matrix[3]);
  3539. result[4] = Math.abs(matrix[4]);
  3540. result[5] = Math.abs(matrix[5]);
  3541. result[6] = Math.abs(matrix[6]);
  3542. result[7] = Math.abs(matrix[7]);
  3543. result[8] = Math.abs(matrix[8]);
  3544. return result;
  3545. };
  3546. /**
  3547. * Computes the determinant of the provided matrix.
  3548. *
  3549. * @param {Matrix3} matrix The matrix to use.
  3550. * @returns {Number} The value of the determinant of the matrix.
  3551. */
  3552. Matrix3.determinant = function (matrix) {
  3553. //>>includeStart('debug', pragmas.debug);
  3554. RuntimeError.Check.typeOf.object("matrix", matrix);
  3555. //>>includeEnd('debug');
  3556. const m11 = matrix[0];
  3557. const m21 = matrix[3];
  3558. const m31 = matrix[6];
  3559. const m12 = matrix[1];
  3560. const m22 = matrix[4];
  3561. const m32 = matrix[7];
  3562. const m13 = matrix[2];
  3563. const m23 = matrix[5];
  3564. const m33 = matrix[8];
  3565. return (
  3566. m11 * (m22 * m33 - m23 * m32) +
  3567. m12 * (m23 * m31 - m21 * m33) +
  3568. m13 * (m21 * m32 - m22 * m31)
  3569. );
  3570. };
  3571. /**
  3572. * Computes the inverse of the provided matrix.
  3573. *
  3574. * @param {Matrix3} matrix The matrix to invert.
  3575. * @param {Matrix3} result The object onto which to store the result.
  3576. * @returns {Matrix3} The modified result parameter.
  3577. *
  3578. * @exception {DeveloperError} matrix is not invertible.
  3579. */
  3580. Matrix3.inverse = function (matrix, result) {
  3581. //>>includeStart('debug', pragmas.debug);
  3582. RuntimeError.Check.typeOf.object("matrix", matrix);
  3583. RuntimeError.Check.typeOf.object("result", result);
  3584. //>>includeEnd('debug');
  3585. const m11 = matrix[0];
  3586. const m21 = matrix[1];
  3587. const m31 = matrix[2];
  3588. const m12 = matrix[3];
  3589. const m22 = matrix[4];
  3590. const m32 = matrix[5];
  3591. const m13 = matrix[6];
  3592. const m23 = matrix[7];
  3593. const m33 = matrix[8];
  3594. const determinant = Matrix3.determinant(matrix);
  3595. //>>includeStart('debug', pragmas.debug);
  3596. if (Math.abs(determinant) <= ComponentDatatype.CesiumMath.EPSILON15) {
  3597. throw new RuntimeError.DeveloperError("matrix is not invertible");
  3598. }
  3599. //>>includeEnd('debug');
  3600. result[0] = m22 * m33 - m23 * m32;
  3601. result[1] = m23 * m31 - m21 * m33;
  3602. result[2] = m21 * m32 - m22 * m31;
  3603. result[3] = m13 * m32 - m12 * m33;
  3604. result[4] = m11 * m33 - m13 * m31;
  3605. result[5] = m12 * m31 - m11 * m32;
  3606. result[6] = m12 * m23 - m13 * m22;
  3607. result[7] = m13 * m21 - m11 * m23;
  3608. result[8] = m11 * m22 - m12 * m21;
  3609. const scale = 1.0 / determinant;
  3610. return Matrix3.multiplyByScalar(result, scale, result);
  3611. };
  3612. const scratchTransposeMatrix$1 = new Matrix3();
  3613. /**
  3614. * Computes the inverse transpose of a matrix.
  3615. *
  3616. * @param {Matrix3} matrix The matrix to transpose and invert.
  3617. * @param {Matrix3} result The object onto which to store the result.
  3618. * @returns {Matrix3} The modified result parameter.
  3619. */
  3620. Matrix3.inverseTranspose = function (matrix, result) {
  3621. //>>includeStart('debug', pragmas.debug);
  3622. RuntimeError.Check.typeOf.object("matrix", matrix);
  3623. RuntimeError.Check.typeOf.object("result", result);
  3624. //>>includeEnd('debug');
  3625. return Matrix3.inverse(
  3626. Matrix3.transpose(matrix, scratchTransposeMatrix$1),
  3627. result
  3628. );
  3629. };
  3630. /**
  3631. * Compares the provided matrices componentwise and returns
  3632. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3633. *
  3634. * @param {Matrix3} [left] The first matrix.
  3635. * @param {Matrix3} [right] The second matrix.
  3636. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3637. */
  3638. Matrix3.equals = function (left, right) {
  3639. return (
  3640. left === right ||
  3641. (defaultValue.defined(left) &&
  3642. defaultValue.defined(right) &&
  3643. left[0] === right[0] &&
  3644. left[1] === right[1] &&
  3645. left[2] === right[2] &&
  3646. left[3] === right[3] &&
  3647. left[4] === right[4] &&
  3648. left[5] === right[5] &&
  3649. left[6] === right[6] &&
  3650. left[7] === right[7] &&
  3651. left[8] === right[8])
  3652. );
  3653. };
  3654. /**
  3655. * Compares the provided matrices componentwise and returns
  3656. * <code>true</code> if they are within the provided epsilon,
  3657. * <code>false</code> otherwise.
  3658. *
  3659. * @param {Matrix3} [left] The first matrix.
  3660. * @param {Matrix3} [right] The second matrix.
  3661. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  3662. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3663. */
  3664. Matrix3.equalsEpsilon = function (left, right, epsilon) {
  3665. epsilon = defaultValue.defaultValue(epsilon, 0);
  3666. return (
  3667. left === right ||
  3668. (defaultValue.defined(left) &&
  3669. defaultValue.defined(right) &&
  3670. Math.abs(left[0] - right[0]) <= epsilon &&
  3671. Math.abs(left[1] - right[1]) <= epsilon &&
  3672. Math.abs(left[2] - right[2]) <= epsilon &&
  3673. Math.abs(left[3] - right[3]) <= epsilon &&
  3674. Math.abs(left[4] - right[4]) <= epsilon &&
  3675. Math.abs(left[5] - right[5]) <= epsilon &&
  3676. Math.abs(left[6] - right[6]) <= epsilon &&
  3677. Math.abs(left[7] - right[7]) <= epsilon &&
  3678. Math.abs(left[8] - right[8]) <= epsilon)
  3679. );
  3680. };
  3681. /**
  3682. * An immutable Matrix3 instance initialized to the identity matrix.
  3683. *
  3684. * @type {Matrix3}
  3685. * @constant
  3686. */
  3687. Matrix3.IDENTITY = Object.freeze(
  3688. new Matrix3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  3689. );
  3690. /**
  3691. * An immutable Matrix3 instance initialized to the zero matrix.
  3692. *
  3693. * @type {Matrix3}
  3694. * @constant
  3695. */
  3696. Matrix3.ZERO = Object.freeze(
  3697. new Matrix3(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  3698. );
  3699. /**
  3700. * The index into Matrix3 for column 0, row 0.
  3701. *
  3702. * @type {Number}
  3703. * @constant
  3704. */
  3705. Matrix3.COLUMN0ROW0 = 0;
  3706. /**
  3707. * The index into Matrix3 for column 0, row 1.
  3708. *
  3709. * @type {Number}
  3710. * @constant
  3711. */
  3712. Matrix3.COLUMN0ROW1 = 1;
  3713. /**
  3714. * The index into Matrix3 for column 0, row 2.
  3715. *
  3716. * @type {Number}
  3717. * @constant
  3718. */
  3719. Matrix3.COLUMN0ROW2 = 2;
  3720. /**
  3721. * The index into Matrix3 for column 1, row 0.
  3722. *
  3723. * @type {Number}
  3724. * @constant
  3725. */
  3726. Matrix3.COLUMN1ROW0 = 3;
  3727. /**
  3728. * The index into Matrix3 for column 1, row 1.
  3729. *
  3730. * @type {Number}
  3731. * @constant
  3732. */
  3733. Matrix3.COLUMN1ROW1 = 4;
  3734. /**
  3735. * The index into Matrix3 for column 1, row 2.
  3736. *
  3737. * @type {Number}
  3738. * @constant
  3739. */
  3740. Matrix3.COLUMN1ROW2 = 5;
  3741. /**
  3742. * The index into Matrix3 for column 2, row 0.
  3743. *
  3744. * @type {Number}
  3745. * @constant
  3746. */
  3747. Matrix3.COLUMN2ROW0 = 6;
  3748. /**
  3749. * The index into Matrix3 for column 2, row 1.
  3750. *
  3751. * @type {Number}
  3752. * @constant
  3753. */
  3754. Matrix3.COLUMN2ROW1 = 7;
  3755. /**
  3756. * The index into Matrix3 for column 2, row 2.
  3757. *
  3758. * @type {Number}
  3759. * @constant
  3760. */
  3761. Matrix3.COLUMN2ROW2 = 8;
  3762. Object.defineProperties(Matrix3.prototype, {
  3763. /**
  3764. * Gets the number of items in the collection.
  3765. * @memberof Matrix3.prototype
  3766. *
  3767. * @type {Number}
  3768. */
  3769. length: {
  3770. get: function () {
  3771. return Matrix3.packedLength;
  3772. },
  3773. },
  3774. });
  3775. /**
  3776. * Duplicates the provided Matrix3 instance.
  3777. *
  3778. * @param {Matrix3} [result] The object onto which to store the result.
  3779. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  3780. */
  3781. Matrix3.prototype.clone = function (result) {
  3782. return Matrix3.clone(this, result);
  3783. };
  3784. /**
  3785. * Compares this matrix to the provided matrix componentwise and returns
  3786. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3787. *
  3788. * @param {Matrix3} [right] The right hand side matrix.
  3789. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3790. */
  3791. Matrix3.prototype.equals = function (right) {
  3792. return Matrix3.equals(this, right);
  3793. };
  3794. /**
  3795. * @private
  3796. */
  3797. Matrix3.equalsArray = function (matrix, array, offset) {
  3798. return (
  3799. matrix[0] === array[offset] &&
  3800. matrix[1] === array[offset + 1] &&
  3801. matrix[2] === array[offset + 2] &&
  3802. matrix[3] === array[offset + 3] &&
  3803. matrix[4] === array[offset + 4] &&
  3804. matrix[5] === array[offset + 5] &&
  3805. matrix[6] === array[offset + 6] &&
  3806. matrix[7] === array[offset + 7] &&
  3807. matrix[8] === array[offset + 8]
  3808. );
  3809. };
  3810. /**
  3811. * Compares this matrix to the provided matrix componentwise and returns
  3812. * <code>true</code> if they are within the provided epsilon,
  3813. * <code>false</code> otherwise.
  3814. *
  3815. * @param {Matrix3} [right] The right hand side matrix.
  3816. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  3817. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  3818. */
  3819. Matrix3.prototype.equalsEpsilon = function (right, epsilon) {
  3820. return Matrix3.equalsEpsilon(this, right, epsilon);
  3821. };
  3822. /**
  3823. * Creates a string representing this Matrix with each row being
  3824. * on a separate line and in the format '(column0, column1, column2)'.
  3825. *
  3826. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  3827. */
  3828. Matrix3.prototype.toString = function () {
  3829. return (
  3830. `(${this[0]}, ${this[3]}, ${this[6]})\n` +
  3831. `(${this[1]}, ${this[4]}, ${this[7]})\n` +
  3832. `(${this[2]}, ${this[5]}, ${this[8]})`
  3833. );
  3834. };
  3835. /**
  3836. * A 4D Cartesian point.
  3837. * @alias Cartesian4
  3838. * @constructor
  3839. *
  3840. * @param {Number} [x=0.0] The X component.
  3841. * @param {Number} [y=0.0] The Y component.
  3842. * @param {Number} [z=0.0] The Z component.
  3843. * @param {Number} [w=0.0] The W component.
  3844. *
  3845. * @see Cartesian2
  3846. * @see Cartesian3
  3847. * @see Packable
  3848. */
  3849. function Cartesian4(x, y, z, w) {
  3850. /**
  3851. * The X component.
  3852. * @type {Number}
  3853. * @default 0.0
  3854. */
  3855. this.x = defaultValue.defaultValue(x, 0.0);
  3856. /**
  3857. * The Y component.
  3858. * @type {Number}
  3859. * @default 0.0
  3860. */
  3861. this.y = defaultValue.defaultValue(y, 0.0);
  3862. /**
  3863. * The Z component.
  3864. * @type {Number}
  3865. * @default 0.0
  3866. */
  3867. this.z = defaultValue.defaultValue(z, 0.0);
  3868. /**
  3869. * The W component.
  3870. * @type {Number}
  3871. * @default 0.0
  3872. */
  3873. this.w = defaultValue.defaultValue(w, 0.0);
  3874. }
  3875. /**
  3876. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  3877. *
  3878. * @param {Number} x The x coordinate.
  3879. * @param {Number} y The y coordinate.
  3880. * @param {Number} z The z coordinate.
  3881. * @param {Number} w The w coordinate.
  3882. * @param {Cartesian4} [result] The object onto which to store the result.
  3883. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3884. */
  3885. Cartesian4.fromElements = function (x, y, z, w, result) {
  3886. if (!defaultValue.defined(result)) {
  3887. return new Cartesian4(x, y, z, w);
  3888. }
  3889. result.x = x;
  3890. result.y = y;
  3891. result.z = z;
  3892. result.w = w;
  3893. return result;
  3894. };
  3895. /**
  3896. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  3897. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  3898. *
  3899. * @param {Color} color The source color.
  3900. * @param {Cartesian4} [result] The object onto which to store the result.
  3901. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3902. */
  3903. Cartesian4.fromColor = function (color, result) {
  3904. //>>includeStart('debug', pragmas.debug);
  3905. RuntimeError.Check.typeOf.object("color", color);
  3906. //>>includeEnd('debug');
  3907. if (!defaultValue.defined(result)) {
  3908. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  3909. }
  3910. result.x = color.red;
  3911. result.y = color.green;
  3912. result.z = color.blue;
  3913. result.w = color.alpha;
  3914. return result;
  3915. };
  3916. /**
  3917. * Duplicates a Cartesian4 instance.
  3918. *
  3919. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  3920. * @param {Cartesian4} [result] The object onto which to store the result.
  3921. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  3922. */
  3923. Cartesian4.clone = function (cartesian, result) {
  3924. if (!defaultValue.defined(cartesian)) {
  3925. return undefined;
  3926. }
  3927. if (!defaultValue.defined(result)) {
  3928. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  3929. }
  3930. result.x = cartesian.x;
  3931. result.y = cartesian.y;
  3932. result.z = cartesian.z;
  3933. result.w = cartesian.w;
  3934. return result;
  3935. };
  3936. /**
  3937. * The number of elements used to pack the object into an array.
  3938. * @type {Number}
  3939. */
  3940. Cartesian4.packedLength = 4;
  3941. /**
  3942. * Stores the provided instance into the provided array.
  3943. *
  3944. * @param {Cartesian4} value The value to pack.
  3945. * @param {Number[]} array The array to pack into.
  3946. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  3947. *
  3948. * @returns {Number[]} The array that was packed into
  3949. */
  3950. Cartesian4.pack = function (value, array, startingIndex) {
  3951. //>>includeStart('debug', pragmas.debug);
  3952. RuntimeError.Check.typeOf.object("value", value);
  3953. RuntimeError.Check.defined("array", array);
  3954. //>>includeEnd('debug');
  3955. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3956. array[startingIndex++] = value.x;
  3957. array[startingIndex++] = value.y;
  3958. array[startingIndex++] = value.z;
  3959. array[startingIndex] = value.w;
  3960. return array;
  3961. };
  3962. /**
  3963. * Retrieves an instance from a packed array.
  3964. *
  3965. * @param {Number[]} array The packed array.
  3966. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  3967. * @param {Cartesian4} [result] The object into which to store the result.
  3968. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3969. */
  3970. Cartesian4.unpack = function (array, startingIndex, result) {
  3971. //>>includeStart('debug', pragmas.debug);
  3972. RuntimeError.Check.defined("array", array);
  3973. //>>includeEnd('debug');
  3974. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3975. if (!defaultValue.defined(result)) {
  3976. result = new Cartesian4();
  3977. }
  3978. result.x = array[startingIndex++];
  3979. result.y = array[startingIndex++];
  3980. result.z = array[startingIndex++];
  3981. result.w = array[startingIndex];
  3982. return result;
  3983. };
  3984. /**
  3985. * Flattens an array of Cartesian4s into an array of components.
  3986. *
  3987. * @param {Cartesian4[]} array The array of cartesians to pack.
  3988. * @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.
  3989. * @returns {Number[]} The packed array.
  3990. */
  3991. Cartesian4.packArray = function (array, result) {
  3992. //>>includeStart('debug', pragmas.debug);
  3993. RuntimeError.Check.defined("array", array);
  3994. //>>includeEnd('debug');
  3995. const length = array.length;
  3996. const resultLength = length * 4;
  3997. if (!defaultValue.defined(result)) {
  3998. result = new Array(resultLength);
  3999. } else if (!Array.isArray(result) && result.length !== resultLength) {
  4000. //>>includeStart('debug', pragmas.debug);
  4001. throw new RuntimeError.DeveloperError(
  4002. "If result is a typed array, it must have exactly array.length * 4 elements"
  4003. );
  4004. //>>includeEnd('debug');
  4005. } else if (result.length !== resultLength) {
  4006. result.length = resultLength;
  4007. }
  4008. for (let i = 0; i < length; ++i) {
  4009. Cartesian4.pack(array[i], result, i * 4);
  4010. }
  4011. return result;
  4012. };
  4013. /**
  4014. * Unpacks an array of cartesian components into an array of Cartesian4s.
  4015. *
  4016. * @param {Number[]} array The array of components to unpack.
  4017. * @param {Cartesian4[]} [result] The array onto which to store the result.
  4018. * @returns {Cartesian4[]} The unpacked array.
  4019. */
  4020. Cartesian4.unpackArray = function (array, result) {
  4021. //>>includeStart('debug', pragmas.debug);
  4022. RuntimeError.Check.defined("array", array);
  4023. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  4024. if (array.length % 4 !== 0) {
  4025. throw new RuntimeError.DeveloperError("array length must be a multiple of 4.");
  4026. }
  4027. //>>includeEnd('debug');
  4028. const length = array.length;
  4029. if (!defaultValue.defined(result)) {
  4030. result = new Array(length / 4);
  4031. } else {
  4032. result.length = length / 4;
  4033. }
  4034. for (let i = 0; i < length; i += 4) {
  4035. const index = i / 4;
  4036. result[index] = Cartesian4.unpack(array, i, result[index]);
  4037. }
  4038. return result;
  4039. };
  4040. /**
  4041. * Creates a Cartesian4 from four consecutive elements in an array.
  4042. * @function
  4043. *
  4044. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  4045. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  4046. * @param {Cartesian4} [result] The object onto which to store the result.
  4047. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4048. *
  4049. * @example
  4050. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  4051. * const v = [1.0, 2.0, 3.0, 4.0];
  4052. * const p = Cesium.Cartesian4.fromArray(v);
  4053. *
  4054. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  4055. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  4056. * const p2 = Cesium.Cartesian4.fromArray(v2, 2);
  4057. */
  4058. Cartesian4.fromArray = Cartesian4.unpack;
  4059. /**
  4060. * Computes the value of the maximum component for the supplied Cartesian.
  4061. *
  4062. * @param {Cartesian4} cartesian The cartesian to use.
  4063. * @returns {Number} The value of the maximum component.
  4064. */
  4065. Cartesian4.maximumComponent = function (cartesian) {
  4066. //>>includeStart('debug', pragmas.debug);
  4067. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4068. //>>includeEnd('debug');
  4069. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4070. };
  4071. /**
  4072. * Computes the value of the minimum component for the supplied Cartesian.
  4073. *
  4074. * @param {Cartesian4} cartesian The cartesian to use.
  4075. * @returns {Number} The value of the minimum component.
  4076. */
  4077. Cartesian4.minimumComponent = function (cartesian) {
  4078. //>>includeStart('debug', pragmas.debug);
  4079. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4080. //>>includeEnd('debug');
  4081. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4082. };
  4083. /**
  4084. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  4085. *
  4086. * @param {Cartesian4} first A cartesian to compare.
  4087. * @param {Cartesian4} second A cartesian to compare.
  4088. * @param {Cartesian4} result The object into which to store the result.
  4089. * @returns {Cartesian4} A cartesian with the minimum components.
  4090. */
  4091. Cartesian4.minimumByComponent = function (first, second, result) {
  4092. //>>includeStart('debug', pragmas.debug);
  4093. RuntimeError.Check.typeOf.object("first", first);
  4094. RuntimeError.Check.typeOf.object("second", second);
  4095. RuntimeError.Check.typeOf.object("result", result);
  4096. //>>includeEnd('debug');
  4097. result.x = Math.min(first.x, second.x);
  4098. result.y = Math.min(first.y, second.y);
  4099. result.z = Math.min(first.z, second.z);
  4100. result.w = Math.min(first.w, second.w);
  4101. return result;
  4102. };
  4103. /**
  4104. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  4105. *
  4106. * @param {Cartesian4} first A cartesian to compare.
  4107. * @param {Cartesian4} second A cartesian to compare.
  4108. * @param {Cartesian4} result The object into which to store the result.
  4109. * @returns {Cartesian4} A cartesian with the maximum components.
  4110. */
  4111. Cartesian4.maximumByComponent = function (first, second, result) {
  4112. //>>includeStart('debug', pragmas.debug);
  4113. RuntimeError.Check.typeOf.object("first", first);
  4114. RuntimeError.Check.typeOf.object("second", second);
  4115. RuntimeError.Check.typeOf.object("result", result);
  4116. //>>includeEnd('debug');
  4117. result.x = Math.max(first.x, second.x);
  4118. result.y = Math.max(first.y, second.y);
  4119. result.z = Math.max(first.z, second.z);
  4120. result.w = Math.max(first.w, second.w);
  4121. return result;
  4122. };
  4123. /**
  4124. * Constrain a value to lie between two values.
  4125. *
  4126. * @param {Cartesian4} value The value to clamp.
  4127. * @param {Cartesian4} min The minimum bound.
  4128. * @param {Cartesian4} max The maximum bound.
  4129. * @param {Cartesian4} result The object into which to store the result.
  4130. * @returns {Cartesian4} The clamped value such that min <= result <= max.
  4131. */
  4132. Cartesian4.clamp = function (value, min, max, result) {
  4133. //>>includeStart('debug', pragmas.debug);
  4134. RuntimeError.Check.typeOf.object("value", value);
  4135. RuntimeError.Check.typeOf.object("min", min);
  4136. RuntimeError.Check.typeOf.object("max", max);
  4137. RuntimeError.Check.typeOf.object("result", result);
  4138. //>>includeEnd('debug');
  4139. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  4140. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  4141. const z = ComponentDatatype.CesiumMath.clamp(value.z, min.z, max.z);
  4142. const w = ComponentDatatype.CesiumMath.clamp(value.w, min.w, max.w);
  4143. result.x = x;
  4144. result.y = y;
  4145. result.z = z;
  4146. result.w = w;
  4147. return result;
  4148. };
  4149. /**
  4150. * Computes the provided Cartesian's squared magnitude.
  4151. *
  4152. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  4153. * @returns {Number} The squared magnitude.
  4154. */
  4155. Cartesian4.magnitudeSquared = function (cartesian) {
  4156. //>>includeStart('debug', pragmas.debug);
  4157. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4158. //>>includeEnd('debug');
  4159. return (
  4160. cartesian.x * cartesian.x +
  4161. cartesian.y * cartesian.y +
  4162. cartesian.z * cartesian.z +
  4163. cartesian.w * cartesian.w
  4164. );
  4165. };
  4166. /**
  4167. * Computes the Cartesian's magnitude (length).
  4168. *
  4169. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  4170. * @returns {Number} The magnitude.
  4171. */
  4172. Cartesian4.magnitude = function (cartesian) {
  4173. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  4174. };
  4175. const distanceScratch$1 = new Cartesian4();
  4176. /**
  4177. * Computes the 4-space distance between two points.
  4178. *
  4179. * @param {Cartesian4} left The first point to compute the distance from.
  4180. * @param {Cartesian4} right The second point to compute the distance to.
  4181. * @returns {Number} The distance between two points.
  4182. *
  4183. * @example
  4184. * // Returns 1.0
  4185. * const d = Cesium.Cartesian4.distance(
  4186. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4187. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  4188. */
  4189. Cartesian4.distance = function (left, right) {
  4190. //>>includeStart('debug', pragmas.debug);
  4191. RuntimeError.Check.typeOf.object("left", left);
  4192. RuntimeError.Check.typeOf.object("right", right);
  4193. //>>includeEnd('debug');
  4194. Cartesian4.subtract(left, right, distanceScratch$1);
  4195. return Cartesian4.magnitude(distanceScratch$1);
  4196. };
  4197. /**
  4198. * Computes the squared distance between two points. Comparing squared distances
  4199. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  4200. *
  4201. * @param {Cartesian4} left The first point to compute the distance from.
  4202. * @param {Cartesian4} right The second point to compute the distance to.
  4203. * @returns {Number} The distance between two points.
  4204. *
  4205. * @example
  4206. * // Returns 4.0, not 2.0
  4207. * const d = Cesium.Cartesian4.distance(
  4208. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4209. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  4210. */
  4211. Cartesian4.distanceSquared = function (left, right) {
  4212. //>>includeStart('debug', pragmas.debug);
  4213. RuntimeError.Check.typeOf.object("left", left);
  4214. RuntimeError.Check.typeOf.object("right", right);
  4215. //>>includeEnd('debug');
  4216. Cartesian4.subtract(left, right, distanceScratch$1);
  4217. return Cartesian4.magnitudeSquared(distanceScratch$1);
  4218. };
  4219. /**
  4220. * Computes the normalized form of the supplied Cartesian.
  4221. *
  4222. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  4223. * @param {Cartesian4} result The object onto which to store the result.
  4224. * @returns {Cartesian4} The modified result parameter.
  4225. */
  4226. Cartesian4.normalize = function (cartesian, result) {
  4227. //>>includeStart('debug', pragmas.debug);
  4228. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4229. RuntimeError.Check.typeOf.object("result", result);
  4230. //>>includeEnd('debug');
  4231. const magnitude = Cartesian4.magnitude(cartesian);
  4232. result.x = cartesian.x / magnitude;
  4233. result.y = cartesian.y / magnitude;
  4234. result.z = cartesian.z / magnitude;
  4235. result.w = cartesian.w / magnitude;
  4236. //>>includeStart('debug', pragmas.debug);
  4237. if (
  4238. isNaN(result.x) ||
  4239. isNaN(result.y) ||
  4240. isNaN(result.z) ||
  4241. isNaN(result.w)
  4242. ) {
  4243. throw new RuntimeError.DeveloperError("normalized result is not a number");
  4244. }
  4245. //>>includeEnd('debug');
  4246. return result;
  4247. };
  4248. /**
  4249. * Computes the dot (scalar) product of two Cartesians.
  4250. *
  4251. * @param {Cartesian4} left The first Cartesian.
  4252. * @param {Cartesian4} right The second Cartesian.
  4253. * @returns {Number} The dot product.
  4254. */
  4255. Cartesian4.dot = function (left, right) {
  4256. //>>includeStart('debug', pragmas.debug);
  4257. RuntimeError.Check.typeOf.object("left", left);
  4258. RuntimeError.Check.typeOf.object("right", right);
  4259. //>>includeEnd('debug');
  4260. return (
  4261. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  4262. );
  4263. };
  4264. /**
  4265. * Computes the componentwise product of two Cartesians.
  4266. *
  4267. * @param {Cartesian4} left The first Cartesian.
  4268. * @param {Cartesian4} right The second Cartesian.
  4269. * @param {Cartesian4} result The object onto which to store the result.
  4270. * @returns {Cartesian4} The modified result parameter.
  4271. */
  4272. Cartesian4.multiplyComponents = function (left, right, result) {
  4273. //>>includeStart('debug', pragmas.debug);
  4274. RuntimeError.Check.typeOf.object("left", left);
  4275. RuntimeError.Check.typeOf.object("right", right);
  4276. RuntimeError.Check.typeOf.object("result", result);
  4277. //>>includeEnd('debug');
  4278. result.x = left.x * right.x;
  4279. result.y = left.y * right.y;
  4280. result.z = left.z * right.z;
  4281. result.w = left.w * right.w;
  4282. return result;
  4283. };
  4284. /**
  4285. * Computes the componentwise quotient of two Cartesians.
  4286. *
  4287. * @param {Cartesian4} left The first Cartesian.
  4288. * @param {Cartesian4} right The second Cartesian.
  4289. * @param {Cartesian4} result The object onto which to store the result.
  4290. * @returns {Cartesian4} The modified result parameter.
  4291. */
  4292. Cartesian4.divideComponents = function (left, right, result) {
  4293. //>>includeStart('debug', pragmas.debug);
  4294. RuntimeError.Check.typeOf.object("left", left);
  4295. RuntimeError.Check.typeOf.object("right", right);
  4296. RuntimeError.Check.typeOf.object("result", result);
  4297. //>>includeEnd('debug');
  4298. result.x = left.x / right.x;
  4299. result.y = left.y / right.y;
  4300. result.z = left.z / right.z;
  4301. result.w = left.w / right.w;
  4302. return result;
  4303. };
  4304. /**
  4305. * Computes the componentwise sum of two Cartesians.
  4306. *
  4307. * @param {Cartesian4} left The first Cartesian.
  4308. * @param {Cartesian4} right The second Cartesian.
  4309. * @param {Cartesian4} result The object onto which to store the result.
  4310. * @returns {Cartesian4} The modified result parameter.
  4311. */
  4312. Cartesian4.add = function (left, right, result) {
  4313. //>>includeStart('debug', pragmas.debug);
  4314. RuntimeError.Check.typeOf.object("left", left);
  4315. RuntimeError.Check.typeOf.object("right", right);
  4316. RuntimeError.Check.typeOf.object("result", result);
  4317. //>>includeEnd('debug');
  4318. result.x = left.x + right.x;
  4319. result.y = left.y + right.y;
  4320. result.z = left.z + right.z;
  4321. result.w = left.w + right.w;
  4322. return result;
  4323. };
  4324. /**
  4325. * Computes the componentwise difference of two Cartesians.
  4326. *
  4327. * @param {Cartesian4} left The first Cartesian.
  4328. * @param {Cartesian4} right The second Cartesian.
  4329. * @param {Cartesian4} result The object onto which to store the result.
  4330. * @returns {Cartesian4} The modified result parameter.
  4331. */
  4332. Cartesian4.subtract = function (left, right, result) {
  4333. //>>includeStart('debug', pragmas.debug);
  4334. RuntimeError.Check.typeOf.object("left", left);
  4335. RuntimeError.Check.typeOf.object("right", right);
  4336. RuntimeError.Check.typeOf.object("result", result);
  4337. //>>includeEnd('debug');
  4338. result.x = left.x - right.x;
  4339. result.y = left.y - right.y;
  4340. result.z = left.z - right.z;
  4341. result.w = left.w - right.w;
  4342. return result;
  4343. };
  4344. /**
  4345. * Multiplies the provided Cartesian componentwise by the provided scalar.
  4346. *
  4347. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  4348. * @param {Number} scalar The scalar to multiply with.
  4349. * @param {Cartesian4} result The object onto which to store the result.
  4350. * @returns {Cartesian4} The modified result parameter.
  4351. */
  4352. Cartesian4.multiplyByScalar = function (cartesian, scalar, result) {
  4353. //>>includeStart('debug', pragmas.debug);
  4354. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4355. RuntimeError.Check.typeOf.number("scalar", scalar);
  4356. RuntimeError.Check.typeOf.object("result", result);
  4357. //>>includeEnd('debug');
  4358. result.x = cartesian.x * scalar;
  4359. result.y = cartesian.y * scalar;
  4360. result.z = cartesian.z * scalar;
  4361. result.w = cartesian.w * scalar;
  4362. return result;
  4363. };
  4364. /**
  4365. * Divides the provided Cartesian componentwise by the provided scalar.
  4366. *
  4367. * @param {Cartesian4} cartesian The Cartesian to be divided.
  4368. * @param {Number} scalar The scalar to divide by.
  4369. * @param {Cartesian4} result The object onto which to store the result.
  4370. * @returns {Cartesian4} The modified result parameter.
  4371. */
  4372. Cartesian4.divideByScalar = function (cartesian, scalar, result) {
  4373. //>>includeStart('debug', pragmas.debug);
  4374. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4375. RuntimeError.Check.typeOf.number("scalar", scalar);
  4376. RuntimeError.Check.typeOf.object("result", result);
  4377. //>>includeEnd('debug');
  4378. result.x = cartesian.x / scalar;
  4379. result.y = cartesian.y / scalar;
  4380. result.z = cartesian.z / scalar;
  4381. result.w = cartesian.w / scalar;
  4382. return result;
  4383. };
  4384. /**
  4385. * Negates the provided Cartesian.
  4386. *
  4387. * @param {Cartesian4} cartesian The Cartesian to be negated.
  4388. * @param {Cartesian4} result The object onto which to store the result.
  4389. * @returns {Cartesian4} The modified result parameter.
  4390. */
  4391. Cartesian4.negate = function (cartesian, result) {
  4392. //>>includeStart('debug', pragmas.debug);
  4393. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4394. RuntimeError.Check.typeOf.object("result", result);
  4395. //>>includeEnd('debug');
  4396. result.x = -cartesian.x;
  4397. result.y = -cartesian.y;
  4398. result.z = -cartesian.z;
  4399. result.w = -cartesian.w;
  4400. return result;
  4401. };
  4402. /**
  4403. * Computes the absolute value of the provided Cartesian.
  4404. *
  4405. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  4406. * @param {Cartesian4} result The object onto which to store the result.
  4407. * @returns {Cartesian4} The modified result parameter.
  4408. */
  4409. Cartesian4.abs = function (cartesian, result) {
  4410. //>>includeStart('debug', pragmas.debug);
  4411. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4412. RuntimeError.Check.typeOf.object("result", result);
  4413. //>>includeEnd('debug');
  4414. result.x = Math.abs(cartesian.x);
  4415. result.y = Math.abs(cartesian.y);
  4416. result.z = Math.abs(cartesian.z);
  4417. result.w = Math.abs(cartesian.w);
  4418. return result;
  4419. };
  4420. const lerpScratch$1 = new Cartesian4();
  4421. /**
  4422. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  4423. *
  4424. * @param {Cartesian4} start The value corresponding to t at 0.0.
  4425. * @param {Cartesian4}end The value corresponding to t at 1.0.
  4426. * @param {Number} t The point along t at which to interpolate.
  4427. * @param {Cartesian4} result The object onto which to store the result.
  4428. * @returns {Cartesian4} The modified result parameter.
  4429. */
  4430. Cartesian4.lerp = function (start, end, t, result) {
  4431. //>>includeStart('debug', pragmas.debug);
  4432. RuntimeError.Check.typeOf.object("start", start);
  4433. RuntimeError.Check.typeOf.object("end", end);
  4434. RuntimeError.Check.typeOf.number("t", t);
  4435. RuntimeError.Check.typeOf.object("result", result);
  4436. //>>includeEnd('debug');
  4437. Cartesian4.multiplyByScalar(end, t, lerpScratch$1);
  4438. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  4439. return Cartesian4.add(lerpScratch$1, result, result);
  4440. };
  4441. const mostOrthogonalAxisScratch$1 = new Cartesian4();
  4442. /**
  4443. * Returns the axis that is most orthogonal to the provided Cartesian.
  4444. *
  4445. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  4446. * @param {Cartesian4} result The object onto which to store the result.
  4447. * @returns {Cartesian4} The most orthogonal axis.
  4448. */
  4449. Cartesian4.mostOrthogonalAxis = function (cartesian, result) {
  4450. //>>includeStart('debug', pragmas.debug);
  4451. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4452. RuntimeError.Check.typeOf.object("result", result);
  4453. //>>includeEnd('debug');
  4454. const f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch$1);
  4455. Cartesian4.abs(f, f);
  4456. if (f.x <= f.y) {
  4457. if (f.x <= f.z) {
  4458. if (f.x <= f.w) {
  4459. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  4460. } else {
  4461. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4462. }
  4463. } else if (f.z <= f.w) {
  4464. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  4465. } else {
  4466. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4467. }
  4468. } else if (f.y <= f.z) {
  4469. if (f.y <= f.w) {
  4470. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  4471. } else {
  4472. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4473. }
  4474. } else if (f.z <= f.w) {
  4475. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  4476. } else {
  4477. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4478. }
  4479. return result;
  4480. };
  4481. /**
  4482. * Compares the provided Cartesians componentwise and returns
  4483. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4484. *
  4485. * @param {Cartesian4} [left] The first Cartesian.
  4486. * @param {Cartesian4} [right] The second Cartesian.
  4487. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  4488. */
  4489. Cartesian4.equals = function (left, right) {
  4490. return (
  4491. left === right ||
  4492. (defaultValue.defined(left) &&
  4493. defaultValue.defined(right) &&
  4494. left.x === right.x &&
  4495. left.y === right.y &&
  4496. left.z === right.z &&
  4497. left.w === right.w)
  4498. );
  4499. };
  4500. /**
  4501. * @private
  4502. */
  4503. Cartesian4.equalsArray = function (cartesian, array, offset) {
  4504. return (
  4505. cartesian.x === array[offset] &&
  4506. cartesian.y === array[offset + 1] &&
  4507. cartesian.z === array[offset + 2] &&
  4508. cartesian.w === array[offset + 3]
  4509. );
  4510. };
  4511. /**
  4512. * Compares the provided Cartesians componentwise and returns
  4513. * <code>true</code> if they pass an absolute or relative tolerance test,
  4514. * <code>false</code> otherwise.
  4515. *
  4516. * @param {Cartesian4} [left] The first Cartesian.
  4517. * @param {Cartesian4} [right] The second Cartesian.
  4518. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  4519. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  4520. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  4521. */
  4522. Cartesian4.equalsEpsilon = function (
  4523. left,
  4524. right,
  4525. relativeEpsilon,
  4526. absoluteEpsilon
  4527. ) {
  4528. return (
  4529. left === right ||
  4530. (defaultValue.defined(left) &&
  4531. defaultValue.defined(right) &&
  4532. ComponentDatatype.CesiumMath.equalsEpsilon(
  4533. left.x,
  4534. right.x,
  4535. relativeEpsilon,
  4536. absoluteEpsilon
  4537. ) &&
  4538. ComponentDatatype.CesiumMath.equalsEpsilon(
  4539. left.y,
  4540. right.y,
  4541. relativeEpsilon,
  4542. absoluteEpsilon
  4543. ) &&
  4544. ComponentDatatype.CesiumMath.equalsEpsilon(
  4545. left.z,
  4546. right.z,
  4547. relativeEpsilon,
  4548. absoluteEpsilon
  4549. ) &&
  4550. ComponentDatatype.CesiumMath.equalsEpsilon(
  4551. left.w,
  4552. right.w,
  4553. relativeEpsilon,
  4554. absoluteEpsilon
  4555. ))
  4556. );
  4557. };
  4558. /**
  4559. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  4560. *
  4561. * @type {Cartesian4}
  4562. * @constant
  4563. */
  4564. Cartesian4.ZERO = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  4565. /**
  4566. * An immutable Cartesian4 instance initialized to (1.0, 1.0, 1.0, 1.0).
  4567. *
  4568. * @type {Cartesian4}
  4569. * @constant
  4570. */
  4571. Cartesian4.ONE = Object.freeze(new Cartesian4(1.0, 1.0, 1.0, 1.0));
  4572. /**
  4573. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  4574. *
  4575. * @type {Cartesian4}
  4576. * @constant
  4577. */
  4578. Cartesian4.UNIT_X = Object.freeze(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  4579. /**
  4580. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  4581. *
  4582. * @type {Cartesian4}
  4583. * @constant
  4584. */
  4585. Cartesian4.UNIT_Y = Object.freeze(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  4586. /**
  4587. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  4588. *
  4589. * @type {Cartesian4}
  4590. * @constant
  4591. */
  4592. Cartesian4.UNIT_Z = Object.freeze(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  4593. /**
  4594. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  4595. *
  4596. * @type {Cartesian4}
  4597. * @constant
  4598. */
  4599. Cartesian4.UNIT_W = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  4600. /**
  4601. * Duplicates this Cartesian4 instance.
  4602. *
  4603. * @param {Cartesian4} [result] The object onto which to store the result.
  4604. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4605. */
  4606. Cartesian4.prototype.clone = function (result) {
  4607. return Cartesian4.clone(this, result);
  4608. };
  4609. /**
  4610. * Compares this Cartesian against the provided Cartesian componentwise and returns
  4611. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4612. *
  4613. * @param {Cartesian4} [right] The right hand side Cartesian.
  4614. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  4615. */
  4616. Cartesian4.prototype.equals = function (right) {
  4617. return Cartesian4.equals(this, right);
  4618. };
  4619. /**
  4620. * Compares this Cartesian against the provided Cartesian componentwise and returns
  4621. * <code>true</code> if they pass an absolute or relative tolerance test,
  4622. * <code>false</code> otherwise.
  4623. *
  4624. * @param {Cartesian4} [right] The right hand side Cartesian.
  4625. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  4626. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  4627. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  4628. */
  4629. Cartesian4.prototype.equalsEpsilon = function (
  4630. right,
  4631. relativeEpsilon,
  4632. absoluteEpsilon
  4633. ) {
  4634. return Cartesian4.equalsEpsilon(
  4635. this,
  4636. right,
  4637. relativeEpsilon,
  4638. absoluteEpsilon
  4639. );
  4640. };
  4641. /**
  4642. * Creates a string representing this Cartesian in the format '(x, y, z, w)'.
  4643. *
  4644. * @returns {String} A string representing the provided Cartesian in the format '(x, y, z, w)'.
  4645. */
  4646. Cartesian4.prototype.toString = function () {
  4647. return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`;
  4648. };
  4649. // scratchU8Array and scratchF32Array are views into the same buffer
  4650. const scratchF32Array = new Float32Array(1);
  4651. const scratchU8Array = new Uint8Array(scratchF32Array.buffer);
  4652. const testU32 = new Uint32Array([0x11223344]);
  4653. const testU8 = new Uint8Array(testU32.buffer);
  4654. const littleEndian = testU8[0] === 0x44;
  4655. /**
  4656. * Packs an arbitrary floating point value to 4 values representable using uint8.
  4657. *
  4658. * @param {Number} value A floating point number.
  4659. * @param {Cartesian4} [result] The Cartesian4 that will contain the packed float.
  4660. * @returns {Cartesian4} A Cartesian4 representing the float packed to values in x, y, z, and w.
  4661. */
  4662. Cartesian4.packFloat = function (value, result) {
  4663. //>>includeStart('debug', pragmas.debug);
  4664. RuntimeError.Check.typeOf.number("value", value);
  4665. //>>includeEnd('debug');
  4666. if (!defaultValue.defined(result)) {
  4667. result = new Cartesian4();
  4668. }
  4669. // scratchU8Array and scratchF32Array are views into the same buffer
  4670. scratchF32Array[0] = value;
  4671. if (littleEndian) {
  4672. result.x = scratchU8Array[0];
  4673. result.y = scratchU8Array[1];
  4674. result.z = scratchU8Array[2];
  4675. result.w = scratchU8Array[3];
  4676. } else {
  4677. // convert from big-endian to little-endian
  4678. result.x = scratchU8Array[3];
  4679. result.y = scratchU8Array[2];
  4680. result.z = scratchU8Array[1];
  4681. result.w = scratchU8Array[0];
  4682. }
  4683. return result;
  4684. };
  4685. /**
  4686. * Unpacks a float packed using Cartesian4.packFloat.
  4687. *
  4688. * @param {Cartesian4} packedFloat A Cartesian4 containing a float packed to 4 values representable using uint8.
  4689. * @returns {Number} The unpacked float.
  4690. * @private
  4691. */
  4692. Cartesian4.unpackFloat = function (packedFloat) {
  4693. //>>includeStart('debug', pragmas.debug);
  4694. RuntimeError.Check.typeOf.object("packedFloat", packedFloat);
  4695. //>>includeEnd('debug');
  4696. // scratchU8Array and scratchF32Array are views into the same buffer
  4697. if (littleEndian) {
  4698. scratchU8Array[0] = packedFloat.x;
  4699. scratchU8Array[1] = packedFloat.y;
  4700. scratchU8Array[2] = packedFloat.z;
  4701. scratchU8Array[3] = packedFloat.w;
  4702. } else {
  4703. // convert from little-endian to big-endian
  4704. scratchU8Array[0] = packedFloat.w;
  4705. scratchU8Array[1] = packedFloat.z;
  4706. scratchU8Array[2] = packedFloat.y;
  4707. scratchU8Array[3] = packedFloat.x;
  4708. }
  4709. return scratchF32Array[0];
  4710. };
  4711. /**
  4712. * A 4x4 matrix, indexable as a column-major order array.
  4713. * Constructor parameters are in row-major order for code readability.
  4714. * @alias Matrix4
  4715. * @constructor
  4716. * @implements {ArrayLike<number>}
  4717. *
  4718. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  4719. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  4720. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  4721. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  4722. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  4723. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  4724. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  4725. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  4726. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  4727. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  4728. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  4729. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  4730. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  4731. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  4732. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  4733. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  4734. *
  4735. * @see Matrix4.fromArray
  4736. * @see Matrix4.fromColumnMajorArray
  4737. * @see Matrix4.fromRowMajorArray
  4738. * @see Matrix4.fromRotationTranslation
  4739. * @see Matrix4.fromTranslationQuaternionRotationScale
  4740. * @see Matrix4.fromTranslationRotationScale
  4741. * @see Matrix4.fromTranslation
  4742. * @see Matrix4.fromScale
  4743. * @see Matrix4.fromUniformScale
  4744. * @see Matrix4.fromRotation
  4745. * @see Matrix4.fromCamera
  4746. * @see Matrix4.computePerspectiveFieldOfView
  4747. * @see Matrix4.computeOrthographicOffCenter
  4748. * @see Matrix4.computePerspectiveOffCenter
  4749. * @see Matrix4.computeInfinitePerspectiveOffCenter
  4750. * @see Matrix4.computeViewportTransformation
  4751. * @see Matrix4.computeView
  4752. * @see Matrix2
  4753. * @see Matrix3
  4754. * @see Packable
  4755. */
  4756. function Matrix4(
  4757. column0Row0,
  4758. column1Row0,
  4759. column2Row0,
  4760. column3Row0,
  4761. column0Row1,
  4762. column1Row1,
  4763. column2Row1,
  4764. column3Row1,
  4765. column0Row2,
  4766. column1Row2,
  4767. column2Row2,
  4768. column3Row2,
  4769. column0Row3,
  4770. column1Row3,
  4771. column2Row3,
  4772. column3Row3
  4773. ) {
  4774. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  4775. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  4776. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  4777. this[3] = defaultValue.defaultValue(column0Row3, 0.0);
  4778. this[4] = defaultValue.defaultValue(column1Row0, 0.0);
  4779. this[5] = defaultValue.defaultValue(column1Row1, 0.0);
  4780. this[6] = defaultValue.defaultValue(column1Row2, 0.0);
  4781. this[7] = defaultValue.defaultValue(column1Row3, 0.0);
  4782. this[8] = defaultValue.defaultValue(column2Row0, 0.0);
  4783. this[9] = defaultValue.defaultValue(column2Row1, 0.0);
  4784. this[10] = defaultValue.defaultValue(column2Row2, 0.0);
  4785. this[11] = defaultValue.defaultValue(column2Row3, 0.0);
  4786. this[12] = defaultValue.defaultValue(column3Row0, 0.0);
  4787. this[13] = defaultValue.defaultValue(column3Row1, 0.0);
  4788. this[14] = defaultValue.defaultValue(column3Row2, 0.0);
  4789. this[15] = defaultValue.defaultValue(column3Row3, 0.0);
  4790. }
  4791. /**
  4792. * The number of elements used to pack the object into an array.
  4793. * @type {Number}
  4794. */
  4795. Matrix4.packedLength = 16;
  4796. /**
  4797. * Stores the provided instance into the provided array.
  4798. *
  4799. * @param {Matrix4} value The value to pack.
  4800. * @param {Number[]} array The array to pack into.
  4801. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  4802. *
  4803. * @returns {Number[]} The array that was packed into
  4804. */
  4805. Matrix4.pack = function (value, array, startingIndex) {
  4806. //>>includeStart('debug', pragmas.debug);
  4807. RuntimeError.Check.typeOf.object("value", value);
  4808. RuntimeError.Check.defined("array", array);
  4809. //>>includeEnd('debug');
  4810. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4811. array[startingIndex++] = value[0];
  4812. array[startingIndex++] = value[1];
  4813. array[startingIndex++] = value[2];
  4814. array[startingIndex++] = value[3];
  4815. array[startingIndex++] = value[4];
  4816. array[startingIndex++] = value[5];
  4817. array[startingIndex++] = value[6];
  4818. array[startingIndex++] = value[7];
  4819. array[startingIndex++] = value[8];
  4820. array[startingIndex++] = value[9];
  4821. array[startingIndex++] = value[10];
  4822. array[startingIndex++] = value[11];
  4823. array[startingIndex++] = value[12];
  4824. array[startingIndex++] = value[13];
  4825. array[startingIndex++] = value[14];
  4826. array[startingIndex] = value[15];
  4827. return array;
  4828. };
  4829. /**
  4830. * Retrieves an instance from a packed array.
  4831. *
  4832. * @param {Number[]} array The packed array.
  4833. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  4834. * @param {Matrix4} [result] The object into which to store the result.
  4835. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  4836. */
  4837. Matrix4.unpack = function (array, startingIndex, result) {
  4838. //>>includeStart('debug', pragmas.debug);
  4839. RuntimeError.Check.defined("array", array);
  4840. //>>includeEnd('debug');
  4841. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4842. if (!defaultValue.defined(result)) {
  4843. result = new Matrix4();
  4844. }
  4845. result[0] = array[startingIndex++];
  4846. result[1] = array[startingIndex++];
  4847. result[2] = array[startingIndex++];
  4848. result[3] = array[startingIndex++];
  4849. result[4] = array[startingIndex++];
  4850. result[5] = array[startingIndex++];
  4851. result[6] = array[startingIndex++];
  4852. result[7] = array[startingIndex++];
  4853. result[8] = array[startingIndex++];
  4854. result[9] = array[startingIndex++];
  4855. result[10] = array[startingIndex++];
  4856. result[11] = array[startingIndex++];
  4857. result[12] = array[startingIndex++];
  4858. result[13] = array[startingIndex++];
  4859. result[14] = array[startingIndex++];
  4860. result[15] = array[startingIndex];
  4861. return result;
  4862. };
  4863. /**
  4864. * Flattens an array of Matrix4s into an array of components. The components
  4865. * are stored in column-major order.
  4866. *
  4867. * @param {Matrix4[]} array The array of matrices to pack.
  4868. * @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.
  4869. * @returns {Number[]} The packed array.
  4870. */
  4871. Matrix4.packArray = function (array, result) {
  4872. //>>includeStart('debug', pragmas.debug);
  4873. RuntimeError.Check.defined("array", array);
  4874. //>>includeEnd('debug');
  4875. const length = array.length;
  4876. const resultLength = length * 16;
  4877. if (!defaultValue.defined(result)) {
  4878. result = new Array(resultLength);
  4879. } else if (!Array.isArray(result) && result.length !== resultLength) {
  4880. //>>includeStart('debug', pragmas.debug);
  4881. throw new RuntimeError.DeveloperError(
  4882. "If result is a typed array, it must have exactly array.length * 16 elements"
  4883. );
  4884. //>>includeEnd('debug');
  4885. } else if (result.length !== resultLength) {
  4886. result.length = resultLength;
  4887. }
  4888. for (let i = 0; i < length; ++i) {
  4889. Matrix4.pack(array[i], result, i * 16);
  4890. }
  4891. return result;
  4892. };
  4893. /**
  4894. * Unpacks an array of column-major matrix components into an array of Matrix4s.
  4895. *
  4896. * @param {Number[]} array The array of components to unpack.
  4897. * @param {Matrix4[]} [result] The array onto which to store the result.
  4898. * @returns {Matrix4[]} The unpacked array.
  4899. */
  4900. Matrix4.unpackArray = function (array, result) {
  4901. //>>includeStart('debug', pragmas.debug);
  4902. RuntimeError.Check.defined("array", array);
  4903. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 16);
  4904. if (array.length % 16 !== 0) {
  4905. throw new RuntimeError.DeveloperError("array length must be a multiple of 16.");
  4906. }
  4907. //>>includeEnd('debug');
  4908. const length = array.length;
  4909. if (!defaultValue.defined(result)) {
  4910. result = new Array(length / 16);
  4911. } else {
  4912. result.length = length / 16;
  4913. }
  4914. for (let i = 0; i < length; i += 16) {
  4915. const index = i / 16;
  4916. result[index] = Matrix4.unpack(array, i, result[index]);
  4917. }
  4918. return result;
  4919. };
  4920. /**
  4921. * Duplicates a Matrix4 instance.
  4922. *
  4923. * @param {Matrix4} matrix The matrix to duplicate.
  4924. * @param {Matrix4} [result] The object onto which to store the result.
  4925. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  4926. */
  4927. Matrix4.clone = function (matrix, result) {
  4928. if (!defaultValue.defined(matrix)) {
  4929. return undefined;
  4930. }
  4931. if (!defaultValue.defined(result)) {
  4932. return new Matrix4(
  4933. matrix[0],
  4934. matrix[4],
  4935. matrix[8],
  4936. matrix[12],
  4937. matrix[1],
  4938. matrix[5],
  4939. matrix[9],
  4940. matrix[13],
  4941. matrix[2],
  4942. matrix[6],
  4943. matrix[10],
  4944. matrix[14],
  4945. matrix[3],
  4946. matrix[7],
  4947. matrix[11],
  4948. matrix[15]
  4949. );
  4950. }
  4951. result[0] = matrix[0];
  4952. result[1] = matrix[1];
  4953. result[2] = matrix[2];
  4954. result[3] = matrix[3];
  4955. result[4] = matrix[4];
  4956. result[5] = matrix[5];
  4957. result[6] = matrix[6];
  4958. result[7] = matrix[7];
  4959. result[8] = matrix[8];
  4960. result[9] = matrix[9];
  4961. result[10] = matrix[10];
  4962. result[11] = matrix[11];
  4963. result[12] = matrix[12];
  4964. result[13] = matrix[13];
  4965. result[14] = matrix[14];
  4966. result[15] = matrix[15];
  4967. return result;
  4968. };
  4969. /**
  4970. * Creates a Matrix4 from 16 consecutive elements in an array.
  4971. * @function
  4972. *
  4973. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  4974. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  4975. * @param {Matrix4} [result] The object onto which to store the result.
  4976. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  4977. *
  4978. * @example
  4979. * // Create the Matrix4:
  4980. * // [1.0, 2.0, 3.0, 4.0]
  4981. * // [1.0, 2.0, 3.0, 4.0]
  4982. * // [1.0, 2.0, 3.0, 4.0]
  4983. * // [1.0, 2.0, 3.0, 4.0]
  4984. *
  4985. * 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];
  4986. * const m = Cesium.Matrix4.fromArray(v);
  4987. *
  4988. * // Create same Matrix4 with using an offset into an array
  4989. * 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];
  4990. * const m2 = Cesium.Matrix4.fromArray(v2, 2);
  4991. */
  4992. Matrix4.fromArray = Matrix4.unpack;
  4993. /**
  4994. * Computes a Matrix4 instance from a column-major order array.
  4995. *
  4996. * @param {Number[]} values The column-major order array.
  4997. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4998. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  4999. */
  5000. Matrix4.fromColumnMajorArray = function (values, result) {
  5001. //>>includeStart('debug', pragmas.debug);
  5002. RuntimeError.Check.defined("values", values);
  5003. //>>includeEnd('debug');
  5004. return Matrix4.clone(values, result);
  5005. };
  5006. /**
  5007. * Computes a Matrix4 instance from a row-major order array.
  5008. * The resulting matrix will be in column-major order.
  5009. *
  5010. * @param {Number[]} values The row-major order array.
  5011. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5012. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5013. */
  5014. Matrix4.fromRowMajorArray = function (values, result) {
  5015. //>>includeStart('debug', pragmas.debug);
  5016. RuntimeError.Check.defined("values", values);
  5017. //>>includeEnd('debug');
  5018. if (!defaultValue.defined(result)) {
  5019. return new Matrix4(
  5020. values[0],
  5021. values[1],
  5022. values[2],
  5023. values[3],
  5024. values[4],
  5025. values[5],
  5026. values[6],
  5027. values[7],
  5028. values[8],
  5029. values[9],
  5030. values[10],
  5031. values[11],
  5032. values[12],
  5033. values[13],
  5034. values[14],
  5035. values[15]
  5036. );
  5037. }
  5038. result[0] = values[0];
  5039. result[1] = values[4];
  5040. result[2] = values[8];
  5041. result[3] = values[12];
  5042. result[4] = values[1];
  5043. result[5] = values[5];
  5044. result[6] = values[9];
  5045. result[7] = values[13];
  5046. result[8] = values[2];
  5047. result[9] = values[6];
  5048. result[10] = values[10];
  5049. result[11] = values[14];
  5050. result[12] = values[3];
  5051. result[13] = values[7];
  5052. result[14] = values[11];
  5053. result[15] = values[15];
  5054. return result;
  5055. };
  5056. /**
  5057. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  5058. * and a Cartesian3 representing the translation.
  5059. *
  5060. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  5061. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  5062. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5063. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5064. */
  5065. Matrix4.fromRotationTranslation = function (rotation, translation, result) {
  5066. //>>includeStart('debug', pragmas.debug);
  5067. RuntimeError.Check.typeOf.object("rotation", rotation);
  5068. //>>includeEnd('debug');
  5069. translation = defaultValue.defaultValue(translation, Cartesian3.ZERO);
  5070. if (!defaultValue.defined(result)) {
  5071. return new Matrix4(
  5072. rotation[0],
  5073. rotation[3],
  5074. rotation[6],
  5075. translation.x,
  5076. rotation[1],
  5077. rotation[4],
  5078. rotation[7],
  5079. translation.y,
  5080. rotation[2],
  5081. rotation[5],
  5082. rotation[8],
  5083. translation.z,
  5084. 0.0,
  5085. 0.0,
  5086. 0.0,
  5087. 1.0
  5088. );
  5089. }
  5090. result[0] = rotation[0];
  5091. result[1] = rotation[1];
  5092. result[2] = rotation[2];
  5093. result[3] = 0.0;
  5094. result[4] = rotation[3];
  5095. result[5] = rotation[4];
  5096. result[6] = rotation[5];
  5097. result[7] = 0.0;
  5098. result[8] = rotation[6];
  5099. result[9] = rotation[7];
  5100. result[10] = rotation[8];
  5101. result[11] = 0.0;
  5102. result[12] = translation.x;
  5103. result[13] = translation.y;
  5104. result[14] = translation.z;
  5105. result[15] = 1.0;
  5106. return result;
  5107. };
  5108. /**
  5109. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  5110. * representation with the rotation represented as a quaternion.
  5111. *
  5112. * @param {Cartesian3} translation The translation transformation.
  5113. * @param {Quaternion} rotation The rotation transformation.
  5114. * @param {Cartesian3} scale The non-uniform scale transformation.
  5115. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5116. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5117. *
  5118. * @example
  5119. * const result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  5120. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  5121. * Cesium.Quaternion.IDENTITY, // rotation
  5122. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  5123. * result);
  5124. */
  5125. Matrix4.fromTranslationQuaternionRotationScale = function (
  5126. translation,
  5127. rotation,
  5128. scale,
  5129. result
  5130. ) {
  5131. //>>includeStart('debug', pragmas.debug);
  5132. RuntimeError.Check.typeOf.object("translation", translation);
  5133. RuntimeError.Check.typeOf.object("rotation", rotation);
  5134. RuntimeError.Check.typeOf.object("scale", scale);
  5135. //>>includeEnd('debug');
  5136. if (!defaultValue.defined(result)) {
  5137. result = new Matrix4();
  5138. }
  5139. const scaleX = scale.x;
  5140. const scaleY = scale.y;
  5141. const scaleZ = scale.z;
  5142. const x2 = rotation.x * rotation.x;
  5143. const xy = rotation.x * rotation.y;
  5144. const xz = rotation.x * rotation.z;
  5145. const xw = rotation.x * rotation.w;
  5146. const y2 = rotation.y * rotation.y;
  5147. const yz = rotation.y * rotation.z;
  5148. const yw = rotation.y * rotation.w;
  5149. const z2 = rotation.z * rotation.z;
  5150. const zw = rotation.z * rotation.w;
  5151. const w2 = rotation.w * rotation.w;
  5152. const m00 = x2 - y2 - z2 + w2;
  5153. const m01 = 2.0 * (xy - zw);
  5154. const m02 = 2.0 * (xz + yw);
  5155. const m10 = 2.0 * (xy + zw);
  5156. const m11 = -x2 + y2 - z2 + w2;
  5157. const m12 = 2.0 * (yz - xw);
  5158. const m20 = 2.0 * (xz - yw);
  5159. const m21 = 2.0 * (yz + xw);
  5160. const m22 = -x2 - y2 + z2 + w2;
  5161. result[0] = m00 * scaleX;
  5162. result[1] = m10 * scaleX;
  5163. result[2] = m20 * scaleX;
  5164. result[3] = 0.0;
  5165. result[4] = m01 * scaleY;
  5166. result[5] = m11 * scaleY;
  5167. result[6] = m21 * scaleY;
  5168. result[7] = 0.0;
  5169. result[8] = m02 * scaleZ;
  5170. result[9] = m12 * scaleZ;
  5171. result[10] = m22 * scaleZ;
  5172. result[11] = 0.0;
  5173. result[12] = translation.x;
  5174. result[13] = translation.y;
  5175. result[14] = translation.z;
  5176. result[15] = 1.0;
  5177. return result;
  5178. };
  5179. /**
  5180. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  5181. *
  5182. * @param {TranslationRotationScale} translationRotationScale The instance.
  5183. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5184. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5185. */
  5186. Matrix4.fromTranslationRotationScale = function (
  5187. translationRotationScale,
  5188. result
  5189. ) {
  5190. //>>includeStart('debug', pragmas.debug);
  5191. RuntimeError.Check.typeOf.object("translationRotationScale", translationRotationScale);
  5192. //>>includeEnd('debug');
  5193. return Matrix4.fromTranslationQuaternionRotationScale(
  5194. translationRotationScale.translation,
  5195. translationRotationScale.rotation,
  5196. translationRotationScale.scale,
  5197. result
  5198. );
  5199. };
  5200. /**
  5201. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  5202. *
  5203. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  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. * @see Matrix4.multiplyByTranslation
  5208. */
  5209. Matrix4.fromTranslation = function (translation, result) {
  5210. //>>includeStart('debug', pragmas.debug);
  5211. RuntimeError.Check.typeOf.object("translation", translation);
  5212. //>>includeEnd('debug');
  5213. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  5214. };
  5215. /**
  5216. * Computes a Matrix4 instance representing a non-uniform scale.
  5217. *
  5218. * @param {Cartesian3} scale The x, y, and z scale factors.
  5219. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5220. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5221. *
  5222. * @example
  5223. * // Creates
  5224. * // [7.0, 0.0, 0.0, 0.0]
  5225. * // [0.0, 8.0, 0.0, 0.0]
  5226. * // [0.0, 0.0, 9.0, 0.0]
  5227. * // [0.0, 0.0, 0.0, 1.0]
  5228. * const m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  5229. */
  5230. Matrix4.fromScale = function (scale, result) {
  5231. //>>includeStart('debug', pragmas.debug);
  5232. RuntimeError.Check.typeOf.object("scale", scale);
  5233. //>>includeEnd('debug');
  5234. if (!defaultValue.defined(result)) {
  5235. return new Matrix4(
  5236. scale.x,
  5237. 0.0,
  5238. 0.0,
  5239. 0.0,
  5240. 0.0,
  5241. scale.y,
  5242. 0.0,
  5243. 0.0,
  5244. 0.0,
  5245. 0.0,
  5246. scale.z,
  5247. 0.0,
  5248. 0.0,
  5249. 0.0,
  5250. 0.0,
  5251. 1.0
  5252. );
  5253. }
  5254. result[0] = scale.x;
  5255. result[1] = 0.0;
  5256. result[2] = 0.0;
  5257. result[3] = 0.0;
  5258. result[4] = 0.0;
  5259. result[5] = scale.y;
  5260. result[6] = 0.0;
  5261. result[7] = 0.0;
  5262. result[8] = 0.0;
  5263. result[9] = 0.0;
  5264. result[10] = scale.z;
  5265. result[11] = 0.0;
  5266. result[12] = 0.0;
  5267. result[13] = 0.0;
  5268. result[14] = 0.0;
  5269. result[15] = 1.0;
  5270. return result;
  5271. };
  5272. /**
  5273. * Computes a Matrix4 instance representing a uniform scale.
  5274. *
  5275. * @param {Number} scale The uniform scale factor.
  5276. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5277. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5278. *
  5279. * @example
  5280. * // Creates
  5281. * // [2.0, 0.0, 0.0, 0.0]
  5282. * // [0.0, 2.0, 0.0, 0.0]
  5283. * // [0.0, 0.0, 2.0, 0.0]
  5284. * // [0.0, 0.0, 0.0, 1.0]
  5285. * const m = Cesium.Matrix4.fromUniformScale(2.0);
  5286. */
  5287. Matrix4.fromUniformScale = function (scale, result) {
  5288. //>>includeStart('debug', pragmas.debug);
  5289. RuntimeError.Check.typeOf.number("scale", scale);
  5290. //>>includeEnd('debug');
  5291. if (!defaultValue.defined(result)) {
  5292. return new Matrix4(
  5293. scale,
  5294. 0.0,
  5295. 0.0,
  5296. 0.0,
  5297. 0.0,
  5298. scale,
  5299. 0.0,
  5300. 0.0,
  5301. 0.0,
  5302. 0.0,
  5303. scale,
  5304. 0.0,
  5305. 0.0,
  5306. 0.0,
  5307. 0.0,
  5308. 1.0
  5309. );
  5310. }
  5311. result[0] = scale;
  5312. result[1] = 0.0;
  5313. result[2] = 0.0;
  5314. result[3] = 0.0;
  5315. result[4] = 0.0;
  5316. result[5] = scale;
  5317. result[6] = 0.0;
  5318. result[7] = 0.0;
  5319. result[8] = 0.0;
  5320. result[9] = 0.0;
  5321. result[10] = scale;
  5322. result[11] = 0.0;
  5323. result[12] = 0.0;
  5324. result[13] = 0.0;
  5325. result[14] = 0.0;
  5326. result[15] = 1.0;
  5327. return result;
  5328. };
  5329. /**
  5330. * Creates a rotation matrix.
  5331. *
  5332. * @param {Matrix3} rotation The rotation matrix.
  5333. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5334. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5335. */
  5336. Matrix4.fromRotation = function (rotation, result) {
  5337. //>>includeStart('debug', pragmas.debug);
  5338. RuntimeError.Check.typeOf.object("rotation", rotation);
  5339. //>>includeEnd('debug');
  5340. if (!defaultValue.defined(result)) {
  5341. result = new Matrix4();
  5342. }
  5343. result[0] = rotation[0];
  5344. result[1] = rotation[1];
  5345. result[2] = rotation[2];
  5346. result[3] = 0.0;
  5347. result[4] = rotation[3];
  5348. result[5] = rotation[4];
  5349. result[6] = rotation[5];
  5350. result[7] = 0.0;
  5351. result[8] = rotation[6];
  5352. result[9] = rotation[7];
  5353. result[10] = rotation[8];
  5354. result[11] = 0.0;
  5355. result[12] = 0.0;
  5356. result[13] = 0.0;
  5357. result[14] = 0.0;
  5358. result[15] = 1.0;
  5359. return result;
  5360. };
  5361. const fromCameraF = new Cartesian3();
  5362. const fromCameraR = new Cartesian3();
  5363. const fromCameraU = new Cartesian3();
  5364. /**
  5365. * Computes a Matrix4 instance from a Camera.
  5366. *
  5367. * @param {Camera} camera The camera to use.
  5368. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5369. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5370. */
  5371. Matrix4.fromCamera = function (camera, result) {
  5372. //>>includeStart('debug', pragmas.debug);
  5373. RuntimeError.Check.typeOf.object("camera", camera);
  5374. //>>includeEnd('debug');
  5375. const position = camera.position;
  5376. const direction = camera.direction;
  5377. const up = camera.up;
  5378. //>>includeStart('debug', pragmas.debug);
  5379. RuntimeError.Check.typeOf.object("camera.position", position);
  5380. RuntimeError.Check.typeOf.object("camera.direction", direction);
  5381. RuntimeError.Check.typeOf.object("camera.up", up);
  5382. //>>includeEnd('debug');
  5383. Cartesian3.normalize(direction, fromCameraF);
  5384. Cartesian3.normalize(
  5385. Cartesian3.cross(fromCameraF, up, fromCameraR),
  5386. fromCameraR
  5387. );
  5388. Cartesian3.normalize(
  5389. Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU),
  5390. fromCameraU
  5391. );
  5392. const sX = fromCameraR.x;
  5393. const sY = fromCameraR.y;
  5394. const sZ = fromCameraR.z;
  5395. const fX = fromCameraF.x;
  5396. const fY = fromCameraF.y;
  5397. const fZ = fromCameraF.z;
  5398. const uX = fromCameraU.x;
  5399. const uY = fromCameraU.y;
  5400. const uZ = fromCameraU.z;
  5401. const positionX = position.x;
  5402. const positionY = position.y;
  5403. const positionZ = position.z;
  5404. const t0 = sX * -positionX + sY * -positionY + sZ * -positionZ;
  5405. const t1 = uX * -positionX + uY * -positionY + uZ * -positionZ;
  5406. const t2 = fX * positionX + fY * positionY + fZ * positionZ;
  5407. // The code below this comment is an optimized
  5408. // version of the commented lines.
  5409. // Rather that create two matrices and then multiply,
  5410. // we just bake in the multiplcation as part of creation.
  5411. // const rotation = new Matrix4(
  5412. // sX, sY, sZ, 0.0,
  5413. // uX, uY, uZ, 0.0,
  5414. // -fX, -fY, -fZ, 0.0,
  5415. // 0.0, 0.0, 0.0, 1.0);
  5416. // const translation = new Matrix4(
  5417. // 1.0, 0.0, 0.0, -position.x,
  5418. // 0.0, 1.0, 0.0, -position.y,
  5419. // 0.0, 0.0, 1.0, -position.z,
  5420. // 0.0, 0.0, 0.0, 1.0);
  5421. // return rotation.multiply(translation);
  5422. if (!defaultValue.defined(result)) {
  5423. return new Matrix4(
  5424. sX,
  5425. sY,
  5426. sZ,
  5427. t0,
  5428. uX,
  5429. uY,
  5430. uZ,
  5431. t1,
  5432. -fX,
  5433. -fY,
  5434. -fZ,
  5435. t2,
  5436. 0.0,
  5437. 0.0,
  5438. 0.0,
  5439. 1.0
  5440. );
  5441. }
  5442. result[0] = sX;
  5443. result[1] = uX;
  5444. result[2] = -fX;
  5445. result[3] = 0.0;
  5446. result[4] = sY;
  5447. result[5] = uY;
  5448. result[6] = -fY;
  5449. result[7] = 0.0;
  5450. result[8] = sZ;
  5451. result[9] = uZ;
  5452. result[10] = -fZ;
  5453. result[11] = 0.0;
  5454. result[12] = t0;
  5455. result[13] = t1;
  5456. result[14] = t2;
  5457. result[15] = 1.0;
  5458. return result;
  5459. };
  5460. /**
  5461. * Computes a Matrix4 instance representing a perspective transformation matrix.
  5462. *
  5463. * @param {Number} fovY The field of view along the Y axis in radians.
  5464. * @param {Number} aspectRatio The aspect ratio.
  5465. * @param {Number} near The distance to the near plane in meters.
  5466. * @param {Number} far The distance to the far plane in meters.
  5467. * @param {Matrix4} result The object in which the result will be stored.
  5468. * @returns {Matrix4} The modified result parameter.
  5469. *
  5470. * @exception {DeveloperError} fovY must be in (0, PI].
  5471. * @exception {DeveloperError} aspectRatio must be greater than zero.
  5472. * @exception {DeveloperError} near must be greater than zero.
  5473. * @exception {DeveloperError} far must be greater than zero.
  5474. */
  5475. Matrix4.computePerspectiveFieldOfView = function (
  5476. fovY,
  5477. aspectRatio,
  5478. near,
  5479. far,
  5480. result
  5481. ) {
  5482. //>>includeStart('debug', pragmas.debug);
  5483. RuntimeError.Check.typeOf.number.greaterThan("fovY", fovY, 0.0);
  5484. RuntimeError.Check.typeOf.number.lessThan("fovY", fovY, Math.PI);
  5485. RuntimeError.Check.typeOf.number.greaterThan("near", near, 0.0);
  5486. RuntimeError.Check.typeOf.number.greaterThan("far", far, 0.0);
  5487. RuntimeError.Check.typeOf.object("result", result);
  5488. //>>includeEnd('debug');
  5489. const bottom = Math.tan(fovY * 0.5);
  5490. const column1Row1 = 1.0 / bottom;
  5491. const column0Row0 = column1Row1 / aspectRatio;
  5492. const column2Row2 = (far + near) / (near - far);
  5493. const column3Row2 = (2.0 * far * near) / (near - far);
  5494. result[0] = column0Row0;
  5495. result[1] = 0.0;
  5496. result[2] = 0.0;
  5497. result[3] = 0.0;
  5498. result[4] = 0.0;
  5499. result[5] = column1Row1;
  5500. result[6] = 0.0;
  5501. result[7] = 0.0;
  5502. result[8] = 0.0;
  5503. result[9] = 0.0;
  5504. result[10] = column2Row2;
  5505. result[11] = -1.0;
  5506. result[12] = 0.0;
  5507. result[13] = 0.0;
  5508. result[14] = column3Row2;
  5509. result[15] = 0.0;
  5510. return result;
  5511. };
  5512. /**
  5513. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  5514. *
  5515. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5516. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5517. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5518. * @param {Number} top The number of meters above of the camera that will be in view.
  5519. * @param {Number} near The distance to the near plane in meters.
  5520. * @param {Number} far The distance to the far plane in meters.
  5521. * @param {Matrix4} result The object in which the result will be stored.
  5522. * @returns {Matrix4} The modified result parameter.
  5523. */
  5524. Matrix4.computeOrthographicOffCenter = function (
  5525. left,
  5526. right,
  5527. bottom,
  5528. top,
  5529. near,
  5530. far,
  5531. result
  5532. ) {
  5533. //>>includeStart('debug', pragmas.debug);
  5534. RuntimeError.Check.typeOf.number("left", left);
  5535. RuntimeError.Check.typeOf.number("right", right);
  5536. RuntimeError.Check.typeOf.number("bottom", bottom);
  5537. RuntimeError.Check.typeOf.number("top", top);
  5538. RuntimeError.Check.typeOf.number("near", near);
  5539. RuntimeError.Check.typeOf.number("far", far);
  5540. RuntimeError.Check.typeOf.object("result", result);
  5541. //>>includeEnd('debug');
  5542. let a = 1.0 / (right - left);
  5543. let b = 1.0 / (top - bottom);
  5544. let c = 1.0 / (far - near);
  5545. const tx = -(right + left) * a;
  5546. const ty = -(top + bottom) * b;
  5547. const tz = -(far + near) * c;
  5548. a *= 2.0;
  5549. b *= 2.0;
  5550. c *= -2.0;
  5551. result[0] = a;
  5552. result[1] = 0.0;
  5553. result[2] = 0.0;
  5554. result[3] = 0.0;
  5555. result[4] = 0.0;
  5556. result[5] = b;
  5557. result[6] = 0.0;
  5558. result[7] = 0.0;
  5559. result[8] = 0.0;
  5560. result[9] = 0.0;
  5561. result[10] = c;
  5562. result[11] = 0.0;
  5563. result[12] = tx;
  5564. result[13] = ty;
  5565. result[14] = tz;
  5566. result[15] = 1.0;
  5567. return result;
  5568. };
  5569. /**
  5570. * Computes a Matrix4 instance representing an off center perspective transformation.
  5571. *
  5572. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5573. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5574. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5575. * @param {Number} top The number of meters above of the camera that will be in view.
  5576. * @param {Number} near The distance to the near plane in meters.
  5577. * @param {Number} far The distance to the far plane in meters.
  5578. * @param {Matrix4} result The object in which the result will be stored.
  5579. * @returns {Matrix4} The modified result parameter.
  5580. */
  5581. Matrix4.computePerspectiveOffCenter = function (
  5582. left,
  5583. right,
  5584. bottom,
  5585. top,
  5586. near,
  5587. far,
  5588. result
  5589. ) {
  5590. //>>includeStart('debug', pragmas.debug);
  5591. RuntimeError.Check.typeOf.number("left", left);
  5592. RuntimeError.Check.typeOf.number("right", right);
  5593. RuntimeError.Check.typeOf.number("bottom", bottom);
  5594. RuntimeError.Check.typeOf.number("top", top);
  5595. RuntimeError.Check.typeOf.number("near", near);
  5596. RuntimeError.Check.typeOf.number("far", far);
  5597. RuntimeError.Check.typeOf.object("result", result);
  5598. //>>includeEnd('debug');
  5599. const column0Row0 = (2.0 * near) / (right - left);
  5600. const column1Row1 = (2.0 * near) / (top - bottom);
  5601. const column2Row0 = (right + left) / (right - left);
  5602. const column2Row1 = (top + bottom) / (top - bottom);
  5603. const column2Row2 = -(far + near) / (far - near);
  5604. const column2Row3 = -1.0;
  5605. const column3Row2 = (-2.0 * far * near) / (far - near);
  5606. result[0] = column0Row0;
  5607. result[1] = 0.0;
  5608. result[2] = 0.0;
  5609. result[3] = 0.0;
  5610. result[4] = 0.0;
  5611. result[5] = column1Row1;
  5612. result[6] = 0.0;
  5613. result[7] = 0.0;
  5614. result[8] = column2Row0;
  5615. result[9] = column2Row1;
  5616. result[10] = column2Row2;
  5617. result[11] = column2Row3;
  5618. result[12] = 0.0;
  5619. result[13] = 0.0;
  5620. result[14] = column3Row2;
  5621. result[15] = 0.0;
  5622. return result;
  5623. };
  5624. /**
  5625. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  5626. *
  5627. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5628. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5629. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5630. * @param {Number} top The number of meters above of the camera that will be in view.
  5631. * @param {Number} near The distance to the near plane in meters.
  5632. * @param {Matrix4} result The object in which the result will be stored.
  5633. * @returns {Matrix4} The modified result parameter.
  5634. */
  5635. Matrix4.computeInfinitePerspectiveOffCenter = function (
  5636. left,
  5637. right,
  5638. bottom,
  5639. top,
  5640. near,
  5641. result
  5642. ) {
  5643. //>>includeStart('debug', pragmas.debug);
  5644. RuntimeError.Check.typeOf.number("left", left);
  5645. RuntimeError.Check.typeOf.number("right", right);
  5646. RuntimeError.Check.typeOf.number("bottom", bottom);
  5647. RuntimeError.Check.typeOf.number("top", top);
  5648. RuntimeError.Check.typeOf.number("near", near);
  5649. RuntimeError.Check.typeOf.object("result", result);
  5650. //>>includeEnd('debug');
  5651. const column0Row0 = (2.0 * near) / (right - left);
  5652. const column1Row1 = (2.0 * near) / (top - bottom);
  5653. const column2Row0 = (right + left) / (right - left);
  5654. const column2Row1 = (top + bottom) / (top - bottom);
  5655. const column2Row2 = -1.0;
  5656. const column2Row3 = -1.0;
  5657. const column3Row2 = -2.0 * near;
  5658. result[0] = column0Row0;
  5659. result[1] = 0.0;
  5660. result[2] = 0.0;
  5661. result[3] = 0.0;
  5662. result[4] = 0.0;
  5663. result[5] = column1Row1;
  5664. result[6] = 0.0;
  5665. result[7] = 0.0;
  5666. result[8] = column2Row0;
  5667. result[9] = column2Row1;
  5668. result[10] = column2Row2;
  5669. result[11] = column2Row3;
  5670. result[12] = 0.0;
  5671. result[13] = 0.0;
  5672. result[14] = column3Row2;
  5673. result[15] = 0.0;
  5674. return result;
  5675. };
  5676. /**
  5677. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  5678. *
  5679. * @param {Object} [viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  5680. * @param {Number} [nearDepthRange=0.0] The near plane distance in window coordinates.
  5681. * @param {Number} [farDepthRange=1.0] The far plane distance in window coordinates.
  5682. * @param {Matrix4} [result] The object in which the result will be stored.
  5683. * @returns {Matrix4} The modified result parameter.
  5684. *
  5685. * @example
  5686. * // Create viewport transformation using an explicit viewport and depth range.
  5687. * const m = Cesium.Matrix4.computeViewportTransformation({
  5688. * x : 0.0,
  5689. * y : 0.0,
  5690. * width : 1024.0,
  5691. * height : 768.0
  5692. * }, 0.0, 1.0, new Cesium.Matrix4());
  5693. */
  5694. Matrix4.computeViewportTransformation = function (
  5695. viewport,
  5696. nearDepthRange,
  5697. farDepthRange,
  5698. result
  5699. ) {
  5700. if (!defaultValue.defined(result)) {
  5701. result = new Matrix4();
  5702. }
  5703. viewport = defaultValue.defaultValue(viewport, defaultValue.defaultValue.EMPTY_OBJECT);
  5704. const x = defaultValue.defaultValue(viewport.x, 0.0);
  5705. const y = defaultValue.defaultValue(viewport.y, 0.0);
  5706. const width = defaultValue.defaultValue(viewport.width, 0.0);
  5707. const height = defaultValue.defaultValue(viewport.height, 0.0);
  5708. nearDepthRange = defaultValue.defaultValue(nearDepthRange, 0.0);
  5709. farDepthRange = defaultValue.defaultValue(farDepthRange, 1.0);
  5710. const halfWidth = width * 0.5;
  5711. const halfHeight = height * 0.5;
  5712. const halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  5713. const column0Row0 = halfWidth;
  5714. const column1Row1 = halfHeight;
  5715. const column2Row2 = halfDepth;
  5716. const column3Row0 = x + halfWidth;
  5717. const column3Row1 = y + halfHeight;
  5718. const column3Row2 = nearDepthRange + halfDepth;
  5719. const column3Row3 = 1.0;
  5720. result[0] = column0Row0;
  5721. result[1] = 0.0;
  5722. result[2] = 0.0;
  5723. result[3] = 0.0;
  5724. result[4] = 0.0;
  5725. result[5] = column1Row1;
  5726. result[6] = 0.0;
  5727. result[7] = 0.0;
  5728. result[8] = 0.0;
  5729. result[9] = 0.0;
  5730. result[10] = column2Row2;
  5731. result[11] = 0.0;
  5732. result[12] = column3Row0;
  5733. result[13] = column3Row1;
  5734. result[14] = column3Row2;
  5735. result[15] = column3Row3;
  5736. return result;
  5737. };
  5738. /**
  5739. * Computes a Matrix4 instance that transforms from world space to view space.
  5740. *
  5741. * @param {Cartesian3} position The position of the camera.
  5742. * @param {Cartesian3} direction The forward direction.
  5743. * @param {Cartesian3} up The up direction.
  5744. * @param {Cartesian3} right The right direction.
  5745. * @param {Matrix4} result The object in which the result will be stored.
  5746. * @returns {Matrix4} The modified result parameter.
  5747. */
  5748. Matrix4.computeView = function (position, direction, up, right, result) {
  5749. //>>includeStart('debug', pragmas.debug);
  5750. RuntimeError.Check.typeOf.object("position", position);
  5751. RuntimeError.Check.typeOf.object("direction", direction);
  5752. RuntimeError.Check.typeOf.object("up", up);
  5753. RuntimeError.Check.typeOf.object("right", right);
  5754. RuntimeError.Check.typeOf.object("result", result);
  5755. //>>includeEnd('debug');
  5756. result[0] = right.x;
  5757. result[1] = up.x;
  5758. result[2] = -direction.x;
  5759. result[3] = 0.0;
  5760. result[4] = right.y;
  5761. result[5] = up.y;
  5762. result[6] = -direction.y;
  5763. result[7] = 0.0;
  5764. result[8] = right.z;
  5765. result[9] = up.z;
  5766. result[10] = -direction.z;
  5767. result[11] = 0.0;
  5768. result[12] = -Cartesian3.dot(right, position);
  5769. result[13] = -Cartesian3.dot(up, position);
  5770. result[14] = Cartesian3.dot(direction, position);
  5771. result[15] = 1.0;
  5772. return result;
  5773. };
  5774. /**
  5775. * Computes an Array from the provided Matrix4 instance.
  5776. * The array will be in column-major order.
  5777. *
  5778. * @param {Matrix4} matrix The matrix to use..
  5779. * @param {Number[]} [result] The Array onto which to store the result.
  5780. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  5781. *
  5782. * @example
  5783. * //create an array from an instance of Matrix4
  5784. * // m = [10.0, 14.0, 18.0, 22.0]
  5785. * // [11.0, 15.0, 19.0, 23.0]
  5786. * // [12.0, 16.0, 20.0, 24.0]
  5787. * // [13.0, 17.0, 21.0, 25.0]
  5788. * const a = Cesium.Matrix4.toArray(m);
  5789. *
  5790. * // m remains the same
  5791. * //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]
  5792. */
  5793. Matrix4.toArray = function (matrix, result) {
  5794. //>>includeStart('debug', pragmas.debug);
  5795. RuntimeError.Check.typeOf.object("matrix", matrix);
  5796. //>>includeEnd('debug');
  5797. if (!defaultValue.defined(result)) {
  5798. return [
  5799. matrix[0],
  5800. matrix[1],
  5801. matrix[2],
  5802. matrix[3],
  5803. matrix[4],
  5804. matrix[5],
  5805. matrix[6],
  5806. matrix[7],
  5807. matrix[8],
  5808. matrix[9],
  5809. matrix[10],
  5810. matrix[11],
  5811. matrix[12],
  5812. matrix[13],
  5813. matrix[14],
  5814. matrix[15],
  5815. ];
  5816. }
  5817. result[0] = matrix[0];
  5818. result[1] = matrix[1];
  5819. result[2] = matrix[2];
  5820. result[3] = matrix[3];
  5821. result[4] = matrix[4];
  5822. result[5] = matrix[5];
  5823. result[6] = matrix[6];
  5824. result[7] = matrix[7];
  5825. result[8] = matrix[8];
  5826. result[9] = matrix[9];
  5827. result[10] = matrix[10];
  5828. result[11] = matrix[11];
  5829. result[12] = matrix[12];
  5830. result[13] = matrix[13];
  5831. result[14] = matrix[14];
  5832. result[15] = matrix[15];
  5833. return result;
  5834. };
  5835. /**
  5836. * Computes the array index of the element at the provided row and column.
  5837. *
  5838. * @param {Number} row The zero-based index of the row.
  5839. * @param {Number} column The zero-based index of the column.
  5840. * @returns {Number} The index of the element at the provided row and column.
  5841. *
  5842. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  5843. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  5844. *
  5845. * @example
  5846. * const myMatrix = new Cesium.Matrix4();
  5847. * const column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  5848. * const column1Row0 = myMatrix[column1Row0Index];
  5849. * myMatrix[column1Row0Index] = 10.0;
  5850. */
  5851. Matrix4.getElementIndex = function (column, row) {
  5852. //>>includeStart('debug', pragmas.debug);
  5853. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  5854. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 3);
  5855. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  5856. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 3);
  5857. //>>includeEnd('debug');
  5858. return column * 4 + row;
  5859. };
  5860. /**
  5861. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  5862. *
  5863. * @param {Matrix4} matrix The matrix to use.
  5864. * @param {Number} index The zero-based index of the column to retrieve.
  5865. * @param {Cartesian4} result The object onto which to store the result.
  5866. * @returns {Cartesian4} The modified result parameter.
  5867. *
  5868. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5869. *
  5870. * @example
  5871. * //returns a Cartesian4 instance with values from the specified column
  5872. * // m = [10.0, 11.0, 12.0, 13.0]
  5873. * // [14.0, 15.0, 16.0, 17.0]
  5874. * // [18.0, 19.0, 20.0, 21.0]
  5875. * // [22.0, 23.0, 24.0, 25.0]
  5876. *
  5877. * //Example 1: Creates an instance of Cartesian
  5878. * const a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  5879. *
  5880. * @example
  5881. * //Example 2: Sets values for Cartesian instance
  5882. * const a = new Cesium.Cartesian4();
  5883. * Cesium.Matrix4.getColumn(m, 2, a);
  5884. *
  5885. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  5886. */
  5887. Matrix4.getColumn = function (matrix, index, result) {
  5888. //>>includeStart('debug', pragmas.debug);
  5889. RuntimeError.Check.typeOf.object("matrix", matrix);
  5890. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5891. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  5892. RuntimeError.Check.typeOf.object("result", result);
  5893. //>>includeEnd('debug');
  5894. const startIndex = index * 4;
  5895. const x = matrix[startIndex];
  5896. const y = matrix[startIndex + 1];
  5897. const z = matrix[startIndex + 2];
  5898. const w = matrix[startIndex + 3];
  5899. result.x = x;
  5900. result.y = y;
  5901. result.z = z;
  5902. result.w = w;
  5903. return result;
  5904. };
  5905. /**
  5906. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  5907. *
  5908. * @param {Matrix4} matrix The matrix to use.
  5909. * @param {Number} index The zero-based index of the column to set.
  5910. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  5911. * @param {Matrix4} result The object onto which to store the result.
  5912. * @returns {Matrix4} The modified result parameter.
  5913. *
  5914. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5915. *
  5916. * @example
  5917. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  5918. * // m = [10.0, 11.0, 12.0, 13.0]
  5919. * // [14.0, 15.0, 16.0, 17.0]
  5920. * // [18.0, 19.0, 20.0, 21.0]
  5921. * // [22.0, 23.0, 24.0, 25.0]
  5922. *
  5923. * const a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  5924. *
  5925. * // m remains the same
  5926. * // a = [10.0, 11.0, 99.0, 13.0]
  5927. * // [14.0, 15.0, 98.0, 17.0]
  5928. * // [18.0, 19.0, 97.0, 21.0]
  5929. * // [22.0, 23.0, 96.0, 25.0]
  5930. */
  5931. Matrix4.setColumn = function (matrix, index, cartesian, result) {
  5932. //>>includeStart('debug', pragmas.debug);
  5933. RuntimeError.Check.typeOf.object("matrix", matrix);
  5934. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5935. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  5936. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  5937. RuntimeError.Check.typeOf.object("result", result);
  5938. //>>includeEnd('debug');
  5939. result = Matrix4.clone(matrix, result);
  5940. const startIndex = index * 4;
  5941. result[startIndex] = cartesian.x;
  5942. result[startIndex + 1] = cartesian.y;
  5943. result[startIndex + 2] = cartesian.z;
  5944. result[startIndex + 3] = cartesian.w;
  5945. return result;
  5946. };
  5947. /**
  5948. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  5949. *
  5950. * @param {Matrix4} matrix The matrix to use.
  5951. * @param {Number} index The zero-based index of the row to retrieve.
  5952. * @param {Cartesian4} result The object onto which to store the result.
  5953. * @returns {Cartesian4} The modified result parameter.
  5954. *
  5955. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5956. *
  5957. * @example
  5958. * //returns a Cartesian4 instance with values from the specified column
  5959. * // m = [10.0, 11.0, 12.0, 13.0]
  5960. * // [14.0, 15.0, 16.0, 17.0]
  5961. * // [18.0, 19.0, 20.0, 21.0]
  5962. * // [22.0, 23.0, 24.0, 25.0]
  5963. *
  5964. * //Example 1: Returns an instance of Cartesian
  5965. * const a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  5966. *
  5967. * @example
  5968. * //Example 2: Sets values for a Cartesian instance
  5969. * const a = new Cesium.Cartesian4();
  5970. * Cesium.Matrix4.getRow(m, 2, a);
  5971. *
  5972. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  5973. */
  5974. Matrix4.getRow = function (matrix, index, result) {
  5975. //>>includeStart('debug', pragmas.debug);
  5976. RuntimeError.Check.typeOf.object("matrix", matrix);
  5977. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5978. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  5979. RuntimeError.Check.typeOf.object("result", result);
  5980. //>>includeEnd('debug');
  5981. const x = matrix[index];
  5982. const y = matrix[index + 4];
  5983. const z = matrix[index + 8];
  5984. const w = matrix[index + 12];
  5985. result.x = x;
  5986. result.y = y;
  5987. result.z = z;
  5988. result.w = w;
  5989. return result;
  5990. };
  5991. /**
  5992. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  5993. *
  5994. * @param {Matrix4} matrix The matrix to use.
  5995. * @param {Number} index The zero-based index of the row to set.
  5996. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  5997. * @param {Matrix4} result The object onto which to store the result.
  5998. * @returns {Matrix4} The modified result parameter.
  5999. *
  6000. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  6001. *
  6002. * @example
  6003. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  6004. * // m = [10.0, 11.0, 12.0, 13.0]
  6005. * // [14.0, 15.0, 16.0, 17.0]
  6006. * // [18.0, 19.0, 20.0, 21.0]
  6007. * // [22.0, 23.0, 24.0, 25.0]
  6008. *
  6009. * const a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  6010. *
  6011. * // m remains the same
  6012. * // a = [10.0, 11.0, 12.0, 13.0]
  6013. * // [14.0, 15.0, 16.0, 17.0]
  6014. * // [99.0, 98.0, 97.0, 96.0]
  6015. * // [22.0, 23.0, 24.0, 25.0]
  6016. */
  6017. Matrix4.setRow = function (matrix, index, cartesian, result) {
  6018. //>>includeStart('debug', pragmas.debug);
  6019. RuntimeError.Check.typeOf.object("matrix", matrix);
  6020. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  6021. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  6022. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6023. RuntimeError.Check.typeOf.object("result", result);
  6024. //>>includeEnd('debug');
  6025. result = Matrix4.clone(matrix, result);
  6026. result[index] = cartesian.x;
  6027. result[index + 4] = cartesian.y;
  6028. result[index + 8] = cartesian.z;
  6029. result[index + 12] = cartesian.w;
  6030. return result;
  6031. };
  6032. /**
  6033. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  6034. * matrix with the provided translation. This assumes the matrix is an affine transformation.
  6035. *
  6036. * @param {Matrix4} matrix The matrix to use.
  6037. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  6038. * @param {Matrix4} result The object onto which to store the result.
  6039. * @returns {Matrix4} The modified result parameter.
  6040. */
  6041. Matrix4.setTranslation = function (matrix, translation, result) {
  6042. //>>includeStart('debug', pragmas.debug);
  6043. RuntimeError.Check.typeOf.object("matrix", matrix);
  6044. RuntimeError.Check.typeOf.object("translation", translation);
  6045. RuntimeError.Check.typeOf.object("result", result);
  6046. //>>includeEnd('debug');
  6047. result[0] = matrix[0];
  6048. result[1] = matrix[1];
  6049. result[2] = matrix[2];
  6050. result[3] = matrix[3];
  6051. result[4] = matrix[4];
  6052. result[5] = matrix[5];
  6053. result[6] = matrix[6];
  6054. result[7] = matrix[7];
  6055. result[8] = matrix[8];
  6056. result[9] = matrix[9];
  6057. result[10] = matrix[10];
  6058. result[11] = matrix[11];
  6059. result[12] = translation.x;
  6060. result[13] = translation.y;
  6061. result[14] = translation.z;
  6062. result[15] = matrix[15];
  6063. return result;
  6064. };
  6065. const scaleScratch1$1 = new Cartesian3();
  6066. /**
  6067. * Computes a new matrix that replaces the scale with the provided scale.
  6068. * This assumes the matrix is an affine transformation.
  6069. *
  6070. * @param {Matrix4} matrix The matrix to use.
  6071. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  6072. * @param {Matrix4} result The object onto which to store the result.
  6073. * @returns {Matrix4} The modified result parameter.
  6074. *
  6075. * @see Matrix4.setUniformScale
  6076. * @see Matrix4.fromScale
  6077. * @see Matrix4.fromUniformScale
  6078. * @see Matrix4.multiplyByScale
  6079. * @see Matrix4.multiplyByUniformScale
  6080. * @see Matrix4.getScale
  6081. */
  6082. Matrix4.setScale = function (matrix, scale, result) {
  6083. //>>includeStart('debug', pragmas.debug);
  6084. RuntimeError.Check.typeOf.object("matrix", matrix);
  6085. RuntimeError.Check.typeOf.object("scale", scale);
  6086. RuntimeError.Check.typeOf.object("result", result);
  6087. //>>includeEnd('debug');
  6088. const existingScale = Matrix4.getScale(matrix, scaleScratch1$1);
  6089. const scaleRatioX = scale.x / existingScale.x;
  6090. const scaleRatioY = scale.y / existingScale.y;
  6091. const scaleRatioZ = scale.z / existingScale.z;
  6092. result[0] = matrix[0] * scaleRatioX;
  6093. result[1] = matrix[1] * scaleRatioX;
  6094. result[2] = matrix[2] * scaleRatioX;
  6095. result[3] = matrix[3];
  6096. result[4] = matrix[4] * scaleRatioY;
  6097. result[5] = matrix[5] * scaleRatioY;
  6098. result[6] = matrix[6] * scaleRatioY;
  6099. result[7] = matrix[7];
  6100. result[8] = matrix[8] * scaleRatioZ;
  6101. result[9] = matrix[9] * scaleRatioZ;
  6102. result[10] = matrix[10] * scaleRatioZ;
  6103. result[11] = matrix[11];
  6104. result[12] = matrix[12];
  6105. result[13] = matrix[13];
  6106. result[14] = matrix[14];
  6107. result[15] = matrix[15];
  6108. return result;
  6109. };
  6110. const scaleScratch2$1 = new Cartesian3();
  6111. /**
  6112. * Computes a new matrix that replaces the scale with the provided uniform scale.
  6113. * This assumes the matrix is an affine transformation.
  6114. *
  6115. * @param {Matrix4} matrix The matrix to use.
  6116. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  6117. * @param {Matrix4} result The object onto which to store the result.
  6118. * @returns {Matrix4} The modified result parameter.
  6119. *
  6120. * @see Matrix4.setScale
  6121. * @see Matrix4.fromScale
  6122. * @see Matrix4.fromUniformScale
  6123. * @see Matrix4.multiplyByScale
  6124. * @see Matrix4.multiplyByUniformScale
  6125. * @see Matrix4.getScale
  6126. */
  6127. Matrix4.setUniformScale = function (matrix, scale, result) {
  6128. //>>includeStart('debug', pragmas.debug);
  6129. RuntimeError.Check.typeOf.object("matrix", matrix);
  6130. RuntimeError.Check.typeOf.number("scale", scale);
  6131. RuntimeError.Check.typeOf.object("result", result);
  6132. //>>includeEnd('debug');
  6133. const existingScale = Matrix4.getScale(matrix, scaleScratch2$1);
  6134. const scaleRatioX = scale / existingScale.x;
  6135. const scaleRatioY = scale / existingScale.y;
  6136. const scaleRatioZ = scale / existingScale.z;
  6137. result[0] = matrix[0] * scaleRatioX;
  6138. result[1] = matrix[1] * scaleRatioX;
  6139. result[2] = matrix[2] * scaleRatioX;
  6140. result[3] = matrix[3];
  6141. result[4] = matrix[4] * scaleRatioY;
  6142. result[5] = matrix[5] * scaleRatioY;
  6143. result[6] = matrix[6] * scaleRatioY;
  6144. result[7] = matrix[7];
  6145. result[8] = matrix[8] * scaleRatioZ;
  6146. result[9] = matrix[9] * scaleRatioZ;
  6147. result[10] = matrix[10] * scaleRatioZ;
  6148. result[11] = matrix[11];
  6149. result[12] = matrix[12];
  6150. result[13] = matrix[13];
  6151. result[14] = matrix[14];
  6152. result[15] = matrix[15];
  6153. return result;
  6154. };
  6155. const scratchColumn$1 = new Cartesian3();
  6156. /**
  6157. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  6158. *
  6159. * @param {Matrix4} matrix The matrix.
  6160. * @param {Cartesian3} result The object onto which to store the result.
  6161. * @returns {Cartesian3} The modified result parameter
  6162. *
  6163. * @see Matrix4.multiplyByScale
  6164. * @see Matrix4.multiplyByUniformScale
  6165. * @see Matrix4.fromScale
  6166. * @see Matrix4.fromUniformScale
  6167. * @see Matrix4.setScale
  6168. * @see Matrix4.setUniformScale
  6169. */
  6170. Matrix4.getScale = function (matrix, result) {
  6171. //>>includeStart('debug', pragmas.debug);
  6172. RuntimeError.Check.typeOf.object("matrix", matrix);
  6173. RuntimeError.Check.typeOf.object("result", result);
  6174. //>>includeEnd('debug');
  6175. result.x = Cartesian3.magnitude(
  6176. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$1)
  6177. );
  6178. result.y = Cartesian3.magnitude(
  6179. Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn$1)
  6180. );
  6181. result.z = Cartesian3.magnitude(
  6182. Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn$1)
  6183. );
  6184. return result;
  6185. };
  6186. const scaleScratch3$1 = new Cartesian3();
  6187. /**
  6188. * Computes the maximum scale assuming the matrix is an affine transformation.
  6189. * The maximum scale is the maximum length of the column vectors in the upper-left
  6190. * 3x3 matrix.
  6191. *
  6192. * @param {Matrix4} matrix The matrix.
  6193. * @returns {Number} The maximum scale.
  6194. */
  6195. Matrix4.getMaximumScale = function (matrix) {
  6196. Matrix4.getScale(matrix, scaleScratch3$1);
  6197. return Cartesian3.maximumComponent(scaleScratch3$1);
  6198. };
  6199. const scaleScratch4$1 = new Cartesian3();
  6200. /**
  6201. * Sets the rotation assuming the matrix is an affine transformation.
  6202. *
  6203. * @param {Matrix4} matrix The matrix.
  6204. * @param {Matrix4} rotation The rotation matrix.
  6205. * @returns {Matrix4} The modified result parameter.
  6206. *
  6207. * @see Matrix4.fromRotation
  6208. * @see Matrix4.getRotation
  6209. */
  6210. Matrix4.setRotation = function (matrix, rotation, result) {
  6211. //>>includeStart('debug', pragmas.debug);
  6212. RuntimeError.Check.typeOf.object("matrix", matrix);
  6213. RuntimeError.Check.typeOf.object("result", result);
  6214. //>>includeEnd('debug');
  6215. const scale = Matrix4.getScale(matrix, scaleScratch4$1);
  6216. result[0] = rotation[0] * scale.x;
  6217. result[1] = rotation[1] * scale.x;
  6218. result[2] = rotation[2] * scale.x;
  6219. result[3] = matrix[3];
  6220. result[4] = rotation[3] * scale.y;
  6221. result[5] = rotation[4] * scale.y;
  6222. result[6] = rotation[5] * scale.y;
  6223. result[7] = matrix[7];
  6224. result[8] = rotation[6] * scale.z;
  6225. result[9] = rotation[7] * scale.z;
  6226. result[10] = rotation[8] * scale.z;
  6227. result[11] = matrix[11];
  6228. result[12] = matrix[12];
  6229. result[13] = matrix[13];
  6230. result[14] = matrix[14];
  6231. result[15] = matrix[15];
  6232. return result;
  6233. };
  6234. const scaleScratch5$1 = new Cartesian3();
  6235. /**
  6236. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  6237. *
  6238. * @param {Matrix4} matrix The matrix.
  6239. * @param {Matrix4} result The object onto which to store the result.
  6240. * @returns {Matrix4} The modified result parameter.
  6241. *
  6242. * @see Matrix4.setRotation
  6243. * @see Matrix4.fromRotation
  6244. */
  6245. Matrix4.getRotation = function (matrix, result) {
  6246. //>>includeStart('debug', pragmas.debug);
  6247. RuntimeError.Check.typeOf.object("matrix", matrix);
  6248. RuntimeError.Check.typeOf.object("result", result);
  6249. //>>includeEnd('debug');
  6250. const scale = Matrix4.getScale(matrix, scaleScratch5$1);
  6251. result[0] = matrix[0] / scale.x;
  6252. result[1] = matrix[1] / scale.x;
  6253. result[2] = matrix[2] / scale.x;
  6254. result[3] = matrix[4] / scale.y;
  6255. result[4] = matrix[5] / scale.y;
  6256. result[5] = matrix[6] / scale.y;
  6257. result[6] = matrix[8] / scale.z;
  6258. result[7] = matrix[9] / scale.z;
  6259. result[8] = matrix[10] / scale.z;
  6260. return result;
  6261. };
  6262. /**
  6263. * Computes the product of two matrices.
  6264. *
  6265. * @param {Matrix4} left The first matrix.
  6266. * @param {Matrix4} right The second matrix.
  6267. * @param {Matrix4} result The object onto which to store the result.
  6268. * @returns {Matrix4} The modified result parameter.
  6269. */
  6270. Matrix4.multiply = function (left, right, result) {
  6271. //>>includeStart('debug', pragmas.debug);
  6272. RuntimeError.Check.typeOf.object("left", left);
  6273. RuntimeError.Check.typeOf.object("right", right);
  6274. RuntimeError.Check.typeOf.object("result", result);
  6275. //>>includeEnd('debug');
  6276. const left0 = left[0];
  6277. const left1 = left[1];
  6278. const left2 = left[2];
  6279. const left3 = left[3];
  6280. const left4 = left[4];
  6281. const left5 = left[5];
  6282. const left6 = left[6];
  6283. const left7 = left[7];
  6284. const left8 = left[8];
  6285. const left9 = left[9];
  6286. const left10 = left[10];
  6287. const left11 = left[11];
  6288. const left12 = left[12];
  6289. const left13 = left[13];
  6290. const left14 = left[14];
  6291. const left15 = left[15];
  6292. const right0 = right[0];
  6293. const right1 = right[1];
  6294. const right2 = right[2];
  6295. const right3 = right[3];
  6296. const right4 = right[4];
  6297. const right5 = right[5];
  6298. const right6 = right[6];
  6299. const right7 = right[7];
  6300. const right8 = right[8];
  6301. const right9 = right[9];
  6302. const right10 = right[10];
  6303. const right11 = right[11];
  6304. const right12 = right[12];
  6305. const right13 = right[13];
  6306. const right14 = right[14];
  6307. const right15 = right[15];
  6308. const column0Row0 =
  6309. left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  6310. const column0Row1 =
  6311. left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  6312. const column0Row2 =
  6313. left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  6314. const column0Row3 =
  6315. left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  6316. const column1Row0 =
  6317. left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  6318. const column1Row1 =
  6319. left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  6320. const column1Row2 =
  6321. left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  6322. const column1Row3 =
  6323. left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  6324. const column2Row0 =
  6325. left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  6326. const column2Row1 =
  6327. left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  6328. const column2Row2 =
  6329. left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  6330. const column2Row3 =
  6331. left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  6332. const column3Row0 =
  6333. left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  6334. const column3Row1 =
  6335. left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  6336. const column3Row2 =
  6337. left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  6338. const column3Row3 =
  6339. left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  6340. result[0] = column0Row0;
  6341. result[1] = column0Row1;
  6342. result[2] = column0Row2;
  6343. result[3] = column0Row3;
  6344. result[4] = column1Row0;
  6345. result[5] = column1Row1;
  6346. result[6] = column1Row2;
  6347. result[7] = column1Row3;
  6348. result[8] = column2Row0;
  6349. result[9] = column2Row1;
  6350. result[10] = column2Row2;
  6351. result[11] = column2Row3;
  6352. result[12] = column3Row0;
  6353. result[13] = column3Row1;
  6354. result[14] = column3Row2;
  6355. result[15] = column3Row3;
  6356. return result;
  6357. };
  6358. /**
  6359. * Computes the sum of two matrices.
  6360. *
  6361. * @param {Matrix4} left The first matrix.
  6362. * @param {Matrix4} right The second matrix.
  6363. * @param {Matrix4} result The object onto which to store the result.
  6364. * @returns {Matrix4} The modified result parameter.
  6365. */
  6366. Matrix4.add = function (left, right, result) {
  6367. //>>includeStart('debug', pragmas.debug);
  6368. RuntimeError.Check.typeOf.object("left", left);
  6369. RuntimeError.Check.typeOf.object("right", right);
  6370. RuntimeError.Check.typeOf.object("result", result);
  6371. //>>includeEnd('debug');
  6372. result[0] = left[0] + right[0];
  6373. result[1] = left[1] + right[1];
  6374. result[2] = left[2] + right[2];
  6375. result[3] = left[3] + right[3];
  6376. result[4] = left[4] + right[4];
  6377. result[5] = left[5] + right[5];
  6378. result[6] = left[6] + right[6];
  6379. result[7] = left[7] + right[7];
  6380. result[8] = left[8] + right[8];
  6381. result[9] = left[9] + right[9];
  6382. result[10] = left[10] + right[10];
  6383. result[11] = left[11] + right[11];
  6384. result[12] = left[12] + right[12];
  6385. result[13] = left[13] + right[13];
  6386. result[14] = left[14] + right[14];
  6387. result[15] = left[15] + right[15];
  6388. return result;
  6389. };
  6390. /**
  6391. * Computes the difference of two matrices.
  6392. *
  6393. * @param {Matrix4} left The first matrix.
  6394. * @param {Matrix4} right The second matrix.
  6395. * @param {Matrix4} result The object onto which to store the result.
  6396. * @returns {Matrix4} The modified result parameter.
  6397. */
  6398. Matrix4.subtract = function (left, right, result) {
  6399. //>>includeStart('debug', pragmas.debug);
  6400. RuntimeError.Check.typeOf.object("left", left);
  6401. RuntimeError.Check.typeOf.object("right", right);
  6402. RuntimeError.Check.typeOf.object("result", result);
  6403. //>>includeEnd('debug');
  6404. result[0] = left[0] - right[0];
  6405. result[1] = left[1] - right[1];
  6406. result[2] = left[2] - right[2];
  6407. result[3] = left[3] - right[3];
  6408. result[4] = left[4] - right[4];
  6409. result[5] = left[5] - right[5];
  6410. result[6] = left[6] - right[6];
  6411. result[7] = left[7] - right[7];
  6412. result[8] = left[8] - right[8];
  6413. result[9] = left[9] - right[9];
  6414. result[10] = left[10] - right[10];
  6415. result[11] = left[11] - right[11];
  6416. result[12] = left[12] - right[12];
  6417. result[13] = left[13] - right[13];
  6418. result[14] = left[14] - right[14];
  6419. result[15] = left[15] - right[15];
  6420. return result;
  6421. };
  6422. /**
  6423. * Computes the product of two matrices assuming the matrices are affine transformation matrices,
  6424. * where the upper left 3x3 elements are any matrix, and
  6425. * the upper three elements in the fourth column are the translation.
  6426. * The bottom row is assumed to be [0, 0, 0, 1].
  6427. * The matrix is not verified to be in the proper form.
  6428. * This method is faster than computing the product for general 4x4
  6429. * matrices using {@link Matrix4.multiply}.
  6430. *
  6431. * @param {Matrix4} left The first matrix.
  6432. * @param {Matrix4} right The second matrix.
  6433. * @param {Matrix4} result The object onto which to store the result.
  6434. * @returns {Matrix4} The modified result parameter.
  6435. *
  6436. * @example
  6437. * 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);
  6438. * const m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  6439. * const m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  6440. */
  6441. Matrix4.multiplyTransformation = function (left, right, result) {
  6442. //>>includeStart('debug', pragmas.debug);
  6443. RuntimeError.Check.typeOf.object("left", left);
  6444. RuntimeError.Check.typeOf.object("right", right);
  6445. RuntimeError.Check.typeOf.object("result", result);
  6446. //>>includeEnd('debug');
  6447. const left0 = left[0];
  6448. const left1 = left[1];
  6449. const left2 = left[2];
  6450. const left4 = left[4];
  6451. const left5 = left[5];
  6452. const left6 = left[6];
  6453. const left8 = left[8];
  6454. const left9 = left[9];
  6455. const left10 = left[10];
  6456. const left12 = left[12];
  6457. const left13 = left[13];
  6458. const left14 = left[14];
  6459. const right0 = right[0];
  6460. const right1 = right[1];
  6461. const right2 = right[2];
  6462. const right4 = right[4];
  6463. const right5 = right[5];
  6464. const right6 = right[6];
  6465. const right8 = right[8];
  6466. const right9 = right[9];
  6467. const right10 = right[10];
  6468. const right12 = right[12];
  6469. const right13 = right[13];
  6470. const right14 = right[14];
  6471. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6472. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6473. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6474. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6475. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6476. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6477. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6478. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6479. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6480. const column3Row0 =
  6481. left0 * right12 + left4 * right13 + left8 * right14 + left12;
  6482. const column3Row1 =
  6483. left1 * right12 + left5 * right13 + left9 * right14 + left13;
  6484. const column3Row2 =
  6485. left2 * right12 + left6 * right13 + left10 * right14 + left14;
  6486. result[0] = column0Row0;
  6487. result[1] = column0Row1;
  6488. result[2] = column0Row2;
  6489. result[3] = 0.0;
  6490. result[4] = column1Row0;
  6491. result[5] = column1Row1;
  6492. result[6] = column1Row2;
  6493. result[7] = 0.0;
  6494. result[8] = column2Row0;
  6495. result[9] = column2Row1;
  6496. result[10] = column2Row2;
  6497. result[11] = 0.0;
  6498. result[12] = column3Row0;
  6499. result[13] = column3Row1;
  6500. result[14] = column3Row2;
  6501. result[15] = 1.0;
  6502. return result;
  6503. };
  6504. /**
  6505. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6506. * by a 3x3 rotation matrix. This is an optimization
  6507. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  6508. *
  6509. * @param {Matrix4} matrix The matrix on the left-hand side.
  6510. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  6511. * @param {Matrix4} result The object onto which to store the result.
  6512. * @returns {Matrix4} The modified result parameter.
  6513. *
  6514. * @example
  6515. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  6516. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  6517. */
  6518. Matrix4.multiplyByMatrix3 = function (matrix, rotation, result) {
  6519. //>>includeStart('debug', pragmas.debug);
  6520. RuntimeError.Check.typeOf.object("matrix", matrix);
  6521. RuntimeError.Check.typeOf.object("rotation", rotation);
  6522. RuntimeError.Check.typeOf.object("result", result);
  6523. //>>includeEnd('debug');
  6524. const left0 = matrix[0];
  6525. const left1 = matrix[1];
  6526. const left2 = matrix[2];
  6527. const left4 = matrix[4];
  6528. const left5 = matrix[5];
  6529. const left6 = matrix[6];
  6530. const left8 = matrix[8];
  6531. const left9 = matrix[9];
  6532. const left10 = matrix[10];
  6533. const right0 = rotation[0];
  6534. const right1 = rotation[1];
  6535. const right2 = rotation[2];
  6536. const right4 = rotation[3];
  6537. const right5 = rotation[4];
  6538. const right6 = rotation[5];
  6539. const right8 = rotation[6];
  6540. const right9 = rotation[7];
  6541. const right10 = rotation[8];
  6542. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6543. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6544. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6545. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6546. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6547. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6548. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6549. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6550. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6551. result[0] = column0Row0;
  6552. result[1] = column0Row1;
  6553. result[2] = column0Row2;
  6554. result[3] = 0.0;
  6555. result[4] = column1Row0;
  6556. result[5] = column1Row1;
  6557. result[6] = column1Row2;
  6558. result[7] = 0.0;
  6559. result[8] = column2Row0;
  6560. result[9] = column2Row1;
  6561. result[10] = column2Row2;
  6562. result[11] = 0.0;
  6563. result[12] = matrix[12];
  6564. result[13] = matrix[13];
  6565. result[14] = matrix[14];
  6566. result[15] = matrix[15];
  6567. return result;
  6568. };
  6569. /**
  6570. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6571. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  6572. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  6573. *
  6574. * @param {Matrix4} matrix The matrix on the left-hand side.
  6575. * @param {Cartesian3} translation The translation on the right-hand side.
  6576. * @param {Matrix4} result The object onto which to store the result.
  6577. * @returns {Matrix4} The modified result parameter.
  6578. *
  6579. * @example
  6580. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  6581. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  6582. */
  6583. Matrix4.multiplyByTranslation = function (matrix, translation, result) {
  6584. //>>includeStart('debug', pragmas.debug);
  6585. RuntimeError.Check.typeOf.object("matrix", matrix);
  6586. RuntimeError.Check.typeOf.object("translation", translation);
  6587. RuntimeError.Check.typeOf.object("result", result);
  6588. //>>includeEnd('debug');
  6589. const x = translation.x;
  6590. const y = translation.y;
  6591. const z = translation.z;
  6592. const tx = x * matrix[0] + y * matrix[4] + z * matrix[8] + matrix[12];
  6593. const ty = x * matrix[1] + y * matrix[5] + z * matrix[9] + matrix[13];
  6594. const tz = x * matrix[2] + y * matrix[6] + z * matrix[10] + matrix[14];
  6595. result[0] = matrix[0];
  6596. result[1] = matrix[1];
  6597. result[2] = matrix[2];
  6598. result[3] = matrix[3];
  6599. result[4] = matrix[4];
  6600. result[5] = matrix[5];
  6601. result[6] = matrix[6];
  6602. result[7] = matrix[7];
  6603. result[8] = matrix[8];
  6604. result[9] = matrix[9];
  6605. result[10] = matrix[10];
  6606. result[11] = matrix[11];
  6607. result[12] = tx;
  6608. result[13] = ty;
  6609. result[14] = tz;
  6610. result[15] = matrix[15];
  6611. return result;
  6612. };
  6613. /**
  6614. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6615. * by an implicit non-uniform scale matrix. This is an optimization
  6616. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  6617. * <code>m</code> must be an affine matrix.
  6618. * This function performs fewer allocations and arithmetic operations.
  6619. *
  6620. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  6621. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  6622. * @param {Matrix4} result The object onto which to store the result.
  6623. * @returns {Matrix4} The modified result parameter.
  6624. *
  6625. *
  6626. * @example
  6627. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  6628. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  6629. *
  6630. * @see Matrix4.multiplyByUniformScale
  6631. * @see Matrix4.fromScale
  6632. * @see Matrix4.fromUniformScale
  6633. * @see Matrix4.setScale
  6634. * @see Matrix4.setUniformScale
  6635. * @see Matrix4.getScale
  6636. */
  6637. Matrix4.multiplyByScale = function (matrix, scale, result) {
  6638. //>>includeStart('debug', pragmas.debug);
  6639. RuntimeError.Check.typeOf.object("matrix", matrix);
  6640. RuntimeError.Check.typeOf.object("scale", scale);
  6641. RuntimeError.Check.typeOf.object("result", result);
  6642. //>>includeEnd('debug');
  6643. const scaleX = scale.x;
  6644. const scaleY = scale.y;
  6645. const scaleZ = scale.z;
  6646. // Faster than Cartesian3.equals
  6647. if (scaleX === 1.0 && scaleY === 1.0 && scaleZ === 1.0) {
  6648. return Matrix4.clone(matrix, result);
  6649. }
  6650. result[0] = scaleX * matrix[0];
  6651. result[1] = scaleX * matrix[1];
  6652. result[2] = scaleX * matrix[2];
  6653. result[3] = matrix[3];
  6654. result[4] = scaleY * matrix[4];
  6655. result[5] = scaleY * matrix[5];
  6656. result[6] = scaleY * matrix[6];
  6657. result[7] = matrix[7];
  6658. result[8] = scaleZ * matrix[8];
  6659. result[9] = scaleZ * matrix[9];
  6660. result[10] = scaleZ * matrix[10];
  6661. result[11] = matrix[11];
  6662. result[12] = matrix[12];
  6663. result[13] = matrix[13];
  6664. result[14] = matrix[14];
  6665. result[15] = matrix[15];
  6666. return result;
  6667. };
  6668. /**
  6669. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  6670. *
  6671. * @param {Matrix4} matrix The matrix on the left-hand side.
  6672. * @param {Number} scale The uniform scale on the right-hand side.
  6673. * @param {Matrix4} result The object onto which to store the result.
  6674. * @returns {Matrix4} The modified result parameter.
  6675. *
  6676. * @example
  6677. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  6678. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  6679. *
  6680. * @see Matrix4.multiplyByScale
  6681. * @see Matrix4.fromScale
  6682. * @see Matrix4.fromUniformScale
  6683. * @see Matrix4.setScale
  6684. * @see Matrix4.setUniformScale
  6685. * @see Matrix4.getScale
  6686. */
  6687. Matrix4.multiplyByUniformScale = function (matrix, scale, result) {
  6688. //>>includeStart('debug', pragmas.debug);
  6689. RuntimeError.Check.typeOf.object("matrix", matrix);
  6690. RuntimeError.Check.typeOf.number("scale", scale);
  6691. RuntimeError.Check.typeOf.object("result", result);
  6692. //>>includeEnd('debug');
  6693. result[0] = matrix[0] * scale;
  6694. result[1] = matrix[1] * scale;
  6695. result[2] = matrix[2] * scale;
  6696. result[3] = matrix[3];
  6697. result[4] = matrix[4] * scale;
  6698. result[5] = matrix[5] * scale;
  6699. result[6] = matrix[6] * scale;
  6700. result[7] = matrix[7];
  6701. result[8] = matrix[8] * scale;
  6702. result[9] = matrix[9] * scale;
  6703. result[10] = matrix[10] * scale;
  6704. result[11] = matrix[11];
  6705. result[12] = matrix[12];
  6706. result[13] = matrix[13];
  6707. result[14] = matrix[14];
  6708. result[15] = matrix[15];
  6709. return result;
  6710. };
  6711. /**
  6712. * Computes the product of a matrix and a column vector.
  6713. *
  6714. * @param {Matrix4} matrix The matrix.
  6715. * @param {Cartesian4} cartesian The vector.
  6716. * @param {Cartesian4} result The object onto which to store the result.
  6717. * @returns {Cartesian4} The modified result parameter.
  6718. */
  6719. Matrix4.multiplyByVector = function (matrix, cartesian, result) {
  6720. //>>includeStart('debug', pragmas.debug);
  6721. RuntimeError.Check.typeOf.object("matrix", matrix);
  6722. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6723. RuntimeError.Check.typeOf.object("result", result);
  6724. //>>includeEnd('debug');
  6725. const vX = cartesian.x;
  6726. const vY = cartesian.y;
  6727. const vZ = cartesian.z;
  6728. const vW = cartesian.w;
  6729. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  6730. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  6731. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  6732. const w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  6733. result.x = x;
  6734. result.y = y;
  6735. result.z = z;
  6736. result.w = w;
  6737. return result;
  6738. };
  6739. /**
  6740. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  6741. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  6742. *
  6743. * @param {Matrix4} matrix The matrix.
  6744. * @param {Cartesian3} cartesian The point.
  6745. * @param {Cartesian3} result The object onto which to store the result.
  6746. * @returns {Cartesian3} The modified result parameter.
  6747. *
  6748. * @example
  6749. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  6750. * const result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  6751. * // A shortcut for
  6752. * // Cartesian3 p = ...
  6753. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  6754. */
  6755. Matrix4.multiplyByPointAsVector = function (matrix, cartesian, result) {
  6756. //>>includeStart('debug', pragmas.debug);
  6757. RuntimeError.Check.typeOf.object("matrix", matrix);
  6758. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6759. RuntimeError.Check.typeOf.object("result", result);
  6760. //>>includeEnd('debug');
  6761. const vX = cartesian.x;
  6762. const vY = cartesian.y;
  6763. const vZ = cartesian.z;
  6764. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  6765. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  6766. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  6767. result.x = x;
  6768. result.y = y;
  6769. result.z = z;
  6770. return result;
  6771. };
  6772. /**
  6773. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  6774. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  6775. *
  6776. * @param {Matrix4} matrix The matrix.
  6777. * @param {Cartesian3} cartesian The point.
  6778. * @param {Cartesian3} result The object onto which to store the result.
  6779. * @returns {Cartesian3} The modified result parameter.
  6780. *
  6781. * @example
  6782. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  6783. * const result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  6784. */
  6785. Matrix4.multiplyByPoint = function (matrix, cartesian, result) {
  6786. //>>includeStart('debug', pragmas.debug);
  6787. RuntimeError.Check.typeOf.object("matrix", matrix);
  6788. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6789. RuntimeError.Check.typeOf.object("result", result);
  6790. //>>includeEnd('debug');
  6791. const vX = cartesian.x;
  6792. const vY = cartesian.y;
  6793. const vZ = cartesian.z;
  6794. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  6795. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  6796. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  6797. result.x = x;
  6798. result.y = y;
  6799. result.z = z;
  6800. return result;
  6801. };
  6802. /**
  6803. * Computes the product of a matrix and a scalar.
  6804. *
  6805. * @param {Matrix4} matrix The matrix.
  6806. * @param {Number} scalar The number to multiply by.
  6807. * @param {Matrix4} result The object onto which to store the result.
  6808. * @returns {Matrix4} The modified result parameter.
  6809. *
  6810. * @example
  6811. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  6812. * // m = [10.0, 11.0, 12.0, 13.0]
  6813. * // [14.0, 15.0, 16.0, 17.0]
  6814. * // [18.0, 19.0, 20.0, 21.0]
  6815. * // [22.0, 23.0, 24.0, 25.0]
  6816. *
  6817. * const a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  6818. *
  6819. * // m remains the same
  6820. * // a = [-20.0, -22.0, -24.0, -26.0]
  6821. * // [-28.0, -30.0, -32.0, -34.0]
  6822. * // [-36.0, -38.0, -40.0, -42.0]
  6823. * // [-44.0, -46.0, -48.0, -50.0]
  6824. */
  6825. Matrix4.multiplyByScalar = function (matrix, scalar, result) {
  6826. //>>includeStart('debug', pragmas.debug);
  6827. RuntimeError.Check.typeOf.object("matrix", matrix);
  6828. RuntimeError.Check.typeOf.number("scalar", scalar);
  6829. RuntimeError.Check.typeOf.object("result", result);
  6830. //>>includeEnd('debug');
  6831. result[0] = matrix[0] * scalar;
  6832. result[1] = matrix[1] * scalar;
  6833. result[2] = matrix[2] * scalar;
  6834. result[3] = matrix[3] * scalar;
  6835. result[4] = matrix[4] * scalar;
  6836. result[5] = matrix[5] * scalar;
  6837. result[6] = matrix[6] * scalar;
  6838. result[7] = matrix[7] * scalar;
  6839. result[8] = matrix[8] * scalar;
  6840. result[9] = matrix[9] * scalar;
  6841. result[10] = matrix[10] * scalar;
  6842. result[11] = matrix[11] * scalar;
  6843. result[12] = matrix[12] * scalar;
  6844. result[13] = matrix[13] * scalar;
  6845. result[14] = matrix[14] * scalar;
  6846. result[15] = matrix[15] * scalar;
  6847. return result;
  6848. };
  6849. /**
  6850. * Computes a negated copy of the provided matrix.
  6851. *
  6852. * @param {Matrix4} matrix The matrix to negate.
  6853. * @param {Matrix4} result The object onto which to store the result.
  6854. * @returns {Matrix4} The modified result parameter.
  6855. *
  6856. * @example
  6857. * //create a new Matrix4 instance which is a negation of a Matrix4
  6858. * // m = [10.0, 11.0, 12.0, 13.0]
  6859. * // [14.0, 15.0, 16.0, 17.0]
  6860. * // [18.0, 19.0, 20.0, 21.0]
  6861. * // [22.0, 23.0, 24.0, 25.0]
  6862. *
  6863. * const a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  6864. *
  6865. * // m remains the same
  6866. * // a = [-10.0, -11.0, -12.0, -13.0]
  6867. * // [-14.0, -15.0, -16.0, -17.0]
  6868. * // [-18.0, -19.0, -20.0, -21.0]
  6869. * // [-22.0, -23.0, -24.0, -25.0]
  6870. */
  6871. Matrix4.negate = function (matrix, result) {
  6872. //>>includeStart('debug', pragmas.debug);
  6873. RuntimeError.Check.typeOf.object("matrix", matrix);
  6874. RuntimeError.Check.typeOf.object("result", result);
  6875. //>>includeEnd('debug');
  6876. result[0] = -matrix[0];
  6877. result[1] = -matrix[1];
  6878. result[2] = -matrix[2];
  6879. result[3] = -matrix[3];
  6880. result[4] = -matrix[4];
  6881. result[5] = -matrix[5];
  6882. result[6] = -matrix[6];
  6883. result[7] = -matrix[7];
  6884. result[8] = -matrix[8];
  6885. result[9] = -matrix[9];
  6886. result[10] = -matrix[10];
  6887. result[11] = -matrix[11];
  6888. result[12] = -matrix[12];
  6889. result[13] = -matrix[13];
  6890. result[14] = -matrix[14];
  6891. result[15] = -matrix[15];
  6892. return result;
  6893. };
  6894. /**
  6895. * Computes the transpose of the provided matrix.
  6896. *
  6897. * @param {Matrix4} matrix The matrix to transpose.
  6898. * @param {Matrix4} result The object onto which to store the result.
  6899. * @returns {Matrix4} The modified result parameter.
  6900. *
  6901. * @example
  6902. * //returns transpose of a Matrix4
  6903. * // m = [10.0, 11.0, 12.0, 13.0]
  6904. * // [14.0, 15.0, 16.0, 17.0]
  6905. * // [18.0, 19.0, 20.0, 21.0]
  6906. * // [22.0, 23.0, 24.0, 25.0]
  6907. *
  6908. * const a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  6909. *
  6910. * // m remains the same
  6911. * // a = [10.0, 14.0, 18.0, 22.0]
  6912. * // [11.0, 15.0, 19.0, 23.0]
  6913. * // [12.0, 16.0, 20.0, 24.0]
  6914. * // [13.0, 17.0, 21.0, 25.0]
  6915. */
  6916. Matrix4.transpose = function (matrix, result) {
  6917. //>>includeStart('debug', pragmas.debug);
  6918. RuntimeError.Check.typeOf.object("matrix", matrix);
  6919. RuntimeError.Check.typeOf.object("result", result);
  6920. //>>includeEnd('debug');
  6921. const matrix1 = matrix[1];
  6922. const matrix2 = matrix[2];
  6923. const matrix3 = matrix[3];
  6924. const matrix6 = matrix[6];
  6925. const matrix7 = matrix[7];
  6926. const matrix11 = matrix[11];
  6927. result[0] = matrix[0];
  6928. result[1] = matrix[4];
  6929. result[2] = matrix[8];
  6930. result[3] = matrix[12];
  6931. result[4] = matrix1;
  6932. result[5] = matrix[5];
  6933. result[6] = matrix[9];
  6934. result[7] = matrix[13];
  6935. result[8] = matrix2;
  6936. result[9] = matrix6;
  6937. result[10] = matrix[10];
  6938. result[11] = matrix[14];
  6939. result[12] = matrix3;
  6940. result[13] = matrix7;
  6941. result[14] = matrix11;
  6942. result[15] = matrix[15];
  6943. return result;
  6944. };
  6945. /**
  6946. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  6947. *
  6948. * @param {Matrix4} matrix The matrix with signed elements.
  6949. * @param {Matrix4} result The object onto which to store the result.
  6950. * @returns {Matrix4} The modified result parameter.
  6951. */
  6952. Matrix4.abs = function (matrix, result) {
  6953. //>>includeStart('debug', pragmas.debug);
  6954. RuntimeError.Check.typeOf.object("matrix", matrix);
  6955. RuntimeError.Check.typeOf.object("result", result);
  6956. //>>includeEnd('debug');
  6957. result[0] = Math.abs(matrix[0]);
  6958. result[1] = Math.abs(matrix[1]);
  6959. result[2] = Math.abs(matrix[2]);
  6960. result[3] = Math.abs(matrix[3]);
  6961. result[4] = Math.abs(matrix[4]);
  6962. result[5] = Math.abs(matrix[5]);
  6963. result[6] = Math.abs(matrix[6]);
  6964. result[7] = Math.abs(matrix[7]);
  6965. result[8] = Math.abs(matrix[8]);
  6966. result[9] = Math.abs(matrix[9]);
  6967. result[10] = Math.abs(matrix[10]);
  6968. result[11] = Math.abs(matrix[11]);
  6969. result[12] = Math.abs(matrix[12]);
  6970. result[13] = Math.abs(matrix[13]);
  6971. result[14] = Math.abs(matrix[14]);
  6972. result[15] = Math.abs(matrix[15]);
  6973. return result;
  6974. };
  6975. /**
  6976. * Compares the provided matrices componentwise and returns
  6977. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6978. *
  6979. * @param {Matrix4} [left] The first matrix.
  6980. * @param {Matrix4} [right] The second matrix.
  6981. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  6982. *
  6983. * @example
  6984. * //compares two Matrix4 instances
  6985. *
  6986. * // a = [10.0, 14.0, 18.0, 22.0]
  6987. * // [11.0, 15.0, 19.0, 23.0]
  6988. * // [12.0, 16.0, 20.0, 24.0]
  6989. * // [13.0, 17.0, 21.0, 25.0]
  6990. *
  6991. * // b = [10.0, 14.0, 18.0, 22.0]
  6992. * // [11.0, 15.0, 19.0, 23.0]
  6993. * // [12.0, 16.0, 20.0, 24.0]
  6994. * // [13.0, 17.0, 21.0, 25.0]
  6995. *
  6996. * if(Cesium.Matrix4.equals(a,b)) {
  6997. * console.log("Both matrices are equal");
  6998. * } else {
  6999. * console.log("They are not equal");
  7000. * }
  7001. *
  7002. * //Prints "Both matrices are equal" on the console
  7003. */
  7004. Matrix4.equals = function (left, right) {
  7005. // Given that most matrices will be transformation matrices, the elements
  7006. // are tested in order such that the test is likely to fail as early
  7007. // as possible. I _think_ this is just as friendly to the L1 cache
  7008. // as testing in index order. It is certainty faster in practice.
  7009. return (
  7010. left === right ||
  7011. (defaultValue.defined(left) &&
  7012. defaultValue.defined(right) &&
  7013. // Translation
  7014. left[12] === right[12] &&
  7015. left[13] === right[13] &&
  7016. left[14] === right[14] &&
  7017. // Rotation/scale
  7018. left[0] === right[0] &&
  7019. left[1] === right[1] &&
  7020. left[2] === right[2] &&
  7021. left[4] === right[4] &&
  7022. left[5] === right[5] &&
  7023. left[6] === right[6] &&
  7024. left[8] === right[8] &&
  7025. left[9] === right[9] &&
  7026. left[10] === right[10] &&
  7027. // Bottom row
  7028. left[3] === right[3] &&
  7029. left[7] === right[7] &&
  7030. left[11] === right[11] &&
  7031. left[15] === right[15])
  7032. );
  7033. };
  7034. /**
  7035. * Compares the provided matrices componentwise and returns
  7036. * <code>true</code> if they are within the provided epsilon,
  7037. * <code>false</code> otherwise.
  7038. *
  7039. * @param {Matrix4} [left] The first matrix.
  7040. * @param {Matrix4} [right] The second matrix.
  7041. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7042. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  7043. *
  7044. * @example
  7045. * //compares two Matrix4 instances
  7046. *
  7047. * // a = [10.5, 14.5, 18.5, 22.5]
  7048. * // [11.5, 15.5, 19.5, 23.5]
  7049. * // [12.5, 16.5, 20.5, 24.5]
  7050. * // [13.5, 17.5, 21.5, 25.5]
  7051. *
  7052. * // b = [10.0, 14.0, 18.0, 22.0]
  7053. * // [11.0, 15.0, 19.0, 23.0]
  7054. * // [12.0, 16.0, 20.0, 24.0]
  7055. * // [13.0, 17.0, 21.0, 25.0]
  7056. *
  7057. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  7058. * console.log("Difference between both the matrices is less than 0.1");
  7059. * } else {
  7060. * console.log("Difference between both the matrices is not less than 0.1");
  7061. * }
  7062. *
  7063. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  7064. */
  7065. Matrix4.equalsEpsilon = function (left, right, epsilon) {
  7066. epsilon = defaultValue.defaultValue(epsilon, 0);
  7067. return (
  7068. left === right ||
  7069. (defaultValue.defined(left) &&
  7070. defaultValue.defined(right) &&
  7071. Math.abs(left[0] - right[0]) <= epsilon &&
  7072. Math.abs(left[1] - right[1]) <= epsilon &&
  7073. Math.abs(left[2] - right[2]) <= epsilon &&
  7074. Math.abs(left[3] - right[3]) <= epsilon &&
  7075. Math.abs(left[4] - right[4]) <= epsilon &&
  7076. Math.abs(left[5] - right[5]) <= epsilon &&
  7077. Math.abs(left[6] - right[6]) <= epsilon &&
  7078. Math.abs(left[7] - right[7]) <= epsilon &&
  7079. Math.abs(left[8] - right[8]) <= epsilon &&
  7080. Math.abs(left[9] - right[9]) <= epsilon &&
  7081. Math.abs(left[10] - right[10]) <= epsilon &&
  7082. Math.abs(left[11] - right[11]) <= epsilon &&
  7083. Math.abs(left[12] - right[12]) <= epsilon &&
  7084. Math.abs(left[13] - right[13]) <= epsilon &&
  7085. Math.abs(left[14] - right[14]) <= epsilon &&
  7086. Math.abs(left[15] - right[15]) <= epsilon)
  7087. );
  7088. };
  7089. /**
  7090. * Gets the translation portion of the provided matrix, assuming the matrix is an affine transformation matrix.
  7091. *
  7092. * @param {Matrix4} matrix The matrix to use.
  7093. * @param {Cartesian3} result The object onto which to store the result.
  7094. * @returns {Cartesian3} The modified result parameter.
  7095. */
  7096. Matrix4.getTranslation = function (matrix, result) {
  7097. //>>includeStart('debug', pragmas.debug);
  7098. RuntimeError.Check.typeOf.object("matrix", matrix);
  7099. RuntimeError.Check.typeOf.object("result", result);
  7100. //>>includeEnd('debug');
  7101. result.x = matrix[12];
  7102. result.y = matrix[13];
  7103. result.z = matrix[14];
  7104. return result;
  7105. };
  7106. /**
  7107. * Gets the upper left 3x3 matrix of the provided matrix.
  7108. *
  7109. * @param {Matrix4} matrix The matrix to use.
  7110. * @param {Matrix3} result The object onto which to store the result.
  7111. * @returns {Matrix3} The modified result parameter.
  7112. *
  7113. * @example
  7114. * // returns a Matrix3 instance from a Matrix4 instance
  7115. *
  7116. * // m = [10.0, 14.0, 18.0, 22.0]
  7117. * // [11.0, 15.0, 19.0, 23.0]
  7118. * // [12.0, 16.0, 20.0, 24.0]
  7119. * // [13.0, 17.0, 21.0, 25.0]
  7120. *
  7121. * const b = new Cesium.Matrix3();
  7122. * Cesium.Matrix4.getMatrix3(m,b);
  7123. *
  7124. * // b = [10.0, 14.0, 18.0]
  7125. * // [11.0, 15.0, 19.0]
  7126. * // [12.0, 16.0, 20.0]
  7127. */
  7128. Matrix4.getMatrix3 = function (matrix, result) {
  7129. //>>includeStart('debug', pragmas.debug);
  7130. RuntimeError.Check.typeOf.object("matrix", matrix);
  7131. RuntimeError.Check.typeOf.object("result", result);
  7132. //>>includeEnd('debug');
  7133. result[0] = matrix[0];
  7134. result[1] = matrix[1];
  7135. result[2] = matrix[2];
  7136. result[3] = matrix[4];
  7137. result[4] = matrix[5];
  7138. result[5] = matrix[6];
  7139. result[6] = matrix[8];
  7140. result[7] = matrix[9];
  7141. result[8] = matrix[10];
  7142. return result;
  7143. };
  7144. const scratchInverseRotation = new Matrix3();
  7145. const scratchMatrix3Zero = new Matrix3();
  7146. const scratchBottomRow = new Cartesian4();
  7147. const scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  7148. /**
  7149. * Computes the inverse of the provided matrix using Cramers Rule.
  7150. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  7151. * If the matrix is a proper rigid transformation, it is more efficient
  7152. * to invert it with {@link Matrix4.inverseTransformation}.
  7153. *
  7154. * @param {Matrix4} matrix The matrix to invert.
  7155. * @param {Matrix4} result The object onto which to store the result.
  7156. * @returns {Matrix4} The modified result parameter.
  7157. *
  7158. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  7159. */
  7160. Matrix4.inverse = function (matrix, result) {
  7161. //>>includeStart('debug', pragmas.debug);
  7162. RuntimeError.Check.typeOf.object("matrix", matrix);
  7163. RuntimeError.Check.typeOf.object("result", result);
  7164. //>>includeEnd('debug');
  7165. //
  7166. // Ported from:
  7167. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  7168. //
  7169. const src0 = matrix[0];
  7170. const src1 = matrix[4];
  7171. const src2 = matrix[8];
  7172. const src3 = matrix[12];
  7173. const src4 = matrix[1];
  7174. const src5 = matrix[5];
  7175. const src6 = matrix[9];
  7176. const src7 = matrix[13];
  7177. const src8 = matrix[2];
  7178. const src9 = matrix[6];
  7179. const src10 = matrix[10];
  7180. const src11 = matrix[14];
  7181. const src12 = matrix[3];
  7182. const src13 = matrix[7];
  7183. const src14 = matrix[11];
  7184. const src15 = matrix[15];
  7185. // calculate pairs for first 8 elements (cofactors)
  7186. let tmp0 = src10 * src15;
  7187. let tmp1 = src11 * src14;
  7188. let tmp2 = src9 * src15;
  7189. let tmp3 = src11 * src13;
  7190. let tmp4 = src9 * src14;
  7191. let tmp5 = src10 * src13;
  7192. let tmp6 = src8 * src15;
  7193. let tmp7 = src11 * src12;
  7194. let tmp8 = src8 * src14;
  7195. let tmp9 = src10 * src12;
  7196. let tmp10 = src8 * src13;
  7197. let tmp11 = src9 * src12;
  7198. // calculate first 8 elements (cofactors)
  7199. const dst0 =
  7200. tmp0 * src5 +
  7201. tmp3 * src6 +
  7202. tmp4 * src7 -
  7203. (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  7204. const dst1 =
  7205. tmp1 * src4 +
  7206. tmp6 * src6 +
  7207. tmp9 * src7 -
  7208. (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  7209. const dst2 =
  7210. tmp2 * src4 +
  7211. tmp7 * src5 +
  7212. tmp10 * src7 -
  7213. (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  7214. const dst3 =
  7215. tmp5 * src4 +
  7216. tmp8 * src5 +
  7217. tmp11 * src6 -
  7218. (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  7219. const dst4 =
  7220. tmp1 * src1 +
  7221. tmp2 * src2 +
  7222. tmp5 * src3 -
  7223. (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  7224. const dst5 =
  7225. tmp0 * src0 +
  7226. tmp7 * src2 +
  7227. tmp8 * src3 -
  7228. (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  7229. const dst6 =
  7230. tmp3 * src0 +
  7231. tmp6 * src1 +
  7232. tmp11 * src3 -
  7233. (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  7234. const dst7 =
  7235. tmp4 * src0 +
  7236. tmp9 * src1 +
  7237. tmp10 * src2 -
  7238. (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  7239. // calculate pairs for second 8 elements (cofactors)
  7240. tmp0 = src2 * src7;
  7241. tmp1 = src3 * src6;
  7242. tmp2 = src1 * src7;
  7243. tmp3 = src3 * src5;
  7244. tmp4 = src1 * src6;
  7245. tmp5 = src2 * src5;
  7246. tmp6 = src0 * src7;
  7247. tmp7 = src3 * src4;
  7248. tmp8 = src0 * src6;
  7249. tmp9 = src2 * src4;
  7250. tmp10 = src0 * src5;
  7251. tmp11 = src1 * src4;
  7252. // calculate second 8 elements (cofactors)
  7253. const dst8 =
  7254. tmp0 * src13 +
  7255. tmp3 * src14 +
  7256. tmp4 * src15 -
  7257. (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  7258. const dst9 =
  7259. tmp1 * src12 +
  7260. tmp6 * src14 +
  7261. tmp9 * src15 -
  7262. (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  7263. const dst10 =
  7264. tmp2 * src12 +
  7265. tmp7 * src13 +
  7266. tmp10 * src15 -
  7267. (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  7268. const dst11 =
  7269. tmp5 * src12 +
  7270. tmp8 * src13 +
  7271. tmp11 * src14 -
  7272. (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  7273. const dst12 =
  7274. tmp2 * src10 +
  7275. tmp5 * src11 +
  7276. tmp1 * src9 -
  7277. (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  7278. const dst13 =
  7279. tmp8 * src11 +
  7280. tmp0 * src8 +
  7281. tmp7 * src10 -
  7282. (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  7283. const dst14 =
  7284. tmp6 * src9 +
  7285. tmp11 * src11 +
  7286. tmp3 * src8 -
  7287. (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  7288. const dst15 =
  7289. tmp10 * src10 +
  7290. tmp4 * src8 +
  7291. tmp9 * src9 -
  7292. (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  7293. // calculate determinant
  7294. let det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  7295. if (Math.abs(det) < ComponentDatatype.CesiumMath.EPSILON21) {
  7296. // Special case for a zero scale matrix that can occur, for example,
  7297. // when a model's node has a [0, 0, 0] scale.
  7298. if (
  7299. Matrix3.equalsEpsilon(
  7300. Matrix4.getMatrix3(matrix, scratchInverseRotation),
  7301. scratchMatrix3Zero,
  7302. ComponentDatatype.CesiumMath.EPSILON7
  7303. ) &&
  7304. Cartesian4.equals(
  7305. Matrix4.getRow(matrix, 3, scratchBottomRow),
  7306. scratchExpectedBottomRow
  7307. )
  7308. ) {
  7309. result[0] = 0.0;
  7310. result[1] = 0.0;
  7311. result[2] = 0.0;
  7312. result[3] = 0.0;
  7313. result[4] = 0.0;
  7314. result[5] = 0.0;
  7315. result[6] = 0.0;
  7316. result[7] = 0.0;
  7317. result[8] = 0.0;
  7318. result[9] = 0.0;
  7319. result[10] = 0.0;
  7320. result[11] = 0.0;
  7321. result[12] = -matrix[12];
  7322. result[13] = -matrix[13];
  7323. result[14] = -matrix[14];
  7324. result[15] = 1.0;
  7325. return result;
  7326. }
  7327. throw new RuntimeError.RuntimeError(
  7328. "matrix is not invertible because its determinate is zero."
  7329. );
  7330. }
  7331. // calculate matrix inverse
  7332. det = 1.0 / det;
  7333. result[0] = dst0 * det;
  7334. result[1] = dst1 * det;
  7335. result[2] = dst2 * det;
  7336. result[3] = dst3 * det;
  7337. result[4] = dst4 * det;
  7338. result[5] = dst5 * det;
  7339. result[6] = dst6 * det;
  7340. result[7] = dst7 * det;
  7341. result[8] = dst8 * det;
  7342. result[9] = dst9 * det;
  7343. result[10] = dst10 * det;
  7344. result[11] = dst11 * det;
  7345. result[12] = dst12 * det;
  7346. result[13] = dst13 * det;
  7347. result[14] = dst14 * det;
  7348. result[15] = dst15 * det;
  7349. return result;
  7350. };
  7351. /**
  7352. * Computes the inverse of the provided matrix assuming it is a proper rigid matrix,
  7353. * where the upper left 3x3 elements are a rotation matrix,
  7354. * and the upper three elements in the fourth column are the translation.
  7355. * The bottom row is assumed to be [0, 0, 0, 1].
  7356. * The matrix is not verified to be in the proper form.
  7357. * This method is faster than computing the inverse for a general 4x4
  7358. * matrix using {@link Matrix4.inverse}.
  7359. *
  7360. * @param {Matrix4} matrix The matrix to invert.
  7361. * @param {Matrix4} result The object onto which to store the result.
  7362. * @returns {Matrix4} The modified result parameter.
  7363. */
  7364. Matrix4.inverseTransformation = function (matrix, result) {
  7365. //>>includeStart('debug', pragmas.debug);
  7366. RuntimeError.Check.typeOf.object("matrix", matrix);
  7367. RuntimeError.Check.typeOf.object("result", result);
  7368. //>>includeEnd('debug');
  7369. //This function is an optimized version of the below 4 lines.
  7370. //const rT = Matrix3.transpose(Matrix4.getMatrix3(matrix));
  7371. //const rTN = Matrix3.negate(rT);
  7372. //const rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  7373. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  7374. const matrix0 = matrix[0];
  7375. const matrix1 = matrix[1];
  7376. const matrix2 = matrix[2];
  7377. const matrix4 = matrix[4];
  7378. const matrix5 = matrix[5];
  7379. const matrix6 = matrix[6];
  7380. const matrix8 = matrix[8];
  7381. const matrix9 = matrix[9];
  7382. const matrix10 = matrix[10];
  7383. const vX = matrix[12];
  7384. const vY = matrix[13];
  7385. const vZ = matrix[14];
  7386. const x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  7387. const y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  7388. const z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  7389. result[0] = matrix0;
  7390. result[1] = matrix4;
  7391. result[2] = matrix8;
  7392. result[3] = 0.0;
  7393. result[4] = matrix1;
  7394. result[5] = matrix5;
  7395. result[6] = matrix9;
  7396. result[7] = 0.0;
  7397. result[8] = matrix2;
  7398. result[9] = matrix6;
  7399. result[10] = matrix10;
  7400. result[11] = 0.0;
  7401. result[12] = x;
  7402. result[13] = y;
  7403. result[14] = z;
  7404. result[15] = 1.0;
  7405. return result;
  7406. };
  7407. const scratchTransposeMatrix = new Matrix4();
  7408. /**
  7409. * Computes the inverse transpose of a matrix.
  7410. *
  7411. * @param {Matrix4} matrix The matrix to transpose and invert.
  7412. * @param {Matrix4} result The object onto which to store the result.
  7413. * @returns {Matrix4} The modified result parameter.
  7414. */
  7415. Matrix4.inverseTranspose = function (matrix, result) {
  7416. //>>includeStart('debug', pragmas.debug);
  7417. RuntimeError.Check.typeOf.object("matrix", matrix);
  7418. RuntimeError.Check.typeOf.object("result", result);
  7419. //>>includeEnd('debug');
  7420. return Matrix4.inverse(
  7421. Matrix4.transpose(matrix, scratchTransposeMatrix),
  7422. result
  7423. );
  7424. };
  7425. /**
  7426. * An immutable Matrix4 instance initialized to the identity matrix.
  7427. *
  7428. * @type {Matrix4}
  7429. * @constant
  7430. */
  7431. Matrix4.IDENTITY = Object.freeze(
  7432. new Matrix4(
  7433. 1.0,
  7434. 0.0,
  7435. 0.0,
  7436. 0.0,
  7437. 0.0,
  7438. 1.0,
  7439. 0.0,
  7440. 0.0,
  7441. 0.0,
  7442. 0.0,
  7443. 1.0,
  7444. 0.0,
  7445. 0.0,
  7446. 0.0,
  7447. 0.0,
  7448. 1.0
  7449. )
  7450. );
  7451. /**
  7452. * An immutable Matrix4 instance initialized to the zero matrix.
  7453. *
  7454. * @type {Matrix4}
  7455. * @constant
  7456. */
  7457. Matrix4.ZERO = Object.freeze(
  7458. new Matrix4(
  7459. 0.0,
  7460. 0.0,
  7461. 0.0,
  7462. 0.0,
  7463. 0.0,
  7464. 0.0,
  7465. 0.0,
  7466. 0.0,
  7467. 0.0,
  7468. 0.0,
  7469. 0.0,
  7470. 0.0,
  7471. 0.0,
  7472. 0.0,
  7473. 0.0,
  7474. 0.0
  7475. )
  7476. );
  7477. /**
  7478. * The index into Matrix4 for column 0, row 0.
  7479. *
  7480. * @type {Number}
  7481. * @constant
  7482. */
  7483. Matrix4.COLUMN0ROW0 = 0;
  7484. /**
  7485. * The index into Matrix4 for column 0, row 1.
  7486. *
  7487. * @type {Number}
  7488. * @constant
  7489. */
  7490. Matrix4.COLUMN0ROW1 = 1;
  7491. /**
  7492. * The index into Matrix4 for column 0, row 2.
  7493. *
  7494. * @type {Number}
  7495. * @constant
  7496. */
  7497. Matrix4.COLUMN0ROW2 = 2;
  7498. /**
  7499. * The index into Matrix4 for column 0, row 3.
  7500. *
  7501. * @type {Number}
  7502. * @constant
  7503. */
  7504. Matrix4.COLUMN0ROW3 = 3;
  7505. /**
  7506. * The index into Matrix4 for column 1, row 0.
  7507. *
  7508. * @type {Number}
  7509. * @constant
  7510. */
  7511. Matrix4.COLUMN1ROW0 = 4;
  7512. /**
  7513. * The index into Matrix4 for column 1, row 1.
  7514. *
  7515. * @type {Number}
  7516. * @constant
  7517. */
  7518. Matrix4.COLUMN1ROW1 = 5;
  7519. /**
  7520. * The index into Matrix4 for column 1, row 2.
  7521. *
  7522. * @type {Number}
  7523. * @constant
  7524. */
  7525. Matrix4.COLUMN1ROW2 = 6;
  7526. /**
  7527. * The index into Matrix4 for column 1, row 3.
  7528. *
  7529. * @type {Number}
  7530. * @constant
  7531. */
  7532. Matrix4.COLUMN1ROW3 = 7;
  7533. /**
  7534. * The index into Matrix4 for column 2, row 0.
  7535. *
  7536. * @type {Number}
  7537. * @constant
  7538. */
  7539. Matrix4.COLUMN2ROW0 = 8;
  7540. /**
  7541. * The index into Matrix4 for column 2, row 1.
  7542. *
  7543. * @type {Number}
  7544. * @constant
  7545. */
  7546. Matrix4.COLUMN2ROW1 = 9;
  7547. /**
  7548. * The index into Matrix4 for column 2, row 2.
  7549. *
  7550. * @type {Number}
  7551. * @constant
  7552. */
  7553. Matrix4.COLUMN2ROW2 = 10;
  7554. /**
  7555. * The index into Matrix4 for column 2, row 3.
  7556. *
  7557. * @type {Number}
  7558. * @constant
  7559. */
  7560. Matrix4.COLUMN2ROW3 = 11;
  7561. /**
  7562. * The index into Matrix4 for column 3, row 0.
  7563. *
  7564. * @type {Number}
  7565. * @constant
  7566. */
  7567. Matrix4.COLUMN3ROW0 = 12;
  7568. /**
  7569. * The index into Matrix4 for column 3, row 1.
  7570. *
  7571. * @type {Number}
  7572. * @constant
  7573. */
  7574. Matrix4.COLUMN3ROW1 = 13;
  7575. /**
  7576. * The index into Matrix4 for column 3, row 2.
  7577. *
  7578. * @type {Number}
  7579. * @constant
  7580. */
  7581. Matrix4.COLUMN3ROW2 = 14;
  7582. /**
  7583. * The index into Matrix4 for column 3, row 3.
  7584. *
  7585. * @type {Number}
  7586. * @constant
  7587. */
  7588. Matrix4.COLUMN3ROW3 = 15;
  7589. Object.defineProperties(Matrix4.prototype, {
  7590. /**
  7591. * Gets the number of items in the collection.
  7592. * @memberof Matrix4.prototype
  7593. *
  7594. * @type {Number}
  7595. */
  7596. length: {
  7597. get: function () {
  7598. return Matrix4.packedLength;
  7599. },
  7600. },
  7601. });
  7602. /**
  7603. * Duplicates the provided Matrix4 instance.
  7604. *
  7605. * @param {Matrix4} [result] The object onto which to store the result.
  7606. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  7607. */
  7608. Matrix4.prototype.clone = function (result) {
  7609. return Matrix4.clone(this, result);
  7610. };
  7611. /**
  7612. * Compares this matrix to the provided matrix componentwise and returns
  7613. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7614. *
  7615. * @param {Matrix4} [right] The right hand side matrix.
  7616. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  7617. */
  7618. Matrix4.prototype.equals = function (right) {
  7619. return Matrix4.equals(this, right);
  7620. };
  7621. /**
  7622. * @private
  7623. */
  7624. Matrix4.equalsArray = function (matrix, array, offset) {
  7625. return (
  7626. matrix[0] === array[offset] &&
  7627. matrix[1] === array[offset + 1] &&
  7628. matrix[2] === array[offset + 2] &&
  7629. matrix[3] === array[offset + 3] &&
  7630. matrix[4] === array[offset + 4] &&
  7631. matrix[5] === array[offset + 5] &&
  7632. matrix[6] === array[offset + 6] &&
  7633. matrix[7] === array[offset + 7] &&
  7634. matrix[8] === array[offset + 8] &&
  7635. matrix[9] === array[offset + 9] &&
  7636. matrix[10] === array[offset + 10] &&
  7637. matrix[11] === array[offset + 11] &&
  7638. matrix[12] === array[offset + 12] &&
  7639. matrix[13] === array[offset + 13] &&
  7640. matrix[14] === array[offset + 14] &&
  7641. matrix[15] === array[offset + 15]
  7642. );
  7643. };
  7644. /**
  7645. * Compares this matrix to the provided matrix componentwise and returns
  7646. * <code>true</code> if they are within the provided epsilon,
  7647. * <code>false</code> otherwise.
  7648. *
  7649. * @param {Matrix4} [right] The right hand side matrix.
  7650. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7651. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  7652. */
  7653. Matrix4.prototype.equalsEpsilon = function (right, epsilon) {
  7654. return Matrix4.equalsEpsilon(this, right, epsilon);
  7655. };
  7656. /**
  7657. * Computes a string representing this Matrix with each row being
  7658. * on a separate line and in the format '(column0, column1, column2, column3)'.
  7659. *
  7660. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  7661. */
  7662. Matrix4.prototype.toString = function () {
  7663. return (
  7664. `(${this[0]}, ${this[4]}, ${this[8]}, ${this[12]})\n` +
  7665. `(${this[1]}, ${this[5]}, ${this[9]}, ${this[13]})\n` +
  7666. `(${this[2]}, ${this[6]}, ${this[10]}, ${this[14]})\n` +
  7667. `(${this[3]}, ${this[7]}, ${this[11]}, ${this[15]})`
  7668. );
  7669. };
  7670. /**
  7671. * A two dimensional region specified as longitude and latitude coordinates.
  7672. *
  7673. * @alias Rectangle
  7674. * @constructor
  7675. *
  7676. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  7677. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  7678. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  7679. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  7680. *
  7681. * @see Packable
  7682. */
  7683. function Rectangle(west, south, east, north) {
  7684. /**
  7685. * The westernmost longitude in radians in the range [-Pi, Pi].
  7686. *
  7687. * @type {Number}
  7688. * @default 0.0
  7689. */
  7690. this.west = defaultValue.defaultValue(west, 0.0);
  7691. /**
  7692. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  7693. *
  7694. * @type {Number}
  7695. * @default 0.0
  7696. */
  7697. this.south = defaultValue.defaultValue(south, 0.0);
  7698. /**
  7699. * The easternmost longitude in radians in the range [-Pi, Pi].
  7700. *
  7701. * @type {Number}
  7702. * @default 0.0
  7703. */
  7704. this.east = defaultValue.defaultValue(east, 0.0);
  7705. /**
  7706. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  7707. *
  7708. * @type {Number}
  7709. * @default 0.0
  7710. */
  7711. this.north = defaultValue.defaultValue(north, 0.0);
  7712. }
  7713. Object.defineProperties(Rectangle.prototype, {
  7714. /**
  7715. * Gets the width of the rectangle in radians.
  7716. * @memberof Rectangle.prototype
  7717. * @type {Number}
  7718. * @readonly
  7719. */
  7720. width: {
  7721. get: function () {
  7722. return Rectangle.computeWidth(this);
  7723. },
  7724. },
  7725. /**
  7726. * Gets the height of the rectangle in radians.
  7727. * @memberof Rectangle.prototype
  7728. * @type {Number}
  7729. * @readonly
  7730. */
  7731. height: {
  7732. get: function () {
  7733. return Rectangle.computeHeight(this);
  7734. },
  7735. },
  7736. });
  7737. /**
  7738. * The number of elements used to pack the object into an array.
  7739. * @type {Number}
  7740. */
  7741. Rectangle.packedLength = 4;
  7742. /**
  7743. * Stores the provided instance into the provided array.
  7744. *
  7745. * @param {Rectangle} value The value to pack.
  7746. * @param {Number[]} array The array to pack into.
  7747. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  7748. *
  7749. * @returns {Number[]} The array that was packed into
  7750. */
  7751. Rectangle.pack = function (value, array, startingIndex) {
  7752. //>>includeStart('debug', pragmas.debug);
  7753. RuntimeError.Check.typeOf.object("value", value);
  7754. RuntimeError.Check.defined("array", array);
  7755. //>>includeEnd('debug');
  7756. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  7757. array[startingIndex++] = value.west;
  7758. array[startingIndex++] = value.south;
  7759. array[startingIndex++] = value.east;
  7760. array[startingIndex] = value.north;
  7761. return array;
  7762. };
  7763. /**
  7764. * Retrieves an instance from a packed array.
  7765. *
  7766. * @param {Number[]} array The packed array.
  7767. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  7768. * @param {Rectangle} [result] The object into which to store the result.
  7769. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  7770. */
  7771. Rectangle.unpack = function (array, startingIndex, result) {
  7772. //>>includeStart('debug', pragmas.debug);
  7773. RuntimeError.Check.defined("array", array);
  7774. //>>includeEnd('debug');
  7775. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  7776. if (!defaultValue.defined(result)) {
  7777. result = new Rectangle();
  7778. }
  7779. result.west = array[startingIndex++];
  7780. result.south = array[startingIndex++];
  7781. result.east = array[startingIndex++];
  7782. result.north = array[startingIndex];
  7783. return result;
  7784. };
  7785. /**
  7786. * Computes the width of a rectangle in radians.
  7787. * @param {Rectangle} rectangle The rectangle to compute the width of.
  7788. * @returns {Number} The width.
  7789. */
  7790. Rectangle.computeWidth = function (rectangle) {
  7791. //>>includeStart('debug', pragmas.debug);
  7792. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  7793. //>>includeEnd('debug');
  7794. let east = rectangle.east;
  7795. const west = rectangle.west;
  7796. if (east < west) {
  7797. east += ComponentDatatype.CesiumMath.TWO_PI;
  7798. }
  7799. return east - west;
  7800. };
  7801. /**
  7802. * Computes the height of a rectangle in radians.
  7803. * @param {Rectangle} rectangle The rectangle to compute the height of.
  7804. * @returns {Number} The height.
  7805. */
  7806. Rectangle.computeHeight = function (rectangle) {
  7807. //>>includeStart('debug', pragmas.debug);
  7808. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  7809. //>>includeEnd('debug');
  7810. return rectangle.north - rectangle.south;
  7811. };
  7812. /**
  7813. * Creates a rectangle given the boundary longitude and latitude in degrees.
  7814. *
  7815. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  7816. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  7817. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  7818. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  7819. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7820. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7821. *
  7822. * @example
  7823. * const rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  7824. */
  7825. Rectangle.fromDegrees = function (west, south, east, north, result) {
  7826. west = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(west, 0.0));
  7827. south = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(south, 0.0));
  7828. east = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(east, 0.0));
  7829. north = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(north, 0.0));
  7830. if (!defaultValue.defined(result)) {
  7831. return new Rectangle(west, south, east, north);
  7832. }
  7833. result.west = west;
  7834. result.south = south;
  7835. result.east = east;
  7836. result.north = north;
  7837. return result;
  7838. };
  7839. /**
  7840. * Creates a rectangle given the boundary longitude and latitude in radians.
  7841. *
  7842. * @param {Number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
  7843. * @param {Number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  7844. * @param {Number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
  7845. * @param {Number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  7846. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7847. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7848. *
  7849. * @example
  7850. * const rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
  7851. */
  7852. Rectangle.fromRadians = function (west, south, east, north, result) {
  7853. if (!defaultValue.defined(result)) {
  7854. return new Rectangle(west, south, east, north);
  7855. }
  7856. result.west = defaultValue.defaultValue(west, 0.0);
  7857. result.south = defaultValue.defaultValue(south, 0.0);
  7858. result.east = defaultValue.defaultValue(east, 0.0);
  7859. result.north = defaultValue.defaultValue(north, 0.0);
  7860. return result;
  7861. };
  7862. /**
  7863. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  7864. *
  7865. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  7866. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7867. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7868. */
  7869. Rectangle.fromCartographicArray = function (cartographics, result) {
  7870. //>>includeStart('debug', pragmas.debug);
  7871. RuntimeError.Check.defined("cartographics", cartographics);
  7872. //>>includeEnd('debug');
  7873. let west = Number.MAX_VALUE;
  7874. let east = -Number.MAX_VALUE;
  7875. let westOverIDL = Number.MAX_VALUE;
  7876. let eastOverIDL = -Number.MAX_VALUE;
  7877. let south = Number.MAX_VALUE;
  7878. let north = -Number.MAX_VALUE;
  7879. for (let i = 0, len = cartographics.length; i < len; i++) {
  7880. const position = cartographics[i];
  7881. west = Math.min(west, position.longitude);
  7882. east = Math.max(east, position.longitude);
  7883. south = Math.min(south, position.latitude);
  7884. north = Math.max(north, position.latitude);
  7885. const lonAdjusted =
  7886. position.longitude >= 0
  7887. ? position.longitude
  7888. : position.longitude + ComponentDatatype.CesiumMath.TWO_PI;
  7889. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  7890. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  7891. }
  7892. if (east - west > eastOverIDL - westOverIDL) {
  7893. west = westOverIDL;
  7894. east = eastOverIDL;
  7895. if (east > ComponentDatatype.CesiumMath.PI) {
  7896. east = east - ComponentDatatype.CesiumMath.TWO_PI;
  7897. }
  7898. if (west > ComponentDatatype.CesiumMath.PI) {
  7899. west = west - ComponentDatatype.CesiumMath.TWO_PI;
  7900. }
  7901. }
  7902. if (!defaultValue.defined(result)) {
  7903. return new Rectangle(west, south, east, north);
  7904. }
  7905. result.west = west;
  7906. result.south = south;
  7907. result.east = east;
  7908. result.north = north;
  7909. return result;
  7910. };
  7911. /**
  7912. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  7913. *
  7914. * @param {Cartesian3[]} cartesians The list of Cartesian instances.
  7915. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  7916. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7917. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7918. */
  7919. Rectangle.fromCartesianArray = function (cartesians, ellipsoid, result) {
  7920. //>>includeStart('debug', pragmas.debug);
  7921. RuntimeError.Check.defined("cartesians", cartesians);
  7922. //>>includeEnd('debug');
  7923. ellipsoid = defaultValue.defaultValue(ellipsoid, Ellipsoid.WGS84);
  7924. let west = Number.MAX_VALUE;
  7925. let east = -Number.MAX_VALUE;
  7926. let westOverIDL = Number.MAX_VALUE;
  7927. let eastOverIDL = -Number.MAX_VALUE;
  7928. let south = Number.MAX_VALUE;
  7929. let north = -Number.MAX_VALUE;
  7930. for (let i = 0, len = cartesians.length; i < len; i++) {
  7931. const position = ellipsoid.cartesianToCartographic(cartesians[i]);
  7932. west = Math.min(west, position.longitude);
  7933. east = Math.max(east, position.longitude);
  7934. south = Math.min(south, position.latitude);
  7935. north = Math.max(north, position.latitude);
  7936. const lonAdjusted =
  7937. position.longitude >= 0
  7938. ? position.longitude
  7939. : position.longitude + ComponentDatatype.CesiumMath.TWO_PI;
  7940. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  7941. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  7942. }
  7943. if (east - west > eastOverIDL - westOverIDL) {
  7944. west = westOverIDL;
  7945. east = eastOverIDL;
  7946. if (east > ComponentDatatype.CesiumMath.PI) {
  7947. east = east - ComponentDatatype.CesiumMath.TWO_PI;
  7948. }
  7949. if (west > ComponentDatatype.CesiumMath.PI) {
  7950. west = west - ComponentDatatype.CesiumMath.TWO_PI;
  7951. }
  7952. }
  7953. if (!defaultValue.defined(result)) {
  7954. return new Rectangle(west, south, east, north);
  7955. }
  7956. result.west = west;
  7957. result.south = south;
  7958. result.east = east;
  7959. result.north = north;
  7960. return result;
  7961. };
  7962. /**
  7963. * Duplicates a Rectangle.
  7964. *
  7965. * @param {Rectangle} rectangle The rectangle to clone.
  7966. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7967. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  7968. */
  7969. Rectangle.clone = function (rectangle, result) {
  7970. if (!defaultValue.defined(rectangle)) {
  7971. return undefined;
  7972. }
  7973. if (!defaultValue.defined(result)) {
  7974. return new Rectangle(
  7975. rectangle.west,
  7976. rectangle.south,
  7977. rectangle.east,
  7978. rectangle.north
  7979. );
  7980. }
  7981. result.west = rectangle.west;
  7982. result.south = rectangle.south;
  7983. result.east = rectangle.east;
  7984. result.north = rectangle.north;
  7985. return result;
  7986. };
  7987. /**
  7988. * Compares the provided Rectangles componentwise and returns
  7989. * <code>true</code> if they pass an absolute or relative tolerance test,
  7990. * <code>false</code> otherwise.
  7991. *
  7992. * @param {Rectangle} [left] The first Rectangle.
  7993. * @param {Rectangle} [right] The second Rectangle.
  7994. * @param {Number} [absoluteEpsilon=0] The absolute epsilon tolerance to use for equality testing.
  7995. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  7996. */
  7997. Rectangle.equalsEpsilon = function (left, right, absoluteEpsilon) {
  7998. absoluteEpsilon = defaultValue.defaultValue(absoluteEpsilon, 0);
  7999. return (
  8000. left === right ||
  8001. (defaultValue.defined(left) &&
  8002. defaultValue.defined(right) &&
  8003. Math.abs(left.west - right.west) <= absoluteEpsilon &&
  8004. Math.abs(left.south - right.south) <= absoluteEpsilon &&
  8005. Math.abs(left.east - right.east) <= absoluteEpsilon &&
  8006. Math.abs(left.north - right.north) <= absoluteEpsilon)
  8007. );
  8008. };
  8009. /**
  8010. * Duplicates this Rectangle.
  8011. *
  8012. * @param {Rectangle} [result] The object onto which to store the result.
  8013. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8014. */
  8015. Rectangle.prototype.clone = function (result) {
  8016. return Rectangle.clone(this, result);
  8017. };
  8018. /**
  8019. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8020. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8021. *
  8022. * @param {Rectangle} [other] The Rectangle to compare.
  8023. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  8024. */
  8025. Rectangle.prototype.equals = function (other) {
  8026. return Rectangle.equals(this, other);
  8027. };
  8028. /**
  8029. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  8030. * <code>false</code> otherwise.
  8031. *
  8032. * @param {Rectangle} [left] The first Rectangle.
  8033. * @param {Rectangle} [right] The second Rectangle.
  8034. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  8035. */
  8036. Rectangle.equals = function (left, right) {
  8037. return (
  8038. left === right ||
  8039. (defaultValue.defined(left) &&
  8040. defaultValue.defined(right) &&
  8041. left.west === right.west &&
  8042. left.south === right.south &&
  8043. left.east === right.east &&
  8044. left.north === right.north)
  8045. );
  8046. };
  8047. /**
  8048. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8049. * <code>true</code> if they are within the provided epsilon,
  8050. * <code>false</code> otherwise.
  8051. *
  8052. * @param {Rectangle} [other] The Rectangle to compare.
  8053. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  8054. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  8055. */
  8056. Rectangle.prototype.equalsEpsilon = function (other, epsilon) {
  8057. return Rectangle.equalsEpsilon(this, other, epsilon);
  8058. };
  8059. /**
  8060. * Checks a Rectangle's properties and throws if they are not in valid ranges.
  8061. *
  8062. * @param {Rectangle} rectangle The rectangle to validate
  8063. *
  8064. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8065. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8066. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8067. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8068. */
  8069. Rectangle.validate = function (rectangle) {
  8070. //>>includeStart('debug', pragmas.debug);
  8071. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8072. const north = rectangle.north;
  8073. RuntimeError.Check.typeOf.number.greaterThanOrEquals(
  8074. "north",
  8075. north,
  8076. -ComponentDatatype.CesiumMath.PI_OVER_TWO
  8077. );
  8078. RuntimeError.Check.typeOf.number.lessThanOrEquals("north", north, ComponentDatatype.CesiumMath.PI_OVER_TWO);
  8079. const south = rectangle.south;
  8080. RuntimeError.Check.typeOf.number.greaterThanOrEquals(
  8081. "south",
  8082. south,
  8083. -ComponentDatatype.CesiumMath.PI_OVER_TWO
  8084. );
  8085. RuntimeError.Check.typeOf.number.lessThanOrEquals("south", south, ComponentDatatype.CesiumMath.PI_OVER_TWO);
  8086. const west = rectangle.west;
  8087. RuntimeError.Check.typeOf.number.greaterThanOrEquals("west", west, -Math.PI);
  8088. RuntimeError.Check.typeOf.number.lessThanOrEquals("west", west, Math.PI);
  8089. const east = rectangle.east;
  8090. RuntimeError.Check.typeOf.number.greaterThanOrEquals("east", east, -Math.PI);
  8091. RuntimeError.Check.typeOf.number.lessThanOrEquals("east", east, Math.PI);
  8092. //>>includeEnd('debug');
  8093. };
  8094. /**
  8095. * Computes the southwest corner of a rectangle.
  8096. *
  8097. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8098. * @param {Cartographic} [result] The object onto which to store the result.
  8099. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8100. */
  8101. Rectangle.southwest = function (rectangle, result) {
  8102. //>>includeStart('debug', pragmas.debug);
  8103. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8104. //>>includeEnd('debug');
  8105. if (!defaultValue.defined(result)) {
  8106. return new Cartographic(rectangle.west, rectangle.south);
  8107. }
  8108. result.longitude = rectangle.west;
  8109. result.latitude = rectangle.south;
  8110. result.height = 0.0;
  8111. return result;
  8112. };
  8113. /**
  8114. * Computes the northwest corner of a rectangle.
  8115. *
  8116. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8117. * @param {Cartographic} [result] The object onto which to store the result.
  8118. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8119. */
  8120. Rectangle.northwest = function (rectangle, result) {
  8121. //>>includeStart('debug', pragmas.debug);
  8122. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8123. //>>includeEnd('debug');
  8124. if (!defaultValue.defined(result)) {
  8125. return new Cartographic(rectangle.west, rectangle.north);
  8126. }
  8127. result.longitude = rectangle.west;
  8128. result.latitude = rectangle.north;
  8129. result.height = 0.0;
  8130. return result;
  8131. };
  8132. /**
  8133. * Computes the northeast corner of a rectangle.
  8134. *
  8135. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8136. * @param {Cartographic} [result] The object onto which to store the result.
  8137. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8138. */
  8139. Rectangle.northeast = function (rectangle, result) {
  8140. //>>includeStart('debug', pragmas.debug);
  8141. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8142. //>>includeEnd('debug');
  8143. if (!defaultValue.defined(result)) {
  8144. return new Cartographic(rectangle.east, rectangle.north);
  8145. }
  8146. result.longitude = rectangle.east;
  8147. result.latitude = rectangle.north;
  8148. result.height = 0.0;
  8149. return result;
  8150. };
  8151. /**
  8152. * Computes the southeast corner of a rectangle.
  8153. *
  8154. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8155. * @param {Cartographic} [result] The object onto which to store the result.
  8156. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8157. */
  8158. Rectangle.southeast = function (rectangle, result) {
  8159. //>>includeStart('debug', pragmas.debug);
  8160. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8161. //>>includeEnd('debug');
  8162. if (!defaultValue.defined(result)) {
  8163. return new Cartographic(rectangle.east, rectangle.south);
  8164. }
  8165. result.longitude = rectangle.east;
  8166. result.latitude = rectangle.south;
  8167. result.height = 0.0;
  8168. return result;
  8169. };
  8170. /**
  8171. * Computes the center of a rectangle.
  8172. *
  8173. * @param {Rectangle} rectangle The rectangle for which to find the center
  8174. * @param {Cartographic} [result] The object onto which to store the result.
  8175. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8176. */
  8177. Rectangle.center = function (rectangle, result) {
  8178. //>>includeStart('debug', pragmas.debug);
  8179. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8180. //>>includeEnd('debug');
  8181. let east = rectangle.east;
  8182. const west = rectangle.west;
  8183. if (east < west) {
  8184. east += ComponentDatatype.CesiumMath.TWO_PI;
  8185. }
  8186. const longitude = ComponentDatatype.CesiumMath.negativePiToPi((west + east) * 0.5);
  8187. const latitude = (rectangle.south + rectangle.north) * 0.5;
  8188. if (!defaultValue.defined(result)) {
  8189. return new Cartographic(longitude, latitude);
  8190. }
  8191. result.longitude = longitude;
  8192. result.latitude = latitude;
  8193. result.height = 0.0;
  8194. return result;
  8195. };
  8196. /**
  8197. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  8198. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  8199. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  8200. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  8201. * coordinates, see {@link Rectangle.simpleIntersection}.
  8202. *
  8203. * @param {Rectangle} rectangle On rectangle to find an intersection
  8204. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8205. * @param {Rectangle} [result] The object onto which to store the result.
  8206. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8207. */
  8208. Rectangle.intersection = function (rectangle, otherRectangle, result) {
  8209. //>>includeStart('debug', pragmas.debug);
  8210. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8211. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8212. //>>includeEnd('debug');
  8213. let rectangleEast = rectangle.east;
  8214. let rectangleWest = rectangle.west;
  8215. let otherRectangleEast = otherRectangle.east;
  8216. let otherRectangleWest = otherRectangle.west;
  8217. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8218. rectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8219. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8220. otherRectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8221. }
  8222. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8223. otherRectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8224. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8225. rectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8226. }
  8227. const west = ComponentDatatype.CesiumMath.negativePiToPi(
  8228. Math.max(rectangleWest, otherRectangleWest)
  8229. );
  8230. const east = ComponentDatatype.CesiumMath.negativePiToPi(
  8231. Math.min(rectangleEast, otherRectangleEast)
  8232. );
  8233. if (
  8234. (rectangle.west < rectangle.east ||
  8235. otherRectangle.west < otherRectangle.east) &&
  8236. east <= west
  8237. ) {
  8238. return undefined;
  8239. }
  8240. const south = Math.max(rectangle.south, otherRectangle.south);
  8241. const north = Math.min(rectangle.north, otherRectangle.north);
  8242. if (south >= north) {
  8243. return undefined;
  8244. }
  8245. if (!defaultValue.defined(result)) {
  8246. return new Rectangle(west, south, east, north);
  8247. }
  8248. result.west = west;
  8249. result.south = south;
  8250. result.east = east;
  8251. result.north = north;
  8252. return result;
  8253. };
  8254. /**
  8255. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  8256. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  8257. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  8258. * and longitude (i.e. projected coordinates).
  8259. *
  8260. * @param {Rectangle} rectangle On rectangle to find an intersection
  8261. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8262. * @param {Rectangle} [result] The object onto which to store the result.
  8263. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8264. */
  8265. Rectangle.simpleIntersection = function (rectangle, otherRectangle, result) {
  8266. //>>includeStart('debug', pragmas.debug);
  8267. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8268. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8269. //>>includeEnd('debug');
  8270. const west = Math.max(rectangle.west, otherRectangle.west);
  8271. const south = Math.max(rectangle.south, otherRectangle.south);
  8272. const east = Math.min(rectangle.east, otherRectangle.east);
  8273. const north = Math.min(rectangle.north, otherRectangle.north);
  8274. if (south >= north || west >= east) {
  8275. return undefined;
  8276. }
  8277. if (!defaultValue.defined(result)) {
  8278. return new Rectangle(west, south, east, north);
  8279. }
  8280. result.west = west;
  8281. result.south = south;
  8282. result.east = east;
  8283. result.north = north;
  8284. return result;
  8285. };
  8286. /**
  8287. * Computes a rectangle that is the union of two rectangles.
  8288. *
  8289. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  8290. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  8291. * @param {Rectangle} [result] The object onto which to store the result.
  8292. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8293. */
  8294. Rectangle.union = function (rectangle, otherRectangle, result) {
  8295. //>>includeStart('debug', pragmas.debug);
  8296. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8297. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8298. //>>includeEnd('debug');
  8299. if (!defaultValue.defined(result)) {
  8300. result = new Rectangle();
  8301. }
  8302. let rectangleEast = rectangle.east;
  8303. let rectangleWest = rectangle.west;
  8304. let otherRectangleEast = otherRectangle.east;
  8305. let otherRectangleWest = otherRectangle.west;
  8306. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8307. rectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8308. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8309. otherRectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8310. }
  8311. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8312. otherRectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8313. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8314. rectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8315. }
  8316. const west = ComponentDatatype.CesiumMath.negativePiToPi(
  8317. Math.min(rectangleWest, otherRectangleWest)
  8318. );
  8319. const east = ComponentDatatype.CesiumMath.negativePiToPi(
  8320. Math.max(rectangleEast, otherRectangleEast)
  8321. );
  8322. result.west = west;
  8323. result.south = Math.min(rectangle.south, otherRectangle.south);
  8324. result.east = east;
  8325. result.north = Math.max(rectangle.north, otherRectangle.north);
  8326. return result;
  8327. };
  8328. /**
  8329. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  8330. *
  8331. * @param {Rectangle} rectangle A rectangle to expand.
  8332. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  8333. * @param {Rectangle} [result] The object onto which to store the result.
  8334. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  8335. */
  8336. Rectangle.expand = function (rectangle, cartographic, result) {
  8337. //>>includeStart('debug', pragmas.debug);
  8338. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8339. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  8340. //>>includeEnd('debug');
  8341. if (!defaultValue.defined(result)) {
  8342. result = new Rectangle();
  8343. }
  8344. result.west = Math.min(rectangle.west, cartographic.longitude);
  8345. result.south = Math.min(rectangle.south, cartographic.latitude);
  8346. result.east = Math.max(rectangle.east, cartographic.longitude);
  8347. result.north = Math.max(rectangle.north, cartographic.latitude);
  8348. return result;
  8349. };
  8350. /**
  8351. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  8352. *
  8353. * @param {Rectangle} rectangle The rectangle
  8354. * @param {Cartographic} cartographic The cartographic to test.
  8355. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  8356. */
  8357. Rectangle.contains = function (rectangle, cartographic) {
  8358. //>>includeStart('debug', pragmas.debug);
  8359. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8360. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  8361. //>>includeEnd('debug');
  8362. let longitude = cartographic.longitude;
  8363. const latitude = cartographic.latitude;
  8364. const west = rectangle.west;
  8365. let east = rectangle.east;
  8366. if (east < west) {
  8367. east += ComponentDatatype.CesiumMath.TWO_PI;
  8368. if (longitude < 0.0) {
  8369. longitude += ComponentDatatype.CesiumMath.TWO_PI;
  8370. }
  8371. }
  8372. return (
  8373. (longitude > west ||
  8374. ComponentDatatype.CesiumMath.equalsEpsilon(longitude, west, ComponentDatatype.CesiumMath.EPSILON14)) &&
  8375. (longitude < east ||
  8376. ComponentDatatype.CesiumMath.equalsEpsilon(longitude, east, ComponentDatatype.CesiumMath.EPSILON14)) &&
  8377. latitude >= rectangle.south &&
  8378. latitude <= rectangle.north
  8379. );
  8380. };
  8381. const subsampleLlaScratch = new Cartographic();
  8382. /**
  8383. * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
  8384. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  8385. * for rectangles that cover the poles or cross the equator.
  8386. *
  8387. * @param {Rectangle} rectangle The rectangle to subsample.
  8388. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  8389. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  8390. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  8391. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  8392. */
  8393. Rectangle.subsample = function (rectangle, ellipsoid, surfaceHeight, result) {
  8394. //>>includeStart('debug', pragmas.debug);
  8395. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8396. //>>includeEnd('debug');
  8397. ellipsoid = defaultValue.defaultValue(ellipsoid, Ellipsoid.WGS84);
  8398. surfaceHeight = defaultValue.defaultValue(surfaceHeight, 0.0);
  8399. if (!defaultValue.defined(result)) {
  8400. result = [];
  8401. }
  8402. let length = 0;
  8403. const north = rectangle.north;
  8404. const south = rectangle.south;
  8405. const east = rectangle.east;
  8406. const west = rectangle.west;
  8407. const lla = subsampleLlaScratch;
  8408. lla.height = surfaceHeight;
  8409. lla.longitude = west;
  8410. lla.latitude = north;
  8411. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8412. length++;
  8413. lla.longitude = east;
  8414. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8415. length++;
  8416. lla.latitude = south;
  8417. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8418. length++;
  8419. lla.longitude = west;
  8420. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8421. length++;
  8422. if (north < 0.0) {
  8423. lla.latitude = north;
  8424. } else if (south > 0.0) {
  8425. lla.latitude = south;
  8426. } else {
  8427. lla.latitude = 0.0;
  8428. }
  8429. for (let i = 1; i < 8; ++i) {
  8430. lla.longitude = -Math.PI + i * ComponentDatatype.CesiumMath.PI_OVER_TWO;
  8431. if (Rectangle.contains(rectangle, lla)) {
  8432. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8433. length++;
  8434. }
  8435. }
  8436. if (lla.latitude === 0.0) {
  8437. lla.longitude = west;
  8438. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8439. length++;
  8440. lla.longitude = east;
  8441. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8442. length++;
  8443. }
  8444. result.length = length;
  8445. return result;
  8446. };
  8447. /**
  8448. * Computes a subsection of a rectangle from normalized coordinates in the range [0.0, 1.0].
  8449. *
  8450. * @param {Rectangle} rectangle The rectangle to subsection.
  8451. * @param {Number} westLerp The west interpolation factor in the range [0.0, 1.0]. Must be less than or equal to eastLerp.
  8452. * @param {Number} southLerp The south interpolation factor in the range [0.0, 1.0]. Must be less than or equal to northLerp.
  8453. * @param {Number} eastLerp The east interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to westLerp.
  8454. * @param {Number} northLerp The north interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to southLerp.
  8455. * @param {Rectangle} [result] The object onto which to store the result.
  8456. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8457. */
  8458. Rectangle.subsection = function (
  8459. rectangle,
  8460. westLerp,
  8461. southLerp,
  8462. eastLerp,
  8463. northLerp,
  8464. result
  8465. ) {
  8466. //>>includeStart('debug', pragmas.debug);
  8467. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8468. RuntimeError.Check.typeOf.number.greaterThanOrEquals("westLerp", westLerp, 0.0);
  8469. RuntimeError.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, 1.0);
  8470. RuntimeError.Check.typeOf.number.greaterThanOrEquals("southLerp", southLerp, 0.0);
  8471. RuntimeError.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, 1.0);
  8472. RuntimeError.Check.typeOf.number.greaterThanOrEquals("eastLerp", eastLerp, 0.0);
  8473. RuntimeError.Check.typeOf.number.lessThanOrEquals("eastLerp", eastLerp, 1.0);
  8474. RuntimeError.Check.typeOf.number.greaterThanOrEquals("northLerp", northLerp, 0.0);
  8475. RuntimeError.Check.typeOf.number.lessThanOrEquals("northLerp", northLerp, 1.0);
  8476. RuntimeError.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, eastLerp);
  8477. RuntimeError.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, northLerp);
  8478. //>>includeEnd('debug');
  8479. if (!defaultValue.defined(result)) {
  8480. result = new Rectangle();
  8481. }
  8482. // This function doesn't use CesiumMath.lerp because it has floating point precision problems
  8483. // when the start and end values are the same but the t changes.
  8484. if (rectangle.west <= rectangle.east) {
  8485. const width = rectangle.east - rectangle.west;
  8486. result.west = rectangle.west + westLerp * width;
  8487. result.east = rectangle.west + eastLerp * width;
  8488. } else {
  8489. const width = ComponentDatatype.CesiumMath.TWO_PI + rectangle.east - rectangle.west;
  8490. result.west = ComponentDatatype.CesiumMath.negativePiToPi(rectangle.west + westLerp * width);
  8491. result.east = ComponentDatatype.CesiumMath.negativePiToPi(rectangle.west + eastLerp * width);
  8492. }
  8493. const height = rectangle.north - rectangle.south;
  8494. result.south = rectangle.south + southLerp * height;
  8495. result.north = rectangle.south + northLerp * height;
  8496. // Fix floating point precision problems when t = 1
  8497. if (westLerp === 1.0) {
  8498. result.west = rectangle.east;
  8499. }
  8500. if (eastLerp === 1.0) {
  8501. result.east = rectangle.east;
  8502. }
  8503. if (southLerp === 1.0) {
  8504. result.south = rectangle.north;
  8505. }
  8506. if (northLerp === 1.0) {
  8507. result.north = rectangle.north;
  8508. }
  8509. return result;
  8510. };
  8511. /**
  8512. * The largest possible rectangle.
  8513. *
  8514. * @type {Rectangle}
  8515. * @constant
  8516. */
  8517. Rectangle.MAX_VALUE = Object.freeze(
  8518. new Rectangle(
  8519. -Math.PI,
  8520. -ComponentDatatype.CesiumMath.PI_OVER_TWO,
  8521. Math.PI,
  8522. ComponentDatatype.CesiumMath.PI_OVER_TWO
  8523. )
  8524. );
  8525. /**
  8526. * A 2D Cartesian point.
  8527. * @alias Cartesian2
  8528. * @constructor
  8529. *
  8530. * @param {Number} [x=0.0] The X component.
  8531. * @param {Number} [y=0.0] The Y component.
  8532. *
  8533. * @see Cartesian3
  8534. * @see Cartesian4
  8535. * @see Packable
  8536. */
  8537. function Cartesian2(x, y) {
  8538. /**
  8539. * The X component.
  8540. * @type {Number}
  8541. * @default 0.0
  8542. */
  8543. this.x = defaultValue.defaultValue(x, 0.0);
  8544. /**
  8545. * The Y component.
  8546. * @type {Number}
  8547. * @default 0.0
  8548. */
  8549. this.y = defaultValue.defaultValue(y, 0.0);
  8550. }
  8551. /**
  8552. * Creates a Cartesian2 instance from x and y coordinates.
  8553. *
  8554. * @param {Number} x The x coordinate.
  8555. * @param {Number} y The y coordinate.
  8556. * @param {Cartesian2} [result] The object onto which to store the result.
  8557. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8558. */
  8559. Cartesian2.fromElements = function (x, y, result) {
  8560. if (!defaultValue.defined(result)) {
  8561. return new Cartesian2(x, y);
  8562. }
  8563. result.x = x;
  8564. result.y = y;
  8565. return result;
  8566. };
  8567. /**
  8568. * Duplicates a Cartesian2 instance.
  8569. *
  8570. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  8571. * @param {Cartesian2} [result] The object onto which to store the result.
  8572. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  8573. */
  8574. Cartesian2.clone = function (cartesian, result) {
  8575. if (!defaultValue.defined(cartesian)) {
  8576. return undefined;
  8577. }
  8578. if (!defaultValue.defined(result)) {
  8579. return new Cartesian2(cartesian.x, cartesian.y);
  8580. }
  8581. result.x = cartesian.x;
  8582. result.y = cartesian.y;
  8583. return result;
  8584. };
  8585. /**
  8586. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  8587. * x and y properties of the Cartesian3 and drops z.
  8588. * @function
  8589. *
  8590. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  8591. * @param {Cartesian2} [result] The object onto which to store the result.
  8592. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8593. */
  8594. Cartesian2.fromCartesian3 = Cartesian2.clone;
  8595. /**
  8596. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  8597. * x and y properties of the Cartesian4 and drops z and w.
  8598. * @function
  8599. *
  8600. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  8601. * @param {Cartesian2} [result] The object onto which to store the result.
  8602. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8603. */
  8604. Cartesian2.fromCartesian4 = Cartesian2.clone;
  8605. /**
  8606. * The number of elements used to pack the object into an array.
  8607. * @type {Number}
  8608. */
  8609. Cartesian2.packedLength = 2;
  8610. /**
  8611. * Stores the provided instance into the provided array.
  8612. *
  8613. * @param {Cartesian2} value The value to pack.
  8614. * @param {Number[]} array The array to pack into.
  8615. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  8616. *
  8617. * @returns {Number[]} The array that was packed into
  8618. */
  8619. Cartesian2.pack = function (value, array, startingIndex) {
  8620. //>>includeStart('debug', pragmas.debug);
  8621. RuntimeError.Check.typeOf.object("value", value);
  8622. RuntimeError.Check.defined("array", array);
  8623. //>>includeEnd('debug');
  8624. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  8625. array[startingIndex++] = value.x;
  8626. array[startingIndex] = value.y;
  8627. return array;
  8628. };
  8629. /**
  8630. * Retrieves an instance from a packed array.
  8631. *
  8632. * @param {Number[]} array The packed array.
  8633. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  8634. * @param {Cartesian2} [result] The object into which to store the result.
  8635. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8636. */
  8637. Cartesian2.unpack = function (array, startingIndex, result) {
  8638. //>>includeStart('debug', pragmas.debug);
  8639. RuntimeError.Check.defined("array", array);
  8640. //>>includeEnd('debug');
  8641. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  8642. if (!defaultValue.defined(result)) {
  8643. result = new Cartesian2();
  8644. }
  8645. result.x = array[startingIndex++];
  8646. result.y = array[startingIndex];
  8647. return result;
  8648. };
  8649. /**
  8650. * Flattens an array of Cartesian2s into an array of components.
  8651. *
  8652. * @param {Cartesian2[]} array The array of cartesians to pack.
  8653. * @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.
  8654. * @returns {Number[]} The packed array.
  8655. */
  8656. Cartesian2.packArray = function (array, result) {
  8657. //>>includeStart('debug', pragmas.debug);
  8658. RuntimeError.Check.defined("array", array);
  8659. //>>includeEnd('debug');
  8660. const length = array.length;
  8661. const resultLength = length * 2;
  8662. if (!defaultValue.defined(result)) {
  8663. result = new Array(resultLength);
  8664. } else if (!Array.isArray(result) && result.length !== resultLength) {
  8665. //>>includeStart('debug', pragmas.debug);
  8666. throw new RuntimeError.DeveloperError(
  8667. "If result is a typed array, it must have exactly array.length * 2 elements"
  8668. );
  8669. //>>includeEnd('debug');
  8670. } else if (result.length !== resultLength) {
  8671. result.length = resultLength;
  8672. }
  8673. for (let i = 0; i < length; ++i) {
  8674. Cartesian2.pack(array[i], result, i * 2);
  8675. }
  8676. return result;
  8677. };
  8678. /**
  8679. * Unpacks an array of cartesian components into an array of Cartesian2s.
  8680. *
  8681. * @param {Number[]} array The array of components to unpack.
  8682. * @param {Cartesian2[]} [result] The array onto which to store the result.
  8683. * @returns {Cartesian2[]} The unpacked array.
  8684. */
  8685. Cartesian2.unpackArray = function (array, result) {
  8686. //>>includeStart('debug', pragmas.debug);
  8687. RuntimeError.Check.defined("array", array);
  8688. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 2);
  8689. if (array.length % 2 !== 0) {
  8690. throw new RuntimeError.DeveloperError("array length must be a multiple of 2.");
  8691. }
  8692. //>>includeEnd('debug');
  8693. const length = array.length;
  8694. if (!defaultValue.defined(result)) {
  8695. result = new Array(length / 2);
  8696. } else {
  8697. result.length = length / 2;
  8698. }
  8699. for (let i = 0; i < length; i += 2) {
  8700. const index = i / 2;
  8701. result[index] = Cartesian2.unpack(array, i, result[index]);
  8702. }
  8703. return result;
  8704. };
  8705. /**
  8706. * Creates a Cartesian2 from two consecutive elements in an array.
  8707. * @function
  8708. *
  8709. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  8710. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  8711. * @param {Cartesian2} [result] The object onto which to store the result.
  8712. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8713. *
  8714. * @example
  8715. * // Create a Cartesian2 with (1.0, 2.0)
  8716. * const v = [1.0, 2.0];
  8717. * const p = Cesium.Cartesian2.fromArray(v);
  8718. *
  8719. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  8720. * const v2 = [0.0, 0.0, 1.0, 2.0];
  8721. * const p2 = Cesium.Cartesian2.fromArray(v2, 2);
  8722. */
  8723. Cartesian2.fromArray = Cartesian2.unpack;
  8724. /**
  8725. * Computes the value of the maximum component for the supplied Cartesian.
  8726. *
  8727. * @param {Cartesian2} cartesian The cartesian to use.
  8728. * @returns {Number} The value of the maximum component.
  8729. */
  8730. Cartesian2.maximumComponent = function (cartesian) {
  8731. //>>includeStart('debug', pragmas.debug);
  8732. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8733. //>>includeEnd('debug');
  8734. return Math.max(cartesian.x, cartesian.y);
  8735. };
  8736. /**
  8737. * Computes the value of the minimum component for the supplied Cartesian.
  8738. *
  8739. * @param {Cartesian2} cartesian The cartesian to use.
  8740. * @returns {Number} The value of the minimum component.
  8741. */
  8742. Cartesian2.minimumComponent = function (cartesian) {
  8743. //>>includeStart('debug', pragmas.debug);
  8744. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8745. //>>includeEnd('debug');
  8746. return Math.min(cartesian.x, cartesian.y);
  8747. };
  8748. /**
  8749. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  8750. *
  8751. * @param {Cartesian2} first A cartesian to compare.
  8752. * @param {Cartesian2} second A cartesian to compare.
  8753. * @param {Cartesian2} result The object into which to store the result.
  8754. * @returns {Cartesian2} A cartesian with the minimum components.
  8755. */
  8756. Cartesian2.minimumByComponent = function (first, second, result) {
  8757. //>>includeStart('debug', pragmas.debug);
  8758. RuntimeError.Check.typeOf.object("first", first);
  8759. RuntimeError.Check.typeOf.object("second", second);
  8760. RuntimeError.Check.typeOf.object("result", result);
  8761. //>>includeEnd('debug');
  8762. result.x = Math.min(first.x, second.x);
  8763. result.y = Math.min(first.y, second.y);
  8764. return result;
  8765. };
  8766. /**
  8767. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  8768. *
  8769. * @param {Cartesian2} first A cartesian to compare.
  8770. * @param {Cartesian2} second A cartesian to compare.
  8771. * @param {Cartesian2} result The object into which to store the result.
  8772. * @returns {Cartesian2} A cartesian with the maximum components.
  8773. */
  8774. Cartesian2.maximumByComponent = function (first, second, result) {
  8775. //>>includeStart('debug', pragmas.debug);
  8776. RuntimeError.Check.typeOf.object("first", first);
  8777. RuntimeError.Check.typeOf.object("second", second);
  8778. RuntimeError.Check.typeOf.object("result", result);
  8779. //>>includeEnd('debug');
  8780. result.x = Math.max(first.x, second.x);
  8781. result.y = Math.max(first.y, second.y);
  8782. return result;
  8783. };
  8784. /**
  8785. * Constrain a value to lie between two values.
  8786. *
  8787. * @param {Cartesian2} value The value to clamp.
  8788. * @param {Cartesian2} min The minimum bound.
  8789. * @param {Cartesian2} max The maximum bound.
  8790. * @param {Cartesian2} result The object into which to store the result.
  8791. * @returns {Cartesian2} The clamped value such that min <= result <= max.
  8792. */
  8793. Cartesian2.clamp = function (value, min, max, result) {
  8794. //>>includeStart('debug', pragmas.debug);
  8795. RuntimeError.Check.typeOf.object("value", value);
  8796. RuntimeError.Check.typeOf.object("min", min);
  8797. RuntimeError.Check.typeOf.object("max", max);
  8798. RuntimeError.Check.typeOf.object("result", result);
  8799. //>>includeEnd('debug');
  8800. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  8801. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  8802. result.x = x;
  8803. result.y = y;
  8804. return result;
  8805. };
  8806. /**
  8807. * Computes the provided Cartesian's squared magnitude.
  8808. *
  8809. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  8810. * @returns {Number} The squared magnitude.
  8811. */
  8812. Cartesian2.magnitudeSquared = function (cartesian) {
  8813. //>>includeStart('debug', pragmas.debug);
  8814. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8815. //>>includeEnd('debug');
  8816. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  8817. };
  8818. /**
  8819. * Computes the Cartesian's magnitude (length).
  8820. *
  8821. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  8822. * @returns {Number} The magnitude.
  8823. */
  8824. Cartesian2.magnitude = function (cartesian) {
  8825. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  8826. };
  8827. const distanceScratch = new Cartesian2();
  8828. /**
  8829. * Computes the distance between two points.
  8830. *
  8831. * @param {Cartesian2} left The first point to compute the distance from.
  8832. * @param {Cartesian2} right The second point to compute the distance to.
  8833. * @returns {Number} The distance between two points.
  8834. *
  8835. * @example
  8836. * // Returns 1.0
  8837. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  8838. */
  8839. Cartesian2.distance = function (left, right) {
  8840. //>>includeStart('debug', pragmas.debug);
  8841. RuntimeError.Check.typeOf.object("left", left);
  8842. RuntimeError.Check.typeOf.object("right", right);
  8843. //>>includeEnd('debug');
  8844. Cartesian2.subtract(left, right, distanceScratch);
  8845. return Cartesian2.magnitude(distanceScratch);
  8846. };
  8847. /**
  8848. * Computes the squared distance between two points. Comparing squared distances
  8849. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  8850. *
  8851. * @param {Cartesian2} left The first point to compute the distance from.
  8852. * @param {Cartesian2} right The second point to compute the distance to.
  8853. * @returns {Number} The distance between two points.
  8854. *
  8855. * @example
  8856. * // Returns 4.0, not 2.0
  8857. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  8858. */
  8859. Cartesian2.distanceSquared = function (left, right) {
  8860. //>>includeStart('debug', pragmas.debug);
  8861. RuntimeError.Check.typeOf.object("left", left);
  8862. RuntimeError.Check.typeOf.object("right", right);
  8863. //>>includeEnd('debug');
  8864. Cartesian2.subtract(left, right, distanceScratch);
  8865. return Cartesian2.magnitudeSquared(distanceScratch);
  8866. };
  8867. /**
  8868. * Computes the normalized form of the supplied Cartesian.
  8869. *
  8870. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  8871. * @param {Cartesian2} result The object onto which to store the result.
  8872. * @returns {Cartesian2} The modified result parameter.
  8873. */
  8874. Cartesian2.normalize = function (cartesian, result) {
  8875. //>>includeStart('debug', pragmas.debug);
  8876. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8877. RuntimeError.Check.typeOf.object("result", result);
  8878. //>>includeEnd('debug');
  8879. const magnitude = Cartesian2.magnitude(cartesian);
  8880. result.x = cartesian.x / magnitude;
  8881. result.y = cartesian.y / magnitude;
  8882. //>>includeStart('debug', pragmas.debug);
  8883. if (isNaN(result.x) || isNaN(result.y)) {
  8884. throw new RuntimeError.DeveloperError("normalized result is not a number");
  8885. }
  8886. //>>includeEnd('debug');
  8887. return result;
  8888. };
  8889. /**
  8890. * Computes the dot (scalar) product of two Cartesians.
  8891. *
  8892. * @param {Cartesian2} left The first Cartesian.
  8893. * @param {Cartesian2} right The second Cartesian.
  8894. * @returns {Number} The dot product.
  8895. */
  8896. Cartesian2.dot = function (left, right) {
  8897. //>>includeStart('debug', pragmas.debug);
  8898. RuntimeError.Check.typeOf.object("left", left);
  8899. RuntimeError.Check.typeOf.object("right", right);
  8900. //>>includeEnd('debug');
  8901. return left.x * right.x + left.y * right.y;
  8902. };
  8903. /**
  8904. * Computes the magnitude of the cross product that would result from implicitly setting the Z coordinate of the input vectors to 0
  8905. *
  8906. * @param {Cartesian2} left The first Cartesian.
  8907. * @param {Cartesian2} right The second Cartesian.
  8908. * @returns {Number} The cross product.
  8909. */
  8910. Cartesian2.cross = function (left, right) {
  8911. //>>includeStart('debug', pragmas.debug);
  8912. RuntimeError.Check.typeOf.object("left", left);
  8913. RuntimeError.Check.typeOf.object("right", right);
  8914. //>>includeEnd('debug');
  8915. return left.x * right.y - left.y * right.x;
  8916. };
  8917. /**
  8918. * Computes the componentwise product of two Cartesians.
  8919. *
  8920. * @param {Cartesian2} left The first Cartesian.
  8921. * @param {Cartesian2} right The second Cartesian.
  8922. * @param {Cartesian2} result The object onto which to store the result.
  8923. * @returns {Cartesian2} The modified result parameter.
  8924. */
  8925. Cartesian2.multiplyComponents = function (left, right, result) {
  8926. //>>includeStart('debug', pragmas.debug);
  8927. RuntimeError.Check.typeOf.object("left", left);
  8928. RuntimeError.Check.typeOf.object("right", right);
  8929. RuntimeError.Check.typeOf.object("result", result);
  8930. //>>includeEnd('debug');
  8931. result.x = left.x * right.x;
  8932. result.y = left.y * right.y;
  8933. return result;
  8934. };
  8935. /**
  8936. * Computes the componentwise quotient of two Cartesians.
  8937. *
  8938. * @param {Cartesian2} left The first Cartesian.
  8939. * @param {Cartesian2} right The second Cartesian.
  8940. * @param {Cartesian2} result The object onto which to store the result.
  8941. * @returns {Cartesian2} The modified result parameter.
  8942. */
  8943. Cartesian2.divideComponents = function (left, right, result) {
  8944. //>>includeStart('debug', pragmas.debug);
  8945. RuntimeError.Check.typeOf.object("left", left);
  8946. RuntimeError.Check.typeOf.object("right", right);
  8947. RuntimeError.Check.typeOf.object("result", result);
  8948. //>>includeEnd('debug');
  8949. result.x = left.x / right.x;
  8950. result.y = left.y / right.y;
  8951. return result;
  8952. };
  8953. /**
  8954. * Computes the componentwise sum of two Cartesians.
  8955. *
  8956. * @param {Cartesian2} left The first Cartesian.
  8957. * @param {Cartesian2} right The second Cartesian.
  8958. * @param {Cartesian2} result The object onto which to store the result.
  8959. * @returns {Cartesian2} The modified result parameter.
  8960. */
  8961. Cartesian2.add = function (left, right, result) {
  8962. //>>includeStart('debug', pragmas.debug);
  8963. RuntimeError.Check.typeOf.object("left", left);
  8964. RuntimeError.Check.typeOf.object("right", right);
  8965. RuntimeError.Check.typeOf.object("result", result);
  8966. //>>includeEnd('debug');
  8967. result.x = left.x + right.x;
  8968. result.y = left.y + right.y;
  8969. return result;
  8970. };
  8971. /**
  8972. * Computes the componentwise difference of two Cartesians.
  8973. *
  8974. * @param {Cartesian2} left The first Cartesian.
  8975. * @param {Cartesian2} right The second Cartesian.
  8976. * @param {Cartesian2} result The object onto which to store the result.
  8977. * @returns {Cartesian2} The modified result parameter.
  8978. */
  8979. Cartesian2.subtract = function (left, right, result) {
  8980. //>>includeStart('debug', pragmas.debug);
  8981. RuntimeError.Check.typeOf.object("left", left);
  8982. RuntimeError.Check.typeOf.object("right", right);
  8983. RuntimeError.Check.typeOf.object("result", result);
  8984. //>>includeEnd('debug');
  8985. result.x = left.x - right.x;
  8986. result.y = left.y - right.y;
  8987. return result;
  8988. };
  8989. /**
  8990. * Multiplies the provided Cartesian componentwise by the provided scalar.
  8991. *
  8992. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  8993. * @param {Number} scalar The scalar to multiply with.
  8994. * @param {Cartesian2} result The object onto which to store the result.
  8995. * @returns {Cartesian2} The modified result parameter.
  8996. */
  8997. Cartesian2.multiplyByScalar = function (cartesian, scalar, result) {
  8998. //>>includeStart('debug', pragmas.debug);
  8999. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9000. RuntimeError.Check.typeOf.number("scalar", scalar);
  9001. RuntimeError.Check.typeOf.object("result", result);
  9002. //>>includeEnd('debug');
  9003. result.x = cartesian.x * scalar;
  9004. result.y = cartesian.y * scalar;
  9005. return result;
  9006. };
  9007. /**
  9008. * Divides the provided Cartesian componentwise by the provided scalar.
  9009. *
  9010. * @param {Cartesian2} cartesian The Cartesian to be divided.
  9011. * @param {Number} scalar The scalar to divide by.
  9012. * @param {Cartesian2} result The object onto which to store the result.
  9013. * @returns {Cartesian2} The modified result parameter.
  9014. */
  9015. Cartesian2.divideByScalar = function (cartesian, scalar, result) {
  9016. //>>includeStart('debug', pragmas.debug);
  9017. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9018. RuntimeError.Check.typeOf.number("scalar", scalar);
  9019. RuntimeError.Check.typeOf.object("result", result);
  9020. //>>includeEnd('debug');
  9021. result.x = cartesian.x / scalar;
  9022. result.y = cartesian.y / scalar;
  9023. return result;
  9024. };
  9025. /**
  9026. * Negates the provided Cartesian.
  9027. *
  9028. * @param {Cartesian2} cartesian The Cartesian to be negated.
  9029. * @param {Cartesian2} result The object onto which to store the result.
  9030. * @returns {Cartesian2} The modified result parameter.
  9031. */
  9032. Cartesian2.negate = function (cartesian, result) {
  9033. //>>includeStart('debug', pragmas.debug);
  9034. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9035. RuntimeError.Check.typeOf.object("result", result);
  9036. //>>includeEnd('debug');
  9037. result.x = -cartesian.x;
  9038. result.y = -cartesian.y;
  9039. return result;
  9040. };
  9041. /**
  9042. * Computes the absolute value of the provided Cartesian.
  9043. *
  9044. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  9045. * @param {Cartesian2} result The object onto which to store the result.
  9046. * @returns {Cartesian2} The modified result parameter.
  9047. */
  9048. Cartesian2.abs = function (cartesian, result) {
  9049. //>>includeStart('debug', pragmas.debug);
  9050. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9051. RuntimeError.Check.typeOf.object("result", result);
  9052. //>>includeEnd('debug');
  9053. result.x = Math.abs(cartesian.x);
  9054. result.y = Math.abs(cartesian.y);
  9055. return result;
  9056. };
  9057. const lerpScratch = new Cartesian2();
  9058. /**
  9059. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  9060. *
  9061. * @param {Cartesian2} start The value corresponding to t at 0.0.
  9062. * @param {Cartesian2} end The value corresponding to t at 1.0.
  9063. * @param {Number} t The point along t at which to interpolate.
  9064. * @param {Cartesian2} result The object onto which to store the result.
  9065. * @returns {Cartesian2} The modified result parameter.
  9066. */
  9067. Cartesian2.lerp = function (start, end, t, result) {
  9068. //>>includeStart('debug', pragmas.debug);
  9069. RuntimeError.Check.typeOf.object("start", start);
  9070. RuntimeError.Check.typeOf.object("end", end);
  9071. RuntimeError.Check.typeOf.number("t", t);
  9072. RuntimeError.Check.typeOf.object("result", result);
  9073. //>>includeEnd('debug');
  9074. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  9075. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  9076. return Cartesian2.add(lerpScratch, result, result);
  9077. };
  9078. const angleBetweenScratch = new Cartesian2();
  9079. const angleBetweenScratch2 = new Cartesian2();
  9080. /**
  9081. * Returns the angle, in radians, between the provided Cartesians.
  9082. *
  9083. * @param {Cartesian2} left The first Cartesian.
  9084. * @param {Cartesian2} right The second Cartesian.
  9085. * @returns {Number} The angle between the Cartesians.
  9086. */
  9087. Cartesian2.angleBetween = function (left, right) {
  9088. //>>includeStart('debug', pragmas.debug);
  9089. RuntimeError.Check.typeOf.object("left", left);
  9090. RuntimeError.Check.typeOf.object("right", right);
  9091. //>>includeEnd('debug');
  9092. Cartesian2.normalize(left, angleBetweenScratch);
  9093. Cartesian2.normalize(right, angleBetweenScratch2);
  9094. return ComponentDatatype.CesiumMath.acosClamped(
  9095. Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2)
  9096. );
  9097. };
  9098. const mostOrthogonalAxisScratch = new Cartesian2();
  9099. /**
  9100. * Returns the axis that is most orthogonal to the provided Cartesian.
  9101. *
  9102. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  9103. * @param {Cartesian2} result The object onto which to store the result.
  9104. * @returns {Cartesian2} The most orthogonal axis.
  9105. */
  9106. Cartesian2.mostOrthogonalAxis = function (cartesian, result) {
  9107. //>>includeStart('debug', pragmas.debug);
  9108. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9109. RuntimeError.Check.typeOf.object("result", result);
  9110. //>>includeEnd('debug');
  9111. const f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  9112. Cartesian2.abs(f, f);
  9113. if (f.x <= f.y) {
  9114. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  9115. } else {
  9116. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  9117. }
  9118. return result;
  9119. };
  9120. /**
  9121. * Compares the provided Cartesians componentwise and returns
  9122. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9123. *
  9124. * @param {Cartesian2} [left] The first Cartesian.
  9125. * @param {Cartesian2} [right] The second Cartesian.
  9126. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  9127. */
  9128. Cartesian2.equals = function (left, right) {
  9129. return (
  9130. left === right ||
  9131. (defaultValue.defined(left) &&
  9132. defaultValue.defined(right) &&
  9133. left.x === right.x &&
  9134. left.y === right.y)
  9135. );
  9136. };
  9137. /**
  9138. * @private
  9139. */
  9140. Cartesian2.equalsArray = function (cartesian, array, offset) {
  9141. return cartesian.x === array[offset] && cartesian.y === array[offset + 1];
  9142. };
  9143. /**
  9144. * Compares the provided Cartesians componentwise and returns
  9145. * <code>true</code> if they pass an absolute or relative tolerance test,
  9146. * <code>false</code> otherwise.
  9147. *
  9148. * @param {Cartesian2} [left] The first Cartesian.
  9149. * @param {Cartesian2} [right] The second Cartesian.
  9150. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  9151. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  9152. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  9153. */
  9154. Cartesian2.equalsEpsilon = function (
  9155. left,
  9156. right,
  9157. relativeEpsilon,
  9158. absoluteEpsilon
  9159. ) {
  9160. return (
  9161. left === right ||
  9162. (defaultValue.defined(left) &&
  9163. defaultValue.defined(right) &&
  9164. ComponentDatatype.CesiumMath.equalsEpsilon(
  9165. left.x,
  9166. right.x,
  9167. relativeEpsilon,
  9168. absoluteEpsilon
  9169. ) &&
  9170. ComponentDatatype.CesiumMath.equalsEpsilon(
  9171. left.y,
  9172. right.y,
  9173. relativeEpsilon,
  9174. absoluteEpsilon
  9175. ))
  9176. );
  9177. };
  9178. /**
  9179. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  9180. *
  9181. * @type {Cartesian2}
  9182. * @constant
  9183. */
  9184. Cartesian2.ZERO = Object.freeze(new Cartesian2(0.0, 0.0));
  9185. /**
  9186. * An immutable Cartesian2 instance initialized to (1.0, 1.0).
  9187. *
  9188. * @type {Cartesian2}
  9189. * @constant
  9190. */
  9191. Cartesian2.ONE = Object.freeze(new Cartesian2(1.0, 1.0));
  9192. /**
  9193. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  9194. *
  9195. * @type {Cartesian2}
  9196. * @constant
  9197. */
  9198. Cartesian2.UNIT_X = Object.freeze(new Cartesian2(1.0, 0.0));
  9199. /**
  9200. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  9201. *
  9202. * @type {Cartesian2}
  9203. * @constant
  9204. */
  9205. Cartesian2.UNIT_Y = Object.freeze(new Cartesian2(0.0, 1.0));
  9206. /**
  9207. * Duplicates this Cartesian2 instance.
  9208. *
  9209. * @param {Cartesian2} [result] The object onto which to store the result.
  9210. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  9211. */
  9212. Cartesian2.prototype.clone = function (result) {
  9213. return Cartesian2.clone(this, result);
  9214. };
  9215. /**
  9216. * Compares this Cartesian against the provided Cartesian componentwise and returns
  9217. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9218. *
  9219. * @param {Cartesian2} [right] The right hand side Cartesian.
  9220. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  9221. */
  9222. Cartesian2.prototype.equals = function (right) {
  9223. return Cartesian2.equals(this, right);
  9224. };
  9225. /**
  9226. * Compares this Cartesian against the provided Cartesian componentwise and returns
  9227. * <code>true</code> if they pass an absolute or relative tolerance test,
  9228. * <code>false</code> otherwise.
  9229. *
  9230. * @param {Cartesian2} [right] The right hand side Cartesian.
  9231. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  9232. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  9233. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  9234. */
  9235. Cartesian2.prototype.equalsEpsilon = function (
  9236. right,
  9237. relativeEpsilon,
  9238. absoluteEpsilon
  9239. ) {
  9240. return Cartesian2.equalsEpsilon(
  9241. this,
  9242. right,
  9243. relativeEpsilon,
  9244. absoluteEpsilon
  9245. );
  9246. };
  9247. /**
  9248. * Creates a string representing this Cartesian in the format '(x, y)'.
  9249. *
  9250. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  9251. */
  9252. Cartesian2.prototype.toString = function () {
  9253. return `(${this.x}, ${this.y})`;
  9254. };
  9255. /**
  9256. * A 2x2 matrix, indexable as a column-major order array.
  9257. * Constructor parameters are in row-major order for code readability.
  9258. * @alias Matrix2
  9259. * @constructor
  9260. * @implements {ArrayLike<number>}
  9261. *
  9262. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  9263. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  9264. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  9265. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  9266. *
  9267. * @see Matrix2.fromArray
  9268. * @see Matrix2.fromColumnMajorArray
  9269. * @see Matrix2.fromRowMajorArray
  9270. * @see Matrix2.fromScale
  9271. * @see Matrix2.fromUniformScale
  9272. * @see Matrix2.fromRotation
  9273. * @see Matrix3
  9274. * @see Matrix4
  9275. */
  9276. function Matrix2(column0Row0, column1Row0, column0Row1, column1Row1) {
  9277. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  9278. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  9279. this[2] = defaultValue.defaultValue(column1Row0, 0.0);
  9280. this[3] = defaultValue.defaultValue(column1Row1, 0.0);
  9281. }
  9282. /**
  9283. * The number of elements used to pack the object into an array.
  9284. * @type {Number}
  9285. */
  9286. Matrix2.packedLength = 4;
  9287. /**
  9288. * Stores the provided instance into the provided array.
  9289. *
  9290. * @param {Matrix2} value The value to pack.
  9291. * @param {Number[]} array The array to pack into.
  9292. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  9293. *
  9294. * @returns {Number[]} The array that was packed into
  9295. */
  9296. Matrix2.pack = function (value, array, startingIndex) {
  9297. //>>includeStart('debug', pragmas.debug);
  9298. RuntimeError.Check.typeOf.object("value", value);
  9299. RuntimeError.Check.defined("array", array);
  9300. //>>includeEnd('debug');
  9301. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  9302. array[startingIndex++] = value[0];
  9303. array[startingIndex++] = value[1];
  9304. array[startingIndex++] = value[2];
  9305. array[startingIndex++] = value[3];
  9306. return array;
  9307. };
  9308. /**
  9309. * Retrieves an instance from a packed array.
  9310. *
  9311. * @param {Number[]} array The packed array.
  9312. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  9313. * @param {Matrix2} [result] The object into which to store the result.
  9314. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  9315. */
  9316. Matrix2.unpack = function (array, startingIndex, result) {
  9317. //>>includeStart('debug', pragmas.debug);
  9318. RuntimeError.Check.defined("array", array);
  9319. //>>includeEnd('debug');
  9320. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  9321. if (!defaultValue.defined(result)) {
  9322. result = new Matrix2();
  9323. }
  9324. result[0] = array[startingIndex++];
  9325. result[1] = array[startingIndex++];
  9326. result[2] = array[startingIndex++];
  9327. result[3] = array[startingIndex++];
  9328. return result;
  9329. };
  9330. /**
  9331. * Flattens an array of Matrix2s into an array of components. The components
  9332. * are stored in column-major order.
  9333. *
  9334. * @param {Matrix2[]} array The array of matrices to pack.
  9335. * @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.
  9336. * @returns {Number[]} The packed array.
  9337. */
  9338. Matrix2.packArray = function (array, result) {
  9339. //>>includeStart('debug', pragmas.debug);
  9340. RuntimeError.Check.defined("array", array);
  9341. //>>includeEnd('debug');
  9342. const length = array.length;
  9343. const resultLength = length * 4;
  9344. if (!defaultValue.defined(result)) {
  9345. result = new Array(resultLength);
  9346. } else if (!Array.isArray(result) && result.length !== resultLength) {
  9347. //>>includeStart('debug', pragmas.debug);
  9348. throw new RuntimeError.DeveloperError(
  9349. "If result is a typed array, it must have exactly array.length * 4 elements"
  9350. );
  9351. //>>includeEnd('debug');
  9352. } else if (result.length !== resultLength) {
  9353. result.length = resultLength;
  9354. }
  9355. for (let i = 0; i < length; ++i) {
  9356. Matrix2.pack(array[i], result, i * 4);
  9357. }
  9358. return result;
  9359. };
  9360. /**
  9361. * Unpacks an array of column-major matrix components into an array of Matrix2s.
  9362. *
  9363. * @param {Number[]} array The array of components to unpack.
  9364. * @param {Matrix2[]} [result] The array onto which to store the result.
  9365. * @returns {Matrix2[]} The unpacked array.
  9366. */
  9367. Matrix2.unpackArray = function (array, result) {
  9368. //>>includeStart('debug', pragmas.debug);
  9369. RuntimeError.Check.defined("array", array);
  9370. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  9371. if (array.length % 4 !== 0) {
  9372. throw new RuntimeError.DeveloperError("array length must be a multiple of 4.");
  9373. }
  9374. //>>includeEnd('debug');
  9375. const length = array.length;
  9376. if (!defaultValue.defined(result)) {
  9377. result = new Array(length / 4);
  9378. } else {
  9379. result.length = length / 4;
  9380. }
  9381. for (let i = 0; i < length; i += 4) {
  9382. const index = i / 4;
  9383. result[index] = Matrix2.unpack(array, i, result[index]);
  9384. }
  9385. return result;
  9386. };
  9387. /**
  9388. * Duplicates a Matrix2 instance.
  9389. *
  9390. * @param {Matrix2} matrix The matrix to duplicate.
  9391. * @param {Matrix2} [result] The object onto which to store the result.
  9392. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided. (Returns undefined if matrix is undefined)
  9393. */
  9394. Matrix2.clone = function (matrix, result) {
  9395. if (!defaultValue.defined(matrix)) {
  9396. return undefined;
  9397. }
  9398. if (!defaultValue.defined(result)) {
  9399. return new Matrix2(matrix[0], matrix[2], matrix[1], matrix[3]);
  9400. }
  9401. result[0] = matrix[0];
  9402. result[1] = matrix[1];
  9403. result[2] = matrix[2];
  9404. result[3] = matrix[3];
  9405. return result;
  9406. };
  9407. /**
  9408. * Creates a Matrix2 from 4 consecutive elements in an array.
  9409. *
  9410. * @function
  9411. * @param {Number[]} array The array whose 4 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  9412. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  9413. * @param {Matrix2} [result] The object onto which to store the result.
  9414. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  9415. *
  9416. * @example
  9417. * // Create the Matrix2:
  9418. * // [1.0, 2.0]
  9419. * // [1.0, 2.0]
  9420. *
  9421. * const v = [1.0, 1.0, 2.0, 2.0];
  9422. * const m = Cesium.Matrix2.fromArray(v);
  9423. *
  9424. * // Create same Matrix2 with using an offset into an array
  9425. * const v2 = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0];
  9426. * const m2 = Cesium.Matrix2.fromArray(v2, 2);
  9427. */
  9428. Matrix2.fromArray = Matrix2.unpack;
  9429. /**
  9430. * Creates a Matrix2 instance from a column-major order array.
  9431. *
  9432. * @param {Number[]} values The column-major order array.
  9433. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9434. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9435. */
  9436. Matrix2.fromColumnMajorArray = function (values, result) {
  9437. //>>includeStart('debug', pragmas.debug);
  9438. RuntimeError.Check.defined("values", values);
  9439. //>>includeEnd('debug');
  9440. return Matrix2.clone(values, result);
  9441. };
  9442. /**
  9443. * Creates a Matrix2 instance from a row-major order array.
  9444. * The resulting matrix will be in column-major order.
  9445. *
  9446. * @param {Number[]} values The row-major order array.
  9447. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9448. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9449. */
  9450. Matrix2.fromRowMajorArray = function (values, result) {
  9451. //>>includeStart('debug', pragmas.debug);
  9452. RuntimeError.Check.defined("values", values);
  9453. //>>includeEnd('debug');
  9454. if (!defaultValue.defined(result)) {
  9455. return new Matrix2(values[0], values[1], values[2], values[3]);
  9456. }
  9457. result[0] = values[0];
  9458. result[1] = values[2];
  9459. result[2] = values[1];
  9460. result[3] = values[3];
  9461. return result;
  9462. };
  9463. /**
  9464. * Computes a Matrix2 instance representing a non-uniform scale.
  9465. *
  9466. * @param {Cartesian2} scale The x and y scale factors.
  9467. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9468. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9469. *
  9470. * @example
  9471. * // Creates
  9472. * // [7.0, 0.0]
  9473. * // [0.0, 8.0]
  9474. * const m = Cesium.Matrix2.fromScale(new Cesium.Cartesian2(7.0, 8.0));
  9475. */
  9476. Matrix2.fromScale = function (scale, result) {
  9477. //>>includeStart('debug', pragmas.debug);
  9478. RuntimeError.Check.typeOf.object("scale", scale);
  9479. //>>includeEnd('debug');
  9480. if (!defaultValue.defined(result)) {
  9481. return new Matrix2(scale.x, 0.0, 0.0, scale.y);
  9482. }
  9483. result[0] = scale.x;
  9484. result[1] = 0.0;
  9485. result[2] = 0.0;
  9486. result[3] = scale.y;
  9487. return result;
  9488. };
  9489. /**
  9490. * Computes a Matrix2 instance representing a uniform scale.
  9491. *
  9492. * @param {Number} scale The uniform scale factor.
  9493. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9494. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9495. *
  9496. * @example
  9497. * // Creates
  9498. * // [2.0, 0.0]
  9499. * // [0.0, 2.0]
  9500. * const m = Cesium.Matrix2.fromUniformScale(2.0);
  9501. */
  9502. Matrix2.fromUniformScale = function (scale, result) {
  9503. //>>includeStart('debug', pragmas.debug);
  9504. RuntimeError.Check.typeOf.number("scale", scale);
  9505. //>>includeEnd('debug');
  9506. if (!defaultValue.defined(result)) {
  9507. return new Matrix2(scale, 0.0, 0.0, scale);
  9508. }
  9509. result[0] = scale;
  9510. result[1] = 0.0;
  9511. result[2] = 0.0;
  9512. result[3] = scale;
  9513. return result;
  9514. };
  9515. /**
  9516. * Creates a rotation matrix.
  9517. *
  9518. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  9519. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9520. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9521. *
  9522. * @example
  9523. * // Rotate a point 45 degrees counterclockwise.
  9524. * const p = new Cesium.Cartesian2(5, 6);
  9525. * const m = Cesium.Matrix2.fromRotation(Cesium.Math.toRadians(45.0));
  9526. * const rotated = Cesium.Matrix2.multiplyByVector(m, p, new Cesium.Cartesian2());
  9527. */
  9528. Matrix2.fromRotation = function (angle, result) {
  9529. //>>includeStart('debug', pragmas.debug);
  9530. RuntimeError.Check.typeOf.number("angle", angle);
  9531. //>>includeEnd('debug');
  9532. const cosAngle = Math.cos(angle);
  9533. const sinAngle = Math.sin(angle);
  9534. if (!defaultValue.defined(result)) {
  9535. return new Matrix2(cosAngle, -sinAngle, sinAngle, cosAngle);
  9536. }
  9537. result[0] = cosAngle;
  9538. result[1] = sinAngle;
  9539. result[2] = -sinAngle;
  9540. result[3] = cosAngle;
  9541. return result;
  9542. };
  9543. /**
  9544. * Creates an Array from the provided Matrix2 instance.
  9545. * The array will be in column-major order.
  9546. *
  9547. * @param {Matrix2} matrix The matrix to use..
  9548. * @param {Number[]} [result] The Array onto which to store the result.
  9549. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  9550. */
  9551. Matrix2.toArray = function (matrix, result) {
  9552. //>>includeStart('debug', pragmas.debug);
  9553. RuntimeError.Check.typeOf.object("matrix", matrix);
  9554. //>>includeEnd('debug');
  9555. if (!defaultValue.defined(result)) {
  9556. return [matrix[0], matrix[1], matrix[2], matrix[3]];
  9557. }
  9558. result[0] = matrix[0];
  9559. result[1] = matrix[1];
  9560. result[2] = matrix[2];
  9561. result[3] = matrix[3];
  9562. return result;
  9563. };
  9564. /**
  9565. * Computes the array index of the element at the provided row and column.
  9566. *
  9567. * @param {Number} row The zero-based index of the row.
  9568. * @param {Number} column The zero-based index of the column.
  9569. * @returns {Number} The index of the element at the provided row and column.
  9570. *
  9571. * @exception {DeveloperError} row must be 0 or 1.
  9572. * @exception {DeveloperError} column must be 0 or 1.
  9573. *
  9574. * @example
  9575. * const myMatrix = new Cesium.Matrix2();
  9576. * const column1Row0Index = Cesium.Matrix2.getElementIndex(1, 0);
  9577. * const column1Row0 = myMatrix[column1Row0Index]
  9578. * myMatrix[column1Row0Index] = 10.0;
  9579. */
  9580. Matrix2.getElementIndex = function (column, row) {
  9581. //>>includeStart('debug', pragmas.debug);
  9582. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  9583. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 1);
  9584. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  9585. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 1);
  9586. //>>includeEnd('debug');
  9587. return column * 2 + row;
  9588. };
  9589. /**
  9590. * Retrieves a copy of the matrix column at the provided index as a Cartesian2 instance.
  9591. *
  9592. * @param {Matrix2} matrix The matrix to use.
  9593. * @param {Number} index The zero-based index of the column to retrieve.
  9594. * @param {Cartesian2} result The object onto which to store the result.
  9595. * @returns {Cartesian2} The modified result parameter.
  9596. *
  9597. * @exception {DeveloperError} index must be 0 or 1.
  9598. */
  9599. Matrix2.getColumn = function (matrix, index, result) {
  9600. //>>includeStart('debug', pragmas.debug);
  9601. RuntimeError.Check.typeOf.object("matrix", matrix);
  9602. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9603. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9604. RuntimeError.Check.typeOf.object("result", result);
  9605. //>>includeEnd('debug');
  9606. const startIndex = index * 2;
  9607. const x = matrix[startIndex];
  9608. const y = matrix[startIndex + 1];
  9609. result.x = x;
  9610. result.y = y;
  9611. return result;
  9612. };
  9613. /**
  9614. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian2 instance.
  9615. *
  9616. * @param {Matrix2} matrix The matrix to use.
  9617. * @param {Number} index The zero-based index of the column to set.
  9618. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified column.
  9619. * @param {Cartesian2} result The object onto which to store the result.
  9620. * @returns {Matrix2} The modified result parameter.
  9621. *
  9622. * @exception {DeveloperError} index must be 0 or 1.
  9623. */
  9624. Matrix2.setColumn = function (matrix, index, cartesian, result) {
  9625. //>>includeStart('debug', pragmas.debug);
  9626. RuntimeError.Check.typeOf.object("matrix", matrix);
  9627. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9628. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9629. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9630. RuntimeError.Check.typeOf.object("result", result);
  9631. //>>includeEnd('debug');
  9632. result = Matrix2.clone(matrix, result);
  9633. const startIndex = index * 2;
  9634. result[startIndex] = cartesian.x;
  9635. result[startIndex + 1] = cartesian.y;
  9636. return result;
  9637. };
  9638. /**
  9639. * Retrieves a copy of the matrix row at the provided index as a Cartesian2 instance.
  9640. *
  9641. * @param {Matrix2} matrix The matrix to use.
  9642. * @param {Number} index The zero-based index of the row to retrieve.
  9643. * @param {Cartesian2} result The object onto which to store the result.
  9644. * @returns {Cartesian2} The modified result parameter.
  9645. *
  9646. * @exception {DeveloperError} index must be 0 or 1.
  9647. */
  9648. Matrix2.getRow = function (matrix, index, result) {
  9649. //>>includeStart('debug', pragmas.debug);
  9650. RuntimeError.Check.typeOf.object("matrix", matrix);
  9651. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9652. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9653. RuntimeError.Check.typeOf.object("result", result);
  9654. //>>includeEnd('debug');
  9655. const x = matrix[index];
  9656. const y = matrix[index + 2];
  9657. result.x = x;
  9658. result.y = y;
  9659. return result;
  9660. };
  9661. /**
  9662. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian2 instance.
  9663. *
  9664. * @param {Matrix2} matrix The matrix to use.
  9665. * @param {Number} index The zero-based index of the row to set.
  9666. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified row.
  9667. * @param {Matrix2} result The object onto which to store the result.
  9668. * @returns {Matrix2} The modified result parameter.
  9669. *
  9670. * @exception {DeveloperError} index must be 0 or 1.
  9671. */
  9672. Matrix2.setRow = function (matrix, index, cartesian, result) {
  9673. //>>includeStart('debug', pragmas.debug);
  9674. RuntimeError.Check.typeOf.object("matrix", matrix);
  9675. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9676. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9677. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9678. RuntimeError.Check.typeOf.object("result", result);
  9679. //>>includeEnd('debug');
  9680. result = Matrix2.clone(matrix, result);
  9681. result[index] = cartesian.x;
  9682. result[index + 2] = cartesian.y;
  9683. return result;
  9684. };
  9685. const scaleScratch1 = new Cartesian2();
  9686. /**
  9687. * Computes a new matrix that replaces the scale with the provided scale.
  9688. * This assumes the matrix is an affine transformation.
  9689. *
  9690. * @param {Matrix2} matrix The matrix to use.
  9691. * @param {Cartesian2} scale The scale that replaces the scale of the provided matrix.
  9692. * @param {Matrix2} result The object onto which to store the result.
  9693. * @returns {Matrix2} The modified result parameter.
  9694. *
  9695. * @see Matrix2.setUniformScale
  9696. * @see Matrix2.fromScale
  9697. * @see Matrix2.fromUniformScale
  9698. * @see Matrix2.multiplyByScale
  9699. * @see Matrix2.multiplyByUniformScale
  9700. * @see Matrix2.getScale
  9701. */
  9702. Matrix2.setScale = function (matrix, scale, result) {
  9703. //>>includeStart('debug', pragmas.debug);
  9704. RuntimeError.Check.typeOf.object("matrix", matrix);
  9705. RuntimeError.Check.typeOf.object("scale", scale);
  9706. RuntimeError.Check.typeOf.object("result", result);
  9707. //>>includeEnd('debug');
  9708. const existingScale = Matrix2.getScale(matrix, scaleScratch1);
  9709. const scaleRatioX = scale.x / existingScale.x;
  9710. const scaleRatioY = scale.y / existingScale.y;
  9711. result[0] = matrix[0] * scaleRatioX;
  9712. result[1] = matrix[1] * scaleRatioX;
  9713. result[2] = matrix[2] * scaleRatioY;
  9714. result[3] = matrix[3] * scaleRatioY;
  9715. return result;
  9716. };
  9717. const scaleScratch2 = new Cartesian2();
  9718. /**
  9719. * Computes a new matrix that replaces the scale with the provided uniform scale.
  9720. * This assumes the matrix is an affine transformation.
  9721. *
  9722. * @param {Matrix2} matrix The matrix to use.
  9723. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  9724. * @param {Matrix2} result The object onto which to store the result.
  9725. * @returns {Matrix2} The modified result parameter.
  9726. *
  9727. * @see Matrix2.setScale
  9728. * @see Matrix2.fromScale
  9729. * @see Matrix2.fromUniformScale
  9730. * @see Matrix2.multiplyByScale
  9731. * @see Matrix2.multiplyByUniformScale
  9732. * @see Matrix2.getScale
  9733. */
  9734. Matrix2.setUniformScale = function (matrix, scale, result) {
  9735. //>>includeStart('debug', pragmas.debug);
  9736. RuntimeError.Check.typeOf.object("matrix", matrix);
  9737. RuntimeError.Check.typeOf.number("scale", scale);
  9738. RuntimeError.Check.typeOf.object("result", result);
  9739. //>>includeEnd('debug');
  9740. const existingScale = Matrix2.getScale(matrix, scaleScratch2);
  9741. const scaleRatioX = scale / existingScale.x;
  9742. const scaleRatioY = scale / existingScale.y;
  9743. result[0] = matrix[0] * scaleRatioX;
  9744. result[1] = matrix[1] * scaleRatioX;
  9745. result[2] = matrix[2] * scaleRatioY;
  9746. result[3] = matrix[3] * scaleRatioY;
  9747. return result;
  9748. };
  9749. const scratchColumn = new Cartesian2();
  9750. /**
  9751. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  9752. *
  9753. * @param {Matrix2} matrix The matrix.
  9754. * @param {Cartesian2} result The object onto which to store the result.
  9755. * @returns {Cartesian2} The modified result parameter.
  9756. *
  9757. * @see Matrix2.multiplyByScale
  9758. * @see Matrix2.multiplyByUniformScale
  9759. * @see Matrix2.fromScale
  9760. * @see Matrix2.fromUniformScale
  9761. * @see Matrix2.setScale
  9762. * @see Matrix2.setUniformScale
  9763. */
  9764. Matrix2.getScale = function (matrix, result) {
  9765. //>>includeStart('debug', pragmas.debug);
  9766. RuntimeError.Check.typeOf.object("matrix", matrix);
  9767. RuntimeError.Check.typeOf.object("result", result);
  9768. //>>includeEnd('debug');
  9769. result.x = Cartesian2.magnitude(
  9770. Cartesian2.fromElements(matrix[0], matrix[1], scratchColumn)
  9771. );
  9772. result.y = Cartesian2.magnitude(
  9773. Cartesian2.fromElements(matrix[2], matrix[3], scratchColumn)
  9774. );
  9775. return result;
  9776. };
  9777. const scaleScratch3 = new Cartesian2();
  9778. /**
  9779. * Computes the maximum scale assuming the matrix is an affine transformation.
  9780. * The maximum scale is the maximum length of the column vectors.
  9781. *
  9782. * @param {Matrix2} matrix The matrix.
  9783. * @returns {Number} The maximum scale.
  9784. */
  9785. Matrix2.getMaximumScale = function (matrix) {
  9786. Matrix2.getScale(matrix, scaleScratch3);
  9787. return Cartesian2.maximumComponent(scaleScratch3);
  9788. };
  9789. const scaleScratch4 = new Cartesian2();
  9790. /**
  9791. * Sets the rotation assuming the matrix is an affine transformation.
  9792. *
  9793. * @param {Matrix2} matrix The matrix.
  9794. * @param {Matrix2} rotation The rotation matrix.
  9795. * @returns {Matrix2} The modified result parameter.
  9796. *
  9797. * @see Matrix2.fromRotation
  9798. * @see Matrix2.getRotation
  9799. */
  9800. Matrix2.setRotation = function (matrix, rotation, result) {
  9801. //>>includeStart('debug', pragmas.debug);
  9802. RuntimeError.Check.typeOf.object("matrix", matrix);
  9803. RuntimeError.Check.typeOf.object("result", result);
  9804. //>>includeEnd('debug');
  9805. const scale = Matrix2.getScale(matrix, scaleScratch4);
  9806. result[0] = rotation[0] * scale.x;
  9807. result[1] = rotation[1] * scale.x;
  9808. result[2] = rotation[2] * scale.y;
  9809. result[3] = rotation[3] * scale.y;
  9810. return result;
  9811. };
  9812. const scaleScratch5 = new Cartesian2();
  9813. /**
  9814. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  9815. *
  9816. * @param {Matrix2} matrix The matrix.
  9817. * @param {Matrix2} result The object onto which to store the result.
  9818. * @returns {Matrix2} The modified result parameter.
  9819. *
  9820. * @see Matrix2.setRotation
  9821. * @see Matrix2.fromRotation
  9822. */
  9823. Matrix2.getRotation = function (matrix, result) {
  9824. //>>includeStart('debug', pragmas.debug);
  9825. RuntimeError.Check.typeOf.object("matrix", matrix);
  9826. RuntimeError.Check.typeOf.object("result", result);
  9827. //>>includeEnd('debug');
  9828. const scale = Matrix2.getScale(matrix, scaleScratch5);
  9829. result[0] = matrix[0] / scale.x;
  9830. result[1] = matrix[1] / scale.x;
  9831. result[2] = matrix[2] / scale.y;
  9832. result[3] = matrix[3] / scale.y;
  9833. return result;
  9834. };
  9835. /**
  9836. * Computes the product of two matrices.
  9837. *
  9838. * @param {Matrix2} left The first matrix.
  9839. * @param {Matrix2} right The second matrix.
  9840. * @param {Matrix2} result The object onto which to store the result.
  9841. * @returns {Matrix2} The modified result parameter.
  9842. */
  9843. Matrix2.multiply = function (left, right, result) {
  9844. //>>includeStart('debug', pragmas.debug);
  9845. RuntimeError.Check.typeOf.object("left", left);
  9846. RuntimeError.Check.typeOf.object("right", right);
  9847. RuntimeError.Check.typeOf.object("result", result);
  9848. //>>includeEnd('debug');
  9849. const column0Row0 = left[0] * right[0] + left[2] * right[1];
  9850. const column1Row0 = left[0] * right[2] + left[2] * right[3];
  9851. const column0Row1 = left[1] * right[0] + left[3] * right[1];
  9852. const column1Row1 = left[1] * right[2] + left[3] * right[3];
  9853. result[0] = column0Row0;
  9854. result[1] = column0Row1;
  9855. result[2] = column1Row0;
  9856. result[3] = column1Row1;
  9857. return result;
  9858. };
  9859. /**
  9860. * Computes the sum of two matrices.
  9861. *
  9862. * @param {Matrix2} left The first matrix.
  9863. * @param {Matrix2} right The second matrix.
  9864. * @param {Matrix2} result The object onto which to store the result.
  9865. * @returns {Matrix2} The modified result parameter.
  9866. */
  9867. Matrix2.add = function (left, right, result) {
  9868. //>>includeStart('debug', pragmas.debug);
  9869. RuntimeError.Check.typeOf.object("left", left);
  9870. RuntimeError.Check.typeOf.object("right", right);
  9871. RuntimeError.Check.typeOf.object("result", result);
  9872. //>>includeEnd('debug');
  9873. result[0] = left[0] + right[0];
  9874. result[1] = left[1] + right[1];
  9875. result[2] = left[2] + right[2];
  9876. result[3] = left[3] + right[3];
  9877. return result;
  9878. };
  9879. /**
  9880. * Computes the difference of two matrices.
  9881. *
  9882. * @param {Matrix2} left The first matrix.
  9883. * @param {Matrix2} right The second matrix.
  9884. * @param {Matrix2} result The object onto which to store the result.
  9885. * @returns {Matrix2} The modified result parameter.
  9886. */
  9887. Matrix2.subtract = function (left, right, result) {
  9888. //>>includeStart('debug', pragmas.debug);
  9889. RuntimeError.Check.typeOf.object("left", left);
  9890. RuntimeError.Check.typeOf.object("right", right);
  9891. RuntimeError.Check.typeOf.object("result", result);
  9892. //>>includeEnd('debug');
  9893. result[0] = left[0] - right[0];
  9894. result[1] = left[1] - right[1];
  9895. result[2] = left[2] - right[2];
  9896. result[3] = left[3] - right[3];
  9897. return result;
  9898. };
  9899. /**
  9900. * Computes the product of a matrix and a column vector.
  9901. *
  9902. * @param {Matrix2} matrix The matrix.
  9903. * @param {Cartesian2} cartesian The column.
  9904. * @param {Cartesian2} result The object onto which to store the result.
  9905. * @returns {Cartesian2} The modified result parameter.
  9906. */
  9907. Matrix2.multiplyByVector = function (matrix, cartesian, result) {
  9908. //>>includeStart('debug', pragmas.debug);
  9909. RuntimeError.Check.typeOf.object("matrix", matrix);
  9910. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9911. RuntimeError.Check.typeOf.object("result", result);
  9912. //>>includeEnd('debug');
  9913. const x = matrix[0] * cartesian.x + matrix[2] * cartesian.y;
  9914. const y = matrix[1] * cartesian.x + matrix[3] * cartesian.y;
  9915. result.x = x;
  9916. result.y = y;
  9917. return result;
  9918. };
  9919. /**
  9920. * Computes the product of a matrix and a scalar.
  9921. *
  9922. * @param {Matrix2} matrix The matrix.
  9923. * @param {Number} scalar The number to multiply by.
  9924. * @param {Matrix2} result The object onto which to store the result.
  9925. * @returns {Matrix2} The modified result parameter.
  9926. */
  9927. Matrix2.multiplyByScalar = function (matrix, scalar, result) {
  9928. //>>includeStart('debug', pragmas.debug);
  9929. RuntimeError.Check.typeOf.object("matrix", matrix);
  9930. RuntimeError.Check.typeOf.number("scalar", scalar);
  9931. RuntimeError.Check.typeOf.object("result", result);
  9932. //>>includeEnd('debug');
  9933. result[0] = matrix[0] * scalar;
  9934. result[1] = matrix[1] * scalar;
  9935. result[2] = matrix[2] * scalar;
  9936. result[3] = matrix[3] * scalar;
  9937. return result;
  9938. };
  9939. /**
  9940. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  9941. *
  9942. * @param {Matrix2} matrix The matrix on the left-hand side.
  9943. * @param {Number} scale The non-uniform scale on the right-hand side.
  9944. * @param {Matrix2} result The object onto which to store the result.
  9945. * @returns {Matrix2} The modified result parameter.
  9946. *
  9947. *
  9948. * @example
  9949. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
  9950. * Cesium.Matrix2.multiplyByScale(m, scale, m);
  9951. *
  9952. * @see Matrix2.multiplyByUniformScale
  9953. * @see Matrix2.fromScale
  9954. * @see Matrix2.fromUniformScale
  9955. * @see Matrix2.setScale
  9956. * @see Matrix2.setUniformScale
  9957. * @see Matrix2.getScale
  9958. */
  9959. Matrix2.multiplyByScale = function (matrix, scale, result) {
  9960. //>>includeStart('debug', pragmas.debug);
  9961. RuntimeError.Check.typeOf.object("matrix", matrix);
  9962. RuntimeError.Check.typeOf.object("scale", scale);
  9963. RuntimeError.Check.typeOf.object("result", result);
  9964. //>>includeEnd('debug');
  9965. result[0] = matrix[0] * scale.x;
  9966. result[1] = matrix[1] * scale.x;
  9967. result[2] = matrix[2] * scale.y;
  9968. result[3] = matrix[3] * scale.y;
  9969. return result;
  9970. };
  9971. /**
  9972. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  9973. *
  9974. * @param {Matrix2} matrix The matrix on the left-hand side.
  9975. * @param {Number} scale The uniform scale on the right-hand side.
  9976. * @param {Matrix2} result The object onto which to store the result.
  9977. * @returns {Matrix2} The modified result parameter.
  9978. *
  9979. * @example
  9980. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromUniformScale(scale), m);
  9981. * Cesium.Matrix2.multiplyByUniformScale(m, scale, m);
  9982. *
  9983. * @see Matrix2.multiplyByScale
  9984. * @see Matrix2.fromScale
  9985. * @see Matrix2.fromUniformScale
  9986. * @see Matrix2.setScale
  9987. * @see Matrix2.setUniformScale
  9988. * @see Matrix2.getScale
  9989. */
  9990. Matrix2.multiplyByUniformScale = function (matrix, scale, result) {
  9991. //>>includeStart('debug', pragmas.debug);
  9992. RuntimeError.Check.typeOf.object("matrix", matrix);
  9993. RuntimeError.Check.typeOf.number("scale", scale);
  9994. RuntimeError.Check.typeOf.object("result", result);
  9995. //>>includeEnd('debug');
  9996. result[0] = matrix[0] * scale;
  9997. result[1] = matrix[1] * scale;
  9998. result[2] = matrix[2] * scale;
  9999. result[3] = matrix[3] * scale;
  10000. return result;
  10001. };
  10002. /**
  10003. * Creates a negated copy of the provided matrix.
  10004. *
  10005. * @param {Matrix2} matrix The matrix to negate.
  10006. * @param {Matrix2} result The object onto which to store the result.
  10007. * @returns {Matrix2} The modified result parameter.
  10008. */
  10009. Matrix2.negate = function (matrix, result) {
  10010. //>>includeStart('debug', pragmas.debug);
  10011. RuntimeError.Check.typeOf.object("matrix", matrix);
  10012. RuntimeError.Check.typeOf.object("result", result);
  10013. //>>includeEnd('debug');
  10014. result[0] = -matrix[0];
  10015. result[1] = -matrix[1];
  10016. result[2] = -matrix[2];
  10017. result[3] = -matrix[3];
  10018. return result;
  10019. };
  10020. /**
  10021. * Computes the transpose of the provided matrix.
  10022. *
  10023. * @param {Matrix2} matrix The matrix to transpose.
  10024. * @param {Matrix2} result The object onto which to store the result.
  10025. * @returns {Matrix2} The modified result parameter.
  10026. */
  10027. Matrix2.transpose = function (matrix, result) {
  10028. //>>includeStart('debug', pragmas.debug);
  10029. RuntimeError.Check.typeOf.object("matrix", matrix);
  10030. RuntimeError.Check.typeOf.object("result", result);
  10031. //>>includeEnd('debug');
  10032. const column0Row0 = matrix[0];
  10033. const column0Row1 = matrix[2];
  10034. const column1Row0 = matrix[1];
  10035. const column1Row1 = matrix[3];
  10036. result[0] = column0Row0;
  10037. result[1] = column0Row1;
  10038. result[2] = column1Row0;
  10039. result[3] = column1Row1;
  10040. return result;
  10041. };
  10042. /**
  10043. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  10044. *
  10045. * @param {Matrix2} matrix The matrix with signed elements.
  10046. * @param {Matrix2} result The object onto which to store the result.
  10047. * @returns {Matrix2} The modified result parameter.
  10048. */
  10049. Matrix2.abs = function (matrix, result) {
  10050. //>>includeStart('debug', pragmas.debug);
  10051. RuntimeError.Check.typeOf.object("matrix", matrix);
  10052. RuntimeError.Check.typeOf.object("result", result);
  10053. //>>includeEnd('debug');
  10054. result[0] = Math.abs(matrix[0]);
  10055. result[1] = Math.abs(matrix[1]);
  10056. result[2] = Math.abs(matrix[2]);
  10057. result[3] = Math.abs(matrix[3]);
  10058. return result;
  10059. };
  10060. /**
  10061. * Compares the provided matrices componentwise and returns
  10062. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10063. *
  10064. * @param {Matrix2} [left] The first matrix.
  10065. * @param {Matrix2} [right] The second matrix.
  10066. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  10067. */
  10068. Matrix2.equals = function (left, right) {
  10069. return (
  10070. left === right ||
  10071. (defaultValue.defined(left) &&
  10072. defaultValue.defined(right) &&
  10073. left[0] === right[0] &&
  10074. left[1] === right[1] &&
  10075. left[2] === right[2] &&
  10076. left[3] === right[3])
  10077. );
  10078. };
  10079. /**
  10080. * @private
  10081. */
  10082. Matrix2.equalsArray = function (matrix, array, offset) {
  10083. return (
  10084. matrix[0] === array[offset] &&
  10085. matrix[1] === array[offset + 1] &&
  10086. matrix[2] === array[offset + 2] &&
  10087. matrix[3] === array[offset + 3]
  10088. );
  10089. };
  10090. /**
  10091. * Compares the provided matrices componentwise and returns
  10092. * <code>true</code> if they are within the provided epsilon,
  10093. * <code>false</code> otherwise.
  10094. *
  10095. * @param {Matrix2} [left] The first matrix.
  10096. * @param {Matrix2} [right] The second matrix.
  10097. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  10098. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  10099. */
  10100. Matrix2.equalsEpsilon = function (left, right, epsilon) {
  10101. epsilon = defaultValue.defaultValue(epsilon, 0);
  10102. return (
  10103. left === right ||
  10104. (defaultValue.defined(left) &&
  10105. defaultValue.defined(right) &&
  10106. Math.abs(left[0] - right[0]) <= epsilon &&
  10107. Math.abs(left[1] - right[1]) <= epsilon &&
  10108. Math.abs(left[2] - right[2]) <= epsilon &&
  10109. Math.abs(left[3] - right[3]) <= epsilon)
  10110. );
  10111. };
  10112. /**
  10113. * An immutable Matrix2 instance initialized to the identity matrix.
  10114. *
  10115. * @type {Matrix2}
  10116. * @constant
  10117. */
  10118. Matrix2.IDENTITY = Object.freeze(new Matrix2(1.0, 0.0, 0.0, 1.0));
  10119. /**
  10120. * An immutable Matrix2 instance initialized to the zero matrix.
  10121. *
  10122. * @type {Matrix2}
  10123. * @constant
  10124. */
  10125. Matrix2.ZERO = Object.freeze(new Matrix2(0.0, 0.0, 0.0, 0.0));
  10126. /**
  10127. * The index into Matrix2 for column 0, row 0.
  10128. *
  10129. * @type {Number}
  10130. * @constant
  10131. *
  10132. * @example
  10133. * const matrix = new Cesium.Matrix2();
  10134. * matrix[Cesium.Matrix2.COLUMN0ROW0] = 5.0; // set column 0, row 0 to 5.0
  10135. */
  10136. Matrix2.COLUMN0ROW0 = 0;
  10137. /**
  10138. * The index into Matrix2 for column 0, row 1.
  10139. *
  10140. * @type {Number}
  10141. * @constant
  10142. *
  10143. * @example
  10144. * const matrix = new Cesium.Matrix2();
  10145. * matrix[Cesium.Matrix2.COLUMN0ROW1] = 5.0; // set column 0, row 1 to 5.0
  10146. */
  10147. Matrix2.COLUMN0ROW1 = 1;
  10148. /**
  10149. * The index into Matrix2 for column 1, row 0.
  10150. *
  10151. * @type {Number}
  10152. * @constant
  10153. *
  10154. * @example
  10155. * const matrix = new Cesium.Matrix2();
  10156. * matrix[Cesium.Matrix2.COLUMN1ROW0] = 5.0; // set column 1, row 0 to 5.0
  10157. */
  10158. Matrix2.COLUMN1ROW0 = 2;
  10159. /**
  10160. * The index into Matrix2 for column 1, row 1.
  10161. *
  10162. * @type {Number}
  10163. * @constant
  10164. *
  10165. * @example
  10166. * const matrix = new Cesium.Matrix2();
  10167. * matrix[Cesium.Matrix2.COLUMN1ROW1] = 5.0; // set column 1, row 1 to 5.0
  10168. */
  10169. Matrix2.COLUMN1ROW1 = 3;
  10170. Object.defineProperties(Matrix2.prototype, {
  10171. /**
  10172. * Gets the number of items in the collection.
  10173. * @memberof Matrix2.prototype
  10174. *
  10175. * @type {Number}
  10176. */
  10177. length: {
  10178. get: function () {
  10179. return Matrix2.packedLength;
  10180. },
  10181. },
  10182. });
  10183. /**
  10184. * Duplicates the provided Matrix2 instance.
  10185. *
  10186. * @param {Matrix2} [result] The object onto which to store the result.
  10187. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  10188. */
  10189. Matrix2.prototype.clone = function (result) {
  10190. return Matrix2.clone(this, result);
  10191. };
  10192. /**
  10193. * Compares this matrix to the provided matrix componentwise and returns
  10194. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10195. *
  10196. * @param {Matrix2} [right] The right hand side matrix.
  10197. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  10198. */
  10199. Matrix2.prototype.equals = function (right) {
  10200. return Matrix2.equals(this, right);
  10201. };
  10202. /**
  10203. * Compares this matrix to the provided matrix componentwise and returns
  10204. * <code>true</code> if they are within the provided epsilon,
  10205. * <code>false</code> otherwise.
  10206. *
  10207. * @param {Matrix2} [right] The right hand side matrix.
  10208. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  10209. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  10210. */
  10211. Matrix2.prototype.equalsEpsilon = function (right, epsilon) {
  10212. return Matrix2.equalsEpsilon(this, right, epsilon);
  10213. };
  10214. /**
  10215. * Creates a string representing this Matrix with each row being
  10216. * on a separate line and in the format '(column0, column1)'.
  10217. *
  10218. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1)'.
  10219. */
  10220. Matrix2.prototype.toString = function () {
  10221. return `(${this[0]}, ${this[2]})\n` + `(${this[1]}, ${this[3]})`;
  10222. };
  10223. exports.Cartesian2 = Cartesian2;
  10224. exports.Cartesian3 = Cartesian3;
  10225. exports.Cartesian4 = Cartesian4;
  10226. exports.Cartographic = Cartographic;
  10227. exports.Ellipsoid = Ellipsoid;
  10228. exports.Matrix2 = Matrix2;
  10229. exports.Matrix3 = Matrix3;
  10230. exports.Matrix4 = Matrix4;
  10231. exports.Rectangle = Rectangle;
  10232. }));