From 163c23e8bd8cd95d26b977db7b5cd2fa9b218600 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sun, 8 Nov 2026 19:39:29 -0500 Subject: [PATCH] feat(engine): place large parts first and keep small ones as filler in strips A sheet that cannot take all the remaining parts now keeps whichever places more part area: the ordinary fill or strips largest part first. The ordinary fill spent skinny parts early because they cover the most area per length, leaving awkward ones for a ragged last sheet. Inside a strip, the strip's own part fills its column first and the others fill around it; before, pairs of smaller parts (counted as twice a part's area) took the strip and the large part lost it. Bands also try largest part first as a third order. At about twice the README sample quantities the second sheet ends at 99.9" instead of 106.2" on master (104.7" before this change), and the help tutorial's parts at 33.6" instead of 34.9". Every other benchmark job is unchanged or better; mixed jobs solve about two to three times slower than master. Co-Authored-By: Claude Opus 5.5 --- .../NestingEngines/Irregular/BandedFill.cs | 21 ++++++++++++++++--- .../Irregular/IrregularNestingEngine.cs | 14 ++++++++++--- docs/nesting-engines.md | 19 ++++++++++------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/OpenNest.Engine/NestingEngines/Irregular/BandedFill.cs b/OpenNest.Engine/NestingEngines/Irregular/BandedFill.cs index 97a33272..ff347d3c 100644 --- a/OpenNest.Engine/NestingEngines/Irregular/BandedFill.cs +++ b/OpenNest.Engine/NestingEngines/Irregular/BandedFill.cs @@ -65,6 +65,8 @@ internal sealed class BandedFill( { bands.Values.OrderByDescending(b => b.Length).ThenBy(b => b.Type).Select(b => b.Type).ToArray(), bands.Values.OrderBy(b => b.Length).ThenBy(b => b.Type).Select(b => b.Type).ToArray(), + // Largest part first: small parts stay last, as filler for the gaps. + bands.Values.OrderByDescending(b => types[b.Type].Area).ThenBy(b => b.Type).Select(b => b.Type).ToArray(), }; int[]? bestOrder = null; SheetFill? best = null; @@ -100,7 +102,8 @@ internal sealed class BandedFill( /// carried strip to strip, so a strip is judged by what it leaves for the rest of the sheet, /// not by its own density alone; the best finished sheet wins. /// - public SheetFill? FillStrips(IReadOnlyList remaining, Func within, CancellationToken token) + public SheetFill? FillStrips(IReadOnlyList remaining, Func within, CancellationToken token, + int beamWidth = Beam) { if (stock.Outline != null) return null; @@ -175,8 +178,20 @@ internal sealed class BandedFill( if (!widths.Any(w => System.Math.Abs(w - width) < 0.05)) widths.Add(width); } + // The strip's own part fills its column first, then the others fill around it. foreach (var width in widths.OrderBy(w => w)) - Offer(new List(), start + width); + { + var reach = System.Math.Min(region.Right, start + width + 1e-6); + if (reach <= state.Front) + continue; + var own = new int[types.Count]; + own[t] = state.Rest[t]; + var left = System.Math.Max(region.Left, start - lookBack); + var column = within(new Box(left, region.Bottom, reach - left, region.Width)) + .Fill(own, token, state.Placed, region.Top); + if (column.Parts.Count > 0) + Offer(column.Parts.ToList(), start + width); + } if (offered == 0) finished.Add(state); } @@ -184,7 +199,7 @@ internal sealed class BandedFill( beam = new List(); foreach (var child in children.OrderBy(Rank).ThenBy(c => c.Front)) { - if (beam.Count >= Beam) + if (beam.Count >= beamWidth) break; if (beam.Any(b => System.Math.Abs(b.Front - child.Front) < 1e-6 && System.Math.Abs(b.Area - child.Area) < 1e-6 && b.Rest.SequenceEqual(child.Rest))) diff --git a/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs b/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs index 8332bcfe..aca4a621 100644 --- a/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs +++ b/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs @@ -411,11 +411,19 @@ public sealed class IrregularNestingEngine : INestingEngine private SheetFill Banded(NestPlateStock stock, int[] remaining, IReadOnlyList? weights, Func?, double?, FrontierPacker> packer) { - // Bands shape where a sheet ends; a sheet that cannot take all the remaining parts - // fills to its end anyway, so it keeps the ordinary fill. + // A sheet that cannot take all the remaining parts fills to its end anyway: it keeps + // whichever places more, the ordinary fill or strips largest part first (one partial + // sheet carried), which leave the small parts for the gaps of the sheets after it. var work = stock.WorkArea; + FrontierPacker Within(OpenNest.Geometry.Box box) => new(types, CacheFor(stock), PairsFor(stock), stock, + PackAxis.Y, 1.0, Work, null, ClampsFor(stock), null, weights, null, null, box); if (types.Sum(t => remaining[t.Index] * t.Area) > work.Length * work.Width) - return packer(null, null).Fill(remaining, token); + { + var plain = packer(null, null).Fill(remaining, token); + var full = new BandedFill(types, BlocksFor(stock), stock, ClampsFor(stock), () => packer(null, null), + CompareSheets, Work, Work.Value + workBudget).FillStrips(remaining, Within, token, 1); + return full != null && CompareSheets(full, plain) < 0 ? full : plain; + } var bands = new BandedFill(types, BlocksFor(stock), stock, ClampsFor(stock), () => packer(null, null), CompareSheets, Work, groupedBudget).Fill(remaining, token); var strips = new BandedFill(types, BlocksFor(stock), stock, ClampsFor(stock), () => packer(null, null), diff --git a/docs/nesting-engines.md b/docs/nesting-engines.md index 87ac06ae..2261f120 100644 --- a/docs/nesting-engines.md +++ b/docs/nesting-engines.md @@ -279,23 +279,27 @@ same seeded weights. On the help tutorial's parts (30 gussets, 20 ells, 10 plate Last of the search variants, Irregular decodes the job once more in full-height strips along X (`BandedFill`), when the grouped variants apply and every part has the same priority. A sheet -that cannot take all the remaining part area keeps the ordinary fill: it fills to its end anyway, -and strips only shape where a sheet ends. Every other sheet is packed two ways and the better -sheet kept (a tie keeps the bands). A person nesting many copies of a few parts lays them out in +that cannot take all the remaining part area keeps whichever places more part area, the ordinary +fill or strips largest part first with one partial sheet carried: large and awkward parts go on +the early sheets, and small, skinny parts stay for the gaps of the later ones. Every other sheet +is packed two ways and the better sheet kept (a tie keeps the bands). A person nesting many copies of a few parts lays them out in tidy columns; packing one part at a time instead leaves each group's last column ragged and scatters the smallest parts into the gaps between groups. - Bands: each type of three or more copies as its Lattice pattern (the pattern interactive Fill gives that part alone: circles stacked or staggered, hexagons tiled, L brackets interlocked in pairs) in the narrowest strip of full work-area height that holds all its copies. Bands stand - side by side a part spacing apart, longest first or shortest first, whichever packs better; + side by side a part spacing apart, longest first, shortest first or largest part first, + whichever packs better; then the copies held back from them and the types with one or two copies fill the gaps under the usual rule. On the better order each type's narrower bands holding fewer copies (at most four, or a third, fewer; at most two per type) are tried in turn, kept when they improve the sheet. - Strips: each strip holds the largest remaining part, as its band (full or narrower) or as a - column one to three copies wide, and every other part fills the strip's gaps, the largest piece - at its leftmost, lowest point first, looking back one part length behind the strip. A beam of + column one to three copies wide that the part fills first, and every other part fills the + strip's gaps, the largest piece at its leftmost, lowest point first, looking back one part + length behind the strip. A pair counts as twice its part's area there, so without the part + filling its own column first, pairs of smaller parts would take its strip. A beam of the four partial sheets densest so far (part area per length used) is carried strip to strip, so a strip is judged by what it leaves for the rest of the sheet, not by its own density; the best finished sheet wins. @@ -312,7 +316,8 @@ scatters the smallest parts into the gaps between groups. Neither way wins every sheet. On the README sample parts (18 L brackets, 14 base plates, 18 clamp jaws, 12 flanges, 24 gussets, 14 hex covers, 30 links on 60 x 120) bands end the layout at 102.9 (104.6 with the desktop's edge spacing and a bottom clamp rail) rather than 107.7 (108.6); with -about twice those quantities over two sheets, strips end the second sheet at 104.7 rather than 106.2. It +about twice those quantities over two sheets, strips end the second sheet at 99.9 rather than 106.2, +and on the help tutorial's parts at 33.6 rather than 34.9. It starts while the work spent stays under three work budgets and replaces the result only by beating it, with its own tail re-plan.