feat(engine): grow edge strips with Expand Spacing

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>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-30 22:11:45 -05:00
1 parent 6834f2905a
commit 55e8ea6084
6 files changed
+322 -63

No files matched your search

@@ -68,6 +68,27 @@ public class ExpanderPatternTests
private static double DiskGap(Part a, Part b) => a.Location.DistanceTo(b.Location) - 2 * Radius;
/// <summary>The smallest gap between disks in different rows: the gap a
/// stretch across the rows opens.</summary>
private static double RowToRowGap(List<Part> parts) => parts
.SelectMany((a, i) => parts.Skip(i + 1)
.Where(b => System.Math.Abs(a.Location.Y - b.Location.Y) > 1e-6)
.Select(b => DiskGap(a, b)))
.Min();
/// <summary>Asserts the bottom and top sheet-edge strips hold half the gap a
/// stretch across the rows opened, and the top one no more than that.</summary>
private static void AssertRowEdgeStrips(Plate plate, List<Part> parts)
{
var half = RowToRowGap(parts) / 2;
var sheet = plate.BoundingBox(false);
var bottom = parts.Min(p => p.BoundingBox.Bottom) - sheet.Bottom;
var top = sheet.Top - parts.Max(p => p.BoundingBox.Top);
Assert.True(bottom >= half - 1e-6, $"bottom strip {bottom} under half the row gap {half}");
Assert.True(top >= half - 1e-6, $"top strip {top} under half the row gap {half}");
Assert.True(top <= half + 0.02, $"top strip {top} wider than half the row gap {half}");
}
[Fact]
public void Expand_StaggeredFillTightAlongX_StretchesOnlyTheRowPitch()
{
@@ -93,11 +114,9 @@ public class ExpanderPatternTests
Assert.Equal(rowPitch, odd[1] - odd[0], 6);
Assert.Equal(even[0] + rowPitch / 2, odd[0], 6);
// The pattern fills the height and only the diagonal gap opened up.
var work = plate.WorkArea();
Assert.True(parts.Min(p => p.BoundingBox.Bottom) >= work.Bottom - 1e-9);
Assert.True(parts.Max(p => p.BoundingBox.Top) <= work.Top + 1e-9);
Assert.True(work.Top - parts.Max(p => p.BoundingBox.Top) < 0.02);
// The pattern fills the height but for edge strips of half the diagonal
// gap, and only the diagonal gap opened up.
AssertRowEdgeStrips(plate, parts);
Assert.Equal(rowMateGap, DiskGap(parts[0], parts[5]), 6);
Assert.True(DiskGap(parts[0], parts[3]) > 1, $"diagonal gap {DiskGap(parts[0], parts[3])}");
Assert.Equal(rowMateGap, result.AchievedSpacing, 2);
@@ -140,13 +159,12 @@ public class ExpanderPatternTests
Assert.Equal(5, spreadYs.Count);
for (var row = 1; row < 5; row++)
Assert.True(spreadYs[row] - spreadYs[row - 1] > rowYs[row] - rowYs[row - 1] + 0.1);
var work = plate.WorkArea();
Assert.True(work.Top - parts.Max(p => p.BoundingBox.Top) < 0.02);
AssertRowEdgeStrips(plate, parts);
Assert.True(parts.SelectMany((a, i) => parts.Skip(i + 1).Select(b => DiskGap(a, b))).Min() >= entryGap - 1e-6);
}
[Fact]
public void Expand_GridWithRoomBothWays_FillsTheWorkAreaEvenly()
public void Expand_GridWithRoomBothWays_FillsThePlateEvenlyWithHalfGapEdgeStrips()
{
var plate = MakePlate(40, 30, 0.5);
var drawing = new Drawing("square", TestDrawingFactory.Rectangle(4, 4));
@@ -167,11 +185,18 @@ public class ExpanderPatternTests
Assert.Equal(ys[1] - ys[0], ys[2] - ys[1], 6);
Assert.True(xs[1] - xs[0] > 5.5 && ys[1] - ys[0] > 5.5);
var work = plate.WorkArea();
Assert.True(parts.Min(p => p.BoundingBox.Left) >= work.Left - 1e-9);
Assert.True(parts.Max(p => p.BoundingBox.Right) <= work.Right + 1e-9);
Assert.True(work.Right - parts.Max(p => p.BoundingBox.Right) + parts.Min(p => p.BoundingBox.Left) - work.Left < 0.02);
Assert.True(work.Top - parts.Max(p => p.BoundingBox.Top) + parts.Min(p => p.BoundingBox.Bottom) - work.Bottom < 0.02);
// Each edge strip is half the gap along its axis: 40 = 3 * 4 + 3 * gap
// across X and 30 = 3 * 4 + 3 * gap across Y.
var gapX = xs[1] - xs[0] - 4;
var gapY = ys[1] - ys[0] - 4;
Assert.Equal(28.0 / 3, gapX, 1);
Assert.Equal(6, gapY, 1);
Assert.Equal(gapX / 2, parts.Min(p => p.BoundingBox.Left), 1);
Assert.Equal(gapX / 2, 40 - parts.Max(p => p.BoundingBox.Right), 1);
Assert.Equal(gapY / 2, parts.Min(p => p.BoundingBox.Bottom), 1);
Assert.Equal(gapY / 2, 30 - parts.Max(p => p.BoundingBox.Top), 1);
Assert.True(parts.Min(p => p.BoundingBox.Left) >= gapX / 2 - 1e-6);
Assert.True(parts.Min(p => p.BoundingBox.Bottom) >= gapY / 2 - 1e-6);
}
[Fact]
@@ -44,21 +44,22 @@ public class ExpanderQualityReviewTests
[Fact]
public void TinyZoneInsideCutoutIsNotReportedAsMaterialOverlap()
{
var plate = MakePlate(30);
plate.ClampZones.Add(new ClampZone("tiny", new Box(5, 5, 1e-5, 1e-5)));
// An edge spacing of half the target holds the frame's edge strips as is.
var plate = new Plate(new Size(12.5, 30.5)) { EdgeSpacing = new Spacing(0.25, 0.25) };
plate.ClampZones.Add(new ClampZone("tiny", new Box(5.25, 5.25, 1e-5, 1e-5)));
var frameProgram = TestDrawingFactory.Rectangle(10, 10);
frameProgram.MoveTo(4, 4);
frameProgram.LineTo(6, 4);
frameProgram.LineTo(6, 6);
frameProgram.LineTo(4, 6);
frameProgram.LineTo(4, 4);
var frame = new Part(new Drawing("neutral-frame", frameProgram), Vector.Zero);
var frame = new Part(new Drawing("neutral-frame", frameProgram), new Vector(0.25, 0.25));
plate.Parts.Add(frame);
var result = Expander.Separate(new List<Part> { frame }, plate, spacing: 0.5);
Assert.True(result.Converged, $"zone inside cutout reported as material: {result.Violations.Count}");
Assert.Empty(result.Violations);
Assert.Equal(0, frame.Location.X);
Assert.Equal(0.25, frame.Location.X);
}
[Fact]
+102 -25
View File
@@ -78,7 +78,7 @@ public class ExpanderTests
}
[Fact]
public void Expand_TwoSquares_BothMoveToTheEdgeFloors()
public void Expand_TwoSquares_BothMoveOutToHalfSpacingEdgeStrips()
{
var plate = MakePlate(24, 24);
var a = AddSquare(plate, 6, 10);
@@ -86,17 +86,88 @@ public class ExpanderTests
var result = Expander.Expand(new List<Part> { a, b }, plate);
// Neither part is an anchor: A slides to the left edge floor (0.5) and
// B to the right one (23.5 - 4 = 19.5), so the gap is 15.
Assert.Equal(0.5, a.Location.X, 1);
// 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(19.5, b.Location.X, 1);
Assert.Equal(16, b.Location.X, 1);
Assert.Equal(10, b.Location.Y, 6);
Assert.True(result.AchievedSpacing >= 14.9, $"achieved {result.AchievedSpacing}");
Assert.True(result.AchievedSpacing <= 15.1, $"achieved {result.AchievedSpacing}");
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);
Assert.True(a.BoundingBox.Left >= 0.5 - 1e-6);
Assert.True(b.BoundingBox.Right <= 23.5 + 1e-6);
}
[Fact]
@@ -382,8 +453,10 @@ public class ExpanderTests
var result = Expander.Expand(new List<Part> { anchor, mover }, plate);
// Seven units of free travel divide evenly across both sides of the mover.
Assert.True(result.AchievedSpacing >= 3.4, $"achieved {result.AchievedSpacing}");
// 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);
}
@@ -476,18 +549,20 @@ public class ExpanderTests
[Fact]
public void Separate_TinyClampZone_IsNotAnInvisibleWall()
{
var plate = MakePlate(30, 12, edge: 0);
plate.ClampZones.Add(new ClampZone("tiny", new Box(6.5, 0, 1e-5, 1e-5)));
var anchor = AddSquare(plate, 1, 0, 2);
var moving = AddSquare(plate, 4, 0, 2);
// 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 <= 6.5 + 1e-6,
$"crossed tiny clamp at x=6.5: right={poses[1].X + 2}");
Assert.Equal(4, moving.Location.X);
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]
@@ -745,17 +820,19 @@ public class ExpanderTests
{
// 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.
var plate = MakePlate(30, 12, edge: 0);
plate.ClampZones.Add(new ClampZone("clamp", new Box(20, 0, 1, 1)));
var a = AddSquare(plate, 1, 0, 2);
var b = AddSquare(plate, 4, 0, 2);
// 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 <= 1e-3, $"a at {a.Location.X:R}");
Assert.True(b.BoundingBox.Right <= 20 + 1e-6, $"b entered the clamp: right={b.BoundingBox.Right:R}");
Assert.True(b.BoundingBox.Right >= 20 - 0.01, $"b stopped short of the clamp: right={b.BoundingBox.Right:R}");
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);
}