| | 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 | | { |
| | 13 | | private Vector2[][] _values2D; |
| | 14 | | private Vector2[] _values1D; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | /// <summary> |
| | 18 | | /// Gets or sets the <see cref="Vector2" /> at the specified index. |
| | 19 | | /// </summary> |
| | 20 | | /// <value> |
| | 21 | | /// The <see cref="Vector2" />. |
| | 22 | | /// </value> |
| | 23 | | /// <param name="index">The index.</param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public Vector2 this[int index] |
| | 26 | | { |
| | 27 | | get |
| 0 | 28 | | { |
| 0 | 29 | | if (index < 0 || index >= Size) |
| 0 | 30 | | return new Vector2(); |
| 0 | 31 | | return _values1D[index] ?? (_values1D[index] = new Vector2()); |
| 0 | 32 | | } |
| 0 | 33 | | set => _values1D[index]?.Set(value); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | /// <inheritdoc /> |
| | 37 | | /// <summary> |
| | 38 | | /// Gets or sets the <see cref="Vector2" /> with the specified x. |
| | 39 | | /// </summary> |
| | 40 | | /// <value> |
| | 41 | | /// The <see cref="Vector2" />. |
| | 42 | | /// </value> |
| | 43 | | /// <param name="x">The x.</param> |
| | 44 | | /// <param name="y">The y.</param> |
| | 45 | | /// <returns></returns> |
| | 46 | | public Vector2 this[int x, int y] |
| | 47 | | { |
| 0 | 48 | | get => _values2D[x][y] ?? (_values2D[x][y] = new Vector2()); |
| 0 | 49 | | set => _values2D[x][y]?.Set(value); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <inheritdoc /> |
| | 53 | | /// <summary> |
| | 54 | | /// Gets the size. |
| | 55 | | /// </summary> |
| | 56 | | /// <value> |
| | 57 | | /// The size. |
| | 58 | | /// </value> |
| 0 | 59 | | public int Size { get; private set; } |
| | 60 | |
|
| | 61 | | /// <inheritdoc /> |
| | 62 | | /// <summary> |
| | 63 | | /// Gets the horizontal size. |
| | 64 | | /// </summary> |
| | 65 | | /// <value> |
| | 66 | | /// The horizontal size. |
| | 67 | | /// </value> |
| 0 | 68 | | public int SizeX { get; private set; } |
| | 69 | |
|
| | 70 | | /// <inheritdoc /> |
| | 71 | | /// <summary> |
| | 72 | | /// Gets the vertical size. |
| | 73 | | /// </summary> |
| | 74 | | /// <value> |
| | 75 | | /// The vertical size. |
| | 76 | | /// </value> |
| 0 | 77 | | public int SizeY { get; private set; } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Gets the stride. |
| | 81 | | /// </summary> |
| | 82 | | /// <value> |
| | 83 | | /// The stride. |
| | 84 | | /// </value> |
| 0 | 85 | | public int Stride { get; } |
| | 86 | |
|
| | 87 | | /// <inheritdoc /> |
| | 88 | | /// <summary> |
| | 89 | | /// Get [this] instance as an array. |
| | 90 | | /// </summary> |
| | 91 | | /// <returns> |
| | 92 | | /// [this] instance as an array |
| | 93 | | /// </returns> |
| 0 | 94 | | public Vector2[] AsArray() => _values1D; |
| | 95 | |
|
| | 96 | | /// <inheritdoc /> |
| | 97 | | /// <summary> |
| | 98 | | /// Get [this] instance as an jagged array |
| | 99 | | /// </summary> |
| | 100 | | /// <returns> |
| | 101 | | /// [this] instance as an jagged array |
| | 102 | | /// </returns> |
| 0 | 103 | | public Vector2[][] AsJaggedArray() => _values2D; |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Initializes a new instance of the <see cref="VectorField2" /> class. |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="sizeX">The size x.</param> |
| | 109 | | /// <param name="sizeY">The size y.</param> |
| | 110 | | /// <param name="stride">The stride.</param> |
| 0 | 111 | | public VectorField2(int sizeX, int sizeY, int stride = 1) |
| 0 | 112 | | { |
| 0 | 113 | | Initialize(sizeX, sizeY); |
| 0 | 114 | | Stride = stride > 0 ? stride : 1; |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <inheritdoc /> |
| | 118 | | /// <summary> |
| | 119 | | /// Updates [this] instance with the data of the specified source. |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="source">The source value.</param> |
| | 122 | | public void Update(ReadOnlyMemory<Vector2> source, float defualtZ) |
| 0 | 123 | | { |
| 0 | 124 | | Parallel.For(0, source.Length, i => |
| 0 | 125 | | { |
| 0 | 126 | | _values1D[i].Set(source.Span[i]); |
| 0 | 127 | | } |
| 0 | 128 | | ); |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// Populates the vectorfield. |
| | 133 | | /// </summary> |
| | 134 | | /// <param name="source">The source.</param> |
| | 135 | | public void Populate(PointCloud3 source) |
| 0 | 136 | | { |
| 0 | 137 | | var pointcloud = source.AsJaggedArray(); |
| | 138 | |
|
| 0 | 139 | | for (var y = Stride; y < SizeY - Stride; ++y) |
| 0 | 140 | | { |
| 0 | 141 | | for (var x = Stride; x < SizeX - Stride; ++x) |
| 0 | 142 | | { |
| 0 | 143 | | if (!pointcloud[x][y].IsValid) |
| 0 | 144 | | { |
| 0 | 145 | | _values2D[x][y].IsValid = false; |
| 0 | 146 | | _values2D[x][y].Set(0, 0); |
| 0 | 147 | | continue; |
| | 148 | | } |
| | 149 | | else |
| 0 | 150 | | _values2D[x][y].IsValid = true; |
| | 151 | |
|
| 0 | 152 | | var x1 = pointcloud[x - Stride][y]; |
| 0 | 153 | | var x2 = pointcloud[x + Stride][y]; |
| 0 | 154 | | var y1 = pointcloud[x][y - Stride]; |
| 0 | 155 | | var y2 = pointcloud[x][y + Stride]; |
| | 156 | |
|
| 0 | 157 | | var deltaX = x1.Z - x2.Z; |
| 0 | 158 | | var deltaY = y1.Z - y2.Z; |
| | 159 | |
|
| 0 | 160 | | _values2D[x][y].Set(deltaX, deltaY); |
| 0 | 161 | | } |
| 0 | 162 | | } |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// Initializes the specified size y. |
| | 167 | | /// </summary> |
| | 168 | | /// <param name="sizeX">The size x.</param> |
| | 169 | | /// <param name="sizeY">The size y.</param> |
| | 170 | | private void Initialize(int sizeX, int sizeY) |
| 0 | 171 | | { |
| 0 | 172 | | SizeX = sizeX; |
| 0 | 173 | | SizeY = sizeY; |
| 0 | 174 | | Size = sizeX * sizeY; |
| | 175 | |
|
| 0 | 176 | | ArrayUtils.InitializeArray(out _values1D, Size); |
| 0 | 177 | | ArrayUtils.InitializeArray(out _values2D, SizeX, SizeY); |
| 0 | 178 | | ArrayUtils.ReferencingArrays(_values1D, _values2D); |
| 0 | 179 | | } |
| | 180 | | } |
| | 181 | | } |