fix(simulator): take each part's perimeter as the posts do

The cut timeline called a contour exterior only when it closed and ran
clockwise, while the posts (ContourWinding.PerimeterIndex, L4/L2 on the
CI Fiber) take each part's contour enclosing the largest bounding box,
closed or not. A perimeter left open at a tab was therefore timed at the
hole feed, and the heat map dropped the whole part as a cutout slug with
its heat: on N26-2ED every washer showed no heat at all. The timeline now
uses the posts' rule, so tabbed or counterclockwise perimeters take the
outside feed and stay in the heat map as cut-free parts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-12-01 10:15:45 -05:00
1 parent 41e8e9496f
commit c96666b59c
7 files changed
+156 -51

No files matched your search

+56 -19
View File
@@ -12,8 +12,6 @@ namespace OpenNest.CNC
/// </summary>
internal static class ContourTiming
{
private const double ClosedTolerance = 1e-4;
public static List<CutSimulationStep> Finish(List<CutSimulationStep> steps, Plate plate, CutParameters parameters)
{
NumberContours(steps);
@@ -145,27 +143,45 @@ namespace OpenNest.CNC
};
/// <summary>
/// Matches the CI Fiber post's interior/exterior choice (L2/L4): a cut-off, or a closed
/// contour wound clockwise, is exterior; everything else is a hole. Lead moves are
/// left out of the winding, as the post samples from the lead-in's end.
/// Matches the posts' perimeter choice (<see cref="Posting.ContourWinding.PerimeterIndex"/>,
/// which sets L4 or L2 on the CI Fiber): in each part, the contour enclosing the largest
/// bounding box is the outside, whichever way it is wound and even when it stops short of
/// its start (a tab). Every other contour is a hole, open pieces included; a cut-off's
/// lines are outsides. Lead moves are left out, as the post samples from the lead-in's end.
/// </summary>
private static void ClassifyHoles(List<CutSimulationStep> steps, Plate plate)
{
foreach (var contour in steps.Where(s => s.ContourIndex >= 0).GroupBy(s => s.ContourIndex))
{
var moves = contour.Where(s => s.Kind == CutSimulationStepKind.Cut).ToList();
if (moves.Count == 0)
continue;
var contours = steps
.Where(s => s.ContourIndex >= 0)
.GroupBy(s => s.ContourIndex)
.Select(g => (Index: g.Key, Steps: g.ToList(), Moves: g.Where(s => s.Kind == CutSimulationStepKind.Cut).ToList()))
.Where(c => c.Moves.Count > 0)
.ToList();
var partIndex = moves[0].PartIndex;
var perimeters = new Dictionary<int, (int Contour, double Area)>();
foreach (var contour in contours)
{
var points = SamplePoints(contour.Moves);
if (!EnclosesArea(points))
continue;
var area = BoundingBoxArea(points);
var part = contour.Moves[0].PartIndex;
if (!perimeters.TryGetValue(part, out var best) || area > best.Area)
perimeters[part] = (contour.Index, area);
}
foreach (var contour in contours)
{
var partIndex = contour.Moves[0].PartIndex;
var isCutOff = partIndex < plate.Parts.Count && plate.Parts[partIndex].BaseDrawing?.IsCutOff == true;
var isHole = !isCutOff && !IsClosedClockwise(moves);
foreach (var step in contour)
var isPerimeter = perimeters.TryGetValue(partIndex, out var perimeter) && perimeter.Contour == contour.Index;
var isHole = !isCutOff && !isPerimeter;
foreach (var step in contour.Steps)
step.IsHole = isHole && step.Kind != CutSimulationStepKind.Scribe;
}
}
private static bool IsClosedClockwise(List<CutSimulationStep> moves)
private static List<Vector> SamplePoints(List<CutSimulationStep> moves)
{
var points = new List<Vector>();
foreach (var move in moves.Where(m => m.Layer is not (LayerType.Leadin or LayerType.Leadout)))
@@ -180,14 +196,35 @@ namespace OpenNest.CNC
}
points.Add(move.End);
}
return points;
}
if (points.Count < 3 || points[0].DistanceTo(points[^1]) > ClosedTolerance)
// The shoelace closes the path back to its start, as the post's does.
private static bool EnclosesArea(List<Vector> points)
{
if (points.Count < 3)
return false;
var area = 0.0;
for (var i = 0; i < points.Count - 1; i++)
area += points[i].X * points[i + 1].Y - points[i + 1].X * points[i].Y;
return area < 0;
for (var i = 0; i < points.Count; i++)
{
var a = points[i];
var b = points[(i + 1) % points.Count];
area += a.X * b.Y - b.X * a.Y;
}
return System.Math.Abs(area / 2) > 1e-9;
}
private static double BoundingBoxArea(List<Vector> points)
{
double minX = double.MaxValue, minY = double.MaxValue, maxX = double.MinValue, maxY = double.MinValue;
foreach (var p in points)
{
minX = System.Math.Min(minX, p.X);
minY = System.Math.Min(minY, p.Y);
maxX = System.Math.Max(maxX, p.X);
maxY = System.Math.Max(maxY, p.Y);
}
return (maxX - minX) * (maxY - minY);
}
}
}
+2 -1
View File
@@ -47,7 +47,8 @@ namespace OpenNest.CNC
/// <summary>
/// True when the move is part of an interior contour, timed at the hole feed. Follows the
/// CI Fiber post's rule: only a closed clockwise contour (or a cut-off) is exterior.
/// posts' rule: each part's perimeter (the contour enclosing the largest bounding box,
/// closed or not) and a cut-off are exterior.
/// </summary>
public bool IsHole { get; internal set; }
@@ -58,8 +58,9 @@ public sealed class CutTimeReportTests : IDisposable
report.CutTime);
}
// Full speed: each square's 40 in of hole contour at 60 in/min is 40 s, plus a 1 s pierce;
// two squares, two sheets, and the rapid between them at 600 in/min.
// Full speed: each square is its part's perimeter, so its 40 in run at the 120 in/min outside
// feed, 20 s, plus a 1 s pierce; two squares, two sheets, and the rapid between them at 600
// in/min.
[Fact]
public void ForPlate_FullSpeed_TheHandWorkedTime()
{
@@ -67,9 +68,9 @@ public sealed class CutTimeReportTests : IDisposable
var report = CutTimeReport.ForPlate(nest.Plates[0], Entered(), Units.Inches);
var rapid = TimeSpan.FromMinutes(20.0 / 600);
var sheet = 2 * (TimeSpan.FromSeconds(40) + TimeSpan.FromSeconds(1)) + rapid;
var sheet = 2 * (TimeSpan.FromSeconds(20) + TimeSpan.FromSeconds(1)) + rapid;
Assert.Equal(sheet.TotalSeconds * 2, report.CutTime.TotalSeconds, 6);
Assert.Equal("02m 48s", report.CutTimeText);
Assert.Equal("01m 28s", report.CutTimeText);
}
[Fact]
+34
View File
@@ -292,6 +292,40 @@ public class CutSimulationTests
Assert.Equal(TimeSpan.FromMinutes(40.0 / 100).TotalSeconds, cuts.Skip(4).Sum(s => s.Duration.TotalSeconds), 6);
}
[Theory]
[InlineData(0.03, false)] // stops short of its start, as a tab leaves it
[InlineData(0, true)] // wound counterclockwise
public void ThePerimeterIsTheOutsideClosedOrNotWhicheverWayItRuns(double gap, bool counterclockwise)
{
// The posts take each part's largest contour as its perimeter (L4 on the CI Fiber).
var program = new Program();
program.Codes.Add(new RapidMove(4, 5));
program.Codes.Add(new LinearMove(6, 5));
program.Codes.Add(new LinearMove(6, 7));
program.Codes.Add(new LinearMove(4, 7));
program.Codes.Add(new LinearMove(4, 5));
program.Codes.Add(new RapidMove(0, 0));
if (counterclockwise)
{
program.Codes.Add(new LinearMove(10, 0));
program.Codes.Add(new LinearMove(10, 10));
program.Codes.Add(new LinearMove(0, 10));
}
else
{
program.Codes.Add(new LinearMove(0, 10));
program.Codes.Add(new LinearMove(10, 10));
program.Codes.Add(new LinearMove(10, 0));
}
program.Codes.Add(new LinearMove(gap, 0));
var sim = CutSimulation.Build(PlateWith((program, Vector.Zero)), Parameters);
var cuts = sim.Steps.Where(s => s.Kind == CutSimulationStepKind.Cut).ToList();
Assert.All(cuts.Take(4), s => Assert.True(s.IsHole));
Assert.All(cuts.Skip(4), s => Assert.False(s.IsHole));
}
[Fact]
public void SkippedEtchContoursLeaveNoTimeAndRejoinTravel()
{
+48 -18
View File
@@ -35,27 +35,38 @@ public class PlateHeatSimulationTests
return p.Material.Density * p.Material.SpecificHeat * p.Thickness * 0.0254 * dx * dx;
}
// Counterclockwise: a cutout under the CI Fiber rule. Clockwise: a part's outside.
private static Program Square(double size, bool clockwise)
// A part with one contour, its perimeter.
private static Program Square(double size)
{
var program = new Program();
program.Codes.Add(new RapidMove(0, 0));
if (clockwise)
{
program.Codes.Add(new LinearMove(0, size));
program.Codes.Add(new LinearMove(size, size));
program.Codes.Add(new LinearMove(size, 0));
}
else
{
program.Codes.Add(new LinearMove(size, 0));
program.Codes.Add(new LinearMove(size, size));
program.Codes.Add(new LinearMove(0, size));
}
program.Codes.Add(new LinearMove(0, size));
program.Codes.Add(new LinearMove(size, size));
program.Codes.Add(new LinearMove(size, 0));
program.Codes.Add(new LinearMove(0, 0));
return program;
}
// A square part with a square cutout in its middle: the hole counterclockwise, then the
// perimeter clockwise. A gap leaves the perimeter that far short of its start, as a tab does.
private static Program Frame(double size, double hole, double gap = 0)
{
var a = (size - hole) / 2;
var b = a + hole;
var program = new Program();
program.Codes.Add(new RapidMove(a, a));
program.Codes.Add(new LinearMove(b, a));
program.Codes.Add(new LinearMove(b, b));
program.Codes.Add(new LinearMove(a, b));
program.Codes.Add(new LinearMove(a, a));
program.Codes.Add(new RapidMove(0, 0));
program.Codes.Add(new LinearMove(0, size));
program.Codes.Add(new LinearMove(size, size));
program.Codes.Add(new LinearMove(size, 0));
program.Codes.Add(new LinearMove(gap, 0));
return program;
}
private static Program Line(double length)
{
var program = new Program();
@@ -134,7 +145,8 @@ public class PlateHeatSimulationTests
[Fact]
public void CutoutSlugFallsAwayWithItsHeat()
{
var (plate, simulation) = Cut(Square(6, clockwise: false), new Vector(10, 10));
// A 10 in part at (8, 8) with a 6 in hole from 10 to 16.
var (plate, simulation) = Cut(Frame(10, 6), new Vector(8, 8));
var heat = new PlateHeatSimulation(plate, simulation, Thermal());
heat.RunToEnd();
@@ -142,13 +154,31 @@ public class PlateHeatSimulationTests
var inside = heat.CellAt(new Vector(13, 13));
Assert.True(heat.IsRemoved(inside));
Assert.Equal(0f, heat.Temperatures[inside]);
Assert.False(heat.IsRemoved(heat.CellAt(new Vector(9.25, 13))));
Assert.False(heat.IsRemoved(heat.CellAt(new Vector(5, 5))));
}
[Theory]
[InlineData(0)]
[InlineData(0.03)] // a tab leaves the perimeter open
public void PartAroundACutoutKeepsItsHeat(double gap)
{
var (plate, simulation) = Cut(Frame(10, 6, gap), new Vector(8, 8));
var heat = new PlateHeatSimulation(plate, simulation, Thermal());
heat.RunToEnd();
// The ring between the hole and the perimeter is the part, not a slug.
var ring = heat.CellAt(new Vector(9.25, 13.25));
Assert.False(heat.IsRemoved(ring));
Assert.True(heat.Temperatures[ring] > 0);
Assert.True(heat.IsRemoved(heat.CellAt(new Vector(13, 13))));
}
[Fact]
public void CutFreePartKeepsItsHeatAcrossTheKerf()
{
var (plate, simulation) = Cut(Square(6, clockwise: true), new Vector(10, 10));
var (plate, simulation) = Cut(Square(6), new Vector(10, 10));
var heat = new PlateHeatSimulation(plate, simulation, Thermal());
heat.RunToEnd();
@@ -175,7 +205,7 @@ public class PlateHeatSimulationTests
[Fact]
public void GoingBackInTimeReplaysTheSameState()
{
var (plate, simulation) = Cut(Square(6, clockwise: false), new Vector(10, 10));
var (plate, simulation) = Cut(Frame(10, 6), new Vector(8, 8));
var heat = new PlateHeatSimulation(plate, simulation, Thermal(20));
var middle = TimeSpan.FromTicks(simulation.TotalTime.Ticks / 2);
@@ -215,7 +245,7 @@ public class PlateHeatSimulationTests
[Fact]
public void WithoutABedTheModelIsUnchanged()
{
var (plate, simulation) = Cut(Square(6, clockwise: true), new Vector(10, 10));
var (plate, simulation) = Cut(Square(6), new Vector(10, 10));
var before = new PlateHeatSimulation(plate, simulation, Thermal(20));
before.RunToEnd(TimeSpan.FromSeconds(60));
+6 -4
View File
@@ -26,10 +26,12 @@ both desktop apps read and write in the user settings. Both apps word the timing
strategies emit contour moves on Display, and the posts cut them. Rapids and Tab spans are
head-up travel, and suppressed moves are left out. A cut that follows travel gets a pierce
dwell. Scribes are not pierced.
- Contours follow the CI Fiber post's interior/exterior (`L2`/`L4`) rule: a cut-off, or a
closed contour wound clockwise (sampled without its lead moves), takes the outside feed.
Everything else, including open pieces, takes the hole feed. The CI Fiber post writes no feeds,
so these are stand-ins for the controller's process tables until calibrated.
- Contours follow the posts' perimeter rule (`ContourWinding.PerimeterIndex`, the CI Fiber's
`L4`/`L2`): a cut-off, or each part's contour enclosing the largest bounding box (sampled
without its lead moves), takes the outside feed, whichever way it is wound and even when it
stops short of its start at a tab. Everything else, including open pieces, takes the hole feed.
The CI Fiber post writes no feeds, so these are stand-ins for the controller's process tables
until calibrated.
- Each cutting run (between a pierce and the next travel) starts and ends at rest. Travel moves
are planned separately and also go from rest to rest.
- Each move follows a trapezoidal profile: it accelerates, cruises at the feed and decelerates. A
+5 -5
View File
@@ -28,11 +28,11 @@ a parked [heat-aware sequencer](#sequencing) uses a cheaper estimate of the same
(a few hundred W/m²·K when the slat is cold), and the slats warm up under a long cut. The slats
are not given a temperature of their own; the default is a lower effective value standing in
for that. An estimate to calibrate, like `AbsorbedPower`.
- Pieces: when a closed contour finishes, a cutout (an interior contour by the CI Fiber rule the
timeline already uses) removes its slug's cells and their heat; a part's outside contour
separates its cells from the skeleton, so no heat crosses the kerf. Only skeleton cells change,
so a part nested in a cutout stays its own piece. Open contours (cut-offs, breaks) change
nothing.
- Pieces: when a closed contour finishes (within half a cell, so a tab's gap counts as closed), a
cutout (an interior contour by the posts' perimeter rule the timeline already uses) removes its
slug's cells and their heat; a part's perimeter separates its cells from the skeleton, so no
heat crosses the kerf. Only skeleton cells change, so a part nested in a cutout stays its own
piece. Open contours (cut-offs, breaks) change nothing.
- Material from the nest's material name and grade (`ThermalMaterial.ForName`: stainless,
aluminum, otherwise mild steel); thickness from the nest.