middleware/src/Tracking/KinectMain.cs
changeset 3 92f19af39024
parent 0 6fefd4afe506
child 5 d40f84d77db4
--- a/middleware/src/Tracking/KinectMain.cs	Fri Mar 09 18:15:12 2012 +0100
+++ b/middleware/src/Tracking/KinectMain.cs	Thu Mar 15 13:33:21 2012 +0100
@@ -1,5 +1,5 @@
 /*
- * Projet : KINECT PROJECTS
+ * Projet : TraKERS
  * Module : MIDDLEWARE
  * Sous-Module : Tracking
  * Classe : KinectMain
@@ -40,7 +40,8 @@
 using Trakers.Tracking.Gestures;
 using Trakers.Tracking.Events;
 using System.Configuration;
-using Kinect.Toolbox;
+using System.Resources;
+using System.Reflection;
 
 namespace Trakers.Tracking
 {
@@ -49,11 +50,17 @@
     public delegate void RightHandTrackedHandler(object o, RightHandTrackedEventArgs e);
     public delegate void LeftHandQuitHandler(object o, LeftHandQuitEventArgs e);
     public delegate void RightHandQuitHandler(object o, RightHandQuitEventArgs e);
-    //Il s'agit des fonctions permettant d'appeler les fonctions des événements Swipe left/right/up/down.
+    //Il s'agit de la fonction permettant d'appeler les fonctions des événements Swipe left/right/up/down.
     public delegate void SwipeHandler(object o, SwipeEventArgs e);
+    //Il s'agit de la fonction permettant d'appeler les fonctions des événements Push/Pull.
+    public delegate void PushHandler(object o, PushEventArgs e);
+    //Il s'agit de la fonction permettant d'appeler les fonctions des événements Jump.
+    public delegate void JumpHandler(object o, JumpEventArgs e);
 
     public class KinectMain
     {
+        //Gestionnaire de ressources.
+        private ResourceManager rm;
         //Fenêtre de debug.
         private Debug.DebugWindow debug;
         //Squelettes (Il y en a 6 par défaut).
@@ -61,13 +68,20 @@
         //Caméra infrarouge (sensor) de la Kinect.
         private KinectSensor kinectSensor;
 
-        //Détecteur de gestes.
+        //Détecteur de swipes.
         private SwipeDetector swipeDetector;
+        //Détecteur de pushes.
+        private PushDetector pushDetector;
+        //Détecteur de jumps.
+        private JumpDetector jumpDetector;
 
         //Distances min/max délimitant le champ de recherche.
         private float minDistHands;
         private float maxDistHands;
 
+        //Temps de rafraichissement pour le timer (Détection de gesture dans le serveur TUIO).
+        private int timerElapsing;
+
         //Serveur TUIO pour la connexion du Middleware vers le Front Atelier.
         private Server server;
 
@@ -76,15 +90,22 @@
         public static event RightHandTrackedHandler RightHandTrackedEvent;
         public static event LeftHandQuitHandler LeftHandQuitEvent;
         public static event RightHandQuitHandler RightHandQuitEvent;
-        //Les événements swipe.
+        //L'événement swipe.
         public static event SwipeHandler SwipeEvent;
+        //L'événement push.
+        public static event PushHandler PushEvent;
+        //L'événement jump.
+        public static event JumpHandler JumpEvent;
             
         //Voici les ID des noeuds d'un squelette.
-        private int hipCenterID = 0, spineID = 1, shoulderCenterID = 2, headID = 3;
-        private int shoulderLeftID = 4, elbowLeftID = 5, wristLeftID = 6, handLeftID = 7;
-        private int shoulderRightID = 8, elbowRightID = 9, wristRightID = 10, handRightID = 11;
-        private int hipLeftID = 12, kneeLeftID = 13, ankleLeftID = 14, footLeftID = 15;
-        private int hipRightID = 16, kneeRightID = 17, ankleRightID = 18, footRightID = 19;
+        public int hipCenterID = 0, spineID = 1, shoulderCenterID = 2, headID = 3;
+        public int shoulderLeftID = 4, elbowLeftID = 5, wristLeftID = 6, handLeftID = 7;
+        public int shoulderRightID = 8, elbowRightID = 9, wristRightID = 10, handRightID = 11;
+        public int hipLeftID = 12, kneeLeftID = 13, ankleLeftID = 14, footLeftID = 15;
+        public int hipRightID = 16, kneeRightID = 17, ankleRightID = 18, footRightID = 19;
+        
+        private string connexionHost;
+        private int connexionPort;
 
         /*
         *  Initialisation de la classe principale.
@@ -93,27 +114,28 @@
         */
         public KinectMain()
         {
+            //Si on n'a pas fait appel au gestionnaire de ressources avant, on le fait là.
+            if(rm == null)
+                rm = new ResourceManager("Trakers.Properties.resources", Assembly.GetExecutingAssembly());
             //On crée la fenêtre de debug.
             debug = new Debug.DebugWindow(this);
             
-            //On crée le détecteur de gestes.
-            swipeDetector = new SwipeDetector();
+            //On crée les détecteurs de gestes.
+            swipeDetector = new SwipeDetector(debug);
+            pushDetector = new PushDetector(debug);
+            jumpDetector = new JumpDetector(debug);
 
             //On tente de charger les paramètres du fichier params.ini.
             //Si on n'y arrive pas, on affiche une erreur et on charge les paramètres par défaut.
             if (!loadParameters())
             {
-                debug.ExceptionLbl.Content = "Impossible de charger les paramètres. Paramètres par défaut.";
+                debug.ExceptionLbl.Content = rm.GetString("loadParametersFail");
                 //Distances de détection des mains par défaut pour la recherche (ici de 1m à 2m de la Kinect).
                 minDistHands = (float)1.0;
                 maxDistHands = (float)1.5;
-            }
-            else if (maxDistHands <= 0 || minDistHands <= 0 || maxDistHands > 4 || minDistHands > 4 || minDistHands >= maxDistHands)
-            {
-                debug.ExceptionLbl.Content = "Paramètres incorrects. Paramètres par défaut.";
-                //Distances de détection des mains par défaut pour la recherche (ici de 1m à 2m de la Kinect).
-                minDistHands = (float)1.0;
-                maxDistHands = (float)1.5;
+                connexionHost = "127.0.0.1";
+                connexionPort = 80;
+                timerElapsing = 1000;
             }
 
             //On affiche la fenêtre de debug.
@@ -125,6 +147,14 @@
         }
 
         /*
+        *  Initialisation de la classe principale avec comme argument le gestionnaire de ressources.
+        */
+        public KinectMain(ResourceManager _rm) : this()
+        {
+            rm = _rm;
+        }
+
+        /*
         *  Initialisation du sensor de la Kinect.
         */
         public void KinectInitialization()
