| | 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 perfItem = new ProcessPerformance(); |
| 0 | 107 | | if (MeasurePerformance) |
| 0 | 108 | | { |
| 0 | 109 | | _stopWatch.Start(); |
| 0 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | if (MeasurePerformance) |
| 0 | 113 | | { |
| 0 | 114 | | _stopWatch.Stop(); |
| 0 | 115 | | perfItem.Preparation = _stopWatch.Elapsed; |
| 0 | 116 | | _stopWatch.Reset(); |
| 0 | 117 | | } |
| | 118 | |
|
| 0 | 119 | | if (MeasurePerformance) |
| 0 | 120 | | { |
| 0 | 121 | | _stopWatch.Start(); |
| 0 | 122 | | } |
| | 123 | |
|
| 0 | 124 | | var candidates = FindExtremaInVectorfield(); |
| | 125 | |
|
| 0 | 126 | | if (MeasurePerformance) |
| 0 | 127 | | { |
| 0 | 128 | | _stopWatch.Stop(); |
| 0 | 129 | | perfItem.Update = _stopWatch.Elapsed; |
| 0 | 130 | | _stopWatch.Reset(); |
| 0 | 131 | | } |
| | 132 | |
|
| 0 | 133 | | if (MeasurePerformance) |
| 0 | 134 | | { |
| 0 | 135 | | _stopWatch.Start(); |
| 0 | 136 | | } |
| | 137 | |
|
| 0 | 138 | | var interactions = ConvertDepthValue(candidates.ToList()); |
| | 139 | |
|
| 0 | 140 | | if (MeasurePerformance) |
| 0 | 141 | | { |
| 0 | 142 | | _stopWatch.Stop(); |
| 0 | 143 | | perfItem.ConvertDepthValue = _stopWatch.Elapsed; |
| 0 | 144 | | _stopWatch.Reset(); |
| 0 | 145 | | } |
| | 146 | |
|
| 0 | 147 | | if (MeasurePerformance) |
| 0 | 148 | | { |
| 0 | 149 | | _stopWatch.Start(); |
| 0 | 150 | | } |
| | 151 | |
|
| 0 | 152 | | var processedInteractions = ComputeExtremumType(interactions, PointCloud.AsJaggedArray()); |
| | 153 | |
|
| 0 | 154 | | var cleanedUpInteractions = RemoveExtremumsBetweenTouches(processedInteractions); |
| | 155 | |
|
| 0 | 156 | | if (MeasurePerformance) |
| 0 | 157 | | { |
| 0 | 158 | | _stopWatch.Stop(); |
| 0 | 159 | | perfItem.ComputeExtremumType = _stopWatch.Elapsed; |
| 0 | 160 | | _stopWatch.Reset(); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | |
|
| 0 | 164 | | if (MeasurePerformance) |
| 0 | 165 | | { |
| 0 | 166 | | _stopWatch.Start(); |
| 0 | 167 | | } |
| | 168 | |
|
| 0 | 169 | | var frame = ComputeSmoothingValue(cleanedUpInteractions); |
| | 170 | |
|
| 0 | 171 | | if (MeasurePerformance) |
| 0 | 172 | | { |
| 0 | 173 | | _stopWatch.Stop(); |
| 0 | 174 | | perfItem.Smoothing = _stopWatch.Elapsed; |
| 0 | 175 | | _stopWatch.Reset(); |
| 0 | 176 | | } |
| | 177 | |
|
| 0 | 178 | | var confidentInteractions = ApplyConfidenceFilter(frame.Interactions); |
| | 179 | |
|
| 0 | 180 | | UpdatePerformanceMetrics(perfItem); |
| | 181 | |
|
| 0 | 182 | | OnNewInteractions(confidentInteractions.ToList()); |
| | 183 | |
|
| 0 | 184 | | return Task.FromResult(processResult); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | /// <summary> |
| | 188 | | /// Initializes the confidence matrix. |
| | 189 | | /// </summary> |
| | 190 | | private void InitializeConfidenceMatrix() |
| 0 | 191 | | { |
| 0 | 192 | | if (VectorField != null && !_isProcessing) |
| 0 | 193 | | ArrayUtils.InitializeArray(out _confidenceMat, VectorField.SizeX, VectorField.SizeY); |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | private IEnumerable<Interaction> FindExtremaInVectorfield() |
| 0 | 197 | | { |
| 0 | 198 | | var stride = VectorField.Stride; |
| 0 | 199 | | var pointCloud = PointCloud.AsJaggedArray(); |
| 0 | 200 | | var vectorField = VectorField.AsJaggedArray(); |
| | 201 | |
|
| 0 | 202 | | var result = new List<Interaction>(); |
| | 203 | |
|
| 0 | 204 | | _isProcessing = true; |
| | 205 | |
|
| 0 | 206 | | Parallel.For(1, (VectorField.SizeX - 1) / stride, (i) => |
| 0 | 207 | | { |
| 0 | 208 | | var x = i * stride; |
| 0 | 209 | |
|
| 0 | 210 | | for (var y = stride; y < VectorField.SizeY - stride; y += stride) |
| 0 | 211 | | { |
| 0 | 212 | | var vCenter = vectorField[x][y]; |
| 0 | 213 | |
|
| 0 | 214 | | if (!vCenter.IsValid) |
| 0 | 215 | | continue; |
| 0 | 216 | |
|
| 0 | 217 | | var vectorX = vCenter.Add(vectorField[x + stride][y]); |
| 0 | 218 | | var vectorX2 = vCenter.Add(vectorField[x - stride][y]); |
| 0 | 219 | | var vectorY = vCenter.Add(vectorField[x][y + stride]); |
| 0 | 220 | | var vectorY2 = vCenter.Add(vectorField[x][y - stride]); |
| 0 | 221 | |
|
| 0 | 222 | | var angleX = vectorX.AngleBetween(vectorX2); |
| 0 | 223 | | var angleY = vectorY.AngleBetween(vectorY2); |
| 0 | 224 | | var angle = angleX + angleY / 2f; |
| 0 | 225 | |
|
| 0 | 226 | | var confidence = _confidenceMat[x][y]; |
| 0 | 227 | | if (float.IsNaN(angle) || angle > MinAngle) |
| 0 | 228 | | { |
| 0 | 229 | | if (confidence > 0) |
| 0 | 230 | | _confidenceMat[x][y]--; |
| 0 | 231 | | } |
| 0 | 232 | | else |
| 0 | 233 | | { |
| 0 | 234 | | lock (result) |
| 0 | 235 | | { |
| 0 | 236 | | result.Add(new Interaction(new Point3(x, y, pointCloud[x][y].Z), InteractionType.None, |
| 0 | 237 | | confidence)); |
| 0 | 238 | | } |
| 0 | 239 | |
|
| 0 | 240 | | if (confidence < MaxConfidence) |
| 0 | 241 | | _confidenceMat[x][y]++; |
| 0 | 242 | | } |
| 0 | 243 | | } |
| 0 | 244 | | }); |
| | 245 | |
|
| 0 | 246 | | _isProcessing = false; |
| | 247 | |
|
| 0 | 248 | | return result.ToArray(); |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | /// <summary> |
| | 252 | | /// Called when [new interactions]. |
| | 253 | | /// </summary> |
| | 254 | | /// <param name="args">The arguments.</param> |
| 0 | 255 | | protected virtual void OnNewInteractions(List<Interaction> args) => NewInteractions?.Invoke(this, args); |
| | 256 | |
|
| | 257 | | } |
| | 258 | |
|
| | 259 | | } |