Cartesian4-5af5bb24.js 37 KB

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