middleware/Debug/DebugWindow.xaml.cs
changeset 15 4b78f179e7ce
child 16 a9ebacd6c089
equal deleted inserted replaced
14:10d5199d9874 15:4b78f179e7ce
       
     1 /*
       
     2 * This file is part of the TraKERS\Middleware package.
       
     3 *
       
     4 * (c) IRI <http://www.iri.centrepompidou.fr/>
       
     5 *
       
     6 * For the full copyright and license information, please view the LICENSE_MIDDLEWARE
       
     7 * file that was distributed with this source code.
       
     8 */
       
     9 
       
    10 /*
       
    11  * Projet : TraKERS
       
    12  * Module : MIDDLEWARE
       
    13  * Sous-Module : Debug
       
    14  * Classe : DebugWindow
       
    15  * 
       
    16  * Auteur : alexandre.bastien@iri.centrepompidou.fr
       
    17  * 
       
    18  * Fonctionnalités : Reçoit des notifications des sous-modules Tracking, Communication et Exception.
       
    19  * Intéragit avec la fenêtre XAML de debug de façon à afficher un rendu visuel.
       
    20  */
       
    21 
       
    22 using System;
       
    23 using System.Windows;
       
    24 using System.Windows.Controls;
       
    25 using System.Windows.Media;
       
    26 using System.Windows.Media.Imaging;
       
    27 using System.Windows.Shapes;
       
    28 using System.Drawing;
       
    29 using Microsoft.Kinect;
       
    30 using System.Resources;
       
    31 using System.Reflection;
       
    32 using System.Timers;
       
    33 using System.Configuration;
       
    34 
       
    35 namespace Trakers.Debug
       
    36 {
       
    37     public partial class DebugWindow : Window
       
    38     {
       
    39         //Gestionnaire de ressources.
       
    40         private ResourceManager rm;
       
    41 
       
    42         //Paramètres du serveur TUIO.
       
    43         private string connexionHost;
       
    44         private int connexionPort;
       
    45         //Temps de rafraichissement pour le timer (Détection de gesture dans le serveur TUIO).
       
    46         private int timerElapsing;
       
    47         //Distances min/max délimitant le champ de recherche.
       
    48         private float minDistHands;
       
    49         private float maxDistHands;
       
    50         private float minDist;
       
    51         private float maxDist;
       
    52         private float zeroPoint;
       
    53         //Paramètres de la mosaïque.
       
    54         private int imagesToShow;
       
    55         //Paramètres de la recherche par courbes.
       
    56         private int takenPoints;
       
    57         private int directionChangeTresholdXY;
       
    58         private float directionChangeTresholdZ;
       
    59 
       
    60         //Timer.
       
    61         private System.Timers.Timer _timer;
       
    62         //Membre permettant d'atteindre la classe KinectMain du sous-module Tracking.
       
    63         //private KinectMain kinectMain;
       
    64         //Tableau contenant une image en couleurs.
       
    65         private byte[] colorPixelData;
       
    66         //Indique si la kinect est allumée/éteinte.
       
    67         private bool on;
       
    68         //Indique si la fenêtre de debug est actuellement en cours de fermeture.
       
    69         private bool closing;
       
    70         //Indique si l'image doit être raffraichie.
       
    71         private Boolean refreshImage;
       
    72 
       
    73         /*
       
    74         * Constructeur : Affiche la fenêtre de debug en lui passant en paramètre une instanciation de la
       
    75         * classe KinectMain.
       
    76         * Au départ, la kinect est éteinte.
       
    77         */
       
    78         public DebugWindow()//KinectMain main)
       
    79         {
       
    80             InitializeComponent();
       
    81             //On fait appel au gestionnaire de ressources.
       
    82             rm = new ResourceManager("Trakers.Debug.Properties.Resources", Assembly.GetExecutingAssembly());
       
    83             //On tente de charger les paramètres du fichier params.ini.
       
    84             //Si on n'y arrive pas, on affiche une erreur et on charge les paramètres par défaut.
       
    85             if (!loadParameters())
       
    86             {
       
    87                 ExceptionLbl.Content = rm.GetString("loadParametersFail");
       
    88                 //Distances de détection des mains par défaut pour la recherche (ici de 1m à 2m de la Kinect).
       
    89                 minDistHands = 1.0f;
       
    90                 maxDistHands = 1.5f;
       
    91                 minDist = 1.0f;
       
    92                 maxDist = 4.0f;
       
    93                 zeroPoint = 1.7f;
       
    94                 connexionHost = "127.0.0.1";
       
    95                 connexionPort = 80;
       
    96                 timerElapsing = 1000;
       
    97                 imagesToShow = 25;
       
    98                 takenPoints = 10;
       
    99                 directionChangeTresholdXY = 10;
       
   100                 directionChangeTresholdZ = 0.01f;
       
   101             }
       
   102 
       
   103             //kinectMain = main;
       
   104             on = true;
       
   105             closing = false;
       
   106             refreshImage = true;
       
   107             try
       
   108             {
       
   109                 //On instancie le timer à N ms.
       
   110                 _timer = new System.Timers.Timer(timerElapsing);
       
   111                 //Dès que le timer est expiré, on appelle _timer_Elapsed.
       
   112                 _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
       
   113             }
       
   114             catch (Exception){}
       
   115         }
       
   116 
       
   117         /*
       
   118          * Méthode d'effacement du label d'exceptions.
       
   119          */
       
   120         public void NoException()
       
   121         {
       
   122             ExceptionLbl.Content = "";
       
   123         }
       
   124 
       
   125         /*
       
   126          * Méthode d'affichage des exceptions dans le label d'exceptions.
       
   127          */
       
   128         public void ShowException(String reference)
       
   129         {
       
   130             ExceptionLbl.Content = rm.GetString(reference);
       
   131         }
       
   132 
       
   133         /*
       
   134          * Méthode de fermeture de l'interface.
       
   135          */
       
   136         public void ShutDownInterface()
       
   137         {
       
   138             DebugImage.Source = null;
       
   139             ExceptionLbl.Content = "";
       
   140         }
       
   141 
       
   142         /*
       
   143          * Méthode appelée à l'expiration du timer pour les gestures et modes.
       
   144          */
       
   145         public void _timer_Elapsed(object sender, ElapsedEventArgs e)
       
   146         {
       
   147             //On débloque le raffraichissement de l'image.
       
   148             refreshImage = true;
       
   149             //On arrête le timer.
       
   150             _timer.Stop();
       
   151         }
       
   152 
       
   153         /*
       
   154         * Getter pour le membre indiquant la fermeture de la fenêtre de debug.
       
   155         */
       
   156         public bool isClosing()
       
   157         {
       
   158             return closing;
       
   159         }
       
   160 
       
   161         /*
       
   162         * Est appelée à la fermeture de la fenêtre.
       
   163         */
       
   164         public void Window_Closed(object sender, EventArgs e)
       
   165         {
       
   166             closing = true;
       
   167         }
       
   168 
       
   169         /*
       
   170         * Bouton ON/OFF.
       
   171         */
       
   172         private void Switch_Click(object sender, RoutedEventArgs e)
       
   173         {
       
   174             //S'il valait faux, il vaut vrai maintenant et inversement.
       
   175             on = !on;
       
   176             //Si la kinect est allumée.
       
   177             if (on)
       
   178             {
       
   179                 //Il affiche OFF (pour éteindre la kinect).
       
   180                 Switch.Content = "OFF";
       
   181                 //On vide le label des exceptions.
       
   182                 ExceptionLbl.Content = "";
       
   183             }
       
   184             else
       
   185             {
       
   186                 //Il affiche ON (pour allumer la kinect).
       
   187                 Switch.Content = "ON";
       
   188                 //On remet à zéro tous les éléments du retour visuel.
       
   189                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   190                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   191                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   192                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   193                 DistanceLbl.Content = "Distance :";
       
   194                 LeftHand.Background = System.Windows.Media.Brushes.DarkGray;
       
   195                 LeftHand.Content = "";
       
   196                 RightHand.Background = System.Windows.Media.Brushes.DarkGray;
       
   197                 RightHand.Content = "";
       
   198                 DebugCanvas.Children.RemoveRange(1, DebugCanvas.Children.Count - 1);
       
   199             }
       
   200         }
       
   201 
       
   202         /*
       
   203         *  Récupère le flux video et met à jour le rendu visuel de debug.
       
   204         */
       
   205         public void RefreshVideo(AllFramesReadyEventArgs e)
       
   206         {
       
   207             if (refreshImage)
       
   208             {
       
   209                 bool receivedData = false;
       
   210                 ColorImageFrame colorImageFrameData;
       
   211                 using (colorImageFrameData = e.OpenColorImageFrame())
       
   212                 {
       
   213                     //Si on ne reçoit pas de trames de la kinect.
       
   214                     if (colorImageFrameData == null)
       
   215                     {
       
   216                         //L'image est supprimée.
       
   217                         DebugImage.Source = null;
       
   218                     }
       
   219                     //Si le tableau stockant l'image en cours est nul.
       
   220                     if (colorPixelData == null)
       
   221                         //On alloue un nouveau tableau.
       
   222                         colorPixelData = new byte[colorImageFrameData.PixelDataLength];
       
   223                     else
       
   224                     {
       
   225                         try
       
   226                         {
       
   227                             //Sinon on met à jour le tableau en copiant le contenu de la trame dans le tableau.
       
   228                             colorImageFrameData.CopyPixelDataTo(colorPixelData);
       
   229                             receivedData = true;
       
   230                         }
       
   231                         catch (Exception) { }
       
   232                     }
       
   233                 }
       
   234                 //Si on a des données dans le tableau et que la kinect est allumée.
       
   235                 if (receivedData && on)
       
   236                 {
       
   237                     //On met à jour l'image de la caméra.
       
   238                     DebugImage.Source = BitmapSource.Create(colorImageFrameData.Width, colorImageFrameData.Height, 96, 96, PixelFormats.Bgr32, null, colorPixelData, colorImageFrameData.Width * colorImageFrameData.BytesPerPixel);
       
   239                     DebugImage.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
       
   240                     DebugImage.VerticalAlignment = System.Windows.VerticalAlignment.Center;
       
   241                     DebugImage.Stretch = Stretch.Fill;
       
   242                     //On annule l'image associée aux gestures.
       
   243                     Gestures.Source = null;
       
   244                 }
       
   245             }
       
   246         }
       
   247 
       
   248         /*
       
   249         * Affiche la distance de l'utilisateur dans le rendu visuel.
       
   250         * Sous forme de nombre en m et de rectangles changeant de couleur en fonction de la distance.
       
   251         */
       
   252         public void showDistance(float proximity)
       
   253         {
       
   254             DistanceLbl.Content = "Proximity : " + proximity + "%";
       
   255 
       
   256             if (proximity == 0)
       
   257             {
       
   258                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   259                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   260                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   261                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   262                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   263                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   264                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   265                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   266                 R8.Fill = System.Windows.Media.Brushes.DarkGray;
       
   267                 R9.Fill = System.Windows.Media.Brushes.DarkGray;
       
   268                 R10.Fill = System.Windows.Media.Brushes.DarkGray;
       
   269             }
       
   270             else if (proximity > 0 && proximity < 10)
       
   271             {
       
   272                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   273                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   274                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   275                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   276                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   277                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   278                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   279                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   280                 R8.Fill = System.Windows.Media.Brushes.DarkGray;
       
   281                 R9.Fill = System.Windows.Media.Brushes.DarkGray;
       
   282                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   283             }
       
   284             else if (proximity > 10 && proximity < 20)
       
   285             {
       
   286                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   287                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   288                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   289                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   290                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   291                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   292                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   293                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   294                 R8.Fill = System.Windows.Media.Brushes.DarkGray;
       
   295                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   296                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   297             }
       
   298             else if (proximity > 20 && proximity < 30)
       
   299             {
       
   300                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   301                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   302                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   303                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   304                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   305                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   306                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   307                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   308                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   309                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   310                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   311             }
       
   312             else if (proximity > 30 && proximity < 40)
       
   313             {
       
   314                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   315                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   316                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   317                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   318                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   319                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   320                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   321                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   322                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   323                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   324                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   325             }
       
   326             else if (proximity > 40 && proximity < 50)
       
   327             {
       
   328                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   329                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   330                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   331                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   332                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   333                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   334                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   335                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   336                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   337                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   338                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   339             }
       
   340             else if (proximity > 50 && proximity < 60)
       
   341             {
       
   342                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   343                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   344                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   345                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   346                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   347                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   348                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   349                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   350                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   351                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   352                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   353             }
       
   354             else if (proximity > 60 && proximity < 70)
       
   355             {
       
   356                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   357                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   358                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   359                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   360                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   361                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   362                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   363                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   364                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   365                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   366                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   367             }
       
   368             else if (proximity > 70 && proximity < 80)
       
   369             {
       
   370                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   371                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   372                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   373                 R3.Fill = System.Windows.Media.Brushes.Yellow;
       
   374                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   375                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   376                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   377                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   378                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   379                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   380                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   381             }
       
   382             else if (proximity > 80 && proximity < 90)
       
   383             {
       
   384                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   385                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   386                 R2.Fill = System.Windows.Media.Brushes.Yellow;
       
   387                 R3.Fill = System.Windows.Media.Brushes.Yellow;
       
   388                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   389                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   390                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   391                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   392                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   393                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   394                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   395             }
       
   396             else if (proximity > 90 && proximity < 100)
       
   397             {
       
   398                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   399                 R1.Fill = System.Windows.Media.Brushes.Yellow;
       
   400                 R2.Fill = System.Windows.Media.Brushes.Yellow;
       
   401                 R3.Fill = System.Windows.Media.Brushes.Yellow;
       
   402                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   403                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   404                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   405                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   406                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   407                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   408                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   409             }
       
   410             else if (proximity == 100)
       
   411             {
       
   412                 R0.Fill = System.Windows.Media.Brushes.Green;
       
   413                 R1.Fill = System.Windows.Media.Brushes.Yellow;
       
   414                 R2.Fill = System.Windows.Media.Brushes.Yellow;
       
   415                 R3.Fill = System.Windows.Media.Brushes.Yellow;
       
   416                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   417                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   418                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   419                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   420                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   421                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   422                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   423             }
       
   424         }
       
   425 
       
   426         /*
       
   427         * Affiche la détection de la main droite via un label.
       
   428         */
       
   429         public void showRightHandRect(bool show)
       
   430         {
       
   431             if (show)
       
   432                 RightHand.Background = System.Windows.Media.Brushes.Blue;
       
   433             else
       
   434                 RightHand.Background = System.Windows.Media.Brushes.DarkGray;
       
   435         }
       
   436 
       
   437         /*
       
   438         * Affiche la détection de la main gauche via un label.
       
   439         */
       
   440         public void showLeftHandRect(bool show)
       
   441         {
       
   442             if (show)
       
   443                 LeftHand.Background = System.Windows.Media.Brushes.Blue;
       
   444             else
       
   445                 LeftHand.Background = System.Windows.Media.Brushes.DarkGray;
       
   446         }
       
   447 
       
   448         /*
       
   449         * Dessine les noeuds du squelette dans le rendu visuel.
       
   450         */
       
   451         public void drawJoints(JointCollection joints, Skeleton first)
       
   452         {
       
   453             if (refreshImage)
       
   454             {
       
   455                 //On enlève tout élément du Canvas à part l'image, de manière à mettre à jour la position du squelette. 
       
   456                 DebugCanvas.Children.RemoveRange(1, DebugCanvas.Children.Count - 1);
       
   457 
       
   458                 //Pour chaque noeud.
       
   459                 foreach (Joint joint in first.Joints)
       
   460                 {
       
   461                     //On crée une ellipse de taille 20 et de largeur 20.
       
   462                     Ellipse node = new Ellipse();
       
   463                     node.Height = 20;
       
   464                     node.Width = 20;
       
   465 
       
   466                     //S'il s'agit d'un noeud de tête, on le colorie en rouge, sinon en bleu.
       
   467                     if (joint.JointType == JointType.Head)
       
   468                         node.Fill = System.Windows.Media.Brushes.Red;
       
   469                     else if (joint.JointType == JointType.ShoulderCenter)
       
   470                         node.Fill = System.Windows.Media.Brushes.Green;
       
   471                     else if (joint.JointType == JointType.HipCenter)
       
   472                         node.Fill = System.Windows.Media.Brushes.Green;
       
   473                     else if (joint.JointType == JointType.HandRight)
       
   474                         node.Fill = System.Windows.Media.Brushes.Red;
       
   475                     else
       
   476                         node.Fill = System.Windows.Media.Brushes.Blue;
       
   477 
       
   478                     //On met à la bonne échelle les coordonnées des positions des noeuds.
       
   479                     Joint scaledJoint = Coding4Fun.Kinect.Wpf.SkeletalExtensions.ScaleTo(joint, 600, 400, 0.75f, 0.75f);
       
   480 
       
   481                     //On positionne le noeud dans le Canvas, et on l'ajoute.
       
   482                     Canvas.SetLeft(node, scaledJoint.Position.X);
       
   483                     Canvas.SetTop(node, scaledJoint.Position.Y);
       
   484                     DebugCanvas.Children.Add(node);
       
   485                 }
       
   486             }
       
   487         }
       
   488 
       
   489         /*
       
   490         * Dessine un os, en ayant en paramètres deux noeuds.
       
   491         */
       
   492         public void drawBone(Joint j1, Joint j2)
       
   493         {
       
   494             //On crée une nouvelle ligne verte d'épaisseur 8 entre les deux noeuds et on l'ajoute au Canvas.
       
   495             Line line = new Line();
       
   496             line.Stroke = System.Windows.Media.Brushes.Green;
       
   497             line.X1 = j1.Position.X;
       
   498             line.X2 = j2.Position.X;
       
   499             line.Y1 = j1.Position.Y;
       
   500             line.Y2 = j2.Position.Y;
       
   501             line.StrokeThickness = 8;
       
   502             DebugCanvas.Children.Add(line);
       
   503         }
       
   504 
       
   505         /*
       
   506         * Dessine le squelette (ensemble des os), en ayant en paramètres tous les noeuds.
       
   507         */
       
   508         public void showSkeleton(Joint hipCenter, Joint spine, Joint shoulderCenter, Joint head, Joint shoulderLeft, Joint elbowLeft, Joint wristLeft, Joint handLeft, Joint shoulderRight, Joint elbowRight, Joint wristRight, Joint handRight, Joint hipLeft, Joint kneeLeft, Joint ankleLeft, Joint footLeft, Joint hipRight, Joint kneeRight, Joint ankleRight, Joint footRight)
       
   509         {
       
   510             if (refreshImage)
       
   511             {
       
   512                 //On met les noeuds deux par deux en fonction de leur position dans le squelette.
       
   513                 drawBone(head, shoulderCenter);
       
   514                 drawBone(shoulderCenter, shoulderLeft);
       
   515                 drawBone(shoulderLeft, elbowLeft);
       
   516                 drawBone(elbowLeft, wristLeft);
       
   517                 drawBone(wristLeft, handLeft);
       
   518                 drawBone(shoulderCenter, shoulderRight);
       
   519                 drawBone(shoulderRight, elbowRight);
       
   520                 drawBone(elbowRight, wristRight);
       
   521                 drawBone(wristRight, handRight);
       
   522                 drawBone(shoulderCenter, spine);
       
   523                 drawBone(spine, hipCenter);
       
   524                 drawBone(hipCenter, hipLeft);
       
   525                 drawBone(hipLeft, kneeLeft);
       
   526                 drawBone(kneeLeft, ankleLeft);
       
   527                 drawBone(ankleLeft, footLeft);
       
   528                 drawBone(hipCenter, hipRight);
       
   529                 drawBone(hipRight, kneeRight);
       
   530                 drawBone(kneeRight, ankleRight);
       
   531                 drawBone(ankleRight, footRight);
       
   532             }
       
   533         }
       
   534 
       
   535         /*
       
   536         * Cache le squelette et le reste de l'interface à part l'image.
       
   537         */
       
   538         public void hideSkeleton()
       
   539         {
       
   540             //On vide le canvas mais en gardant l'image.
       
   541             if(DebugCanvas.Children.Count > 1)
       
   542                 DebugCanvas.Children.RemoveRange(1, DebugCanvas.Children.Count - 1);
       
   543             //On colore en gris tous les indicateurs.
       
   544             R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   545             R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   546             R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   547             R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   548             R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   549             R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   550             R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   551             R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   552             R8.Fill = System.Windows.Media.Brushes.DarkGray;
       
   553             R9.Fill = System.Windows.Media.Brushes.DarkGray;
       
   554             R10.Fill = System.Windows.Media.Brushes.DarkGray;
       
   555         }
       
   556 
       
   557         /*
       
   558         * Affiche la position de la main gauche dans le rendu visuel.
       
   559         */
       
   560         public void showLeftHandCoord(String coord)
       
   561         {
       
   562             LeftHand.Content = coord;
       
   563         }
       
   564 
       
   565         /*
       
   566         * Affiche la position de la main gauche dans le rendu visuel.
       
   567         */
       
   568         public void showRightHandCoord(String coord)
       
   569         {
       
   570             RightHand.Content = coord;
       
   571         }
       
   572 
       
   573         /*
       
   574          * Méthode associée à l'événement : Ouvrir la fenêtre de paramétrage via le menu.
       
   575          */
       
   576         private void Parameters_Click(object sender, RoutedEventArgs e)
       
   577         {
       
   578             DebugParameters param = new DebugParameters(this);
       
   579 
       
   580             try
       
   581             {
       
   582                 param.ShowDialog();
       
   583             }
       
   584             catch (Exception)
       
   585             {
       
   586                 ExceptionLbl.Content = rm.GetString("loadParamFail");
       
   587             }
       
   588         }
       
   589 
       
   590         /*
       
   591          * Méthode associée à l'événement : Quitter via le menu.
       
   592          */
       
   593         public void Quit_Click(object sender, RoutedEventArgs e)
       
   594         {
       
   595             closing = true;
       
   596             //On éteint la Kinect (pour éviter qu'elle reste allumée même lorsque le programme est éteint).
       
   597  
       
   598             Application.Current.Shutdown();
       
   599         }
       
   600 
       
   601         /*
       
   602          * Permet d'initialiser la Kinect dès que la fenêtre est lancée.
       
   603          */
       
   604         private void Window_Loaded(object sender, RoutedEventArgs e)
       
   605         {
       
   606             //kinectMain.KinectInitialization();
       
   607         }
       
   608 
       
   609         /*
       
   610          * Méthode d'affichage des gestures.
       
   611          */
       
   612         public void showGesture(String gesture)
       
   613         {
       
   614             if (refreshImage)
       
   615             {
       
   616                 refreshImage = false;
       
   617                 _timer.Start();
       
   618                 Bitmap bitmap = null;
       
   619                 //S'il s'agit de telle ou telle gesture, on prend l'image correspondante dans les ressources,
       
   620                 //on la convertit et on l'affiche.
       
   621                 switch (gesture)
       
   622                 {
       
   623                     case "SWIPE-LEFT": bitmap = (Bitmap)rm.GetObject("swipe_left");
       
   624                         break;
       
   625                     case "SWIPE-RIGHT": bitmap = (Bitmap)rm.GetObject("swipe_right");
       
   626                         break;
       
   627                     case "PUSH-RIGHT": bitmap = (Bitmap)rm.GetObject("push_right");
       
   628                         break;
       
   629                     case "PUSH-LEFT": bitmap = (Bitmap)rm.GetObject("push_left");
       
   630                         break;
       
   631                     case "PUSH-BOTH": bitmap = (Bitmap)rm.GetObject("push_both");
       
   632                         break;
       
   633                     case "PULL-RIGHT": bitmap = (Bitmap)rm.GetObject("pull_right");
       
   634                         break;
       
   635                     case "PULL-LEFT": bitmap = (Bitmap)rm.GetObject("pull_left");
       
   636                         break;
       
   637                     case "PULL-BOTH": bitmap = (Bitmap)rm.GetObject("pull_both");
       
   638                         break;
       
   639                 }
       
   640                 Gestures.Source = CreateBitmapSourceFromBitmap(bitmap);
       
   641             }
       
   642 
       
   643             DebugImage.Source = null;
       
   644             hideSkeleton();
       
   645         }
       
   646 
       
   647         /*
       
   648          * Méthode d'indication de raffraichissement de l'image ("on la raffraichit ou pas ?").
       
   649          */
       
   650         public void setRefreshImage(bool refresh)
       
   651         {
       
   652             refreshImage = refresh;
       
   653         }
       
   654 
       
   655         /*
       
   656          * Méthode de conversion de Bitmap (des ressources) en BitmapSource (du debug).
       
   657          */
       
   658         public static BitmapSource CreateBitmapSourceFromBitmap(Bitmap bitmap)
       
   659         {
       
   660             if (bitmap == null)
       
   661                 return null;
       
   662 
       
   663             return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       
   664                 bitmap.GetHbitmap(),
       
   665                 IntPtr.Zero,
       
   666                 Int32Rect.Empty,
       
   667                 BitmapSizeOptions.FromEmptyOptions());
       
   668         }
       
   669 
       
   670         /*
       
   671         *  Méthode de chargement des paramètres (position du champ de recherche...).
       
   672         */
       
   673         public bool loadParameters()
       
   674         {
       
   675             try
       
   676             {
       
   677                 minDistHands = Properties.Settings.Default.searchMinDistance;
       
   678                 maxDistHands = Properties.Settings.Default.searchMaxDistance;
       
   679                 minDist = Properties.Settings.Default.minDistance;
       
   680                 maxDist = Properties.Settings.Default.maxDistance;
       
   681                 zeroPoint = Properties.Settings.Default.zeroPoint;
       
   682                 connexionHost = Properties.Settings.Default.connexionHost;
       
   683                 connexionPort = Properties.Settings.Default.connexionPort;
       
   684                 timerElapsing = Properties.Settings.Default.timerElapsing;
       
   685                 imagesToShow = Properties.Settings.Default.imagesToShow;
       
   686                 takenPoints = Properties.Settings.Default.takenPoints;
       
   687                 directionChangeTresholdXY = Properties.Settings.Default.directionChangeTresholdXY;
       
   688                 directionChangeTresholdZ = Properties.Settings.Default.directionChangeTresholdZ;
       
   689             }
       
   690             catch (Exception)
       
   691             {
       
   692                 return false;
       
   693             }
       
   694 
       
   695             if (maxDistHands <= 0f || minDistHands <= 0f || maxDistHands > maxDist || minDistHands > maxDist ||
       
   696                 minDistHands >= maxDistHands || zeroPoint < maxDistHands || minDistHands > minDist ||
       
   697                 zeroPoint >= maxDist || connexionPort < 0 || timerElapsing < 0 || imagesToShow < 1 ||
       
   698                 takenPoints <= 0 || directionChangeTresholdXY < 0 || directionChangeTresholdZ < 0)
       
   699             {
       
   700                 ExceptionLbl.Content = rm.GetString("loadParametersIncorrect");
       
   701                 return false;
       
   702             }
       
   703             return true;
       
   704         }
       
   705 
       
   706         /*
       
   707          * Met à jour les nouveaux paramètres dans la configuration.
       
   708          */
       
   709         public void updateParameters()
       
   710         {
       
   711             //userPositionDetector.setParams(minDist, maxDist, minDistHands, maxDistHands, zeroPoint);
       
   712             //segmenter.setParams(takenPoints, directionChangeTresholdXY, directionChangeTresholdZ);
       
   713 
       
   714             //On récupère la config.
       
   715             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
       
   716             //On met à jour.
       
   717             Properties.Settings.Default.Context.Remove("searchMinDistance");
       
   718             Properties.Settings.Default.Context.Add("searchMinDistance", minDistHands.ToString());
       
   719             Properties.Settings.Default.Context.Remove("searchMaxDistance");
       
   720             Properties.Settings.Default.Context.Add("searchMaxDistance", maxDistHands.ToString());
       
   721             Properties.Settings.Default.Context.Remove("minDistance");
       
   722             Properties.Settings.Default.Context.Add("minDistance", minDist.ToString());
       
   723             Properties.Settings.Default.Context.Remove("maxDistance");
       
   724             Properties.Settings.Default.Context.Add("maxDistance", maxDist.ToString());
       
   725             Properties.Settings.Default.Context.Remove("zeroPoint");
       
   726             Properties.Settings.Default.Context.Add("zeroPoint", zeroPoint.ToString());
       
   727             Properties.Settings.Default.Context.Remove("connexionHost");
       
   728             Properties.Settings.Default.Context.Add("connexionHost", connexionHost);
       
   729             Properties.Settings.Default.Context.Remove("connexionPort");
       
   730             Properties.Settings.Default.Context.Add("connexionPort", connexionPort.ToString());
       
   731             Properties.Settings.Default.Context.Remove("timerElapsing");
       
   732             Properties.Settings.Default.Context.Add("timerElapsing", timerElapsing.ToString());
       
   733             Properties.Settings.Default.Context.Remove("imagesToShow");
       
   734             Properties.Settings.Default.Context.Add("imagesToShow", imagesToShow.ToString());
       
   735             Properties.Settings.Default.Context.Remove("takenPoints");
       
   736             Properties.Settings.Default.Context.Add("takenPoints", takenPoints.ToString());
       
   737             Properties.Settings.Default.Context.Remove("directionChangeTresholdXY");
       
   738             Properties.Settings.Default.Context.Add("directionChangeTresholdXY", directionChangeTresholdXY.ToString());
       
   739             Properties.Settings.Default.Context.Remove("directionChangeTresholdZ");
       
   740             Properties.Settings.Default.Context.Add("directionChangeTresholdZ", directionChangeTresholdZ.ToString());
       
   741 
       
   742             //Sauvegarde la configuration.
       
   743             Properties.Settings.Default.Save();
       
   744             Properties.Settings.Default.Reload();
       
   745         }
       
   746 
       
   747         /*
       
   748          * Getters et setters des paramètres du Middleware.
       
   749          */
       
   750         public void setMinDistHands(float min)
       
   751         {
       
   752             minDistHands = min;
       
   753         }
       
   754         public void setMaxDistHands(float max)
       
   755         {
       
   756             maxDistHands = max;
       
   757         }
       
   758         public void setMinDist(float min)
       
   759         {
       
   760             minDist = min;
       
   761         }
       
   762         public void setMaxDist(float max)
       
   763         {
       
   764             maxDist = max;
       
   765         }
       
   766         public void setZeroPoint(float zero)
       
   767         {
       
   768             zeroPoint = zero;
       
   769         }
       
   770         public void setConnexionHost(String host)
       
   771         {
       
   772             connexionHost = host;
       
   773         }
       
   774         public void setConnexionPort(int port)
       
   775         {
       
   776             connexionPort = port;
       
   777         }
       
   778         public void setTimerElapsing(int time)
       
   779         {
       
   780             timerElapsing = time;
       
   781         }
       
   782         public void setImagesToShow(int _imagesToShow)
       
   783         {
       
   784             imagesToShow = _imagesToShow;
       
   785         }
       
   786         public void setTakenPoints(int _takenPoints)
       
   787         {
       
   788             takenPoints = _takenPoints;
       
   789         }
       
   790         public void setDirectionChangeTresholdXY(int _directionChangeTresholdXY)
       
   791         {
       
   792             directionChangeTresholdXY = _directionChangeTresholdXY;
       
   793         }
       
   794         public void setDirectionChangeTresholdZ(float _directionChangeTresholdZ)
       
   795         {
       
   796             directionChangeTresholdZ = _directionChangeTresholdZ;
       
   797         }
       
   798         public void setSwitch(Button _switch)
       
   799         {
       
   800             Switch = _switch;
       
   801         }
       
   802         public void setOn(bool _on)
       
   803         {
       
   804             on = _on;
       
   805         }
       
   806         public void setQuitMenu(MenuItem quitMenu)
       
   807         {
       
   808             QuitMenu = quitMenu;
       
   809         }
       
   810 
       
   811         public float getMinDistHands()
       
   812         {
       
   813             return minDistHands;
       
   814         }
       
   815         public float getMaxDistHands()
       
   816         {
       
   817             return maxDistHands;
       
   818         }
       
   819         public float getMinDist()
       
   820         {
       
   821             return minDist;
       
   822         }
       
   823         public float getMaxDist()
       
   824         {
       
   825             return maxDist;
       
   826         }
       
   827         public float getZeroPoint()
       
   828         {
       
   829             return zeroPoint;
       
   830         }
       
   831         public String getConnexionHost()
       
   832         {
       
   833             return connexionHost;
       
   834         }
       
   835         public int getConnexionPort()
       
   836         {
       
   837             return connexionPort;
       
   838         }
       
   839         public int getTimerElapsing()
       
   840         {
       
   841             return timerElapsing;
       
   842         }
       
   843         public int getImagesToShow()
       
   844         {
       
   845             return imagesToShow;
       
   846         }
       
   847         public int getTakenPoints()
       
   848         {
       
   849             return takenPoints;
       
   850         }
       
   851         public int getDirectionChangeTresholdXY()
       
   852         {
       
   853             return directionChangeTresholdXY;
       
   854         }
       
   855         public float getDirectionChangeTresholdZ()
       
   856         {
       
   857             return directionChangeTresholdZ;
       
   858         }
       
   859         public Button getSwitch()
       
   860         {
       
   861             return Switch;
       
   862         }
       
   863         public bool getOn()
       
   864         {
       
   865             return on;
       
   866         }
       
   867         public MenuItem getQuitMenu()
       
   868         {
       
   869             return QuitMenu;
       
   870         }
       
   871     }
       
   872 }