TouchEventVisualizer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 touchDown(object sender, PalmTouchEventArgs ptea)
  25. {
  26. positions.Add(ptea.TrackID, new List<Vector2D>());
  27. positions[ptea.TrackID].Add(ptea.Position);
  28. Kalman2DPositionFilter kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
  29. kalmanFilter.setInitialPosition(ptea.Position);
  30. kalmanFilters.Add(ptea.TrackID, kalmanFilter);
  31. }
  32. public void touchMove(object sender, PalmTouchEventArgs ptea) {
  33. positions[ptea.TrackID].Add(kalmanFilters[ptea.TrackID].getCorrectedPosition(ptea.Position));
  34. }
  35. public void touchUp(object sender, PalmTouchEventArgs ptea) {
  36. positions.Remove(ptea.TrackID);
  37. kalmanFilters.Remove(ptea.TrackID);
  38. }
  39. public void updateImage() {
  40. OutputImage = new OutputImage(width, height);
  41. //border
  42. OutputImage.drawRectangle(0, 0, width - 1, height - 1, Constants.TouchEventVisualizerGridColor);
  43. //draw grid
  44. int numRows = Constants.PalmGridRows;
  45. int numColumns = Constants.PalmGridColumns;
  46. int widthPerColumn = width / numColumns;
  47. int heightPerRow = height / numRows;
  48. for (int i = 0; i <= numColumns; i++)
  49. {
  50. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Constants.TouchEventVisualizerGridColor);
  51. }
  52. for (int i = 0; i <= numRows; i++)
  53. {
  54. OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0,i*heightPerRow), new Vector2D(width-1,i*heightPerRow)), Constants.TouchEventVisualizerGridColor);
  55. }
  56. Vector2D maxPixel = Constants.ImageMaxPixel;
  57. foreach (int id in positions.Keys) {
  58. int numPositions = positions[id].Count;
  59. for (int i = 1; i < numPositions; i++) {
  60. OutputImage.drawLineSegment(new LineSegment2D(positions[id][i - 1].scale(maxPixel), positions[id][i].scale(maxPixel)), Constants.TouchEventVisualizerLineColor);
  61. }
  62. Vector2D lastPos = positions[id][numPositions - 1].scale(maxPixel);
  63. OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, Constants.TouchEventVisualizerPointColor);
  64. }
  65. }
  66. public void reset()
  67. {
  68. OutputImage = new OutputImage(width, height);
  69. positions.Clear();
  70. kalmanFilters.Clear();
  71. }
  72. }
  73. }