TouchEventVisualizer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 int lastUpdated = -1;
  15. private int width, height;
  16. private List<Vector2D> lastTouchPositions;
  17. private List<Vector2D> currentTouchPositions;
  18. private Kalman2DPositionFilter kalmanFilter;
  19. public TouchEventVisualizer(int width, int height)
  20. {
  21. this.width = width;
  22. this.height = height;
  23. OutputImage = new Graphics.OutputImage(width, height);
  24. }
  25. public void addPalmTouchEvent(PalmTouchEvent e, int currentFrame)
  26. {
  27. Vector2D pos;
  28. if (lastUpdated == -1 || (currentFrame - lastUpdated) > 5 || currentTouchPositions == null || kalmanFilter == null)
  29. {
  30. lastTouchPositions = currentTouchPositions;
  31. currentTouchPositions = new List<Vector2D>();
  32. kalmanFilter = new Kalman2DPositionFilter();
  33. kalmanFilter.setInitialPosition(e.RelativePalmPosition);
  34. pos = e.RelativePalmPosition;
  35. }
  36. else
  37. {
  38. pos = kalmanFilter.getCorrectedPosition(e.RelativePalmPosition);
  39. }
  40. int size = Math.Min(width, height);
  41. currentTouchPositions.Add(new Vector2D((int)(pos.X * size), (int)(size - pos.Y * size)));
  42. lastUpdated = currentFrame;
  43. }
  44. public void updateImage()
  45. {
  46. OutputImage = new OutputImage(width, height);
  47. //-- Border --
  48. int size = Math.Min(width, height);
  49. OutputImage.drawRectangle(0, 0, size - 1, size - 1, Color.White);
  50. //-- Grid --
  51. int numRows = Constants.PalmGridRows;
  52. int numColumns = Constants.PalmGridColumns;
  53. for (int i = 1; i < numRows; i++)
  54. {
  55. int tmp = size / numRows * i;
  56. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, tmp), new Vector2D(size - 2, tmp)), Constants.PalmGridColor);
  57. }
  58. for (int i = 1; i < numColumns; i++)
  59. {
  60. int tmp = size / numColumns * i;
  61. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(tmp, 0), new Vector2D(tmp, size - 2)), Constants.PalmGridColor);
  62. }
  63. //-- Current Touch Event --
  64. if (currentTouchPositions != null)
  65. {
  66. for (int i = 1; i < currentTouchPositions.Count; ++i)
  67. {
  68. OutputImage.drawLineSegment(new LineSegment2D(currentTouchPositions[i - 1], currentTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
  69. }
  70. OutputImage.fillCircle(currentTouchPositions.Last<Vector2D>().IntX, currentTouchPositions.Last<Vector2D>().IntY, 3, Constants.TouchEventVisualizerPointColor);
  71. }
  72. //-- Last Touch Event --
  73. if (lastTouchPositions != null)
  74. {
  75. for (int i = 1; i < lastTouchPositions.Count; ++i)
  76. {
  77. OutputImage.drawLineSegment(new LineSegment2D(lastTouchPositions[i - 1], lastTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
  78. }
  79. OutputImage.fillCircle(lastTouchPositions.Last<Vector2D>().IntX, lastTouchPositions.Last<Vector2D>().IntY, 3, Constants.TouchEventVisualizerPointColor);
  80. }
  81. }
  82. public void Reset()
  83. {
  84. lastTouchPositions = null;
  85. currentTouchPositions = null;
  86. OutputImage = new OutputImage(width, height);
  87. lastUpdated = -1;
  88. }
  89. }
  90. }