using System;
using System.Collections.Generic;
using System.Linq;
using OpenNest.Diagnostics;
using OpenNest.Geometry;
namespace OpenNest.CNC.CuttingPlanning;
/// Owned value motions; no references to live programs, instructions or settings.
public sealed class OwnedExecution
{
internal OwnedExecution(IEnumerable motions)
{
Motions = Array.AsReadOnly(motions.ToArray());
}
public IReadOnlyList Motions { get; }
public Vector DeparturePoint => Motions[^1].End;
public bool HasCuttingContour => Motions.Any(move => !move.Rapid
&& move.Layer is LayerType.Cut or LayerType.Display
&& move.Length > PostVerificationGeometry.Epsilon);
public double RapidDistanceFrom(Vector arrival)
{
PostVerificationGeometry.Validate(arrival);
var total = 0.0;
for (var index = 0; index < Motions.Count; index++)
{
var move = Motions[index];
if (move.Rapid && (index == 0 ? arrival : move.Start) is { } start)
total += start.DistanceTo(move.End);
}
return total;
}
}
/// An immutable motion value; native line/arc geometry stays private to Core.
public sealed class ExecutionMotion
{
internal ExecutionMotion(Vector? start, Vector end, bool rapid, LayerType layer,
PostVerificationGeometry.Curve curve, bool headRaised = false, int? code = null, int? callCode = null)
{
Start = start;
End = end;
Rapid = rapid;
Layer = layer;
Curve = curve;
HeadRaised = headRaised;
Code = code;
CallCode = callCode;
}
public Vector? Start { get; }
public Vector End { get; }
public bool Rapid { get; }
public LayerType Layer { get; }
/// A rapid made with the head raised over parts already cut ().
public bool HeadRaised { get; }
///
/// The index of the code this motion runs in the read program's own codes; null for a motion
/// inside a sub-program and for the rapid a program that opens with a cut is given.
///
public int? Code { get; }
///
/// For a motion inside a sub-program, the index of the read program's own call that runs it;
/// null for the program's own motions.
///
internal int? CallCode { get; }
public double Length => Curve?.Length ?? 0;
internal PostVerificationGeometry.Curve Curve { get; }
internal ExecutionMotion WithStart(Vector? start) => new(start, End, Rapid, Layer, Curve, HeadRaised, Code, CallCode);
}