diff --git a/OpenNest.Core/Diagnostics/PostVerificationGeometry.cs b/OpenNest.Core/Diagnostics/PostVerificationGeometry.cs index c87e3152..81855229 100644 --- a/OpenNest.Core/Diagnostics/PostVerificationGeometry.cs +++ b/OpenNest.Core/Diagnostics/PostVerificationGeometry.cs @@ -392,9 +392,26 @@ internal static class PostVerificationGeometry || InteriorWitness(other.Start, this, witness) || InteriorWitness(other.End, this, witness)); if (SameSupport(other)) points.Clear(); + // Two curves that meet end to end share that vertex, and the native query is + // ill-conditioned there: circles meeting at a shallow angle, or a line meeting its own + // fillet, put their root up to 1e-5 off the joint, which no joint tolerance can tell + // from a cut that touches beside it. Both shapes are known, so their other + // intersection, if there is one, follows from the shared vertex exactly instead. + var joint = SameSupport(other) ? null : SharedVertex(other); + var adjacent = joint.HasValue; + if (joint is { } vertex) + { + points.Clear(); + points.Add(vertex); + if (SecondIntersection(other, vertex) is { } second && second.DistanceTo(vertex) > Epsilon + && Contains(second) && other.Contains(second) + && !IsOutsideArcSpan(second) && !other.IsOutsideArcSpan(second)) + points.Add(second); + } foreach (var point in points) Validate(point); - var nativeContactCount = points.Count; + // Analytic contacts of adjacent curves are certain; the ray guard below is for native queries. + var nativeContactCount = adjacent ? int.MaxValue : points.Count; foreach (var point in new[] { Start, End, other.Start, other.End }) if (Contains(point) && other.Contains(point) && !points.Exists(p => p.DistanceTo(point) <= Epsilon)) @@ -412,6 +429,49 @@ internal static class PostVerificationGeometry || (point.DistanceTo(curve.Start) > tolerance && point.DistanceTo(curve.End) > tolerance)); } + /// + /// The end this curve and share, within , as + /// this curve has it; null when they share none or either is a full circle. + /// + private Vector? SharedVertex(Curve other) + { + if (Center.HasValue && !IsFiniteArc || other.Center.HasValue && !other.IsFiniteArc) + return null; + foreach (var end in new[] { End, Start }) + if (end.DistanceTo(other.Start) <= Epsilon || end.DistanceTo(other.End) <= Epsilon) + return end; + return null; + } + + /// + /// Where the supports of this curve and meet again, given that + /// they meet at : two circles meet again at the vertex's mirror + /// image across the line through their centers, and a line meets a circle again at the + /// far end of its chord through the vertex. Two lines meet only once. The result is on + /// both supports; it may be the vertex itself (tangency) or off either finite span. + /// + private Vector? SecondIntersection(Curve other, Vector vertex) + { + if (Center is { } center && other.Center is { } otherCenter) + { + var axis = otherCenter - center; + var distance = otherCenter.DistanceTo(center); + if (distance <= Epsilon) + return null; + var unit = axis * (1 / distance); + var foot = center + unit * Dot(vertex - center, unit); + return foot * 2 - vertex; + } + if (Center is null && other.Center is null) + return null; + var line = Center is null ? this : other; + var circle = Center is null ? other.Center!.Value : Center!.Value; + if (line.Length <= Epsilon) + return null; + var direction = (line.End - line.Start) * (1 / line.Length); + return vertex + direction * (2 * Dot(circle - vertex, direction)); + } + /// /// Whether exact ray semantics find touching /// anywhere except at a line endpoint the curve contains, which is already a recorded contact. diff --git a/OpenNest.Tests/CuttingPlanning/LeadMaterialSnapshotTests.cs b/OpenNest.Tests/CuttingPlanning/LeadMaterialSnapshotTests.cs index 46911475..dd8acd0c 100644 --- a/OpenNest.Tests/CuttingPlanning/LeadMaterialSnapshotTests.cs +++ b/OpenNest.Tests/CuttingPlanning/LeadMaterialSnapshotTests.cs @@ -20,7 +20,9 @@ public class LeadMaterialSnapshotTests var arcs = execution.Motions.Where(m => m.Curve?.IsFiniteArc == true).Select(m => m.Curve!).ToArray(); var contacts = arcs[0].Contacts(arcs[1], out var overlap); Assert.False(overlap); - Assert.Contains(contacts, p => arcs.Any(a => a.IsOutsideArcSpan(p))); + // Adjacent curves meet exactly at their joint: the second crossing of these near-tangent + // circles, 0.000106 away on the radius-18 arc's extension, is off that arc's span. + Assert.Equal(location, Assert.Single(contacts)); var material = LeadMaterialSnapshot.Capture(program, location); @@ -45,8 +47,8 @@ public class LeadMaterialSnapshotTests private static Program ArcJoint(double centerOffset, bool reverseSecond, bool atSeam) { - // Near-tangent circles meet at zero and about 0.000106 away. The native - // angular band of the radius-18 arc admits its extension beyond zero. + // Near-tangent circles meet at zero and about 0.000106 away, on the radius-18 arc's + // extension beyond zero, within the native query's angular band. var first = new Vector(-18, 18); var center = new Vector(centerOffset, 1); var end = center + new Vector(center.DistanceTo(Vector.Zero), 0); diff --git a/OpenNest.Tests/Diagnostics/AdjacentCurveContactTests.cs b/OpenNest.Tests/Diagnostics/AdjacentCurveContactTests.cs new file mode 100644 index 00000000..fbacad25 --- /dev/null +++ b/OpenNest.Tests/Diagnostics/AdjacentCurveContactTests.cs @@ -0,0 +1,102 @@ +using OpenNest.Diagnostics; +using OpenNest.Geometry; + +namespace OpenNest.Tests.Diagnostics; + +/// +/// Contacts between two curves that meet end to end. The native intersection of their supports +/// is ill-conditioned at such a joint (two circles meeting at a shallow angle, or a line meeting +/// its fillet), so the checks derive the pair's other intersection from the shared vertex instead. +/// +public class AdjacentCurveContactTests +{ + private static PostVerificationGeometry.Curve Arc(Vector start, Vector end, Vector center, bool clockwise = false) => + PostVerificationGeometry.Curve.Create(start, end, center, clockwise); + + private static PostVerificationGeometry.Curve Line(Vector start, Vector end) => + PostVerificationGeometry.Curve.Create(start, end, null, false); + + // A half circle of radius 5 whose center is off by delta, as a DXF arc rounded apart from + // its neighbors is, followed by a quarter circle of radius 3 that continues it tangentially. + // Both ends still meet within the closure epsilon; the circles then cross again beside the + // joint, where the native query puts a contact a few 1e-5 off the vertex. + [Theory] + [InlineData(0)] + [InlineData(-1e-11)] + [InlineData(-2e-11)] + [InlineData(-3e-11)] + [InlineData(4e-9)] + [InlineData(-4e-9)] + public void TangentArcsMeetingEndToEnd_TouchOnlyAtTheirVertex(double delta) + { + var vertex = new Vector(10, 10); + var first = Arc(new Vector(10, 0), vertex, new Vector(10, 5 + delta)); + var second = Arc(vertex, new Vector(7, 7), new Vector(10, 7)); + + var contacts = first.Contacts(second, out var overlap); + var reversed = second.Contacts(first, out var reversedOverlap); + + Assert.False(overlap); + Assert.False(reversedOverlap); + Assert.Equal(vertex, Assert.Single(contacts)); + Assert.Equal(vertex, Assert.Single(reversed)); + } + + [Fact] + public void LineMeetingItsFilletEndToEnd_TouchesOnlyAtTheirVertex() + { + var vertex = new Vector(10, 0); + var edge = Line(new Vector(0, 0), vertex); + var fillet = Arc(vertex, new Vector(15, 5), new Vector(10, 5)); + + Assert.Equal(vertex, Assert.Single(edge.Contacts(fillet, out var overlap))); + Assert.Equal(vertex, Assert.Single(fillet.Contacts(edge, out _))); + Assert.False(overlap); + } + + [Fact] + public void ArcsMeetingEndToEnd_StillReportWhereTheyCrossAgain() + { + // The unit circle's upper half and, from the same vertex, a circle through (0, 1). + var vertex = new Vector(1, 0); + var upper = Arc(vertex, new Vector(-1, 0), new Vector(0, 0)); + var other = Arc(vertex, new Vector(0, 1), new Vector(1, 1)); + + var contacts = upper.Contacts(other, out var overlap); + + Assert.False(overlap); + Assert.Equal(2, contacts.Count); + Assert.Contains(contacts, p => p.DistanceTo(vertex) <= 1e-9); + Assert.Contains(contacts, p => p.DistanceTo(new Vector(0, 1)) <= 1e-9); + } + + [Fact] + public void LineMeetingAnArcEndToEnd_StillReportsTheChordsFarEnd() + { + // A chord of the unit circle from (1, 0) to (0, 1), then the circle's upper three quarters + // from (1, 0) through (0, 1): they share (1, 0) and cross at (0, 1). + var vertex = new Vector(1, 0); + var chord = Line(new Vector(0, 1), vertex); + var arc = Arc(vertex, new Vector(0, -1), new Vector(0, 0)); + + var contacts = chord.Contacts(arc, out var overlap); + + Assert.False(overlap); + Assert.Equal(2, contacts.Count); + Assert.Contains(contacts, p => p.DistanceTo(vertex) <= 1e-9); + Assert.Contains(contacts, p => p.DistanceTo(new Vector(0, 1)) <= 1e-9); + } + + [Fact] + public void ArcsMeetingEndToEnd_IgnoreASecondCrossingOffEitherSpan() + { + // The unit circle's right quarter and, from (0, 1), a circle through (-1, 0) that the + // first arc never reaches. + var vertex = new Vector(0, 1); + var quarter = Arc(new Vector(1, 0), vertex, new Vector(0, 0)); + var other = Arc(vertex, new Vector(-2, 1), new Vector(-1, 1)); + + Assert.Equal(vertex, Assert.Single(quarter.Contacts(other, out var overlap))); + Assert.False(overlap); + } +} diff --git a/OpenNest.Tests/Diagnostics/PostVerificationAnalyzerTests.cs b/OpenNest.Tests/Diagnostics/PostVerificationAnalyzerTests.cs index 20b1063c..f88fee52 100644 --- a/OpenNest.Tests/Diagnostics/PostVerificationAnalyzerTests.cs +++ b/OpenNest.Tests/Diagnostics/PostVerificationAnalyzerTests.cs @@ -545,6 +545,39 @@ public class PostVerificationAnalyzerTests || Find(report, PostVerificationKind.Incomplete).Any()); } + [Fact] + public void Rapid_TabbedPerimeterOfTangentArcsWithALeadOutIsRetained() + { + // A D-shaped part: a half circle of radius 5 continued tangentially by a quarter circle + // of radius 3, the half circle's center off by 2e-11 as a DXF arc rounded apart from its + // neighbor is. The arcs still meet within the closure epsilon, but their circles cross + // again 2e-5 beside the joint, inside the native query's angular padding and past the + // proof's joint tolerance: read as a loop, the open perimeter was not retained. Adjacent + // curves meet exactly, so the tabbed perimeter with its perpendicular lead-out passes. + var clean = new Program(); + clean.MoveTo(0, 0); + clean.LineTo(10, 0); + clean.ArcTo(new Vector(10, 10), new Vector(10, 5 - 2e-11), RotationType.CCW); + clean.ArcTo(new Vector(7, 7), new Vector(10, 7), RotationType.CCW); + clean.LineTo(0, 7); + clean.LineTo(0, 0); + var part = new Part(new Drawing("d", clean)); + var program = new Program(); + program.MoveTo(0, -0.2); + program.Codes.Add(new LinearMove(0, 0) { Layer = LayerType.Leadin }); + program.LineTo(10, 0); + program.ArcTo(new Vector(10, 10), new Vector(10, 5 - 2e-11), RotationType.CCW); + program.ArcTo(new Vector(7, 7), new Vector(10, 7), RotationType.CCW); + program.LineTo(0, 7); + program.LineTo(0, 0.2); + program.Codes.Add(new LinearMove(-0.1, 0.2) { Layer = LayerType.Leadout }); + part.RestoreLeadInProgram(program, false); + + var report = Analyze(part, Scribe(-2, 5), Scribe(16, 5)); + + Assert.Empty(Find(report, PostVerificationKind.Incomplete)); + } + [Theory] [InlineData(true)] [InlineData(false)] diff --git a/docs/cutting-planner.md b/docs/cutting-planner.md index ef314094..c986823f 100644 --- a/docs/cutting-planner.md +++ b/docs/cutting-planner.md @@ -66,12 +66,22 @@ Nominal material validation recognizes contacts at shared endpoints of adjacent contour segments using `OpenNest.Math.Tolerance.Epsilon` (0.00001 model units). This avoids false self-intersection warnings from rounding at arc/line joints, including the contour seam. Overlapping segments and contacts away from those -joints or between separate contours still fail validation. For adjacent finite arcs -with a shared endpoint, material validation filters native contacts against both -actual arc spans: the native query's angular padding can otherwise report a -supporting-circle intersection beyond one arc's endpoint. This does not increase -the joint-distance tolerance, discard overlapping arcs, or waive a second intersection -inside both spans. Lead and rapid checks keep their conservative contact queries. +joints or between separate contours still fail validation. Two curves that meet end to +end (within the check epsilon, 1e-8) are not put to the native intersection query at +all: it is ill-conditioned at such a joint (two circles meeting at a shallow angle, as a +fillet continuing into a larger arc, or a line meeting its fillet), and lands its root up +to 1e-5 off the vertex, which no joint tolerance can tell from a cut that touches beside +the joint. `PostVerificationGeometry.Curve.Contacts` reports the shared vertex itself and +derives the pair's other intersection from it (two circles meet again at the vertex's mirror +image across the line through their centers; a line meets a circle again at the far end of +its chord), keeping it only when it lies on both finite spans. Every consumer of contacts +(material validation, lead checks, nested-part containment and the open-cut proof) sees the +same exact joint, so a part whose DXF arcs were rounded apart from their neighbors by 1e-9 +neither fails material validation nor loses its tabbed perimeter's retention proof. For +adjacent finite arcs, material validation still filters any native contact against both +actual arc spans. None of this increases the joint-distance tolerance, discards overlapping +arcs, or waives a second intersection inside both spans. Lead and rapid checks keep their +conservative contact queries. Source contour closure in material capture and emitter preparation accepts endpoint gaps up to 0.000001 model units (inches for an inch nest), with a floating-point roundoff allowance diff --git a/docs/post-verification.md b/docs/post-verification.md index a8f5e0b3..5b4b3e1c 100644 --- a/docs/post-verification.md +++ b/docs/post-verification.md @@ -153,7 +153,10 @@ they may form no loop, counting each closed contour with its leads as one point. an open end must also stay out of the gap beyond it: a lead-out never reaches ahead of the end it leaves, and a lead-in never behind the start it enters. Anything else (pieces that meet exactly, a lead that bridges or curls into a tab, cuts from edge to edge) is reported -as incomplete, needing manual review. Kerf, tab strength and other parts' cuts are not modeled. Plan +as incomplete, needing manual review. Kerf, tab strength and other parts' cuts are not modeled. +Consecutive cuts of one piece meet at their shared vertex exactly, however their arcs were +rounded (see [cutting dependencies](cutting-planner.md#cutting-dependencies)): the proof +never mistakes a fillet's joint for a loop. Plan Cutting runs the same proof, so its own [tabbed cutouts](tabbed-cutouts.md) (pieces without lead-outs) pass, and so do its tabbed perimeters followed by a lead-out that stays out of the tab. A program that still has a tabbed cutout's `Tab` motions (never planned) is