From 1972aa17c5a8c7d04e1858fa0d64bce4cae4f106 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sun, 22 Nov 2026 20:32:02 -0500 Subject: [PATCH] fix(geometry): give tangent circles their tangent point, never NaN Circle-circle intersection passed its containment test for internally tangent circles while roundoff left r^2 - a^2 a hair below zero, so the tangent point came back NaN. Material capture refused such parts as "nonfinite program coordinates" and Plan Cutting fell back to a best-effort plan: N26-2JJ's crescents (an inner arc tangent to the outer one) failed whenever they were rotated 180 degrees, but only on the live DWG geometry, since saving rounds the program and hides it. Clamp r^2 - a^2 at zero. The externally tangent lead in NativeContactRegressionTests is now refused on its contact rather than as an incomplete check. The problem-nest corpus is identical to master. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Core/Geometry/Intersect.cs | 4 +- .../NativeContactRegressionTests.cs | 7 +- .../TangentArcCrescentTests.cs | 79 +++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 OpenNest.Tests/CuttingPlanning/TangentArcCrescentTests.cs diff --git a/OpenNest.Core/Geometry/Intersect.cs b/OpenNest.Core/Geometry/Intersect.cs index 9057498e..0593cb9a 100644 --- a/OpenNest.Core/Geometry/Intersect.cs +++ b/OpenNest.Core/Geometry/Intersect.cs @@ -156,7 +156,9 @@ namespace OpenNest.Geometry - circle2.Radius * circle2.Radius + distance * distance ) / (2.0 * distance); - var h = System.Math.Sqrt(circle1.Radius * circle1.Radius - a * a); + // The checks above make r^2 - a^2 nonnegative in exact arithmetic. For tangent circles + // roundoff can leave it a hair below zero: that is the tangent point, not NaN. + var h = System.Math.Sqrt(System.Math.Max(circle1.Radius * circle1.Radius - a * a, 0)); var pt = new Vector( circle1.Center.X + (a * d.X) / distance, diff --git a/OpenNest.Tests/CuttingPlanning/NativeContactRegressionTests.cs b/OpenNest.Tests/CuttingPlanning/NativeContactRegressionTests.cs index 02bbb685..e0166ce2 100644 --- a/OpenNest.Tests/CuttingPlanning/NativeContactRegressionTests.cs +++ b/OpenNest.Tests/CuttingPlanning/NativeContactRegressionTests.cs @@ -8,8 +8,11 @@ namespace OpenNest.Tests.CuttingPlanning; public class NativeContactRegressionTests { + // The lead arc's circle and the obstacle's are externally tangent at the witness. Their + // supporting-circle query once gave NaN there, which native arc filters would have dropped as + // no contact; it now gives the tangent point, so the lead is refused on the contact itself. [Fact] - public void TangentArcLead_RefusesFilteredSupportingCircleNaNs() + public void TangentArcLead_RefusesTheTangentContact() { var target = LeadMaterialSnapshot.Capture(LeadPathValidationTests.Rectangle(0, -1, 1, 1), Vector.Zero); var obstacle = new Program(); @@ -27,7 +30,7 @@ public class NativeContactRegressionTests Assert.True(execution.Motions[1].Curve.Contains(witness)); Assert.True(material.Rings[0][0].Contains(witness)); var result = LeadPathValidator.Check(execution, target, [material]); - Assert.False(result.IsComplete); + Assert.True(result.IsComplete); Assert.False(result.IsClear); } diff --git a/OpenNest.Tests/CuttingPlanning/TangentArcCrescentTests.cs b/OpenNest.Tests/CuttingPlanning/TangentArcCrescentTests.cs new file mode 100644 index 00000000..1feba942 --- /dev/null +++ b/OpenNest.Tests/CuttingPlanning/TangentArcCrescentTests.cs @@ -0,0 +1,79 @@ +using OpenNest.CNC; +using OpenNest.CNC.CuttingStrategy; +using OpenNest.Engine.CuttingPlanning; +using OpenNest.Geometry; + +namespace OpenNest.Tests.CuttingPlanning; + +// N26-2JJ: a crescent imported from a DWG, an r 5 outer arc and an r 4.2819 inner arc whose +// circle is internally tangent to the outer one (their centers are the radii's difference apart), +// joined by two r 0.125 corner fillets. Rotated 180 degrees, roundoff let the circle-circle query +// pass its containment test with r^2 - a^2 slightly negative, so the tangent point came back NaN, +// material capture refused the part as "nonfinite program coordinates", and Plan Cutting fell back +// to a best-effort plan. The same drawing saved and reopened (its program rounded to 10 decimals) +// planned Ready, so only the live, unrounded drawing failed. +public class TangentArcCrescentTests +{ + // The incremental program CadImporter built from the DWG, bit for bit (G91; centers relative + // to each arc's start). Saving rounds it to 10 decimals, which hides the failure. + private static Drawing Crescent() + { + var program = new Program(Mode.Incremental); + program.Codes.Add(new RapidMove(0, 0)); + program.Codes.Add(new ArcMove(-0.1894982776402756, 0.16240295780501235, + -0.08997074872414146, 0.08677709590679505, RotationType.CW)); + program.Codes.Add(new ArcMove(-6.818663342650833, 0, + -3.4093316713254147, 2.5905761847873965, RotationType.CCW)); + program.Codes.Add(new ArcMove(-0.1894982776402756, -0.16240295780501235, + -0.09952752891613414, -0.0756258618982173, RotationType.CW)); + program.Codes.Add(new ArcMove(7.197659897931384, -3.552713678800501E-15, + 3.598829948965694, 3.4710838362718306, RotationType.CW)); + return new Drawing("Crescent", program); + } + + private static CuttingParameters Parameters() => new() + { + ExternalLeadIn = new LineLeadIn { Length = 0.15, ApproachAngle = 60 }, + ExternalLeadOut = new LineLeadOut { Length = 0.1, ApproachAngle = 60 }, + InternalLeadIn = new LineLeadIn { Length = 0.15, ApproachAngle = 60 }, + InternalLeadOut = new NoLeadOut(), + PierceClearance = 0.0625 + }; + + [Theory] + [InlineData(0, 8.848829948965689, 2.699876137818065)] + [InlineData(System.Math.PI, 6.776175051034306, 18.518801312399)] + public void TangentInnerArc_PlansReadyAtEitherRotation(double rotation, double x, double y) + { + var nest = new Nest(); + var plate = nest.CreatePlate(); + plate.Size = new Size(60, 120); + var part = new Part(Crescent()); + part.Rotate(rotation); + part.Offset(new Vector(x, y)); + plate.Parts.Add(part); + + var proposal = CuttingPlanBatch.Capture([plate], Parameters(), preservePartOrder: false).Plan(); + + var planned = Assert.Single(proposal.Plates); + Assert.True(planned.IsReady, string.Join("\n", proposal.Describe("in"))); + } + + // The rotated crescent's two supporting circles as material capture builds them. Queried with + // the outer circle first, r^2 - a^2 is -1.07e-14: the tangent point, never NaN. + [Fact] + public void InternallyTangentCircles_GiveTheFiniteTangentPointEitherWay() + { + var outer = new Circle(new Vector(10.375004999999996, 15.047717476127165), 4.999999999999998); + var inner = new Circle(new Vector(10.375004999999996, 15.765822169806588), 4.281895306320575); + var tangent = new Vector(outer.Center.X, outer.Center.Y + outer.Radius); + + foreach (var (first, second) in new[] { (outer, inner), (inner, outer) }) + { + Assert.True(first.Intersects(second, out var points)); + var point = Assert.Single(points); + Assert.True(double.IsFinite(point.X) && double.IsFinite(point.Y)); + Assert.True(point.DistanceTo(tangent) < 1e-9, $"{point} is not the tangent point"); + } + } +}