using System; using System.Collections.Generic; using System.Linq; using System.Threading; using OpenNest.CNC.Thermal; using OpenNest.Geometry; namespace OpenNest.Engine.Sequencing { /// /// What the heat-aware order needs of one part: where its cut starts and ends, its cut extent /// (where its heat goes) and how long the torch is on. /// internal readonly record struct HeatAwareItem(Vector Entry, Vector Exit, Box Extent, double TorchSeconds) { internal Vector Center => Extent.Center; /// The heat's initial spread: a quarter of the extent's squared diagonal. internal double Spread2 => (Extent.Length * Extent.Length + Extent.Width * Extent.Width) / 4; } /// /// Orders parts forward from a start point: each next part is the dependency-ready one with the /// least travel seconds plus weight times the estimated heat (°C) at its center when the /// tool gets there, from a of the parts already cut. So the /// route leaves a hot zone, but only as far as it must. Without a thickness, or with no weight, /// it is a nearest-neighbor order by travel. It ignores whether a move passes over parts already /// cut; see docs/plate-heat-map.md#sequencing. /// internal static class HeatAwareOrder { /// Every part's data, by ordinal. /// The ordinals to order; prerequisites outside this set count as cut. /// The tool position before the first part. /// Ordinals that must come before each part; must be acyclic. /// Parts already cut, in order, from : their heat /// is laid down first. Null for none. /// Where the tool was before . internal static int[] Plan(IReadOnlyList items, IReadOnlyList parts, Vector start, IReadOnlyList> prerequisites, ThermalParameters thermal, double weight, CancellationToken token, IReadOnlyList done = null, Vector? origin = null) { var estimate = new HeatSpreadEstimate(thermal ?? new ThermalParameters()); var heated = estimate.HasThickness && double.IsFinite(weight) && weight > 0; var time = 0.0; if (done is { Count: > 0 }) { var position = origin ?? start; foreach (var part in done) { var item = items[part]; time += estimate.TravelSeconds(position.DistanceTo(item.Entry)) + item.TorchSeconds; estimate.Add(item.Center, item.Spread2, item.TorchSeconds, time); position = item.Exit; } } var pending = new HashSet(parts); var remaining = parts.ToList(); var order = new int[parts.Count]; var current = start; for (var step = 0; step < order.Length; step++) { token.ThrowIfCancellationRequested(); var best = -1; var bestCost = double.MaxValue; foreach (var part in remaining) { if (prerequisites[part].Any(pending.Contains)) continue; var item = items[part]; var travel = estimate.TravelSeconds(current.DistanceTo(item.Entry)); var cost = heated ? travel + weight * estimate.TemperatureAt(item.Center, time + travel) : travel; // Strictly cheaper only, so ties keep the given order. if (cost < bestCost) { best = part; bestCost = cost; } } if (best < 0) throw new InvalidOperationException("Part prerequisites form a cycle."); var chosen = items[best]; time += estimate.TravelSeconds(current.DistanceTo(chosen.Entry)) + chosen.TorchSeconds; if (heated) estimate.Add(chosen.Center, chosen.Spread2, chosen.TorchSeconds, time); current = chosen.Exit; order[step] = best; pending.Remove(best); remaining.Remove(best); } return order; } } }