118 lines
5.6 KiB
C#
118 lines
5.6 KiB
C#
using System;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using System.IO;
|
||
|
|
using MultiWheelC.TrajectoryPlanning.PathSmoothing;
|
||
|
|
using Newtonsoft.Json;
|
||
|
|
|
||
|
|
namespace MultiWheelC.TrajectoryPlanning.PathSmoothing.Test;
|
||
|
|
|
||
|
|
public sealed class LocalG2DiagnosticEvidenceLoader
|
||
|
|
{
|
||
|
|
private const string SourceMeasurementBatchSha256 = "ac8166828813d85bf6f8b58f985839e5b2a049e75cb94186ed04f59d540e4eed";
|
||
|
|
private const string FixtureSha256 = "3d05daee5a211b3e7aa0b77193423b5fa07d3135e241a4413be3518fc7efe563";
|
||
|
|
private const string CandidateStableKey = "single-turn/s0/r0/w5/seed2";
|
||
|
|
private const string CandidateSha256 = "7cefb76c77e48a49bf3212e7a2472e23036c3899daa94b5c6b68fa5db29a3392";
|
||
|
|
|
||
|
|
public LocalG2DiagnosticEvidence LoadAndVerify(string evidencePath)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(evidencePath))
|
||
|
|
throw new InvalidDataException("Diagnostic evidence path is required.");
|
||
|
|
|
||
|
|
LocalG2DiagnosticEvidence evidence = JsonConvert.DeserializeObject<LocalG2DiagnosticEvidence>(File.ReadAllText(evidencePath));
|
||
|
|
Validate(evidence);
|
||
|
|
return evidence;
|
||
|
|
}
|
||
|
|
catch (InvalidDataException)
|
||
|
|
{
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
catch (Exception exception)
|
||
|
|
{
|
||
|
|
throw new InvalidDataException("Local G2 diagnostic evidence is invalid.", exception);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void Validate(LocalG2DiagnosticEvidence evidence)
|
||
|
|
{
|
||
|
|
if (evidence == null ||
|
||
|
|
!string.Equals(evidence.SourceMeasurementBatchSha256, SourceMeasurementBatchSha256, StringComparison.Ordinal) ||
|
||
|
|
!string.Equals(evidence.FixtureSha256, FixtureSha256, StringComparison.Ordinal) ||
|
||
|
|
!string.Equals(evidence.ScenarioId, "single-turn", StringComparison.Ordinal) ||
|
||
|
|
!string.Equals(evidence.CandidateStableKey, CandidateStableKey, StringComparison.Ordinal) ||
|
||
|
|
!string.Equals(evidence.CandidateSha256, CandidateSha256, StringComparison.Ordinal) ||
|
||
|
|
evidence.CandidateIndex != 5 || evidence.SegmentIndex != 0 ||
|
||
|
|
!string.Equals(evidence.EvaluatorResult, "InsufficientClearance", StringComparison.Ordinal) ||
|
||
|
|
!string.Equals(evidence.StopGate, "Clearance", StringComparison.Ordinal) ||
|
||
|
|
!string.Equals(evidence.PublishedStatus, "Unchanged", StringComparison.Ordinal) ||
|
||
|
|
!IsFinite(evidence.WindowStartArcLengthMeters) || !IsFinite(evidence.WindowEndArcLengthMeters) ||
|
||
|
|
evidence.WindowEndArcLengthMeters.Value <= evidence.WindowStartArcLengthMeters.Value ||
|
||
|
|
!IsFinite(evidence.StartGeometricCurvaturePerMeter) || !IsFinite(evidence.EndGeometricCurvaturePerMeter) ||
|
||
|
|
!IsFinite(evidence.StartVehicleCurvaturePerMeter) || !IsFinite(evidence.EndVehicleCurvaturePerMeter) ||
|
||
|
|
evidence.CandidatePoints == null || evidence.CandidatePoints.Count != 10)
|
||
|
|
{
|
||
|
|
throw new InvalidDataException("Local G2 diagnostic evidence contract is invalid.");
|
||
|
|
}
|
||
|
|
|
||
|
|
double previousArc = 0d;
|
||
|
|
for (int index = 0; index < evidence.CandidatePoints.Count; index++)
|
||
|
|
{
|
||
|
|
LocalG2DiagnosticEvidencePoint point = evidence.CandidatePoints[index];
|
||
|
|
if (point == null || !IsFinite(point.X) || !IsFinite(point.Y) || !IsFinite(point.ReferenceArcLengthMeters) ||
|
||
|
|
!IsFinite(point.HeadingRadians) || !IsFinite(point.UnwrappedHeadingRadians) ||
|
||
|
|
point.Source != (int)SmoothedPathPointSource.LocalG2Transition ||
|
||
|
|
(index > 0 && point.ReferenceArcLengthMeters.Value <= previousArc))
|
||
|
|
{
|
||
|
|
throw new InvalidDataException("Local G2 diagnostic evidence point contract is invalid.");
|
||
|
|
}
|
||
|
|
|
||
|
|
previousArc = point.ReferenceArcLengthMeters.Value;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Math.Abs(evidence.CandidatePoints[0].ReferenceArcLengthMeters.Value - evidence.WindowStartArcLengthMeters.Value) > 1e-8d ||
|
||
|
|
Math.Abs(evidence.CandidatePoints[evidence.CandidatePoints.Count - 1].ReferenceArcLengthMeters.Value - evidence.WindowEndArcLengthMeters.Value) > 1e-8d)
|
||
|
|
{
|
||
|
|
throw new InvalidDataException("Local G2 diagnostic evidence window contract is invalid.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool IsFinite(double? value)
|
||
|
|
{
|
||
|
|
return value.HasValue && IsFinite(value.Value);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool IsFinite(double value) => !double.IsNaN(value) && !double.IsInfinity(value);
|
||
|
|
}
|
||
|
|
|
||
|
|
public sealed class LocalG2DiagnosticEvidence
|
||
|
|
{
|
||
|
|
public string SourceMeasurementBatchSha256 { get; set; }
|
||
|
|
public string FixtureSha256 { get; set; }
|
||
|
|
public string ScenarioId { get; set; }
|
||
|
|
public string CandidateStableKey { get; set; }
|
||
|
|
public string CandidateSha256 { get; set; }
|
||
|
|
public int CandidateIndex { get; set; }
|
||
|
|
public int SegmentIndex { get; set; }
|
||
|
|
public double? WindowStartArcLengthMeters { get; set; }
|
||
|
|
public double? WindowEndArcLengthMeters { get; set; }
|
||
|
|
public double? StartGeometricCurvaturePerMeter { get; set; }
|
||
|
|
public double? EndGeometricCurvaturePerMeter { get; set; }
|
||
|
|
public double? StartVehicleCurvaturePerMeter { get; set; }
|
||
|
|
public double? EndVehicleCurvaturePerMeter { get; set; }
|
||
|
|
public List<LocalG2DiagnosticEvidencePoint> CandidatePoints { get; set; }
|
||
|
|
public string EvaluatorResult { get; set; }
|
||
|
|
public string StopGate { get; set; }
|
||
|
|
public string PublishedStatus { get; set; }
|
||
|
|
}
|
||
|
|
|
||
|
|
public sealed class LocalG2DiagnosticEvidencePoint
|
||
|
|
{
|
||
|
|
public double? X { get; set; }
|
||
|
|
public double? Y { get; set; }
|
||
|
|
public double? ReferenceArcLengthMeters { get; set; }
|
||
|
|
public double? HeadingRadians { get; set; }
|
||
|
|
public double? UnwrappedHeadingRadians { get; set; }
|
||
|
|
public int Source { get; set; }
|
||
|
|
}
|