Math-61ede240.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 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/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281'], function (exports, when, Check) { 'use strict';
  24. /*
  25. I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
  26. so it's better encapsulated. Now you can have multiple random number generators
  27. and they won't stomp all over eachother's state.
  28. If you want to use this as a substitute for Math.random(), use the random()
  29. method like so:
  30. var m = new MersenneTwister();
  31. var randomNumber = m.random();
  32. You can also call the other genrand_{foo}() methods on the instance.
  33. If you want to use a specific seed in order to get a repeatable random
  34. sequence, pass an integer into the constructor:
  35. var m = new MersenneTwister(123);
  36. and that will always produce the same random sequence.
  37. Sean McCullough (banksean@gmail.com)
  38. */
  39. /*
  40. A C-program for MT19937, with initialization improved 2002/1/26.
  41. Coded by Takuji Nishimura and Makoto Matsumoto.
  42. Before using, initialize the state by using init_genrand(seed)
  43. or init_by_array(init_key, key_length).
  44. */
  45. /**
  46. @license
  47. mersenne-twister.js - https://gist.github.com/banksean/300494
  48. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  49. All rights reserved.
  50. Redistribution and use in source and binary forms, with or without
  51. modification, are permitted provided that the following conditions
  52. are met:
  53. 1. Redistributions of source code must retain the above copyright
  54. notice, this list of conditions and the following disclaimer.
  55. 2. Redistributions in binary form must reproduce the above copyright
  56. notice, this list of conditions and the following disclaimer in the
  57. documentation and/or other materials provided with the distribution.
  58. 3. The names of its contributors may not be used to endorse or promote
  59. products derived from this software without specific prior written
  60. permission.
  61. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  62. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  63. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  64. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  65. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  66. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  67. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  68. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  69. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  70. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  71. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72. */
  73. /*
  74. Any feedback is very welcome.
  75. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  76. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  77. */
  78. var MersenneTwister = function(seed) {
  79. if (seed == undefined) {
  80. seed = new Date().getTime();
  81. }
  82. /* Period parameters */
  83. this.N = 624;
  84. this.M = 397;
  85. this.MATRIX_A = 0x9908b0df; /* constant vector a */
  86. this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
  87. this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
  88. this.mt = new Array(this.N); /* the array for the state vector */
  89. this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */
  90. this.init_genrand(seed);
  91. };
  92. /* initializes mt[N] with a seed */
  93. MersenneTwister.prototype.init_genrand = function(s) {
  94. this.mt[0] = s >>> 0;
  95. for (this.mti=1; this.mti<this.N; this.mti++) {
  96. var s = this.mt[this.mti-1] ^ (this.mt[this.mti-1] >>> 30);
  97. this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)
  98. + this.mti;
  99. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  100. /* In the previous versions, MSBs of the seed affect */
  101. /* only MSBs of the array mt[]. */
  102. /* 2002/01/09 modified by Makoto Matsumoto */
  103. this.mt[this.mti] >>>= 0;
  104. /* for >32 bit machines */
  105. }
  106. };
  107. /* initialize by an array with array-length */
  108. /* init_key is the array for initializing keys */
  109. /* key_length is its length */
  110. /* slight change for C++, 2004/2/26 */
  111. //MersenneTwister.prototype.init_by_array = function(init_key, key_length) {
  112. // var i, j, k;
  113. // this.init_genrand(19650218);
  114. // i=1; j=0;
  115. // k = (this.N>key_length ? this.N : key_length);
  116. // for (; k; k--) {
  117. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30)
  118. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525)))
  119. // + init_key[j] + j; /* non linear */
  120. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  121. // i++; j++;
  122. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  123. // if (j>=key_length) j=0;
  124. // }
  125. // for (k=this.N-1; k; k--) {
  126. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
  127. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941))
  128. // - i; /* non linear */
  129. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  130. // i++;
  131. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  132. // }
  133. //
  134. // this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  135. //}
  136. /* generates a random number on [0,0xffffffff]-interval */
  137. MersenneTwister.prototype.genrand_int32 = function() {
  138. var y;
  139. var mag01 = new Array(0x0, this.MATRIX_A);
  140. /* mag01[x] = x * MATRIX_A for x=0,1 */
  141. if (this.mti >= this.N) { /* generate N words at one time */
  142. var kk;
  143. if (this.mti == this.N+1) /* if init_genrand() has not been called, */
  144. this.init_genrand(5489); /* a default initial seed is used */
  145. for (kk=0;kk<this.N-this.M;kk++) {
  146. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  147. this.mt[kk] = this.mt[kk+this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
  148. }
  149. for (;kk<this.N-1;kk++) {
  150. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  151. this.mt[kk] = this.mt[kk+(this.M-this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  152. }
  153. y = (this.mt[this.N-1]&this.UPPER_MASK)|(this.mt[0]&this.LOWER_MASK);
  154. this.mt[this.N-1] = this.mt[this.M-1] ^ (y >>> 1) ^ mag01[y & 0x1];
  155. this.mti = 0;
  156. }
  157. y = this.mt[this.mti++];
  158. /* Tempering */
  159. y ^= (y >>> 11);
  160. y ^= (y << 7) & 0x9d2c5680;
  161. y ^= (y << 15) & 0xefc60000;
  162. y ^= (y >>> 18);
  163. return y >>> 0;
  164. };
  165. /* generates a random number on [0,0x7fffffff]-interval */
  166. //MersenneTwister.prototype.genrand_int31 = function() {
  167. // return (this.genrand_int32()>>>1);
  168. //}
  169. /* generates a random number on [0,1]-real-interval */
  170. //MersenneTwister.prototype.genrand_real1 = function() {
  171. // return this.genrand_int32()*(1.0/4294967295.0);
  172. // /* divided by 2^32-1 */
  173. //}
  174. /* generates a random number on [0,1)-real-interval */
  175. MersenneTwister.prototype.random = function() {
  176. return this.genrand_int32()*(1.0/4294967296.0);
  177. /* divided by 2^32 */
  178. };
  179. /**
  180. * Math functions.
  181. *
  182. * @exports CesiumMath
  183. * @alias Math
  184. */
  185. var CesiumMath = {};
  186. CesiumMath.Radius = 6378137.0;
  187. /**
  188. * 0.1
  189. * @type {Number}
  190. * @constant
  191. */
  192. CesiumMath.EPSILON1 = 0.1;
  193. /**
  194. * 0.01
  195. * @type {Number}
  196. * @constant
  197. */
  198. CesiumMath.EPSILON2 = 0.01;
  199. /**
  200. * 0.001
  201. * @type {Number}
  202. * @constant
  203. */
  204. CesiumMath.EPSILON3 = 0.001;
  205. /**
  206. * 0.0001
  207. * @type {Number}
  208. * @constant
  209. */
  210. CesiumMath.EPSILON4 = 0.0001;
  211. /**
  212. * 0.00001
  213. * @type {Number}
  214. * @constant
  215. */
  216. CesiumMath.EPSILON5 = 0.00001;
  217. /**
  218. * 0.000001
  219. * @type {Number}
  220. * @constant
  221. */
  222. CesiumMath.EPSILON6 = 0.000001;
  223. /**
  224. * 0.0000001
  225. * @type {Number}
  226. * @constant
  227. */
  228. CesiumMath.EPSILON7 = 0.0000001;
  229. /**
  230. * 0.00000001
  231. * @type {Number}
  232. * @constant
  233. */
  234. CesiumMath.EPSILON8 = 0.00000001;
  235. /**
  236. * 0.000000001
  237. * @type {Number}
  238. * @constant
  239. */
  240. CesiumMath.EPSILON9 = 0.000000001;
  241. /**
  242. * 0.0000000001
  243. * @type {Number}
  244. * @constant
  245. */
  246. CesiumMath.EPSILON10 = 0.0000000001;
  247. /**
  248. * 0.00000000001
  249. * @type {Number}
  250. * @constant
  251. */
  252. CesiumMath.EPSILON11 = 0.00000000001;
  253. /**
  254. * 0.000000000001
  255. * @type {Number}
  256. * @constant
  257. */
  258. CesiumMath.EPSILON12 = 0.000000000001;
  259. /**
  260. * 0.0000000000001
  261. * @type {Number}
  262. * @constant
  263. */
  264. CesiumMath.EPSILON13 = 0.0000000000001;
  265. /**
  266. * 0.00000000000001
  267. * @type {Number}
  268. * @constant
  269. */
  270. CesiumMath.EPSILON14 = 0.00000000000001;
  271. /**
  272. * 0.000000000000001
  273. * @type {Number}
  274. * @constant
  275. */
  276. CesiumMath.EPSILON15 = 0.000000000000001;
  277. /**
  278. * 0.0000000000000001
  279. * @type {Number}
  280. * @constant
  281. */
  282. CesiumMath.EPSILON16 = 0.0000000000000001;
  283. /**
  284. * 0.00000000000000001
  285. * @type {Number}
  286. * @constant
  287. */
  288. CesiumMath.EPSILON17 = 0.00000000000000001;
  289. /**
  290. * 0.000000000000000001
  291. * @type {Number}
  292. * @constant
  293. */
  294. CesiumMath.EPSILON18 = 0.000000000000000001;
  295. /**
  296. * 0.0000000000000000001
  297. * @type {Number}
  298. * @constant
  299. */
  300. CesiumMath.EPSILON19 = 0.0000000000000000001;
  301. /**
  302. * 0.00000000000000000001
  303. * @type {Number}
  304. * @constant
  305. */
  306. CesiumMath.EPSILON20 = 0.00000000000000000001;
  307. /**
  308. * 0.000000000000000000001
  309. * @type {Number}
  310. * @constant
  311. */
  312. CesiumMath.EPSILON21 = 0.000000000000000000001;
  313. /**
  314. * The gravitational parameter of the Earth in meters cubed
  315. * per second squared as defined by the WGS84 model: 3.986004418e14
  316. * @type {Number}
  317. * @constant
  318. */
  319. CesiumMath.GRAVITATIONALPARAMETER = 3.986004418e14;
  320. /**
  321. * Radius of the sun in meters: 6.955e8
  322. * @type {Number}
  323. * @constant
  324. */
  325. CesiumMath.SOLAR_RADIUS = 6.955e8;
  326. /**
  327. * The mean radius of the moon, according to the "Report of the IAU/IAG Working Group on
  328. * Cartographic Coordinates and Rotational Elements of the Planets and satellites: 2000",
  329. * Celestial Mechanics 82: 83-110, 2002.
  330. * @type {Number}
  331. * @constant
  332. */
  333. CesiumMath.LUNAR_RADIUS = 1737400.0;
  334. /**
  335. * 64 * 1024
  336. * @type {Number}
  337. * @constant
  338. */
  339. CesiumMath.SIXTY_FOUR_KILOBYTES = 64 * 1024;
  340. /**
  341. * Returns the sign of the value; 1 if the value is positive, -1 if the value is
  342. * negative, or 0 if the value is 0.
  343. *
  344. * @function
  345. * @param {Number} value The value to return the sign of.
  346. * @returns {Number} The sign of value.
  347. */
  348. CesiumMath.sign = when.defaultValue(Math.sign, function sign(value) {
  349. value = +value; // coerce to number
  350. if (value === 0 || value !== value) {
  351. // zero or NaN
  352. return value;
  353. }
  354. return value > 0 ? 1 : -1;
  355. });
  356. /**
  357. * Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
  358. * This is similar to {@link CesiumMath#sign} except that returns 1.0 instead of
  359. * 0.0 when the input value is 0.0.
  360. * @param {Number} value The value to return the sign of.
  361. * @returns {Number} The sign of value.
  362. */
  363. CesiumMath.signNotZero = function(value) {
  364. return value < 0.0 ? -1.0 : 1.0;
  365. };
  366. /**
  367. * Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMax]
  368. * @param {Number} value The scalar value in the range [-1.0, 1.0]
  369. * @param {Number} [rangeMax=255] The maximum value in the mapped range, 255 by default.
  370. * @returns {Number} A SNORM value, where 0 maps to -1.0 and rangeMax maps to 1.0.
  371. *
  372. * @see CesiumMath.fromSNorm
  373. */
  374. CesiumMath.toSNorm = function(value, rangeMax) {
  375. rangeMax = when.defaultValue(rangeMax, 255);
  376. return Math.round((CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMax);
  377. };
  378. /**
  379. * Converts a SNORM value in the range [0, rangeMax] to a scalar in the range [-1.0, 1.0].
  380. * @param {Number} value SNORM value in the range [0, 255]
  381. * @param {Number} [rangeMax=255] The maximum value in the SNORM range, 255 by default.
  382. * @returns {Number} Scalar in the range [-1.0, 1.0].
  383. *
  384. * @see CesiumMath.toSNorm
  385. */
  386. CesiumMath.fromSNorm = function(value, rangeMax) {
  387. rangeMax = when.defaultValue(rangeMax, 255);
  388. return CesiumMath.clamp(value, 0.0, rangeMax) / rangeMax * 2.0 - 1.0;
  389. };
  390. /**
  391. * Converts a scalar value in the range [rangeMinimum, rangeMaximum] to a scalar in the range [0.0, 1.0]
  392. * @param {Number} value The scalar value in the range [rangeMinimum, rangeMaximum]
  393. * @param {Number} rangeMinimum The minimum value in the mapped range.
  394. * @param {Number} rangeMaximum The maximum value in the mapped range.
  395. * @returns {Number} A scalar value, where rangeMinimum maps to 0.0 and rangeMaximum maps to 1.0.
  396. */
  397. CesiumMath.normalize = function(value, rangeMinimum, rangeMaximum) {
  398. rangeMaximum = Math.max(rangeMaximum - rangeMinimum, 0.0);
  399. return rangeMaximum === 0.0 ? 0.0 : CesiumMath.clamp((value - rangeMinimum) / rangeMaximum, 0.0, 1.0);
  400. };
  401. /**
  402. * Returns the hyperbolic sine of a number.
  403. * The hyperbolic sine of <em>value</em> is defined to be
  404. * (<em>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></em>)/2.0
  405. * where <i>e</i> is Euler's number, approximately 2.71828183.
  406. *
  407. * <p>Special cases:
  408. * <ul>
  409. * <li>If the argument is NaN, then the result is NaN.</li>
  410. *
  411. * <li>If the argument is infinite, then the result is an infinity
  412. * with the same sign as the argument.</li>
  413. *
  414. * <li>If the argument is zero, then the result is a zero with the
  415. * same sign as the argument.</li>
  416. * </ul>
  417. *</p>
  418. *
  419. * @function
  420. * @param {Number} value The number whose hyperbolic sine is to be returned.
  421. * @returns {Number} The hyperbolic sine of <code>value</code>.
  422. */
  423. CesiumMath.sinh = when.defaultValue(Math.sinh, function sinh(value) {
  424. return (Math.exp(value) - Math.exp(-value)) / 2.0;
  425. });
  426. /**
  427. * Returns the hyperbolic cosine of a number.
  428. * The hyperbolic cosine of <strong>value</strong> is defined to be
  429. * (<em>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></em>)/2.0
  430. * where <i>e</i> is Euler's number, approximately 2.71828183.
  431. *
  432. * <p>Special cases:
  433. * <ul>
  434. * <li>If the argument is NaN, then the result is NaN.</li>
  435. *
  436. * <li>If the argument is infinite, then the result is positive infinity.</li>
  437. *
  438. * <li>If the argument is zero, then the result is 1.0.</li>
  439. * </ul>
  440. *</p>
  441. *
  442. * @function
  443. * @param {Number} value The number whose hyperbolic cosine is to be returned.
  444. * @returns {Number} The hyperbolic cosine of <code>value</code>.
  445. */
  446. CesiumMath.cosh = when.defaultValue(Math.cosh, function cosh(value) {
  447. return (Math.exp(value) + Math.exp(-value)) / 2.0;
  448. });
  449. /**
  450. * Computes the linear interpolation of two values.
  451. *
  452. * @param {Number} p The start value to interpolate.
  453. * @param {Number} q The end value to interpolate.
  454. * @param {Number} time The time of interpolation generally in the range <code>[0.0, 1.0]</code>.
  455. * @returns {Number} The linearly interpolated value.
  456. *
  457. * @example
  458. * var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0
  459. */
  460. CesiumMath.lerp = function(p, q, time) {
  461. return ((1.0 - time) * p) + (time * q);
  462. };
  463. /**
  464. * pi
  465. *
  466. * @type {Number}
  467. * @constant
  468. */
  469. CesiumMath.PI = Math.PI;
  470. /**
  471. * 1/pi
  472. *
  473. * @type {Number}
  474. * @constant
  475. */
  476. CesiumMath.ONE_OVER_PI = 1.0 / Math.PI;
  477. /**
  478. * pi/2
  479. *
  480. * @type {Number}
  481. * @constant
  482. */
  483. CesiumMath.PI_OVER_TWO = Math.PI / 2.0;
  484. /**
  485. * pi/3
  486. *
  487. * @type {Number}
  488. * @constant
  489. */
  490. CesiumMath.PI_OVER_THREE = Math.PI / 3.0;
  491. /**
  492. * pi/4
  493. *
  494. * @type {Number}
  495. * @constant
  496. */
  497. CesiumMath.PI_OVER_FOUR = Math.PI / 4.0;
  498. /**
  499. * pi/6
  500. *
  501. * @type {Number}
  502. * @constant
  503. */
  504. CesiumMath.PI_OVER_SIX = Math.PI / 6.0;
  505. /**
  506. * 3pi/2
  507. *
  508. * @type {Number}
  509. * @constant
  510. */
  511. CesiumMath.THREE_PI_OVER_TWO = 3.0 * Math.PI / 2.0;
  512. /**
  513. * 2pi
  514. *
  515. * @type {Number}
  516. * @constant
  517. */
  518. CesiumMath.TWO_PI = 2.0 * Math.PI;
  519. /**
  520. * 1/2pi
  521. *
  522. * @type {Number}
  523. * @constant
  524. */
  525. CesiumMath.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI);
  526. /**
  527. * The number of radians in a degree.
  528. *
  529. * @type {Number}
  530. * @constant
  531. * @default Math.PI / 180.0
  532. */
  533. CesiumMath.RADIANS_PER_DEGREE = Math.PI / 180.0;
  534. /**
  535. * The number of degrees in a radian.
  536. *
  537. * @type {Number}
  538. * @constant
  539. * @default 180.0 / Math.PI
  540. */
  541. CesiumMath.DEGREES_PER_RADIAN = 180.0 / Math.PI;
  542. /**
  543. * The number of radians in an arc second.
  544. *
  545. * @type {Number}
  546. * @constant
  547. * @default {@link CesiumMath.RADIANS_PER_DEGREE} / 3600.0
  548. */
  549. CesiumMath.RADIANS_PER_ARCSECOND = CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  550. /**
  551. * Converts degrees to radians.
  552. * @param {Number} degrees The angle to convert in degrees.
  553. * @returns {Number} The corresponding angle in radians.
  554. */
  555. CesiumMath.toRadians = function(degrees) {
  556. //>>includeStart('debug', pragmas.debug);
  557. if (!when.defined(degrees)) {
  558. throw new Check.DeveloperError('degrees is required.');
  559. }
  560. //>>includeEnd('debug');
  561. return degrees * CesiumMath.RADIANS_PER_DEGREE;
  562. };
  563. /**
  564. * Converts radians to degrees.
  565. * @param {Number} radians The angle to convert in radians.
  566. * @returns {Number} The corresponding angle in degrees.
  567. */
  568. CesiumMath.toDegrees = function(radians) {
  569. //>>includeStart('debug', pragmas.debug);
  570. if (!when.defined(radians)) {
  571. throw new Check.DeveloperError('radians is required.');
  572. }
  573. //>>includeEnd('debug');
  574. return radians * CesiumMath.DEGREES_PER_RADIAN;
  575. };
  576. /**
  577. * Converts a longitude value, in radians, to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  578. *
  579. * @param {Number} angle The longitude value, in radians, to convert to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  580. * @returns {Number} The equivalent longitude value in the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  581. *
  582. * @example
  583. * // Convert 270 degrees to -90 degrees longitude
  584. * var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));
  585. */
  586. CesiumMath.convertLongitudeRange = function(angle) {
  587. //>>includeStart('debug', pragmas.debug);
  588. if (!when.defined(angle)) {
  589. throw new Check.DeveloperError('angle is required.');
  590. }
  591. //>>includeEnd('debug');
  592. var twoPi = CesiumMath.TWO_PI;
  593. var simplified = angle - Math.floor(angle / twoPi) * twoPi;
  594. if (simplified < -Math.PI) {
  595. return simplified + twoPi;
  596. }
  597. if (simplified >= Math.PI) {
  598. return simplified - twoPi;
  599. }
  600. return simplified;
  601. };
  602. /**
  603. * Convenience function that clamps a latitude value, in radians, to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  604. * Useful for sanitizing data before use in objects requiring correct range.
  605. *
  606. * @param {Number} angle The latitude value, in radians, to clamp to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  607. * @returns {Number} The latitude value clamped to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  608. *
  609. * @example
  610. * // Clamp 108 degrees latitude to 90 degrees latitude
  611. * var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));
  612. */
  613. CesiumMath.clampToLatitudeRange = function(angle) {
  614. //>>includeStart('debug', pragmas.debug);
  615. if (!when.defined(angle)) {
  616. throw new Check.DeveloperError('angle is required.');
  617. }
  618. //>>includeEnd('debug');
  619. return CesiumMath.clamp(angle, -1*CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO);
  620. };
  621. /**
  622. * Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
  623. *
  624. * @param {Number} angle in radians
  625. * @returns {Number} The angle in the range [<code>-CesiumMath.PI</code>, <code>CesiumMath.PI</code>].
  626. */
  627. CesiumMath.negativePiToPi = function(angle) {
  628. //>>includeStart('debug', pragmas.debug);
  629. if (!when.defined(angle)) {
  630. throw new Check.DeveloperError('angle is required.');
  631. }
  632. //>>includeEnd('debug');
  633. return CesiumMath.zeroToTwoPi(angle + CesiumMath.PI) - CesiumMath.PI;
  634. };
  635. /**
  636. * Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
  637. *
  638. * @param {Number} angle in radians
  639. * @returns {Number} The angle in the range [0, <code>CesiumMath.TWO_PI</code>].
  640. */
  641. CesiumMath.zeroToTwoPi = function(angle) {
  642. //>>includeStart('debug', pragmas.debug);
  643. if (!when.defined(angle)) {
  644. throw new Check.DeveloperError('angle is required.');
  645. }
  646. //>>includeEnd('debug');
  647. var mod = CesiumMath.mod(angle, CesiumMath.TWO_PI);
  648. if (Math.abs(mod) < CesiumMath.EPSILON14 && Math.abs(angle) > CesiumMath.EPSILON14) {
  649. return CesiumMath.TWO_PI;
  650. }
  651. return mod;
  652. };
  653. /**
  654. * The modulo operation that also works for negative dividends.
  655. *
  656. * @param {Number} m The dividend.
  657. * @param {Number} n The divisor.
  658. * @returns {Number} The remainder.
  659. */
  660. CesiumMath.mod = function(m, n) {
  661. //>>includeStart('debug', pragmas.debug);
  662. if (!when.defined(m)) {
  663. throw new Check.DeveloperError('m is required.');
  664. }
  665. if (!when.defined(n)) {
  666. throw new Check.DeveloperError('n is required.');
  667. }
  668. //>>includeEnd('debug');
  669. return ((m % n) + n) % n;
  670. };
  671. /**
  672. * Determines if two values are equal using an absolute or relative tolerance test. This is useful
  673. * to avoid problems due to roundoff error when comparing floating-point values directly. The values are
  674. * first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed.
  675. * Use this test if you are unsure of the magnitudes of left and right.
  676. *
  677. * @param {Number} left The first value to compare.
  678. * @param {Number} right The other value to compare.
  679. * @param {Number} relativeEpsilon The maximum inclusive delta between <code>left</code> and <code>right</code> for the relative tolerance test.
  680. * @param {Number} [absoluteEpsilon=relativeEpsilon] The maximum inclusive delta between <code>left</code> and <code>right</code> for the absolute tolerance test.
  681. * @returns {Boolean} <code>true</code> if the values are equal within the epsilon; otherwise, <code>false</code>.
  682. *
  683. * @example
  684. * var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
  685. * var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2); // false
  686. * var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
  687. * var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false
  688. */
  689. CesiumMath.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  690. //>>includeStart('debug', pragmas.debug);
  691. if (!when.defined(left)) {
  692. throw new Check.DeveloperError('left is required.');
  693. }
  694. if (!when.defined(right)) {
  695. throw new Check.DeveloperError('right is required.');
  696. }
  697. if (!when.defined(relativeEpsilon)) {
  698. throw new Check.DeveloperError('relativeEpsilon is required.');
  699. }
  700. //>>includeEnd('debug');
  701. absoluteEpsilon = when.defaultValue(absoluteEpsilon, relativeEpsilon);
  702. var absDiff = Math.abs(left - right);
  703. return absDiff <= absoluteEpsilon || absDiff <= relativeEpsilon * Math.max(Math.abs(left), Math.abs(right));
  704. };
  705. /**
  706. * Determines if the left value is less than the right value. If the two values are within
  707. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
  708. *
  709. * @param {Number} left The first number to compare.
  710. * @param {Number} right The second number to compare.
  711. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  712. * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> by more than
  713. * <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is greater or if the two
  714. * values are nearly equal.
  715. */
  716. CesiumMath.lessThan = function(left, right, absoluteEpsilon) {
  717. //>>includeStart('debug', pragmas.debug);
  718. if (!when.defined(left)) {
  719. throw new Check.DeveloperError('first is required.');
  720. }
  721. if (!when.defined(right)) {
  722. throw new Check.DeveloperError('second is required.');
  723. }
  724. if (!when.defined(absoluteEpsilon)) {
  725. throw new Check.DeveloperError('relativeEpsilon is required.');
  726. }
  727. //>>includeEnd('debug');
  728. return left - right < -absoluteEpsilon;
  729. };
  730. /**
  731. * Determines if the left value is less than or equal to the right value. If the two values are within
  732. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
  733. *
  734. * @param {Number} left The first number to compare.
  735. * @param {Number} right The second number to compare.
  736. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  737. * @returns {Boolean} <code>true</code> if <code>left</code> is less than <code>right</code> or if the
  738. * the values are nearly equal.
  739. */
  740. CesiumMath.lessThanOrEquals = function(left, right, absoluteEpsilon) {
  741. //>>includeStart('debug', pragmas.debug);
  742. if (!when.defined(left)) {
  743. throw new Check.DeveloperError('first is required.');
  744. }
  745. if (!when.defined(right)) {
  746. throw new Check.DeveloperError('second is required.');
  747. }
  748. if (!when.defined(absoluteEpsilon)) {
  749. throw new Check.DeveloperError('relativeEpsilon is required.');
  750. }
  751. //>>includeEnd('debug');
  752. return left - right < absoluteEpsilon;
  753. };
  754. /**
  755. * Determines if the left value is greater the right value. If the two values are within
  756. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns false.
  757. *
  758. * @param {Number} left The first number to compare.
  759. * @param {Number} right The second number to compare.
  760. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  761. * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> by more than
  762. * <code>absoluteEpsilon<code>. <code>false</code> if <code>left</code> is less or if the two
  763. * values are nearly equal.
  764. */
  765. CesiumMath.greaterThan = function(left, right, absoluteEpsilon) {
  766. //>>includeStart('debug', pragmas.debug);
  767. if (!when.defined(left)) {
  768. throw new Check.DeveloperError('first is required.');
  769. }
  770. if (!when.defined(right)) {
  771. throw new Check.DeveloperError('second is required.');
  772. }
  773. if (!when.defined(absoluteEpsilon)) {
  774. throw new Check.DeveloperError('relativeEpsilon is required.');
  775. }
  776. //>>includeEnd('debug');
  777. return left - right > absoluteEpsilon;
  778. };
  779. /**
  780. * Determines if the left value is greater than or equal to the right value. If the two values are within
  781. * <code>absoluteEpsilon</code> of each other, they are considered equal and this function returns true.
  782. *
  783. * @param {Number} left The first number to compare.
  784. * @param {Number} right The second number to compare.
  785. * @param {Number} absoluteEpsilon The absolute epsilon to use in comparison.
  786. * @returns {Boolean} <code>true</code> if <code>left</code> is greater than <code>right</code> or if the
  787. * the values are nearly equal.
  788. */
  789. CesiumMath.greaterThanOrEquals = function(left, right, absoluteEpsilon) {
  790. //>>includeStart('debug', pragmas.debug);
  791. if (!when.defined(left)) {
  792. throw new Check.DeveloperError('first is required.');
  793. }
  794. if (!when.defined(right)) {
  795. throw new Check.DeveloperError('second is required.');
  796. }
  797. if (!when.defined(absoluteEpsilon)) {
  798. throw new Check.DeveloperError('relativeEpsilon is required.');
  799. }
  800. //>>includeEnd('debug');
  801. return left - right > -absoluteEpsilon;
  802. };
  803. var factorials = [1];
  804. /**
  805. * Computes the factorial of the provided number.
  806. *
  807. * @param {Number} n The number whose factorial is to be computed.
  808. * @returns {Number} The factorial of the provided number or undefined if the number is less than 0.
  809. *
  810. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  811. *
  812. *
  813. * @example
  814. * //Compute 7!, which is equal to 5040
  815. * var computedFactorial = Cesium.Math.factorial(7);
  816. *
  817. * @see {@link http://en.wikipedia.org/wiki/Factorial|Factorial on Wikipedia}
  818. */
  819. CesiumMath.factorial = function(n) {
  820. //>>includeStart('debug', pragmas.debug);
  821. if (typeof n !== 'number' || n < 0) {
  822. throw new Check.DeveloperError('A number greater than or equal to 0 is required.');
  823. }
  824. //>>includeEnd('debug');
  825. var length = factorials.length;
  826. if (n >= length) {
  827. var sum = factorials[length - 1];
  828. for (var i = length; i <= n; i++) {
  829. var next = sum * i;
  830. factorials.push(next);
  831. sum = next;
  832. }
  833. }
  834. return factorials[n];
  835. };
  836. /**
  837. * Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
  838. *
  839. * @param {Number} [n] The number to be incremented.
  840. * @param {Number} [maximumValue] The maximum incremented value before rolling over to the minimum value.
  841. * @param {Number} [minimumValue=0.0] The number reset to after the maximum value has been exceeded.
  842. * @returns {Number} The incremented number.
  843. *
  844. * @exception {DeveloperError} Maximum value must be greater than minimum value.
  845. *
  846. * @example
  847. * var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
  848. * var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0
  849. */
  850. CesiumMath.incrementWrap = function(n, maximumValue, minimumValue) {
  851. minimumValue = when.defaultValue(minimumValue, 0.0);
  852. //>>includeStart('debug', pragmas.debug);
  853. if (!when.defined(n)) {
  854. throw new Check.DeveloperError('n is required.');
  855. }
  856. if (maximumValue <= minimumValue) {
  857. throw new Check.DeveloperError('maximumValue must be greater than minimumValue.');
  858. }
  859. //>>includeEnd('debug');
  860. ++n;
  861. if (n > maximumValue) {
  862. n = minimumValue;
  863. }
  864. return n;
  865. };
  866. /**
  867. * Determines if a positive integer is a power of two.
  868. *
  869. * @param {Number} n The positive integer to test.
  870. * @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
  871. *
  872. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  873. *
  874. * @example
  875. * var t = Cesium.Math.isPowerOfTwo(16); // true
  876. * var f = Cesium.Math.isPowerOfTwo(20); // false
  877. */
  878. CesiumMath.isPowerOfTwo = function(n) {
  879. //>>includeStart('debug', pragmas.debug);
  880. if (typeof n !== 'number' || n < 0) {
  881. throw new Check.DeveloperError('A number greater than or equal to 0 is required.');
  882. }
  883. //>>includeEnd('debug');
  884. return (n !== 0) && ((n & (n - 1)) === 0);
  885. };
  886. /**
  887. * Computes the next power-of-two integer greater than or equal to the provided positive integer.
  888. *
  889. * @param {Number} n The positive integer to test.
  890. * @returns {Number} The next power-of-two integer.
  891. *
  892. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  893. *
  894. * @example
  895. * var n = Cesium.Math.nextPowerOfTwo(29); // 32
  896. * var m = Cesium.Math.nextPowerOfTwo(32); // 32
  897. */
  898. CesiumMath.nextPowerOfTwo = function(n) {
  899. //>>includeStart('debug', pragmas.debug);
  900. if (typeof n !== 'number' || n < 0) {
  901. throw new Check.DeveloperError('A number greater than or equal to 0 is required.');
  902. }
  903. //>>includeEnd('debug');
  904. // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  905. --n;
  906. n |= n >> 1;
  907. n |= n >> 2;
  908. n |= n >> 4;
  909. n |= n >> 8;
  910. n |= n >> 16;
  911. ++n;
  912. return n;
  913. };
  914. /**
  915. * Constraint a value to lie between two values.
  916. *
  917. * @param {Number} value The value to constrain.
  918. * @param {Number} min The minimum value.
  919. * @param {Number} max The maximum value.
  920. * @returns {Number} The value clamped so that min <= value <= max.
  921. */
  922. CesiumMath.clamp = function(value, min, max) {
  923. //>>includeStart('debug', pragmas.debug);
  924. if (!when.defined(value)) {
  925. throw new Check.DeveloperError('value is required');
  926. }
  927. if (!when.defined(min)) {
  928. throw new Check.DeveloperError('min is required.');
  929. }
  930. if (!when.defined(max)) {
  931. throw new Check.DeveloperError('max is required.');
  932. }
  933. //>>includeEnd('debug');
  934. return value < min ? min : value > max ? max : value;
  935. };
  936. var randomNumberGenerator = new MersenneTwister();
  937. /**
  938. * Sets the seed used by the random number generator
  939. * in {@link CesiumMath#nextRandomNumber}.
  940. *
  941. * @param {Number} seed An integer used as the seed.
  942. */
  943. CesiumMath.setRandomNumberSeed = function(seed) {
  944. //>>includeStart('debug', pragmas.debug);
  945. if (!when.defined(seed)) {
  946. throw new Check.DeveloperError('seed is required.');
  947. }
  948. //>>includeEnd('debug');
  949. randomNumberGenerator = new MersenneTwister(seed);
  950. };
  951. /**
  952. * Generates a random floating point number in the range of [0.0, 1.0)
  953. * using a Mersenne twister.
  954. *
  955. * @returns {Number} A random number in the range of [0.0, 1.0).
  956. *
  957. * @see CesiumMath.setRandomNumberSeed
  958. * @see {@link http://en.wikipedia.org/wiki/Mersenne_twister|Mersenne twister on Wikipedia}
  959. */
  960. CesiumMath.nextRandomNumber = function() {
  961. return randomNumberGenerator.random();
  962. };
  963. /**
  964. * Generates a random number between two numbers.
  965. *
  966. * @param {Number} min The minimum value.
  967. * @param {Number} max The maximum value.
  968. * @returns {Number} A random number between the min and max.
  969. */
  970. CesiumMath.randomBetween = function(min, max) {
  971. return CesiumMath.nextRandomNumber() * (max - min) + min;
  972. };
  973. /**
  974. * Computes <code>Math.acos(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  975. * so that the function will never return NaN.
  976. *
  977. * @param {Number} value The value for which to compute acos.
  978. * @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,
  979. * whichever is closer, if the value is outside the range.
  980. */
  981. CesiumMath.acosClamped = function(value) {
  982. //>>includeStart('debug', pragmas.debug);
  983. if (!when.defined(value)) {
  984. throw new Check.DeveloperError('value is required.');
  985. }
  986. //>>includeEnd('debug');
  987. return Math.acos(CesiumMath.clamp(value, -1.0, 1.0));
  988. };
  989. /**
  990. * Computes <code>Math.asin(value)</code>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  991. * so that the function will never return NaN.
  992. *
  993. * @param {Number} value The value for which to compute asin.
  994. * @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,
  995. * whichever is closer, if the value is outside the range.
  996. */
  997. CesiumMath.asinClamped = function(value) {
  998. //>>includeStart('debug', pragmas.debug);
  999. if (!when.defined(value)) {
  1000. throw new Check.DeveloperError('value is required.');
  1001. }
  1002. //>>includeEnd('debug');
  1003. return Math.asin(CesiumMath.clamp(value, -1.0, 1.0));
  1004. };
  1005. /**
  1006. * Finds the chord length between two points given the circle's radius and the angle between the points.
  1007. *
  1008. * @param {Number} angle The angle between the two points.
  1009. * @param {Number} radius The radius of the circle.
  1010. * @returns {Number} The chord length.
  1011. */
  1012. CesiumMath.chordLength = function(angle, radius) {
  1013. //>>includeStart('debug', pragmas.debug);
  1014. if (!when.defined(angle)) {
  1015. throw new Check.DeveloperError('angle is required.');
  1016. }
  1017. if (!when.defined(radius)) {
  1018. throw new Check.DeveloperError('radius is required.');
  1019. }
  1020. //>>includeEnd('debug');
  1021. return 2.0 * radius * Math.sin(angle * 0.5);
  1022. };
  1023. /**
  1024. * Finds the logarithm of a number to a base.
  1025. *
  1026. * @param {Number} number The number.
  1027. * @param {Number} base The base.
  1028. * @returns {Number} The result.
  1029. */
  1030. CesiumMath.logBase = function(number, base) {
  1031. //>>includeStart('debug', pragmas.debug);
  1032. if (!when.defined(number)) {
  1033. throw new Check.DeveloperError('number is required.');
  1034. }
  1035. if (!when.defined(base)) {
  1036. throw new Check.DeveloperError('base is required.');
  1037. }
  1038. //>>includeEnd('debug');
  1039. return Math.log(number) / Math.log(base);
  1040. };
  1041. /**
  1042. * Finds the cube root of a number.
  1043. * Returns NaN if <code>number</code> is not provided.
  1044. *
  1045. * @function
  1046. * @param {Number} [number] The number.
  1047. * @returns {Number} The result.
  1048. */
  1049. CesiumMath.cbrt = when.defaultValue(Math.cbrt, function cbrt(number) {
  1050. var result = Math.pow(Math.abs(number), 1.0 / 3.0);
  1051. return number < 0.0 ? -result : result;
  1052. });
  1053. /**
  1054. * Finds the base 2 logarithm of a number.
  1055. *
  1056. * @function
  1057. * @param {Number} number The number.
  1058. * @returns {Number} The result.
  1059. */
  1060. CesiumMath.log2 = when.defaultValue(Math.log2, function log2(number) {
  1061. return Math.log(number) * Math.LOG2E;
  1062. });
  1063. /**
  1064. * @private
  1065. */
  1066. CesiumMath.fog = function(distanceToCamera, density) {
  1067. var scalar = distanceToCamera * density;
  1068. return 1.0 - Math.exp(-(scalar * scalar));
  1069. };
  1070. /**
  1071. * Computes a fast approximation of Atan for input in the range [-1, 1].
  1072. *
  1073. * Based on Michal Drobot's approximation from ShaderFastLibs,
  1074. * which in turn is based on "Efficient approximations for the arctangent function,"
  1075. * Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006.
  1076. * Adapted from ShaderFastLibs under MIT License.
  1077. *
  1078. * @param {Number} x An input number in the range [-1, 1]
  1079. * @returns {Number} An approximation of atan(x)
  1080. */
  1081. CesiumMath.fastApproximateAtan = function(x) {
  1082. //>>includeStart('debug', pragmas.debug);
  1083. Check.Check.typeOf.number('x', x);
  1084. //>>includeEnd('debug');
  1085. return x * (-0.1784 * Math.abs(x) - 0.0663 * x * x + 1.0301);
  1086. };
  1087. /**
  1088. * Computes a fast approximation of Atan2(x, y) for arbitrary input scalars.
  1089. *
  1090. * Range reduction math based on nvidia's cg reference implementation: http://developer.download.nvidia.com/cg/atan2.html
  1091. *
  1092. * @param {Number} x An input number that isn't zero if y is zero.
  1093. * @param {Number} y An input number that isn't zero if x is zero.
  1094. * @returns {Number} An approximation of atan2(x, y)
  1095. */
  1096. CesiumMath.fastApproximateAtan2 = function(x, y) {
  1097. //>>includeStart('debug', pragmas.debug);
  1098. Check.Check.typeOf.number('x', x);
  1099. Check.Check.typeOf.number('y', y);
  1100. //>>includeEnd('debug');
  1101. // atan approximations are usually only reliable over [-1, 1]
  1102. // So reduce the range by flipping whether x or y is on top based on which is bigger.
  1103. var opposite;
  1104. var adjacent;
  1105. var t = Math.abs(x); // t used as swap and atan result.
  1106. opposite = Math.abs(y);
  1107. adjacent = Math.max(t, opposite);
  1108. opposite = Math.min(t, opposite);
  1109. var oppositeOverAdjacent = opposite / adjacent;
  1110. //>>includeStart('debug', pragmas.debug);
  1111. if (isNaN(oppositeOverAdjacent)) {
  1112. throw new Check.DeveloperError('either x or y must be nonzero');
  1113. }
  1114. //>>includeEnd('debug');
  1115. t = CesiumMath.fastApproximateAtan(oppositeOverAdjacent);
  1116. // Undo range reduction
  1117. t = Math.abs(y) > Math.abs(x) ? CesiumMath.PI_OVER_TWO - t : t;
  1118. t = x < 0.0 ? CesiumMath.PI - t : t;
  1119. t = y < 0.0 ? -t : t;
  1120. return t;
  1121. };
  1122. exports.CesiumMath = CesiumMath;
  1123. });