refactor(engine): extract RemnantFiller for iterative remnant filling

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-16 22:33:00 -04:00
parent c8587929b5
commit 319eace472
2 changed files with 217 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Threading;
using OpenNest.Geometry;
namespace OpenNest
{
/// <summary>
/// Iteratively fills remnant boxes with items using a RemnantFinder.
/// After each fill, re-discovers free rectangles and tries again
/// until no more items can be placed.
/// </summary>
public class RemnantFiller
{
private readonly RemnantFinder finder;
private readonly double spacing;
public RemnantFiller(Box workArea, double spacing)
{
this.spacing = spacing;
finder = new RemnantFinder(workArea);
}
public void AddObstacles(IEnumerable<Part> parts)
{
foreach (var part in parts)
finder.AddObstacle(part.BoundingBox.Offset(spacing));
}
public List<Part> FillItems(
List<NestItem> items,
Func<NestItem, Box, List<Part>> fillFunc,
CancellationToken token = default,
IProgress<NestProgress> progress = null)
{
if (items == null || items.Count == 0)
return new List<Part>();
var allParts = new List<Part>();
var madeProgress = true;
// Track quantities locally — do not mutate the input NestItem objects.
var localQty = new Dictionary<string, int>();
foreach (var item in items)
localQty[item.Drawing.Name] = item.Quantity;
while (madeProgress && !token.IsCancellationRequested)
{
madeProgress = false;
var minRemnantDim = double.MaxValue;
foreach (var item in items)
{
var qty = localQty[item.Drawing.Name];
if (qty <= 0)
continue;
var bb = item.Drawing.Program.BoundingBox();
var dim = System.Math.Min(bb.Width, bb.Length);
if (dim < minRemnantDim)
minRemnantDim = dim;
}
if (minRemnantDim == double.MaxValue)
break;
var freeBoxes = finder.FindRemnants(minRemnantDim);
if (freeBoxes.Count == 0)
break;
foreach (var item in items)
{
if (token.IsCancellationRequested)
break;
var qty = localQty[item.Drawing.Name];
if (qty == 0)
continue;
var itemBbox = item.Drawing.Program.BoundingBox();
var minItemDim = System.Math.Min(itemBbox.Width, itemBbox.Length);
foreach (var box in freeBoxes)
{
if (System.Math.Min(box.Width, box.Length) < minItemDim)
continue;
var fillItem = new NestItem { Drawing = item.Drawing, Quantity = qty };
var remnantParts = fillFunc(fillItem, box);
if (remnantParts != null && remnantParts.Count > 0)
{
allParts.AddRange(remnantParts);
localQty[item.Drawing.Name] = System.Math.Max(0, qty - remnantParts.Count);
foreach (var p in remnantParts)
finder.AddObstacle(p.BoundingBox.Offset(spacing));
madeProgress = true;
break;
}
}
if (madeProgress)
break;
}
}
return allParts;
}
}
}

View File

@@ -0,0 +1,105 @@
using OpenNest.Geometry;
namespace OpenNest.Tests;
public class RemnantFillerTests2
{
private static Drawing MakeSquareDrawing(double size)
{
var pgm = new OpenNest.CNC.Program();
pgm.Codes.Add(new OpenNest.CNC.RapidMove(new Vector(0, 0)));
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(size, 0)));
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(size, size)));
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(0, size)));
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(0, 0)));
return new Drawing("sq", pgm);
}
[Fact]
public void FillItems_PlacesPartsInRemnants()
{
var workArea = new Box(0, 0, 100, 100);
var filler = new RemnantFiller(workArea, 1.0);
// Place a large obstacle leaving a 40x100 strip on the right
filler.AddObstacles(new[] { TestHelpers.MakePartAt(0, 0, 50) });
var drawing = MakeSquareDrawing(10);
var items = new List<NestItem>
{
new NestItem { Drawing = drawing, Quantity = 5 }
};
Func<NestItem, Box, List<Part>> fillFunc = (ni, b) =>
{
var plate = new Plate(b.Width, b.Length);
var engine = new DefaultNestEngine(plate);
return engine.Fill(ni, b, null, System.Threading.CancellationToken.None);
};
var placed = filler.FillItems(items, fillFunc);
Assert.True(placed.Count > 0, "Should place parts in remaining space");
}
[Fact]
public void FillItems_DoesNotMutateItemQuantities()
{
var workArea = new Box(0, 0, 100, 100);
var filler = new RemnantFiller(workArea, 1.0);
var drawing = MakeSquareDrawing(10);
var items = new List<NestItem>
{
new NestItem { Drawing = drawing, Quantity = 3 }
};
Func<NestItem, Box, List<Part>> fillFunc = (ni, b) =>
{
var plate = new Plate(b.Width, b.Length);
var engine = new DefaultNestEngine(plate);
return engine.Fill(ni, b, null, System.Threading.CancellationToken.None);
};
filler.FillItems(items, fillFunc);
Assert.Equal(3, items[0].Quantity);
}
[Fact]
public void FillItems_EmptyItems_ReturnsEmpty()
{
var workArea = new Box(0, 0, 100, 100);
var filler = new RemnantFiller(workArea, 1.0);
Func<NestItem, Box, List<Part>> fillFunc = (ni, b) => new List<Part>();
var result = filler.FillItems(new List<NestItem>(), fillFunc);
Assert.Empty(result);
}
[Fact]
public void FillItems_RespectsCancellation()
{
var cts = new System.Threading.CancellationTokenSource();
cts.Cancel();
var workArea = new Box(0, 0, 100, 100);
var filler = new RemnantFiller(workArea, 1.0);
var drawing = MakeSquareDrawing(10);
var items = new List<NestItem>
{
new NestItem { Drawing = drawing, Quantity = 5 }
};
Func<NestItem, Box, List<Part>> fillFunc = (ni, b) =>
new List<Part> { TestHelpers.MakePartAt(0, 0, 10) };
var result = filler.FillItems(items, fillFunc, cts.Token);
// Should not throw, returns whatever was placed
Assert.NotNull(result);
}
}