mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-02 11:01:37 -05:00
Expand Spacing kept the first selected part fixed and stalled on full plates. - A pair of selected parts now shares each push (later part half, earlier part the rest, later part any remainder), so no selected part is an anchor. Both shares run to the full target, so a part between two short gaps no longer ping-pongs while its partners never move. - Clamps act like the work-area edge: a part may close on the keep-out but never enter it, and the clamp gap neither chases the target spacing nor counts as the achieved spacing. Before, clamps held their full entry gap and a part resting just above one capped the whole search. - A part flush on an edge can slide along it again. A push that leaned into the edge by rounding (parts turned a quarter or half) divided zero room by a tiny component and pinned every move. - Faster search with identical layouts: per-part pair lists, clearances remembered at the positions measured, box-proven clamp pairs skipped, trial-move gaps measured only up to their threshold, the overlap clipping run only for outlines that touch, cross or nest, and ring distances measured at offsets without copying outlines. Clearance gains the bounded and offset distance used for this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
377 lines
12 KiB
C#
377 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Tests.Geometry;
|
|
|
|
public class ClearanceTests
|
|
{
|
|
private const double Tol = 1e-9;
|
|
|
|
private static Polygon Square(double x, double y, double w, double h)
|
|
{
|
|
var p = new Polygon
|
|
{
|
|
Vertices = new List<Vector>
|
|
{
|
|
new Vector(x, y),
|
|
new Vector(x + w, y),
|
|
new Vector(x + w, y + h),
|
|
new Vector(x, y + h),
|
|
}
|
|
};
|
|
p.Close();
|
|
p.UpdateBounds();
|
|
return p;
|
|
}
|
|
|
|
private static Polygon Triangle(params double[] xy)
|
|
{
|
|
var p = new Polygon();
|
|
for (var i = 0; i + 1 < xy.Length; i += 2)
|
|
p.Vertices.Add(new Vector(xy[i], xy[i + 1]));
|
|
p.Close();
|
|
p.UpdateBounds();
|
|
return p;
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void OpenPathBoundaryDistanceUsesSegmentInterior_NotFirstEndpoint(bool vertical)
|
|
{
|
|
var part = Square(0, 0, 2, 2);
|
|
var path = vertical
|
|
? new Line(new Vector(10, -20), new Vector(10, 20))
|
|
: new Line(new Vector(-20, 10), new Vector(20, 10));
|
|
var lines = new List<Line> { path };
|
|
|
|
var initial = Clearance.BoundaryDistance(part.ToLines(), lines);
|
|
Assert.Equal(8, initial.Distance, 6); // Independent axis-aligned box gap.
|
|
Assert.Equal(vertical ? -1 : 0, initial.Direction.X, 6);
|
|
Assert.Equal(vertical ? 0 : -1, initial.Direction.Y, 6);
|
|
|
|
// A parallel translation leaves the true gap unchanged even though
|
|
// distance to the first endpoint changes substantially.
|
|
part.Offset(vertical ? new Vector(0, 3) : new Vector(3, 0));
|
|
Assert.Equal(8, Clearance.BoundaryDistance(part.ToLines(), lines).Distance, 6);
|
|
}
|
|
|
|
// ---- Separation ----
|
|
|
|
[Fact]
|
|
public void Between_SeparatedHorizontally_DistanceAndDirection()
|
|
{
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(3, 0, 1, 1);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.Equal(2.0, r.Distance, 6);
|
|
// Pushing a away from b means moving left.
|
|
Assert.Equal(-1.0, r.Direction.X, 6);
|
|
Assert.Equal(0.0, r.Direction.Y, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_SeparatedDiagonally_CornerDistance()
|
|
{
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(2, 2, 1, 1);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.Equal(System.Math.Sqrt(2.0), r.Distance, 6);
|
|
Assert.Equal(-1 / System.Math.Sqrt(2), r.Direction.X, 6);
|
|
Assert.Equal(-1 / System.Math.Sqrt(2), r.Direction.Y, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_Touching_ZeroDistance()
|
|
{
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(1, 0, 2, 1);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.True(System.Math.Abs(r.Distance) < 1e-6, $"expected ~0, got {r.Distance}");
|
|
var mag = System.Math.Sqrt(
|
|
r.Direction.X * r.Direction.X + r.Direction.Y * r.Direction.Y
|
|
);
|
|
Assert.Equal(1.0, mag, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_VertexToEdge_DistanceIsPerpendicular()
|
|
{
|
|
// Triangle above a wide square; the base sits 3 above the square's top edge.
|
|
var a = Triangle(1, 3, 3, 3, 2, 4);
|
|
var b = Square(0, -4, 10, 4); // top edge at y = 0
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.Equal(3.0, r.Distance, 6); // base y=3 to y=0
|
|
Assert.Equal(0.0, r.Direction.X, 6);
|
|
Assert.Equal(1.0, r.Direction.Y, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_ParallelStaggeredEdges_MinimumAcrossAllPairs()
|
|
{
|
|
// Two L-ish shapes (as simple polys) offset so the true minimum is
|
|
// between mid-edges, not vertices.
|
|
var a = Square(0, 0, 4, 1);
|
|
var b = Square(1, 2, 1, 3);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.Equal(1.0, r.Distance, 6);
|
|
Assert.Equal(-1.0, r.Direction.Y, 6);
|
|
}
|
|
|
|
// ---- Penetration ----
|
|
|
|
[Fact]
|
|
public void Between_OverlappingSquares_MinimumTranslationAxis()
|
|
{
|
|
// Overlap 0.5 in X, 1.0 in Y -> cheapest exit is X.
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(0.5, 0, 1.5, 1);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.Equal(-0.5, r.Distance, 6);
|
|
Assert.Equal(-1.0, r.Direction.X, 6); // push a left, out of b
|
|
Assert.Equal(0.0, r.Direction.Y, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_OverlappingVerticallyCheaper_ExitsInY()
|
|
{
|
|
// Overlap 0.8 in X, 0.2 in Y -> cheapest exit is Y.
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(0.2, 0.8, 1.2, 1.8);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.Equal(-0.2, r.Distance, 6);
|
|
Assert.Equal(0.0, r.Direction.X, 6);
|
|
Assert.Equal(-1.0, r.Direction.Y, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_ContainedSquare_ExitsThroughNearestWall()
|
|
{
|
|
// Inner square near the left wall: the translation that ENDS the overlap
|
|
// carries its right edge (x=1.2) past the outer's left edge (x=0).
|
|
var outer = Square(0, 0, 10, 10);
|
|
var inner = Square(0.2, 4, 1, 1);
|
|
|
|
var r = Clearance.Between(inner, outer);
|
|
|
|
Assert.Equal(-1.2, r.Distance, 6);
|
|
Assert.Equal(-1.0, r.Direction.X, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_ConcentricSquares_DepthIsExitTranslation()
|
|
{
|
|
var outer = Square(0, 0, 10, 10);
|
|
var inner = Square(2, 2, 4, 4); // spans [2,6]; leftmost exit carries 6 to 0
|
|
|
|
var r = Clearance.Between(inner, outer);
|
|
|
|
Assert.Equal(-6.0, r.Distance, 6);
|
|
Assert.Equal(0.0, r.Direction.X, 6);
|
|
Assert.Equal(-1.0, r.Direction.Y, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_TrianglesPenetrating_ReportsNegativeDepth()
|
|
{
|
|
var a = Triangle(0, 0, 4, 0, 2, 3);
|
|
var b = Triangle(1, 0, 5, 0, 3, 3);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
Assert.True(r.Distance < 0, $"expected penetration, got {r.Distance}");
|
|
}
|
|
|
|
// ---- Direction is actionable: moving a by -Distance * dir clears contact ----
|
|
|
|
[Fact]
|
|
public void Between_PenetrationApplyingDirection_EndsContact()
|
|
{
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(0.3, 0, 1.6, 2);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
|
|
var moved = (Polygon)a.Clone();
|
|
moved.Offset(r.Direction * (-r.Distance + 0.001));
|
|
moved.UpdateBounds();
|
|
|
|
Assert.False(Collision.HasOverlap(moved, b));
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_SeparationApplyingDirection_NeverReducesDistance()
|
|
{
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(4, 1, 2, 2);
|
|
|
|
var r = Clearance.Between(a, b);
|
|
Assert.True(r.Distance > 0);
|
|
|
|
// A tiny step along the reported direction must not move closer.
|
|
var moved = (Polygon)a.Clone();
|
|
moved.Offset(r.Direction * (r.Distance / 2));
|
|
moved.UpdateBounds();
|
|
|
|
var r2 = Clearance.Between(moved, b);
|
|
Assert.True(
|
|
r2.Distance >= r.Distance - Tol,
|
|
$"moving along dir reduced clearance {r.Distance} -> {r2.Distance}"
|
|
);
|
|
}
|
|
|
|
// ---- Determinism ----
|
|
|
|
[Fact]
|
|
public void Between_RepeatedCalls_IdenticalResult()
|
|
{
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(0.5, 0.25, 2, 1.5);
|
|
|
|
var r1 = Clearance.Between(a, b);
|
|
var r2 = Clearance.Between(a, b);
|
|
|
|
Assert.Equal(r1.Distance, r2.Distance);
|
|
Assert.Equal(r1.Direction.X, r2.Direction.X);
|
|
Assert.Equal(r1.Direction.Y, r2.Direction.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void Between_SymmetricSwap_MirrorsDirection()
|
|
{
|
|
var a = Square(0, 0, 1, 1);
|
|
var b = Square(0.5, 0, 1.5, 1);
|
|
|
|
var ab = Clearance.Between(a, b);
|
|
var ba = Clearance.Between(b, a);
|
|
|
|
Assert.Equal(ab.Distance, ba.Distance, 6);
|
|
Assert.Equal(-ab.Direction.X, ba.Direction.X, 6);
|
|
Assert.Equal(-ab.Direction.Y, ba.Direction.Y, 6);
|
|
}
|
|
|
|
// ---- Agreement with the Collision oracle ----
|
|
|
|
[Fact]
|
|
public void Between_SignMatchesCollisionVerdict()
|
|
{
|
|
var polygons = new List<Polygon>
|
|
{
|
|
Square(0, 0, 1, 1),
|
|
Square(1, 0, 2, 1),
|
|
Square(0.5, 0, 1.5, 1),
|
|
Square(0.25, 0.25, 0.75, 0.75),
|
|
Square(5, 5, 6, 6),
|
|
Triangle(0, 0, 2, 0, 1, 2),
|
|
Triangle(0.5, -1, 2.5, -1, 1.5, 1),
|
|
};
|
|
|
|
for (var i = 0; i < polygons.Count; i++)
|
|
{
|
|
for (var j = i + 1; j < polygons.Count; j++)
|
|
{
|
|
var overlaps = Collision.HasOverlap(polygons[i], polygons[j]);
|
|
var r = Clearance.Between(polygons[i], polygons[j]);
|
|
|
|
if (overlaps)
|
|
{
|
|
Assert.True(
|
|
r.Distance <= Tol,
|
|
$"pair {i},{j}: Collision overlaps but clearance {r.Distance}"
|
|
);
|
|
}
|
|
else
|
|
{
|
|
Assert.True(
|
|
r.Distance >= -Tol,
|
|
$"pair {i},{j}: Collision clear but clearance {r.Distance}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void BoundaryDistance_AtOffsetsAndWithinReach_MatchesMovedCopies()
|
|
{
|
|
// The Expander measures prepared rings at offsets, often only up to a reach.
|
|
// Both must give exactly what the moved copies give: same distance and the
|
|
// same direction, including parallel edges where many points tie.
|
|
var random = new Random(1234);
|
|
Polygon Ring()
|
|
{
|
|
var points = new List<Vector>();
|
|
var count = random.Next(3, 12);
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
var angle = 2 * System.Math.PI * i / count;
|
|
var radius = 1 + random.NextDouble();
|
|
points.Add(new Vector(radius * System.Math.Cos(angle), radius * System.Math.Sin(angle)));
|
|
}
|
|
var ring = new Polygon { Vertices = points };
|
|
ring.Close();
|
|
ring.UpdateBounds();
|
|
return ring;
|
|
}
|
|
|
|
Polygon Moved(Polygon ring, Vector offset)
|
|
{
|
|
var copy = (Polygon)ring.Clone();
|
|
copy.UpdateBounds();
|
|
copy.Offset(offset);
|
|
return copy;
|
|
}
|
|
|
|
var shapes = new List<Polygon> { Square(0, 0, 2, 2), Square(0, 0, 3, 1) };
|
|
for (var i = 0; i < 40; i++)
|
|
shapes.Add(Ring());
|
|
|
|
for (var trial = 0; trial < 400; trial++)
|
|
{
|
|
var a = shapes[random.Next(shapes.Count)];
|
|
var b = shapes[random.Next(shapes.Count)];
|
|
var offsetA = new Vector(random.NextDouble() * 10, random.NextDouble() * 10);
|
|
var offsetB = offsetA + new Vector(random.NextDouble() * 8 - 4, random.NextDouble() * 8 - 4);
|
|
var expected = Clearance.BoundaryDistance(Moved(a, offsetA), Moved(b, offsetB));
|
|
|
|
var atOffsets = Clearance.BoundaryDistance(a.Vertices, offsetA, b.Vertices, offsetB, double.MaxValue);
|
|
Assert.Equal(expected.Distance, atOffsets.Distance);
|
|
Assert.Equal(expected.Direction.X, atOffsets.Direction.X);
|
|
Assert.Equal(expected.Direction.Y, atOffsets.Direction.Y);
|
|
|
|
var reach = random.NextDouble() * 3;
|
|
foreach (var bounded in new[]
|
|
{
|
|
Clearance.BoundaryDistance(a.Vertices, offsetA, b.Vertices, offsetB, reach),
|
|
Clearance.BoundaryDistance(Moved(a, offsetA), Moved(b, offsetB), reach),
|
|
})
|
|
{
|
|
if (expected.Distance < reach)
|
|
{
|
|
Assert.Equal(expected.Distance, bounded.Distance);
|
|
Assert.Equal(expected.Direction.X, bounded.Direction.X);
|
|
Assert.Equal(expected.Direction.Y, bounded.Direction.Y);
|
|
}
|
|
else
|
|
Assert.Equal(reach, bounded.Distance);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|