• Cesium源码之CesiumMath(1.62版)


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

    参考:https://cesium.com/downloads/cesiumjs/releases/b29/Documentation/CesiumMath.html

    https://cesium.com/downloads/cesiumjs/releases/1.62/

    https://cesium.com/downloads/cesiumjs/releases/1.62/Build/Documentation/index.html

  • 相关阅读:
    Selenium的自我总结1
    软件测试之我看
    Appium学习笔记3_Genymotion模拟器安装
    Appium学习笔记2_Android获取元素篇
    Appium学习笔记1_获取到APK安装包的Package以及Activity属性值
    MySql绿色版安装配置
    BUG管理工具——Mantis安装配置
    Git—学习笔记1
    TestNG—学习笔记2
    redis主从哨兵模式
  • 原文地址:https://www.cnblogs.com/2008nmj/p/15631790.html
Copyright © 2020-2023  润新知