diff --git a/OpenNest.Core/CNC/CuttingPlanning/ContourFindings.cs b/OpenNest.Core/CNC/CuttingPlanning/ContourFindings.cs new file mode 100644 index 00000000..a681dba5 --- /dev/null +++ b/OpenNest.Core/CNC/CuttingPlanning/ContourFindings.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using OpenNest.Diagnostics; + +namespace OpenNest.CNC.CuttingPlanning; + +/// +/// Where 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. +/// +internal sealed class ContourFindings +{ + /// Findings against the motions planning chose, or every finding when + /// is null. + /// Findings whose offending motion a fixed emission owns; null + /// reports them as strict. + /// Crossings made by a rapid with the head raised; null drops them. + /// 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. + internal ContourFindings(List strict, List fixedFindings = null, + List raised = null, List warnings = null) + { + Strict = strict; + Fixed = fixedFindings; + Raised = raised; + Warnings = warnings; + } + + internal List Strict { get; } + + internal List Fixed { get; } + + internal List Raised { get; } + + internal List Warnings { get; } + + /// The list a finding goes to, by whether a fixed emission owns its motion. + internal List TargetFor(bool fixedEmission) => + fixedEmission && Fixed != null ? Fixed : Strict; + + internal void Add(PostVerificationFinding finding, bool fixedEmission) => TargetFor(fixedEmission).Add(finding); +} diff --git a/OpenNest.Core/CNC/CuttingPlanning/ReleasedContourState.cs b/OpenNest.Core/CNC/CuttingPlanning/ReleasedContourState.cs index 21dcf141..77ddd7e9 100644 --- a/OpenNest.Core/CNC/CuttingPlanning/ReleasedContourState.cs +++ b/OpenNest.Core/CNC/CuttingPlanning/ReleasedContourState.cs @@ -102,7 +102,7 @@ public sealed class ReleasedContourState var moves = execution.Motions.ToArray(); moves[0] = moves[0].WithStart(arrival); var findings = new List(); - 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(); var raisedFindings = new List(); var warnings = new List(); - 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(); - 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 moves, bool cutoff, - List findings, int plate, int part, CancellationToken token, - Func isFixed = null, List fixedFindings = null, - int contoursBefore = 0, List raisedFindings = null, - List 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 moves, bool cutoff, ContourFindings findings, + int plate, int part, CancellationToken token, Func isFixed = null, int contoursBefore = 0) { var current = new CutGroup(); var unfinished = new List(); @@ -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(); + } + } + + /// + /// Reports each completed contour the rapid from crosses or touches, + /// in obstacle order: as raised when the head is up, otherwise by whether the rapid is owned. + /// + 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); + } + } + + /// + /// 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 ). + /// + private static bool OpenCutsRetained(List closed, List unfinished, List 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); + + /// + /// 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. + /// + private void AddOpenCutObstacle(List 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(); - } } /// diff --git a/OpenNest.Core/Diagnostics/PostVerificationAnalyzer.cs b/OpenNest.Core/Diagnostics/PostVerificationAnalyzer.cs index 2217c732..e1d50fa0 100644 --- a/OpenNest.Core/Diagnostics/PostVerificationAnalyzer.cs +++ b/OpenNest.Core/Diagnostics/PostVerificationAnalyzer.cs @@ -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) {