Files
OpenNest/OpenNest.Tests/CuttingStrategy/TabbedLeadOutTests.cs
T
ajandClaude Opus 5.5 5776fa80c9 fix(cutting): leave out a tabbed outside's lead-out that would cut its tab free
A tab stops the outside cut short of its entry, and the lead-out leaves from
there. On a counterclockwise outside a line lead-out angles forward, toward
the tab: a 0.1 lead-out at 60 degrees runs 0.05 along the edge, past a 0.03
tab, and crosses the lead-in, so the two cuts sever the tab. The released-
contour check rightly refused every such part, so a plate of support-tabbed
strips found no cut plan at all.

The emitter now drops a tabbed outside's lead-out that touches the lead-in or
passes within a kerf of it, as tabbed cutouts and breakoffs already end at
their tabs. Lead-outs that stay clear and untabbed contours are unchanged.
The tests that expected the crossing leads now expect them left out, and a
new test keeps the check's refusal of a crossing lead-out covered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:41:17 -05:00

229 lines
11 KiB
C#

using OpenNest.CNC;
using OpenNest.CNC.CuttingPlanning;
using OpenNest.CNC.CuttingStrategy;
using OpenNest.Geometry;
using OpenNest.Tests.CuttingPlanning;
namespace OpenNest.Tests.CuttingStrategy;
/// <summary>
/// A tab trims the perimeter short of its entry, so the lead-out leaves from where the
/// trimmed cut actually ends. Generating it from the nominal entry emitted an arc whose
/// start was off its own radius. A lead-out that would reach the lead-in across the tab gap
/// would cut the tab free, so the cut then stops at its tab with no lead-out.
/// </summary>
public class TabbedLeadOutTests
{
private const double TabSize = 0.2;
private const double LeadRadius = 0.2;
[Fact]
public void ArcLeadOut_LeavesTangentlyFromTheTrimmedCutEnd()
{
var execution = Emit(ExplicitContourTests.Square(false), new Vector(-2, 5),
new ArcLeadOut { Radius = LeadRadius }, tabs: true);
var (cuts, leadout) = Split(execution);
var end = cuts[^1].End;
Assert.Equal(0, end.X, 9);
Assert.Equal(TabSize, System.Math.Abs(end.Y - 5), 9);
Assert.Equal(end, leadout.Start!.Value);
var arc = Assert.IsType<Arc>(leadout.Curve.ToEntity());
Assert.Equal(LeadRadius, arc.Radius, 9);
// Centered on the actual end's outward normal, so it touches the edge only where it starts.
Assert.Equal(-LeadRadius, arc.Center.X, 9);
Assert.Equal(end.Y, arc.Center.Y, 9);
}
[Fact]
public void ArcLeadOut_ThatWouldCrossTheLeadIn_IsLeftOut()
{
// Wound the other way, the arc runs forward over the tab gap and through the lead-in.
var execution = Emit(ExplicitContourTests.Square(true), new Vector(-2, 5),
new ArcLeadOut { Radius = LeadRadius }, tabs: true);
var cuts = execution.Motions.Where(m => m.Layer == LayerType.Display && !m.Rapid).ToArray();
Assert.Equal(TabSize, System.Math.Abs(cuts[^1].End.Y - 5), 9);
Assert.DoesNotContain(execution.Motions, m => m.Layer == LayerType.Leadout);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void LineLeadOut_AngledIntoASmallTab_IsLeftOutOnlyWhenItWouldCrossTheLeadIn(bool reversed)
{
// A strip tabbed for support with 60 degree leads and a tab shorter than the lead-out's
// forward run: wound one way the lead-out angles away from the tab, the other way it
// crosses the lead-in and would cut the tab free.
var clean = new Program(Mode.Absolute);
if (reversed)
{
clean.MoveTo(0, 0); clean.LineTo(11.8, 0); clean.LineTo(11.8, 3.8); clean.LineTo(0, 3.8); clean.LineTo(0, 0);
}
else
{
clean.MoveTo(0, 0); clean.LineTo(0, 3.8); clean.LineTo(11.8, 3.8); clean.LineTo(11.8, 0); clean.LineTo(0, 0);
}
var parameters = new CuttingParameters
{
Kerf = 0.014,
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(),
TabsEnabled = true,
TabConfig = new NormalTab { Size = 0.03 },
};
var emitted = new ContourCuttingStrategy { Parameters = parameters }.Apply(clean, new Vector(5, -2));
var execution = ExecutionMotionReader.Read(emitted.Program, Vector.Zero, null, default);
var leadIn = Assert.Single(execution.Motions.Where(m => m.Layer == LayerType.Leadin));
var leadOuts = execution.Motions.Where(m => m.Layer == LayerType.Leadout).ToArray();
if (reversed)
Assert.Empty(leadOuts);
else
{
var leadOut = Assert.Single(leadOuts);
Assert.False(new Line(leadIn.Start!.Value, leadIn.End).Intersects(new Line(leadOut.Start!.Value, leadOut.End)));
}
Assert.Empty(new ReleasedContourState().Check(execution, new Vector(5, -2), 1));
}
[Fact]
public void ArcLeadOut_OnCurvedPerimeter_IsTangentAtTheTrimmedEnd()
{
// Half disc: the entry is on the arc, so the trimmed end's radial differs from the entry's.
var clean = new Program();
clean.MoveTo(0, 5); clean.LineTo(10, 5); clean.ArcTo(0, 5, 5, 5, RotationType.CW);
var execution = Emit(clean, new Vector(5, -2), new ArcLeadOut { Radius = LeadRadius }, tabs: true);
var (cuts, leadout) = Split(execution);
var center = new Vector(5, 5);
var end = cuts[^1].End;
Assert.Equal(5, end.DistanceTo(center), 9);
Assert.Equal(TabSize, end.DistanceTo(new Vector(5, 0)), 9);
Assert.Equal(end, leadout.Start!.Value);
var arc = Assert.IsType<Arc>(leadout.Curve.ToEntity());
Assert.Equal(LeadRadius, arc.Radius, 9);
// Externally tangent to the perimeter at the actual end: center on its radial line.
Assert.Equal(5 + LeadRadius, arc.Center.DistanceTo(center), 9);
var radial = end - center;
var offset = arc.Center - end;
Assert.Equal(0, radial.X * offset.Y - radial.Y * offset.X, 9);
}
[Fact]
public void LineLeadOut_RunsAlongTheEndNormalFromTheTrimmedCutEnd()
{
var execution = Emit(ExplicitContourTests.Square(false), new Vector(-2, 5),
new LineLeadOut { Length = 0.3 }, tabs: true);
var (cuts, leadout) = Split(execution);
Assert.Equal(cuts[^1].End, leadout.Start!.Value);
Assert.Equal(-0.3, leadout.End.X, 9);
Assert.Equal(cuts[^1].End.Y, leadout.End.Y, 9);
}
[Fact]
public void UntabbedArcLeadOut_StillLeavesFromTheEntry()
{
var execution = Emit(ExplicitContourTests.Square(false), new Vector(-2, 5),
new ArcLeadOut { Radius = LeadRadius }, tabs: false);
var (cuts, leadout) = Split(execution);
Assert.Equal(new Vector(0, 5), cuts[^1].End);
var arc = Assert.IsType<Arc>(leadout.Curve.ToEntity());
Assert.Equal(-LeadRadius, arc.Center.X, 9);
Assert.Equal(5, arc.Center.Y, 9);
}
[Fact]
public void LineLeads_TabWrappingAFilletedCorner_ShareTheCornerBisector()
{
// Entered at the fillet joint the tab trims back around: on its own edge's normal the
// lead-in and lead-out would sit 150 degrees apart, not the 60 degree V of a straight edge.
const double fillet = 0.015;
const double tab = 0.03;
var clean = new Program(Mode.Absolute);
clean.MoveTo(0, 0); clean.LineTo(0, 10 - fillet);
clean.ArcTo(fillet, 10, fillet, 10 - fillet, RotationType.CW);
clean.LineTo(10, 10); clean.LineTo(10, 0); clean.LineTo(0, 0);
var part = new Part(new Drawing("tabbed-fillet", clean));
var perimeter = LeadInTestHelpers.Profile(part).Perimeter;
var entry = new Vector(fillet, 10);
var entity = perimeter.Entities.OfType<Line>().First(l => l.StartPoint.DistanceTo(entry) < 1e-9);
var parameters = new CuttingParameters
{
ExternalLeadIn = new LineLeadIn { Length = 0.15, ApproachAngle = 60 },
ExternalLeadOut = new LineLeadOut { Length = 0.1, ApproachAngle = 60 },
TabsEnabled = true,
TabConfig = new NormalTab { Size = tab },
};
part.ApplySingleLeadIn(parameters, entry, entity, ContourType.External);
var geometry = part.Program.ToGeometry();
var leadIn = Assert.IsType<Line>(Assert.Single(geometry.Where(e => e.Layer == SpecialLayers.Leadin)));
var leadOut = Assert.IsType<Line>(Assert.Single(geometry.Where(e => e.Layer == SpecialLayers.Leadout)));
LeadInTestHelpers.AssertPoint(entry, leadIn.EndPoint);
Assert.Equal(tab, leadOut.StartPoint.DistanceTo(entry), 9);
// The cut stops on the other edge, past the corner.
Assert.Equal(0, leadOut.StartPoint.X, 9);
const double bisector = 3 * System.Math.PI / 4;
var spread = OpenNest.Math.Angle.ToRadians(30);
LeadInTestHelpers.AssertPoint(entry + Polar(0.15, bisector - spread), leadIn.StartPoint);
LeadInTestHelpers.AssertPoint(leadOut.StartPoint + Polar(0.1, bisector + spread), leadOut.EndPoint);
}
[Fact]
public void LineLeads_TabWrappingAFilletedCornerTheOtherWay_LeaveOutTheLeadOutThatCrossesTheLeadIn()
{
// Wound the other way, the shared bisector puts the lead-out across the lead-in.
const double fillet = 0.015;
var clean = new Program(Mode.Absolute);
clean.MoveTo(fillet, 10); clean.ArcTo(0, 10 - fillet, fillet, 10 - fillet, RotationType.CCW);
clean.LineTo(0, 0); clean.LineTo(10, 0); clean.LineTo(10, 10); clean.LineTo(fillet, 10);
var part = new Part(new Drawing("tabbed-fillet", clean));
var perimeter = LeadInTestHelpers.Profile(part).Perimeter;
var entry = new Vector(0, 10 - fillet);
var entity = perimeter.Entities.OfType<Line>().First(l => l.StartPoint.DistanceTo(entry) < 1e-9);
var parameters = new CuttingParameters
{
ExternalLeadIn = new LineLeadIn { Length = 0.15, ApproachAngle = 60 },
ExternalLeadOut = new LineLeadOut { Length = 0.1, ApproachAngle = 60 },
TabsEnabled = true,
TabConfig = new NormalTab { Size = 0.03 },
};
part.ApplySingleLeadIn(parameters, entry, entity, ContourType.External);
var geometry = part.Program.ToGeometry();
var leadIn = Assert.IsType<Line>(Assert.Single(geometry.Where(e => e.Layer == SpecialLayers.Leadin)));
LeadInTestHelpers.AssertPoint(entry, leadIn.EndPoint);
LeadInTestHelpers.AssertPoint(entry + Polar(0.15, 3 * System.Math.PI / 4 - OpenNest.Math.Angle.ToRadians(30)),
leadIn.StartPoint);
Assert.DoesNotContain(geometry, e => e.Layer == SpecialLayers.Leadout);
}
private static Vector Polar(double length, double angle) =>
new(length * System.Math.Cos(angle), length * System.Math.Sin(angle));
private static OwnedExecution Emit(Program clean, Vector approach, LeadOut leadOut, bool tabs)
{
var parameters = ExplicitContourTests.Parameters();
parameters.TabsEnabled = tabs;
parameters.TabConfig = new NormalTab { Size = TabSize };
parameters.ExternalLeadOut = leadOut;
var emitted = new ContourCuttingStrategy { Parameters = parameters }.Apply(clean, approach);
return ExecutionMotionReader.Read(emitted.Program, Vector.Zero, null, default);
}
private static (ExecutionMotion[] Cuts, ExecutionMotion Leadout) Split(OwnedExecution execution)
{
var cuts = execution.Motions.Where(m => m.Layer == LayerType.Display && !m.Rapid).ToArray();
Assert.NotEmpty(cuts);
return (cuts, Assert.Single(execution.Motions.Where(m => m.Layer == LayerType.Leadout)));
}
}