mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-26 07:37:39 -05:00
feat(cutouts): bundle every repeated ring and seat inserts against the bore wall
The 32-bundle limit now counts only bundles the lattice and NFP searches were consulted for; round inserts in round holes are packed analytically and are bundled up to 4,096. A round insert sits against the wall at the hole's lowest point unless the centered layout seats more, so the room left is one crescent that smaller round parts fill. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
d37cc93371
commit
5af42230c2
5 files changed
+118
-35
No files matched your search
@@ -13,15 +13,16 @@ public class CutoutCirclePackerTests
|
||||
private const double Spacing = 0.25;
|
||||
|
||||
[Fact]
|
||||
public void SingleInsertSitsConcentric()
|
||||
public void SingleInsertSitsAgainstTheWallAtTheLowestPoint()
|
||||
{
|
||||
var frame = JobBuilder.Part("frame", TestShapes.Ring(20, 10), 1);
|
||||
var insert = JobBuilder.Part("disc", TestShapes.Disc(3), 1);
|
||||
|
||||
var pose = Assert.Single(CutoutCirclePacker.Fill(frame, 0, new[] { insert }, Spacing));
|
||||
|
||||
// A 3 radius disc in a 5 radius bore: the spacing from the wall, less the margins.
|
||||
Assert.Equal(0, pose.X, 6);
|
||||
Assert.Equal(0, pose.Y, 6);
|
||||
Assert.InRange(pose.Y, -1.75, -1.7);
|
||||
AssertPhysical(frame, new[] { (insert, new Vector()) }, new[] { pose });
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,44 @@ public class CutoutPipelineChainTests
|
||||
return builder.Build(complete ? NestJobStopReason.Completed : NestJobStopReason.NoPlacementFound);
|
||||
}
|
||||
|
||||
private static List<NestJobPart> BundledParts(Program frame, Program insert, int quantity)
|
||||
{
|
||||
var request = new NestPipelineRequest("stub",
|
||||
new[] { frame, insert }.Select((p, i) =>
|
||||
new NestItem { Drawing = new Drawing($"d{i}", p), Quantity = quantity }).ToArray(),
|
||||
new[] { new NestPlateStock("sheet", new Size(Sheet, Sheet), null, 0.25) });
|
||||
var engine = new Stub(job => new NestJobResultBuilder(job).Build(NestJobStopReason.NoPlacementFound));
|
||||
NestPipeline.RunCutoutTrial(engine, "stub", request);
|
||||
return Assert.Single(engine.Jobs).Parts.ToList();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EveryRepeatedRingTakesItsDisc()
|
||||
{
|
||||
// Discs in round bores are packed analytically, so the limit on searched bundles does
|
||||
// not stop a job of repeated rings short.
|
||||
var parts = BundledParts(TestShapes.Ring(10, 6), TestShapes.Disc(2.5), 40);
|
||||
|
||||
Assert.Equal(40, parts.Count);
|
||||
Assert.All(parts, p => Assert.StartsWith("__cutout-proxy-part-0-", p.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SearchedBundlesStopAtTheLimit()
|
||||
{
|
||||
// A square in a square window needs the lattice and NFP searches: 32 frames are
|
||||
// bundled and the rest of the demand stays ordinary.
|
||||
var frame = TestShapes.Rectangle(8, 8);
|
||||
frame.Codes.Add(new RapidMove(2, 2));
|
||||
foreach (var (x, y) in new[] { (6.0, 2.0), (6.0, 6.0), (2.0, 6.0), (2.0, 2.0) })
|
||||
frame.Codes.Add(new LinearMove(x, y));
|
||||
var parts = BundledParts(frame, TestShapes.Rectangle(3, 3), 40);
|
||||
|
||||
Assert.Equal(32, parts.Count(p => p.Id.StartsWith("__cutout-proxy-")));
|
||||
Assert.Equal(8, parts.Single(p => p.Id == "part-0").Quantity);
|
||||
Assert.Equal(8, parts.Single(p => p.Id == "part-1").Quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FourLevelChainIsOneBundleThatCutsInnermostFirst()
|
||||
{
|
||||
@@ -58,9 +96,10 @@ public class CutoutPipelineChainTests
|
||||
Assert.Equal(NestJobStatus.Complete, result.Status);
|
||||
Assert.Equal(new[] { "part-3", "part-2", "part-1", "part-0" },
|
||||
Assert.Single(result.Raw.Plates).Placements.Select(p => p.PartId));
|
||||
// A single insert sits concentric at every level.
|
||||
Assert.All(result.Raw.Plates[0].Placements, p =>
|
||||
Assert.True(new Vector(p.X, p.Y).DistanceTo(new Vector(Sheet / 2, Sheet / 2)) < 1e-6));
|
||||
// A single insert sits against its frame's bore at the lowest point, at every level.
|
||||
var placements = result.Raw.Plates[0].Placements;
|
||||
Assert.All(placements, p => Assert.Equal(Sheet / 2, p.X, 6));
|
||||
Assert.All(placements.Zip(placements.Skip(1)), pair => Assert.True(pair.First.Y < pair.Second.Y - 0.5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -13,10 +13,12 @@ namespace OpenNest.Engine.Jobs.Cutouts;
|
||||
/// circle (<see cref="Item"/>), so the hole and inserts are discs and placement is analytic.
|
||||
/// Never changes demand, stock or a live plate.</summary>
|
||||
/// <remarks>Two greedy passes over the copies, largest first, choose among candidate centers
|
||||
/// tangent to two of {hole wall, placed discs} plus the hole's center. One grows from the
|
||||
/// center, so a single insert sits concentric and keeps its own bore free for a later level;
|
||||
/// the other lays rings against the wall first, which reproduces the ring-shaped optimal
|
||||
/// packings of up to nine equal circles. The higher count wins, ties keep the centered pass.
|
||||
/// tangent to two of {hole wall, placed discs} plus the hole's center. One lays discs against
|
||||
/// the wall first, starting at the hole's lowest point, which reproduces the ring-shaped optimal
|
||||
/// packings of up to nine equal circles and leaves the room a large insert does not use as one
|
||||
/// crescent that smaller inserts fill; the other grows from the center, which wins where a
|
||||
/// center disc and a ring around it hold more. The pass seating more area wins, ties keep the
|
||||
/// wall pass, so a single insert sits against the wall at the lowest point.
|
||||
/// Every pose is certified with <see cref="NestLayoutCheck.Clears"/> before it is returned.</remarks>
|
||||
internal static class CutoutCirclePacker
|
||||
{
|
||||
@@ -120,9 +122,9 @@ internal static class CutoutCirclePacker
|
||||
discs = discs.OrderByDescending(d => d.Radius).ToList();
|
||||
|
||||
var gap = spacing + Margin;
|
||||
var centered = Greedy(discs, wall, gap, fromCenter: true, token);
|
||||
var walled = discs.Count > 1 ? Greedy(discs, wall, gap, fromCenter: false, token) : centered;
|
||||
var chosen = CutoutSeatedArea.More(Seated(walled), Seated(centered)) ? walled : centered;
|
||||
var walled = Greedy(discs, wall, gap, fromCenter: false, token);
|
||||
var centered = discs.Count > 1 ? Greedy(discs, wall, gap, fromCenter: true, token) : walled;
|
||||
var chosen = CutoutSeatedArea.More(Seated(centered), Seated(walled)) ? centered : walled;
|
||||
|
||||
var framePose = new NestJobPlacement(frame.Id, 0, 0, 0, 0);
|
||||
var accepted = new List<(Disc Disc, Vector Center, NestJobPlacement Pose)>();
|
||||
|
||||
@@ -21,7 +21,12 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
{
|
||||
// A job with thousands of repeats still offers the residual as ordinary demand;
|
||||
// do not multiply expensive Clipper/Fill preparation by every physical instance.
|
||||
private const int MaxBundlesPerJob = 32;
|
||||
// Counts the bundles the lattice and NFP searches were consulted for.
|
||||
private const int MaxSearchedBundlesPerJob = 32;
|
||||
|
||||
// Bundles packed as discs alone cost a few analytic passes each, so a job of repeated
|
||||
// rings nests them all; this only bounds the preparation of a very large job.
|
||||
private const int MaxBundlesPerJob = 4096;
|
||||
|
||||
/// <summary>Most parts on one frame-in-frame chain: flange, washer, smaller washer, slug.</summary>
|
||||
internal const int MaxLevels = 4;
|
||||
@@ -80,7 +85,9 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
// rotation policies, not on the frame angle itself: automatic-rotation inserts ask the
|
||||
// same question at every angle, and so does every instance of the same frame while the
|
||||
// remaining demand is unchanged. Replay those answers.
|
||||
var proposals = new Dictionary<string, IReadOnlyList<NestJobPlacement>>(StringComparer.Ordinal);
|
||||
var proposals = new Dictionary<string, (IReadOnlyList<NestJobPlacement> Poses, bool Searched)>(
|
||||
StringComparer.Ordinal);
|
||||
var searchedBundles = 0;
|
||||
// Smallest first: a frame can only hold smaller parts, so its candidate inner bundles
|
||||
// already exist, and small holes take small inserts before large holes are tried.
|
||||
var frames = original.Parts.Where(p => geometry[p.Id] is { Cutouts.Count: > 0 })
|
||||
@@ -101,6 +108,9 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
break;
|
||||
IReadOnlyList<NestJobPlacement>? chosen = null;
|
||||
double frameAngle = 0;
|
||||
// Past the limit a hole is offered to the disc packer alone.
|
||||
var search = searchedBundles < MaxSearchedBundlesPerJob;
|
||||
var searched = false;
|
||||
// Outer-perimeter symmetry cannot deduplicate asymmetric cutouts.
|
||||
foreach (var angle in frame.Rotation.EnumerateAngles(maxSamples: 16))
|
||||
{
|
||||
@@ -127,7 +137,7 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
: eligible.Where(p => round[p.Id] && !solid.GetValueOrDefault(p.Id)).ToArray();
|
||||
if (flat.Length == 0 && nested.Length == 0)
|
||||
continue;
|
||||
foreach (var pose in Propose(frame, hole, flat, nested))
|
||||
foreach (var pose in Propose(frame, hole, flat, nested, search, ref searched))
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var source = geometry[pose.PartId]!;
|
||||
@@ -151,6 +161,8 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
throw new InvalidOperationException("Cutout proxy ID collides with a requirement.");
|
||||
var levels = 1 + chosen.Max(p => bundles.TryGetValue(p.PartId, out var b) ? b.Levels : 1);
|
||||
bundles.Add(proxyId, new Bundle(frame.Id, chosen, levels, outline[frame.Id] + Weight(chosen)));
|
||||
if (searched)
|
||||
searchedBundles++;
|
||||
var proxy = new NestJobPart(proxyId, frame.Geometry, 1, frame.Priority,
|
||||
RotationPolicy.Fixed(frameAngle));
|
||||
proxies.Add(proxy);
|
||||
@@ -182,19 +194,25 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
|
||||
// Ties keep the flat router's proposal, which predates nesting.
|
||||
IReadOnlyList<NestJobPlacement> Propose(NestJobPart frame, int hole,
|
||||
NestJobPart[] flat, NestJobPart[] nested)
|
||||
NestJobPart[] flat, NestJobPart[] nested, bool search, ref bool searched)
|
||||
{
|
||||
var key = ProposalKey(frame, hole, flat, nested);
|
||||
if (proposals.TryGetValue(key, out var replay))
|
||||
return replay;
|
||||
var proposed = ProposeCore(frame, hole, flat, nested);
|
||||
proposals[key] = proposed;
|
||||
return proposed;
|
||||
var key = ProposalKey(frame, hole, flat, nested, search);
|
||||
if (!proposals.TryGetValue(key, out var proposed))
|
||||
{
|
||||
proposed = ProposeCore(frame, hole, flat, nested, search);
|
||||
proposals[key] = proposed;
|
||||
}
|
||||
searched |= proposed.Searched;
|
||||
return proposed.Poses;
|
||||
}
|
||||
|
||||
IReadOnlyList<NestJobPlacement> ProposeCore(NestJobPart frame, int hole,
|
||||
NestJobPart[] flat, NestJobPart[] nested)
|
||||
(IReadOnlyList<NestJobPlacement> Poses, bool Searched) ProposeCore(NestJobPart frame, int hole,
|
||||
NestJobPart[] flat, NestJobPart[] nested, bool search)
|
||||
{
|
||||
// A round hole whose flat inserts are all round is the disc packer's alone, inside
|
||||
// the router too: no lattice or NFP search runs for it.
|
||||
var discs = CutoutCirclePacker.RoundHole(geometry[frame.Id]!.Cutouts[hole]) != null
|
||||
&& flat.All(p => round[p.Id]);
|
||||
IReadOnlyList<NestJobPlacement> poses = Array.Empty<NestJobPlacement>();
|
||||
if (nested.Length > 0)
|
||||
poses = CutoutCirclePacker.Fill(frame, hole,
|
||||
@@ -202,10 +220,11 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
// Seating every flat copy as a disc leaves the slower proposals nothing to beat.
|
||||
var flatIds = flat.Select(p => p.Id).ToHashSet(StringComparer.Ordinal);
|
||||
if (flat.Length == 0
|
||||
|| poses.Count(p => flatIds.Contains(p.PartId)) == flat.Sum(p => (long)p.Quantity))
|
||||
return poses;
|
||||
|| poses.Count(p => flatIds.Contains(p.PartId)) == flat.Sum(p => (long)p.Quantity)
|
||||
|| !(search || discs))
|
||||
return (poses, false);
|
||||
var router = CutoutRouter.Fill(frame, hole, flat, spacing, memo, token);
|
||||
return CutoutSeatedArea.More(Weight(poses), Weight(router)) ? poses : router;
|
||||
return (CutoutSeatedArea.More(Weight(poses), Weight(router)) ? poses : router, !discs);
|
||||
}
|
||||
|
||||
// Outline area a proposal seats: a nested bundle counts its frame and contents.
|
||||
@@ -213,10 +232,11 @@ internal sealed class CutoutPipelinePrepass : IPipelinePrepass
|
||||
poses.Sum(p => bundles.TryGetValue(p.PartId, out var b) ? b.Area : outline[p.PartId]);
|
||||
}
|
||||
|
||||
private static string ProposalKey(NestJobPart frame, int hole, NestJobPart[] flat, NestJobPart[] nested)
|
||||
private static string ProposalKey(NestJobPart frame, int hole, NestJobPart[] flat, NestJobPart[] nested,
|
||||
bool search)
|
||||
{
|
||||
var key = new System.Text.StringBuilder();
|
||||
key.Append(frame.Id).Append('|').Append(hole);
|
||||
key.Append(frame.Id).Append('|').Append(hole).Append(search ? "|s" : "|d");
|
||||
foreach (var part in flat)
|
||||
Append(part, 'f');
|
||||
foreach (var part in nested)
|
||||
|
||||
+27
-6
@@ -671,6 +671,19 @@ Default ranks candidates (valid, fewest unplaced, lowest cost). The last sheet,
|
||||
sheets together, are then re-packed onto each single stock and replaced when that lowers the
|
||||
whole job's cost, so a small remainder does not finish on a large sheet.
|
||||
|
||||
Largest first sets two large discs side by side before any smaller one is placed, so a disc that
|
||||
would fit between them, were they a little further apart, ends up beyond both. Rows and columns
|
||||
therefore each run once more with the order searched (`DiscPacker.PackSqueezed`): the sheet's
|
||||
discs are placed again by the same rule in other orders, taking the best move of one disc to
|
||||
another position while a move makes the layout end sooner along the fill axis, and the room that
|
||||
frees is refilled from the remaining demand (at most three rounds). Only sheets of 24 discs or
|
||||
fewer are searched, within 600 decodes, and a sheet no order shortens offers no extra layout. A
|
||||
shortened layout is listed first, along the sheet's longer side first, so it wins an exact tie
|
||||
with the layout it shortens; a plain layout that leaves a larger compactness offcut still wins.
|
||||
A 54, a 53 and a 20 inch disc on 60 x 120 at 0.25 spacing end at 111.1 with the 20 against the
|
||||
top edge between the other two, rather than at the end of the sheet, and a 21 inch disc then
|
||||
fits on the same sheet.
|
||||
|
||||
## Filling cutouts
|
||||
|
||||
Every whole-job `NestPipeline.Run` (desktop Auto Nest, Console, MCP and API) first places parts
|
||||
@@ -749,9 +762,11 @@ more outline area than the others.
|
||||
When the cutout is one circle and every insert fills at least 0.8 of its enclosing circle
|
||||
(discs, washers, gears by their tip circle), `CutoutCirclePacker` first packs the whole insert
|
||||
list as discs: largest first, at centers tangent to two of the hole wall and the placed discs,
|
||||
or at the hole's center. It tries a center-out pass, so a single insert sits concentric, and a
|
||||
wall-first pass, which reaches the optimal counts for up to nine equal discs, and the pass
|
||||
seating more outline area wins, the center-out pass on a tie. Each gap is the
|
||||
or at the hole's center. It tries a wall-first pass, which starts at the hole's lowest point and
|
||||
reaches the optimal counts for up to nine equal discs, and a center-out pass, and the pass
|
||||
seating more outline area wins, the wall-first pass on a tie. So a single insert sits against the
|
||||
wall at the lowest point, not concentric, and the room it leaves is one crescent that smaller
|
||||
round parts fill, largest first. Each gap is the
|
||||
spacing plus twice `NestTolerances.ValidationOutline`, and every pose passes `NestLayoutCheck`.
|
||||
Its result is the router's proposal for that hole: the lattice and NFP searches never placed
|
||||
more there and were slow on round inserts, most of all when demand exceeds what fits.
|
||||
@@ -781,9 +796,15 @@ then every physical pose, stock, rotation, quantity and fulfillment is independe
|
||||
before binding caller drawings. Forged or geometrically invalid composites return no bindable
|
||||
plates even with invalid-result consent. Candidate evaluation progress is forwarded
|
||||
with unverified proxy commitment counts zeroed; transformed proxy commits are hidden. Physical
|
||||
commits are reported only after physical validation. Preparation is bounded to 32 bundles
|
||||
and 16 base sweep-angle samples per frame (automatic quarter-turns are all retained);
|
||||
remaining copies stay ordinary requirements. Within one preparation, geometry reads and
|
||||
commits are reported only after physical validation. Preparation is bounded to 16 base
|
||||
sweep-angle samples per frame (automatic quarter-turns are all retained) and to 32 bundles that
|
||||
the lattice and NFP searches were consulted for; past that a hole is offered to the disc packer
|
||||
alone. Round inserts in a round hole are packed analytically and do not count, so a job of
|
||||
repeated rings bundles every copy, up to 4,096 bundles; 20 each of 58 ring sizes seats 700 of its
|
||||
1,160 rings in bores, on 160 sheets of 60 x 120 where 32 bundles left 217. Remaining copies stay
|
||||
ordinary requirements. The spacing check answers a part lying wholly inside another's cutout
|
||||
without its triangle test (`SpacingOutline.Overlaps`), which a seated ring made as slow as it
|
||||
gets: every triangle pair overlaps and each piece is subtracted by the whole cutout. Within one preparation, geometry reads and
|
||||
`NestLayoutCheck.Clears` answers are replayed (`CutoutMemo`: same geometry, bit-identical poses),
|
||||
so repeated frame instances and frame angles do not repeat that check, its main cost; layouts are
|
||||
the same as without the replay. A hole's proposal is also replayed whenever the same frame, hole
|
||||
|
||||
Reference in new issue
Block a user