using System.Collections.Generic;
namespace OpenNest.Diagnostics;
///
/// 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.
///
internal sealed class ExtentGrid
{
private readonly int[] always;
private readonly List[] cells;
private readonly int columns;
private readonly int rows;
private readonly double width;
private readonly double height;
internal ExtentGrid(IReadOnlyList extents)
{
Count = extents.Count;
var always = new List();
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[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()).Add(i);
}
}
/// The number of extents indexed, including those not well conditioned.
internal int Count { get; }
/// The union of every well-conditioned extent; when there is none.
internal PostVerificationGeometry.Extent Whole { get; }
///
/// The indices, ascending, of every extent not provably more than
/// beyond along X or Y. The caller passes a well-conditioned query
/// and a finite, nonnegative margin.
///
internal List Near(PostVerificationGeometry.Extent query, double margin) => cells.Length == 0
? new List(always)
: Collect(Row(query.MinY - margin), Row(query.MaxY + margin),
Column(query.MinX - margin), Column(query.MaxX + margin));
///
/// The indices, ascending, of every extent not provably more than
/// above or below , whatever its X. The caller
/// passes finite values and a nonnegative margin.
///
internal List InRows(double minY, double maxY, double margin) => cells.Length == 0
? new List(always)
: Collect(Row(minY - margin), Row(maxY + margin), 0, columns - 1);
private List Collect(int y0, int y1, int x0, int x1)
{
var found = new List(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);
}