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