Skip to content

Commit

Permalink
Moving toward a wee bit of consistency in testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
RossNordby committed Jan 19, 2025
1 parent 32da509 commit 98a0037
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 278 deletions.
31 changes: 22 additions & 9 deletions BepuPhysics/Collidables/ConvexHullHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BepuPhysics.Constraints.Contact;
#define DEBUG_STEPS
using BepuUtilities;
using BepuUtilities.Collections;
using BepuUtilities.Memory;
Expand All @@ -9,6 +9,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


namespace BepuPhysics.Collidables
{
/// <summary>
Expand Down Expand Up @@ -522,12 +523,7 @@ static void AddFaceEdgesToTestList(BufferPool pool,
}
}

static void AddIfNotPresent(ref QuickList<int> list, int value, BufferPool pool)
{
if (!list.Contains(value))
list.Allocate(pool) = value;
}

#if DEBUG_STEPS
public struct DebugStep
{
public EdgeEndpoints SourceEdge;
Expand Down Expand Up @@ -614,15 +610,19 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
{
ComputeHull(points, pool, out hullData, out _);
}

#endif

/// <summary>
/// Computes the convex hull of a set of points.
/// </summary>
/// <param name="points">Point set to compute the convex hull of.</param>
/// <param name="pool">Buffer pool to pull memory from when creating the hull.</param>
/// <param name="hullData">Convex hull of the input point set.</param>
#if DEBUG_STEPS
public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullData hullData, out List<DebugStep> steps)
#else
public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullData hullData)
#endif
{
steps = new List<DebugStep>();
if (points.Length <= 0)
Expand Down Expand Up @@ -777,9 +777,11 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
if (Vector3.Dot(basisX, edgeToAdd.FaceNormal) > 0)
Helpers.Swap(ref edgeToAdd.Endpoints.A, ref edgeToAdd.Endpoints.B);
}
#if DEBUG_STEPS
Vector3Wide.ReadFirst(initialBasisX, out var debugInitialBasisX);
Vector3Wide.ReadFirst(initialBasisY, out var debugInitialBasisY);
steps.Add(new DebugStep(initialSourceEdge, rawFaceVertexIndices, initialFaceNormal, debugInitialBasisX, debugInitialBasisY, reducedFaceIndices, reducedFaceIndices.Count >= 3 ? 0 : -1).FillHistory(allowVertices, faces));
#endif

while (edgesToTest.Count > 0)
{
Expand Down Expand Up @@ -811,13 +813,17 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa

if (reducedFaceIndices.Count < 3)
{
#if DEBUG_STEPS
steps.Add(new DebugStep(edgeToTest.Endpoints, rawFaceVertexIndices, faceNormal, basisX, basisY, reducedFaceIndices, -1).FillHistory(allowVertices, faces));
#endif
//Degenerate face found; don't bother creating work for it.
continue;
}
// Brute force scan all the faces to see if the new face is coplanar with any of them.
#if DEBUG_STEPS
var step = new DebugStep(edgeToTest.Endpoints, rawFaceVertexIndices, faceNormal, basisX, basisY, reducedFaceIndices, faces.Count);
Console.WriteLine($"step count: {steps.Count}");
#endif
bool mergedFace = false;
for (int i = 0; i < faces.Count; ++i)
{
Expand All @@ -841,12 +847,16 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
}
}
// Rerun reduction for the merged face.
#if DEBUG_STEPS
step.RecordOverwrittenFace(face.VertexIndices);
#endif
face.VertexIndices.Count = 0;
facePoints.Count = 0;
face.VertexIndices.EnsureCapacity(rawFaceVertexIndices.Count, pool);
ReduceFace(ref rawFaceVertexIndices, faceNormal, points, planeEpsilonNarrow, ref facePoints, ref allowVertices, ref face.VertexIndices);
#if DEBUG_STEPS
step.UpdateForFaceMerge(rawFaceVertexIndices, face.VertexIndices, allowVertices, i);
#endif
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.
Expand Down Expand Up @@ -876,8 +886,10 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
}
if (deletedFace)
{
#if DEBUG_STEPS
Console.WriteLine($"Deleting face {i}");
step.RecordDeletedFace(face.VertexIndices);
#endif
// Edges may have been exposed by the deletion of the face.
// Adjust the edge-face counts.
for (int j = 0; j < face.VertexIndices.Count; ++j)
Expand Down Expand Up @@ -911,11 +923,12 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
}
}
faces.Count -= deletedFaceCount;
#if DEBUG_STEPS
step.FillHistory(allowVertices, faces);
steps.Add(step);

if (steps.Count > 500)
break;
#endif
}

edgesToTest.Dispose(pool);
Expand Down
Loading

0 comments on commit 98a0037

Please sign in to comment.