Math.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. import MersenneTwister from '../ThirdParty/mersenne-twister.js';
  2. import Check from './Check.js';
  3. import defaultValue from './defaultValue.js';
  4. import defined from './defined.js';
  5. import DeveloperError from './DeveloperError.js';
  6. /**
  7. * Math functions.
  8. *
  9. * @exports CesiumMath
  10. * @alias Math
  11. */
  12. var CesiumMath = {};
  13. /**
  14. * 0.1
  15. * @type {Number}
  16. * @constant
  17. */
  18. CesiumMath.EPSILON1 = 0.1;
  19. /**
  20. * 0.01
  21. * @type {Number}
  22. * @constant
  23. */
  24. CesiumMath.EPSILON2 = 0.01;
  25. /**
  26. * 0.001
  27. * @type {Number}
  28. * @constant
  29. */
  30. CesiumMath.EPSILON3 = 0.001;
  31. /**
  32. * 0.0001
  33. * @type {Number}
  34. * @constant
  35. */
  36. CesiumMath.EPSILON4 = 0.0001;
  37. /**
  38. * 0.00001
  39. * @type {Number}
  40. * @constant
  41. */
  42. CesiumMath.EPSILON5 = 0.00001;
  43. /**
  44. * 0.000001
  45. * @type {Number}
  46. * @constant
  47. */
  48. CesiumMath.EPSILON6 = 0.000001;
  49. /**
  50. * 0.0000001
  51. * @type {Number}
  52. * @constant
  53. */
  54. CesiumMath.EPSILON7 = 0.0000001;
  55. /**
  56. * 0.00000001
  57. * @type {Number}
  58. * @constant
  59. */
  60. CesiumMath.EPSILON8 = 0.00000001;
  61. /**
  62. * 0.000000001
  63. * @type {Number}
  64. * @constant
  65. */
  66. CesiumMath.EPSILON9 = 0.000000001;
  67. /**
  68. * 0.0000000001
  69. * @type {Number}
  70. * @constant
  71. */
  72. CesiumMath.EPSILON10 = 0.0000000001;
  73. /**
  74. * 0.00000000001
  75. * @type {Number}
  76. * @constant
  77. */
  78. CesiumMath.EPSILON11 = 0.00000000001;
  79. /**
  80. * 0.000000000001
  81. * @type {Number}
  82. * @constant
  83. */
  84. CesiumMath.EPSILON12 = 0.000000000001;
  85. /**
  86. * 0.0000000000001
  87. * @type {Number}
  88. * @constant
  89. */
  90. CesiumMath.EPSILON13 = 0.0000000000001;
  91. /**
  92. * 0.00000000000001
  93. * @type {Number}
  94. * @constant
  95. */
  96. CesiumMath.EPSILON14 = 0.00000000000001;
  97. /**
  98. * 0.000000000000001
  99. * @type {Number}
  100. * @constant
  101. */
  102. CesiumMath.EPSILON15 = 0.000000000000001;
  103. /**
  104. * 0.0000000000000001
  105. * @type {Number}
  106. * @constant
  107. */
  108. CesiumMath.EPSILON16 = 0.0000000000000001;
  109. /**
  110. * 0.00000000000000001
  111. * @type {Number}
  112. * @constant
  113. */
  114. CesiumMath.EPSILON17 = 0.00000000000000001;
  115. /**
  116. * 0.000000000000000001
  117. * @type {Number}
  118. * @constant
  119. */
  120. CesiumMath.EPSILON18 = 0.000000000000000001;
  121. /**
  122. * 0.0000000000000000001
  123. * @type {Number}
  124. * @constant
  125. */
  126. CesiumMath.EPSILON19 = 0.0000000000000000001;
  127. /**
  128. * 0.00000000000000000001
  129. * @type {Number}
  130. * @constant
  131. */
  132. CesiumMath.EPSILON20 = 0.00000000000000000001;
  133. /**
  134. * 0.000000000000000000001
  135. * @type {Number}
  136. * @constant
  137. */
  138. CesiumMath.EPSILON21 = 0.000000000000000000001;
  139. /**
  140. * The gravitational parameter of the Earth in meters cubed
  141. * per second squared as defined by the WGS84 model: 3.986004418e14
  142. * @type {Number}
  143. * @constant
  144. */
  145. CesiumMath.GRAVITATIONALPARAMETER = 3.986004418e14;
  146. /**
  147. * Radius of the sun in meters: 6.955e8
  148. * @type {Number}
  149. * @constant
  150. */
  151. CesiumMath.SOLAR_RADIUS = 6.955e8;
  152. /**
  153. * The mean radius of the moon, according to the "Report of the IAU/IAG Working Group on
  154. * Cartographic Coordinates and Rotational Elements of the Planets and satellites: 2000",
  155. * Celestial Mechanics 82: 83-110, 2002.
  156. * @type {Number}
  157. * @constant
  158. */
  159. CesiumMath.LUNAR_RADIUS = 1737400.0;
  160. /**
  161. * 64 * 1024
  162. * @type {Number}
  163. * @constant
  164. */
  165. CesiumMath.SIXTY_FOUR_KILOBYTES = 64 * 1024;
  166. /**
  167. * 4 * 1024 * 1024 * 1024
  168. * @type {Number}
  169. * @constant
  170. */
  171. CesiumMath.FOUR_GIGABYTES = 4 * 1024 * 1024 * 1024;
  172. /**
  173. * Returns the sign of the value; 1 if the value is positive, -1 if the value is
  174. * negative, or 0 if the value is 0.
  175. *
  176. * @function
  177. * @param {Number} value The value to return the sign of.
  178. * @returns {Number} The sign of value.
  179. */
  180. CesiumMath.sign = defaultValue(Math.sign, function sign(value) {
  181. value = +value; // coerce to number
  182. if (value === 0 || value !== value) {
  183. // zero or NaN
  184. return value;
  185. }
  186. return value > 0 ? 1 : -1;
  187. });
  188. /**
  189. * Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
  190. * This is similar to {@link CesiumMath#sign} except that returns 1.0 instead of
  191. * 0.0 when the input value is 0.0.
  192. * @param {Number} value The value to return the sign of.
  193. * @returns {Number} The sign of value.
  194. */
  195. CesiumMath.signNotZero = function(value) {
  196. return value < 0.0 ? -1.0 : 1.0;
  197. };
  198. /**
  199. * Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMaximum]
  200. * @param {Number} value The scalar value in the range [-1.0, 1.0]
  201. * @param {Number} [rangeMaximum=255] The maximum value in the mapped range, 255 by default.
  202. * @returns {Number} A SNORM value, where 0 maps to -1.0 and rangeMaximum maps to 1.0.
  203. *
  204. * @see CesiumMath.fromSNorm
  205. */
  206. CesiumMath.toSNorm = function(value, rangeMaximum) {
  207. rangeMaximum = defaultValue(rangeMaximum, 255);
  208. return Math.round((CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMaximum);
  209. };
  210. /**
  211. * Converts a SNORM value in the range [0, rangeMaximum] to a scalar in the range [-1.0, 1.0].
  212. * @param {Number} value SNORM value in the range [0, rangeMaximum]
  213. * @param {Number} [rangeMaximum=255] The maximum value in the SNORM range, 255 by default.
  214. * @returns {Number} Scalar in the range [-1.0, 1.0].
  215. *
  216. * @see CesiumMath.toSNorm
  217. */
  218. CesiumMath.fromSNorm = function(value, rangeMaximum) {
  219. rangeMaximum = defaultValue(rangeMaximum, 255);
  220. return CesiumMath.clamp(value, 0.0, rangeMaximum) / rangeMaximum * 2.0 - 1.0;
  221. };
  222. /**
  223. * Converts a scalar value in the range [rangeMinimum, rangeMaximum] to a scalar in the range [0.0, 1.0]
  224. * @param {Number} value The scalar value in the range [rangeMinimum, rangeMaximum]
  225. * @param {Number} rangeMinimum The minimum value in the mapped range.
  226. * @param {Number} rangeMaximum The maximum value in the mapped range.
  227. * @returns {Number} A scalar value, where rangeMinimum maps to 0.0 and rangeMaximum maps to 1.0.
  228. */
  229. CesiumMath.normalize = function(value, rangeMinimum, rangeMaximum) {
  230. rangeMaximum = Math.max(rangeMaximum - rangeMinimum, 0.0);
  231. return rangeMaximum === 0.0 ? 0.0 : CesiumMath.clamp((value - rangeMinimum) / rangeMaximum, 0.0, 1.0);
  232. };
  233. /**
  234. * Returns the hyperbolic sine of a number.
  235. * The hyperbolic sine of <em>value</em> is defined to be
  236. * (<em>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></em>)/2.0
  237. * where <i>e</i> is Euler's number, approximately 2.71828183.
  238. *
  239. * <p>Special cases:
  240. * <ul>
  241. * <li>If the argument is NaN, then the result is NaN.</li>
  242. *
  243. * <li>If the argument is infinite, then the result is an infinity
  244. * with the same sign as the argument.</li>
  245. *
  246. * <li>If the argument is zero, then the result is a zero with the
  247. * same sign as the argument.</li>
  248. * </ul>
  249. *</p>
  250. *
  251. * @function
  252. * @param {Number} value The number whose hyperbolic sine is to be returned.
  253. * @returns {Number} The hyperbolic sine of <code>value</code>.
  254. */
  255. CesiumMath.sinh = defaultValue(Math.sinh, function sinh(value) {
  256. return (Math.exp(value) - Math.exp(-value)) / 2.0;
  257. });
  258. /**
  259. * Returns the hyperbolic cosine of a number.
  260. * The hyperbolic cosine of <strong>value</strong> is defined to be
  261. * (<em>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></em>)/2.0
  262. * where <i>e</i> is Euler's number, approximately 2.71828183.
  263. *
  264. * <p>Special cases:
  265. * <ul>
  266. * <li>If the argument is NaN, then the result is NaN.</li>
  267. *
  268. * <li>If the argument is infinite, then the result is positive infinity.</li>
  269. *
  270. * <li>If the argument is zero, then the result is 1.0.</li>
  271. * </ul>
  272. *</p>
  273. *
  274. * @function
  275. * @param {Number} value The number whose hyperbolic cosine is to be returned.
  276. * @returns {Number} The hyperbolic cosine of <code>value</code>.
  277. */
  278. CesiumMath.cosh = defaultValue(Math.cosh, function cosh(value) {
  279. return (Math.exp(value) + Math.exp(-value)) / 2.0;
  280. });
  281. /**
  282. * Computes the linear interpolation of two values.
  283. *
  284. * @param {Number} p The start value to interpolate.
  285. * @param {Number} q The end value to interpolate.
  286. * @param {Number} time The time of interpolation generally in the range <code>[0.0, 1.0]</code>.
  287. * @returns {Number} The linearly interpolated value.
  288. *
  289. * @example
  290. * var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0
  291. */
  292. CesiumMath.lerp = function(p, q, time) {
  293. return ((1.0 - time) * p) + (time * q);
  294. };
  295. /**
  296. * pi
  297. *
  298. * @type {Number}
  299. * @constant
  300. */
  301. CesiumMath.PI = Math.PI;
  302. /**
  303. * 1/pi
  304. *
  305. * @type {Number}
  306. * @constant
  307. */
  308. CesiumMath.ONE_OVER_PI = 1.0 / Math.PI;
  309. /**
  310. * pi/2
  311. *
  312. * @type {Number}
  313. * @constant
  314. */
  315. CesiumMath.PI_OVER_TWO = Math.PI / 2.0;
  316. /**
  317. * pi/3
  318. *
  319. * @type {Number}
  320. * @constant
  321. */
  322. CesiumMath.PI_OVER_THREE = Math.PI / 3.0;
  323. /**
  324. * pi/4
  325. *
  326. * @type {Number}
  327. * @constant
  328. */
  329. CesiumMath.PI_OVER_FOUR = Math.PI / 4.0;
  330. /**
  331. * pi/6
  332. *
  333. * @type {Number}
  334. * @constant
  335. */
  336. CesiumMath.PI_OVER_SIX = Math.PI / 6.0;
  337. /**
  338. * 3pi/2
  339. *
  340. * @type {Number}
  341. * @constant
  342. */
  343. CesiumMath.THREE_PI_OVER_TWO = 3.0 * Math.PI / 2.0;
  344. /**
  345. * 2pi
  346. *
  347. * @type {Number}
  348. * @constant
  349. */
  350. CesiumMath.TWO_PI = 2.0 * Math.PI;
  351. /**
  352. * 1/2pi
  353. *
  354. * @type {Number}
  355. * @constant
  356. */
  357. CesiumMath.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI);
  358. /**
  359. * The number of radians in a degree.
  360. *
  361. * @type {Number}
  362. * @constant
  363. * @default Math.PI / 180.0
  364. */
  365. CesiumMath.RADIANS_PER_DEGREE = Math.PI / 180.0;
  366. /**
  367. * The number of degrees in a radian.
  368. *
  369. * @type {Number}
  370. * @constant
  371. * @default 180.0 / Math.PI
  372. */
  373. CesiumMath.DEGREES_PER_RADIAN = 180.0 / Math.PI;
  374. /**
  375. * The number of radians in an arc second.
  376. *
  377. * @type {Number}
  378. * @constant
  379. * @default {@link CesiumMath.RADIANS_PER_DEGREE} / 3600.0
  380. */
  381. CesiumMath.RADIANS_PER_ARCSECOND = CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  382. /**
  383. * Converts degrees to radians.
  384. * @param {Number} degrees The angle to convert in degrees.
  385. * @returns {Number} The corresponding angle in radians.
  386. */
  387. CesiumMath.toRadians = function(degrees) {
  388. //>>includeStart('debug', pragmas.debug);
  389. if (!defined(degrees)) {
  390. throw new DeveloperError('degrees is required.');
  391. }
  392. //>>includeEnd('debug');
  393. return degrees * CesiumMath.RADIANS_PER_DEGREE;
  394. };
  395. /**
  396. * Converts radians to degrees.
  397. * @param {Number} radians The angle to convert in radians.
  398. * @returns {Number} The corresponding angle in degrees.
  399. */
  400. CesiumMath.toDegrees = function(radians) {
  401. //>>includeStart('debug', pragmas.debug);
  402. if (!defined(radians)) {
  403. throw new DeveloperError('radians is required.');
  404. }
  405. //>>includeEnd('debug');
  406. return radians * CesiumMath.DEGREES_PER_RADIAN;
  407. };
  408. /**
  409. * Converts a longitude value, in radians, to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  410. *
  411. * @param {Number} angle The longitude value, in radians, to convert to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  412. * @returns {Number} The equivalent longitude value in the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  413. *
  414. * @example
  415. * // Convert 270 degrees to -90 degrees longitude
  416. * var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));
  417. */
  418. CesiumMath.convertLongitudeRange = function(angle) {
  419. //>>includeStart('debug', pragmas.debug);
  420. if (!defined(angle)) {
  421. throw new DeveloperError('angle is required.');
  422. }
  423. //>>includeEnd('debug');
  424. var twoPi = CesiumMath.TWO_PI;
  425. var simplified = angle - Math.floor(angle / twoPi) * twoPi;
  426. if (simplified < -Math.PI) {
  427. return simplified + twoPi;
  428. }
  429. if (simplified >= Math.PI) {
  430. return simplified - twoPi;
  431. }
  432. return simplified;
  433. };
  434. /**
  435. * Convenience function that clamps a latitude value, in radians, to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  436. * Useful for sanitizing data before use in objects requiring correct range.
  437. *
  438. * @param {Number} angle The latitude value, in radians, to clamp to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  439. * @returns {Number} The latitude value clamped to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  440. *
  441. * @example
  442. * // Clamp 108 degrees latitude to 90 degrees latitude
  443. * var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));
  444. */
  445. CesiumMath.clampToLatitudeRange = function(angle) {
  446. //>>includeStart('debug', pragmas.debug);
  447. if (!defined(angle)) {
  448. throw new DeveloperError('angle is required.');
  449. }
  450. //>>includeEnd('debug');
  451. return CesiumMath.clamp(angle, -1*CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO);
  452. };
  453. /**
  454. * Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
  455. *
  456. * @param {Number} angle in radians
  457. * @returns {Number} The angle in the range [<code>-CesiumMath.PI</code>, <code>CesiumMath.PI</code>].
  458. */
  459. CesiumMath.negativePiToPi = function(angle) {
  460. //>>includeStart('debug', pragmas.debug);
  461. if (!defined(angle)) {
  462. throw new DeveloperError('angle is required.');
  463. }
  464. //>>includeEnd('debug');
  465. return CesiumMath.zeroToTwoPi(angle + CesiumMath.PI) - CesiumMath.PI;
  466. };
  467. /**
  468. * Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
  469. *
  470. * @param {Number} angle in radians
  471. * @returns {Number} The angle in the range [0, <code>CesiumMath.TWO_PI</code>].
  472. */
  473. CesiumMath.zeroToTwoPi = function(angle) {
  474. //>>includeStart('debug', pragmas.debug);
  475. if (!defined(angle)) {
  476. throw new DeveloperError('angle is required.');
  477. }
  478. //>>includeEnd('debug');
  479. var mod = CesiumMath.mod(angle, CesiumMath.TWO_PI);
  480. if (Math.abs(mod) < CesiumMath.EPSILON14 && Math.abs(angle) > CesiumMath.EPSILON14) {
  481. return CesiumMath.TWO_PI;
  482. }
  483. return mod;
  484. };
  485. /**
  486. * The modulo operation that also works for negative dividends.
  487. *
  488. * @param {Number} m The dividend.
  489. * @param {Number} n The divisor.
  490. * @returns {Number} The remainder.
  491. */
  492. CesiumMath.mod = function(m, n) {
  493. //>>includeStart('debug', pragmas.debug);
  494. if (!defined(m)) {
  495. throw new DeveloperError('m is required.');
  496. }
  497. if (!defined(n)) {
  498. throw new DeveloperError('n is required.');
  499. }
  500. //>>includeEnd('debug');
  501. return ((m % n) + n) % n;
  502. };
  503. /**
  504. * Determines if two values are equal using an absolute or relative tolerance test. This is useful
  505. * to avoid problems due to roundoff error when comparing floating-point values directly. The values are
  506. * first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed.
  507. * Use this test if you are unsure of the magnitudes of left and right.
  508. *
  509. * @param {Number} left The first value to compare.
  510. * @param {Number} right The other value to compare.
  511. * @param {Number} relativeEpsilon The maximum inclusive delta between <code>left</code> and <code>right</code> for the relative tolerance test.
  512. * @param {Number} [absoluteEpsilon=relativeEpsilon] The maximum inclusive delta between <code>left</code> and <code>right</code> for the absolute tolerance test.
  513. * @returns {Boolean} <code>true</code> if the values are equal within the epsilon; otherwise, <code>false</code>.
  514. *
  515. * @example
  516. * var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
  517. * var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2); // false
  518. * var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
  519. * var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false
  520. */
  521. CesiumMath.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  522. //>>includeStart('debug', pragmas.debug);
  523. if (!defined(left)) {
  524. throw new DeveloperError('left is required.');
  525. }
  526. if (!defined(right)) {
  527. throw new DeveloperError('right is required.');
  528. }
  529. if (!defined(relativeEpsilon)) {
  530. throw new DeveloperError('relativeEpsilon is required.');
  531. }
  532. //>>includeEnd('debug');
  533. absoluteEpsilon = defaultValue(absoluteEpsilon, relativeEpsilon);
  534. var absDiff = Math.abs(left - right);
  535. return absDiff <= absoluteEpsilon || absDiff <= relativeEpsilon * Math.max(Math.abs(left), Math.abs(right));
  536. };
  537. /**
  538. * Determines if the left value is less than the right value. If the two values are within
  539. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
  540. *
  541. * @param {Number} left The first number to compare.
  542. * @param {Number} right The second number to compare.
  543. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  544. * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> by more than
  545. * <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is greater or if the two
  546. * values are nearly equal.
  547. */
  548. CesiumMath.lessThan = function(left, right, absoluteEpsilon) {
  549. //>>includeStart('debug', pragmas.debug);
  550. if (!defined(left)) {
  551. throw new DeveloperError('first is required.');
  552. }
  553. if (!defined(right)) {
  554. throw new DeveloperError('second is required.');
  555. }
  556. if (!defined(absoluteEpsilon)) {
  557. throw new DeveloperError('relativeEpsilon is required.');
  558. }
  559. //>>includeEnd('debug');
  560. return left - right < -absoluteEpsilon;
  561. };
  562. /**
  563. * Determines if the left value is less than or equal to the right value. If the two values are within
  564. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
  565. *
  566. * @param {Number} left The first number to compare.
  567. * @param {Number} right The second number to compare.
  568. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  569. * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> or if the
  570. * the values are nearly equal.
  571. */
  572. CesiumMath.lessThanOrEquals = function(left, right, absoluteEpsilon) {
  573. //>>includeStart('debug', pragmas.debug);
  574. if (!defined(left)) {
  575. throw new DeveloperError('first is required.');
  576. }
  577. if (!defined(right)) {
  578. throw new DeveloperError('second is required.');
  579. }
  580. if (!defined(absoluteEpsilon)) {
  581. throw new DeveloperError('relativeEpsilon is required.');
  582. }
  583. //>>includeEnd('debug');
  584. return left - right < absoluteEpsilon;
  585. };
  586. /**
  587. * Determines if the left value is greater the right value. If the two values are within
  588. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
  589. *
  590. * @param {Number} left The first number to compare.
  591. * @param {Number} right The second number to compare.
  592. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  593. * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> by more than
  594. * <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is less or if the two
  595. * values are nearly equal.
  596. */
  597. CesiumMath.greaterThan = function(left, right, absoluteEpsilon) {
  598. //>>includeStart('debug', pragmas.debug);
  599. if (!defined(left)) {
  600. throw new DeveloperError('first is required.');
  601. }
  602. if (!defined(right)) {
  603. throw new DeveloperError('second is required.');
  604. }
  605. if (!defined(absoluteEpsilon)) {
  606. throw new DeveloperError('relativeEpsilon is required.');
  607. }
  608. //>>includeEnd('debug');
  609. return left - right > absoluteEpsilon;
  610. };
  611. /**
  612. * Determines if the left value is greater than or equal to the right value. If the two values are within
  613. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
  614. *
  615. * @param {Number} left The first number to compare.
  616. * @param {Number} right The second number to compare.
  617. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  618. * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> or if the
  619. * the values are nearly equal.
  620. */
  621. CesiumMath.greaterThanOrEquals = function(left, right, absoluteEpsilon) {
  622. //>>includeStart('debug', pragmas.debug);
  623. if (!defined(left)) {
  624. throw new DeveloperError('first is required.');
  625. }
  626. if (!defined(right)) {
  627. throw new DeveloperError('second is required.');
  628. }
  629. if (!defined(absoluteEpsilon)) {
  630. throw new DeveloperError('relativeEpsilon is required.');
  631. }
  632. //>>includeEnd('debug');
  633. return left - right > -absoluteEpsilon;
  634. };
  635. var factorials = [1];
  636. /**
  637. * Computes the factorial of the provided number.
  638. *
  639. * @param {Number} n The number whose factorial is to be computed.
  640. * @returns {Number} The factorial of the provided number or undefined if the number is less than 0.
  641. *
  642. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  643. *
  644. *
  645. * @example
  646. * //Compute 7!, which is equal to 5040
  647. * var computedFactorial = Cesium.Math.factorial(7);
  648. *
  649. * @see {@link http://en.wikipedia.org/wiki/Factorial|Factorial on Wikipedia}
  650. */
  651. CesiumMath.factorial = function(n) {
  652. //>>includeStart('debug', pragmas.debug);
  653. if (typeof n !== 'number' || n < 0) {
  654. throw new DeveloperError('A number greater than or equal to 0 is required.');
  655. }
  656. //>>includeEnd('debug');
  657. var length = factorials.length;
  658. if (n >= length) {
  659. var sum = factorials[length - 1];
  660. for (var i = length; i <= n; i++) {
  661. var next = sum * i;
  662. factorials.push(next);
  663. sum = next;
  664. }
  665. }
  666. return factorials[n];
  667. };
  668. /**
  669. * Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
  670. *
  671. * @param {Number} [n] The number to be incremented.
  672. * @param {Number} [maximumValue] The maximum incremented value before rolling over to the minimum value.
  673. * @param {Number} [minimumValue=0.0] The number reset to after the maximum value has been exceeded.
  674. * @returns {Number} The incremented number.
  675. *
  676. * @exception {DeveloperError} Maximum value must be greater than minimum value.
  677. *
  678. * @example
  679. * var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
  680. * var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0
  681. */
  682. CesiumMath.incrementWrap = function(n, maximumValue, minimumValue) {
  683. minimumValue = defaultValue(minimumValue, 0.0);
  684. //>>includeStart('debug', pragmas.debug);
  685. if (!defined(n)) {
  686. throw new DeveloperError('n is required.');
  687. }
  688. if (maximumValue <= minimumValue) {
  689. throw new DeveloperError('maximumValue must be greater than minimumValue.');
  690. }
  691. //>>includeEnd('debug');
  692. ++n;
  693. if (n > maximumValue) {
  694. n = minimumValue;
  695. }
  696. return n;
  697. };
  698. /**
  699. * Determines if a positive integer is a power of two.
  700. *
  701. * @param {Number} n The positive integer to test.
  702. * @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
  703. *
  704. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  705. *
  706. * @example
  707. * var t = Cesium.Math.isPowerOfTwo(16); // true
  708. * var f = Cesium.Math.isPowerOfTwo(20); // false
  709. */
  710. CesiumMath.isPowerOfTwo = function(n) {
  711. //>>includeStart('debug', pragmas.debug);
  712. if (typeof n !== 'number' || n < 0) {
  713. throw new DeveloperError('A number greater than or equal to 0 is required.');
  714. }
  715. //>>includeEnd('debug');
  716. return (n !== 0) && ((n & (n - 1)) === 0);
  717. };
  718. /**
  719. * Computes the next power-of-two integer greater than or equal to the provided positive integer.
  720. *
  721. * @param {Number} n The positive integer to test.
  722. * @returns {Number} The next power-of-two integer.
  723. *
  724. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  725. *
  726. * @example
  727. * var n = Cesium.Math.nextPowerOfTwo(29); // 32
  728. * var m = Cesium.Math.nextPowerOfTwo(32); // 32
  729. */
  730. CesiumMath.nextPowerOfTwo = function(n) {
  731. //>>includeStart('debug', pragmas.debug);
  732. if (typeof n !== 'number' || n < 0) {
  733. throw new DeveloperError('A number greater than or equal to 0 is required.');
  734. }
  735. //>>includeEnd('debug');
  736. // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  737. --n;
  738. n |= n >> 1;
  739. n |= n >> 2;
  740. n |= n >> 4;
  741. n |= n >> 8;
  742. n |= n >> 16;
  743. ++n;
  744. return n;
  745. };
  746. /**
  747. * Constraint a value to lie between two values.
  748. *
  749. * @param {Number} value The value to constrain.
  750. * @param {Number} min The minimum value.
  751. * @param {Number} max The maximum value.
  752. * @returns {Number} The value clamped so that min <= value <= max.
  753. */
  754. CesiumMath.clamp = function(value, min, max) {
  755. //>>includeStart('debug', pragmas.debug);
  756. if (!defined(value)) {
  757. throw new DeveloperError('value is required');
  758. }
  759. if (!defined(min)) {
  760. throw new DeveloperError('min is required.');
  761. }
  762. if (!defined(max)) {
  763. throw new DeveloperError('max is required.');
  764. }
  765. //>>includeEnd('debug');
  766. return value < min ? min : value > max ? max : value;
  767. };
  768. var randomNumberGenerator = new MersenneTwister();
  769. /**
  770. * Sets the seed used by the random number generator
  771. * in {@link CesiumMath#nextRandomNumber}.
  772. *
  773. * @param {Number} seed An integer used as the seed.
  774. */
  775. CesiumMath.setRandomNumberSeed = function(seed) {
  776. //>>includeStart('debug', pragmas.debug);
  777. if (!defined(seed)) {
  778. throw new DeveloperError('seed is required.');
  779. }
  780. //>>includeEnd('debug');
  781. randomNumberGenerator = new MersenneTwister(seed);
  782. };
  783. /**
  784. * Generates a random floating point number in the range of [0.0, 1.0)
  785. * using a Mersenne twister.
  786. *
  787. * @returns {Number} A random number in the range of [0.0, 1.0).
  788. *
  789. * @see CesiumMath.setRandomNumberSeed
  790. * @see {@link http://en.wikipedia.org/wiki/Mersenne_twister|Mersenne twister on Wikipedia}
  791. */
  792. CesiumMath.nextRandomNumber = function() {
  793. return randomNumberGenerator.random();
  794. };
  795. /**
  796. * Generates a random number between two numbers.
  797. *
  798. * @param {Number} min The minimum value.
  799. * @param {Number} max The maximum value.
  800. * @returns {Number} A random number between the min and max.
  801. */
  802. CesiumMath.randomBetween = function(min, max) {
  803. return CesiumMath.nextRandomNumber() * (max - min) + min;
  804. };
  805. /**
  806. * Computes <code>Math.acos(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  807. * so that the function will never return NaN.
  808. *
  809. * @param {Number} value The value for which to compute acos.
  810. * @returns {Number} The acos of the value if the value is in the range [-1.0, 1.0], or the acos of -1.0 or 1.0,
  811. * whichever is closer, if the value is outside the range.
  812. */
  813. CesiumMath.acosClamped = function(value) {
  814. //>>includeStart('debug', pragmas.debug);
  815. if (!defined(value)) {
  816. throw new DeveloperError('value is required.');
  817. }
  818. //>>includeEnd('debug');
  819. return Math.acos(CesiumMath.clamp(value, -1.0, 1.0));
  820. };
  821. /**
  822. * Computes <code>Math.asin(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  823. * so that the function will never return NaN.
  824. *
  825. * @param {Number} value The value for which to compute asin.
  826. * @returns {Number} The asin of the value if the value is in the range [-1.0, 1.0], or the asin of -1.0 or 1.0,
  827. * whichever is closer, if the value is outside the range.
  828. */
  829. CesiumMath.asinClamped = function(value) {
  830. //>>includeStart('debug', pragmas.debug);
  831. if (!defined(value)) {
  832. throw new DeveloperError('value is required.');
  833. }
  834. //>>includeEnd('debug');
  835. return Math.asin(CesiumMath.clamp(value, -1.0, 1.0));
  836. };
  837. /**
  838. * Finds the chord length between two points given the circle's radius and the angle between the points.
  839. *
  840. * @param {Number} angle The angle between the two points.
  841. * @param {Number} radius The radius of the circle.
  842. * @returns {Number} The chord length.
  843. */
  844. CesiumMath.chordLength = function(angle, radius) {
  845. //>>includeStart('debug', pragmas.debug);
  846. if (!defined(angle)) {
  847. throw new DeveloperError('angle is required.');
  848. }
  849. if (!defined(radius)) {
  850. throw new DeveloperError('radius is required.');
  851. }
  852. //>>includeEnd('debug');
  853. return 2.0 * radius * Math.sin(angle * 0.5);
  854. };
  855. /**
  856. * Finds the logarithm of a number to a base.
  857. *
  858. * @param {Number} number The number.
  859. * @param {Number} base The base.
  860. * @returns {Number} The result.
  861. */
  862. CesiumMath.logBase = function(number, base) {
  863. //>>includeStart('debug', pragmas.debug);
  864. if (!defined(number)) {
  865. throw new DeveloperError('number is required.');
  866. }
  867. if (!defined(base)) {
  868. throw new DeveloperError('base is required.');
  869. }
  870. //>>includeEnd('debug');
  871. return Math.log(number) / Math.log(base);
  872. };
  873. /**
  874. * Finds the cube root of a number.
  875. * Returns NaN if <code>number</code> is not provided.
  876. *
  877. * @function
  878. * @param {Number} [number] The number.
  879. * @returns {Number} The result.
  880. */
  881. CesiumMath.cbrt = defaultValue(Math.cbrt, function cbrt(number) {
  882. var result = Math.pow(Math.abs(number), 1.0 / 3.0);
  883. return number < 0.0 ? -result : result;
  884. });
  885. /**
  886. * Finds the base 2 logarithm of a number.
  887. *
  888. * @function
  889. * @param {Number} number The number.
  890. * @returns {Number} The result.
  891. */
  892. CesiumMath.log2 = defaultValue(Math.log2, function log2(number) {
  893. return Math.log(number) * Math.LOG2E;
  894. });
  895. /**
  896. * @private
  897. */
  898. CesiumMath.fog = function(distanceToCamera, density) {
  899. var scalar = distanceToCamera * density;
  900. return 1.0 - Math.exp(-(scalar * scalar));
  901. };
  902. /**
  903. * Computes a fast approximation of Atan for input in the range [-1, 1].
  904. *
  905. * Based on Michal Drobot's approximation from ShaderFastLibs,
  906. * which in turn is based on "Efficient approximations for the arctangent function,"
  907. * Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006.
  908. * Adapted from ShaderFastLibs under MIT License.
  909. *
  910. * @param {Number} x An input number in the range [-1, 1]
  911. * @returns {Number} An approximation of atan(x)
  912. */
  913. CesiumMath.fastApproximateAtan = function(x) {
  914. //>>includeStart('debug', pragmas.debug);
  915. Check.typeOf.number('x', x);
  916. //>>includeEnd('debug');
  917. return x * (-0.1784 * Math.abs(x) - 0.0663 * x * x + 1.0301);
  918. };
  919. /**
  920. * Computes a fast approximation of Atan2(x, y) for arbitrary input scalars.
  921. *
  922. * Range reduction math based on nvidia's cg reference implementation: http://developer.download.nvidia.com/cg/atan2.html
  923. *
  924. * @param {Number} x An input number that isn't zero if y is zero.
  925. * @param {Number} y An input number that isn't zero if x is zero.
  926. * @returns {Number} An approximation of atan2(x, y)
  927. */
  928. CesiumMath.fastApproximateAtan2 = function(x, y) {
  929. //>>includeStart('debug', pragmas.debug);
  930. Check.typeOf.number('x', x);
  931. Check.typeOf.number('y', y);
  932. //>>includeEnd('debug');
  933. // atan approximations are usually only reliable over [-1, 1]
  934. // So reduce the range by flipping whether x or y is on top based on which is bigger.
  935. var opposite;
  936. var adjacent;
  937. var t = Math.abs(x); // t used as swap and atan result.
  938. opposite = Math.abs(y);
  939. adjacent = Math.max(t, opposite);
  940. opposite = Math.min(t, opposite);
  941. var oppositeOverAdjacent = opposite / adjacent;
  942. //>>includeStart('debug', pragmas.debug);
  943. if (isNaN(oppositeOverAdjacent)) {
  944. throw new DeveloperError('either x or y must be nonzero');
  945. }
  946. //>>includeEnd('debug');
  947. t = CesiumMath.fastApproximateAtan(oppositeOverAdjacent);
  948. // Undo range reduction
  949. t = Math.abs(y) > Math.abs(x) ? CesiumMath.PI_OVER_TWO - t : t;
  950. t = x < 0.0 ? CesiumMath.PI - t : t;
  951. t = y < 0.0 ? -t : t;
  952. return t;
  953. };
  954. export default CesiumMath;