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