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:
ajandClaude Opus 5.5 committed 2026-11-28 18:36:40 -05:00
1 parent 8a1d89cea5
commit 409d30d29e
15 files changed
+108 -154

No files matched your search

+2 -13
View File
@@ -111,19 +111,8 @@ namespace OpenNest.Breakoffs
return sum / 2;
}
public static bool Contains(IReadOnlyList<Vector> loop, Vector point)
{
var inside = false;
for (int i = 0, j = loop.Count - 1; i < loop.Count; j = i++)
{
var a = loop[i];
var b = loop[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;
}
public static bool Contains(IReadOnlyList<Vector> loop, Vector point) =>
RingContainment.Contains(loop, point);
private static bool Locate(IReadOnlyList<Entity> path, double distance, out Entity entity, out double local)
{
@@ -373,19 +373,8 @@ namespace OpenNest.CommonLines
return seen.All(s => s);
}
private static bool Inside(IReadOnlyList<Vector> ring, Vector point)
{
var inside = false;
for (int i = 0, j = ring.Count - 1; i < ring.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;
}
private static bool Inside(IReadOnlyList<Vector> ring, Vector point) =>
RingContainment.Contains(ring, point);
private static double Dot(Vector a, Vector b) => a.X * b.X + a.Y * b.Y;
+20 -21
View File
@@ -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
+9 -13
View File
@@ -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;
+2 -15
View File
@@ -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);
}
+26
View File
@@ -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;
}
}
+17 -13
View File
@@ -98,17 +98,9 @@ namespace OpenNest
Vector nextPartStart
)
{
preLeadInRotation = Rotation;
var strategy = new CNC.CuttingStrategy.ContourCuttingStrategy
{
Parameters = parameters,
};
var strategy = BeginLeadIns(parameters);
var result = strategy.Apply(Program, approachPoint, nextPartStart);
Program = result.Program;
ownsProgram = true;
CuttingParameters = parameters;
HasManualLeadIns = true;
UpdateBounds();
CommitLeadIns(result.Program, parameters);
}
public void ApplySingleLeadIn(
@@ -117,14 +109,26 @@ namespace OpenNest
Geometry.Entity entity,
CNC.CuttingStrategy.ContourType contourType
)
{
var strategy = BeginLeadIns(parameters);
var result = strategy.ApplySingle(Program, point, entity, contourType);
CommitLeadIns(result.Program, parameters);
}
/// <summary>Records the clean-drawing pose and returns the strategy for <paramref name="parameters"/>.</summary>
private CNC.CuttingStrategy.ContourCuttingStrategy BeginLeadIns(CNC.CuttingStrategy.CuttingParameters parameters)
{
preLeadInRotation = Rotation;
var strategy = new CNC.CuttingStrategy.ContourCuttingStrategy
return new CNC.CuttingStrategy.ContourCuttingStrategy
{
Parameters = parameters,
};
var result = strategy.ApplySingle(Program, point, entity, contourType);
Program = result.Program;
}
/// <summary>Installs the owned program the strategy produced as this part's manual lead-ins.</summary>
private void CommitLeadIns(Program program, CNC.CuttingStrategy.CuttingParameters parameters)
{
Program = program;
ownsProgram = true;
CuttingParameters = parameters;
HasManualLeadIns = true;
+2 -1
View File
@@ -144,7 +144,8 @@ namespace OpenNest
return entities;
}
private static List<Entity> CopyEntitiesAtLocation(List<Entity> source, Vector location)
/// <summary>Clones of <paramref name="source"/>, each offset by <paramref name="location"/>.</summary>
internal static List<Entity> CopyEntitiesAtLocation(List<Entity> source, Vector location)
{
var result = new List<Entity>(source.Count);
+3 -19
View File
@@ -205,25 +205,7 @@ namespace OpenNest
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void Offset(double x, double y)
{
// Remove cut-off parts before transforming
for (var i = Parts.Count - 1; i >= 0; i--)
{
if (Parts[i].BaseDrawing.IsCutOff)
Parts.RemoveAt(i);
}
for (var i = 0; i < Parts.Count; ++i)
{
var part = Parts[i];
part.Offset(x, y);
}
// Transform cut-off positions
foreach (var cutoff in CutOffs)
cutoff.Position = new Vector(cutoff.Position.X + x, cutoff.Position.Y + y);
}
public void Offset(double x, double y) => Offset(new Vector(x, y));
/// <summary>
/// Offsets the parts on the plate.
@@ -231,6 +213,7 @@ namespace OpenNest
/// <param name="voffset"></param>
public void Offset(Vector voffset)
{
// Remove cut-off parts before transforming
for (var i = Parts.Count - 1; i >= 0; i--)
{
if (Parts[i].BaseDrawing.IsCutOff)
@@ -243,6 +226,7 @@ namespace OpenNest
part.Offset(voffset);
}
// Transform cut-off positions
foreach (var cutoff in CutOffs)
cutoff.Position = new Vector(
cutoff.Position.X + voffset.X,
+10 -6
View File
@@ -133,9 +133,7 @@ namespace OpenNest.Shapes
/// </summary>
public static ShapeDimension HoleLeader(string property, Vector center, double radius, Vector direction, string prefix = "")
{
var length = System.Math.Sqrt(direction.X * direction.X + direction.Y * direction.Y);
var unit = new Vector(direction.X / length, direction.Y / length);
var edge = new Vector(center.X + unit.X * radius, center.Y + unit.Y * radius);
var (unit, edge) = EdgeFacing(center, radius, direction);
return new(property, ShapeDimensionKind.Leader, edge, edge, unit, prefix)
{
HoleCenter = center,
@@ -151,9 +149,7 @@ namespace OpenNest.Shapes
/// </summary>
public static ShapeDimension CircleLeader(string property, Vector center, double radius, Vector direction, string prefix = "")
{
var length = System.Math.Sqrt(direction.X * direction.X + direction.Y * direction.Y);
var unit = new Vector(direction.X / length, direction.Y / length);
var edge = new Vector(center.X + unit.X * radius, center.Y + unit.Y * radius);
var (unit, edge) = EdgeFacing(center, radius, direction);
return new(property, ShapeDimensionKind.Leader, edge, edge, unit, prefix)
{
CircleCenter = center,
@@ -161,6 +157,14 @@ namespace OpenNest.Shapes
};
}
/// <summary>The unit of <paramref name="direction"/> and the point on the circle it faces.</summary>
private static (Vector Unit, Vector Edge) EdgeFacing(Vector center, double radius, Vector direction)
{
var length = System.Math.Sqrt(direction.X * direction.X + direction.Y * direction.Y);
var unit = new Vector(direction.X / length, direction.Y / length);
return (unit, new Vector(center.X + unit.X * radius, center.Y + unit.Y * radius));
}
/// <summary>
/// A radius leader on the arc at <paramref name="center"/> from <paramref name="startAngle"/>
/// counterclockwise to <paramref name="endAngle"/> (radians): it starts at the arc's middle
+2 -1
View File
@@ -295,7 +295,8 @@ namespace OpenNest
return path;
}
private static double Perimeter(PathD path)
/// <summary>The length of the closed <paramref name="path"/>, its last point joined to its first.</summary>
internal static double Perimeter(PathD path)
{
var length = 0.0;
for (var i = 0; i < path.Count; i++)
+9 -7
View File
@@ -169,17 +169,13 @@ namespace OpenNest.Support
/// </summary>
public IReadOnlyList<(double Start, double End)> SpansAtY(double y)
{
var crossings = new List<double>();
if (!IsComplete || y < bounds.Bottom || y > bounds.Top)
return Array.Empty<(double, double)>();
var crossings = new List<double>();
AddCrossings(outline, y, crossings);
foreach (var hole in holes)
AddCrossings(hole, y, crossings);
crossings.Sort();
var spans = new List<(double, double)>(crossings.Count / 2);
for (var i = 0; i + 1 < crossings.Count; i += 2)
spans.Add((crossings[i], crossings[i + 1]));
return spans;
return PairCrossings(crossings);
}
/// <summary>
@@ -188,12 +184,18 @@ namespace OpenNest.Support
/// </summary>
public IReadOnlyList<(double Start, double End)> SpansAtX(double x)
{
var crossings = new List<double>();
if (!IsComplete || x < bounds.Left || x > bounds.Right)
return Array.Empty<(double, double)>();
var crossings = new List<double>();
AddCrossingsAtX(outline, x, crossings);
foreach (var hole in holes)
AddCrossingsAtX(hole, x, crossings);
return PairCrossings(crossings);
}
/// <summary>Sorts the crossings and pairs them into the spans between them.</summary>
private static List<(double Start, double End)> PairCrossings(List<double> crossings)
{
crossings.Sort();
var spans = new List<(double, double)>(crossings.Count / 2);
for (var i = 0; i + 1 < crossings.Count; i += 2)
+1 -10
View File
@@ -219,16 +219,7 @@ public sealed class PlateHitTester
{
var inside = false;
foreach (var ring in rings)
{
for (int i = 0, j = ring.Length - 1; i < ring.Length; 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;
}
}
inside ^= RingContainment.Contains(ring, point);
return inside;
}
+2 -11
View File
@@ -1193,17 +1193,8 @@ namespace OpenNest.Engine.Fill
return enter < leave;
}
private static List<Entity> CloneEntitiesAt(List<Entity> entities, Vector position)
{
var result = new List<Entity>(entities.Count);
foreach (var entity in entities)
{
var copy = entity.Clone();
copy.Offset(position);
result.Add(copy);
}
return result;
}
private static List<Entity> CloneEntitiesAt(List<Entity> entities, Vector position) =>
PartGeometry.CopyEntitiesAtLocation(entities, position);
private static bool TryConvexDirectExit(
RingSet moving, RingSet stationary, Vector movePos, Vector fixedPos,
@@ -161,17 +161,7 @@ internal sealed class BlockCatalog : IDisposable
return width;
}
private static double Perimeter(PathD outline)
{
var perimeter = 0.0;
for (var i = 0; i < outline.Count; i++)
{
var a = outline[i];
var b = outline[(i + 1) % outline.Count];
perimeter += System.Math.Sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}
return perimeter;
}
private static double Perimeter(PathD outline) => SheetOutline.Perimeter(outline);
}
/// <summary>