Mise en place de la reconnaissance de formes
authorPAMPHILE Jonathan <pamphile@efrei.fr>
Wed, 28 Oct 2009 11:57:38 +0100
changeset 178 56041bd3761e
parent 177 89cbcb52b674
child 179 97ef988c92c2
Mise en place de la reconnaissance de formes
src/FingersDance.GestureControl/GestureControl.cs
src/FingersDance.GestureControl/GestureVector.cs
src/FingersDance.GestureControl/SurfaceGestureVectorDirection.cs
--- a/src/FingersDance.GestureControl/GestureControl.cs	Tue Oct 27 10:10:26 2009 +0100
+++ b/src/FingersDance.GestureControl/GestureControl.cs	Wed Oct 28 11:57:38 2009 +0100
@@ -110,37 +110,41 @@
 
         protected void AreaStrokeCollected(object source, RoutedEventArgs e)
         {
-            if (inProgess == null)
+            try
             {
-                inProgess = new Thread(RunCapture);
-                inProgess.Start();
-            }
-            else
-            {
-                inProgess.Abort();
-                inProgess = null;
-                inProgess = new Thread(RunCapture);
-                inProgess.Start();
+                if (inProgess == null)
+                {
+                    inProgess = new Thread(RunCapture);
+                    inProgess.Start();
+                }
+                else
+                {
+                    inProgess.Abort();
+                    inProgess = null;
+                    inProgess = new Thread(RunCapture);
+                    inProgess.Start();
+                }
+                Debug.WriteLine("collected", "Stroke");
+                System.Windows.Controls.InkCanvasStrokeCollectedEventArgs tmp = e as System.Windows.Controls.InkCanvasStrokeCollectedEventArgs;
+                // Apply a rotation with the angle of the first contact
+                Matrix rot = Matrix.Identity;
+                rot.Rotate(-this.angle);
+                Stroke s = tmp.Stroke;
+                s.Transform(rot, true);
+                // Get a list of point from a Bezier curve
+                StylusPointCollection tmp2 = s.GetBezierStylusPoints();
+                // Generate a list of SurfaceGesturePoint, just X and Y but can be improve
+                List<SurfaceGesturePoint> pointList = new List<SurfaceGesturePoint>();
+                foreach (StylusPoint p in tmp2)
+                    pointList.Add(new SurfaceGesturePoint { X = p.X, Y = p.Y });
+                // create the gesture analyser and set the list
+                SurfaceGesture gesture = new SurfaceGesture(pointList, 1);
+                // try to get a pattern from the list, if one, raise a event
+                _Gestures.Add(gesture);
+                SurfaceInkCanvas ink = e.OriginalSource as SurfaceInkCanvas;
+                ink.Strokes.Clear();
             }
-            Debug.WriteLine("collected", "Stroke");
-            System.Windows.Controls.InkCanvasStrokeCollectedEventArgs tmp = e as System.Windows.Controls.InkCanvasStrokeCollectedEventArgs;
-            // Apply a rotation with the angle of the first contact
-            Matrix rot = Matrix.Identity;
-            rot.Rotate(-this.angle);
-            Stroke s = tmp.Stroke;
-            s.Transform(rot, true);
-            // Get a list of point from a Bezier curve
-            StylusPointCollection tmp2 = s.GetBezierStylusPoints();
-            // Generate a list of SurfaceGesturePoint, just X and Y but can be improve
-            List<SurfaceGesturePoint> pointList = new List<SurfaceGesturePoint>();
-            foreach (StylusPoint p in tmp2)
-                pointList.Add(new SurfaceGesturePoint { X = p.X, Y = p.Y });
-            // create the gesture analyser and set the list
-            SurfaceGesture gesture = new SurfaceGesture(pointList, 1);
-            // try to get a pattern from the list, if one, raise a event
-            _Gestures.Add(gesture);
-            SurfaceInkCanvas ink = e.OriginalSource as SurfaceInkCanvas;
-            ink.Strokes.Clear();
+            catch { }
         }
 
         void RunCapture()
--- a/src/FingersDance.GestureControl/GestureVector.cs	Tue Oct 27 10:10:26 2009 +0100
+++ b/src/FingersDance.GestureControl/GestureVector.cs	Wed Oct 28 11:57:38 2009 +0100
@@ -126,7 +126,10 @@
         private void Generate(List<SurfaceGesturePoint> list)
         {
             if (list.Count < 2)
+            {
+                this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.TAP, Lenght = 0 });
                 return;
+            }
             SurfaceGestureVectorDirection lastDirection = SurfaceGestureVectorDirection.NONE;
             int lastPoint = 0;
             SurfaceGesturePoint LastChange = list[0];
@@ -213,41 +216,66 @@
                 }
                 ++lastPoint;
             }
+            Analyse(list);
+        }
 
+        private void Analyse(List<SurfaceGesturePoint> list)
+        {
             //  Analyse de la forme
 
-            int up = 0, down = 0, left = 0, right = 0, upleft = 0, upright = 0, downleft = 0, downright = 0;
+            double up = 0, down = 0, left = 0, right = 0, upleft = 0, upright = 0, downleft = 0, downright = 0;
             foreach (SurfaceGestureVector elt in this)
             {
                 switch (elt.Direction)
                 {
                     case SurfaceGestureVectorDirection.DOWN:
-                        down++;
+                        down += elt.Lenght;
                         break;
                     case SurfaceGestureVectorDirection.DOWNLEFT:
-                        downleft++;
+                        downleft += elt.Lenght;
                         break;
                     case SurfaceGestureVectorDirection.DOWNRIGHT:
-                        downright++;
+                        downright += elt.Lenght;
                         break;
                     case SurfaceGestureVectorDirection.LEFT:
-                        left++;
+                        left += elt.Lenght;
                         break;
                     case SurfaceGestureVectorDirection.RIGHT:
-                        right++;
+                        right += elt.Lenght;
                         break;
                     case SurfaceGestureVectorDirection.UP:
-                        up++;
+                        up += elt.Lenght;
                         break;
                     case SurfaceGestureVectorDirection.UPLEFT:
-                        upleft++;
+                        upleft += elt.Lenght;
                         break;
                     case SurfaceGestureVectorDirection.UPRIGHT:
-                        upright++;
+                        upright += elt.Lenght;
                         break;
                 }
             }
+
+            double diffX = Math.Abs(list[0].X - list[list.Count - 1].X);
+            double diffY = Math.Abs(list[0].Y - list[list.Count - 1].Y);
+
+            if (diffX < 5)
+            {
+                this.Clear();
+                if (up > down)
+                    this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.UP, Lenght = up });
+                else
+                    this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.DOWN, Lenght = down });
+            }
+            else if (diffY < 5)
+            {
+                this.Clear();
+                if (left > right)
+                    this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.LEFT, Lenght = left });
+                else
+                    this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.RIGHT, Lenght = right });
+            }
         }
+
         private SurfaceGestureVectorDirection GetHorizontal(SurfaceGesturePoint p1, SurfaceGesturePoint p2)
         {
             if (p1.Y < p2.Y)
--- a/src/FingersDance.GestureControl/SurfaceGestureVectorDirection.cs	Tue Oct 27 10:10:26 2009 +0100
+++ b/src/FingersDance.GestureControl/SurfaceGestureVectorDirection.cs	Wed Oct 28 11:57:38 2009 +0100
@@ -19,5 +19,6 @@
         DOWNLEFT,
         DOWNRIGHT,
         NONE,
+        TAP,
     }
 }