| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using UnityEngine; |
| | | 4 | | |
| | | 5 | | // Base implementation of a 2D interpolator. |
| | | 6 | | public abstract class Interpolator2DBase : IInterpolator2D { |
| | | 7 | | // 2D interpolator data points. |
| | | 8 | | protected readonly List<Interpolator2DDataPoint> _data; |
| | | 9 | | |
| | 1 | 10 | | public IReadOnlyList<Interpolator2DDataPoint> Data => _data; |
| | | 11 | | |
| | 48 | 12 | | public Interpolator2DBase(IEnumerable<Interpolator2DDataPoint> data) { |
| | 24 | 13 | | _data = data?.ToList() ?? new List<Interpolator2DDataPoint>(); |
| | 24 | 14 | | } |
| | 36 | 15 | | public Interpolator2DBase(string[] csvLines) : this(ParseCsvLines(csvLines)) {} |
| | | 16 | | |
| | | 17 | | // Interpolate the value. |
| | | 18 | | public abstract Interpolator2DDataPoint Interpolate(float x, float y); |
| | 0 | 19 | | public Interpolator2DDataPoint Interpolate(in Vector2 coordinates) { |
| | 0 | 20 | | return Interpolate(coordinates.x, coordinates.y); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | // Parse the CSV lines into 2D data points. |
| | 12 | 24 | | private static IEnumerable<Interpolator2DDataPoint> ParseCsvLines(IEnumerable<string> lines) { |
| | 12 | 25 | | var parsedDataPoints = new List<Interpolator2DDataPoint>(); |
| | 1758 | 26 | | foreach (string line in lines) { |
| | 575 | 27 | | if (string.IsNullOrWhiteSpace(line)) { |
| | 1 | 28 | | continue; |
| | | 29 | | } |
| | 573 | 30 | | string[] values = line.Split(','); |
| | 573 | 31 | | (bool success, List<float> parsedValues) = |
| | | 32 | | Interpolator2DDataPoint.ValidateAndParseData(values); |
| | 1138 | 33 | | if (success && parsedValues.Count >= 2) { |
| | 565 | 34 | | parsedDataPoints.Add(new Interpolator2DDataPoint { |
| | | 35 | | Coordinates = new Vector2(parsedValues[0], parsedValues[1]), |
| | | 36 | | Data = parsedValues.Skip(2).ToList(), |
| | | 37 | | }); |
| | 565 | 38 | | } |
| | 573 | 39 | | } |
| | 12 | 40 | | return parsedDataPoints; |
| | 12 | 41 | | } |
| | | 42 | | } |