| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using ReFlex.Core.Common.Components; |
| | | 7 | | using ReFlex.Core.Common.Interfaces; |
| | | 8 | | using ReFlex.Core.Common.Util; |
| | | 9 | | using ReFlex.Core.Interactivity.Interfaces; |
| | | 10 | | using ReFlex.Core.Interactivity.Util; |
| | | 11 | | using Math = System.Math; |
| | | 12 | | |
| | | 13 | | namespace ReFlex.Core.Interactivity.Components |
| | | 14 | | { |
| | | 15 | | public abstract class InteractionObserverBase : IInteractionObserver, IPerformanceReporter |
| | | 16 | | { |
| | | 17 | | #region Fields |
| | | 18 | | |
| | 46 | 19 | | private readonly Stopwatch _stopWatch = new(); |
| | | 20 | | |
| | | 21 | | private readonly InteractionSmoothingBehaviour _smoothingBehaviour; |
| | 46 | 22 | | private int _numSmoothingFrames = 5; |
| | 46 | 23 | | private int _historySize = 25; |
| | 46 | 24 | | private int _maxNumEmptyFramesBetween = 3; |
| | 46 | 25 | | private float _touchMergeDistance2d = 64f; |
| | 46 | 26 | | private float _depthScale = 100.0f; |
| | | 27 | | private FilterType _filterType; |
| | | 28 | | private float _maxConfidence; |
| | | 29 | | private int _extremumTypeCheckRadius; |
| | | 30 | | private int _extremumTypeCheckNumSamples; |
| | | 31 | | |
| | 46 | 32 | | private Tuple<int, int>[] _fixedSamples = Array.Empty<Tuple<int, int>>(); |
| | 46 | 33 | | private Tuple<int, int>[] _stochasticSamples = Array.Empty<Tuple<int, int>>(); |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Properties |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or sets the distance. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <value> |
| | | 43 | | /// The distance. |
| | | 44 | | /// </value> |
| | 238 | 45 | | public float Distance { get; set; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets or sets the minimum depth. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <value> |
| | | 51 | | /// The maximum depth. |
| | | 52 | | /// </value> |
| | 180 | 53 | | public float MinDistance { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets or sets the maximum depth. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <value> |
| | | 59 | | /// The maximum depth. |
| | | 60 | | /// </value> |
| | 82 | 61 | | public float MaxDistance { get; set; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// defines how many points should be checked to determine the type of the extremum |
| | | 65 | | /// </summary> |
| | | 66 | | public int ExtremumTypeCheckNumSamples |
| | | 67 | | { |
| | 1080 | 68 | | get => _extremumTypeCheckNumSamples; |
| | | 69 | | set |
| | 38 | 70 | | { |
| | 38 | 71 | | if (_extremumTypeCheckNumSamples == value) |
| | 0 | 72 | | return; |
| | 38 | 73 | | _extremumTypeCheckNumSamples = value; |
| | 38 | 74 | | UpdateSamples(); |
| | 38 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// The pixel radius in which the points are sampled for deriving the type of the local extremum |
| | | 80 | | /// </summary> |
| | | 81 | | public int ExtremumTypeCheckRadius |
| | | 82 | | { |
| | 1312 | 83 | | get => _extremumTypeCheckRadius; |
| | | 84 | | set |
| | 38 | 85 | | { |
| | 38 | 86 | | if (_extremumTypeCheckRadius == value) |
| | 0 | 87 | | return; |
| | 38 | 88 | | _extremumTypeCheckRadius = value; |
| | 38 | 89 | | UpdateSamples(); |
| | 38 | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Which ratio of "correctly aligned" points should match to distinguish between Undefined or Minimum/Maximum ? |
| | | 95 | | /// </summary> |
| | 80 | 96 | | public float ExtremumTypeCheckFittingPercentage { get; set; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Defines how neighboring the points are sampled for determining the type of extremum. |
| | | 100 | | /// </summary> |
| | 134 | 101 | | public ExtremumTypeCheckMethod ExtremumTypeCheckMethod { get; set; } |
| | | 102 | | |
| | | 103 | | public int InteractionHistorySize |
| | | 104 | | { |
| | 46 | 105 | | get => _historySize; |
| | | 106 | | set |
| | 36 | 107 | | { |
| | 36 | 108 | | _historySize = value; |
| | 36 | 109 | | if (_smoothingBehaviour != null) |
| | 36 | 110 | | { |
| | 36 | 111 | | _smoothingBehaviour.NumFramesHistory = value; |
| | | 112 | | // update depending smoothing/filter values |
| | 36 | 113 | | NumSmoothingFrames = _numSmoothingFrames; |
| | 36 | 114 | | MaxNumEmptyFramesBetween = _maxNumEmptyFramesBetween; |
| | 36 | 115 | | } |
| | 36 | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public int NumSmoothingFrames |
| | | 120 | | { |
| | 0 | 121 | | get => _numSmoothingFrames; |
| | | 122 | | set |
| | 72 | 123 | | { |
| | 72 | 124 | | _numSmoothingFrames = value; |
| | 72 | 125 | | if (_smoothingBehaviour != null) |
| | 72 | 126 | | _smoothingBehaviour.NumFramesSmoothing = value < _smoothingBehaviour.NumFramesHistory |
| | 72 | 127 | | ? _smoothingBehaviour.NumFramesHistory |
| | 72 | 128 | | : value; |
| | 72 | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | // TODO: write tests for negative (!) values and values larger than history |
| | | 133 | | public int MaxNumEmptyFramesBetween |
| | | 134 | | { |
| | 0 | 135 | | get => _maxNumEmptyFramesBetween; |
| | | 136 | | set |
| | 44 | 137 | | { |
| | 44 | 138 | | _maxNumEmptyFramesBetween = value; |
| | 44 | 139 | | if (_smoothingBehaviour != null) |
| | 44 | 140 | | _smoothingBehaviour.MaxNumEmptyFramesBetween = value < _smoothingBehaviour.NumFramesHistory |
| | 44 | 141 | | ? _smoothingBehaviour.NumFramesHistory |
| | 44 | 142 | | : value; |
| | 44 | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public float TouchMergeDistance2D |
| | | 147 | | { |
| | 0 | 148 | | get => _touchMergeDistance2d; |
| | | 149 | | set |
| | 36 | 150 | | { |
| | 36 | 151 | | _touchMergeDistance2d = value; |
| | 36 | 152 | | if (_smoothingBehaviour != null) |
| | 36 | 153 | | _smoothingBehaviour.TouchMergeDistance2D = value; |
| | 36 | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | public float DepthScale |
| | | 158 | | { |
| | 0 | 159 | | get => _depthScale; |
| | | 160 | | set |
| | 8 | 161 | | { |
| | 8 | 162 | | _depthScale = value; |
| | 8 | 163 | | if (_smoothingBehaviour != null) |
| | 8 | 164 | | _smoothingBehaviour.DepthScale = value; |
| | 8 | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | public FilterType FilterType |
| | | 169 | | { |
| | 0 | 170 | | get => _filterType; |
| | | 171 | | set |
| | 28 | 172 | | { |
| | 28 | 173 | | _filterType = value; |
| | 28 | 174 | | if (_smoothingBehaviour != null) |
| | 28 | 175 | | _smoothingBehaviour.UpdateFilterType(_filterType); |
| | 28 | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Gets or sets the minimum angle. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <value> |
| | | 184 | | /// The minimum angle. |
| | | 185 | | /// </value> |
| | 8 | 186 | | public virtual float MinAngle { get; set; } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Gets or sets the minimum confidence. |
| | | 190 | | /// </summary> |
| | | 191 | | /// <value> |
| | | 192 | | /// The minimum confidence. |
| | | 193 | | /// </value> |
| | 60 | 194 | | public virtual float MinConfidence { get; set; } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Gets or sets the maximum confidence. |
| | | 198 | | /// </summary> |
| | | 199 | | /// <value> |
| | | 200 | | /// The maximum confidence. |
| | | 201 | | /// </value> |
| | | 202 | | public virtual float MaxConfidence |
| | | 203 | | { |
| | 36 | 204 | | get => _maxConfidence; |
| | | 205 | | set |
| | 36 | 206 | | { |
| | 36 | 207 | | _maxConfidence = value; |
| | 36 | 208 | | if (_smoothingBehaviour != null) |
| | 36 | 209 | | _smoothingBehaviour.MaxConfidence = (int) MaxConfidence; |
| | 36 | 210 | | } |
| | | 211 | | } |
| | | 212 | | |
| | 260 | 213 | | public bool MeasurePerformance { get; set; } |
| | | 214 | | |
| | 36 | 215 | | protected int FrameId { get; private set; } |
| | | 216 | | |
| | | 217 | | #region Abstract Members |
| | | 218 | | |
| | | 219 | | public abstract ObserverType Type { get; } |
| | | 220 | | public abstract PointCloud3 PointCloud { get; set; } |
| | | 221 | | public abstract VectorField2 VectorField { get; set; } |
| | | 222 | | |
| | | 223 | | public event EventHandler<IList<Interaction>> NewInteractions; |
| | | 224 | | |
| | | 225 | | public event EventHandler<PerformanceDataItem> PerformanceDataUpdated; |
| | | 226 | | |
| | | 227 | | public event EventHandler<IList<InteractionFrame>> InteractionHistoryUpdated; |
| | | 228 | | |
| | | 229 | | #endregion |
| | | 230 | | |
| | | 231 | | #region Constructor |
| | | 232 | | |
| | 46 | 233 | | protected InteractionObserverBase() |
| | 46 | 234 | | { |
| | 46 | 235 | | _smoothingBehaviour = new InteractionSmoothingBehaviour(InteractionHistorySize); |
| | 46 | 236 | | } |
| | | 237 | | |
| | | 238 | | #endregion |
| | | 239 | | |
| | | 240 | | public void UpdateFrameId(int frameId) |
| | 0 | 241 | | { |
| | 0 | 242 | | FrameId = frameId; |
| | 0 | 243 | | } |
| | | 244 | | |
| | | 245 | | #endregion |
| | | 246 | | |
| | | 247 | | protected abstract Task<Tuple<IEnumerable<Interaction>, ProcessPerformance>> Analyze(ProcessPerformance performa |
| | | 248 | | |
| | 0 | 249 | | protected virtual Task ReportPerformance(ProcessPerformance performance) => Task.CompletedTask; |
| | | 250 | | |
| | | 251 | | protected virtual Task<ProcessingResult> CheckInitialState() |
| | 0 | 252 | | { |
| | 0 | 253 | | return Task.FromResult(new ProcessingResult(ProcessServiceStatus.Available)); |
| | 0 | 254 | | } |
| | | 255 | | |
| | | 256 | | public virtual async Task<ProcessingResult> Update() |
| | 36 | 257 | | { |
| | 36 | 258 | | var processResult = await CheckInitialState(); |
| | | 259 | | |
| | 36 | 260 | | if (processResult.ServiceStatus != ProcessServiceStatus.Available) |
| | 12 | 261 | | { |
| | 12 | 262 | | UpdatePerformanceMetrics(new ProcessPerformance(), DateTime.Now.Ticks); |
| | 12 | 263 | | return processResult; |
| | | 264 | | } |
| | | 265 | | |
| | 24 | 266 | | var processingDateTime = DateTime.Now.Ticks; |
| | | 267 | | |
| | 24 | 268 | | if (MeasurePerformance) |
| | 2 | 269 | | { |
| | 2 | 270 | | _stopWatch.Start(); |
| | 2 | 271 | | } |
| | | 272 | | |
| | 24 | 273 | | var processed = await Analyze(processResult.PerformanceMeasurement); |
| | | 274 | | |
| | 24 | 275 | | var candidates = processed.Item1; |
| | 24 | 276 | | var perfItem = processed.Item2; |
| | | 277 | | |
| | 24 | 278 | | if (MeasurePerformance) |
| | 2 | 279 | | { |
| | 2 | 280 | | _stopWatch.Stop(); |
| | 2 | 281 | | perfItem.Update = _stopWatch.Elapsed - perfItem.Preparation; |
| | 2 | 282 | | _stopWatch.Reset(); |
| | 2 | 283 | | } |
| | | 284 | | |
| | 24 | 285 | | if (MeasurePerformance) |
| | 2 | 286 | | { |
| | 2 | 287 | | _stopWatch.Start(); |
| | 2 | 288 | | } |
| | | 289 | | |
| | 24 | 290 | | var interactions = ConvertDepthValue(candidates.ToList()); |
| | | 291 | | |
| | 24 | 292 | | if (MeasurePerformance) |
| | 2 | 293 | | { |
| | 2 | 294 | | _stopWatch.Stop(); |
| | 2 | 295 | | perfItem.ConvertDepthValue = _stopWatch.Elapsed; |
| | 2 | 296 | | _stopWatch.Reset(); |
| | 2 | 297 | | } |
| | | 298 | | |
| | 24 | 299 | | if (MeasurePerformance) |
| | 2 | 300 | | { |
| | 2 | 301 | | _stopWatch.Start(); |
| | 2 | 302 | | } |
| | | 303 | | |
| | 24 | 304 | | var processedInteractions = ComputeExtremumType(interactions, PointCloud.AsJaggedArray()); |
| | | 305 | | |
| | 24 | 306 | | var cleanedUpInteractions = RemoveExtremumsBetweenTouches(processedInteractions); |
| | | 307 | | |
| | 24 | 308 | | if (MeasurePerformance) |
| | 2 | 309 | | { |
| | 2 | 310 | | _stopWatch.Stop(); |
| | 2 | 311 | | perfItem.ComputeExtremumType = _stopWatch.Elapsed; |
| | 2 | 312 | | _stopWatch.Reset(); |
| | 2 | 313 | | } |
| | | 314 | | |
| | | 315 | | |
| | 24 | 316 | | if (MeasurePerformance) |
| | 2 | 317 | | { |
| | 2 | 318 | | _stopWatch.Start(); |
| | 2 | 319 | | } |
| | | 320 | | |
| | 24 | 321 | | var frame = ComputeSmoothingValue(cleanedUpInteractions); |
| | | 322 | | |
| | 24 | 323 | | if (MeasurePerformance) |
| | 2 | 324 | | { |
| | 2 | 325 | | _stopWatch.Stop(); |
| | 2 | 326 | | perfItem.Smoothing = _stopWatch.Elapsed; |
| | 2 | 327 | | _stopWatch.Reset(); |
| | 2 | 328 | | } |
| | | 329 | | |
| | 24 | 330 | | var confidentInteractions = ApplyConfidenceFilter(SelectSmoothedInteractions(frame.Interactions)); |
| | | 331 | | |
| | | 332 | | // Populate optional remote stage timings before publishing the shared dictionary. |
| | 24 | 333 | | var reportTask = MeasurePerformance ? ReportPerformance(perfItem) : Task.CompletedTask; |
| | 24 | 334 | | processResult.PerformanceMeasurement = perfItem; |
| | 24 | 335 | | UpdatePerformanceMetrics(perfItem, processingDateTime); |
| | | 336 | | |
| | 24 | 337 | | OnNewInteractions(confidentInteractions.ToList()); |
| | | 338 | | |
| | 24 | 339 | | await reportTask; |
| | | 340 | | |
| | 24 | 341 | | return processResult; |
| | 36 | 342 | | } |
| | | 343 | | |
| | | 344 | | /// <summary>Limits the smoothed output without changing the tracking history.</summary> |
| | | 345 | | protected virtual IEnumerable<Interaction> SelectSmoothedInteractions(IEnumerable<Interaction> interactions) |
| | 20 | 346 | | => interactions; |
| | | 347 | | |
| | | 348 | | protected InteractionFrame ComputeSmoothingValue(IList<Interaction> rawInteractions) |
| | 24 | 349 | | { |
| | 24 | 350 | | var result = _smoothingBehaviour.Update(rawInteractions); |
| | 24 | 351 | | InteractionHistoryUpdated?.Invoke(this, _smoothingBehaviour.InteractionsFramesCache); |
| | | 352 | | |
| | 24 | 353 | | return result; |
| | 24 | 354 | | } |
| | | 355 | | |
| | | 356 | | protected virtual ExtremumDescription ComputeExtremumType(Point3[][] pointsArray, int xPos, int yPos) |
| | 54 | 357 | | { |
| | 54 | 358 | | if (ExtremumTypeCheckMethod == ExtremumTypeCheckMethod.Global) |
| | 20 | 359 | | { |
| | | 360 | | // Global skips neighbourhood validation, but retains the same sensor-depth |
| | | 361 | | // convention as the spatial classifier: pushes are minima, pulls maxima. |
| | 20 | 362 | | var depth = pointsArray[xPos][yPos].Z; |
| | 20 | 363 | | var type = !float.IsFinite(depth) || depth == Distance ? ExtremumType.Undefined |
| | 20 | 364 | | : depth < Distance ? ExtremumType.Minimum : ExtremumType.Maximum; |
| | 20 | 365 | | return new ExtremumDescription |
| | 20 | 366 | | { |
| | 20 | 367 | | Type = type, |
| | 20 | 368 | | NumFittingPoints = type == ExtremumType.Undefined ? 0 : ExtremumTypeCheckNumSamples, |
| | 20 | 369 | | PercentageFittingPoints = type == ExtremumType.Undefined ? 0 : 1.0f |
| | 20 | 370 | | }; |
| | | 371 | | } |
| | | 372 | | |
| | 34 | 373 | | var xMax = pointsArray.Length - 1; |
| | 34 | 374 | | var yMax = xMax > 0 ? pointsArray[0].Length - 1 : 0; |
| | | 375 | | |
| | | 376 | | // refZ is the absolute distance from sensor |
| | 34 | 377 | | var refZ = Math.Abs(pointsArray[xPos][yPos].Z); |
| | | 378 | | |
| | 34 | 379 | | var numCloser = 0; |
| | 34 | 380 | | var numFarther = 0; |
| | | 381 | | |
| | | 382 | | Tuple<int, int>[] samples; |
| | 34 | 383 | | switch (ExtremumTypeCheckMethod) |
| | | 384 | | { |
| | | 385 | | case ExtremumTypeCheckMethod.StochasticStatic: |
| | 0 | 386 | | samples = _stochasticSamples; |
| | 0 | 387 | | break; |
| | | 388 | | case ExtremumTypeCheckMethod.StochasticDynamic: |
| | 0 | 389 | | samples = GenerateSamples(); |
| | 0 | 390 | | break; |
| | | 391 | | case ExtremumTypeCheckMethod.Global: |
| | 0 | 392 | | throw new ArgumentException("invalid Method 'Global' at this point."); |
| | | 393 | | case ExtremumTypeCheckMethod.FixedRadius: |
| | | 394 | | default: |
| | 34 | 395 | | samples = _fixedSamples; |
| | 34 | 396 | | break; |
| | | 397 | | } |
| | | 398 | | |
| | 612 | 399 | | for (var i = 0; i < samples.Length; i++) |
| | 272 | 400 | | { |
| | 272 | 401 | | var sample = GetSample(pointsArray, xPos, yPos, samples, i, xMax, yMax); |
| | 272 | 402 | | if (Math.Abs(sample.Z) > refZ) |
| | 176 | 403 | | numFarther++; |
| | | 404 | | else |
| | 96 | 405 | | numCloser++; |
| | 272 | 406 | | } |
| | | 407 | | |
| | | 408 | | |
| | 34 | 409 | | var ratioMax = ExtremumTypeCheckNumSamples > 0 ? (float)numCloser / ExtremumTypeCheckNumSamples : 0; |
| | 34 | 410 | | var ratioMin = ExtremumTypeCheckNumSamples > 0 ? (float)numFarther / ExtremumTypeCheckNumSamples : 0; |
| | | 411 | | |
| | 34 | 412 | | if (numCloser > numFarther && ratioMax > ExtremumTypeCheckFittingPercentage) |
| | 12 | 413 | | { |
| | 12 | 414 | | return new ExtremumDescription |
| | 12 | 415 | | { |
| | 12 | 416 | | Type = ExtremumType.Maximum, |
| | 12 | 417 | | NumFittingPoints = numCloser, |
| | 12 | 418 | | PercentageFittingPoints = ratioMax |
| | 12 | 419 | | }; |
| | | 420 | | } |
| | | 421 | | |
| | 22 | 422 | | if (numFarther > numCloser && ratioMin > ExtremumTypeCheckFittingPercentage) |
| | 22 | 423 | | { |
| | 22 | 424 | | return new ExtremumDescription |
| | 22 | 425 | | { |
| | 22 | 426 | | Type = ExtremumType.Minimum, |
| | 22 | 427 | | NumFittingPoints = numFarther, |
| | 22 | 428 | | PercentageFittingPoints = ratioMin |
| | 22 | 429 | | }; |
| | | 430 | | } |
| | | 431 | | |
| | 0 | 432 | | return new ExtremumDescription |
| | 0 | 433 | | { |
| | 0 | 434 | | Type = ExtremumType.Undefined, |
| | 0 | 435 | | NumFittingPoints = Math.Max(numCloser, numFarther), |
| | 0 | 436 | | PercentageFittingPoints = Math.Max(ratioMin, ratioMax) |
| | 0 | 437 | | }; |
| | | 438 | | |
| | 54 | 439 | | } |
| | | 440 | | |
| | | 441 | | protected List<Interaction> ConvertDepthValue(List<Interaction> interactions) |
| | 32 | 442 | | { |
| | 32 | 443 | | var result = new List<Interaction>(); |
| | | 444 | | |
| | 224 | 445 | | foreach (var item in interactions) |
| | 64 | 446 | | { |
| | 64 | 447 | | if (item?.Position == null || !float.IsFinite(item.Position.Z) || |
| | 64 | 448 | | Math.Abs(item.Position.Z - Distance) <= MinDistance) |
| | 22 | 449 | | continue; |
| | | 450 | | |
| | 42 | 451 | | if (item.Position.Z < Distance) |
| | 28 | 452 | | { |
| | 28 | 453 | | item.Type = InteractionType.Push; |
| | 28 | 454 | | var depth = (Distance - item.Position.Z - MinDistance) / (MinDistance - MaxDistance); |
| | | 455 | | |
| | 28 | 456 | | if (depth > 0) |
| | 0 | 457 | | continue; |
| | | 458 | | |
| | 28 | 459 | | if (depth < -1) |
| | 0 | 460 | | depth = -1; |
| | | 461 | | |
| | 28 | 462 | | item.Position.Z = depth; |
| | 28 | 463 | | } |
| | | 464 | | else |
| | 14 | 465 | | { |
| | 14 | 466 | | item.Type = InteractionType.Pull; |
| | 14 | 467 | | var depth = (item.Position.Z - MinDistance - Distance) / (MaxDistance - MinDistance); |
| | | 468 | | |
| | 14 | 469 | | if (depth < 0) |
| | 0 | 470 | | continue; |
| | | 471 | | |
| | 14 | 472 | | if (depth > 1) |
| | 0 | 473 | | depth = 1; |
| | | 474 | | |
| | 14 | 475 | | item.Position.Z = depth; |
| | 14 | 476 | | } |
| | | 477 | | |
| | 42 | 478 | | result.Add(item); |
| | 42 | 479 | | } |
| | | 480 | | |
| | 32 | 481 | | return result; |
| | 32 | 482 | | } |
| | | 483 | | |
| | | 484 | | protected IEnumerable<Interaction> ApplyConfidenceFilter(IEnumerable<Interaction> interactions) |
| | 24 | 485 | | { |
| | 56 | 486 | | return interactions.ToArray().Where(item => item.Confidence > MinConfidence); |
| | 24 | 487 | | } |
| | | 488 | | |
| | | 489 | | protected List<Interaction> ComputeExtremumType(List<Interaction> interactions, Point3[][] pointsArray) |
| | 24 | 490 | | { |
| | 24 | 491 | | interactions.ForEach(interaction => |
| | 58 | 492 | | interaction.ExtremumDescription = ComputeExtremumType(pointsArray, (int)interaction.Position.X, (int)int |
| | | 493 | | |
| | 24 | 494 | | return interactions; |
| | 24 | 495 | | } |
| | | 496 | | |
| | | 497 | | /// <summary> |
| | | 498 | | /// generic approach: removes all maximums below the surface and all minimums above the surface. Removal is base |
| | | 499 | | /// Points witch <see cref="ExtremumType.Undefined"/> are also removed. |
| | | 500 | | /// </summary> |
| | | 501 | | /// <param name="interactions">the list of preprocessed interactions with valid ExtremumDescription</param> |
| | | 502 | | /// <returns></returns> |
| | | 503 | | protected List<Interaction> RemoveExtremumsBetweenTouches(List<Interaction> interactions) |
| | 24 | 504 | | { |
| | 24 | 505 | | return interactions.Where((touch) => |
| | 24 | 506 | | // invalid or undefined ExtremumType |
| | 34 | 507 | | touch.ExtremumDescription != null && touch.ExtremumDescription.Type != ExtremumType.Undefined && |
| | 34 | 508 | | // minimums below the surface |
| | 34 | 509 | | ((touch.Position.Z < 0 && touch.ExtremumDescription.Type == ExtremumType.Minimum) || |
| | 34 | 510 | | // maximums above the surface |
| | 34 | 511 | | (touch.Position.Z > 0 && touch.ExtremumDescription.Type == ExtremumType.Maximum)) |
| | 24 | 512 | | |
| | 24 | 513 | | ).ToList(); |
| | 24 | 514 | | } |
| | | 515 | | |
| | | 516 | | protected void UpdatePerformanceMetrics(ProcessPerformance perfItem, long startTime) |
| | 36 | 517 | | { |
| | 36 | 518 | | var pData = new PerformanceDataItem |
| | 36 | 519 | | { |
| | 36 | 520 | | FrameId = Convert.ToInt32(FrameId), |
| | 36 | 521 | | FrameStart = startTime, |
| | 36 | 522 | | FrameEnd = DateTime.Now.Ticks, |
| | 36 | 523 | | Process = perfItem, |
| | 36 | 524 | | Stage = PerformanceDataStage.ProcessingData |
| | 36 | 525 | | }; |
| | | 526 | | |
| | 36 | 527 | | PerformanceDataUpdated?.Invoke(this, pData); |
| | 36 | 528 | | } |
| | | 529 | | |
| | | 530 | | protected void UpdateInteractionFrames(List<Interaction> cleanedUpInteractions, InteractionFrame currentFrame) |
| | 0 | 531 | | { |
| | 0 | 532 | | currentFrame.Interactions.ForEach((interaction) => |
| | 0 | 533 | | { |
| | 0 | 534 | | if (cleanedUpInteractions.FirstOrDefault((i) => i.TouchId == interaction.TouchId) == null) |
| | 0 | 535 | | { |
| | 0 | 536 | | if (interaction.Confidence > 0) |
| | 0 | 537 | | interaction.Confidence--; |
| | 0 | 538 | | } |
| | 0 | 539 | | }); |
| | | 540 | | |
| | 0 | 541 | | currentFrame.Interactions.RemoveAll((interaction) => interaction.Confidence <= 0); |
| | | 542 | | |
| | 0 | 543 | | _smoothingBehaviour.UpdateCachedFrame(currentFrame); |
| | 0 | 544 | | InteractionHistoryUpdated?.Invoke(this, _smoothingBehaviour.InteractionsFramesCache); |
| | 0 | 545 | | } |
| | | 546 | | |
| | 24 | 547 | | protected void OnNewInteractions(List<Interaction> args) => NewInteractions?.Invoke(this, args); |
| | | 548 | | |
| | | 549 | | /// <summary> |
| | | 550 | | /// Calculates the average distance for the currently stored PointCloud. |
| | | 551 | | /// </summary> |
| | | 552 | | /// <returns></returns> |
| | | 553 | | public float CalculateAverageDistance() |
| | 0 | 554 | | { |
| | 0 | 555 | | var sum = 0f; |
| | 0 | 556 | | var points = PointCloud?.AsArray(); |
| | | 557 | | |
| | 0 | 558 | | if (points == null) |
| | 0 | 559 | | return sum; |
| | | 560 | | |
| | 0 | 561 | | var numValidSamples = 0; |
| | | 562 | | |
| | 0 | 563 | | for (var i = 0; i < points.Length; ++i) |
| | 0 | 564 | | { |
| | 0 | 565 | | if (!points[i].IsValid || points[i].IsFiltered) |
| | 0 | 566 | | continue; |
| | | 567 | | |
| | 0 | 568 | | numValidSamples++; |
| | 0 | 569 | | sum += points[i].Z; |
| | 0 | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | return (float) decimal.Round((decimal) (sum / numValidSamples), 2); |
| | 0 | 573 | | } |
| | | 574 | | |
| | | 575 | | private static Point3 GetSample(Point3[][] pointsArray, int xPos, int yPos, Tuple<int, int>[] samples, int i, in |
| | 272 | 576 | | { |
| | 272 | 577 | | var xIdx = xPos + samples[i].Item1; |
| | 272 | 578 | | xIdx = Math.Min(Math.Max(xIdx, 0), xMax); |
| | | 579 | | |
| | 272 | 580 | | var yIdx = yPos + samples[i].Item2; |
| | 272 | 581 | | yIdx = Math.Min(Math.Max(yIdx, 0), yMax); |
| | 272 | 582 | | var sample = pointsArray[xIdx][yIdx]; |
| | | 583 | | |
| | 272 | 584 | | return sample; |
| | 272 | 585 | | } |
| | | 586 | | |
| | | 587 | | private Tuple<int, int>[] GenerateSamples() |
| | 76 | 588 | | { |
| | 76 | 589 | | var result = new List<Tuple<int, int>>(); |
| | | 590 | | |
| | 76 | 591 | | var rnd = new Random(); |
| | | 592 | | |
| | 76 | 593 | | var min = (int)Math.Floor(0.5f * ExtremumTypeCheckRadius); |
| | 76 | 594 | | var max = ExtremumTypeCheckRadius; |
| | | 595 | | |
| | 920 | 596 | | for (var i = 0; i < ExtremumTypeCheckNumSamples; i++) |
| | 384 | 597 | | { |
| | 384 | 598 | | var xStochastic = rnd.Next(min, max); |
| | 384 | 599 | | var yStochastic = rnd.Next(min, max); |
| | | 600 | | |
| | 384 | 601 | | result.Add(new Tuple<int, int>(xStochastic, yStochastic)); |
| | 384 | 602 | | } |
| | | 603 | | |
| | 76 | 604 | | return result.ToArray(); |
| | 76 | 605 | | } |
| | | 606 | | |
| | | 607 | | private void UpdateSamples() |
| | 76 | 608 | | { |
| | 76 | 609 | | var samplesFixed = new List<Tuple<int, int>>(); |
| | | 610 | | |
| | 920 | 611 | | for (var i = 0; i < ExtremumTypeCheckNumSamples; i++) |
| | 384 | 612 | | { |
| | 384 | 613 | | var p = (float)i / ExtremumTypeCheckRadius; |
| | 384 | 614 | | var xFixed = (int) Math.Floor(ExtremumTypeCheckRadius * Math.Cos(p * 2 * Math.PI)); |
| | 384 | 615 | | var yFixed = (int) Math.Floor(ExtremumTypeCheckRadius * Math.Sin(p * 2 * Math.PI)); |
| | | 616 | | |
| | 384 | 617 | | samplesFixed.Add(new Tuple<int, int>(xFixed, yFixed)); |
| | 384 | 618 | | } |
| | 76 | 619 | | _fixedSamples = samplesFixed.ToArray(); |
| | | 620 | | |
| | 76 | 621 | | _stochasticSamples = GenerateSamples(); |
| | 76 | 622 | | } |
| | | 623 | | |
| | | 624 | | |
| | | 625 | | } |
| | | 626 | | } |