fix(planning): route a cut-off from its start to its far end

A cut-off always runs away from its fixed edge: the origin side, the only
edge guaranteed to be a real sheet edge. The part order is proposed as a
traveling-salesman path over one point per placement, and a cut-off's
point was the middle of its line. The route did not know the tool must
arrive at the line's start and leaves at its far end, so the parts
before a sheet-splitting line finished level with its middle, and the
rapid down to its start crossed parts already cut.

Each placement now has an entry and an exit point; they differ only for
cut-offs (start and departure). Nearest neighbor and Or-opt go on from
each part's exit, and 2-opt costs a reversed run along its own directed
legs; with no cut-off on the plate the exits are not passed and the
symmetric route is unchanged. The part cut before a cut-off faces its
start.

Cut-off direction is not chosen here: the order reads each cut-off's start
and end from its program as given, and the joint search may still reverse a
head-locked far-edge segment.
This commit is contained in:
aj committed 2026-11-01 16:51:09 -05:00
1 parent 277719a1d6
commit 4e92d0e8f3
6 files changed
+196 -27

No files matched your search

@@ -20,9 +20,11 @@ internal static class CuttingNestedOrder
/// <param name="Prerequisites">Ordinals that must come before each part, learned rules included.</param>
/// <param name="Inside">Per part ordinal, the parts nested inside its cutouts at any depth.</param>
/// <param name="HostEntry">The point on a host's contours nearest a tool position.</param>
/// <param name="Exits">Optional point each part is left from (a cut-off's far end); null when
/// every part is left from its center.</param>
internal sealed record Context(IReadOnlyList<Vector> Centers,
IReadOnlyList<IReadOnlyCollection<int>> Prerequisites, IReadOnlyList<IReadOnlyCollection<int>> Inside,
Func<int, Vector, Vector> HostEntry, CancellationToken Token);
Func<int, Vector, Vector> HostEntry, CancellationToken Token, IReadOnlyList<Vector> Exits = null);
/// <param name="parts">The part ordinals to order; prerequisites outside this set count as done.</param>
/// <param name="start">The modeled tool position before the first of these parts.</param>
@@ -63,13 +65,14 @@ internal static class CuttingNestedOrder
int[] rootOrder;
try
{
rootOrder = CuttingPartOrder.Plan(roots, context.Centers, start, hostsLast, context.Token, endpoint);
rootOrder = CuttingPartOrder.Plan(roots, context.Centers, start, hostsLast, context.Token, endpoint, context.Exits);
}
catch (InvalidOperationException)
{
try
{
rootOrder = CuttingPartOrder.Plan(roots, context.Centers, start, groupRules, context.Token, endpoint);
rootOrder = CuttingPartOrder.Plan(roots, context.Centers, start, groupRules, context.Token, endpoint,
context.Exits);
}
catch (InvalidOperationException)
{
@@ -84,7 +87,7 @@ internal static class CuttingNestedOrder
if (inner.Length != 0)
order.AddRange(Plan(inner, position, context, context.HostEntry(root, position)));
order.Add(root);
position = context.Centers[root];
position = (context.Exits ?? context.Centers)[root];
}
var placed = new HashSet<int>();
foreach (var part in order)
@@ -95,6 +98,7 @@ internal static class CuttingNestedOrder
}
return order.ToArray();
int[] Flat() => CuttingPartOrder.Plan(parts, context.Centers, start, context.Prerequisites, context.Token, endpoint);
int[] Flat() => CuttingPartOrder.Plan(parts, context.Centers, start, context.Prerequisites, context.Token, endpoint,
context.Exits);
}
}
@@ -10,7 +10,9 @@ namespace OpenNest.Engine.CuttingPlanning;
/// Proposes a whole-part visiting order as an open traveling-salesman path from the start point
/// over part centers: nearest neighbor, then 2-opt and Or-opt improvement, never placing a part
/// before one of its prerequisites. It is only a proposal; the forward search still checks every
/// rapid and lead along it.
/// rapid and lead along it. A part may be left from somewhere other than where it is reached (a
/// cut-off is entered at its start and left at its far end): each leg then runs from one part's
/// exit to the next part's center, and a reversed run is costed along its own directed legs.
/// </summary>
internal static class CuttingPartOrder
{
@@ -24,8 +26,11 @@ internal static class CuttingPartOrder
/// <param name="start">The modeled tool position before the first of these parts.</param>
/// <param name="prerequisites">Ordinals that must come before each part; must be acyclic.</param>
/// <param name="endpoint">Optional fixed final position after the last of these parts.</param>
/// <param name="exits">Optional point each part is left from, per part ordinal; null when every
/// part is left from its center.</param>
internal static int[] Plan(IReadOnlyList<int> parts, IReadOnlyList<Vector> centers, Vector start,
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, CancellationToken token, Vector? endpoint = null)
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, CancellationToken token, Vector? endpoint = null,
IReadOnlyList<Vector> exits = null)
{
var local = new Dictionary<int, int>();
for (var i = 0; i < parts.Count; i++)
@@ -34,7 +39,8 @@ internal static class CuttingPartOrder
var localPrerequisites = parts
.Select(p => (IReadOnlyCollection<int>)prerequisites[p].Where(local.ContainsKey).Select(q => local[q]).ToArray())
.ToArray();
return Plan(localCenters, start, localPrerequisites, token, endpoint).Select(i => parts[i]).ToArray();
var localExits = exits == null ? null : parts.Select(p => exits[p]).ToArray();
return Plan(localCenters, start, localPrerequisites, token, endpoint, localExits).Select(i => parts[i]).ToArray();
}
/// <param name="centers">One representative point per part ordinal.</param>
@@ -45,24 +51,29 @@ internal static class CuttingPartOrder
/// terminal edge). The terminal edge joins EVERY improvement delta, not just final
/// scoring. Null keeps the previous whole-part open-route semantics exactly.
/// </param>
/// <param name="exits">
/// Optional point each part is left from; null (every part left from its center) keeps the
/// symmetric route exactly.
/// </param>
internal static int[] Plan(IReadOnlyList<Vector> centers, Vector start,
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, CancellationToken token, Vector? endpoint = null)
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, CancellationToken token, Vector? endpoint = null,
IReadOnlyList<Vector> exits = null)
{
var count = centers.Count;
var order = NearestNeighbor(centers, start, prerequisites, token);
var order = NearestNeighbor(centers, exits ?? centers, start, prerequisites, token);
var position = new int[count];
for (var pass = 0; pass < MaxPasses; pass++)
{
token.ThrowIfCancellationRequested();
var improved = TwoOpt(order, centers, start, prerequisites, position, token, endpoint);
improved |= OrOpt(order, centers, start, prerequisites, position, token, endpoint);
var improved = TwoOpt(order, centers, exits, start, prerequisites, position, token, endpoint);
improved |= OrOpt(order, centers, exits ?? centers, start, prerequisites, position, token, endpoint);
if (!improved)
break;
}
return order;
}
private static int[] NearestNeighbor(IReadOnlyList<Vector> centers, Vector start,
private static int[] NearestNeighbor(IReadOnlyList<Vector> centers, IReadOnlyList<Vector> exits, Vector start,
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, CancellationToken token)
{
var count = centers.Count;
@@ -89,39 +100,56 @@ internal static class CuttingPartOrder
throw new InvalidOperationException("Part prerequisites form a cycle.");
placed[best] = true;
order[step] = best;
current = centers[best];
current = exits[best];
}
return order;
}
// Reverses order[i..j] when that shortens the open path and keeps every prerequisite earlier.
private static bool TwoOpt(int[] order, IReadOnlyList<Vector> centers, Vector start,
// With exits, a leg runs from one part's exit to the next part's center, so the reversed run's
// own legs change length too; without them they cancel and are left out, exactly as before.
private static bool TwoOpt(int[] order, IReadOnlyList<Vector> centers, IReadOnlyList<Vector> exits, Vector start,
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, int[] position, CancellationToken token,
Vector? endpoint = null)
{
var improved = false;
var count = order.Length;
var leave = exits ?? centers;
// The terminal edge belongs to every delta: reversing the route's tail swaps which
// endpoint-side center faces the fixed final position.
double Tail(Vector from) => endpoint is { } e ? from.DistanceTo(e) : 0;
for (var i = 0; i < count - 1; i++)
{
token.ThrowIfCancellationRequested();
// The run's internal legs, forward and reversed, grown one part at a time.
double forward = 0, reversed = 0;
for (var j = i + 1; j < count; j++)
{
var before = Point(i - 1).DistanceTo(centers[order[i]])
+ (j + 1 < count ? centers[order[j]].DistanceTo(centers[order[j + 1]]) : Tail(centers[order[j]]));
var after = Point(i - 1).DistanceTo(centers[order[j]])
+ (j + 1 < count ? centers[order[i]].DistanceTo(centers[order[j + 1]]) : Tail(centers[order[i]]));
if (exits != null)
{
forward += leave[order[j - 1]].DistanceTo(centers[order[j]]);
reversed += leave[order[j]].DistanceTo(centers[order[j - 1]]);
}
var before = Point(i - 1).DistanceTo(centers[order[i]]) + forward
+ (j + 1 < count ? leave[order[j]].DistanceTo(centers[order[j + 1]]) : Tail(leave[order[j]]));
var after = Point(i - 1).DistanceTo(centers[order[j]]) + reversed
+ (j + 1 < count ? leave[order[i]].DistanceTo(centers[order[j + 1]]) : Tail(leave[order[i]]));
if (after >= before - Epsilon || !CanReverse(order, i, j, prerequisites, position))
continue;
Array.Reverse(order, i, j - i + 1);
improved = true;
// The run is reversed now; its legs are re-measured from scratch.
forward = reversed = 0;
for (var k = i + 1; k <= j && exits != null; k++)
{
forward += leave[order[k - 1]].DistanceTo(centers[order[k]]);
reversed += leave[order[k]].DistanceTo(centers[order[k - 1]]);
}
}
}
return improved;
Vector Point(int index) => index < 0 ? start : centers[order[index]];
Vector Point(int index) => index < 0 ? start : leave[order[index]];
}
// A reversal breaks a prerequisite only when both parts lie inside the reversed span.
@@ -138,7 +166,7 @@ internal static class CuttingPartOrder
}
// Moves a run of one to three parts to a later or earlier gap when that shortens the path.
private static bool OrOpt(int[] order, IReadOnlyList<Vector> centers, Vector start,
private static bool OrOpt(int[] order, IReadOnlyList<Vector> centers, IReadOnlyList<Vector> exits, Vector start,
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, int[] position, CancellationToken token,
Vector? endpoint = null)
{
@@ -172,7 +200,7 @@ internal static class CuttingPartOrder
// end — the fixed endpoint when one was supplied, so the terminal edge is in every delta).
double Gap(int a, int b)
{
var from = a < 0 ? start : centers[order[a]];
var from = a < 0 ? start : exits[order[a]];
if (b >= count)
return endpoint is { } e ? from.DistanceTo(e) : 0;
if (b < 0)
@@ -199,6 +199,7 @@ internal static class JointCuttingPlanSearch
private static CuttingNestedOrder.Context NestedOrder(CuttingPlanSnapshot snapshot, Vector[] centers,
HashSet<int>[] prerequisites, CancellationToken token)
{
var exits = snapshot.Placements.Any(p => p.IsCutOff) ? snapshot.Placements.Select(Exit).ToArray() : null;
var inside = snapshot.Placements.Select((placement, host) => placement.IsCutOff ? []
: snapshot.Dependencies.PrerequisitesOf(host).Where(p => !snapshot.Placements[p].IsCutOff).ToArray()).ToArray();
var contours = new Dictionary<int, Entity[]>();
@@ -210,7 +211,7 @@ internal static class JointCuttingPlanSearch
.Select(m => m.Curve.ToEntity()).ToArray();
return entities.Length == 0 ? centers[host]
: entities.Select(e => e.ClosestPointTo(position)).MinBy(point => point.DistanceTo(position));
}, token);
}, token, exits);
}
// When the approach crossed the part cut just before the one it left from, the tool went on
@@ -268,9 +269,17 @@ internal static class JointCuttingPlanSearch
return false;
}
// The center of the part's placed cut material, for ordering only.
// Where the tool reaches a part, for ordering only: the center of its placed cut material,
// or a cut-off's start. A cut-off runs from a fixed edge, so it is entered at its start and
// left at its far end (Exit); its middle is neither.
private static Vector Center(FixedProgramPlacement placement) =>
CutExtent(placement)?.Center ?? placement.Execution.DeparturePoint;
placement.IsCutOff && placement.Execution.Motions.FirstOrDefault(m => !m.Rapid) is { Start: { } start }
? start
: CutExtent(placement)?.Center ?? placement.Execution.DeparturePoint;
// Where the tool leaves a part, for ordering only: a cut-off's far end, otherwise its center.
private static Vector Exit(FixedProgramPlacement placement) =>
placement.IsCutOff ? placement.Execution.DeparturePoint : Center(placement);
// The extent of the part's placed cut material, or null when it has none.
private static Box CutExtent(FixedProgramPlacement placement)
@@ -444,7 +453,7 @@ internal static class JointCuttingPlanSearch
for (var i = node.Order.Length + 1; i < sequence.Length; i++)
if (!finished.Contains(sequence[i]) && !IsSegment(snapshot.Placements[sequence[i]]))
return snapshot.Placements[sequence[i]];
var from = Center(source);
var from = Exit(source);
FixedProgramPlacement best = null;
var bestDistance = double.PositiveInfinity;
foreach (var candidate in snapshot.Placements