Cartesian3.js 37 KB

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