| | | 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 | | |
| | | 9 | | namespace ReFlex.Core.Interactivity.Components |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Finds multiple Interactions on a 2d vectorfield. |
| | | 13 | | /// </summary> |
| | | 14 | | public class MultiInteractionObserver : InteractionObserverBase |
| | | 15 | | { |
| | | 16 | | #region Fields |
| | | 17 | | |
| | | 18 | | private PointCloud3 _pointCloud; |
| | | 19 | | private VectorField2 _vectorField; |
| | | 20 | | private int[][] _confidenceMat; |
| | | 21 | | |
| | 12 | 22 | | private bool _isProcessing = false; |
| | | 23 | | |
| | 12 | 24 | | private readonly Stopwatch _stopWatch = new(); |
| | | 25 | | |
| | | 26 | | |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Properties |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the type. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <value> |
| | | 35 | | /// The type. |
| | | 36 | | /// </value> |
| | 0 | 37 | | public override ObserverType Type => ObserverType.MultiTouch; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or sets the point cloud. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <value> |
| | | 43 | | /// The point cloud. |
| | | 44 | | /// </value> |
| | | 45 | | public override PointCloud3 PointCloud |
| | | 46 | | { |
| | 24 | 47 | | get => _pointCloud; |
| | | 48 | | set |
| | 14 | 49 | | { |
| | 14 | 50 | | if (_pointCloud == value) |
| | 0 | 51 | | return; |
| | | 52 | | |
| | 14 | 53 | | _pointCloud = value; |
| | 14 | 54 | | InitializeConfidenceMatrix(); |
| | 14 | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets or sets the vector field. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <value> |
| | | 62 | | /// The vector field. |
| | | 63 | | /// </value> |
| | | 64 | | public override VectorField2 VectorField |
| | | 65 | | { |
| | 14 | 66 | | get => _vectorField; |
| | | 67 | | set |
| | 4 | 68 | | { |
| | 4 | 69 | | if (_vectorField == value) |
| | 4 | 70 | | return; |
| | | 71 | | |
| | 0 | 72 | | _vectorField = value; |
| | 0 | 73 | | InitializeConfidenceMatrix(); |
| | 4 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | #endregion |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Initializes a new instance of the <see cref="MultiInteractionObserver"/> class. |
| | | 81 | | /// </summary> |
| | 12 | 82 | | public MultiInteractionObserver() |
| | 12 | 83 | | { |
| | 12 | 84 | | } |
| | | 85 | | |
| | | 86 | | protected override Task<ProcessingResult> CheckInitialState() |
| | 2 | 87 | | { |
| | 2 | 88 | | if (PointCloud == null || |
| | 2 | 89 | | VectorField == null || |
| | 2 | 90 | | _confidenceMat == null) |
| | 2 | 91 | | return Task.FromResult(new ProcessingResult(ProcessServiceStatus.Error)); |
| | | 92 | | |
| | 0 | 93 | | var processResult = new ProcessingResult(ProcessServiceStatus.Available); |
| | | 94 | | |
| | 0 | 95 | | return Task.FromResult( |
| | 0 | 96 | | _isProcessing |
| | 0 | 97 | | ? new ProcessingResult(ProcessServiceStatus.Skipped) |
| | 0 | 98 | | : processResult); |
| | 2 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Initializes the confidence matrix. |
| | | 103 | | /// </summary> |
| | | 104 | | private void InitializeConfidenceMatrix() |
| | 14 | 105 | | { |
| | 14 | 106 | | if (VectorField != null && !_isProcessing) |
| | 0 | 107 | | ArrayUtils.InitializeArray(out _confidenceMat, VectorField.SizeX, VectorField.SizeY); |
| | 14 | 108 | | } |
| | | 109 | | |
| | | 110 | | protected override Task<Tuple<IEnumerable<Interaction>, ProcessPerformance>> Analyze(ProcessPerformance performa |
| | 0 | 111 | | { |
| | 0 | 112 | | performance.Preparation = TimeSpan.Zero; |
| | | 113 | | |
| | 0 | 114 | | var stride = VectorField.Stride; |
| | 0 | 115 | | var pointCloud = PointCloud.AsJaggedArray(); |
| | 0 | 116 | | var vectorField = VectorField.AsJaggedArray(); |
| | | 117 | | |
| | 0 | 118 | | var result = new List<Interaction>(); |
| | | 119 | | |
| | 0 | 120 | | _isProcessing = true; |
| | | 121 | | |
| | 0 | 122 | | Parallel.For(1, (VectorField.SizeX - 1) / stride, (i) => |
| | 0 | 123 | | { |
| | 0 | 124 | | var x = i * stride; |
| | 0 | 125 | | |
| | 0 | 126 | | for (var y = stride; y < VectorField.SizeY - stride; y += stride) |
| | 0 | 127 | | { |
| | 0 | 128 | | var vCenter = vectorField[x][y]; |
| | 0 | 129 | | |
| | 0 | 130 | | if (!vCenter.IsValid) |
| | 0 | 131 | | continue; |
| | 0 | 132 | | |
| | 0 | 133 | | var vectorX = vCenter.Add(vectorField[x + stride][y]); |
| | 0 | 134 | | var vectorX2 = vCenter.Add(vectorField[x - stride][y]); |
| | 0 | 135 | | var vectorY = vCenter.Add(vectorField[x][y + stride]); |
| | 0 | 136 | | var vectorY2 = vCenter.Add(vectorField[x][y - stride]); |
| | 0 | 137 | | |
| | 0 | 138 | | var angleX = vectorX.AngleBetween(vectorX2); |
| | 0 | 139 | | var angleY = vectorY.AngleBetween(vectorY2); |
| | 0 | 140 | | var angle = angleX + angleY / 2f; |
| | 0 | 141 | | |
| | 0 | 142 | | var confidence = _confidenceMat[x][y]; |
| | 0 | 143 | | if (float.IsNaN(angle) || angle > MinAngle) |
| | 0 | 144 | | { |
| | 0 | 145 | | if (confidence > 0) |
| | 0 | 146 | | _confidenceMat[x][y]--; |
| | 0 | 147 | | } |
| | 0 | 148 | | else |
| | 0 | 149 | | { |
| | 0 | 150 | | lock (result) |
| | 0 | 151 | | { |
| | 0 | 152 | | result.Add(new Interaction(new Point3(x, y, pointCloud[x][y].Z), InteractionType.None, |
| | 0 | 153 | | confidence)); |
| | 0 | 154 | | } |
| | 0 | 155 | | |
| | 0 | 156 | | if (confidence < MaxConfidence) |
| | 0 | 157 | | _confidenceMat[x][y]++; |
| | 0 | 158 | | } |
| | 0 | 159 | | } |
| | 0 | 160 | | }); |
| | | 161 | | |
| | 0 | 162 | | _isProcessing = false; |
| | | 163 | | |
| | 0 | 164 | | return Task.FromResult(new Tuple<IEnumerable<Interaction>, ProcessPerformance>(result.ToArray(), performance |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | } |