mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
refactor(planning): group AnalyzeMoves findings in a sink
ReleasedContourState.AnalyzeMoves took its strict, fixed, raised and warning finding lists as four parameters. They now travel in one ContourFindings sink, which also routes a finding by fixed ownership. The rapid-crossing loop, the open-cut retention proof and the part-end open-cut obstacle move to private methods. Finding order, contents and cancellation checks are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
aeef9b58cd
commit
42036a321a
3 files changed
+158
-97
No files matched your search
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.Diagnostics;
|
||||
|
||||
namespace OpenNest.CNC.CuttingPlanning;
|
||||
|
||||
/// <summary>
|
||||
/// Where <see cref="ReleasedContourState"/> reports one program's findings: strict findings, and,
|
||||
/// when given, those it returns apart. Each list is shared with the caller and filled in order.
|
||||
/// </summary>
|
||||
internal sealed class ContourFindings
|
||||
{
|
||||
/// <param name="strict">Findings against the motions planning chose, or every finding when
|
||||
/// <paramref name="fixedFindings"/> is null.</param>
|
||||
/// <param name="fixedFindings">Findings whose offending motion a fixed emission owns; null
|
||||
/// reports them as strict.</param>
|
||||
/// <param name="raised">Crossings made by a rapid with the head raised; null drops them.</param>
|
||||
/// <param name="warnings">Where a configured lead-out that may cut its tabbed outside free is
|
||||
/// reported; null leaves it an incomplete check, as for programs judged as they are.</param>
|
||||
internal ContourFindings(List<PostVerificationFinding> strict, List<PostVerificationFinding> fixedFindings = null,
|
||||
List<PostVerificationFinding> raised = null, List<PostVerificationFinding> warnings = null)
|
||||
{
|
||||
Strict = strict;
|
||||
Fixed = fixedFindings;
|
||||
Raised = raised;
|
||||
Warnings = warnings;
|
||||
}
|
||||
|
||||
internal List<PostVerificationFinding> Strict { get; }
|
||||
|
||||
internal List<PostVerificationFinding> Fixed { get; }
|
||||
|
||||
internal List<PostVerificationFinding> Raised { get; }
|
||||
|
||||
internal List<PostVerificationFinding> Warnings { get; }
|
||||
|
||||
/// <summary>The list a finding goes to, by whether a fixed emission owns its motion.</summary>
|
||||
internal List<PostVerificationFinding> TargetFor(bool fixedEmission) =>
|
||||
fixedEmission && Fixed != null ? Fixed : Strict;
|
||||
|
||||
internal void Add(PostVerificationFinding finding, bool fixedEmission) => TargetFor(fixedEmission).Add(finding);
|
||||
}
|
||||
@@ -102,7 +102,7 @@ public sealed class ReleasedContourState
|
||||
var moves = execution.Motions.ToArray();
|
||||
moves[0] = moves[0].WithStart(arrival);
|
||||
var findings = new List<PostVerificationFinding>();
|
||||
AnalyzeMoves(moves, cutoff, findings, 1, partNumber, token, fixedFindings: new(), raisedFindings: new());
|
||||
AnalyzeMoves(moves, cutoff, new(findings, fixedFindings: new(), raised: new()), 1, partNumber, token);
|
||||
return findings.AsReadOnly();
|
||||
}
|
||||
|
||||
@@ -131,8 +131,8 @@ public sealed class ReleasedContourState
|
||||
var fixedFindings = new List<PostVerificationFinding>();
|
||||
var raisedFindings = new List<PostVerificationFinding>();
|
||||
var warnings = new List<PostVerificationFinding>();
|
||||
AnalyzeMoves(moves, cutoff, findings, 1, partNumber, token, isFixed, fixedFindings,
|
||||
raisedFindings: raisedFindings, warnings: settingsLeadOuts ? warnings : null);
|
||||
AnalyzeMoves(moves, cutoff, new(findings, fixedFindings, raisedFindings, settingsLeadOuts ? warnings : null),
|
||||
1, partNumber, token, isFixed);
|
||||
return (findings.AsReadOnly(), fixedFindings.AsReadOnly(), raisedFindings.AsReadOnly(), warnings.AsReadOnly());
|
||||
}
|
||||
|
||||
@@ -155,17 +155,14 @@ public sealed class ReleasedContourState
|
||||
var moves = execution.Motions.ToArray();
|
||||
moves[0] = moves[0].WithStart(arrival);
|
||||
var findings = new List<PostVerificationFinding>();
|
||||
AnalyzeMoves(moves, false, findings, 1, partNumber, token, contoursBefore: partContours);
|
||||
AnalyzeMoves(moves, false, new(findings), 1, partNumber, token, contoursBefore: partContours);
|
||||
return partClosed ? findings.AsReadOnly() : null;
|
||||
}
|
||||
|
||||
// warnings: where a configured lead-out that may cut its tabbed outside free is reported (see
|
||||
// Check); without it, as for programs judged as they are, it stays an incomplete check.
|
||||
internal void AnalyzeMoves(IReadOnlyList<ExecutionMotion> moves, bool cutoff,
|
||||
List<PostVerificationFinding> findings, int plate, int part, CancellationToken token,
|
||||
Func<int, bool> isFixed = null, List<PostVerificationFinding> fixedFindings = null,
|
||||
int contoursBefore = 0, List<PostVerificationFinding> raisedFindings = null,
|
||||
List<PostVerificationFinding> warnings = null)
|
||||
// findings.Warnings: where a configured lead-out that may cut its tabbed outside free is reported
|
||||
// (see Check); without it, as for programs judged as they are, it stays an incomplete check.
|
||||
internal void AnalyzeMoves(IReadOnlyList<ExecutionMotion> moves, bool cutoff, ContourFindings findings,
|
||||
int plate, int part, CancellationToken token, Func<int, bool> isFixed = null, int contoursBefore = 0)
|
||||
{
|
||||
var current = new CutGroup();
|
||||
var unfinished = new List<CutGroup>();
|
||||
@@ -240,48 +237,7 @@ public sealed class ReleasedContourState
|
||||
if (move.HeadRaised && leaving != null && start.DistanceTo(exitPoint) <= PostVerificationGeometry.Epsilon
|
||||
&& LeavesOnce(leaving, start, move.End, token))
|
||||
continue;
|
||||
var reach = new PostVerificationGeometry.Extent(System.Math.Min(start.X, move.End.X),
|
||||
System.Math.Min(start.Y, move.End.Y), System.Math.Max(start.X, move.End.X),
|
||||
System.Math.Max(start.Y, move.End.Y));
|
||||
foreach (var obstacle in obstacles)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
// A rapid well clear of a contour's extent can neither cross nor touch it.
|
||||
if (reach.IsClearOf(obstacle.Extent))
|
||||
continue;
|
||||
// A tabbed part is avoided only while planning still looks for a way around it.
|
||||
if (obstacle.Tabbed && AvoidTabbedPart?.Invoke(obstacle.Part) != true)
|
||||
continue;
|
||||
// A rapid leaving an overcut departs from its retraced tail, which lies on the first
|
||||
// cut only within the seam tolerance its arcs were matched at. Contact within that
|
||||
// much of the departure is the departure itself.
|
||||
var from = start;
|
||||
if (obstacle.OvercutEnd is { } tailEnd && tailEnd.DistanceTo(start) <= PostVerificationGeometry.Epsilon
|
||||
&& start.DistanceTo(move.End) > PostVerificationGeometry.RaySeamTolerance)
|
||||
from = start + (move.End - start) * (PostVerificationGeometry.RaySeamTolerance / start.DistanceTo(move.End));
|
||||
if (!PostVerificationGeometry.Crosses(from, move.End, obstacle.Curves, token))
|
||||
continue;
|
||||
var crossing = new PostVerificationFinding(PostVerificationKind.RapidCrossing, plate, part,
|
||||
obstacle.Part, (move.HeadRaised ? "Head-raised" : "Direct") + " XY rapid crosses or touches "
|
||||
+ (obstacle.TabMayNotHold
|
||||
? $"the tabbed outside of part {obstacle.Part}, which its tab may not hold level."
|
||||
: obstacle.TabCutFree
|
||||
? $"the tabbed outside of part {obstacle.Part}, whose lead-out may cut its tab free."
|
||||
: obstacle.Tabbed
|
||||
? TabbedOutside(obstacle.Part)
|
||||
: $"completed untabbed contour {obstacle.Contour} of part {obstacle.Part}."))
|
||||
{
|
||||
Location = (start + move.End) * 0.5,
|
||||
RapidStart = start,
|
||||
RapidEnd = move.End,
|
||||
ContactPoints = PostVerificationGeometry.ContactPoints(from, move.End, obstacle.Curves, token)
|
||||
};
|
||||
// The head clears a cut part on a raised rapid; the crossing is the raise's reason.
|
||||
if (move.HeadRaised)
|
||||
raisedFindings?.Add(crossing);
|
||||
else
|
||||
Add(crossing, owned);
|
||||
}
|
||||
CheckRapid(move, start, owned, findings, plate, part, token);
|
||||
}
|
||||
else if (move.Layer == LayerType.Leadin)
|
||||
{
|
||||
@@ -371,20 +327,17 @@ public sealed class ReleasedContourState
|
||||
// Slug slices (docs/support-tabs.md#slug-slices) cross one another inside one of the part's
|
||||
// own holes: what they enclose is that hole's slug, scrap, so they take no part in the proof.
|
||||
var retained = !cutoff && (unfinished.Count > 1 || deferred.Count > 0)
|
||||
&& (remainders || unfinished.All(group => LeadsClearOpenEnds(group, unfinished.Count == 1 && otherOpen.Count == 0))
|
||||
&& OpenCutsEncloseNothing(closed, unfinished
|
||||
.Concat(otherOpen.Where(group => !CutsOnlySlug(group, closed, unfinished, token)))
|
||||
.Append(loose).ToList(), token));
|
||||
&& (remainders || OpenCutsRetained(closed, unfinished, otherOpen, loose, token));
|
||||
// A tabbed outside whose configured lead-out the proof cannot clear: the lead-out may run
|
||||
// over the tab gap into the lead-in and cut the part free. The lead-out is kept as
|
||||
// configured and reported, and the part counts as cut free from here on.
|
||||
var cutFree = warnings != null && !retained && !cutoff && !member && unfinished.Count == 1
|
||||
var cutFree = findings.Warnings != null && !retained && !cutoff && !member && unfinished.Count == 1
|
||||
&& deferredLeadOuts.Count > 0 && !deferredLeadOuts.Any(lead => lead.Fixed) && !unfinished[0].Fixed
|
||||
&& IsOutside(unfinished[0], closed);
|
||||
if (cutFree)
|
||||
{
|
||||
deferred.Clear();
|
||||
warnings.Add(new(PostVerificationKind.TabLeadOut, plate, part, null,
|
||||
findings.Warnings.Add(new(PostVerificationKind.TabLeadOut, plate, part, null,
|
||||
"The lead-out of this tabbed outside runs past its tab gap toward the lead-in and may cut the "
|
||||
+ "tab free, so the part is treated as cut free. Shorten or turn the lead-out, or enlarge the tab, "
|
||||
+ "to keep the part in the sheet.")
|
||||
@@ -399,7 +352,110 @@ public sealed class ReleasedContourState
|
||||
"Multiple interrupted/open cutting fragments remain. Their combined cuts may release material; " +
|
||||
"they cannot be assumed to be retained by tabs. Review rapid travel manually."),
|
||||
unfinished.Any(chain => chain.Fixed));
|
||||
// A part that may tip once cut is avoided like one cut free. The retention proof above is
|
||||
AddOpenCutObstacle(unfinished, remainders, cutoff, cutFree, part, contourNumber);
|
||||
partContours = contourNumber;
|
||||
partClosed = !cutoff && isFixed == null && unfinished.Count == 0 && deferred.Count == 0 && !reportedTab
|
||||
&& !inBreakCut && loose.Leads.Count == 0;
|
||||
|
||||
void Add(PostVerificationFinding finding, bool fixedEmission) => findings.Add(finding, fixedEmission);
|
||||
|
||||
// Records the finding's place among the others, in case it is reported after all.
|
||||
void Defer(PostVerificationFinding finding, bool fixedEmission)
|
||||
{
|
||||
var target = findings.TargetFor(fixedEmission);
|
||||
deferred.Add((target, target.Count, finding));
|
||||
}
|
||||
|
||||
void Finish()
|
||||
{
|
||||
if (current.Curves.Count == 0)
|
||||
return;
|
||||
// A real uncut gap leaves the contour attached. CuttingParameters can be stale;
|
||||
// no flag or tab configuration is used as evidence of retention.
|
||||
if (!cutoff && (current.OvercutEnd.HasValue || PostVerificationGeometry.Closed(current.Curves)))
|
||||
{
|
||||
lastObstacle = new(part, contourNumber, current.Curves.ToArray(), current.OvercutEnd);
|
||||
obstacles.Add(lastObstacle);
|
||||
closed.Add(current);
|
||||
lastClosed = current;
|
||||
}
|
||||
else if (!cutoff)
|
||||
unfinished.Add(current);
|
||||
current = new CutGroup();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports each completed contour the rapid from <paramref name="start"/> crosses or touches,
|
||||
/// in obstacle order: as raised when the head is up, otherwise by whether the rapid is owned.
|
||||
/// </summary>
|
||||
private void CheckRapid(ExecutionMotion move, Vector start, bool owned, ContourFindings findings,
|
||||
int plate, int part, CancellationToken token)
|
||||
{
|
||||
var reach = new PostVerificationGeometry.Extent(System.Math.Min(start.X, move.End.X),
|
||||
System.Math.Min(start.Y, move.End.Y), System.Math.Max(start.X, move.End.X),
|
||||
System.Math.Max(start.Y, move.End.Y));
|
||||
foreach (var obstacle in obstacles)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
// A rapid well clear of a contour's extent can neither cross nor touch it.
|
||||
if (reach.IsClearOf(obstacle.Extent))
|
||||
continue;
|
||||
// A tabbed part is avoided only while planning still looks for a way around it.
|
||||
if (obstacle.Tabbed && AvoidTabbedPart?.Invoke(obstacle.Part) != true)
|
||||
continue;
|
||||
// A rapid leaving an overcut departs from its retraced tail, which lies on the first
|
||||
// cut only within the seam tolerance its arcs were matched at. Contact within that
|
||||
// much of the departure is the departure itself.
|
||||
var from = start;
|
||||
if (obstacle.OvercutEnd is { } tailEnd && tailEnd.DistanceTo(start) <= PostVerificationGeometry.Epsilon
|
||||
&& start.DistanceTo(move.End) > PostVerificationGeometry.RaySeamTolerance)
|
||||
from = start + (move.End - start) * (PostVerificationGeometry.RaySeamTolerance / start.DistanceTo(move.End));
|
||||
if (!PostVerificationGeometry.Crosses(from, move.End, obstacle.Curves, token))
|
||||
continue;
|
||||
var crossing = new PostVerificationFinding(PostVerificationKind.RapidCrossing, plate, part,
|
||||
obstacle.Part, (move.HeadRaised ? "Head-raised" : "Direct") + " XY rapid crosses or touches "
|
||||
+ (obstacle.TabMayNotHold
|
||||
? $"the tabbed outside of part {obstacle.Part}, which its tab may not hold level."
|
||||
: obstacle.TabCutFree
|
||||
? $"the tabbed outside of part {obstacle.Part}, whose lead-out may cut its tab free."
|
||||
: obstacle.Tabbed
|
||||
? TabbedOutside(obstacle.Part)
|
||||
: $"completed untabbed contour {obstacle.Contour} of part {obstacle.Part}."))
|
||||
{
|
||||
Location = (start + move.End) * 0.5,
|
||||
RapidStart = start,
|
||||
RapidEnd = move.End,
|
||||
ContactPoints = PostVerificationGeometry.ContactPoints(from, move.End, obstacle.Curves, token)
|
||||
};
|
||||
// The head clears a cut part on a raised rapid; the crossing is the raise's reason.
|
||||
if (move.HeadRaised)
|
||||
findings.Raised?.Add(crossing);
|
||||
else
|
||||
findings.Add(crossing, owned);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a part's open cuts, with its break cuts that free no more than a hole's slug and
|
||||
/// its loose leads, provably retain all their material (see <see cref="OpenCutsEncloseNothing"/>).
|
||||
/// </summary>
|
||||
private static bool OpenCutsRetained(List<CutGroup> closed, List<CutGroup> unfinished, List<CutGroup> otherOpen,
|
||||
CutGroup loose, CancellationToken token) =>
|
||||
unfinished.All(group => LeadsClearOpenEnds(group, unfinished.Count == 1 && otherOpen.Count == 0))
|
||||
&& OpenCutsEncloseNothing(closed, unfinished
|
||||
.Concat(otherOpen.Where(group => !CutsOnlySlug(group, closed, unfinished, token)))
|
||||
.Append(loose).ToList(), token);
|
||||
|
||||
/// <summary>
|
||||
/// When a part ends with open cuts, adds the obstacle they leave: a common-line member's
|
||||
/// remainder pieces joined into its outside, or the longest open cut closed across its tab gap
|
||||
/// when that part is cut free, may not be held level or is avoided as tabbed.
|
||||
/// </summary>
|
||||
private void AddOpenCutObstacle(List<CutGroup> unfinished, bool remainders, bool cutoff, bool cutFree,
|
||||
int part, int contourNumber)
|
||||
{
|
||||
// A part that may tip once cut is avoided like one cut free. The retention proof is
|
||||
// unchanged: the part is still attached by its tab, just not held level by it.
|
||||
if (remainders)
|
||||
{
|
||||
@@ -430,42 +486,6 @@ public sealed class ReleasedContourState
|
||||
Tabbed = !cutFree && !mayNotHold,
|
||||
});
|
||||
}
|
||||
partContours = contourNumber;
|
||||
partClosed = !cutoff && isFixed == null && unfinished.Count == 0 && deferred.Count == 0 && !reportedTab
|
||||
&& !inBreakCut && loose.Leads.Count == 0;
|
||||
|
||||
void Add(PostVerificationFinding finding, bool fixedEmission)
|
||||
{
|
||||
if (fixedEmission && fixedFindings != null)
|
||||
fixedFindings.Add(finding);
|
||||
else
|
||||
findings.Add(finding);
|
||||
}
|
||||
|
||||
// Records the finding's place among the others, in case it is reported after all.
|
||||
void Defer(PostVerificationFinding finding, bool fixedEmission)
|
||||
{
|
||||
var target = fixedEmission && fixedFindings != null ? fixedFindings : findings;
|
||||
deferred.Add((target, target.Count, finding));
|
||||
}
|
||||
|
||||
void Finish()
|
||||
{
|
||||
if (current.Curves.Count == 0)
|
||||
return;
|
||||
// A real uncut gap leaves the contour attached. CuttingParameters can be stale;
|
||||
// no flag or tab configuration is used as evidence of retention.
|
||||
if (!cutoff && (current.OvercutEnd.HasValue || PostVerificationGeometry.Closed(current.Curves)))
|
||||
{
|
||||
lastObstacle = new(part, contourNumber, current.Curves.ToArray(), current.OvercutEnd);
|
||||
obstacles.Add(lastObstacle);
|
||||
closed.Add(current);
|
||||
lastClosed = current;
|
||||
}
|
||||
else if (!cutoff)
|
||||
unfinished.Add(current);
|
||||
current = new CutGroup();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -154,7 +154,7 @@ public static class PostVerificationAnalyzer
|
||||
&& move.Layer is LayerType.Cut or LayerType.Display
|
||||
&& move.Curve.Length > PostVerificationGeometry.Epsilon))
|
||||
Incomplete("Placed program has no cutting contour motions for this drawing.");
|
||||
obstacles.AnalyzeMoves(moves, cutoff, findings, plateNumber, partNumber, cancellationToken);
|
||||
obstacles.AnalyzeMoves(moves, cutoff, new(findings), plateNumber, partNumber, cancellationToken);
|
||||
if (clamps is { Count: > 0 })
|
||||
foreach (var move in moves)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user