diff --git a/OpenNest.Core/Support/SlugSlicer.cs b/OpenNest.Core/Support/SlugSlicer.cs index 6923000b..d27413ab 100644 --- a/OpenNest.Core/Support/SlugSlicer.cs +++ b/OpenNest.Core/Support/SlugSlicer.cs @@ -46,7 +46,11 @@ namespace OpenNest.Support /// least from the hole, is longer than it, and is pierced /// along itself from its start (at most halfway) where the pierce /// keeps from the hole; a slice whose pierce fits at - /// neither end is left out. Rows (lines along X) come first, then columns, each set from + /// neither end is left out. So is a slice that would only cut a strip off the slug next to + /// the hole's side, narrower all along it than the bed's points keep from the line + /// (): the strip rests on nothing, so the slice + /// separates no contacts and would only cut close beside the part's edge, overheating it + /// and leaving a sliver loose to flip up. Rows (lines along X) come first, then columns, each set from /// its end nearer the last slice, and each slice starts at its end nearer where the one /// before it ended, so the head runs back and forth. /// @@ -75,11 +79,16 @@ namespace OpenNest.Support var bounds = Bounds(ring); var world = new Box(bounds.Left + location.X, bounds.Bottom + location.Y, bounds.Length, bounds.Width); var lines = bed.SliceLines(world, quadrant); - // Each line's pieces inside the eroded hole, local; lines that miss it are dropped. + var outline = new PathsD { ToPath(ring) }; + // Each line's pieces inside the eroded hole, local, but for those that cut off only a + // strip resting on nothing; lines left with none are dropped. var groups = new[] { true, false }.Select(alongX => lines .Where(line => line.AlongX == alongX) .OrderBy(line => line.At) - .Select(line => Segments(eroded, line, location, standoff)) + .Select(line => Segments(eroded, line, location, standoff) + .Where(segment => !CutsOffABareStrip(outline, segment, line.AlongX, + bed.SliceLineClearance(line.AlongX), standoff)) + .ToList()) .Where(segments => segments.Count > 0) .ToList()) .ToArray(); @@ -142,6 +151,49 @@ namespace OpenNest.Support : null; } + // Whether the slice cuts off, on one side, only a strip of the slug nearer the line than + // `clearance` everywhere: the hole split along the slice's whole chord leaves, on that + // side, only pieces that close. No contact lies that near a slice line, so the strip + // rests on nothing and leaving the slice out only joins it to the piece beside it. Other + // slices only split the strip further, so they are not needed to show it is bare. + private static bool CutsOffABareStrip(PathsD outline, (Vector A, Vector B) segment, bool alongX, + double clearance, double standoff) + { + if (!(clearance > standoff)) + return false; + var at = alongX ? segment.A.Y : segment.A.X; + var low = alongX ? segment.A.X : segment.A.Y; + var high = alongX ? segment.B.X : segment.B.Y; + // The chord of the hole the slice lies on: its kerf meets the cutout's at both ends. + var chord = Spans(outline, alongX, at).FirstOrDefault(span => span.Low <= low && span.High >= high); + if (chord == default) + return false; + const double cut = 1e-3; + var from = chord.Low - cut; + var to = chord.High + cut; + var cutter = alongX + ? new PathD { new(from, at - cut), new(to, at - cut), new(to, at + cut), new(from, at + cut) } + : new PathD { new(at - cut, from), new(at + cut, from), new(at + cut, to), new(at - cut, to) }; + var pieces = Clipper.Difference(outline, new PathsD { cutter }, FillRule.NonZero, ClipperBridge.Precision); + // How far the pieces along the chord reach from it, below and above. + var reach = new double?[2]; + foreach (var piece in pieces) + { + // Offsets from the slice line across it, and positions along it. + var across = piece.Select(p => (alongX ? p.y : p.x) - at).ToArray(); + var along = piece.Select(p => alongX ? p.x : p.y).ToArray(); + if (along.Max() < from || along.Min() > to || across.Min(d => System.Math.Abs(d)) > 2 * cut) + continue; + // A piece on both sides joins them around the chord (another of the hole's + // crossings of the line): the slice does not cut anything off there. + if (across.Min() < 0 && across.Max() > 0) + return false; + var side = across.Max() > 0 ? 1 : 0; + reach[side] = System.Math.Max(reach[side] ?? 0, across.Max(d => System.Math.Abs(d))); + } + return reach.Any(r => r < clearance); + } + // Where the line at `at` (Y along X, or X along Y) runs inside the eroded paths, sorted. private static List<(double Low, double High)> Spans(PathsD paths, bool alongX, double at) { diff --git a/OpenNest.Core/Support/SupportBed.cs b/OpenNest.Core/Support/SupportBed.cs index 116b1194..fa63dd64 100644 --- a/OpenNest.Core/Support/SupportBed.cs +++ b/OpenNest.Core/Support/SupportBed.cs @@ -61,6 +61,13 @@ namespace OpenNest.Support /// public abstract IReadOnlyList SliceLines(Box region, int quadrant); + /// + /// How far every contact stays from a slice line along X () or Y, + /// wherever the sheet is registered within its tolerance: material nearer a line than this + /// rests on nothing. 0 when a contact may lie on the line (a slat's center line). + /// + public virtual double SliceLineClearance(bool alongX) => 0; + /// This layout with every length multiplied by (a unit change). public abstract SupportBed Scaled(double factor); diff --git a/OpenNest.Core/Support/SupportGrid.cs b/OpenNest.Core/Support/SupportGrid.cs index 4011ab9c..b793a78e 100644 --- a/OpenNest.Core/Support/SupportGrid.cs +++ b/OpenNest.Core/Support/SupportGrid.cs @@ -91,6 +91,10 @@ namespace OpenNest.Support return points; } + /// Half a pitch, less the registration tolerance the points may shift by. + public override double SliceLineClearance(bool alongX) => + System.Math.Max(0, (alongX ? PitchY : PitchX) / 2 - RegistrationTolerance); + /// /// Every line midway between two neighboring rows or columns of points that crosses /// : a cut along one stays half a pitch from every point, so it diff --git a/OpenNest.Tests/Support/SlugSliceTests.cs b/OpenNest.Tests/Support/SlugSliceTests.cs index 2f61b35d..cb9173fa 100644 --- a/OpenNest.Tests/Support/SlugSliceTests.cs +++ b/OpenNest.Tests/Support/SlugSliceTests.cs @@ -130,6 +130,55 @@ public class SlugSliceTests Assert.Empty(SlugSlicer.Slices(Bed, 1, Slot, Location, Kerf, reach: 0.1, pierceClearance: 0.2)); } + // Where each slice lies in world coordinates: "X=5" for a column, "Y=5" for a row. + private static string[] Lines(IEnumerable slices) => slices + .Select(s => s.Start.X == s.End.X + ? $"X={System.Math.Round(s.Start.X + Location.X, 6)}" + : $"Y={System.Math.Round(s.Start.Y + Location.Y, 6)}") + .Order() + .ToArray(); + + [Fact] + public void SliceLineClearance_IsHalfAPitchLessTheRegistrationTolerance() + { + Assert.Equal(1, Bed.SliceLineClearance(true)); + Assert.Equal(0.875, new SupportGrid(2, 3, registrationTolerance: 0.125).SliceLineClearance(false)); + Assert.Equal(1.375, new SupportGrid(2, 3, registrationTolerance: 0.125).SliceLineClearance(true)); + // A slat's center line is a slice line, so its contacts lie on it. + Assert.Equal(0, new SupportSlats(SlatAxis.Y, 3, 0.25).SliceLineClearance(false)); + } + + [Fact] + public void Slices_LeaveOutASliceThatCutsOffOnlyAStripRestingOnNothing() + { + // World X 4.9375-8.9, Y 3.5-7.5 on points at even X and Y. The column midline at X = 5 + // runs 1/16 inside the left side and the row midline at Y = 7 half an inch below the top: + // the strips they cut off hold no point. X = 7 and Y = 5 divide points. + Vector[] world = [new(4.9375, 3.5), new(8.9, 3.5), new(8.9, 7.5), new(4.9375, 7.5)]; + var hole = world.Select(v => v - Location).ToArray(); + + var slices = SlugSlicer.Slices(Bed, 1, hole, Location, Kerf, reach: 0.3, pierceClearance: 0.0625); + + Assert.Equal(["X=7", "Y=5"], Lines(slices)); + } + + [Fact] + public void Slices_KeepASliceWhoseStripHoldsAPoint_EvenWherePartOfTheCutoutComesClose() + { + // World X 3.5-8.9, Y 3.5-7.5, with a 0.2 long bump 1/16 short of the column midline at + // X = 5: left of that slice the slug still reaches the points at X = 4. + Vector[] world = + [ + new(3.5, 3.5), new(8.9, 3.5), new(8.9, 7.5), new(3.5, 7.5), + new(3.5, 6.0), new(4.9375, 6.0), new(4.9375, 5.8), new(3.5, 5.8), + ]; + var hole = world.Select(v => v - Location).ToArray(); + + var slices = SlugSlicer.Slices(Bed, 1, hole, Location, Kerf, reach: 0.3, pierceClearance: 0.0625); + + Assert.Contains("X=5", Lines(slices)); + } + [Fact] public void PlanCutting_SlicesATippingSlugBeforeItsCutout() { diff --git a/docs/support-tabs.md b/docs/support-tabs.md index 33826415..c48baff0 100644 --- a/docs/support-tabs.md +++ b/docs/support-tabs.md @@ -220,6 +220,14 @@ between the supports instead of the whole slug tipping. cutout chorded at 0.001, eroded with round corners), so the pieces stay attached to the part until the cutout itself is cut, its kerf meets the slices' ends and they all drop together. No kerf, no slices. A slice no longer than the kerf is left out. +- Strips that rest on nothing: a slice that would cut off only a strip of the slug lying nearer + its line than the bed's points ever come (`SupportBed.SliceLineClearance`: half the pitch + across that line, less the registration tolerance) is left out. The strip is found by + splitting the hole along the slice's whole chord, so one that opens into wider material + beyond the slice's ends keeps the slice. Such a slice separates no points: without it the + strip joins the piece beside it, still on one point, and the head does not cut close + alongside the part's edge, which overheats it and leaves a sliver loose to flip up. A slat's + center line holds contacts, so slat slices are never left out this way. - Which cutouts: one that holds another of the part's cuts (a contour, break cut or tabbed cutout), or whose box holds another part's box (a part nested in it), is never sliced, since a slice would cut what is inside. Tabbed cutouts keep their slugs, so they are never sliced.