-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBenchmarkCast.cs
More file actions
416 lines (332 loc) · 12.6 KB
/
BenchmarkCast.cs
File metadata and controls
416 lines (332 loc) · 12.6 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
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System.Collections.Generic;
using System.Numerics;
using Box2D.NET.Samples.Extensions;
using Box2D.NET.Samples.Primitives;
using ImGuiNET;
using static Box2D.NET.B2Geometries;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;
using static Box2D.NET.B2Worlds;
using static Box2D.NET.Shared.RandomSupports;
using static Box2D.NET.B2Timers;
namespace Box2D.NET.Samples.Samples.Benchmarks;
public class BenchmarkCast : Sample
{
private static readonly int SampleBenchmarkCast = SampleFactory.Shared.RegisterSample("Benchmark", "Cast", Create);
private QueryType m_queryType;
private List<B2Vec2> m_origins = new List<B2Vec2>();
private List<B2Vec2> m_translations = new List<B2Vec2>();
private float m_minTime;
private float m_buildTime;
private int m_rowCount, m_columnCount;
private int m_updateType;
private int m_drawIndex;
private float m_radius;
private float m_fill;
private float m_ratio;
private float m_grid;
private bool m_topDown;
private int sampleCount;
private int hitCount = 0;
private int nodeVisits = 0;
private int leafVisits = 0;
private float ms = 0.0f;
private static Sample Create(SampleAppContext ctx, Settings settings)
{
return new BenchmarkCast(ctx, settings);
}
public BenchmarkCast(SampleAppContext ctx, Settings settings) : base(ctx, settings)
{
if (settings.restart == false)
{
m_context.camera.m_center = new B2Vec2(500.0f, 500.0f);
m_context.camera.m_zoom = 25.0f * 21.0f;
// settings.drawShapes = m_context.g_sampleDebug;
}
m_queryType = QueryType.e_circleCast;
m_ratio = 5.0f;
m_grid = 1.0f;
m_fill = 0.1f;
m_rowCount = m_context.sampleDebug ? 100 : 1000;
m_columnCount = m_context.sampleDebug ? 100 : 1000;
m_minTime = 1e6f;
m_drawIndex = 0;
m_topDown = false;
m_buildTime = 0.0f;
m_radius = 0.1f;
g_seed = 1234;
sampleCount = m_context.sampleDebug ? 100 : 10000;
m_origins.Resize(sampleCount);
m_translations.Resize(sampleCount);
float extent = m_rowCount * m_grid;
// Pre-compute rays to avoid randomizer overhead
for (int i = 0; i < sampleCount; ++i)
{
B2Vec2 rayStart = RandomVec2(0.0f, extent);
B2Vec2 rayEnd = RandomVec2(0.0f, extent);
m_origins[i] = rayStart;
m_translations[i] = rayEnd - rayStart;
}
BuildScene();
}
void BuildScene()
{
g_seed = 1234;
b2DestroyWorld(m_worldId);
B2WorldDef worldDef = b2DefaultWorldDef();
m_worldId = b2CreateWorld(ref worldDef);
ulong ticks = b2GetTicks();
B2BodyDef bodyDef = b2DefaultBodyDef();
B2ShapeDef shapeDef = b2DefaultShapeDef();
float y = 0.0f;
for (int i = 0; i < m_rowCount; ++i)
{
float x = 0.0f;
for (int j = 0; j < m_columnCount; ++j)
{
float fillTest = RandomFloatRange(0.0f, 1.0f);
if (fillTest <= m_fill)
{
bodyDef.position = new B2Vec2(x, y);
B2BodyId bodyId = b2CreateBody(m_worldId, ref bodyDef);
float ratio = RandomFloatRange(1.0f, m_ratio);
float halfWidth = RandomFloatRange(0.05f, 0.25f);
B2Polygon box;
if (RandomFloat() > 0.0f)
{
box = b2MakeBox(ratio * halfWidth, halfWidth);
}
else
{
box = b2MakeBox(halfWidth, ratio * halfWidth);
}
int category = RandomIntRange(0, 2);
shapeDef.filter.categoryBits = (ulong)(1 << category);
if (category == 0)
{
shapeDef.material.customColor = (uint)B2HexColor.b2_colorBox2DBlue;
}
else if (category == 1)
{
shapeDef.material.customColor = (uint)B2HexColor.b2_colorBox2DYellow;
}
else
{
shapeDef.material.customColor = (uint)B2HexColor.b2_colorBox2DGreen;
}
b2CreatePolygonShape(bodyId, ref shapeDef, ref box);
}
x += m_grid;
}
y += m_grid;
}
if (m_topDown)
{
b2World_RebuildStaticTree(m_worldId);
}
m_buildTime = b2GetMilliseconds(ticks);
m_minTime = 1e6f;
}
static float CastCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
{
CastResult result = context as CastResult;
result.point = point;
result.fraction = fraction;
result.hit = true;
return fraction;
}
static bool OverlapCallback(B2ShapeId shapeId, object context)
{
OverlapResult result = context as OverlapResult;
if (result.count < 32)
{
B2AABB aabb = b2Shape_GetAABB(shapeId);
result.points[result.count] = b2AABB_Center(aabb);
result.count += 1;
}
return true;
}
public override void Step(Settings settings)
{
base.Step(settings);
B2QueryFilter filter = b2DefaultQueryFilter();
filter.maskBits = 1;
hitCount = 0;
nodeVisits = 0;
leafVisits = 0;
ms = 0.0f;
sampleCount = (int)m_origins.Count;
if (m_queryType == QueryType.e_rayCast)
{
ulong ticks = b2GetTicks();
B2RayResult drawResult = new B2RayResult();
for (int i = 0; i < sampleCount; ++i)
{
B2Vec2 origin = m_origins[i];
B2Vec2 translation = m_translations[i];
B2RayResult result = b2World_CastRayClosest(m_worldId, origin, translation, filter);
if (i == m_drawIndex)
{
drawResult = result;
}
nodeVisits += result.nodeVisits;
leafVisits += result.leafVisits;
hitCount += result.hit ? 1 : 0;
}
ms = b2GetMilliseconds(ticks);
m_minTime = b2MinFloat(m_minTime, ms);
B2Vec2 p1 = m_origins[m_drawIndex];
B2Vec2 p2 = p1 + m_translations[m_drawIndex];
m_context.draw.DrawSegment(p1, p2, B2HexColor.b2_colorWhite);
m_context.draw.DrawPoint(p1, 5.0f, B2HexColor.b2_colorGreen);
m_context.draw.DrawPoint(p2, 5.0f, B2HexColor.b2_colorRed);
if (drawResult.hit)
{
m_context.draw.DrawPoint(drawResult.point, 5.0f, B2HexColor.b2_colorWhite);
}
}
else if (m_queryType == QueryType.e_circleCast)
{
ulong ticks = b2GetTicks();
CastResult drawResult = new CastResult();
for (int i = 0; i < sampleCount; ++i)
{
B2Circle circle = new B2Circle(m_origins[i], m_radius);
B2Vec2 translation = m_translations[i];
CastResult result = new CastResult();
B2TreeStats traversalResult = b2World_CastCircle(m_worldId, ref circle, translation, filter, CastCallback, result);
if (i == m_drawIndex)
{
drawResult = result;
}
nodeVisits += traversalResult.nodeVisits;
leafVisits += traversalResult.leafVisits;
hitCount += result.hit ? 1 : 0;
}
ms = b2GetMilliseconds(ticks);
m_minTime = b2MinFloat(m_minTime, ms);
B2Vec2 p1 = m_origins[m_drawIndex];
B2Vec2 p2 = p1 + m_translations[m_drawIndex];
m_context.draw.DrawSegment(p1, p2, B2HexColor.b2_colorWhite);
m_context.draw.DrawPoint(p1, 5.0f, B2HexColor.b2_colorGreen);
m_context.draw.DrawPoint(p2, 5.0f, B2HexColor.b2_colorRed);
if (drawResult.hit)
{
B2Vec2 t = b2Lerp(p1, p2, drawResult.fraction);
m_context.draw.DrawCircle(t, m_radius, B2HexColor.b2_colorWhite);
m_context.draw.DrawPoint(drawResult.point, 5.0f, B2HexColor.b2_colorWhite);
}
}
else if (m_queryType == QueryType.e_overlap)
{
ulong ticks = b2GetTicks();
OverlapResult drawResult = new OverlapResult();
B2Vec2 extent = new B2Vec2(m_radius, m_radius);
OverlapResult result = new OverlapResult();
for (int i = 0; i < sampleCount; ++i)
{
B2Vec2 origin = m_origins[i];
B2AABB aabb = new B2AABB(origin - extent, origin + extent);
result.count = 0;
B2TreeStats traversalResult = b2World_OverlapAABB(m_worldId, aabb, filter, OverlapCallback, result);
if (i == m_drawIndex)
{
drawResult = result;
}
nodeVisits += traversalResult.nodeVisits;
leafVisits += traversalResult.leafVisits;
hitCount += result.count;
}
ms = b2GetMilliseconds(ticks);
m_minTime = b2MinFloat(m_minTime, ms);
{
B2Vec2 origin = m_origins[m_drawIndex];
B2AABB aabb = new B2AABB(origin - extent, origin + extent);
m_context.draw.DrawAABB(aabb, B2HexColor.b2_colorWhite);
}
for (int i = 0; i < drawResult.count; ++i)
{
m_context.draw.DrawPoint(drawResult.points[i], 5.0f, B2HexColor.b2_colorHotPink);
}
}
}
public override void UpdateGui()
{
base.UpdateGui();
float height = 240.0f;
ImGui.SetNextWindowPos(new Vector2(10.0f, m_context.camera.m_height - height - 50.0f), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(200.0f, height));
ImGui.Begin("Cast", ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoResize);
ImGui.PushItemWidth(100.0f);
bool changed = false;
string[] queryTypes = { "Ray", "Circle", "Overlap" };
int queryType = (int)m_queryType;
if (ImGui.Combo("Query", ref queryType, queryTypes, queryTypes.Length))
{
m_queryType = (QueryType)queryType;
if (m_queryType == QueryType.e_overlap)
{
m_radius = 5.0f;
}
else
{
m_radius = 0.1f;
}
changed = true;
}
if (ImGui.SliderInt("rows", ref m_rowCount, 0, 1000, "%d"))
{
changed = true;
}
if (ImGui.SliderInt("columns", ref m_columnCount, 0, 1000, "%d"))
{
changed = true;
}
if (ImGui.SliderFloat("fill", ref m_fill, 0.0f, 1.0f, "%.2f"))
{
changed = true;
}
if (ImGui.SliderFloat("grid", ref m_grid, 0.5f, 2.0f, "%.2f"))
{
changed = true;
}
if (ImGui.SliderFloat("ratio", ref m_ratio, 1.0f, 10.0f, "%.2f"))
{
changed = true;
}
if (ImGui.Checkbox("top down", ref m_topDown))
{
changed = true;
}
if (ImGui.Button("Draw Next"))
{
m_drawIndex = (m_drawIndex + 1) % m_origins.Count;
}
ImGui.PopItemWidth();
ImGui.End();
if (changed)
{
BuildScene();
}
}
public override void Draw(Settings settings)
{
base.Draw(settings);
m_context.draw.DrawString(5, m_textLine, $"build time ms = {m_buildTime:g}");
m_textLine += m_textIncrement;
m_context.draw.DrawString(5, m_textLine, $"hit count = {hitCount}, node visits = {nodeVisits}, leaf visits = {leafVisits}");
m_textLine += m_textIncrement;
m_context.draw.DrawString(5, m_textLine, $"total ms = {ms:F3}");
m_textLine += m_textIncrement;
m_context.draw.DrawString(5, m_textLine, $"min total ms = {m_minTime:F3}");
m_textLine += m_textIncrement;
float aveRayCost = 1000.0f * m_minTime / (float)sampleCount;
m_context.draw.DrawString(5, m_textLine, $"average us = {aveRayCost:F2}");
m_textLine += m_textIncrement;
}
}