Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: misc refinement cleanup #89

Merged
merged 1 commit into from
Nov 7, 2023
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
62 changes: 36 additions & 26 deletions Runtime/Triangulator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.Burst;
Expand Down Expand Up @@ -1894,7 +1895,9 @@ private int UnsafeInsertPoint(float2 p, int initTriangle, NativeList<int> heQueu
visitedTriangles[initTriangle] = true;
RecalculateBadTriangles(p);

BuildStarPolygon();
ProcessBadTriangles(pId, heQueue, tQueue);
BuildNewTriangles(pId, heQueue, tQueue);

return pId;
}
Expand Down Expand Up @@ -1938,9 +1941,9 @@ private void VisitEdge(float2 p, int t0, int t1)
}
}

private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<int> tQueue)
private void BuildStarPolygon()
{
// 1. Find the "first" halfedge of the polygon.
// Find the "first" halfedge of the polygon.
var initHe = -1;
for (int i = 0; i < badTriangles.Length; i++)
{
Expand All @@ -1963,7 +1966,7 @@ private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<in
}
}

// 2. Build polygon path from halfedges and points.
// Build polygon path from halfedges and points.
var id = initHe;
var initPoint = pathPoints[0];
while (true)
Expand All @@ -1983,8 +1986,11 @@ private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<in
}
id = he;
}
}

// 3. Remove bad triangles and recalculate polygon path halfedges.
private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<int> tQueue)
{
// Remove bad triangles and recalculate polygon path halfedges.
badTriangles.Sort();
for (int t = badTriangles.Length - 1; t >= 0; t--)
{
Expand Down Expand Up @@ -2048,12 +2054,25 @@ private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<in
}
}
}
}

// 4. Create triangles, circles, and halfedges for inserted point pId.
private void RemoveHalfedge(int he, int offset)
{
var ohe = halfedges[he];
var o = ohe > he ? ohe - offset : ohe;
if (o > -1)
{
halfedges[o] = -1;
}
halfedges.RemoveAt(he);
}

private void BuildNewTriangles(int pId, NativeList<int> heQueue, NativeList<int> tQueue)
{
// Build triangles/circles for inserted point pId.
var initTriangles = triangles.Length;
triangles.Length += 3 * pathPoints.Length;
circles.Length += pathPoints.Length;

for (int i = 0; i < pathPoints.Length - 1; i++)
{
triangles[initTriangles + 3 * i + 0] = pId;
Expand All @@ -2066,6 +2085,7 @@ private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<in
triangles[^1] = pathPoints[0];
circles[^1] = CalculateCircumCircle(pId, pathPoints[^1], pathPoints[0], outputPositions.AsArray());

// Build half-edges for inserted point pId.
var heOffset = halfedges.Length;
halfedges.Length += 3 * pathPoints.Length;
for (int i = 0; i < pathPoints.Length - 1; i++)
Expand All @@ -2079,15 +2099,6 @@ private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<in
halfedges[3 * i + 2 + heOffset] = 3 * i + 3 + heOffset;
halfedges[3 * i + 3 + heOffset] = 3 * i + 2 + heOffset;
}

for (int i = heOffset; i < halfedges.Length; i++)
{
if (IsEncroached(i))
{
heQueue.Add(i);
}
}

var phe = pathHalfedges[^1];
halfedges[heOffset + 3 * (pathPoints.Length - 1) + 1] = phe;
if (phe != -1)
Expand All @@ -2097,6 +2108,16 @@ private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<in
halfedges[heOffset] = heOffset + 3 * (pathPoints.Length - 1) + 2;
halfedges[heOffset + 3 * (pathPoints.Length - 1) + 2] = heOffset;

// Enqueue created edges.
for (int i = heOffset; i < halfedges.Length; i++)
{
if (IsEncroached(i))
{
heQueue.Add(i);
}
}

// Enqueue created triangles.
if (tQueue.IsCreated)
{
for (int i = initTriangles / 3; i < circles.Length; i++)
Expand All @@ -2108,17 +2129,6 @@ private void ProcessBadTriangles(int pId, NativeList<int> heQueue, NativeList<in
}
}
}

private void RemoveHalfedge(int he, int offset)
{
var ohe = halfedges[he];
var o = ohe > he ? ohe - offset : ohe;
if (o > -1)
{
halfedges[o] = -1;
}
halfedges.RemoveAt(he);
}
}

private interface IPlantingSeedJobMode<TSelf>
Expand Down
Loading