-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
1280 lines (1160 loc) · 40.9 KB
/
Utils.cs
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
using System;
using System.Text;
/// <summary>
/// Implements geometric math utilities for single-precision numbers.
/// </summary>
public static class Utils
{
/// <summary>
/// An angle in degrees is multiplied by this constant to convert it to
/// radians. PI / 180.0, approximately 0.0174532924 .
/// </summary>
public const float DegToRad = 0.0174532924f;
/// <summary>
/// The smallest positive non-zero value. Useful for testing approximation
/// between two floats. Set to 0.000001 .
/// </summary>
public const float Epsilon = 0.000001f;
/// <summary>
/// Four-thirds, 4.0 / 3.0 . Approximately 1.33333333 . Useful when creating
/// a circular shape with a series of Bezier curves.
/// </summary>
public const float FourThirds = 1.33333333f;
/// <summary>
/// An approximation of TAU / ( PHI * PHI ) , 2.39996314 . Useful for
/// replicating phyllotaxis. In degrees, 137.50777 .
/// </summary>
public const float GoldenAngle = 2.39996314f;
/// <summary>
/// PI divided by 2.0 . Approximately 1.57079637 .
/// </summary>
public const float HalfPi = 1.57079637f;
/// <summary>
/// Base value used by hash code functions.
/// </summary>
public const int HashBase = -2128831035;
/// <summary>
/// Multiplier used by hash code functions.
/// </summary>
public const int HashMul = 16777619;
/// <summary>
/// Magnitude for orthogonal handles when four curve knots are used to
/// approximate an ellipse or circle (90 degrees per knot), Derived from
/// (Math.Sqrt(2.0) - 1.0) * 4.0 / 3.0 .
/// </summary>
public const float Kappa = 0.552285f;
/// <summary>
/// The hash base multiplied by the hash multiplier.
/// </summary>
public const int MulBase = 84696351;
/// <summary>
/// One-255th, 1.0 / 255.0 . Useful when converting a color with channels in
/// the range [0, 255] to a color in the range [0.0, 1.0] . Approximately
/// 0.003921569 .
/// </summary>
public const float One255 = 0.003921569f;
/// <summary>
/// One divided by 360 degrees, 1.0 / 360.0 ; approximately 0.00277777785 .
/// Useful for converting an index in a for-loop to an angle in degrees.
/// </summary>
public const float One360 = 0.00277777785f;
/// <summary>
/// One divided by PI . Useful when converting inclinations to the range
/// [0.0, 1.0] . Approximately 0.318309873 .
/// </summary>
public const float OnePi = 0.318309873f;
/// <summary>
/// An approximation of 1.0 / SQRT ( 2.0 ) , 0.707106769 .
/// </summary>
public const float OneSqrt2 = 0.707106769f;
/// <summary>
/// An approximation of 1.0 / SQRT ( 3.0 ) , 0.577350259 .
/// </summary>
public const float OneSqrt3 = 0.577350259f;
/// <summary>
/// One divided by TAU . Useful for converting an index in a for-loop to an
/// angle. Approximately 0.159154937 .
/// </summary>
public const float OneTau = 0.159154937f;
/// <summary>
/// One-third, 1.0 / 3.0 . Approximately 0.333333333 . Useful for setting
/// handles on the knot of a Bezier curve.
/// </summary>
public const float OneThird = 0.333333333f;
/// <summary>
/// An approximation of PHI , or ( 1.0 + SQRT ( 5.0 ) ) / 2.0 , 1.618034 .
/// </summary>
public const float Phi = 1.618034f;
/// <summary>
/// An angle in radians is multiplied by this constant to convert it to
/// degrees. 180.0 / PI , approximately 57.29578 .
/// </summary>
public const float RadToDeg = 57.29578f;
/// <summary>
/// An approximation of sqrt ( 3.0 ) , 1.7320508 .
/// </summary>
public const float Sqrt3 = 1.7320508f;
/// <summary>
/// An approximation of sqrt ( 3.0 ) / 2.0 , 0.8660254 .
/// </summary>
public const float Sqrt32 = 0.8660254f;
/// <summary>
/// An approximation of TAU, 6.28318548 . Equal to 2.0 PI .
/// </summary>
public const float Tau = 6.28318548f;
/// <summary>
/// PI divided by 3.0 , 1.04719758 . Useful for describing the field of
/// view in a perspective camera.
/// </summary>
public const float ThirdPi = 1.04719758f;
/// <summary>
/// Two-thirds, 2.0 / 3.0 . Approximately 0.6666667 .
/// </summary>
public const float TwoThirds = 0.6666667f;
/// <summary>
/// Evaluates two floats like booleans using the AND logic gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static int And(in float a, in float b)
{
return ((a != 0.0f) & (b != 0.0f)) ? 1 : 0;
}
/// <summary>
/// A quick approximation test. Tests to see if the absolute of the
/// difference between two values is less than a tolerance. Does not handle
/// edge cases.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <param name="tolerance">tolerance</param>
/// <returns>evaluation</returns>
public static bool Approx(
in float a,
in float b,
in float tolerance = Utils.Epsilon)
{
return Utils.Diff(a, b) <= tolerance;
}
/// <summary>
/// Raises a real number to the next greatest integer.
/// </summary>
/// <param name="v">input value</param>
/// <returns>raised value</returns>
public static int Ceil(in float v)
{
return (v > 0.0f) ? (int)v + 1 : (int)v;
}
/// <summary>
/// Clamps an real number between a lower and an upper bound.
/// </summary>
/// <param name="v">input value</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>clamped value</returns>
public static float Clamp(
in float v,
in float lb = 0.0f,
in float ub = 1.0f)
{
return (v < lb) ? lb : (v > ub) ? ub : v;
}
/// <summary>
/// Clamps an integer between a lower and an upper bound.
/// </summary>
/// <param name="v">input value</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>clamped value</returns>
public static int Clamp(in int v, in int lb, in int ub)
{
return (v < lb) ? lb : (v > ub) ? ub : v;
}
/// <summary>
/// Returns the first floating-point argument with the sign of the second
/// floating-point argument. Returns zero if the sign is zero.
/// </summary>
/// <param name="mag">magnitude</param>
/// <param name="sign">sign</param>
/// <returns>magnified sign</returns>
public static float CopySign(in float mag, in float sign)
{
// Don't use abs * sign, as the latter has more
// flexibility in terms of how you deal with zero sign.
// return Utils.Abs (mag) * Utils.Sign (sign);
return (sign < -0.0f) ? -MathF.Abs(mag) :
(sign > 0.0f) ? MathF.Abs(mag) :
0.0f;
}
/// <summary>
/// Returns the first floating-point argument with the sign of the second
/// floating-point argument. Returns zero if the sign is zero.
/// </summary>
/// <param name="mag">magnitude</param>
/// <param name="sign">sign</param>
/// <returns>magnified sign</returns>
public static int CopySign(in int mag, in int sign)
{
return (sign < 0) ? -Math.Abs(mag) :
(sign > 0) ? Math.Abs(mag) :
0;
}
/// <summary>
/// Finds the approximate cotangent of the angle in radians. Equivalent to
/// dividing the cosine of the angle by the sine, or to 1.0 / tan ( a ) .
/// </summary>
/// <param name="radians">angle in radians</param>
/// <returns>cotangent</returns>
public static float Cot(in float radians)
{
double rd = radians;
double sint = Math.Sin(rd);
return (sint != 0.0d) ? (float)(Math.Cos(rd) / sint) : 0.0f;
}
/// <summary>
/// Finds the absolute value of the right operand minus the left.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static float Diff(in float a, in float b)
{
return MathF.Abs(b - a);
}
/// <summary>
/// Finds the absolute value of the right operand minus the left.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static int Diff(in int a, in int b)
{
return Math.Abs(b - a);
}
/// <summary>
/// Finds the distance between two angles.
/// Angles are expected to be in radians.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>distance</returns>
public static float DistAngle(in float a, in float b)
{
return Utils.DistAngleSigned(a, b);
}
/// <summary>
/// Finds the distance between two periodic values. Example ranges are
/// 360.0 for degrees, 1.0 for hues or Tau for radians.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>distance</returns>
public static float DistAngle(in float a, in float b, in float r)
{
return Utils.DistAngleSigned(a, b, r);
}
/// <summary>
/// Finds the signed distance between two angles.
/// Angles are expected to be in radians.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>signed distance</returns>
public static float DistAngleSigned(in float a, in float b)
{
float diff = (a - b + MathF.PI) % Utils.Tau - MathF.PI;
return diff < -MathF.PI ? diff + Utils.Tau : diff;
}
/// <summary>
/// Finds the signed distance between two periodic values. Example ranges
/// are 360.0 for degrees, 1.0 for hues or Tau for radians.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <param name="r">range</param>
/// <returns>signed distance</returns>
public static float DistAngleSigned(in float a, in float b, in float r)
{
float halfRange = r * 0.5f;
float diff = (a - b + halfRange) % r - halfRange;
return diff < -halfRange ? diff + r : diff;
}
/// <summary>
/// Finds the unsigned distance between two angles. Angles are expected to
/// be in radians.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>unsigned distance</returns>
public static float DistAngleUnsigned(in float a, in float b)
{
return MathF.PI - MathF.Abs(MathF.Abs(Utils.WrapRadians(b) -
Utils.WrapRadians(a)) - MathF.PI);
}
/// <summary>
/// Finds the unsigned distance between two periodic values. Example ranges
/// are 360.0 for degrees, 1.0 for hues or Tau for radians.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <param name="r">range</param>
/// <returns>unsigned distance</returns>
public static float DistAngleUnsigned(in float a, in float b, in float r)
{
float halfRange = r * 0.5f;
return halfRange - MathF.Abs(MathF.Abs(
Utils.RemFloor(b, r) -
Utils.RemFloor(a, r)) - halfRange);
}
/// <summary>
/// Divides the left operand by the right, but returns zero when the
/// denominator is zero.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static int Div(in int a, in int b)
{
return (b != 0) ? a / b : 0;
}
/// <summary>
/// Divides the left operand by the right, but returns zero when the
/// denominator is zero.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static float Div(in float a, in float b)
{
return (b != 0.0f) ? a / b : 0.0f;
}
/// <summary>
/// Floors a real number to the next least integer.
/// </summary>
/// <param name="v">input value</param>
/// <returns>floored value</returns>
public static int Floor(in float v)
{
return (v > 0.0f) ? (int)v : (int)v - 1;
}
/// <summary>
/// Finds the signed fractional portion of the input value by subtracting
/// the value's truncation from the value. Not the same as GLSL fract.
/// </summary>
/// <param name="v">input value</param>
/// <returns>fraction</returns>
public static float Fract(in float v)
{
return v - MathF.Truncate(v);
}
/// <summary>
/// Finds one divided by the square root of an input value. Returns zero if
/// the input is zero.
/// </summary>
/// <param name="v">input value</param>
/// <returns>inverse square root</returns>
public static float InvSqrt(in float v)
{
return (v > 0.0f) ? Utils.InvSqrtUnchecked(v) : 0.0f;
}
/// <summary>
/// Finds one divided by the square root of an input value. Does not check
/// if the input is zero.
/// </summary>
/// <param name="v">input value</param>
/// <returns>inverse square root</returns>
public static float InvSqrtUnchecked(in float v)
{
return (float)(1.0d / Math.Sqrt(v));
}
/// <summary>
/// Eases from an origin angle in radians to a destination angle according
/// to a factor in [0.0, 1.0] .
/// </summary>
/// <param name="origin">origin angle</param>
/// <param name="dest">destination angle</param>
/// <param name="t">factor</param>
/// <param name="range">range</param>
/// <returns>angle</returns>
public static float LerpAngle(
in float origin,
in float dest,
in float t = 0.5f,
in float range = Utils.Tau)
{
return Utils.LerpAngleNear(origin, dest, t, range);
}
/// <summary>
/// Eases from an origin angle in radians to a destination angle using the
/// shortest direction (either clockwise or counter clockwise) according to
/// a factor in [0.0, 1.0] .
/// </summary>
/// <param name="origin">origin angle</param>
/// <param name="dest">destination angle</param>
/// <param name="t">factor</param>
/// <param name="range">range</param>
/// <returns>angle</returns>
public static float LerpAngleNear(
in float origin,
in float dest,
in float t = 0.5f,
in float range = Utils.Tau)
{
float o = Utils.RemFloor(origin, range);
float d = Utils.RemFloor(dest, range);
float diff = d - o;
if (diff == 0.0f) { return o; }
float halfRange = range * 0.5f;
if (o < d && diff > halfRange)
{
return Utils.RemFloor(
(1.0f - t) * (o + range) +
t * d,
range);
}
if (o > d && diff < -halfRange)
{
return Utils.RemFloor(
(1.0f - t) * o +
t * (d + range),
range);
}
return (1.0f - t) * o + t * d;
}
/// <summary>
/// Eases from an origin angle in radians to a destination angle using the
/// furthest direction (either clockwise or counter clockwise) according to
/// a factor in [0.0, 1.0] .
/// </summary>
/// <param name="origin">origin angle</param>
/// <param name="dest">destination angle</param>
/// <param name="t">factor</param>
/// <param name="range">range</param>
/// <returns>angle</returns>
public static float LerpAngleFar(
in float origin,
in float dest,
in float t = 0.5f,
in float range = Utils.Tau)
{
float o = Utils.RemFloor(origin, range);
float d = Utils.RemFloor(dest, range);
float diff = d - o;
float halfRange = range * 0.5f;
if (diff == 0.0f || (o < diff && diff < -halfRange))
{
return Utils.RemFloor(
(1.0f - t) * (o + range) + t * d,
range);
}
if (o > d && diff > -halfRange)
{
return Utils.RemFloor(
(1.0f - t) * o + t * (d + range),
range);
}
return (1.0f - t) * o + t * d;
}
/// <summary>
/// Eases from an origin angle in radians to a destination angle using the
/// counter clockwise direction according to
/// a factor in [0.0, 1.0] .
/// </summary>
/// <param name="origin">origin angle</param>
/// <param name="dest">destination angle</param>
/// <param name="t">factor</param>
/// <param name="range">range</param>
/// <returns>angle</returns>
public static float LerpAngleCCW(
in float origin,
in float dest,
in float t = 0.5f,
in float range = Utils.Tau)
{
float o = Utils.RemFloor(origin, range);
float d = Utils.RemFloor(dest, range);
float diff = d - o;
if (diff == 0.0f) { return o; }
if (o > d)
{
return Utils.RemFloor(
(1.0f - t) * o +
t * (d + range),
range);
}
return (1.0f - t) * o + t * d;
}
/// <summary>
/// Eases from an origin angle in radians to a destination angle using the
/// clockwise direction according to
/// a factor in [0.0, 1.0] .
/// </summary>
/// <param name="origin">origin angle</param>
/// <param name="dest">destination angle</param>
/// <param name="t">factor</param>
/// <param name="range">range</param>
/// <returns>angle</returns>
public static float LerpAngleCW(
in float origin,
in float dest,
in float t = 0.5f,
in float range = Utils.Tau)
{
float o = Utils.RemFloor(origin, range);
float d = Utils.RemFloor(dest, range);
float diff = d - o;
if (diff == 0.0f) { return d; }
if (o < d)
{
return Utils.RemFloor(
(1.0f - t) * (o + range) +
t * d,
range);
}
return (1.0f - t) * o + t * d;
}
/// <summary>
/// Finds the linear step between a left and right edge given an input
/// factor.
/// </summary>
/// <param name="edge0">left edge</param>
/// <param name="edge1">right edge</param>
/// <param name="x">factor</param>
/// <returns>linear step</returns>
public static float LinearStep(
in float edge0 = 0.0f,
in float edge1 = 1.0f,
in float x = 0.5f)
{
float denom = edge1 - edge0;
if (denom != 0.0f)
{
return Utils.Clamp((x - edge0) / denom, 0.0f, 1.0f);
}
return 0.0f;
}
/// <summary>
/// Finds the greater, or maximum, of two values.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>maximum value</returns>
public static int Max(in int a, in int b)
{
return (a > b) ? a : b;
}
/// <summary>
/// Finds the greatest, or maximum, among three values.
/// </summary>
/// <param name="a">first operand</param>
/// <param name="b">second operand</param>
/// <param name="c">third operand</param>
/// <returns>maximum value</returns>
public static int Max(in int a, in int b, in int c)
{
return Utils.Max(Utils.Max(a, b), c);
}
/// <summary>
/// Finds the greatest, or maximum, among three values.
/// </summary>
/// <param name="a">first operand</param>
/// <param name="b">second operand</param>
/// <param name="c">third operand</param>
/// <returns>maximum value</returns>
public static float Max(in float a, in float b, in float c)
{
return MathF.Max(MathF.Max(a, b), c);
}
/// <summary>
/// Finds the greatest, or maximum, among a list of values.
/// Returns zero if the list contains no values.
/// </summary>
/// <param name="values">list of values</param>
/// <returns>maximum value</returns>
public static float Max(params float[] values)
{
int len = values.Length;
if (len < 1) { return 0.0f; }
// Beware MinValue in C# differs from MIN_VALUE in Java.
// https://learn.microsoft.com/en-us/dotnet/api/system.single.minvalue
float result = float.MinValue;
for (int i = 0; i < len; ++i)
{
float v = values[i];
if (v > result) { result = v; }
}
return result;
}
/// <summary>
/// Finds the lesser, or minimum, of two values.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>minimum value</returns>
public static int Min(in int a, in int b)
{
return (a < b) ? a : b;
}
/// <summary>
/// Finds the lesser, or minimum, among three values.
/// </summary>
/// <param name="a">first operand</param>
/// <param name="b">second operand</param>
/// <param name="c">third operand</param>
/// <returns>maximum value</returns>
public static int Min(in int a, in int b, in int c)
{
return Utils.Min(Utils.Min(a, b), c);
}
/// <summary>
/// Finds the least, or minimum, among three values.
/// </summary>
/// <param name="a">first operand</param>
/// <param name="b">second operand</param>
/// <param name="c">third operand</param>
/// <returns>minimum value</returns>
public static float Min(in float a, in float b, in float c)
{
return MathF.Min(MathF.Min(a, b), c);
}
/// <summary>
/// Finds the least, or minimum, among a list of values.
/// Returns zero if the list contains no values.
/// </summary>
/// <param name="values">list of values</param>
/// <returns>minimum value</returns>
public static float Min(params float[] values)
{
int len = values.Length;
if (len < 1) { return 0.0f; }
float result = float.MaxValue;
for (int i = 0; i < len; ++i)
{
float v = values[i];
if (v < result) { result = v; }
}
return result;
}
/// <summary>
/// Mixes two values by a factor. The mix is unclamped by default.
/// </summary>
/// <param name="o">origin</param>
/// <param name="d">destination</param>
/// <param name="t">factor</param>
/// <returns>mix</returns>
public static float Mix(in float o, in float d, in float t = 0.5f)
{
return (1.0f - t) * o + t * d;
}
/// <summary>
/// Generates a random number with normal distribution. Based on the
/// Box-Muller transform as described here:
/// https://www.wikiwand.com/en/Box%E2%80%93Muller_transform . Could
/// generate a tuple of numbers, but only returns the x coordinate.
/// </summary>
/// <param name="rng">input value</param>
/// <param name="sigma">standard deviation</param>
/// <param name="mu">mean</param>
/// <returns>random number</returns>
public static float NextGaussian(
in System.Random rng,
in float sigma = 1.0f,
in float mu = 0.0f)
{
double u1;
do
{
u1 = rng.NextDouble();
} while (u1 <= Utils.Epsilon);
double u2 = rng.NextDouble();
double mag = sigma * Math.Sqrt(-2.0d * Math.Log(u1));
const double tau = 6.283185307179586d;
double x = mag * Math.Cos(tau * u2) + mu;
// double y = mag * Math.Sin(tau * u2) + mu;
return (float)x;
}
/// <summary>
/// Finds the next power of 2 for a signed integer, i.e., multiplies the
/// next power by the integer's sign. Returns zero if the input is zero.
/// </summary>
/// <param name="v">value</param>
/// <returns>next power of two</returns>
public static int NextPowerOf2(in int v)
{
if (v != 0)
{
int vSgn = 1;
int vAbs = v;
if (v < 0) { vAbs = -v; vSgn = -1; }
int p = 1;
while (p < vAbs) { p <<= 1; }
return p * vSgn;
}
return 0;
}
/// <summary>
/// Finds the negation of a float holding a boolean value.
/// </summary>
/// <param name="v">input value</param>
/// <returns>negation</returns>
public static int Not(in float v)
{
return (v != 0.0f) ? 0 : 1;
}
/// <summary>
/// Evaluates two floats like booleans, using the inclusive or (OR) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static int Or(in float a, in float b)
{
return ((a != 0.0f) | (b != 0.0f)) ? 1 : 0;
}
/// <summary>
/// Oscillates between [0.0, 1.0] based on an input step.
/// </summary>
/// <param name="t">factor</param>
/// <param name="pause">pause</param>
/// <returns>oscillation</returns>
public static float PingPong(in float t, in float pause = 1.0f)
{
return Utils.PingPong(0.0f, 1.0f, t, pause);
}
/// <summary>
/// Oscillates between a lower and upper bound based on an input step.
/// </summary>
/// <param name="a">lower bound</param>
/// <param name="b">upper bound</param>
/// <param name="t">factor</param>
/// <returns>oscillation</returns>
public static int PingPong(in int a, in int b, in float t)
{
return (int)Utils.PingPong((float)a, (float)b, t, 1.0f);
}
/// <summary>
/// Oscillates between a lower and upper bound based on an input step.
/// </summary>
/// <param name="a">lower bound</param>
/// <param name="b">upper bound</param>
/// <param name="t">factor</param>
/// <param name="pause">pause</param>
/// <returns>oscillation</returns>
public static float PingPong(
in float a,
in float b,
in float t,
in float pause = 1.0f)
{
float x = 0.5f + 0.5f * pause * MathF.Sin(MathF.PI * (t - 0.5f));
if (t <= 0.0f) { return a; }
if (t >= 1.0f) { return b; }
return (1.0f - x) * a + x * b;
}
/// <summary>
/// Quantizes a signed number according to a number of levels.
/// </summary>
/// <param name="v">input value</param>
/// <param name="levels">levels</param>
/// <returns>quantized value</returns>
public static float Quantize(in float v, in int levels)
{
return Utils.QuantizeSigned(v, levels);
}
/// <summary>
/// Quantizes a signed number according to a number of levels.
/// The quantization is centered about the range.
/// </summary>
/// <param name="v">input value</param>
/// <param name="levels">levels</param>
/// <returns>quantized value</returns>
public static float QuantizeSigned(in float v, in int levels)
{
if (levels > 0)
{
float lf = levels;
return MathF.Floor(0.5f + v * lf) / lf;
}
return v;
}
/// <summary>
/// Quantizes a positive number according to a number of levels.
/// The quantization is based on the left edge.
/// </summary>
/// <param name="v">input value</param>
/// <param name="levels">levels</param>
/// <returns>quantized value</returns>
public static float QuantizeUnsigned(in float v, in int levels)
{
if (levels > 1)
{
float lf = levels;
return MathF.Max(0.0f,
(MathF.Ceiling(v * lf) - 1.0f) / (lf - 1.0f));
}
return MathF.Max(0.0f, v);
}
/// <summary>
/// Maps an input value from an original range to a target range. If the
/// upper and lower bound of the original range are equal, will return the
/// value unchanged. Clamps the result to the lower and upper bounds of the
/// target range.
/// </summary>
/// <param name="v">input value</param>
/// <param name="lbOrig">lower bound of origin range</param>
/// <param name="ubOrig">upper bound of origin range</param>
/// <param name="lbDest">lower bound of destination range</param>
/// <param name="ubDest">upper bound of destination range</param>
/// <returns>mapped value</returns>
public static float Remap(
in float v,
in float lbOrig = -1.0f,
in float ubOrig = 1.0f,
in float lbDest = 0.0f,
in float ubDest = 1.0f)
{
float d = ubOrig - lbOrig;
if (d == 0.0f) { return v; }
float n = ( v - lbOrig ) / d;
if (n <= 0.0f) { return lbDest; }
if (n >= 1.0f) { return ubDest; }
return lbDest + n * (ubDest - lbDest);
}
/// <summary>
/// Maps an input value from an original range to a target range. If the
/// upper and lower bound of the original range are equal, will return the
/// value unchanged. Clamps the result to the lower and upper bounds of the
/// target range. If gamma is zero, returns the linear remap. Takes the
/// absolute of gamma.
/// </summary>
/// <param name="v">input value</param>
/// <param name="lbOrig">lower bound of origin range</param>
/// <param name="ubOrig">upper bound of origin range</param>
/// <param name="lbDest">lower bound of destination range</param>
/// <param name="ubDest">upper bound of destination range</param>
/// <param name="g">gamma</param>
/// <returns>mapped value</returns>
public static float Remap(
in float v,
in float lbOrig = -1.0f,
in float ubOrig = 1.0f,
in float lbDest = 0.0f,
in float ubDest = 1.0f,
in float g = 1.0f)
{
if (g == 0.0f)
{
return Utils.Remap(v, lbOrig, ubOrig, lbDest, ubDest);
}
double d = ubOrig - lbOrig;
if (d == 0.0d) { return v; }
double n = ( v - lbOrig ) / d;
if (n <= 0.0d) { return lbDest; }
if (n >= 1.0d) { return ubDest; }
return (float)(lbDest +
Math.Pow(n, 1.0d / Math.Abs(g)) * (ubDest - lbDest));
}
/// <summary>
/// Applies floor modulo to the operands. Returns the left operand when the
/// right operand is zero.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static int RemFloor(in int a, in int b)
{
return (b != 0) ? (a % b + b) % b : a;
}
/// <summary>
/// Applies floor modulo to the operands. Returns the left operand when the
/// right operand is zero.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static float RemFloor(in float a, in float b)
{
return (b != 0.0f) ? a - b * MathF.Floor(a / b) : a;
}
/// <summary>
/// Applies the modulo operator (%) to the operands.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static int RemTrunc(in int a, in int b)
{
return (b != 0) ? a % b : a;
}
/// <summary>
/// Applies the modulo operator (%) to the operands, implicitly using the
/// formula a - b trunc ( a / b ) .
///
/// When the left operand is negative and the right operand is positive, the
/// result will be negative. For periodic values, such as an angle, where
/// the direction of change could be either clockwise or counterclockwise,
/// use RemFloor.
///
/// If the right operand is one, use fract ( a ) or a - trunc ( a ) instead.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static float RemTrunc(in float a, in float b)
{