Cartesian2-a5d6dde9.js 138 KB

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