From eaf99fba8ba10f1bf54b306a073fe6aaaccbaf40 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 23 Nov 2026 00:49:10 -0500 Subject: [PATCH] fix(irregular): cap Lattice block requests at the rectangle's capacity Block requests were keyed and charged by every copy still wanted, so a high-demand part (140 copies) spent a large share of the work budget per Fill and never reused a block from one sheet to the next. Later sheets got no Lattice block for the strip beside their large parts: on N26-2J9 a 48 x 96 sheet held 25 small parts where a Fill of the strip holds 32. Requests now ask for at most what the rectangle's area could hold of the part's material. Fill stops at the copies asked only when it could place more, so the patterns are unchanged; they cost less and are shared. Co-Authored-By: Claude Opus 5.5 --- .../NestingEngines/IrregularBlockTests.cs | 25 ++++++++++++-- .../NestingEngines/Irregular/BlockCatalog.cs | 33 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs b/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs index 1f3f579f..1d4b1095 100644 --- a/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs +++ b/OpenNest.Engine.Tests/NestingEngines/IrregularBlockTests.cs @@ -44,6 +44,26 @@ public class IrregularBlockTests Assert.Same(block, catalog.Get(types[0], 7, new Box(0, 0, 60, 40), CancellationToken.None)); } + [Fact] + public void DemandBeyondTheRectanglesCapacityReusesOneFill() + { + // A 20 x 20 box holds at most 400 / 39 copies of the ell's material, so wanting 40 or 60 + // asks for the same Fill: one preparation, charged as for the capped copies. + var job = Job(new[] { Part("ell", LShape(9, 7, 3), 60, RotationPolicy.Automatic) }, + new[] { Stock("sheet", 40, 60, spacing: 0.25) }); + var types = PartCatalog.Build(job); + var counter = new WorkCounter(); + using var catalog = new BlockCatalog(0.25, types, NoPairs, counter, long.MaxValue); + var box = new Box(0, 0, 20, 20); + var block = catalog.Get(types[0], 60, box, CancellationToken.None); + var charged = counter.Value; + Assert.NotEmpty(block); + Assert.Same(block, catalog.Get(types[0], 40, box, CancellationToken.None)); + Assert.Equal(1, catalog.PreparationCount); + Assert.Equal(charged, counter.Value); + Assert.True(charged < 60L * 60 * types[0].Orientations[0].Outline.Count); + } + [Theory] [InlineData(0)] [InlineData(2.1)] @@ -115,9 +135,10 @@ public class IrregularBlockTests 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. + // Counted and charged as the Fill would have been, so later decisions do not change: for + // the 13 copies the strip's 500 square inches could hold of the ell's 39, not all 20. Assert.Equal(1, blocks.PreparationCount); - Assert.Equal(20L * 20 * types[0].Orientations[0].Outline.Count, counter.Value); + Assert.Equal(13L * 13 * types[0].Orientations[0].Outline.Count, counter.Value); Assert.Empty(blocks.Band(types[0], 20, strip, CancellationToken.None)); } diff --git a/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs b/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs index f1053c9d..d8779301 100644 --- a/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs +++ b/OpenNest.Engine/NestingEngines/Irregular/BlockCatalog.cs @@ -43,6 +43,9 @@ internal sealed class BlockCatalog : IDisposable /// Margin for Fill's boundary rounding in a . private const double FillRoundingMargin = 0.001; + /// Relative slack on a rectangle's area capacity, for Fill's boundary rounding. + private const double CapacitySlack = 0.01; + /// Decimals a certified pose's position is rounded to. private const int PositionDecimals = 8; @@ -67,6 +70,9 @@ internal sealed class BlockCatalog : IDisposable private readonly Dictionary footprints = new(); + /// Per type, its material area (holes excluded), which no two copies can share. + private readonly Dictionary materialAreas = new(); + /// Per type, the blocks has prepared. private readonly Dictionary preparations = new(); @@ -196,6 +202,9 @@ internal sealed class BlockCatalog : IDisposable orientations[type.Index] = WithPairOrientations(type, pairs); if (type.Orientations.Count > 0) footprints[type.Index] = Footprint.Of(type.Orientations[0]); + if (JobPartGeometry.TryRead(type.Part.Geometry)?.MaterialArea is double area && area > 0 + && double.IsFinite(area)) + materialAreas[type.Index] = area; } } @@ -209,6 +218,24 @@ internal sealed class BlockCatalog : IDisposable return poses.Distinct().ToList(); } + /// + /// The request for up to copies in the rectangle, the copies held to + /// as many as the rectangle's area could hold of the type's material. A Fill stops at the copies + /// asked only when it could place more, so asking for more than can fit proposes the same + /// pattern; capped, one pattern serves every sheet that wants at least that many, rather than + /// a new Fill, charged by the square of the copies, for each remaining demand. + /// + private Request RequestFor(PartType type, int quantity, Box rectangle) + { + if (materialAreas.TryGetValue(type.Index, out var area) && rectangle.Length > 0 && rectangle.Width > 0) + { + var capacity = rectangle.Area() * (1 + CapacitySlack) / area + 1; + if (capacity < quantity) + quantity = (int)capacity; + } + return new Request(type, quantity, rectangle); + } + /// Blocks prepared so far, over all types. internal int PreparationCount => preparations.Values.Sum(); @@ -253,7 +280,7 @@ internal sealed class BlockCatalog : IDisposable public IReadOnlyList Get(PartType type, int quantity, Box rectangle, CancellationToken token) { token.ThrowIfCancellationRequested(); - var request = new Request(type, quantity, rectangle); + var request = RequestFor(type, quantity, rectangle); if (request.IsEmpty) return Array.Empty(); var key = request.Key; @@ -323,7 +350,7 @@ internal sealed class BlockCatalog : IDisposable Drawing? prefetch = null) { token.ThrowIfCancellationRequested(); - var request = new Request(type, quantity, rectangle); + var request = RequestFor(type, quantity, rectangle); if (request.IsEmpty) return Array.Empty(); var entry = BandFor(request, token, prefetch); @@ -356,7 +383,7 @@ internal sealed class BlockCatalog : IDisposable public double RejectedBandLength(PartType type, int quantity, Box rectangle) { lock (bands) - return bands.TryGetValue(new Request(type, quantity, rectangle).Key, out var entry) ? entry.Rejected : 0; + return bands.TryGetValue(RequestFor(type, quantity, rectangle).Key, out var entry) ? entry.Rejected : 0; } /// The band's entry, its pattern proposed here when the catalog has none; charges