using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MultiWheelC.TrajectoryPlanning.CoarsePath;
namespace MultiWheelC.TrajectoryPlanning.PathSmoothing.Processing;
/// 已校验、已按单一行驶方向分割并重采样的路径段。
public sealed class PreparedDirectionSegment
{
/// 创建不可变方向段。
/// 在 中从零开始的段索引。
/// 本段唯一的车辆行驶方向。
/// 本段的非空采样点集合;构造后会复制为只读快照。
/// 若首点紧随换向,则为 。
/// 若末点紧邻换向,则为 。
public PreparedDirectionSegment(
int segmentIndex,
TravelDirection direction,
IReadOnlyList points,
bool startsAtGearSwitch,
bool endsAtGearSwitch)
: this(segmentIndex, direction, points, startsAtGearSwitch, endsAtGearSwitch, null)
{
}
/// 创建带有真实起始车辆曲率边界状态的不可变方向段。
/// 在 中从零开始的段索引。
/// 本段唯一的车辆行驶方向。
/// 本段的非空采样点集合;构造后会复制为只读快照。
/// 若首点紧随换向,则为 。
/// 若末点紧邻换向,则为 。
/// 物理起点车辆曲率边界,单位 1/m;未知时为 。
public PreparedDirectionSegment(
int segmentIndex,
TravelDirection direction,
IReadOnlyList points,
bool startsAtGearSwitch,
bool endsAtGearSwitch,
double? startVehicleCurvaturePerMeter)
{
if (segmentIndex < 0) throw new ArgumentOutOfRangeException(nameof(segmentIndex));
if (points == null || points.Count == 0) throw new ArgumentException("A prepared segment requires points.", nameof(points));
if (startVehicleCurvaturePerMeter.HasValue && !IsFinite(startVehicleCurvaturePerMeter.Value))
throw new ArgumentOutOfRangeException(nameof(startVehicleCurvaturePerMeter));
SegmentIndex = segmentIndex;
Direction = direction;
Points = CopyReadOnly(points);
StartsAtGearSwitch = startsAtGearSwitch;
EndsAtGearSwitch = endsAtGearSwitch;
StartVehicleCurvaturePerMeter = startVehicleCurvaturePerMeter;
}
/// 从零开始的分段序号;在 中必须与其位置一致。
public int SegmentIndex { get; }
/// 该段的唯一行驶方向。
public TravelDirection Direction { get; }
/// 不包含相邻段点的本段不可变采样点。
public IReadOnlyList Points { get; }
/// 本段首点是否为换向后保留的新方向点。
public bool StartsAtGearSwitch { get; }
/// 本段末点之后是否紧邻换向点。
public bool EndsAtGearSwitch { get; }
/// 原始车辆在本段物理起点的曲率边界状态,单位 1/m。
public double? StartVehicleCurvaturePerMeter { get; }
private static bool IsFinite(double value) => !double.IsNaN(value) && !double.IsInfinity(value);
private static IReadOnlyList CopyReadOnly(IReadOnlyList source)
{
var copy = new List(source.Count);
for (int index = 0; index < source.Count; index++) copy.Add(source[index]);
return new ReadOnlyCollection(copy);
}
}