TouchEventVisualizer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Recognition.TouchRecognition;
  3. using BBIWARG.Utility;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Linq;
  9. namespace BBIWARG.Output.DebugOutput
  10. {
  11. /// <summary>
  12. /// Provides an image showing touch events.
  13. /// </summary>
  14. internal class TouchEventVisualizer
  15. {
  16. /// <summary>
  17. /// all touch events starting at the last TouchDown, indexed by a unique id
  18. /// </summary>
  19. private Dictionary<int, List<Vector2D>> activeTouches;
  20. /// <summary>
  21. /// relative times of last frame updates which are stored for a limited time, indexed by a unique id
  22. /// </summary>
  23. private Dictionary<int, long> lastUpdates;
  24. /// <summary>
  25. /// the next free id, used as a unique id for the dictionaries
  26. /// </summary>
  27. private int nextFreeID;
  28. /// <summary>
  29. /// old touch events which are stored for a limited time, indexed by a unique id
  30. /// </summary>
  31. private Dictionary<int, List<Vector2D>> oldTouches;
  32. /// <summary>
  33. /// used to prevent running <see cref="handleNewFrameData"/> and <see cref="getOutputImage"/> simultaneously from different threads
  34. /// </summary>
  35. private Object sync;
  36. /// <summary>
  37. /// used to store the relative time at which the touch events occurred
  38. /// </summary>
  39. private Stopwatch timer;
  40. /// <summary>
  41. /// Creates a TouchEventVisualizer.
  42. /// </summary>
  43. public TouchEventVisualizer()
  44. {
  45. sync = new object();
  46. reset();
  47. }
  48. /// <summary>
  49. /// Returns an output image showing the touch events.
  50. /// </summary>
  51. /// <param name="imageSize">output image size</param>
  52. /// <param name="numRows">number of rows in the palm grid</param>
  53. /// <param name="numColumns">number columns in the palm grid</param>
  54. /// <returns>image showing touch events</returns>
  55. public OutputImage getOutputImage(ImageSize imageSize, int numRows, int numColumns, int sliderPos, int sliderMax, int sliderCurr)
  56. {
  57. lock (sync)
  58. {
  59. long currentTime = timer.ElapsedMilliseconds;
  60. removeOldPositions(currentTime - Parameters.TouchEventVisualizerFadeOutTime);
  61. OutputImage outputImage = new OutputImage(imageSize);
  62. int imageWidth = imageSize.Width;
  63. int imageHeight = imageSize.Height;
  64. // border
  65. outputImage.drawBorder(Parameters.TouchEventVisualizerGridColor);
  66. // draw grid
  67. int widthPerColumn = imageWidth / numColumns;
  68. int heightPerRow = imageHeight / numRows;
  69. // find active blocks
  70. List<List<bool>> activeBlocks = getActiveBlocks(numRows, numColumns, sliderPos, sliderMax, sliderCurr);
  71. if (numRows * numColumns > 1)
  72. {
  73. for (int i = 0; i < activeTouches.Values.Count; i++)
  74. {
  75. List<Vector2D> positions = activeTouches.Values.ElementAt(i);
  76. Vector2D lastPosition = positions.Last();
  77. int activeRow = (int)Math.Min(lastPosition.Y * numRows, numRows - 1);
  78. int activeCol = (int)Math.Min(lastPosition.X * activeBlocks.ElementAt(activeRow).Count,
  79. activeBlocks.ElementAt(activeRow).Count - 1);
  80. if (activeRow != sliderPos)
  81. activeBlocks.ElementAt(activeRow)[activeCol] = true;
  82. if (activeRow == sliderPos)
  83. {
  84. if (Parameters.PalmSliderLastTouched != -1)
  85. {
  86. int temp = Parameters.PalmSliderCurr + (activeCol - Parameters.PalmSliderLastTouched);
  87. if (temp < 0)
  88. Parameters.PalmSliderCurr = 0;
  89. if (temp > Parameters.PalmSliderMax)
  90. Parameters.PalmSliderCurr = Parameters.PalmSliderMax;
  91. else
  92. Parameters.PalmSliderCurr = temp;
  93. Parameters.PalmSliderLastTouched = activeCol;
  94. }
  95. else if (Parameters.PalmSliderLastTouched == -1)
  96. {
  97. Parameters.PalmSliderLastTouched = activeCol;
  98. }
  99. }
  100. else
  101. {
  102. Parameters.PalmSliderLastTouched = -1;
  103. }
  104. }
  105. if (activeTouches.Values.Count < 1)
  106. {
  107. Parameters.PalmSliderLastTouched = -1;
  108. }
  109. }
  110. if(sliderPos > -1 && sliderPos < numRows)
  111. {
  112. for (int i = 0; i < Parameters.PalmSliderCurr; i++)
  113. {
  114. activeBlocks.ElementAt(sliderPos)[i] = true;
  115. }
  116. }
  117. // draw blocks
  118. int index = 1;
  119. for (int row = 0; row < numRows; row++)
  120. {
  121. for (int col = 0; col < activeBlocks.ElementAt(row).Count; col++)
  122. {
  123. if (activeBlocks.ElementAt(row)[col])
  124. outputImage.fillRectangle(new Rectangle(col * imageWidth/activeBlocks.ElementAt(row).Count,
  125. row * heightPerRow, imageWidth / activeBlocks.ElementAt(row).Count, heightPerRow),
  126. Parameters.TouchEventVisualizerActiveBlockColor);
  127. if(row != sliderPos)
  128. {
  129. int x = (int)((col + 0.5f) * imageWidth / activeBlocks.ElementAt(row).Count) - 5;
  130. int y = (int)((row + 0.5f) * heightPerRow) + 5;
  131. outputImage.drawText(new Point(x, y), index.ToString(), Parameters.TouchEventVisualizerTextColor);
  132. index++;
  133. }
  134. }
  135. }
  136. // draw grid
  137. for(int i = 0; i < activeBlocks.Count; i++)
  138. {
  139. for(int j = 0; j < activeBlocks.ElementAt(i).Count; j++)
  140. {
  141. outputImage.drawLineSegment(new LineSegment2D(
  142. new Vector2D(j * imageWidth/activeBlocks.ElementAt(i).Count, i * heightPerRow),
  143. new Vector2D(j * imageWidth / activeBlocks.ElementAt(i).Count, (i + 1) * heightPerRow -1)),
  144. Parameters.TouchEventVisualizerGridColor);
  145. }
  146. }
  147. //for (int i = 0; i <= numColumns; i++)
  148. // outputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, imageHeight - 1)), Parameters.TouchEventVisualizerGridColor);
  149. for (int i = 0; i <= numRows; i++)
  150. outputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(imageWidth - 1, i * heightPerRow)), Parameters.TouchEventVisualizerGridColor);
  151. // draw active touches
  152. foreach (List<Vector2D> positions in activeTouches.Values)
  153. outputImage.drawTouchGesture(positions, imageSize.MaxPixel);
  154. // draw old touches (fade out)
  155. foreach (int id in oldTouches.Keys)
  156. {
  157. List<Vector2D> positions = oldTouches[id];
  158. long lastUpdate = lastUpdates[id];
  159. float opacity = 1 - ((currentTime - lastUpdate) / (float)Parameters.TouchEventVisualizerFadeOutTime);
  160. outputImage.drawTouchGesture(positions, imageSize.MaxPixel, opacity);
  161. }
  162. return outputImage;
  163. }
  164. }
  165. private List<List<bool>> getActiveBlocks(int numRows, int numColumns, int sliderPos, int sliderMax, int sliderCurr)
  166. {
  167. List<List<bool>> res = new List<List<bool>>();
  168. for(int i = 0; i < numRows; i++)
  169. {
  170. List<bool> temp = new List<bool>();
  171. if (i == sliderPos)
  172. {
  173. for (int j = 0; j < sliderMax; j++) temp.Add(false);
  174. }
  175. else
  176. {
  177. for (int j = 0; j < numColumns; j++) temp.Add(false);
  178. }
  179. res.Add(temp);
  180. }
  181. return res;
  182. }
  183. /// <summary>
  184. /// Handles the event that a new frame is finished processing by updating the touch events.
  185. /// </summary>
  186. /// <param name="sender">event sender</param>
  187. /// <param name="e">event arguments</param>
  188. public void handleNewFrameData(object sender, NewProcessedFrameEventArgs e)
  189. {
  190. FrameData frameData = e.FrameData;
  191. lock (frameData) lock (sync)
  192. {
  193. if (frameData.ResetFlag)
  194. reset();
  195. foreach (TouchEvent te in frameData.TouchEvents)
  196. {
  197. switch (te.Type)
  198. {
  199. case TouchEventType.Down:
  200. activeTouches.Add(te.Touch.TrackID, new List<Vector2D>());
  201. activeTouches[te.Touch.TrackID].Add(te.Touch.RelativePosition);
  202. break;
  203. case TouchEventType.Move:
  204. activeTouches[te.Touch.TrackID].Add(te.Touch.RelativePosition);
  205. break;
  206. case TouchEventType.Up:
  207. activeTouches[te.Touch.TrackID].Add(te.Touch.RelativePosition);
  208. oldTouches.Add(nextFreeID, activeTouches[te.Touch.TrackID]);
  209. lastUpdates.Add(nextFreeID, timer.ElapsedMilliseconds);
  210. activeTouches.Remove(te.Touch.TrackID);
  211. nextFreeID++;
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// Resets the touch events and the timer.
  219. /// </summary>
  220. public void reset()
  221. {
  222. timer = new Stopwatch();
  223. timer.Start();
  224. nextFreeID = 1;
  225. activeTouches = new Dictionary<int, List<Vector2D>>();
  226. oldTouches = new Dictionary<int, List<Vector2D>>();
  227. lastUpdates = new Dictionary<int, long>();
  228. }
  229. /// <summary>
  230. /// Removes old touch events and update times.
  231. /// </summary>
  232. /// <param name="breakTime">every touch event which occurred before breakTime is removed</param>
  233. private void removeOldPositions(long breakTime)
  234. {
  235. List<int> ids = new List<int>(lastUpdates.Keys);
  236. for (int i = ids.Count - 1; i >= 0; i--)
  237. {
  238. int id = ids[i];
  239. if (breakTime > lastUpdates[id])
  240. {
  241. oldTouches.Remove(id);
  242. lastUpdates.Remove(id);
  243. }
  244. }
  245. }
  246. }
  247. }