TrackedTouch.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Recognition.Tracking;
  7. using bbiwarg.Utility;
  8. namespace bbiwarg.Recognition.TouchRecognition
  9. {
  10. /// <summary>
  11. /// signature of the touchEvent event
  12. /// </summary>
  13. /// <param name="sender">the event sender</param>
  14. /// <param name="e">the touch event</param>
  15. public delegate void TouchEventHandler(object sender, TouchEvent e);
  16. /// <summary>
  17. /// Represents a touch that is tracked for several frames
  18. /// </summary>
  19. public class TrackedTouch : TrackedObject<Touch>
  20. {
  21. /// <summary>
  22. /// the kalman filter for the absolute position prediction
  23. /// </summary>
  24. private Kalman2DPositionFilter absolutePositionKalman;
  25. /// <summary>
  26. /// the prediction of the absolute position
  27. /// </summary>
  28. public Vector2D AbsolutePositionPrediction { get { return absolutePositionKalman.getPrediction(); } }
  29. /// <summary>
  30. /// the optimized touch (using the predicted absolute position)
  31. /// </summary>
  32. public Touch OptimizedTouch { get; private set; }
  33. /// <summary>
  34. /// indicates if the touch is currently active
  35. /// </summary>
  36. public bool IsTouchActive { get; private set; }
  37. /// <summary>
  38. /// the track ID of the touching finger
  39. /// </summary>
  40. public int FingerID { get; private set; }
  41. /// <summary>
  42. /// the event which is fired, if a touchEvent occurs
  43. /// </summary>
  44. public event TouchEventHandler TouchEvent;
  45. /// <summary>
  46. /// Initializes a new instance of the TrackedTouch class.
  47. /// </summary>
  48. /// <param name="id">The track ID.</param>
  49. /// <param name="detectedTouch">The detected touch.</param>
  50. /// <param name="numFramesDetectedUntilTracked">The number of consecutive frames detected until the touch is considered to be tracked (touchDown).</param>
  51. /// <param name="numFramesLostUntilDeleted">The number of consecutive frames lost until the touch is deleted (touchUp).</param>
  52. public TrackedTouch(int id, Touch detectedTouch, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  53. : base(id, detectedTouch, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  54. {
  55. absolutePositionKalman = new Kalman2DPositionFilter(Parameters.TouchmXX, Parameters.TouchmXY, Parameters.TouchmYY);
  56. absolutePositionKalman.setInitialPosition(detectedTouch.AbsolutePosition);
  57. FingerID = detectedTouch.Finger.TrackID;
  58. IsTouchActive = false;
  59. logStateChange();
  60. }
  61. /// <summary>
  62. /// Updates the tracked touch, logs the state change, updates the optimized touch and the absolute position predicton and triggers touch events.
  63. /// </summary>
  64. /// <param name="detectedTouch">the detected touch</param>
  65. public override void updateFrame(Touch detectedTouch)
  66. {
  67. base.updateFrame(detectedTouch);
  68. if (NumFramesInCurrentState == 1)
  69. logStateChange();
  70. if (detectedTouch != null)
  71. {
  72. absolutePositionKalman.getCorrectedPosition(detectedTouch.AbsolutePosition);
  73. updateOptimizedTouch(detectedTouch);
  74. if (CurrentState == TrackingState.Tracked)
  75. {
  76. if (!IsTouchActive)
  77. TriggerTouchDown();
  78. else
  79. TriggerTouchMove();
  80. }
  81. }
  82. else if (IsTouchActive && CurrentState == TrackingState.Deleted)
  83. TriggerTouchUp();
  84. }
  85. /// <summary>
  86. /// Updates the optimized touch using the absolute position prediction
  87. /// </summary>
  88. /// <param name="detectedTouch">the detected touch</param>
  89. private void updateOptimizedTouch(Touch detectedTouch)
  90. {
  91. OptimizedTouch = new Touch(AbsolutePositionPrediction, detectedTouch.Finger, detectedTouch.Palm);
  92. }
  93. /// <summary>
  94. /// logs the state change
  95. /// </summary>
  96. private void logStateChange()
  97. {
  98. String stateAsString = CurrentState.ToString().ToLower();
  99. Logger.log(String.Format("Touch #{0} {1}", this.ID, stateAsString), LogSubject.TouchTracker);
  100. }
  101. /// <summary>
  102. /// Fires a touch event with type = down
  103. /// </summary>
  104. private void TriggerTouchDown()
  105. {
  106. if (TouchEvent != null)
  107. {
  108. IsTouchActive = true;
  109. TouchEvent(this, new TouchEvent(TouchEventType.Down, OptimizedTouch));
  110. }
  111. }
  112. /// <summary>
  113. /// Fires a touch event with type = move
  114. /// </summary>
  115. private void TriggerTouchMove()
  116. {
  117. if (TouchEvent != null)
  118. {
  119. TouchEvent(this, new TouchEvent(TouchEventType.Move, OptimizedTouch));
  120. }
  121. }
  122. /// <summary>
  123. /// Fires a touch event with type = up
  124. /// </summary>
  125. private void TriggerTouchUp()
  126. {
  127. if (TouchEvent != null)
  128. {
  129. IsTouchActive = false;
  130. TouchEvent(this, new TouchEvent(TouchEventType.Up, OptimizedTouch));
  131. }
  132. }
  133. }
  134. }