perf(irregular): skip Lattice fills in rectangles that cannot hold a block

A free rectangle narrower than the part at every rotation (its convex-hull
width, less the outline's flattening tolerance), or smaller than three
copies, can only give Fill two members or fewer, which never form a block.
Block and band requests for such rectangles no longer run the Fill. They
still count as a preparation and are charged as before, so the work meter
and every layout stay byte-identical. On N26-2GT this skips 31 fills
(about 7.6 s of fill time across the solve, 0.4 s before the first plan).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-18 01:36:19 -05:00
1 parent 683ece4d3c
commit fcf205ab44
3 files changed
+73 -3

No files matched your search

@@ -105,6 +105,22 @@ public class IrregularBlockTests
Assert.Single(fill.Parts);
}
[Fact]
public void RectangleNarrowerThanThePartIsChargedAsAnEmptyProposal()
{
var job = Job(new[] { Part("ell", LShape(9, 7, 3), 20, RotationPolicy.Automatic) },
new[] { Stock("sheet", 40, 200, spacing: 0.25) });
var types = PartCatalog.Build(job);
var counter = new WorkCounter();
using var blocks = new BlockCatalog(0.25, types, NoPairs, counter, long.MaxValue);
var strip = new Box(0, 0, 200, 2.5);
Assert.Empty(blocks.Get(types[0], 20, strip, CancellationToken.None));
// Counted and charged as the Fill would have been, so later decisions do not change.
Assert.Equal(1, blocks.PreparationCount);
Assert.Equal(20L * 20 * types[0].Orientations[0].Outline.Count, counter.Value);
Assert.Empty(blocks.Band(types[0], 20, strip, CancellationToken.None));
}
[Fact]
public void ForbiddenRotationsAreRejectedBeforeAddingGroupOrientations()
{
@@ -22,6 +22,7 @@ internal sealed class BlockCatalog : IDisposable
private readonly Dictionary<int, Drawing> drawings = new();
private readonly Func<NestJobPart, Drawing>? drawingFor;
private readonly Dictionary<int, List<Orientation>> orientations = new();
private readonly Dictionary<int, (double Width, double Area)> footprints = new();
private readonly Dictionary<int, int> attempts = new();
internal int PreparationCount => attempts.Values.Sum();
private readonly Dictionary<(int Type, int Quantity, double Length, double Width), IReadOnlyList<Placed>> cache = new();
@@ -55,9 +56,53 @@ internal sealed class BlockCatalog : IDisposable
if (pairs.TryGetValue(type.Index, out var found))
poses.AddRange(found.SelectMany(p => new[] { p.A, p.B }));
orientations[type.Index] = poses.Distinct().ToList();
if (type.Orientations.Count > 0)
footprints[type.Index] = Footprint(type.Orientations[0]);
}
}
/// <summary>
/// Lower bounds on the true part's width at any rotation and on its area: the outline's
/// convex-hull width and area, less what flattening within the outline's tolerance (and a
/// margin for Fill's boundary rounding) could hide.
/// </summary>
private static (double Width, double Area) Footprint(Orientation orientation)
{
var outline = orientation.Outline;
var hull = ConvexHull.Compute(outline.Select(p => new Vector(p.x, p.y)).ToList()).Vertices;
var width = double.PositiveInfinity;
for (var i = 0; i < hull.Count; i++)
{
var a = hull[i];
var b = hull[(i + 1) % hull.Count];
var length = a.DistanceTo(b);
if (length <= Tolerance.Epsilon)
continue;
var across = hull.Max(p => System.Math.Abs((b.X - a.X) * (p.Y - a.Y) - (b.Y - a.Y) * (p.X - a.X)) / length);
width = System.Math.Min(width, across);
}
var perimeter = 0.0;
for (var i = 0; i < outline.Count; i++)
{
var a = outline[i];
var b = outline[(i + 1) % outline.Count];
perimeter += System.Math.Sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}
const double margin = 0.001;
return (double.IsFinite(width) ? width - 2 * orientation.Tolerance - margin : 0,
System.Math.Abs(Clipper.Area(outline)) - perimeter * (orientation.Tolerance + margin));
}
/// <summary>
/// True when the rectangle cannot hold three copies of the type: it is narrower than the part
/// at every rotation, or smaller than three parts. Fill could return at most two members there,
/// which never form a block, so its proposal is known to be empty without running it.
/// </summary>
private bool CannotHoldBlock(PartType type, Box rectangle) =>
footprints.TryGetValue(type.Index, out var footprint)
&& (System.Math.Min(rectangle.Length, rectangle.Width) < footprint.Width
|| rectangle.Area() < 3 * footprint.Area);
internal static IReadOnlyList<Box> Rectangles(Box work, IReadOnlyList<Placed> placed, double spacing)
{
var free = new PathsD { new PathD
@@ -127,8 +172,10 @@ internal sealed class BlockCatalog : IDisposable
{
// Types may build bands in parallel: each type's drawing and orientation list are its
// own. Prefetches of one type fill their own drawings and only read the orientations.
var (members, poses) = Propose(type, quantity, rectangle, token,
spacing + System.Math.Pow(10, -NoFitCache.Precision), prefetch ?? DrawingOf(type));
var (members, poses) = CannotHoldBlock(type, rectangle)
? (Array.Empty<Placed>(), null)
: Propose(type, quantity, rectangle, token,
spacing + System.Math.Pow(10, -NoFitCache.Precision), prefetch ?? DrawingOf(type));
lock (bands)
if (!bands.TryGetValue(key, out entry))
bands[key] = entry = new BandEntry(members, poses, Cost(type, quantity));
@@ -173,7 +220,11 @@ internal sealed class BlockCatalog : IDisposable
// PrivatePlateFill.Run below is a full independent NFP-based pack whose own operations
// never touch this solve's effort meter; charge it here using the same factors - demand
// and outline complexity - that drive its real cost, so the shared budget actually sees it.
// A rectangle that cannot hold a block is charged and counted as if filled, so the meter,
// and every later decision it steers, reads the same as when the Fill ran.
counter.Add(Cost(type, quantity));
if (CannotHoldBlock(type, rectangle))
return Array.Empty<Placed>();
var (members, poses) = Propose(type, quantity, rectangle, token, spacing, DrawingOf(type));
return members ?? Bind(type, poses!, orientations[type.Index], token);
}
+4 -1
View File
@@ -248,7 +248,10 @@ the empty work area and physical leftover space for up to two high-area rectangl
outlines are expanded by part spacing before rectangle search. Each sheet prepares blocks initially
and after its first placement, for up to four high-demand-area types; each type has at most eight
new Lattice preparations per spacing per solve. Repeated rectangles reuse private drawing/candidate
caches. Quantity-one and quantity-two requests never run Lattice blocks.
caches. Quantity-one and quantity-two requests never run Lattice blocks. A rectangle narrower than
the part at every rotation, or too small for three copies, cannot hold a block, so it is not
filled (for a block or a band); it still counts as a preparation and is charged as one, so the
layouts are those the fill would have given.
Block members are trimmed to remaining demand, mapped back to source-frame rotations, and checked
for legal rotations and internal material clearance before competing with singles and pairs.