-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFiniteAxisIntersectionBenchmarks.cs
More file actions
463 lines (426 loc) · 15.4 KB
/
Copy pathFiniteAxisIntersectionBenchmarks.cs
File metadata and controls
463 lines (426 loc) · 15.4 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
using System;
using BenchmarkDotNet.Attributes;
using FixedMathSharp.Geometry;
namespace FixedMathSharp.Benchmarks;
[MemoryDiagnoser]
public class FiniteAxisIntersectionBenchmarks
{
private FixedSegment2d _query2D;
private FixedBoundCircle _circle;
private FixedSegment2d _capsuleAxis2D;
private FixedSegment _query3D;
private FixedBoundSphere _sphere;
private FixedSegment _capsuleAxis3D;
private FixedSegment _cylinderAxis;
private FixedSegment _expandedCylinderQuery;
private Vector2d _capsuleCenter2D;
private Vector2d _capsuleDirection2D;
private Vector3d _capsuleCenter3D;
private Vector3d _capsuleDirection3D;
private Vector2d _distancePoint2D;
private Vector2d _distanceDirection2D;
private Vector3d _distancePoint3D;
private Vector3d _distanceDirection3D;
private Vector2d _wideCapsuleCenter2D;
private Vector2d _wideCapsulePoint2D;
private Vector2d _wideCapsuleDirection2D;
private Fixed64 _wideAxisLength;
private Fixed64 _wideRadius;
private Fixed64 _wideExpansion;
private Vector3d _cylinderCenter;
private Vector3d _cylinderDirection;
private FixedSegment _arbitraryRawQuery;
private FixedSegment _arbitraryRawAxis;
private FixedRay2d _boundedRay2D;
private FixedRay _boundedRay3D;
private Fixed64 _boundedRayMaximum;
private Fixed64 _pointParameter;
private Fixed64 _radius;
private Fixed64 _axisLength;
private Fixed64 _radialExpansion;
private Fixed64 _axialExpansion;
private Fixed64 _arbitraryRawRadius;
private FixedSegment _roundedBoxQuery;
private FixedBoundBox _roundedBox;
private Fixed64 _roundedBoxExpansion;
private Fixed64 _roundedBoxLength;
private FixedQuaternion _rigidRotation;
[Params(1, 100_000)]
public int Scale { get; set; }
[GlobalSetup]
public void Setup()
{
Fixed64 scale = (Fixed64)Scale;
Fixed64 doubleScale = scale * 2;
_radius = scale * Fixed64.Half;
_axisLength = doubleScale;
_radialExpansion = scale * Fixed64.Quarter;
_axialExpansion = scale * Fixed64.Half;
_query2D = new FixedSegment2d(
new Vector2d(-doubleScale, Fixed64.Zero),
new Vector2d(doubleScale, Fixed64.Zero));
_capsuleAxis2D = new FixedSegment2d(
new Vector2d(Fixed64.Zero, -scale),
new Vector2d(Fixed64.Zero, scale));
_capsuleCenter2D = Vector2d.Zero;
_capsuleDirection2D = Vector2d.Forward;
_circle = new FixedBoundCircle(_capsuleCenter2D, _radius);
_query3D = new FixedSegment(
new Vector3d(-doubleScale, Fixed64.Zero, Fixed64.Zero),
new Vector3d(doubleScale, Fixed64.Zero, Fixed64.Zero));
_capsuleAxis3D = new FixedSegment(
new Vector3d(Fixed64.Zero, -scale, Fixed64.Zero),
new Vector3d(Fixed64.Zero, scale, Fixed64.Zero));
_capsuleCenter3D = Vector3d.Zero;
_capsuleDirection3D = Vector3d.Up;
_sphere = new FixedBoundSphere(_capsuleCenter3D, _radius);
_distancePoint2D = new Vector2d(
scale * Fixed64.FromFraction(3, 2),
-scale * Fixed64.Half);
_distanceDirection2D = new Vector2d(Fixed64.One, Fixed64.Two).Normalized;
_distancePoint3D = new Vector3d(_distancePoint2D.X, _distancePoint2D.Y, Fixed64.Zero);
_distanceDirection3D = new Vector3d(
_distanceDirection2D.X,
_distanceDirection2D.Y,
Fixed64.Zero);
_wideCapsuleCenter2D = new Vector2d(Fixed64.MaxValue - (Fixed64)5 * scale, Fixed64.Zero);
_wideCapsulePoint2D = new Vector2d(Fixed64.MaxValue - scale, (Fixed64)8 * scale);
_wideCapsuleDirection2D = new Vector2d(Fixed64.One, Fixed64.One).Normalized;
_wideAxisLength = (Fixed64)20 * scale;
_wideRadius = (Fixed64)2 * scale;
_wideExpansion = scale;
_cylinderAxis = _capsuleAxis3D;
_cylinderCenter = Vector3d.Zero;
_cylinderDirection = Vector3d.Up;
_expandedCylinderQuery = new FixedSegment(
new Vector3d(-doubleScale, scale + _radialExpansion, Fixed64.Zero),
new Vector3d(doubleScale, scale + _radialExpansion, Fixed64.Zero));
Fixed64 oneRaw = Fixed64.FromRaw(1);
_arbitraryRawRadius = scale * Fixed64.Half;
_arbitraryRawQuery = new FixedSegment(
new Vector3d(-doubleScale + oneRaw, Fixed64.Zero, Fixed64.Zero),
new Vector3d(doubleScale, Fixed64.Zero, Fixed64.Zero));
_arbitraryRawAxis = new FixedSegment(
new Vector3d(Fixed64.Zero, -scale, -scale),
new Vector3d(Fixed64.Zero, scale, scale + oneRaw));
_boundedRay2D = new FixedRay2d(_query2D.Start, Vector2d.Right);
_boundedRay3D = new FixedRay(_query3D.Start, Vector3d.Right);
_boundedRayMaximum = (Fixed64)4 * scale;
_pointParameter = scale;
_roundedBoxQuery = new FixedSegment(
new Vector3d(doubleScale, scale * Fixed64.FromFraction(4, 5), Fixed64.Zero),
new Vector3d(Fixed64.Zero, scale * Fixed64.FromFraction(4, 5), Fixed64.Zero));
_roundedBox = FixedBoundBox.FromCenterAndSize(Vector3d.Zero, Vector3d.One * scale);
_roundedBoxExpansion = scale * Fixed64.Half;
_roundedBoxLength = doubleScale;
_rigidRotation = FixedQuaternion.FromEulerAnglesInDegrees(
(Fixed64)17,
(Fixed64)29,
(Fixed64)11);
if (!Circle2DDistanceInterval()
|| !Sphere3DDistanceInterval()
|| !Capsule2DIntersectionInterval()
|| !Capsule3DIntersectionInterval()
|| !CenteredCapsule2DIntersectionInterval()
|| !CenteredCapsule3DIntersectionInterval()
|| CenteredCapsule2DDistance() == Fixed64.MaxValue
|| CenteredCapsule3DDistance() == Fixed64.MaxValue
|| CenteredCapsule2DWideDistance() == Fixed64.MaxValue
|| !CenteredCapsule2DContainsWidePoint()
|| CenteredCapsule2DNormalAndSurface() == Vector2d.Zero
|| !FiniteCylinderIntersectionInterval()
|| !CenteredExpandedCylinderIntersectionInterval()
|| !ArbitraryRawCylinderIntersectionInterval()
|| !BoundedRayCapsule2DIntersectionInterval()
|| !BoundedRayCapsule3DIntersectionInterval()
|| !BoundedRayCenteredCapsule2DIntersectionInterval()
|| !BoundedRayCenteredCapsule3DIntersectionInterval()
|| !BoundedRayFiniteCylinderIntersectionInterval()
|| !CenteredCapsule2DDistanceInterval()
|| !CenteredCapsule3DDistanceInterval()
|| !CardinalRigidCapsule3DDistanceInterval()
|| !RotatedRigidCapsule3DDistanceInterval()
|| !RigidCylinder3DDistanceInterval()
|| !RigidCone3DDistanceInterval()
|| !FiniteCylinderDistanceInterval()
|| !SweptSphereBoxFirstDistance())
{
throw new InvalidOperationException("Finite-axis benchmark scenarios must intersect their targets.");
}
}
[Benchmark]
public bool Circle2DDistanceInterval() =>
_query2D.TryGetCircleIntersectionDistanceInterval(
_circle,
_boundedRayMaximum,
out _,
out _);
[Benchmark]
public bool Sphere3DDistanceInterval() =>
_query3D.TryGetSphereIntersectionDistanceInterval(
_sphere,
_boundedRayMaximum,
out _,
out _);
[Benchmark]
public bool Capsule2DIntersectionInterval() =>
_query2D.TryGetCapsuleIntersectionInterval(
_capsuleAxis2D,
_radius,
Fixed64.Zero,
out _,
out _);
[Benchmark]
public bool Capsule3DIntersectionInterval() =>
_query3D.TryGetCapsuleIntersectionInterval(
_capsuleAxis3D,
_radius,
Fixed64.Zero,
out _,
out _);
[Benchmark]
public bool CenteredCapsule2DIntersectionInterval() =>
_query2D.TryGetCapsuleIntersectionInterval(
_capsuleCenter2D,
_capsuleDirection2D,
_axisLength,
_radius,
Fixed64.Zero,
out _,
out _);
[Benchmark]
public bool CenteredCapsule3DIntersectionInterval() =>
_query3D.TryGetCapsuleIntersectionInterval(
_capsuleCenter3D,
_capsuleDirection3D,
_axisLength,
_radius,
Fixed64.Zero,
out _,
out _);
[Benchmark]
public Fixed64 CenteredCapsule2DDistance() =>
FixedSegment2d.GetDistanceToCenteredCapsule(
_distancePoint2D,
_capsuleCenter2D,
_distanceDirection2D,
_axisLength,
_radius);
[Benchmark]
public Fixed64 CenteredCapsule3DDistance() =>
FixedSegment.GetDistanceToCenteredCapsule(
_distancePoint3D,
_capsuleCenter3D,
_distanceDirection3D,
_axisLength,
_radius);
[Benchmark]
public Fixed64 CenteredCapsule2DWideDistance() =>
FixedSegment2d.GetDistanceToCenteredCapsule(
_wideCapsulePoint2D,
_wideCapsuleCenter2D,
_wideCapsuleDirection2D,
_wideAxisLength,
_wideRadius);
[Benchmark]
public bool CenteredCapsule2DContainsWidePoint() =>
FixedSegment2d.ContainsPointInCenteredCapsule(
_wideCapsulePoint2D,
_wideCapsuleCenter2D,
_wideCapsuleDirection2D,
_wideAxisLength,
_wideRadius,
_wideExpansion);
[Benchmark]
public Vector2d CenteredCapsule2DNormalAndSurface()
{
Vector2d direction = FixedSegment2d.GetDirectionFromCenteredAxis(
_wideCapsulePoint2D,
_wideCapsuleCenter2D,
_wideCapsuleDirection2D,
_wideAxisLength);
return FixedSegment2d.TryGetSurfacePointOnCenteredCapsule(
_wideCapsulePoint2D,
_wideCapsuleCenter2D,
_wideCapsuleDirection2D,
_wideAxisLength,
_wideRadius,
direction,
out Vector2d surfacePoint)
? surfacePoint
: Vector2d.Zero;
}
[Benchmark]
public bool FiniteCylinderIntersectionInterval() =>
_query3D.TryGetFiniteCylinderIntersectionInterval(
_cylinderAxis,
_radius,
Fixed64.Zero,
out _,
out _);
[Benchmark]
public bool CenteredExpandedCylinderIntersectionInterval() =>
_expandedCylinderQuery.TryGetFiniteCylinderIntersectionInterval(
_cylinderCenter,
_cylinderDirection,
_axisLength,
_radius,
_radialExpansion,
_axialExpansion,
out _,
out _);
[Benchmark]
public bool ArbitraryRawCylinderIntersectionInterval() =>
_arbitraryRawQuery.TryGetFiniteCylinderIntersectionInterval(
_arbitraryRawAxis,
_arbitraryRawRadius,
Fixed64.Zero,
out _,
out _);
[Benchmark]
public bool BoundedRayCapsule2DIntersectionInterval() =>
_boundedRay2D.TryGetCapsuleIntersectionInterval(
_capsuleAxis2D,
_radius,
_boundedRayMaximum,
out _,
out _);
[Benchmark]
public bool BoundedRayCapsule3DIntersectionInterval() =>
_boundedRay3D.TryGetCapsuleIntersectionInterval(
_capsuleAxis3D,
_radius,
_boundedRayMaximum,
out _,
out _);
[Benchmark]
public bool BoundedRayCenteredCapsule2DIntersectionInterval() =>
_boundedRay2D.TryGetCapsuleIntersectionInterval(
_capsuleCenter2D,
_capsuleDirection2D,
_axisLength,
_radius,
_boundedRayMaximum,
out _,
out _);
[Benchmark]
public bool BoundedRayCenteredCapsule3DIntersectionInterval() =>
_boundedRay3D.TryGetCapsuleIntersectionInterval(
_capsuleCenter3D,
_capsuleDirection3D,
_axisLength,
_radius,
_boundedRayMaximum,
out _,
out _);
[Benchmark]
public bool BoundedRayFiniteCylinderIntersectionInterval() =>
_boundedRay3D.TryGetFiniteCylinderIntersectionInterval(
_cylinderAxis,
_radius,
_boundedRayMaximum,
out _,
out _);
[Benchmark]
public Vector3d BoundedRayGetPoint() =>
_boundedRay3D.GetPoint(_pointParameter);
[Benchmark]
public bool CenteredCapsule2DDistanceInterval() =>
_query2D.TryGetCapsuleIntersectionDistanceInterval(
_capsuleCenter2D,
_capsuleDirection2D,
_axisLength,
_radius,
Fixed64.Zero,
_boundedRayMaximum,
out _,
out _,
out _,
out _);
[Benchmark]
public bool CenteredCapsule3DDistanceInterval() =>
_query3D.TryGetCapsuleIntersectionDistanceInterval(
_capsuleCenter3D,
_capsuleDirection3D,
_axisLength,
_radius,
Fixed64.Zero,
_boundedRayMaximum,
out _,
out _,
out _,
out _);
[Benchmark]
public bool CardinalRigidCapsule3DDistanceInterval() =>
_query3D.TryGetCapsuleIntersectionDistanceInterval(
_capsuleCenter3D,
FixedQuaternion.Identity,
_axisLength,
_radius,
Fixed64.Zero,
_boundedRayMaximum,
out _,
out _,
out _,
out _);
[Benchmark]
public bool RotatedRigidCapsule3DDistanceInterval() =>
_query3D.TryGetCapsuleIntersectionDistanceInterval(
_capsuleCenter3D,
_rigidRotation,
_axisLength,
_radius,
Fixed64.Zero,
_boundedRayMaximum,
out _,
out _,
out _,
out _);
[Benchmark]
public bool RigidCylinder3DDistanceInterval() =>
_query3D.TryGetFiniteCylinderIntersectionDistanceInterval(
_cylinderCenter,
_rigidRotation,
_axisLength,
_radius,
Fixed64.Zero,
Fixed64.Zero,
_boundedRayMaximum,
out _,
out _,
out _,
out _);
[Benchmark]
public bool RigidCone3DDistanceInterval() =>
_query3D.TryGetCenteredFiniteConeIntersectionDistanceInterval(
_cylinderCenter,
_rigidRotation,
_axisLength,
_radius,
_boundedRayMaximum,
out _,
out _,
out _,
out _);
[Benchmark]
public bool FiniteCylinderDistanceInterval() =>
_query3D.TryGetFiniteCylinderIntersectionDistanceInterval(
_cylinderAxis,
_radius,
Fixed64.Zero,
_boundedRayMaximum,
out _,
out _,
out _,
out _);
[Benchmark]
public bool SweptSphereBoxFirstDistance() =>
_roundedBoxQuery.TryGetSweptSphereBoxIntersectionDistance(
_roundedBox,
_roundedBoxExpansion,
_roundedBoxLength,
out _);
[Benchmark]
public Vector3d SegmentGetPointAtDistance() =>
_query3D.GetPointAtDistance(_pointParameter, _boundedRayMaximum);
}