mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 10:39:26 -05:00
The 5k-washers corpus plate planned in about 90 s alone and timed out at the corpus's 120 s limit whenever anything else ran, making the corpus fragile. Profiling showed nearly all of it was linear scans over every part, repeated per candidate: each lead check walked all 5,742 materials (and first re-tested that all were complete), each rapid walked every completed contour, each expansion rebuilt a HashSet of every cut part, each checker copy regrew its obstacle list, and the nested-part search tested all 33 million part pairs. A uniform grid over extents (ExtentGrid) now leaves out only parts whose extents lie beyond the margin the existing skip tests use, and the rest are visited in their original order, so every check returns exactly what the full scan returned (same findings, same first obstacle, same budget charges): - LeadPathValidator visits only materials not clear of the lead's queries or its midpoint's row, through a shared LeadMaterialSet built once per walk and replay; ContourEntryFeasibility reuses that set instead of copying it per part. - ReleasedContourState keeps a read-only obstacle prefix with a grid, shared by copies, so copies stay cheap and rapids test only nearby contours. - The search keeps one ordinal flag array per order instead of a HashSet per call. - CuttingDependencyGraph tests each part only against hosts whose bounds can hold it. Plate 1 alone: 93.3 s -> 12.3 s (capture 2.7 -> 1.2 s, plan 90.6 -> 11.1 s), with the identical plan digest and 86,245 expansions; plate 2 also plans identically. The problem-nest corpus (part-order,entries) gives every plate the same outcome and expansion count as master, and the Collision kernel and every tolerance are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
4.6 KiB
C#
106 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace OpenNest.Diagnostics;
|
|
|
|
/// <summary>
|
|
/// A uniform grid over many extents, for scans that skip every extent clear of a query, so a
|
|
/// plate of thousands of parts does not test each one for every rapid or lead. It returns in
|
|
/// ascending order every extent whose cells a query widened by a margin reaches, and every extent
|
|
/// that is not well conditioned. An extent it leaves out is well conditioned and lies more than
|
|
/// the margin beyond the query along X or Y, so a scan that skips only such extents, and keeps
|
|
/// the order, gives the full scan's result. Read only once built, so scans may share it.
|
|
/// </summary>
|
|
internal sealed class ExtentGrid
|
|
{
|
|
private readonly int[] always;
|
|
private readonly List<int>[] cells;
|
|
private readonly int columns;
|
|
private readonly int rows;
|
|
private readonly double width;
|
|
private readonly double height;
|
|
|
|
internal ExtentGrid(IReadOnlyList<PostVerificationGeometry.Extent> extents)
|
|
{
|
|
Count = extents.Count;
|
|
var always = new List<int>();
|
|
var whole = PostVerificationGeometry.Extent.None;
|
|
for (var i = 0; i < extents.Count; i++)
|
|
{
|
|
if (extents[i].IsWellConditioned)
|
|
whole = whole.Union(extents[i]);
|
|
else
|
|
always.Add(i);
|
|
}
|
|
this.always = always.ToArray();
|
|
Whole = whole;
|
|
var indexed = extents.Count - always.Count;
|
|
if (indexed == 0)
|
|
{
|
|
cells = [];
|
|
return;
|
|
}
|
|
columns = rows = System.Math.Max(1, (int)System.Math.Ceiling(System.Math.Sqrt(indexed)));
|
|
width = Size(whole.MaxX - whole.MinX, columns);
|
|
height = Size(whole.MaxY - whole.MinY, rows);
|
|
cells = new List<int>[columns * rows];
|
|
for (var i = 0; i < extents.Count; i++)
|
|
{
|
|
var extent = extents[i];
|
|
if (!extent.IsWellConditioned)
|
|
continue;
|
|
for (var y = Row(extent.MinY); y <= Row(extent.MaxY); y++)
|
|
for (var x = Column(extent.MinX); x <= Column(extent.MaxX); x++)
|
|
(cells[y * columns + x] ??= new List<int>()).Add(i);
|
|
}
|
|
}
|
|
|
|
/// <summary>The number of extents indexed, including those not well conditioned.</summary>
|
|
internal int Count { get; }
|
|
|
|
/// <summary>The union of every well-conditioned extent; <see cref="PostVerificationGeometry.Extent.None"/> when there is none.</summary>
|
|
internal PostVerificationGeometry.Extent Whole { get; }
|
|
|
|
/// <summary>
|
|
/// The indices, ascending, of every extent not provably more than <paramref name="margin"/>
|
|
/// beyond <paramref name="query"/> along X or Y. The caller passes a well-conditioned query
|
|
/// and a finite, nonnegative margin.
|
|
/// </summary>
|
|
internal List<int> Near(PostVerificationGeometry.Extent query, double margin) => cells.Length == 0
|
|
? new List<int>(always)
|
|
: Collect(Row(query.MinY - margin), Row(query.MaxY + margin),
|
|
Column(query.MinX - margin), Column(query.MaxX + margin));
|
|
|
|
/// <summary>
|
|
/// The indices, ascending, of every extent not provably more than <paramref name="margin"/>
|
|
/// above <paramref name="maxY"/> or below <paramref name="minY"/>, whatever its X. The caller
|
|
/// passes finite values and a nonnegative margin.
|
|
/// </summary>
|
|
internal List<int> InRows(double minY, double maxY, double margin) => cells.Length == 0
|
|
? new List<int>(always)
|
|
: Collect(Row(minY - margin), Row(maxY + margin), 0, columns - 1);
|
|
|
|
private List<int> Collect(int y0, int y1, int x0, int x1)
|
|
{
|
|
var found = new List<int>(always);
|
|
for (var y = y0; y <= y1; y++)
|
|
for (var x = x0; x <= x1; x++)
|
|
if (cells[y * columns + x] is { } cell)
|
|
found.AddRange(cell);
|
|
found.Sort();
|
|
var unique = 0;
|
|
for (var i = 0; i < found.Count; i++)
|
|
if (i == 0 || found[i] != found[i - 1])
|
|
found[unique++] = found[i];
|
|
found.RemoveRange(unique, found.Count - unique);
|
|
return found;
|
|
}
|
|
|
|
// A degenerate span (every extent at one coordinate) takes any positive cell size.
|
|
private static double Size(double span, int count) => span / count is var size && size > 0 ? size : 1;
|
|
|
|
// Monotone in the coordinate, so a cell range left out lies wholly beyond the query's.
|
|
private int Column(double x) => (int)System.Math.Clamp(System.Math.Floor((x - Whole.MinX) / width), 0, columns - 1);
|
|
|
|
private int Row(double y) => (int)System.Math.Clamp(System.Math.Floor((y - Whole.MinY) / height), 0, rows - 1);
|
|
}
|