diff --git a/OpenNest.Core/Geometry/SpacingOutline.cs b/OpenNest.Core/Geometry/SpacingOutline.cs index 34238da3..6e33a4df 100644 --- a/OpenNest.Core/Geometry/SpacingOutline.cs +++ b/OpenNest.Core/Geometry/SpacingOutline.cs @@ -107,6 +107,11 @@ namespace OpenNest.Geometry public bool Overlaps(SpacingOutline other) { ArgumentNullException.ThrowIfNull(other); + // A part seated in another's cutout shares nothing with it, and is the triangle + // test's worst case: every triangle pair overlaps and each piece must be subtracted + // by the whole cutout before the answer is no. + if (InsideACutout(this, other) || InsideACutout(other, this)) + return false; var at = Triangles; var bt = other.Triangles; // Keeping translations at zero preserves the reference's floating-point operation order. @@ -114,6 +119,88 @@ namespace OpenNest.Geometry ?? Collision.HasOverlap(Perimeter, other.Perimeter, Holes, other.Holes); } + /// True when 's perimeter lies strictly inside one + /// cutout of , so the two share no material. A perimeter that + /// touches or crosses the cutout's edge is not decided here. + private static bool InsideACutout(SpacingOutline inner, SpacingOutline outer) + { + if (outer.Holes.Count == 0) + return false; + var perimeter = inner.Perimeter.Vertices; + var (left, bottom, right, top) = Bounds(perimeter); + + foreach (var hole in outer.Holes) + { + var (holeLeft, holeBottom, holeRight, holeTop) = Bounds(hole.Vertices); + + if (left <= holeLeft || right >= holeRight || bottom <= holeBottom || top >= holeTop) + continue; + if (StrictlyInside(perimeter, hole)) + return true; + } + + return false; + } + + // A cutout is a simple polygon, so a closed outline with every vertex inside it and no + // edge meeting its edges lies wholly inside it. + private static bool StrictlyInside(List outline, Polygon cutout) + { + var edge = cutout.Vertices; + // A closed polygon repeats its first vertex; its closing edge is the wrap-around. + var outlineCount = Distinct(outline); + var edgeCount = Distinct(edge); + + if (outlineCount < 3 || edgeCount < 3) + return false; + + for (var i = 0; i < outlineCount; i++) + if (!cutout.ContainsPoint(outline[i])) + return false; + + for (var i = 0; i < outlineCount; i++) + { + var a = outline[i]; + var b = outline[(i + 1) % outlineCount]; + + for (var j = 0; j < edgeCount; j++) + { + var c = edge[j]; + var d = edge[(j + 1) % edgeCount]; + + // Touching and collinear edges count as meeting: leave those to the full test. + if (Side(a, b, c) * Side(a, b, d) <= 0 && Side(c, d, a) * Side(c, d, b) <= 0) + return false; + } + } + + return true; + } + + private static int Distinct(List vertices) => + vertices.Count > 1 && vertices[0] == vertices[^1] ? vertices.Count - 1 : vertices.Count; + + private static double Side(Vector a, Vector b, Vector p) => + (b.X - a.X) * (p.Y - a.Y) - (b.Y - a.Y) * (p.X - a.X); + + private static (double Left, double Bottom, double Right, double Top) Bounds(List vertices) + { + var left = double.PositiveInfinity; + var bottom = double.PositiveInfinity; + var right = double.NegativeInfinity; + var top = double.NegativeInfinity; + + foreach (var vertex in vertices) + { + left = System.Math.Min(left, vertex.X); + bottom = System.Math.Min(bottom, vertex.Y); + right = System.Math.Max(right, vertex.X); + top = System.Math.Max(top, vertex.Y); + } + + return (left, bottom, right, top); + } + private static void ToWorld(Polygon polygon, Vector location) { polygon.Offset(location); diff --git a/OpenNest.Tests/Geometry/SpacingOutlineCutoutTests.cs b/OpenNest.Tests/Geometry/SpacingOutlineCutoutTests.cs new file mode 100644 index 00000000..cab0f952 --- /dev/null +++ b/OpenNest.Tests/Geometry/SpacingOutlineCutoutTests.cs @@ -0,0 +1,99 @@ +using OpenNest.Geometry; + +namespace OpenNest.Tests.Geometry; + +/// A part seated in another part's cutout is answered without the triangle test; the +/// answer must be the reference kernel's for every pose, seated or not. +public class SpacingOutlineCutoutTests +{ + private static Polygon Ngon(double x, double y, double radius, int sides, bool closed = true) + { + var polygon = new Polygon(); + for (var i = 0; i < sides; i++) + { + var angle = 2 * System.Math.PI * i / sides; + polygon.Vertices.Add(new Vector(x + radius * System.Math.Cos(angle), y + radius * System.Math.Sin(angle))); + } + if (closed) + polygon.Vertices.Add(polygon.Vertices[0]); + polygon.UpdateBounds(); + return polygon; + } + + private static Polygon Path(params (double X, double Y)[] points) + { + var polygon = new Polygon(); + foreach (var (x, y) in points) + polygon.Vertices.Add(new Vector(x, y)); + polygon.Vertices.Add(polygon.Vertices[0]); + polygon.UpdateBounds(); + return polygon; + } + + private static bool Reference(SpacingOutline a, SpacingOutline b) => + Collision.HasOverlap(a.Perimeter, b.Perimeter, a.Holes, b.Holes); + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void RingInABoreMatchesTheReferenceAtEveryOffset(bool closed) + { + var frame = new SpacingOutline(Ngon(0, 0, 10, 64, closed), new List { Ngon(0, 0, 6, 48, closed) }); + var random = new Random(5826); + var clear = 0; + var overlapping = 0; + for (var sample = 0; sample < 400; sample++) + { + var radius = 1 + random.NextDouble() * 4.5; + var x = random.NextDouble() * 14 - 7; + var y = random.NextDouble() * 14 - 7; + var insert = new SpacingOutline(Ngon(x, y, radius, 40, closed), + new List { Ngon(x, y, radius / 2, 24, closed) }); + var expected = Reference(insert, frame); + + Assert.Equal(expected, insert.Overlaps(frame)); + Assert.Equal(expected, frame.Overlaps(insert)); + if (expected) + overlapping++; + else + clear++; + } + Assert.True(clear > 20 && overlapping > 20, $"{clear} clear, {overlapping} overlapping"); + } + + [Fact] + public void SeatedDiscIsClear() + { + var frame = new SpacingOutline(Ngon(0, 0, 10, 64), new List { Ngon(0, 0, 6, 48) }); + var disc = new SpacingOutline(Ngon(0.5, -0.5, 5, 40), new List()); + + Assert.False(disc.Overlaps(frame)); + Assert.False(frame.Overlaps(disc)); + } + + [Fact] + public void OutlineWithEveryVertexInAConcaveCutoutButAnEdgeAcrossItsNotchOverlaps() + { + // A U-shaped cutout: the bar's corners sit in the two arms, its middle crosses the + // material between them. + var cutout = Path((1, 1), (9, 1), (9, 9), (6, 9), (6, 3), (4, 3), (4, 9), (1, 9)); + var frame = new SpacingOutline(Path((0, 0), (10, 0), (10, 10), (0, 10)), new List { cutout }); + var bar = new SpacingOutline(Path((2, 6), (8, 6), (8, 7), (2, 7)), new List()); + + Assert.True(Reference(bar, frame)); + Assert.True(bar.Overlaps(frame)); + Assert.True(frame.Overlaps(bar)); + } + + [Fact] + public void OutlineInsideAConcaveCutoutIsClear() + { + var cutout = Path((1, 1), (9, 1), (9, 9), (6, 9), (6, 3), (4, 3), (4, 9), (1, 9)); + var frame = new SpacingOutline(Path((0, 0), (10, 0), (10, 10), (0, 10)), new List { cutout }); + var bar = new SpacingOutline(Path((2, 1.5), (8, 1.5), (8, 2.5), (2, 2.5)), new List()); + + Assert.False(Reference(bar, frame)); + Assert.False(bar.Overlaps(frame)); + Assert.False(frame.Overlaps(bar)); + } +}