fix(preview): stop part labels hanging on twin-outline drawings

A drawing of two equal, separate outlines (RC-204441A23 PT13) froze the
drawing library preview. ShapeProfile takes one outline as the perimeter
and the other as a cutout, so the net area is rounding noise and
ThicknessPrecision fell to 1e-9. A rectangle's pole is a whole ridge, so
the label search refined it into billions of cells on the UI thread.

Net area negligible next to the outline now falls back to the outline's
own width, and the paint-time label search has a cell budget so no shape
can stall painting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-02 07:50:06 -05:00
1 parent d4e622271a
commit f9b718e890
3 files changed
+45 -4

No files matched your search

+14 -3
View File
@@ -28,12 +28,16 @@ namespace OpenNest.Geometry
/// A precision proportional to the shape's mean material width (twice the net area over
/// the total outline length, holes included), so small or slender shapes refine further
/// than large solid ones. A fixed precision near the shape's size stops at the first
/// interior cell.
/// interior cell. Holes that leave no net area (a second outline mistaken for a hole, so
/// lying outside the first) fall back to the outline alone: a near-zero precision would
/// refine a pole ridge, such as a rectangle's centerline, into billions of cells.
/// </summary>
public static double ThicknessPrecision(Polygon outer, IList<Polygon> holes = null, double fraction = 0.05)
{
var area = outer.Area();
var length = ClosedLength(outer);
var outerArea = outer.Area();
var outerLength = ClosedLength(outer);
var area = outerArea;
var length = outerLength;
if (holes != null)
{
@@ -44,6 +48,13 @@ namespace OpenNest.Geometry
}
}
// Equal outlines leave rounding noise, not material.
if (!(area > outerArea * 1e-6) || !(length > 0))
{
area = outerArea;
length = outerLength;
}
if (!(area > 0) || !(length > 0))
return 1e-9;
+28
View File
@@ -153,6 +153,34 @@ public class PolyLabelTests
Assert.True(PolyLabel.ThicknessPrecision(Square(100), new[] { hole }) < PolyLabel.ThicknessPrecision(Square(100)));
}
[Fact]
public async Task ThicknessPrecision_HoleOutsideTheOutline_FallsBackToTheOutline()
{
// A drawing of two equal rectangles side by side: the profile takes one as the
// perimeter and the other as a cutout, so the net area is zero. A near-zero precision
// refined the rectangle's whole centerline ridge and never finished.
static Polygon Rect(double x, double length, double width)
{
var p = new Polygon();
p.Vertices.Add(new Vector(x, 0));
p.Vertices.Add(new Vector(x + length, 0));
p.Vertices.Add(new Vector(x + length, width));
p.Vertices.Add(new Vector(x, width));
return p;
}
var outer = Rect(0, 15.456693, 9.055118);
// The real drawing's second outline came out 1e-13 smaller: a net area of rounding noise.
var holes = new[] { Rect(16.401575, 15.456693 - 1e-14, 9.055118) };
var precision = PolyLabel.ThicknessPrecision(outer, holes);
Assert.Equal(PolyLabel.ThicknessPrecision(outer), precision, 12);
var pole = await Task.Run(() => PolyLabel.Find(outer, holes, precision)).WaitAsync(TimeSpan.FromSeconds(10));
Assert.True(outer.ContainsPoint(pole));
Assert.Equal(9.055118 / 2, pole.Y, 0.5);
}
[Fact]
public void DegeneratePolygon_ReturnsFallback()
{
+3 -1
View File
@@ -13,6 +13,7 @@ namespace OpenNest
public class LayoutPart : IPart
{
private static Color selectedColor = Color.FromArgb(90, 150, 200, 255);
private const int LabelSearchCells = 20000;
// GDI+ objects are not thread-safe, so views on different UI threads each draw
// with their own; sharing one fails with "Object is currently in use elsewhere".
@@ -335,7 +336,8 @@ namespace OpenNest
holes.Add(cutout.ToPolygonWithTolerance(0.1));
}
return PolyLabel.Find(outer, holes, PolyLabel.ThicknessPrecision(outer, holes));
// The label is cosmetic: a budget keeps any shape from stalling the paint.
return PolyLabel.Find(outer, holes, PolyLabel.ThicknessPrecision(outer, holes), LabelSearchCells);
}
/// <summary>Rebuilds the part's paths from its program and maps them to the view.</summary>