| | 1 | | using System.Diagnostics; |
| | 2 | | using ReFlex.Core.Common.Components; |
| | 3 | | using ReFlex.Core.Common.Util; |
| | 4 | | using ArrayUtils = PointCloud.Benchmark.Common.ArrayUtils; |
| | 5 | | using Math = System.Math; |
| | 6 | |
|
| | 7 | | namespace PointCloud.Benchmark.Interactivity |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Finds multiple Interactions on a 2d vectorfield. |
| | 11 | | /// </summary> |
| | 12 | | public class MultiInteractionObserver : InteractionObserverBase |
| | 13 | | { |
| | 14 | | #region Fields |
| | 15 | |
|
| | 16 | | private PointCloud3 _pointCloud; |
| | 17 | | private VectorField2 _vectorField; |
| | 18 | | private int[][] _confidenceMat; |
| | 19 | | private List<Interaction> _candidates; |
| | 20 | |
|
| 0 | 21 | | private readonly Stopwatch _stopWatch = new(); |
| | 22 | |
|
| | 23 | |
|
| | 24 | | #endregion |
| | 25 | |
|
| | 26 | | #region Properties |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Gets the type. |
| | 30 | | /// </summary> |
| | 31 | | /// <value> |
| | 32 | | /// The type. |
| | 33 | | /// </value> |
| 0 | 34 | | public override ObserverType Type => ObserverType.MultiTouch; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets or sets the point cloud. |
| | 38 | | /// </summary> |
| | 39 | | /// <value> |
| | 40 | | /// The point cloud. |
| | 41 | | /// </value> |
| | 42 | | public override PointCloud3 PointCloud |
| | 43 | | { |
| 0 | 44 | | get => _pointCloud; |
| | 45 | | set |
| 0 | 46 | | { |
| 0 | 47 | | if (_pointCloud == value) |
| 0 | 48 | | return; |
| | 49 | |
|
| 0 | 50 | | _pointCloud = value; |
| 0 | 51 | | InitializeConfidenceMatrix(); |
| 0 | 52 | | } |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Gets or sets the vector field. |
| | 57 | | /// </summary> |
| | 58 | | /// <value> |
| | 59 | | /// The vector field. |
| | 60 | | /// </value> |
| | 61 | | public override VectorField2 VectorField |
| | 62 | | { |
| 0 | 63 | | get => _vectorField; |
| | 64 | | set |
| 0 | 65 | | { |
| 0 | 66 | | if (_vectorField == value) |
| 0 | 67 | | return; |
| | 68 | |
|
| 0 | 69 | | _vectorField = value; |
| 0 | 70 | | InitializeConfidenceMatrix(); |
| 0 | 71 | | } |
| | 72 | | } |
| | 73 | |
|
| | 74 | | #endregion |
| | 75 | |
|
| | 76 | | #region Events |
| | 77 | | public override event EventHandler<IList<Interaction>> NewInteractions; |
| | 78 | |
|
| | 79 | | #endregion |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Initializes a new instance of the <see cref="MultiInteractionObserver"/> class. |
| | 83 | | /// </summary> |
| 0 | 84 | | public MultiInteractionObserver() |
| 0 | 85 | | { |
| 0 | 86 | | _candidates = new List<Interaction>(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Updates this instance. |
| | 91 | | /// </summary> |
| | 92 | | public override Task<ProcessingResult> Update() |
| 0 | 93 | | { |
| 0 | 94 | | if (PointCloud == null || |
| 0 | 95 | | VectorField == null || |
| 0 | 96 | | _confidenceMat == null) |
| 0 | 97 | | return Task.FromResult(new ProcessingResult(ProcessServiceStatus.Error)); |
| | 98 | |
|
| 0 | 99 | | var processResult = new ProcessingResult(ProcessServiceStatus.Available); |
| | 100 | |
|
| 0 | 101 | | var perfItem = new ProcessPerformance(); |
| 0 | 102 | | if (MeasurePerformance) |
| 0 | 103 | | { |
| 0 | 104 | | _stopWatch.Start(); |
| 0 | 105 | | } |
| | 106 | |
|
| 0 | 107 | | UpdateVectorfield(); |
| | 108 | |
|
| 0 | 109 | | _candidates.Clear(); |
| | 110 | |
|
| 0 | 111 | | if (MeasurePerformance) |
| 0 | 112 | | { |
| 0 | 113 | | _stopWatch.Stop(); |
| 0 | 114 | | perfItem.Preparation = _stopWatch.Elapsed; |
| 0 | 115 | | _stopWatch.Reset(); |
| 0 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | if (MeasurePerformance) |
| 0 | 119 | | { |
| 0 | 120 | | _stopWatch.Start(); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | |
|
| | 124 | |
|
| 0 | 125 | | if (MeasurePerformance) |
| 0 | 126 | | { |
| 0 | 127 | | _stopWatch.Stop(); |
| 0 | 128 | | perfItem.Update = _stopWatch.Elapsed; |
| 0 | 129 | | _stopWatch.Reset(); |
| 0 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | if (MeasurePerformance) |
| 0 | 133 | | { |
| 0 | 134 | | _stopWatch.Start(); |
| 0 | 135 | | } |
| | 136 | |
|
| 0 | 137 | | var interactions = ConvertDepthValue(_candidates.ToList()); |
| | 138 | |
|
| 0 | 139 | | if (MeasurePerformance) |
| 0 | 140 | | { |
| 0 | 141 | | _stopWatch.Stop(); |
| 0 | 142 | | perfItem.ConvertDepthValue = _stopWatch.Elapsed; |
| 0 | 143 | | _stopWatch.Reset(); |
| 0 | 144 | | } |
| | 145 | |
|
| 0 | 146 | | if (MeasurePerformance) |
| 0 | 147 | | { |
| 0 | 148 | | _stopWatch.Start(); |
| 0 | 149 | | } |
| | 150 | |
|
| 0 | 151 | | var frame = ComputeSmoothingValue(interactions); |
| | 152 | |
|
| 0 | 153 | | if (MeasurePerformance) |
| 0 | 154 | | { |
| 0 | 155 | | _stopWatch.Stop(); |
| 0 | 156 | | perfItem.Smoothing = _stopWatch.Elapsed; |
| 0 | 157 | | _stopWatch.Reset(); |
| 0 | 158 | | } |
| | 159 | |
|
| 0 | 160 | | var confidentInteractions = ApplyConfidenceFilter(frame.Interactions); |
| | 161 | |
|
| 0 | 162 | | if (MeasurePerformance) |
| 0 | 163 | | { |
| 0 | 164 | | _stopWatch.Start(); |
| 0 | 165 | | } |
| | 166 | |
|
| 0 | 167 | | var processedInteractions = ComputeExtremumType(confidentInteractions.ToList(), PointCloud.AsJaggedArray()); |
| | 168 | |
|
| 0 | 169 | | if (MeasurePerformance) |
| 0 | 170 | | { |
| 0 | 171 | | _stopWatch.Stop(); |
| 0 | 172 | | perfItem.ComputeExtremumType = _stopWatch.Elapsed; |
| 0 | 173 | | _stopWatch.Reset(); |
| 0 | 174 | | } |
| | 175 | |
|
| 0 | 176 | | UpdatePerformanceMetrics(perfItem); |
| | 177 | |
|
| | 178 | |
|
| 0 | 179 | | OnNewInteractions(processedInteractions); |
| | 180 | |
|
| 0 | 181 | | return Task.FromResult(processResult); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// Initializes the confidence matrix. |
| | 186 | | /// </summary> |
| | 187 | | private void InitializeConfidenceMatrix() |
| 0 | 188 | | { |
| 0 | 189 | | if (VectorField != null) |
| 0 | 190 | | ArrayUtils.InitializeArray(out _confidenceMat, VectorField.SizeX, VectorField.SizeY); |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | public void UpdateVectorfield() |
| 0 | 194 | | { |
| 0 | 195 | | var stride = VectorField.Stride; |
| 0 | 196 | | var pointCloud = PointCloud.AsJaggedArray(); |
| 0 | 197 | | var vectorField = VectorField.AsJaggedArray(); |
| | 198 | |
|
| 0 | 199 | | for (var x = stride; x < VectorField.SizeX - stride; x++) |
| | 200 | | // Parallel.ForEach(Iterate(stride, VectorField.SizeX - stride, stride), (x) => |
| 0 | 201 | | { |
| 0 | 202 | | for (var y = stride; y < VectorField.SizeY - stride; y++) |
| 0 | 203 | | { |
| 0 | 204 | | if (!vectorField[x][y].IsValid) |
| 0 | 205 | | continue; |
| | 206 | |
|
| 0 | 207 | | var vectorX = vectorField[x][y].Add(vectorField[x + stride][y]); |
| 0 | 208 | | var vectorX2 = vectorField[x][y].Add(vectorField[x - stride][y]); |
| 0 | 209 | | var vectorY = vectorField[x][y].Add(vectorField[x][y + stride]); |
| 0 | 210 | | var vectorY2 = vectorField[x][y].Add(vectorField[x][y - stride]); |
| | 211 | |
|
| 0 | 212 | | var angleX = vectorX.AngleBetween(vectorX2); |
| 0 | 213 | | var angleY = vectorY.AngleBetween(vectorY2); |
| 0 | 214 | | var angle = angleX + angleY / 2; |
| | 215 | |
|
| 0 | 216 | | var confidence = _confidenceMat[x][y]; |
| 0 | 217 | | if (float.IsNaN(angle) || angle > MinAngle) |
| 0 | 218 | | { |
| 0 | 219 | | if (confidence > 0) |
| 0 | 220 | | _confidenceMat[x][y]--; |
| 0 | 221 | | } |
| | 222 | | else |
| 0 | 223 | | { |
| 0 | 224 | | _candidates.Add(new Interaction(new Point3(x, y, pointCloud[x][y].Z), InteractionType.None, |
| 0 | 225 | | confidence)); |
| | 226 | |
|
| 0 | 227 | | if (confidence < MaxConfidence) |
| 0 | 228 | | _confidenceMat[x][y]++; |
| 0 | 229 | | } |
| 0 | 230 | | } |
| 0 | 231 | | } |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | public void UpdateVectorfieldParallel() |
| 0 | 235 | | { |
| 0 | 236 | | var stride = VectorField.Stride; |
| 0 | 237 | | var pointCloud = PointCloud.AsJaggedArray(); |
| 0 | 238 | | var vectorField = VectorField.AsJaggedArray(); |
| | 239 | |
|
| | 240 | | // for (var x = stride; x < VectorField.SizeX - stride; x+= stride) |
| 0 | 241 | | Parallel.For(stride, VectorField.SizeX - stride, (x) => |
| 0 | 242 | | { |
| 0 | 243 | | for (var y = stride; y < VectorField.SizeY - stride; y++) |
| 0 | 244 | | { |
| 0 | 245 | | if (!vectorField[x][y].IsValid) |
| 0 | 246 | | continue; |
| 0 | 247 | |
|
| 0 | 248 | | var vectorX = vectorField[x][y].Add(vectorField[x + stride][y]); |
| 0 | 249 | | var vectorX2 = vectorField[x][y].Add(vectorField[x - stride][y]); |
| 0 | 250 | | var vectorY = vectorField[x][y].Add(vectorField[x][y + stride]); |
| 0 | 251 | | var vectorY2 = vectorField[x][y].Add(vectorField[x][y - stride]); |
| 0 | 252 | |
|
| 0 | 253 | | var angleX = vectorX.AngleBetween(vectorX2); |
| 0 | 254 | | var angleY = vectorY.AngleBetween(vectorY2); |
| 0 | 255 | | var angle = angleX + angleY / 2; |
| 0 | 256 | |
|
| 0 | 257 | | var confidence = _confidenceMat[x][y]; |
| 0 | 258 | | if (float.IsNaN(angle) || angle > MinAngle) |
| 0 | 259 | | { |
| 0 | 260 | | if (confidence > 0) |
| 0 | 261 | | _confidenceMat[x][y]--; |
| 0 | 262 | | } |
| 0 | 263 | | else |
| 0 | 264 | | { |
| 0 | 265 | | _candidates.Add(new Interaction(new Point3(x, y, pointCloud[x][y].Z), InteractionType.None, |
| 0 | 266 | | confidence)); |
| 0 | 267 | |
|
| 0 | 268 | | if (confidence < MaxConfidence) |
| 0 | 269 | | _confidenceMat[x][y]++; |
| 0 | 270 | | } |
| 0 | 271 | | } |
| 0 | 272 | | }); |
| 0 | 273 | | } |
| | 274 | |
|
| | 275 | | public void UpdateVectorfieldParallelStepped() |
| 0 | 276 | | { |
| 0 | 277 | | var stride = VectorField.Stride; |
| 0 | 278 | | var pointCloud = PointCloud.AsJaggedArray(); |
| 0 | 279 | | var vectorField = VectorField.AsJaggedArray(); |
| | 280 | |
|
| | 281 | | // for (var x = stride; x < VectorField.SizeX - stride; x+= stride) |
| 0 | 282 | | Parallel.For(1, VectorField.SizeX - stride, (i) => |
| 0 | 283 | | { |
| 0 | 284 | | var x = i * stride; |
| 0 | 285 | |
|
| 0 | 286 | | for (var y = stride; y < VectorField.SizeY - stride; y += stride) |
| 0 | 287 | | { |
| 0 | 288 | | var vCenter = vectorField[x][y]; |
| 0 | 289 | |
|
| 0 | 290 | | if (!vCenter.IsValid) |
| 0 | 291 | | continue; |
| 0 | 292 | |
|
| 0 | 293 | | var vectorX = vCenter.Add(vectorField[x + stride][y]); |
| 0 | 294 | | var vectorX2 = vCenter.Add(vectorField[x - stride][y]); |
| 0 | 295 | | var vectorY = vCenter.Add(vectorField[x][y + stride]); |
| 0 | 296 | | var vectorY2 = vCenter.Add(vectorField[x][y - stride]); |
| 0 | 297 | |
|
| 0 | 298 | | var angleX = vectorX.AngleBetween(vectorX2); |
| 0 | 299 | | var angleY = vectorY.AngleBetween(vectorY2); |
| 0 | 300 | | var angle = angleX + angleY / 2f; |
| 0 | 301 | |
|
| 0 | 302 | | var confidence = _confidenceMat[x][y]; |
| 0 | 303 | | if (float.IsNaN(angle) || angle > MinAngle) |
| 0 | 304 | | { |
| 0 | 305 | | if (confidence > 0) |
| 0 | 306 | | _confidenceMat[x][y]--; |
| 0 | 307 | | } |
| 0 | 308 | | else |
| 0 | 309 | | { |
| 0 | 310 | | _candidates.Add(new Interaction(new Point3(x, y, pointCloud[x][y].Z), InteractionType.None, |
| 0 | 311 | | confidence)); |
| 0 | 312 | |
|
| 0 | 313 | | if (confidence < MaxConfidence) |
| 0 | 314 | | _confidenceMat[x][y]++; |
| 0 | 315 | | } |
| 0 | 316 | | } |
| 0 | 317 | | }); |
| 0 | 318 | | } |
| | 319 | |
|
| | 320 | | public void UpdateVectorfieldStepped1d() |
| 0 | 321 | | { |
| 0 | 322 | | var stride = VectorField.Stride; |
| 0 | 323 | | var pointCloud = PointCloud.AsSpan(); |
| 0 | 324 | | var vectorField = VectorField.AsArray().AsMemory(); |
| | 325 | |
|
| 0 | 326 | | var width = VectorField.SizeX; |
| | 327 | |
|
| 0 | 328 | | for (var x = stride; x < VectorField.SizeX - stride; x += stride) |
| | 329 | | // Parallel.ForEach(SteppedIterator.SteppedIntegerList(stride, VectorField.SizeX - stride, stride), (x) |
| 0 | 330 | | { |
| 0 | 331 | | for (var y = stride; y < VectorField.SizeY - stride; y += stride) |
| 0 | 332 | | { |
| 0 | 333 | | var idx = y * width + x; |
| | 334 | |
|
| 0 | 335 | | if (!vectorField.Span[idx].IsValid) |
| 0 | 336 | | continue; |
| | 337 | |
|
| | 338 | | // idx = y * width + x + stride == idx + stride |
| 0 | 339 | | var vectorX = vectorField.Span[idx].Add(vectorField.Span[idx + stride]); |
| | 340 | |
|
| | 341 | | // idx = y * width + x + stride == idx + stride |
| 0 | 342 | | var vectorX2 = vectorField.Span[idx].Add(vectorField.Span[idx - stride]); |
| | 343 | |
|
| 0 | 344 | | var idx2 = (y + stride) * width + x; |
| 0 | 345 | | var vectorY = vectorField.Span[idx].Add(vectorField.Span[idx2]); |
| | 346 | |
|
| 0 | 347 | | var idx3 = (y - stride) * width + x; |
| 0 | 348 | | var vectorY2 = vectorField.Span[idx].Add(vectorField.Span[idx3]); |
| | 349 | |
|
| 0 | 350 | | var angleX = vectorX.AngleBetween(vectorX2); |
| 0 | 351 | | var angleY = vectorY.AngleBetween(vectorY2); |
| 0 | 352 | | var angle = angleX + angleY / 2; |
| | 353 | |
|
| 0 | 354 | | var confidence = _confidenceMat[x][y]; |
| 0 | 355 | | if (float.IsNaN(angle) || angle > MinAngle) |
| 0 | 356 | | { |
| 0 | 357 | | if (confidence > 0) |
| 0 | 358 | | _confidenceMat[x][y]--; |
| 0 | 359 | | } |
| | 360 | | else |
| 0 | 361 | | { |
| 0 | 362 | | _candidates.Add(new Interaction(new Point3(x, y, pointCloud[idx].Z), InteractionType.None, |
| 0 | 363 | | confidence)); |
| | 364 | |
|
| 0 | 365 | | if (confidence < MaxConfidence) |
| 0 | 366 | | _confidenceMat[x][y]++; |
| 0 | 367 | | } |
| 0 | 368 | | } |
| 0 | 369 | | } |
| | 370 | | //); |
| 0 | 371 | | } |
| | 372 | |
|
| | 373 | | public void UpdateVectorfieldSteppedParallel1d() |
| 0 | 374 | | { |
| 0 | 375 | | var stride = VectorField.Stride; |
| 0 | 376 | | var pointCloud = PointCloud.AsMemory(); |
| 0 | 377 | | var vectorField = VectorField.AsArray().AsMemory(); |
| | 378 | |
|
| 0 | 379 | | var width = VectorField.SizeX; |
| | 380 | |
|
| | 381 | | // for (var x = stride; x < VectorField.SizeX - stride; x+= stride) |
| 0 | 382 | | Parallel.ForEach(SteppedIterator.SteppedIntegerList(stride, VectorField.SizeX - stride, stride), (x) => |
| 0 | 383 | | { |
| 0 | 384 | | for (var y = stride; y < VectorField.SizeY - stride; y += stride) |
| 0 | 385 | | { |
| 0 | 386 | | var idx = y * width + x; |
| 0 | 387 | |
|
| 0 | 388 | | if (!vectorField.Span[idx].IsValid) |
| 0 | 389 | | continue; |
| 0 | 390 | |
|
| 0 | 391 | | var vCenter = vectorField.Span[idx]; |
| 0 | 392 | |
|
| 0 | 393 | | // idx = y * width + x + stride == idx + stride |
| 0 | 394 | | var vectorX = vCenter.Add(vectorField.Span[idx + stride]); |
| 0 | 395 | |
|
| 0 | 396 | | // idx = y * width + x + stride == idx + stride |
| 0 | 397 | | var vectorX2 = vCenter.Add(vectorField.Span[idx - stride]); |
| 0 | 398 | |
|
| 0 | 399 | | var idx2 = (y + stride) * width + x; |
| 0 | 400 | | var vectorY = vCenter.Add(vectorField.Span[idx2]); |
| 0 | 401 | |
|
| 0 | 402 | | var idx3 = (y - stride) * width + x; |
| 0 | 403 | | var vectorY2 = vCenter.Add(vectorField.Span[idx3]); |
| 0 | 404 | |
|
| 0 | 405 | | var angleX = vectorX.AngleBetween(vectorX2); |
| 0 | 406 | | var angleY = vectorY.AngleBetween(vectorY2); |
| 0 | 407 | | var angle = angleX + angleY / 2f; |
| 0 | 408 | |
|
| 0 | 409 | | var confidence = _confidenceMat[x][y]; |
| 0 | 410 | | if (float.IsNaN(angle) || angle > MinAngle) |
| 0 | 411 | | { |
| 0 | 412 | | if (confidence > 0) |
| 0 | 413 | | _confidenceMat[x][y]--; |
| 0 | 414 | | } |
| 0 | 415 | | else |
| 0 | 416 | | { |
| 0 | 417 | | _candidates.Add(new Interaction(new Point3(x, y, pointCloud.Span[idx].Z), InteractionType.None, |
| 0 | 418 | | confidence)); |
| 0 | 419 | |
|
| 0 | 420 | | if (confidence < MaxConfidence) |
| 0 | 421 | | _confidenceMat[x][y]++; |
| 0 | 422 | | } |
| 0 | 423 | | } |
| 0 | 424 | | }); |
| | 425 | | //); |
| 0 | 426 | | } |
| | 427 | |
|
| | 428 | | public IEnumerable<Interaction> ApplyConfidenceFilter1() |
| 0 | 429 | | { |
| 0 | 430 | | return _candidates.Where(item => item.Confidence > MinConfidence); |
| 0 | 431 | | } |
| | 432 | |
|
| | 433 | | public IEnumerable<Interaction> ApplyConfidenceFilter2() |
| 0 | 434 | | { |
| 0 | 435 | | return _candidates.ToArray().Where(item => item.Confidence > MinConfidence); |
| 0 | 436 | | } |
| | 437 | |
|
| | 438 | | public IEnumerable<Interaction> ApplyConfidenceFilter3() |
| 0 | 439 | | { |
| 0 | 440 | | return _candidates.AsReadOnly().Where(item => item.Confidence > MinConfidence); |
| 0 | 441 | | } |
| | 442 | |
|
| | 443 | | public IEnumerable<Interaction> ApplyConfidenceFilter4() |
| 0 | 444 | | { |
| 0 | 445 | | var span = _candidates.ToArray().AsSpan(); |
| 0 | 446 | | var result = new List<Interaction>(); |
| 0 | 447 | | for (var i = 0; i < span.Length; i++) |
| 0 | 448 | | { |
| 0 | 449 | | var item = span[i]; |
| 0 | 450 | | if (item.Confidence > MinConfidence) |
| 0 | 451 | | result.Add(item); |
| 0 | 452 | | } |
| | 453 | |
|
| 0 | 454 | | return result; |
| 0 | 455 | | } |
| | 456 | |
|
| | 457 | | /// <summary> |
| | 458 | | /// Called when [new interactions]. |
| | 459 | | /// </summary> |
| | 460 | | /// <param name="args">The arguments.</param> |
| 0 | 461 | | protected virtual void OnNewInteractions(List<Interaction> args) => NewInteractions?.Invoke(this, args); |
| | 462 | |
|
| | 463 | |
|
| | 464 | | protected override IEnumerable<Interaction> ApplyConfidenceFilter(IEnumerable<Interaction> interactions) |
| 0 | 465 | | { |
| 0 | 466 | | return interactions.Where(item => item.Confidence > MinConfidence); |
| 0 | 467 | | } |
| | 468 | |
|
| | 469 | |
|
| | 470 | |
|
| | 471 | | protected override List<Interaction> ComputeExtremumType(List<Interaction> interactions, Point3[][] pointsArray) |
| 0 | 472 | | { |
| 0 | 473 | | interactions.ForEach(interaction => |
| 0 | 474 | | interaction.ExtremumDescription = ComputeExtremumType(pointsArray, (int)interaction.Position.X, (int)int |
| | 475 | |
|
| 0 | 476 | | return interactions; |
| 0 | 477 | | } |
| | 478 | |
|
| | 479 | | /// <summary> |
| | 480 | | /// Calculates the average distance for the currently stored PointCloud. |
| | 481 | | /// </summary> |
| | 482 | | /// <returns></returns> |
| | 483 | | public override float CalculateAverageDistance() |
| 0 | 484 | | { |
| 0 | 485 | | var sum = 0f; |
| 0 | 486 | | var points = PointCloud?.AsArray(); |
| | 487 | |
|
| 0 | 488 | | if (points == null) |
| 0 | 489 | | return sum; |
| | 490 | |
|
| 0 | 491 | | var numValidSamples = 0; |
| | 492 | |
|
| 0 | 493 | | for (var i = 0; i < points.Length; ++i) |
| 0 | 494 | | { |
| 0 | 495 | | if (!points[i].IsValid || points[i].IsFiltered) |
| 0 | 496 | | continue; |
| | 497 | |
|
| 0 | 498 | | numValidSamples++; |
| 0 | 499 | | sum += points[i].Z; |
| 0 | 500 | | } |
| | 501 | |
|
| 0 | 502 | | return (float) decimal.Round((decimal) (sum / numValidSamples), 2); |
| 0 | 503 | | } |
| | 504 | |
|
| | 505 | | private static Point3 GetSample(Point3[][] pointsArray, int xPos, int yPos, Tuple<int, int>[] samples, int i, in |
| 0 | 506 | | { |
| 0 | 507 | | var xIdx = xPos + samples[i].Item1; |
| 0 | 508 | | xIdx = Math.Min(Math.Max(xIdx, 0), xMax); |
| | 509 | |
|
| 0 | 510 | | var yIdx = yPos + samples[i].Item2; |
| 0 | 511 | | yIdx = Math.Min(Math.Max(yIdx, 0), yMax); |
| 0 | 512 | | var sample = pointsArray[xIdx][yIdx]; |
| | 513 | |
|
| 0 | 514 | | return sample; |
| 0 | 515 | | } |
| | 516 | |
|
| | 517 | | private Tuple<int, int>[] GenerateSamples() |
| 0 | 518 | | { |
| 0 | 519 | | var result = new List<Tuple<int, int>>(); |
| | 520 | |
|
| 0 | 521 | | var rnd = new Random(); |
| | 522 | |
|
| 0 | 523 | | var min = (int)Math.Floor(0.5f * ExtremumTypeCheckRadius); |
| 0 | 524 | | var max = ExtremumTypeCheckRadius; |
| | 525 | |
|
| 0 | 526 | | for (var i = 0; i < ExtremumTypeCheckNumSamples; i++) |
| 0 | 527 | | { |
| 0 | 528 | | var xStochastic = rnd.Next(min, max); |
| 0 | 529 | | var yStochastic = rnd.Next(min, max); |
| | 530 | |
|
| 0 | 531 | | result.Add(new Tuple<int, int>(xStochastic, yStochastic)); |
| 0 | 532 | | } |
| | 533 | |
|
| 0 | 534 | | return result.ToArray(); |
| 0 | 535 | | } |
| | 536 | |
|
| | 537 | | private void UpdateSamples() |
| 0 | 538 | | { |
| 0 | 539 | | var samplesFixed = new List<Tuple<int, int>>(); |
| | 540 | |
|
| 0 | 541 | | for (var i = 0; i < ExtremumTypeCheckNumSamples; i++) |
| 0 | 542 | | { |
| 0 | 543 | | var p = (float)i / ExtremumTypeCheckRadius; |
| 0 | 544 | | var xFixed = (int) Math.Floor(ExtremumTypeCheckRadius * Math.Cos(p * 2 * Math.PI)); |
| 0 | 545 | | var yFixed = (int) Math.Floor(ExtremumTypeCheckRadius * Math.Sin(p * 2 * Math.PI)); |
| | 546 | |
|
| 0 | 547 | | samplesFixed.Add(new Tuple<int, int>(xFixed, yFixed)); |
| 0 | 548 | | } |
| 0 | 549 | | _fixedSamples = samplesFixed.ToArray(); |
| | 550 | |
|
| 0 | 551 | | _stochasticSamples = GenerateSamples(); |
| 0 | 552 | | } |
| | 553 | |
|
| | 554 | | } |
| | 555 | |
|
| | 556 | | } |