TouchEventVisualizer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.Recognition.TouchRecognition;
  8. using bbiwarg.Utility;
  9. namespace bbiwarg.Graphics
  10. {
  11. class TouchEventVisualizer
  12. {
  13. public OutputImage OutputImage { get; private set; }
  14. private int width, height;
  15. private Dictionary<int, List<Vector2D>> positions;
  16. private Dictionary<int, Kalman2DPositionFilter> kalmanFilters;
  17. public TouchEventVisualizer(int width, int height)
  18. {
  19. this.width = width;
  20. this.height = height;
  21. this.positions = new Dictionary<int, List<Vector2D>>();
  22. this.kalmanFilters = new Dictionary<int, Kalman2DPositionFilter>();
  23. }
  24. public void addTouchEvent(int trackID, Vector2D relPos) {
  25. positions.Add(trackID, new List<Vector2D>());
  26. positions[trackID].Add(relPos);
  27. Kalman2DPositionFilter kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
  28. kalmanFilter.setInitialPosition(relPos);
  29. kalmanFilters.Add(trackID, kalmanFilter);
  30. }
  31. public void updateTouchEvent(int trackID, Vector2D relPos) {
  32. positions[trackID].Add(kalmanFilters[trackID].getCorrectedPosition(relPos));
  33. }
  34. public void removeTouchEvent(int trackID) {
  35. positions.Remove(trackID);
  36. kalmanFilters.Remove(trackID);
  37. }
  38. public void updateImage() {
  39. OutputImage = new OutputImage(width, height);
  40. //border
  41. OutputImage.drawRectangle(0, 0, width - 1, height - 1, Constants.TouchEventVisualizerGridColor);
  42. //draw grid
  43. int numRows = Constants.PalmGridRows;
  44. int numColumns = Constants.PalmGridColumns;
  45. int widthPerColumn = width / numColumns;
  46. int heightPerRow = height / numRows;
  47. for (int i = 0; i <= numColumns; i++)
  48. {
  49. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Constants.TouchEventVisualizerGridColor);
  50. }
  51. for (int i = 0; i <= numRows; i++)
  52. {
  53. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0,i*heightPerRow), new Vector2D(width-1,i*heightPerRow)), Constants.TouchEventVisualizerGridColor);
  54. }
  55. Vector2D maxPixel = Constants.ImageMaxPixel;
  56. foreach (int id in positions.Keys) {
  57. int numPositions = positions[id].Count;
  58. for (int i = 1; i < numPositions; i++) {
  59. OutputImage.drawLineSegment(new LineSegment2D(positions[id][i - 1].scale(maxPixel), positions[id][i].scale(maxPixel)), Constants.TouchEventVisualizerLineColor);
  60. }
  61. Vector2D lastPos = positions[id][numPositions - 1].scale(maxPixel);
  62. OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, Constants.TouchEventVisualizerPointColor);
  63. }
  64. }
  65. /*
  66. public void addPalmTouchEvent(PalmTouchEvent e, int currentFrame)
  67. {
  68. Vector2D pos;
  69. if (lastUpdated == -1 || (currentFrame - lastUpdated) > 5 || currentTouchPositions == null || kalmanFilter == null)
  70. {
  71. lastTouchPositions = currentTouchPositions;
  72. currentTouchPositions = new List<Vector2D>();
  73. kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
  74. kalmanFilter.setInitialPosition(e.RelativePalmPosition);
  75. pos = e.RelativePalmPosition;
  76. }
  77. else
  78. {
  79. pos = kalmanFilter.getCorrectedPosition(e.RelativePalmPosition);
  80. }
  81. int size = Math.Min(width, height);
  82. currentTouchPositions.Add(new Vector2D((int)(pos.X * size), (int)(size - pos.Y * size)));
  83. lastUpdated = currentFrame;
  84. }
  85. public void updateImage()
  86. {
  87. OutputImage = new OutputImage(width, height);
  88. bool touched = currentTouchPositions != null;
  89. int touchX = touched ? currentTouchPositions.Last<Vector2D>().IntX : -1;
  90. int touchY = touched ? currentTouchPositions.Last<Vector2D>().IntY : -1;
  91. //-- Border --
  92. int size = Math.Min(width, height);
  93. OutputImage.drawRectangle(0, 0, size - 1, size - 1, Color.White);
  94. //-- Grid --
  95. int numRows = Constants.PalmGridRows;
  96. int numColumns = Constants.PalmGridColumns;
  97. int cellX1 = -1;
  98. int cellY1 = -1;
  99. int cellX2 = -1;
  100. int cellY2 = -1;
  101. int tmp = -1;
  102. int tmp2 = -1;
  103. for (int i = 0; i < numRows; i++)
  104. {
  105. tmp = size / numRows * i;
  106. tmp2 = size / numRows * (i + 1);
  107. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, tmp), new Vector2D(size - 2, tmp)), Constants.PalmGridColor);
  108. if (touched)
  109. if (touchY >= tmp && touchY < tmp2)
  110. {
  111. cellY1 = tmp;
  112. cellY2 = tmp2;
  113. }
  114. }
  115. for (int i = 0; i < numColumns; i++)
  116. {
  117. tmp = size / numColumns * i;
  118. tmp2 = size / numColumns * (i + 1);
  119. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(tmp, 0), new Vector2D(tmp, size - 2)), Constants.PalmGridColor);
  120. if (touched)
  121. if (touchX >= tmp && touchX < tmp2)
  122. {
  123. cellX1 = tmp;
  124. cellX2 = tmp2;
  125. }
  126. }
  127. if (touched)
  128. OutputImage.drawRectangle(cellX1, cellY1, size/numColumns, size/numRows, Constants.PalmGridColor, -1);
  129. //-- Current Touch Event --
  130. if (touched)
  131. {
  132. for (int i = 1; i < currentTouchPositions.Count; ++i)
  133. {
  134. OutputImage.drawLineSegment(new LineSegment2D(currentTouchPositions[i - 1], currentTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
  135. }
  136. OutputImage.fillCircle(touchX, touchY, 3, Constants.TouchEventVisualizerPointColor);
  137. }
  138. //-- Last Touch Event --
  139. if (lastTouchPositions != null)
  140. {
  141. for (int i = 1; i < lastTouchPositions.Count; ++i)
  142. {
  143. OutputImage.drawLineSegment(new LineSegment2D(lastTouchPositions[i - 1], lastTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
  144. }
  145. OutputImage.fillCircle(lastTouchPositions.Last<Vector2D>().IntX, lastTouchPositions.Last<Vector2D>().IntY, 3, Constants.TouchEventVisualizerPointColor);
  146. }
  147. }
  148. */
  149. public void Reset()
  150. {
  151. OutputImage = new OutputImage(width, height);
  152. positions.Clear();
  153. kalmanFilters.Clear();
  154. }
  155. }
  156. }