Make PhysicsHull a ref struct (#5297)

* Make PhysicsHull a ref struct

First time I've used it but seemed like a good candidate considering it's temporary.

* weh

---------

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
metalgearsloth
2024-07-16 01:43:33 +10:00
committed by GitHub
parent 033699d7d6
commit 2664061993
3 changed files with 29 additions and 22 deletions

View File

@@ -36,6 +36,7 @@ END TEMPLATE-->
### Breaking changes
* Fixes large entities causing entity spawn menu to break.
* Made PhysicsHull an internal ref struct for some PolygonShape speedup.
### New features

View File

@@ -1,5 +1,6 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
@@ -8,11 +9,17 @@ namespace Robust.Shared.Physics;
/// <summary>
/// Convex hull used for poly collision.
/// </summary>
public record struct PhysicsHull()
internal ref struct PhysicsHull()
{
public readonly Vector2[] Points = new Vector2[PhysicsConstants.MaxPolygonVertices];
public Span<Vector2> Points;
public int Count;
public PhysicsHull(Span<Vector2> vertices, int count) : this()
{
Count = count;
Points = vertices[..count];
}
private static PhysicsHull RecurseHull(Vector2 p1, Vector2 p2, Span<Vector2> ps, int count)
{
PhysicsHull hull = new();
@@ -58,6 +65,7 @@ public record struct PhysicsHull()
return hull;
}
hull.Points = new Vector2[PhysicsConstants.MaxPolygonVertices];
var bestPoint = ps[bestIndex];
// compute hull to the right of p1-bestPoint
@@ -93,7 +101,8 @@ public record struct PhysicsHull()
PhysicsHull hull = new();
if (count is < 3 or > PhysicsConstants.MaxPolygonVertices)
{
{
hull.Count = 0;
DebugTools.Assert(false);
// check your data
return hull;
@@ -206,11 +215,14 @@ public record struct PhysicsHull()
var hull2 = RecurseHull(p2, p1, leftPoints, leftCount);
if (hull1.Count == 0 && hull2.Count == 0)
{
{
hull.Count = 0;
// all points collinear
return hull;
}
hull.Points = new Vector2[PhysicsConstants.MaxPolygonVertices];
// stitch hulls together, preserving CCW winding order
hull.Points[hull.Count++] = p1;

View File

@@ -23,6 +23,7 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
@@ -61,18 +62,12 @@ namespace Robust.Shared.Physics.Collision.Shapes
/// <summary>
/// The radius of this polygon.
/// </summary>
[DataField("radius"), Access(typeof(SharedPhysicsSystem), Friend = AccessPermissions.ReadWriteExecute, Other = AccessPermissions.Read)]
[DataField, Access(typeof(SharedPhysicsSystem), Friend = AccessPermissions.ReadWriteExecute, Other = AccessPermissions.Read)]
public float Radius { get; set; } = PhysicsConstants.PolygonRadius;
public bool Set(List<Vector2> vertices)
{
Span<Vector2> verts = stackalloc Vector2[vertices.Count];
for (var i = 0; i < vertices.Count; i++)
{
verts[i] = vertices[i];
}
var verts = CollectionsMarshal.AsSpan(vertices);
return Set(verts, vertices.Count);
}
@@ -91,7 +86,7 @@ namespace Robust.Shared.Physics.Collision.Shapes
return true;
}
public void Set(PhysicsHull hull)
internal void Set(PhysicsHull hull)
{
DebugTools.Assert(hull.Count >= 3);
var vertexCount = hull.Count;
@@ -188,17 +183,16 @@ namespace Robust.Shared.Physics.Collision.Shapes
Set(Vertices.AsSpan(), VertexCount);
}
public bool Set(Box2Rotated bounds)
public void Set(Box2Rotated bounds)
{
Span<Vector2> vertices = stackalloc Vector2[]
{
bounds.BottomLeft,
bounds.BottomRight,
bounds.TopRight,
bounds.TopLeft,
};
Span<Vector2> verts = stackalloc Vector2[4];
verts[0] = bounds.BottomLeft;
verts[1] = bounds.BottomRight;
verts[2] = bounds.TopRight;
verts[3] = bounds.TopLeft;
return Set(vertices, 4);
var hull = new PhysicsHull(verts, 4);
Set(hull);
}
public void SetAsBox(Box2 box)