Cartesian2-85064f09.js 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337'], function (exports, when, Check, _Math, Cartographic) { 'use strict';
  24. function initialize(ellipsoid, x, y, z) {
  25. x = when.defaultValue(x, 0.0);
  26. y = when.defaultValue(y, 0.0);
  27. z = when.defaultValue(z, 0.0);
  28. //>>includeStart('debug', pragmas.debug);
  29. Check.Check.typeOf.number.greaterThanOrEquals('x', x, 0.0);
  30. Check.Check.typeOf.number.greaterThanOrEquals('y', y, 0.0);
  31. Check.Check.typeOf.number.greaterThanOrEquals('z', z, 0.0);
  32. //>>includeEnd('debug');
  33. if(_Math.CesiumMath.equalsEpsilon(z, 6356752.3142451793, _Math.CesiumMath.EPSILON10)){
  34. _Math.CesiumMath.Radius = z;
  35. }
  36. ellipsoid._radii = new Cartographic.Cartesian3(x, y, z);
  37. ellipsoid._radiiSquared = new Cartographic.Cartesian3(x * x,
  38. y * y,
  39. z * z);
  40. ellipsoid._radiiToTheFourth = new Cartographic.Cartesian3(x * x * x * x,
  41. y * y * y * y,
  42. z * z * z * z);
  43. ellipsoid._oneOverRadii = new Cartographic.Cartesian3(x === 0.0 ? 0.0 : 1.0 / x,
  44. y === 0.0 ? 0.0 : 1.0 / y,
  45. z === 0.0 ? 0.0 : 1.0 / z);
  46. ellipsoid._oneOverRadiiSquared = new Cartographic.Cartesian3(x === 0.0 ? 0.0 : 1.0 / (x * x),
  47. y === 0.0 ? 0.0 : 1.0 / (y * y),
  48. z === 0.0 ? 0.0 : 1.0 / (z * z));
  49. ellipsoid._minimumRadius = Math.min(x, y, z);
  50. ellipsoid._maximumRadius = Math.max(x, y, z);
  51. ellipsoid._centerToleranceSquared = _Math.CesiumMath.EPSILON1;
  52. if (ellipsoid._radiiSquared.z !== 0) {
  53. ellipsoid._squaredXOverSquaredZ = ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  54. }
  55. }
  56. /**
  57. * A quadratic surface defined in Cartesian coordinates by the equation
  58. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  59. * by Cesium to represent the shape of planetary bodies.
  60. *
  61. * Rather than constructing this object directly, one of the provided
  62. * constants is normally used.
  63. * @alias Ellipsoid
  64. * @constructor
  65. *
  66. * @param {Number} [x=0] The radius in the x direction.
  67. * @param {Number} [y=0] The radius in the y direction.
  68. * @param {Number} [z=0] The radius in the z direction.
  69. *
  70. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  71. *
  72. * @see Ellipsoid.fromCartesian3
  73. * @see Ellipsoid.WGS84
  74. * @see Ellipsoid.UNIT_SPHERE
  75. */
  76. function Ellipsoid(x, y, z) {
  77. this._radii = undefined;
  78. this._radiiSquared = undefined;
  79. this._radiiToTheFourth = undefined;
  80. this._oneOverRadii = undefined;
  81. this._oneOverRadiiSquared = undefined;
  82. this._minimumRadius = undefined;
  83. this._maximumRadius = undefined;
  84. this._centerToleranceSquared = undefined;
  85. this._squaredXOverSquaredZ = undefined;
  86. initialize(this, x, y, z);
  87. }
  88. Object.defineProperties(Ellipsoid.prototype, {
  89. /**
  90. * Gets the radii of the ellipsoid.
  91. * @memberof Ellipsoid.prototype
  92. * @type {Cartesian3}
  93. * @readonly
  94. */
  95. radii : {
  96. get: function() {
  97. return this._radii;
  98. }
  99. },
  100. /**
  101. * Gets the squared radii of the ellipsoid.
  102. * @memberof Ellipsoid.prototype
  103. * @type {Cartesian3}
  104. * @readonly
  105. */
  106. radiiSquared : {
  107. get : function() {
  108. return this._radiiSquared;
  109. }
  110. },
  111. /**
  112. * Gets the radii of the ellipsoid raise to the fourth power.
  113. * @memberof Ellipsoid.prototype
  114. * @type {Cartesian3}
  115. * @readonly
  116. */
  117. radiiToTheFourth : {
  118. get : function() {
  119. return this._radiiToTheFourth;
  120. }
  121. },
  122. /**
  123. * Gets one over the radii of the ellipsoid.
  124. * @memberof Ellipsoid.prototype
  125. * @type {Cartesian3}
  126. * @readonly
  127. */
  128. oneOverRadii : {
  129. get : function() {
  130. return this._oneOverRadii;
  131. }
  132. },
  133. /**
  134. * Gets one over the squared radii of the ellipsoid.
  135. * @memberof Ellipsoid.prototype
  136. * @type {Cartesian3}
  137. * @readonly
  138. */
  139. oneOverRadiiSquared : {
  140. get : function() {
  141. return this._oneOverRadiiSquared;
  142. }
  143. },
  144. /**
  145. * Gets the minimum radius of the ellipsoid.
  146. * @memberof Ellipsoid.prototype
  147. * @type {Number}
  148. * @readonly
  149. */
  150. minimumRadius : {
  151. get : function() {
  152. return this._minimumRadius;
  153. }
  154. },
  155. /**
  156. * Gets the maximum radius of the ellipsoid.
  157. * @memberof Ellipsoid.prototype
  158. * @type {Number}
  159. * @readonly
  160. */
  161. maximumRadius : {
  162. get : function() {
  163. return this._maximumRadius;
  164. }
  165. }
  166. });
  167. /**
  168. * Duplicates an Ellipsoid instance.
  169. *
  170. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  171. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  172. * instance should be created.
  173. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  174. */
  175. Ellipsoid.clone = function(ellipsoid, result) {
  176. if (!when.defined(ellipsoid)) {
  177. return undefined;
  178. }
  179. var radii = ellipsoid._radii;
  180. if (!when.defined(result)) {
  181. return new Ellipsoid(radii.x, radii.y, radii.z);
  182. }
  183. Cartographic.Cartesian3.clone(radii, result._radii);
  184. Cartographic.Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  185. Cartographic.Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  186. Cartographic.Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  187. Cartographic.Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  188. result._minimumRadius = ellipsoid._minimumRadius;
  189. result._maximumRadius = ellipsoid._maximumRadius;
  190. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  191. return result;
  192. };
  193. /**
  194. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  195. *
  196. * @param {Cartesian3} [cartesian=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  197. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  198. * instance should be created.
  199. * @returns {Ellipsoid} A new Ellipsoid instance.
  200. *
  201. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  202. *
  203. * @see Ellipsoid.WGS84
  204. * @see Ellipsoid.UNIT_SPHERE
  205. */
  206. Ellipsoid.fromCartesian3 = function(cartesian, result) {
  207. if (!when.defined(result)) {
  208. result = new Ellipsoid();
  209. }
  210. if (!when.defined(cartesian)) {
  211. return result;
  212. }
  213. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  214. return result;
  215. };
  216. /**
  217. * An Ellipsoid instance initialized to the WGS84 standard.
  218. *
  219. * @type {Ellipsoid}
  220. * @constant
  221. */
  222. Ellipsoid.WGS84 = Object.freeze(new Ellipsoid(6378137.0, 6378137.0, _Math.CesiumMath.Radius));
  223. Ellipsoid.XIAN80 = Object.freeze(new Ellipsoid(6378140.0, 6378140.0, 6356755.29));
  224. Ellipsoid.CGCS2000 = Object.freeze(new Ellipsoid(6378137.0, 6378137.0, 6356752.31));
  225. /**
  226. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  227. *
  228. * @type {Ellipsoid}
  229. * @constant
  230. */
  231. Ellipsoid.UNIT_SPHERE = Object.freeze(new Ellipsoid(1.0, 1.0, 1.0));
  232. /**
  233. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  234. *
  235. * @type {Ellipsoid}
  236. * @constant
  237. */
  238. Ellipsoid.MOON = Object.freeze(new Ellipsoid(_Math.CesiumMath.LUNAR_RADIUS, _Math.CesiumMath.LUNAR_RADIUS, _Math.CesiumMath.LUNAR_RADIUS));
  239. /**
  240. * Duplicates an Ellipsoid instance.
  241. *
  242. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  243. * instance should be created.
  244. * @returns {Ellipsoid} The cloned Ellipsoid.
  245. */
  246. Ellipsoid.prototype.clone = function(result) {
  247. return Ellipsoid.clone(this, result);
  248. };
  249. /**
  250. * The number of elements used to pack the object into an array.
  251. * @type {Number}
  252. */
  253. Ellipsoid.packedLength = Cartographic.Cartesian3.packedLength;
  254. /**
  255. * Stores the provided instance into the provided array.
  256. *
  257. * @param {Ellipsoid} value The value to pack.
  258. * @param {Number[]} array The array to pack into.
  259. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  260. *
  261. * @returns {Number[]} The array that was packed into
  262. */
  263. Ellipsoid.pack = function(value, array, startingIndex) {
  264. //>>includeStart('debug', pragmas.debug);
  265. Check.Check.typeOf.object('value', value);
  266. Check.Check.defined('array', array);
  267. //>>includeEnd('debug');
  268. startingIndex = when.defaultValue(startingIndex, 0);
  269. Cartographic.Cartesian3.pack(value._radii, array, startingIndex);
  270. return array;
  271. };
  272. /**
  273. * Retrieves an instance from a packed array.
  274. *
  275. * @param {Number[]} array The packed array.
  276. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  277. * @param {Ellipsoid} [result] The object into which to store the result.
  278. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  279. */
  280. Ellipsoid.unpack = function(array, startingIndex, result) {
  281. //>>includeStart('debug', pragmas.debug);
  282. Check.Check.defined('array', array);
  283. //>>includeEnd('debug');
  284. startingIndex = when.defaultValue(startingIndex, 0);
  285. var radii = Cartographic.Cartesian3.unpack(array, startingIndex);
  286. return Ellipsoid.fromCartesian3(radii, result);
  287. };
  288. /**
  289. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  290. * @function
  291. *
  292. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  293. * @param {Cartesian3} [result] The object onto which to store the result.
  294. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  295. */
  296. Ellipsoid.prototype.geocentricSurfaceNormal = Cartographic.Cartesian3.normalize;
  297. /**
  298. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  299. *
  300. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  301. * @param {Cartesian3} [result] The object onto which to store the result.
  302. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  303. */
  304. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function(cartographic, result) {
  305. //>>includeStart('debug', pragmas.debug);
  306. Check.Check.typeOf.object('cartographic', cartographic);
  307. //>>includeEnd('debug');
  308. var longitude = cartographic.longitude;
  309. var latitude = cartographic.latitude;
  310. var cosLatitude = Math.cos(latitude);
  311. var x = cosLatitude * Math.cos(longitude);
  312. var y = cosLatitude * Math.sin(longitude);
  313. var z = Math.sin(latitude);
  314. if (!when.defined(result)) {
  315. result = new Cartographic.Cartesian3();
  316. }
  317. result.x = x;
  318. result.y = y;
  319. result.z = z;
  320. return Cartographic.Cartesian3.normalize(result, result);
  321. };
  322. /**
  323. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  324. *
  325. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  326. * @param {Cartesian3} [result] The object onto which to store the result.
  327. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  328. */
  329. Ellipsoid.prototype.geodeticSurfaceNormal = function(cartesian, result) {
  330. if (!when.defined(result)) {
  331. result = new Cartographic.Cartesian3();
  332. }
  333. result = Cartographic.Cartesian3.multiplyComponents(cartesian, this._oneOverRadiiSquared, result);
  334. return Cartographic.Cartesian3.normalize(result, result);
  335. };
  336. var cartographicToCartesianNormal = new Cartographic.Cartesian3();
  337. var cartographicToCartesianK = new Cartographic.Cartesian3();
  338. /**
  339. * Converts the provided cartographic to Cartesian representation.
  340. *
  341. * @param {Cartographic} cartographic The cartographic position.
  342. * @param {Cartesian3} [result] The object onto which to store the result.
  343. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  344. *
  345. * @example
  346. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  347. * var position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  348. * var cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  349. */
  350. Ellipsoid.prototype.cartographicToCartesian = function(cartographic, result) {
  351. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  352. var n = cartographicToCartesianNormal;
  353. var k = cartographicToCartesianK;
  354. this.geodeticSurfaceNormalCartographic(cartographic, n);
  355. Cartographic.Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  356. var gamma = Math.sqrt(Cartographic.Cartesian3.dot(n, k));
  357. Cartographic.Cartesian3.divideByScalar(k, gamma, k);
  358. Cartographic.Cartesian3.multiplyByScalar(n, cartographic.height, n);
  359. if (!when.defined(result)) {
  360. result = new Cartographic.Cartesian3();
  361. }
  362. return Cartographic.Cartesian3.add(k, n, result);
  363. };
  364. /**
  365. * Converts the provided array of cartographics to an array of Cartesians.
  366. *
  367. * @param {Cartographic[]} cartographics An array of cartographic positions.
  368. * @param {Cartesian3[]} [result] The object onto which to store the result.
  369. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  370. *
  371. * @example
  372. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  373. * var positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  374. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  375. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  376. * var cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  377. */
  378. Ellipsoid.prototype.cartographicArrayToCartesianArray = function(cartographics, result) {
  379. //>>includeStart('debug', pragmas.debug);
  380. Check.Check.defined('cartographics', cartographics);
  381. //>>includeEnd('debug')
  382. var length = cartographics.length;
  383. if (!when.defined(result)) {
  384. result = new Array(length);
  385. } else {
  386. result.length = length;
  387. }
  388. for ( var i = 0; i < length; i++) {
  389. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  390. }
  391. return result;
  392. };
  393. var cartesianToCartographicN = new Cartographic.Cartesian3();
  394. var cartesianToCartographicP = new Cartographic.Cartesian3();
  395. var cartesianToCartographicH = new Cartographic.Cartesian3();
  396. /**
  397. * Converts the provided cartesian to cartographic representation.
  398. * The cartesian is undefined at the center of the ellipsoid.
  399. *
  400. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  401. * @param {Cartographic} [result] The object onto which to store the result.
  402. * @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.
  403. *
  404. * @example
  405. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  406. * var position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  407. * var cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  408. */
  409. Ellipsoid.prototype.cartesianToCartographic = function(cartesian, result) {
  410. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  411. var p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  412. if (!when.defined(p)) {
  413. return undefined;
  414. }
  415. var n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  416. var h = Cartographic.Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  417. var longitude = Math.atan2(n.y, n.x);
  418. var latitude = Math.asin(n.z);
  419. var height = _Math.CesiumMath.sign(Cartographic.Cartesian3.dot(h, cartesian)) * Cartographic.Cartesian3.magnitude(h);
  420. if (!when.defined(result)) {
  421. return new Cartographic.Cartographic(longitude, latitude, height);
  422. }
  423. result.longitude = longitude;
  424. result.latitude = latitude;
  425. result.height = height;
  426. return result;
  427. };
  428. /**
  429. * Converts the provided array of cartesians to an array of cartographics.
  430. *
  431. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  432. * @param {Cartographic[]} [result] The object onto which to store the result.
  433. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  434. *
  435. * @example
  436. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  437. * var positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  438. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  439. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  440. * var cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  441. */
  442. Ellipsoid.prototype.cartesianArrayToCartographicArray = function(cartesians, result) {
  443. //>>includeStart('debug', pragmas.debug);
  444. Check.Check.defined('cartesians', cartesians);
  445. //>>includeEnd('debug');
  446. var length = cartesians.length;
  447. if (!when.defined(result)) {
  448. result = new Array(length);
  449. } else {
  450. result.length = length;
  451. }
  452. for ( var i = 0; i < length; ++i) {
  453. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  454. }
  455. return result;
  456. };
  457. /**
  458. * Scales the provided Cartesian position along the geodetic surface normal
  459. * so that it is on the surface of this ellipsoid. If the position is
  460. * at the center of the ellipsoid, this function returns undefined.
  461. *
  462. * @param {Cartesian3} cartesian The Cartesian position to scale.
  463. * @param {Cartesian3} [result] The object onto which to store the result.
  464. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  465. */
  466. Ellipsoid.prototype.scaleToGeodeticSurface = function(cartesian, result) {
  467. return Cartographic.scaleToGeodeticSurface(cartesian, this._oneOverRadii, this._oneOverRadiiSquared, this._centerToleranceSquared, result);
  468. };
  469. /**
  470. * Scales the provided Cartesian position along the geocentric surface normal
  471. * so that it is on the surface of this ellipsoid.
  472. *
  473. * @param {Cartesian3} cartesian The Cartesian position to scale.
  474. * @param {Cartesian3} [result] The object onto which to store the result.
  475. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  476. */
  477. Ellipsoid.prototype.scaleToGeocentricSurface = function(cartesian, result) {
  478. //>>includeStart('debug', pragmas.debug);
  479. Check.Check.typeOf.object('cartesian', cartesian);
  480. //>>includeEnd('debug');
  481. if (!when.defined(result)) {
  482. result = new Cartographic.Cartesian3();
  483. }
  484. var positionX = cartesian.x;
  485. var positionY = cartesian.y;
  486. var positionZ = cartesian.z;
  487. var oneOverRadiiSquared = this._oneOverRadiiSquared;
  488. var beta = 1.0 / Math.sqrt((positionX * positionX) * oneOverRadiiSquared.x +
  489. (positionY * positionY) * oneOverRadiiSquared.y +
  490. (positionZ * positionZ) * oneOverRadiiSquared.z);
  491. return Cartographic.Cartesian3.multiplyByScalar(cartesian, beta, result);
  492. };
  493. /**
  494. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  495. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  496. *
  497. * @param {Cartesian3} position The position to transform.
  498. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  499. * return a new instance.
  500. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  501. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  502. */
  503. Ellipsoid.prototype.transformPositionToScaledSpace = function(position, result) {
  504. if (!when.defined(result)) {
  505. result = new Cartographic.Cartesian3();
  506. }
  507. return Cartographic.Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  508. };
  509. /**
  510. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  511. * its components by the result of {@link Ellipsoid#radii}.
  512. *
  513. * @param {Cartesian3} position The position to transform.
  514. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  515. * return a new instance.
  516. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  517. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  518. */
  519. Ellipsoid.prototype.transformPositionFromScaledSpace = function(position, result) {
  520. if (!when.defined(result)) {
  521. result = new Cartographic.Cartesian3();
  522. }
  523. return Cartographic.Cartesian3.multiplyComponents(position, this._radii, result);
  524. };
  525. /**
  526. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  527. * <code>true</code> if they are equal, <code>false</code> otherwise.
  528. *
  529. * @param {Ellipsoid} [right] The other Ellipsoid.
  530. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  531. */
  532. Ellipsoid.prototype.equals = function(right) {
  533. return (this === right) ||
  534. (when.defined(right) &&
  535. Cartographic.Cartesian3.equals(this._radii, right._radii));
  536. };
  537. /**
  538. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  539. *
  540. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  541. */
  542. Ellipsoid.prototype.toString = function() {
  543. return this._radii.toString();
  544. };
  545. /**
  546. * Computes a point which is the intersection of the surface normal with the z-axis.
  547. *
  548. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  549. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  550. * 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.
  551. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  552. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  553. * @param {Cartesian3} [result] The cartesian to which to copy the result, or undefined to create and
  554. * return a new instance.
  555. * @returns {Cartesian3 | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  556. *
  557. * @exception {DeveloperError} position is required.
  558. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  559. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  560. */
  561. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function(position, buffer, result) {
  562. //>>includeStart('debug', pragmas.debug);
  563. Check.Check.typeOf.object('position', position);
  564. if (!_Math.CesiumMath.equalsEpsilon(this._radii.x, this._radii.y, _Math.CesiumMath.EPSILON15)) {
  565. throw new Check.DeveloperError('Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)');
  566. }
  567. Check.Check.typeOf.number.greaterThan('Ellipsoid.radii.z', this._radii.z, 0);
  568. //>>includeEnd('debug');
  569. buffer = when.defaultValue(buffer, 0.0);
  570. var squaredXOverSquaredZ = this._squaredXOverSquaredZ;
  571. if (!when.defined(result)) {
  572. result = new Cartographic.Cartesian3();
  573. }
  574. result.x = 0.0;
  575. result.y = 0.0;
  576. result.z = position.z * (1 - squaredXOverSquaredZ);
  577. if (Math.abs(result.z) >= this._radii.z - buffer) {
  578. return undefined;
  579. }
  580. return result;
  581. };
  582. /**
  583. * A two dimensional region specified as longitude and latitude coordinates.
  584. *
  585. * @alias Rectangle
  586. * @constructor
  587. *
  588. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  589. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  590. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  591. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  592. *
  593. * @see Packable
  594. */
  595. function Rectangle(west, south, east, north) {
  596. /**
  597. * The westernmost longitude in radians in the range [-Pi, Pi].
  598. *
  599. * @type {Number}
  600. * @default 0.0
  601. */
  602. this.west = when.defaultValue(west, 0.0);
  603. /**
  604. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  605. *
  606. * @type {Number}
  607. * @default 0.0
  608. */
  609. this.south = when.defaultValue(south, 0.0);
  610. /**
  611. * The easternmost longitude in radians in the range [-Pi, Pi].
  612. *
  613. * @type {Number}
  614. * @default 0.0
  615. */
  616. this.east = when.defaultValue(east, 0.0);
  617. /**
  618. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  619. *
  620. * @type {Number}
  621. * @default 0.0
  622. */
  623. this.north = when.defaultValue(north, 0.0);
  624. }
  625. Object.defineProperties(Rectangle.prototype, {
  626. /**
  627. * Gets the width of the rectangle in radians.
  628. * @memberof Rectangle.prototype
  629. * @type {Number}
  630. */
  631. width : {
  632. get : function() {
  633. return Rectangle.computeWidth(this);
  634. }
  635. },
  636. /**
  637. * Gets the height of the rectangle in radians.
  638. * @memberof Rectangle.prototype
  639. * @type {Number}
  640. */
  641. height : {
  642. get : function() {
  643. return Rectangle.computeHeight(this);
  644. }
  645. }
  646. });
  647. /**
  648. * The number of elements used to pack the object into an array.
  649. * @type {Number}
  650. */
  651. Rectangle.packedLength = 4;
  652. /**
  653. * Stores the provided instance into the provided array.
  654. *
  655. * @param {Rectangle} value The value to pack.
  656. * @param {Number[]} array The array to pack into.
  657. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  658. *
  659. * @returns {Number[]} The array that was packed into
  660. */
  661. Rectangle.pack = function(value, array, startingIndex) {
  662. //>>includeStart('debug', pragmas.debug);
  663. Check.Check.typeOf.object('value', value);
  664. Check.Check.defined('array', array);
  665. //>>includeEnd('debug');
  666. startingIndex = when.defaultValue(startingIndex, 0);
  667. array[startingIndex++] = value.west;
  668. array[startingIndex++] = value.south;
  669. array[startingIndex++] = value.east;
  670. array[startingIndex] = value.north;
  671. return array;
  672. };
  673. /**
  674. * Retrieves an instance from a packed array.
  675. *
  676. * @param {Number[]} array The packed array.
  677. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  678. * @param {Rectangle} [result] The object into which to store the result.
  679. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  680. */
  681. Rectangle.unpack = function(array, startingIndex, result) {
  682. //>>includeStart('debug', pragmas.debug);
  683. Check.Check.defined('array', array);
  684. //>>includeEnd('debug');
  685. startingIndex = when.defaultValue(startingIndex, 0);
  686. if (!when.defined(result)) {
  687. result = new Rectangle();
  688. }
  689. result.west = array[startingIndex++];
  690. result.south = array[startingIndex++];
  691. result.east = array[startingIndex++];
  692. result.north = array[startingIndex];
  693. return result;
  694. };
  695. /**
  696. * Computes the width of a rectangle in radians.
  697. * @param {Rectangle} rectangle The rectangle to compute the width of.
  698. * @returns {Number} The width.
  699. */
  700. Rectangle.computeWidth = function(rectangle) {
  701. //>>includeStart('debug', pragmas.debug);
  702. Check.Check.typeOf.object('rectangle', rectangle);
  703. //>>includeEnd('debug');
  704. var east = rectangle.east;
  705. var west = rectangle.west;
  706. if (east < west) {
  707. east += _Math.CesiumMath.TWO_PI;
  708. }
  709. return east - west;
  710. };
  711. /**
  712. * Computes the height of a rectangle in radians.
  713. * @param {Rectangle} rectangle The rectangle to compute the height of.
  714. * @returns {Number} The height.
  715. */
  716. Rectangle.computeHeight = function(rectangle) {
  717. //>>includeStart('debug', pragmas.debug);
  718. Check.Check.typeOf.object('rectangle', rectangle);
  719. //>>includeEnd('debug');
  720. return rectangle.north - rectangle.south;
  721. };
  722. /**
  723. * Creates a rectangle given the boundary longitude and latitude in degrees.
  724. *
  725. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  726. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  727. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  728. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  729. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  730. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  731. *
  732. * @example
  733. * var rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  734. */
  735. Rectangle.fromDegrees = function(west, south, east, north, result) {
  736. west = _Math.CesiumMath.toRadians(when.defaultValue(west, 0.0));
  737. south = _Math.CesiumMath.toRadians(when.defaultValue(south, 0.0));
  738. east = _Math.CesiumMath.toRadians(when.defaultValue(east, 0.0));
  739. north = _Math.CesiumMath.toRadians(when.defaultValue(north, 0.0));
  740. if (!when.defined(result)) {
  741. return new Rectangle(west, south, east, north);
  742. }
  743. result.west = west;
  744. result.south = south;
  745. result.east = east;
  746. result.north = north;
  747. return result;
  748. };
  749. /**
  750. * Creates a rectangle given the boundary longitude and latitude in radians.
  751. *
  752. * @param {Number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
  753. * @param {Number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  754. * @param {Number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
  755. * @param {Number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  756. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  757. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  758. *
  759. * @example
  760. * var rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
  761. */
  762. Rectangle.fromRadians = function(west, south, east, north, result) {
  763. if (!when.defined(result)) {
  764. return new Rectangle(west, south, east, north);
  765. }
  766. result.west = when.defaultValue(west, 0.0);
  767. result.south = when.defaultValue(south, 0.0);
  768. result.east = when.defaultValue(east, 0.0);
  769. result.north = when.defaultValue(north, 0.0);
  770. return result;
  771. };
  772. /**
  773. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  774. *
  775. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  776. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  777. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  778. */
  779. Rectangle.fromCartographicArray = function(cartographics, result) {
  780. //>>includeStart('debug', pragmas.debug);
  781. Check.Check.defined('cartographics', cartographics);
  782. //>>includeEnd('debug');
  783. var west = Number.MAX_VALUE;
  784. var east = -Number.MAX_VALUE;
  785. var westOverIDL = Number.MAX_VALUE;
  786. var eastOverIDL = -Number.MAX_VALUE;
  787. var south = Number.MAX_VALUE;
  788. var north = -Number.MAX_VALUE;
  789. for ( var i = 0, len = cartographics.length; i < len; i++) {
  790. var position = cartographics[i];
  791. west = Math.min(west, position.longitude);
  792. east = Math.max(east, position.longitude);
  793. south = Math.min(south, position.latitude);
  794. north = Math.max(north, position.latitude);
  795. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + _Math.CesiumMath.TWO_PI;
  796. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  797. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  798. }
  799. if(east - west > eastOverIDL - westOverIDL) {
  800. west = westOverIDL;
  801. east = eastOverIDL;
  802. if (east > _Math.CesiumMath.PI) {
  803. east = east - _Math.CesiumMath.TWO_PI;
  804. }
  805. if (west > _Math.CesiumMath.PI) {
  806. west = west - _Math.CesiumMath.TWO_PI;
  807. }
  808. }
  809. if (!when.defined(result)) {
  810. return new Rectangle(west, south, east, north);
  811. }
  812. result.west = west;
  813. result.south = south;
  814. result.east = east;
  815. result.north = north;
  816. return result;
  817. };
  818. /**
  819. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  820. *
  821. * @param {Cartesian3[]} cartesians The list of Cartesian instances.
  822. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  823. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  824. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  825. */
  826. Rectangle.fromCartesianArray = function(cartesians, ellipsoid, result) {
  827. //>>includeStart('debug', pragmas.debug);
  828. Check.Check.defined('cartesians', cartesians);
  829. //>>includeEnd('debug');
  830. ellipsoid = when.defaultValue(ellipsoid, Ellipsoid.WGS84);
  831. var west = Number.MAX_VALUE;
  832. var east = -Number.MAX_VALUE;
  833. var westOverIDL = Number.MAX_VALUE;
  834. var eastOverIDL = -Number.MAX_VALUE;
  835. var south = Number.MAX_VALUE;
  836. var north = -Number.MAX_VALUE;
  837. for ( var i = 0, len = cartesians.length; i < len; i++) {
  838. var position = ellipsoid.cartesianToCartographic(cartesians[i]);
  839. west = Math.min(west, position.longitude);
  840. east = Math.max(east, position.longitude);
  841. south = Math.min(south, position.latitude);
  842. north = Math.max(north, position.latitude);
  843. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + _Math.CesiumMath.TWO_PI;
  844. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  845. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  846. }
  847. if(east - west > eastOverIDL - westOverIDL) {
  848. west = westOverIDL;
  849. east = eastOverIDL;
  850. if (east > _Math.CesiumMath.PI) {
  851. east = east - _Math.CesiumMath.TWO_PI;
  852. }
  853. if (west > _Math.CesiumMath.PI) {
  854. west = west - _Math.CesiumMath.TWO_PI;
  855. }
  856. }
  857. if (!when.defined(result)) {
  858. return new Rectangle(west, south, east, north);
  859. }
  860. result.west = west;
  861. result.south = south;
  862. result.east = east;
  863. result.north = north;
  864. return result;
  865. };
  866. /**
  867. * Duplicates a Rectangle.
  868. *
  869. * @param {Rectangle} rectangle The rectangle to clone.
  870. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  871. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  872. */
  873. Rectangle.clone = function(rectangle, result) {
  874. if (!when.defined(rectangle)) {
  875. return undefined;
  876. }
  877. if (!when.defined(result)) {
  878. return new Rectangle(rectangle.west, rectangle.south, rectangle.east, rectangle.north);
  879. }
  880. result.west = rectangle.west;
  881. result.south = rectangle.south;
  882. result.east = rectangle.east;
  883. result.north = rectangle.north;
  884. return result;
  885. };
  886. /**
  887. * Compares the provided Rectangles componentwise and returns
  888. * <code>true</code> if they pass an absolute or relative tolerance test,
  889. * <code>false</code> otherwise.
  890. *
  891. * @param {Rectangle} [left] The first Rectangle.
  892. * @param {Rectangle} [right] The second Rectangle.
  893. * @param {Number} absoluteEpsilon The absolute epsilon tolerance to use for equality testing.
  894. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  895. */
  896. Rectangle.equalsEpsilon = function(left, right, absoluteEpsilon) {
  897. //>>includeStart('debug', pragmas.debug);
  898. Check.Check.typeOf.number('absoluteEpsilon', absoluteEpsilon);
  899. //>>includeEnd('debug');
  900. return (left === right) ||
  901. (when.defined(left) &&
  902. when.defined(right) &&
  903. (Math.abs(left.west - right.west) <= absoluteEpsilon) &&
  904. (Math.abs(left.south - right.south) <= absoluteEpsilon) &&
  905. (Math.abs(left.east - right.east) <= absoluteEpsilon) &&
  906. (Math.abs(left.north - right.north) <= absoluteEpsilon));
  907. };
  908. /**
  909. * Duplicates this Rectangle.
  910. *
  911. * @param {Rectangle} [result] The object onto which to store the result.
  912. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  913. */
  914. Rectangle.prototype.clone = function(result) {
  915. return Rectangle.clone(this, result);
  916. };
  917. /**
  918. * Compares the provided Rectangle with this Rectangle componentwise and returns
  919. * <code>true</code> if they are equal, <code>false</code> otherwise.
  920. *
  921. * @param {Rectangle} [other] The Rectangle to compare.
  922. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  923. */
  924. Rectangle.prototype.equals = function(other) {
  925. return Rectangle.equals(this, other);
  926. };
  927. /**
  928. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  929. * <code>false</code> otherwise.
  930. *
  931. * @param {Rectangle} [left] The first Rectangle.
  932. * @param {Rectangle} [right] The second Rectangle.
  933. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  934. */
  935. Rectangle.equals = function(left, right) {
  936. return (left === right) ||
  937. ((when.defined(left)) &&
  938. (when.defined(right)) &&
  939. (left.west === right.west) &&
  940. (left.south === right.south) &&
  941. (left.east === right.east) &&
  942. (left.north === right.north));
  943. };
  944. /**
  945. * Compares the provided Rectangle with this Rectangle componentwise and returns
  946. * <code>true</code> if they are within the provided epsilon,
  947. * <code>false</code> otherwise.
  948. *
  949. * @param {Rectangle} [other] The Rectangle to compare.
  950. * @param {Number} epsilon The epsilon to use for equality testing.
  951. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  952. */
  953. Rectangle.prototype.equalsEpsilon = function(other, epsilon) {
  954. //>>includeStart('debug', pragmas.debug);
  955. Check.Check.typeOf.number('epsilon', epsilon);
  956. //>>includeEnd('debug');
  957. return Rectangle.equalsEpsilon(this, other, epsilon);
  958. };
  959. /**
  960. * Checks a Rectangle's properties and throws if they are not in valid ranges.
  961. *
  962. * @param {Rectangle} rectangle The rectangle to validate
  963. *
  964. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  965. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  966. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  967. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  968. */
  969. Rectangle.validate = function(rectangle) {
  970. //>>includeStart('debug', pragmas.debug);
  971. Check.Check.typeOf.object('rectangle', rectangle);
  972. var north = rectangle.north;
  973. Check.Check.typeOf.number.greaterThanOrEquals('north', north, -_Math.CesiumMath.PI_OVER_TWO);
  974. Check.Check.typeOf.number.lessThanOrEquals('north', north, _Math.CesiumMath.PI_OVER_TWO);
  975. var south = rectangle.south;
  976. Check.Check.typeOf.number.greaterThanOrEquals('south', south, -_Math.CesiumMath.PI_OVER_TWO);
  977. Check.Check.typeOf.number.lessThanOrEquals('south', south, _Math.CesiumMath.PI_OVER_TWO);
  978. var west = rectangle.west;
  979. Check.Check.typeOf.number.greaterThanOrEquals('west', west, -Math.PI);
  980. Check.Check.typeOf.number.lessThanOrEquals('west', west, Math.PI);
  981. var east = rectangle.east;
  982. Check.Check.typeOf.number.greaterThanOrEquals('east', east, -Math.PI);
  983. Check.Check.typeOf.number.lessThanOrEquals('east', east, Math.PI);
  984. //>>includeEnd('debug');
  985. };
  986. /**
  987. * Computes the southwest corner of a rectangle.
  988. *
  989. * @param {Rectangle} rectangle The rectangle for which to find the corner
  990. * @param {Cartographic} [result] The object onto which to store the result.
  991. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  992. */
  993. Rectangle.southwest = function(rectangle, result) {
  994. //>>includeStart('debug', pragmas.debug);
  995. Check.Check.typeOf.object('rectangle', rectangle);
  996. //>>includeEnd('debug');
  997. if (!when.defined(result)) {
  998. return new Cartographic.Cartographic(rectangle.west, rectangle.south);
  999. }
  1000. result.longitude = rectangle.west;
  1001. result.latitude = rectangle.south;
  1002. result.height = 0.0;
  1003. return result;
  1004. };
  1005. /**
  1006. * Computes the northwest corner of a rectangle.
  1007. *
  1008. * @param {Rectangle} rectangle The rectangle for which to find the corner
  1009. * @param {Cartographic} [result] The object onto which to store the result.
  1010. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  1011. */
  1012. Rectangle.northwest = function(rectangle, result) {
  1013. //>>includeStart('debug', pragmas.debug);
  1014. Check.Check.typeOf.object('rectangle', rectangle);
  1015. //>>includeEnd('debug');
  1016. if (!when.defined(result)) {
  1017. return new Cartographic.Cartographic(rectangle.west, rectangle.north);
  1018. }
  1019. result.longitude = rectangle.west;
  1020. result.latitude = rectangle.north;
  1021. result.height = 0.0;
  1022. return result;
  1023. };
  1024. /**
  1025. * Computes the northeast corner of a rectangle.
  1026. *
  1027. * @param {Rectangle} rectangle The rectangle for which to find the corner
  1028. * @param {Cartographic} [result] The object onto which to store the result.
  1029. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  1030. */
  1031. Rectangle.northeast = function(rectangle, result) {
  1032. //>>includeStart('debug', pragmas.debug);
  1033. Check.Check.typeOf.object('rectangle', rectangle);
  1034. //>>includeEnd('debug');
  1035. if (!when.defined(result)) {
  1036. return new Cartographic.Cartographic(rectangle.east, rectangle.north);
  1037. }
  1038. result.longitude = rectangle.east;
  1039. result.latitude = rectangle.north;
  1040. result.height = 0.0;
  1041. return result;
  1042. };
  1043. /**
  1044. * Computes the southeast corner of a rectangle.
  1045. *
  1046. * @param {Rectangle} rectangle The rectangle for which to find the corner
  1047. * @param {Cartographic} [result] The object onto which to store the result.
  1048. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  1049. */
  1050. Rectangle.southeast = function(rectangle, result) {
  1051. //>>includeStart('debug', pragmas.debug);
  1052. Check.Check.typeOf.object('rectangle', rectangle);
  1053. //>>includeEnd('debug');
  1054. if (!when.defined(result)) {
  1055. return new Cartographic.Cartographic(rectangle.east, rectangle.south);
  1056. }
  1057. result.longitude = rectangle.east;
  1058. result.latitude = rectangle.south;
  1059. result.height = 0.0;
  1060. return result;
  1061. };
  1062. /**
  1063. * Computes the center of a rectangle.
  1064. *
  1065. * @param {Rectangle} rectangle The rectangle for which to find the center
  1066. * @param {Cartographic} [result] The object onto which to store the result.
  1067. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  1068. */
  1069. Rectangle.center = function(rectangle, result) {
  1070. //>>includeStart('debug', pragmas.debug);
  1071. Check.Check.typeOf.object('rectangle', rectangle);
  1072. //>>includeEnd('debug');
  1073. var east = rectangle.east;
  1074. var west = rectangle.west;
  1075. if (east < west) {
  1076. east += _Math.CesiumMath.TWO_PI;
  1077. }
  1078. var longitude = _Math.CesiumMath.negativePiToPi((west + east) * 0.5);
  1079. var latitude = (rectangle.south + rectangle.north) * 0.5;
  1080. if (!when.defined(result)) {
  1081. return new Cartographic.Cartographic(longitude, latitude);
  1082. }
  1083. result.longitude = longitude;
  1084. result.latitude = latitude;
  1085. result.height = 0.0;
  1086. return result;
  1087. };
  1088. /**
  1089. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  1090. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  1091. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  1092. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  1093. * coordinates, see {@link Rectangle.simpleIntersection}.
  1094. *
  1095. * @param {Rectangle} rectangle On rectangle to find an intersection
  1096. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  1097. * @param {Rectangle} [result] The object onto which to store the result.
  1098. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  1099. */
  1100. Rectangle.intersection = function(rectangle, otherRectangle, result) {
  1101. //>>includeStart('debug', pragmas.debug);
  1102. Check.Check.typeOf.object('rectangle', rectangle);
  1103. Check.Check.typeOf.object('otherRectangle', otherRectangle);
  1104. //>>includeEnd('debug');
  1105. var rectangleEast = rectangle.east;
  1106. var rectangleWest = rectangle.west;
  1107. var otherRectangleEast = otherRectangle.east;
  1108. var otherRectangleWest = otherRectangle.west;
  1109. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  1110. rectangleEast += _Math.CesiumMath.TWO_PI;
  1111. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  1112. otherRectangleEast += _Math.CesiumMath.TWO_PI;
  1113. }
  1114. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  1115. otherRectangleWest += _Math.CesiumMath.TWO_PI;
  1116. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  1117. rectangleWest += _Math.CesiumMath.TWO_PI;
  1118. }
  1119. var west = _Math.CesiumMath.negativePiToPi(Math.max(rectangleWest, otherRectangleWest));
  1120. var east = _Math.CesiumMath.negativePiToPi(Math.min(rectangleEast, otherRectangleEast));
  1121. if ((rectangle.west < rectangle.east || otherRectangle.west < otherRectangle.east) && east <= west) {
  1122. return undefined;
  1123. }
  1124. var south = Math.max(rectangle.south, otherRectangle.south);
  1125. var north = Math.min(rectangle.north, otherRectangle.north);
  1126. if (south >= north) {
  1127. return undefined;
  1128. }
  1129. if (!when.defined(result)) {
  1130. return new Rectangle(west, south, east, north);
  1131. }
  1132. result.west = west;
  1133. result.south = south;
  1134. result.east = east;
  1135. result.north = north;
  1136. return result;
  1137. };
  1138. /**
  1139. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  1140. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  1141. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  1142. * and longitude (i.e. projected coordinates).
  1143. *
  1144. * @param {Rectangle} rectangle On rectangle to find an intersection
  1145. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  1146. * @param {Rectangle} [result] The object onto which to store the result.
  1147. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  1148. */
  1149. Rectangle.simpleIntersection = function(rectangle, otherRectangle, result) {
  1150. //>>includeStart('debug', pragmas.debug);
  1151. Check.Check.typeOf.object('rectangle', rectangle);
  1152. Check.Check.typeOf.object('otherRectangle', otherRectangle);
  1153. //>>includeEnd('debug');
  1154. var west = Math.max(rectangle.west, otherRectangle.west);
  1155. var south = Math.max(rectangle.south, otherRectangle.south);
  1156. var east = Math.min(rectangle.east, otherRectangle.east);
  1157. var north = Math.min(rectangle.north, otherRectangle.north);
  1158. if (south >= north || west >= east) {
  1159. return undefined;
  1160. }
  1161. if (!when.defined(result)) {
  1162. return new Rectangle(west, south, east, north);
  1163. }
  1164. result.west = west;
  1165. result.south = south;
  1166. result.east = east;
  1167. result.north = north;
  1168. return result;
  1169. };
  1170. /**
  1171. * Computes a rectangle that is the union of two rectangles.
  1172. *
  1173. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  1174. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  1175. * @param {Rectangle} [result] The object onto which to store the result.
  1176. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  1177. */
  1178. Rectangle.union = function(rectangle, otherRectangle, result) {
  1179. //>>includeStart('debug', pragmas.debug);
  1180. Check.Check.typeOf.object('rectangle', rectangle);
  1181. Check.Check.typeOf.object('otherRectangle', otherRectangle);
  1182. //>>includeEnd('debug');
  1183. if (!when.defined(result)) {
  1184. result = new Rectangle();
  1185. }
  1186. var rectangleEast = rectangle.east;
  1187. var rectangleWest = rectangle.west;
  1188. var otherRectangleEast = otherRectangle.east;
  1189. var otherRectangleWest = otherRectangle.west;
  1190. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  1191. rectangleEast += _Math.CesiumMath.TWO_PI;
  1192. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  1193. otherRectangleEast += _Math.CesiumMath.TWO_PI;
  1194. }
  1195. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  1196. otherRectangleWest += _Math.CesiumMath.TWO_PI;
  1197. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  1198. rectangleWest += _Math.CesiumMath.TWO_PI;
  1199. }
  1200. var west = _Math.CesiumMath.convertLongitudeRange(Math.min(rectangleWest, otherRectangleWest));
  1201. var east = _Math.CesiumMath.convertLongitudeRange(Math.max(rectangleEast, otherRectangleEast));
  1202. result.west = west;
  1203. result.south = Math.min(rectangle.south, otherRectangle.south);
  1204. result.east = east;
  1205. result.north = Math.max(rectangle.north, otherRectangle.north);
  1206. return result;
  1207. };
  1208. /**
  1209. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  1210. *
  1211. * @param {Rectangle} rectangle A rectangle to expand.
  1212. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  1213. * @param {Rectangle} [result] The object onto which to store the result.
  1214. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  1215. */
  1216. Rectangle.expand = function(rectangle, cartographic, result) {
  1217. //>>includeStart('debug', pragmas.debug);
  1218. Check.Check.typeOf.object('rectangle', rectangle);
  1219. Check.Check.typeOf.object('cartographic', cartographic);
  1220. //>>includeEnd('debug');
  1221. if (!when.defined(result)) {
  1222. result = new Rectangle();
  1223. }
  1224. result.west = Math.min(rectangle.west, cartographic.longitude);
  1225. result.south = Math.min(rectangle.south, cartographic.latitude);
  1226. result.east = Math.max(rectangle.east, cartographic.longitude);
  1227. result.north = Math.max(rectangle.north, cartographic.latitude);
  1228. return result;
  1229. };
  1230. /**
  1231. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  1232. *
  1233. * @param {Rectangle} rectangle The rectangle
  1234. * @param {Cartographic} cartographic The cartographic to test.
  1235. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  1236. */
  1237. Rectangle.contains = function(rectangle, cartographic) {
  1238. //>>includeStart('debug', pragmas.debug);
  1239. Check.Check.typeOf.object('rectangle', rectangle);
  1240. Check.Check.typeOf.object('cartographic', cartographic);
  1241. //>>includeEnd('debug');
  1242. var longitude = cartographic.longitude;
  1243. var latitude = cartographic.latitude;
  1244. var west = rectangle.west;
  1245. var east = rectangle.east;
  1246. if (east < west) {
  1247. east += _Math.CesiumMath.TWO_PI;
  1248. if (longitude < 0.0) {
  1249. longitude += _Math.CesiumMath.TWO_PI;
  1250. }
  1251. }
  1252. return (longitude > west || _Math.CesiumMath.equalsEpsilon(longitude, west, _Math.CesiumMath.EPSILON14)) &&
  1253. (longitude < east || _Math.CesiumMath.equalsEpsilon(longitude, east, _Math.CesiumMath.EPSILON14)) &&
  1254. latitude >= rectangle.south &&
  1255. latitude <= rectangle.north;
  1256. };
  1257. var subsampleLlaScratch = new Cartographic.Cartographic();
  1258. /**
  1259. * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
  1260. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  1261. * for rectangles that cover the poles or cross the equator.
  1262. *
  1263. * @param {Rectangle} rectangle The rectangle to subsample.
  1264. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  1265. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  1266. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  1267. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  1268. */
  1269. Rectangle.subsample = function(rectangle, ellipsoid, surfaceHeight, result) {
  1270. //>>includeStart('debug', pragmas.debug);
  1271. Check.Check.typeOf.object('rectangle', rectangle);
  1272. //>>includeEnd('debug');
  1273. ellipsoid = when.defaultValue(ellipsoid, Ellipsoid.WGS84);
  1274. surfaceHeight = when.defaultValue(surfaceHeight, 0.0);
  1275. if (!when.defined(result)) {
  1276. result = [];
  1277. }
  1278. var length = 0;
  1279. var north = rectangle.north;
  1280. var south = rectangle.south;
  1281. var east = rectangle.east;
  1282. var west = rectangle.west;
  1283. var lla = subsampleLlaScratch;
  1284. lla.height = surfaceHeight;
  1285. lla.longitude = west;
  1286. lla.latitude = north;
  1287. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  1288. length++;
  1289. lla.longitude = east;
  1290. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  1291. length++;
  1292. lla.latitude = south;
  1293. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  1294. length++;
  1295. lla.longitude = west;
  1296. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  1297. length++;
  1298. if (north < 0.0) {
  1299. lla.latitude = north;
  1300. } else if (south > 0.0) {
  1301. lla.latitude = south;
  1302. } else {
  1303. lla.latitude = 0.0;
  1304. }
  1305. for ( var i = 1; i < 8; ++i) {
  1306. lla.longitude = -Math.PI + i * _Math.CesiumMath.PI_OVER_TWO;
  1307. if (Rectangle.contains(rectangle, lla)) {
  1308. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  1309. length++;
  1310. }
  1311. }
  1312. if (lla.latitude === 0.0) {
  1313. lla.longitude = west;
  1314. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  1315. length++;
  1316. lla.longitude = east;
  1317. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  1318. length++;
  1319. }
  1320. result.length = length;
  1321. return result;
  1322. };
  1323. var scratchCartographic = new Cartographic.Cartographic();
  1324. Rectangle.prototype.contains = function(rectangle) {
  1325. return Rectangle.contains(this, Rectangle.southwest(rectangle, scratchCartographic))
  1326. && Rectangle.contains(this, Rectangle.northwest(rectangle, scratchCartographic))
  1327. && Rectangle.contains(this, Rectangle.southeast(rectangle, scratchCartographic))
  1328. && Rectangle.contains(this, Rectangle.northeast(rectangle, scratchCartographic));
  1329. };
  1330. /**
  1331. * The largest possible rectangle.
  1332. *
  1333. * @type {Rectangle}
  1334. * @constant
  1335. */
  1336. Rectangle.MAX_VALUE = Object.freeze(new Rectangle(-Math.PI, -_Math.CesiumMath.PI_OVER_TWO, Math.PI, _Math.CesiumMath.PI_OVER_TWO));
  1337. /**
  1338. * A 2D Cartesian point.
  1339. * @alias Cartesian2
  1340. * @constructor
  1341. *
  1342. * @param {Number} [x=0.0] The X component.
  1343. * @param {Number} [y=0.0] The Y component.
  1344. *
  1345. * @see Cartesian3
  1346. * @see Cartesian4
  1347. * @see Packable
  1348. */
  1349. function Cartesian2(x, y) {
  1350. /**
  1351. * The X component.
  1352. * @type {Number}
  1353. * @default 0.0
  1354. */
  1355. this.x = when.defaultValue(x, 0.0);
  1356. /**
  1357. * The Y component.
  1358. * @type {Number}
  1359. * @default 0.0
  1360. */
  1361. this.y = when.defaultValue(y, 0.0);
  1362. }
  1363. /**
  1364. * Creates a Cartesian2 instance from x and y coordinates.
  1365. *
  1366. * @param {Number} x The x coordinate.
  1367. * @param {Number} y The y coordinate.
  1368. * @param {Cartesian2} [result] The object onto which to store the result.
  1369. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1370. */
  1371. Cartesian2.fromElements = function(x, y, result) {
  1372. if (!when.defined(result)) {
  1373. return new Cartesian2(x, y);
  1374. }
  1375. result.x = x;
  1376. result.y = y;
  1377. return result;
  1378. };
  1379. /**
  1380. * Duplicates a Cartesian2 instance.
  1381. *
  1382. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  1383. * @param {Cartesian2} [result] The object onto which to store the result.
  1384. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  1385. */
  1386. Cartesian2.clone = function(cartesian, result) {
  1387. if (!when.defined(cartesian)) {
  1388. return undefined;
  1389. }
  1390. if (!when.defined(result)) {
  1391. return new Cartesian2(cartesian.x, cartesian.y);
  1392. }
  1393. result.x = cartesian.x;
  1394. result.y = cartesian.y;
  1395. return result;
  1396. };
  1397. /**
  1398. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  1399. * x and y properties of the Cartesian3 and drops z.
  1400. * @function
  1401. *
  1402. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  1403. * @param {Cartesian2} [result] The object onto which to store the result.
  1404. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1405. */
  1406. Cartesian2.fromCartesian3 = Cartesian2.clone;
  1407. /**
  1408. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  1409. * x and y properties of the Cartesian4 and drops z and w.
  1410. * @function
  1411. *
  1412. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  1413. * @param {Cartesian2} [result] The object onto which to store the result.
  1414. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1415. */
  1416. Cartesian2.fromCartesian4 = Cartesian2.clone;
  1417. /**
  1418. * The number of elements used to pack the object into an array.
  1419. * @type {Number}
  1420. */
  1421. Cartesian2.packedLength = 2;
  1422. /**
  1423. * Stores the provided instance into the provided array.
  1424. *
  1425. * @param {Cartesian2} value The value to pack.
  1426. * @param {Number[]} array The array to pack into.
  1427. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1428. *
  1429. * @returns {Number[]} The array that was packed into
  1430. */
  1431. Cartesian2.pack = function(value, array, startingIndex) {
  1432. //>>includeStart('debug', pragmas.debug);
  1433. Check.Check.typeOf.object('value', value);
  1434. Check.Check.defined('array', array);
  1435. //>>includeEnd('debug');
  1436. startingIndex = when.defaultValue(startingIndex, 0);
  1437. array[startingIndex++] = value.x;
  1438. array[startingIndex] = value.y;
  1439. return array;
  1440. };
  1441. /**
  1442. * Retrieves an instance from a packed array.
  1443. *
  1444. * @param {Number[]} array The packed array.
  1445. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1446. * @param {Cartesian2} [result] The object into which to store the result.
  1447. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1448. */
  1449. Cartesian2.unpack = function(array, startingIndex, result) {
  1450. //>>includeStart('debug', pragmas.debug);
  1451. Check.Check.defined('array', array);
  1452. //>>includeEnd('debug');
  1453. startingIndex = when.defaultValue(startingIndex, 0);
  1454. if (!when.defined(result)) {
  1455. result = new Cartesian2();
  1456. }
  1457. result.x = array[startingIndex++];
  1458. result.y = array[startingIndex];
  1459. return result;
  1460. };
  1461. /**
  1462. * Flattens an array of Cartesian2s into and array of components.
  1463. *
  1464. * @param {Cartesian2[]} array The array of cartesians to pack.
  1465. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 2 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 2) elements.
  1466. * @returns {Number[]} The packed array.
  1467. */
  1468. Cartesian2.packArray = function(array, result) {
  1469. //>>includeStart('debug', pragmas.debug);
  1470. Check.Check.defined('array', array);
  1471. //>>includeEnd('debug');
  1472. var length = array.length;
  1473. var resultLength = length * 2;
  1474. if (!when.defined(result)) {
  1475. result = new Array(resultLength);
  1476. } else if (!Array.isArray(result) && result.length !== resultLength) {
  1477. throw new Check.DeveloperError('If result is a typed array, it must have exactly array.length * 2 elements');
  1478. } else if (result.length !== resultLength) {
  1479. result.length = resultLength;
  1480. }
  1481. for (var i = 0; i < length; ++i) {
  1482. Cartesian2.pack(array[i], result, i * 2);
  1483. }
  1484. return result;
  1485. };
  1486. /**
  1487. * Unpacks an array of cartesian components into and array of Cartesian2s.
  1488. *
  1489. * @param {Number[]} array The array of components to unpack.
  1490. * @param {Cartesian2[]} [result] The array onto which to store the result.
  1491. * @returns {Cartesian2[]} The unpacked array.
  1492. */
  1493. Cartesian2.unpackArray = function(array, result) {
  1494. //>>includeStart('debug', pragmas.debug);
  1495. Check.Check.defined('array', array);
  1496. Check.Check.typeOf.number.greaterThanOrEquals('array.length', array.length, 2);
  1497. if (array.length % 2 !== 0) {
  1498. throw new Check.DeveloperError('array length must be a multiple of 2.');
  1499. }
  1500. //>>includeEnd('debug');
  1501. var length = array.length;
  1502. if (!when.defined(result)) {
  1503. result = new Array(length / 2);
  1504. } else {
  1505. result.length = length / 2;
  1506. }
  1507. for (var i = 0; i < length; i += 2) {
  1508. var index = i / 2;
  1509. result[index] = Cartesian2.unpack(array, i, result[index]);
  1510. }
  1511. return result;
  1512. };
  1513. /**
  1514. * Creates a Cartesian2 from two consecutive elements in an array.
  1515. * @function
  1516. *
  1517. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  1518. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  1519. * @param {Cartesian2} [result] The object onto which to store the result.
  1520. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1521. *
  1522. * @example
  1523. * // Create a Cartesian2 with (1.0, 2.0)
  1524. * var v = [1.0, 2.0];
  1525. * var p = Cesium.Cartesian2.fromArray(v);
  1526. *
  1527. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  1528. * var v2 = [0.0, 0.0, 1.0, 2.0];
  1529. * var p2 = Cesium.Cartesian2.fromArray(v2, 2);
  1530. */
  1531. Cartesian2.fromArray = Cartesian2.unpack;
  1532. /**
  1533. * Computes the value of the maximum component for the supplied Cartesian.
  1534. *
  1535. * @param {Cartesian2} cartesian The cartesian to use.
  1536. * @returns {Number} The value of the maximum component.
  1537. */
  1538. Cartesian2.maximumComponent = function(cartesian) {
  1539. //>>includeStart('debug', pragmas.debug);
  1540. Check.Check.typeOf.object('cartesian', cartesian);
  1541. //>>includeEnd('debug');
  1542. return Math.max(cartesian.x, cartesian.y);
  1543. };
  1544. /**
  1545. * Computes the value of the minimum component for the supplied Cartesian.
  1546. *
  1547. * @param {Cartesian2} cartesian The cartesian to use.
  1548. * @returns {Number} The value of the minimum component.
  1549. */
  1550. Cartesian2.minimumComponent = function(cartesian) {
  1551. //>>includeStart('debug', pragmas.debug);
  1552. Check.Check.typeOf.object('cartesian', cartesian);
  1553. //>>includeEnd('debug');
  1554. return Math.min(cartesian.x, cartesian.y);
  1555. };
  1556. /**
  1557. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  1558. *
  1559. * @param {Cartesian2} first A cartesian to compare.
  1560. * @param {Cartesian2} second A cartesian to compare.
  1561. * @param {Cartesian2} result The object into which to store the result.
  1562. * @returns {Cartesian2} A cartesian with the minimum components.
  1563. */
  1564. Cartesian2.minimumByComponent = function(first, second, result) {
  1565. //>>includeStart('debug', pragmas.debug);
  1566. Check.Check.typeOf.object('first', first);
  1567. Check.Check.typeOf.object('second', second);
  1568. Check.Check.typeOf.object('result', result);
  1569. //>>includeEnd('debug');
  1570. result.x = Math.min(first.x, second.x);
  1571. result.y = Math.min(first.y, second.y);
  1572. return result;
  1573. };
  1574. /**
  1575. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  1576. *
  1577. * @param {Cartesian2} first A cartesian to compare.
  1578. * @param {Cartesian2} second A cartesian to compare.
  1579. * @param {Cartesian2} result The object into which to store the result.
  1580. * @returns {Cartesian2} A cartesian with the maximum components.
  1581. */
  1582. Cartesian2.maximumByComponent = function(first, second, result) {
  1583. //>>includeStart('debug', pragmas.debug);
  1584. Check.Check.typeOf.object('first', first);
  1585. Check.Check.typeOf.object('second', second);
  1586. Check.Check.typeOf.object('result', result);
  1587. //>>includeEnd('debug');
  1588. result.x = Math.max(first.x, second.x);
  1589. result.y = Math.max(first.y, second.y);
  1590. return result;
  1591. };
  1592. /**
  1593. * Computes the provided Cartesian's squared magnitude.
  1594. *
  1595. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  1596. * @returns {Number} The squared magnitude.
  1597. */
  1598. Cartesian2.magnitudeSquared = function(cartesian) {
  1599. //>>includeStart('debug', pragmas.debug);
  1600. Check.Check.typeOf.object('cartesian', cartesian);
  1601. //>>includeEnd('debug');
  1602. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  1603. };
  1604. /**
  1605. * Computes the Cartesian's magnitude (length).
  1606. *
  1607. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  1608. * @returns {Number} The magnitude.
  1609. */
  1610. Cartesian2.magnitude = function(cartesian) {
  1611. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  1612. };
  1613. var distanceScratch = new Cartesian2();
  1614. /**
  1615. * Computes the distance between two points.
  1616. *
  1617. * @param {Cartesian2} left The first point to compute the distance from.
  1618. * @param {Cartesian2} right The second point to compute the distance to.
  1619. * @returns {Number} The distance between two points.
  1620. *
  1621. * @example
  1622. * // Returns 1.0
  1623. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  1624. */
  1625. Cartesian2.distance = function(left, right) {
  1626. //>>includeStart('debug', pragmas.debug);
  1627. Check.Check.typeOf.object('left', left);
  1628. Check.Check.typeOf.object('right', right);
  1629. //>>includeEnd('debug');
  1630. Cartesian2.subtract(left, right, distanceScratch);
  1631. return Cartesian2.magnitude(distanceScratch);
  1632. };
  1633. /**
  1634. * Computes the squared distance between two points. Comparing squared distances
  1635. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  1636. *
  1637. * @param {Cartesian2} left The first point to compute the distance from.
  1638. * @param {Cartesian2} right The second point to compute the distance to.
  1639. * @returns {Number} The distance between two points.
  1640. *
  1641. * @example
  1642. * // Returns 4.0, not 2.0
  1643. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  1644. */
  1645. Cartesian2.distanceSquared = function(left, right) {
  1646. //>>includeStart('debug', pragmas.debug);
  1647. Check.Check.typeOf.object('left', left);
  1648. Check.Check.typeOf.object('right', right);
  1649. //>>includeEnd('debug');
  1650. Cartesian2.subtract(left, right, distanceScratch);
  1651. return Cartesian2.magnitudeSquared(distanceScratch);
  1652. };
  1653. /**
  1654. * Computes the normalized form of the supplied Cartesian.
  1655. *
  1656. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  1657. * @param {Cartesian2} result The object onto which to store the result.
  1658. * @returns {Cartesian2} The modified result parameter.
  1659. */
  1660. Cartesian2.normalize = function(cartesian, result) {
  1661. //>>includeStart('debug', pragmas.debug);
  1662. Check.Check.typeOf.object('cartesian', cartesian);
  1663. Check.Check.typeOf.object('result', result);
  1664. //>>includeEnd('debug');
  1665. var magnitude = Cartesian2.magnitude(cartesian);
  1666. result.x = cartesian.x / magnitude;
  1667. result.y = cartesian.y / magnitude;
  1668. //>>includeStart('debug', pragmas.debug);
  1669. if (isNaN(result.x) || isNaN(result.y)) {
  1670. throw new Check.DeveloperError('normalized result is not a number');
  1671. }
  1672. //>>includeEnd('debug');
  1673. return result;
  1674. };
  1675. /**
  1676. * Computes the dot (scalar) product of two Cartesians.
  1677. *
  1678. * @param {Cartesian2} left The first Cartesian.
  1679. * @param {Cartesian2} right The second Cartesian.
  1680. * @returns {Number} The dot product.
  1681. */
  1682. Cartesian2.dot = function(left, right) {
  1683. //>>includeStart('debug', pragmas.debug);
  1684. Check.Check.typeOf.object('left', left);
  1685. Check.Check.typeOf.object('right', right);
  1686. //>>includeEnd('debug');
  1687. return left.x * right.x + left.y * right.y;
  1688. };
  1689. /**
  1690. * Computes the componentwise product of two Cartesians.
  1691. *
  1692. * @param {Cartesian2} left The first Cartesian.
  1693. * @param {Cartesian2} right The second Cartesian.
  1694. * @param {Cartesian2} result The object onto which to store the result.
  1695. * @returns {Cartesian2} The modified result parameter.
  1696. */
  1697. Cartesian2.multiplyComponents = function(left, right, result) {
  1698. //>>includeStart('debug', pragmas.debug);
  1699. Check.Check.typeOf.object('left', left);
  1700. Check.Check.typeOf.object('right', right);
  1701. Check.Check.typeOf.object('result', result);
  1702. //>>includeEnd('debug');
  1703. result.x = left.x * right.x;
  1704. result.y = left.y * right.y;
  1705. return result;
  1706. };
  1707. /**
  1708. * Computes the componentwise quotient of two Cartesians.
  1709. *
  1710. * @param {Cartesian2} left The first Cartesian.
  1711. * @param {Cartesian2} right The second Cartesian.
  1712. * @param {Cartesian2} result The object onto which to store the result.
  1713. * @returns {Cartesian2} The modified result parameter.
  1714. */
  1715. Cartesian2.divideComponents = function(left, right, result) {
  1716. //>>includeStart('debug', pragmas.debug);
  1717. Check.Check.typeOf.object('left', left);
  1718. Check.Check.typeOf.object('right', right);
  1719. Check.Check.typeOf.object('result', result);
  1720. //>>includeEnd('debug');
  1721. result.x = left.x / right.x;
  1722. result.y = left.y / right.y;
  1723. return result;
  1724. };
  1725. /**
  1726. * Computes the componentwise sum of two Cartesians.
  1727. *
  1728. * @param {Cartesian2} left The first Cartesian.
  1729. * @param {Cartesian2} right The second Cartesian.
  1730. * @param {Cartesian2} result The object onto which to store the result.
  1731. * @returns {Cartesian2} The modified result parameter.
  1732. */
  1733. Cartesian2.add = function(left, right, result) {
  1734. //>>includeStart('debug', pragmas.debug);
  1735. Check.Check.typeOf.object('left', left);
  1736. Check.Check.typeOf.object('right', right);
  1737. Check.Check.typeOf.object('result', result);
  1738. //>>includeEnd('debug');
  1739. result.x = left.x + right.x;
  1740. result.y = left.y + right.y;
  1741. return result;
  1742. };
  1743. /**
  1744. * Computes the componentwise difference of two Cartesians.
  1745. *
  1746. * @param {Cartesian2} left The first Cartesian.
  1747. * @param {Cartesian2} right The second Cartesian.
  1748. * @param {Cartesian2} result The object onto which to store the result.
  1749. * @returns {Cartesian2} The modified result parameter.
  1750. */
  1751. Cartesian2.subtract = function(left, right, result) {
  1752. //>>includeStart('debug', pragmas.debug);
  1753. Check.Check.typeOf.object('left', left);
  1754. Check.Check.typeOf.object('right', right);
  1755. Check.Check.typeOf.object('result', result);
  1756. //>>includeEnd('debug');
  1757. result.x = left.x - right.x;
  1758. result.y = left.y - right.y;
  1759. return result;
  1760. };
  1761. /**
  1762. * Multiplies the provided Cartesian componentwise by the provided scalar.
  1763. *
  1764. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  1765. * @param {Number} scalar The scalar to multiply with.
  1766. * @param {Cartesian2} result The object onto which to store the result.
  1767. * @returns {Cartesian2} The modified result parameter.
  1768. */
  1769. Cartesian2.multiplyByScalar = function(cartesian, scalar, result) {
  1770. //>>includeStart('debug', pragmas.debug);
  1771. Check.Check.typeOf.object('cartesian', cartesian);
  1772. Check.Check.typeOf.number('scalar', scalar);
  1773. Check.Check.typeOf.object('result', result);
  1774. //>>includeEnd('debug');
  1775. result.x = cartesian.x * scalar;
  1776. result.y = cartesian.y * scalar;
  1777. return result;
  1778. };
  1779. /**
  1780. * Divides the provided Cartesian componentwise by the provided scalar.
  1781. *
  1782. * @param {Cartesian2} cartesian The Cartesian to be divided.
  1783. * @param {Number} scalar The scalar to divide by.
  1784. * @param {Cartesian2} result The object onto which to store the result.
  1785. * @returns {Cartesian2} The modified result parameter.
  1786. */
  1787. Cartesian2.divideByScalar = function(cartesian, scalar, result) {
  1788. //>>includeStart('debug', pragmas.debug);
  1789. Check.Check.typeOf.object('cartesian', cartesian);
  1790. Check.Check.typeOf.number('scalar', scalar);
  1791. Check.Check.typeOf.object('result', result);
  1792. //>>includeEnd('debug');
  1793. result.x = cartesian.x / scalar;
  1794. result.y = cartesian.y / scalar;
  1795. return result;
  1796. };
  1797. /**
  1798. * Negates the provided Cartesian.
  1799. *
  1800. * @param {Cartesian2} cartesian The Cartesian to be negated.
  1801. * @param {Cartesian2} result The object onto which to store the result.
  1802. * @returns {Cartesian2} The modified result parameter.
  1803. */
  1804. Cartesian2.negate = function(cartesian, result) {
  1805. //>>includeStart('debug', pragmas.debug);
  1806. Check.Check.typeOf.object('cartesian', cartesian);
  1807. Check.Check.typeOf.object('result', result);
  1808. //>>includeEnd('debug');
  1809. result.x = -cartesian.x;
  1810. result.y = -cartesian.y;
  1811. return result;
  1812. };
  1813. /**
  1814. * Computes the absolute value of the provided Cartesian.
  1815. *
  1816. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  1817. * @param {Cartesian2} result The object onto which to store the result.
  1818. * @returns {Cartesian2} The modified result parameter.
  1819. */
  1820. Cartesian2.abs = function(cartesian, result) {
  1821. //>>includeStart('debug', pragmas.debug);
  1822. Check.Check.typeOf.object('cartesian', cartesian);
  1823. Check.Check.typeOf.object('result', result);
  1824. //>>includeEnd('debug');
  1825. result.x = Math.abs(cartesian.x);
  1826. result.y = Math.abs(cartesian.y);
  1827. return result;
  1828. };
  1829. var lerpScratch = new Cartesian2();
  1830. /**
  1831. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  1832. *
  1833. * @param {Cartesian2} start The value corresponding to t at 0.0.
  1834. * @param {Cartesian2} end The value corresponding to t at 1.0.
  1835. * @param {Number} t The point along t at which to interpolate.
  1836. * @param {Cartesian2} result The object onto which to store the result.
  1837. * @returns {Cartesian2} The modified result parameter.
  1838. */
  1839. Cartesian2.lerp = function(start, end, t, result) {
  1840. //>>includeStart('debug', pragmas.debug);
  1841. Check.Check.typeOf.object('start', start);
  1842. Check.Check.typeOf.object('end', end);
  1843. Check.Check.typeOf.number('t', t);
  1844. Check.Check.typeOf.object('result', result);
  1845. //>>includeEnd('debug');
  1846. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  1847. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  1848. return Cartesian2.add(lerpScratch, result, result);
  1849. };
  1850. var angleBetweenScratch = new Cartesian2();
  1851. var angleBetweenScratch2 = new Cartesian2();
  1852. /**
  1853. * Returns the angle, in radians, between the provided Cartesians.
  1854. *
  1855. * @param {Cartesian2} left The first Cartesian.
  1856. * @param {Cartesian2} right The second Cartesian.
  1857. * @returns {Number} The angle between the Cartesians.
  1858. */
  1859. Cartesian2.angleBetween = function(left, right) {
  1860. //>>includeStart('debug', pragmas.debug);
  1861. Check.Check.typeOf.object('left', left);
  1862. Check.Check.typeOf.object('right', right);
  1863. //>>includeEnd('debug');
  1864. Cartesian2.normalize(left, angleBetweenScratch);
  1865. Cartesian2.normalize(right, angleBetweenScratch2);
  1866. return _Math.CesiumMath.acosClamped(Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2));
  1867. };
  1868. var mostOrthogonalAxisScratch = new Cartesian2();
  1869. /**
  1870. * Returns the axis that is most orthogonal to the provided Cartesian.
  1871. *
  1872. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  1873. * @param {Cartesian2} result The object onto which to store the result.
  1874. * @returns {Cartesian2} The most orthogonal axis.
  1875. */
  1876. Cartesian2.mostOrthogonalAxis = function(cartesian, result) {
  1877. //>>includeStart('debug', pragmas.debug);
  1878. Check.Check.typeOf.object('cartesian', cartesian);
  1879. Check.Check.typeOf.object('result', result);
  1880. //>>includeEnd('debug');
  1881. var f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  1882. Cartesian2.abs(f, f);
  1883. if (f.x <= f.y) {
  1884. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  1885. } else {
  1886. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  1887. }
  1888. return result;
  1889. };
  1890. /**
  1891. * Compares the provided Cartesians componentwise and returns
  1892. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1893. *
  1894. * @param {Cartesian2} [left] The first Cartesian.
  1895. * @param {Cartesian2} [right] The second Cartesian.
  1896. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1897. */
  1898. Cartesian2.equals = function(left, right) {
  1899. return (left === right) ||
  1900. ((when.defined(left)) &&
  1901. (when.defined(right)) &&
  1902. (left.x === right.x) &&
  1903. (left.y === right.y));
  1904. };
  1905. /**
  1906. * @private
  1907. */
  1908. Cartesian2.equalsArray = function(cartesian, array, offset) {
  1909. return cartesian.x === array[offset] &&
  1910. cartesian.y === array[offset + 1];
  1911. };
  1912. /**
  1913. * Compares the provided Cartesians componentwise and returns
  1914. * <code>true</code> if they pass an absolute or relative tolerance test,
  1915. * <code>false</code> otherwise.
  1916. *
  1917. * @param {Cartesian2} [left] The first Cartesian.
  1918. * @param {Cartesian2} [right] The second Cartesian.
  1919. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1920. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1921. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1922. */
  1923. Cartesian2.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  1924. return (left === right) ||
  1925. (when.defined(left) &&
  1926. when.defined(right) &&
  1927. _Math.CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  1928. _Math.CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon));
  1929. };
  1930. /**
  1931. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  1932. *
  1933. * @type {Cartesian2}
  1934. * @constant
  1935. */
  1936. Cartesian2.ZERO = Object.freeze(new Cartesian2(0.0, 0.0));
  1937. /**
  1938. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  1939. *
  1940. * @type {Cartesian2}
  1941. * @constant
  1942. */
  1943. Cartesian2.UNIT_X = Object.freeze(new Cartesian2(1.0, 0.0));
  1944. /**
  1945. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  1946. *
  1947. * @type {Cartesian2}
  1948. * @constant
  1949. */
  1950. Cartesian2.UNIT_Y = Object.freeze(new Cartesian2(0.0, 1.0));
  1951. /**
  1952. * Duplicates this Cartesian2 instance.
  1953. *
  1954. * @param {Cartesian2} [result] The object onto which to store the result.
  1955. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1956. */
  1957. Cartesian2.prototype.clone = function(result) {
  1958. return Cartesian2.clone(this, result);
  1959. };
  1960. /**
  1961. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1962. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1963. *
  1964. * @param {Cartesian2} [right] The right hand side Cartesian.
  1965. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1966. */
  1967. Cartesian2.prototype.equals = function(right) {
  1968. return Cartesian2.equals(this, right);
  1969. };
  1970. /**
  1971. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1972. * <code>true</code> if they pass an absolute or relative tolerance test,
  1973. * <code>false</code> otherwise.
  1974. *
  1975. * @param {Cartesian2} [right] The right hand side Cartesian.
  1976. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1977. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1978. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1979. */
  1980. Cartesian2.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  1981. return Cartesian2.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  1982. };
  1983. /**
  1984. * Creates a string representing this Cartesian in the format '(x, y)'.
  1985. *
  1986. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  1987. */
  1988. Cartesian2.prototype.toString = function() {
  1989. return '(' + this.x + ', ' + this.y + ')';
  1990. };
  1991. exports.Cartesian2 = Cartesian2;
  1992. exports.Ellipsoid = Ellipsoid;
  1993. exports.Rectangle = Rectangle;
  1994. });