| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using NLog; |
| | | 5 | | using ReFlex.Core.Common.Components; |
| | | 6 | | using ReFlex.Core.Filtering.Components; |
| | | 7 | | using ReFlex.Core.Interactivity.Util; |
| | | 8 | | using Math = System.Math; |
| | | 9 | | |
| | | 10 | | namespace ReFlex.Core.Interactivity.Components |
| | | 11 | | { |
| | | 12 | | public class InteractionSmoothingBehaviour |
| | | 13 | | { |
| | 1 | 14 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | | 15 | | |
| | | 16 | | #region Fields |
| | | 17 | | |
| | 11 | 18 | | private List<InteractionFrame> _interactionFrames = new List<InteractionFrame>(); |
| | | 19 | | |
| | 11 | 20 | | private int _frameId = 0; |
| | 11 | 21 | | private int _maxTouchId = 0; |
| | 11 | 22 | | private FilterType _type = FilterType.None; |
| | | 23 | | private IPointFilter _filter; |
| | | 24 | | |
| | 11 | 25 | | private float _depthScale = 500f; |
| | | 26 | | |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Properties |
| | | 30 | | |
| | 482 | 31 | | public float TouchMergeDistance2D { get; set; } = 64f; |
| | | 32 | | |
| | 461 | 33 | | public int NumFramesHistory { get; set; } |
| | | 34 | | |
| | 419 | 35 | | public int NumFramesSmoothing { get; set; } |
| | | 36 | | |
| | 539 | 37 | | public int MaxNumEmptyFramesBetween { get; set; } |
| | | 38 | | |
| | 250 | 39 | | public int CurrentFrameId => _frameId; |
| | 250 | 40 | | public int CurrentMaxId => _maxTouchId; |
| | | 41 | | |
| | 507 | 42 | | public int MaxConfidence { get; set; } |
| | | 43 | | |
| | | 44 | | public float DepthScale |
| | | 45 | | { |
| | 0 | 46 | | get => _depthScale; |
| | 0 | 47 | | set => _depthScale = value; |
| | | 48 | | } |
| | | 49 | | |
| | 1000 | 50 | | public InteractionFrame[] InteractionsFramesCache => _interactionFrames.ToArray(); |
| | | 51 | | |
| | | 52 | | #endregion |
| | | 53 | | |
| | | 54 | | #region Constructor |
| | | 55 | | |
| | 11 | 56 | | public InteractionSmoothingBehaviour(int numFramesHistory) |
| | 11 | 57 | | { |
| | 11 | 58 | | NumFramesHistory = numFramesHistory; |
| | 11 | 59 | | _filter = new SimpleMovingAverageFilter((int) Math.Floor(NumFramesHistory / 2d)); |
| | 11 | 60 | | } |
| | | 61 | | |
| | | 62 | | #endregion |
| | | 63 | | |
| | | 64 | | #region public Methods |
| | | 65 | | |
| | | 66 | | public void Reset() |
| | 1 | 67 | | { |
| | 1 | 68 | | _frameId = 0; |
| | 1 | 69 | | _maxTouchId = 0; |
| | 1 | 70 | | _interactionFrames.Clear(); |
| | 1 | 71 | | } |
| | | 72 | | |
| | | 73 | | public void UpdateFilterType(FilterType type) |
| | 11 | 74 | | { |
| | 11 | 75 | | _type = type; |
| | | 76 | | |
| | 11 | 77 | | switch (type) |
| | | 78 | | { |
| | | 79 | | case FilterType.MovingAverage: |
| | 0 | 80 | | _filter = new SimpleMovingAverageFilter((int) Math.Floor(NumFramesHistory / 2d)); |
| | 0 | 81 | | break; |
| | | 82 | | case FilterType.WeightedMovingAverage: |
| | 0 | 83 | | _filter = new WeightedMovingAverageFilter((int) Math.Floor(NumFramesHistory / 2d)); |
| | 0 | 84 | | break; |
| | | 85 | | case FilterType.PolynomialFit: |
| | 0 | 86 | | _filter = new PolynomialFitFilter(); |
| | 0 | 87 | | break; |
| | | 88 | | case FilterType.WeightedPolynomialFit: |
| | 0 | 89 | | _filter = new WeightedPolynomialFitFilter(); |
| | 0 | 90 | | break; |
| | | 91 | | case FilterType.SavitzkyGolay: |
| | 0 | 92 | | { |
| | 0 | 93 | | var sidePoints = NumFramesHistory / 2 - 1; |
| | 0 | 94 | | var order = Math.Min(sidePoints, 3); |
| | 0 | 95 | | _filter = new SavitzkyGolayFilter(sidePoints, order); |
| | 0 | 96 | | break; |
| | | 97 | | } |
| | | 98 | | case FilterType.Butterworth: |
| | 0 | 99 | | _filter = new ButterworthFilter(); |
| | 0 | 100 | | break; |
| | | 101 | | default: |
| | 11 | 102 | | _filter = null; |
| | 11 | 103 | | break; |
| | | 104 | | } |
| | | 105 | | |
| | 11 | 106 | | Logger.Info($"switched Interaction Processing Smoothing Filter to {_filter?.GetType().FullName ?? "None\""}" |
| | 11 | 107 | | } |
| | | 108 | | |
| | | 109 | | public InteractionFrame Update(IList<Interaction> rawInteractions) |
| | 249 | 110 | | { |
| | 249 | 111 | | _frameId++; |
| | | 112 | | |
| | | 113 | | // reconstruct ids by mapping touches in history |
| | 249 | 114 | | var mappedInteractionIds = MapClosestInteraction(rawInteractions.Take(20).ToList()); |
| | | 115 | | |
| | | 116 | | // store raw interactions with associated ids |
| | 249 | 117 | | var newFrame = new InteractionFrame(_frameId, mappedInteractionIds); |
| | | 118 | | |
| | 249 | 119 | | _interactionFrames.Add(newFrame); |
| | | 120 | | |
| | | 121 | | // remove old entries / update when _numFrames has changed |
| | 249 | 122 | | UpdateInteractionFramesList(); |
| | | 123 | | |
| | | 124 | | // create new Frame for smoothed values |
| | 249 | 125 | | var result = new InteractionFrame(_frameId); |
| | | 126 | | |
| | 249 | 127 | | var frames = _interactionFrames.ToArray(); |
| | | 128 | | |
| | 249 | 129 | | var allInteractionIds = frames |
| | 5470 | 130 | | .SelectMany(frame => frame.Interactions.Select(interaction => interaction.TouchId)).Distinct().ToList(); |
| | | 131 | | |
| | 249 | 132 | | allInteractionIds.ForEach(id => |
| | 496 | 133 | | { |
| | 4866 | 134 | | var lastFrameId = frames.OrderByDescending(frame => frame.FrameId).FirstOrDefault(frame => |
| | 2392 | 135 | | frame.Interactions.FirstOrDefault(interaction => Equals(interaction.TouchId, id)) != null) |
| | 496 | 136 | | ?.FrameId ?? -1; |
| | 249 | 137 | | |
| | 249 | 138 | | // remove touch ids that are "too old" --> prevent touch from being still displayed after the finger lef |
| | 249 | 139 | | // if touch id "returns" after a while, do not filter it out |
| | 579 | 140 | | if (_frameId - lastFrameId > MaxNumEmptyFramesBetween && newFrame.Interactions.FirstOrDefault(interactio |
| | 88 | 141 | | return; |
| | 249 | 142 | | |
| | 408 | 143 | | var smoothed = SmoothInteraction(id); |
| | 408 | 144 | | if (smoothed != null) |
| | 408 | 145 | | result.Interactions.Add(smoothed); |
| | 745 | 146 | | }); |
| | | 147 | | |
| | | 148 | | |
| | 249 | 149 | | return result; |
| | 249 | 150 | | } |
| | | 151 | | |
| | | 152 | | #endregion |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// order list of frames by frame id descending and remove old frames |
| | | 156 | | /// </summary> |
| | | 157 | | private void UpdateInteractionFramesList() |
| | 249 | 158 | | { |
| | 249 | 159 | | if (_interactionFrames.Count > NumFramesHistory) |
| | 190 | 160 | | { |
| | | 161 | | try |
| | 190 | 162 | | { |
| | 2280 | 163 | | _interactionFrames = _interactionFrames.OrderByDescending(frame => frame.FrameId) |
| | 190 | 164 | | .Take(NumFramesHistory).ToList(); |
| | 190 | 165 | | } |
| | 0 | 166 | | catch (Exception e) |
| | 0 | 167 | | { |
| | 0 | 168 | | Logger.Error(e, $"Exception catched in {GetType()}.{nameof(this.UpdateInteractionFramesList)}."); |
| | 0 | 169 | | } |
| | | 170 | | |
| | 190 | 171 | | } |
| | 249 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// replaces the frame in the cache with the updated frame (if FrameId is existing, otherwise, nothing is change |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="updatedFrame">Frame containing updated values</param> |
| | | 178 | | public void UpdateCachedFrame(InteractionFrame updatedFrame) |
| | 0 | 179 | | { |
| | 0 | 180 | | var frameIdxToBeReplaced = _interactionFrames.FindIndex((f) => f.FrameId == updatedFrame.FrameId); |
| | 0 | 181 | | if (frameIdxToBeReplaced < 0) |
| | 0 | 182 | | return; |
| | | 183 | | |
| | | 184 | | // _interactionFrames[frameIdxToBeReplaced] = updatedFrame; |
| | 0 | 185 | | _interactionFrames[frameIdxToBeReplaced].Interactions.ForEach((interaction) => |
| | 0 | 186 | | { |
| | 0 | 187 | | var updatedInteraction = |
| | 0 | 188 | | updatedFrame.Interactions.FirstOrDefault((update) => update.TouchId == interaction.TouchId); |
| | 0 | 189 | | if (updatedInteraction != null) |
| | 0 | 190 | | { |
| | 0 | 191 | | interaction.Confidence = updatedInteraction.Confidence; |
| | 0 | 192 | | interaction.ExtremumDescription = updatedInteraction.ExtremumDescription; |
| | 0 | 193 | | |
| | 0 | 194 | | } |
| | 0 | 195 | | }); |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Apply smoothing according to chosen filter type. |
| | | 200 | | /// Extracts touches of a given id from frames history and sends them to smoothing algorithm |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="touchId">the id of the touch, which should be smoothed</param> |
| | | 203 | | /// <returns>Interaction (last entry in history) with smoothed position</returns> |
| | | 204 | | private Interaction SmoothInteraction(int touchId) |
| | 408 | 205 | | { |
| | 408 | 206 | | var interactionsHistory = new List<Interaction>(); |
| | | 207 | | |
| | 3962 | 208 | | var frames = new List<InteractionFrame>(_interactionFrames.OrderBy(frame => frame.FrameId)); |
| | | 209 | | |
| | 408 | 210 | | frames.ForEach(frame => |
| | 3554 | 211 | | { |
| | 3554 | 212 | | var lastTouch = |
| | 8046 | 213 | | frame.Interactions.FirstOrDefault(interaction => Equals(interaction.TouchId, touchId)); |
| | 3554 | 214 | | if (lastTouch == null) |
| | 577 | 215 | | return; |
| | 408 | 216 | | |
| | 2977 | 217 | | interactionsHistory.Add(new Interaction(lastTouch)); |
| | 3962 | 218 | | }); |
| | | 219 | | |
| | 408 | 220 | | var smooth = interactionsHistory.LastOrDefault(); |
| | | 221 | | |
| | 3385 | 222 | | var raw = interactionsHistory.Select(interaction => interaction.Position).ToList(); |
| | | 223 | | |
| | 408 | 224 | | if (NumFramesSmoothing > 0 && |
| | 408 | 225 | | smooth != null && raw.Count >= NumFramesSmoothing && |
| | 408 | 226 | | _type != FilterType.None && _filter != null) |
| | 0 | 227 | | { |
| | 0 | 228 | | var framesForSmoothing = raw.Take(NumFramesSmoothing).ToList(); |
| | 0 | 229 | | framesForSmoothing.ForEach(p => p.Z *= _depthScale); |
| | 0 | 230 | | smooth.Position = _filter.Process(framesForSmoothing).First(); |
| | 0 | 231 | | smooth.Position.Z /= _depthScale; |
| | 0 | 232 | | framesForSmoothing.ForEach(p => p.Z /= _depthScale); |
| | 0 | 233 | | } |
| | | 234 | | |
| | 408 | 235 | | return smooth; |
| | 408 | 236 | | } |
| | | 237 | | |
| | | 238 | | |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Try to identify an interaction that can be associated with the given interaction (from another frame, or smo |
| | | 242 | | /// returns null, if no interaction can be found which is within the <see cref="TouchMergeDistance2D"/>. |
| | | 243 | | /// </summary> |
| | | 244 | | /// <param name="frameToSearch"></param> |
| | | 245 | | /// <param name="source"></param> |
| | | 246 | | /// <returns></returns> |
| | | 247 | | private List<Interaction> MapClosestInteraction(List<Interaction> rawInteractions) |
| | 249 | 248 | | { |
| | | 249 | | // no past interactions: assign Ids |
| | 249 | 250 | | if (_interactionFrames.Count == 0) |
| | 11 | 251 | | { |
| | 11 | 252 | | rawInteractions.ForEach(AssignMaxId); |
| | 11 | 253 | | return rawInteractions; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | // reset touch id to negative value |
| | 564 | 257 | | rawInteractions.ForEach(Interaction => Interaction.TouchId = -1); |
| | | 258 | | |
| | | 259 | | // step 1: look in past frames for |
| | | 260 | | |
| | 4395 | 261 | | var pastFrames = _interactionFrames.Where(frame => frame.Interactions.Count > 0).OrderByDescending(frame => |
| | | 262 | | |
| | 238 | 263 | | var result = new List<Interaction>(); |
| | | 264 | | |
| | 238 | 265 | | var candidates = rawInteractions.ToArray(); |
| | | 266 | | |
| | 238 | 267 | | var i = pastFrames.Length - 1; |
| | | 268 | | |
| | 595 | 269 | | while (candidates.Length != 0 && i >= 0) |
| | 357 | 270 | | { |
| | | 271 | | // List : candidateIdx, Array<touchId, distances> (ordered by distance desc)) |
| | 357 | 272 | | var distances = |
| | 456 | 273 | | candidates.Select((interaction, idx) => Tuple.Create(idx, |
| | 456 | 274 | | pastFrames[i].Interactions |
| | 654 | 275 | | .Select(otherInteraction => Tuple.Create(otherInteraction, Point3.Squared2DDistance(interact |
| | 654 | 276 | | .OrderBy(tpl => tpl.Item2) |
| | 456 | 277 | | .ToList())) |
| | 357 | 278 | | .ToList(); |
| | | 279 | | |
| | 357 | 280 | | var duplicateCount = distances.Count; |
| | | 281 | | |
| | 714 | 282 | | while (duplicateCount != 0) |
| | 357 | 283 | | { |
| | | 284 | | // // if a point has no corresponding point |
| | | 285 | | // distances.Where(dist => dist.Item2.Count == 0).ToList().ForEach(tpl => |
| | | 286 | | // { |
| | | 287 | | // var interaction = new Interaction(candidates[tpl.Item1]); |
| | | 288 | | // AssignMaxId(interaction); |
| | | 289 | | // result.Add(interaction); |
| | | 290 | | // } |
| | | 291 | | // ); |
| | | 292 | | |
| | | 293 | | // remove all points which have no next point |
| | 813 | 294 | | distances.RemoveAll(dist => dist.Item2.Count == 0); |
| | | 295 | | |
| | 1725 | 296 | | var duplicates = distances.Where(dist => dist.Item2.Count != 0).GroupBy(tpl => tpl.Item1).Where(grou |
| | | 297 | | |
| | 357 | 298 | | duplicateCount = duplicateCount > 0 ? duplicates.Count : duplicateCount; |
| | | 299 | | |
| | 357 | 300 | | duplicates.ForEach( |
| | 357 | 301 | | duplicate => |
| | 0 | 302 | | { |
| | 0 | 303 | | var ordered = duplicate.ToList().OrderBy(elem => elem.Item2[0].Item2).ToList(); |
| | 0 | 304 | | for (var n = 1; n < ordered.Count; n++) |
| | 0 | 305 | | { |
| | 357 | 306 | | // remove duplicate distance |
| | 0 | 307 | | ordered[n].Item2 |
| | 0 | 308 | | .RemoveAt(0); |
| | 0 | 309 | | } |
| | 357 | 310 | | }); |
| | 357 | 311 | | } |
| | | 312 | | |
| | 357 | 313 | | distances.ForEach(dist => |
| | 456 | 314 | | { |
| | 456 | 315 | | if (dist.Item2[0].Item2 < TouchMergeDistance2D) |
| | 301 | 316 | | { |
| | 301 | 317 | | var interaction = new Interaction(candidates[dist.Item1]); |
| | 301 | 318 | | interaction.TouchId = dist.Item2[0].Item1.TouchId; |
| | 357 | 319 | | |
| | 301 | 320 | | interaction.Confidence = ComputeConfidence(interaction.TouchId, interaction.Confidence, pastFram |
| | 301 | 321 | | candidates[dist.Item1].TouchId = dist.Item2[0].Item1.TouchId; |
| | 357 | 322 | | |
| | 357 | 323 | | // prevent adding duplicate id's |
| | 400 | 324 | | var alreadyAdded = result.FirstOrDefault(inter => Equals(inter.TouchId, interaction.TouchId)); |
| | 357 | 325 | | |
| | 357 | 326 | | // id does not exist - add point |
| | 301 | 327 | | if (alreadyAdded == null) |
| | 301 | 328 | | { |
| | 301 | 329 | | result.Add(interaction); |
| | 301 | 330 | | } |
| | 357 | 331 | | else |
| | 0 | 332 | | { |
| | 357 | 333 | | // TODO: find better selection of associated point (distance and time) |
| | 0 | 334 | | if (alreadyAdded.Confidence < interaction.Confidence) |
| | 0 | 335 | | { |
| | 0 | 336 | | result.Remove(alreadyAdded); |
| | 0 | 337 | | interaction.Confidence++; |
| | 0 | 338 | | result.Add(interaction); |
| | 0 | 339 | | } |
| | 357 | 340 | | else |
| | 0 | 341 | | { |
| | 0 | 342 | | alreadyAdded.Confidence = interaction.Confidence; |
| | 0 | 343 | | } |
| | 357 | 344 | | |
| | 0 | 345 | | } |
| | 301 | 346 | | } |
| | 813 | 347 | | }); |
| | | 348 | | |
| | 813 | 349 | | candidates = candidates.Where(interaction => interaction.TouchId < 0).ToArray(); |
| | | 350 | | |
| | 357 | 351 | | i--; |
| | 357 | 352 | | } |
| | | 353 | | |
| | 238 | 354 | | var newInteractions = candidates.ToList(); |
| | 238 | 355 | | newInteractions.ForEach(AssignMaxId); |
| | | 356 | | |
| | 238 | 357 | | result.AddRange(newInteractions); |
| | | 358 | | |
| | 564 | 359 | | return result.OrderBy(interaction => interaction.TouchId).ToList(); |
| | 249 | 360 | | } |
| | | 361 | | |
| | | 362 | | /// <summary> |
| | | 363 | | /// Assigns a new id to the given point and increments current maximum touch id. |
| | | 364 | | /// </summary> |
| | | 365 | | /// <param name="interaction">the touch point which should get a new unique id</param> |
| | | 366 | | private void AssignMaxId(Interaction interaction) |
| | 38 | 367 | | { |
| | 38 | 368 | | interaction.TouchId = _maxTouchId; |
| | 38 | 369 | | _maxTouchId++; |
| | 38 | 370 | | } |
| | | 371 | | |
| | | 372 | | private int ComputeConfidence(int touchId, float currentConfidence, InteractionFrame[] pastFrames) |
| | 301 | 373 | | { |
| | | 374 | | // find maximum confidence in history for touch id |
| | 3142 | 375 | | var maxExistingConfidence = pastFrames.SelectMany(frames => frames.Interactions) |
| | 7871 | 376 | | .Where(inter => Equals(inter.TouchId, touchId)).Max(inter => inter.Confidence); |
| | | 377 | | |
| | | 378 | | // increment and clamp to max value |
| | 301 | 379 | | return (int) Math.Min(Math.Max(currentConfidence, maxExistingConfidence) + 1, MaxConfidence); |
| | 301 | 380 | | } |
| | | 381 | | } |
| | | 382 | | } |