Forráskód Böngészése

touchEventVisualizer now keeps touchGestures for some time and fades them out

Alexander Hendrich 10 éve
szülő
commit
b551840035
2 módosított fájl, 66 hozzáadás és 30 törlés
  1. 7 4
      bbiwarg/Constants.cs
  2. 59 26
      bbiwarg/Graphics/TouchEventVisualizer.cs

+ 7 - 4
bbiwarg/Constants.cs

@@ -12,7 +12,7 @@ namespace bbiwarg
     class Constants
     {
         // Logger
-        public static readonly LogSubject LogLevel = LogSubject.FingerTracker;
+        public static readonly LogSubject LogLevel = LogSubject.TouchEvents;
         public static readonly int ConsoleWidth = 90;
         public static readonly int ConsoleHeight = 30;
         
@@ -99,16 +99,19 @@ namespace bbiwarg
         public static readonly int TouchEventFloodfillHighDiff = 3;
         public static readonly int TouchEventTipInsideFactor = 2;
         public static readonly int TouchEventTipOutsideFactor = 7;
-        public static readonly float TouchmXX = 0.0008f;
+        public static readonly float TouchmXX = 0.0016f;
         public static readonly float TouchmXY = 0.0f;
-        public static readonly float TouchmYY = 0.0008f;
+        public static readonly float TouchmYY = 0.0016f;
         public static readonly float TouchProcessNoise = 3.0e-4f;
 
         // touch tracking
         public static readonly int TouchEventNumFramesDetectedUntilTracked = 1;
-        public static readonly int TouchEventNumFramesLostUntilDeleted = 3;
+        public static readonly int TouchEventNumFramesLostUntilDeleted = 5;
         public static readonly float TouchEventMinSimilarityForTracking = 0.7f;
 
+        // touchEventVisualizer
+        public static readonly int TouchEventVisualizerFadeOutTime = 1500;
+
         // colors
         public static readonly Color ColorDetected = Color.Turquoise;
         public static readonly Color ColorTracked = Color.Yellow;

+ 59 - 26
bbiwarg/Graphics/TouchEventVisualizer.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Drawing;
+using System.Diagnostics;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -14,40 +15,61 @@ namespace bbiwarg.Graphics
         public OutputImage OutputImage { get; private set; }
 
         private int width, height;
+        private Stopwatch timer;
+
+        private Dictionary<int, List<Vector2D>> currentPositions;
+        private Dictionary<long, List<Vector2D>> oldPositions;
 
-        private Dictionary<int, List<Vector2D>> positions;
-        private Dictionary<int, Kalman2DPositionFilter> kalmanFilters;
 
         public TouchEventVisualizer(int width, int height)
         {
             this.width = width;
             this.height = height;
+            this.timer = new Stopwatch();
+            this.currentPositions = new Dictionary<int, List<Vector2D>>();
+            this.oldPositions = new Dictionary<long, List<Vector2D>>();
 
-            this.positions = new Dictionary<int, List<Vector2D>>();
-            this.kalmanFilters = new Dictionary<int, Kalman2DPositionFilter>();
+            this.timer.Start();
         }
 
         public void touchDown(object sender, PalmTouchEventArgs ptea)
         {
-            positions.Add(ptea.TrackID, new List<Vector2D>());
-            positions[ptea.TrackID].Add(ptea.Position);
-
-            Kalman2DPositionFilter kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
-            kalmanFilter.setInitialPosition(ptea.Position);
-            kalmanFilters.Add(ptea.TrackID, kalmanFilter);
+            List<Vector2D> posList = new List<Vector2D>();
+            posList.Add(ptea.Position);
+            currentPositions.Add(ptea.TrackID, posList);
         }
 
 
-        public void touchMove(object sender, PalmTouchEventArgs ptea) {
-            positions[ptea.TrackID].Add(kalmanFilters[ptea.TrackID].getCorrectedPosition(ptea.Position));
+        public void touchMove(object sender, PalmTouchEventArgs ptea)
+        {
+            currentPositions[ptea.TrackID].Add(ptea.Position);
         }
 
-        public void touchUp(object sender, PalmTouchEventArgs ptea) {
-            positions.Remove(ptea.TrackID);
-            kalmanFilters.Remove(ptea.TrackID);
+        public void touchUp(object sender, PalmTouchEventArgs ptea)
+        {
+            currentPositions[ptea.TrackID].Add(ptea.Position);
+
+            List<Vector2D> posList = currentPositions[ptea.TrackID];
+            oldPositions.Add(timer.ElapsedMilliseconds, posList);
+
+            currentPositions.Remove(ptea.TrackID);
         }
 
-        public void updateImage() {
+        public void updateImage()
+        {
+            //remove old positions
+            long currentTime = timer.ElapsedMilliseconds;
+
+            List<long> removeKeys = new List<long>();
+            foreach (long lastUpdateTime in oldPositions.Keys)
+            {
+                if (currentTime - lastUpdateTime > Constants.TouchEventVisualizerFadeOutTime)
+                    removeKeys.Add(lastUpdateTime);
+            }
+            foreach (long lastUpdateTime in removeKeys)
+                oldPositions.Remove(lastUpdateTime);
+
+
             OutputImage = new OutputImage(width, height);
 
             //border
@@ -66,25 +88,36 @@ namespace bbiwarg.Graphics
 
             for (int i = 0; i <= numRows; i++)
             {
-                OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0,i*heightPerRow), new Vector2D(width-1,i*heightPerRow)), Constants.TouchEventVisualizerGridColor);
+                OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0, i * heightPerRow), new Vector2D(width - 1, i * heightPerRow)), Constants.TouchEventVisualizerGridColor);
             }
 
+            foreach (int id in currentPositions.Keys)
+                drawTouchGesture(currentPositions[id], 1);
+            foreach (long lastUpdate in oldPositions.Keys)
+                drawTouchGesture(oldPositions[lastUpdate], 1 - ((currentTime - lastUpdate) / (float)Constants.TouchEventVisualizerFadeOutTime));
+
+        }
+
+        public void drawTouchGesture(List<Vector2D> positions, float opacity)
+        {
             Vector2D maxPixel = new Vector2D(width - 1, height - 1);
-            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);
+            int numPositions = positions.Count; 
+            Color lineColor = Constants.TouchEventVisualizerLineColor;
+            Color pointColor = Constants.TouchEventVisualizerPointColor;
+            Color lineColorFaded = Color.FromArgb((int)(opacity * lineColor.R), (int)(opacity * lineColor.G), (int)(opacity * lineColor.B));
+            Color pointColorFaded = Color.FromArgb((int)(opacity * pointColor.R), (int)(opacity * pointColor.G), (int)(opacity * pointColor.B));
+            for (int i = 1; i < numPositions; i++)
+            {
+                OutputImage.drawLineSegment(new LineSegment2D(positions[i - 1].scale(maxPixel), positions[i].scale(maxPixel)), lineColorFaded);
             }
+            Vector2D lastPos = positions[numPositions - 1].scale(maxPixel);
+            OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, pointColorFaded);
         }
 
         public void reset()
         {
             OutputImage = new OutputImage(width, height);
-            positions.Clear();
-            kalmanFilters.Clear();
+            currentPositions.Clear();
         }
     }
 }