diff --git a/OpenNest.Engine/Fill/PairFiller.cs b/OpenNest.Engine/Fill/PairFiller.cs index 5dfb98f0..27fb9243 100644 --- a/OpenNest.Engine/Fill/PairFiller.cs +++ b/OpenNest.Engine/Fill/PairFiller.cs @@ -613,9 +613,12 @@ namespace OpenNest.Engine.Fill int Bound(List gridParts) => gridParts.Count + remnantFiller.EstimateUpperBound(gridParts, workArea, maxUtilization, partArea); - List WithRemnant(List gridParts) + // The remnant's top and right strips are combined only when together they could lift + // the grid past toBeat parts. + List WithRemnant(List gridParts, int toBeat) { - var remnantParts = remnantFiller.Fill(gridParts, drawing, workArea, token, bestFits); + var remnantParts = remnantFiller.Fill(gridParts, drawing, workArea, token, bestFits, + combinedToBeat: toBeat - gridParts.Count); if (remnantParts == null || remnantParts.Count == 0) return gridParts; @@ -666,7 +669,7 @@ namespace OpenNest.Engine.Fill if (best != null && Bound(gridParts) <= best.Count) break; // sorted descending, so remaining are even smaller - var total = WithRemnant(gridParts); + var total = WithRemnant(gridParts, System.Math.Max(best?.Count ?? 0, minCountToBeat)); if (comparer.IsBetter(total, best, workArea)) best = total; } @@ -686,7 +689,7 @@ namespace OpenNest.Engine.Fill if (Bound(wholeParts) <= toBeat) continue; - var total = WithRemnant(wholeParts); + var total = WithRemnant(wholeParts, toBeat); if (total.Count > toBeat && comparer.IsBetter(total, best, workArea)) best = total; } diff --git a/OpenNest.Engine/Fill/PairRemnantFiller.cs b/OpenNest.Engine/Fill/PairRemnantFiller.cs index dcadfd40..6d204704 100644 --- a/OpenNest.Engine/Fill/PairRemnantFiller.cs +++ b/OpenNest.Engine/Fill/PairRemnantFiller.cs @@ -89,7 +89,8 @@ namespace OpenNest.Engine.Fill Drawing drawing, Box workArea, CancellationToken token, - List bestFits = null + List bestFits = null, + int combinedToBeat = 0 ) { var gridBox = ((IEnumerable)gridParts).GetBoundingBox(); @@ -103,31 +104,98 @@ namespace OpenNest.Engine.Fill List bestRemnant = null; + void Consider(List parts) + { + if (parts != null && parts.Count > (bestRemnant?.Count ?? 0)) + bestRemnant = parts; + } + // Try top remnant (full width, above grid) var topY = gridBox.Top + partSpacing; var topLength = workArea.Top - topY; - if (topLength >= minDim) + var hasTop = topLength >= minDim; + List top = null; + if (hasTop) { - var topBox = new Box(workArea.X, topY, workArea.Length, topLength); - var parts = FillRemnantBox(drawing, topBox, bestFits, token); - if (parts != null && parts.Count > (bestRemnant?.Count ?? 0)) - bestRemnant = parts; + top = FillRemnantBox(drawing, new Box(workArea.X, topY, workArea.Length, topLength), bestFits, token); + Consider(top); } // Try right remnant (full height, right of grid) var rightX = gridBox.Right + partSpacing; var rightWidth = workArea.Right - rightX; - if (rightWidth >= minDim) + var hasRight = rightWidth >= minDim; + List right = null; + if (hasRight) { - var rightBox = new Box(rightX, workArea.Y, rightWidth, workArea.Width); - var parts = FillRemnantBox(drawing, rightBox, bestFits, token); - if (parts != null && parts.Count > (bestRemnant?.Count ?? 0)) - bestRemnant = parts; + right = FillRemnantBox(drawing, new Box(rightX, workArea.Y, rightWidth, workArea.Width), bestFits, token); + Consider(right); + } + + // With both strips open, the L can hold both: one strip at full span and the other + // cut short at the grid's edge. A grid of whole pairs that leaves a strip above it and + // a column beside it holds parts in each, which neither strip alone places (N26-2H6). + // Each combination replaces the best only by placing more, and is filled only when it + // could also place more than combinedToBeat: the short strip holds no more than the + // full one it is cut from, nor more than its area allows. + if (hasTop && hasRight) + { + var gridHeight = gridBox.Top - workArea.Y; + if (top != null && gridHeight >= minDim) + { + Consider(Combine(top, () => FillRemnantBox(drawing, + new Box(rightX, workArea.Y, rightWidth, gridHeight), bestFits, token, complete: false), + ShortBound(right, rightWidth * gridHeight, drawing.Area), + System.Math.Max(bestRemnant?.Count ?? 0, combinedToBeat))); + } + + var gridWidth = gridBox.Right - workArea.X; + if (right != null && gridWidth >= minDim) + { + Consider(Combine(right, () => FillRemnantBox(drawing, + new Box(workArea.X, topY, gridWidth, topLength), bestFits, token, complete: false), + ShortBound(top, gridWidth * topLength, drawing.Area), + System.Math.Max(bestRemnant?.Count ?? 0, combinedToBeat))); + } } return bestRemnant; } + /// + /// The most parts a strip cut short can be expected to hold: no more than the full strip + /// held, and no more than its area allows. + /// + private static int ShortBound(List full, double shortArea, double partArea) + { + var bound = full?.Count ?? 0; + return partArea > 0 ? System.Math.Min(bound, (int)(shortArea / partArea)) : bound; + } + + /// + /// with the short strip's parts added, or null when the short + /// strip's cannot lift the total above + /// , or it holds none. + /// + private static List Combine( + List full, + System.Func> fillShort, + int shortBound, + int toBeat) + { + if (full.Count + shortBound <= toBeat) + return null; + + var shortParts = fillShort(); + if (shortParts == null || shortParts.Count == 0) + return null; + + var total = new List(full.Count + shortParts.Count); + total.AddRange(full); + total.AddRange(shortParts); + return total; + } + private List FillRemnantBox( Drawing drawing, Box remnantBox, diff --git a/OpenNest.Tests/Fill/PairRemnantFillerTests.cs b/OpenNest.Tests/Fill/PairRemnantFillerTests.cs index dba0698a..91c41896 100644 --- a/OpenNest.Tests/Fill/PairRemnantFillerTests.cs +++ b/OpenNest.Tests/Fill/PairRemnantFillerTests.cs @@ -62,6 +62,43 @@ public class PairRemnantFillerTests Assert.Empty(PartOverlapChecker.FindOverlappingPairs(check.Parts)); } + /// + /// A grid ending 5.75 short of both the top and the right of a 20 x 20 work area leaves an + /// L: the top strip at full width holds 9 rectangles and the right strip at full height 9, + /// but the top strip and the column beside the grid hold 15 together (N26-2H6). + /// + [Fact] + public void Fill_LShapedRemnant_FillsBothStrips() + { + var drawing = new Drawing("rect", TestHelpers.MakeClosedProgram( + new[] { new Vector(0, 0), new Vector(4, 0), new Vector(4, 2), new Vector(0, 2), new Vector(0, 0) })); + var workArea = new Box(0, 0, 20, 20); + var low = Part.CreateAtOrigin(drawing); + var high = Part.CreateAtOrigin(drawing); + high.Offset(new Vector(10, 12)); + high.UpdateBounds(); + var grid = new List { low, high }; + FillResultCache.Clear(); + + var remnant = new PairRemnantFiller(Spacing, new DefaultFillComparer()) + .Fill(grid, drawing, workArea, CancellationToken.None); + + Assert.NotNull(remnant); + Assert.True(remnant.Count >= 15, $"remnant placed {remnant.Count}"); + foreach (var part in remnant) + { + Assert.True(part.BoundingBox.Left >= workArea.Left - Tolerance.Epsilon); + Assert.True(part.BoundingBox.Right <= workArea.Right + Tolerance.Epsilon); + Assert.True(part.BoundingBox.Bottom >= workArea.Bottom - Tolerance.Epsilon); + Assert.True(part.BoundingBox.Top <= workArea.Top + Tolerance.Epsilon); + } + + var check = new Plate { Size = new Size(20, 20), PartSpacing = Spacing }; + check.Parts.AddRange(grid); + check.Parts.AddRange(remnant); + Assert.Empty(PartOverlapChecker.FindOverlappingPairs(check.Parts)); + } + [Fact] public void Fill_WithoutBestFits_KeepsSingles() {