| | 1 | | using System; |
| | 2 | | using System.Threading.Tasks; |
| | 3 | | using ReFlex.Core.Common.Interfaces; |
| | 4 | |
|
| | 5 | | namespace 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 | | { |
| 0 | 13 | | private static readonly float _interpolationStrength = 0.7f; |
| | 14 | |
|
| 0 | 15 | | 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 |
| 0 | 33 | | { |
| 0 | 34 | | if (index < 0 || index >= Size) |
| 0 | 35 | | return new Vector2(); |
| 0 | 36 | | return _values1D[index] ?? (_values1D[index] = new Vector2()); |
| 0 | 37 | | } |
| 0 | 38 | | 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 | | { |
| 0 | 53 | | get => _values2D[x][y] ?? (_values2D[x][y] = new Vector2()); |
| 0 | 54 | | 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> |
| 0 | 64 | | 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> |
| 0 | 73 | | 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> |
| 0 | 82 | | public int SizeY { get; private set; } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Gets the stride. |
| | 86 | | /// </summary> |
| | 87 | | /// <value> |
| | 88 | | /// The stride. |
| | 89 | | /// </value> |
| 0 | 90 | | 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> |
| 0 | 99 | | 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> |
| 0 | 108 | | 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> |
| 0 | 116 | | public VectorField2(int sizeX, int sizeY, int stride = 1) |
| 0 | 117 | | { |
| 0 | 118 | | Initialize(sizeX, sizeY); |
| 0 | 119 | | Stride = stride > 0 ? stride : 1; |
| 0 | 120 | | } |
| | 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) |
| 0 | 128 | | { |
| 0 | 129 | | Parallel.For(0, source.Length, i => |
| 0 | 130 | | { |
| 0 | 131 | | _values1D[i].Set(source.Span[i]); |
| 0 | 132 | | } |
| 0 | 133 | | ); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Populates the vectorfield. |
| | 138 | | /// </summary> |
| | 139 | | /// <param name="source">The source.</param> |
| | 140 | | public void Populate(PointCloud3 source) |
| 0 | 141 | | { |
| 0 | 142 | | var pointcloud = source.AsJaggedArray(); |
| | 143 | |
|
| 0 | 144 | | for (var y = Stride; y < SizeY - Stride; ++y) |
| 0 | 145 | | { |
| 0 | 146 | | for (var x = Stride; x < SizeX - Stride; ++x) |
| 0 | 147 | | { |
| 0 | 148 | | if (!pointcloud[x][y].IsValid) |
| 0 | 149 | | { |
| 0 | 150 | | _values2D[x][y].IsValid = false; |
| 0 | 151 | | _values2D[x][y].Set(0, 0); |
| 0 | 152 | | continue; |
| | 153 | | } |
| | 154 | | else |
| 0 | 155 | | _values2D[x][y].IsValid = true; |
| | 156 | |
|
| 0 | 157 | | var x1 = pointcloud[x - Stride][y]; |
| 0 | 158 | | var x2 = pointcloud[x + Stride][y]; |
| 0 | 159 | | var y1 = pointcloud[x][y - Stride]; |
| 0 | 160 | | var y2 = pointcloud[x][y + Stride]; |
| | 161 | |
|
| 0 | 162 | | var deltaX = x1.Z - x2.Z; |
| 0 | 163 | | var deltaY = y1.Z - y2.Z; |
| | 164 | |
|
| 0 | 165 | | var dX = x2.X - x1.X; |
| 0 | 166 | | var dY = y2.Y - y1.Y; |
| | 167 | |
|
| 0 | 168 | | deltaX /= dX; |
| 0 | 169 | | deltaY /= dY; |
| | 170 | |
|
| 0 | 171 | | if (!_hasProcessedValues) |
| 0 | 172 | | { |
| 0 | 173 | | _values2D[x][y].Set(deltaX, deltaY); |
| 0 | 174 | | } |
| | 175 | | else |
| 0 | 176 | | { |
| 0 | 177 | | var exisitingValue = _lastProcessedValues[x, y]; |
| 0 | 178 | | var interpolatedX = deltaX * _interpolationStrength + (1f - _interpolationStrength) * exisitingValue.X; |
| 0 | 179 | | var interpolatedY = deltaY * _interpolationStrength + (1f - _interpolationStrength) * exisitingValue.Y; |
| 0 | 180 | | _values2D[x][y].Set(interpolatedX, interpolatedY); |
| 0 | 181 | | _lastProcessedValues[x, y].Set(interpolatedX, interpolatedY); |
| 0 | 182 | | } |
| 0 | 183 | | } |
| 0 | 184 | | } |
| | 185 | |
|
| 0 | 186 | | _hasProcessedValues = true; |
| 0 | 187 | | } |
| | 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) |
| 0 | 195 | | { |
| 0 | 196 | | SizeX = sizeX; |
| 0 | 197 | | SizeY = sizeY; |
| 0 | 198 | | Size = sizeX * sizeY; |
| | 199 | |
|
| 0 | 200 | | ArrayUtils.InitializeArray(out _values1D, Size); |
| 0 | 201 | | ArrayUtils.InitializeArray(out _values2D, SizeX, SizeY); |
| 0 | 202 | | ArrayUtils.ReferencingArrays(_values1D, _values2D); |
| | 203 | |
|
| 0 | 204 | | ArrayUtils.InitializeArray(out _lastProcessedValues, SizeX, SizeY); |
| 0 | 205 | | _hasProcessedValues = false; |
| 0 | 206 | | } |
| | 207 | | } |
| | 208 | | } |