TouchTracker.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Recognition.Tracking;
  3. using BBIWARG.Utility;
  4. using System.Collections.Generic;
  5. namespace BBIWARG.Recognition.TouchRecognition
  6. {
  7. /// <summary>
  8. /// Keeps track of touches over a period of time and generates touch events
  9. /// </summary>
  10. internal class TouchTracker : Tracker<Touch, TrackedTouch>
  11. {
  12. /// <summary>
  13. /// the touch events in the current frame
  14. /// </summary>
  15. private List<TouchEvent> touchEvents;
  16. /// <summary>
  17. /// Initializes a new instance of the TouchTracker class.
  18. /// </summary>
  19. /// <param name="imageSize">Size of the input image.</param>
  20. public TouchTracker(ImageSize imageSize)
  21. : base(imageSize)
  22. {
  23. touchEvents = new List<TouchEvent>();
  24. }
  25. /// <summary>
  26. /// Calculates the similarity [0-1] of a tracked touch and a detected touch.
  27. /// </summary>
  28. /// <param name="trackedTouch">the tracked touch</param>
  29. /// <param name="detectedTouch">the detected touch</param>
  30. /// <returns>the similarity</returns>
  31. public override float calculateSimilarity(TrackedTouch trackedTouch, Touch detectedTouch)
  32. {
  33. return (trackedTouch.FingerID == detectedTouch.Finger.TrackID) ? 1 : 0;
  34. }
  35. /// <summary>
  36. /// Gets all accumulated TouchEvents since the last flush.
  37. /// </summary>
  38. /// <returns>all accumulated touchEvents</returns>
  39. public List<TouchEvent> flushTouchEvents()
  40. {
  41. List<TouchEvent> flushedTouchEvents = touchEvents;
  42. touchEvents = new List<TouchEvent>();
  43. return flushedTouchEvents;
  44. }
  45. /// <summary>
  46. /// Updates the tracked touches, and stores the (optimized) touches in frameData.trackedTouches and the touch events in frameData.touchEvents.
  47. /// </summary>
  48. /// <param name="frameData">the current frame</param>
  49. public void trackTouches(FrameData frameData)
  50. {
  51. trackObjects(frameData.DetectedTouches);
  52. frameData.TrackedTouches = getOptimizedTouches();
  53. frameData.TouchEvents = flushTouchEvents();
  54. }
  55. /// <summary>
  56. /// Creates a new TrackedTouch
  57. /// </summary>
  58. /// <param name="detectedObject">the detected touch</param>
  59. /// <returns>a new TrackedTouch</returns>
  60. protected override TrackedTouch createTrackedObject(Touch detectedObject)
  61. {
  62. TrackedTouch tt = new TrackedTouch(idPool.getNextUnusedID(), detectedObject, Parameters.TouchTrackerNumFramesDetectedUntilTracked, Parameters.TouchTrackerNumFramesLostUntilDeleted);
  63. tt.TouchEvent += handleTouchEvent;
  64. return tt;
  65. }
  66. /// <summary>
  67. /// Get all optimized representations of all tracked touches
  68. /// </summary>
  69. /// <returns>all optimized tracked touches</returns>
  70. private List<Touch> getOptimizedTouches()
  71. {
  72. List<Touch> optimizedTouchs = new List<Touch>();
  73. foreach (TrackedTouch tp in TrackedObjects)
  74. {
  75. if (tp.IsTouchActive)
  76. optimizedTouchs.Add(tp.OptimizedTouch);
  77. }
  78. return optimizedTouchs;
  79. }
  80. /// <summary>
  81. /// Handles a new TouchEvent
  82. /// </summary>
  83. /// <param name="sender">the sender of the event</param>
  84. /// <param name="e">the touch event</param>
  85. private void handleTouchEvent(object sender, TouchEvent e)
  86. {
  87. touchEvents.Add(e);
  88. }
  89. }
  90. }