fix(fill): fill both strips of a pair grid's L remnant

The pair fill's remnant step filled the top strip or the right strip,
whichever held more, never both. A grid of whole pairs that leaves a
strip above and a column beside it lost to the grid with a half-pair top
row: N26-2H6 filled 85 parts where 88 fit. Each full strip is now also
tried with the other cut short at the grid's edge, only when that could
beat the best layout so far, and without the short strip's grid
completion, keeping the fill corpus at master's time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-12-03 23:34:14 -05:00
1 parent 207c9c8cf1
commit 13f852259b
3 files changed
+123 -15

No files matched your search

+7 -4
View File
@@ -613,9 +613,12 @@ namespace OpenNest.Engine.Fill
int Bound(List<Part> gridParts) =>
gridParts.Count + remnantFiller.EstimateUpperBound(gridParts, workArea, maxUtilization, partArea);
List<Part> WithRemnant(List<Part> gridParts)
// The remnant's top and right strips are combined only when together they could lift
// the grid past toBeat parts.
List<Part> WithRemnant(List<Part> 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;
}
+79 -11
View File
@@ -89,7 +89,8 @@ namespace OpenNest.Engine.Fill
Drawing drawing,
Box workArea,
CancellationToken token,
List<BestFitResult> bestFits = null
List<BestFitResult> bestFits = null,
int combinedToBeat = 0
)
{
var gridBox = ((IEnumerable<IBoundable>)gridParts).GetBoundingBox();
@@ -103,31 +104,98 @@ namespace OpenNest.Engine.Fill
List<Part> bestRemnant = null;
void Consider(List<Part> 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<Part> 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<Part> 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;
}
/// <summary>
/// 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.
/// </summary>
private static int ShortBound(List<Part> full, double shortArea, double partArea)
{
var bound = full?.Count ?? 0;
return partArea > 0 ? System.Math.Min(bound, (int)(shortArea / partArea)) : bound;
}
/// <summary>
/// <paramref name="full"/> with the short strip's parts added, or null when the short
/// strip's <paramref name="shortBound"/> cannot lift the total above
/// <paramref name="toBeat"/>, or it holds none.
/// </summary>
private static List<Part> Combine(
List<Part> full,
System.Func<List<Part>> 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<Part>(full.Count + shortParts.Count);
total.AddRange(full);
total.AddRange(shortParts);
return total;
}
private List<Part> FillRemnantBox(
Drawing drawing,
Box remnantBox,
@@ -62,6 +62,43 @@ public class PairRemnantFillerTests
Assert.Empty(PartOverlapChecker.FindOverlappingPairs(check.Parts));
}
/// <summary>
/// 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).
/// </summary>
[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<Part> { 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()
{