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