Skip to content

Commit

Permalink
refactor: extract halfedges list
Browse files Browse the repository at this point in the history
Move the allocation of `halfedges` from the job to `Triangulator`.
This change is required to adapt other algorithms for the `halfedges` approach.
  • Loading branch information
andywiecko committed Sep 26, 2023
1 parent 1697593 commit a029bb2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Runtime/Triangulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public class OutputData
private NativeList<float2> outputPositions;
private NativeList<int> outputTriangles;
private NativeList<Triangle> triangles;
private NativeList<int> halfedges;
private NativeList<Circle> circles;
private NativeList<Edge3> trianglesToEdges;
private NativeHashMap<Edge, FixedList32Bytes<int>> edgesToTriangles;
Expand All @@ -245,6 +246,7 @@ public Triangulator(int capacity, Allocator allocator)
outputPositions = new(capacity, allocator);
outputTriangles = new(6 * capacity, allocator);
triangles = new(capacity, allocator);
halfedges = new(6 * capacity, allocator);
circles = new(capacity, allocator);
trianglesToEdges = new(capacity, allocator);
edgesToTriangles = new(capacity, allocator);
Expand All @@ -269,6 +271,7 @@ public void Dispose()
outputPositions.Dispose();
outputTriangles.Dispose();
triangles.Dispose();
halfedges.Dispose();
circles.Dispose();
trianglesToEdges.Dispose();
edgesToTriangles.Dispose();
Expand Down Expand Up @@ -743,6 +746,7 @@ private struct ClearDataJob : IJob
private NativeList<float2> outputPositions;
private NativeList<int> outputTriangles;
private NativeList<Triangle> triangles;
private NativeList<int> halfedges;
private NativeList<Circle> circles;
private NativeList<Edge3> trianglesToEdges;
private NativeHashMap<Edge, FixedList32Bytes<int>> edgesToTriangles;
Expand All @@ -756,6 +760,7 @@ public ClearDataJob(Triangulator triangulator)
outputPositions = triangulator.outputPositions;
outputTriangles = triangulator.outputTriangles;
triangles = triangulator.triangles;
halfedges = triangulator.halfedges;
circles = triangulator.circles;
trianglesToEdges = triangulator.trianglesToEdges;
edgesToTriangles = triangulator.edgesToTriangles;
Expand All @@ -774,6 +779,7 @@ public void Execute()
outputPositions.Clear();
outputTriangles.Clear();
triangles.Clear();
halfedges.Clear();
circles.Clear();
trianglesToEdges.Clear();
edgesToTriangles.Clear();
Expand Down Expand Up @@ -805,6 +811,7 @@ private struct DistComparer : IComparer<int>
private NativeArray<float2> inputPositions;
private NativeList<float2> outputPositions;
private NativeList<Triangle> trianglesRaw;
private NativeList<int> halfedgesRaw;

[NativeDisableContainerSafetyRestriction]
private NativeArray<float2> positions;
Expand Down Expand Up @@ -838,6 +845,7 @@ public DelaunayTriangulationJob(Triangulator triangulator)
inputPositions = triangulator.Input.Positions;
outputPositions = triangulator.outputPositions;
trianglesRaw = triangulator.triangles;
halfedgesRaw = triangulator.halfedges;

positions = default;
ids = default;
Expand Down Expand Up @@ -906,8 +914,8 @@ public void Execute()
var maxTriangles = math.max(2 * n - 5, 0);
trianglesRaw.Length = maxTriangles;
triangles = trianglesRaw.AsArray().Reinterpret<int>(3 * sizeof(int));

using var _halfedges = halfedges = new(3 * maxTriangles, Allocator.Temp);
halfedgesRaw.Length = 3 * maxTriangles;
halfedges = halfedgesRaw.AsArray();

hashSize = (int)math.ceil(math.sqrt(n));
using var _hullPrev = hullPrev = new(n, Allocator.Temp);
Expand Down Expand Up @@ -1084,6 +1092,7 @@ public void Execute()
}

trianglesRaw.Length = trianglesLen / 3;
halfedgesRaw.Length = trianglesLen;
}

private int Legalize(int a)
Expand Down

0 comments on commit a029bb2

Please sign in to comment.