| | 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(); |
| 0 | 65 | | var start = DateTime.Now.Ticks; |
| | 66 | |
|
| 0 | 67 | | if (MeasurePerformance) |
| 0 | 68 | | { |
| 0 | 69 | | _stopWatch.Start(); |
| 0 | 70 | | } |
| 0 | 71 | | var extreme = FindGlobalExtreme(PointCloud, Distance); |
| 0 | 72 | | if (MeasurePerformance) |
| 0 | 73 | | { |
| 0 | 74 | | _stopWatch.Stop(); |
| 0 | 75 | | perfItem.Update = _stopWatch.Elapsed; |
| 0 | 76 | | _stopWatch.Reset(); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | float depth; |
| | 80 | | InteractionType type; |
| | 81 | |
|
| | 82 | |
|
| 0 | 83 | | if (MeasurePerformance) |
| 0 | 84 | | { |
| 0 | 85 | | _stopWatch.Start(); |
| 0 | 86 | | } |
| | 87 | |
|
| 0 | 88 | | var extremumType = ComputeExtremumType(PointCloud.AsJaggedArray(), (int) extreme.X, (int) extreme.Y); |
| | 89 | |
|
| 0 | 90 | | if (MeasurePerformance) |
| 0 | 91 | | { |
| 0 | 92 | | _stopWatch.Stop(); |
| 0 | 93 | | perfItem.ComputeExtremumType = _stopWatch.Elapsed; |
| 0 | 94 | | _stopWatch.Reset(); |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | if (MeasurePerformance) |
| 0 | 98 | | { |
| 0 | 99 | | _stopWatch.Start(); |
| 0 | 100 | | } |
| | 101 | |
|
| 0 | 102 | | if (extreme.Z < Distance + MinDistance && extreme.Z > Distance - MinDistance) |
| 0 | 103 | | { |
| 0 | 104 | | type = InteractionType.None; |
| 0 | 105 | | depth = 0; |
| 0 | 106 | | } |
| 0 | 107 | | else if (extreme.Z < Distance) |
| 0 | 108 | | { |
| 0 | 109 | | type = InteractionType.Push; |
| 0 | 110 | | depth = (Distance - extreme.Z - MinDistance) / (MinDistance - MaxDistance); |
| | 111 | |
|
| 0 | 112 | | if (depth > 0) |
| 0 | 113 | | depth = 0; |
| | 114 | |
|
| 0 | 115 | | if (depth < -1) |
| 0 | 116 | | depth = -1; |
| 0 | 117 | | } |
| | 118 | | else |
| 0 | 119 | | { |
| 0 | 120 | | type = InteractionType.Pull; |
| 0 | 121 | | depth = (extreme.Z - MinDistance - Distance) / (MaxDistance - MinDistance); |
| | 122 | |
|
| 0 | 123 | | if (depth < 0) |
| 0 | 124 | | depth = 0; |
| | 125 | |
|
| 0 | 126 | | if (depth > 1) |
| 0 | 127 | | depth = 1; |
| 0 | 128 | | } |
| | 129 | |
|
| 0 | 130 | | extreme.Z = depth; |
| | 131 | |
|
| 0 | 132 | | if (MeasurePerformance) |
| 0 | 133 | | { |
| 0 | 134 | | _stopWatch.Stop(); |
| 0 | 135 | | perfItem.ConvertDepthValue = _stopWatch.Elapsed; |
| 0 | 136 | | _stopWatch.Reset(); |
| 0 | 137 | | } |
| | 138 | |
|
| 0 | 139 | | if (MeasurePerformance) |
| 0 | 140 | | { |
| 0 | 141 | | _stopWatch.Start(); |
| 0 | 142 | | } |
| | 143 | |
|
| 0 | 144 | | var TOLERANCE = 0.001; |
| | 145 | |
|
| 0 | 146 | | var result = new List<Interaction>(); |
| | 147 | |
|
| 0 | 148 | | if (Math.Abs(depth) > TOLERANCE) |
| 0 | 149 | | { |
| 0 | 150 | | var interaction = new Interaction(extreme, type, 1); |
| 0 | 151 | | interaction.ExtremumDescription = extremumType; |
| | 152 | |
|
| 0 | 153 | | var frame = ComputeSmoothingValue(interaction.AsList()); |
| | 154 | |
|
| 0 | 155 | | var smoothed = frame.Interactions; |
| | 156 | |
|
| 0 | 157 | | if (smoothed.Count > 0) |
| 0 | 158 | | { |
| 0 | 159 | | result = smoothed.Take(1).ToList(); |
| | 160 | |
|
| 0 | 161 | | var lastTime = smoothed.Max(item => item.Time); |
| 0 | 162 | | var id = smoothed.Max(item => item.TouchId); |
| 0 | 163 | | var last = smoothed.FirstOrDefault( |
| 0 | 164 | | it => Equals(it.Time, lastTime) && Equals(it.TouchId, id)); |
| | 165 | |
|
| 0 | 166 | | if (last != null) |
| 0 | 167 | | { |
| 0 | 168 | | var smoothedItem = new Interaction(last) |
| 0 | 169 | | { |
| 0 | 170 | | TouchId = id |
| 0 | 171 | | }; |
| | 172 | |
|
| 0 | 173 | | result = new List<Interaction> { smoothedItem }; |
| 0 | 174 | | } |
| 0 | 175 | | } |
| | 176 | | else |
| 0 | 177 | | { |
| 0 | 178 | | result = smoothed; |
| 0 | 179 | | } |
| 0 | 180 | | } |
| | 181 | |
|
| 0 | 182 | | if (MeasurePerformance) |
| 0 | 183 | | { |
| 0 | 184 | | _stopWatch.Stop(); |
| 0 | 185 | | perfItem.Smoothing = _stopWatch.Elapsed; |
| 0 | 186 | | _stopWatch.Reset(); |
| 0 | 187 | | } |
| | 188 | |
|
| 0 | 189 | | UpdatePerformanceMetrics(perfItem, start); |
| | 190 | |
|
| 0 | 191 | | NewInteractions?.Invoke(this, result); |
| | 192 | |
|
| 0 | 193 | | return Task.FromResult(processResult); |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | /// <summary> |
| | 197 | | /// Finds the global extreme. |
| | 198 | | /// </summary> |
| | 199 | | /// <param name="source">The source.</param> |
| | 200 | | /// <param name="averageDepth">The average depth.</param> |
| | 201 | | /// <returns></returns> |
| | 202 | | public static Point3 FindGlobalExtreme(PointCloud3 source, float averageDepth) |
| 0 | 203 | | { |
| 0 | 204 | | var candidate = new Point3(0, 0, averageDepth); |
| | 205 | |
|
| 0 | 206 | | if (source == null) |
| 0 | 207 | | return candidate; |
| | 208 | |
|
| 0 | 209 | | var candidates = source.AsJaggedArray(); |
| | 210 | |
|
| 0 | 211 | | for (var x = 0; x < source.SizeX; ++x) |
| 0 | 212 | | { |
| 0 | 213 | | for (var y = 0; y < source.SizeY; ++y) |
| 0 | 214 | | { |
| 0 | 215 | | var curr = candidates[x][y]; |
| | 216 | |
|
| 0 | 217 | | if (!curr.IsValid) |
| 0 | 218 | | continue; |
| | 219 | |
|
| 0 | 220 | | if (!(Math.Abs(curr.Z - averageDepth) |
| 0 | 221 | | >= Math.Abs(candidate.Z - averageDepth))) |
| 0 | 222 | | continue; |
| | 223 | |
|
| 0 | 224 | | candidate.X = x; |
| 0 | 225 | | candidate.Y = y; |
| 0 | 226 | | candidate.Z = candidates[x][y].Z; |
| 0 | 227 | | } |
| 0 | 228 | | } |
| | 229 | |
|
| 0 | 230 | | return candidate; |
| 0 | 231 | | } |
| | 232 | | } |
| | 233 | |
|
| | 234 | | } |