diff --git a/Runtime/Triangulator.cs b/Runtime/Triangulator.cs index a43cb2b..eab698a 100644 --- a/Runtime/Triangulator.cs +++ b/Runtime/Triangulator.cs @@ -3004,8 +3004,7 @@ private void SplitEdge(int he, NativeList heQueue, NativeList tQueue) if (halfedges[he] != -1) { - UnsafeInsertPointBulk(p, initTriangle: he / 3); - AdaptQueues(heQueue, tQueue); + UnsafeInsertPointBulk(p, initTriangle: he / 3, heQueue, tQueue); ProcessPathHalfedgesForEnqueueing(heQueue, tQueue, boundary: false); var h0 = triangles.Length - 3; @@ -3053,8 +3052,7 @@ private void SplitEdge(int he, NativeList heQueue, NativeList tQueue) } else { - UnsafeInsertPointBoundary(p, initHe: he); - AdaptQueues(heQueue, tQueue); + UnsafeInsertPointBoundary(p, initHe: he, heQueue, tQueue); ProcessPathHalfedgesForEnqueueing(heQueue, tQueue, boundary: true); //var h0 = triangles.Length - 3; @@ -3111,8 +3109,7 @@ private void SplitTriangle(int tId, NativeList heQueue, NativeList tQu if (edges.IsEmpty) { - UnsafeInsertPointBulk(c.Center, initTriangle: tId); - AdaptQueues(heQueue, tQueue); + UnsafeInsertPointBulk(c.Center, initTriangle: tId, heQueue, tQueue); ProcessPathHalfedgesForEnqueueing(heQueue, tQueue, boundary: false); } else @@ -3145,8 +3142,8 @@ private bool AngleIsTooSmall(int tId, T minimumAngle) return UnsafeTriangulator.AngleIsTooSmall(pA, pB, pC, minimumAngle); } - private void UnsafeInsertPointBulk(T2 p, int initTriangle) => new UnsafeBowerWatson(this).UnsafeInsertPointBulk(p, initTriangle); - private void UnsafeInsertPointBoundary(T2 p, int initHe) => new UnsafeBowerWatson(this).UnsafeInsertPointBoundary(p, initHe); + private void UnsafeInsertPointBulk(T2 p, int initTriangle, NativeList heQueue, NativeList tQueue) => new UnsafeBowerWatson(this).UnsafeInsertPointBulk(p, initTriangle, heQueue, tQueue); + private void UnsafeInsertPointBoundary(T2 p, int initHe, NativeList heQueue, NativeList tQueue) => new UnsafeBowerWatson(this).UnsafeInsertPointBoundary(p, initHe, heQueue, tQueue); public struct UnsafeBowerWatson { @@ -3197,20 +3194,20 @@ private int UnsafeInsertPointCommon(T2 p, int initTriangle) return pId; } - public void UnsafeInsertPointBulk(T2 p, int initTriangle) + public void UnsafeInsertPointBulk(T2 p, int initTriangle, NativeList heQueue = default, NativeList tQueue = default) { var pId = UnsafeInsertPointCommon(p, initTriangle); var initHe = FindInitPolygonHalfedge(); BuildPolygon(initHe, amphitheater: false); - ProcessBadTriangles(); + ProcessBadTriangles(heQueue, tQueue); BuildNewTrianglesForStar(pId); } - public void UnsafeInsertPointBoundary(T2 p, int initHe) + public void UnsafeInsertPointBoundary(T2 p, int initHe, NativeList heQueue = default, NativeList tQueue = default) { var pId = UnsafeInsertPointCommon(p, initHe / 3); BuildPolygon(initHe, amphitheater: true); - ProcessBadTriangles(); + ProcessBadTriangles(heQueue, tQueue); BuildNewTrianglesForAmphitheater(pId); } @@ -3295,7 +3292,7 @@ private int FindInitPolygonHalfedge() return -1; } - private void ProcessBadTriangles() + private void ProcessBadTriangles(NativeList heQueue, NativeList tQueue) { static void DisableHe(NativeList halfedges, int he, int rId) { @@ -3364,6 +3361,8 @@ static void AdaptHe(NativeList halfedges, int he, int rId, int wId) PathHalfedges[i] -= 3; } } + + AdaptQueues(wId, heQueue, tQueue); } } @@ -3502,26 +3501,22 @@ private void BuildNewTrianglesForAmphitheater(int pId) halfedges[heOffset] = -1; halfedges[heOffset + 3 * (PathPoints.Length - 2) + 2] = -1; } - } - private void AdaptQueues(NativeList heQueue, NativeList tQueue) - { - for (int t = badTriangles.Length - 1; t >= 0; t--) + private readonly void AdaptQueues(int wId, NativeList heQueue, NativeList tQueue) { - var tId = badTriangles[t]; - + // NOTE: we use write index `wId`, since queues will be changed during adaptation. if (heQueue.IsCreated) { for (int i = 0; i < heQueue.Length; i++) { var he = heQueue[i]; - if (he / 3 == tId) + if (he / 3 == wId) { heQueue[i] = -1; continue; } - if (he > 3 * tId + 2) + if (he > 3 * wId + 2) { heQueue[i] -= 3; } @@ -3533,13 +3528,13 @@ private void AdaptQueues(NativeList heQueue, NativeList tQueue) for (int i = 0; i < tQueue.Length; i++) { var q = tQueue[i]; - if (q == tId) + if (q == wId) { tQueue[i] = -1; continue; } - if (q > tId) + if (q > wId) { tQueue[i]--; }