fix(geometry): stop PolyLabel refinement when centres cannot advance

This commit is contained in:
aj committed 2026-10-15 23:31:14 -04:00
1 parent 1672b62717
commit 12f2ce5deb
2 files changed
+19

No files matched your search

+6
View File
@@ -120,6 +120,12 @@ namespace OpenNest.Geometry
halfCell = cell.HalfSize / 2;
// Far from the origin a half-step may no longer move the centre; such a cell
// cannot be split, and its identical children would multiply without end.
if (cell.X - halfCell == cell.X || cell.X + halfCell == cell.X
|| cell.Y - halfCell == cell.Y || cell.Y + halfCell == cell.Y)
continue;
var newCells = new[]
{
new Cell(cell.X - halfCell, cell.Y - halfCell, halfCell, outer, holes),
@@ -83,6 +83,19 @@ public class PolyLabelDifferentialTests
Assert.InRange(unbounded.Y, 0, 1);
}
// Delta review: past the grid, refining a cell whose half-step no longer moves its centre
// made four identical children per level, so a fine unbounded search exhausted memory.
[Fact]
public async Task Find_UnboundedRefinementFinishesWhenSplitsStopAdvancing()
{
var far = Rectangle(1e16, 0, 8, 1);
var search = Task.Run(() => PolyLabel.Find(far, null, 1e-6));
Assert.Same(search, await Task.WhenAny(search, Task.Delay(TimeSpan.FromSeconds(20))));
Assert.InRange((await search).Y, 0, 1);
}
private static (Polygon outer, List<Polygon> holes) RandomShape(Random random)
{
var scale = System.Math.Pow(10, random.NextDouble() * 4 - 1);