| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using UnityEngine; |
| | | 3 | | |
| | | 4 | | // The 2D nearest neighbor interpolator class interpolates values on a 2D grid using nearest |
| | | 5 | | // neighbor interpolation. |
| | | 6 | | public class NearestNeighborInterpolator2D : Interpolator2DBase { |
| | | 7 | | // K-D tree for nearest neighbor interpolation. |
| | | 8 | | private KDTree<Interpolator2DDataPoint> _tree; |
| | | 9 | | |
| | 24 | 10 | | public NearestNeighborInterpolator2D(IEnumerable<Interpolator2DDataPoint> data) : base(data) { |
| | 12 | 11 | | _tree = new KDTree<Interpolator2DDataPoint>( |
| | 230 | 12 | | _data, (Interpolator2DDataPoint point) => point.Coordinates); |
| | 12 | 13 | | } |
| | 24 | 14 | | public NearestNeighborInterpolator2D(string[] csvLines) : base(csvLines) { |
| | 12 | 15 | | _tree = new KDTree<Interpolator2DDataPoint>( |
| | 3504 | 16 | | _data, (Interpolator2DDataPoint point) => point.Coordinates); |
| | 12 | 17 | | } |
| | | 18 | | |
| | | 19 | | // Interpolate the value using nearest neighbor interpolation. |
| | 43 | 20 | | public override Interpolator2DDataPoint Interpolate(float x, float y) { |
| | 43 | 21 | | Interpolator2DDataPoint closestPoint = _tree.NearestNeighbor(new Vector2(x, y)); |
| | 45 | 22 | | if (closestPoint == null) { |
| | 2 | 23 | | Debug.LogError("No data points available for interpolation."); |
| | 2 | 24 | | return new Interpolator2DDataPoint { Coordinates = new Vector2(x, y), |
| | | 25 | | Data = new List<float>() }; |
| | | 26 | | } |
| | 41 | 27 | | return closestPoint; |
| | 43 | 28 | | } |
| | | 29 | | } |