TouchTracker.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Images;
  7. namespace bbiwarg.Detectors.Touch
  8. {
  9. class TouchTracker
  10. {
  11. private OutputImage outputImage;
  12. private List<TouchEvent>[] detectedTouchEvents;
  13. private int framesUntilTracked;
  14. public List<TouchEvent> TrackedTouchEvents;
  15. public TouchTracker()
  16. {
  17. framesUntilTracked = 2;
  18. detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
  19. for (int i = 0; i < framesUntilTracked; i++)
  20. {
  21. detectedTouchEvents[i] = new List<TouchEvent>();
  22. }
  23. }
  24. public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame, OutputImage outputImage)
  25. {
  26. this.outputImage = outputImage;
  27. for (int i = (framesUntilTracked - 1); i > 0; i--)
  28. {
  29. detectedTouchEvents[i] = detectedTouchEvents[i - 1];
  30. }
  31. detectedTouchEvents[0] = touchEventsThisFrame;
  32. findTrackedTouches();
  33. }
  34. private void findTrackedTouches()
  35. {
  36. TrackedTouchEvents = new List<TouchEvent>();
  37. foreach (TouchEvent te in detectedTouchEvents[0])
  38. {
  39. bool tracked = true;
  40. for (int i = 1; i < framesUntilTracked; i++)
  41. {
  42. if (!hasSimilarTouchEventInFrame(te, i)) tracked = false;
  43. }
  44. if (tracked)
  45. {
  46. outputImage.fillCircle(te.Position.IntX, te.Position.IntY, 5, 127, 0, 0);
  47. TrackedTouchEvents.Add(te);
  48. }
  49. }
  50. }
  51. private bool hasSimilarTouchEventInFrame(TouchEvent touchEvent, int frame)
  52. {
  53. foreach (TouchEvent te in detectedTouchEvents[frame])
  54. {
  55. if (touchEvent.isSimilarTo(te))
  56. return true;
  57. }
  58. return false;
  59. }
  60. }
  61. }