feat: add remainder strip re-fill to improve pattern fill density

After the main fill, detect if the last column/row is an "oddball"
with fewer parts than the main grid. If so, remove those parts and
re-fill the remainder strip independently using all strategies
(linear, rect best-fit, pairs). Improves 30→32 parts on the test
case (96x48 plate with 30x7.5 interlocking parts).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-09 19:15:42 -04:00
parent 3516199f25
commit 3705d50546
2 changed files with 551 additions and 11 deletions

View File

@@ -30,6 +30,29 @@ namespace OpenNest
}
public bool Fill(NestItem item, Box workArea)
{
var best = FindBestFill(item, workArea);
// Try improving by filling the remainder strip separately.
var improved = TryRemainderImprovement(item, workArea, best);
if (IsBetterFill(improved, best))
{
Debug.WriteLine($"[Fill] Remainder improvement: {improved.Count} parts (was {best?.Count ?? 0})");
best = improved;
}
if (best == null || best.Count == 0)
return false;
if (item.Quantity > 0 && best.Count > item.Quantity)
best = best.Take(item.Quantity).ToList();
Plate.Parts.AddRange(best);
return true;
}
private List<Part> FindBestFill(NestItem item, Box workArea)
{
var bestRotation = RotationAnalysis.FindBestRotation(item);
@@ -76,12 +99,12 @@ namespace OpenNest
best = v;
}
Debug.WriteLine($"[Fill(NestItem,Box)] Linear: {best?.Count ?? 0} parts | WorkArea: {workArea.Width:F1}x{workArea.Height:F1} | Angles: {angles.Count}");
Debug.WriteLine($"[FindBestFill] Linear: {best?.Count ?? 0} parts | WorkArea: {workArea.Width:F1}x{workArea.Height:F1} | Angles: {angles.Count}");
// Try rectangle best-fit (mixes orientations to fill remnant strips).
var rectResult = FillRectangleBestFit(item, workArea);
Debug.WriteLine($"[Fill(NestItem,Box)] RectBestFit: {rectResult?.Count ?? 0} parts");
Debug.WriteLine($"[FindBestFill] RectBestFit: {rectResult?.Count ?? 0} parts");
if (IsBetterFill(rectResult, best))
best = rectResult;
@@ -89,19 +112,12 @@ namespace OpenNest
// Try pair-based approach.
var pairResult = FillWithPairs(item, workArea);
Debug.WriteLine($"[Fill(NestItem,Box)] Pair: {pairResult.Count} parts | Winner: {(IsBetterFill(pairResult, best) ? "Pair" : "Linear")}");
Debug.WriteLine($"[FindBestFill] Pair: {pairResult.Count} parts | Winner: {(IsBetterFill(pairResult, best) ? "Pair" : "Linear")}");
if (IsBetterFill(pairResult, best))
best = pairResult;
if (best == null || best.Count == 0)
return false;
if (item.Quantity > 0 && best.Count > item.Quantity)
best = best.Take(item.Quantity).ToList();
Plate.Parts.AddRange(best);
return true;
return best;
}
public bool Fill(List<Part> groupParts, Box workArea)
@@ -131,6 +147,15 @@ namespace OpenNest
if (IsBetterFill(pairResult, best))
best = pairResult;
// Try improving by filling the remainder strip separately.
var improved = TryRemainderImprovement(nestItem, workArea, best);
if (IsBetterFill(improved, best))
{
Debug.WriteLine($"[Fill(groupParts,Box)] Remainder improvement: {improved.Count} parts (was {best?.Count ?? 0})");
best = improved;
}
}
if (best == null || best.Count == 0)
@@ -286,6 +311,139 @@ namespace OpenNest
return IsBetterFill(candidate, current);
}
/// <summary>
/// Groups parts into positional clusters along the given axis.
/// Parts whose center positions are separated by more than half
/// the part dimension start a new cluster.
/// </summary>
private static List<List<Part>> ClusterParts(List<Part> parts, bool horizontal)
{
var sorted = horizontal
? parts.OrderBy(p => p.BoundingBox.Center.X).ToList()
: parts.OrderBy(p => p.BoundingBox.Center.Y).ToList();
var refDim = horizontal
? sorted.Max(p => p.BoundingBox.Width)
: sorted.Max(p => p.BoundingBox.Height);
var gapThreshold = refDim * 0.5;
var clusters = new List<List<Part>>();
var current = new List<Part> { sorted[0] };
for (var i = 1; i < sorted.Count; i++)
{
var prevCenter = horizontal
? sorted[i - 1].BoundingBox.Center.X
: sorted[i - 1].BoundingBox.Center.Y;
var currCenter = horizontal
? sorted[i].BoundingBox.Center.X
: sorted[i].BoundingBox.Center.Y;
if (currCenter - prevCenter > gapThreshold)
{
clusters.Add(current);
current = new List<Part>();
}
current.Add(sorted[i]);
}
clusters.Add(current);
return clusters;
}
private List<Part> TryStripRefill(NestItem item, Box workArea, List<Part> parts, bool horizontal)
{
if (parts == null || parts.Count < 3)
return null;
var clusters = ClusterParts(parts, horizontal);
if (clusters.Count < 2)
return null;
// Determine the mode (most common) cluster count, excluding the last cluster.
var mainClusters = clusters.Take(clusters.Count - 1).ToList();
var modeCount = mainClusters
.GroupBy(c => c.Count)
.OrderByDescending(g => g.Count())
.First()
.Key;
var lastCluster = clusters[clusters.Count - 1];
// Only attempt refill if the last cluster is smaller than the mode.
if (lastCluster.Count >= modeCount)
return null;
Debug.WriteLine($"[TryStripRefill] {(horizontal ? "H" : "V")} clusters: {clusters.Count}, mode: {modeCount}, last: {lastCluster.Count}");
// Build the main parts list (everything except the last cluster).
var mainParts = clusters.Take(clusters.Count - 1).SelectMany(c => c).ToList();
var mainBox = ((IEnumerable<IBoundable>)mainParts).GetBoundingBox();
// Compute the strip box from the main grid edge to the work area edge.
Box stripBox;
if (horizontal)
{
var stripLeft = mainBox.Right + Plate.PartSpacing;
var stripWidth = workArea.Right - stripLeft;
if (stripWidth <= 0)
return null;
stripBox = new Box(stripLeft, workArea.Y, stripWidth, workArea.Height);
}
else
{
var stripBottom = mainBox.Top + Plate.PartSpacing;
var stripHeight = workArea.Top - stripBottom;
if (stripHeight <= 0)
return null;
stripBox = new Box(workArea.X, stripBottom, workArea.Width, stripHeight);
}
Debug.WriteLine($"[TryStripRefill] Strip: {stripBox.Width:F1}x{stripBox.Height:F1} at ({stripBox.X:F1},{stripBox.Y:F1})");
var stripParts = FindBestFill(item, stripBox);
if (stripParts == null || stripParts.Count <= lastCluster.Count)
{
Debug.WriteLine($"[TryStripRefill] No improvement: strip={stripParts?.Count ?? 0} vs oddball={lastCluster.Count}");
return null;
}
Debug.WriteLine($"[TryStripRefill] Improvement: strip={stripParts.Count} vs oddball={lastCluster.Count}");
var combined = new List<Part>(mainParts.Count + stripParts.Count);
combined.AddRange(mainParts);
combined.AddRange(stripParts);
return combined;
}
private List<Part> TryRemainderImprovement(NestItem item, Box workArea, List<Part> currentBest)
{
if (currentBest == null || currentBest.Count < 3)
return null;
List<Part> best = null;
var hResult = TryStripRefill(item, workArea, currentBest, horizontal: true);
if (IsBetterFill(hResult, best))
best = hResult;
var vResult = TryStripRefill(item, workArea, currentBest, horizontal: false);
if (IsBetterFill(vResult, best))
best = vResult;
return best;
}
private Pattern BuildRotatedPattern(List<Part> groupParts, double angle)
{
var pattern = new Pattern();