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: bw build polygon #292

Merged
merged 1 commit into from
Nov 14, 2024
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
59 changes: 19 additions & 40 deletions Runtime/Triangulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3200,15 +3200,16 @@ private int UnsafeInsertPointCommon(T2 p, int initTriangle)
public void UnsafeInsertPointBulk(T2 p, int initTriangle)
{
var pId = UnsafeInsertPointCommon(p, initTriangle);
BuildStarPolygon();
var initHe = FindInitPolygonHalfedge();
BuildPolygon(initHe, amphitheater: false);
ProcessBadTriangles();
BuildNewTrianglesForStar(pId);
}

public void UnsafeInsertPointBoundary(T2 p, int initHe)
{
var pId = UnsafeInsertPointCommon(p, initHe / 3);
BuildAmphitheaterPolygon(initHe);
BuildPolygon(initHe, amphitheater: true);
ProcessBadTriangles();
BuildNewTrianglesForAmphitheater(pId);
}
Expand Down Expand Up @@ -3239,7 +3240,7 @@ private void RecalculateBadTriangles(T2 p)
}
}

private void BuildAmphitheaterPolygon(int initHe)
private void BuildPolygon(int initHe, bool amphitheater)
{
var triangles = Output.Triangles;

Expand All @@ -3254,66 +3255,44 @@ private void BuildAmphitheaterPolygon(int initHe)
}

var he = Output.Halfedges[id];
if (he == -1 || !BadTriangles.Contains(he / 3))
if (he == -1 || !VisitedTriangles[he / 3])
{
PathPoints.Add(triangles[id]);
PathHalfedges.Add(he);
continue;
}
id = he;
}
PathPoints.Add(triangles[initHe]);
PathHalfedges.Add(-1);

if (amphitheater)
{
PathPoints.Add(triangles[initHe]);
PathHalfedges.Add(-1);
}
}

private void BuildStarPolygon()
// TODO: this operation can be optimized.
private int FindInitPolygonHalfedge()
{
var triangles = Output.Triangles;
var halfedges = Output.Halfedges;

// Find the "first" halfedge of the polygon.
var initHe = -1;
for (int i = 0; i < BadTriangles.Length; i++)
{
var tId = BadTriangles[i];
for (int t = 0; t < 3; t++)
{
var he = 3 * tId + t;
var ohe = halfedges[he];
if (ohe == -1 || !BadTriangles.Contains(ohe / 3))
var ohe = Output.Halfedges[he];
if (ohe == -1 || !VisitedTriangles[ohe / 3])
{
PathPoints.Add(triangles[he]);
PathPoints.Add(Output.Triangles[he]);
PathHalfedges.Add(ohe);
initHe = he;
break;
return he;
}
}
if (initHe != -1)
{
break;
}
}

// Build polygon path from halfedges and points.
var id = initHe;
var initPoint = PathPoints[0];
while (true)
{
id = NextHalfedge(id);
if (triangles[id] == initPoint)
{
break;
}

var he = halfedges[id];
if (he == -1 || !BadTriangles.Contains(he / 3))
{
PathPoints.Add(triangles[id]);
PathHalfedges.Add(he);
continue;
}
id = he;
}
// Note: This should be guaranteed that such `he` exists in proper mesh.
return -1;
}

private void ProcessBadTriangles()
Expand Down
Loading