mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-29 11:21:34 -05:00
perf(engine): run pair grid overlap checks only where the ranking needs them
FillLinear.FillDeferred returns a grid whose final overlap check has not run; Resolve runs it and returns exactly what Fill returns, and Fill is FillDeferred(...).Resolve(). PairFiller sorts a candidate's grids with CompareByCount, which answers from the counts a pending grid can still have (its tiling or its bounding-box fallback) and runs the check only when they could order the pair differently; the early abort and the remnant pass resolve the grids they use. Every answer equals the checked comparison, so List.Sort makes the same moves, ties included, and layouts are bit-identical. A check that throws during the sort surfaces unwrapped. On the bracket fill this skips about 35% of the pair grids' final checks by work. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
538ccebd6d
commit
683ece4d3c
4 files changed
+400
-38
No files matched your search
@@ -421,17 +421,35 @@ namespace OpenNest.Engine.Fill
|
||||
/// produces overlapping parts.
|
||||
/// </summary>
|
||||
private List<Part> TilePatternBbox(Pattern basePattern, NestDirection direction)
|
||||
{
|
||||
var copies = BboxCopies(basePattern, direction);
|
||||
var copyDistance = GetDimension(basePattern.BoundingBox, direction) + PartSpacing;
|
||||
var result = new List<Part>();
|
||||
|
||||
for (var count = 1; count <= copies; count++)
|
||||
{
|
||||
var offset = MakeOffset(direction, copyDistance * count);
|
||||
|
||||
foreach (var part in basePattern.Parts)
|
||||
result.Add(part.CloneAtOffset(offset));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The number of whole copies <see cref="TilePatternBbox"/> adds, found without cloning.
|
||||
/// </summary>
|
||||
private int BboxCopies(Pattern basePattern, NestDirection direction)
|
||||
{
|
||||
var copyDistance = GetDimension(basePattern.BoundingBox, direction) + PartSpacing;
|
||||
|
||||
if (copyDistance <= 0)
|
||||
return new List<Part>();
|
||||
return 0;
|
||||
|
||||
var dim = GetDimension(basePattern.BoundingBox, direction);
|
||||
var start = GetStart(basePattern.BoundingBox, direction);
|
||||
var limit = GetLimit(direction);
|
||||
|
||||
var result = new List<Part>();
|
||||
var count = 1;
|
||||
|
||||
while (true)
|
||||
@@ -441,15 +459,10 @@ namespace OpenNest.Engine.Fill
|
||||
if (nextPos + dim > limit + Tolerance.Epsilon)
|
||||
break;
|
||||
|
||||
var offset = MakeOffset(direction, copyDistance * count);
|
||||
|
||||
foreach (var part in basePattern.Parts)
|
||||
result.Add(part.CloneAtOffset(offset));
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
return result;
|
||||
return count - 1;
|
||||
}
|
||||
|
||||
private static bool HasOverlappingParts(
|
||||
@@ -521,7 +534,11 @@ namespace OpenNest.Engine.Fill
|
||||
/// a row, then tiling that row along the perpendicular axis to form a grid.
|
||||
/// After the grid is formed, fills the remaining strip with individual parts.
|
||||
/// </summary>
|
||||
private List<Part> FillGrid(Pattern pattern, NestDirection direction, OffsetPerimeterCache cache)
|
||||
/// <remarks>
|
||||
/// Step 1's check runs here, as it decides how the grid is built. The final check, on the
|
||||
/// whole grid, is left to the returned grid's <see cref="DeferredGrid.Resolve"/>.
|
||||
/// </remarks>
|
||||
private DeferredGrid FillGrid(Pattern pattern, NestDirection direction, OffsetPerimeterCache cache)
|
||||
{
|
||||
var perpAxis = PerpendicularAxis(direction);
|
||||
|
||||
@@ -547,21 +564,16 @@ namespace OpenNest.Engine.Fill
|
||||
{
|
||||
row = Tile(pattern, perpAxis, cache, MaxParts);
|
||||
|
||||
if (pattern.Parts.Count > 1 && HasOverlappingParts(row, out var a2, out var b2))
|
||||
{
|
||||
LogOverlap("Step1-PerpOnly", perpAxis, pattern, row, a2, b2);
|
||||
row = new List<Part>(pattern.Parts);
|
||||
row.AddRange(TilePatternBbox(pattern, perpAxis));
|
||||
}
|
||||
|
||||
return row;
|
||||
return pattern.Parts.Count > 1
|
||||
? new DeferredGrid(this, row, "Step1-PerpOnly", perpAxis, pattern)
|
||||
: new DeferredGrid(row);
|
||||
}
|
||||
|
||||
// A capped fill whose first row already holds enough parts needs no more rows.
|
||||
// Step 2's check could only switch perpendicular tiling to the bbox fallback; it
|
||||
// never repairs the row itself, so returning the row loses no protection.
|
||||
if (MaxParts > 0 && row.Count >= MaxParts)
|
||||
return row;
|
||||
return new DeferredGrid(row);
|
||||
|
||||
// Step 2: Build row pattern and tile along perpendicular axis
|
||||
var rowPattern = new Pattern();
|
||||
@@ -573,15 +585,83 @@ namespace OpenNest.Engine.Fill
|
||||
// Only the unchanged row is covered by Step 1's clean verdict: skip Step 2
|
||||
// only when the perpendicular tiling appended zero parts, so gridResult
|
||||
// still contains the same Part objects, in the same order and poses.
|
||||
if ((!rowIsVerified || gridResult.Count != row.Count)
|
||||
&& HasOverlappingParts(gridResult, out var a3, out var b3))
|
||||
return !rowIsVerified || gridResult.Count != row.Count
|
||||
? new DeferredGrid(this, gridResult, "Step2-Perp", perpAxis, rowPattern)
|
||||
: new DeferredGrid(gridResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A grid tiled by <see cref="FillGrid"/> whose final overlap check may not have run yet.
|
||||
/// Until <see cref="Resolve"/> runs it, the grid holds either its tiling, when no two
|
||||
/// parts overlap, or the fallback that tiles the same pattern by bounding boxes
|
||||
/// (<see cref="TilePatternBbox"/>); both part counts are known without the check.
|
||||
/// <see cref="Resolve"/> returns exactly what the eager fill returned, so a caller may
|
||||
/// skip the check only on a grid whose outcome it never uses. Not thread-safe.
|
||||
/// </summary>
|
||||
internal sealed class DeferredGrid
|
||||
{
|
||||
private readonly FillLinear owner;
|
||||
private readonly string step;
|
||||
private readonly NestDirection axis;
|
||||
private Pattern basePattern;
|
||||
private List<Part> parts;
|
||||
private int? fallbackCount;
|
||||
|
||||
/// <summary>A grid with no check left to run.</summary>
|
||||
internal DeferredGrid(List<Part> parts) => this.parts = parts;
|
||||
|
||||
/// <summary>
|
||||
/// A grid whose tiling <paramref name="parts"/> of <paramref name="basePattern"/> along
|
||||
/// <paramref name="axis"/> still has to pass the overlap check.
|
||||
/// </summary>
|
||||
internal DeferredGrid(
|
||||
FillLinear owner,
|
||||
List<Part> parts,
|
||||
string step,
|
||||
NestDirection axis,
|
||||
Pattern basePattern
|
||||
)
|
||||
{
|
||||
LogOverlap("Step2-Perp", perpAxis, rowPattern, gridResult, a3, b3);
|
||||
gridResult = new List<Part>(rowPattern.Parts);
|
||||
gridResult.AddRange(TilePatternBbox(rowPattern, perpAxis));
|
||||
this.owner = owner;
|
||||
this.parts = parts;
|
||||
this.step = step;
|
||||
this.axis = axis;
|
||||
this.basePattern = basePattern;
|
||||
}
|
||||
|
||||
return gridResult;
|
||||
/// <summary>True until <see cref="Resolve"/> has run the overlap check.</summary>
|
||||
public bool IsPending => basePattern != null;
|
||||
|
||||
/// <summary>
|
||||
/// The part count of the grid as it stands: its tiling while the check is pending, the
|
||||
/// checked parts once resolved.
|
||||
/// </summary>
|
||||
public int Count => parts.Count;
|
||||
|
||||
/// <summary>
|
||||
/// While <see cref="IsPending"/>, the part count of the bounding-box fallback, should
|
||||
/// the check fail.
|
||||
/// </summary>
|
||||
public int FallbackCount =>
|
||||
fallbackCount ??= (1 + owner.BboxCopies(basePattern, axis)) * basePattern.Parts.Count;
|
||||
|
||||
/// <summary>Runs the overlap check if it is pending and returns the grid's parts.</summary>
|
||||
public List<Part> Resolve()
|
||||
{
|
||||
if (basePattern == null)
|
||||
return parts;
|
||||
|
||||
if (HasOverlappingParts(parts, out var a, out var b))
|
||||
{
|
||||
owner.LogOverlap(step, axis, basePattern, parts, a, b);
|
||||
var fallback = new List<Part>(basePattern.Parts);
|
||||
fallback.AddRange(owner.TilePatternBbox(basePattern, axis));
|
||||
parts = fallback;
|
||||
}
|
||||
|
||||
basePattern = null;
|
||||
return parts;
|
||||
}
|
||||
}
|
||||
|
||||
private void LogOverlap(
|
||||
@@ -672,11 +752,18 @@ namespace OpenNest.Engine.Fill
|
||||
/// <summary>
|
||||
/// Fills the work area by tiling a pre-built pattern along both axes.
|
||||
/// </summary>
|
||||
public List<Part> Fill(Pattern pattern, NestDirection primaryAxis)
|
||||
public List<Part> Fill(Pattern pattern, NestDirection primaryAxis) =>
|
||||
FillDeferred(pattern, primaryAxis).Resolve();
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Fill(Pattern, NestDirection)"/> with the grid's final overlap check left to
|
||||
/// the caller: <see cref="DeferredGrid.Resolve"/> returns exactly what Fill returns.
|
||||
/// </summary>
|
||||
internal DeferredGrid FillDeferred(Pattern pattern, NestDirection primaryAxis)
|
||||
{
|
||||
var cache = new OffsetPerimeterCache(HalfSpacing);
|
||||
if (pattern.Parts.Count == 0)
|
||||
return new List<Part>();
|
||||
return new DeferredGrid(new List<Part>());
|
||||
|
||||
var offset = WorkArea.Location - pattern.BoundingBox.Location;
|
||||
var basePattern = pattern.Clone(offset);
|
||||
@@ -685,7 +772,7 @@ namespace OpenNest.Engine.Fill
|
||||
basePattern.BoundingBox.Width > WorkArea.Width + Tolerance.Epsilon
|
||||
|| basePattern.BoundingBox.Length > WorkArea.Length + Tolerance.Epsilon
|
||||
)
|
||||
return new List<Part>();
|
||||
return new DeferredGrid(new List<Part>());
|
||||
|
||||
return FillGrid(basePattern, primaryAxis, cache);
|
||||
}
|
||||
@@ -702,7 +789,7 @@ namespace OpenNest.Engine.Fill
|
||||
if (seed.Parts.Count == 0)
|
||||
return new List<Part>();
|
||||
|
||||
return FillGrid(seed, primaryAxis, cache);
|
||||
return FillGrid(seed, primaryAxis, cache).Resolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using OpenNest.Engine;
|
||||
@@ -554,30 +555,34 @@ namespace OpenNest.Engine.Fill
|
||||
CancellationToken token
|
||||
)
|
||||
{
|
||||
// Phase 1: evaluate all grids (fast)
|
||||
var grids = new List<(List<Part> Parts, NestDirection Dir)>();
|
||||
// Phase 1: tile all grids (fast). Each grid's final overlap check, the bulk of the
|
||||
// work, waits until the ranking or Phase 2 needs its outcome (see CompareByCount).
|
||||
var grids = new List<FillLinear.DeferredGrid>();
|
||||
var engine = new FillLinear(workArea, partSpacing) { Label = "Pairs" };
|
||||
foreach (var (pattern, dir) in claimed)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
var gridParts = engine.Fill(pattern, dir);
|
||||
if (gridParts != null && gridParts.Count > 0)
|
||||
grids.Add((gridParts, dir));
|
||||
var grid = engine.FillDeferred(pattern, dir);
|
||||
if (grid.IsPending && (grid.Count == 0 || grid.FallbackCount == 0))
|
||||
grid.Resolve();
|
||||
if (grid.IsPending || grid.Count > 0)
|
||||
grids.Add(grid);
|
||||
}
|
||||
|
||||
if (grids.Count == 0)
|
||||
return null;
|
||||
|
||||
// Sort by count descending so we try the best grids first
|
||||
grids.Sort((a, b) => b.Parts.Count.CompareTo(a.Parts.Count));
|
||||
SortByCount(grids);
|
||||
|
||||
// Early abort: if the best grid + optimistic remnant can't beat the global best, skip Phase 2
|
||||
if (minCountToBeat > 0)
|
||||
{
|
||||
var topCount = grids[0].Parts.Count;
|
||||
var topParts = grids[0].Resolve();
|
||||
var topCount = topParts.Count;
|
||||
var optimisticRemnant = remnantFiller.EstimateUpperBound(
|
||||
grids[0].Parts,
|
||||
topParts,
|
||||
workArea,
|
||||
maxUtilization,
|
||||
partArea
|
||||
@@ -594,10 +599,12 @@ namespace OpenNest.Engine.Fill
|
||||
// Phase 2: try remnant for each grid, skip if grid is too far behind
|
||||
List<Part> best = null;
|
||||
|
||||
foreach (var (gridParts, dir) in grids)
|
||||
foreach (var grid in grids)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
var gridParts = grid.Resolve();
|
||||
|
||||
// If this grid + max possible remnant can't beat current best, skip
|
||||
if (best != null)
|
||||
{
|
||||
@@ -631,6 +638,73 @@ namespace OpenNest.Engine.Fill
|
||||
return best;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts grids with <see cref="CompareByCount"/>. A check that throws during the sort
|
||||
/// surfaces as itself, as it did when every grid was checked before sorting, rather than
|
||||
/// wrapped by <see cref="List{T}.Sort(Comparison{T})"/>.
|
||||
/// </summary>
|
||||
internal static void SortByCount(List<FillLinear.DeferredGrid> grids)
|
||||
{
|
||||
ExceptionDispatchInfo failure = null;
|
||||
grids.Sort((a, b) =>
|
||||
{
|
||||
if (failure != null)
|
||||
return 0;
|
||||
|
||||
try
|
||||
{
|
||||
return CompareByCount(a, b);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
failure = ExceptionDispatchInfo.Capture(ex);
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
failure?.Throw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Orders grids by part count, descending, as <c>b.Count.CompareTo(a.Count)</c> on their
|
||||
/// checked parts. A grid whose check is pending may keep its tiling or fall back to its
|
||||
/// bounding-box tiling; when those counts could order the pair differently, the check
|
||||
/// runs. Every answer is the one the checked counts give, so the sort makes the same
|
||||
/// moves, ties included, as on grids checked up front, and the checks it skips are of
|
||||
/// grids it never needed to tell apart.
|
||||
/// </summary>
|
||||
internal static int CompareByCount(FillLinear.DeferredGrid a, FillLinear.DeferredGrid b)
|
||||
{
|
||||
if (ReferenceEquals(a, b))
|
||||
return 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (TryCompareByCount(a, b, out var order))
|
||||
return order;
|
||||
|
||||
if (a.IsPending)
|
||||
a.Resolve();
|
||||
else
|
||||
b.Resolve();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The order of <paramref name="a"/> and <paramref name="b"/> when every count either
|
||||
/// grid can still have gives the same one.
|
||||
/// </summary>
|
||||
private static bool TryCompareByCount(
|
||||
FillLinear.DeferredGrid a,
|
||||
FillLinear.DeferredGrid b,
|
||||
out int order
|
||||
)
|
||||
{
|
||||
order = b.Count.CompareTo(a.Count);
|
||||
return (!a.IsPending || b.Count.CompareTo(a.FallbackCount) == order)
|
||||
&& (!b.IsPending || b.FallbackCount.CompareTo(a.Count) == order)
|
||||
&& (!a.IsPending || !b.IsPending || b.FallbackCount.CompareTo(a.FallbackCount) == order);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The BuildTilingAngles
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user