@@ -143,13 +173,21 @@
                 //Quand le Middleware reçoit des trames de la Kinect, on va dans cette fonction.
                 kinectSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(AllFramesReady);
 
+                //On applique des paramètres d'ajustement pour le squelette.
+                TransformSmoothParameters parameters = new TransformSmoothParameters();
+                parameters.Smoothing = 0.2f;
+                parameters.Correction = 0.8f;
+                parameters.Prediction = 0.0f;
+                parameters.JitterRadius = 0.5f;
+                parameters.MaxDeviationRadius = 0.5f;
+                kinectSensor.SkeletonStream.Enable(parameters);
                 //On démarre la Kinect.
                 kinectSensor.Start();
                 debug.ExceptionLbl.Content = "";
             }
             catch (System.Exception)
             {
-                debug.ExceptionLbl.Content = "Kinect non connectée.";
+                debug.ExceptionLbl.Content = rm.GetString("KinectNotConnected");
             }
 
             //Pour les événements main gauche/droite entre dans/quitte le champ, on a 4 listeners.
@@ -169,12 +207,20 @@
             RightHandQuitListener rightHandQuitListener = new RightHandQuitListener();
             RightHandQuitEvent += new RightHandQuitHandler(rightHandQuitListener.ShowOnScreen);
 
