| | 1 | | using ReFlex.Core.Common.Components; |
| | 2 | | using ReFlex.Core.Common.Interfaces; |
| | 3 | |
|
| | 4 | | namespace PointCloud.Benchmark.Common |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// A ordered Quantity of <see cref="Point3" /> |
| | 8 | | /// </summary> |
| | 9 | | /// <seealso cref="IPointCloud{Point3}" /> |
| | 10 | | /// <inheritdoc /> |
| | 11 | | public class PointCloud3 : IDepthImage<Point3> |
| | 12 | | { |
| | 13 | | #region fields |
| | 14 | |
|
| | 15 | | private Point3[] _values1D; |
| | 16 | | private Point3[][] _values2D; |
| | 17 | |
|
| | 18 | | #endregion |
| | 19 | |
|
| | 20 | | #region indexer |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Gets or sets the <see cref="Point3" /> at the specified index. |
| | 24 | | /// </summary> |
| | 25 | | /// <value> |
| | 26 | | /// The <see cref="Point3" />. |
| | 27 | | /// </value> |
| | 28 | | /// <param name="index">The index.</param> |
| | 29 | | /// <returns></returns> |
| | 30 | | /// <inheritdoc /> |
| | 31 | | public Point3 this[int index] |
| | 32 | | { |
| | 33 | | get |
| 2109536 | 34 | | { |
| 2109536 | 35 | | if(index < 0 || index >= Size) |
| 2 | 36 | | return new Point3(); |
| 2109534 | 37 | | return _values1D[index] ?? (_values1D[index] = new Point3()); |
| 2109536 | 38 | | } |
| 0 | 39 | | set => _values1D[index]?.Set(value); |
| | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets or sets the <see cref="Point3" /> with the specified x. |
| | 44 | | /// </summary> |
| | 45 | | /// <value> |
| | 46 | | /// The <see cref="Point3" />. |
| | 47 | | /// </value> |
| | 48 | | /// <param name="x">The x.</param> |
| | 49 | | /// <param name="y">The y.</param> |
| | 50 | | /// <returns></returns> |
| | 51 | | /// <inheritdoc /> |
| | 52 | | public Point3 this[int x, int y] |
| | 53 | | { |
| 1060962 | 54 | | get => _values2D[x][y] ?? (_values2D[x][y] = new Point3()); |
| 0 | 55 | | set => _values2D[x][y]?.Set(value); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | #endregion |
| | 59 | |
|
| | 60 | | #region properties |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets the size. |
| | 64 | | /// </summary> |
| | 65 | | /// <value> |
| | 66 | | /// The size. |
| | 67 | | /// </value> |
| | 68 | | /// <inheritdoc /> |
| 2160162 | 69 | | public int Size { get; private set; } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Gets the horizontal size. |
| | 73 | | /// </summary> |
| | 74 | | /// <value> |
| | 75 | | /// The horizontal size. |
| | 76 | | /// </value> |
| | 77 | | /// <inheritdoc /> |
| 2630 | 78 | | public int SizeX { get; private set; } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Gets the vertical size. |
| | 82 | | /// </summary> |
| | 83 | | /// <value> |
| | 84 | | /// The vertical size. |
| | 85 | | /// </value> |
| | 86 | | /// <inheritdoc /> |
| 2226263 | 87 | | public int SizeY { get; private set; } |
| | 88 | |
|
| | 89 | | #endregion |
| | 90 | |
|
| | 91 | | #region constructor |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Initializes a new instance of the <see cref="PointCloud3" /> class. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="sizeX">The size x.</param> |
| | 97 | | /// <param name="sizeY">The size y.</param> |
| 24 | 98 | | public PointCloud3(int sizeX, int sizeY) => Initialize(sizeX, sizeY); |
| | 99 | |
|
| | 100 | | #endregion |
| | 101 | |
|
| | 102 | | #region methods |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Updates [this] instance with the data of the specified source. |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="source">The source value.</param> |
| | 108 | | /// <inheritdoc /> |
| | 109 | | public void Update(Point3[] source) |
| 12 | 110 | | { |
| 13190136 | 111 | | for (var i = 0; i < source.Length; ++i) { |
| 4396704 | 112 | | _values1D[i].Set(source[i]); |
| 4396704 | 113 | | } |
| 12 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Updates [this] instance with the data of the specified source. |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="source">The source value.</param> |
| | 120 | | /// <inheritdoc /> |
| | 121 | | public void Update(ReadOnlySpan<Point3> source) |
| 0 | 122 | | { |
| 0 | 123 | | for (var i = 0; i < source.Length; ++i) { |
| 0 | 124 | | _values1D[i].Set(source[i]); |
| 0 | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | /// <summary> |
| | 129 | | /// Updates [this] instance with the data of the specified source. |
| | 130 | | /// </summary> |
| | 131 | | /// <param name="source">The source value.</param> |
| | 132 | | /// <inheritdoc /> |
| | 133 | | public void Update2(ReadOnlySpan<Point3> source) |
| 0 | 134 | | { |
| 0 | 135 | | var valSpan = _values1D.AsSpan(); |
| | 136 | |
|
| 0 | 137 | | for (var i = 0; i < source.Length; ++i) { |
| 0 | 138 | | valSpan[i].Set(source[i]); |
| 0 | 139 | | } |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Updates [this] instance with the data of the specified source. |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="source">The source value.</param> |
| | 146 | | /// <inheritdoc /> |
| | 147 | | public void UpdateParallel(ReadOnlyMemory<Point3> source) |
| 0 | 148 | | { |
| 0 | 149 | | Parallel.For(0, source.Length, i => |
| 0 | 150 | | // for (var i = 0; i < source.Length; ++i) |
| 0 | 151 | | { |
| 0 | 152 | | _values1D[i].Set(source.Span[i]); |
| 0 | 153 | | } |
| 0 | 154 | | ); |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | /// <summary> |
| | 158 | | /// Updates [this] instance with the data of the specified source. |
| | 159 | | /// </summary> |
| | 160 | | /// <param name="source">The source value.</param> |
| | 161 | | /// <inheritdoc /> |
| | 162 | | public void Update(ReadOnlyMemory<Point3> source) |
| 0 | 163 | | { |
| 0 | 164 | | for (var i = 0; i < source.Length; ++i) |
| 0 | 165 | | { |
| 0 | 166 | | _values1D[i].Set(source.Span[i]); |
| 0 | 167 | | } |
| | 168 | |
|
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// Updates [this] instance with the data of the specified source. |
| | 173 | | /// </summary> |
| | 174 | | /// <param name="source">The source value.</param> |
| | 175 | | /// <inheritdoc /> |
| | 176 | | [Obsolete("Benchmark showed that this is 3 times slower than Update() method")] |
| | 177 | | public void UpdateParallel(Point3[] source) |
| 0 | 178 | | { |
| 0 | 179 | | Parallel.For(0, source.Length, (i) => |
| 0 | 180 | | { |
| 0 | 181 | | _values1D[i].Set(source[i]); |
| 0 | 182 | | }); |
| | 183 | |
|
| 0 | 184 | | Array.Copy(source, _values1D, source.Length); |
| 0 | 185 | | ArrayUtils.ReferencingArrays(_values1D, _values2D); |
| | 186 | |
|
| | 187 | | // for (int i = 0; i < source.Length; ++i) { |
| | 188 | | // _values1D[i].Set(source[i]); |
| | 189 | | // } |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// Initializes the specified size y. |
| | 194 | | /// </summary> |
| | 195 | | /// <param name="sizeX">The size x.</param> |
| | 196 | | /// <param name="sizeY">The size y.</param> |
| | 197 | | public void Initialize(int sizeX, int sizeY) |
| 12 | 198 | | { |
| 12 | 199 | | SizeX = sizeX; |
| 12 | 200 | | SizeY = sizeY; |
| 12 | 201 | | Size = sizeX * sizeY; |
| | 202 | |
|
| 12 | 203 | | ArrayUtils.InitializeArray(out _values1D, Size); |
| 12 | 204 | | ArrayUtils.InitializeArray(out _values2D, SizeX, sizeY); |
| 12 | 205 | | ArrayUtils.ReferencingArrays(_values1D, _values2D); |
| 12 | 206 | | } |
| | 207 | |
|
| 3 | 208 | | public Point3[] AsArray() => _values1D; |
| | 209 | |
|
| 2 | 210 | | public Span<Point3> AsSpan() => _values1D.AsSpan(); |
| | 211 | |
|
| 0 | 212 | | public Memory<Point3> AsMemory() => _values1D.AsMemory(); |
| | 213 | |
|
| 3 | 214 | | public Point3[][] AsJaggedArray() => _values2D; |
| | 215 | | public void Update(ReadOnlyMemory<Point3> source, float defaultZ) |
| 0 | 216 | | { |
| 0 | 217 | | Update(source); |
| 0 | 218 | | } |
| | 219 | |
|
| | 220 | | #endregion |
| | 221 | | } |
| | 222 | | } |