| | 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.Util; |
| | 8 | | using ReFlex.Core.Interactivity.Interfaces; |
| | 9 | |
|
| | 10 | | namespace ReFlex.Core.Interactivity.Components; |
| | 11 | |
|
| | 12 | | public class RemoteInteractionProcessor: InteractionObserverBase |
| | 13 | | { |
| | 14 | | private readonly IRemoteInteractionProcessorService _service; |
| 0 | 15 | | private readonly Stopwatch _stopWatch = new(); |
| | 16 | |
|
| 0 | 17 | | public RemoteInteractionProcessor(IRemoteInteractionProcessorService service) |
| 0 | 18 | | { |
| 0 | 19 | | _service = service; |
| 0 | 20 | | } |
| | 21 | |
|
| 0 | 22 | | public override ObserverType Type { get; } = ObserverType.Remote; |
| 0 | 23 | | public override PointCloud3 PointCloud { get; set; } |
| 0 | 24 | | public override VectorField2 VectorField { get; set; } |
| | 25 | |
|
| | 26 | | public override event EventHandler<IList<Interaction>> NewInteractions; |
| | 27 | |
|
| | 28 | | public override async Task<ProcessingResult> Update() |
| 0 | 29 | | { |
| 0 | 30 | | if (!_service.IsConnected) |
| 0 | 31 | | { |
| 0 | 32 | | var success = await _service.Connect(); |
| 0 | 33 | | if (!success) |
| 0 | 34 | | { |
| 0 | 35 | | return new ProcessingResult(ProcessServiceStatus.Error); |
| | 36 | | } |
| 0 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | if (_service.IsBusy) |
| 0 | 40 | | return new ProcessingResult(); |
| | 41 | |
|
| 0 | 42 | | var result = new ProcessingResult(ProcessServiceStatus.Available); |
| | 43 | |
|
| 0 | 44 | | var processed = await _service.Update(PointCloud, result.PerformanceMeasurement, MeasurePerformance); |
| | 45 | |
|
| 0 | 46 | | var candidates = processed.Item1; |
| 0 | 47 | | var perfItem = new ProcessPerformance { Preparation = processed.Item2.Preparation, Update = processed.Item2.Upda |
| 0 | 48 | | if (MeasurePerformance) |
| 0 | 49 | | { |
| 0 | 50 | | _stopWatch.Start(); |
| 0 | 51 | | } |
| | 52 | |
|
| 0 | 53 | | var interactions = ConvertDepthValue(candidates.ToList()); |
| | 54 | |
|
| 0 | 55 | | if (MeasurePerformance) |
| 0 | 56 | | { |
| 0 | 57 | | _stopWatch.Stop(); |
| 0 | 58 | | perfItem.ConvertDepthValue = _stopWatch.Elapsed; |
| 0 | 59 | | _stopWatch.Reset(); |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | if (MeasurePerformance) |
| 0 | 63 | | { |
| 0 | 64 | | _stopWatch.Start(); |
| 0 | 65 | | } |
| 0 | 66 | | var frame = ComputeSmoothingValue(interactions); |
| 0 | 67 | | if (MeasurePerformance) |
| 0 | 68 | | { |
| 0 | 69 | | _stopWatch.Stop(); |
| 0 | 70 | | perfItem.Smoothing = _stopWatch.Elapsed; |
| 0 | 71 | | _stopWatch.Reset(); |
| 0 | 72 | | } |
| | 73 | |
|
| 0 | 74 | | if (MeasurePerformance) |
| 0 | 75 | | { |
| 0 | 76 | | _stopWatch.Start(); |
| 0 | 77 | | } |
| 0 | 78 | | var processedInteractions = ComputeExtremumType(frame.Interactions, PointCloud.AsJaggedArray()); |
| 0 | 79 | | if (MeasurePerformance) |
| 0 | 80 | | { |
| 0 | 81 | | _stopWatch.Stop(); |
| 0 | 82 | | perfItem.ComputeExtremumType = _stopWatch.Elapsed; |
| 0 | 83 | | _stopWatch.Reset(); |
| 0 | 84 | | } |
| | 85 | |
|
| 0 | 86 | | UpdatePerformanceMetrics(perfItem); |
| | 87 | |
|
| 0 | 88 | | NewInteractions?.Invoke(this, processedInteractions); |
| | 89 | |
|
| 0 | 90 | | return result; |
| 0 | 91 | | } |
| | 92 | | } |