mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-03 08:39:25 -05:00
refactor(core): share duplicated geometry helpers
Route the four identical even-odd ray casts through RingContainment.Contains and share the arc-intersection, span-pairing, overlap-region, lead-in, plate-offset, leader-edge, entity-copy and path-perimeter duplicates. Expressions and loop order are unchanged, so results are bit-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a1d89cea5
commit
409d30d29e
15 files changed
+108
-154
No files matched your search
@@ -208,15 +208,8 @@ namespace OpenNest.Geometry
|
||||
)
|
||||
{
|
||||
var regions = new List<Polygon>();
|
||||
var stopAtFirst = verdictOnly
|
||||
&& (holesA == null || holesA.Count == 0)
|
||||
&& (holesB == null || holesB.Count == 0);
|
||||
ClipTrianglePairs(trisA, trisB, regions, stopAtFirst);
|
||||
|
||||
if (regions.Count > 0)
|
||||
regions = SubtractHoles(regions, holesA, holesB);
|
||||
|
||||
return regions;
|
||||
ClipTrianglePairs(trisA, trisB, regions, StopAtFirst(verdictOnly, holesA, holesB));
|
||||
return SubtractHolesFromAny(regions, holesA, holesB);
|
||||
}
|
||||
|
||||
private static List<Polygon> OverlapRegions(
|
||||
@@ -228,20 +221,26 @@ namespace OpenNest.Geometry
|
||||
)
|
||||
{
|
||||
var regions = new List<Polygon>();
|
||||
|
||||
// Without holes nothing is subtracted, so for a caller that needs only the verdict
|
||||
// the first clipped region decides it. Check returns the regions, so it gets all.
|
||||
var stopAtFirst = verdictOnly
|
||||
&& (holesA == null || holesA.Count == 0)
|
||||
&& (holesB == null || holesB.Count == 0);
|
||||
ClipTrianglePairs(trisA, trisB, regions, stopAtFirst);
|
||||
|
||||
if (regions.Count > 0)
|
||||
regions = SubtractHoles(regions, holesA, holesB);
|
||||
|
||||
return regions;
|
||||
ClipTrianglePairs(trisA, trisB, regions, StopAtFirst(verdictOnly, holesA, holesB));
|
||||
return SubtractHolesFromAny(regions, holesA, holesB);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Without holes nothing is subtracted, so for a caller that needs only the verdict
|
||||
/// the first clipped region decides it. Check returns the regions, so it gets all.
|
||||
/// </summary>
|
||||
private static bool StopAtFirst(bool verdictOnly, List<Polygon> holesA, List<Polygon> holesB) =>
|
||||
verdictOnly
|
||||
&& (holesA == null || holesA.Count == 0)
|
||||
&& (holesB == null || holesB.Count == 0);
|
||||
|
||||
/// <summary>The clipped regions less the holes; an empty list is returned as is.</summary>
|
||||
private static List<Polygon> SubtractHolesFromAny(
|
||||
List<Polygon> regions,
|
||||
List<Polygon> holesA,
|
||||
List<Polygon> holesB
|
||||
) => regions.Count > 0 ? SubtractHoles(regions, holesA, holesB) : regions;
|
||||
|
||||
/// <summary>
|
||||
/// Clips every triangle pair whose bounding boxes overlap, in (A, B) order, adding the
|
||||
/// positive-area regions. A triangle whose box misses the union of the other list's
|
||||
|
||||
@@ -24,24 +24,20 @@ namespace OpenNest.Geometry
|
||||
|
||||
internal static bool Intersects(Arc arc, Circle circle, out List<Vector> pts)
|
||||
{
|
||||
var c1 = new Circle(arc.Center, arc.Radius);
|
||||
|
||||
if (!Intersects(c1, circle, out pts))
|
||||
{
|
||||
pts = new List<Vector>();
|
||||
return false;
|
||||
}
|
||||
|
||||
pts = PointsOnArc(arc, pts);
|
||||
|
||||
return pts.Count > 0;
|
||||
var hit = Intersects(new Circle(arc.Center, arc.Radius), circle, out pts);
|
||||
return KeepOnArc(arc, hit, ref pts);
|
||||
}
|
||||
|
||||
internal static bool Intersects(Arc arc, Line line, out List<Vector> pts)
|
||||
{
|
||||
var c1 = new Circle(arc.Center, arc.Radius);
|
||||
var hit = Intersects(new Circle(arc.Center, arc.Radius), line, out pts);
|
||||
return KeepOnArc(arc, hit, ref pts);
|
||||
}
|
||||
|
||||
if (!Intersects(c1, line, out pts))
|
||||
/// <summary>Keeps the points of the arc's circle intersection that lie on the arc.</summary>
|
||||
private static bool KeepOnArc(Arc arc, bool circleHit, ref List<Vector> pts)
|
||||
{
|
||||
if (!circleHit)
|
||||
{
|
||||
pts = new List<Vector>();
|
||||
return false;
|
||||
|
||||
@@ -143,19 +143,6 @@ public static class KeepOutOverlap
|
||||
|
||||
/// <summary>Even-odd ray cast; exact for a point that is not on the ring (callers only
|
||||
/// ask after ruling out any edge through the rectangle containing the point).</summary>
|
||||
private static bool Contains(Polygon ring, Vector point)
|
||||
{
|
||||
var inside = false;
|
||||
var vertices = ring.Vertices;
|
||||
var count = vertices.Count;
|
||||
for (int i = 0, j = count - 1; i < count; j = i++)
|
||||
{
|
||||
var a = vertices[i];
|
||||
var b = vertices[j];
|
||||
if ((a.Y > point.Y) != (b.Y > point.Y)
|
||||
&& point.X < (b.X - a.X) * (point.Y - a.Y) / (b.Y - a.Y) + a.X)
|
||||
inside = !inside;
|
||||
}
|
||||
return inside;
|
||||
}
|
||||
private static bool Contains(Polygon ring, Vector point) =>
|
||||
RingContainment.Contains(ring.Vertices, point);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenNest.Geometry;
|
||||
|
||||
/// <summary>Even-odd point-in-ring test shared by callers that need the same answer bit for bit.</summary>
|
||||
public static class RingContainment
|
||||
{
|
||||
/// <summary>
|
||||
/// Even-odd ray cast of <paramref name="point"/> against the closed <paramref name="ring"/>
|
||||
/// (the last vertex joins the first). The answer for a point on the ring is unspecified.
|
||||
/// </summary>
|
||||
public static bool Contains(IReadOnlyList<Vector> ring, Vector point)
|
||||
{
|
||||
var inside = false;
|
||||
var count = ring.Count;
|
||||
for (int i = 0, j = count - 1; i < count; j = i++)
|
||||
{
|
||||
var a = ring[i];
|
||||
var b = ring[j];
|
||||
if ((a.Y > point.Y) != (b.Y > point.Y)
|
||||
&& point.X < (b.X - a.X) * (point.Y - a.Y) / (b.Y - a.Y) + a.X)
|
||||
inside = !inside;
|
||||
}
|
||||
return inside;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user