mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-05 15:19:27 -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
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user