Skip to content
Merged
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
19 changes: 13 additions & 6 deletions BepuPhysics/Collidables/ConvexHullHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static int FindNextIndexForFaceHull(Vector2 start, Vector2 previousEdgeDirection

}

static void ReduceFace(ref QuickList<int> faceVertexIndices, Vector3 faceNormal, Span<Vector3> points, float planeEpsilon, ref QuickList<Vector2> facePoints, ref Buffer<int> allowVertex, ref QuickList<int> reducedIndices)
static void ReduceFace(ref QuickList<int> faceVertexIndices, Vector3 faceNormal, Span<Vector3> points, float planeEpsilon, ref QuickList<Vector2> facePoints, ref Buffer<int> allowVertex, ref QuickList<int> reducedIndices, ref IndexSet reducedIndicesSet)
{
Debug.Assert(facePoints.Count == 0 && reducedIndices.Count == 0 && facePoints.Span.Length >= faceVertexIndices.Count && reducedIndices.Span.Length >= faceVertexIndices.Count);
for (int i = faceVertexIndices.Count - 1; i >= 0; --i)
Expand Down Expand Up @@ -394,14 +394,16 @@ static void ReduceFace(ref QuickList<int> faceVertexIndices, Vector3 faceNormal,
var greatestDistance = (float)Math.Sqrt(greatestDistanceSquared);
var initialOffsetDirection = (facePoints[initialIndex] - centroid) / greatestDistance;
var previousEdgeDirection = new Vector2(initialOffsetDirection.Y, -initialOffsetDirection.X);
reducedIndicesSet.Clear();
reducedIndicesSet.AddUnsafely(faceVertexIndices[initialIndex]);
reducedIndices.AllocateUnsafely() = faceVertexIndices[initialIndex];

var previousEndIndex = initialIndex;
for (int i = 0; i < facePoints.Count; ++i)
{
var nextIndex = FindNextIndexForFaceHull(facePoints[previousEndIndex], previousEdgeDirection, planeEpsilon, ref facePoints);
//This can return -1 in the event of a completely degenerate face.
if (nextIndex == -1 || reducedIndices.Contains(faceVertexIndices[nextIndex]))
if (nextIndex == -1 || reducedIndicesSet.Contains(faceVertexIndices[nextIndex]))
{
if (nextIndex >= 0)
{
Expand All @@ -413,6 +415,8 @@ static void ReduceFace(ref QuickList<int> faceVertexIndices, Vector3 faceNormal,
Debug.Assert(cycleStartIndex >= 0);
if (cycleStartIndex > 0)
{
for (int j = 0; j < cycleStartIndex; ++j)
reducedIndicesSet.Remove(reducedIndices[j]);
//Note that order matters; can't do a last element swapping remove.
reducedIndices.Span.CopyTo(cycleStartIndex, reducedIndices.Span, 0, reducedIndices.Count - cycleStartIndex);
reducedIndices.Count -= cycleStartIndex;
Expand All @@ -421,6 +425,7 @@ static void ReduceFace(ref QuickList<int> faceVertexIndices, Vector3 faceNormal,
break;
}
reducedIndices.AllocateUnsafely() = faceVertexIndices[nextIndex];
reducedIndicesSet.AddUnsafely(faceVertexIndices[nextIndex]);
previousEdgeDirection = Vector2.Normalize(facePoints[nextIndex] - facePoints[previousEndIndex]);
previousEndIndex = nextIndex;
}
Expand All @@ -429,7 +434,7 @@ static void ReduceFace(ref QuickList<int> faceVertexIndices, Vector3 faceNormal,
for (int i = 0; i < faceVertexIndices.Count; ++i)
{
var index = faceVertexIndices[i];
if (!reducedIndices.Contains(index))
if (!reducedIndicesSet.Contains(index))
{
allowVertex[index] = 0;
}
Expand Down Expand Up @@ -738,9 +743,10 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
Debug.Assert(rawFaceVertexIndices.Count >= 2);
var facePoints = new QuickList<Vector2>(points.Length, pool);
var reducedFaceIndices = new QuickList<int>(points.Length, pool);
var reducedIndicesSet = new IndexSet(pool, points.Length);


ReduceFace(ref rawFaceVertexIndices, initialFaceNormal, points, planeSlabEpsilonNarrow, ref facePoints, ref allowVertices, ref reducedFaceIndices);
ReduceFace(ref rawFaceVertexIndices, initialFaceNormal, points, planeSlabEpsilonNarrow, ref facePoints, ref allowVertices, ref reducedFaceIndices, ref reducedIndicesSet);

var faces = new QuickList<EarlyFace>(points.Length, pool);
var edgesToTest = new QuickList<EdgeToTest>(points.Length, pool);
Expand Down Expand Up @@ -806,7 +812,7 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
FindExtremeFace(basisXBundle, basisYBundle, basisOrigin, edgeToTest.Endpoints, ref pointBundles, indexOffsetBundle, allowVertices, points.Length, ref projectedOnX, ref projectedOnY, planeSlabEpsilon, ref rawFaceVertexIndices, out var faceNormal);
reducedFaceIndices.Count = 0;
facePoints.Count = 0;
ReduceFace(ref rawFaceVertexIndices, faceNormal, points, planeSlabEpsilonNarrow, ref facePoints, ref allowVertices, ref reducedFaceIndices);
ReduceFace(ref rawFaceVertexIndices, faceNormal, points, planeSlabEpsilonNarrow, ref facePoints, ref allowVertices, ref reducedFaceIndices, ref reducedIndicesSet);

if (reducedFaceIndices.Count < 3)
{
Expand Down Expand Up @@ -852,7 +858,7 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
face.VertexIndices.Count = 0;
facePoints.Count = 0;
face.VertexIndices.EnsureCapacity(rawFaceVertexIndices.Count, pool);
ReduceFace(ref rawFaceVertexIndices, faceNormal, points, planeSlabEpsilonNarrow, ref facePoints, ref allowVertices, ref face.VertexIndices);
ReduceFace(ref rawFaceVertexIndices, faceNormal, points, planeSlabEpsilonNarrow, ref facePoints, ref allowVertices, ref face.VertexIndices, ref reducedIndicesSet);
#if DEBUG_STEPS
step.UpdateForFaceMerge(rawFaceVertexIndices, face.VertexIndices, allowVertices, i);
#endif
Expand Down Expand Up @@ -933,6 +939,7 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
edgesToTest.Dispose(pool);
facePoints.Dispose(pool);
reducedFaceIndices.Dispose(pool);
reducedIndicesSet.Dispose(pool);
rawFaceVertexIndices.Dispose(pool);
pool.Return(ref allowVertices);
pool.Return(ref projectedOnX);
Expand Down
Loading