123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Recognition.TouchRecognition;
- using bbiwarg.Utility;
- namespace bbiwarg.Graphics
- {
- class TouchEventVisualizer
- {
- public OutputImage OutputImage { get; private set; }
- private int width, height;
- private Dictionary<int, List<Vector2D>> positions;
- private Dictionary<int, Kalman2DPositionFilter> kalmanFilters;
- public TouchEventVisualizer(int width, int height)
- {
- this.width = width;
- this.height = height;
- this.positions = new Dictionary<int, List<Vector2D>>();
- this.kalmanFilters = new Dictionary<int, Kalman2DPositionFilter>();
- }
- public void addTouchEvent(int trackID, Vector2D relPos) {
- positions.Add(trackID, new List<Vector2D>());
- positions[trackID].Add(relPos);
- Kalman2DPositionFilter kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
- kalmanFilter.setInitialPosition(relPos);
- kalmanFilters.Add(trackID, kalmanFilter);
- }
- public void updateTouchEvent(int trackID, Vector2D relPos) {
- positions[trackID].Add(kalmanFilters[trackID].getCorrectedPosition(relPos));
- }
- public void removeTouchEvent(int trackID) {
- positions.Remove(trackID);
- kalmanFilters.Remove(trackID);
- }
- public void updateImage() {
- OutputImage = new OutputImage(width, height);
- //border
- OutputImage.drawRectangle(0, 0, width - 1, height - 1, Constants.TouchEventVisualizerGridColor);
- //draw grid
- int numRows = Constants.PalmGridRows;
- int numColumns = Constants.PalmGridColumns;
- int widthPerColumn = width / numColumns;
- int heightPerRow = height / numRows;
- for (int i = 0; i <= numColumns; i++)
- {
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Constants.TouchEventVisualizerGridColor);
- }
- for (int i = 0; i <= numRows; i++)
- {
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0,i*heightPerRow), new Vector2D(width-1,i*heightPerRow)), Constants.TouchEventVisualizerGridColor);
- }
- Vector2D maxPixel = Constants.ImageMaxPixel;
- foreach (int id in positions.Keys) {
- int numPositions = positions[id].Count;
- for (int i = 1; i < numPositions; i++) {
- OutputImage.drawLineSegment(new LineSegment2D(positions[id][i - 1].scale(maxPixel), positions[id][i].scale(maxPixel)), Constants.TouchEventVisualizerLineColor);
- }
- Vector2D lastPos = positions[id][numPositions - 1].scale(maxPixel);
- OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, Constants.TouchEventVisualizerPointColor);
- }
- }
- /*
- public void addPalmTouchEvent(PalmTouchEvent e, int currentFrame)
- {
- Vector2D pos;
- if (lastUpdated == -1 || (currentFrame - lastUpdated) > 5 || currentTouchPositions == null || kalmanFilter == null)
- {
- lastTouchPositions = currentTouchPositions;
- currentTouchPositions = new List<Vector2D>();
- kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
- kalmanFilter.setInitialPosition(e.RelativePalmPosition);
- pos = e.RelativePalmPosition;
- }
- else
- {
- pos = kalmanFilter.getCorrectedPosition(e.RelativePalmPosition);
- }
- int size = Math.Min(width, height);
- currentTouchPositions.Add(new Vector2D((int)(pos.X * size), (int)(size - pos.Y * size)));
- lastUpdated = currentFrame;
- }
- public void updateImage()
- {
- OutputImage = new OutputImage(width, height);
- bool touched = currentTouchPositions != null;
- int touchX = touched ? currentTouchPositions.Last<Vector2D>().IntX : -1;
- int touchY = touched ? currentTouchPositions.Last<Vector2D>().IntY : -1;
- //-- Border --
- int size = Math.Min(width, height);
- OutputImage.drawRectangle(0, 0, size - 1, size - 1, Color.White);
- //-- Grid --
- int numRows = Constants.PalmGridRows;
- int numColumns = Constants.PalmGridColumns;
- int cellX1 = -1;
- int cellY1 = -1;
- int cellX2 = -1;
- int cellY2 = -1;
- int tmp = -1;
- int tmp2 = -1;
-
- for (int i = 0; i < numRows; i++)
- {
- tmp = size / numRows * i;
- tmp2 = size / numRows * (i + 1);
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, tmp), new Vector2D(size - 2, tmp)), Constants.PalmGridColor);
- if (touched)
- if (touchY >= tmp && touchY < tmp2)
- {
- cellY1 = tmp;
- cellY2 = tmp2;
- }
- }
- for (int i = 0; i < numColumns; i++)
- {
- tmp = size / numColumns * i;
- tmp2 = size / numColumns * (i + 1);
- OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(tmp, 0), new Vector2D(tmp, size - 2)), Constants.PalmGridColor);
- if (touched)
- if (touchX >= tmp && touchX < tmp2)
- {
- cellX1 = tmp;
- cellX2 = tmp2;
- }
- }
- if (touched)
- OutputImage.drawRectangle(cellX1, cellY1, size/numColumns, size/numRows, Constants.PalmGridColor, -1);
- //-- Current Touch Event --
- if (touched)
- {
- for (int i = 1; i < currentTouchPositions.Count; ++i)
- {
- OutputImage.drawLineSegment(new LineSegment2D(currentTouchPositions[i - 1], currentTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
- }
- OutputImage.fillCircle(touchX, touchY, 3, Constants.TouchEventVisualizerPointColor);
- }
- //-- Last Touch Event --
- if (lastTouchPositions != null)
- {
- for (int i = 1; i < lastTouchPositions.Count; ++i)
- {
- OutputImage.drawLineSegment(new LineSegment2D(lastTouchPositions[i - 1], lastTouchPositions[i]), Constants.TouchEventVisualizerLineColor);
- }
- OutputImage.fillCircle(lastTouchPositions.Last<Vector2D>().IntX, lastTouchPositions.Last<Vector2D>().IntY, 3, Constants.TouchEventVisualizerPointColor);
- }
- }
- */
- public void Reset()
- {
- OutputImage = new OutputImage(width, height);
- positions.Clear();
- kalmanFilters.Clear();
- }
- }
- }
|