Files
OpenNest/OpenNest.Core/Geometry/CollisionTriangle.cs
T
ajandClaude Opus 5.5 7ae01978f5 perf(core): clip collision triangles without a polygon per triangle
Collision.HasOverlap, Check and PartOverlapChecker now triangulate into
CollisionTriangle values (each ear's open vertices and box sides) and clip
them in two reused stack buffers, instead of building a Polygon entity per
triangle and three lists per clip. Same ears, vertices, bounds, pairs and
arithmetic, so verdicts and regions are bit-identical.

On the N26-2GT job a 30-copy Lattice fill of an 80-vertex arc-cornered
bracket fell from 15 s to about 4 s with identical poses, and Irregular's
first plan from 66 s to about 26 s, inside Default's 60 s limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 01:36:19 -05:00

68 lines
2.4 KiB
C#

namespace OpenNest.Geometry
{
/// <summary>
/// One ear of <see cref="ConvexDecomposition.Triangulate(Polygon)"/> as the collision clip loop
/// reads its triangle polygon: the open vertices and the bounding-box sides, without building
/// the polygon. Every value is bit for bit what the loop reads from the polygon.
/// </summary>
internal readonly struct CollisionTriangle
{
public CollisionTriangle(Vector prev, Vector curr, Vector next)
{
// The polygon is [prev, curr, next], closed with prev by Polygon.Close only when prev
// and next differ by its fuzzy equality. Its open vertices drop a last vertex exactly
// equal to the first: the closing vertex, or next when it was not closed.
V0 = prev;
V1 = curr;
V2 = next;
Count = prev != next || !(prev.X == next.X && prev.Y == next.Y) ? 3 : 2;
// Polygon.UpdateBounds over the same vertices; the closing vertex repeats prev.
var minX = prev.X;
var maxX = prev.X;
var minY = prev.Y;
var maxY = prev.Y;
Include(curr, ref minX, ref maxX, ref minY, ref maxY);
Include(next, ref minX, ref maxX, ref minY, ref maxY);
// Box sides as Box computes them from its location and size.
Left = minX;
Bottom = minY;
Right = minX + (maxX - minX);
Top = minY + (maxY - minY);
}
public Vector V0 { get; }
public Vector V1 { get; }
public Vector V2 { get; }
/// <summary>Open vertices: 3, or 2 when next is exactly prev.</summary>
public int Count { get; }
public double Left { get; }
public double Right { get; }
public double Bottom { get; }
public double Top { get; }
public Vector this[int index] => index switch
{
0 => V0,
1 => V1,
_ => V2,
};
private static void Include(Vector vertex, ref double minX, ref double maxX, ref double minY, ref double maxY)
{
if (vertex.X < minX)
minX = vertex.X;
else if (vertex.X > maxX)
maxX = vertex.X;
if (vertex.Y < minY)
minY = vertex.Y;
else if (vertex.Y > maxY)
maxY = vertex.Y;
}
}
}