< Summary

Class:NearestNeighborInterpolator2D
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Utils/Interpolator2D.cs
Covered lines:0
Uncovered lines:15
Coverable lines:15
Total lines:86
Line coverage:0% (0 of 15)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:3
Method coverage:0% (0 of 3)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NearestNeighborInterpolator2D(...)0%6200%
NearestNeighborInterpolator2D(...)0%6200%
Interpolate(...)0%6200%

File(s)

/github/workspace/Assets/Scripts/Utils/Interpolator2D.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5
 6// 2D interpolator data point.
 7public class Interpolator2DDataPoint {
 8  // 2D coordinates.
 9  public Vector2 Coordinates { get; }
 10
 11  // Arbitrary data consisting of floats.
 12  public List<float> Data { get; }
 13
 14  public Interpolator2DDataPoint() {}
 15  public Interpolator2DDataPoint(in Vector2 coordinates, in List<float> data) {
 16    Coordinates = coordinates;
 17    Data = data;
 18  }
 19
 20  // Validate and parse the data from strings to floats.
 21  public static (bool, List<float>) ValidateAndParseData(in string[] values) {
 22    List<float> parsedValues = new List<float>();
 23    for (int i = 0; i < values.Length; ++i) {
 24      if (!float.TryParse(values[i], out float parsedValue)) {
 25        return (false, new List<float>());
 26      }
 27      parsedValues.Add(parsedValue);
 28    }
 29    return (true, parsedValues);
 30  }
 31}
 32
 33// The 2D interpolator class interpolates values on a 2D grid.
 34// The first two columns specify the coordinates of each data point that are used for interpolating
 35// the remaining data values.
 36public abstract class IInterpolator2D {
 37  // 2D interpolator data points.
 38  protected readonly List<Interpolator2DDataPoint> _data = new List<Interpolator2DDataPoint>();
 39
 40  public IInterpolator2D(in string[] csvLines) {
 41    foreach (string line in csvLines) {
 42      string[] values = line.Split(',');
 43      (bool success, List<float> parsedValues) =
 44          Interpolator2DDataPoint.ValidateAndParseData(values);
 45      if (success && parsedValues.Count >= 2) {
 46        _data.Add(new Interpolator2DDataPoint(new Vector2(parsedValues[0], parsedValues[1]),
 47                                              parsedValues.Skip(2).ToList()));
 48      }
 49    }
 50  }
 51  public IInterpolator2D(List<Interpolator2DDataPoint> data) {
 52    _data = data;
 53  }
 54
 55  // Interpolate the value.
 56  public abstract Interpolator2DDataPoint Interpolate(float x, float y);
 57  public Interpolator2DDataPoint Interpolate(in Vector2 coordinates) {
 58    return Interpolate(coordinates.x, coordinates.y);
 59  }
 60}
 61
 62// The 2D nearest neighbor interpolator class interpolates values on a 2D grid using nearest
 63// neighbor interpolation.
 64public class NearestNeighborInterpolator2D : IInterpolator2D {
 65  // K-D tree for nearest neighbor interpolation.
 66  private KDTree<Interpolator2DDataPoint> _tree;
 67
 068  public NearestNeighborInterpolator2D(in string[] csvLines) : base(csvLines) {
 069    _tree = new KDTree<Interpolator2DDataPoint>(
 070        _data, (Interpolator2DDataPoint point) => point.Coordinates);
 071  }
 072  public NearestNeighborInterpolator2D(List<Interpolator2DDataPoint> data) : base(data) {
 073    _tree = new KDTree<Interpolator2DDataPoint>(
 074        _data, (Interpolator2DDataPoint point) => point.Coordinates);
 075  }
 76
 77  // Interpolate the value using nearest neighbor interpolation.
 078  public override Interpolator2DDataPoint Interpolate(float x, float y) {
 079    Interpolator2DDataPoint closestPoint = _tree.NearestNeighbor(new Vector2(x, y));
 080    if (closestPoint == null) {
 081      Debug.LogError("No data points available for interpolation.");
 082      return new Interpolator2DDataPoint(new Vector2(x, y), new List<float>());
 83    }
 084    return closestPoint;
 085  }
 86}