From 409d30d29ea660d54bf057767f37853722c7b203 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sat, 28 Nov 2026 18:36:20 -0500 Subject: [PATCH] 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 --- OpenNest.Core/Breakoffs/BreakoffPath.cs | 15 +------ .../CommonLines/CommonLineGeometry.cs | 15 +------ OpenNest.Core/Geometry/Collision.cs | 41 +++++++++---------- OpenNest.Core/Geometry/Intersect.cs | 22 ++++------ OpenNest.Core/Geometry/KeepOutOverlap.cs | 17 +------- OpenNest.Core/Geometry/RingContainment.cs | 26 ++++++++++++ OpenNest.Core/Part.cs | 30 ++++++++------ OpenNest.Core/PartGeometry.cs | 3 +- OpenNest.Core/Plate.Transforms.cs | 22 ++-------- OpenNest.Core/Shapes/ShapeDimension.cs | 16 +++++--- OpenNest.Core/SheetOutline.cs | 3 +- OpenNest.Core/Support/SupportMaterial.cs | 16 ++++---- .../Interaction/PlateHitTester.cs | 11 +---- OpenNest.Engine/Fill/Expander.cs | 13 +----- .../NestingEngines/Irregular/BlockCatalog.cs | 12 +----- 15 files changed, 108 insertions(+), 154 deletions(-) create mode 100644 OpenNest.Core/Geometry/RingContainment.cs diff --git a/OpenNest.Core/Breakoffs/BreakoffPath.cs b/OpenNest.Core/Breakoffs/BreakoffPath.cs index ca37e9f1..18d8eefc 100644 --- a/OpenNest.Core/Breakoffs/BreakoffPath.cs +++ b/OpenNest.Core/Breakoffs/BreakoffPath.cs @@ -111,19 +111,8 @@ namespace OpenNest.Breakoffs return sum / 2; } - public static bool Contains(IReadOnlyList 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 loop, Vector point) => + RingContainment.Contains(loop, point); private static bool Locate(IReadOnlyList path, double distance, out Entity entity, out double local) { diff --git a/OpenNest.Core/CommonLines/CommonLineGeometry.cs b/OpenNest.Core/CommonLines/CommonLineGeometry.cs index f7598276..2e3e70ad 100644 --- a/OpenNest.Core/CommonLines/CommonLineGeometry.cs +++ b/OpenNest.Core/CommonLines/CommonLineGeometry.cs @@ -373,19 +373,8 @@ namespace OpenNest.CommonLines return seen.All(s => s); } - private static bool Inside(IReadOnlyList 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 ring, Vector point) => + RingContainment.Contains(ring, point); private static double Dot(Vector a, Vector b) => a.X * b.X + a.Y * b.Y; diff --git a/OpenNest.Core/Geometry/Collision.cs b/OpenNest.Core/Geometry/Collision.cs index 011cb1b5..2425c1d4 100644 --- a/OpenNest.Core/Geometry/Collision.cs +++ b/OpenNest.Core/Geometry/Collision.cs @@ -208,15 +208,8 @@ namespace OpenNest.Geometry ) { var regions = new List(); - 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 OverlapRegions( @@ -228,20 +221,26 @@ namespace OpenNest.Geometry ) { var regions = new List(); - - // 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); } + /// + /// 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. + /// + private static bool StopAtFirst(bool verdictOnly, List holesA, List holesB) => + verdictOnly + && (holesA == null || holesA.Count == 0) + && (holesB == null || holesB.Count == 0); + + /// The clipped regions less the holes; an empty list is returned as is. + private static List SubtractHolesFromAny( + List regions, + List holesA, + List holesB + ) => regions.Count > 0 ? SubtractHoles(regions, holesA, holesB) : regions; + /// /// 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 diff --git a/OpenNest.Core/Geometry/Intersect.cs b/OpenNest.Core/Geometry/Intersect.cs index 85d8301b..fb051391 100644 --- a/OpenNest.Core/Geometry/Intersect.cs +++ b/OpenNest.Core/Geometry/Intersect.cs @@ -24,24 +24,20 @@ namespace OpenNest.Geometry internal static bool Intersects(Arc arc, Circle circle, out List pts) { - var c1 = new Circle(arc.Center, arc.Radius); - - if (!Intersects(c1, circle, out pts)) - { - pts = new List(); - 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 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)) + /// Keeps the points of the arc's circle intersection that lie on the arc. + private static bool KeepOnArc(Arc arc, bool circleHit, ref List pts) + { + if (!circleHit) { pts = new List(); return false; diff --git a/OpenNest.Core/Geometry/KeepOutOverlap.cs b/OpenNest.Core/Geometry/KeepOutOverlap.cs index da5b8592..c88b9b0f 100644 --- a/OpenNest.Core/Geometry/KeepOutOverlap.cs +++ b/OpenNest.Core/Geometry/KeepOutOverlap.cs @@ -143,19 +143,6 @@ public static class KeepOutOverlap /// 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). - 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); } diff --git a/OpenNest.Core/Geometry/RingContainment.cs b/OpenNest.Core/Geometry/RingContainment.cs new file mode 100644 index 00000000..192bf196 --- /dev/null +++ b/OpenNest.Core/Geometry/RingContainment.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; + +namespace OpenNest.Geometry; + +/// Even-odd point-in-ring test shared by callers that need the same answer bit for bit. +public static class RingContainment +{ + /// + /// Even-odd ray cast of against the closed + /// (the last vertex joins the first). The answer for a point on the ring is unspecified. + /// + public static bool Contains(IReadOnlyList 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; + } +} diff --git a/OpenNest.Core/Part.cs b/OpenNest.Core/Part.cs index cfca26c0..8600ab89 100644 --- a/OpenNest.Core/Part.cs +++ b/OpenNest.Core/Part.cs @@ -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); + } + + /// Records the clean-drawing pose and returns the strategy for . + 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; + } + + /// Installs the owned program the strategy produced as this part's manual lead-ins. + private void CommitLeadIns(Program program, CNC.CuttingStrategy.CuttingParameters parameters) + { + Program = program; ownsProgram = true; CuttingParameters = parameters; HasManualLeadIns = true; diff --git a/OpenNest.Core/PartGeometry.cs b/OpenNest.Core/PartGeometry.cs index 7b42239a..ecb1c383 100644 --- a/OpenNest.Core/PartGeometry.cs +++ b/OpenNest.Core/PartGeometry.cs @@ -144,7 +144,8 @@ namespace OpenNest return entities; } - private static List CopyEntitiesAtLocation(List source, Vector location) + /// Clones of , each offset by . + internal static List CopyEntitiesAtLocation(List source, Vector location) { var result = new List(source.Count); diff --git a/OpenNest.Core/Plate.Transforms.cs b/OpenNest.Core/Plate.Transforms.cs index c4fb70b4..57f89114 100644 --- a/OpenNest.Core/Plate.Transforms.cs +++ b/OpenNest.Core/Plate.Transforms.cs @@ -205,25 +205,7 @@ namespace OpenNest /// /// /// - 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)); /// /// Offsets the parts on the plate. @@ -231,6 +213,7 @@ namespace OpenNest /// 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, diff --git a/OpenNest.Core/Shapes/ShapeDimension.cs b/OpenNest.Core/Shapes/ShapeDimension.cs index ed9533d8..cf522bb4 100644 --- a/OpenNest.Core/Shapes/ShapeDimension.cs +++ b/OpenNest.Core/Shapes/ShapeDimension.cs @@ -133,9 +133,7 @@ namespace OpenNest.Shapes /// 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 /// 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 }; } + /// The unit of and the point on the circle it faces. + 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)); + } + /// /// A radius leader on the arc at from /// counterclockwise to (radians): it starts at the arc's middle diff --git a/OpenNest.Core/SheetOutline.cs b/OpenNest.Core/SheetOutline.cs index a85bc35a..03345b4a 100644 --- a/OpenNest.Core/SheetOutline.cs +++ b/OpenNest.Core/SheetOutline.cs @@ -295,7 +295,8 @@ namespace OpenNest return path; } - private static double Perimeter(PathD path) + /// The length of the closed , its last point joined to its first. + internal static double Perimeter(PathD path) { var length = 0.0; for (var i = 0; i < path.Count; i++) diff --git a/OpenNest.Core/Support/SupportMaterial.cs b/OpenNest.Core/Support/SupportMaterial.cs index 1388574e..e861c4f6 100644 --- a/OpenNest.Core/Support/SupportMaterial.cs +++ b/OpenNest.Core/Support/SupportMaterial.cs @@ -169,17 +169,13 @@ namespace OpenNest.Support /// public IReadOnlyList<(double Start, double End)> SpansAtY(double y) { - var crossings = new List(); if (!IsComplete || y < bounds.Bottom || y > bounds.Top) return Array.Empty<(double, double)>(); + var crossings = new List(); 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); } /// @@ -188,12 +184,18 @@ namespace OpenNest.Support /// public IReadOnlyList<(double Start, double End)> SpansAtX(double x) { - var crossings = new List(); if (!IsComplete || x < bounds.Left || x > bounds.Right) return Array.Empty<(double, double)>(); + var crossings = new List(); AddCrossingsAtX(outline, x, crossings); foreach (var hole in holes) AddCrossingsAtX(hole, x, crossings); + return PairCrossings(crossings); + } + + /// Sorts the crossings and pairs them into the spans between them. + private static List<(double Start, double End)> PairCrossings(List crossings) + { crossings.Sort(); var spans = new List<(double, double)>(crossings.Count / 2); for (var i = 0; i + 1 < crossings.Count; i += 2) diff --git a/OpenNest.Desktop/Interaction/PlateHitTester.cs b/OpenNest.Desktop/Interaction/PlateHitTester.cs index 4dc83824..0d061ebe 100644 --- a/OpenNest.Desktop/Interaction/PlateHitTester.cs +++ b/OpenNest.Desktop/Interaction/PlateHitTester.cs @@ -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; } diff --git a/OpenNest.Engine/Fill/Expander.cs b/OpenNest.Engine/Fill/Expander.cs index ad7c5f73..6fe9096c 100644 --- a/OpenNest.Engine/Fill/Expander.cs +++ b/OpenNest.Engine/Fill/Expander.cs @@ -1193,17 +1193,8 @@ namespace OpenNest.Engine.Fill return enter < leave; } - private static List CloneEntitiesAt(List entities, Vector position) - { - var result = new List(entities.Count); - foreach (var entity in entities) - { - var copy = entity.Clone(); - copy.Offset(position); - result.Add(copy); - } - return result; - } + private static List CloneEntitiesAt(List entities, Vector position) => + PartGeometry.CopyEntitiesAtLocation(entities, position); private static bool TryConvexDirectExit( RingSet moving, RingSet stationary, Vector movePos, Vector fixedPos, diff --git a/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs b/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs index 97ebfbbc..f8f450d7 100644 --- a/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs +++ b/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs @@ -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); } ///