Skip to content
This repository was archived by the owner on Jun 28, 2026. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion Robust.Benchmarks/NumericsHelpers/Box2RotatedBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Numerics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using BenchmarkDotNet.Attributes;
using Robust.Shared.Analyzers;
using Robust.Shared.Maths;
Expand All @@ -16,3 +18,43 @@ public Matrix3x2 GetTransform()
return Box.Transform;
}
}

[Virtual, DisassemblyDiagnoser]
public class Box2RotatedBoundingBoxBenchmark
{
public Box2Rotated ZeroRotationBox = new(new Box2(-1, -2, 3, 4), Angle.Zero, new Vector2(0.25f, -0.5f));
public Box2Rotated RotatedBox = new(new Box2(-1, -2, 3, 4), 0.7f, new Vector2(0.25f, -0.5f));

private Box2 _boxResult;

[Benchmark(Baseline = true)]
public void CurrentZeroRotation()
{
_boxResult = ZeroRotationBox.CalcBoundingBox();
}

[Benchmark]
public void NoCheckZeroRotation()
{
_boxResult = CalcBoundingBoxNoRotationCheck(ZeroRotationBox);
}

[Benchmark]
public void CurrentRotated()
{
_boxResult = RotatedBox.CalcBoundingBox();
}

[Benchmark]
public void NoCheckRotated()
{
_boxResult = CalcBoundingBoxNoRotationCheck(RotatedBox);
}

private static Box2 CalcBoundingBoxNoRotationCheck(in Box2Rotated box)
{
box.GetVertices(out var x, out var y);
var aabb = SimdHelpers.GetAABB(x, y);
return Unsafe.As<Vector128<float>, Box2>(ref aabb);
}
}
4 changes: 4 additions & 0 deletions Robust.Shared.Maths/Box2Rotated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ public readonly Box2Rotated Enlarged(float value)
/// <summary>
/// Calculates the smallest AABB that will encompass the rotated box. The AABB is in local space.
/// </summary>
[Pure]
public readonly Box2 CalcBoundingBox()
{
if (Rotation == Angle.Zero)
return Box;

GetVertices(out var x, out var y);
var aabb = SimdHelpers.GetAABB(x, y);
return Unsafe.As<Vector128<float>, Box2>(ref aabb);
Expand Down
Loading