diff --git a/OpenNest.Core/Geometry/PolyLabel.cs b/OpenNest.Core/Geometry/PolyLabel.cs index 4677cd01..0b625605 100644 --- a/OpenNest.Core/Geometry/PolyLabel.cs +++ b/OpenNest.Core/Geometry/PolyLabel.cs @@ -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. /// public static double ThicknessPrecision(Polygon outer, IList 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; diff --git a/OpenNest.Tests/Geometry/PolyLabelTests.cs b/OpenNest.Tests/Geometry/PolyLabelTests.cs index e2f45def..e1a680b8 100644 --- a/OpenNest.Tests/Geometry/PolyLabelTests.cs +++ b/OpenNest.Tests/Geometry/PolyLabelTests.cs @@ -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() { diff --git a/OpenNest/LayoutPart.cs b/OpenNest/LayoutPart.cs index eda7268a..45b7d8af 100644 --- a/OpenNest/LayoutPart.cs +++ b/OpenNest/LayoutPart.cs @@ -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); } /// Rebuilds the part's paths from its program and maps them to the view.