Cartesian2-2a723276.js 156 KB

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