mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 09:29:25 -05:00
Expand Spacing widened gaps between parts but left each part's strip to the sheet edge at the plate's edge spacing, so past twice that spacing the edge strip became the thinnest web on the plate. A strip takes heat from one cut while a web between parts takes it from two, so each edge strip now holds at least half the spacing (never less than EdgeSpacing). The pair search pushes edge parts inward and only accepts a spacing whose strips hold. The pattern stretch sizes each axis's strips from the smallest gap that axis widens, so stretching along the rows before the rows open does not use up the edge room. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
912 lines
36 KiB
C#
912 lines
36 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using OpenNest.Clamps;
|
|
using OpenNest.CNC;
|
|
using OpenNest.Engine.Fill;
|
|
using OpenNest.Engine.Tests.Jobs;
|
|
using OpenNest.Geometry;
|
|
using Xunit;
|
|
|
|
namespace OpenNest.Engine.Tests.Fill;
|
|
|
|
/// <summary>
|
|
/// PlateView spacing expander: grows part-to-part spacing with the work area
|
|
/// and non-selected parts as hard boundaries.
|
|
/// </summary>
|
|
public class ExpanderTests
|
|
{
|
|
private static Program Rectangle(double width = 4, double length = 4) =>
|
|
TestDrawingFactory.Rectangle(width, length);
|
|
|
|
private static Part AddSquare(Plate plate, double x, double y, double size = 4)
|
|
{
|
|
var part = new Part(new Drawing($"sq{plate.Parts.Count}", Rectangle(size, size)), new Vector(x, y));
|
|
plate.Parts.Add(part);
|
|
return part;
|
|
}
|
|
|
|
private static Part AddRectangle(Plate plate, double x, double y, double length, double width)
|
|
{
|
|
var part = new Part(new Drawing($"rect{plate.Parts.Count}", Rectangle(length, width)), new Vector(x, y));
|
|
plate.Parts.Add(part);
|
|
return part;
|
|
}
|
|
|
|
private static Plate MakePlate(double lengthX, double widthY, double edge = 0.5)
|
|
{
|
|
var plate = new Plate(new Size(widthY, lengthX));
|
|
plate.EdgeSpacing = new Spacing(edge, edge);
|
|
return plate;
|
|
}
|
|
|
|
/// <summary>Independent clearance oracle: naive vertex/segment min distance over raw part lines.</summary>
|
|
private static double BruteClearance(Part a, Part b)
|
|
{
|
|
var linesA = PartGeometry.GetPartLines(a);
|
|
var linesB = PartGeometry.GetPartLines(b);
|
|
|
|
double min = double.MaxValue;
|
|
foreach (var la in linesA)
|
|
foreach (var lb in linesB)
|
|
{
|
|
min = System.Math.Min(min, PointSegment(a, la.StartPoint, lb));
|
|
min = System.Math.Min(min, PointSegment(a, la.EndPoint, lb));
|
|
min = System.Math.Min(min, PointSegment(b, lb.StartPoint, la));
|
|
min = System.Math.Min(min, PointSegment(b, lb.EndPoint, la));
|
|
}
|
|
return min;
|
|
}
|
|
|
|
private static double PointSegment(Part owner, Vector pt, Line seg)
|
|
{
|
|
var d = seg.EndPoint - seg.StartPoint;
|
|
var len2 = d.DotProduct(d);
|
|
var t = len2 <= 1e-12 ? 0 : System.Math.Clamp((pt - seg.StartPoint).DotProduct(d) / len2, 0, 1);
|
|
return pt.DistanceTo(seg.StartPoint + d * t);
|
|
}
|
|
|
|
private static void AssertNoOverlaps(Plate plate)
|
|
{
|
|
for (var i = 0; i < plate.Parts.Count; i++)
|
|
for (var j = i + 1; j < plate.Parts.Count; j++)
|
|
Assert.False(
|
|
plate.Parts[i].Intersects(plate.Parts[j], out _),
|
|
$"{plate.Parts[i].BaseDrawing.Name} overlaps {plate.Parts[j].BaseDrawing.Name}"
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_TwoSquares_BothMoveOutToHalfSpacingEdgeStrips()
|
|
{
|
|
var plate = MakePlate(24, 24);
|
|
var a = AddSquare(plate, 6, 10);
|
|
var b = AddSquare(plate, 14, 10);
|
|
|
|
var result = Expander.Expand(new List<Part> { a, b }, plate);
|
|
|
|
// Neither part is an anchor, and each edge strip grows to half the gap:
|
|
// 24 = 4 + 4 + s + 2 * (s / 2), so the gap is 8 and each strip is 4.
|
|
Assert.Equal(4, a.Location.X, 1);
|
|
Assert.Equal(10, a.Location.Y, 6);
|
|
Assert.Equal(16, b.Location.X, 1);
|
|
Assert.Equal(10, b.Location.Y, 6);
|
|
Assert.True(result.AchievedSpacing >= 7.98, $"achieved {result.AchievedSpacing}");
|
|
Assert.True(result.AchievedSpacing <= 8.01, $"achieved {result.AchievedSpacing}");
|
|
AssertNoOverlaps(plate);
|
|
Assert.True(a.BoundingBox.Left >= result.AchievedSpacing / 2 - 1e-6);
|
|
Assert.True(b.BoundingBox.Right <= 24 - result.AchievedSpacing / 2 + 1e-6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_RowFlushOnTheEdgeSpacing_MovesInwardToHalfSpacingStrips()
|
|
{
|
|
// A fill-like row placed on the plate's edge spacing (0.25) at a 0.25 gap.
|
|
var plate = MakePlate(30, 10, edge: 0.25);
|
|
var parts = new List<Part>
|
|
{
|
|
AddSquare(plate, 0.25, 0.25),
|
|
AddSquare(plate, 4.5, 0.25),
|
|
AddSquare(plate, 8.75, 0.25),
|
|
};
|
|
|
|
var result = Expander.Expand(parts, plate);
|
|
|
|
// 30 = 3 * 4 + 2 * s + 2 * (s / 2) across X, so s = 6 and each X strip is 3.
|
|
Assert.Empty(result.Violations);
|
|
Assert.True(result.AchievedSpacing >= 5.98 && result.AchievedSpacing <= 6.01,
|
|
$"achieved {result.AchievedSpacing}");
|
|
var half = result.AchievedSpacing / 2;
|
|
foreach (var part in parts)
|
|
{
|
|
var box = part.BoundingBox;
|
|
Assert.True(box.Left >= half - 1e-6, $"{part.BaseDrawing.Name} left strip {box.Left}");
|
|
Assert.True(30 - box.Right >= half - 1e-6, $"{part.BaseDrawing.Name} right strip {30 - box.Right}");
|
|
Assert.True(box.Bottom >= half - 1e-6, $"{part.BaseDrawing.Name} bottom strip {box.Bottom}");
|
|
Assert.True(10 - box.Top >= half - 1e-6, $"{part.BaseDrawing.Name} top strip {10 - box.Top}");
|
|
}
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_SpacingWithinTwiceTheEdgeSpacing_LeavesEdgePartsOnTheEdgeSpacing()
|
|
{
|
|
// Half the spacing never exceeds the 0.5 edge spacing, so the strips keep it.
|
|
var plate = MakePlate(30, 10, edge: 0.5);
|
|
var a = AddSquare(plate, 0.5, 0.5);
|
|
var b = AddSquare(plate, 4.75, 0.5);
|
|
|
|
var result = Expander.Expand(new List<Part> { a, b }, plate, new Expander.Options { MaxSpacing = 1 });
|
|
|
|
Assert.Empty(result.Violations);
|
|
Assert.True(result.AchievedSpacing >= 0.99, $"achieved {result.AchievedSpacing}");
|
|
Assert.Equal(0.5, a.BoundingBox.Bottom, 6);
|
|
Assert.Equal(0.5, b.BoundingBox.Bottom, 6);
|
|
Assert.True(a.BoundingBox.Left >= 0.5 - 1e-9);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_EdgePartBoxedInByAFixedPart_StopsWhereItsStripHoldsHalfTheSpacing()
|
|
{
|
|
// The selected part sits on the left edge with a fixed part 2 to its right:
|
|
// its strip s / 2 and its gap s share those 2 units, so s tops out at 4 / 3.
|
|
// The other selected part starts 0.25 from the fixed part, so the spacing
|
|
// has room to grow before the edge strip stops it.
|
|
var plate = MakePlate(40, 10, edge: 0);
|
|
var edgePart = AddSquare(plate, 0, 3, 2);
|
|
var other = AddSquare(plate, 6.25, 3, 2);
|
|
var wall = AddSquare(plate, 4, 0, 2);
|
|
wall.Offset(new Vector(0, 3));
|
|
|
|
var result = Expander.Expand(new List<Part> { edgePart, other }, plate);
|
|
|
|
Assert.Empty(result.Violations);
|
|
Assert.True(result.AchievedSpacing >= 1.32 && result.AchievedSpacing <= 1.34,
|
|
$"achieved {result.AchievedSpacing}");
|
|
Assert.True(edgePart.BoundingBox.Left >= result.AchievedSpacing / 2 - 1e-6,
|
|
$"edge strip {edgePart.BoundingBox.Left}");
|
|
Assert.Equal(new Vector(4, 3), wall.Location);
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_SandwichedBetweenWalls_ConvergesOnlyToInitialGaps_AndKeepsWalls()
|
|
{
|
|
var plate = MakePlate(26, 10, edge: 0.0);
|
|
var wallL = AddSquare(plate, 0, 3);
|
|
var wallR = AddSquare(plate, 18, 3);
|
|
var a = AddSquare(plate, 6, 3);
|
|
var b = AddSquare(plate, 12, 3);
|
|
|
|
var result = Expander.Expand(new List<Part> { a, b }, plate);
|
|
|
|
// Every gap starts at exactly 2.0; straight separation moves cannot open
|
|
// the row (opening one gap costs another), so the run stays at ~2.0.
|
|
Assert.Equal(0, wallL.Location.X, 6);
|
|
Assert.Equal(18, wallR.Location.X, 6);
|
|
Assert.True(
|
|
result.AchievedSpacing >= 1.9 && result.AchievedSpacing <= 2.05,
|
|
$"achieved {result.AchievedSpacing}"
|
|
);
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_OverlappingPair_SeparatesAndClearsOverlap()
|
|
{
|
|
var plate = MakePlate(30, 12);
|
|
var a = AddSquare(plate, 5, 4);
|
|
var b = AddSquare(plate, 7, 4); // 2.0 overlap in X
|
|
|
|
var result = Expander.Expand(new List<Part> { a, b }, plate, new Expander.Options
|
|
{
|
|
InitialStep = 0.5,
|
|
MaxSpacing = 3,
|
|
});
|
|
|
|
Assert.False(a.Intersects(b, out _));
|
|
var gap = b.Location.X - (a.Location.X + 4);
|
|
Assert.True(gap >= 2.99, $"gap {gap}");
|
|
Assert.True(result.AchievedSpacing >= 2.9);
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_PinnedPart_ReportsViolationsWithoutOverlap()
|
|
{
|
|
var plate = MakePlate(20, 20, edge: 0.0);
|
|
var pinned = AddSquare(plate, 8, 8);
|
|
// Walls 0.2 clear on all four sides.
|
|
var left = AddSquare(plate, 3.8, 8);
|
|
var right = AddSquare(plate, 12.2, 8);
|
|
var bottom = AddSquare(plate, 8, 3.8);
|
|
var top = AddSquare(plate, 8, 12.2);
|
|
|
|
var (converged, positions, violations) = Expander.Separate(
|
|
new List<Part> { pinned },
|
|
plate,
|
|
spacing: 1.0
|
|
);
|
|
|
|
Assert.False(converged);
|
|
Assert.NotEmpty(violations);
|
|
// The pinned part may slide into the walls but never through them.
|
|
AssertNoOverlaps(plate);
|
|
Assert.Equal(3.8, left.Location.X, 6);
|
|
Assert.Equal(12.2, right.Location.X, 6);
|
|
Assert.Equal(3.8, bottom.Location.Y, 6);
|
|
Assert.Equal(12.2, top.Location.Y, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_CanceledBeforeRun_LeavesEverythingInPlace()
|
|
{
|
|
var plate = MakePlate(24, 24);
|
|
var a = AddSquare(plate, 6, 10);
|
|
var b = AddSquare(plate, 14, 10);
|
|
using var cts = new CancellationTokenSource();
|
|
cts.Cancel();
|
|
|
|
var result = Expander.Expand(
|
|
new List<Part> { a, b },
|
|
plate,
|
|
token: cts.Token
|
|
);
|
|
|
|
Assert.True(result.Canceled);
|
|
Assert.Equal(6, a.Location.X, 6);
|
|
Assert.Equal(14, b.Location.X, 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_ThreeInRow_OracleConfirmsSpacing()
|
|
{
|
|
var plate = MakePlate(60, 14);
|
|
var a = AddSquare(plate, 5, 5);
|
|
var b = AddSquare(plate, 10, 5);
|
|
var c = AddSquare(plate, 15, 5);
|
|
|
|
var result = Expander.Expand(
|
|
new List<Part> { a, b, c },
|
|
plate,
|
|
new Expander.Options { MaxSpacing = 8 }
|
|
);
|
|
|
|
Assert.True(result.AchievedSpacing >= 7.9);
|
|
Assert.True(a.Location.X < 5, $"first selected part stayed at {a.Location.X:R}"); // no anchor
|
|
|
|
// Independent oracle: every pair clears the reported spacing.
|
|
var parts = new List<Part> { a, b, c };
|
|
for (var i = 0; i < parts.Count; i++)
|
|
for (var j = i + 1; j < parts.Count; j++)
|
|
{
|
|
var clearance = BruteClearance(parts[i], parts[j]);
|
|
Assert.True(
|
|
clearance >= result.AchievedSpacing - 0.01,
|
|
$"{parts[i].BaseDrawing.Name}/{parts[j].BaseDrawing.Name}: oracle {clearance} < reported {result.AchievedSpacing}"
|
|
);
|
|
}
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
private static Program RectangleWithHole(
|
|
double width,
|
|
double length,
|
|
double hx,
|
|
double hy,
|
|
double hw,
|
|
double hh
|
|
)
|
|
{
|
|
var program = Rectangle(width, length);
|
|
program.MoveTo(hx, hy);
|
|
program.LineTo(hx + hw, hy);
|
|
program.LineTo(hx + hw, hy + hh);
|
|
program.LineTo(hx, hy + hh);
|
|
program.LineTo(hx, hy);
|
|
return program;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hole-subtracting overlap check matching NestValidator semantics (a part in
|
|
/// a cutout is legal). Part.Intersects is perimeter-only, so it cannot
|
|
/// certify part-in-cutout layouts.
|
|
/// </summary>
|
|
private static bool MateriallyOverlaps(Part a, Part b)
|
|
{
|
|
var (outerA, holesA) = Rings(a);
|
|
var (outerB, holesB) = Rings(b);
|
|
return Collision.HasOverlap(outerA, outerB, holesA, holesB);
|
|
}
|
|
|
|
private static (Polygon Outer, List<Polygon> Holes) Rings(Part part)
|
|
{
|
|
var entities = OpenNest.Converters.ConvertProgram
|
|
.ToGeometry(part.Program)
|
|
.Where(e => SpecialLayers.IsMaterial(e.Layer))
|
|
.ToList();
|
|
var profile = new ShapeProfile(entities);
|
|
|
|
var outer = profile.Perimeter.ToPolygonWithTolerance(0.001);
|
|
outer.Offset(part.Location);
|
|
|
|
var holes = new List<Polygon>();
|
|
foreach (var cutout in profile.Cutouts)
|
|
{
|
|
var hole = cutout.ToPolygonWithTolerance(0.001);
|
|
hole.Offset(part.Location);
|
|
holes.Add(hole);
|
|
}
|
|
|
|
return (outer, holes.Count == 0 ? null : holes);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_PartInsideCutout_KeepsLegalAndClearsHoleWalls()
|
|
{
|
|
var plate = MakePlate(40, 24);
|
|
|
|
// Wall part with a 10x10 cutout; a small selected part sits inside it.
|
|
var wall = new Part(
|
|
new Drawing(
|
|
"wall",
|
|
RectangleWithHole(20, 20, 5, 5, 10, 10)
|
|
),
|
|
new Vector(0, 0)
|
|
);
|
|
plate.Parts.Add(wall);
|
|
|
|
var inside = new Part(new Drawing("inside", Rectangle(2, 2)), new Vector(9, 9));
|
|
var other = new Part(new Drawing("other", Rectangle(2, 2)), new Vector(30, 9));
|
|
plate.Parts.Add(inside);
|
|
plate.Parts.Add(other);
|
|
|
|
var result = Expander.Expand(
|
|
new List<Part> { inside, other },
|
|
plate,
|
|
new Expander.Options { MaxSpacing = 2 }
|
|
);
|
|
|
|
// Part-in-cutout is legal, never a material overlap.
|
|
Assert.False(MateriallyOverlaps(inside, wall));
|
|
Assert.False(MateriallyOverlaps(other, wall));
|
|
Assert.False(MateriallyOverlaps(inside, other));
|
|
Assert.True(result.AchievedSpacing >= 1.9);
|
|
|
|
// The part that started in the cutout must clear the hole walls too.
|
|
var holeLeft = 5;
|
|
var holeRight = 15;
|
|
var gapLeft = inside.Location.X - holeLeft;
|
|
var gapRight = holeRight - (inside.Location.X + 2);
|
|
var gapBottom = inside.Location.Y - holeLeft;
|
|
var gapTop = holeRight - (inside.Location.Y + 2);
|
|
var minGap = System.Math.Min(
|
|
System.Math.Min(gapLeft, gapRight),
|
|
System.Math.Min(gapBottom, gapTop)
|
|
);
|
|
Assert.True(minGap >= 1.9, $"closest hole-wall gap {minGap}");
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_DegenerateInputs_Throw()
|
|
{
|
|
var plate = MakePlate(10, 10);
|
|
var a = AddSquare(plate, 1, 1);
|
|
var stranger = new Part(new Drawing("stranger", Rectangle()), new Vector(50, 50));
|
|
|
|
Assert.Throws<ArgumentException>(() => Expander.Expand(new List<Part>(), plate));
|
|
Assert.Throws<ArgumentException>(() => Expander.Expand(new List<Part> { a }, plate));
|
|
Assert.Throws<ArgumentException>(() => Expander.Expand(new List<Part> { a, stranger }, plate));
|
|
Assert.Throws<ArgumentNullException>(() => Expander.Expand(new List<Part> { a, a }, null));
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_WideEntryGap_ClosesOnlyToTheTarget()
|
|
{
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
var anchor = AddSquare(plate, 1, 4, 2);
|
|
var mover = AddSquare(plate, 4, 4, 2);
|
|
var wall = AddSquare(plate, 12, 4, 2);
|
|
|
|
var (converged, positions, _) = Expander.Separate(
|
|
new List<Part> { anchor, mover }, plate, spacing: 3
|
|
);
|
|
|
|
// The wall is six units away at entry, so the mover may spend the three
|
|
// units above the target; it needs only two of them.
|
|
Assert.True(converged);
|
|
Assert.True(wall.Location.X - (positions[1].X + 2) >= 3);
|
|
Assert.Equal(new Vector(4, 4), mover.Location);
|
|
Assert.Equal(new Vector(12, 4), wall.Location);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_EntryGapBelowTheTarget_StaysAFloor()
|
|
{
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
var anchor = AddSquare(plate, 1, 4, 2);
|
|
var mover = AddSquare(plate, 4, 4, 2);
|
|
AddSquare(plate, 7, 4, 2);
|
|
|
|
var (converged, positions, _) = Expander.Separate(
|
|
new List<Part> { anchor, mover }, plate, spacing: 3
|
|
);
|
|
|
|
// One unit from the wall and from the anchor at entry: the 3-unit target
|
|
// does not fit, and neither gap may close below its one-unit entry gap
|
|
// to help the other. The parts may still spend the edge's slack.
|
|
Assert.False(converged);
|
|
Assert.True(positions[1].X - (positions[0].X + 2) >= 1 - 1e-6, $"selected gap at {positions[1].X:R}");
|
|
Assert.True(7 - (positions[1].X + 2) >= 1 - 1e-6, $"wall gap at {positions[1].X:R}");
|
|
Assert.True(positions[0].X >= -1e-9);
|
|
Assert.Equal(4, positions[1].Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_MiddlePart_SettlesBetweenItsNeighbors()
|
|
{
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
var anchor = AddSquare(plate, 1, 4, 2);
|
|
var mover = AddSquare(plate, 4, 4, 2);
|
|
var wall = AddSquare(plate, 12, 4, 2);
|
|
|
|
var result = Expander.Expand(new List<Part> { anchor, mover }, plate);
|
|
|
|
// Eight units of free travel hold the left edge strip and two gaps:
|
|
// s / 2 + s + s = 8, so the spacing settles at 3.2.
|
|
Assert.True(result.AchievedSpacing >= 3.19, $"achieved {result.AchievedSpacing}");
|
|
Assert.True(anchor.Location.X >= result.AchievedSpacing / 2 - 1e-6, $"anchor at {anchor.Location.X}");
|
|
Assert.Equal(new Vector(12, 4), wall.Location);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_RetriesMiddleAfterLaterBlockerMoves()
|
|
{
|
|
var plate = MakePlate(60, 14, edge: 0);
|
|
var first = AddSquare(plate, 5, 5);
|
|
var middle = AddSquare(plate, 10, 5);
|
|
var last = AddSquare(plate, 14, 5);
|
|
var selected = new List<Part> { first, middle, last };
|
|
|
|
var (converged, positions, violations) = Expander.Separate(selected, plate, spacing: 2);
|
|
Assert.True(converged, $"remaining: {violations.Count}");
|
|
Assert.Empty(violations);
|
|
Assert.True(positions[1].X >= positions[0].X + 6 - 1e-4);
|
|
Assert.True(positions[2].X >= positions[1].X + 6 - 1e-4);
|
|
Assert.Equal(10, middle.Location.X); // Separate is scratch-only.
|
|
Assert.Equal(14, last.Location.X);
|
|
|
|
var result = Expander.Expand(selected, plate, new Expander.Options { MaxSpacing = 2 });
|
|
Assert.True(result.AchievedSpacing >= 1.99, $"achieved {result.AchievedSpacing}");
|
|
Assert.True(middle.Location.X >= first.Location.X + 6 - 1e-4);
|
|
Assert.True(last.Location.X >= middle.Location.X + 6 - 1e-4);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_CannotPassThroughFullHeightWall_ToClearEndpoint()
|
|
{
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
var anchor = AddSquare(plate, 1, 4, 2);
|
|
var moving = AddSquare(plate, 4, 4, 2);
|
|
var wall = AddRectangle(plate, 6.5, 0, 0.2, 12);
|
|
|
|
// Target 5 puts the otherwise clear endpoint beyond the wall AND
|
|
// preserves the entry wall gap of 0.5. An endpoint-only check passes it.
|
|
var (converged, poses, violations) = Expander.Separate(
|
|
new List<Part> { anchor, moving }, plate, spacing: 5
|
|
);
|
|
Assert.False(converged);
|
|
Assert.NotEmpty(violations);
|
|
Assert.True(poses[1].X + 2 <= wall.Location.X + 1e-6,
|
|
$"move crossed wall: right={poses[1].X + 2}, wall={wall.Location.X}");
|
|
Assert.Equal(4, moving.Location.X); // public Separate never commits.
|
|
Assert.Equal(6.5, wall.Location.X);
|
|
|
|
var result = Expander.Expand(new List<Part> { anchor, moving }, plate,
|
|
new Expander.Options { InitialStep = 5, MaxSpacing = 5 });
|
|
Assert.True(moving.Location.X + 2 <= wall.Location.X + 1e-6);
|
|
Assert.True(result.AchievedSpacing < 5);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_OverlappingPair_CannotExitAcrossThirdWall()
|
|
{
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
var anchor = AddSquare(plate, 5, 4);
|
|
var moving = AddSquare(plate, 7, 4); // Overlaps anchor by two.
|
|
var wall = AddRectangle(plate, 12, 0, 0.2, 12);
|
|
|
|
var (converged, poses, violations) = Expander.Separate(
|
|
new List<Part> { anchor, moving }, plate, spacing: 5
|
|
);
|
|
Assert.False(converged);
|
|
Assert.NotEmpty(violations);
|
|
Assert.True(poses[1].X + 4 <= wall.Location.X + 1e-6,
|
|
$"overlap exit crossed third wall: {poses[1].X}");
|
|
Assert.Equal(7, moving.Location.X);
|
|
Assert.Equal(12, wall.Location.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_CannotCrossOppositeCutoutWall()
|
|
{
|
|
var plate = MakePlate(40, 20, edge: 0);
|
|
var host = new Part(new Drawing("host", RectangleWithHole(10, 10, 0.2, 0.2, 9.6, 9.6)), new Vector(5, 1));
|
|
plate.Parts.Add(host);
|
|
var anchor = AddSquare(plate, 1, 5, 1);
|
|
var moving = AddSquare(plate, 5.2, 5, 1); // Touches inner left wall.
|
|
|
|
var (converged, poses, _) = Expander.Separate(
|
|
new List<Part> { anchor, moving }, plate, spacing: 14
|
|
);
|
|
Assert.False(converged);
|
|
Assert.True(poses[1].X + 1 <= 14.8 + 1e-6,
|
|
$"insert crossed opposite hole wall: {poses[1].X}");
|
|
Assert.Equal(5.2, moving.Location.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_TinyClampZone_IsNotAnInvisibleWall()
|
|
{
|
|
// An edge spacing of half the target keeps the parts on the low edge,
|
|
// where the clamp blocks their row.
|
|
var plate = MakePlate(36, 18, edge: 3);
|
|
plate.ClampZones.Add(new ClampZone("tiny", new Box(9.5, 3, 1e-5, 1e-5)));
|
|
var anchor = AddSquare(plate, 4, 3, 2);
|
|
var moving = AddSquare(plate, 7, 3, 2);
|
|
|
|
var (converged, poses, _) = Expander.Separate(
|
|
new List<Part> { anchor, moving }, plate, spacing: 6
|
|
);
|
|
Assert.False(converged);
|
|
Assert.True(poses[1].X + 2 <= 9.5 + 1e-6,
|
|
$"crossed tiny clamp at x=9.5: right={poses[1].X + 2}");
|
|
Assert.Equal(7, moving.Location.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_RemoteTinyClampDoesNotPinUnrelatedGrowth()
|
|
{
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
plate.ClampZones.Add(new ClampZone("tiny", new Box(0, 0, 1e-5, 1e-5)));
|
|
var anchor = AddSquare(plate, 1, 4, 2);
|
|
var moving = AddSquare(plate, 4, 4, 2);
|
|
|
|
var (converged, poses, violations) = Expander.Separate(
|
|
new List<Part> { anchor, moving }, plate, spacing: 3
|
|
);
|
|
Assert.True(converged, $"remote zone pinned growth: {violations.Count} violations, " +
|
|
$"poses {poses[0].X:R},{poses[1].X:R}; " +
|
|
string.Join("; ", violations.Select(v => $"{v.A.BaseDrawing.Name}/{v.B.BaseDrawing.Name}={v.Achieved:R}")));
|
|
Assert.True(poses[1].X - poses[0].X >= 5 - 1e-4, $"opened {poses[1].X - poses[0].X:R}");
|
|
Assert.Equal(4, moving.Location.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_TouchingHoleWall_UsesOppositeDirectionToOpenGap()
|
|
{
|
|
var plate = MakePlate(40, 24, edge: 0);
|
|
var host = new Part(new Drawing("host", RectangleWithHole(20, 20, 5, 5, 10, 10)), new Vector(0, 0));
|
|
plate.Parts.Add(host);
|
|
var insert = AddSquare(plate, 13, 9, 2);
|
|
|
|
var (converged, poses, violations) = Expander.Separate(
|
|
new List<Part> { insert }, plate, spacing: 1
|
|
);
|
|
Assert.True(converged, $"contact still blocked: {violations.Count}");
|
|
Assert.True(poses[0].X <= 12 + 1e-4, $"insert at x={poses[0].X}");
|
|
Assert.True(poses[0].X >= 5 + 1 - 1e-4);
|
|
Assert.Equal(13, insert.Location.X);
|
|
Assert.Equal(0, host.Location.X);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(13, 9)]
|
|
[InlineData(5, 9)]
|
|
[InlineData(9, 13)]
|
|
[InlineData(9, 5)]
|
|
[InlineData(12.9, 9)] // Positive gap uses its ordinary nearest-boundary direction.
|
|
public void Separate_FourHoleSides_ReachARealUnitGap(double x, double y)
|
|
{
|
|
var plate = MakePlate(40, 24, edge: 0);
|
|
var host = new Part(new Drawing("host", RectangleWithHole(20, 20, 5, 5, 10, 10)), Vector.Zero);
|
|
plate.Parts.Add(host);
|
|
var insert = AddSquare(plate, x, y, 2);
|
|
|
|
var (converged, poses, violations) = Expander.Separate(new List<Part> { insert }, plate, 1);
|
|
Assert.True(converged, $"{x},{y}: {violations.Count} violations");
|
|
Assert.InRange(poses[0].X, 6 - 1e-4, 12 + 1e-4);
|
|
Assert.InRange(poses[0].Y, 6 - 1e-4, 12 + 1e-4);
|
|
Assert.Equal(x, insert.Location.X);
|
|
Assert.Equal(y, insert.Location.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_TouchingHoleInsertWithSecondSelectedPart_StaysInsideCutout()
|
|
{
|
|
var plate = MakePlate(40, 24, edge: 0);
|
|
var host = new Part(new Drawing("host", RectangleWithHole(20, 20, 5, 5, 10, 10)), Vector.Zero);
|
|
plate.Parts.Add(host);
|
|
var insert = AddSquare(plate, 13, 9, 2);
|
|
var other = AddSquare(plate, 30, 9, 2);
|
|
|
|
var result = Expander.Expand(new List<Part> { insert, other }, plate,
|
|
new Expander.Options { MaxSpacing = 1 });
|
|
Assert.False(result.Canceled);
|
|
Assert.True(result.AchievedSpacing >= 0.99);
|
|
Assert.InRange(insert.Location.X, 6 - 1e-4, 12 + 1e-4);
|
|
Assert.False(MateriallyOverlaps(insert, host));
|
|
Assert.Equal(0, host.Location.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_ReportsPartBlockerRatherThanPlateEdge()
|
|
{
|
|
var plate = MakePlate(20, 20, edge: 0);
|
|
var pinned = AddSquare(plate, 8, 8);
|
|
AddSquare(plate, 3.8, 8);
|
|
AddSquare(plate, 12.2, 8);
|
|
AddSquare(plate, 8, 3.8);
|
|
AddSquare(plate, 8, 12.2);
|
|
|
|
var (_, poses, violations) = Expander.Separate(new List<Part> { pinned }, plate, 1);
|
|
Assert.NotEmpty(violations);
|
|
Assert.All(violations, v => Assert.False(v.BlockedByEdge,
|
|
$"{v.B.BaseDrawing.Name}: blocker before the work-area edge, pose={poses[0]}"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_ReportsActualPlateEdgeWhenItLimitsMovement()
|
|
{
|
|
var plate = MakePlate(12, 12, edge: 0);
|
|
var anchor = AddSquare(plate, 1, 4, 2);
|
|
var moving = AddSquare(plate, 7, 4, 2);
|
|
|
|
var (_, poses, violations) = Expander.Separate(new List<Part> { anchor, moving }, plate, 9);
|
|
var pair = Assert.Single(violations.Where(v => ReferenceEquals(v.A, anchor) && ReferenceEquals(v.B, moving)));
|
|
Assert.True(pair.BlockedByEdge);
|
|
Assert.True(poses[1].X + 2 <= 12 + 1e-6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_ReportsPartBeforeFartherEdge_OnNarrowPlate()
|
|
{
|
|
var plate = MakePlate(9, 2, edge: 0);
|
|
var anchor = AddSquare(plate, 0, 0, 2);
|
|
var moving = AddSquare(plate, 3, 0, 2);
|
|
var wall = AddRectangle(plate, 5.5, 0, 0.2, 2);
|
|
|
|
var (_, poses, violations) = Expander.Separate(new List<Part> { anchor, moving }, plate, 4);
|
|
var pair = Assert.Single(violations.Where(v => ReferenceEquals(v.A, anchor) && ReferenceEquals(v.B, moving)));
|
|
Assert.False(pair.BlockedByEdge);
|
|
Assert.True(poses[1].X + 2 <= wall.Location.X + 1e-6);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData((int)Expander.Checkpoint.AfterScratchMove)]
|
|
[InlineData((int)Expander.Checkpoint.AfterSuccessfulProbe)]
|
|
[InlineData((int)Expander.Checkpoint.BeforeApply)]
|
|
public void Expand_CancellationAtDeterministicCheckpoint_LeavesLiveStateBitIdentical(int stage)
|
|
{
|
|
var cancelAt = (Expander.Checkpoint)stage;
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
var anchor = AddSquare(plate, 5, 4);
|
|
var moving = AddSquare(plate, 10, 4);
|
|
var wall = AddSquare(plate, 0, 4);
|
|
var before = plate.Parts.Select(p => (
|
|
Part: p, Program: p.Program,
|
|
X: BitConverter.DoubleToInt64Bits(p.Location.X),
|
|
Y: BitConverter.DoubleToInt64Bits(p.Location.Y),
|
|
Left: BitConverter.DoubleToInt64Bits(p.BoundingBox.Left))).ToArray();
|
|
var partSpacing = plate.PartSpacing;
|
|
var edgeSpacing = plate.EdgeSpacing;
|
|
using var cts = new CancellationTokenSource();
|
|
var seen = false;
|
|
|
|
var result = Expander.ExpandObserved(new List<Part> { anchor, moving }, plate,
|
|
new Expander.Options { MaxSpacing = 3 }, checkpoint =>
|
|
{
|
|
if (checkpoint != cancelAt)
|
|
return;
|
|
seen = true;
|
|
cts.Cancel();
|
|
}, cts.Token);
|
|
|
|
Assert.True(seen, $"checkpoint {cancelAt} was not reached");
|
|
Assert.True(result.Canceled);
|
|
foreach (var state in before)
|
|
{
|
|
Assert.Same(state.Program, state.Part.Program);
|
|
Assert.Equal(state.X, BitConverter.DoubleToInt64Bits(state.Part.Location.X));
|
|
Assert.Equal(state.Y, BitConverter.DoubleToInt64Bits(state.Part.Location.Y));
|
|
Assert.Equal(state.Left, BitConverter.DoubleToInt64Bits(state.Part.BoundingBox.Left));
|
|
}
|
|
Assert.Equal(partSpacing, plate.PartSpacing);
|
|
Assert.Equal(edgeSpacing, plate.EdgeSpacing);
|
|
Assert.Equal(0, wall.Location.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_PinnedBetweenFixedParts_StopsAfterOneNoProgressPass()
|
|
{
|
|
var plate = MakePlate(20, 20, edge: 0);
|
|
var pinned = AddSquare(plate, 8, 8);
|
|
AddSquare(plate, 3.8, 8);
|
|
AddSquare(plate, 12.2, 8);
|
|
AddSquare(plate, 8, 3.8);
|
|
AddSquare(plate, 8, 12.2);
|
|
var passes = 0;
|
|
var initial = pinned.Location;
|
|
|
|
var (converged, poses, violations) = Expander.SeparateObserved(
|
|
new List<Part> { pinned }, plate, spacing: 1,
|
|
checkpoint =>
|
|
{
|
|
if (checkpoint == Expander.Checkpoint.AfterPairPass)
|
|
passes++;
|
|
}, maxIterations: 10000
|
|
);
|
|
|
|
Assert.False(converged);
|
|
Assert.NotEmpty(violations);
|
|
Assert.Equal(1, passes); // Not a MaxIterations spin or wall-clock assertion.
|
|
Assert.Equal(initial, poses[0]);
|
|
Assert.Equal(initial, pinned.Location);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_WallsAndUnselectedPairs_StayExactlyAtClearance()
|
|
{
|
|
// Selection must not be pushed to open gaps between parts it excludes.
|
|
var plate = MakePlate(40, 12);
|
|
var w1 = AddSquare(plate, 2, 4);
|
|
var w2 = AddSquare(plate, 6.5, 4); // 0.5 apart from w1, both unselected
|
|
var a = AddSquare(plate, 14, 4);
|
|
var b = AddSquare(plate, 20, 4);
|
|
|
|
Expander.Expand(new List<Part> { a, b }, plate);
|
|
|
|
Assert.Equal(2, w1.Location.X, 6);
|
|
Assert.Equal(6.5, w2.Location.X, 6);
|
|
Assert.True(a.Intersects(w1, out _) == false);
|
|
Assert.True(b.Intersects(w2, out _) == false);
|
|
}
|
|
|
|
private static Program RectangleWithRoundHoles(double length, double width, int holes)
|
|
{
|
|
var program = new Program();
|
|
for (var i = 1; i <= holes; i++)
|
|
{
|
|
var center = new Vector(i * length / (holes + 1), width / 2);
|
|
var start = new Vector(center.X + 0.5, center.Y);
|
|
program.MoveTo(start);
|
|
program.ArcTo(start, center, RotationType.CW);
|
|
}
|
|
program.MoveTo(0, 0);
|
|
program.LineTo(length, 0);
|
|
program.LineTo(length, width);
|
|
program.LineTo(0, width);
|
|
program.LineTo(0, 0);
|
|
return program;
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_PackedPartsWithManyHoles_FinishesWellBeforeTheBudget()
|
|
{
|
|
// Every floor check on a packed grid measures all its neighbors. Without
|
|
// pruning far pairs and hole rings this takes minutes, not seconds.
|
|
var plate = MakePlate(120, 60, edge: 0.25);
|
|
var drawing = new Drawing("holes", RectangleWithRoundHoles(26, 6, 12));
|
|
var selected = new List<Part>();
|
|
for (var row = 0; row < 3; row++)
|
|
for (var column = 0; column < 4; column++)
|
|
{
|
|
var part = new Part(drawing, new Vector(0.25 + column * 26.25, 0.25 + row * 6.25));
|
|
plate.Parts.Add(part);
|
|
selected.Add(part);
|
|
}
|
|
|
|
using var budget = new CancellationTokenSource(TimeSpan.FromSeconds(30));
|
|
var result = Expander.Expand(selected, plate, null, budget.Token);
|
|
|
|
Assert.False(result.Canceled);
|
|
Assert.True(result.AchievedSpacing >= 0.25 - 1e-3, $"achieved {result.AchievedSpacing}");
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_PartMayCloseOnAClamp_LikeAnEdge()
|
|
{
|
|
// The clamp keep-out is a hard boundary: B may slide right up to it
|
|
// (its entry gap is not a floor), and the gap to it is not a part gap.
|
|
// The plate's edge spacing covers half the spacing, so no edge strip grows.
|
|
var plate = MakePlate(46, 28, edge: 8);
|
|
// The clamp clearance is the bottom edge spacing, so its keep-out starts at 28.
|
|
plate.ClampZones.Add(new ClampZone("clamp", new Box(36, 8, 1, 1)));
|
|
var a = AddSquare(plate, 9, 8, 2);
|
|
var b = AddSquare(plate, 12, 8, 2);
|
|
|
|
var result = Expander.Expand(new List<Part> { a, b }, plate);
|
|
|
|
Assert.Empty(result.Violations);
|
|
Assert.True(a.Location.X <= 8 + 1e-3, $"a at {a.Location.X:R}");
|
|
Assert.True(b.BoundingBox.Right <= 28 + 1e-6, $"b entered the clamp: right={b.BoundingBox.Right:R}");
|
|
Assert.True(b.BoundingBox.Right >= 28 - 0.01, $"b stopped short of the clamp: right={b.BoundingBox.Right:R}");
|
|
Assert.True(result.AchievedSpacing >= 15.9, $"achieved {result.AchievedSpacing:R}");
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_PartJustAboveAClamp_IsNotTheSmallestGap()
|
|
{
|
|
// A part resting 0.005 above a clamp neither caps the spacing at that gap
|
|
// nor has to climb off the clamp to the target spacing.
|
|
var plate = MakePlate(30, 12, edge: 0);
|
|
plate.ClampZones.Add(new ClampZone("clamp", new Box(2, 0, 1, 1)));
|
|
var a = AddSquare(plate, 1, 1.005, 4);
|
|
var b = AddSquare(plate, 5.5, 1.005, 4);
|
|
|
|
var result = Expander.Expand(new List<Part> { a, b }, plate, new Expander.Options { MaxSpacing = 2 });
|
|
|
|
Assert.Empty(result.Violations);
|
|
Assert.True(result.AchievedSpacing >= 1.99, $"achieved {result.AchievedSpacing:R}");
|
|
Assert.Equal(1.005, a.Location.Y, 6);
|
|
Assert.Equal(1.005, b.Location.Y, 6);
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
|
|
[Fact]
|
|
public void Separate_SqueezedMiddlePart_PartnersShareTheShortfall()
|
|
{
|
|
// A column like N26-2J3 plate 2's: 3.75 x 9 parts, gaps 0.25 then 0.257,
|
|
// selected out of stacking order. A half push lands a part between two
|
|
// short gaps just past the trigger; unless its partners take their share
|
|
// too, the next pair pushes it straight back and the column stalls.
|
|
var plate = MakePlate(20, 60, edge: 0);
|
|
var stack = new List<Part>();
|
|
var y = 0.002;
|
|
foreach (var gap in new[] { 0.25, 0.257, 0.257, 0.257, 0.257, 0.0 })
|
|
{
|
|
stack.Add(AddRectangle(plate, 5, y, 3.75, 9));
|
|
y += 9 + gap;
|
|
}
|
|
var selected = new List<Part> { stack[1], stack[0], stack[3], stack[2], stack[5], stack[4] };
|
|
|
|
var (converged, positions, violations) = Expander.Separate(selected, plate, spacing: 0.3);
|
|
|
|
Assert.True(converged, string.Join("; ", violations.Select(v => $"{v.A.BaseDrawing.Name}/{v.B?.BaseDrawing.Name}={v.Achieved:R}")));
|
|
var tops = selected.Select((part, i) => (Part: part, Y: positions[i].Y)).OrderBy(e => e.Y).ToList();
|
|
for (var i = 1; i < tops.Count; i++)
|
|
Assert.True(tops[i].Y - tops[i - 1].Y - 9 >= 0.3 - 1e-6, $"gap {i}: {tops[i].Y - tops[i - 1].Y - 9:R}");
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_PartFlushOnTheBottomEdge_StillSlidesAlongIt()
|
|
{
|
|
// Parts turned a quarter and a half carry rounding in their vertices, so the
|
|
// push between these two leans into the bottom edge by about 3e-15, as
|
|
// on N26-2J3's plates. Zero room below the right part must not pin its move
|
|
// along the edge: before the fix this pair could not open at all.
|
|
var plate = MakePlate(60, 20, edge: 0.25);
|
|
Part Turned(double length, double angle, double left)
|
|
{
|
|
var part = new Part(new Drawing($"turned{plate.Parts.Count}", Rectangle(length, 5.302)));
|
|
part.Rotate(angle);
|
|
part.Offset(new Vector(left - part.BoundingBox.Left, 0.25 - part.BoundingBox.Bottom));
|
|
plate.Parts.Add(part);
|
|
return part;
|
|
}
|
|
var leftPart = Turned(12.5, System.Math.PI / 2, 0.25);
|
|
var rightPart = Turned(12.5, System.Math.PI, leftPart.BoundingBox.Right + 0.285);
|
|
|
|
var result = Expander.Expand(new List<Part> { leftPart, rightPart }, plate,
|
|
new Expander.Options { MaxSpacing = 2 });
|
|
|
|
Assert.True(result.AchievedSpacing >= 1.99, $"achieved {result.AchievedSpacing:R}");
|
|
Assert.True(rightPart.BoundingBox.Bottom >= 0.25 - 1e-6);
|
|
AssertNoOverlaps(plate);
|
|
}
|
|
}
|
|
|