Cartesian3.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. import Check from './Check.js';
  2. import defaultValue from './defaultValue.js';
  3. import defined from './defined.js';
  4. import DeveloperError from './DeveloperError.js';
  5. import freezeObject from './freezeObject.js';
  6. import CesiumMath from './Math.js';
  7. /**
  8. * A 3D Cartesian point.
  9. * @alias Cartesian3
  10. * @constructor
  11. *
  12. * @param {Number} [x=0.0] The X component.
  13. * @param {Number} [y=0.0] The Y component.
  14. * @param {Number} [z=0.0] The Z component.
  15. *
  16. * @see Cartesian2
  17. * @see Cartesian4
  18. * @see Packable
  19. */
  20. function Cartesian3(x, y, z) {
  21. /**
  22. * The X component.
  23. * @type {Number}
  24. * @default 0.0
  25. */
  26. this.x = defaultValue(x, 0.0);
  27. /**
  28. * The Y component.
  29. * @type {Number}
  30. * @default 0.0
  31. */
  32. this.y = defaultValue(y, 0.0);
  33. /**
  34. * The Z component.
  35. * @type {Number}
  36. * @default 0.0
  37. */
  38. this.z = defaultValue(z, 0.0);
  39. }
  40. /**
  41. * Converts the provided Spherical into Cartesian3 coordinates.
  42. *
  43. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  44. * @param {Cartesian3} [result] The object onto which to store the result.
  45. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  46. */
  47. Cartesian3.fromSpherical = function(spherical, result) {
  48. //>>includeStart('debug', pragmas.debug);
  49. Check.typeOf.object('spherical', spherical);
  50. //>>includeEnd('debug');
  51. if (!defined(result)) {
  52. result = new Cartesian3();
  53. }
  54. var clock = spherical.clock;
  55. var cone = spherical.cone;
  56. var magnitude = defaultValue(spherical.magnitude, 1.0);
  57. var radial = magnitude * Math.sin(cone);
  58. result.x = radial * Math.cos(clock);
  59. result.y = radial * Math.sin(clock);
  60. result.z = magnitude * Math.cos(cone);
  61. return result;
  62. };
  63. /**
  64. * Creates a Cartesian3 instance from x, y and z coordinates.
  65. *
  66. * @param {Number} x The x coordinate.
  67. * @param {Number} y The y coordinate.
  68. * @param {Number} z The z coordinate.
  69. * @param {Cartesian3} [result] The object onto which to store the result.
  70. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  71. */
  72. Cartesian3.fromElements = function(x, y, z, result) {
  73. if (!defined(result)) {
  74. return new Cartesian3(x, y, z);
  75. }
  76. result.x = x;
  77. result.y = y;
  78. result.z = z;
  79. return result;
  80. };
  81. /**
  82. * Duplicates a Cartesian3 instance.
  83. *
  84. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  85. * @param {Cartesian3} [result] The object onto which to store the result.
  86. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  87. */
  88. Cartesian3.clone = function(cartesian, result) {
  89. if (!defined(cartesian)) {
  90. return undefined;
  91. }
  92. if (!defined(result)) {
  93. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  94. }
  95. result.x = cartesian.x;
  96. result.y = cartesian.y;
  97. result.z = cartesian.z;
  98. return result;
  99. };
  100. /**
  101. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  102. * x, y, and z properties of the Cartesian4 and drops w.
  103. * @function
  104. *
  105. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  106. * @param {Cartesian3} [result] The object onto which to store the result.
  107. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  108. */
  109. Cartesian3.fromCartesian4 = Cartesian3.clone;
  110. /**
  111. * The number of elements used to pack the object into an array.
  112. * @type {Number}
  113. */
  114. Cartesian3.packedLength = 3;
  115. /**
  116. * Stores the provided instance into the provided array.
  117. *
  118. * @param {Cartesian3} value The value to pack.
  119. * @param {Number[]} array The array to pack into.
  120. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  121. *
  122. * @returns {Number[]} The array that was packed into
  123. */
  124. Cartesian3.pack = function(value, array, startingIndex) {
  125. //>>includeStart('debug', pragmas.debug);
  126. Check.typeOf.object('value', value);
  127. Check.defined('array', array);
  128. //>>includeEnd('debug');
  129. startingIndex = defaultValue(startingIndex, 0);
  130. array[startingIndex++] = value.x;
  131. array[startingIndex++] = value.y;
  132. array[startingIndex] = value.z;
  133. return array;
  134. };
  135. /**
  136. * Retrieves an instance from a packed array.
  137. *
  138. * @param {Number[]} array The packed array.
  139. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  140. * @param {Cartesian3} [result] The object into which to store the result.
  141. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  142. */
  143. Cartesian3.unpack = function(array, startingIndex, result) {
  144. //>>includeStart('debug', pragmas.debug);
  145. Check.defined('array', array);
  146. //>>includeEnd('debug');
  147. startingIndex = defaultValue(startingIndex, 0);
  148. if (!defined(result)) {
  149. result = new Cartesian3();
  150. }
  151. result.x = array[startingIndex++];
  152. result.y = array[startingIndex++];
  153. result.z = array[startingIndex];
  154. return result;
  155. };
  156. /**
  157. * Flattens an array of Cartesian3s into an array of components.
  158. *
  159. * @param {Cartesian3[]} array The array of cartesians to pack.
  160. * @param {Number[]} [result] The array onto which to store the result.
  161. * @returns {Number[]} The packed array.
  162. */
  163. Cartesian3.packArray = function(array, result) {
  164. //>>includeStart('debug', pragmas.debug);
  165. Check.defined('array', array);
  166. //>>includeEnd('debug');
  167. var length = array.length;
  168. if (!defined(result)) {
  169. result = new Array(length * 3);
  170. } else {
  171. result.length = length * 3;
  172. }
  173. for (var i = 0; i < length; ++i) {
  174. Cartesian3.pack(array[i], result, i * 3);
  175. }
  176. return result;
  177. };
  178. /**
  179. * Unpacks an array of cartesian components into an array of Cartesian3s.
  180. *
  181. * @param {Number[]} array The array of components to unpack.
  182. * @param {Cartesian3[]} [result] The array onto which to store the result.
  183. * @returns {Cartesian3[]} The unpacked array.
  184. */
  185. Cartesian3.unpackArray = function(array, result) {
  186. //>>includeStart('debug', pragmas.debug);
  187. Check.defined('array', array);
  188. Check.typeOf.number.greaterThanOrEquals('array.length', array.length, 3);
  189. if (array.length % 3 !== 0) {
  190. throw new DeveloperError('array length must be a multiple of 3.');
  191. }
  192. //>>includeEnd('debug');
  193. var length = array.length;
  194. if (!defined(result)) {
  195. result = new Array(length / 3);
  196. } else {
  197. result.length = length / 3;
  198. }
  199. for (var i = 0; i < length; i += 3) {
  200. var index = i / 3;
  201. result[index] = Cartesian3.unpack(array, i, result[index]);
  202. }
  203. return result;
  204. };
  205. /**
  206. * Creates a Cartesian3 from three consecutive elements in an array.
  207. * @function
  208. *
  209. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  210. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  211. * @param {Cartesian3} [result] The object onto which to store the result.
  212. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  213. *
  214. * @example
  215. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  216. * var v = [1.0, 2.0, 3.0];
  217. * var p = Cesium.Cartesian3.fromArray(v);
  218. *
  219. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  220. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  221. * var p2 = Cesium.Cartesian3.fromArray(v2, 2);
  222. */
  223. Cartesian3.fromArray = Cartesian3.unpack;
  224. /**
  225. * Computes the value of the maximum component for the supplied Cartesian.
  226. *
  227. * @param {Cartesian3} cartesian The cartesian to use.
  228. * @returns {Number} The value of the maximum component.
  229. */
  230. Cartesian3.maximumComponent = function(cartesian) {
  231. //>>includeStart('debug', pragmas.debug);
  232. Check.typeOf.object('cartesian', cartesian);
  233. //>>includeEnd('debug');
  234. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  235. };
  236. /**
  237. * Computes the value of the minimum component for the supplied Cartesian.
  238. *
  239. * @param {Cartesian3} cartesian The cartesian to use.
  240. * @returns {Number} The value of the minimum component.
  241. */
  242. Cartesian3.minimumComponent = function(cartesian) {
  243. //>>includeStart('debug', pragmas.debug);
  244. Check.typeOf.object('cartesian', cartesian);
  245. //>>includeEnd('debug');
  246. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  247. };
  248. /**
  249. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  250. *
  251. * @param {Cartesian3} first A cartesian to compare.
  252. * @param {Cartesian3} second A cartesian to compare.
  253. * @param {Cartesian3} result The object into which to store the result.
  254. * @returns {Cartesian3} A cartesian with the minimum components.
  255. */
  256. Cartesian3.minimumByComponent = function(first, second, result) {
  257. //>>includeStart('debug', pragmas.debug);
  258. Check.typeOf.object('first', first);
  259. Check.typeOf.object('second', second);
  260. Check.typeOf.object('result', result);
  261. //>>includeEnd('debug');
  262. result.x = Math.min(first.x, second.x);
  263. result.y = Math.min(first.y, second.y);
  264. result.z = Math.min(first.z, second.z);
  265. return result;
  266. };
  267. /**
  268. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  269. *
  270. * @param {Cartesian3} first A cartesian to compare.
  271. * @param {Cartesian3} second A cartesian to compare.
  272. * @param {Cartesian3} result The object into which to store the result.
  273. * @returns {Cartesian3} A cartesian with the maximum components.
  274. */
  275. Cartesian3.maximumByComponent = function(first, second, result) {
  276. //>>includeStart('debug', pragmas.debug);
  277. Check.typeOf.object('first', first);
  278. Check.typeOf.object('second', second);
  279. Check.typeOf.object('result', result);
  280. //>>includeEnd('debug');
  281. result.x = Math.max(first.x, second.x);
  282. result.y = Math.max(first.y, second.y);
  283. result.z = Math.max(first.z, second.z);
  284. return result;
  285. };
  286. /**
  287. * Computes the provided Cartesian's squared magnitude.
  288. *
  289. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  290. * @returns {Number} The squared magnitude.
  291. */
  292. Cartesian3.magnitudeSquared = function(cartesian) {
  293. //>>includeStart('debug', pragmas.debug);
  294. Check.typeOf.object('cartesian', cartesian);
  295. //>>includeEnd('debug');
  296. return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z;
  297. };
  298. /**
  299. * Computes the Cartesian's magnitude (length).
  300. *
  301. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  302. * @returns {Number} The magnitude.
  303. */
  304. Cartesian3.magnitude = function(cartesian) {
  305. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  306. };
  307. var distanceScratch = new Cartesian3();
  308. /**
  309. * Computes the distance between two points.
  310. *
  311. * @param {Cartesian3} left The first point to compute the distance from.
  312. * @param {Cartesian3} right The second point to compute the distance to.
  313. * @returns {Number} The distance between two points.
  314. *
  315. * @example
  316. * // Returns 1.0
  317. * var d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  318. */
  319. Cartesian3.distance = function(left, right) {
  320. //>>includeStart('debug', pragmas.debug);
  321. Check.typeOf.object('left', left);
  322. Check.typeOf.object('right', right);
  323. //>>includeEnd('debug');
  324. Cartesian3.subtract(left, right, distanceScratch);
  325. return Cartesian3.magnitude(distanceScratch);
  326. };
  327. /**
  328. * Computes the squared distance between two points. Comparing squared distances
  329. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  330. *
  331. * @param {Cartesian3} left The first point to compute the distance from.
  332. * @param {Cartesian3} right The second point to compute the distance to.
  333. * @returns {Number} The distance between two points.
  334. *
  335. * @example
  336. * // Returns 4.0, not 2.0
  337. * var d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  338. */
  339. Cartesian3.distanceSquared = function(left, right) {
  340. //>>includeStart('debug', pragmas.debug);
  341. Check.typeOf.object('left', left);
  342. Check.typeOf.object('right', right);
  343. //>>includeEnd('debug');
  344. Cartesian3.subtract(left, right, distanceScratch);
  345. return Cartesian3.magnitudeSquared(distanceScratch);
  346. };
  347. /**
  348. * Computes the normalized form of the supplied Cartesian.
  349. *
  350. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  351. * @param {Cartesian3} result The object onto which to store the result.
  352. * @returns {Cartesian3} The modified result parameter.
  353. */
  354. Cartesian3.normalize = function(cartesian, result) {
  355. //>>includeStart('debug', pragmas.debug);
  356. Check.typeOf.object('cartesian', cartesian);
  357. Check.typeOf.object('result', result);
  358. //>>includeEnd('debug');
  359. var magnitude = Cartesian3.magnitude(cartesian);
  360. result.x = cartesian.x / magnitude;
  361. result.y = cartesian.y / magnitude;
  362. result.z = cartesian.z / magnitude;
  363. //>>includeStart('debug', pragmas.debug);
  364. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  365. throw new DeveloperError('normalized result is not a number');
  366. }
  367. //>>includeEnd('debug');
  368. return result;
  369. };
  370. /**
  371. * Computes the dot (scalar) product of two Cartesians.
  372. *
  373. * @param {Cartesian3} left The first Cartesian.
  374. * @param {Cartesian3} right The second Cartesian.
  375. * @returns {Number} The dot product.
  376. */
  377. Cartesian3.dot = function(left, right) {
  378. //>>includeStart('debug', pragmas.debug);
  379. Check.typeOf.object('left', left);
  380. Check.typeOf.object('right', right);
  381. //>>includeEnd('debug');
  382. return left.x * right.x + left.y * right.y + left.z * right.z;
  383. };
  384. /**
  385. * Computes the componentwise product of two Cartesians.
  386. *
  387. * @param {Cartesian3} left The first Cartesian.
  388. * @param {Cartesian3} right The second Cartesian.
  389. * @param {Cartesian3} result The object onto which to store the result.
  390. * @returns {Cartesian3} The modified result parameter.
  391. */
  392. Cartesian3.multiplyComponents = function(left, right, result) {
  393. //>>includeStart('debug', pragmas.debug);
  394. Check.typeOf.object('left', left);
  395. Check.typeOf.object('right', right);
  396. Check.typeOf.object('result', result);
  397. //>>includeEnd('debug');
  398. result.x = left.x * right.x;
  399. result.y = left.y * right.y;
  400. result.z = left.z * right.z;
  401. return result;
  402. };
  403. /**
  404. * Computes the componentwise quotient of two Cartesians.
  405. *
  406. * @param {Cartesian3} left The first Cartesian.
  407. * @param {Cartesian3} right The second Cartesian.
  408. * @param {Cartesian3} result The object onto which to store the result.
  409. * @returns {Cartesian3} The modified result parameter.
  410. */
  411. Cartesian3.divideComponents = function(left, right, result) {
  412. //>>includeStart('debug', pragmas.debug);
  413. Check.typeOf.object('left', left);
  414. Check.typeOf.object('right', right);
  415. Check.typeOf.object('result', result);
  416. //>>includeEnd('debug');
  417. result.x = left.x / right.x;
  418. result.y = left.y / right.y;
  419. result.z = left.z / right.z;
  420. return result;
  421. };
  422. /**
  423. * Computes the componentwise sum of two Cartesians.
  424. *
  425. * @param {Cartesian3} left The first Cartesian.
  426. * @param {Cartesian3} right The second Cartesian.
  427. * @param {Cartesian3} result The object onto which to store the result.
  428. * @returns {Cartesian3} The modified result parameter.
  429. */
  430. Cartesian3.add = function(left, right, result) {
  431. //>>includeStart('debug', pragmas.debug);
  432. Check.typeOf.object('left', left);
  433. Check.typeOf.object('right', right);
  434. Check.typeOf.object('result', result);
  435. //>>includeEnd('debug');
  436. result.x = left.x + right.x;
  437. result.y = left.y + right.y;
  438. result.z = left.z + right.z;
  439. return result;
  440. };
  441. /**
  442. * Computes the componentwise difference of two Cartesians.
  443. *
  444. * @param {Cartesian3} left The first Cartesian.
  445. * @param {Cartesian3} right The second Cartesian.
  446. * @param {Cartesian3} result The object onto which to store the result.
  447. * @returns {Cartesian3} The modified result parameter.
  448. */
  449. Cartesian3.subtract = function(left, right, result) {
  450. //>>includeStart('debug', pragmas.debug);
  451. Check.typeOf.object('left', left);
  452. Check.typeOf.object('right', right);
  453. Check.typeOf.object('result', result);
  454. //>>includeEnd('debug');
  455. result.x = left.x - right.x;
  456. result.y = left.y - right.y;
  457. result.z = left.z - right.z;
  458. return result;
  459. };
  460. /**
  461. * Multiplies the provided Cartesian componentwise by the provided scalar.
  462. *
  463. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  464. * @param {Number} scalar The scalar to multiply with.
  465. * @param {Cartesian3} result The object onto which to store the result.
  466. * @returns {Cartesian3} The modified result parameter.
  467. */
  468. Cartesian3.multiplyByScalar = function(cartesian, scalar, result) {
  469. //>>includeStart('debug', pragmas.debug);
  470. Check.typeOf.object('cartesian', cartesian);
  471. Check.typeOf.number('scalar', scalar);
  472. Check.typeOf.object('result', result);
  473. //>>includeEnd('debug');
  474. result.x = cartesian.x * scalar;
  475. result.y = cartesian.y * scalar;
  476. result.z = cartesian.z * scalar;
  477. return result;
  478. };
  479. /**
  480. * Divides the provided Cartesian componentwise by the provided scalar.
  481. *
  482. * @param {Cartesian3} cartesian The Cartesian to be divided.
  483. * @param {Number} scalar The scalar to divide by.
  484. * @param {Cartesian3} result The object onto which to store the result.
  485. * @returns {Cartesian3} The modified result parameter.
  486. */
  487. Cartesian3.divideByScalar = function(cartesian, scalar, result) {
  488. //>>includeStart('debug', pragmas.debug);
  489. Check.typeOf.object('cartesian', cartesian);
  490. Check.typeOf.number('scalar', scalar);
  491. Check.typeOf.object('result', result);
  492. //>>includeEnd('debug');
  493. result.x = cartesian.x / scalar;
  494. result.y = cartesian.y / scalar;
  495. result.z = cartesian.z / scalar;
  496. return result;
  497. };
  498. /**
  499. * Negates the provided Cartesian.
  500. *
  501. * @param {Cartesian3} cartesian The Cartesian to be negated.
  502. * @param {Cartesian3} result The object onto which to store the result.
  503. * @returns {Cartesian3} The modified result parameter.
  504. */
  505. Cartesian3.negate = function(cartesian, result) {
  506. //>>includeStart('debug', pragmas.debug);
  507. Check.typeOf.object('cartesian', cartesian);
  508. Check.typeOf.object('result', result);
  509. //>>includeEnd('debug');
  510. result.x = -cartesian.x;
  511. result.y = -cartesian.y;
  512. result.z = -cartesian.z;
  513. return result;
  514. };
  515. /**
  516. * Computes the absolute value of the provided Cartesian.
  517. *
  518. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  519. * @param {Cartesian3} result The object onto which to store the result.
  520. * @returns {Cartesian3} The modified result parameter.
  521. */
  522. Cartesian3.abs = function(cartesian, result) {
  523. //>>includeStart('debug', pragmas.debug);
  524. Check.typeOf.object('cartesian', cartesian);
  525. Check.typeOf.object('result', result);
  526. //>>includeEnd('debug');
  527. result.x = Math.abs(cartesian.x);
  528. result.y = Math.abs(cartesian.y);
  529. result.z = Math.abs(cartesian.z);
  530. return result;
  531. };
  532. var lerpScratch = new Cartesian3();
  533. /**
  534. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  535. *
  536. * @param {Cartesian3} start The value corresponding to t at 0.0.
  537. * @param {Cartesian3} end The value corresponding to t at 1.0.
  538. * @param {Number} t The point along t at which to interpolate.
  539. * @param {Cartesian3} result The object onto which to store the result.
  540. * @returns {Cartesian3} The modified result parameter.
  541. */
  542. Cartesian3.lerp = function(start, end, t, result) {
  543. //>>includeStart('debug', pragmas.debug);
  544. Check.typeOf.object('start', start);
  545. Check.typeOf.object('end', end);
  546. Check.typeOf.number('t', t);
  547. Check.typeOf.object('result', result);
  548. //>>includeEnd('debug');
  549. Cartesian3.multiplyByScalar(end, t, lerpScratch);
  550. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  551. return Cartesian3.add(lerpScratch, result, result);
  552. };
  553. var angleBetweenScratch = new Cartesian3();
  554. var angleBetweenScratch2 = new Cartesian3();
  555. /**
  556. * Returns the angle, in radians, between the provided Cartesians.
  557. *
  558. * @param {Cartesian3} left The first Cartesian.
  559. * @param {Cartesian3} right The second Cartesian.
  560. * @returns {Number} The angle between the Cartesians.
  561. */
  562. Cartesian3.angleBetween = function(left, right) {
  563. //>>includeStart('debug', pragmas.debug);
  564. Check.typeOf.object('left', left);
  565. Check.typeOf.object('right', right);
  566. //>>includeEnd('debug');
  567. Cartesian3.normalize(left, angleBetweenScratch);
  568. Cartesian3.normalize(right, angleBetweenScratch2);
  569. var cosine = Cartesian3.dot(angleBetweenScratch, angleBetweenScratch2);
  570. var sine = Cartesian3.magnitude(Cartesian3.cross(angleBetweenScratch, angleBetweenScratch2, angleBetweenScratch));
  571. return Math.atan2(sine, cosine);
  572. };
  573. var mostOrthogonalAxisScratch = new Cartesian3();
  574. /**
  575. * Returns the axis that is most orthogonal to the provided Cartesian.
  576. *
  577. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  578. * @param {Cartesian3} result The object onto which to store the result.
  579. * @returns {Cartesian3} The most orthogonal axis.
  580. */
  581. Cartesian3.mostOrthogonalAxis = function(cartesian, result) {
  582. //>>includeStart('debug', pragmas.debug);
  583. Check.typeOf.object('cartesian', cartesian);
  584. Check.typeOf.object('result', result);
  585. //>>includeEnd('debug');
  586. var f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch);
  587. Cartesian3.abs(f, f);
  588. if (f.x <= f.y) {
  589. if (f.x <= f.z) {
  590. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  591. } else {
  592. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  593. }
  594. } else if (f.y <= f.z) {
  595. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  596. } else {
  597. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  598. }
  599. return result;
  600. };
  601. /**
  602. * Projects vector a onto vector b
  603. * @param {Cartesian3} a The vector that needs projecting
  604. * @param {Cartesian3} b The vector to project onto
  605. * @param {Cartesian3} result The result cartesian
  606. * @returns {Cartesian3} The modified result parameter
  607. */
  608. Cartesian3.projectVector = function(a, b, result) {
  609. //>>includeStart('debug', pragmas.debug);
  610. Check.defined('a', a);
  611. Check.defined('b', b);
  612. Check.defined('result', result);
  613. //>>includeEnd('debug');
  614. var scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
  615. return Cartesian3.multiplyByScalar(b, scalar, result);
  616. };
  617. /**
  618. * Compares the provided Cartesians componentwise and returns
  619. * <code>true</code> if they are equal, <code>false</code> otherwise.
  620. *
  621. * @param {Cartesian3} [left] The first Cartesian.
  622. * @param {Cartesian3} [right] The second Cartesian.
  623. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  624. */
  625. Cartesian3.equals = function(left, right) {
  626. return (left === right) ||
  627. ((defined(left)) &&
  628. (defined(right)) &&
  629. (left.x === right.x) &&
  630. (left.y === right.y) &&
  631. (left.z === right.z));
  632. };
  633. /**
  634. * @private
  635. */
  636. Cartesian3.equalsArray = function(cartesian, array, offset) {
  637. return cartesian.x === array[offset] &&
  638. cartesian.y === array[offset + 1] &&
  639. cartesian.z === array[offset + 2];
  640. };
  641. /**
  642. * Compares the provided Cartesians componentwise and returns
  643. * <code>true</code> if they pass an absolute or relative tolerance test,
  644. * <code>false</code> otherwise.
  645. *
  646. * @param {Cartesian3} [left] The first Cartesian.
  647. * @param {Cartesian3} [right] The second Cartesian.
  648. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  649. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  650. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  651. */
  652. Cartesian3.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  653. return (left === right) ||
  654. (defined(left) &&
  655. defined(right) &&
  656. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  657. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon) &&
  658. CesiumMath.equalsEpsilon(left.z, right.z, relativeEpsilon, absoluteEpsilon));
  659. };
  660. /**
  661. * Computes the cross (outer) product of two Cartesians.
  662. *
  663. * @param {Cartesian3} left The first Cartesian.
  664. * @param {Cartesian3} right The second Cartesian.
  665. * @param {Cartesian3} result The object onto which to store the result.
  666. * @returns {Cartesian3} The cross product.
  667. */
  668. Cartesian3.cross = function(left, right, result) {
  669. //>>includeStart('debug', pragmas.debug);
  670. Check.typeOf.object('left', left);
  671. Check.typeOf.object('right', right);
  672. Check.typeOf.object('result', result);
  673. //>>includeEnd('debug');
  674. var leftX = left.x;
  675. var leftY = left.y;
  676. var leftZ = left.z;
  677. var rightX = right.x;
  678. var rightY = right.y;
  679. var rightZ = right.z;
  680. var x = leftY * rightZ - leftZ * rightY;
  681. var y = leftZ * rightX - leftX * rightZ;
  682. var z = leftX * rightY - leftY * rightX;
  683. result.x = x;
  684. result.y = y;
  685. result.z = z;
  686. return result;
  687. };
  688. /**
  689. * Computes the midpoint between the right and left Cartesian.
  690. * @param {Cartesian3} left The first Cartesian.
  691. * @param {Cartesian3} right The second Cartesian.
  692. * @param {Cartesian3} result The object onto which to store the result.
  693. * @returns {Cartesian3} The midpoint.
  694. */
  695. Cartesian3.midpoint = function(left, right, result) {
  696. //>>includeStart('debug', pragmas.debug);
  697. Check.typeOf.object('left', left);
  698. Check.typeOf.object('right', right);
  699. Check.typeOf.object('result', result);
  700. //>>includeEnd('debug');
  701. result.x = (left.x + right.x) * 0.5;
  702. result.y = (left.y + right.y) * 0.5;
  703. result.z = (left.z + right.z) * 0.5;
  704. return result;
  705. };
  706. /**
  707. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  708. *
  709. * @param {Number} longitude The longitude, in degrees
  710. * @param {Number} latitude The latitude, in degrees
  711. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  712. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  713. * @param {Cartesian3} [result] The object onto which to store the result.
  714. * @returns {Cartesian3} The position
  715. *
  716. * @example
  717. * var position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  718. */
  719. Cartesian3.fromDegrees = function(longitude, latitude, height, ellipsoid, result) {
  720. //>>includeStart('debug', pragmas.debug);
  721. Check.typeOf.number('longitude', longitude);
  722. Check.typeOf.number('latitude', latitude);
  723. //>>includeEnd('debug');
  724. longitude = CesiumMath.toRadians(longitude);
  725. latitude = CesiumMath.toRadians(latitude);
  726. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  727. };
  728. var scratchN = new Cartesian3();
  729. var scratchK = new Cartesian3();
  730. var wgs84RadiiSquared = new Cartesian3(6378137.0 * 6378137.0, 6378137.0 * 6378137.0, 6356752.3142451793 * 6356752.3142451793);
  731. /**
  732. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  733. *
  734. * @param {Number} longitude The longitude, in radians
  735. * @param {Number} latitude The latitude, in radians
  736. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  737. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  738. * @param {Cartesian3} [result] The object onto which to store the result.
  739. * @returns {Cartesian3} The position
  740. *
  741. * @example
  742. * var position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  743. */
  744. Cartesian3.fromRadians = function(longitude, latitude, height, ellipsoid, result) {
  745. //>>includeStart('debug', pragmas.debug);
  746. Check.typeOf.number('longitude', longitude);
  747. Check.typeOf.number('latitude', latitude);
  748. //>>includeEnd('debug');
  749. height = defaultValue(height, 0.0);
  750. var radiiSquared = defined(ellipsoid) ? ellipsoid.radiiSquared : wgs84RadiiSquared;
  751. var cosLatitude = Math.cos(latitude);
  752. scratchN.x = cosLatitude * Math.cos(longitude);
  753. scratchN.y = cosLatitude * Math.sin(longitude);
  754. scratchN.z = Math.sin(latitude);
  755. scratchN = Cartesian3.normalize(scratchN, scratchN);
  756. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  757. var gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  758. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  759. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  760. if (!defined(result)) {
  761. result = new Cartesian3();
  762. }
  763. return Cartesian3.add(scratchK, scratchN, result);
  764. };
  765. /**
  766. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  767. *
  768. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  769. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  770. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  771. * @returns {Cartesian3[]} The array of positions.
  772. *
  773. * @example
  774. * var positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  775. */
  776. Cartesian3.fromDegreesArray = function(coordinates, ellipsoid, result) {
  777. //>>includeStart('debug', pragmas.debug);
  778. Check.defined('coordinates', coordinates);
  779. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  780. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  781. }
  782. //>>includeEnd('debug');
  783. var length = coordinates.length;
  784. if (!defined(result)) {
  785. result = new Array(length / 2);
  786. } else {
  787. result.length = length / 2;
  788. }
  789. for (var i = 0; i < length; i += 2) {
  790. var longitude = coordinates[i];
  791. var latitude = coordinates[i + 1];
  792. var index = i / 2;
  793. result[index] = Cartesian3.fromDegrees(longitude, latitude, 0, ellipsoid, result[index]);
  794. }
  795. return result;
  796. };
  797. /**
  798. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  799. *
  800. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  801. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  802. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  803. * @returns {Cartesian3[]} The array of positions.
  804. *
  805. * @example
  806. * var positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  807. */
  808. Cartesian3.fromRadiansArray = function(coordinates, ellipsoid, result) {
  809. //>>includeStart('debug', pragmas.debug);
  810. Check.defined('coordinates', coordinates);
  811. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  812. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  813. }
  814. //>>includeEnd('debug');
  815. var length = coordinates.length;
  816. if (!defined(result)) {
  817. result = new Array(length / 2);
  818. } else {
  819. result.length = length / 2;
  820. }
  821. for (var i = 0; i < length; i += 2) {
  822. var longitude = coordinates[i];
  823. var latitude = coordinates[i + 1];
  824. var index = i / 2;
  825. result[index] = Cartesian3.fromRadians(longitude, latitude, 0, ellipsoid, result[index]);
  826. }
  827. return result;
  828. };
  829. /**
  830. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  831. *
  832. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  833. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  834. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  835. * @returns {Cartesian3[]} The array of positions.
  836. *
  837. * @example
  838. * var positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  839. */
  840. Cartesian3.fromDegreesArrayHeights = function(coordinates, ellipsoid, result) {
  841. //>>includeStart('debug', pragmas.debug);
  842. Check.defined('coordinates', coordinates);
  843. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  844. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  845. }
  846. //>>includeEnd('debug');
  847. var length = coordinates.length;
  848. if (!defined(result)) {
  849. result = new Array(length / 3);
  850. } else {
  851. result.length = length / 3;
  852. }
  853. for (var i = 0; i < length; i += 3) {
  854. var longitude = coordinates[i];
  855. var latitude = coordinates[i + 1];
  856. var height = coordinates[i + 2];
  857. var index = i / 3;
  858. result[index] = Cartesian3.fromDegrees(longitude, latitude, height, ellipsoid, result[index]);
  859. }
  860. return result;
  861. };
  862. /**
  863. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  864. *
  865. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  866. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  867. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  868. * @returns {Cartesian3[]} The array of positions.
  869. *
  870. * @example
  871. * var positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  872. */
  873. Cartesian3.fromRadiansArrayHeights = function(coordinates, ellipsoid, result) {
  874. //>>includeStart('debug', pragmas.debug);
  875. Check.defined('coordinates', coordinates);
  876. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  877. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  878. }
  879. //>>includeEnd('debug');
  880. var length = coordinates.length;
  881. if (!defined(result)) {
  882. result = new Array(length / 3);
  883. } else {
  884. result.length = length / 3;
  885. }
  886. for (var i = 0; i < length; i += 3) {
  887. var longitude = coordinates[i];
  888. var latitude = coordinates[i + 1];
  889. var height = coordinates[i + 2];
  890. var index = i / 3;
  891. result[index] = Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result[index]);
  892. }
  893. return result;
  894. };
  895. /**
  896. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  897. *
  898. * @type {Cartesian3}
  899. * @constant
  900. */
  901. Cartesian3.ZERO = freezeObject(new Cartesian3(0.0, 0.0, 0.0));
  902. /**
  903. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  904. *
  905. * @type {Cartesian3}
  906. * @constant
  907. */
  908. Cartesian3.UNIT_X = freezeObject(new Cartesian3(1.0, 0.0, 0.0));
  909. /**
  910. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  911. *
  912. * @type {Cartesian3}
  913. * @constant
  914. */
  915. Cartesian3.UNIT_Y = freezeObject(new Cartesian3(0.0, 1.0, 0.0));
  916. /**
  917. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  918. *
  919. * @type {Cartesian3}
  920. * @constant
  921. */
  922. Cartesian3.UNIT_Z = freezeObject(new Cartesian3(0.0, 0.0, 1.0));
  923. /**
  924. * Duplicates this Cartesian3 instance.
  925. *
  926. * @param {Cartesian3} [result] The object onto which to store the result.
  927. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  928. */
  929. Cartesian3.prototype.clone = function(result) {
  930. return Cartesian3.clone(this, result);
  931. };
  932. /**
  933. * Compares this Cartesian against the provided Cartesian componentwise and returns
  934. * <code>true</code> if they are equal, <code>false</code> otherwise.
  935. *
  936. * @param {Cartesian3} [right] The right hand side Cartesian.
  937. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  938. */
  939. Cartesian3.prototype.equals = function(right) {
  940. return Cartesian3.equals(this, right);
  941. };
  942. /**
  943. * Compares this Cartesian against the provided Cartesian componentwise and returns
  944. * <code>true</code> if they pass an absolute or relative tolerance test,
  945. * <code>false</code> otherwise.
  946. *
  947. * @param {Cartesian3} [right] The right hand side Cartesian.
  948. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  949. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  950. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  951. */
  952. Cartesian3.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  953. return Cartesian3.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  954. };
  955. /**
  956. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  957. *
  958. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  959. */
  960. Cartesian3.prototype.toString = function() {
  961. return '(' + this.x + ', ' + this.y + ', ' + this.z + ')';
  962. };
  963. export default Cartesian3;