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