diff --git a/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs b/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs index 25910622..6776ad6d 100644 --- a/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs +++ b/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs @@ -374,15 +374,7 @@ public class ClampZoneInteractiveFillTests // A 120 x 60 sheet with four front clamps 20 apart. Nine 6.1-high rows fit only with // the front row beside the clamps, so the parts that straddle a clamp are dropped, which // opens room for one between two clamps that the fill must take. - var zones = new[] { 20.0, 40.0, 60.0, 80.0 } - .Select(x => new Box(x, 0, 1.5, 0.625)).ToArray(); - var plate = new Plate(new Size(60, 120)) - { - EdgeSpacing = new Spacing(Edge, Edge), - PartSpacing = PartSpacing, - }; - for (var i = 0; i < zones.Length; i++) - plate.ClampZones.Add(new ClampZone($"front-{i}", zones[i])); + var plate = RailPlate(out var zones); var drawing = RectDrawing(12, 6.1); var item = new NestItem { Drawing = drawing }; @@ -399,6 +391,43 @@ public class ClampZoneInteractiveFillTests Assert.Contains(parts, part => part.BoundingBox.Bottom < zones[0].Top + Edge); } + [Fact] + public void TheClampedRowLinesUpBetweenTheClamps() + { + // 8-wide parts on the clamp-blind grid straddle the clamps, and the gaps dropping them + // leaves fit at most one more. Refilled between the clamps, the front row holds two + // parts in each 18-wide space and four past the last clamp. + var plate = RailPlate(out var zones); + var unclamped = RailPlate(out _); + unclamped.ClampZones.Clear(); + var item = new NestItem { Drawing = RectDrawing(8, 6.1) }; + var demand = new List { item }; + + var parts = PlateFillService.FillDroppingClampHits( + (demanded, box, _) => PlateFillService.FillItem("Fill", unclamped, demanded, box, + null, CancellationToken.None), + plate, item, demand, plate.WorkArea(), null, CancellationToken.None); + + AssertClearOfClamps(parts, zones); + var band = PlateFillService.ClampedEdgeBand(plate, plate.WorkArea()); + Assert.Equal(12, parts.Count(part => part.BoundingBox.Bottom < band.Top)); + } + + /// A 120 x 60 sheet with four 1.5 x 0.625 front clamps 20 apart. + private static Plate RailPlate(out Box[] zones) + { + zones = new[] { 20.0, 40.0, 60.0, 80.0 } + .Select(x => new Box(x, 0, 1.5, 0.625)).ToArray(); + var plate = new Plate(new Size(60, 120)) + { + EdgeSpacing = new Spacing(Edge, Edge), + PartSpacing = PartSpacing, + }; + for (var i = 0; i < zones.Length; i++) + plate.ClampZones.Add(new ClampZone($"front-{i}", zones[i])); + return plate; + } + private sealed class InlineProgress(System.Action report) : System.IProgress { public void Report(NestProgress value) => report(value); diff --git a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs index 5762135e..d0adf63e 100644 --- a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs +++ b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs @@ -5,6 +5,7 @@ using System.Threading; using OpenNest.Engine.Fill; using OpenNest.Engine.Jobs.Placement.Fillers; using OpenNest.Geometry; +using OpenNest.Math; namespace OpenNest.Engine.Jobs.Placement; @@ -86,7 +87,7 @@ public static partial class PlateFillService if (dropped) { var before = result.Count; - FillGapsBesideClamps(fill, plate, item, demand, area, result, token); + result = RefillBesideClamps(fill, plate, item, demand, area, result, token); // The progress sink re-certifies these and promotes them only over its floor. if (result.Count > before) progress?.Report(new NestProgress @@ -100,6 +101,120 @@ public static partial class PlateFillService return result; } + /// + /// The better of two refills of a layout that lost parts at the clamps: its gaps filled + /// with every part left in place, or the clamped edge's row taken out and refilled + /// between the clamps, which lines the row up with the space the clamps leave instead + /// of the grid the clamp-blind fill chose. The repacked row wins only with more parts. + /// + private static List RefillBesideClamps( + Func, List> fill, Plate plate, + NestItem item, List demand, Box area, List layout, + CancellationToken token) + { + var gapFilled = new List(layout); + FillGapsBesideClamps(fill, plate, item, demand, area, gapFilled, token); + var band = ClampedEdgeBand(plate, area); + if (band == null || token.IsCancellationRequested) + return gapFilled; + + var repacked = layout.Where(part => !Overlaps(part.BoundingBox, band)).ToList(); + if (repacked.Count == layout.Count) + return gapFilled; + foreach (var slot in ClampedEdgeSlots(plate, area, band, repacked)) + { + if (token.IsCancellationRequested + || (item.Quantity > 0 && repacked.Count >= item.Quantity)) + break; + if (!Fits(slot, item.Drawing.Program.BoundingBox())) + continue; + var remaining = item.Quantity > 0 ? item.Quantity - repacked.Count : item.Quantity; + AddClear(repacked, fill(CopyDemand(item, remaining), slot, null), plate, slot, demand); + } + FillGapsBesideClamps(fill, plate, item, demand, area, repacked, token); + return repacked.Count > gapFilled.Count ? repacked : gapFilled; + } + + /// + /// The free space along the clamped edge once its row is taken out, one slot between each + /// pair of clamp keep-outs (and one past each end), each running from the edge to the + /// nearest part beyond the band at the part spacing. Slots never overlap, so filling one + /// never steals another's space. + /// + private static List ClampedEdgeSlots(Plate plate, Box area, Box band, + List remaining) + { + var alongX = band.Length >= area.Length - Tolerance.Epsilon; + var keepOuts = InteractiveClamps.ClearanceRectangles(plate) + .Where(keepOut => Overlaps(keepOut, band)) + .Select(keepOut => alongX ? (Low: keepOut.Left, High: keepOut.Right) + : (Low: keepOut.Bottom, High: keepOut.Top)) + .OrderBy(span => span.Low); + var occupied = plate.Parts.Where(part => !part.BaseDrawing.IsCutOff) + .Concat(remaining).Select(part => part.BoundingBox).ToList(); + var spacing = plate.PartSpacing; + var slots = new List(); + + void Add(double low, double high) + { + if (high - low <= Tolerance.Epsilon) + return; + var beside = occupied.Where(box => alongX + ? box.Left < high && box.Right > low + : box.Bottom < high && box.Top > low).ToList(); + Box slot; + if (alongX && band.Bottom <= area.Bottom) + { + var top = beside.Select(box => box.Bottom - spacing).Append(area.Top).Min(); + slot = new Box(low, area.Bottom, high - low, top - area.Bottom); + } + else if (alongX) + { + var bottom = beside.Select(box => box.Top + spacing).Append(area.Bottom).Max(); + slot = new Box(low, bottom, high - low, area.Top - bottom); + } + else if (band.Left <= area.Left) + { + var right = beside.Select(box => box.Left - spacing).Append(area.Right).Min(); + slot = new Box(area.Left, low, right - area.Left, high - low); + } + else + { + var left = beside.Select(box => box.Right + spacing).Append(area.Left).Max(); + slot = new Box(left, low, area.Right - left, high - low); + } + if (slot.Length > 0 && slot.Width > 0) + slots.Add(slot); + } + + var start = alongX ? area.Left : area.Bottom; + foreach (var span in keepOuts) + { + Add(start, span.Low); + start = System.Math.Max(start, span.High); + } + Add(start, alongX ? area.Right : area.Top); + return slots; + } + + /// + /// The strip of between its clamped edge and the far side of the + /// clamp keep-outs, or null when finds no single clamped edge. + /// + internal static Box ClampedEdgeBand(Plate plate, Box area) + { + var free = ClampFreeArea(plate, area); + if (free == null) + return null; + if (free.Bottom > area.Bottom) + return new Box(area.Left, area.Bottom, area.Length, free.Bottom - area.Bottom); + if (free.Top < area.Top) + return new Box(area.Left, free.Top, area.Length, area.Top - free.Top); + if (free.Left > area.Left) + return new Box(area.Left, area.Bottom, free.Left - area.Left, area.Width); + return new Box(free.Right, area.Bottom, area.Right - free.Right, area.Width); + } + /// Gap-fill rounds after dropping clamp hits; a round that adds nothing ends it. private const int MaxGapFillRounds = 3;