using System.Collections.Generic; using OpenNest.CNC; using OpenNest.CutOffs; using OpenNest.Desktop.Interaction; using OpenNest.Geometry; using OpenNest.Math; using SkiaSharp; namespace OpenNest.Avalonia.Controls { /// /// Plate > Simulate Cutting over the plate, as the WinForms cut simulation overlay: the cut /// trail grows behind the torch, travel is traced faintly in dashes, and parts (and cut-off /// segments) the torch has not reached yet are drawn muted. Display only; the shared /// holds the playhead. /// public sealed partial class PlateCanvas { // The finished trail is kept in world-space chunks of this many moves, built once each, // so a frame only rebuilds the moves since the last full chunk. private const int TrailChunkSize = 256; private CutSimulationPlayback cutSimulation; private SimulationTrail simulationTrail; /// The playhead drawn over the plate, or null when not simulating. public CutSimulationPlayback CutSimulation { get => cutSimulation; set { cutSimulation = value; simulationTrail = null; InvalidateVisual(); } } // True for a part the torch has not started yet. private bool IsPartPending(int partIndex) => cutSimulation?.IsPartPending(partIndex) == true; // While simulating, each of the cut-off's trimmed segments is its own part, found by its // drawing's reference, and mutes until the torch reaches it. False when not simulating. private bool BuildSimulatedCutOff(Scene scene, CutOff cutOff) { if (cutSimulation?.IsActive != true) return false; for (var i = 0; i < plate.Parts.Count; i++) { var part = plate.Parts[i]; if (!ReferenceEquals(part.BaseDrawing, cutOff.Drawing)) continue; var strokes = IsPartPending(i) ? scene.MutedCutOffStrokes : scene.CutOffStrokes; strokes.AddRange(CutOff.CutStrokes(part.Program, part.Location)); } return true; } private void BuildSimulation(Scene scene) { if (cutSimulation?.Simulation is not { Steps.Count: > 0 } sim) return; var time = cutSimulation.Time; var index = sim.IndexAt(time); var finished = System.Math.Clamp(index, 0, sim.Steps.Count); if (simulationTrail == null || !ReferenceEquals(simulationTrail.Simulation, sim)) simulationTrail = new SimulationTrail(sim); scene.Trail = simulationTrail.Chunks(finished); if (index >= sim.Steps.Count) { scene.Head = new SimulationHead(sim.Steps[^1].End, null, 0); return; } var step = sim.Steps[index]; var fraction = sim.FractionAt(index, time); if (step.Kind != CutSimulationStepKind.Pierce && fraction > 0) { var path = new SKPath(); path.MoveTo((float)step.Start.X, (float)step.Start.Y); AddMove(path, step, fraction); scene.Current = (path, step.Kind); } scene.Head = new SimulationHead(step.PointAt(fraction), step.Kind, fraction); } // A move from where the path stands, cut short at fraction of its length. Arcs go in no // more than half a turn at a time, so a full circle stays a circle. private static void AddMove(SKPath path, CutSimulationStep step, double fraction) { if (!step.IsArc || step.Radius <= Tolerance.Epsilon) { var end = fraction >= 1 ? step.End : step.PointAt(fraction); path.LineTo((float)end.X, (float)end.Y); return; } var oval = new SKRect((float)(step.Center.X - step.Radius), (float)(step.Center.Y - step.Radius), (float)(step.Center.X + step.Radius), (float)(step.Center.Y + step.Radius)); var sweep = step.Sweep * fraction; var pieces = System.Math.Max(1, (int)System.Math.Ceiling(System.Math.Abs(sweep) / System.Math.PI)); for (var i = 0; i < pieces; i++) { var start = step.StartAngle + sweep * i / pieces; path.ArcTo(oval, (float)Angle.ToDegrees(start), (float)Angle.ToDegrees(sweep / pieces), false); } } private sealed record SimulationHead(Vector Location, CutSimulationStepKind? Kind, double Fraction); /// One run of finished moves in world space: cuts, etches and travel apart. private sealed class TrailChunk { public int Start; public int End; public readonly SKPath Cuts = new(); public readonly SKPath Scribes = new(); public readonly SKPath Travel = new(); } /// /// The finished moves of one timeline. Full chunks are built once and never changed, so /// the render thread may draw them while the next frame is built. /// private sealed class SimulationTrail { private readonly List full = new(); public SimulationTrail(CutSimulation simulation) { Simulation = simulation; } public CutSimulation Simulation { get; } /// The chunks drawing moves [0, ). public IReadOnlyList Chunks(int finished) { // Scrubbing back drops the chunks past the playhead. while (full.Count > 0 && full[^1].End > finished) full.RemoveAt(full.Count - 1); var built = full.Count == 0 ? 0 : full[^1].End; while (built + TrailChunkSize <= finished) { full.Add(Build(built, built + TrailChunkSize)); built += TrailChunkSize; } var chunks = new List(full); if (built < finished) chunks.Add(Build(built, finished)); return chunks; } // Consecutive moves of a kind join into one figure, as the WinForms trail joins them. private TrailChunk Build(int from, int to) { var chunk = new TrailChunk { Start = from, End = to }; int lastCut = -2, lastScribe = -2, lastTravel = -2; for (var i = from; i < to; i++) { var step = Simulation.Steps[i]; switch (step.Kind) { case CutSimulationStepKind.Pierce: continue; case CutSimulationStepKind.Cut: Add(chunk.Cuts, step, i, ref lastCut); break; case CutSimulationStepKind.Scribe: Add(chunk.Scribes, step, i, ref lastScribe); break; default: Add(chunk.Travel, step, i, ref lastTravel); break; } } return chunk; } private static void Add(SKPath path, CutSimulationStep step, int index, ref int last) { if (last != index - 1) path.MoveTo((float)step.Start.X, (float)step.Start.Y); last = index; AddMove(path, step, 1); } } private sealed partial class Scene { public readonly List MutedCutOffStrokes = new(); public IReadOnlyList Trail; public (SKPath Path, CutSimulationStepKind Kind)? Current; public SimulationHead Head; } private sealed partial class PlatePainter { private static readonly SKColor MutedFill = new(220, 220, 220); // Gainsboro private static readonly SKColor MutedLine = new(128, 128, 128); // Gray private static readonly SKColor MutedLabel = new(105, 105, 105); // DimGray private static readonly SKColor TrailRapid = new(70, 150, 255, 150); private static readonly SKColor TrailGlow = new(255, 140, 0, 70); private static readonly SKColor TrailCut = new(255, 69, 0); // OrangeRed private static readonly SKColor TrailScribe = new(186, 85, 211); // MediumOrchid private static readonly SKColor HeadTravel = new(30, 144, 255); // DodgerBlue private static readonly SKColor HeadGlow = new(255, 165, 0, 90); // Orange private static readonly SKColor PierceRing = new(255, 215, 0); // Gold private void DrawMutedCutOffs() { if (scene.MutedCutOffStrokes.Count == 0) return; using var pen = Stroke(MutedLine, 2, new float[] { 6, 2 }); foreach (var stroke in scene.MutedCutOffStrokes) canvas.DrawLine(Screen(stroke.StartPoint), Screen(stroke.EndPoint), pen); } private void DrawSimulation() { if (scene.Head == null) return; using var rapid = WorldStroke(TrailRapid, 1, new float[] { 3, 1 }); using var glow = RoundStroke(TrailGlow, 6); using var cut = RoundStroke(TrailCut, 2); using var scribe = WorldStroke(TrailScribe, 1.5f); canvas.Save(); canvas.Concat(ref matrix); if (scene.Trail != null) { foreach (var chunk in scene.Trail) canvas.DrawPath(chunk.Travel, rapid); foreach (var chunk in scene.Trail) { canvas.DrawPath(chunk.Cuts, glow); canvas.DrawPath(chunk.Cuts, cut); } foreach (var chunk in scene.Trail) canvas.DrawPath(chunk.Scribes, scribe); } if (scene.Current is { } current) { switch (current.Kind) { case CutSimulationStepKind.Cut: canvas.DrawPath(current.Path, glow); canvas.DrawPath(current.Path, cut); break; case CutSimulationStepKind.Scribe: canvas.DrawPath(current.Path, scribe); break; default: canvas.DrawPath(current.Path, rapid); break; } } canvas.Restore(); DrawHead(scene.Head); } private SKPaint RoundStroke(SKColor color, float width) { var paint = WorldStroke(color, width); paint.StrokeJoin = SKStrokeJoin.Round; paint.StrokeCap = SKStrokeCap.Round; return paint; } // The torch: a ringed crosshair, glowing while it cuts, with a fading ring while it pierces. private void DrawHead(SimulationHead head) { var center = Screen(head.Location); var color = head.Kind switch { CutSimulationStepKind.Cut or CutSimulationStepKind.Pierce => TrailCut, CutSimulationStepKind.Scribe => TrailScribe, _ => HeadTravel, }; if (head.Kind == CutSimulationStepKind.Pierce) { var ring = 4 + 14 * (float)head.Fraction; using var piercePen = Stroke(PierceRing.WithAlpha((byte)(255 * (1 - head.Fraction))), 2); canvas.DrawCircle(center, ring, piercePen); } if (head.Kind is CutSimulationStepKind.Cut or CutSimulationStepKind.Pierce) { using var glowFill = Fill(HeadGlow); canvas.DrawCircle(center, 9, glowFill); } const float radius = 5; using var halo = Stroke(SKColors.White, 3); using var pen = Stroke(color, 1.5f); canvas.DrawCircle(center, radius, halo); canvas.DrawCircle(center, radius, pen); canvas.DrawLine(center.X - radius * 1.8f, center.Y, center.X + radius * 1.8f, center.Y, pen); canvas.DrawLine(center.X, center.Y - radius * 1.8f, center.X, center.Y + radius * 1.8f, pen); } } } }