using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenNest.CNC;
using OpenNest.CNC.Thermal;
namespace OpenNest.Desktop.Interaction;
///
/// 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.
///
public sealed class HeatEstimate
{
private HeatEstimate(PlateHeatSimulation heat, float scale)
{
Heat = heat;
Scale = scale;
}
/// The model, back at the start.
public PlateHeatSimulation Heat { get; }
///
/// The rise to color as hottest, the top of the . Pierces leave
/// single very hot cells, so this is where the hottest half percent begins, held within the
/// scale's limits.
///
public float Scale { get; }
/// Builds and runs the model.
public static HeatEstimate Run(Plate plate, CutSimulation simulation, ThermalParameters parameters,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var heat = new PlateHeatSimulation(plate, simulation, parameters);
heat.RunToEnd(cancellationToken: cancellationToken);
var scale = HeatColorScale.Top(HotRise(heat));
heat.Reset();
return new HeatEstimate(heat, scale);
}
/// True when both timelines cut and travel the same, at the same times.
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)));
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 float HotRise(PlateHeatSimulation heat)
{
var all = heat.Peaks;
var peaks = new List();
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 ? 0 : peaks[(int)(0.995 * (peaks.Count - 1))];
}
}