TouchTracker.cs 3.3 KB

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