TrackedTouch.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, TouchEvent e);
  11. public class TrackedTouch : TrackedObject<Touch>
  12. {
  13. private Kalman2DPositionFilter absolutePositionKalman;
  14. public Vector2D AbsolutePositionPrediction { get { return absolutePositionKalman.getPrediction(); } }
  15. public Touch OptimizedTouch { get; private set; }
  16. public bool IsTouchActive { get; private set; }
  17. public int FingerID { get; private set; }
  18. public event TouchEventHandler TouchEvent;
  19. public TrackedTouch(int id, Touch detectedTouch, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  20. : base(id, detectedTouch, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  21. {
  22. absolutePositionKalman = new Kalman2DPositionFilter(Parameters.TouchmXX, Parameters.TouchmXY, Parameters.TouchmYY);
  23. absolutePositionKalman.setInitialPosition(detectedTouch.AbsolutePosition);
  24. FingerID = detectedTouch.Finger.TrackID;
  25. IsTouchActive = false;
  26. logStateChange();
  27. }
  28. public override void updateFrame(Touch detectedTouch)
  29. {
  30. base.updateFrame(detectedTouch);
  31. if (NumFramesInCurrentState == 1)
  32. logStateChange();
  33. if (detectedTouch != null)
  34. {
  35. absolutePositionKalman.getCorrectedPosition(detectedTouch.AbsolutePosition);
  36. updateOptimizedTouch(detectedTouch);
  37. if (CurrentState == TrackingState.Tracked)
  38. {
  39. if (!IsTouchActive)
  40. TriggerTouchDown();
  41. else
  42. TriggerTouchMove();
  43. }
  44. }
  45. else if (IsTouchActive && CurrentState == TrackingState.Deleted)
  46. TriggerTouchUp();
  47. }
  48. private void updateOptimizedTouch(Touch detectedTouch)
  49. {
  50. OptimizedTouch = new Touch(AbsolutePositionPrediction, detectedTouch.Finger, detectedTouch.Palm);
  51. }
  52. private void logStateChange()
  53. {
  54. String stateAsString = CurrentState.ToString().ToLower();
  55. Logger.log(String.Format("Touch #{0} {1}", this.ID, stateAsString), LogSubject.TouchTracker);
  56. }
  57. private void TriggerTouchDown()
  58. {
  59. if (TouchEvent != null)
  60. {
  61. IsTouchActive = true;
  62. TouchEvent(this, new TouchEvent(TouchEventType.Down, OptimizedTouch));
  63. }
  64. }
  65. private void TriggerTouchMove()
  66. {
  67. if (TouchEvent != null)
  68. {
  69. TouchEvent(this, new TouchEvent(TouchEventType.Move, OptimizedTouch));
  70. }
  71. }
  72. private void TriggerTouchUp()
  73. {
  74. if (TouchEvent != null)
  75. {
  76. IsTouchActive = false;
  77. TouchEvent(this, new TouchEvent(TouchEventType.Up, OptimizedTouch));
  78. }
  79. }
  80. }
  81. }