Skip to content

Commit

Permalink
More aggressive filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
RossNordby committed Nov 17, 2024
1 parent 57203ce commit 10d037e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions BepuPhysics/Collidables/ConvexHullHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ static void AddFace(ref QuickList<EarlyFace> faces, BufferPool pool, Vector3 nor
static void AddFaceEdgesToTestList(BufferPool pool,
ref QuickList<int> reducedFaceIndices,
ref QuickList<EdgeToTest> edgesToTest,
ref QuickSet<EdgeEndpoints, EdgeEndpoints> submittedEdgeTests,
ref QuickDictionary<EdgeEndpoints, int, EdgeEndpoints> edgeFaceCounts,
Vector3 faceNormal, int newFaceIndex)
{
var previousIndex = reducedFaceIndices[reducedFaceIndices.Count - 1];
Expand All @@ -507,14 +507,19 @@ static void AddFaceEdgesToTestList(BufferPool pool,
endpoints.A = previousIndex;
endpoints.B = reducedFaceIndices[i];
previousIndex = endpoints.B;
if (!submittedEdgeTests.Contains(endpoints))
if (!edgeFaceCounts.FindOrAllocateSlot(ref endpoints, pool, out var slotIndex))
{
EdgeToTest nextEdgeToTest;
nextEdgeToTest.Endpoints = endpoints;
nextEdgeToTest.FaceNormal = faceNormal;
nextEdgeToTest.FaceIndex = newFaceIndex;
edgesToTest.Allocate(pool) = nextEdgeToTest;
submittedEdgeTests.Add(endpoints, pool);
edgeFaceCounts.Values[slotIndex] = 1;
}
else
{
//No need to test this edge; it's already been submitted by a different face.
edgeFaceCounts.Values[slotIndex]++;
}
}
}
Expand Down Expand Up @@ -549,7 +554,7 @@ internal DebugStep(EdgeEndpoints sourceEdge, QuickList<int> rawVertexIndices, Ve
BasisY = basisY;
Raw = ((Span<int>)rawVertexIndices).ToArray();
Reduced = ((Span<int>)reducedVertexIndices).ToArray();
OverwrittenOriginal = null;
OverwrittenOriginal = null;
FaceIndex = faceIndex;
}

Expand All @@ -566,7 +571,7 @@ internal DebugStep FillHistory(Buffer<int> allowVertex, QuickList<EarlyFace> fac
FaceIndices.Add(face.VertexIndices[j]);
FaceNormals[i] = face.Normal;
}
AllowVertex = new bool[allowVertex.Length];
AllowVertex = new bool[allowVertex.Length];
for (int i = 0; i < allowVertex.Length; ++i)
{
AllowVertex[i] = allowVertex[i] != 0;
Expand Down Expand Up @@ -729,7 +734,7 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa

var faces = new QuickList<EarlyFace>(points.Length, pool);
var edgesToTest = new QuickList<EdgeToTest>(points.Length, pool);
var submittedEdgeTests = new QuickSet<EdgeEndpoints, EdgeEndpoints>(points.Length, pool);
var edgeFaceCounts = new QuickDictionary<EdgeEndpoints, int, EdgeEndpoints>(points.Length, pool);
if (reducedFaceIndices.Count >= 3)
{
//The initial face search found an actual face! That's a bit surprising since we didn't start from an edge offset, but rather an arbitrary direction.
Expand Down Expand Up @@ -768,6 +773,11 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
while (edgesToTest.Count > 0)
{
edgesToTest.Pop(out var edgeToTest);
if (edgeFaceCounts.TryGetValue(ref edgeToTest.Endpoints, out var edgeFaceCount) && edgeFaceCount >= 2)
{
//This edge is already part of two faces; no need to test it further.
continue;
}

ref var edgeA = ref points[edgeToTest.Endpoints.A];
ref var edgeB = ref points[edgeToTest.Endpoints.B];
Expand Down Expand Up @@ -827,7 +837,7 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
ReduceFace(ref rawFaceVertexIndices, faceNormal, points, planeEpsilonNarrow, ref facePoints, ref allowVertices, ref face.VertexIndices);
step.UpdateForFaceMerge(rawFaceVertexIndices, face.VertexIndices, allowVertices, i);
mergedFace = true;

// It's possible for the merged face to have invalidated a previous face that wouldn't necessarily be detected as something to merge.
break;
}
Expand All @@ -836,7 +846,7 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
{
var faceCountPriorToAdd = faces.Count;
AddFace(ref faces, pool, faceNormal, reducedFaceIndices);
AddFaceEdgesToTestList(pool, ref reducedFaceIndices, ref edgesToTest, ref submittedEdgeTests, faceNormal, faceCountPriorToAdd);
AddFaceEdgesToTestList(pool, ref reducedFaceIndices, ref edgesToTest, ref edgeFaceCounts, faceNormal, faceCountPriorToAdd);
}
step.FillHistory(allowVertices, faces);
steps.Add(step);
Expand Down
2 changes: 1 addition & 1 deletion Demos/SpecializedTests/ConvexHullTestDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ void DrawFace(DebugStep step, int[] reduced, bool deleted, int i)
{
var localStep = debugSteps[i];
DrawFace(localStep, localStep.Reduced, false, i);
if (localStep.OverwrittenOriginal != null)
if (showDeleted && localStep.OverwrittenOriginal != null)
{
DrawFace(localStep, localStep.OverwrittenOriginal, true, i);
}
Expand Down

0 comments on commit 10d037e

Please sign in to comment.