mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-23 12:27:41 -05:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
aa5675dc64
commit
eaf99fba8b
2 files changed
+53
-5
No files matched your search
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,9 @@ internal sealed class BlockCatalog : IDisposable
|
||||
/// <summary>Margin for Fill's boundary rounding in a <see cref="Footprint"/>.</summary>
|
||||
private const double FillRoundingMargin = 0.001;
|
||||
|
||||
/// <summary>Relative slack on a rectangle's area capacity, for Fill's boundary rounding.</summary>
|
||||
private const double CapacitySlack = 0.01;
|
||||
|
||||
/// <summary>Decimals a certified pose's position is rounded to.</summary>
|
||||
private const int PositionDecimals = 8;
|
||||
|
||||
@@ -67,6 +70,9 @@ internal sealed class BlockCatalog : IDisposable
|
||||
|
||||
private readonly Dictionary<int, Footprint> footprints = new();
|
||||
|
||||
/// <summary>Per type, its material area (holes excluded), which no two copies can share.</summary>
|
||||
private readonly Dictionary<int, double> materialAreas = new();
|
||||
|
||||
/// <summary>Per type, the blocks <see cref="Get"/> has prepared.</summary>
|
||||
private readonly Dictionary<int, int> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The request for up to <paramref name="quantity"/> 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.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Blocks prepared so far, over all types.</summary>
|
||||
internal int PreparationCount => preparations.Values.Sum();
|
||||
|
||||
@@ -253,7 +280,7 @@ internal sealed class BlockCatalog : IDisposable
|
||||
public IReadOnlyList<Placed> 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<Placed>();
|
||||
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<Placed>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The band's entry, its pattern proposed here when the catalog has none; charges
|
||||
|
||||
Reference in new issue
Block a user