Matrix3-41c58dde.js 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283
  1. define(['exports', './Check-6ede7e26', './defaultValue-fe22d8c0', './Math-0a2ac845'], (function (exports, Check, defaultValue, Math$1) { 'use strict';
  2. /**
  3. * A 3D Cartesian point.
  4. * @alias Cartesian3
  5. * @constructor
  6. *
  7. * @param {number} [x=0.0] The X component.
  8. * @param {number} [y=0.0] The Y component.
  9. * @param {number} [z=0.0] The Z component.
  10. *
  11. * @see Cartesian2
  12. * @see Cartesian4
  13. * @see Packable
  14. */
  15. function Cartesian3(x, y, z) {
  16. /**
  17. * The X component.
  18. * @type {number}
  19. * @default 0.0
  20. */
  21. this.x = defaultValue.defaultValue(x, 0.0);
  22. /**
  23. * The Y component.
  24. * @type {number}
  25. * @default 0.0
  26. */
  27. this.y = defaultValue.defaultValue(y, 0.0);
  28. /**
  29. * The Z component.
  30. * @type {number}
  31. * @default 0.0
  32. */
  33. this.z = defaultValue.defaultValue(z, 0.0);
  34. }
  35. /**
  36. * Converts the provided Spherical into Cartesian3 coordinates.
  37. *
  38. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  39. * @param {Cartesian3} [result] The object onto which to store the result.
  40. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  41. */
  42. Cartesian3.fromSpherical = function (spherical, result) {
  43. //>>includeStart('debug', pragmas.debug);
  44. Check.Check.typeOf.object("spherical", spherical);
  45. //>>includeEnd('debug');
  46. if (!defaultValue.defined(result)) {
  47. result = new Cartesian3();
  48. }
  49. const clock = spherical.clock;
  50. const cone = spherical.cone;
  51. const magnitude = defaultValue.defaultValue(spherical.magnitude, 1.0);
  52. const radial = magnitude * Math.sin(cone);
  53. result.x = radial * Math.cos(clock);
  54. result.y = radial * Math.sin(clock);
  55. result.z = magnitude * Math.cos(cone);
  56. return result;
  57. };
  58. /**
  59. * Creates a Cartesian3 instance from x, y and z coordinates.
  60. *
  61. * @param {number} x The x coordinate.
  62. * @param {number} y The y coordinate.
  63. * @param {number} z The z coordinate.
  64. * @param {Cartesian3} [result] The object onto which to store the result.
  65. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  66. */
  67. Cartesian3.fromElements = function (x, y, z, result) {
  68. if (!defaultValue.defined(result)) {
  69. return new Cartesian3(x, y, z);
  70. }
  71. result.x = x;
  72. result.y = y;
  73. result.z = z;
  74. return result;
  75. };
  76. /**
  77. * Duplicates a Cartesian3 instance.
  78. *
  79. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  80. * @param {Cartesian3} [result] The object onto which to store the result.
  81. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  82. */
  83. Cartesian3.clone = function (cartesian, result) {
  84. if (!defaultValue.defined(cartesian)) {
  85. return undefined;
  86. }
  87. if (!defaultValue.defined(result)) {
  88. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  89. }
  90. result.x = cartesian.x;
  91. result.y = cartesian.y;
  92. result.z = cartesian.z;
  93. return result;
  94. };
  95. /**
  96. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  97. * x, y, and z properties of the Cartesian4 and drops w.
  98. * @function
  99. *
  100. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  101. * @param {Cartesian3} [result] The object onto which to store the result.
  102. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  103. */
  104. Cartesian3.fromCartesian4 = Cartesian3.clone;
  105. /**
  106. * The number of elements used to pack the object into an array.
  107. * @type {number}
  108. */
  109. Cartesian3.packedLength = 3;
  110. /**
  111. * Stores the provided instance into the provided array.
  112. *
  113. * @param {Cartesian3} value The value to pack.
  114. * @param {number[]} array The array to pack into.
  115. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  116. *
  117. * @returns {number[]} The array that was packed into
  118. */
  119. Cartesian3.pack = function (value, array, startingIndex) {
  120. //>>includeStart('debug', pragmas.debug);
  121. Check.Check.typeOf.object("value", value);
  122. Check.Check.defined("array", array);
  123. //>>includeEnd('debug');
  124. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  125. array[startingIndex++] = value.x;
  126. array[startingIndex++] = value.y;
  127. array[startingIndex] = value.z;
  128. return array;
  129. };
  130. /**
  131. * Retrieves an instance from a packed array.
  132. *
  133. * @param {number[]} array The packed array.
  134. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  135. * @param {Cartesian3} [result] The object into which to store the result.
  136. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  137. */
  138. Cartesian3.unpack = function (array, startingIndex, result) {
  139. //>>includeStart('debug', pragmas.debug);
  140. Check.Check.defined("array", array);
  141. //>>includeEnd('debug');
  142. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  143. if (!defaultValue.defined(result)) {
  144. result = new Cartesian3();
  145. }
  146. result.x = array[startingIndex++];
  147. result.y = array[startingIndex++];
  148. result.z = array[startingIndex];
  149. return result;
  150. };
  151. /**
  152. * Flattens an array of Cartesian3s into an array of components.
  153. *
  154. * @param {Cartesian3[]} array The array of cartesians to pack.
  155. * @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.
  156. * @returns {number[]} The packed array.
  157. */
  158. Cartesian3.packArray = function (array, result) {
  159. //>>includeStart('debug', pragmas.debug);
  160. Check.Check.defined("array", array);
  161. //>>includeEnd('debug');
  162. const length = array.length;
  163. const resultLength = length * 3;
  164. if (!defaultValue.defined(result)) {
  165. result = new Array(resultLength);
  166. } else if (!Array.isArray(result) && result.length !== resultLength) {
  167. //>>includeStart('debug', pragmas.debug);
  168. throw new Check.DeveloperError(
  169. "If result is a typed array, it must have exactly array.length * 3 elements"
  170. );
  171. //>>includeEnd('debug');
  172. } else if (result.length !== resultLength) {
  173. result.length = resultLength;
  174. }
  175. for (let i = 0; i < length; ++i) {
  176. Cartesian3.pack(array[i], result, i * 3);
  177. }
  178. return result;
  179. };
  180. /**
  181. * Unpacks an array of cartesian components into an array of Cartesian3s.
  182. *
  183. * @param {number[]} array The array of components to unpack.
  184. * @param {Cartesian3[]} [result] The array onto which to store the result.
  185. * @returns {Cartesian3[]} The unpacked array.
  186. */
  187. Cartesian3.unpackArray = function (array, result) {
  188. //>>includeStart('debug', pragmas.debug);
  189. Check.Check.defined("array", array);
  190. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 3);
  191. if (array.length % 3 !== 0) {
  192. throw new Check.DeveloperError("array length must be a multiple of 3.");
  193. }
  194. //>>includeEnd('debug');
  195. const length = array.length;
  196. if (!defaultValue.defined(result)) {
  197. result = new Array(length / 3);
  198. } else {
  199. result.length = length / 3;
  200. }
  201. for (let i = 0; i < length; i += 3) {
  202. const index = i / 3;
  203. result[index] = Cartesian3.unpack(array, i, result[index]);
  204. }
  205. return result;
  206. };
  207. /**
  208. * Creates a Cartesian3 from three consecutive elements in an array.
  209. * @function
  210. *
  211. * @param {number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  212. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  213. * @param {Cartesian3} [result] The object onto which to store the result.
  214. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  215. *
  216. * @example
  217. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  218. * const v = [1.0, 2.0, 3.0];
  219. * const p = Cesium.Cartesian3.fromArray(v);
  220. *
  221. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  222. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  223. * const p2 = Cesium.Cartesian3.fromArray(v2, 2);
  224. */
  225. Cartesian3.fromArray = Cartesian3.unpack;
  226. /**
  227. * Computes the value of the maximum component for the supplied Cartesian.
  228. *
  229. * @param {Cartesian3} cartesian The cartesian to use.
  230. * @returns {number} The value of the maximum component.
  231. */
  232. Cartesian3.maximumComponent = function (cartesian) {
  233. //>>includeStart('debug', pragmas.debug);
  234. Check.Check.typeOf.object("cartesian", cartesian);
  235. //>>includeEnd('debug');
  236. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  237. };
  238. /**
  239. * Computes the value of the minimum component for the supplied Cartesian.
  240. *
  241. * @param {Cartesian3} cartesian The cartesian to use.
  242. * @returns {number} The value of the minimum component.
  243. */
  244. Cartesian3.minimumComponent = function (cartesian) {
  245. //>>includeStart('debug', pragmas.debug);
  246. Check.Check.typeOf.object("cartesian", cartesian);
  247. //>>includeEnd('debug');
  248. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  249. };
  250. /**
  251. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  252. *
  253. * @param {Cartesian3} first A cartesian to compare.
  254. * @param {Cartesian3} second A cartesian to compare.
  255. * @param {Cartesian3} result The object into which to store the result.
  256. * @returns {Cartesian3} A cartesian with the minimum components.
  257. */
  258. Cartesian3.minimumByComponent = function (first, second, result) {
  259. //>>includeStart('debug', pragmas.debug);
  260. Check.Check.typeOf.object("first", first);
  261. Check.Check.typeOf.object("second", second);
  262. Check.Check.typeOf.object("result", result);
  263. //>>includeEnd('debug');
  264. result.x = Math.min(first.x, second.x);
  265. result.y = Math.min(first.y, second.y);
  266. result.z = Math.min(first.z, second.z);
  267. return result;
  268. };
  269. /**
  270. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  271. *
  272. * @param {Cartesian3} first A cartesian to compare.
  273. * @param {Cartesian3} second A cartesian to compare.
  274. * @param {Cartesian3} result The object into which to store the result.
  275. * @returns {Cartesian3} A cartesian with the maximum components.
  276. */
  277. Cartesian3.maximumByComponent = function (first, second, result) {
  278. //>>includeStart('debug', pragmas.debug);
  279. Check.Check.typeOf.object("first", first);
  280. Check.Check.typeOf.object("second", second);
  281. Check.Check.typeOf.object("result", result);
  282. //>>includeEnd('debug');
  283. result.x = Math.max(first.x, second.x);
  284. result.y = Math.max(first.y, second.y);
  285. result.z = Math.max(first.z, second.z);
  286. return result;
  287. };
  288. /**
  289. * Constrain a value to lie between two values.
  290. *
  291. * @param {Cartesian3} cartesian The value to clamp.
  292. * @param {Cartesian3} min The minimum bound.
  293. * @param {Cartesian3} max The maximum bound.
  294. * @param {Cartesian3} result The object into which to store the result.
  295. * @returns {Cartesian3} The clamped value such that min <= value <= max.
  296. */
  297. Cartesian3.clamp = function (value, min, max, result) {
  298. //>>includeStart('debug', pragmas.debug);
  299. Check.Check.typeOf.object("value", value);
  300. Check.Check.typeOf.object("min", min);
  301. Check.Check.typeOf.object("max", max);
  302. Check.Check.typeOf.object("result", result);
  303. //>>includeEnd('debug');
  304. const x = Math$1.CesiumMath.clamp(value.x, min.x, max.x);
  305. const y = Math$1.CesiumMath.clamp(value.y, min.y, max.y);
  306. const z = Math$1.CesiumMath.clamp(value.z, min.z, max.z);
  307. result.x = x;
  308. result.y = y;
  309. result.z = z;
  310. return result;
  311. };
  312. /**
  313. * Computes the provided Cartesian's squared magnitude.
  314. *
  315. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  316. * @returns {number} The squared magnitude.
  317. */
  318. Cartesian3.magnitudeSquared = function (cartesian) {
  319. //>>includeStart('debug', pragmas.debug);
  320. Check.Check.typeOf.object("cartesian", cartesian);
  321. //>>includeEnd('debug');
  322. return (
  323. cartesian.x * cartesian.x +
  324. cartesian.y * cartesian.y +
  325. cartesian.z * cartesian.z
  326. );
  327. };
  328. /**
  329. * Computes the Cartesian's magnitude (length).
  330. *
  331. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  332. * @returns {number} The magnitude.
  333. */
  334. Cartesian3.magnitude = function (cartesian) {
  335. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  336. };
  337. const distanceScratch = new Cartesian3();
  338. /**
  339. * Computes the distance between two points.
  340. *
  341. * @param {Cartesian3} left The first point to compute the distance from.
  342. * @param {Cartesian3} right The second point to compute the distance to.
  343. * @returns {number} The distance between two points.
  344. *
  345. * @example
  346. * // Returns 1.0
  347. * const d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  348. */
  349. Cartesian3.distance = function (left, right) {
  350. //>>includeStart('debug', pragmas.debug);
  351. Check.Check.typeOf.object("left", left);
  352. Check.Check.typeOf.object("right", right);
  353. //>>includeEnd('debug');
  354. Cartesian3.subtract(left, right, distanceScratch);
  355. return Cartesian3.magnitude(distanceScratch);
  356. };
  357. /**
  358. * Computes the squared distance between two points. Comparing squared distances
  359. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  360. *
  361. * @param {Cartesian3} left The first point to compute the distance from.
  362. * @param {Cartesian3} right The second point to compute the distance to.
  363. * @returns {number} The distance between two points.
  364. *
  365. * @example
  366. * // Returns 4.0, not 2.0
  367. * const d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  368. */
  369. Cartesian3.distanceSquared = function (left, right) {
  370. //>>includeStart('debug', pragmas.debug);
  371. Check.Check.typeOf.object("left", left);
  372. Check.Check.typeOf.object("right", right);
  373. //>>includeEnd('debug');
  374. Cartesian3.subtract(left, right, distanceScratch);
  375. return Cartesian3.magnitudeSquared(distanceScratch);
  376. };
  377. /**
  378. * Computes the normalized form of the supplied Cartesian.
  379. *
  380. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  381. * @param {Cartesian3} result The object onto which to store the result.
  382. * @returns {Cartesian3} The modified result parameter.
  383. */
  384. Cartesian3.normalize = function (cartesian, result) {
  385. //>>includeStart('debug', pragmas.debug);
  386. Check.Check.typeOf.object("cartesian", cartesian);
  387. Check.Check.typeOf.object("result", result);
  388. //>>includeEnd('debug');
  389. const magnitude = Cartesian3.magnitude(cartesian);
  390. result.x = cartesian.x / magnitude;
  391. result.y = cartesian.y / magnitude;
  392. result.z = cartesian.z / magnitude;
  393. //>>includeStart('debug', pragmas.debug);
  394. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  395. throw new Check.DeveloperError("normalized result is not a number");
  396. }
  397. //>>includeEnd('debug');
  398. return result;
  399. };
  400. /**
  401. * Computes the dot (scalar) product of two Cartesians.
  402. *
  403. * @param {Cartesian3} left The first Cartesian.
  404. * @param {Cartesian3} right The second Cartesian.
  405. * @returns {number} The dot product.
  406. */
  407. Cartesian3.dot = function (left, right) {
  408. //>>includeStart('debug', pragmas.debug);
  409. Check.Check.typeOf.object("left", left);
  410. Check.Check.typeOf.object("right", right);
  411. //>>includeEnd('debug');
  412. return left.x * right.x + left.y * right.y + left.z * right.z;
  413. };
  414. /**
  415. * Computes the componentwise product of two Cartesians.
  416. *
  417. * @param {Cartesian3} left The first Cartesian.
  418. * @param {Cartesian3} right The second Cartesian.
  419. * @param {Cartesian3} result The object onto which to store the result.
  420. * @returns {Cartesian3} The modified result parameter.
  421. */
  422. Cartesian3.multiplyComponents = function (left, right, result) {
  423. //>>includeStart('debug', pragmas.debug);
  424. Check.Check.typeOf.object("left", left);
  425. Check.Check.typeOf.object("right", right);
  426. Check.Check.typeOf.object("result", result);
  427. //>>includeEnd('debug');
  428. result.x = left.x * right.x;
  429. result.y = left.y * right.y;
  430. result.z = left.z * right.z;
  431. return result;
  432. };
  433. /**
  434. * Computes the componentwise quotient of two Cartesians.
  435. *
  436. * @param {Cartesian3} left The first Cartesian.
  437. * @param {Cartesian3} right The second Cartesian.
  438. * @param {Cartesian3} result The object onto which to store the result.
  439. * @returns {Cartesian3} The modified result parameter.
  440. */
  441. Cartesian3.divideComponents = function (left, right, result) {
  442. //>>includeStart('debug', pragmas.debug);
  443. Check.Check.typeOf.object("left", left);
  444. Check.Check.typeOf.object("right", right);
  445. Check.Check.typeOf.object("result", result);
  446. //>>includeEnd('debug');
  447. result.x = left.x / right.x;
  448. result.y = left.y / right.y;
  449. result.z = left.z / right.z;
  450. return result;
  451. };
  452. /**
  453. * Computes the componentwise sum of two Cartesians.
  454. *
  455. * @param {Cartesian3} left The first Cartesian.
  456. * @param {Cartesian3} right The second Cartesian.
  457. * @param {Cartesian3} result The object onto which to store the result.
  458. * @returns {Cartesian3} The modified result parameter.
  459. */
  460. Cartesian3.add = function (left, right, result) {
  461. //>>includeStart('debug', pragmas.debug);
  462. Check.Check.typeOf.object("left", left);
  463. Check.Check.typeOf.object("right", right);
  464. Check.Check.typeOf.object("result", result);
  465. //>>includeEnd('debug');
  466. result.x = left.x + right.x;
  467. result.y = left.y + right.y;
  468. result.z = left.z + right.z;
  469. return result;
  470. };
  471. /**
  472. * Computes the componentwise difference of two Cartesians.
  473. *
  474. * @param {Cartesian3} left The first Cartesian.
  475. * @param {Cartesian3} right The second Cartesian.
  476. * @param {Cartesian3} result The object onto which to store the result.
  477. * @returns {Cartesian3} The modified result parameter.
  478. */
  479. Cartesian3.subtract = function (left, right, result) {
  480. //>>includeStart('debug', pragmas.debug);
  481. Check.Check.typeOf.object("left", left);
  482. Check.Check.typeOf.object("right", right);
  483. Check.Check.typeOf.object("result", result);
  484. //>>includeEnd('debug');
  485. result.x = left.x - right.x;
  486. result.y = left.y - right.y;
  487. result.z = left.z - right.z;
  488. return result;
  489. };
  490. /**
  491. * Multiplies the provided Cartesian componentwise by the provided scalar.
  492. *
  493. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  494. * @param {number} scalar The scalar to multiply with.
  495. * @param {Cartesian3} result The object onto which to store the result.
  496. * @returns {Cartesian3} The modified result parameter.
  497. */
  498. Cartesian3.multiplyByScalar = function (cartesian, scalar, result) {
  499. //>>includeStart('debug', pragmas.debug);
  500. Check.Check.typeOf.object("cartesian", cartesian);
  501. Check.Check.typeOf.number("scalar", scalar);
  502. Check.Check.typeOf.object("result", result);
  503. //>>includeEnd('debug');
  504. result.x = cartesian.x * scalar;
  505. result.y = cartesian.y * scalar;
  506. result.z = cartesian.z * scalar;
  507. return result;
  508. };
  509. /**
  510. * Divides the provided Cartesian componentwise by the provided scalar.
  511. *
  512. * @param {Cartesian3} cartesian The Cartesian to be divided.
  513. * @param {number} scalar The scalar to divide by.
  514. * @param {Cartesian3} result The object onto which to store the result.
  515. * @returns {Cartesian3} The modified result parameter.
  516. */
  517. Cartesian3.divideByScalar = function (cartesian, scalar, result) {
  518. //>>includeStart('debug', pragmas.debug);
  519. Check.Check.typeOf.object("cartesian", cartesian);
  520. Check.Check.typeOf.number("scalar", scalar);
  521. Check.Check.typeOf.object("result", result);
  522. //>>includeEnd('debug');
  523. result.x = cartesian.x / scalar;
  524. result.y = cartesian.y / scalar;
  525. result.z = cartesian.z / scalar;
  526. return result;
  527. };
  528. /**
  529. * Negates the provided Cartesian.
  530. *
  531. * @param {Cartesian3} cartesian The Cartesian to be negated.
  532. * @param {Cartesian3} result The object onto which to store the result.
  533. * @returns {Cartesian3} The modified result parameter.
  534. */
  535. Cartesian3.negate = function (cartesian, result) {
  536. //>>includeStart('debug', pragmas.debug);
  537. Check.Check.typeOf.object("cartesian", cartesian);
  538. Check.Check.typeOf.object("result", result);
  539. //>>includeEnd('debug');
  540. result.x = -cartesian.x;
  541. result.y = -cartesian.y;
  542. result.z = -cartesian.z;
  543. return result;
  544. };
  545. /**
  546. * Computes the absolute value of the provided Cartesian.
  547. *
  548. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  549. * @param {Cartesian3} result The object onto which to store the result.
  550. * @returns {Cartesian3} The modified result parameter.
  551. */
  552. Cartesian3.abs = function (cartesian, result) {
  553. //>>includeStart('debug', pragmas.debug);
  554. Check.Check.typeOf.object("cartesian", cartesian);
  555. Check.Check.typeOf.object("result", result);
  556. //>>includeEnd('debug');
  557. result.x = Math.abs(cartesian.x);
  558. result.y = Math.abs(cartesian.y);
  559. result.z = Math.abs(cartesian.z);
  560. return result;
  561. };
  562. const lerpScratch = new Cartesian3();
  563. /**
  564. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  565. *
  566. * @param {Cartesian3} start The value corresponding to t at 0.0.
  567. * @param {Cartesian3} end The value corresponding to t at 1.0.
  568. * @param {number} t The point along t at which to interpolate.
  569. * @param {Cartesian3} result The object onto which to store the result.
  570. * @returns {Cartesian3} The modified result parameter.
  571. */
  572. Cartesian3.lerp = function (start, end, t, result) {
  573. //>>includeStart('debug', pragmas.debug);
  574. Check.Check.typeOf.object("start", start);
  575. Check.Check.typeOf.object("end", end);
  576. Check.Check.typeOf.number("t", t);
  577. Check.Check.typeOf.object("result", result);
  578. //>>includeEnd('debug');
  579. Cartesian3.multiplyByScalar(end, t, lerpScratch);
  580. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  581. return Cartesian3.add(lerpScratch, result, result);
  582. };
  583. const angleBetweenScratch = new Cartesian3();
  584. const angleBetweenScratch2 = new Cartesian3();
  585. /**
  586. * Returns the angle, in radians, between the provided Cartesians.
  587. *
  588. * @param {Cartesian3} left The first Cartesian.
  589. * @param {Cartesian3} right The second Cartesian.
  590. * @returns {number} The angle between the Cartesians.
  591. */
  592. Cartesian3.angleBetween = function (left, right) {
  593. //>>includeStart('debug', pragmas.debug);
  594. Check.Check.typeOf.object("left", left);
  595. Check.Check.typeOf.object("right", right);
  596. //>>includeEnd('debug');
  597. Cartesian3.normalize(left, angleBetweenScratch);
  598. Cartesian3.normalize(right, angleBetweenScratch2);
  599. const cosine = Cartesian3.dot(angleBetweenScratch, angleBetweenScratch2);
  600. const sine = Cartesian3.magnitude(
  601. Cartesian3.cross(
  602. angleBetweenScratch,
  603. angleBetweenScratch2,
  604. angleBetweenScratch
  605. )
  606. );
  607. return Math.atan2(sine, cosine);
  608. };
  609. const mostOrthogonalAxisScratch = new Cartesian3();
  610. /**
  611. * Returns the axis that is most orthogonal to the provided Cartesian.
  612. *
  613. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  614. * @param {Cartesian3} result The object onto which to store the result.
  615. * @returns {Cartesian3} The most orthogonal axis.
  616. */
  617. Cartesian3.mostOrthogonalAxis = function (cartesian, result) {
  618. //>>includeStart('debug', pragmas.debug);
  619. Check.Check.typeOf.object("cartesian", cartesian);
  620. Check.Check.typeOf.object("result", result);
  621. //>>includeEnd('debug');
  622. const f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch);
  623. Cartesian3.abs(f, f);
  624. if (f.x <= f.y) {
  625. if (f.x <= f.z) {
  626. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  627. } else {
  628. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  629. }
  630. } else if (f.y <= f.z) {
  631. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  632. } else {
  633. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  634. }
  635. return result;
  636. };
  637. /**
  638. * Projects vector a onto vector b
  639. * @param {Cartesian3} a The vector that needs projecting
  640. * @param {Cartesian3} b The vector to project onto
  641. * @param {Cartesian3} result The result cartesian
  642. * @returns {Cartesian3} The modified result parameter
  643. */
  644. Cartesian3.projectVector = function (a, b, result) {
  645. //>>includeStart('debug', pragmas.debug);
  646. Check.Check.defined("a", a);
  647. Check.Check.defined("b", b);
  648. Check.Check.defined("result", result);
  649. //>>includeEnd('debug');
  650. const scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
  651. return Cartesian3.multiplyByScalar(b, scalar, result);
  652. };
  653. /**
  654. * Compares the provided Cartesians componentwise and returns
  655. * <code>true</code> if they are equal, <code>false</code> otherwise.
  656. *
  657. * @param {Cartesian3} [left] The first Cartesian.
  658. * @param {Cartesian3} [right] The second Cartesian.
  659. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  660. */
  661. Cartesian3.equals = function (left, right) {
  662. return (
  663. left === right ||
  664. (defaultValue.defined(left) &&
  665. defaultValue.defined(right) &&
  666. left.x === right.x &&
  667. left.y === right.y &&
  668. left.z === right.z)
  669. );
  670. };
  671. /**
  672. * @private
  673. */
  674. Cartesian3.equalsArray = function (cartesian, array, offset) {
  675. return (
  676. cartesian.x === array[offset] &&
  677. cartesian.y === array[offset + 1] &&
  678. cartesian.z === array[offset + 2]
  679. );
  680. };
  681. /**
  682. * Compares the provided Cartesians componentwise and returns
  683. * <code>true</code> if they pass an absolute or relative tolerance test,
  684. * <code>false</code> otherwise.
  685. *
  686. * @param {Cartesian3} [left] The first Cartesian.
  687. * @param {Cartesian3} [right] The second Cartesian.
  688. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  689. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  690. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  691. */
  692. Cartesian3.equalsEpsilon = function (
  693. left,
  694. right,
  695. relativeEpsilon,
  696. absoluteEpsilon
  697. ) {
  698. return (
  699. left === right ||
  700. (defaultValue.defined(left) &&
  701. defaultValue.defined(right) &&
  702. Math$1.CesiumMath.equalsEpsilon(
  703. left.x,
  704. right.x,
  705. relativeEpsilon,
  706. absoluteEpsilon
  707. ) &&
  708. Math$1.CesiumMath.equalsEpsilon(
  709. left.y,
  710. right.y,
  711. relativeEpsilon,
  712. absoluteEpsilon
  713. ) &&
  714. Math$1.CesiumMath.equalsEpsilon(
  715. left.z,
  716. right.z,
  717. relativeEpsilon,
  718. absoluteEpsilon
  719. ))
  720. );
  721. };
  722. /**
  723. * Computes the cross (outer) product of two Cartesians.
  724. *
  725. * @param {Cartesian3} left The first Cartesian.
  726. * @param {Cartesian3} right The second Cartesian.
  727. * @param {Cartesian3} result The object onto which to store the result.
  728. * @returns {Cartesian3} The cross product.
  729. */
  730. Cartesian3.cross = function (left, right, result) {
  731. //>>includeStart('debug', pragmas.debug);
  732. Check.Check.typeOf.object("left", left);
  733. Check.Check.typeOf.object("right", right);
  734. Check.Check.typeOf.object("result", result);
  735. //>>includeEnd('debug');
  736. const leftX = left.x;
  737. const leftY = left.y;
  738. const leftZ = left.z;
  739. const rightX = right.x;
  740. const rightY = right.y;
  741. const rightZ = right.z;
  742. const x = leftY * rightZ - leftZ * rightY;
  743. const y = leftZ * rightX - leftX * rightZ;
  744. const z = leftX * rightY - leftY * rightX;
  745. result.x = x;
  746. result.y = y;
  747. result.z = z;
  748. return result;
  749. };
  750. /**
  751. * Computes the midpoint between the right and left Cartesian.
  752. * @param {Cartesian3} left The first Cartesian.
  753. * @param {Cartesian3} right The second Cartesian.
  754. * @param {Cartesian3} result The object onto which to store the result.
  755. * @returns {Cartesian3} The midpoint.
  756. */
  757. Cartesian3.midpoint = function (left, right, result) {
  758. //>>includeStart('debug', pragmas.debug);
  759. Check.Check.typeOf.object("left", left);
  760. Check.Check.typeOf.object("right", right);
  761. Check.Check.typeOf.object("result", result);
  762. //>>includeEnd('debug');
  763. result.x = (left.x + right.x) * 0.5;
  764. result.y = (left.y + right.y) * 0.5;
  765. result.z = (left.z + right.z) * 0.5;
  766. return result;
  767. };
  768. /**
  769. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  770. *
  771. * @param {number} longitude The longitude, in degrees
  772. * @param {number} latitude The latitude, in degrees
  773. * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
  774. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  775. * @param {Cartesian3} [result] The object onto which to store the result.
  776. * @returns {Cartesian3} The position
  777. *
  778. * @example
  779. * const position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  780. */
  781. Cartesian3.fromDegrees = function (
  782. longitude,
  783. latitude,
  784. height,
  785. ellipsoid,
  786. result
  787. ) {
  788. //>>includeStart('debug', pragmas.debug);
  789. Check.Check.typeOf.number("longitude", longitude);
  790. Check.Check.typeOf.number("latitude", latitude);
  791. //>>includeEnd('debug');
  792. longitude = Math$1.CesiumMath.toRadians(longitude);
  793. latitude = Math$1.CesiumMath.toRadians(latitude);
  794. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  795. };
  796. let scratchN = new Cartesian3();
  797. let scratchK = new Cartesian3();
  798. const wgs84RadiiSquared = new Cartesian3(
  799. 6378137.0 * 6378137.0,
  800. 6378137.0 * 6378137.0,
  801. 6356752.3142451793 * 6356752.3142451793
  802. );
  803. /**
  804. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  805. *
  806. * @param {number} longitude The longitude, in radians
  807. * @param {number} latitude The latitude, in radians
  808. * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
  809. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  810. * @param {Cartesian3} [result] The object onto which to store the result.
  811. * @returns {Cartesian3} The position
  812. *
  813. * @example
  814. * const position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  815. */
  816. Cartesian3.fromRadians = function (
  817. longitude,
  818. latitude,
  819. height,
  820. ellipsoid,
  821. result
  822. ) {
  823. //>>includeStart('debug', pragmas.debug);
  824. Check.Check.typeOf.number("longitude", longitude);
  825. Check.Check.typeOf.number("latitude", latitude);
  826. //>>includeEnd('debug');
  827. height = defaultValue.defaultValue(height, 0.0);
  828. const radiiSquared = defaultValue.defined(ellipsoid)
  829. ? ellipsoid.radiiSquared
  830. : wgs84RadiiSquared;
  831. const cosLatitude = Math.cos(latitude);
  832. scratchN.x = cosLatitude * Math.cos(longitude);
  833. scratchN.y = cosLatitude * Math.sin(longitude);
  834. scratchN.z = Math.sin(latitude);
  835. scratchN = Cartesian3.normalize(scratchN, scratchN);
  836. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  837. const gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  838. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  839. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  840. if (!defaultValue.defined(result)) {
  841. result = new Cartesian3();
  842. }
  843. return Cartesian3.add(scratchK, scratchN, result);
  844. };
  845. /**
  846. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  847. *
  848. * @param {number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  849. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  850. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  851. * @returns {Cartesian3[]} The array of positions.
  852. *
  853. * @example
  854. * const positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  855. */
  856. Cartesian3.fromDegreesArray = function (coordinates, ellipsoid, result) {
  857. //>>includeStart('debug', pragmas.debug);
  858. Check.Check.defined("coordinates", coordinates);
  859. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  860. throw new Check.DeveloperError(
  861. "the number of coordinates must be a multiple of 2 and at least 2"
  862. );
  863. }
  864. //>>includeEnd('debug');
  865. const length = coordinates.length;
  866. if (!defaultValue.defined(result)) {
  867. result = new Array(length / 2);
  868. } else {
  869. result.length = length / 2;
  870. }
  871. for (let i = 0; i < length; i += 2) {
  872. const longitude = coordinates[i];
  873. const latitude = coordinates[i + 1];
  874. const index = i / 2;
  875. result[index] = Cartesian3.fromDegrees(
  876. longitude,
  877. latitude,
  878. 0,
  879. ellipsoid,
  880. result[index]
  881. );
  882. }
  883. return result;
  884. };
  885. /**
  886. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  887. *
  888. * @param {number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  889. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  890. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  891. * @returns {Cartesian3[]} The array of positions.
  892. *
  893. * @example
  894. * const positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  895. */
  896. Cartesian3.fromRadiansArray = function (coordinates, ellipsoid, result) {
  897. //>>includeStart('debug', pragmas.debug);
  898. Check.Check.defined("coordinates", coordinates);
  899. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  900. throw new Check.DeveloperError(
  901. "the number of coordinates must be a multiple of 2 and at least 2"
  902. );
  903. }
  904. //>>includeEnd('debug');
  905. const length = coordinates.length;
  906. if (!defaultValue.defined(result)) {
  907. result = new Array(length / 2);
  908. } else {
  909. result.length = length / 2;
  910. }
  911. for (let i = 0; i < length; i += 2) {
  912. const longitude = coordinates[i];
  913. const latitude = coordinates[i + 1];
  914. const index = i / 2;
  915. result[index] = Cartesian3.fromRadians(
  916. longitude,
  917. latitude,
  918. 0,
  919. ellipsoid,
  920. result[index]
  921. );
  922. }
  923. return result;
  924. };
  925. /**
  926. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  927. *
  928. * @param {number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  929. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  930. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  931. * @returns {Cartesian3[]} The array of positions.
  932. *
  933. * @example
  934. * const positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  935. */
  936. Cartesian3.fromDegreesArrayHeights = function (coordinates, ellipsoid, result) {
  937. //>>includeStart('debug', pragmas.debug);
  938. Check.Check.defined("coordinates", coordinates);
  939. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  940. throw new Check.DeveloperError(
  941. "the number of coordinates must be a multiple of 3 and at least 3"
  942. );
  943. }
  944. //>>includeEnd('debug');
  945. const length = coordinates.length;
  946. if (!defaultValue.defined(result)) {
  947. result = new Array(length / 3);
  948. } else {
  949. result.length = length / 3;
  950. }
  951. for (let i = 0; i < length; i += 3) {
  952. const longitude = coordinates[i];
  953. const latitude = coordinates[i + 1];
  954. const height = coordinates[i + 2];
  955. const index = i / 3;
  956. result[index] = Cartesian3.fromDegrees(
  957. longitude,
  958. latitude,
  959. height,
  960. ellipsoid,
  961. result[index]
  962. );
  963. }
  964. return result;
  965. };
  966. /**
  967. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  968. *
  969. * @param {number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  970. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  971. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  972. * @returns {Cartesian3[]} The array of positions.
  973. *
  974. * @example
  975. * const positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  976. */
  977. Cartesian3.fromRadiansArrayHeights = function (coordinates, ellipsoid, result) {
  978. //>>includeStart('debug', pragmas.debug);
  979. Check.Check.defined("coordinates", coordinates);
  980. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  981. throw new Check.DeveloperError(
  982. "the number of coordinates must be a multiple of 3 and at least 3"
  983. );
  984. }
  985. //>>includeEnd('debug');
  986. const length = coordinates.length;
  987. if (!defaultValue.defined(result)) {
  988. result = new Array(length / 3);
  989. } else {
  990. result.length = length / 3;
  991. }
  992. for (let i = 0; i < length; i += 3) {
  993. const longitude = coordinates[i];
  994. const latitude = coordinates[i + 1];
  995. const height = coordinates[i + 2];
  996. const index = i / 3;
  997. result[index] = Cartesian3.fromRadians(
  998. longitude,
  999. latitude,
  1000. height,
  1001. ellipsoid,
  1002. result[index]
  1003. );
  1004. }
  1005. return result;
  1006. };
  1007. /**
  1008. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  1009. *
  1010. * @type {Cartesian3}
  1011. * @constant
  1012. */
  1013. Cartesian3.ZERO = Object.freeze(new Cartesian3(0.0, 0.0, 0.0));
  1014. /**
  1015. * An immutable Cartesian3 instance initialized to (1.0, 1.0, 1.0).
  1016. *
  1017. * @type {Cartesian3}
  1018. * @constant
  1019. */
  1020. Cartesian3.ONE = Object.freeze(new Cartesian3(1.0, 1.0, 1.0));
  1021. /**
  1022. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  1023. *
  1024. * @type {Cartesian3}
  1025. * @constant
  1026. */
  1027. Cartesian3.UNIT_X = Object.freeze(new Cartesian3(1.0, 0.0, 0.0));
  1028. /**
  1029. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  1030. *
  1031. * @type {Cartesian3}
  1032. * @constant
  1033. */
  1034. Cartesian3.UNIT_Y = Object.freeze(new Cartesian3(0.0, 1.0, 0.0));
  1035. /**
  1036. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  1037. *
  1038. * @type {Cartesian3}
  1039. * @constant
  1040. */
  1041. Cartesian3.UNIT_Z = Object.freeze(new Cartesian3(0.0, 0.0, 1.0));
  1042. /**
  1043. * Duplicates this Cartesian3 instance.
  1044. *
  1045. * @param {Cartesian3} [result] The object onto which to store the result.
  1046. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1047. */
  1048. Cartesian3.prototype.clone = function (result) {
  1049. return Cartesian3.clone(this, result);
  1050. };
  1051. /**
  1052. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1053. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1054. *
  1055. * @param {Cartesian3} [right] The right hand side Cartesian.
  1056. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1057. */
  1058. Cartesian3.prototype.equals = function (right) {
  1059. return Cartesian3.equals(this, right);
  1060. };
  1061. /**
  1062. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1063. * <code>true</code> if they pass an absolute or relative tolerance test,
  1064. * <code>false</code> otherwise.
  1065. *
  1066. * @param {Cartesian3} [right] The right hand side Cartesian.
  1067. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  1068. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1069. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1070. */
  1071. Cartesian3.prototype.equalsEpsilon = function (
  1072. right,
  1073. relativeEpsilon,
  1074. absoluteEpsilon
  1075. ) {
  1076. return Cartesian3.equalsEpsilon(
  1077. this,
  1078. right,
  1079. relativeEpsilon,
  1080. absoluteEpsilon
  1081. );
  1082. };
  1083. /**
  1084. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  1085. *
  1086. * @returns {string} A string representing this Cartesian in the format '(x, y, z)'.
  1087. */
  1088. Cartesian3.prototype.toString = function () {
  1089. return `(${this.x}, ${this.y}, ${this.z})`;
  1090. };
  1091. const scaleToGeodeticSurfaceIntersection = new Cartesian3();
  1092. const scaleToGeodeticSurfaceGradient = new Cartesian3();
  1093. /**
  1094. * Scales the provided Cartesian position along the geodetic surface normal
  1095. * so that it is on the surface of this ellipsoid. If the position is
  1096. * at the center of the ellipsoid, this function returns undefined.
  1097. *
  1098. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1099. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  1100. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  1101. * @param {number} centerToleranceSquared Tolerance for closeness to the center.
  1102. * @param {Cartesian3} [result] The object onto which to store the result.
  1103. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1104. *
  1105. * @function scaleToGeodeticSurface
  1106. *
  1107. * @private
  1108. */
  1109. function scaleToGeodeticSurface(
  1110. cartesian,
  1111. oneOverRadii,
  1112. oneOverRadiiSquared,
  1113. centerToleranceSquared,
  1114. result
  1115. ) {
  1116. //>>includeStart('debug', pragmas.debug);
  1117. if (!defaultValue.defined(cartesian)) {
  1118. throw new Check.DeveloperError("cartesian is required.");
  1119. }
  1120. if (!defaultValue.defined(oneOverRadii)) {
  1121. throw new Check.DeveloperError("oneOverRadii is required.");
  1122. }
  1123. if (!defaultValue.defined(oneOverRadiiSquared)) {
  1124. throw new Check.DeveloperError("oneOverRadiiSquared is required.");
  1125. }
  1126. if (!defaultValue.defined(centerToleranceSquared)) {
  1127. throw new Check.DeveloperError("centerToleranceSquared is required.");
  1128. }
  1129. //>>includeEnd('debug');
  1130. const positionX = cartesian.x;
  1131. const positionY = cartesian.y;
  1132. const positionZ = cartesian.z;
  1133. const oneOverRadiiX = oneOverRadii.x;
  1134. const oneOverRadiiY = oneOverRadii.y;
  1135. const oneOverRadiiZ = oneOverRadii.z;
  1136. const x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  1137. const y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  1138. const z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  1139. // Compute the squared ellipsoid norm.
  1140. const squaredNorm = x2 + y2 + z2;
  1141. const ratio = Math.sqrt(1.0 / squaredNorm);
  1142. // As an initial approximation, assume that the radial intersection is the projection point.
  1143. const intersection = Cartesian3.multiplyByScalar(
  1144. cartesian,
  1145. ratio,
  1146. scaleToGeodeticSurfaceIntersection
  1147. );
  1148. // If the position is near the center, the iteration will not converge.
  1149. if (squaredNorm < centerToleranceSquared) {
  1150. return !isFinite(ratio)
  1151. ? undefined
  1152. : Cartesian3.clone(intersection, result);
  1153. }
  1154. const oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  1155. const oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  1156. const oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  1157. // Use the gradient at the intersection point in place of the true unit normal.
  1158. // The difference in magnitude will be absorbed in the multiplier.
  1159. const gradient = scaleToGeodeticSurfaceGradient;
  1160. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  1161. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  1162. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  1163. // Compute the initial guess at the normal vector multiplier, lambda.
  1164. let lambda =
  1165. ((1.0 - ratio) * Cartesian3.magnitude(cartesian)) /
  1166. (0.5 * Cartesian3.magnitude(gradient));
  1167. let correction = 0.0;
  1168. let func;
  1169. let denominator;
  1170. let xMultiplier;
  1171. let yMultiplier;
  1172. let zMultiplier;
  1173. let xMultiplier2;
  1174. let yMultiplier2;
  1175. let zMultiplier2;
  1176. let xMultiplier3;
  1177. let yMultiplier3;
  1178. let zMultiplier3;
  1179. do {
  1180. lambda -= correction;
  1181. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  1182. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  1183. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  1184. xMultiplier2 = xMultiplier * xMultiplier;
  1185. yMultiplier2 = yMultiplier * yMultiplier;
  1186. zMultiplier2 = zMultiplier * zMultiplier;
  1187. xMultiplier3 = xMultiplier2 * xMultiplier;
  1188. yMultiplier3 = yMultiplier2 * yMultiplier;
  1189. zMultiplier3 = zMultiplier2 * zMultiplier;
  1190. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  1191. // "denominator" here refers to the use of this expression in the velocity and acceleration
  1192. // computations in the sections to follow.
  1193. denominator =
  1194. x2 * xMultiplier3 * oneOverRadiiSquaredX +
  1195. y2 * yMultiplier3 * oneOverRadiiSquaredY +
  1196. z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  1197. const derivative = -2.0 * denominator;
  1198. correction = func / derivative;
  1199. } while (Math.abs(func) > Math$1.CesiumMath.EPSILON12);
  1200. if (!defaultValue.defined(result)) {
  1201. return new Cartesian3(
  1202. positionX * xMultiplier,
  1203. positionY * yMultiplier,
  1204. positionZ * zMultiplier
  1205. );
  1206. }
  1207. result.x = positionX * xMultiplier;
  1208. result.y = positionY * yMultiplier;
  1209. result.z = positionZ * zMultiplier;
  1210. return result;
  1211. }
  1212. /**
  1213. * A position defined by longitude, latitude, and height.
  1214. * @alias Cartographic
  1215. * @constructor
  1216. *
  1217. * @param {number} [longitude=0.0] The longitude, in radians.
  1218. * @param {number} [latitude=0.0] The latitude, in radians.
  1219. * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
  1220. *
  1221. * @see Ellipsoid
  1222. */
  1223. function Cartographic(longitude, latitude, height) {
  1224. /**
  1225. * The longitude, in radians.
  1226. * @type {number}
  1227. * @default 0.0
  1228. */
  1229. this.longitude = defaultValue.defaultValue(longitude, 0.0);
  1230. /**
  1231. * The latitude, in radians.
  1232. * @type {number}
  1233. * @default 0.0
  1234. */
  1235. this.latitude = defaultValue.defaultValue(latitude, 0.0);
  1236. /**
  1237. * The height, in meters, above the ellipsoid.
  1238. * @type {number}
  1239. * @default 0.0
  1240. */
  1241. this.height = defaultValue.defaultValue(height, 0.0);
  1242. }
  1243. /**
  1244. * Creates a new Cartographic instance from longitude and latitude
  1245. * specified in radians.
  1246. *
  1247. * @param {number} longitude The longitude, in radians.
  1248. * @param {number} latitude The latitude, in radians.
  1249. * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
  1250. * @param {Cartographic} [result] The object onto which to store the result.
  1251. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1252. */
  1253. Cartographic.fromRadians = function (longitude, latitude, height, result) {
  1254. //>>includeStart('debug', pragmas.debug);
  1255. Check.Check.typeOf.number("longitude", longitude);
  1256. Check.Check.typeOf.number("latitude", latitude);
  1257. //>>includeEnd('debug');
  1258. height = defaultValue.defaultValue(height, 0.0);
  1259. if (!defaultValue.defined(result)) {
  1260. return new Cartographic(longitude, latitude, height);
  1261. }
  1262. result.longitude = longitude;
  1263. result.latitude = latitude;
  1264. result.height = height;
  1265. return result;
  1266. };
  1267. /**
  1268. * Creates a new Cartographic instance from longitude and latitude
  1269. * specified in degrees. The values in the resulting object will
  1270. * be in radians.
  1271. *
  1272. * @param {number} longitude The longitude, in degrees.
  1273. * @param {number} latitude The latitude, in degrees.
  1274. * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
  1275. * @param {Cartographic} [result] The object onto which to store the result.
  1276. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1277. */
  1278. Cartographic.fromDegrees = function (longitude, latitude, height, result) {
  1279. //>>includeStart('debug', pragmas.debug);
  1280. Check.Check.typeOf.number("longitude", longitude);
  1281. Check.Check.typeOf.number("latitude", latitude);
  1282. //>>includeEnd('debug');
  1283. longitude = Math$1.CesiumMath.toRadians(longitude);
  1284. latitude = Math$1.CesiumMath.toRadians(latitude);
  1285. return Cartographic.fromRadians(longitude, latitude, height, result);
  1286. };
  1287. const cartesianToCartographicN$1 = new Cartesian3();
  1288. const cartesianToCartographicP$1 = new Cartesian3();
  1289. const cartesianToCartographicH$1 = new Cartesian3();
  1290. const wgs84OneOverRadii = new Cartesian3(
  1291. 1.0 / 6378137.0,
  1292. 1.0 / 6378137.0,
  1293. 1.0 / 6356752.3142451793
  1294. );
  1295. const wgs84OneOverRadiiSquared = new Cartesian3(
  1296. 1.0 / (6378137.0 * 6378137.0),
  1297. 1.0 / (6378137.0 * 6378137.0),
  1298. 1.0 / (6356752.3142451793 * 6356752.3142451793)
  1299. );
  1300. const wgs84CenterToleranceSquared = Math$1.CesiumMath.EPSILON1;
  1301. /**
  1302. * Creates a new Cartographic instance from a Cartesian position. The values in the
  1303. * resulting object will be in radians.
  1304. *
  1305. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1306. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1307. * @param {Cartographic} [result] The object onto which to store the result.
  1308. * @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.
  1309. */
  1310. Cartographic.fromCartesian = function (cartesian, ellipsoid, result) {
  1311. const oneOverRadii = defaultValue.defined(ellipsoid)
  1312. ? ellipsoid.oneOverRadii
  1313. : wgs84OneOverRadii;
  1314. const oneOverRadiiSquared = defaultValue.defined(ellipsoid)
  1315. ? ellipsoid.oneOverRadiiSquared
  1316. : wgs84OneOverRadiiSquared;
  1317. const centerToleranceSquared = defaultValue.defined(ellipsoid)
  1318. ? ellipsoid._centerToleranceSquared
  1319. : wgs84CenterToleranceSquared;
  1320. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1321. const p = scaleToGeodeticSurface(
  1322. cartesian,
  1323. oneOverRadii,
  1324. oneOverRadiiSquared,
  1325. centerToleranceSquared,
  1326. cartesianToCartographicP$1
  1327. );
  1328. if (!defaultValue.defined(p)) {
  1329. return undefined;
  1330. }
  1331. let n = Cartesian3.multiplyComponents(
  1332. p,
  1333. oneOverRadiiSquared,
  1334. cartesianToCartographicN$1
  1335. );
  1336. n = Cartesian3.normalize(n, n);
  1337. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH$1);
  1338. const longitude = Math.atan2(n.y, n.x);
  1339. const latitude = Math.asin(n.z);
  1340. const height =
  1341. Math$1.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1342. if (!defaultValue.defined(result)) {
  1343. return new Cartographic(longitude, latitude, height);
  1344. }
  1345. result.longitude = longitude;
  1346. result.latitude = latitude;
  1347. result.height = height;
  1348. return result;
  1349. };
  1350. /**
  1351. * Creates a new Cartesian3 instance from a Cartographic input. The values in the inputted
  1352. * object should be in radians.
  1353. *
  1354. * @param {Cartographic} cartographic Input to be converted into a Cartesian3 output.
  1355. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1356. * @param {Cartesian3} [result] The object onto which to store the result.
  1357. * @returns {Cartesian3} The position
  1358. */
  1359. Cartographic.toCartesian = function (cartographic, ellipsoid, result) {
  1360. //>>includeStart('debug', pragmas.debug);
  1361. Check.Check.defined("cartographic", cartographic);
  1362. //>>includeEnd('debug');
  1363. return Cartesian3.fromRadians(
  1364. cartographic.longitude,
  1365. cartographic.latitude,
  1366. cartographic.height,
  1367. ellipsoid,
  1368. result
  1369. );
  1370. };
  1371. /**
  1372. * Duplicates a Cartographic instance.
  1373. *
  1374. * @param {Cartographic} cartographic The cartographic to duplicate.
  1375. * @param {Cartographic} [result] The object onto which to store the result.
  1376. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  1377. */
  1378. Cartographic.clone = function (cartographic, result) {
  1379. if (!defaultValue.defined(cartographic)) {
  1380. return undefined;
  1381. }
  1382. if (!defaultValue.defined(result)) {
  1383. return new Cartographic(
  1384. cartographic.longitude,
  1385. cartographic.latitude,
  1386. cartographic.height
  1387. );
  1388. }
  1389. result.longitude = cartographic.longitude;
  1390. result.latitude = cartographic.latitude;
  1391. result.height = cartographic.height;
  1392. return result;
  1393. };
  1394. /**
  1395. * Compares the provided cartographics componentwise and returns
  1396. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1397. *
  1398. * @param {Cartographic} [left] The first cartographic.
  1399. * @param {Cartographic} [right] The second cartographic.
  1400. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1401. */
  1402. Cartographic.equals = function (left, right) {
  1403. return (
  1404. left === right ||
  1405. (defaultValue.defined(left) &&
  1406. defaultValue.defined(right) &&
  1407. left.longitude === right.longitude &&
  1408. left.latitude === right.latitude &&
  1409. left.height === right.height)
  1410. );
  1411. };
  1412. /**
  1413. * Compares the provided cartographics componentwise and returns
  1414. * <code>true</code> if they are within the provided epsilon,
  1415. * <code>false</code> otherwise.
  1416. *
  1417. * @param {Cartographic} [left] The first cartographic.
  1418. * @param {Cartographic} [right] The second cartographic.
  1419. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  1420. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1421. */
  1422. Cartographic.equalsEpsilon = function (left, right, epsilon) {
  1423. epsilon = defaultValue.defaultValue(epsilon, 0);
  1424. return (
  1425. left === right ||
  1426. (defaultValue.defined(left) &&
  1427. defaultValue.defined(right) &&
  1428. Math.abs(left.longitude - right.longitude) <= epsilon &&
  1429. Math.abs(left.latitude - right.latitude) <= epsilon &&
  1430. Math.abs(left.height - right.height) <= epsilon)
  1431. );
  1432. };
  1433. /**
  1434. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  1435. *
  1436. * @type {Cartographic}
  1437. * @constant
  1438. */
  1439. Cartographic.ZERO = Object.freeze(new Cartographic(0.0, 0.0, 0.0));
  1440. /**
  1441. * Duplicates this instance.
  1442. *
  1443. * @param {Cartographic} [result] The object onto which to store the result.
  1444. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1445. */
  1446. Cartographic.prototype.clone = function (result) {
  1447. return Cartographic.clone(this, result);
  1448. };
  1449. /**
  1450. * Compares the provided against this cartographic componentwise and returns
  1451. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1452. *
  1453. * @param {Cartographic} [right] The second cartographic.
  1454. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1455. */
  1456. Cartographic.prototype.equals = function (right) {
  1457. return Cartographic.equals(this, right);
  1458. };
  1459. /**
  1460. * Compares the provided against this cartographic componentwise and returns
  1461. * <code>true</code> if they are within the provided epsilon,
  1462. * <code>false</code> otherwise.
  1463. *
  1464. * @param {Cartographic} [right] The second cartographic.
  1465. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  1466. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1467. */
  1468. Cartographic.prototype.equalsEpsilon = function (right, epsilon) {
  1469. return Cartographic.equalsEpsilon(this, right, epsilon);
  1470. };
  1471. /**
  1472. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  1473. *
  1474. * @returns {string} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  1475. */
  1476. Cartographic.prototype.toString = function () {
  1477. return `(${this.longitude}, ${this.latitude}, ${this.height})`;
  1478. };
  1479. function initialize(ellipsoid, x, y, z) {
  1480. x = defaultValue.defaultValue(x, 0.0);
  1481. y = defaultValue.defaultValue(y, 0.0);
  1482. z = defaultValue.defaultValue(z, 0.0);
  1483. //>>includeStart('debug', pragmas.debug);
  1484. Check.Check.typeOf.number.greaterThanOrEquals("x", x, 0.0);
  1485. Check.Check.typeOf.number.greaterThanOrEquals("y", y, 0.0);
  1486. Check.Check.typeOf.number.greaterThanOrEquals("z", z, 0.0);
  1487. //>>includeEnd('debug');
  1488. ellipsoid._radii = new Cartesian3(x, y, z);
  1489. ellipsoid._radiiSquared = new Cartesian3(x * x, y * y, z * z);
  1490. ellipsoid._radiiToTheFourth = new Cartesian3(
  1491. x * x * x * x,
  1492. y * y * y * y,
  1493. z * z * z * z
  1494. );
  1495. ellipsoid._oneOverRadii = new Cartesian3(
  1496. x === 0.0 ? 0.0 : 1.0 / x,
  1497. y === 0.0 ? 0.0 : 1.0 / y,
  1498. z === 0.0 ? 0.0 : 1.0 / z
  1499. );
  1500. ellipsoid._oneOverRadiiSquared = new Cartesian3(
  1501. x === 0.0 ? 0.0 : 1.0 / (x * x),
  1502. y === 0.0 ? 0.0 : 1.0 / (y * y),
  1503. z === 0.0 ? 0.0 : 1.0 / (z * z)
  1504. );
  1505. ellipsoid._minimumRadius = Math.min(x, y, z);
  1506. ellipsoid._maximumRadius = Math.max(x, y, z);
  1507. ellipsoid._centerToleranceSquared = Math$1.CesiumMath.EPSILON1;
  1508. if (ellipsoid._radiiSquared.z !== 0) {
  1509. ellipsoid._squaredXOverSquaredZ =
  1510. ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  1511. }
  1512. }
  1513. /**
  1514. * A quadratic surface defined in Cartesian coordinates by the equation
  1515. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  1516. * by Cesium to represent the shape of planetary bodies.
  1517. *
  1518. * Rather than constructing this object directly, one of the provided
  1519. * constants is normally used.
  1520. * @alias Ellipsoid
  1521. * @constructor
  1522. *
  1523. * @param {number} [x=0] The radius in the x direction.
  1524. * @param {number} [y=0] The radius in the y direction.
  1525. * @param {number} [z=0] The radius in the z direction.
  1526. *
  1527. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1528. *
  1529. * @see Ellipsoid.fromCartesian3
  1530. * @see Ellipsoid.WGS84
  1531. * @see Ellipsoid.UNIT_SPHERE
  1532. */
  1533. function Ellipsoid(x, y, z) {
  1534. this._radii = undefined;
  1535. this._radiiSquared = undefined;
  1536. this._radiiToTheFourth = undefined;
  1537. this._oneOverRadii = undefined;
  1538. this._oneOverRadiiSquared = undefined;
  1539. this._minimumRadius = undefined;
  1540. this._maximumRadius = undefined;
  1541. this._centerToleranceSquared = undefined;
  1542. this._squaredXOverSquaredZ = undefined;
  1543. initialize(this, x, y, z);
  1544. }
  1545. Object.defineProperties(Ellipsoid.prototype, {
  1546. /**
  1547. * Gets the radii of the ellipsoid.
  1548. * @memberof Ellipsoid.prototype
  1549. * @type {Cartesian3}
  1550. * @readonly
  1551. */
  1552. radii: {
  1553. get: function () {
  1554. return this._radii;
  1555. },
  1556. },
  1557. /**
  1558. * Gets the squared radii of the ellipsoid.
  1559. * @memberof Ellipsoid.prototype
  1560. * @type {Cartesian3}
  1561. * @readonly
  1562. */
  1563. radiiSquared: {
  1564. get: function () {
  1565. return this._radiiSquared;
  1566. },
  1567. },
  1568. /**
  1569. * Gets the radii of the ellipsoid raise to the fourth power.
  1570. * @memberof Ellipsoid.prototype
  1571. * @type {Cartesian3}
  1572. * @readonly
  1573. */
  1574. radiiToTheFourth: {
  1575. get: function () {
  1576. return this._radiiToTheFourth;
  1577. },
  1578. },
  1579. /**
  1580. * Gets one over the radii of the ellipsoid.
  1581. * @memberof Ellipsoid.prototype
  1582. * @type {Cartesian3}
  1583. * @readonly
  1584. */
  1585. oneOverRadii: {
  1586. get: function () {
  1587. return this._oneOverRadii;
  1588. },
  1589. },
  1590. /**
  1591. * Gets one over the squared radii of the ellipsoid.
  1592. * @memberof Ellipsoid.prototype
  1593. * @type {Cartesian3}
  1594. * @readonly
  1595. */
  1596. oneOverRadiiSquared: {
  1597. get: function () {
  1598. return this._oneOverRadiiSquared;
  1599. },
  1600. },
  1601. /**
  1602. * Gets the minimum radius of the ellipsoid.
  1603. * @memberof Ellipsoid.prototype
  1604. * @type {number}
  1605. * @readonly
  1606. */
  1607. minimumRadius: {
  1608. get: function () {
  1609. return this._minimumRadius;
  1610. },
  1611. },
  1612. /**
  1613. * Gets the maximum radius of the ellipsoid.
  1614. * @memberof Ellipsoid.prototype
  1615. * @type {number}
  1616. * @readonly
  1617. */
  1618. maximumRadius: {
  1619. get: function () {
  1620. return this._maximumRadius;
  1621. },
  1622. },
  1623. });
  1624. /**
  1625. * Duplicates an Ellipsoid instance.
  1626. *
  1627. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  1628. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1629. * instance should be created.
  1630. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  1631. */
  1632. Ellipsoid.clone = function (ellipsoid, result) {
  1633. if (!defaultValue.defined(ellipsoid)) {
  1634. return undefined;
  1635. }
  1636. const radii = ellipsoid._radii;
  1637. if (!defaultValue.defined(result)) {
  1638. return new Ellipsoid(radii.x, radii.y, radii.z);
  1639. }
  1640. Cartesian3.clone(radii, result._radii);
  1641. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  1642. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  1643. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  1644. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  1645. result._minimumRadius = ellipsoid._minimumRadius;
  1646. result._maximumRadius = ellipsoid._maximumRadius;
  1647. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  1648. return result;
  1649. };
  1650. /**
  1651. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  1652. *
  1653. * @param {Cartesian3} [cartesian=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  1654. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1655. * instance should be created.
  1656. * @returns {Ellipsoid} A new Ellipsoid instance.
  1657. *
  1658. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1659. *
  1660. * @see Ellipsoid.WGS84
  1661. * @see Ellipsoid.UNIT_SPHERE
  1662. */
  1663. Ellipsoid.fromCartesian3 = function (cartesian, result) {
  1664. if (!defaultValue.defined(result)) {
  1665. result = new Ellipsoid();
  1666. }
  1667. if (!defaultValue.defined(cartesian)) {
  1668. return result;
  1669. }
  1670. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  1671. return result;
  1672. };
  1673. /**
  1674. * An Ellipsoid instance initialized to the WGS84 standard.
  1675. *
  1676. * @type {Ellipsoid}
  1677. * @constant
  1678. */
  1679. Ellipsoid.WGS84 = Object.freeze(
  1680. new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793)
  1681. );
  1682. /**
  1683. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  1684. *
  1685. * @type {Ellipsoid}
  1686. * @constant
  1687. */
  1688. Ellipsoid.UNIT_SPHERE = Object.freeze(new Ellipsoid(1.0, 1.0, 1.0));
  1689. /**
  1690. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  1691. *
  1692. * @type {Ellipsoid}
  1693. * @constant
  1694. */
  1695. Ellipsoid.MOON = Object.freeze(
  1696. new Ellipsoid(
  1697. Math$1.CesiumMath.LUNAR_RADIUS,
  1698. Math$1.CesiumMath.LUNAR_RADIUS,
  1699. Math$1.CesiumMath.LUNAR_RADIUS
  1700. )
  1701. );
  1702. /**
  1703. * Duplicates an Ellipsoid instance.
  1704. *
  1705. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1706. * instance should be created.
  1707. * @returns {Ellipsoid} The cloned Ellipsoid.
  1708. */
  1709. Ellipsoid.prototype.clone = function (result) {
  1710. return Ellipsoid.clone(this, result);
  1711. };
  1712. /**
  1713. * The number of elements used to pack the object into an array.
  1714. * @type {number}
  1715. */
  1716. Ellipsoid.packedLength = Cartesian3.packedLength;
  1717. /**
  1718. * Stores the provided instance into the provided array.
  1719. *
  1720. * @param {Ellipsoid} value The value to pack.
  1721. * @param {number[]} array The array to pack into.
  1722. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  1723. *
  1724. * @returns {number[]} The array that was packed into
  1725. */
  1726. Ellipsoid.pack = function (value, array, startingIndex) {
  1727. //>>includeStart('debug', pragmas.debug);
  1728. Check.Check.typeOf.object("value", value);
  1729. Check.Check.defined("array", array);
  1730. //>>includeEnd('debug');
  1731. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1732. Cartesian3.pack(value._radii, array, startingIndex);
  1733. return array;
  1734. };
  1735. /**
  1736. * Retrieves an instance from a packed array.
  1737. *
  1738. * @param {number[]} array The packed array.
  1739. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  1740. * @param {Ellipsoid} [result] The object into which to store the result.
  1741. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  1742. */
  1743. Ellipsoid.unpack = function (array, startingIndex, result) {
  1744. //>>includeStart('debug', pragmas.debug);
  1745. Check.Check.defined("array", array);
  1746. //>>includeEnd('debug');
  1747. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1748. const radii = Cartesian3.unpack(array, startingIndex);
  1749. return Ellipsoid.fromCartesian3(radii, result);
  1750. };
  1751. /**
  1752. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  1753. * @function
  1754. *
  1755. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  1756. * @param {Cartesian3} [result] The object onto which to store the result.
  1757. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1758. */
  1759. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  1760. /**
  1761. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1762. *
  1763. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  1764. * @param {Cartesian3} [result] The object onto which to store the result.
  1765. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1766. */
  1767. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function (
  1768. cartographic,
  1769. result
  1770. ) {
  1771. //>>includeStart('debug', pragmas.debug);
  1772. Check.Check.typeOf.object("cartographic", cartographic);
  1773. //>>includeEnd('debug');
  1774. const longitude = cartographic.longitude;
  1775. const latitude = cartographic.latitude;
  1776. const cosLatitude = Math.cos(latitude);
  1777. const x = cosLatitude * Math.cos(longitude);
  1778. const y = cosLatitude * Math.sin(longitude);
  1779. const z = Math.sin(latitude);
  1780. if (!defaultValue.defined(result)) {
  1781. result = new Cartesian3();
  1782. }
  1783. result.x = x;
  1784. result.y = y;
  1785. result.z = z;
  1786. return Cartesian3.normalize(result, result);
  1787. };
  1788. /**
  1789. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1790. *
  1791. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  1792. * @param {Cartesian3} [result] The object onto which to store the result.
  1793. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided, or undefined if a normal cannot be found.
  1794. */
  1795. Ellipsoid.prototype.geodeticSurfaceNormal = function (cartesian, result) {
  1796. if (
  1797. Cartesian3.equalsEpsilon(cartesian, Cartesian3.ZERO, Math$1.CesiumMath.EPSILON14)
  1798. ) {
  1799. return undefined;
  1800. }
  1801. if (!defaultValue.defined(result)) {
  1802. result = new Cartesian3();
  1803. }
  1804. result = Cartesian3.multiplyComponents(
  1805. cartesian,
  1806. this._oneOverRadiiSquared,
  1807. result
  1808. );
  1809. return Cartesian3.normalize(result, result);
  1810. };
  1811. const cartographicToCartesianNormal = new Cartesian3();
  1812. const cartographicToCartesianK = new Cartesian3();
  1813. /**
  1814. * Converts the provided cartographic to Cartesian representation.
  1815. *
  1816. * @param {Cartographic} cartographic The cartographic position.
  1817. * @param {Cartesian3} [result] The object onto which to store the result.
  1818. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1819. *
  1820. * @example
  1821. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  1822. * const position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  1823. * const cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  1824. */
  1825. Ellipsoid.prototype.cartographicToCartesian = function (cartographic, result) {
  1826. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  1827. const n = cartographicToCartesianNormal;
  1828. const k = cartographicToCartesianK;
  1829. this.geodeticSurfaceNormalCartographic(cartographic, n);
  1830. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  1831. const gamma = Math.sqrt(Cartesian3.dot(n, k));
  1832. Cartesian3.divideByScalar(k, gamma, k);
  1833. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  1834. if (!defaultValue.defined(result)) {
  1835. result = new Cartesian3();
  1836. }
  1837. return Cartesian3.add(k, n, result);
  1838. };
  1839. /**
  1840. * Converts the provided array of cartographics to an array of Cartesians.
  1841. *
  1842. * @param {Cartographic[]} cartographics An array of cartographic positions.
  1843. * @param {Cartesian3[]} [result] The object onto which to store the result.
  1844. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  1845. *
  1846. * @example
  1847. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  1848. * const positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  1849. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  1850. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  1851. * const cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  1852. */
  1853. Ellipsoid.prototype.cartographicArrayToCartesianArray = function (
  1854. cartographics,
  1855. result
  1856. ) {
  1857. //>>includeStart('debug', pragmas.debug);
  1858. Check.Check.defined("cartographics", cartographics);
  1859. //>>includeEnd('debug')
  1860. const length = cartographics.length;
  1861. if (!defaultValue.defined(result)) {
  1862. result = new Array(length);
  1863. } else {
  1864. result.length = length;
  1865. }
  1866. for (let i = 0; i < length; i++) {
  1867. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  1868. }
  1869. return result;
  1870. };
  1871. const cartesianToCartographicN = new Cartesian3();
  1872. const cartesianToCartographicP = new Cartesian3();
  1873. const cartesianToCartographicH = new Cartesian3();
  1874. /**
  1875. * Converts the provided cartesian to cartographic representation.
  1876. * The cartesian is undefined at the center of the ellipsoid.
  1877. *
  1878. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1879. * @param {Cartographic} [result] The object onto which to store the result.
  1880. * @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.
  1881. *
  1882. * @example
  1883. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  1884. * const position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  1885. * const cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  1886. */
  1887. Ellipsoid.prototype.cartesianToCartographic = function (cartesian, result) {
  1888. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1889. const p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  1890. if (!defaultValue.defined(p)) {
  1891. return undefined;
  1892. }
  1893. const n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  1894. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  1895. const longitude = Math.atan2(n.y, n.x);
  1896. const latitude = Math.asin(n.z);
  1897. const height =
  1898. Math$1.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1899. if (!defaultValue.defined(result)) {
  1900. return new Cartographic(longitude, latitude, height);
  1901. }
  1902. result.longitude = longitude;
  1903. result.latitude = latitude;
  1904. result.height = height;
  1905. return result;
  1906. };
  1907. /**
  1908. * Converts the provided array of cartesians to an array of cartographics.
  1909. *
  1910. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  1911. * @param {Cartographic[]} [result] The object onto which to store the result.
  1912. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  1913. *
  1914. * @example
  1915. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  1916. * const positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  1917. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  1918. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  1919. * const cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  1920. */
  1921. Ellipsoid.prototype.cartesianArrayToCartographicArray = function (
  1922. cartesians,
  1923. result
  1924. ) {
  1925. //>>includeStart('debug', pragmas.debug);
  1926. Check.Check.defined("cartesians", cartesians);
  1927. //>>includeEnd('debug');
  1928. const length = cartesians.length;
  1929. if (!defaultValue.defined(result)) {
  1930. result = new Array(length);
  1931. } else {
  1932. result.length = length;
  1933. }
  1934. for (let i = 0; i < length; ++i) {
  1935. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  1936. }
  1937. return result;
  1938. };
  1939. /**
  1940. * Scales the provided Cartesian position along the geodetic surface normal
  1941. * so that it is on the surface of this ellipsoid. If the position is
  1942. * at the center of the ellipsoid, this function returns undefined.
  1943. *
  1944. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1945. * @param {Cartesian3} [result] The object onto which to store the result.
  1946. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1947. */
  1948. Ellipsoid.prototype.scaleToGeodeticSurface = function (cartesian, result) {
  1949. return scaleToGeodeticSurface(
  1950. cartesian,
  1951. this._oneOverRadii,
  1952. this._oneOverRadiiSquared,
  1953. this._centerToleranceSquared,
  1954. result
  1955. );
  1956. };
  1957. /**
  1958. * Scales the provided Cartesian position along the geocentric surface normal
  1959. * so that it is on the surface of this ellipsoid.
  1960. *
  1961. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1962. * @param {Cartesian3} [result] The object onto which to store the result.
  1963. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1964. */
  1965. Ellipsoid.prototype.scaleToGeocentricSurface = function (cartesian, result) {
  1966. //>>includeStart('debug', pragmas.debug);
  1967. Check.Check.typeOf.object("cartesian", cartesian);
  1968. //>>includeEnd('debug');
  1969. if (!defaultValue.defined(result)) {
  1970. result = new Cartesian3();
  1971. }
  1972. const positionX = cartesian.x;
  1973. const positionY = cartesian.y;
  1974. const positionZ = cartesian.z;
  1975. const oneOverRadiiSquared = this._oneOverRadiiSquared;
  1976. const beta =
  1977. 1.0 /
  1978. Math.sqrt(
  1979. positionX * positionX * oneOverRadiiSquared.x +
  1980. positionY * positionY * oneOverRadiiSquared.y +
  1981. positionZ * positionZ * oneOverRadiiSquared.z
  1982. );
  1983. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  1984. };
  1985. /**
  1986. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  1987. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  1988. *
  1989. * @param {Cartesian3} position The position to transform.
  1990. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  1991. * return a new instance.
  1992. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  1993. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  1994. */
  1995. Ellipsoid.prototype.transformPositionToScaledSpace = function (
  1996. position,
  1997. result
  1998. ) {
  1999. if (!defaultValue.defined(result)) {
  2000. result = new Cartesian3();
  2001. }
  2002. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  2003. };
  2004. /**
  2005. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  2006. * its components by the result of {@link Ellipsoid#radii}.
  2007. *
  2008. * @param {Cartesian3} position The position to transform.
  2009. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2010. * return a new instance.
  2011. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  2012. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2013. */
  2014. Ellipsoid.prototype.transformPositionFromScaledSpace = function (
  2015. position,
  2016. result
  2017. ) {
  2018. if (!defaultValue.defined(result)) {
  2019. result = new Cartesian3();
  2020. }
  2021. return Cartesian3.multiplyComponents(position, this._radii, result);
  2022. };
  2023. /**
  2024. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  2025. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2026. *
  2027. * @param {Ellipsoid} [right] The other Ellipsoid.
  2028. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2029. */
  2030. Ellipsoid.prototype.equals = function (right) {
  2031. return (
  2032. this === right ||
  2033. (defaultValue.defined(right) && Cartesian3.equals(this._radii, right._radii))
  2034. );
  2035. };
  2036. /**
  2037. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2038. *
  2039. * @returns {string} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2040. */
  2041. Ellipsoid.prototype.toString = function () {
  2042. return this._radii.toString();
  2043. };
  2044. /**
  2045. * Computes a point which is the intersection of the surface normal with the z-axis.
  2046. *
  2047. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  2048. * @param {number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  2049. * 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.
  2050. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  2051. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  2052. * @param {Cartesian3} [result] The cartesian to which to copy the result, or undefined to create and
  2053. * return a new instance.
  2054. * @returns {Cartesian3 | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  2055. *
  2056. * @exception {DeveloperError} position is required.
  2057. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  2058. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  2059. */
  2060. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function (
  2061. position,
  2062. buffer,
  2063. result
  2064. ) {
  2065. //>>includeStart('debug', pragmas.debug);
  2066. Check.Check.typeOf.object("position", position);
  2067. if (
  2068. !Math$1.CesiumMath.equalsEpsilon(
  2069. this._radii.x,
  2070. this._radii.y,
  2071. Math$1.CesiumMath.EPSILON15
  2072. )
  2073. ) {
  2074. throw new Check.DeveloperError(
  2075. "Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)"
  2076. );
  2077. }
  2078. Check.Check.typeOf.number.greaterThan("Ellipsoid.radii.z", this._radii.z, 0);
  2079. //>>includeEnd('debug');
  2080. buffer = defaultValue.defaultValue(buffer, 0.0);
  2081. const squaredXOverSquaredZ = this._squaredXOverSquaredZ;
  2082. if (!defaultValue.defined(result)) {
  2083. result = new Cartesian3();
  2084. }
  2085. result.x = 0.0;
  2086. result.y = 0.0;
  2087. result.z = position.z * (1 - squaredXOverSquaredZ);
  2088. if (Math.abs(result.z) >= this._radii.z - buffer) {
  2089. return undefined;
  2090. }
  2091. return result;
  2092. };
  2093. const abscissas = [
  2094. 0.14887433898163,
  2095. 0.43339539412925,
  2096. 0.67940956829902,
  2097. 0.86506336668898,
  2098. 0.97390652851717,
  2099. 0.0,
  2100. ];
  2101. const weights = [
  2102. 0.29552422471475,
  2103. 0.26926671930999,
  2104. 0.21908636251598,
  2105. 0.14945134915058,
  2106. 0.066671344308684,
  2107. 0.0,
  2108. ];
  2109. /**
  2110. * Compute the 10th order Gauss-Legendre Quadrature of the given definite integral.
  2111. *
  2112. * @param {number} a The lower bound for the integration.
  2113. * @param {number} b The upper bound for the integration.
  2114. * @param {Ellipsoid~RealValuedScalarFunction} func The function to integrate.
  2115. * @returns {number} The value of the integral of the given function over the given domain.
  2116. *
  2117. * @private
  2118. */
  2119. function gaussLegendreQuadrature(a, b, func) {
  2120. //>>includeStart('debug', pragmas.debug);
  2121. Check.Check.typeOf.number("a", a);
  2122. Check.Check.typeOf.number("b", b);
  2123. Check.Check.typeOf.func("func", func);
  2124. //>>includeEnd('debug');
  2125. // The range is half of the normal range since the five weights add to one (ten weights add to two).
  2126. // The values of the abscissas are multiplied by two to account for this.
  2127. const xMean = 0.5 * (b + a);
  2128. const xRange = 0.5 * (b - a);
  2129. let sum = 0.0;
  2130. for (let i = 0; i < 5; i++) {
  2131. const dx = xRange * abscissas[i];
  2132. sum += weights[i] * (func(xMean + dx) + func(xMean - dx));
  2133. }
  2134. // Scale the sum to the range of x.
  2135. sum *= xRange;
  2136. return sum;
  2137. }
  2138. /**
  2139. * A real valued scalar function.
  2140. * @callback Ellipsoid~RealValuedScalarFunction
  2141. *
  2142. * @param {number} x The value used to evaluate the function.
  2143. * @returns {number} The value of the function at x.
  2144. *
  2145. * @private
  2146. */
  2147. /**
  2148. * Computes an approximation of the surface area of a rectangle on the surface of an ellipsoid using
  2149. * Gauss-Legendre 10th order quadrature.
  2150. *
  2151. * @param {Rectangle} rectangle The rectangle used for computing the surface area.
  2152. * @returns {number} The approximate area of the rectangle on the surface of this ellipsoid.
  2153. */
  2154. Ellipsoid.prototype.surfaceArea = function (rectangle) {
  2155. //>>includeStart('debug', pragmas.debug);
  2156. Check.Check.typeOf.object("rectangle", rectangle);
  2157. //>>includeEnd('debug');
  2158. const minLongitude = rectangle.west;
  2159. let maxLongitude = rectangle.east;
  2160. const minLatitude = rectangle.south;
  2161. const maxLatitude = rectangle.north;
  2162. while (maxLongitude < minLongitude) {
  2163. maxLongitude += Math$1.CesiumMath.TWO_PI;
  2164. }
  2165. const radiiSquared = this._radiiSquared;
  2166. const a2 = radiiSquared.x;
  2167. const b2 = radiiSquared.y;
  2168. const c2 = radiiSquared.z;
  2169. const a2b2 = a2 * b2;
  2170. return gaussLegendreQuadrature(minLatitude, maxLatitude, function (lat) {
  2171. // phi represents the angle measured from the north pole
  2172. // sin(phi) = sin(pi / 2 - lat) = cos(lat), cos(phi) is similar
  2173. const sinPhi = Math.cos(lat);
  2174. const cosPhi = Math.sin(lat);
  2175. return (
  2176. Math.cos(lat) *
  2177. gaussLegendreQuadrature(minLongitude, maxLongitude, function (lon) {
  2178. const cosTheta = Math.cos(lon);
  2179. const sinTheta = Math.sin(lon);
  2180. return Math.sqrt(
  2181. a2b2 * cosPhi * cosPhi +
  2182. c2 *
  2183. (b2 * cosTheta * cosTheta + a2 * sinTheta * sinTheta) *
  2184. sinPhi *
  2185. sinPhi
  2186. );
  2187. })
  2188. );
  2189. });
  2190. };
  2191. /**
  2192. * A 3x3 matrix, indexable as a column-major order array.
  2193. * Constructor parameters are in row-major order for code readability.
  2194. * @alias Matrix3
  2195. * @constructor
  2196. * @implements {ArrayLike<number>}
  2197. *
  2198. * @param {number} [column0Row0=0.0] The value for column 0, row 0.
  2199. * @param {number} [column1Row0=0.0] The value for column 1, row 0.
  2200. * @param {number} [column2Row0=0.0] The value for column 2, row 0.
  2201. * @param {number} [column0Row1=0.0] The value for column 0, row 1.
  2202. * @param {number} [column1Row1=0.0] The value for column 1, row 1.
  2203. * @param {number} [column2Row1=0.0] The value for column 2, row 1.
  2204. * @param {number} [column0Row2=0.0] The value for column 0, row 2.
  2205. * @param {number} [column1Row2=0.0] The value for column 1, row 2.
  2206. * @param {number} [column2Row2=0.0] The value for column 2, row 2.
  2207. *
  2208. * @see Matrix3.fromArray
  2209. * @see Matrix3.fromColumnMajorArray
  2210. * @see Matrix3.fromRowMajorArray
  2211. * @see Matrix3.fromQuaternion
  2212. * @see Matrix3.fromHeadingPitchRoll
  2213. * @see Matrix3.fromScale
  2214. * @see Matrix3.fromUniformScale
  2215. * @see Matrix3.fromCrossProduct
  2216. * @see Matrix3.fromRotationX
  2217. * @see Matrix3.fromRotationY
  2218. * @see Matrix3.fromRotationZ
  2219. * @see Matrix2
  2220. * @see Matrix4
  2221. */
  2222. function Matrix3(
  2223. column0Row0,
  2224. column1Row0,
  2225. column2Row0,
  2226. column0Row1,
  2227. column1Row1,
  2228. column2Row1,
  2229. column0Row2,
  2230. column1Row2,
  2231. column2Row2
  2232. ) {
  2233. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  2234. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  2235. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  2236. this[3] = defaultValue.defaultValue(column1Row0, 0.0);
  2237. this[4] = defaultValue.defaultValue(column1Row1, 0.0);
  2238. this[5] = defaultValue.defaultValue(column1Row2, 0.0);
  2239. this[6] = defaultValue.defaultValue(column2Row0, 0.0);
  2240. this[7] = defaultValue.defaultValue(column2Row1, 0.0);
  2241. this[8] = defaultValue.defaultValue(column2Row2, 0.0);
  2242. }
  2243. /**
  2244. * The number of elements used to pack the object into an array.
  2245. * @type {number}
  2246. */
  2247. Matrix3.packedLength = 9;
  2248. /**
  2249. * Stores the provided instance into the provided array.
  2250. *
  2251. * @param {Matrix3} value The value to pack.
  2252. * @param {number[]} array The array to pack into.
  2253. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  2254. *
  2255. * @returns {number[]} The array that was packed into
  2256. */
  2257. Matrix3.pack = function (value, array, startingIndex) {
  2258. //>>includeStart('debug', pragmas.debug);
  2259. Check.Check.typeOf.object("value", value);
  2260. Check.Check.defined("array", array);
  2261. //>>includeEnd('debug');
  2262. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2263. array[startingIndex++] = value[0];
  2264. array[startingIndex++] = value[1];
  2265. array[startingIndex++] = value[2];
  2266. array[startingIndex++] = value[3];
  2267. array[startingIndex++] = value[4];
  2268. array[startingIndex++] = value[5];
  2269. array[startingIndex++] = value[6];
  2270. array[startingIndex++] = value[7];
  2271. array[startingIndex++] = value[8];
  2272. return array;
  2273. };
  2274. /**
  2275. * Retrieves an instance from a packed array.
  2276. *
  2277. * @param {number[]} array The packed array.
  2278. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  2279. * @param {Matrix3} [result] The object into which to store the result.
  2280. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2281. */
  2282. Matrix3.unpack = function (array, startingIndex, result) {
  2283. //>>includeStart('debug', pragmas.debug);
  2284. Check.Check.defined("array", array);
  2285. //>>includeEnd('debug');
  2286. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2287. if (!defaultValue.defined(result)) {
  2288. result = new Matrix3();
  2289. }
  2290. result[0] = array[startingIndex++];
  2291. result[1] = array[startingIndex++];
  2292. result[2] = array[startingIndex++];
  2293. result[3] = array[startingIndex++];
  2294. result[4] = array[startingIndex++];
  2295. result[5] = array[startingIndex++];
  2296. result[6] = array[startingIndex++];
  2297. result[7] = array[startingIndex++];
  2298. result[8] = array[startingIndex++];
  2299. return result;
  2300. };
  2301. /**
  2302. * Flattens an array of Matrix3s into an array of components. The components
  2303. * are stored in column-major order.
  2304. *
  2305. * @param {Matrix3[]} array The array of matrices to pack.
  2306. * @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.
  2307. * @returns {number[]} The packed array.
  2308. */
  2309. Matrix3.packArray = function (array, result) {
  2310. //>>includeStart('debug', pragmas.debug);
  2311. Check.Check.defined("array", array);
  2312. //>>includeEnd('debug');
  2313. const length = array.length;
  2314. const resultLength = length * 9;
  2315. if (!defaultValue.defined(result)) {
  2316. result = new Array(resultLength);
  2317. } else if (!Array.isArray(result) && result.length !== resultLength) {
  2318. //>>includeStart('debug', pragmas.debug);
  2319. throw new Check.DeveloperError(
  2320. "If result is a typed array, it must have exactly array.length * 9 elements"
  2321. );
  2322. //>>includeEnd('debug');
  2323. } else if (result.length !== resultLength) {
  2324. result.length = resultLength;
  2325. }
  2326. for (let i = 0; i < length; ++i) {
  2327. Matrix3.pack(array[i], result, i * 9);
  2328. }
  2329. return result;
  2330. };
  2331. /**
  2332. * Unpacks an array of column-major matrix components into an array of Matrix3s.
  2333. *
  2334. * @param {number[]} array The array of components to unpack.
  2335. * @param {Matrix3[]} [result] The array onto which to store the result.
  2336. * @returns {Matrix3[]} The unpacked array.
  2337. */
  2338. Matrix3.unpackArray = function (array, result) {
  2339. //>>includeStart('debug', pragmas.debug);
  2340. Check.Check.defined("array", array);
  2341. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 9);
  2342. if (array.length % 9 !== 0) {
  2343. throw new Check.DeveloperError("array length must be a multiple of 9.");
  2344. }
  2345. //>>includeEnd('debug');
  2346. const length = array.length;
  2347. if (!defaultValue.defined(result)) {
  2348. result = new Array(length / 9);
  2349. } else {
  2350. result.length = length / 9;
  2351. }
  2352. for (let i = 0; i < length; i += 9) {
  2353. const index = i / 9;
  2354. result[index] = Matrix3.unpack(array, i, result[index]);
  2355. }
  2356. return result;
  2357. };
  2358. /**
  2359. * Duplicates a Matrix3 instance.
  2360. *
  2361. * @param {Matrix3} matrix The matrix to duplicate.
  2362. * @param {Matrix3} [result] The object onto which to store the result.
  2363. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  2364. */
  2365. Matrix3.clone = function (matrix, result) {
  2366. if (!defaultValue.defined(matrix)) {
  2367. return undefined;
  2368. }
  2369. if (!defaultValue.defined(result)) {
  2370. return new Matrix3(
  2371. matrix[0],
  2372. matrix[3],
  2373. matrix[6],
  2374. matrix[1],
  2375. matrix[4],
  2376. matrix[7],
  2377. matrix[2],
  2378. matrix[5],
  2379. matrix[8]
  2380. );
  2381. }
  2382. result[0] = matrix[0];
  2383. result[1] = matrix[1];
  2384. result[2] = matrix[2];
  2385. result[3] = matrix[3];
  2386. result[4] = matrix[4];
  2387. result[5] = matrix[5];
  2388. result[6] = matrix[6];
  2389. result[7] = matrix[7];
  2390. result[8] = matrix[8];
  2391. return result;
  2392. };
  2393. /**
  2394. * Creates a Matrix3 from 9 consecutive elements in an array.
  2395. *
  2396. * @function
  2397. * @param {number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  2398. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  2399. * @param {Matrix3} [result] The object onto which to store the result.
  2400. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2401. *
  2402. * @example
  2403. * // Create the Matrix3:
  2404. * // [1.0, 2.0, 3.0]
  2405. * // [1.0, 2.0, 3.0]
  2406. * // [1.0, 2.0, 3.0]
  2407. *
  2408. * const v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2409. * const m = Cesium.Matrix3.fromArray(v);
  2410. *
  2411. * // Create same Matrix3 with using an offset into an array
  2412. * const v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2413. * const m2 = Cesium.Matrix3.fromArray(v2, 2);
  2414. */
  2415. Matrix3.fromArray = Matrix3.unpack;
  2416. /**
  2417. * Creates a Matrix3 instance from a column-major order array.
  2418. *
  2419. * @param {number[]} values The column-major order array.
  2420. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2421. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2422. */
  2423. Matrix3.fromColumnMajorArray = function (values, result) {
  2424. //>>includeStart('debug', pragmas.debug);
  2425. Check.Check.defined("values", values);
  2426. //>>includeEnd('debug');
  2427. return Matrix3.clone(values, result);
  2428. };
  2429. /**
  2430. * Creates a Matrix3 instance from a row-major order array.
  2431. * The resulting matrix will be in column-major order.
  2432. *
  2433. * @param {number[]} values The row-major order array.
  2434. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2435. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2436. */
  2437. Matrix3.fromRowMajorArray = function (values, result) {
  2438. //>>includeStart('debug', pragmas.debug);
  2439. Check.Check.defined("values", values);
  2440. //>>includeEnd('debug');
  2441. if (!defaultValue.defined(result)) {
  2442. return new Matrix3(
  2443. values[0],
  2444. values[1],
  2445. values[2],
  2446. values[3],
  2447. values[4],
  2448. values[5],
  2449. values[6],
  2450. values[7],
  2451. values[8]
  2452. );
  2453. }
  2454. result[0] = values[0];
  2455. result[1] = values[3];
  2456. result[2] = values[6];
  2457. result[3] = values[1];
  2458. result[4] = values[4];
  2459. result[5] = values[7];
  2460. result[6] = values[2];
  2461. result[7] = values[5];
  2462. result[8] = values[8];
  2463. return result;
  2464. };
  2465. /**
  2466. * Computes a 3x3 rotation matrix from the provided quaternion.
  2467. *
  2468. * @param {Quaternion} quaternion the quaternion to use.
  2469. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2470. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  2471. */
  2472. Matrix3.fromQuaternion = function (quaternion, result) {
  2473. //>>includeStart('debug', pragmas.debug);
  2474. Check.Check.typeOf.object("quaternion", quaternion);
  2475. //>>includeEnd('debug');
  2476. const x2 = quaternion.x * quaternion.x;
  2477. const xy = quaternion.x * quaternion.y;
  2478. const xz = quaternion.x * quaternion.z;
  2479. const xw = quaternion.x * quaternion.w;
  2480. const y2 = quaternion.y * quaternion.y;
  2481. const yz = quaternion.y * quaternion.z;
  2482. const yw = quaternion.y * quaternion.w;
  2483. const z2 = quaternion.z * quaternion.z;
  2484. const zw = quaternion.z * quaternion.w;
  2485. const w2 = quaternion.w * quaternion.w;
  2486. const m00 = x2 - y2 - z2 + w2;
  2487. const m01 = 2.0 * (xy - zw);
  2488. const m02 = 2.0 * (xz + yw);
  2489. const m10 = 2.0 * (xy + zw);
  2490. const m11 = -x2 + y2 - z2 + w2;
  2491. const m12 = 2.0 * (yz - xw);
  2492. const m20 = 2.0 * (xz - yw);
  2493. const m21 = 2.0 * (yz + xw);
  2494. const m22 = -x2 - y2 + z2 + w2;
  2495. if (!defaultValue.defined(result)) {
  2496. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2497. }
  2498. result[0] = m00;
  2499. result[1] = m10;
  2500. result[2] = m20;
  2501. result[3] = m01;
  2502. result[4] = m11;
  2503. result[5] = m21;
  2504. result[6] = m02;
  2505. result[7] = m12;
  2506. result[8] = m22;
  2507. return result;
  2508. };
  2509. /**
  2510. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  2511. *
  2512. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  2513. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2514. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  2515. */
  2516. Matrix3.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  2517. //>>includeStart('debug', pragmas.debug);
  2518. Check.Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  2519. //>>includeEnd('debug');
  2520. const cosTheta = Math.cos(-headingPitchRoll.pitch);
  2521. const cosPsi = Math.cos(-headingPitchRoll.heading);
  2522. const cosPhi = Math.cos(headingPitchRoll.roll);
  2523. const sinTheta = Math.sin(-headingPitchRoll.pitch);
  2524. const sinPsi = Math.sin(-headingPitchRoll.heading);
  2525. const sinPhi = Math.sin(headingPitchRoll.roll);
  2526. const m00 = cosTheta * cosPsi;
  2527. const m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  2528. const m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  2529. const m10 = cosTheta * sinPsi;
  2530. const m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  2531. const m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  2532. const m20 = -sinTheta;
  2533. const m21 = sinPhi * cosTheta;
  2534. const m22 = cosPhi * cosTheta;
  2535. if (!defaultValue.defined(result)) {
  2536. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2537. }
  2538. result[0] = m00;
  2539. result[1] = m10;
  2540. result[2] = m20;
  2541. result[3] = m01;
  2542. result[4] = m11;
  2543. result[5] = m21;
  2544. result[6] = m02;
  2545. result[7] = m12;
  2546. result[8] = m22;
  2547. return result;
  2548. };
  2549. /**
  2550. * Computes a Matrix3 instance representing a non-uniform scale.
  2551. *
  2552. * @param {Cartesian3} scale The x, y, and z scale factors.
  2553. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2554. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2555. *
  2556. * @example
  2557. * // Creates
  2558. * // [7.0, 0.0, 0.0]
  2559. * // [0.0, 8.0, 0.0]
  2560. * // [0.0, 0.0, 9.0]
  2561. * const m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2562. */
  2563. Matrix3.fromScale = function (scale, result) {
  2564. //>>includeStart('debug', pragmas.debug);
  2565. Check.Check.typeOf.object("scale", scale);
  2566. //>>includeEnd('debug');
  2567. if (!defaultValue.defined(result)) {
  2568. return new Matrix3(scale.x, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, scale.z);
  2569. }
  2570. result[0] = scale.x;
  2571. result[1] = 0.0;
  2572. result[2] = 0.0;
  2573. result[3] = 0.0;
  2574. result[4] = scale.y;
  2575. result[5] = 0.0;
  2576. result[6] = 0.0;
  2577. result[7] = 0.0;
  2578. result[8] = scale.z;
  2579. return result;
  2580. };
  2581. /**
  2582. * Computes a Matrix3 instance representing a uniform scale.
  2583. *
  2584. * @param {number} scale The uniform scale factor.
  2585. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2586. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2587. *
  2588. * @example
  2589. * // Creates
  2590. * // [2.0, 0.0, 0.0]
  2591. * // [0.0, 2.0, 0.0]
  2592. * // [0.0, 0.0, 2.0]
  2593. * const m = Cesium.Matrix3.fromUniformScale(2.0);
  2594. */
  2595. Matrix3.fromUniformScale = function (scale, result) {
  2596. //>>includeStart('debug', pragmas.debug);
  2597. Check.Check.typeOf.number("scale", scale);
  2598. //>>includeEnd('debug');
  2599. if (!defaultValue.defined(result)) {
  2600. return new Matrix3(scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, scale);
  2601. }
  2602. result[0] = scale;
  2603. result[1] = 0.0;
  2604. result[2] = 0.0;
  2605. result[3] = 0.0;
  2606. result[4] = scale;
  2607. result[5] = 0.0;
  2608. result[6] = 0.0;
  2609. result[7] = 0.0;
  2610. result[8] = scale;
  2611. return result;
  2612. };
  2613. /**
  2614. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  2615. *
  2616. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  2617. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2618. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2619. *
  2620. * @example
  2621. * // Creates
  2622. * // [0.0, -9.0, 8.0]
  2623. * // [9.0, 0.0, -7.0]
  2624. * // [-8.0, 7.0, 0.0]
  2625. * const m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2626. */
  2627. Matrix3.fromCrossProduct = function (vector, result) {
  2628. //>>includeStart('debug', pragmas.debug);
  2629. Check.Check.typeOf.object("vector", vector);
  2630. //>>includeEnd('debug');
  2631. if (!defaultValue.defined(result)) {
  2632. return new Matrix3(
  2633. 0.0,
  2634. -vector.z,
  2635. vector.y,
  2636. vector.z,
  2637. 0.0,
  2638. -vector.x,
  2639. -vector.y,
  2640. vector.x,
  2641. 0.0
  2642. );
  2643. }
  2644. result[0] = 0.0;
  2645. result[1] = vector.z;
  2646. result[2] = -vector.y;
  2647. result[3] = -vector.z;
  2648. result[4] = 0.0;
  2649. result[5] = vector.x;
  2650. result[6] = vector.y;
  2651. result[7] = -vector.x;
  2652. result[8] = 0.0;
  2653. return result;
  2654. };
  2655. /**
  2656. * Creates a rotation matrix around the x-axis.
  2657. *
  2658. * @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2659. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2660. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2661. *
  2662. * @example
  2663. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  2664. * const p = new Cesium.Cartesian3(5, 6, 7);
  2665. * const m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  2666. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2667. */
  2668. Matrix3.fromRotationX = function (angle, result) {
  2669. //>>includeStart('debug', pragmas.debug);
  2670. Check.Check.typeOf.number("angle", angle);
  2671. //>>includeEnd('debug');
  2672. const cosAngle = Math.cos(angle);
  2673. const sinAngle = Math.sin(angle);
  2674. if (!defaultValue.defined(result)) {
  2675. return new Matrix3(
  2676. 1.0,
  2677. 0.0,
  2678. 0.0,
  2679. 0.0,
  2680. cosAngle,
  2681. -sinAngle,
  2682. 0.0,
  2683. sinAngle,
  2684. cosAngle
  2685. );
  2686. }
  2687. result[0] = 1.0;
  2688. result[1] = 0.0;
  2689. result[2] = 0.0;
  2690. result[3] = 0.0;
  2691. result[4] = cosAngle;
  2692. result[5] = sinAngle;
  2693. result[6] = 0.0;
  2694. result[7] = -sinAngle;
  2695. result[8] = cosAngle;
  2696. return result;
  2697. };
  2698. /**
  2699. * Creates a rotation matrix around the y-axis.
  2700. *
  2701. * @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2702. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2703. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2704. *
  2705. * @example
  2706. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  2707. * const p = new Cesium.Cartesian3(5, 6, 7);
  2708. * const m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  2709. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2710. */
  2711. Matrix3.fromRotationY = function (angle, result) {
  2712. //>>includeStart('debug', pragmas.debug);
  2713. Check.Check.typeOf.number("angle", angle);
  2714. //>>includeEnd('debug');
  2715. const cosAngle = Math.cos(angle);
  2716. const sinAngle = Math.sin(angle);
  2717. if (!defaultValue.defined(result)) {
  2718. return new Matrix3(
  2719. cosAngle,
  2720. 0.0,
  2721. sinAngle,
  2722. 0.0,
  2723. 1.0,
  2724. 0.0,
  2725. -sinAngle,
  2726. 0.0,
  2727. cosAngle
  2728. );
  2729. }
  2730. result[0] = cosAngle;
  2731. result[1] = 0.0;
  2732. result[2] = -sinAngle;
  2733. result[3] = 0.0;
  2734. result[4] = 1.0;
  2735. result[5] = 0.0;
  2736. result[6] = sinAngle;
  2737. result[7] = 0.0;
  2738. result[8] = cosAngle;
  2739. return result;
  2740. };
  2741. /**
  2742. * Creates a rotation matrix around the z-axis.
  2743. *
  2744. * @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2745. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2746. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2747. *
  2748. * @example
  2749. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  2750. * const p = new Cesium.Cartesian3(5, 6, 7);
  2751. * const m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  2752. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2753. */
  2754. Matrix3.fromRotationZ = function (angle, result) {
  2755. //>>includeStart('debug', pragmas.debug);
  2756. Check.Check.typeOf.number("angle", angle);
  2757. //>>includeEnd('debug');
  2758. const cosAngle = Math.cos(angle);
  2759. const sinAngle = Math.sin(angle);
  2760. if (!defaultValue.defined(result)) {
  2761. return new Matrix3(
  2762. cosAngle,
  2763. -sinAngle,
  2764. 0.0,
  2765. sinAngle,
  2766. cosAngle,
  2767. 0.0,
  2768. 0.0,
  2769. 0.0,
  2770. 1.0
  2771. );
  2772. }
  2773. result[0] = cosAngle;
  2774. result[1] = sinAngle;
  2775. result[2] = 0.0;
  2776. result[3] = -sinAngle;
  2777. result[4] = cosAngle;
  2778. result[5] = 0.0;
  2779. result[6] = 0.0;
  2780. result[7] = 0.0;
  2781. result[8] = 1.0;
  2782. return result;
  2783. };
  2784. /**
  2785. * Creates an Array from the provided Matrix3 instance.
  2786. * The array will be in column-major order.
  2787. *
  2788. * @param {Matrix3} matrix The matrix to use..
  2789. * @param {number[]} [result] The Array onto which to store the result.
  2790. * @returns {number[]} The modified Array parameter or a new Array instance if one was not provided.
  2791. */
  2792. Matrix3.toArray = function (matrix, result) {
  2793. //>>includeStart('debug', pragmas.debug);
  2794. Check.Check.typeOf.object("matrix", matrix);
  2795. //>>includeEnd('debug');
  2796. if (!defaultValue.defined(result)) {
  2797. return [
  2798. matrix[0],
  2799. matrix[1],
  2800. matrix[2],
  2801. matrix[3],
  2802. matrix[4],
  2803. matrix[5],
  2804. matrix[6],
  2805. matrix[7],
  2806. matrix[8],
  2807. ];
  2808. }
  2809. result[0] = matrix[0];
  2810. result[1] = matrix[1];
  2811. result[2] = matrix[2];
  2812. result[3] = matrix[3];
  2813. result[4] = matrix[4];
  2814. result[5] = matrix[5];
  2815. result[6] = matrix[6];
  2816. result[7] = matrix[7];
  2817. result[8] = matrix[8];
  2818. return result;
  2819. };
  2820. /**
  2821. * Computes the array index of the element at the provided row and column.
  2822. *
  2823. * @param {number} column The zero-based index of the column.
  2824. * @param {number} row The zero-based index of the row.
  2825. * @returns {number} The index of the element at the provided row and column.
  2826. *
  2827. * @exception {DeveloperError} row must be 0, 1, or 2.
  2828. * @exception {DeveloperError} column must be 0, 1, or 2.
  2829. *
  2830. * @example
  2831. * const myMatrix = new Cesium.Matrix3();
  2832. * const column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  2833. * const column1Row0 = myMatrix[column1Row0Index]
  2834. * myMatrix[column1Row0Index] = 10.0;
  2835. */
  2836. Matrix3.getElementIndex = function (column, row) {
  2837. //>>includeStart('debug', pragmas.debug);
  2838. Check.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  2839. Check.Check.typeOf.number.lessThanOrEquals("row", row, 2);
  2840. Check.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  2841. Check.Check.typeOf.number.lessThanOrEquals("column", column, 2);
  2842. //>>includeEnd('debug');
  2843. return column * 3 + row;
  2844. };
  2845. /**
  2846. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  2847. *
  2848. * @param {Matrix3} matrix The matrix to use.
  2849. * @param {number} index The zero-based index of the column to retrieve.
  2850. * @param {Cartesian3} result The object onto which to store the result.
  2851. * @returns {Cartesian3} The modified result parameter.
  2852. *
  2853. * @exception {DeveloperError} index must be 0, 1, or 2.
  2854. */
  2855. Matrix3.getColumn = function (matrix, index, result) {
  2856. //>>includeStart('debug', pragmas.debug);
  2857. Check.Check.typeOf.object("matrix", matrix);
  2858. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2859. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2860. Check.Check.typeOf.object("result", result);
  2861. //>>includeEnd('debug');
  2862. const startIndex = index * 3;
  2863. const x = matrix[startIndex];
  2864. const y = matrix[startIndex + 1];
  2865. const z = matrix[startIndex + 2];
  2866. result.x = x;
  2867. result.y = y;
  2868. result.z = z;
  2869. return result;
  2870. };
  2871. /**
  2872. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  2873. *
  2874. * @param {Matrix3} matrix The matrix to use.
  2875. * @param {number} index The zero-based index of the column to set.
  2876. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  2877. * @param {Matrix3} result The object onto which to store the result.
  2878. * @returns {Matrix3} The modified result parameter.
  2879. *
  2880. * @exception {DeveloperError} index must be 0, 1, or 2.
  2881. */
  2882. Matrix3.setColumn = function (matrix, index, cartesian, result) {
  2883. //>>includeStart('debug', pragmas.debug);
  2884. Check.Check.typeOf.object("matrix", matrix);
  2885. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2886. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2887. Check.Check.typeOf.object("cartesian", cartesian);
  2888. Check.Check.typeOf.object("result", result);
  2889. //>>includeEnd('debug');
  2890. result = Matrix3.clone(matrix, result);
  2891. const startIndex = index * 3;
  2892. result[startIndex] = cartesian.x;
  2893. result[startIndex + 1] = cartesian.y;
  2894. result[startIndex + 2] = cartesian.z;
  2895. return result;
  2896. };
  2897. /**
  2898. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  2899. *
  2900. * @param {Matrix3} matrix The matrix to use.
  2901. * @param {number} index The zero-based index of the row to retrieve.
  2902. * @param {Cartesian3} result The object onto which to store the result.
  2903. * @returns {Cartesian3} The modified result parameter.
  2904. *
  2905. * @exception {DeveloperError} index must be 0, 1, or 2.
  2906. */
  2907. Matrix3.getRow = function (matrix, index, result) {
  2908. //>>includeStart('debug', pragmas.debug);
  2909. Check.Check.typeOf.object("matrix", matrix);
  2910. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2911. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2912. Check.Check.typeOf.object("result", result);
  2913. //>>includeEnd('debug');
  2914. const x = matrix[index];
  2915. const y = matrix[index + 3];
  2916. const z = matrix[index + 6];
  2917. result.x = x;
  2918. result.y = y;
  2919. result.z = z;
  2920. return result;
  2921. };
  2922. /**
  2923. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  2924. *
  2925. * @param {Matrix3} matrix The matrix to use.
  2926. * @param {number} index The zero-based index of the row to set.
  2927. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  2928. * @param {Matrix3} result The object onto which to store the result.
  2929. * @returns {Matrix3} The modified result parameter.
  2930. *
  2931. * @exception {DeveloperError} index must be 0, 1, or 2.
  2932. */
  2933. Matrix3.setRow = function (matrix, index, cartesian, result) {
  2934. //>>includeStart('debug', pragmas.debug);
  2935. Check.Check.typeOf.object("matrix", matrix);
  2936. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2937. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2938. Check.Check.typeOf.object("cartesian", cartesian);
  2939. Check.Check.typeOf.object("result", result);
  2940. //>>includeEnd('debug');
  2941. result = Matrix3.clone(matrix, result);
  2942. result[index] = cartesian.x;
  2943. result[index + 3] = cartesian.y;
  2944. result[index + 6] = cartesian.z;
  2945. return result;
  2946. };
  2947. const scaleScratch1 = new Cartesian3();
  2948. /**
  2949. * Computes a new matrix that replaces the scale with the provided scale.
  2950. * This assumes the matrix is an affine transformation.
  2951. *
  2952. * @param {Matrix3} matrix The matrix to use.
  2953. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  2954. * @param {Matrix3} result The object onto which to store the result.
  2955. * @returns {Matrix3} The modified result parameter.
  2956. *
  2957. * @see Matrix3.setUniformScale
  2958. * @see Matrix3.fromScale
  2959. * @see Matrix3.fromUniformScale
  2960. * @see Matrix3.multiplyByScale
  2961. * @see Matrix3.multiplyByUniformScale
  2962. * @see Matrix3.getScale
  2963. */
  2964. Matrix3.setScale = function (matrix, scale, result) {
  2965. //>>includeStart('debug', pragmas.debug);
  2966. Check.Check.typeOf.object("matrix", matrix);
  2967. Check.Check.typeOf.object("scale", scale);
  2968. Check.Check.typeOf.object("result", result);
  2969. //>>includeEnd('debug');
  2970. const existingScale = Matrix3.getScale(matrix, scaleScratch1);
  2971. const scaleRatioX = scale.x / existingScale.x;
  2972. const scaleRatioY = scale.y / existingScale.y;
  2973. const scaleRatioZ = scale.z / existingScale.z;
  2974. result[0] = matrix[0] * scaleRatioX;
  2975. result[1] = matrix[1] * scaleRatioX;
  2976. result[2] = matrix[2] * scaleRatioX;
  2977. result[3] = matrix[3] * scaleRatioY;
  2978. result[4] = matrix[4] * scaleRatioY;
  2979. result[5] = matrix[5] * scaleRatioY;
  2980. result[6] = matrix[6] * scaleRatioZ;
  2981. result[7] = matrix[7] * scaleRatioZ;
  2982. result[8] = matrix[8] * scaleRatioZ;
  2983. return result;
  2984. };
  2985. const scaleScratch2 = new Cartesian3();
  2986. /**
  2987. * Computes a new matrix that replaces the scale with the provided uniform scale.
  2988. * This assumes the matrix is an affine transformation.
  2989. *
  2990. * @param {Matrix3} matrix The matrix to use.
  2991. * @param {number} scale The uniform scale that replaces the scale of the provided matrix.
  2992. * @param {Matrix3} result The object onto which to store the result.
  2993. * @returns {Matrix3} The modified result parameter.
  2994. *
  2995. * @see Matrix3.setScale
  2996. * @see Matrix3.fromScale
  2997. * @see Matrix3.fromUniformScale
  2998. * @see Matrix3.multiplyByScale
  2999. * @see Matrix3.multiplyByUniformScale
  3000. * @see Matrix3.getScale
  3001. */
  3002. Matrix3.setUniformScale = function (matrix, scale, result) {
  3003. //>>includeStart('debug', pragmas.debug);
  3004. Check.Check.typeOf.object("matrix", matrix);
  3005. Check.Check.typeOf.number("scale", scale);
  3006. Check.Check.typeOf.object("result", result);
  3007. //>>includeEnd('debug');
  3008. const existingScale = Matrix3.getScale(matrix, scaleScratch2);
  3009. const scaleRatioX = scale / existingScale.x;
  3010. const scaleRatioY = scale / existingScale.y;
  3011. const scaleRatioZ = scale / existingScale.z;
  3012. result[0] = matrix[0] * scaleRatioX;
  3013. result[1] = matrix[1] * scaleRatioX;
  3014. result[2] = matrix[2] * scaleRatioX;
  3015. result[3] = matrix[3] * scaleRatioY;
  3016. result[4] = matrix[4] * scaleRatioY;
  3017. result[5] = matrix[5] * scaleRatioY;
  3018. result[6] = matrix[6] * scaleRatioZ;
  3019. result[7] = matrix[7] * scaleRatioZ;
  3020. result[8] = matrix[8] * scaleRatioZ;
  3021. return result;
  3022. };
  3023. const scratchColumn = new Cartesian3();
  3024. /**
  3025. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  3026. *
  3027. * @param {Matrix3} matrix The matrix.
  3028. * @param {Cartesian3} result The object onto which to store the result.
  3029. * @returns {Cartesian3} The modified result parameter.
  3030. *
  3031. * @see Matrix3.multiplyByScale
  3032. * @see Matrix3.multiplyByUniformScale
  3033. * @see Matrix3.fromScale
  3034. * @see Matrix3.fromUniformScale
  3035. * @see Matrix3.setScale
  3036. * @see Matrix3.setUniformScale
  3037. */
  3038. Matrix3.getScale = function (matrix, result) {
  3039. //>>includeStart('debug', pragmas.debug);
  3040. Check.Check.typeOf.object("matrix", matrix);
  3041. Check.Check.typeOf.object("result", result);
  3042. //>>includeEnd('debug');
  3043. result.x = Cartesian3.magnitude(
  3044. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn)
  3045. );
  3046. result.y = Cartesian3.magnitude(
  3047. Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn)
  3048. );
  3049. result.z = Cartesian3.magnitude(
  3050. Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn)
  3051. );
  3052. return result;
  3053. };
  3054. const scaleScratch3 = new Cartesian3();
  3055. /**
  3056. * Computes the maximum scale assuming the matrix is an affine transformation.
  3057. * The maximum scale is the maximum length of the column vectors.
  3058. *
  3059. * @param {Matrix3} matrix The matrix.
  3060. * @returns {number} The maximum scale.
  3061. */
  3062. Matrix3.getMaximumScale = function (matrix) {
  3063. Matrix3.getScale(matrix, scaleScratch3);
  3064. return Cartesian3.maximumComponent(scaleScratch3);
  3065. };
  3066. const scaleScratch4 = new Cartesian3();
  3067. /**
  3068. * Sets the rotation assuming the matrix is an affine transformation.
  3069. *
  3070. * @param {Matrix3} matrix The matrix.
  3071. * @param {Matrix3} rotation The rotation matrix.
  3072. * @param {Matrix3} result The object onto which to store the result.
  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. Check.Check.typeOf.object("matrix", matrix);
  3080. Check.Check.typeOf.object("result", result);
  3081. //>>includeEnd('debug');
  3082. const scale = Matrix3.getScale(matrix, scaleScratch4);
  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 = 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. Check.Check.typeOf.object("matrix", matrix);
  3107. Check.Check.typeOf.object("result", result);
  3108. //>>includeEnd('debug');
  3109. const scale = Matrix3.getScale(matrix, scaleScratch5);
  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. Check.Check.typeOf.object("left", left);
  3132. Check.Check.typeOf.object("right", right);
  3133. Check.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. Check.Check.typeOf.object("left", left);
  3175. Check.Check.typeOf.object("right", right);
  3176. Check.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. Check.Check.typeOf.object("left", left);
  3200. Check.Check.typeOf.object("right", right);
  3201. Check.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. Check.Check.typeOf.object("matrix", matrix);
  3225. Check.Check.typeOf.object("cartesian", cartesian);
  3226. Check.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. Check.Check.typeOf.object("matrix", matrix);
  3250. Check.Check.typeOf.number("scalar", scalar);
  3251. Check.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 {Cartesian3} 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. Check.Check.typeOf.object("matrix", matrix);
  3287. Check.Check.typeOf.object("scale", scale);
  3288. Check.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. Check.Check.typeOf.object("matrix", matrix);
  3323. Check.Check.typeOf.number("scale", scale);
  3324. Check.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. Check.Check.typeOf.object("matrix", matrix);
  3347. Check.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. Check.Check.typeOf.object("matrix", matrix);
  3370. Check.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 = Math$1.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. Check.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 = Math$1.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. Check.Check.typeOf.object("matrix", matrix);
  3533. Check.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. Check.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. Check.Check.typeOf.object("matrix", matrix);
  3583. Check.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) <= Math$1.CesiumMath.EPSILON15) {
  3597. throw new Check.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 = 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. Check.Check.typeOf.object("matrix", matrix);
  3623. Check.Check.typeOf.object("result", result);
  3624. //>>includeEnd('debug');
  3625. return Matrix3.inverse(
  3626. Matrix3.transpose(matrix, scratchTransposeMatrix),
  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. exports.Cartesian3 = Cartesian3;
  3836. exports.Cartographic = Cartographic;
  3837. exports.Ellipsoid = Ellipsoid;
  3838. exports.Matrix3 = Matrix3;
  3839. }));