| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Implementation.Interfaces; |
| | | 6 | | using ReFlex.Core.Common.Components; |
| | | 7 | | using ReFlex.Core.Common.Interfaces; |
| | | 8 | | using ReFlex.Core.Common.Util; |
| | | 9 | | using ReFlex.Core.Interactivity.Components; |
| | | 10 | | using ReFlex.Core.Interactivity.Interfaces; |
| | | 11 | | using ReFlex.Core.Interactivity.Util; |
| | | 12 | | |
| | | 13 | | namespace Implementation.Components |
| | | 14 | | { |
| | | 15 | | public class InteractionManager : IInteractionManager, IDisposable |
| | | 16 | | { |
| | | 17 | | private IInteractionObserver _interactionObserver; |
| | | 18 | | private IList<Interaction> _interactions; |
| | | 19 | | private readonly IDepthImageManager _depthImageManager; |
| | | 20 | | private readonly IPerformanceAggregator _performanceAggregator; |
| | | 21 | | |
| | | 22 | | private readonly IRemoteInteractionProcessorService _service; |
| | | 23 | | |
| | | 24 | | private float _distance; |
| | | 25 | | private float _minDistance; |
| | | 26 | | private float _maxDistance; |
| | | 27 | | private float _minAngle; |
| | | 28 | | private int _minConfidence; |
| | | 29 | | private int _maxConfidence; |
| | | 30 | | private int _numSmoothFrames; |
| | | 31 | | private int _interactionHistorySize; |
| | | 32 | | private int _maxNumEmptyFramesBetween; |
| | 0 | 33 | | private float _smoothingDistanceSquared = 64f; |
| | 0 | 34 | | private float _depthScale = 100.0f; |
| | | 35 | | |
| | 0 | 36 | | public IList<Interaction> Interactions => _interactions ?? new List<Interaction>(); |
| | | 37 | | |
| | | 38 | | public ObserverType Type |
| | | 39 | | { |
| | 0 | 40 | | get => _interactionObserver?.Type ?? ObserverType.None; |
| | | 41 | | set |
| | 0 | 42 | | { |
| | 0 | 43 | | switch (value) |
| | | 44 | | { |
| | | 45 | | case ObserverType.Remote: |
| | 0 | 46 | | UpdateObserver(new RemoteInteractionProcessor(_service)); |
| | 0 | 47 | | break; |
| | | 48 | | case ObserverType.MultiTouch: |
| | 0 | 49 | | UpdateObserver(new MultiInteractionObserver()); |
| | | 50 | | |
| | 0 | 51 | | break; |
| | | 52 | | case ObserverType.SingleTouch: |
| | 0 | 53 | | UpdateObserver(new SingleInteractionObserver()); |
| | | 54 | | |
| | 0 | 55 | | break; |
| | | 56 | | case ObserverType.None: |
| | 0 | 57 | | UpdateObserver(new NoInteractionObserver()); |
| | 0 | 58 | | break; |
| | | 59 | | default: |
| | 0 | 60 | | throw new ArgumentOutOfRangeException(nameof(value), value, null); |
| | | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public float ComputeZeroPlaneDistance() |
| | 0 | 66 | | { |
| | 0 | 67 | | if (_interactionObserver == null) |
| | 0 | 68 | | return 0; |
| | | 69 | | |
| | 0 | 70 | | return _interactionObserver.CalculateAverageDistance(); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private void UpdateObserver(IInteractionObserver observer) |
| | 0 | 74 | | { |
| | 0 | 75 | | if (_interactionObserver != null) |
| | 0 | 76 | | { |
| | 0 | 77 | | _interactionObserver.NewInteractions -= OnNewInteractions; |
| | 0 | 78 | | _interactionObserver.InteractionHistoryUpdated -= OnHistoryUpdated; |
| | 0 | 79 | | _performanceAggregator.UnregisterReporter(observer as IPerformanceReporter); |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | if (observer == null) |
| | 0 | 83 | | return; |
| | | 84 | | |
| | 0 | 85 | | observer.Distance = _distance; |
| | 0 | 86 | | observer.MinDistance = _minDistance; |
| | 0 | 87 | | observer.MaxDistance = _maxDistance; |
| | 0 | 88 | | observer.MinConfidence = _minConfidence; |
| | 0 | 89 | | observer.MaxConfidence = _maxConfidence; |
| | 0 | 90 | | observer.MinAngle = _minAngle; |
| | 0 | 91 | | observer.PointCloud = _depthImageManager.PointCloud; |
| | 0 | 92 | | observer.VectorField = _depthImageManager.VectorField; |
| | 0 | 93 | | observer.NumSmoothingFrames = _numSmoothFrames; |
| | 0 | 94 | | observer.InteractionHistorySize = _interactionHistorySize; |
| | 0 | 95 | | observer.TouchMergeDistance2D = _smoothingDistanceSquared; |
| | 0 | 96 | | observer.MaxNumEmptyFramesBetween = _maxNumEmptyFramesBetween; |
| | 0 | 97 | | observer.DepthScale = _depthScale; |
| | 0 | 98 | | _interactionObserver = observer; |
| | | 99 | | |
| | 0 | 100 | | _interactionObserver.NewInteractions += OnNewInteractions; |
| | 0 | 101 | | _interactionObserver.InteractionHistoryUpdated += OnHistoryUpdated; |
| | 0 | 102 | | _performanceAggregator.RegisterReporter(_interactionObserver as IPerformanceReporter); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | private void OnHistoryUpdated(object sender, IList<InteractionFrame> e) |
| | 0 | 106 | | { |
| | 0 | 107 | | InteractionHistoryUpdated?.Invoke(this, e); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | public float Distance |
| | | 111 | | { |
| | 0 | 112 | | get => _interactionObserver?.Distance ?? 0; |
| | | 113 | | set |
| | 0 | 114 | | { |
| | 0 | 115 | | if (_interactionObserver != null) |
| | 0 | 116 | | _interactionObserver.Distance = value; |
| | | 117 | | |
| | 0 | 118 | | _distance = value; |
| | 0 | 119 | | } |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | public float MinDistance |
| | | 123 | | { |
| | 0 | 124 | | get => _interactionObserver?.MinDistance ?? 0; |
| | | 125 | | set |
| | 0 | 126 | | { |
| | 0 | 127 | | if (_interactionObserver != null) |
| | 0 | 128 | | _interactionObserver.MinDistance = value; |
| | | 129 | | |
| | 0 | 130 | | _minDistance = value; |
| | 0 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public float MaxDistance |
| | | 135 | | { |
| | 0 | 136 | | get => _interactionObserver?.MaxDistance ?? 0; |
| | | 137 | | set |
| | 0 | 138 | | { |
| | 0 | 139 | | if (_interactionObserver != null) |
| | 0 | 140 | | _interactionObserver.MaxDistance = value; |
| | | 141 | | |
| | 0 | 142 | | _maxDistance = value; |
| | 0 | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public float MinAngle |
| | | 147 | | { |
| | 0 | 148 | | get => _interactionObserver?.MinAngle ?? 0; |
| | | 149 | | set |
| | 0 | 150 | | { |
| | 0 | 151 | | if (_interactionObserver != null) |
| | 0 | 152 | | _interactionObserver.MinAngle = value; |
| | | 153 | | |
| | 0 | 154 | | _minAngle = value; |
| | 0 | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | public int MinConfidence |
| | | 159 | | { |
| | 0 | 160 | | get => (int)(_interactionObserver?.MinConfidence ?? 0); |
| | | 161 | | set |
| | 0 | 162 | | { |
| | 0 | 163 | | if (_interactionObserver != null) |
| | 0 | 164 | | _interactionObserver.MinConfidence = value; |
| | | 165 | | |
| | 0 | 166 | | _minConfidence = value; |
| | 0 | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | public int MaxConfidence |
| | | 171 | | { |
| | 0 | 172 | | get => (int)(_interactionObserver?.MaxConfidence ?? 0); |
| | | 173 | | set |
| | 0 | 174 | | { |
| | 0 | 175 | | if (_interactionObserver != null) |
| | 0 | 176 | | _interactionObserver.MaxConfidence = value; |
| | | 177 | | |
| | 0 | 178 | | _maxConfidence = value; |
| | 0 | 179 | | } |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | public int InteractionHistorySize |
| | | 183 | | { |
| | 0 | 184 | | get => _interactionObserver?.InteractionHistorySize ?? 0; |
| | | 185 | | set |
| | 0 | 186 | | { |
| | 0 | 187 | | if (_interactionObserver != null) |
| | 0 | 188 | | _interactionObserver.InteractionHistorySize = value; |
| | | 189 | | |
| | 0 | 190 | | _interactionHistorySize = value; |
| | 0 | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | public int NumSmoothingFrames |
| | | 195 | | { |
| | 0 | 196 | | get => _interactionObserver?.NumSmoothingFrames ?? 0; |
| | | 197 | | set |
| | 0 | 198 | | { |
| | 0 | 199 | | if (_interactionObserver != null) |
| | 0 | 200 | | _interactionObserver.NumSmoothingFrames = value; |
| | | 201 | | |
| | 0 | 202 | | _numSmoothFrames = value; |
| | 0 | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | public int MaxNumEmptyFramesBetween |
| | | 207 | | { |
| | 0 | 208 | | get => _interactionObserver?.MaxNumEmptyFramesBetween ?? 0; |
| | | 209 | | set |
| | 0 | 210 | | { |
| | 0 | 211 | | if (_interactionObserver != null) |
| | 0 | 212 | | _interactionObserver.MaxNumEmptyFramesBetween = value; |
| | | 213 | | |
| | 0 | 214 | | _maxNumEmptyFramesBetween = value; |
| | 0 | 215 | | } |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | public float TouchMergeDistance2D |
| | | 219 | | { |
| | 0 | 220 | | get => _interactionObserver?.TouchMergeDistance2D ?? 0f; |
| | | 221 | | set |
| | 0 | 222 | | { |
| | 0 | 223 | | if (_interactionObserver != null) |
| | 0 | 224 | | _interactionObserver.TouchMergeDistance2D = value; |
| | | 225 | | |
| | 0 | 226 | | _smoothingDistanceSquared = value; |
| | 0 | 227 | | } |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | public float DepthScale |
| | | 231 | | { |
| | 0 | 232 | | get => _interactionObserver?.DepthScale ?? 0f; |
| | | 233 | | set |
| | 0 | 234 | | { |
| | 0 | 235 | | if (_interactionObserver != null) |
| | 0 | 236 | | _interactionObserver.DepthScale = value; |
| | | 237 | | |
| | 0 | 238 | | _depthScale = value; |
| | 0 | 239 | | } |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | public FilterType FilterType { |
| | 0 | 243 | | get => _interactionObserver?.FilterType ?? FilterType.None; |
| | | 244 | | set |
| | 0 | 245 | | { |
| | 0 | 246 | | if (_interactionObserver != null) |
| | 0 | 247 | | _interactionObserver.FilterType = value; |
| | 0 | 248 | | } |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | public int ExtremumTypeCheckNumSamples |
| | | 252 | | { |
| | 0 | 253 | | get => _interactionObserver?.ExtremumTypeCheckNumSamples ?? 0; |
| | | 254 | | set |
| | 0 | 255 | | { |
| | 0 | 256 | | if (_interactionObserver != null) |
| | 0 | 257 | | _interactionObserver.ExtremumTypeCheckNumSamples = value; |
| | 0 | 258 | | } |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | public int ExtremumTypeCheckRadius |
| | | 262 | | { |
| | 0 | 263 | | get => _interactionObserver?.ExtremumTypeCheckRadius ?? 0; |
| | | 264 | | set |
| | 0 | 265 | | { |
| | 0 | 266 | | if (_interactionObserver != null) |
| | 0 | 267 | | _interactionObserver.ExtremumTypeCheckRadius = value; |
| | 0 | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | public float ExtremumTypeCheckFittingPercentage |
| | | 272 | | { |
| | 0 | 273 | | get => _interactionObserver?.ExtremumTypeCheckFittingPercentage ?? 0f; |
| | | 274 | | set |
| | 0 | 275 | | { |
| | 0 | 276 | | if (_interactionObserver != null) |
| | 0 | 277 | | _interactionObserver.ExtremumTypeCheckFittingPercentage = value; |
| | 0 | 278 | | } |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | public ExtremumTypeCheckMethod ExtremumTypeCheckMethod |
| | | 282 | | { |
| | 0 | 283 | | get => _interactionObserver?.ExtremumTypeCheckMethod ?? ExtremumTypeCheckMethod.Global; |
| | | 284 | | set |
| | 0 | 285 | | { |
| | 0 | 286 | | if (_interactionObserver != null) |
| | 0 | 287 | | _interactionObserver.ExtremumTypeCheckMethod = value; |
| | 0 | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | public PointCloud3 PointCloud |
| | | 292 | | { |
| | 0 | 293 | | get => _interactionObserver?.PointCloud; |
| | 0 | 294 | | set => _interactionObserver.PointCloud = value; |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | public VectorField2 VectorField |
| | | 298 | | { |
| | 0 | 299 | | get => _interactionObserver?.VectorField; |
| | 0 | 300 | | set => _interactionObserver.VectorField = value; |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | public float InputDistance { get; set; } |
| | | 304 | | |
| | 0 | 305 | | public InteractionManager(IDepthImageManager depthImageManager, IRemoteInteractionProcessorService service, IPer |
| | 0 | 306 | | { |
| | 0 | 307 | | _depthImageManager = depthImageManager; |
| | 0 | 308 | | _service = service; |
| | 0 | 309 | | _performanceAggregator = performanceAggregator; |
| | | 310 | | |
| | 0 | 311 | | depthImageManager.PointcloudFiltered += OnPointcloudChanged; |
| | 0 | 312 | | depthImageManager.VectorfieldChanged += OnVectorfieldChanged; |
| | 0 | 313 | | } |
| | | 314 | | |
| | | 315 | | public void Init(ObserverType type) |
| | 0 | 316 | | { |
| | 0 | 317 | | Type = type; |
| | 0 | 318 | | } |
| | | 319 | | |
| | | 320 | | public async Task<ProcessServiceStatus> Update() |
| | 0 | 321 | | { |
| | 0 | 322 | | if (_interactionObserver == null) |
| | 0 | 323 | | return ProcessServiceStatus.Error; |
| | | 324 | | |
| | 0 | 325 | | return (await _interactionObserver.Update())?.ServiceStatus ?? ProcessServiceStatus.Error; |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | public event EventHandler<IList<Interaction>> InteractionsUpdated; |
| | | 329 | | |
| | | 330 | | public event EventHandler<IList<InteractionFrame>> InteractionHistoryUpdated; |
| | | 331 | | |
| | | 332 | | public void Dispose() |
| | 0 | 333 | | { |
| | 0 | 334 | | if (_depthImageManager != null) |
| | 0 | 335 | | { |
| | 0 | 336 | | _depthImageManager.PointcloudFiltered -= OnPointcloudChanged; |
| | 0 | 337 | | _depthImageManager.VectorfieldChanged -= OnVectorfieldChanged; |
| | 0 | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | if (_interactionObserver != null) |
| | 0 | 341 | | { |
| | 0 | 342 | | _interactionObserver.NewInteractions -= OnNewInteractions; |
| | 0 | 343 | | _interactionObserver.InteractionHistoryUpdated -= OnHistoryUpdated; |
| | | 344 | | |
| | 0 | 345 | | _performanceAggregator.UnregisterReporter(_interactionObserver as IPerformanceReporter); |
| | 0 | 346 | | } |
| | 0 | 347 | | } |
| | | 348 | | |
| | | 349 | | private void OnPointcloudChanged(object sender, PointCloud3 source) |
| | 0 | 350 | | { |
| | 0 | 351 | | if (_interactionObserver != null) |
| | 0 | 352 | | _interactionObserver.PointCloud = source; |
| | 0 | 353 | | } |
| | | 354 | | |
| | | 355 | | private void OnVectorfieldChanged(object sender, VectorField2 source) |
| | 0 | 356 | | { |
| | 0 | 357 | | if (_interactionObserver != null) |
| | 0 | 358 | | _interactionObserver.VectorField = source; |
| | 0 | 359 | | } |
| | | 360 | | |
| | | 361 | | private void OnNewInteractions(object sender, IList<Interaction> interactions) |
| | 0 | 362 | | { |
| | 0 | 363 | | lock (interactions) |
| | 0 | 364 | | { |
| | 0 | 365 | | var copy = interactions.Select(interaction => new Interaction(interaction)).ToList(); |
| | 0 | 366 | | _interactions = RemoveClusteredInteractions(copy); |
| | 0 | 367 | | } |
| | | 368 | | |
| | 0 | 369 | | InteractionsUpdated?.Invoke(this, _interactions); |
| | 0 | 370 | | } |
| | | 371 | | |
| | | 372 | | private IList<Interaction> RemoveClusteredInteractions(IList<Interaction> rawInteractions) |
| | 0 | 373 | | { |
| | 0 | 374 | | var filteredCopy = new List<Interaction>(); |
| | | 375 | | |
| | 0 | 376 | | for (var i = 0; i < rawInteractions.Count; i++) |
| | 0 | 377 | | { |
| | 0 | 378 | | var currPt = rawInteractions[i]; |
| | 0 | 379 | | var distances = filteredCopy.Select(inter => Point3.Squared2DDistance(inter.Position, currPt.Position)). |
| | | 380 | | |
| | 0 | 381 | | var addPoint = true; |
| | | 382 | | |
| | 0 | 383 | | for (var j = 0; j < distances.Count; j++) |
| | 0 | 384 | | { |
| | 0 | 385 | | var distance = distances[j]; |
| | 0 | 386 | | if (distance < InputDistance) |
| | 0 | 387 | | { |
| | 0 | 388 | | addPoint = false; |
| | 0 | 389 | | break; |
| | | 390 | | } |
| | 0 | 391 | | } |
| | 0 | 392 | | if (addPoint) |
| | 0 | 393 | | filteredCopy.Add(currPt); |
| | 0 | 394 | | } |
| | 0 | 395 | | return filteredCopy; |
| | 0 | 396 | | } |
| | | 397 | | |
| | | 398 | | |
| | | 399 | | } |
| | | 400 | | } |