Cartesian4.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 4D Cartesian point.
  8. * @alias Cartesian4
  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. * @param {Number} [w=0.0] The W component.
  15. *
  16. * @see Cartesian2
  17. * @see Cartesian3
  18. * @see Packable
  19. */
  20. function Cartesian4(x, y, z, w) {
  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. * The W component.
  41. * @type {Number}
  42. * @default 0.0
  43. */
  44. this.w = defaultValue(w, 0.0);
  45. }
  46. /**
  47. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  48. *
  49. * @param {Number} x The x coordinate.
  50. * @param {Number} y The y coordinate.
  51. * @param {Number} z The z coordinate.
  52. * @param {Number} w The w coordinate.
  53. * @param {Cartesian4} [result] The object onto which to store the result.
  54. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  55. */
  56. Cartesian4.fromElements = function (x, y, z, w, result) {
  57. if (!defined(result)) {
  58. return new Cartesian4(x, y, z, w);
  59. }
  60. result.x = x;
  61. result.y = y;
  62. result.z = z;
  63. result.w = w;
  64. return result;
  65. };
  66. /**
  67. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  68. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  69. *
  70. * @param {Color} color The source color.
  71. * @param {Cartesian4} [result] The object onto which to store the result.
  72. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  73. */
  74. Cartesian4.fromColor = function (color, result) {
  75. //>>includeStart('debug', pragmas.debug);
  76. Check.typeOf.object("color", color);
  77. //>>includeEnd('debug');
  78. if (!defined(result)) {
  79. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  80. }
  81. result.x = color.red;
  82. result.y = color.green;
  83. result.z = color.blue;
  84. result.w = color.alpha;
  85. return result;
  86. };
  87. /**
  88. * Duplicates a Cartesian4 instance.
  89. *
  90. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  91. * @param {Cartesian4} [result] The object onto which to store the result.
  92. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  93. */
  94. Cartesian4.clone = function (cartesian, result) {
  95. if (!defined(cartesian)) {
  96. return undefined;
  97. }
  98. if (!defined(result)) {
  99. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  100. }
  101. result.x = cartesian.x;
  102. result.y = cartesian.y;
  103. result.z = cartesian.z;
  104. result.w = cartesian.w;
  105. return result;
  106. };
  107. /**
  108. * The number of elements used to pack the object into an array.
  109. * @type {Number}
  110. */
  111. Cartesian4.packedLength = 4;
  112. /**
  113. * Stores the provided instance into the provided array.
  114. *
  115. * @param {Cartesian4} value The value to pack.
  116. * @param {Number[]} array The array to pack into.
  117. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  118. *
  119. * @returns {Number[]} The array that was packed into
  120. */
  121. Cartesian4.pack = function (value, array, startingIndex) {
  122. //>>includeStart('debug', pragmas.debug);
  123. Check.typeOf.object("value", value);
  124. Check.defined("array", array);
  125. //>>includeEnd('debug');
  126. startingIndex = defaultValue(startingIndex, 0);
  127. array[startingIndex++] = value.x;
  128. array[startingIndex++] = value.y;
  129. array[startingIndex++] = value.z;
  130. array[startingIndex] = value.w;
  131. return array;
  132. };
  133. /**
  134. * Retrieves an instance from a packed array.
  135. *
  136. * @param {Number[]} array The packed array.
  137. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  138. * @param {Cartesian4} [result] The object into which to store the result.
  139. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  140. */
  141. Cartesian4.unpack = function (array, startingIndex, result) {
  142. //>>includeStart('debug', pragmas.debug);
  143. Check.defined("array", array);
  144. //>>includeEnd('debug');
  145. startingIndex = defaultValue(startingIndex, 0);
  146. if (!defined(result)) {
  147. result = new Cartesian4();
  148. }
  149. result.x = array[startingIndex++];
  150. result.y = array[startingIndex++];
  151. result.z = array[startingIndex++];
  152. result.w = array[startingIndex];
  153. return result;
  154. };
  155. /**
  156. * Flattens an array of Cartesian4s into and array of components.
  157. *
  158. * @param {Cartesian4[]} 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 * 4 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 4) elements.
  160. * @returns {Number[]} The packed array.
  161. */
  162. Cartesian4.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 * 4;
  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 * 4 elements"
  173. );
  174. } else if (result.length !== resultLength) {
  175. result.length = resultLength;
  176. }
  177. for (var i = 0; i < length; ++i) {
  178. Cartesian4.pack(array[i], result, i * 4);
  179. }
  180. return result;
  181. };
  182. /**
  183. * Unpacks an array of cartesian components into and array of Cartesian4s.
  184. *
  185. * @param {Number[]} array The array of components to unpack.
  186. * @param {Cartesian4[]} [result] The array onto which to store the result.
  187. * @returns {Cartesian4[]} The unpacked array.
  188. */
  189. Cartesian4.unpackArray = function (array, result) {
  190. //>>includeStart('debug', pragmas.debug);
  191. Check.defined("array", array);
  192. Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  193. if (array.length % 4 !== 0) {
  194. throw new DeveloperError("array length must be a multiple of 4.");
  195. }
  196. //>>includeEnd('debug');
  197. var length = array.length;
  198. if (!defined(result)) {
  199. result = new Array(length / 4);
  200. } else {
  201. result.length = length / 4;
  202. }
  203. for (var i = 0; i < length; i += 4) {
  204. var index = i / 4;
  205. result[index] = Cartesian4.unpack(array, i, result[index]);
  206. }
  207. return result;
  208. };
  209. /**
  210. * Creates a Cartesian4 from four consecutive elements in an array.
  211. * @function
  212. *
  213. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  214. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  215. * @param {Cartesian4} [result] The object onto which to store the result.
  216. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  217. *
  218. * @example
  219. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  220. * var v = [1.0, 2.0, 3.0, 4.0];
  221. * var p = Cesium.Cartesian4.fromArray(v);
  222. *
  223. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  224. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  225. * var p2 = Cesium.Cartesian4.fromArray(v2, 2);
  226. */
  227. Cartesian4.fromArray = Cartesian4.unpack;
  228. /**
  229. * Computes the value of the maximum component for the supplied Cartesian.
  230. *
  231. * @param {Cartesian4} cartesian The cartesian to use.
  232. * @returns {Number} The value of the maximum component.
  233. */
  234. Cartesian4.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, cartesian.w);
  239. };
  240. /**
  241. * Computes the value of the minimum component for the supplied Cartesian.
  242. *
  243. * @param {Cartesian4} cartesian The cartesian to use.
  244. * @returns {Number} The value of the minimum component.
  245. */
  246. Cartesian4.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, cartesian.w);
  251. };
  252. /**
  253. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  254. *
  255. * @param {Cartesian4} first A cartesian to compare.
  256. * @param {Cartesian4} second A cartesian to compare.
  257. * @param {Cartesian4} result The object into which to store the result.
  258. * @returns {Cartesian4} A cartesian with the minimum components.
  259. */
  260. Cartesian4.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. result.w = Math.min(first.w, second.w);
  270. return result;
  271. };
  272. /**
  273. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  274. *
  275. * @param {Cartesian4} first A cartesian to compare.
  276. * @param {Cartesian4} second A cartesian to compare.
  277. * @param {Cartesian4} result The object into which to store the result.
  278. * @returns {Cartesian4} A cartesian with the maximum components.
  279. */
  280. Cartesian4.maximumByComponent = function (first, second, result) {
  281. //>>includeStart('debug', pragmas.debug);
  282. Check.typeOf.object("first", first);
  283. Check.typeOf.object("second", second);
  284. Check.typeOf.object("result", result);
  285. //>>includeEnd('debug');
  286. result.x = Math.max(first.x, second.x);
  287. result.y = Math.max(first.y, second.y);
  288. result.z = Math.max(first.z, second.z);
  289. result.w = Math.max(first.w, second.w);
  290. return result;
  291. };
  292. /**
  293. * Computes the provided Cartesian's squared magnitude.
  294. *
  295. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  296. * @returns {Number} The squared magnitude.
  297. */
  298. Cartesian4.magnitudeSquared = function (cartesian) {
  299. //>>includeStart('debug', pragmas.debug);
  300. Check.typeOf.object("cartesian", cartesian);
  301. //>>includeEnd('debug');
  302. return (
  303. cartesian.x * cartesian.x +
  304. cartesian.y * cartesian.y +
  305. cartesian.z * cartesian.z +
  306. cartesian.w * cartesian.w
  307. );
  308. };
  309. /**
  310. * Computes the Cartesian's magnitude (length).
  311. *
  312. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  313. * @returns {Number} The magnitude.
  314. */
  315. Cartesian4.magnitude = function (cartesian) {
  316. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  317. };
  318. var distanceScratch = new Cartesian4();
  319. /**
  320. * Computes the 4-space distance between two points.
  321. *
  322. * @param {Cartesian4} left The first point to compute the distance from.
  323. * @param {Cartesian4} right The second point to compute the distance to.
  324. * @returns {Number} The distance between two points.
  325. *
  326. * @example
  327. * // Returns 1.0
  328. * var d = Cesium.Cartesian4.distance(
  329. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  330. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  331. */
  332. Cartesian4.distance = function (left, right) {
  333. //>>includeStart('debug', pragmas.debug);
  334. Check.typeOf.object("left", left);
  335. Check.typeOf.object("right", right);
  336. //>>includeEnd('debug');
  337. Cartesian4.subtract(left, right, distanceScratch);
  338. return Cartesian4.magnitude(distanceScratch);
  339. };
  340. /**
  341. * Computes the squared distance between two points. Comparing squared distances
  342. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  343. *
  344. * @param {Cartesian4} left The first point to compute the distance from.
  345. * @param {Cartesian4} right The second point to compute the distance to.
  346. * @returns {Number} The distance between two points.
  347. *
  348. * @example
  349. * // Returns 4.0, not 2.0
  350. * var d = Cesium.Cartesian4.distance(
  351. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  352. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  353. */
  354. Cartesian4.distanceSquared = function (left, right) {
  355. //>>includeStart('debug', pragmas.debug);
  356. Check.typeOf.object("left", left);
  357. Check.typeOf.object("right", right);
  358. //>>includeEnd('debug');
  359. Cartesian4.subtract(left, right, distanceScratch);
  360. return Cartesian4.magnitudeSquared(distanceScratch);
  361. };
  362. /**
  363. * Computes the normalized form of the supplied Cartesian.
  364. *
  365. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  366. * @param {Cartesian4} result The object onto which to store the result.
  367. * @returns {Cartesian4} The modified result parameter.
  368. */
  369. Cartesian4.normalize = function (cartesian, result) {
  370. //>>includeStart('debug', pragmas.debug);
  371. Check.typeOf.object("cartesian", cartesian);
  372. Check.typeOf.object("result", result);
  373. //>>includeEnd('debug');
  374. var magnitude = Cartesian4.magnitude(cartesian);
  375. result.x = cartesian.x / magnitude;
  376. result.y = cartesian.y / magnitude;
  377. result.z = cartesian.z / magnitude;
  378. result.w = cartesian.w / magnitude;
  379. //>>includeStart('debug', pragmas.debug);
  380. if (
  381. isNaN(result.x) ||
  382. isNaN(result.y) ||
  383. isNaN(result.z) ||
  384. isNaN(result.w)
  385. ) {
  386. throw new DeveloperError("normalized result is not a number");
  387. }
  388. //>>includeEnd('debug');
  389. return result;
  390. };
  391. /**
  392. * Computes the dot (scalar) product of two Cartesians.
  393. *
  394. * @param {Cartesian4} left The first Cartesian.
  395. * @param {Cartesian4} right The second Cartesian.
  396. * @returns {Number} The dot product.
  397. */
  398. Cartesian4.dot = function (left, right) {
  399. //>>includeStart('debug', pragmas.debug);
  400. Check.typeOf.object("left", left);
  401. Check.typeOf.object("right", right);
  402. //>>includeEnd('debug');
  403. return (
  404. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  405. );
  406. };
  407. /**
  408. * Computes the componentwise product of two Cartesians.
  409. *
  410. * @param {Cartesian4} left The first Cartesian.
  411. * @param {Cartesian4} right The second Cartesian.
  412. * @param {Cartesian4} result The object onto which to store the result.
  413. * @returns {Cartesian4} The modified result parameter.
  414. */
  415. Cartesian4.multiplyComponents = function (left, right, result) {
  416. //>>includeStart('debug', pragmas.debug);
  417. Check.typeOf.object("left", left);
  418. Check.typeOf.object("right", right);
  419. Check.typeOf.object("result", result);
  420. //>>includeEnd('debug');
  421. result.x = left.x * right.x;
  422. result.y = left.y * right.y;
  423. result.z = left.z * right.z;
  424. result.w = left.w * right.w;
  425. return result;
  426. };
  427. /**
  428. * Computes the componentwise quotient of two Cartesians.
  429. *
  430. * @param {Cartesian4} left The first Cartesian.
  431. * @param {Cartesian4} right The second Cartesian.
  432. * @param {Cartesian4} result The object onto which to store the result.
  433. * @returns {Cartesian4} The modified result parameter.
  434. */
  435. Cartesian4.divideComponents = function (left, right, result) {
  436. //>>includeStart('debug', pragmas.debug);
  437. Check.typeOf.object("left", left);
  438. Check.typeOf.object("right", right);
  439. Check.typeOf.object("result", result);
  440. //>>includeEnd('debug');
  441. result.x = left.x / right.x;
  442. result.y = left.y / right.y;
  443. result.z = left.z / right.z;
  444. result.w = left.w / right.w;
  445. return result;
  446. };
  447. /**
  448. * Computes the componentwise sum of two Cartesians.
  449. *
  450. * @param {Cartesian4} left The first Cartesian.
  451. * @param {Cartesian4} right The second Cartesian.
  452. * @param {Cartesian4} result The object onto which to store the result.
  453. * @returns {Cartesian4} The modified result parameter.
  454. */
  455. Cartesian4.add = function (left, right, result) {
  456. //>>includeStart('debug', pragmas.debug);
  457. Check.typeOf.object("left", left);
  458. Check.typeOf.object("right", right);
  459. Check.typeOf.object("result", result);
  460. //>>includeEnd('debug');
  461. result.x = left.x + right.x;
  462. result.y = left.y + right.y;
  463. result.z = left.z + right.z;
  464. result.w = left.w + right.w;
  465. return result;
  466. };
  467. /**
  468. * Computes the componentwise difference of two Cartesians.
  469. *
  470. * @param {Cartesian4} left The first Cartesian.
  471. * @param {Cartesian4} right The second Cartesian.
  472. * @param {Cartesian4} result The object onto which to store the result.
  473. * @returns {Cartesian4} The modified result parameter.
  474. */
  475. Cartesian4.subtract = function (left, right, result) {
  476. //>>includeStart('debug', pragmas.debug);
  477. Check.typeOf.object("left", left);
  478. Check.typeOf.object("right", right);
  479. Check.typeOf.object("result", result);
  480. //>>includeEnd('debug');
  481. result.x = left.x - right.x;
  482. result.y = left.y - right.y;
  483. result.z = left.z - right.z;
  484. result.w = left.w - right.w;
  485. return result;
  486. };
  487. /**
  488. * Multiplies the provided Cartesian componentwise by the provided scalar.
  489. *
  490. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  491. * @param {Number} scalar The scalar to multiply with.
  492. * @param {Cartesian4} result The object onto which to store the result.
  493. * @returns {Cartesian4} The modified result parameter.
  494. */
  495. Cartesian4.multiplyByScalar = 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. result.w = cartesian.w * scalar;
  505. return result;
  506. };
  507. /**
  508. * Divides the provided Cartesian componentwise by the provided scalar.
  509. *
  510. * @param {Cartesian4} cartesian The Cartesian to be divided.
  511. * @param {Number} scalar The scalar to divide by.
  512. * @param {Cartesian4} result The object onto which to store the result.
  513. * @returns {Cartesian4} The modified result parameter.
  514. */
  515. Cartesian4.divideByScalar = function (cartesian, scalar, result) {
  516. //>>includeStart('debug', pragmas.debug);
  517. Check.typeOf.object("cartesian", cartesian);
  518. Check.typeOf.number("scalar", scalar);
  519. Check.typeOf.object("result", result);
  520. //>>includeEnd('debug');
  521. result.x = cartesian.x / scalar;
  522. result.y = cartesian.y / scalar;
  523. result.z = cartesian.z / scalar;
  524. result.w = cartesian.w / scalar;
  525. return result;
  526. };
  527. /**
  528. * Negates the provided Cartesian.
  529. *
  530. * @param {Cartesian4} cartesian The Cartesian to be negated.
  531. * @param {Cartesian4} result The object onto which to store the result.
  532. * @returns {Cartesian4} The modified result parameter.
  533. */
  534. Cartesian4.negate = function (cartesian, result) {
  535. //>>includeStart('debug', pragmas.debug);
  536. Check.typeOf.object("cartesian", cartesian);
  537. Check.typeOf.object("result", result);
  538. //>>includeEnd('debug');
  539. result.x = -cartesian.x;
  540. result.y = -cartesian.y;
  541. result.z = -cartesian.z;
  542. result.w = -cartesian.w;
  543. return result;
  544. };
  545. /**
  546. * Computes the absolute value of the provided Cartesian.
  547. *
  548. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  549. * @param {Cartesian4} result The object onto which to store the result.
  550. * @returns {Cartesian4} The modified result parameter.
  551. */
  552. Cartesian4.abs = function (cartesian, result) {
  553. //>>includeStart('debug', pragmas.debug);
  554. Check.typeOf.object("cartesian", cartesian);
  555. Check.typeOf.object("result", result);
  556. //>>includeEnd('debug');
  557. result.x = Math.abs(cartesian.x);
  558. result.y = Math.abs(cartesian.y);
  559. result.z = Math.abs(cartesian.z);
  560. result.w = Math.abs(cartesian.w);
  561. return result;
  562. };
  563. var lerpScratch = new Cartesian4();
  564. /**
  565. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  566. *
  567. * @param {Cartesian4} start The value corresponding to t at 0.0.
  568. * @param {Cartesian4}end The value corresponding to t at 1.0.
  569. * @param {Number} t The point along t at which to interpolate.
  570. * @param {Cartesian4} result The object onto which to store the result.
  571. * @returns {Cartesian4} The modified result parameter.
  572. */
  573. Cartesian4.lerp = function (start, end, t, result) {
  574. //>>includeStart('debug', pragmas.debug);
  575. Check.typeOf.object("start", start);
  576. Check.typeOf.object("end", end);
  577. Check.typeOf.number("t", t);
  578. Check.typeOf.object("result", result);
  579. //>>includeEnd('debug');
  580. Cartesian4.multiplyByScalar(end, t, lerpScratch);
  581. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  582. return Cartesian4.add(lerpScratch, result, result);
  583. };
  584. var mostOrthogonalAxisScratch = new Cartesian4();
  585. /**
  586. * Returns the axis that is most orthogonal to the provided Cartesian.
  587. *
  588. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  589. * @param {Cartesian4} result The object onto which to store the result.
  590. * @returns {Cartesian4} The most orthogonal axis.
  591. */
  592. Cartesian4.mostOrthogonalAxis = function (cartesian, result) {
  593. //>>includeStart('debug', pragmas.debug);
  594. Check.typeOf.object("cartesian", cartesian);
  595. Check.typeOf.object("result", result);
  596. //>>includeEnd('debug');
  597. var f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch);
  598. Cartesian4.abs(f, f);
  599. if (f.x <= f.y) {
  600. if (f.x <= f.z) {
  601. if (f.x <= f.w) {
  602. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  603. } else {
  604. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  605. }
  606. } else if (f.z <= f.w) {
  607. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  608. } else {
  609. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  610. }
  611. } else if (f.y <= f.z) {
  612. if (f.y <= f.w) {
  613. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  614. } else {
  615. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  616. }
  617. } else if (f.z <= f.w) {
  618. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  619. } else {
  620. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  621. }
  622. return result;
  623. };
  624. /**
  625. * Compares the provided Cartesians componentwise and returns
  626. * <code>true</code> if they are equal, <code>false</code> otherwise.
  627. *
  628. * @param {Cartesian4} [left] The first Cartesian.
  629. * @param {Cartesian4} [right] The second Cartesian.
  630. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  631. */
  632. Cartesian4.equals = function (left, right) {
  633. return (
  634. left === right ||
  635. (defined(left) &&
  636. defined(right) &&
  637. left.x === right.x &&
  638. left.y === right.y &&
  639. left.z === right.z &&
  640. left.w === right.w)
  641. );
  642. };
  643. /**
  644. * @private
  645. */
  646. Cartesian4.equalsArray = function (cartesian, array, offset) {
  647. return (
  648. cartesian.x === array[offset] &&
  649. cartesian.y === array[offset + 1] &&
  650. cartesian.z === array[offset + 2] &&
  651. cartesian.w === array[offset + 3]
  652. );
  653. };
  654. /**
  655. * Compares the provided Cartesians componentwise and returns
  656. * <code>true</code> if they pass an absolute or relative tolerance test,
  657. * <code>false</code> otherwise.
  658. *
  659. * @param {Cartesian4} [left] The first Cartesian.
  660. * @param {Cartesian4} [right] The second Cartesian.
  661. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  662. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  663. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  664. */
  665. Cartesian4.equalsEpsilon = function (
  666. left,
  667. right,
  668. relativeEpsilon,
  669. absoluteEpsilon
  670. ) {
  671. return (
  672. left === right ||
  673. (defined(left) &&
  674. defined(right) &&
  675. CesiumMath.equalsEpsilon(
  676. left.x,
  677. right.x,
  678. relativeEpsilon,
  679. absoluteEpsilon
  680. ) &&
  681. CesiumMath.equalsEpsilon(
  682. left.y,
  683. right.y,
  684. relativeEpsilon,
  685. absoluteEpsilon
  686. ) &&
  687. CesiumMath.equalsEpsilon(
  688. left.z,
  689. right.z,
  690. relativeEpsilon,
  691. absoluteEpsilon
  692. ) &&
  693. CesiumMath.equalsEpsilon(
  694. left.w,
  695. right.w,
  696. relativeEpsilon,
  697. absoluteEpsilon
  698. ))
  699. );
  700. };
  701. /**
  702. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  703. *
  704. * @type {Cartesian4}
  705. * @constant
  706. */
  707. Cartesian4.ZERO = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  708. /**
  709. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  710. *
  711. * @type {Cartesian4}
  712. * @constant
  713. */
  714. Cartesian4.UNIT_X = Object.freeze(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  715. /**
  716. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  717. *
  718. * @type {Cartesian4}
  719. * @constant
  720. */
  721. Cartesian4.UNIT_Y = Object.freeze(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  722. /**
  723. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  724. *
  725. * @type {Cartesian4}
  726. * @constant
  727. */
  728. Cartesian4.UNIT_Z = Object.freeze(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  729. /**
  730. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  731. *
  732. * @type {Cartesian4}
  733. * @constant
  734. */
  735. Cartesian4.UNIT_W = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  736. /**
  737. * Duplicates this Cartesian4 instance.
  738. *
  739. * @param {Cartesian4} [result] The object onto which to store the result.
  740. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  741. */
  742. Cartesian4.prototype.clone = function (result) {
  743. return Cartesian4.clone(this, result);
  744. };
  745. /**
  746. * Compares this Cartesian against the provided Cartesian componentwise and returns
  747. * <code>true</code> if they are equal, <code>false</code> otherwise.
  748. *
  749. * @param {Cartesian4} [right] The right hand side Cartesian.
  750. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  751. */
  752. Cartesian4.prototype.equals = function (right) {
  753. return Cartesian4.equals(this, right);
  754. };
  755. /**
  756. * Compares this Cartesian against the provided Cartesian componentwise and returns
  757. * <code>true</code> if they pass an absolute or relative tolerance test,
  758. * <code>false</code> otherwise.
  759. *
  760. * @param {Cartesian4} [right] The right hand side Cartesian.
  761. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  762. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  763. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  764. */
  765. Cartesian4.prototype.equalsEpsilon = function (
  766. right,
  767. relativeEpsilon,
  768. absoluteEpsilon
  769. ) {
  770. return Cartesian4.equalsEpsilon(
  771. this,
  772. right,
  773. relativeEpsilon,
  774. absoluteEpsilon
  775. );
  776. };
  777. /**
  778. * Creates a string representing this Cartesian in the format '(x, y, z, w)'.
  779. *
  780. * @returns {String} A string representing the provided Cartesian in the format '(x, y, z, w)'.
  781. */
  782. Cartesian4.prototype.toString = function () {
  783. return "(" + this.x + ", " + this.y + ", " + this.z + ", " + this.w + ")";
  784. };
  785. var scratchFloatArray = new Float32Array(1);
  786. var SHIFT_LEFT_8 = 256.0;
  787. var SHIFT_LEFT_16 = 65536.0;
  788. var SHIFT_LEFT_24 = 16777216.0;
  789. var SHIFT_RIGHT_8 = 1.0 / SHIFT_LEFT_8;
  790. var SHIFT_RIGHT_16 = 1.0 / SHIFT_LEFT_16;
  791. var SHIFT_RIGHT_24 = 1.0 / SHIFT_LEFT_24;
  792. var BIAS = 38.0;
  793. /**
  794. * Packs an arbitrary floating point value to 4 values representable using uint8.
  795. *
  796. * @param {Number} value A floating point number
  797. * @param {Cartesian4} [result] The Cartesian4 that will contain the packed float.
  798. * @returns {Cartesian4} A Cartesian4 representing the float packed to values in x, y, z, and w.
  799. */
  800. Cartesian4.packFloat = function (value, result) {
  801. //>>includeStart('debug', pragmas.debug);
  802. Check.typeOf.number("value", value);
  803. //>>includeEnd('debug');
  804. if (!defined(result)) {
  805. result = new Cartesian4();
  806. }
  807. // Force the value to 32 bit precision
  808. scratchFloatArray[0] = value;
  809. value = scratchFloatArray[0];
  810. if (value === 0.0) {
  811. return Cartesian4.clone(Cartesian4.ZERO, result);
  812. }
  813. var sign = value < 0.0 ? 1.0 : 0.0;
  814. var exponent;
  815. if (!isFinite(value)) {
  816. value = 0.1;
  817. exponent = BIAS;
  818. } else {
  819. value = Math.abs(value);
  820. exponent = Math.floor(CesiumMath.logBase(value, 10)) + 1.0;
  821. value = value / Math.pow(10.0, exponent);
  822. }
  823. var temp = value * SHIFT_LEFT_8;
  824. result.x = Math.floor(temp);
  825. temp = (temp - result.x) * SHIFT_LEFT_8;
  826. result.y = Math.floor(temp);
  827. temp = (temp - result.y) * SHIFT_LEFT_8;
  828. result.z = Math.floor(temp);
  829. result.w = (exponent + BIAS) * 2.0 + sign;
  830. return result;
  831. };
  832. /**
  833. * Unpacks a float packed using Cartesian4.packFloat.
  834. *
  835. * @param {Cartesian4} packedFloat A Cartesian4 containing a float packed to 4 values representable using uint8.
  836. * @returns {Number} The unpacked float.
  837. * @private
  838. */
  839. Cartesian4.unpackFloat = function (packedFloat) {
  840. //>>includeStart('debug', pragmas.debug);
  841. Check.typeOf.object("packedFloat", packedFloat);
  842. //>>includeEnd('debug');
  843. var temp = packedFloat.w / 2.0;
  844. var exponent = Math.floor(temp);
  845. var sign = (temp - exponent) * 2.0;
  846. exponent = exponent - BIAS;
  847. sign = sign * 2.0 - 1.0;
  848. sign = -sign;
  849. if (exponent >= BIAS) {
  850. return sign < 0.0 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
  851. }
  852. var unpacked = sign * packedFloat.x * SHIFT_RIGHT_8;
  853. unpacked += sign * packedFloat.y * SHIFT_RIGHT_16;
  854. unpacked += sign * packedFloat.z * SHIFT_RIGHT_24;
  855. return unpacked * Math.pow(10.0, exponent);
  856. };
  857. export default Cartesian4;