TouchEventVisualizer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. bool touched = currentTouchPositions != null ? true : false;
  47. int touchX = touched ? currentTouchPositions.Last<Vector2D>().IntX : -1;
  48. int touchY = touched ? currentTouchPositions.Last<Vector2D>().IntY : -1;
  49. //-- Border --
  50. int size = Math.Min(width, height);
  51. OutputImage.drawRectangle(0, 0, size - 1, size - 1, Color.White);
  52. //-- Grid --
  53. int numRows = Constants.PalmGridRows;
  54. int numColumns = Constants.PalmGridColumns;
  55. int cellX1 = -1;
  56. int cellY1 = -1;
  57. int cellX2 = -1;
  58. int cellY2 = -1;
  59. int tmp = -1;
  60. int tmp2 = -1;
  61. for (int i = 0; i < numRows; i++)
  62. {
  63. tmp = size / numRows * i;
  64. tmp2 = size / numRows * (i + 1);
  65. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, tmp), new Vector2D(size - 2, tmp)), Constants.PalmGridColor);
  66. if (touched)
  67. if (touchY >= tmp && touchY < tmp2)
  68. {
  69. cellY1 = tmp;
  70. cellY2 = tmp2;
  71. }
  72. }
  73. for (int i = 0; i < numColumns; i++)
  74. {
  75. tmp = size / numColumns * i;
  76. tmp2 = size / numColumns * (i + 1);
  77. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(tmp, 0), new Vector2D(tmp, size - 2)), Constants.PalmGridColor);
  78. if (touched)
  79. if (touchX >= tmp && touchX < tmp2)
  80. {
  81. cellX1 = tmp;
  82. cellX2 = tmp2;
  83. }
  84. }
  85. if (touched)
  86. OutputImage.drawRectangle(cellX1, cellY1, size/numColumns, size/numRows, Color.Gray, -1);
  87. //-- Current Touch Event --
  88. if (touched)
  89. {
  90. for (int i = 1; i < currentTouchPositions.Count; ++i)
  91. {
  92. OutputImage.drawLineSegment(new LineSegment2D(currentTouchPositions[i - 1], currentTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
  93. }
  94. OutputImage.fillCircle(touchX, touchY, 3, Constants.TouchEventVisualizerPointColor);
  95. }
  96. //-- Last Touch Event --
  97. if (lastTouchPositions != null)
  98. {
  99. for (int i = 1; i < lastTouchPositions.Count; ++i)
  100. {
  101. OutputImage.drawLineSegment(new LineSegment2D(lastTouchPositions[i - 1], lastTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
  102. }
  103. OutputImage.fillCircle(lastTouchPositions.Last<Vector2D>().IntX, lastTouchPositions.Last<Vector2D>().IntY, 3, Constants.TouchEventVisualizerPointColor);
  104. }
  105. }
  106. public void Reset()
  107. {
  108. lastTouchPositions = null;
  109. currentTouchPositions = null;
  110. OutputImage = new OutputImage(width, height);
  111. lastUpdated = -1;
  112. }
  113. }
  114. }