Cartesian4.js 32 KB

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