Math.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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(
  209. (CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMaximum
  210. );
  211. };
  212. /**
  213. * Converts a SNORM value in the range [0, rangeMaximum] to a scalar in the range [-1.0, 1.0].
  214. * @param {Number} value SNORM value in the range [0, rangeMaximum]
  215. * @param {Number} [rangeMaximum=255] The maximum value in the SNORM range, 255 by default.
  216. * @returns {Number} Scalar in the range [-1.0, 1.0].
  217. *
  218. * @see CesiumMath.toSNorm
  219. */
  220. CesiumMath.fromSNorm = function (value, rangeMaximum) {
  221. rangeMaximum = defaultValue(rangeMaximum, 255);
  222. return (
  223. (CesiumMath.clamp(value, 0.0, rangeMaximum) / rangeMaximum) * 2.0 - 1.0
  224. );
  225. };
  226. /**
  227. * Converts a scalar value in the range [rangeMinimum, rangeMaximum] to a scalar in the range [0.0, 1.0]
  228. * @param {Number} value The scalar value in the range [rangeMinimum, rangeMaximum]
  229. * @param {Number} rangeMinimum The minimum value in the mapped range.
  230. * @param {Number} rangeMaximum The maximum value in the mapped range.
  231. * @returns {Number} A scalar value, where rangeMinimum maps to 0.0 and rangeMaximum maps to 1.0.
  232. */
  233. CesiumMath.normalize = function (value, rangeMinimum, rangeMaximum) {
  234. rangeMaximum = Math.max(rangeMaximum - rangeMinimum, 0.0);
  235. return rangeMaximum === 0.0
  236. ? 0.0
  237. : CesiumMath.clamp((value - rangeMinimum) / rangeMaximum, 0.0, 1.0);
  238. };
  239. /**
  240. * Returns the hyperbolic sine of a number.
  241. * The hyperbolic sine of <em>value</em> is defined to be
  242. * (<em>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></em>)/2.0
  243. * where <i>e</i> is Euler's number, approximately 2.71828183.
  244. *
  245. * <p>Special cases:
  246. * <ul>
  247. * <li>If the argument is NaN, then the result is NaN.</li>
  248. *
  249. * <li>If the argument is infinite, then the result is an infinity
  250. * with the same sign as the argument.</li>
  251. *
  252. * <li>If the argument is zero, then the result is a zero with the
  253. * same sign as the argument.</li>
  254. * </ul>
  255. *</p>
  256. *
  257. * @function
  258. * @param {Number} value The number whose hyperbolic sine is to be returned.
  259. * @returns {Number} The hyperbolic sine of <code>value</code>.
  260. */
  261. CesiumMath.sinh = defaultValue(Math.sinh, function sinh(value) {
  262. return (Math.exp(value) - Math.exp(-value)) / 2.0;
  263. });
  264. /**
  265. * Returns the hyperbolic cosine of a number.
  266. * The hyperbolic cosine of <strong>value</strong> is defined to be
  267. * (<em>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></em>)/2.0
  268. * where <i>e</i> is Euler's number, approximately 2.71828183.
  269. *
  270. * <p>Special cases:
  271. * <ul>
  272. * <li>If the argument is NaN, then the result is NaN.</li>
  273. *
  274. * <li>If the argument is infinite, then the result is positive infinity.</li>
  275. *
  276. * <li>If the argument is zero, then the result is 1.0.</li>
  277. * </ul>
  278. *</p>
  279. *
  280. * @function
  281. * @param {Number} value The number whose hyperbolic cosine is to be returned.
  282. * @returns {Number} The hyperbolic cosine of <code>value</code>.
  283. */
  284. CesiumMath.cosh = defaultValue(Math.cosh, function cosh(value) {
  285. return (Math.exp(value) + Math.exp(-value)) / 2.0;
  286. });
  287. /**
  288. * Computes the linear interpolation of two values.
  289. *
  290. * @param {Number} p The start value to interpolate.
  291. * @param {Number} q The end value to interpolate.
  292. * @param {Number} time The time of interpolation generally in the range <code>[0.0, 1.0]</code>.
  293. * @returns {Number} The linearly interpolated value.
  294. *
  295. * @example
  296. * var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0
  297. */
  298. CesiumMath.lerp = function (p, q, time) {
  299. return (1.0 - time) * p + time * q;
  300. };
  301. /**
  302. * pi
  303. *
  304. * @type {Number}
  305. * @constant
  306. */
  307. CesiumMath.PI = Math.PI;
  308. /**
  309. * 1/pi
  310. *
  311. * @type {Number}
  312. * @constant
  313. */
  314. CesiumMath.ONE_OVER_PI = 1.0 / Math.PI;
  315. /**
  316. * pi/2
  317. *
  318. * @type {Number}
  319. * @constant
  320. */
  321. CesiumMath.PI_OVER_TWO = Math.PI / 2.0;
  322. /**
  323. * pi/3
  324. *
  325. * @type {Number}
  326. * @constant
  327. */
  328. CesiumMath.PI_OVER_THREE = Math.PI / 3.0;
  329. /**
  330. * pi/4
  331. *
  332. * @type {Number}
  333. * @constant
  334. */
  335. CesiumMath.PI_OVER_FOUR = Math.PI / 4.0;
  336. /**
  337. * pi/6
  338. *
  339. * @type {Number}
  340. * @constant
  341. */
  342. CesiumMath.PI_OVER_SIX = Math.PI / 6.0;
  343. /**
  344. * 3pi/2
  345. *
  346. * @type {Number}
  347. * @constant
  348. */
  349. CesiumMath.THREE_PI_OVER_TWO = (3.0 * Math.PI) / 2.0;
  350. /**
  351. * 2pi
  352. *
  353. * @type {Number}
  354. * @constant
  355. */
  356. CesiumMath.TWO_PI = 2.0 * Math.PI;
  357. /**
  358. * 1/2pi
  359. *
  360. * @type {Number}
  361. * @constant
  362. */
  363. CesiumMath.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI);
  364. /**
  365. * The number of radians in a degree.
  366. *
  367. * @type {Number}
  368. * @constant
  369. */
  370. CesiumMath.RADIANS_PER_DEGREE = Math.PI / 180.0;
  371. /**
  372. * The number of degrees in a radian.
  373. *
  374. * @type {Number}
  375. * @constant
  376. */
  377. CesiumMath.DEGREES_PER_RADIAN = 180.0 / Math.PI;
  378. /**
  379. * The number of radians in an arc second.
  380. *
  381. * @type {Number}
  382. * @constant
  383. */
  384. CesiumMath.RADIANS_PER_ARCSECOND = CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  385. /**
  386. * Converts degrees to radians.
  387. * @param {Number} degrees The angle to convert in degrees.
  388. * @returns {Number} The corresponding angle in radians.
  389. */
  390. CesiumMath.toRadians = function (degrees) {
  391. //>>includeStart('debug', pragmas.debug);
  392. if (!defined(degrees)) {
  393. throw new DeveloperError("degrees is required.");
  394. }
  395. //>>includeEnd('debug');
  396. return degrees * CesiumMath.RADIANS_PER_DEGREE;
  397. };
  398. /**
  399. * Converts radians to degrees.
  400. * @param {Number} radians The angle to convert in radians.
  401. * @returns {Number} The corresponding angle in degrees.
  402. */
  403. CesiumMath.toDegrees = function (radians) {
  404. //>>includeStart('debug', pragmas.debug);
  405. if (!defined(radians)) {
  406. throw new DeveloperError("radians is required.");
  407. }
  408. //>>includeEnd('debug');
  409. return radians * CesiumMath.DEGREES_PER_RADIAN;
  410. };
  411. /**
  412. * Converts a longitude value, in radians, to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  413. *
  414. * @param {Number} angle The longitude value, in radians, to convert to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  415. * @returns {Number} The equivalent longitude value in the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  416. *
  417. * @example
  418. * // Convert 270 degrees to -90 degrees longitude
  419. * var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));
  420. */
  421. CesiumMath.convertLongitudeRange = function (angle) {
  422. //>>includeStart('debug', pragmas.debug);
  423. if (!defined(angle)) {
  424. throw new DeveloperError("angle is required.");
  425. }
  426. //>>includeEnd('debug');
  427. var twoPi = CesiumMath.TWO_PI;
  428. var simplified = angle - Math.floor(angle / twoPi) * twoPi;
  429. if (simplified < -Math.PI) {
  430. return simplified + twoPi;
  431. }
  432. if (simplified >= Math.PI) {
  433. return simplified - twoPi;
  434. }
  435. return simplified;
  436. };
  437. /**
  438. * Convenience function that clamps a latitude value, in radians, to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  439. * Useful for sanitizing data before use in objects requiring correct range.
  440. *
  441. * @param {Number} angle The latitude value, in radians, to clamp to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  442. * @returns {Number} The latitude value clamped to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  443. *
  444. * @example
  445. * // Clamp 108 degrees latitude to 90 degrees latitude
  446. * var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));
  447. */
  448. CesiumMath.clampToLatitudeRange = function (angle) {
  449. //>>includeStart('debug', pragmas.debug);
  450. if (!defined(angle)) {
  451. throw new DeveloperError("angle is required.");
  452. }
  453. //>>includeEnd('debug');
  454. return CesiumMath.clamp(
  455. angle,
  456. -1 * CesiumMath.PI_OVER_TWO,
  457. CesiumMath.PI_OVER_TWO
  458. );
  459. };
  460. /**
  461. * Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
  462. *
  463. * @param {Number} angle in radians
  464. * @returns {Number} The angle in the range [<code>-CesiumMath.PI</code>, <code>CesiumMath.PI</code>].
  465. */
  466. CesiumMath.negativePiToPi = function (angle) {
  467. //>>includeStart('debug', pragmas.debug);
  468. if (!defined(angle)) {
  469. throw new DeveloperError("angle is required.");
  470. }
  471. //>>includeEnd('debug');
  472. return CesiumMath.zeroToTwoPi(angle + CesiumMath.PI) - CesiumMath.PI;
  473. };
  474. /**
  475. * Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
  476. *
  477. * @param {Number} angle in radians
  478. * @returns {Number} The angle in the range [0, <code>CesiumMath.TWO_PI</code>].
  479. */
  480. CesiumMath.zeroToTwoPi = function (angle) {
  481. //>>includeStart('debug', pragmas.debug);
  482. if (!defined(angle)) {
  483. throw new DeveloperError("angle is required.");
  484. }
  485. //>>includeEnd('debug');
  486. var mod = CesiumMath.mod(angle, CesiumMath.TWO_PI);
  487. if (
  488. Math.abs(mod) < CesiumMath.EPSILON14 &&
  489. Math.abs(angle) > CesiumMath.EPSILON14
  490. ) {
  491. return CesiumMath.TWO_PI;
  492. }
  493. return mod;
  494. };
  495. /**
  496. * The modulo operation that also works for negative dividends.
  497. *
  498. * @param {Number} m The dividend.
  499. * @param {Number} n The divisor.
  500. * @returns {Number} The remainder.
  501. */
  502. CesiumMath.mod = function (m, n) {
  503. //>>includeStart('debug', pragmas.debug);
  504. if (!defined(m)) {
  505. throw new DeveloperError("m is required.");
  506. }
  507. if (!defined(n)) {
  508. throw new DeveloperError("n is required.");
  509. }
  510. //>>includeEnd('debug');
  511. return ((m % n) + n) % n;
  512. };
  513. /**
  514. * Determines if two values are equal using an absolute or relative tolerance test. This is useful
  515. * to avoid problems due to roundoff error when comparing floating-point values directly. The values are
  516. * first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed.
  517. * Use this test if you are unsure of the magnitudes of left and right.
  518. *
  519. * @param {Number} left The first value to compare.
  520. * @param {Number} right The other value to compare.
  521. * @param {Number} [relativeEpsilon=0] The maximum inclusive delta between <code>left</code> and <code>right</code> for the relative tolerance test.
  522. * @param {Number} [absoluteEpsilon=relativeEpsilon] The maximum inclusive delta between <code>left</code> and <code>right</code> for the absolute tolerance test.
  523. * @returns {Boolean} <code>true</code> if the values are equal within the epsilon; otherwise, <code>false</code>.
  524. *
  525. * @example
  526. * var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
  527. * var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2); // false
  528. * var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
  529. * var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false
  530. */
  531. CesiumMath.equalsEpsilon = function (
  532. left,
  533. right,
  534. relativeEpsilon,
  535. absoluteEpsilon
  536. ) {
  537. //>>includeStart('debug', pragmas.debug);
  538. if (!defined(left)) {
  539. throw new DeveloperError("left is required.");
  540. }
  541. if (!defined(right)) {
  542. throw new DeveloperError("right is required.");
  543. }
  544. //>>includeEnd('debug');
  545. relativeEpsilon = defaultValue(relativeEpsilon, 0.0);
  546. absoluteEpsilon = defaultValue(absoluteEpsilon, relativeEpsilon);
  547. var absDiff = Math.abs(left - right);
  548. return (
  549. absDiff <= absoluteEpsilon ||
  550. absDiff <= relativeEpsilon * Math.max(Math.abs(left), Math.abs(right))
  551. );
  552. };
  553. /**
  554. * Determines if the left value is less than the right value. If the two values are within
  555. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
  556. *
  557. * @param {Number} left The first number to compare.
  558. * @param {Number} right The second number to compare.
  559. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  560. * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> by more than
  561. * <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is greater or if the two
  562. * values are nearly equal.
  563. */
  564. CesiumMath.lessThan = function (left, right, absoluteEpsilon) {
  565. //>>includeStart('debug', pragmas.debug);
  566. if (!defined(left)) {
  567. throw new DeveloperError("first is required.");
  568. }
  569. if (!defined(right)) {
  570. throw new DeveloperError("second is required.");
  571. }
  572. if (!defined(absoluteEpsilon)) {
  573. throw new DeveloperError("relativeEpsilon is required.");
  574. }
  575. //>>includeEnd('debug');
  576. return left - right < -absoluteEpsilon;
  577. };
  578. /**
  579. * Determines if the left value is less than or equal to the right value. If the two values are within
  580. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
  581. *
  582. * @param {Number} left The first number to compare.
  583. * @param {Number} right The second number to compare.
  584. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  585. * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> or if the
  586. * the values are nearly equal.
  587. */
  588. CesiumMath.lessThanOrEquals = function (left, right, absoluteEpsilon) {
  589. //>>includeStart('debug', pragmas.debug);
  590. if (!defined(left)) {
  591. throw new DeveloperError("first is required.");
  592. }
  593. if (!defined(right)) {
  594. throw new DeveloperError("second is required.");
  595. }
  596. if (!defined(absoluteEpsilon)) {
  597. throw new DeveloperError("relativeEpsilon is required.");
  598. }
  599. //>>includeEnd('debug');
  600. return left - right < absoluteEpsilon;
  601. };
  602. /**
  603. * Determines if the left value is greater the right value. If the two values are within
  604. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
  605. *
  606. * @param {Number} left The first number to compare.
  607. * @param {Number} right The second number to compare.
  608. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  609. * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> by more than
  610. * <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is less or if the two
  611. * values are nearly equal.
  612. */
  613. CesiumMath.greaterThan = function (left, right, absoluteEpsilon) {
  614. //>>includeStart('debug', pragmas.debug);
  615. if (!defined(left)) {
  616. throw new DeveloperError("first is required.");
  617. }
  618. if (!defined(right)) {
  619. throw new DeveloperError("second is required.");
  620. }
  621. if (!defined(absoluteEpsilon)) {
  622. throw new DeveloperError("relativeEpsilon is required.");
  623. }
  624. //>>includeEnd('debug');
  625. return left - right > absoluteEpsilon;
  626. };
  627. /**
  628. * Determines if the left value is greater than or equal to the right value. If the two values are within
  629. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
  630. *
  631. * @param {Number} left The first number to compare.
  632. * @param {Number} right The second number to compare.
  633. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  634. * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> or if the
  635. * the values are nearly equal.
  636. */
  637. CesiumMath.greaterThanOrEquals = function (left, right, absoluteEpsilon) {
  638. //>>includeStart('debug', pragmas.debug);
  639. if (!defined(left)) {
  640. throw new DeveloperError("first is required.");
  641. }
  642. if (!defined(right)) {
  643. throw new DeveloperError("second is required.");
  644. }
  645. if (!defined(absoluteEpsilon)) {
  646. throw new DeveloperError("relativeEpsilon is required.");
  647. }
  648. //>>includeEnd('debug');
  649. return left - right > -absoluteEpsilon;
  650. };
  651. var factorials = [1];
  652. /**
  653. * Computes the factorial of the provided number.
  654. *
  655. * @param {Number} n The number whose factorial is to be computed.
  656. * @returns {Number} The factorial of the provided number or undefined if the number is less than 0.
  657. *
  658. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  659. *
  660. *
  661. * @example
  662. * //Compute 7!, which is equal to 5040
  663. * var computedFactorial = Cesium.Math.factorial(7);
  664. *
  665. * @see {@link http://en.wikipedia.org/wiki/Factorial|Factorial on Wikipedia}
  666. */
  667. CesiumMath.factorial = function (n) {
  668. //>>includeStart('debug', pragmas.debug);
  669. if (typeof n !== "number" || n < 0) {
  670. throw new DeveloperError(
  671. "A number greater than or equal to 0 is required."
  672. );
  673. }
  674. //>>includeEnd('debug');
  675. var length = factorials.length;
  676. if (n >= length) {
  677. var sum = factorials[length - 1];
  678. for (var i = length; i <= n; i++) {
  679. var next = sum * i;
  680. factorials.push(next);
  681. sum = next;
  682. }
  683. }
  684. return factorials[n];
  685. };
  686. /**
  687. * Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
  688. *
  689. * @param {Number} [n] The number to be incremented.
  690. * @param {Number} [maximumValue] The maximum incremented value before rolling over to the minimum value.
  691. * @param {Number} [minimumValue=0.0] The number reset to after the maximum value has been exceeded.
  692. * @returns {Number} The incremented number.
  693. *
  694. * @exception {DeveloperError} Maximum value must be greater than minimum value.
  695. *
  696. * @example
  697. * var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
  698. * var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0
  699. */
  700. CesiumMath.incrementWrap = function (n, maximumValue, minimumValue) {
  701. minimumValue = defaultValue(minimumValue, 0.0);
  702. //>>includeStart('debug', pragmas.debug);
  703. if (!defined(n)) {
  704. throw new DeveloperError("n is required.");
  705. }
  706. if (maximumValue <= minimumValue) {
  707. throw new DeveloperError("maximumValue must be greater than minimumValue.");
  708. }
  709. //>>includeEnd('debug');
  710. ++n;
  711. if (n > maximumValue) {
  712. n = minimumValue;
  713. }
  714. return n;
  715. };
  716. /**
  717. * Determines if a positive integer is a power of two.
  718. *
  719. * @param {Number} n The positive integer to test.
  720. * @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
  721. *
  722. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  723. *
  724. * @example
  725. * var t = Cesium.Math.isPowerOfTwo(16); // true
  726. * var f = Cesium.Math.isPowerOfTwo(20); // false
  727. */
  728. CesiumMath.isPowerOfTwo = function (n) {
  729. //>>includeStart('debug', pragmas.debug);
  730. if (typeof n !== "number" || n < 0) {
  731. throw new DeveloperError(
  732. "A number greater than or equal to 0 is required."
  733. );
  734. }
  735. //>>includeEnd('debug');
  736. return n !== 0 && (n & (n - 1)) === 0;
  737. };
  738. /**
  739. * Computes the next power-of-two integer greater than or equal to the provided positive integer.
  740. *
  741. * @param {Number} n The positive integer to test.
  742. * @returns {Number} The next power-of-two integer.
  743. *
  744. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  745. *
  746. * @example
  747. * var n = Cesium.Math.nextPowerOfTwo(29); // 32
  748. * var m = Cesium.Math.nextPowerOfTwo(32); // 32
  749. */
  750. CesiumMath.nextPowerOfTwo = function (n) {
  751. //>>includeStart('debug', pragmas.debug);
  752. if (typeof n !== "number" || n < 0) {
  753. throw new DeveloperError(
  754. "A number greater than or equal to 0 is required."
  755. );
  756. }
  757. //>>includeEnd('debug');
  758. // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  759. --n;
  760. n |= n >> 1;
  761. n |= n >> 2;
  762. n |= n >> 4;
  763. n |= n >> 8;
  764. n |= n >> 16;
  765. ++n;
  766. return n;
  767. };
  768. /**
  769. * Constraint a value to lie between two values.
  770. *
  771. * @param {Number} value The value to constrain.
  772. * @param {Number} min The minimum value.
  773. * @param {Number} max The maximum value.
  774. * @returns {Number} The value clamped so that min <= value <= max.
  775. */
  776. CesiumMath.clamp = function (value, min, max) {
  777. //>>includeStart('debug', pragmas.debug);
  778. if (!defined(value)) {
  779. throw new DeveloperError("value is required");
  780. }
  781. if (!defined(min)) {
  782. throw new DeveloperError("min is required.");
  783. }
  784. if (!defined(max)) {
  785. throw new DeveloperError("max is required.");
  786. }
  787. //>>includeEnd('debug');
  788. return value < min ? min : value > max ? max : value;
  789. };
  790. var randomNumberGenerator = new MersenneTwister();
  791. /**
  792. * Sets the seed used by the random number generator
  793. * in {@link CesiumMath#nextRandomNumber}.
  794. *
  795. * @param {Number} seed An integer used as the seed.
  796. */
  797. CesiumMath.setRandomNumberSeed = function (seed) {
  798. //>>includeStart('debug', pragmas.debug);
  799. if (!defined(seed)) {
  800. throw new DeveloperError("seed is required.");
  801. }
  802. //>>includeEnd('debug');
  803. randomNumberGenerator = new MersenneTwister(seed);
  804. };
  805. /**
  806. * Generates a random floating point number in the range of [0.0, 1.0)
  807. * using a Mersenne twister.
  808. *
  809. * @returns {Number} A random number in the range of [0.0, 1.0).
  810. *
  811. * @see CesiumMath.setRandomNumberSeed
  812. * @see {@link http://en.wikipedia.org/wiki/Mersenne_twister|Mersenne twister on Wikipedia}
  813. */
  814. CesiumMath.nextRandomNumber = function () {
  815. return randomNumberGenerator.random();
  816. };
  817. /**
  818. * Generates a random number between two numbers.
  819. *
  820. * @param {Number} min The minimum value.
  821. * @param {Number} max The maximum value.
  822. * @returns {Number} A random number between the min and max.
  823. */
  824. CesiumMath.randomBetween = function (min, max) {
  825. return CesiumMath.nextRandomNumber() * (max - min) + min;
  826. };
  827. /**
  828. * Computes <code>Math.acos(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  829. * so that the function will never return NaN.
  830. *
  831. * @param {Number} value The value for which to compute acos.
  832. * @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,
  833. * whichever is closer, if the value is outside the range.
  834. */
  835. CesiumMath.acosClamped = function (value) {
  836. //>>includeStart('debug', pragmas.debug);
  837. if (!defined(value)) {
  838. throw new DeveloperError("value is required.");
  839. }
  840. //>>includeEnd('debug');
  841. return Math.acos(CesiumMath.clamp(value, -1.0, 1.0));
  842. };
  843. /**
  844. * Computes <code>Math.asin(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  845. * so that the function will never return NaN.
  846. *
  847. * @param {Number} value The value for which to compute asin.
  848. * @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,
  849. * whichever is closer, if the value is outside the range.
  850. */
  851. CesiumMath.asinClamped = function (value) {
  852. //>>includeStart('debug', pragmas.debug);
  853. if (!defined(value)) {
  854. throw new DeveloperError("value is required.");
  855. }
  856. //>>includeEnd('debug');
  857. return Math.asin(CesiumMath.clamp(value, -1.0, 1.0));
  858. };
  859. /**
  860. * Finds the chord length between two points given the circle's radius and the angle between the points.
  861. *
  862. * @param {Number} angle The angle between the two points.
  863. * @param {Number} radius The radius of the circle.
  864. * @returns {Number} The chord length.
  865. */
  866. CesiumMath.chordLength = function (angle, radius) {
  867. //>>includeStart('debug', pragmas.debug);
  868. if (!defined(angle)) {
  869. throw new DeveloperError("angle is required.");
  870. }
  871. if (!defined(radius)) {
  872. throw new DeveloperError("radius is required.");
  873. }
  874. //>>includeEnd('debug');
  875. return 2.0 * radius * Math.sin(angle * 0.5);
  876. };
  877. /**
  878. * Finds the logarithm of a number to a base.
  879. *
  880. * @param {Number} number The number.
  881. * @param {Number} base The base.
  882. * @returns {Number} The result.
  883. */
  884. CesiumMath.logBase = function (number, base) {
  885. //>>includeStart('debug', pragmas.debug);
  886. if (!defined(number)) {
  887. throw new DeveloperError("number is required.");
  888. }
  889. if (!defined(base)) {
  890. throw new DeveloperError("base is required.");
  891. }
  892. //>>includeEnd('debug');
  893. return Math.log(number) / Math.log(base);
  894. };
  895. /**
  896. * Finds the cube root of a number.
  897. * Returns NaN if <code>number</code> is not provided.
  898. *
  899. * @function
  900. * @param {Number} [number] The number.
  901. * @returns {Number} The result.
  902. */
  903. CesiumMath.cbrt = defaultValue(Math.cbrt, function cbrt(number) {
  904. var result = Math.pow(Math.abs(number), 1.0 / 3.0);
  905. return number < 0.0 ? -result : result;
  906. });
  907. /**
  908. * Finds the base 2 logarithm of a number.
  909. *
  910. * @function
  911. * @param {Number} number The number.
  912. * @returns {Number} The result.
  913. */
  914. CesiumMath.log2 = defaultValue(Math.log2, function log2(number) {
  915. return Math.log(number) * Math.LOG2E;
  916. });
  917. /**
  918. * @private
  919. */
  920. CesiumMath.fog = function (distanceToCamera, density) {
  921. var scalar = distanceToCamera * density;
  922. return 1.0 - Math.exp(-(scalar * scalar));
  923. };
  924. /**
  925. * Computes a fast approximation of Atan for input in the range [-1, 1].
  926. *
  927. * Based on Michal Drobot's approximation from ShaderFastLibs,
  928. * which in turn is based on "Efficient approximations for the arctangent function,"
  929. * Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006.
  930. * Adapted from ShaderFastLibs under MIT License.
  931. *
  932. * @param {Number} x An input number in the range [-1, 1]
  933. * @returns {Number} An approximation of atan(x)
  934. */
  935. CesiumMath.fastApproximateAtan = function (x) {
  936. //>>includeStart('debug', pragmas.debug);
  937. Check.typeOf.number("x", x);
  938. //>>includeEnd('debug');
  939. return x * (-0.1784 * Math.abs(x) - 0.0663 * x * x + 1.0301);
  940. };
  941. /**
  942. * Computes a fast approximation of Atan2(x, y) for arbitrary input scalars.
  943. *
  944. * Range reduction math based on nvidia's cg reference implementation: http://developer.download.nvidia.com/cg/atan2.html
  945. *
  946. * @param {Number} x An input number that isn't zero if y is zero.
  947. * @param {Number} y An input number that isn't zero if x is zero.
  948. * @returns {Number} An approximation of atan2(x, y)
  949. */
  950. CesiumMath.fastApproximateAtan2 = function (x, y) {
  951. //>>includeStart('debug', pragmas.debug);
  952. Check.typeOf.number("x", x);
  953. Check.typeOf.number("y", y);
  954. //>>includeEnd('debug');
  955. // atan approximations are usually only reliable over [-1, 1]
  956. // So reduce the range by flipping whether x or y is on top based on which is bigger.
  957. var opposite;
  958. var adjacent;
  959. var t = Math.abs(x); // t used as swap and atan result.
  960. opposite = Math.abs(y);
  961. adjacent = Math.max(t, opposite);
  962. opposite = Math.min(t, opposite);
  963. var oppositeOverAdjacent = opposite / adjacent;
  964. //>>includeStart('debug', pragmas.debug);
  965. if (isNaN(oppositeOverAdjacent)) {
  966. throw new DeveloperError("either x or y must be nonzero");
  967. }
  968. //>>includeEnd('debug');
  969. t = CesiumMath.fastApproximateAtan(oppositeOverAdjacent);
  970. // Undo range reduction
  971. t = Math.abs(y) > Math.abs(x) ? CesiumMath.PI_OVER_TWO - t : t;
  972. t = x < 0.0 ? CesiumMath.PI - t : t;
  973. t = y < 0.0 ? -t : t;
  974. return t;
  975. };
  976. export default CesiumMath;