-            //Fonction appelée lorsque l'utilisateur effectue un swipe right.
-            SwipeEventListener swipeListener = new SwipeEventListener();
+            //Fonction appelée lorsque l'utilisateur effectue un Swipe right/left/up/down.
+            SwipeListener swipeListener = new SwipeListener();
             SwipeEvent += new SwipeHandler(swipeListener.ShowOnScreen);
 
+            //Fonction appelée lorsque l'utilisateur effectue un Push/Pull.
+            PushListener pushListener = new PushListener();
+            PushEvent += new PushHandler(pushListener.ShowOnScreen);
+
+            //Fonction appelée lorsque l'utilisateur effectue un Jump.
+            JumpListener jumpListener = new JumpListener();
+            JumpEvent += new JumpHandler(jumpListener.ShowOnScreen);
+
             //On connecte le serveur à l'adresse locale sur le port 80.
-            server = new Server("127.0.0.1", 80);
+            server = new Server(connexionHost, connexionPort, timerElapsing);
         }
 
         /*
@@ -193,14 +239,14 @@
             }
             catch (System.Exception)
             {
-                debug.ExceptionLbl.Content = "Kinect non connectée.";
+                debug.ExceptionLbl.Content = rm.GetString("KinectNotConnected");
             }
         }
 
         /*
         *  Récupère le premier squelette.
         */
