| | 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 | | using Math = System.Math; |
| | 10 | |
|
| | 11 | | namespace ReFlex.Core.Interactivity.Components |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Observes a depthimage and reports single touch interactions. |
| | 15 | | /// </summary> |
| | 16 | | /// <seealso cref="IInteractionObserver" /> |
| | 17 | | /// <inheritdoc /> |
| | 18 | | /// <seealso cref="T:ReFlex.Core.Interactivity.Interfaces.IInteractionObserver" /> |
| | 19 | | public class SingleInteractionObserver : InteractionObserverBase |
| | 20 | | { |
| 0 | 21 | | private readonly Stopwatch _stopWatch = new(); |
| | 22 | |
|
| | 23 | | #region Properties |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Gets the type. |
| | 27 | | /// </summary> |
| | 28 | | /// <value> |
| | 29 | | /// The type. |
| | 30 | | /// </value> |
| 0 | 31 | | public override ObserverType Type => ObserverType.SingleTouch; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets or sets the source. |
| | 35 | | /// </summary> |
| | 36 | | /// <value> |
| | 37 | | /// The source. |
| | 38 | | /// </value> |
| 0 | 39 | | public override PointCloud3 PointCloud { get; set; } |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| | 42 | | /// <summary> |
| | 43 | | /// Gets or sets the vector field. |
| | 44 | | /// </summary> |
| | 45 | | /// <value> |
| | 46 | | /// The vector field. |
| | 47 | | /// </value> |
| 0 | 48 | | public override VectorField2 VectorField { get; set; } |
| | 49 | |
|
| | 50 | | #endregion |
| | 51 | |
|
| | 52 | | #region Events |
| | 53 | |
|
| | 54 | | public override event EventHandler<IList<Interaction>> NewInteractions; |
| | 55 | |
|
| | 56 | | #endregion |
| | 57 | |
|
| | 58 | | public override Task<ProcessingResult> Update() |
| 0 | 59 | | { |
| 0 | 60 | | if (PointCloud == null) |
| 0 | 61 | | return Task.FromResult(new ProcessingResult(ProcessServiceStatus.Error)); |
| | 62 | |
|
| 0 | 63 | | var processResult = new ProcessingResult(ProcessServiceStatus.Available); |
| 0 | 64 | | var perfItem = new ProcessPerformance(); |
| | 65 | |
|
| 0 | 66 | | if (MeasurePerformance) |
| 0 | 67 | | { |
| 0 | 68 | | _stopWatch.Start(); |
| 0 | 69 | | } |
| 0 | 70 | | var extreme = FindGlobalExtreme(PointCloud, Distance); |
| 0 | 71 | | if (MeasurePerformance) |
| 0 | 72 | | { |
| 0 | 73 | | _stopWatch.Stop(); |
| 0 | 74 | | perfItem.Update = _stopWatch.Elapsed; |
| 0 | 75 | | _stopWatch.Reset(); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | float depth; |
| | 79 | | InteractionType type; |
| | 80 | |
|
| | 81 | |
|
| 0 | 82 | | if (MeasurePerformance) |
| 0 | 83 | | { |
| 0 | 84 | | _stopWatch.Start(); |
| 0 | 85 | | } |
| | 86 | |
|
| 0 | 87 | | var extremumType = ComputeExtremumType(PointCloud.AsJaggedArray(), (int) extreme.X, (int) extreme.Y); |
| | 88 | |
|
| 0 | 89 | | if (MeasurePerformance) |
| 0 | 90 | | { |
| 0 | 91 | | _stopWatch.Stop(); |
| 0 | 92 | | perfItem.ComputeExtremumType = _stopWatch.Elapsed; |
| 0 | 93 | | _stopWatch.Reset(); |
| 0 | 94 | | } |
| | 95 | |
|
| 0 | 96 | | if (MeasurePerformance) |
| 0 | 97 | | { |
| 0 | 98 | | _stopWatch.Start(); |
| 0 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | if (extreme.Z < Distance + MinDistance && extreme.Z > Distance - MinDistance) |
| 0 | 102 | | { |
| 0 | 103 | | type = InteractionType.None; |
| 0 | 104 | | depth = 0; |
| 0 | 105 | | } |
| 0 | 106 | | else if (extreme.Z < Distance) |
| 0 | 107 | | { |
| 0 | 108 | | type = InteractionType.Push; |
| 0 | 109 | | depth = (Distance - extreme.Z - MinDistance) / (MinDistance - MaxDistance); |
| | 110 | |
|
| 0 | 111 | | if (depth > 0) |
| 0 | 112 | | depth = 0; |
| | 113 | |
|
| 0 | 114 | | if (depth < -1) |
| 0 | 115 | | depth = -1; |
| 0 | 116 | | } |
| | 117 | | else |
| 0 | 118 | | { |
| 0 | 119 | | type = InteractionType.Pull; |
| 0 | 120 | | depth = (extreme.Z - MinDistance - Distance) / (MaxDistance - MinDistance); |
| | 121 | |
|
| 0 | 122 | | if (depth < 0) |
| 0 | 123 | | depth = 0; |
| | 124 | |
|
| 0 | 125 | | if (depth > 1) |
| 0 | 126 | | depth = 1; |
| 0 | 127 | | } |
| | 128 | |
|
| 0 | 129 | | extreme.Z = depth; |
| | 130 | |
|
| 0 | 131 | | if (MeasurePerformance) |
| 0 | 132 | | { |
| 0 | 133 | | _stopWatch.Stop(); |
| 0 | 134 | | perfItem.ConvertDepthValue = _stopWatch.Elapsed; |
| 0 | 135 | | _stopWatch.Reset(); |
| 0 | 136 | | } |
| | 137 | |
|
| 0 | 138 | | if (MeasurePerformance) |
| 0 | 139 | | { |
| 0 | 140 | | _stopWatch.Start(); |
| 0 | 141 | | } |
| | 142 | |
|
| 0 | 143 | | var TOLERANCE = 0.001; |
| | 144 | |
|
| 0 | 145 | | var result = new List<Interaction>(); |
| | 146 | |
|
| 0 | 147 | | if (Math.Abs(depth) > TOLERANCE) |
| 0 | 148 | | { |
| 0 | 149 | | var interaction = new Interaction(extreme, type, 1); |
| 0 | 150 | | interaction.ExtremumDescription = extremumType; |
| | 151 | |
|
| 0 | 152 | | var frame = ComputeSmoothingValue(interaction.AsList()); |
| | 153 | |
|
| 0 | 154 | | var smoothed = frame.Interactions; |
| | 155 | |
|
| 0 | 156 | | if (smoothed.Count > 0) |
| 0 | 157 | | { |
| 0 | 158 | | result = smoothed.Take(1).ToList(); |
| | 159 | |
|
| 0 | 160 | | var lastTime = smoothed.Max(item => item.Time); |
| 0 | 161 | | var id = smoothed.Max(item => item.TouchId); |
| 0 | 162 | | var last = smoothed.FirstOrDefault( |
| 0 | 163 | | it => Equals(it.Time, lastTime) && Equals(it.TouchId, id)); |
| | 164 | |
|
| 0 | 165 | | if (last != null) |
| 0 | 166 | | { |
| 0 | 167 | | var smoothedItem = new Interaction(last) |
| 0 | 168 | | { |
| 0 | 169 | | TouchId = id |
| 0 | 170 | | }; |
| | 171 | |
|
| 0 | 172 | | result = new List<Interaction> { smoothedItem }; |
| 0 | 173 | | } |
| 0 | 174 | | } |
| | 175 | | else |
| 0 | 176 | | { |
| 0 | 177 | | result = smoothed; |
| 0 | 178 | | } |
| 0 | 179 | | } |
| | 180 | |
|
| 0 | 181 | | if (MeasurePerformance) |
| 0 | 182 | | { |
| 0 | 183 | | _stopWatch.Stop(); |
| 0 | 184 | | perfItem.Smoothing = _stopWatch.Elapsed; |
| 0 | 185 | | _stopWatch.Reset(); |
| 0 | 186 | | } |
| | 187 | |
|
| 0 | 188 | | UpdatePerformanceMetrics(perfItem); |
| | 189 | |
|
| 0 | 190 | | NewInteractions?.Invoke(this, result); |
| | 191 | |
|
| 0 | 192 | | return Task.FromResult(processResult); |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | /// <summary> |
| | 196 | | /// Finds the global extreme. |
| | 197 | | /// </summary> |
| | 198 | | /// <param name="source">The source.</param> |
| | 199 | | /// <param name="averageDepth">The average depth.</param> |
| | 200 | | /// <returns></returns> |
| | 201 | | public static Point3 FindGlobalExtreme(PointCloud3 source, float averageDepth) |
| 0 | 202 | | { |
| 0 | 203 | | var candidate = new Point3(0, 0, averageDepth); |
| | 204 | |
|
| 0 | 205 | | if (source == null) |
| 0 | 206 | | return candidate; |
| | 207 | |
|
| 0 | 208 | | var candidates = source.AsJaggedArray(); |
| | 209 | |
|
| 0 | 210 | | for (var x = 0; x < source.SizeX; ++x) |
| 0 | 211 | | { |
| 0 | 212 | | for (var y = 0; y < source.SizeY; ++y) |
| 0 | 213 | | { |
| 0 | 214 | | var curr = candidates[x][y]; |
| | 215 | |
|
| 0 | 216 | | if (!curr.IsValid) |
| 0 | 217 | | continue; |
| | 218 | |
|
| 0 | 219 | | if (!(Math.Abs(curr.Z - averageDepth) |
| 0 | 220 | | >= Math.Abs(candidate.Z - averageDepth))) |
| 0 | 221 | | continue; |
| | 222 | |
|
| 0 | 223 | | candidate.X = x; |
| 0 | 224 | | candidate.Y = y; |
| 0 | 225 | | candidate.Z = candidates[x][y].Z; |
| 0 | 226 | | } |
| 0 | 227 | | } |
| | 228 | |
|
| 0 | 229 | | return candidate; |
| 0 | 230 | | } |
| | 231 | | } |
| | 232 | |
|
| | 233 | | } |