From fcf205ab440ff7c1837cf12d75de838a49626f28 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 17 Nov 2026 21:57:36 -0500 Subject: [PATCH] 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 --- .../NestingEngines/IrregularBlockTests.cs | 16 ++++++ .../NestingEngines/Irregular/BlockCatalog.cs | 55 ++++++++++++++++++- docs/nesting-engines.md | 5 +- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs b/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs index 38152c78..1f3f579f 100644 --- a/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs +++ b/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs @@ -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() { diff --git a/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs b/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs index a5ac61fe..05824c09 100644 --- a/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs +++ b/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs @@ -22,6 +22,7 @@ internal sealed class BlockCatalog : IDisposable private readonly Dictionary drawings = new(); private readonly Func? drawingFor; private readonly Dictionary> orientations = new(); + private readonly Dictionary footprints = new(); private readonly Dictionary attempts = new(); internal int PreparationCount => attempts.Values.Sum(); private readonly Dictionary<(int Type, int Quantity, double Length, double Width), IReadOnlyList> 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]); } } + /// + /// 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. + /// + 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)); + } + + /// + /// 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. + /// + 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 Rectangles(Box work, IReadOnlyList 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(), 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(); var (members, poses) = Propose(type, quantity, rectangle, token, spacing, DrawingOf(type)); return members ?? Bind(type, poses!, orientations[type.Index], token); } diff --git a/docs/nesting-engines.md b/docs/nesting-engines.md index 246316cc..f8db31e1 100644 --- a/docs/nesting-engines.md +++ b/docs/nesting-engines.md @@ -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.