TouchEventVisualizer.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.Detectors.Touch;
  8. using bbiwarg.Utility;
  9. namespace bbiwarg.Graphics
  10. {
  11. class TouchEventVisualizer
  12. {
  13. public OutputImage OutputImage { get; private set; }
  14. private List<Kalman2DPositionFilter> kalmanFilters;
  15. private List<List<Vector2D>> touchPositions;
  16. private int lastUpdated = -1;
  17. private int width, height;
  18. public TouchEventVisualizer(int width, int height)
  19. {
  20. OutputImage = new Graphics.OutputImage(width, height);
  21. touchPositions = new List<List<Vector2D>>();
  22. kalmanFilters = new List<Kalman2DPositionFilter>();
  23. this.width = width;
  24. this.height = height;
  25. }
  26. public void addPalmTouchEvent(PalmTouchEvent e, int currentFrame)
  27. {
  28. List<Vector2D> currentList;
  29. Kalman2DPositionFilter currentFilter;
  30. bool addList = false;
  31. if (lastUpdated == -1 || (currentFrame - lastUpdated) > 5)
  32. {
  33. currentList = new List<Vector2D>();
  34. currentFilter = new Kalman2DPositionFilter(e.RelativePalmPosition);
  35. addList = true;
  36. }
  37. else
  38. {
  39. currentList = touchPositions.Last<List<Vector2D>>();
  40. currentFilter = kalmanFilters.Last<Kalman2DPositionFilter>();
  41. }
  42. Vector2D pos;
  43. if (currentFilter == null)
  44. {
  45. currentFilter = new Kalman2DPositionFilter(e.RelativePalmPosition);
  46. pos = e.RelativePalmPosition;
  47. }
  48. else
  49. {
  50. pos = currentFilter.getCorrectedPosition(e.RelativePalmPosition);
  51. }
  52. currentList.Add(new Vector2D((int)(pos.X * width), (int)(height - pos.Y * height)));
  53. if (addList)
  54. {
  55. touchPositions.Add(currentList);
  56. kalmanFilters.Add(currentFilter);
  57. }
  58. lastUpdated = currentFrame;
  59. }
  60. public void updateImage()
  61. {
  62. OutputImage = new OutputImage(width, height);
  63. foreach (List<Vector2D> positions in touchPositions)
  64. {
  65. for (int i = 1; i < positions.Count; ++i)
  66. {
  67. OutputImage.drawLineSegment(new LineSegment2D(positions[i - 1], positions[i]), Color.White);
  68. }
  69. if (touchPositions.Count != 0)
  70. OutputImage.fillCircle(positions.Last<Vector2D>().IntX, positions.Last<Vector2D>().IntY, 3, Color.Red);
  71. }
  72. }
  73. public void Reset()
  74. {
  75. touchPositions.Clear();
  76. OutputImage = new OutputImage(width, height);
  77. lastUpdated = -1;
  78. }
  79. }
  80. }