TouchEventVisualizer.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. this.width = width;
  21. this.height = height;
  22. OutputImage = new Graphics.OutputImage(width, height);
  23. touchPositions = new List<List<Vector2D>>();
  24. kalmanFilters = new List<Kalman2DPositionFilter>();
  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. int size = Math.Min(width, height);
  53. currentList.Add(new Vector2D((int)(pos.X * size), (int)(size - pos.Y * size)));
  54. if (addList)
  55. {
  56. touchPositions.Add(currentList);
  57. kalmanFilters.Add(currentFilter);
  58. }
  59. lastUpdated = currentFrame;
  60. }
  61. public void updateImage()
  62. {
  63. OutputImage = new OutputImage(width, height);
  64. int size = Math.Min(width, height);
  65. OutputImage.drawRectangle(0, 0, size - 1, size - 1, Color.White);
  66. foreach (List<Vector2D> positions in touchPositions)
  67. {
  68. for (int i = 1; i < positions.Count; ++i)
  69. {
  70. OutputImage.drawLineSegment(new LineSegment2D(positions[i - 1], positions[i]), Constants.TouchEventVisualizerLineColor);
  71. }
  72. if (touchPositions.Count != 0)
  73. OutputImage.fillCircle(positions.Last<Vector2D>().IntX, positions.Last<Vector2D>().IntY, 3, Constants.TouchEventVisualizerPointColor);
  74. }
  75. }
  76. public void Reset()
  77. {
  78. touchPositions.Clear();
  79. OutputImage = new OutputImage(width, height);
  80. lastUpdated = -1;
  81. }
  82. }
  83. }