TrackedTouchEvent.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY);
  38. absolutePositionKalman.setInitialPosition(detectedTouchEvent.AbsolutePosition);
  39. relativePositionKalman = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.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. Vector2D correctedAbsolutePosition = absolutePositionKalman.getCorrectedPosition(detectedTouchEvent.AbsolutePosition);
  53. Vector2D correctedRelativePosition = relativePositionKalman.getCorrectedPosition(detectedTouchEvent.RelativePosition);
  54. if (CurrentState == TrackingState.Tracked)
  55. {
  56. if (!isTouchActive)
  57. TriggerTouchDown(new TouchEventArgs(ID, correctedAbsolutePosition, correctedRelativePosition));
  58. else
  59. TriggerTouchMove(new TouchEventArgs(ID, correctedAbsolutePosition, correctedRelativePosition));
  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. //finger similarity
  68. float fingerSimilarity = (fingerID == detectedTouchEvent.Finger.TrackID) ? 1 : 0;
  69. //absolute position similarity
  70. float absoluteDistance = detectedTouchEvent.AbsolutePosition.getDistanceTo(AbsolutePositionPrediction);
  71. float absoluteSimilarity = Math.Max(0, 1 - absoluteDistance / Constants.ImageDiagonalLength);
  72. //relative position similarity
  73. float relativeDistance = detectedTouchEvent.RelativePosition.getDistanceTo(RelativePositionPrediction);
  74. float relativeSimilarity = Math.Max(0, 1 - relativeDistance / 1f);
  75. float similarity = fingerSimilarity * absoluteSimilarity * relativeSimilarity;
  76. return similarity;
  77. }
  78. private void logStateChange()
  79. {
  80. String stateAsString = CurrentState.ToString().ToLower();
  81. Logger.log(String.Format("TouchEvent #{0} {1}", this.ID, stateAsString), LogSubject.TouchTracker);
  82. }
  83. private void TriggerTouchDown(TouchEventArgs e)
  84. {
  85. if (TouchDown != null)
  86. {
  87. isTouchActive = true;
  88. TouchDown(this, e);
  89. Logger.log(String.Format("TouchEvent #{0} TouchDown at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
  90. }
  91. }
  92. private void TriggerTouchMove(TouchEventArgs e)
  93. {
  94. if (TouchMove != null)
  95. {
  96. TouchMove(this, e);
  97. Logger.log(String.Format("TouchEvent #{0} TouchMove at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
  98. }
  99. }
  100. private void TriggerTouchUp(TouchEventArgs e)
  101. {
  102. if (TouchUp != null)
  103. {
  104. isTouchActive = false;
  105. TouchUp(this, e);
  106. Logger.log(String.Format("TouchEvent #{0} TouchUp at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
  107. }
  108. }
  109. }
  110. }