TouchTracker.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Graphics;
  9. using bbiwarg.Recognition.Tracking;
  10. using bbiwarg.Utility;
  11. namespace bbiwarg.Recognition.TouchRecognition
  12. {
  13. public class PalmTouchEventArgs : EventArgs
  14. {
  15. public int TrackID { get; private set; }
  16. public Vector2D Position { get; private set; }
  17. public PalmTouchEventArgs(int trackID, Vector2D position)
  18. {
  19. TrackID = trackID;
  20. Position = position;
  21. }
  22. }
  23. public delegate void PalmTouchEventHandler(object sender, PalmTouchEventArgs e);
  24. class TouchTracker : Tracker<TouchEvent>
  25. {
  26. public List<TouchEvent> TouchEvents { get { return TrackedObjects; } }
  27. public Dictionary<int, Kalman2DPositionFilter> kalmanFilters;
  28. public event PalmTouchEventHandler PalmTouchDown;
  29. public event PalmTouchEventHandler PalmTouchMove;
  30. public event PalmTouchEventHandler PalmTouchUp;
  31. protected virtual void OnPalmTouchDown(PalmTouchEventArgs e) { if (PalmTouchDown != null) PalmTouchDown(this, e); }
  32. protected virtual void OnPalmTouchMove(PalmTouchEventArgs e) { if (PalmTouchMove != null) PalmTouchMove(this, e); }
  33. protected virtual void OnPalmTouchUp(PalmTouchEventArgs e) { if (PalmTouchUp != null) PalmTouchUp(this, e); }
  34. public TouchTracker()
  35. : base(Constants.TouchEventNumFramesDetectedUntilTracked, Constants.TouchEventNumFramesLostUntilDeleted, Constants.TouchEventMinSimilarityForTracking)
  36. {
  37. kalmanFilters = new Dictionary<int, Kalman2DPositionFilter>();
  38. }
  39. public new void reset() {
  40. foreach (int id in kalmanFilters.Keys) {
  41. OnPalmTouchUp(new PalmTouchEventArgs(id, kalmanFilters[id].getPrediction()));
  42. }
  43. kalmanFilters.Clear();
  44. base.reset();
  45. }
  46. protected override void onDetect(object sender, EventArgs e)
  47. {
  48. TrackableObjectHistory<TouchEvent> history = (TrackableObjectHistory<TouchEvent>)sender;
  49. if (history.NumFramesInCurrentState == 1)
  50. Logger.log("TouchEvent #" + history.ID.ToString() + " detected", LogSubject.TouchTracker);
  51. }
  52. protected override void onTrack(object sender, EventArgs e)
  53. {
  54. TrackableObjectHistory<TouchEvent> history = (TrackableObjectHistory<TouchEvent>)sender;
  55. if (history.NumFramesInCurrentState == 1)
  56. Logger.log("TouchEvent #" + history.ID.ToString() + " tracked", LogSubject.TouchTracker);
  57. if (history.LastObject is PalmTouchEvent)
  58. {
  59. PalmTouchEvent pte = (PalmTouchEvent)history.LastObject;
  60. if (history.NumFramesInCurrentState == 1)
  61. palmTouchDown(history.ID, pte.RelativePalmPosition);
  62. else
  63. palmTouchMove(history.ID, pte.RelativePalmPosition);
  64. }
  65. }
  66. protected override void onRetrack(object sender, EventArgs e)
  67. {
  68. TrackableObjectHistory<TouchEvent> history = (TrackableObjectHistory<TouchEvent>)sender;
  69. if (history.NumFramesInCurrentState == 1)
  70. Logger.log("TouchEvent #" + history.ID.ToString() + " retracked", LogSubject.TouchTracker);
  71. if (history.LastObject is PalmTouchEvent)
  72. {
  73. PalmTouchEvent pte = (PalmTouchEvent)history.LastObject;
  74. palmTouchMove(history.ID, pte.RelativePalmPosition);
  75. }
  76. else throw new ArgumentException();
  77. }
  78. protected override void onLoose(object sender, EventArgs e)
  79. {
  80. TrackableObjectHistory<TouchEvent> history = (TrackableObjectHistory<TouchEvent>)sender;
  81. if (history.NumFramesInCurrentState == 1)
  82. Logger.log("TouchEvent #" + history.ID.ToString() + " lost", LogSubject.TouchTracker);
  83. }
  84. protected override void onDelete(object sender, EventArgs e)
  85. {
  86. TrackableObjectHistory<TouchEvent> history = (TrackableObjectHistory<TouchEvent>)sender;
  87. if (history.NumFramesInCurrentState == 1)
  88. Logger.log("TouchEvent #" + history.ID.ToString() + " deleted", LogSubject.TouchTracker);
  89. if (history.LastObject is PalmTouchEvent)
  90. {
  91. PalmTouchEvent pte = (PalmTouchEvent)history.LastObject;
  92. palmTouchUp(history.ID, pte.RelativePalmPosition);
  93. }
  94. else throw new ArgumentException();
  95. }
  96. private void palmTouchDown(int id, Vector2D relPos) {
  97. Kalman2DPositionFilter kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
  98. kalmanFilter.setInitialPosition(relPos);
  99. kalmanFilters.Add(id, kalmanFilter);
  100. OnPalmTouchDown(new PalmTouchEventArgs(id, relPos));
  101. Logger.log("TouchEvent #" + id.ToString() + " touchDown at (" + relPos.X + ", " + relPos.Y + ")", LogSubject.TouchEvents);
  102. }
  103. private void palmTouchMove(int id, Vector2D relPos) {
  104. Vector2D correctedRelPos = kalmanFilters[id].getCorrectedPosition(relPos);
  105. OnPalmTouchMove(new PalmTouchEventArgs(id, correctedRelPos));
  106. Logger.log("TouchEvent #" + id.ToString() + " touchMove to (" + relPos.X + ", " + relPos.Y + ")", LogSubject.TouchEvents);
  107. }
  108. private void palmTouchUp(int id, Vector2D relPos) {
  109. Kalman2DPositionFilter kalmanFilter;
  110. if (kalmanFilters.TryGetValue(id, out kalmanFilter))
  111. {
  112. Vector2D correctedRelPos = kalmanFilter.getCorrectedPosition(relPos);
  113. OnPalmTouchUp(new PalmTouchEventArgs(id, correctedRelPos));
  114. kalmanFilters.Remove(id);
  115. Logger.log("TouchEvent #" + id.ToString() + " touchUp at (" + relPos.X + ", " + relPos.Y + ")", LogSubject.TouchEvents);
  116. }
  117. }
  118. }
  119. }