-        Skeleton GetFirstSkeleton(AllFramesReadyEventArgs e)
+        Skeleton GetFirstSkeleton(object sender, AllFramesReadyEventArgs e)
         {
             using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame())
             {
@@ -220,7 +266,7 @@
         /*
         *  Récupère le squelette le plus proche.
         */
-        Skeleton GetNearestSkeleton(AllFramesReadyEventArgs e)
+        Skeleton GetNearestSkeleton(object sender, AllFramesReadyEventArgs e)
         {
             using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame())
             {
@@ -275,11 +321,11 @@
             //On récupère le premier squelette tracké.
             //Skeleton first = GetFirstSkeleton(e);
             //On récupère le plus proche squelette tracké.
-            Skeleton first = GetNearestSkeleton(e);
+            Skeleton first = GetNearestSkeleton(sender, e);
             //Si celui-ci n’est pas nul
             if (first == null)
                 return;
-
+            
             //Si ce squelette est tracké (donc suivi et reconnu par la camera)
             if (first.TrackingState == SkeletonTrackingState.Tracked)
             {
@@ -291,33 +337,30 @@
                 Joint hipRight = getJoint(first, hipRightID), kneeRight = getJoint(first, kneeRightID), ankleRight = getJoint(first, ankleRightID), footRight = getJoint(first, footRightID);
 
                 //On construit l'historique des postures.
-                /*List<Joint> joints = new List<Joint>();
+                List<Joint> joints = new List<Joint>();
                 joints.Clear();
-                joints.Add(footRight);
-                joints.Add(ankleRight);
-                joints.Add(kneeRight);
-                joints.Add(hipRight);
-                joints.Add(footLeft);
-                joints.Add(ankleLeft);
-                joints.Add(kneeLeft);
-                joints.Add(hipLeft);
-                joints.Add(handRight);
-                joints.Add(wristRight);
-                joints.Add(elbowRight);
-                joints.Add(shoulderRight);
-                joints.Add(handLeft);
-                joints.Add(wristLeft);
-                joints.Add(elbowLeft);
-                joints.Add(shoulderLeft);
-                joints.Add(head);
-                joints.Add(shoulderCenter);
-                joints.Add(spine);
-                joints.Add(hipCenter);
-                swipeDetector.UpdateSkeletonHistory(joints);*/
-
-                SkeletonDisplayManager sdm = new SkeletonDisplayManager(kinectSensor, debug.DebugCanvas);
-                //sdm.Draw();
-
+                joints.Insert(hipCenterID, hipCenter);
+                joints.Insert(spineID, spine);
+                joints.Insert(shoulderCenterID, shoulderCenter);
+                joints.Insert(headID, head);
+                joints.Insert(shoulderLeftID, shoulderLeft);
+                joints.Insert(elbowLeftID, elbowLeft);
+                joints.Insert(wristLeftID, wristLeft);
+                joints.Insert(handLeftID, handLeft);
+                joints.Insert(shoulderRightID, shoulderRight);
+                joints.Insert(elbowRightID, elbowRight);
+                joints.Insert(wristRightID, wristRight);
+                joints.Insert(handRightID, handRight);
+                joints.Insert(hipLeftID, hipLeft);
+                joints.Insert(kneeLeftID, kneeLeft);
+                joints.Insert(ankleLeftID, ankleLeft);
+                joints.Insert(footLeftID, footLeft);
+                joints.Insert(hipRightID, hipRight);
+                joints.Insert(kneeRightID, kneeRight);
+                joints.Insert(ankleRightID, ankleRight);
+                joints.Insert(footRightID, footRight);
+                GestureDetector.UpdateSkeletonHistory(joints);
+                
                 //On obtient sa distance à la Kinect.
                 float distance = first.Position.Z;
 
@@ -356,10 +399,38 @@
                     OnSwipeEvent(swipeEvent);
                 }
 
+                //Si l'utilisateur effectue un swipe right.
+                if (swipeDetector.CheckForSwipeRight())
+                {
+                    SwipeEventArgs swipeEvent = new SwipeEventArgs(debug, server, SwipeDetector.Direction.RIGHT);
+                    OnSwipeEvent(swipeEvent);
+                }
+
+                //Enum sur la main qui effectue le geste.
+                PushDetector.Hand handPush;
+                //Si l'utilisateur effectue un push.
+                if ((handPush = pushDetector.CheckForPush()) != PushDetector.Hand.NONE)
+                {
+                    PushEventArgs pushEvent = new PushEventArgs(debug, server, PushDetector.Direction.PUSH, handPush);
+                    OnPushEvent(pushEvent);
+                }
+                //Si l'utilisateur effectue un pull.
+                if ((handPush = pushDetector.CheckForPull()) != PushDetector.Hand.NONE)
+                {
+                    PushEventArgs pushEvent = new PushEventArgs(debug, server, PushDetector.Direction.PULL, handPush);
+                    OnPushEvent(pushEvent);
+                }
+
+                //Si l'utilisateur effectue un saut.
+                /*if (jumpDetector.CheckForJump())
+                {
+                    JumpEventArgs jumpEvent = new JumpEventArgs(debug, server);
+                    OnJumpEvent(jumpEvent);
+                }*/
 
                 //Dessine le squelette dans le debug.
-                //debug.drawJoints(first.Joints, first);
-                //debug.showSkeleton(hipCenter, spine, shoulderCenter, head, shoulderLeft, elbowLeft, wristLeft, handLeft, shoulderRight, elbowRight, wristRight, handRight, hipLeft, kneeLeft, ankleLeft, footLeft, hipRight, kneeRight, ankleRight, footRight);
+                debug.drawJoints(first.Joints, first);
+                debug.showSkeleton(hipCenter, spine, shoulderCenter, head, shoulderLeft, elbowLeft, wristLeft, handLeft, shoulderRight, elbowRight, wristRight, handRight, hipLeft, kneeLeft, ankleLeft, footLeft, hipRight, kneeRight, ankleRight, footRight);
             }
         }
 
