TrackedTouchEvent.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. public delegate void TouchEventHandler(object sender, TouchEventArgs e);
  11. public class TouchEventArgs : EventArgs
  12. {
  13. public int TrackID { get; private set; }
  14. public Vector2D AbsolutePosition { get; private set; }
  15. public Vector2D RelativePosition { get; private set; }
  16. public TouchEventArgs(int trackID, Vector2D absolutePosition, Vector2D relativePosition)
  17. {
  18. TrackID = trackID;
  19. AbsolutePosition = absolutePosition;
  20. RelativePosition = relativePosition;
  21. }
  22. }
  23. class TrackedTouchEvent : TrackedObject<TouchEvent>
  24. {
  25. private Kalman2DPositionFilter absolutePositionKalman;
  26. private Kalman2DPositionFilter relativePositionKalman;
  27. private int fingerID;
  28. private bool isTouchActive;
  29. public Vector2D AbsolutePositionPrediction { get { return absolutePositionKalman.getPrediction(); } }
  30. public Vector2D RelativePositionPrediction { get { return relativePositionKalman.getPrediction(); } }
  31. public event TouchEventHandler TouchDown;
  32. public event TouchEventHandler TouchMove;
  33. public event TouchEventHandler TouchUp;
  34. public TrackedTouchEvent(int id, TouchEvent detectedTouchEvent, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  35. : base(id, detectedTouchEvent, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  36. {
  37. absolutePositionKalman = new Kalman2DPositionFilter(Parameters.TouchmXX, Parameters.TouchmXY, Parameters.TouchmYY);
  38. absolutePositionKalman.setInitialPosition(detectedTouchEvent.AbsolutePosition);
  39. relativePositionKalman = new Kalman2DPositionFilter(Parameters.TouchmXX, Parameters.TouchmXY, Parameters.TouchmYY);
  40. relativePositionKalman.setInitialPosition(detectedTouchEvent.RelativePosition);
  41. fingerID = detectedTouchEvent.Finger.TrackID;
  42. isTouchActive = false;
  43. logStateChange();
  44. }
  45. public override void updateFrame(TouchEvent detectedTouchEvent)
  46. {
  47. base.updateFrame(detectedTouchEvent);
  48. if (NumFramesInCurrentState == 1)
  49. logStateChange();
  50. if (detectedTouchEvent != null)
  51. {
  52. absolutePositionKalman.getCorrectedPosition(detectedTouchEvent.AbsolutePosition);
  53. relativePositionKalman.getCorrectedPosition(detectedTouchEvent.RelativePosition);
  54. if (CurrentState == TrackingState.Tracked)
  55. {
  56. if (!isTouchActive)
  57. TriggerTouchDown(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
  58. else
  59. TriggerTouchMove(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
  60. }
  61. }
  62. else if (isTouchActive && CurrentState == TrackingState.Deleted)
  63. TriggerTouchUp(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
  64. }
  65. public override float calculateSimilarity(TouchEvent detectedTouchEvent)
  66. {
  67. float fingerSimilarity = (fingerID == detectedTouchEvent.Finger.TrackID) ? 1 : 0;
  68. float absolutePositionSimilarity = getPositionSimilarity(AbsolutePositionPrediction, detectedTouchEvent.AbsolutePosition, Parameters.TouchTrackerMaxAbsolutePositionMove);
  69. float relativePositionSimilarity = getPositionSimilarity(RelativePositionPrediction, detectedTouchEvent.RelativePosition, Parameters.TouchTrackerMaxRelativePositionMove);
  70. float similarity = fingerSimilarity * absolutePositionSimilarity * relativePositionSimilarity;
  71. return similarity;
  72. }
  73. private void logStateChange()
  74. {
  75. String stateAsString = CurrentState.ToString().ToLower();
  76. Logger.log(String.Format("TouchEvent #{0} {1}", this.ID, stateAsString), LogSubject.TouchTracker);
  77. }
  78. private void TriggerTouchDown(TouchEventArgs e)
  79. {
  80. if (TouchDown != null)
  81. {
  82. isTouchActive = true;
  83. TouchDown(this, e);
  84. Logger.log(String.Format("TouchEvent #{0} TouchDown at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
  85. }
  86. }
  87. private void TriggerTouchMove(TouchEventArgs e)
  88. {
  89. if (TouchMove != null)
  90. {
  91. TouchMove(this, e);
  92. Logger.log(String.Format("TouchEvent #{0} TouchMove at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
  93. }
  94. }
  95. private void TriggerTouchUp(TouchEventArgs e)
  96. {
  97. if (TouchUp != null)
  98. {
  99. isTouchActive = false;
  100. TouchUp(this, e);
  101. Logger.log(String.Format("TouchEvent #{0} TouchUp at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
  102. }
  103. }
  104. }
  105. }