< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Common.Components.VectorField2
Assembly: ReFlex.Core.Common
File(s): D:\a\reflex\reflex\library\src\Core\Common\Components\VectorField2.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 75
Coverable lines: 75
Total lines: 208
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
.ctor(...)0%620%
get_Item(...)0%4260%
set_Item(...)0%620%
get_Item(...)0%620%
set_Item(...)0%620%
get_Size()100%210%
get_SizeX()100%210%
get_SizeY()100%210%
get_Stride()100%210%
AsArray()100%210%
AsJaggedArray()100%210%
Update(...)100%210%
Populate(...)0%7280%
Initialize(...)100%210%

File(s)

D:\a\reflex\reflex\library\src\Core\Common\Components\VectorField2.cs

#LineLine coverage
 1using System;
 2using System.Threading.Tasks;
 3using ReFlex.Core.Common.Interfaces;
 4
 5namespace ReFlex.Core.Common.Components
 6{
 7  /// <inheritdoc />
 8  /// <summary>
 9  /// A ordered Quantity of <see cref="Vector2" />
 10  /// </summary>
 11  public class VectorField2 : IDepthImage<Vector2>
 12  {
 013    private static readonly float _interpolationStrength = 0.7f;
 14
 015    private bool _hasProcessedValues = false;
 16
 17    private Vector2[][] _values2D;
 18    private Vector2[] _values1D;
 19    private Vector2[,] _lastProcessedValues;
 20
 21    /// <inheritdoc />
 22    /// <summary>
 23    /// Gets or sets the <see cref="Vector2" /> at the specified index.
 24    /// </summary>
 25    /// <value>
 26    /// The <see cref="Vector2" />.
 27    /// </value>
 28    /// <param name="index">The index.</param>
 29    /// <returns></returns>
 30    public Vector2 this[int index]
 31    {
 32      get
 033      {
 034        if (index < 0 || index >= Size)
 035          return new Vector2();
 036        return _values1D[index] ?? (_values1D[index] = new Vector2());
 037      }
 038      set => _values1D[index]?.Set(value);
 39    }
 40
 41    /// <inheritdoc />
 42    /// <summary>
 43    /// Gets or sets the <see cref="Vector2" /> with the specified x.
 44    /// </summary>
 45    /// <value>
 46    /// The <see cref="Vector2" />.
 47    /// </value>
 48    /// <param name="x">The x.</param>
 49    /// <param name="y">The y.</param>
 50    /// <returns></returns>
 51    public Vector2 this[int x, int y]
 52    {
 053      get => _values2D[x][y] ?? (_values2D[x][y] = new Vector2());
 054      set => _values2D[x][y]?.Set(value);
 55    }
 56
 57    /// <inheritdoc />
 58    /// <summary>
 59    /// Gets the size.
 60    /// </summary>
 61    /// <value>
 62    /// The size.
 63    /// </value>
 064    public int Size { get; private set; }
 65
 66    /// <inheritdoc />
 67    /// <summary>
 68    /// Gets the horizontal size.
 69    /// </summary>
 70    /// <value>
 71    /// The horizontal size.
 72    /// </value>
 073    public int SizeX { get; private set; }
 74
 75    /// <inheritdoc />
 76    /// <summary>
 77    /// Gets the vertical size.
 78    /// </summary>
 79    /// <value>
 80    /// The vertical size.
 81    /// </value>
 082    public int SizeY { get; private set; }
 83
 84    /// <summary>
 85    /// Gets the stride.
 86    /// </summary>
 87    /// <value>
 88    /// The stride.
 89    /// </value>
 090    public int Stride { get; }
 91
 92    /// <inheritdoc />
 93    /// <summary>
 94    /// Get [this] instance as an array.
 95    /// </summary>
 96    /// <returns>
 97    /// [this] instance as an array
 98    /// </returns>
 099    public Vector2[] AsArray() => _values1D;
 100
 101    /// <inheritdoc />
 102    /// <summary>
 103    /// Get [this] instance as an jagged array
 104    /// </summary>
 105    /// <returns>
 106    /// [this] instance as an jagged array
 107    /// </returns>
 0108    public Vector2[][] AsJaggedArray() => _values2D;
 109
 110    /// <summary>
 111    /// Initializes a new instance of the <see cref="VectorField2" /> class.
 112    /// </summary>
 113    /// <param name="sizeX">The size x.</param>
 114    /// <param name="sizeY">The size y.</param>
 115    /// <param name="stride">The stride.</param>
 0116    public VectorField2(int sizeX, int sizeY, int stride = 1)
 0117    {
 0118      Initialize(sizeX, sizeY);
 0119      Stride = stride > 0 ? stride : 1;
 0120    }
 121
 122    /// <inheritdoc />
 123    /// <summary>
 124    /// Updates [this] instance with the data of the specified source.
 125    /// </summary>
 126    /// <param name="source">The source value.</param>
 127    public void Update(ReadOnlyMemory<Vector2> source, float defualtZ)
 0128    {
 0129      Parallel.For(0, source.Length, i =>
 0130          {
 0131            _values1D[i].Set(source.Span[i]);
 0132          }
 0133      );
 0134    }
 135
 136    /// <summary>
 137    /// Populates the vectorfield.
 138    /// </summary>
 139    /// <param name="source">The source.</param>
 140    public void Populate(PointCloud3 source)
 0141    {
 0142      var pointcloud = source.AsJaggedArray();
 143
 0144      for (var y = Stride; y < SizeY - Stride; ++y)
 0145      {
 0146        for (var x = Stride; x < SizeX - Stride; ++x)
 0147        {
 0148          if (!pointcloud[x][y].IsValid)
 0149          {
 0150            _values2D[x][y].IsValid = false;
 0151            _values2D[x][y].Set(0, 0);
 0152            continue;
 153          }
 154          else
 0155            _values2D[x][y].IsValid = true;
 156
 0157          var x1 = pointcloud[x - Stride][y];
 0158          var x2 = pointcloud[x + Stride][y];
 0159          var y1 = pointcloud[x][y - Stride];
 0160          var y2 = pointcloud[x][y + Stride];
 161
 0162          var deltaX = x1.Z - x2.Z;
 0163          var deltaY = y1.Z - y2.Z;
 164
 0165          var dX = x2.X - x1.X;
 0166          var dY = y2.Y - y1.Y;
 167
 0168          deltaX /= dX;
 0169          deltaY /= dY;
 170
 0171          if (!_hasProcessedValues)
 0172          {
 0173            _values2D[x][y].Set(deltaX, deltaY);
 0174          }
 175          else
 0176          {
 0177            var exisitingValue = _lastProcessedValues[x, y];
 0178            var interpolatedX = deltaX * _interpolationStrength + (1f - _interpolationStrength) * exisitingValue.X;
 0179            var interpolatedY = deltaY * _interpolationStrength + (1f - _interpolationStrength) * exisitingValue.Y;
 0180            _values2D[x][y].Set(interpolatedX, interpolatedY);
 0181            _lastProcessedValues[x, y].Set(interpolatedX, interpolatedY);
 0182          }
 0183        }
 0184      }
 185
 0186      _hasProcessedValues = true;
 0187    }
 188
 189    /// <summary>
 190    /// Initializes the specified size y.
 191    /// </summary>
 192    /// <param name="sizeX">The size x.</param>
 193    /// <param name="sizeY">The size y.</param>
 194    private void Initialize(int sizeX, int sizeY)
 0195    {
 0196      SizeX = sizeX;
 0197      SizeY = sizeY;
 0198      Size = sizeX * sizeY;
 199
 0200      ArrayUtils.InitializeArray(out _values1D, Size);
 0201      ArrayUtils.InitializeArray(out _values2D, SizeX, SizeY);
 0202      ArrayUtils.ReferencingArrays(_values1D, _values2D);
 203
 0204      ArrayUtils.InitializeArray(out _lastProcessedValues, SizeX, SizeY);
 0205      _hasProcessedValues = false;
 0206    }
 207  }
 208}