Cartesian4.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 an 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. const length = array.length;
  167. const resultLength = length * 4;
  168. if (!defined(result)) {
  169. result = new Array(resultLength);
  170. } else if (!Array.isArray(result) && result.length !== resultLength) {
  171. //>>includeStart('debug', pragmas.debug);
  172. throw new DeveloperError(
  173. "If result is a typed array, it must have exactly array.length * 4 elements"
  174. );
  175. //>>includeEnd('debug');
  176. } else if (result.length !== resultLength) {
  177. result.length = resultLength;
  178. }
  179. for (let i = 0; i < length; ++i) {
  180. Cartesian4.pack(array[i], result, i * 4);
  181. }
  182. return result;
  183. };
  184. /**
  185. * Unpacks an array of cartesian components into an array of Cartesian4s.
  186. *
  187. * @param {number[]} array The array of components to unpack.
  188. * @param {Cartesian4[]} [result] The array onto which to store the result.
  189. * @returns {Cartesian4[]} The unpacked array.
  190. */
  191. Cartesian4.unpackArray = function (array, result) {
  192. //>>includeStart('debug', pragmas.debug);
  193. Check.defined("array", array);
  194. Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  195. if (array.length % 4 !== 0) {
  196. throw new DeveloperError("array length must be a multiple of 4.");
  197. }
  198. //>>includeEnd('debug');
  199. const length = array.length;
  200. if (!defined(result)) {
  201. result = new Array(length / 4);
  202. } else {
  203. result.length = length / 4;
  204. }
  205. for (let i = 0; i < length; i += 4) {
  206. const index = i / 4;
  207. result[index] = Cartesian4.unpack(array, i, result[index]);
  208. }
  209. return result;
  210. };
  211. /**
  212. * Creates a Cartesian4 from four consecutive elements in an array.
  213. * @function
  214. *
  215. * @param {number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  216. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  217. * @param {Cartesian4} [result] The object onto which to store the result.
  218. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  219. *
  220. * @example
  221. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  222. * const v = [1.0, 2.0, 3.0, 4.0];
  223. * const p = Cesium.Cartesian4.fromArray(v);
  224. *
  225. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  226. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  227. * const p2 = Cesium.Cartesian4.fromArray(v2, 2);
  228. */
  229. Cartesian4.fromArray = Cartesian4.unpack;
  230. /**
  231. * Computes the value of the maximum component for the supplied Cartesian.
  232. *
  233. * @param {Cartesian4} cartesian The cartesian to use.
  234. * @returns {number} The value of the maximum component.
  235. */
  236. Cartesian4.maximumComponent = function (cartesian) {
  237. //>>includeStart('debug', pragmas.debug);
  238. Check.typeOf.object("cartesian", cartesian);
  239. //>>includeEnd('debug');
  240. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  241. };
  242. /**
  243. * Computes the value of the minimum component for the supplied Cartesian.
  244. *
  245. * @param {Cartesian4} cartesian The cartesian to use.
  246. * @returns {number} The value of the minimum component.
  247. */
  248. Cartesian4.minimumComponent = function (cartesian) {
  249. //>>includeStart('debug', pragmas.debug);
  250. Check.typeOf.object("cartesian", cartesian);
  251. //>>includeEnd('debug');
  252. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  253. };
  254. /**
  255. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  256. *
  257. * @param {Cartesian4} first A cartesian to compare.
  258. * @param {Cartesian4} second A cartesian to compare.
  259. * @param {Cartesian4} result The object into which to store the result.
  260. * @returns {Cartesian4} A cartesian with the minimum components.
  261. */
  262. Cartesian4.minimumByComponent = function (first, second, result) {
  263. //>>includeStart('debug', pragmas.debug);
  264. Check.typeOf.object("first", first);
  265. Check.typeOf.object("second", second);
  266. Check.typeOf.object("result", result);
  267. //>>includeEnd('debug');
  268. result.x = Math.min(first.x, second.x);
  269. result.y = Math.min(first.y, second.y);
  270. result.z = Math.min(first.z, second.z);
  271. result.w = Math.min(first.w, second.w);
  272. return result;
  273. };
  274. /**
  275. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  276. *
  277. * @param {Cartesian4} first A cartesian to compare.
  278. * @param {Cartesian4} second A cartesian to compare.
  279. * @param {Cartesian4} result The object into which to store the result.
  280. * @returns {Cartesian4} A cartesian with the maximum components.
  281. */
  282. Cartesian4.maximumByComponent = function (first, second, result) {
  283. //>>includeStart('debug', pragmas.debug);
  284. Check.typeOf.object("first", first);
  285. Check.typeOf.object("second", second);
  286. Check.typeOf.object("result", result);
  287. //>>includeEnd('debug');
  288. result.x = Math.max(first.x, second.x);
  289. result.y = Math.max(first.y, second.y);
  290. result.z = Math.max(first.z, second.z);
  291. result.w = Math.max(first.w, second.w);
  292. return result;
  293. };
  294. /**
  295. * Constrain a value to lie between two values.
  296. *
  297. * @param {Cartesian4} value The value to clamp.
  298. * @param {Cartesian4} min The minimum bound.
  299. * @param {Cartesian4} max The maximum bound.
  300. * @param {Cartesian4} result The object into which to store the result.
  301. * @returns {Cartesian4} The clamped value such that min <= result <= max.
  302. */
  303. Cartesian4.clamp = function (value, min, max, result) {
  304. //>>includeStart('debug', pragmas.debug);
  305. Check.typeOf.object("value", value);
  306. Check.typeOf.object("min", min);
  307. Check.typeOf.object("max", max);
  308. Check.typeOf.object("result", result);
  309. //>>includeEnd('debug');
  310. const x = CesiumMath.clamp(value.x, min.x, max.x);
  311. const y = CesiumMath.clamp(value.y, min.y, max.y);
  312. const z = CesiumMath.clamp(value.z, min.z, max.z);
  313. const w = CesiumMath.clamp(value.w, min.w, max.w);
  314. result.x = x;
  315. result.y = y;
  316. result.z = z;
  317. result.w = w;
  318. return result;
  319. };
  320. /**
  321. * Computes the provided Cartesian's squared magnitude.
  322. *
  323. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  324. * @returns {number} The squared magnitude.
  325. */
  326. Cartesian4.magnitudeSquared = function (cartesian) {
  327. //>>includeStart('debug', pragmas.debug);
  328. Check.typeOf.object("cartesian", cartesian);
  329. //>>includeEnd('debug');
  330. return (
  331. cartesian.x * cartesian.x +
  332. cartesian.y * cartesian.y +
  333. cartesian.z * cartesian.z +
  334. cartesian.w * cartesian.w
  335. );
  336. };
  337. /**
  338. * Computes the Cartesian's magnitude (length).
  339. *
  340. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  341. * @returns {number} The magnitude.
  342. */
  343. Cartesian4.magnitude = function (cartesian) {
  344. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  345. };
  346. const distanceScratch = new Cartesian4();
  347. /**
  348. * Computes the 4-space distance between two points.
  349. *
  350. * @param {Cartesian4} left The first point to compute the distance from.
  351. * @param {Cartesian4} right The second point to compute the distance to.
  352. * @returns {number} The distance between two points.
  353. *
  354. * @example
  355. * // Returns 1.0
  356. * const d = Cesium.Cartesian4.distance(
  357. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  358. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  359. */
  360. Cartesian4.distance = function (left, right) {
  361. //>>includeStart('debug', pragmas.debug);
  362. Check.typeOf.object("left", left);
  363. Check.typeOf.object("right", right);
  364. //>>includeEnd('debug');
  365. Cartesian4.subtract(left, right, distanceScratch);
  366. return Cartesian4.magnitude(distanceScratch);
  367. };
  368. /**
  369. * Computes the squared distance between two points. Comparing squared distances
  370. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  371. *
  372. * @param {Cartesian4} left The first point to compute the distance from.
  373. * @param {Cartesian4} right The second point to compute the distance to.
  374. * @returns {number} The distance between two points.
  375. *
  376. * @example
  377. * // Returns 4.0, not 2.0
  378. * const d = Cesium.Cartesian4.distance(
  379. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  380. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  381. */
  382. Cartesian4.distanceSquared = function (left, right) {
  383. //>>includeStart('debug', pragmas.debug);
  384. Check.typeOf.object("left", left);
  385. Check.typeOf.object("right", right);
  386. //>>includeEnd('debug');
  387. Cartesian4.subtract(left, right, distanceScratch);
  388. return Cartesian4.magnitudeSquared(distanceScratch);
  389. };
  390. /**
  391. * Computes the normalized form of the supplied Cartesian.
  392. *
  393. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  394. * @param {Cartesian4} result The object onto which to store the result.
  395. * @returns {Cartesian4} The modified result parameter.
  396. */
  397. Cartesian4.normalize = function (cartesian, result) {
  398. //>>includeStart('debug', pragmas.debug);
  399. Check.typeOf.object("cartesian", cartesian);
  400. Check.typeOf.object("result", result);
  401. //>>includeEnd('debug');
  402. const magnitude = Cartesian4.magnitude(cartesian);
  403. result.x = cartesian.x / magnitude;
  404. result.y = cartesian.y / magnitude;
  405. result.z = cartesian.z / magnitude;
  406. result.w = cartesian.w / magnitude;
  407. //>>includeStart('debug', pragmas.debug);
  408. if (
  409. isNaN(result.x) ||
  410. isNaN(result.y) ||
  411. isNaN(result.z) ||
  412. isNaN(result.w)
  413. ) {
  414. throw new DeveloperError("normalized result is not a number");
  415. }
  416. //>>includeEnd('debug');
  417. return result;
  418. };
  419. /**
  420. * Computes the dot (scalar) product of two Cartesians.
  421. *
  422. * @param {Cartesian4} left The first Cartesian.
  423. * @param {Cartesian4} right The second Cartesian.
  424. * @returns {number} The dot product.
  425. */
  426. Cartesian4.dot = function (left, right) {
  427. //>>includeStart('debug', pragmas.debug);
  428. Check.typeOf.object("left", left);
  429. Check.typeOf.object("right", right);
  430. //>>includeEnd('debug');
  431. return (
  432. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  433. );
  434. };
  435. /**
  436. * Computes the componentwise product of two Cartesians.
  437. *
  438. * @param {Cartesian4} left The first Cartesian.
  439. * @param {Cartesian4} right The second Cartesian.
  440. * @param {Cartesian4} result The object onto which to store the result.
  441. * @returns {Cartesian4} The modified result parameter.
  442. */
  443. Cartesian4.multiplyComponents = function (left, right, result) {
  444. //>>includeStart('debug', pragmas.debug);
  445. Check.typeOf.object("left", left);
  446. Check.typeOf.object("right", right);
  447. Check.typeOf.object("result", result);
  448. //>>includeEnd('debug');
  449. result.x = left.x * right.x;
  450. result.y = left.y * right.y;
  451. result.z = left.z * right.z;
  452. result.w = left.w * right.w;
  453. return result;
  454. };
  455. /**
  456. * Computes the componentwise quotient of two Cartesians.
  457. *
  458. * @param {Cartesian4} left The first Cartesian.
  459. * @param {Cartesian4} right The second Cartesian.
  460. * @param {Cartesian4} result The object onto which to store the result.
  461. * @returns {Cartesian4} The modified result parameter.
  462. */
  463. Cartesian4.divideComponents = function (left, right, result) {
  464. //>>includeStart('debug', pragmas.debug);
  465. Check.typeOf.object("left", left);
  466. Check.typeOf.object("right", right);
  467. Check.typeOf.object("result", result);
  468. //>>includeEnd('debug');
  469. result.x = left.x / right.x;
  470. result.y = left.y / right.y;
  471. result.z = left.z / right.z;
  472. result.w = left.w / right.w;
  473. return result;
  474. };
  475. /**
  476. * Computes the componentwise sum of two Cartesians.
  477. *
  478. * @param {Cartesian4} left The first Cartesian.
  479. * @param {Cartesian4} right The second Cartesian.
  480. * @param {Cartesian4} result The object onto which to store the result.
  481. * @returns {Cartesian4} The modified result parameter.
  482. */
  483. Cartesian4.add = function (left, right, result) {
  484. //>>includeStart('debug', pragmas.debug);
  485. Check.typeOf.object("left", left);
  486. Check.typeOf.object("right", right);
  487. Check.typeOf.object("result", result);
  488. //>>includeEnd('debug');
  489. result.x = left.x + right.x;
  490. result.y = left.y + right.y;
  491. result.z = left.z + right.z;
  492. result.w = left.w + right.w;
  493. return result;
  494. };
  495. /**
  496. * Computes the componentwise difference of two Cartesians.
  497. *
  498. * @param {Cartesian4} left The first Cartesian.
  499. * @param {Cartesian4} right The second Cartesian.
  500. * @param {Cartesian4} result The object onto which to store the result.
  501. * @returns {Cartesian4} The modified result parameter.
  502. */
  503. Cartesian4.subtract = function (left, right, result) {
  504. //>>includeStart('debug', pragmas.debug);
  505. Check.typeOf.object("left", left);
  506. Check.typeOf.object("right", right);
  507. Check.typeOf.object("result", result);
  508. //>>includeEnd('debug');
  509. result.x = left.x - right.x;
  510. result.y = left.y - right.y;
  511. result.z = left.z - right.z;
  512. result.w = left.w - right.w;
  513. return result;
  514. };
  515. /**
  516. * Multiplies the provided Cartesian componentwise by the provided scalar.
  517. *
  518. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  519. * @param {number} scalar The scalar to multiply with.
  520. * @param {Cartesian4} result The object onto which to store the result.
  521. * @returns {Cartesian4} The modified result parameter.
  522. */
  523. Cartesian4.multiplyByScalar = function (cartesian, scalar, result) {
  524. //>>includeStart('debug', pragmas.debug);
  525. Check.typeOf.object("cartesian", cartesian);
  526. Check.typeOf.number("scalar", scalar);
  527. Check.typeOf.object("result", result);
  528. //>>includeEnd('debug');
  529. result.x = cartesian.x * scalar;
  530. result.y = cartesian.y * scalar;
  531. result.z = cartesian.z * scalar;
  532. result.w = cartesian.w * scalar;
  533. return result;
  534. };
  535. /**
  536. * Divides the provided Cartesian componentwise by the provided scalar.
  537. *
  538. * @param {Cartesian4} cartesian The Cartesian to be divided.
  539. * @param {number} scalar The scalar to divide by.
  540. * @param {Cartesian4} result The object onto which to store the result.
  541. * @returns {Cartesian4} The modified result parameter.
  542. */
  543. Cartesian4.divideByScalar = function (cartesian, scalar, result) {
  544. //>>includeStart('debug', pragmas.debug);
  545. Check.typeOf.object("cartesian", cartesian);
  546. Check.typeOf.number("scalar", scalar);
  547. Check.typeOf.object("result", result);
  548. //>>includeEnd('debug');
  549. result.x = cartesian.x / scalar;
  550. result.y = cartesian.y / scalar;
  551. result.z = cartesian.z / scalar;
  552. result.w = cartesian.w / scalar;
  553. return result;
  554. };
  555. /**
  556. * Negates the provided Cartesian.
  557. *
  558. * @param {Cartesian4} cartesian The Cartesian to be negated.
  559. * @param {Cartesian4} result The object onto which to store the result.
  560. * @returns {Cartesian4} The modified result parameter.
  561. */
  562. Cartesian4.negate = function (cartesian, result) {
  563. //>>includeStart('debug', pragmas.debug);
  564. Check.typeOf.object("cartesian", cartesian);
  565. Check.typeOf.object("result", result);
  566. //>>includeEnd('debug');
  567. result.x = -cartesian.x;
  568. result.y = -cartesian.y;
  569. result.z = -cartesian.z;
  570. result.w = -cartesian.w;
  571. return result;
  572. };
  573. /**
  574. * Computes the absolute value of the provided Cartesian.
  575. *
  576. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  577. * @param {Cartesian4} result The object onto which to store the result.
  578. * @returns {Cartesian4} The modified result parameter.
  579. */
  580. Cartesian4.abs = function (cartesian, result) {
  581. //>>includeStart('debug', pragmas.debug);
  582. Check.typeOf.object("cartesian", cartesian);
  583. Check.typeOf.object("result", result);
  584. //>>includeEnd('debug');
  585. result.x = Math.abs(cartesian.x);
  586. result.y = Math.abs(cartesian.y);
  587. result.z = Math.abs(cartesian.z);
  588. result.w = Math.abs(cartesian.w);
  589. return result;
  590. };
  591. const lerpScratch = new Cartesian4();
  592. /**
  593. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  594. *
  595. * @param {Cartesian4} start The value corresponding to t at 0.0.
  596. * @param {Cartesian4}end The value corresponding to t at 1.0.
  597. * @param {number} t The point along t at which to interpolate.
  598. * @param {Cartesian4} result The object onto which to store the result.
  599. * @returns {Cartesian4} The modified result parameter.
  600. */
  601. Cartesian4.lerp = function (start, end, t, result) {
  602. //>>includeStart('debug', pragmas.debug);
  603. Check.typeOf.object("start", start);
  604. Check.typeOf.object("end", end);
  605. Check.typeOf.number("t", t);
  606. Check.typeOf.object("result", result);
  607. //>>includeEnd('debug');
  608. Cartesian4.multiplyByScalar(end, t, lerpScratch);
  609. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  610. return Cartesian4.add(lerpScratch, result, result);
  611. };
  612. const mostOrthogonalAxisScratch = new Cartesian4();
  613. /**
  614. * Returns the axis that is most orthogonal to the provided Cartesian.
  615. *
  616. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  617. * @param {Cartesian4} result The object onto which to store the result.
  618. * @returns {Cartesian4} The most orthogonal axis.
  619. */
  620. Cartesian4.mostOrthogonalAxis = function (cartesian, result) {
  621. //>>includeStart('debug', pragmas.debug);
  622. Check.typeOf.object("cartesian", cartesian);
  623. Check.typeOf.object("result", result);
  624. //>>includeEnd('debug');
  625. const f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch);
  626. Cartesian4.abs(f, f);
  627. if (f.x <= f.y) {
  628. if (f.x <= f.z) {
  629. if (f.x <= f.w) {
  630. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  631. } else {
  632. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  633. }
  634. } else if (f.z <= f.w) {
  635. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  636. } else {
  637. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  638. }
  639. } else if (f.y <= f.z) {
  640. if (f.y <= f.w) {
  641. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  642. } else {
  643. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  644. }
  645. } else if (f.z <= f.w) {
  646. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  647. } else {
  648. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  649. }
  650. return result;
  651. };
  652. /**
  653. * Compares the provided Cartesians componentwise and returns
  654. * <code>true</code> if they are equal, <code>false</code> otherwise.
  655. *
  656. * @param {Cartesian4} [left] The first Cartesian.
  657. * @param {Cartesian4} [right] The second Cartesian.
  658. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  659. */
  660. Cartesian4.equals = function (left, right) {
  661. return (
  662. left === right ||
  663. (defined(left) &&
  664. defined(right) &&
  665. left.x === right.x &&
  666. left.y === right.y &&
  667. left.z === right.z &&
  668. left.w === right.w)
  669. );
  670. };
  671. /**
  672. * @private
  673. */
  674. Cartesian4.equalsArray = function (cartesian, array, offset) {
  675. return (
  676. cartesian.x === array[offset] &&
  677. cartesian.y === array[offset + 1] &&
  678. cartesian.z === array[offset + 2] &&
  679. cartesian.w === array[offset + 3]
  680. );
  681. };
  682. /**
  683. * Compares the provided Cartesians componentwise and returns
  684. * <code>true</code> if they pass an absolute or relative tolerance test,
  685. * <code>false</code> otherwise.
  686. *
  687. * @param {Cartesian4} [left] The first Cartesian.
  688. * @param {Cartesian4} [right] The second Cartesian.
  689. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  690. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  691. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  692. */
  693. Cartesian4.equalsEpsilon = function (
  694. left,
  695. right,
  696. relativeEpsilon,
  697. absoluteEpsilon
  698. ) {
  699. return (
  700. left === right ||
  701. (defined(left) &&
  702. defined(right) &&
  703. CesiumMath.equalsEpsilon(
  704. left.x,
  705. right.x,
  706. relativeEpsilon,
  707. absoluteEpsilon
  708. ) &&
  709. CesiumMath.equalsEpsilon(
  710. left.y,
  711. right.y,
  712. relativeEpsilon,
  713. absoluteEpsilon
  714. ) &&
  715. CesiumMath.equalsEpsilon(
  716. left.z,
  717. right.z,
  718. relativeEpsilon,
  719. absoluteEpsilon
  720. ) &&
  721. CesiumMath.equalsEpsilon(
  722. left.w,
  723. right.w,
  724. relativeEpsilon,
  725. absoluteEpsilon
  726. ))
  727. );
  728. };
  729. /**
  730. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  731. *
  732. * @type {Cartesian4}
  733. * @constant
  734. */
  735. Cartesian4.ZERO = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  736. /**
  737. * An immutable Cartesian4 instance initialized to (1.0, 1.0, 1.0, 1.0).
  738. *
  739. * @type {Cartesian4}
  740. * @constant
  741. */
  742. Cartesian4.ONE = Object.freeze(new Cartesian4(1.0, 1.0, 1.0, 1.0));
  743. /**
  744. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  745. *
  746. * @type {Cartesian4}
  747. * @constant
  748. */
  749. Cartesian4.UNIT_X = Object.freeze(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  750. /**
  751. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  752. *
  753. * @type {Cartesian4}
  754. * @constant
  755. */
  756. Cartesian4.UNIT_Y = Object.freeze(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  757. /**
  758. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  759. *
  760. * @type {Cartesian4}
  761. * @constant
  762. */
  763. Cartesian4.UNIT_Z = Object.freeze(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  764. /**
  765. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  766. *
  767. * @type {Cartesian4}
  768. * @constant
  769. */
  770. Cartesian4.UNIT_W = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  771. /**
  772. * Duplicates this Cartesian4 instance.
  773. *
  774. * @param {Cartesian4} [result] The object onto which to store the result.
  775. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  776. */
  777. Cartesian4.prototype.clone = function (result) {
  778. return Cartesian4.clone(this, result);
  779. };
  780. /**
  781. * Compares this Cartesian against the provided Cartesian componentwise and returns
  782. * <code>true</code> if they are equal, <code>false</code> otherwise.
  783. *
  784. * @param {Cartesian4} [right] The right hand side Cartesian.
  785. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  786. */
  787. Cartesian4.prototype.equals = function (right) {
  788. return Cartesian4.equals(this, right);
  789. };
  790. /**
  791. * Compares this Cartesian against the provided Cartesian componentwise and returns
  792. * <code>true</code> if they pass an absolute or relative tolerance test,
  793. * <code>false</code> otherwise.
  794. *
  795. * @param {Cartesian4} [right] The right hand side Cartesian.
  796. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  797. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  798. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  799. */
  800. Cartesian4.prototype.equalsEpsilon = function (
  801. right,
  802. relativeEpsilon,
  803. absoluteEpsilon
  804. ) {
  805. return Cartesian4.equalsEpsilon(
  806. this,
  807. right,
  808. relativeEpsilon,
  809. absoluteEpsilon
  810. );
  811. };
  812. /**
  813. * Creates a string representing this Cartesian in the format '(x, y, z, w)'.
  814. *
  815. * @returns {string} A string representing the provided Cartesian in the format '(x, y, z, w)'.
  816. */
  817. Cartesian4.prototype.toString = function () {
  818. return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`;
  819. };
  820. // scratchU8Array and scratchF32Array are views into the same buffer
  821. const scratchF32Array = new Float32Array(1);
  822. const scratchU8Array = new Uint8Array(scratchF32Array.buffer);
  823. const testU32 = new Uint32Array([0x11223344]);
  824. const testU8 = new Uint8Array(testU32.buffer);
  825. const littleEndian = testU8[0] === 0x44;
  826. /**
  827. * Packs an arbitrary floating point value to 4 values representable using uint8.
  828. *
  829. * @param {number} value A floating point number.
  830. * @param {Cartesian4} [result] The Cartesian4 that will contain the packed float.
  831. * @returns {Cartesian4} A Cartesian4 representing the float packed to values in x, y, z, and w.
  832. */
  833. Cartesian4.packFloat = function (value, result) {
  834. //>>includeStart('debug', pragmas.debug);
  835. Check.typeOf.number("value", value);
  836. //>>includeEnd('debug');
  837. if (!defined(result)) {
  838. result = new Cartesian4();
  839. }
  840. // scratchU8Array and scratchF32Array are views into the same buffer
  841. scratchF32Array[0] = value;
  842. if (littleEndian) {
  843. result.x = scratchU8Array[0];
  844. result.y = scratchU8Array[1];
  845. result.z = scratchU8Array[2];
  846. result.w = scratchU8Array[3];
  847. } else {
  848. // convert from big-endian to little-endian
  849. result.x = scratchU8Array[3];
  850. result.y = scratchU8Array[2];
  851. result.z = scratchU8Array[1];
  852. result.w = scratchU8Array[0];
  853. }
  854. return result;
  855. };
  856. /**
  857. * Unpacks a float packed using Cartesian4.packFloat.
  858. *
  859. * @param {Cartesian4} packedFloat A Cartesian4 containing a float packed to 4 values representable using uint8.
  860. * @returns {number} The unpacked float.
  861. * @private
  862. */
  863. Cartesian4.unpackFloat = function (packedFloat) {
  864. //>>includeStart('debug', pragmas.debug);
  865. Check.typeOf.object("packedFloat", packedFloat);
  866. //>>includeEnd('debug');
  867. // scratchU8Array and scratchF32Array are views into the same buffer
  868. if (littleEndian) {
  869. scratchU8Array[0] = packedFloat.x;
  870. scratchU8Array[1] = packedFloat.y;
  871. scratchU8Array[2] = packedFloat.z;
  872. scratchU8Array[3] = packedFloat.w;
  873. } else {
  874. // convert from little-endian to big-endian
  875. scratchU8Array[0] = packedFloat.w;
  876. scratchU8Array[1] = packedFloat.z;
  877. scratchU8Array[2] = packedFloat.y;
  878. scratchU8Array[3] = packedFloat.x;
  879. }
  880. return scratchF32Array[0];
  881. };
  882. export default Cartesian4;