| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Linq; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | // The launch angle CSV interpolator loads launch angle data from a CSV file and provides |
| | | 7 | | // interpolated values for arbitrary target positions. |
| | | 8 | | public class LaunchAngleCsvInterpolator : LaunchAngleInterpolatorBase { |
| | | 9 | | // Path to the CSV file. |
| | | 10 | | // The first two columns of the CSV file specify the coordinates of each data point. |
| | | 11 | | // The third column denotes the launch angle in degrees, and the fourth column denotes the time to |
| | | 12 | | // reach the target position. |
| | | 13 | | private readonly string _relativePath; |
| | | 14 | | |
| | | 15 | | // Delegate for loading the CSV file. |
| | | 16 | | public delegate string ConfigLoaderDelegate(string path); |
| | | 17 | | private readonly ConfigLoaderDelegate _configLoader; |
| | | 18 | | |
| | | 19 | | public LaunchAngleCsvInterpolator(IAgent agent) |
| | 0 | 20 | | : this(agent, path: Path.Combine("Planning", "hydra70_launch_angle.csv"), |
| | 0 | 21 | | configLoader: ConfigLoader.LoadFromStreamingAssets) {} |
| | | 22 | | public LaunchAngleCsvInterpolator(IAgent agent, string path, ConfigLoaderDelegate configLoader) |
| | 18 | 23 | | : base(agent) { |
| | 9 | 24 | | _relativePath = path; |
| | 9 | 25 | | _configLoader = configLoader; |
| | 9 | 26 | | } |
| | | 27 | | |
| | | 28 | | // Initialize the interpolator. |
| | 7 | 29 | | protected override void InitInterpolator() { |
| | 7 | 30 | | string fileContent = _configLoader(_relativePath); |
| | 8 | 31 | | if (string.IsNullOrEmpty(fileContent)) { |
| | 1 | 32 | | throw new InvalidOperationException($"CSV file {_relativePath} is either null or empty."); |
| | | 33 | | } |
| | | 34 | | |
| | 6 | 35 | | string[] csvLines = fileContent.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); |
| | 573 | 36 | | string[] dataLines = csvLines.Where(line => !string.IsNullOrWhiteSpace(line)).ToArray(); |
| | 6 | 37 | | if (dataLines.Length < 1) { |
| | 0 | 38 | | throw new InvalidOperationException( |
| | | 39 | | $"No data points available for interpolation in {_relativePath}."); |
| | | 40 | | } |
| | | 41 | | |
| | 6 | 42 | | try { |
| | 6 | 43 | | _interpolator = new NearestNeighborInterpolator2D(dataLines); |
| | 6 | 44 | | } catch (Exception e) { |
| | 0 | 45 | | throw new InvalidOperationException( |
| | | 46 | | $"Failed to initialize interpolator from {_relativePath}.", e); |
| | | 47 | | } |
| | 6 | 48 | | } |
| | | 49 | | } |