Files
OpenNest/OpenNest.Core/Geometry/ConvexHull.cs
T
ajandClaude Fable 5.2 af2712e700 perf(core): triangulate, hull and flatten with the same results and less work
Ear clipping walked every remaining vertex from the first after each ear,
re-reading a remembered verdict for most of them. It now keeps the set of
vertices whose verdict can have changed (an ear's two neighbors and the
vertices it blocked) and takes the lowest, which tests the same vertices
in the same order. The ear test reads the remaining vertices from packed
arrays, several at a time where the hardware allows, with the arithmetic
of PointInTriangle per lane. Working arrays are kept per thread.

ConvexHull sorts in a per-thread buffer with the position as tie-break,
the order the stable two-key LINQ sort gave. Shape flattening adds arc
points straight to the polygon; Arc's sweep, segment-count and point
arithmetic moved to static helpers so callers can flatten an arc without
building one.

A 246-vertex triangulation drops from about 410 to 115 microseconds.
Triangles, hulls and polygons are bit-identical: the existing frozen
ConvexDecomposition oracle tests pass, and new tests compare the hull and
the flattening with the earlier code.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-24 13:26:51 -05:00

108 lines
3.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
namespace OpenNest.Geometry
{
public static class ConvexHull
{
public static Polygon Compute(IList<Vector> points)
{
if (points.Count < 3)
{
var result = new Polygon();
result.Vertices.AddRange(points);
return result;
}
// By X, then Y, equal points in their given order: the order a stable sort on
// those two keys gives. The position breaks ties, so no two entries compare equal
// and the unstable array sort has only one result. The buffer is kept per thread.
var count = points.Count;
var sorted = sortBuffer;
if (sorted == null || sorted.Length < count)
sortBuffer = sorted = new SortedPoint[System.Math.Max(count, 64)];
for (var i = 0; i < count; i++)
sorted[i] = new SortedPoint(points[i], i);
System.Array.Sort(sorted, 0, count);
var lower = new List<Vector>();
for (var i = 0; i < count; i++)
{
var p = sorted[i].Point;
while (
lower.Count >= 2
&& Cross(lower[lower.Count - 2], lower[lower.Count - 1], p) <= 0
)
lower.RemoveAt(lower.Count - 1);
lower.Add(p);
}
var upper = new List<Vector>();
for (int i = count - 1; i >= 0; i--)
{
var p = sorted[i].Point;
while (
upper.Count >= 2
&& Cross(upper[upper.Count - 2], upper[upper.Count - 1], p) <= 0
)
upper.RemoveAt(upper.Count - 1);
upper.Add(p);
}
// Remove last point of each half because it's repeated
lower.RemoveAt(lower.Count - 1);
upper.RemoveAt(upper.Count - 1);
lower.AddRange(upper);
var hull = new Polygon();
hull.Vertices.AddRange(lower);
hull.Close();
hull.UpdateBounds();
return hull;
}
[System.ThreadStatic]
private static SortedPoint[] sortBuffer;
private readonly struct SortedPoint : System.IComparable<SortedPoint>
{
private readonly int position;
public SortedPoint(Vector point, int position)
{
Point = point;
this.position = position;
}
public Vector Point { get; }
public int CompareTo(SortedPoint other)
{
var byX = Point.X.CompareTo(other.Point.X);
if (byX != 0)
return byX;
var byY = Point.Y.CompareTo(other.Point.Y);
return byY != 0 ? byY : position.CompareTo(other.position);
}
}
private static double Cross(Vector o, Vector a, Vector b)
{
return (a.X - o.X) * (b.Y - o.Y) - (a.Y - o.Y) * (b.X - o.X);
}
}
}