| | | 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 | | |
| | 0 | 22 | | private bool _isProcessing = false; |
| | | 23 | | |
| | 0 | 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 | | { |
| | 0 | 47 | | get => _pointCloud; |
| | | 48 | | set |
| | 0 | 49 | | { |
| | 0 | 50 | | if (_pointCloud == value) |
| | 0 | 51 | | return; |
| | | 52 | | |
| | 0 | 53 | | _pointCloud = value; |
| | 0 | 54 | | InitializeConfidenceMatrix(); |
| | 0 | 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 | | { |
| | 0 | 66 | | get => _vectorField; |
| | | 67 | | set |
| | 0 | 68 | | { |
| | 0 | 69 | | if (_vectorField == value) |
| | 0 | 70 | | return; |
| | | 71 | | |
| | 0 | 72 | | _vectorField = value; |
| | 0 | 73 | | InitializeConfidenceMatrix(); |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | #endregion |
| | | 78 | | |
| | | 79 | | #region Events |
| | | 80 | | public override event EventHandler<IList<Interaction>> NewInteractions; |
| | | 81 | | |
| | | 82 | | #endregion |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Initializes a new instance of the <see cref="MultiInteractionObserver"/> class. |
| | | 86 | | /// </summary> |
| | 0 | 87 | | public MultiInteractionObserver() |
| | 0 | 88 | | { |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Updates this instance. |
| | | 93 | | /// </summary> |
| | | 94 | | public override Task<ProcessingResult> Update() |
| | 0 | 95 | | { |
| | 0 | 96 | | if (PointCloud == null || |
| | 0 | 97 | | VectorField == null || |
| | 0 | 98 | | _confidenceMat == null) |
| | 0 | 99 | | return Task.FromResult(new ProcessingResult(ProcessServiceStatus.Error)); |
| | | 100 | | |
| | 0 | 101 | | var processResult = new ProcessingResult(ProcessServiceStatus.Available); |
| | | 102 | | |
| | 0 | 103 | | if (_isProcessing) |
| | 0 | 104 | | return Task.FromResult(new ProcessingResult(ProcessServiceStatus.Skipped)); |
| | | 105 | | |
| | 0 | 106 | | var processingDateTime = DateTime.Now.Ticks; |
| | 0 | 107 | | var perfItem = new ProcessPerformance(); |
| | 0 | 108 | | if (MeasurePerformance) |
| | 0 | 109 | | { |
| | 0 | 110 | | _stopWatch.Start(); |
| | 0 | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | if (MeasurePerformance) |
| | 0 | 114 | | { |
| | 0 | 115 | | _stopWatch.Stop(); |
| | 0 | 116 | | perfItem.Preparation = _stopWatch.Elapsed; |
| | 0 | 117 | | _stopWatch.Reset(); |
| | 0 | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | if (MeasurePerformance) |
| | 0 | 121 | | { |
| | 0 | 122 | | _stopWatch.Start(); |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | var candidates = FindExtremaInVectorfield(); |
| | | 126 | | |
| | 0 | 127 | | if (MeasurePerformance) |
| | 0 | 128 | | { |
| | 0 | 129 | | _stopWatch.Stop(); |
| | 0 | 130 | | perfItem.Update = _stopWatch.Elapsed; |
| | 0 | 131 | | _stopWatch.Reset(); |
| | 0 | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | if (MeasurePerformance) |
| | 0 | 135 | | { |
| | 0 | 136 | | _stopWatch.Start(); |
| | 0 | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | var interactions = ConvertDepthValue(candidates.ToList()); |
| | | 140 | | |
| | 0 | 141 | | if (MeasurePerformance) |
| | 0 | 142 | | { |
| | 0 | 143 | | _stopWatch.Stop(); |
| | 0 | 144 | | perfItem.ConvertDepthValue = _stopWatch.Elapsed; |
| | 0 | 145 | | _stopWatch.Reset(); |
| | 0 | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | if (MeasurePerformance) |
| | 0 | 149 | | { |
| | 0 | 150 | | _stopWatch.Start(); |
| | 0 | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | var processedInteractions = ComputeExtremumType(interactions, PointCloud.AsJaggedArray()); |
| | | 154 | | |
| | 0 | 155 | | var cleanedUpInteractions = RemoveExtremumsBetweenTouches(processedInteractions); |
| | | 156 | | |
| | 0 | 157 | | if (MeasurePerformance) |
| | 0 | 158 | | { |
| | 0 | 159 | | _stopWatch.Stop(); |
| | 0 | 160 | | perfItem.ComputeExtremumType = _stopWatch.Elapsed; |
| | 0 | 161 | | _stopWatch.Reset(); |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | |
| | 0 | 165 | | if (MeasurePerformance) |
| | 0 | 166 | | { |
| | 0 | 167 | | _stopWatch.Start(); |
| | 0 | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | var frame = ComputeSmoothingValue(cleanedUpInteractions); |
| | | 171 | | |
| | 0 | 172 | | if (MeasurePerformance) |
| | 0 | 173 | | { |
| | 0 | 174 | | _stopWatch.Stop(); |
| | 0 | 175 | | perfItem.Smoothing = _stopWatch.Elapsed; |
| | 0 | 176 | | _stopWatch.Reset(); |
| | 0 | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | var confidentInteractions = ApplyConfidenceFilter(frame.Interactions); |
| | | 180 | | |
| | 0 | 181 | | UpdatePerformanceMetrics(perfItem, processingDateTime); |
| | | 182 | | |
| | 0 | 183 | | OnNewInteractions(confidentInteractions.ToList()); |
| | | 184 | | |
| | 0 | 185 | | return Task.FromResult(processResult); |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Initializes the confidence matrix. |
| | | 190 | | /// </summary> |
| | | 191 | | private void InitializeConfidenceMatrix() |
| | 0 | 192 | | { |
| | 0 | 193 | | if (VectorField != null && !_isProcessing) |
| | 0 | 194 | | ArrayUtils.InitializeArray(out _confidenceMat, VectorField.SizeX, VectorField.SizeY); |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | private IEnumerable<Interaction> FindExtremaInVectorfield() |
| | 0 | 198 | | { |
| | 0 | 199 | | var stride = VectorField.Stride; |
| | 0 | 200 | | var pointCloud = PointCloud.AsJaggedArray(); |
| | 0 | 201 | | var vectorField = VectorField.AsJaggedArray(); |
| | | 202 | | |
| | 0 | 203 | | var result = new List<Interaction>(); |
| | | 204 | | |
| | 0 | 205 | | _isProcessing = true; |
| | | 206 | | |
| | 0 | 207 | | Parallel.For(1, (VectorField.SizeX - 1) / stride, (i) => |
| | 0 | 208 | | { |
| | 0 | 209 | | var x = i * stride; |
| | 0 | 210 | | |
| | 0 | 211 | | for (var y = stride; y < VectorField.SizeY - stride; y += stride) |
| | 0 | 212 | | { |
| | 0 | 213 | | var vCenter = vectorField[x][y]; |
| | 0 | 214 | | |
| | 0 | 215 | | if (!vCenter.IsValid) |
| | 0 | 216 | | continue; |
| | 0 | 217 | | |
| | 0 | 218 | | var vectorX = vCenter.Add(vectorField[x + stride][y]); |
| | 0 | 219 | | var vectorX2 = vCenter.Add(vectorField[x - stride][y]); |
| | 0 | 220 | | var vectorY = vCenter.Add(vectorField[x][y + stride]); |
| | 0 | 221 | | var vectorY2 = vCenter.Add(vectorField[x][y - stride]); |
| | 0 | 222 | | |
| | 0 | 223 | | var angleX = vectorX.AngleBetween(vectorX2); |
| | 0 | 224 | | var angleY = vectorY.AngleBetween(vectorY2); |
| | 0 | 225 | | var angle = angleX + angleY / 2f; |
| | 0 | 226 | | |
| | 0 | 227 | | var confidence = _confidenceMat[x][y]; |
| | 0 | 228 | | if (float.IsNaN(angle) || angle > MinAngle) |
| | 0 | 229 | | { |
| | 0 | 230 | | if (confidence > 0) |
| | 0 | 231 | | _confidenceMat[x][y]--; |
| | 0 | 232 | | } |
| | 0 | 233 | | else |
| | 0 | 234 | | { |
| | 0 | 235 | | lock (result) |
| | 0 | 236 | | { |
| | 0 | 237 | | result.Add(new Interaction(new Point3(x, y, pointCloud[x][y].Z), InteractionType.None, |
| | 0 | 238 | | confidence)); |
| | 0 | 239 | | } |
| | 0 | 240 | | |
| | 0 | 241 | | if (confidence < MaxConfidence) |
| | 0 | 242 | | _confidenceMat[x][y]++; |
| | 0 | 243 | | } |
| | 0 | 244 | | } |
| | 0 | 245 | | }); |
| | | 246 | | |
| | 0 | 247 | | _isProcessing = false; |
| | | 248 | | |
| | 0 | 249 | | return result.ToArray(); |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Called when [new interactions]. |
| | | 254 | | /// </summary> |
| | | 255 | | /// <param name="args">The arguments.</param> |
| | 0 | 256 | | protected virtual void OnNewInteractions(List<Interaction> args) => NewInteractions?.Invoke(this, args); |
| | | 257 | | |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | } |