Files
OpenNest/OpenNest.Core/CNC/Thermal/HeatStepSchedule.cs
T
ajandClaude Fable 5.2 ff8f97f44a perf(thermal): take the heat map's short steps only around the torch
While the torch cuts, every cell of the plate took steps short enough to
follow it: 4 to 76 times more steps than diffusion needs. On the processor
only the cells within four cells of the torch's recent path take them now;
the rest of the plate takes one long step per slot. A full sheet at
quarter-inch cells runs in 0.23-0.45 s instead of 0.9-2.9 s, and a fast
feed no longer makes it slower.

Peaks stay within 1% of the color scale at the 99th percentile of the
whole-plate result. Graphics cards keep whole-plate steps. Decay factors
for the support bed are worked out per distinct contact fraction.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-02 20:21:08 -05:00

106 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
namespace OpenNest.CNC.Thermal
{
/// <summary>
/// The time steps of a <see cref="PlateHeatSimulation"/> run. Time is divided into slots as
/// long as the diffusion limit allows, and a slot in which the torch cuts is split into equal
/// steps short enough for that move's speed, so only the time that needs short steps pays for
/// them. The boundaries depend on the timeline alone, so a replay takes the same steps.
/// </summary>
internal sealed class HeatStepSchedule
{
// Slots are split only into these counts, so a run has few distinct step lengths and
// each one's decay factors can be kept.
private static readonly int[] Splits = { 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48, 64, 80, 96, 128, 160, 200 };
// Two times closer than this share of a slot are the same boundary.
private const double Tolerance = 1e-9;
private readonly int[] splits;
/// <param name="simulation">The timeline.</param>
/// <param name="slotSeconds">The longest step the diffusion allows.</param>
/// <param name="cellSize">Cell edge in the plate's units.</param>
/// <param name="travelCells">The most cells the torch may cross in one step.</param>
/// <param name="minSeconds">No step is split shorter than this for the torch's sake.</param>
public HeatStepSchedule(CutSimulation simulation, double slotSeconds, double cellSize,
double travelCells, double minSeconds)
{
SlotSeconds = slotSeconds;
var total = simulation.TotalTime.TotalSeconds;
splits = new int[(int)System.Math.Max(0, System.Math.Ceiling(total / slotSeconds))];
Array.Fill(splits, 1);
foreach (var step in simulation.Steps)
{
var seconds = step.Duration.TotalSeconds;
if (step.Kind != CutSimulationStepKind.Cut || seconds <= 0 || step.Length <= 0)
continue;
var limit = System.Math.Max(minSeconds, travelCells * cellSize * seconds / step.Length);
var split = SplitFor(slotSeconds / limit);
if (split == 1)
continue;
var start = step.StartTime.TotalSeconds;
var first = (int)System.Math.Floor(start / slotSeconds + Tolerance);
var last = (int)System.Math.Ceiling((start + seconds) / slotSeconds - Tolerance) - 1;
for (var slot = System.Math.Max(0, first); slot <= System.Math.Min(splits.Length - 1, last); slot++)
splits[slot] = System.Math.Max(splits[slot], split);
}
foreach (var split in splits)
StepCount += split;
}
public double SlotSeconds { get; }
/// <summary>Steps from the start to the end of the cut.</summary>
public long StepCount { get; }
/// <summary>
/// The first step boundary after <paramref name="time"/>, with the count its slot is
/// split into. Past the cut, slots are whole.
/// </summary>
public (double Time, int Split) Next(double time)
{
var (slot, index, split) = StepAfter(time);
return (End(slot, index, split), split);
}
/// <summary>
/// The step that ends first after <paramref name="time"/>: its slot, its place in the
/// slot (1 to the split) and the count the slot is split into.
/// </summary>
public (long Slot, int Index, int Split) StepAfter(double time)
{
var slot = (long)System.Math.Floor(time / SlotSeconds + Tolerance);
while (true)
{
var split = slot < splits.Length ? splits[slot] : 1;
var start = slot * SlotSeconds;
var index = System.Math.Max(1, (long)System.Math.Floor((time - start) / SlotSeconds * split + Tolerance) + 1);
if (index <= split)
return (slot, (int)index, split);
slot++;
}
}
/// <summary>When step <paramref name="index"/> (1 to <paramref name="split"/>) of a slot ends.</summary>
public double End(long slot, int index, int split) =>
index == split ? (slot + 1) * SlotSeconds : slot * SlotSeconds + SlotSeconds * index / split;
private static int SplitFor(double needed)
{
foreach (var split in Splits)
{
if (split >= needed - 1e-6)
return split;
}
return Splits[^1];
}
}
}