Files
OpenNest/OpenNest.Desktop/Interaction/HeatEstimate.cs
T
ajandClaude Sonnet 5.5 f9f5299d16 feat(thermal): finer heat map, on a graphics card when there is one
Split only cutting time into short steps, raise the automatic resolution,
add an optional ILGPU accelerator with CPU fallback, and estimate heat off
the UI thread.

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

100 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenNest.CNC;
using OpenNest.CNC.Thermal;
namespace OpenNest.Desktop.Interaction;
/// <summary>
/// A heat model run over its whole cut, ready to play: what the cut simulator does off the UI
/// thread before it shows the heat layer.
/// </summary>
public sealed class HeatEstimate : IDisposable
{
private HeatEstimate(PlateHeatSimulation heat, float scale, string device)
{
Heat = heat;
Scale = scale;
Device = device;
}
/// <summary>The model, back at the start.</summary>
public PlateHeatSimulation Heat { get; }
/// <summary>
/// The rise to color as hottest. Pierces leave single very hot cells, and scaling to them
/// would wash out the areas where heat builds up, so this is the hottest half percent.
/// </summary>
public float Scale { get; }
/// <summary>The graphics card the model runs on; null on the processor.</summary>
public string Device { get; }
/// <summary>
/// Builds and runs the model, on <paramref name="accelerator"/> when there is one and on the
/// processor (with the coarser cells that suit it) when that fails.
/// </summary>
/// <param name="parameters">The model's inputs for the given accelerator, or none.</param>
public static HeatEstimate Run(Plate plate, CutSimulation simulation,
Func<IHeatAccelerator, ThermalParameters> parameters, IHeatAccelerator accelerator,
CancellationToken cancellationToken = default)
{
if (accelerator != null)
{
try
{
return Run(plate, simulation, parameters(accelerator), accelerator.Name, cancellationToken);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Out of card memory or a driver fault: the processor still can.
}
}
return Run(plate, simulation, parameters(null), null, cancellationToken);
}
/// <summary>True when both timelines cut and travel the same, at the same times.</summary>
public static bool SameMoves(CutSimulation a, CutSimulation b) =>
a != null && b != null && a.Steps.Count == b.Steps.Count
&& a.Steps.Zip(b.Steps).All(pair => Move(pair.First).Equals(Move(pair.Second)));
public void Dispose() => Heat.Dispose();
private static object Move(CutSimulationStep s) =>
(s.Kind, s.Start, s.End, s.IsArc, s.Center, s.Sweep, s.StartTime, s.Duration, (s.Layer, s.IsHole, s.ContourIndex));
private static HeatEstimate Run(Plate plate, CutSimulation simulation, ThermalParameters parameters,
string device, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var heat = new PlateHeatSimulation(plate, simulation, parameters);
try
{
heat.RunToEnd(cancellationToken: cancellationToken);
var scale = ColorScale(heat);
heat.Reset();
return new HeatEstimate(heat, scale, heat.IsAccelerated ? device : null);
}
catch
{
heat.Dispose();
throw;
}
}
private static float ColorScale(PlateHeatSimulation heat)
{
var all = heat.Peaks;
var peaks = new List<float>();
for (var i = 0; i < all.Length; i++)
{
if (!heat.IsRemoved(i) && all[i] > 0)
peaks.Add(all[i]);
}
peaks.Sort();
return peaks.Count == 0 ? 1f : peaks[(int)(0.995 * (peaks.Count - 1))];
}
}