using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using bbiwarg.Detectors.TouchDetection; using bbiwarg.Utility; namespace bbiwarg.Graphics { class TouchEventVisualizer { public OutputImage OutputImage { get; private set; } private int lastUpdated = -1; private int width, height; private List lastTouchPositions; private List currentTouchPositions; private Kalman2DPositionFilter kalmanFilter; public TouchEventVisualizer(int width, int height) { this.width = width; this.height = height; OutputImage = new Graphics.OutputImage(width, height); } public void addPalmTouchEvent(PalmTouchEvent e, int currentFrame) { Vector2D pos; if (lastUpdated == -1 || (currentFrame - lastUpdated) > 5 || currentTouchPositions == null || kalmanFilter == null) { lastTouchPositions = currentTouchPositions; currentTouchPositions = new List(); 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 ? true : false; int touchX = touched ? currentTouchPositions.Last().IntX : -1; int touchY = touched ? currentTouchPositions.Last().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().IntX, lastTouchPositions.Last().IntY, 3, Constants.TouchEventVisualizerPointColor); } } public void Reset() { lastTouchPositions = null; currentTouchPositions = null; OutputImage = new OutputImage(width, height); lastUpdated = -1; } } }