@@ -417,22 +488,22 @@
         }
 
         /*
-        *  Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un swipe up.
+        *  Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un push.
         */
-        /*public static void OnSwipeUpEvent(SwipeUpEventArgs e)
+        public static void OnPushEvent(PushEventArgs e)
         {
-            if (SwipeUpEvent != null)
-                SwipeUpEvent(new object(), e);
-        }*/
+            if (PushEvent != null)
+                PushEvent(new object(), e);
+        }
 
         /*
-        *  Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un swipe down.
+        *  Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un saut.
         */
-        /*public static void OnSwipeDownEvent(SwipeDownEventArgs e)
+        public static void OnJumpEvent(JumpEventArgs e)
         {
-            if (SwipeDownEvent != null)
-                SwipeDownEvent(new object(), e);
-        }*/
+            if (JumpEvent != null)
+                JumpEvent(new object(), e);
+        }
 
         /*
         *  Méthode de chargement des paramètres (position du champ de recherche...).
@@ -441,14 +512,42 @@
         {
             try
             {
-                minDistHands = float.Parse(ConfigurationManager.AppSettings["searchMinDistance"]);
-                maxDistHands = float.Parse(ConfigurationManager.AppSettings["searchMaxDistance"]);
+                minDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMinDistance"]);
+                maxDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMaxDistance"]);
+                connexionHost = ConfigurationManager.AppSettings["connexionHost"];
+                connexionPort = int.Parse(ConfigurationManager.AppSettings["connexionPort"]);
+                hipCenterID = int.Parse(ConfigurationManager.AppSettings["hipCenterID"]);
+                spineID = int.Parse(ConfigurationManager.AppSettings["spineID"]);
+                shoulderCenterID = int.Parse(ConfigurationManager.AppSettings["shoulderCenterID"]);
+                headID = int.Parse(ConfigurationManager.AppSettings["headID"]);
+                shoulderLeftID = int.Parse(ConfigurationManager.AppSettings["shoulderLeftID"]);
+                elbowLeftID = int.Parse(ConfigurationManager.AppSettings["elbowLeftID"]);
+                wristLeftID = int.Parse(ConfigurationManager.AppSettings["wristLeftID"]);
+                handLeftID = int.Parse(ConfigurationManager.AppSettings["handLeftID"]);
+                shoulderRightID = int.Parse(ConfigurationManager.AppSettings["shoulderRightID"]);
+                elbowRightID = int.Parse(ConfigurationManager.AppSettings["elbowRightID"]);
+                wristRightID = int.Parse(ConfigurationManager.AppSettings["wristRightID"]);
+                handRightID = int.Parse(ConfigurationManager.AppSettings["handRightID"]);
+                hipLeftID = int.Parse(ConfigurationManager.AppSettings["hipLeftID"]);
+                kneeLeftID = int.Parse(ConfigurationManager.AppSettings["kneeLeftID"]);
+                ankleLeftID = int.Parse(ConfigurationManager.AppSettings["ankleLeftID"]);
+                footLeftID = int.Parse(ConfigurationManager.AppSettings["footLeftID"]);
+                hipRightID = int.Parse(ConfigurationManager.AppSettings["hipRightID"]);
+                kneeRightID = int.Parse(ConfigurationManager.AppSettings["kneeRightID"]);
+                ankleRightID = int.Parse(ConfigurationManager.AppSettings["ankleRightID"]);
+                footRightID = int.Parse(ConfigurationManager.AppSettings["footRightID"]);
+                timerElapsing = int.Parse(ConfigurationManager.AppSettings["timerElapsing"]);
             }
             catch (Exception)
             {
                 return false;
             }
-
+            
+            if (maxDistHands <= 0 || minDistHands <= 0 || maxDistHands > 4 || minDistHands > 4 || minDistHands >= maxDistHands)
+            {
+                debug.ExceptionLbl.Content = rm.GetString("loadParametersIncorrect");
+                return false;
+            }
             return true;
         }
     }