-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBoundsBenchmarks.cs
More file actions
1014 lines (847 loc) · 29.1 KB
/
Copy pathBoundsBenchmarks.cs
File metadata and controls
1014 lines (847 loc) · 29.1 KB
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 BenchmarkDotNet.Attributes;
using FixedMathSharp.Bounds;
using System;
using System.Collections.Generic;
namespace FixedMathSharp.Benchmarks;
/// <summary>
/// Normalizes loop-style bounds benchmarks so BenchmarkDotNet reports one
/// fixture operation instead of the whole sampled batch.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
internal sealed class SampledBenchmarkAttribute : BenchmarkAttribute
{
public SampledBenchmarkAttribute()
: this(BenchmarkFixtures.SampleCount)
{
}
public SampledBenchmarkAttribute(int operationsPerInvoke)
{
OperationsPerInvoke = operationsPerInvoke;
}
}
[MemoryDiagnoser]
public class BoundsBenchmarks
{
private readonly FixedBoundArea[] _areas = CreateAreas();
private readonly FixedBoundBox[] _boxes = CreateBoxes();
private readonly FixedBoundCircle[] _circles = CreateCircles();
private readonly FixedSegment2d[] _segments2d = CreateSegments2d();
private readonly FixedSegment[] _segments3d = CreateSegments3d();
private readonly FixedTriangle2d[] _triangles2d = CreateTriangles2d();
private readonly FixedTriangle[] _triangles3d = CreateTriangles3d();
private readonly Vector3d[] _boxCornerBuffer = new Vector3d[FixedBoundBox.CornerCount];
private readonly Vector3d[] _cornerBuffer = new Vector3d[FixedBoundFrustum.CornerCount];
private readonly FixedBoundFrustum[] _frustums = CreateFrustums();
private readonly Fixed4x4[] _matrices = BenchmarkFixtures.PerspectiveMatrices;
private readonly FixedPlane[] _planeBuffer = new FixedPlane[FixedBoundFrustum.PlaneCount];
private readonly FixedPlane[] _planes = CreatePlanes();
private readonly Vector3d[] _points = BenchmarkFixtures.VectorsA;
private readonly Vector2d[] _points2d = BenchmarkFixtures.Vector2sA;
private readonly FixedRay2d[] _rays2d = CreateRays2d();
private readonly Vector3d[] _spherePointCloud = CreateSpherePointCloud();
private readonly FixedRay[] _rays = CreateRays();
private readonly FixedBoundSphere[] _spheres = CreateSpheres();
[SampledBenchmark]
public Fixed64 AreaConstructCenterSize()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _points2d.Length; i++)
{
Vector2d size = new(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8));
var area = FixedBoundArea.FromCenterAndSize(_points2d[i] * Fixed64.Quarter, size);
accumulator += area.Min.X + area.Max.Y;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 AreaConstructMinMax()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _points2d.Length; i++)
{
Vector2d min = _points2d[i] * Fixed64.Quarter;
Vector2d max = min + new Vector2d(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8));
var area = FixedBoundArea.FromMinMax(max, min);
accumulator += area.Min.X + area.Max.Y;
}
return accumulator;
}
[SampledBenchmark]
public int AreaContainsPoint()
{
int count = 0;
for (int i = 0; i < _areas.Length; i++)
{
if (_areas[i].Contains(_points2d[i]))
count++;
}
return count;
}
[SampledBenchmark]
public int AreaIntersectsArea()
{
int count = 0;
for (int i = 0; i < _areas.Length; i++)
{
if (_areas[i].Intersects(_areas[(i + 1) & (BenchmarkFixtures.SampleCount - 1)]))
count++;
}
return count;
}
[SampledBenchmark]
public int AreaIntersectsAreaStrict()
{
int count = 0;
for (int i = 0; i < _areas.Length; i++)
{
if (_areas[i].IntersectsStrict(_areas[(i + 1) & (BenchmarkFixtures.SampleCount - 1)]))
count++;
}
return count;
}
[SampledBenchmark]
public Vector2d AreaClampPoint()
{
Vector2d accumulator = Vector2d.Zero;
for (int i = 0; i < _areas.Length; i++)
accumulator += _areas[i].ClampPoint(_points2d[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark(BenchmarkFixtures.SampleCount - 1)]
public FixedBoundArea AreaUnion()
{
FixedBoundArea accumulator = _areas[0];
for (int i = 1; i < _areas.Length; i++)
accumulator = FixedBoundArea.Union(accumulator, _areas[i]);
return accumulator;
}
[SampledBenchmark]
public int CircleContainsPoint()
{
int count = 0;
for (int i = 0; i < _circles.Length; i++)
{
if (_circles[i].Contains(_points2d[i]))
count++;
}
return count;
}
[SampledBenchmark]
public int CircleIntersectsCircle()
{
int count = 0;
for (int i = 0; i < _circles.Length; i++)
{
if (_circles[i].Intersects(_circles[(i + 1) & (BenchmarkFixtures.SampleCount - 1)]))
count++;
}
return count;
}
[SampledBenchmark]
public int CircleIntersectsArea()
{
int count = 0;
for (int i = 0; i < _circles.Length; i++)
{
if (_circles[i].Intersects(_areas[i]))
count++;
}
return count;
}
[SampledBenchmark]
public Vector2d CircleClampPoint()
{
Vector2d accumulator = Vector2d.Zero;
for (int i = 0; i < _circles.Length; i++)
accumulator += _circles[i].ClampPoint(_points2d[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Vector2d CircleProjectPoint()
{
Vector2d accumulator = Vector2d.Zero;
for (int i = 0; i < _circles.Length; i++)
accumulator += _circles[i].ProjectPoint(_points2d[(i + 53) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Vector2d Segment2dClosestPoint()
{
Vector2d accumulator = Vector2d.Zero;
for (int i = 0; i < _segments2d.Length; i++)
accumulator += _segments2d[i].ClosestPoint(_points2d[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Fixed64 Segment2dDistanceSquared()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _segments2d.Length; i++)
accumulator += _segments2d[i].DistanceSquared(_points2d[(i + 53) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Vector3d Segment3dClosestPoint()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _segments3d.Length; i++)
accumulator += _segments3d[i].ClosestPoint(_points[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Fixed64 Segment3dDistanceSquared()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _segments3d.Length; i++)
accumulator += _segments3d[i].DistanceSquared(_points[(i + 53) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Fixed64 Triangle2dArea()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _triangles2d.Length; i++)
accumulator += _triangles2d[i].Area;
return accumulator;
}
[SampledBenchmark(BenchmarkFixtures.SampleCount - 1)]
public FixedBoundArea Triangle2dBounds()
{
FixedBoundArea accumulator = _triangles2d[0].Bounds;
for (int i = 1; i < _triangles2d.Length; i++)
accumulator = FixedBoundArea.Union(accumulator, _triangles2d[i].Bounds);
return accumulator;
}
[SampledBenchmark]
public int Triangle2dContainsPoint()
{
int count = 0;
for (int i = 0; i < _triangles2d.Length; i++)
{
if (_triangles2d[i].Contains(_points2d[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]))
count++;
}
return count;
}
[SampledBenchmark]
public Vector2d Triangle2dClosestPoint()
{
Vector2d accumulator = Vector2d.Zero;
for (int i = 0; i < _triangles2d.Length; i++)
accumulator += _triangles2d[i].ClosestPoint(_points2d[(i + 53) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Vector2d Triangle2dGetPoint()
{
Vector2d accumulator = Vector2d.Zero;
for (int i = 0; i < _triangles2d.Length; i++)
accumulator += _triangles2d[i].GetPoint(Fixed64.Quarter, Fixed64.Half);
return accumulator;
}
[SampledBenchmark]
public Fixed64 Triangle2dBarycentricWeights()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _triangles2d.Length; i++)
{
if (_triangles2d[i].TryGetBarycentricWeights(_triangles2d[i].Centroid, out Fixed64 weightA, out Fixed64 weightB, out Fixed64 weightC))
accumulator += weightA + weightB + weightC;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 Triangle3dArea()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _triangles3d.Length; i++)
accumulator += _triangles3d[i].Area;
return accumulator;
}
[SampledBenchmark(BenchmarkFixtures.SampleCount - 1)]
public FixedBoundBox Triangle3dBounds()
{
FixedBoundBox accumulator = _triangles3d[0].Bounds;
for (int i = 1; i < _triangles3d.Length; i++)
accumulator = FixedBoundBox.Union(accumulator, _triangles3d[i].Bounds);
return accumulator;
}
[SampledBenchmark]
public int Triangle3dContainsPoint()
{
int count = 0;
for (int i = 0; i < _triangles3d.Length; i++)
{
if (_triangles3d[i].Contains(_points[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]))
count++;
}
return count;
}
[SampledBenchmark]
public Vector3d Triangle3dClosestPoint()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _triangles3d.Length; i++)
accumulator += _triangles3d[i].ClosestPoint(_points[(i + 53) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Vector3d Triangle3dGetPoint()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _triangles3d.Length; i++)
accumulator += _triangles3d[i].GetPoint(Fixed64.Quarter, Fixed64.Half);
return accumulator;
}
[SampledBenchmark]
public Fixed64 Triangle3dProjectedBarycentricWeights()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _triangles3d.Length; i++)
{
if (_triangles3d[i].TryGetProjectedBarycentricWeights(_triangles3d[i].Centroid, out Fixed64 weightA, out Fixed64 weightB, out Fixed64 weightC))
accumulator += weightA + weightB + weightC;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 Ray2dIntersectsArea()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _rays2d.Length; i++)
{
Fixed64? hit = _rays2d[i].Intersects(_areas[i]);
if (hit.HasValue)
accumulator += hit.Value;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 Ray2dIntersectsCircle()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _rays2d.Length; i++)
{
Fixed64? hit = _rays2d[i].Intersects(_circles[i]);
if (hit.HasValue)
accumulator += hit.Value;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 BoxConstructCenterSize()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _points.Length; i++)
{
Vector3d size = new(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8),
Fixed64.One + Fixed64.FromFraction(i % 3, 8));
var box = FixedBoundBox.FromCenterAndSize(_points[i] * Fixed64.Quarter, size);
accumulator += box.Min.X + box.Max.Z;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 BoxConstructMinMax()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _points.Length; i++)
{
Vector3d min = _points[i] * Fixed64.Quarter;
Vector3d max = min + new Vector3d(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8),
Fixed64.One + Fixed64.FromFraction(i % 3, 8));
var box = FixedBoundBox.FromMinMax(max, min);
accumulator += box.Min.X + box.Max.Z;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 BoxSetMinMax()
{
Fixed64 accumulator = Fixed64.Zero;
var box = _boxes[0];
for (int i = 0; i < _boxes.Length; i++)
{
box.SetMinMax(_boxes[i].Min, _boxes[i].Max);
accumulator += box.Center.X;
}
return accumulator;
}
[SampledBenchmark]
public int BoxContainsPoint()
{
int count = 0;
for (int i = 0; i < _boxes.Length; i++)
{
if (_boxes[i].Contains(_points[i]))
count++;
}
return count;
}
[SampledBenchmark]
public int BoxIntersectsBox()
{
int count = 0;
for (int i = 0; i < _boxes.Length; i++)
{
if (_boxes[i].Intersects(_boxes[(i + 1) & (BenchmarkFixtures.SampleCount - 1)]))
count++;
}
return count;
}
[SampledBenchmark]
public int BoxIntersectsSphere()
{
int count = 0;
for (int i = 0; i < _boxes.Length; i++)
{
if (_boxes[i].Intersects(_spheres[i]))
count++;
}
return count;
}
[SampledBenchmark]
public Vector3d BoxClampPoint()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _boxes.Length; i++)
accumulator += _boxes[i].ClampPoint(_points[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public Vector3d BoxGetCorner()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _boxes.Length; i++)
accumulator += _boxes[i].GetCorner(i & (FixedBoundBox.CornerCount - 1));
return accumulator;
}
[SampledBenchmark]
public Vector3d BoxCopyCorners()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _boxes.Length; i++)
{
_boxes[i].CopyCorners(_boxCornerBuffer);
accumulator += _boxCornerBuffer[i & (FixedBoundBox.CornerCount - 1)];
}
return accumulator;
}
[SampledBenchmark(BenchmarkFixtures.SampleCount - 1)]
public FixedBoundBox BoxUnion()
{
FixedBoundBox accumulator = _boxes[0];
for (int i = 1; i < _boxes.Length; i++)
accumulator = FixedBoundBox.Union(accumulator, _boxes[i]);
return accumulator;
}
[SampledBenchmark]
public int SphereContainsPoint()
{
int count = 0;
for (int i = 0; i < _spheres.Length; i++)
{
if (_spheres[i].Contains(_points[i]))
count++;
}
return count;
}
[SampledBenchmark]
public int SphereIntersectsSphere()
{
int count = 0;
for (int i = 0; i < _spheres.Length; i++)
{
if (_spheres[i].Intersects(_spheres[(i + 1) & (BenchmarkFixtures.SampleCount - 1)]))
count++;
}
return count;
}
[SampledBenchmark]
public int SphereIntersectsBox()
{
int count = 0;
for (int i = 0; i < _spheres.Length; i++)
{
if (_spheres[i].Intersects(_boxes[i]))
count++;
}
return count;
}
[SampledBenchmark]
public Vector3d SphereClampPoint()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _spheres.Length; i++)
accumulator += _spheres[i].ClampPoint(_points[(i + 53) & (BenchmarkFixtures.SampleCount - 1)]);
return accumulator;
}
[SampledBenchmark]
public FixedBoundSphere SphereCreateFromBoundingBox()
{
FixedBoundSphere accumulator = _spheres[0];
for (int i = 0; i < _boxes.Length; i++)
accumulator = FixedBoundSphere.CreateMerged(accumulator, FixedBoundSphere.CreateFromBoundingBox(_boxes[i]));
return accumulator;
}
[Benchmark]
public FixedBoundSphere SphereCreateFromPointsArray()
{
return FixedBoundSphere.CreateFromPoints(_spherePointCloud);
}
[Benchmark]
public FixedBoundSphere SphereCreateFromPointsSpan()
{
return FixedBoundSphere.CreateFromPoints(_spherePointCloud.AsSpan());
}
[Benchmark]
public FixedBoundSphere SphereCreateFromPointsEnumerable()
{
return FixedBoundSphere.CreateFromPoints(EnumerateSpherePointCloud());
}
[SampledBenchmark]
public FixedBoundSphere SphereTransform()
{
FixedBoundSphere accumulator = _spheres[0];
for (int i = 0; i < _spheres.Length; i++)
accumulator = FixedBoundSphere.CreateMerged(accumulator, _spheres[i].Transform(BenchmarkFixtures.Matrices[i]));
return accumulator;
}
[SampledBenchmark]
public int FrustumCreateFromMatrix()
{
int count = 0;
for (int i = 0; i < _matrices.Length; i++)
{
var frustum = new FixedBoundFrustum(_matrices[i]);
if (frustum.Contains(_points[i]) != FixedEnclosureType.Disjoint)
count++;
}
return count;
}
[SampledBenchmark]
public long FrustumConstructOnly()
{
long accumulator = 0;
for (int i = 0; i < _matrices.Length; i++)
{
var frustum = new FixedBoundFrustum(_matrices[i]);
accumulator += frustum.Min.X.m_rawValue;
}
return accumulator;
}
[SampledBenchmark]
public long FrustumSetMatrix()
{
long accumulator = 0;
var frustum = _frustums[0];
for (int i = 0; i < _matrices.Length; i++)
{
frustum.Matrix = _matrices[i];
accumulator += frustum.Min.X.m_rawValue;
}
return accumulator;
}
[SampledBenchmark]
public int FrustumContainsPoint()
{
int count = 0;
for (int i = 0; i < _frustums.Length; i++)
{
if (_frustums[i].Contains(_points[i]) != FixedEnclosureType.Disjoint)
count++;
}
return count;
}
[SampledBenchmark]
public int FrustumIntersectsBox()
{
int count = 0;
for (int i = 0; i < _frustums.Length; i++)
{
if (_frustums[i].Intersects(_boxes[i]))
count++;
}
return count;
}
[SampledBenchmark]
public int FrustumIntersectsSphere()
{
int count = 0;
for (int i = 0; i < _frustums.Length; i++)
{
if (_frustums[i].Intersects(_spheres[i]))
count++;
}
return count;
}
[SampledBenchmark]
public Fixed64 FrustumIntersectsRay()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _frustums.Length; i++)
{
Fixed64? hit = _frustums[i].Intersects(_rays[i]);
if (hit.HasValue)
accumulator += hit.Value;
}
return accumulator;
}
[SampledBenchmark]
public Vector3d FrustumGetCornersIntoArray()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _frustums.Length; i++)
{
_frustums[i].GetCorners(_cornerBuffer);
accumulator += _cornerBuffer[i & (FixedBoundFrustum.CornerCount - 1)];
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 FrustumGetPlanesIntoArray()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _frustums.Length; i++)
{
_frustums[i].GetPlanes(_planeBuffer);
accumulator += _planeBuffer[i % FixedBoundFrustum.PlaneCount].D;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 RayIntersectsBox()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _rays.Length; i++)
{
Fixed64? hit = _rays[i].Intersects(_boxes[i]);
if (hit.HasValue)
accumulator += hit.Value;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 RayIntersectsSphere()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _rays.Length; i++)
{
Fixed64? hit = _rays[i].Intersects(_spheres[i]);
if (hit.HasValue)
accumulator += hit.Value;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 RayIntersectsPlane()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _rays.Length; i++)
{
Fixed64? hit = _rays[i].Intersects(_planes[i]);
if (hit.HasValue)
accumulator += hit.Value;
}
return accumulator;
}
[SampledBenchmark]
public Fixed64 PlaneDotCoordinate()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _planes.Length; i++)
accumulator += _planes[i].DotCoordinate(_points[i]);
return accumulator;
}
[SampledBenchmark]
public int PlaneIntersectsBox()
{
int count = 0;
for (int i = 0; i < _planes.Length; i++)
count += (int)_planes[i].Intersects(_boxes[i]);
return count;
}
[SampledBenchmark]
public int PlaneIntersectsSphere()
{
int count = 0;
for (int i = 0; i < _planes.Length; i++)
count += (int)_planes[i].Intersects(_spheres[i]);
return count;
}
private static FixedBoundArea[] CreateAreas()
{
var areas = new FixedBoundArea[BenchmarkFixtures.SampleCount];
for (int i = 0; i < areas.Length; i++)
{
Vector2d center = BenchmarkFixtures.Vector2sA[i] * Fixed64.Quarter;
Vector2d half = new(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8));
areas[i] = FixedBoundArea.FromMinMax(center - half, center + half);
}
return areas;
}
private static FixedBoundBox[] CreateBoxes()
{
var boxes = new FixedBoundBox[BenchmarkFixtures.SampleCount];
for (int i = 0; i < boxes.Length; i++)
{
Vector3d center = BenchmarkFixtures.VectorsA[i] * Fixed64.Quarter;
Vector3d half = new(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8),
Fixed64.One + Fixed64.FromFraction(i % 3, 8));
boxes[i] = FixedBoundBox.FromMinMax(center - half, center + half);
}
return boxes;
}
private static FixedBoundCircle[] CreateCircles()
{
var circles = new FixedBoundCircle[BenchmarkFixtures.SampleCount];
for (int i = 0; i < circles.Length; i++)
{
Vector2d center = BenchmarkFixtures.Vector2sB[i] * Fixed64.Quarter;
Fixed64 radius = Fixed64.One + Fixed64.FromFraction((i % 9) + 1, 4);
circles[i] = new FixedBoundCircle(center, radius);
}
return circles;
}
private static FixedSegment2d[] CreateSegments2d()
{
var segments = new FixedSegment2d[BenchmarkFixtures.SampleCount];
for (int i = 0; i < segments.Length; i++)
{
Vector2d start = BenchmarkFixtures.Vector2sA[i] * Fixed64.Quarter;
Vector2d delta = new(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8));
if ((i & 1) != 0)
delta.X = -delta.X;
segments[i] = new FixedSegment2d(start, start + delta);
}
return segments;
}
private static FixedSegment[] CreateSegments3d()
{
var segments = new FixedSegment[BenchmarkFixtures.SampleCount];
for (int i = 0; i < segments.Length; i++)
{
Vector3d start = BenchmarkFixtures.VectorsA[i] * Fixed64.Quarter;
Vector3d delta = new(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.One + Fixed64.FromFraction(i % 7, 8),
Fixed64.One + Fixed64.FromFraction(i % 3, 8));
if ((i & 1) != 0)
delta.X = -delta.X;
if ((i & 2) != 0)
delta.Z = -delta.Z;
segments[i] = new FixedSegment(start, start + delta);
}
return segments;
}
private static FixedTriangle2d[] CreateTriangles2d()
{
var triangles = new FixedTriangle2d[BenchmarkFixtures.SampleCount];
for (int i = 0; i < triangles.Length; i++)
{
Vector2d a = BenchmarkFixtures.Vector2sA[i] * Fixed64.Quarter;
Vector2d b = a + new Vector2d(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.FromFraction(i % 3, 16));
Vector2d c = a + new Vector2d(
Fixed64.FromFraction(i % 7, 16),
Fixed64.One + Fixed64.FromFraction(i % 7, 8));
triangles[i] = (i & 1) == 0
? new FixedTriangle2d(a, b, c)
: new FixedTriangle2d(a, c, b);
}
return triangles;
}
private static FixedTriangle[] CreateTriangles3d()
{
var triangles = new FixedTriangle[BenchmarkFixtures.SampleCount];
for (int i = 0; i < triangles.Length; i++)
{
Vector3d a = BenchmarkFixtures.VectorsA[i] * Fixed64.Quarter;
Vector3d b = a + new Vector3d(
Fixed64.One + Fixed64.FromFraction(i % 5, 8),
Fixed64.FromFraction(i % 3, 16),
Fixed64.FromFraction(i % 5, 16));
Vector3d c = a + new Vector3d(
Fixed64.FromFraction(i % 7, 16),
Fixed64.One + Fixed64.FromFraction(i % 7, 8),
Fixed64.One + Fixed64.FromFraction(i % 3, 8));
triangles[i] = (i & 1) == 0
? new FixedTriangle(a, b, c)
: new FixedTriangle(a, c, b);
}
return triangles;
}
private static FixedRay2d[] CreateRays2d()
{
var rays = new FixedRay2d[BenchmarkFixtures.SampleCount];
for (int i = 0; i < rays.Length; i++)
{
Vector2d direction = BenchmarkFixtures.Vector2sA[(i + 37) & (BenchmarkFixtures.SampleCount - 1)].Normalized;
if (direction.EqualsZero())
direction = Vector2d.Right;
rays[i] = new FixedRay2d(BenchmarkFixtures.Vector2sB[i], direction);
}
return rays;
}
private static FixedBoundFrustum[] CreateFrustums()
{
var frustums = new FixedBoundFrustum[BenchmarkFixtures.SampleCount];
for (int i = 0; i < frustums.Length; i++)
frustums[i] = new FixedBoundFrustum(BenchmarkFixtures.PerspectiveMatrices[i]);
return frustums;
}
private static FixedPlane[] CreatePlanes()
{
var planes = new FixedPlane[BenchmarkFixtures.SampleCount];
for (int i = 0; i < planes.Length; i++)
planes[i] = new FixedPlane(BenchmarkFixtures.NormalizedAxes[i], Fixed64.FromFraction((i % 7) - 3, 2));
return planes;
}
private static FixedRay[] CreateRays()
{
var rays = new FixedRay[BenchmarkFixtures.SampleCount];
for (int i = 0; i < rays.Length; i++)
rays[i] = new FixedRay(BenchmarkFixtures.VectorsB[i], BenchmarkFixtures.NormalizedAxes[(i + 37) & (BenchmarkFixtures.SampleCount - 1)]);
return rays;
}
private static FixedBoundSphere[] CreateSpheres()
{
var spheres = new FixedBoundSphere[BenchmarkFixtures.SampleCount];
for (int i = 0; i < spheres.Length; i++)
{
Vector3d center = BenchmarkFixtures.VectorsB[i] * Fixed64.Quarter;
Fixed64 radius = Fixed64.One + Fixed64.FromFraction((i % 9) + 1, 4);
spheres[i] = new FixedBoundSphere(center, radius);
}
return spheres;
}
private static Vector3d[] CreateSpherePointCloud()
{
var points = new Vector3d[BenchmarkFixtures.SampleCount];