mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 07:19:24 -05:00
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:
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]
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,10 @@ namespace OpenNest.Engine.Fill
|
||||
/// Input with overlaps is accepted for safe direct separation; unresolved
|
||||
/// overlaps remain in the violation report. Moves are straight-line only —
|
||||
/// a pair that requires routing around a blocker remains unresolved.
|
||||
/// Plate edges retain their EdgeSpacing floor.
|
||||
/// Each sheet-edge strip grows with the spacing: a part's box stays at least
|
||||
/// half the target spacing from the plate edge, and never inside the
|
||||
/// plate's EdgeSpacing, so the strip holds as much material per cut as a
|
||||
/// web between two parts.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Mover policy: both parts of a violated selected pair move apart, sharing
|
||||
@@ -103,8 +106,8 @@ namespace OpenNest.Engine.Fill
|
||||
/// <summary>
|
||||
/// Raises the part-to-part spacing of the selection as far as the plate
|
||||
/// and its other parts allow, applying the best spacing found. Plate
|
||||
/// PartSpacing/EdgeSpacing are not modified; edges keep their own
|
||||
/// EdgeSpacing floor while only part-to-part clearance chases the target.
|
||||
/// PartSpacing/EdgeSpacing are not modified; each edge strip keeps at least
|
||||
/// its EdgeSpacing and grows to half the target spacing beyond that.
|
||||
/// Mutates <paramref name="selected"/> locations only after the final
|
||||
/// proposal passes validation; failed individual probes and cancellation
|
||||
/// leave all live locations unchanged. A completed run can apply safe
|
||||
@@ -399,6 +402,20 @@ namespace OpenNest.Engine.Fill
|
||||
// a complete pass without progress stops genuinely pinned layouts.
|
||||
var moved = 0.0;
|
||||
|
||||
// Edge strips first: a part closer to the sheet edge than half the
|
||||
// spacing moves inward, so pair pushes then open around it.
|
||||
for (var i = 0; i < context.MoverCount; i++)
|
||||
{
|
||||
var applied = PushFromEdge(context, positions, i, spacing, epsMove);
|
||||
if (applied > 0)
|
||||
{
|
||||
moved += applied;
|
||||
observer?.Invoke(Checkpoint.AfterScratchMove);
|
||||
if (token.IsCancellationRequested)
|
||||
return (false, start, violations);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var pair in context.Pairs)
|
||||
{
|
||||
if (token.IsCancellationRequested)
|
||||
@@ -504,7 +521,54 @@ namespace OpenNest.Engine.Fill
|
||||
}
|
||||
|
||||
violations.AddRange(context.WorkAreaViolations(positions));
|
||||
return (violations.Count == 0, positions, violations);
|
||||
// An edge strip short of half the spacing fails the probe but is not a
|
||||
// violation: the edge is no part, and the strip never counts toward the
|
||||
// reported spacing.
|
||||
var edgesHold = true;
|
||||
for (var i = 0; i < context.MoverCount && edgesHold; i++)
|
||||
edgesHold = context.EdgeShortfall(i, positions[i], spacing).DistanceTo(Vector.Zero) <= EdgeTolerance;
|
||||
return (violations.Count == 0 && edgesHold, positions, violations);
|
||||
}
|
||||
|
||||
/// <summary>How far short of its edge limit a part may sit and still hold it.</summary>
|
||||
private const double EdgeTolerance = 1e-6;
|
||||
|
||||
/// <summary>Moves a part that sits closer to the sheet edge than half the
|
||||
/// spacing straight inward on the scratch positions, as far as material and
|
||||
/// every pair floor allow; returns the distance moved.</summary>
|
||||
private static double PushFromEdge(
|
||||
SeparationContext context, List<Vector> positions, int mover, double spacing, double epsMove)
|
||||
{
|
||||
var shortfall = context.EdgeShortfall(mover, positions[mover], spacing);
|
||||
var length = shortfall.DistanceTo(Vector.Zero);
|
||||
if (length <= EdgeTolerance)
|
||||
return 0;
|
||||
|
||||
var start = positions[mover];
|
||||
var direction = shortfall / length;
|
||||
var need = length + 2 * EdgeTolerance;
|
||||
var room = context.ClipToMaterial(mover, positions,
|
||||
direction, context.ClipToWorkArea(mover, start, direction, need, spacing));
|
||||
if (room <= epsMove)
|
||||
return 0;
|
||||
if (!context.MaintainsValidity(mover, start + direction * room, positions, null, 0, spacing))
|
||||
{
|
||||
var lo = 0.0;
|
||||
var hi = room;
|
||||
for (var bisect = 0; bisect < 24 && hi - lo > 1e-6; bisect++)
|
||||
{
|
||||
var mid = (lo + hi) / 2;
|
||||
if (context.MaintainsValidity(mover, start + direction * mid, positions, null, 0, spacing))
|
||||
lo = mid;
|
||||
else
|
||||
hi = mid;
|
||||
}
|
||||
room = lo;
|
||||
if (room <= epsMove)
|
||||
return 0;
|
||||
}
|
||||
positions[mover] = start + direction * room;
|
||||
return room;
|
||||
}
|
||||
|
||||
// Shares of a pair's shortfall, in order: a lone mover takes it all; a pair of
|
||||
@@ -563,7 +627,7 @@ namespace OpenNest.Engine.Fill
|
||||
double distance, Vector direction, double need, double spacing, double epsMove)
|
||||
{
|
||||
var start = positions[mover];
|
||||
var edgeRoom = context.ClipToWorkArea(mover, start, direction, need);
|
||||
var edgeRoom = context.ClipToWorkArea(mover, start, direction, need, spacing);
|
||||
var room = context.ClipToMaterial(mover, positions, direction, edgeRoom);
|
||||
var edgeLimited = edgeRoom < need - epsMove;
|
||||
var partLimited = room < edgeRoom - epsMove;
|
||||
@@ -637,6 +701,7 @@ namespace OpenNest.Engine.Fill
|
||||
private readonly int clampStart; // synthetic walls, after actual plate parts
|
||||
private readonly IReadOnlyList<Box> clampBoxes; // exact placement-frame keep-outs
|
||||
private readonly Box workArea;
|
||||
private readonly Spacing edgeSpacing;
|
||||
|
||||
public readonly List<Pair> Pairs = new();
|
||||
public readonly double SpacingCap;
|
||||
@@ -684,6 +749,7 @@ namespace OpenNest.Engine.Fill
|
||||
int clampStart,
|
||||
IReadOnlyList<Box> clampBoxes,
|
||||
Box workArea,
|
||||
Spacing edgeSpacing,
|
||||
double spacingCap
|
||||
)
|
||||
{
|
||||
@@ -694,6 +760,7 @@ namespace OpenNest.Engine.Fill
|
||||
this.clampStart = clampStart;
|
||||
this.clampBoxes = clampBoxes;
|
||||
this.workArea = workArea;
|
||||
this.edgeSpacing = edgeSpacing;
|
||||
SpacingCap = spacingCap;
|
||||
}
|
||||
|
||||
@@ -739,6 +806,7 @@ namespace OpenNest.Engine.Fill
|
||||
clampStart,
|
||||
clampBoxes,
|
||||
workArea,
|
||||
plate.EdgeSpacing,
|
||||
spacingCap
|
||||
);
|
||||
context.BuildPairs(token);
|
||||
@@ -884,6 +952,55 @@ namespace OpenNest.Engine.Fill
|
||||
|
||||
public bool IsMover(int index) => index < moverCount;
|
||||
|
||||
public int MoverCount => moverCount;
|
||||
|
||||
/// <summary>
|
||||
/// The limits a part's box keeps at a target spacing: each sheet-edge
|
||||
/// strip is at least half the spacing wide, and never narrower than the
|
||||
/// plate's own edge spacing on that side (the work area).
|
||||
/// </summary>
|
||||
public (double Left, double Bottom, double Right, double Top) EdgeLimits(double spacing)
|
||||
{
|
||||
var half = System.Math.Max(0, spacing) / 2;
|
||||
return (
|
||||
workArea.Left + System.Math.Max(0, half - edgeSpacing.Left),
|
||||
workArea.Bottom + System.Math.Max(0, half - edgeSpacing.Bottom),
|
||||
workArea.Right - System.Math.Max(0, half - edgeSpacing.Right),
|
||||
workArea.Top - System.Math.Max(0, half - edgeSpacing.Top));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The inward move that would bring a mover's box to its edge limits at
|
||||
/// a target spacing; zero when it holds them. A cut-off runs to the edge
|
||||
/// by design, and a part outside the work area is reported as an edge
|
||||
/// violation instead, so neither has an edge target.
|
||||
/// </summary>
|
||||
public Vector EdgeShortfall(int index, Vector position, double spacing)
|
||||
{
|
||||
if (parts[index].BaseDrawing.IsCutOff || !InsideWorkArea(index, position))
|
||||
return Vector.Zero;
|
||||
var limits = EdgeLimits(spacing);
|
||||
var box = localBoxes[index].Translate(position);
|
||||
return new Vector(
|
||||
System.Math.Max(0, limits.Left - box.Left) - System.Math.Max(0, box.Right - limits.Right),
|
||||
System.Math.Max(0, limits.Bottom - box.Bottom) - System.Math.Max(0, box.Top - limits.Top));
|
||||
}
|
||||
|
||||
/// <summary>The smallest clearance between two movers that
|
||||
/// <paramref name="include"/> accepts, at the given positions; 0 when none.</summary>
|
||||
public double SelectedClearance(List<Vector> positions, Func<int, int, bool> include)
|
||||
{
|
||||
var min = double.MaxValue;
|
||||
foreach (var pair in Pairs)
|
||||
{
|
||||
if (!IsMover(pair.IndexB) || !include(pair.IndexA, pair.IndexB)
|
||||
|| ProvesClearance(pair, positions, min))
|
||||
continue;
|
||||
min = System.Math.Min(min, PairClearance(pair.IndexA, pair.IndexB, positions).Distance);
|
||||
}
|
||||
return min == double.MaxValue ? 0 : min;
|
||||
}
|
||||
|
||||
public Box WorkArea => workArea;
|
||||
|
||||
/// <summary>True when anything besides the movers can block them.</summary>
|
||||
@@ -1184,20 +1301,25 @@ namespace OpenNest.Engine.Fill
|
||||
|
||||
/// <summary>
|
||||
/// Largest α ≤ need such that translating the part by direction·α keeps
|
||||
/// its AABB inside the work area.
|
||||
/// its AABB inside the work area and no closer to a sheet edge than its
|
||||
/// edge limit at <paramref name="spacing"/>, or than it already is when it
|
||||
/// sits outside that limit, so it may still slide along and away.
|
||||
/// </summary>
|
||||
public double ClipToWorkArea(int index, Vector position, Vector direction, double need)
|
||||
public double ClipToWorkArea(int index, Vector position, Vector direction, double need, double spacing)
|
||||
{
|
||||
// Each side gets the same allowance InsideWorkArea accepts. A part
|
||||
// flush on an edge (no room at all) otherwise pins every move whose
|
||||
// direction leans into that edge by rounding, such as (1, -1e-17)
|
||||
// along the bottom edge: zero room over a tiny component is zero.
|
||||
const double Allowance = 1e-9;
|
||||
var box = localBoxes[index];
|
||||
var left = position.X + box.Left - workArea.Left + Allowance;
|
||||
var right = workArea.Right - (position.X + box.Right) + Allowance;
|
||||
var bottom = position.Y + box.Bottom - workArea.Bottom + Allowance;
|
||||
var top = workArea.Top - (position.Y + box.Top) + Allowance;
|
||||
var box = localBoxes[index].Translate(position);
|
||||
var limits = parts[index].BaseDrawing.IsCutOff
|
||||
? (workArea.Left, workArea.Bottom, workArea.Right, workArea.Top)
|
||||
: EdgeLimits(spacing);
|
||||
var left = box.Left - System.Math.Min(limits.Left, System.Math.Max(workArea.Left, box.Left)) + Allowance;
|
||||
var right = System.Math.Max(limits.Right, System.Math.Min(workArea.Right, box.Right)) - box.Right + Allowance;
|
||||
var bottom = box.Bottom - System.Math.Min(limits.Bottom, System.Math.Max(workArea.Bottom, box.Bottom)) + Allowance;
|
||||
var top = System.Math.Max(limits.Top, System.Math.Min(workArea.Top, box.Top)) - box.Top + Allowance;
|
||||
|
||||
var max = need;
|
||||
if (direction.X > 0)
|
||||
@@ -1573,10 +1695,14 @@ namespace OpenNest.Engine.Fill
|
||||
{
|
||||
// Only comparisons with a threshold follow, so each clearance is
|
||||
// measured just past it (WithinReach): far segments are skipped.
|
||||
var improved = PairClearanceWithin(active.IndexA, active.IndexB, positions,
|
||||
WithinReach(activeDistance + 1e-7));
|
||||
if (!double.IsFinite(improved) || improved <= activeDistance + 1e-7)
|
||||
return false;
|
||||
// An edge push has no active pair; it only has to keep the floors.
|
||||
if (active != null)
|
||||
{
|
||||
var improved = PairClearanceWithin(active.IndexA, active.IndexB, positions,
|
||||
WithinReach(activeDistance + 1e-7));
|
||||
if (!double.IsFinite(improved) || improved <= activeDistance + 1e-7)
|
||||
return false;
|
||||
}
|
||||
foreach (var pair in pairsOf[index])
|
||||
{
|
||||
var floor = pair.FloorAt(spacing) - FloorTolerance;
|
||||
|
||||
@@ -387,7 +387,31 @@ namespace OpenNest.Engine.Fill
|
||||
var entryLow = bounds.Min(LowOf);
|
||||
var entryHigh = bounds.Max(HighOf);
|
||||
|
||||
// Stretched extent with no shift; null when it is wider than the area.
|
||||
// The smallest gap this stretch widens: between selected parts whose
|
||||
// lattice points differ along this axis. A gap across the other axis
|
||||
// (a row gap while stretching along the rows) is not this axis's to
|
||||
// open, and a shift moves the whole selection, so neither counts.
|
||||
var gaps = new Dictionary<double, double>();
|
||||
double GapAt(double scale)
|
||||
{
|
||||
if (!gaps.TryGetValue(scale, out var gap))
|
||||
gaps[scale] = gap = context.SelectedClearance(
|
||||
horizontal
|
||||
? Positions(scale, 0, ScaleY, shiftY)
|
||||
: Positions(ScaleX, shiftX, scale, 0),
|
||||
(a, b) => System.Math.Abs(Of(lattice[a]) - Of(lattice[b])) > tolerance);
|
||||
return gap;
|
||||
}
|
||||
|
||||
// Where the stretched selection may reach along this axis: each edge
|
||||
// strip at least half the gap this stretch widens.
|
||||
(double Low, double High) Room(double scale)
|
||||
{
|
||||
var limits = context.EdgeLimits(GapAt(scale));
|
||||
return horizontal ? (limits.Left, limits.Right) : (limits.Bottom, limits.Top);
|
||||
}
|
||||
|
||||
// Stretched extent with no shift; null when it is wider than its room.
|
||||
(double Low, double High)? Extent(double scale)
|
||||
{
|
||||
var low = double.MaxValue;
|
||||
@@ -398,7 +422,8 @@ namespace OpenNest.Engine.Fill
|
||||
low = System.Math.Min(low, LowOf(bounds[i]) + move);
|
||||
high = System.Math.Max(high, HighOf(bounds[i]) + move);
|
||||
}
|
||||
return high - low <= areaHigh - areaLow ? (low, high) : null;
|
||||
var room = Room(scale);
|
||||
return high - low <= room.High - room.Low ? (low, high) : null;
|
||||
}
|
||||
|
||||
double ShiftFor(double scale, Placement placement)
|
||||
@@ -410,7 +435,8 @@ namespace OpenNest.Engine.Fill
|
||||
Placement.High => entryHigh - high,
|
||||
_ => (entryLow + entryHigh - low - high) / 2,
|
||||
};
|
||||
return System.Math.Clamp(shift, areaLow - low, areaHigh - high);
|
||||
var room = Room(scale);
|
||||
return System.Math.Clamp(shift, room.Low - low, room.High - high);
|
||||
}
|
||||
|
||||
bool Valid(double scale, Placement placement)
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
# Expand Spacing
|
||||
|
||||
`Plate > Expand Spacing` moves selected parts by translation only. Any selected part may move: a pair of selected parts shares each push (the later part takes half, the earlier part the rest, and the later part whatever its partner has no room for), so the selection opens about its middle and a part on the edge leaves the push to its partner. A selected part pushes away from a nonselected part alone, and nonselected parts remain fixed. The search probes ever wider spacings (doubling, then bisecting between the last spacing that converged and the first that failed) and applies the widest one that converged. The plate retains its own `EdgeSpacing` floor; the command does not change `PartSpacing` or `EdgeSpacing`. To push apart only the parts the overlap check flags, use [Fix Spacing](fix-spacing.md).
|
||||
`Plate > Expand Spacing` moves selected parts by translation only. Any selected part may move: a pair of selected parts shares each push (the later part takes half, the earlier part the rest, and the later part whatever its partner has no room for), so the selection opens about its middle and a part on the edge leaves the push to its partner. A selected part pushes away from a nonselected part alone, and nonselected parts remain fixed. The search probes ever wider spacings (doubling, then bisecting between the last spacing that converged and the first that failed) and applies the widest one that converged. The command does not change `PartSpacing` or `EdgeSpacing`. To push apart only the parts the overlap check flags, use [Fix Spacing](fix-spacing.md).
|
||||
|
||||
## Edge strips
|
||||
|
||||
The strip of sheet between a part and the plate edge grows with the spacing: at a target spacing *s*, each selected part's box stays at least *s*/2 from every plate edge, and never inside that side's `EdgeSpacing`. A web between two parts takes heat from a cut on each side, while an edge strip takes it from one, so a strip half as wide holds about as much material per cut. Until *s* passes twice the edge spacing the strips are just the edge spacing, as before. Beyond that, a part closer to the edge than *s*/2 is pushed straight inward, as far as material and its pair floors allow. A probe converges only when every strip holds, so a part boxed in between the edge and a fixed part caps the spacing at the widest *s* its strip and gap can share. The strip never counts toward the reported spacing. When no wider spacing converges, the layout keeps its entry spacing, and edge parts move inward only as far as that spacing's floors allow. Cut-offs run to the edge by design and have no strip, and a part already outside the work area is reported as an edge violation instead. A clamp keep-out stays a hard boundary with no strip.
|
||||
|
||||
## Repeating patterns
|
||||
|
||||
A selection that repeats on a lattice, such as a fill, is stretched as one pattern instead of going through the pair search below. Parts of one drawing at one rotation form a motif group, and every group must sit on the same two lattice translations (one for a single row) to within 2% of a pitch. Each group needs at least two parts, and one group needs at least three for a row or four for a grid, because any two or three points fit some lattice. A single-drawing selection that fits no one lattice also counts when it forms rows along X (or Y): at least two rows of at least two parts, one of at least three, whose steps along the row are whole multiples of one shared pitch. Such rows may stack at any spacing and offset, as when staggered rows are topped by straight ones, and each part then stretches on its own. Cut-offs, overlapping selections and selections that start outside the work area always use the pair search.
|
||||
|
||||
The lattice is scaled along X, then along Y, about its center, and each part moves by the displacement of its motif copy's lattice point, so interlocked pairs and other multi-part motifs stay rigid. Along each axis the largest scale is used whose stretched selection fits the work area and keeps every floor: no selected gap closes below the smallest selected gap at entry, a gap to a nonselected part closes no further than that, cut-offs keep their entry gaps and no part enters a clamp keep-out. A part may not pass through a nonselected part, cut-off or clamp on its straight way from the entry layout. The stretched selection is tried centered on where it was, then held at its low edge, then at its high edge. An axis with no room keeps its pitch, so a fill whose rows run edge to edge along X only opens its rows along Y: the row gap stays and the diagonal gap grows. The status bar reads "Spread the repeating pattern" with the smallest gap, or says the pattern has no room to spread.
|
||||
The lattice is scaled along X, then along Y, about its center, and each part moves by the displacement of its motif copy's lattice point, so interlocked pairs and other multi-part motifs stay rigid. Along each axis the largest scale is used whose stretched selection fits the work area, less edge strips on that axis's two sides of half the smallest gap the stretch widens (between parts whose lattice points differ along the axis, so a row gap does not hold back a stretch along the rows), and keeps every floor: no selected gap closes below the smallest selected gap at entry, a gap to a nonselected part closes no further than that, cut-offs keep their entry gaps and no part enters a clamp keep-out. A part may not pass through a nonselected part, cut-off or clamp on its straight way from the entry layout. The stretched selection is tried centered on where it was, then held at its low edge, then at its high edge. An axis with no room keeps its pitch, so a fill whose rows run edge to edge along X only opens its rows along Y: the row gap stays and the diagonal gap grows. The status bar reads "Spread the repeating pattern" with the smallest gap, or says the pattern has no room to spread.
|
||||
|
||||
Every selected/selected and selected/nonselected pair has a clearance floor, measured against material with cutouts subtracted. A pair of parts may close to the spacing being tried but never below its own starting gap when that gap is already smaller, so a wide gap can be spent evenly across a layout and a tight one cannot shrink. A cut-off always keeps its full starting gap. A clamp is a hard edge, like the work-area edge: its keep-out (the zone grown by the clamp clearance) may be approached until a part touches it but never entered, and the gap to it neither has to reach the spacing being tried nor counts toward the reported spacing. A later move may reuse slack opened by an earlier move. Existing overlaps are accepted for attempted separation; a remaining overlap is reported as a violation, not a successful positive gap. Geometry whose material side is unresolved at sub-scale is conservatively reported as a violation when native extents intersect; that negative marker is not a measured penetration depth. An unchanged selected part already outside the work area also yields an edge violation and zero credited spacing. Temporarily blocked pairs are retried after another part moves, while a pass without positive movement stops the relaxation. An individual violation's `BlockedByEdge` flag is true only when the work-area edge is the first limiting cause; the desktop status currently displays a blocked-pair count, not those individual causes.
|
||||
|
||||
|
||||
Reference in new issue
Block a user