| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Implementation.Interfaces; |
| | | 8 | | using ReFlex.Core.Common.Components; |
| | | 9 | | using ReFlex.Core.Common.Interfaces; |
| | | 10 | | using ReFlex.Core.Common.Util; |
| | | 11 | | using ReFlex.Core.Interactivity.Components; |
| | | 12 | | using ReFlex.Core.Interactivity.Interfaces; |
| | | 13 | | using ReFlex.Core.Interactivity.Util; |
| | | 14 | | using ReFlex.Core.Events; |
| | | 15 | | |
| | | 16 | | namespace Implementation.Components |
| | | 17 | | { |
| | | 18 | | public class InteractionManager : IInteractionManager, IDisposable |
| | | 19 | | { |
| | | 20 | | private IInteractionObserver _interactionObserver; |
| | 36 | 21 | | private readonly object _publicationSync = new(); |
| | | 22 | | private readonly ITrackingManager _trackingManager; |
| | 36 | 23 | | private readonly object _snapshotSync = new(); |
| | 36 | 24 | | private readonly Stack<TrackingObservation> _spareSnapshots = new(2); |
| | | 25 | | private readonly DepthImageManager _snapshotSource; |
| | | 26 | | private TrackingObservation _pendingObservation; |
| | | 27 | | private TrackingObservation _lastSnapshot; |
| | | 28 | | private bool _disposed; |
| | 36 | 29 | | private readonly SemaphoreSlim _updateGate = new(1, 1); |
| | 36 | 30 | | private long _lastSequence = -1; |
| | 36 | 31 | | private double _lastObservationTime = double.NegativeInfinity; |
| | 0 | 32 | | protected virtual double GetTimestampSeconds() => (double)Stopwatch.GetTimestamp() / Stopwatch.Frequency; |
| | | 33 | | private IList<Interaction> _interactions; |
| | | 34 | | private bool _useVelocityPrediction; |
| | 36 | 35 | | private double _predictionHorizonSeconds = .05; |
| | 36 | 36 | | private int _numFramesForPrediction = 3; |
| | | 37 | | private bool _useSecondDerivation; |
| | 36 | 38 | | private float _secondDerivationMagnitude = .5f; |
| | | 39 | | private readonly IDepthImageManager _depthImageManager; |
| | | 40 | | private readonly IPerformanceAggregator _performanceAggregator; |
| | | 41 | | |
| | | 42 | | private readonly IRemoteInteractionProcessorService _service; |
| | | 43 | | |
| | | 44 | | private float _distance; |
| | | 45 | | private float _minDistance; |
| | | 46 | | private float _maxDistance; |
| | | 47 | | private float _minAngle; |
| | | 48 | | private int _minConfidence; |
| | | 49 | | private int _maxConfidence; |
| | | 50 | | private int _numSmoothFrames; |
| | | 51 | | private int _interactionHistorySize; |
| | 36 | 52 | | private double _contactRetentionSeconds = .1; |
| | 36 | 53 | | private double _observationTimeoutSeconds = 1; |
| | 36 | 54 | | private bool _useMinimumCostAssignment = true; |
| | 36 | 55 | | private float _assignmentMissCost = InteractionSmoothingBehaviour.DefaultAssignmentMissCost; |
| | 36 | 56 | | private float _assignmentBirthCost = InteractionSmoothingBehaviour.DefaultAssignmentBirthCost; |
| | 36 | 57 | | private float _confidenceGainPerSecond = 30; |
| | 36 | 58 | | private float _confidenceDecayPerSecond = 30; |
| | 36 | 59 | | private float _smoothingDistanceSquared = 64f; |
| | 36 | 60 | | private float _depthScale = 100.0f; |
| | | 61 | | private FilterType _filterType; |
| | | 62 | | private int _extremumTypeCheckNumSamples; |
| | | 63 | | private int _extremumTypeCheckRadius; |
| | 36 | 64 | | private float _extremumMergeRadius = ExtremumConsolidator.DefaultRadius; |
| | | 65 | | private float _extremumTypeCheckFittingPercentage; |
| | | 66 | | private ExtremumTypeCheckMethod _extremumTypeCheckMethod; |
| | | 67 | | |
| | 172 | 68 | | public IList<Interaction> Interactions => _interactions ?? new List<Interaction>(); |
| | | 69 | | |
| | | 70 | | public double PredictionHorizonSeconds |
| | | 71 | | { |
| | 0 | 72 | | get => _interactionObserver?.PredictionHorizonSeconds ?? _predictionHorizonSeconds; |
| | | 73 | | set |
| | 2 | 74 | | { |
| | 2 | 75 | | if (_interactionObserver != null) _interactionObserver.PredictionHorizonSeconds = value; |
| | 2 | 76 | | _predictionHorizonSeconds = value; |
| | 2 | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public bool UseVelocityPrediction |
| | | 81 | | { |
| | 0 | 82 | | get => _interactionObserver?.UseVelocityPrediction ?? _useVelocityPrediction; |
| | | 83 | | set |
| | 26 | 84 | | { |
| | 26 | 85 | | if (_interactionObserver != null) _interactionObserver.UseVelocityPrediction = value; |
| | 26 | 86 | | _useVelocityPrediction = value; |
| | 26 | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public int NumFramesForPrediction |
| | | 91 | | { |
| | 0 | 92 | | get => _interactionObserver?.NumFramesForPrediction ?? _numFramesForPrediction; |
| | | 93 | | set |
| | 2 | 94 | | { |
| | 2 | 95 | | if (_interactionObserver != null) _interactionObserver.NumFramesForPrediction = value; |
| | 2 | 96 | | _numFramesForPrediction = value; |
| | 2 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | public bool UseSecondDerivation |
| | | 101 | | { |
| | 0 | 102 | | get => _interactionObserver?.UseSecondDerivation ?? _useSecondDerivation; |
| | | 103 | | set |
| | 2 | 104 | | { |
| | 2 | 105 | | if (_interactionObserver != null) _interactionObserver.UseSecondDerivation = value; |
| | 2 | 106 | | _useSecondDerivation = value; |
| | 2 | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public float SecondDerivationMagnitude |
| | | 111 | | { |
| | 0 | 112 | | get => _interactionObserver?.SecondDerivationMagnitude ?? _secondDerivationMagnitude; |
| | | 113 | | set |
| | 2 | 114 | | { |
| | 2 | 115 | | if (_interactionObserver != null) _interactionObserver.SecondDerivationMagnitude = value; |
| | 2 | 116 | | _secondDerivationMagnitude = value; |
| | 2 | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | public ObserverType Type |
| | | 121 | | { |
| | 12 | 122 | | get => _interactionObserver?.Type ?? ObserverType.None; |
| | | 123 | | set |
| | 44 | 124 | | { |
| | 44 | 125 | | lock (_publicationSync) |
| | 44 | 126 | | lock (_snapshotSync) |
| | 44 | 127 | | UpdateObserver(CreateObserver(value)); |
| | 44 | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 20 | 131 | | protected virtual IInteractionObserver CreateObserver(ObserverType type) => type switch |
| | 20 | 132 | | { |
| | 16 | 133 | | ObserverType.Remote => new RemoteInteractionProcessor(_service), |
| | 4 | 134 | | ObserverType.MultiTouch => new MultiInteractionObserver(), |
| | 0 | 135 | | ObserverType.SingleTouch => new SingleInteractionObserver(), |
| | 0 | 136 | | ObserverType.None => new NoInteractionObserver(), |
| | 0 | 137 | | _ => throw new ArgumentOutOfRangeException(nameof(type)) |
| | 20 | 138 | | }; |
| | | 139 | | |
| | | 140 | | public float ComputeZeroPlaneDistance() => |
| | 0 | 141 | | ReadObserverRasters(() => _interactionObserver?.CalculateAverageDistance() ?? 0); |
| | | 142 | | |
| | | 143 | | private T ReadObserverRasters<T>(Func<T> read) |
| | 4 | 144 | | { |
| | 4 | 145 | | if (_snapshotSource == null) return read(); |
| | 4 | 146 | | return _snapshotSource.ReadRasters(() => |
| | 4 | 147 | | { |
| | 8 | 148 | | lock (_snapshotSync) return read(); |
| | 8 | 149 | | }); |
| | 4 | 150 | | } |
| | | 151 | | |
| | | 152 | | private void UpdateObserver(IInteractionObserver observer) |
| | 56 | 153 | | { |
| | 56 | 154 | | if (_interactionObserver != null) |
| | 20 | 155 | | { |
| | 20 | 156 | | _interactionObserver.NewInteractions -= OnNewInteractions; |
| | 20 | 157 | | _interactionObserver.NewVelocities -= OnNewVelocities; |
| | 20 | 158 | | _interactionObserver.InteractionHistoryUpdated -= OnHistoryUpdated; |
| | 20 | 159 | | _performanceAggregator.UnregisterReporter(_interactionObserver as IPerformanceReporter); |
| | 20 | 160 | | } |
| | | 161 | | |
| | 56 | 162 | | if (observer == null) |
| | 0 | 163 | | return; |
| | | 164 | | |
| | 56 | 165 | | observer.Distance = _distance; |
| | 56 | 166 | | observer.MinDistance = _minDistance; |
| | 56 | 167 | | observer.MaxDistance = _maxDistance; |
| | 56 | 168 | | observer.MinConfidence = _minConfidence; |
| | 56 | 169 | | observer.MaxConfidence = _maxConfidence; |
| | 56 | 170 | | observer.MinAngle = _minAngle; |
| | 56 | 171 | | observer.PointCloud = _depthImageManager.PointCloud; |
| | 56 | 172 | | observer.VectorField = _depthImageManager.VectorField; |
| | 56 | 173 | | observer.InteractionHistorySize = _interactionHistorySize; |
| | 56 | 174 | | observer.NumSmoothingFrames = _numSmoothFrames; |
| | 56 | 175 | | observer.TouchMergeDistance2D = _smoothingDistanceSquared; |
| | 56 | 176 | | observer.ContactRetentionSeconds = _contactRetentionSeconds; |
| | 56 | 177 | | observer.ObservationTimeoutSeconds = _observationTimeoutSeconds; |
| | 56 | 178 | | observer.UseMinimumCostAssignment = _useMinimumCostAssignment; |
| | 56 | 179 | | observer.AssignmentMissCost = _assignmentMissCost; |
| | 56 | 180 | | observer.AssignmentBirthCost = _assignmentBirthCost; |
| | 56 | 181 | | observer.ConfidenceGainPerSecond = _confidenceGainPerSecond; |
| | 56 | 182 | | observer.ConfidenceDecayPerSecond = _confidenceDecayPerSecond; |
| | 56 | 183 | | observer.DepthScale = _depthScale; |
| | 56 | 184 | | observer.FilterType = _filterType; |
| | 56 | 185 | | observer.PredictionHorizonSeconds = _predictionHorizonSeconds; |
| | 56 | 186 | | observer.UseVelocityPrediction = _useVelocityPrediction; |
| | 56 | 187 | | observer.NumFramesForPrediction = _numFramesForPrediction; |
| | 56 | 188 | | observer.UseSecondDerivation = _useSecondDerivation; |
| | 56 | 189 | | observer.SecondDerivationMagnitude = _secondDerivationMagnitude; |
| | 56 | 190 | | observer.ExtremumTypeCheckRadius = _extremumTypeCheckRadius; |
| | 56 | 191 | | observer.ExtremumMergeRadius = _extremumMergeRadius; |
| | 56 | 192 | | observer.ExtremumTypeCheckNumSamples = _extremumTypeCheckNumSamples; |
| | 56 | 193 | | observer.ExtremumTypeCheckFittingPercentage = _extremumTypeCheckFittingPercentage; |
| | 56 | 194 | | observer.ExtremumTypeCheckMethod = _extremumTypeCheckMethod; |
| | 56 | 195 | | _interactionObserver = observer; |
| | | 196 | | |
| | 56 | 197 | | _interactionObserver.NewInteractions += OnNewInteractions; |
| | 56 | 198 | | _interactionObserver.NewVelocities += OnNewVelocities; |
| | 56 | 199 | | _interactionObserver.InteractionHistoryUpdated += OnHistoryUpdated; |
| | 56 | 200 | | _performanceAggregator.RegisterReporter(_interactionObserver as IPerformanceReporter); |
| | 56 | 201 | | } |
| | | 202 | | |
| | | 203 | | private void OnHistoryUpdated(object sender, IList<InteractionFrame> e) |
| | 66 | 204 | | { |
| | 66 | 205 | | lock (_publicationSync) |
| | 66 | 206 | | { |
| | 66 | 207 | | if (_disposed || !ReferenceEquals(sender, _interactionObserver)) return; |
| | 66 | 208 | | InteractionHistoryUpdated?.Invoke(this, e); |
| | 66 | 209 | | } |
| | 66 | 210 | | } |
| | | 211 | | |
| | | 212 | | public float Distance |
| | | 213 | | { |
| | 0 | 214 | | get => _interactionObserver?.Distance ?? 0; |
| | | 215 | | set |
| | 32 | 216 | | { |
| | 32 | 217 | | if (_interactionObserver != null) |
| | 0 | 218 | | _interactionObserver.Distance = value; |
| | | 219 | | |
| | 32 | 220 | | _distance = value; |
| | 32 | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | public float MinDistance |
| | | 225 | | { |
| | 0 | 226 | | get => _interactionObserver?.MinDistance ?? 0; |
| | | 227 | | set |
| | 32 | 228 | | { |
| | 32 | 229 | | if (_interactionObserver != null) |
| | 0 | 230 | | _interactionObserver.MinDistance = value; |
| | | 231 | | |
| | 32 | 232 | | _minDistance = value; |
| | 32 | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public float MaxDistance |
| | | 237 | | { |
| | 0 | 238 | | get => _interactionObserver?.MaxDistance ?? 0; |
| | | 239 | | set |
| | 32 | 240 | | { |
| | 32 | 241 | | if (_interactionObserver != null) |
| | 0 | 242 | | _interactionObserver.MaxDistance = value; |
| | | 243 | | |
| | 32 | 244 | | _maxDistance = value; |
| | 32 | 245 | | } |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | public float MinAngle |
| | | 249 | | { |
| | 0 | 250 | | get => _interactionObserver?.MinAngle ?? 0; |
| | | 251 | | set |
| | 0 | 252 | | { |
| | 0 | 253 | | if (_interactionObserver != null) |
| | 0 | 254 | | _interactionObserver.MinAngle = value; |
| | | 255 | | |
| | 0 | 256 | | _minAngle = value; |
| | 0 | 257 | | } |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | public int MinConfidence |
| | | 261 | | { |
| | 0 | 262 | | get => (int)(_interactionObserver?.MinConfidence ?? 0); |
| | | 263 | | set |
| | 28 | 264 | | { |
| | 28 | 265 | | if (_interactionObserver != null) |
| | 4 | 266 | | _interactionObserver.MinConfidence = value; |
| | | 267 | | |
| | 28 | 268 | | _minConfidence = value; |
| | 28 | 269 | | } |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | public int MaxConfidence |
| | | 273 | | { |
| | 0 | 274 | | get => (int)(_interactionObserver?.MaxConfidence ?? 0); |
| | | 275 | | set |
| | 32 | 276 | | { |
| | 32 | 277 | | if (_interactionObserver != null) |
| | 0 | 278 | | _interactionObserver.MaxConfidence = value; |
| | | 279 | | |
| | 32 | 280 | | _maxConfidence = value; |
| | 32 | 281 | | } |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | public int InteractionHistorySize |
| | | 285 | | { |
| | 0 | 286 | | get => _interactionObserver?.InteractionHistorySize ?? 0; |
| | | 287 | | set |
| | 32 | 288 | | { |
| | 32 | 289 | | if (_interactionObserver != null) |
| | 0 | 290 | | _interactionObserver.InteractionHistorySize = value; |
| | | 291 | | |
| | 32 | 292 | | _interactionHistorySize = value; |
| | 32 | 293 | | } |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | public int NumSmoothingFrames |
| | | 297 | | { |
| | 0 | 298 | | get => _interactionObserver?.NumSmoothingFrames ?? 0; |
| | | 299 | | set |
| | 26 | 300 | | { |
| | 26 | 301 | | if (_interactionObserver != null) |
| | 0 | 302 | | _interactionObserver.NumSmoothingFrames = value; |
| | | 303 | | |
| | 26 | 304 | | _numSmoothFrames = value; |
| | 26 | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | public bool UseMinimumCostAssignment |
| | | 309 | | { |
| | 0 | 310 | | get => _useMinimumCostAssignment; |
| | | 311 | | set |
| | 4 | 312 | | { |
| | 6 | 313 | | if (_interactionObserver != null) _interactionObserver.UseMinimumCostAssignment = value; |
| | 4 | 314 | | _useMinimumCostAssignment = value; |
| | 4 | 315 | | } |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | public float AssignmentMissCost |
| | | 319 | | { |
| | 0 | 320 | | get => _assignmentMissCost; |
| | | 321 | | set |
| | 4 | 322 | | { |
| | 4 | 323 | | if (!float.IsFinite(value) || value < 0) throw new ArgumentOutOfRangeException(nameof(value)); |
| | 6 | 324 | | if (_interactionObserver != null) _interactionObserver.AssignmentMissCost = value; |
| | 4 | 325 | | _assignmentMissCost = value; |
| | 4 | 326 | | } |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | public float AssignmentBirthCost |
| | | 330 | | { |
| | 0 | 331 | | get => _assignmentBirthCost; |
| | | 332 | | set |
| | 4 | 333 | | { |
| | 4 | 334 | | if (!float.IsFinite(value) || value < 0) throw new ArgumentOutOfRangeException(nameof(value)); |
| | 6 | 335 | | if (_interactionObserver != null) _interactionObserver.AssignmentBirthCost = value; |
| | 4 | 336 | | _assignmentBirthCost = value; |
| | 4 | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | public float ConfidenceGainPerSecond |
| | | 341 | | { |
| | 0 | 342 | | get => _confidenceGainPerSecond; |
| | | 343 | | set |
| | 2 | 344 | | { |
| | 2 | 345 | | if (!float.IsFinite(value) || value < 0) throw new ArgumentOutOfRangeException(nameof(value)); |
| | 2 | 346 | | if (_interactionObserver != null) _interactionObserver.ConfidenceGainPerSecond = value; |
| | 2 | 347 | | _confidenceGainPerSecond = value; |
| | 2 | 348 | | } |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | public float ConfidenceDecayPerSecond |
| | | 352 | | { |
| | 0 | 353 | | get => _confidenceDecayPerSecond; |
| | | 354 | | set |
| | 2 | 355 | | { |
| | 2 | 356 | | if (!float.IsFinite(value) || value < 0) throw new ArgumentOutOfRangeException(nameof(value)); |
| | 2 | 357 | | if (_interactionObserver != null) _interactionObserver.ConfidenceDecayPerSecond = value; |
| | 2 | 358 | | _confidenceDecayPerSecond = value; |
| | 2 | 359 | | } |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | public double ContactRetentionSeconds |
| | | 363 | | { |
| | 0 | 364 | | get => _interactionObserver?.ContactRetentionSeconds ?? _contactRetentionSeconds; |
| | | 365 | | set |
| | 26 | 366 | | { |
| | 26 | 367 | | if (_interactionObserver != null) |
| | 0 | 368 | | _interactionObserver.ContactRetentionSeconds = value; |
| | | 369 | | |
| | 26 | 370 | | _contactRetentionSeconds = double.IsFinite(value) && value >= 0 ? value : |
| | 26 | 371 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | 26 | 372 | | } |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | public double ObservationTimeoutSeconds |
| | | 376 | | { |
| | 0 | 377 | | get => _interactionObserver?.ObservationTimeoutSeconds ?? _observationTimeoutSeconds; |
| | | 378 | | set |
| | 34 | 379 | | { |
| | 34 | 380 | | if (!double.IsFinite(value) || value < 0) throw new ArgumentOutOfRangeException(nameof(value)); |
| | 34 | 381 | | if (_interactionObserver != null) _interactionObserver.ObservationTimeoutSeconds = value; |
| | 34 | 382 | | _observationTimeoutSeconds = value; |
| | 34 | 383 | | } |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | public float TouchMergeDistance2D |
| | | 387 | | { |
| | 0 | 388 | | get => _interactionObserver?.TouchMergeDistance2D ?? 0f; |
| | | 389 | | set |
| | 26 | 390 | | { |
| | 26 | 391 | | if (_interactionObserver != null) |
| | 2 | 392 | | _interactionObserver.TouchMergeDistance2D = value; |
| | | 393 | | |
| | 26 | 394 | | _smoothingDistanceSquared = value; |
| | 26 | 395 | | } |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | public float DepthScale |
| | | 399 | | { |
| | 0 | 400 | | get => _interactionObserver?.DepthScale ?? 0f; |
| | | 401 | | set |
| | 0 | 402 | | { |
| | 0 | 403 | | if (_interactionObserver != null) |
| | 0 | 404 | | _interactionObserver.DepthScale = value; |
| | | 405 | | |
| | 0 | 406 | | _depthScale = value; |
| | 0 | 407 | | } |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | public FilterType FilterType { |
| | 6 | 411 | | get => _interactionObserver?.FilterType ?? _filterType; |
| | | 412 | | set |
| | 6 | 413 | | { |
| | 6 | 414 | | if (_interactionObserver != null) |
| | 6 | 415 | | _interactionObserver.FilterType = value; |
| | 6 | 416 | | _filterType = value; |
| | 6 | 417 | | } |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | public int ExtremumTypeCheckNumSamples |
| | | 421 | | { |
| | 2 | 422 | | get => _interactionObserver?.ExtremumTypeCheckNumSamples ?? _extremumTypeCheckNumSamples; |
| | | 423 | | set |
| | 28 | 424 | | { |
| | 28 | 425 | | if (_interactionObserver != null) |
| | 2 | 426 | | _interactionObserver.ExtremumTypeCheckNumSamples = value; |
| | 28 | 427 | | _extremumTypeCheckNumSamples = value; |
| | 28 | 428 | | } |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | public float ExtremumMergeRadius |
| | | 432 | | { |
| | 0 | 433 | | get => _interactionObserver?.ExtremumMergeRadius ?? _extremumMergeRadius; |
| | | 434 | | set |
| | 2 | 435 | | { |
| | 2 | 436 | | ExtremumConsolidator.ValidateRadius(value); |
| | 2 | 437 | | if (_interactionObserver != null) _interactionObserver.ExtremumMergeRadius = value; |
| | 2 | 438 | | _extremumMergeRadius = value; |
| | 2 | 439 | | } |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | public int ExtremumTypeCheckRadius |
| | | 443 | | { |
| | 2 | 444 | | get => _interactionObserver?.ExtremumTypeCheckRadius ?? _extremumTypeCheckRadius; |
| | | 445 | | set |
| | 28 | 446 | | { |
| | 28 | 447 | | if (_interactionObserver != null) |
| | 2 | 448 | | _interactionObserver.ExtremumTypeCheckRadius = value; |
| | 28 | 449 | | _extremumTypeCheckRadius = value; |
| | 28 | 450 | | } |
| | | 451 | | } |
| | | 452 | | |
| | | 453 | | public float ExtremumTypeCheckFittingPercentage |
| | | 454 | | { |
| | 2 | 455 | | get => _interactionObserver?.ExtremumTypeCheckFittingPercentage ?? _extremumTypeCheckFittingPercentage; |
| | | 456 | | set |
| | 4 | 457 | | { |
| | 4 | 458 | | if (_interactionObserver != null) |
| | 2 | 459 | | _interactionObserver.ExtremumTypeCheckFittingPercentage = value; |
| | 4 | 460 | | _extremumTypeCheckFittingPercentage = value; |
| | 4 | 461 | | } |
| | | 462 | | } |
| | | 463 | | |
| | | 464 | | public ExtremumTypeCheckMethod ExtremumTypeCheckMethod |
| | | 465 | | { |
| | 2 | 466 | | get => _interactionObserver?.ExtremumTypeCheckMethod ?? _extremumTypeCheckMethod; |
| | | 467 | | set |
| | 34 | 468 | | { |
| | 34 | 469 | | if (_interactionObserver != null) |
| | 2 | 470 | | _interactionObserver.ExtremumTypeCheckMethod = value; |
| | 34 | 471 | | _extremumTypeCheckMethod = value; |
| | 34 | 472 | | } |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | public PointCloud3 PointCloud |
| | | 476 | | { |
| | 0 | 477 | | get => ReadObserverRasters(() => |
| | 0 | 478 | | { |
| | 0 | 479 | | var cloud = _interactionObserver?.PointCloud; |
| | 0 | 480 | | return _snapshotSource == null ? cloud : TrackingObservation.CopyCloud(cloud); |
| | 0 | 481 | | }); |
| | 0 | 482 | | set => _interactionObserver.PointCloud = value; |
| | | 483 | | } |
| | | 484 | | |
| | | 485 | | public VectorField2 VectorField |
| | | 486 | | { |
| | 4 | 487 | | get => ReadObserverRasters(() => |
| | 4 | 488 | | { |
| | 4 | 489 | | var vectors = _interactionObserver?.VectorField; |
| | 4 | 490 | | return _snapshotSource == null ? vectors : TrackingObservation.CopyVectors(vectors); |
| | 8 | 491 | | }); |
| | 0 | 492 | | set => _interactionObserver.VectorField = value; |
| | | 493 | | } |
| | | 494 | | |
| | 8 | 495 | | public float InputDistance { get; set; } |
| | | 496 | | |
| | 36 | 497 | | public InteractionManager(IDepthImageManager depthImageManager, IRemoteInteractionProcessorService service, |
| | 36 | 498 | | IPerformanceAggregator performanceAggregator, ITrackingManager trackingManager = null) |
| | 36 | 499 | | { |
| | 36 | 500 | | _depthImageManager = depthImageManager; |
| | 36 | 501 | | _service = service; |
| | 36 | 502 | | _performanceAggregator = performanceAggregator; |
| | 36 | 503 | | _trackingManager = trackingManager; |
| | | 504 | | |
| | 36 | 505 | | _snapshotSource = depthImageManager as DepthImageManager; |
| | 36 | 506 | | if (_snapshotSource != null) |
| | 8 | 507 | | _snapshotSource.SnapshotSourceReady += OnSnapshotSourceReady; |
| | | 508 | | else |
| | 28 | 509 | | depthImageManager.ObservationReady += OnObservationReady; |
| | | 510 | | |
| | 36 | 511 | | if (_trackingManager != null) |
| | 8 | 512 | | _trackingManager.TrackingStateChanged += OnTrackingStateChanged; |
| | 36 | 513 | | } |
| | | 514 | | |
| | | 515 | | public bool UseObservationSnapshots |
| | | 516 | | { |
| | 0 | 517 | | get => _snapshotSource?.UseObservationSnapshots ?? true; |
| | | 518 | | set |
| | 8 | 519 | | { |
| | 8 | 520 | | lock (_snapshotSync) |
| | 8 | 521 | | { |
| | 10 | 522 | | if (_snapshotSource == null || _snapshotSource.UseObservationSnapshots == value) return; |
| | 6 | 523 | | _snapshotSource.UseObservationSnapshots = value; |
| | 6 | 524 | | _pendingObservation = null; |
| | 6 | 525 | | _spareSnapshots.Clear(); |
| | 6 | 526 | | } |
| | 8 | 527 | | } |
| | | 528 | | } |
| | | 529 | | |
| | | 530 | | public void Init(ObserverType type) |
| | 8 | 531 | | { |
| | 8 | 532 | | Type = type; |
| | 8 | 533 | | } |
| | | 534 | | |
| | 6 | 535 | | private void OnTrackingStateChanged(object sender, TrackingStateChangedEventArgs args) => ResetTracking(); |
| | | 536 | | |
| | | 537 | | public void ResetTracking() |
| | 12 | 538 | | { |
| | | 539 | | // Serialize publication with the reset, without waiting for remote analysis to finish. |
| | 12 | 540 | | lock (_publicationSync) |
| | 12 | 541 | | { |
| | 12 | 542 | | lock (_snapshotSync) |
| | 12 | 543 | | { |
| | 12 | 544 | | if (_disposed) return; |
| | 12 | 545 | | UpdateObserver(CreateObserver(Type)); |
| | 12 | 546 | | _pendingObservation = null; |
| | 12 | 547 | | _lastSnapshot = null; |
| | 12 | 548 | | _spareSnapshots.Clear(); |
| | 12 | 549 | | _lastSequence = -1; |
| | 12 | 550 | | _lastObservationTime = double.NegativeInfinity; |
| | 12 | 551 | | _interactions = new List<Interaction>(); |
| | 12 | 552 | | } |
| | 12 | 553 | | InteractionsUpdated?.Invoke(this, _interactions); |
| | 12 | 554 | | VelocitiesUpdated?.Invoke(this, new List<InteractionVelocity>()); |
| | 12 | 555 | | InteractionHistoryUpdated?.Invoke(this, new List<InteractionFrame>()); |
| | 12 | 556 | | } |
| | 12 | 557 | | } |
| | | 558 | | |
| | 10 | 559 | | public void AdvanceTime() => _interactionObserver?.AdvanceTime(GetTimestampSeconds()); |
| | | 560 | | |
| | | 561 | | public async Task<ProcessServiceStatus> Update() |
| | 92 | 562 | | { |
| | 92 | 563 | | await _updateGate.WaitAsync(); |
| | 92 | 564 | | var reservedRasters = false; |
| | | 565 | | try |
| | 92 | 566 | | { |
| | | 567 | | IInteractionObserver observer; |
| | | 568 | | TrackingObservation observation; |
| | 92 | 569 | | lock (_snapshotSync) |
| | 92 | 570 | | { |
| | 92 | 571 | | if (_disposed) return ProcessServiceStatus.Skipped; |
| | 92 | 572 | | observer = _interactionObserver; |
| | 92 | 573 | | if (observer == null) return ProcessServiceStatus.Error; |
| | 92 | 574 | | observation = _pendingObservation; |
| | 92 | 575 | | if (observation is { OwnsRasters: false } && |
| | 92 | 576 | | !(reservedRasters = _snapshotSource.TryReserveRasters())) |
| | 0 | 577 | | { |
| | | 578 | | // Retry on the next tick. Age existing contacts while the producer is busy. |
| | 0 | 579 | | observation = null; |
| | 0 | 580 | | } |
| | | 581 | | else |
| | 92 | 582 | | { |
| | 92 | 583 | | _pendingObservation = null; |
| | | 584 | | // A failed producer update may have already modified the last live pair. |
| | 92 | 585 | | if (reservedRasters && observation.Sequence != _snapshotSource.RasterSequence) |
| | 0 | 586 | | observation = null; |
| | 92 | 587 | | } |
| | 92 | 588 | | if (observation == null || observation.Sequence <= _lastSequence || |
| | 92 | 589 | | observation.TimestampSeconds <= _lastObservationTime) |
| | 10 | 590 | | { |
| | 10 | 591 | | RecycleSnapshot(observation); |
| | 10 | 592 | | observation = null; |
| | 10 | 593 | | } |
| | | 594 | | else |
| | 82 | 595 | | { |
| | 82 | 596 | | _lastSequence = observation.Sequence; |
| | 82 | 597 | | _lastObservationTime = observation.TimestampSeconds; |
| | 82 | 598 | | } |
| | 92 | 599 | | } |
| | 92 | 600 | | if (observation == null) |
| | 10 | 601 | | { |
| | 10 | 602 | | observer.AdvanceTime(GetTimestampSeconds()); |
| | 10 | 603 | | return ProcessServiceStatus.Skipped; |
| | | 604 | | } |
| | 164 | 605 | | try { return (await observer.Update(observation)).ServiceStatus; } |
| | 2 | 606 | | catch (Exception exception) |
| | 2 | 607 | | { |
| | 2 | 608 | | NLog.LogManager.GetCurrentClassLogger().Warn(exception, "Interaction analysis failed; aging active t |
| | | 609 | | // Transport/analysis failures must age the published list, not leave stale contacts. |
| | 2 | 610 | | observer.AdvanceTime(GetTimestampSeconds()); |
| | 2 | 611 | | return ProcessServiceStatus.Error; |
| | | 612 | | } |
| | | 613 | | finally |
| | 82 | 614 | | { |
| | | 615 | | // The observer retains its input for diagnostics and distance calculation. |
| | | 616 | | // Release the previous pair only after the new update has finished. |
| | 82 | 617 | | lock (_snapshotSync) |
| | 82 | 618 | | { |
| | 82 | 619 | | if (ReferenceEquals(observer, _interactionObserver)) |
| | 76 | 620 | | { |
| | 76 | 621 | | RecycleSnapshot(_lastSnapshot); |
| | 76 | 622 | | _lastSnapshot = _disposed || _snapshotSource == null || !observation.OwnsRasters ? null : ob |
| | 76 | 623 | | } |
| | 82 | 624 | | } |
| | 82 | 625 | | } |
| | | 626 | | } |
| | | 627 | | finally |
| | 92 | 628 | | { |
| | 100 | 629 | | if (reservedRasters) _snapshotSource.ReleaseRasters(); |
| | 92 | 630 | | _updateGate.Release(); |
| | 92 | 631 | | } |
| | 92 | 632 | | } |
| | | 633 | | |
| | | 634 | | private void OnObservationReady(object sender, TrackingObservation observation) |
| | 64 | 635 | | { |
| | 64 | 636 | | lock (_snapshotSync) |
| | 64 | 637 | | { |
| | 128 | 638 | | if (!_disposed) _pendingObservation = observation; |
| | 64 | 639 | | } |
| | 64 | 640 | | } |
| | | 641 | | |
| | | 642 | | private void OnSnapshotSourceReady(long sequence, double timestamp, PointCloud3 cloud, VectorField2 vectors) |
| | 30 | 643 | | { |
| | 30 | 644 | | lock (_snapshotSync) |
| | 30 | 645 | | { |
| | 30 | 646 | | if (_disposed) return; |
| | 30 | 647 | | if (!_snapshotSource.UseObservationSnapshots) |
| | 8 | 648 | | { |
| | 8 | 649 | | _pendingObservation = new TrackingObservation(sequence, timestamp, cloud, vectors, null, copyRasters |
| | 8 | 650 | | return; |
| | | 651 | | } |
| | | 652 | | |
| | | 653 | | // A pending pair has no reader. Prefer overwriting it when the consumer lags. |
| | 22 | 654 | | var spare = _pendingObservation; |
| | 22 | 655 | | _pendingObservation = null; |
| | 40 | 656 | | if (spare == null) _spareSnapshots.TryPop(out spare); |
| | 22 | 657 | | _pendingObservation = new TrackingObservation(sequence, timestamp, cloud, vectors, spare); |
| | 22 | 658 | | } |
| | 30 | 659 | | } |
| | | 660 | | |
| | | 661 | | // Called under _snapshotSync. Public event observations are never recycled. |
| | | 662 | | private void RecycleSnapshot(TrackingObservation observation) |
| | 86 | 663 | | { |
| | 86 | 664 | | if (!_disposed && _snapshotSource is { UseObservationSnapshots: true } && |
| | 86 | 665 | | observation is { OwnsRasters: true } && _spareSnapshots.Count < 2) |
| | 10 | 666 | | _spareSnapshots.Push(observation); |
| | 86 | 667 | | } |
| | | 668 | | |
| | | 669 | | public event EventHandler<IList<Interaction>> InteractionsUpdated; |
| | | 670 | | public event EventHandler<IList<InteractionVelocity>> VelocitiesUpdated; |
| | | 671 | | |
| | | 672 | | private void OnNewVelocities(object sender, IList<InteractionVelocity> velocities) |
| | 96 | 673 | | { |
| | 96 | 674 | | lock (_publicationSync) |
| | 96 | 675 | | { |
| | 96 | 676 | | if (_disposed || !ReferenceEquals(sender, _interactionObserver)) return; |
| | 162 | 677 | | var publishedIds = Interactions.Select(item => item.TouchId).ToHashSet(); |
| | 384 | 678 | | foreach (var item in velocities) |
| | 48 | 679 | | { |
| | 48 | 680 | | if (item.IsPublished && !publishedIds.Contains(item.TouchId)) |
| | 2 | 681 | | { |
| | 2 | 682 | | item.IsPublished = false; |
| | 2 | 683 | | item.SuppressionReason = "cluster"; |
| | 2 | 684 | | } |
| | 48 | 685 | | } |
| | 96 | 686 | | VelocitiesUpdated?.Invoke(this, velocities); |
| | 96 | 687 | | } |
| | 96 | 688 | | } |
| | | 689 | | |
| | | 690 | | public event EventHandler<IList<InteractionFrame>> InteractionHistoryUpdated; |
| | | 691 | | |
| | | 692 | | public void Dispose() |
| | 38 | 693 | | { |
| | 38 | 694 | | lock (_publicationSync) |
| | 38 | 695 | | { |
| | 38 | 696 | | lock (_snapshotSync) |
| | 38 | 697 | | { |
| | 40 | 698 | | if (_disposed) return; |
| | 36 | 699 | | _disposed = true; |
| | 36 | 700 | | _pendingObservation = null; |
| | 36 | 701 | | _lastSnapshot = null; |
| | 36 | 702 | | _spareSnapshots.Clear(); |
| | 36 | 703 | | } |
| | | 704 | | |
| | 36 | 705 | | if (_snapshotSource != null) |
| | 8 | 706 | | _snapshotSource.SnapshotSourceReady -= OnSnapshotSourceReady; |
| | | 707 | | else |
| | 28 | 708 | | _depthImageManager.ObservationReady -= OnObservationReady; |
| | | 709 | | |
| | 36 | 710 | | if (_interactionObserver != null) |
| | 36 | 711 | | { |
| | 36 | 712 | | _interactionObserver.NewInteractions -= OnNewInteractions; |
| | 36 | 713 | | _interactionObserver.NewVelocities -= OnNewVelocities; |
| | 36 | 714 | | _interactionObserver.InteractionHistoryUpdated -= OnHistoryUpdated; |
| | | 715 | | |
| | 36 | 716 | | _performanceAggregator.UnregisterReporter(_interactionObserver as IPerformanceReporter); |
| | 36 | 717 | | } |
| | 36 | 718 | | if (_trackingManager != null) |
| | 8 | 719 | | _trackingManager.TrackingStateChanged -= OnTrackingStateChanged; |
| | 36 | 720 | | } |
| | 38 | 721 | | } |
| | | 722 | | |
| | | 723 | | private void OnNewInteractions(object sender, IList<Interaction> interactions) |
| | 96 | 724 | | { |
| | 96 | 725 | | lock (_publicationSync) |
| | 96 | 726 | | { |
| | 96 | 727 | | if (_disposed || !ReferenceEquals(sender, _interactionObserver)) return; |
| | 96 | 728 | | lock (interactions) |
| | 96 | 729 | | { |
| | 164 | 730 | | var copy = interactions.Select(interaction => new Interaction(interaction)).ToList(); |
| | 96 | 731 | | _interactions = RemoveClusteredInteractions(copy); |
| | 96 | 732 | | } |
| | | 733 | | |
| | 96 | 734 | | InteractionsUpdated?.Invoke(this, _interactions); |
| | 96 | 735 | | } |
| | 96 | 736 | | } |
| | | 737 | | |
| | | 738 | | private IList<Interaction> RemoveClusteredInteractions(IList<Interaction> rawInteractions) |
| | 98 | 739 | | { |
| | 98 | 740 | | var filteredCopy = new List<Interaction>(); |
| | 106 | 741 | | rawInteractions = rawInteractions.OrderBy(item => item.TrackingState == InteractionTrackingState.Lost) |
| | 114 | 742 | | .ThenByDescending(item => item.Confidence).ThenBy(item => item.TouchId).ToList(); |
| | 340 | 743 | | for (var i = 0; i < rawInteractions.Count; i++) |
| | 72 | 744 | | { |
| | 72 | 745 | | var currPt = rawInteractions[i]; |
| | 76 | 746 | | var distances = filteredCopy.Select(inter => Point3.Squared2DDistance(inter.Position, currPt.Position)). |
| | | 747 | | |
| | 72 | 748 | | var addPoint = true; |
| | | 749 | | |
| | 144 | 750 | | for (var j = 0; j < distances.Count; j++) |
| | 4 | 751 | | { |
| | 4 | 752 | | var distance = distances[j]; |
| | 4 | 753 | | if (distance < InputDistance) |
| | 4 | 754 | | { |
| | 4 | 755 | | addPoint = false; |
| | 4 | 756 | | break; |
| | | 757 | | } |
| | 0 | 758 | | } |
| | 72 | 759 | | if (addPoint) |
| | 68 | 760 | | filteredCopy.Add(currPt); |
| | 72 | 761 | | } |
| | 98 | 762 | | return filteredCopy; |
| | 98 | 763 | | } |
| | | 764 | | |
| | | 765 | | |
| | | 766 | | } |
| | | 767 | | } |