TrackedTouch.cs 5.3 KB

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