middleware/src/Debug/DebugWindow.xaml.cs
changeset 11 a1bf0d21022e
parent 8 e4e7db2435f8
child 13 50de8e8f44d7
equal deleted inserted replaced
10:925b7ee746e3 11:a1bf0d21022e
    42 using System.Threading;
    42 using System.Threading;
    43 using Trakers.Tracking.Events;
    43 using Trakers.Tracking.Events;
    44 using Trakers.Tracking.Gestures;
    44 using Trakers.Tracking.Gestures;
    45 using System.Resources;
    45 using System.Resources;
    46 using System.Reflection;
    46 using System.Reflection;
       
    47 using System.Timers;
       
    48 using Trakers.Communication;
    47 
    49 
    48 namespace Trakers.Debug
    50 namespace Trakers.Debug
    49 {
    51 {
    50     public partial class DebugWindow : Window
    52     public partial class DebugWindow : Window
    51     {
    53     {
    52         //Gestionnaire de ressources.
    54         //Gestionnaire de ressources.
    53         private ResourceManager rm;
    55         private ResourceManager rm;
       
    56         //Timer.
       
    57         private System.Timers.Timer _timer;
    54         //Membre permettant d'atteindre la classe KinectMain du sous-module Tracking.
    58         //Membre permettant d'atteindre la classe KinectMain du sous-module Tracking.
    55         private KinectMain kinectMain;
    59         private KinectMain kinectMain;
    56         //Tableau contenant une image en couleurs.
    60         //Tableau contenant une image en couleurs.
    57         private byte[] colorPixelData;
    61         private byte[] colorPixelData;
    58         //Indique si la kinect est allumée/éteinte.
    62         //Indique si la kinect est allumée/éteinte.
    59         private bool on;
    63         private bool on;
    60         //Indique si la fenêtre de debug est actuellement en cours de fermeture.
    64         //Indique si la fenêtre de debug est actuellement en cours de fermeture.
    61         private bool closing;
    65         private bool closing;
       
    66         //Indique si l'image doit être raffraichie.
       
    67         private Boolean refreshImage;
    62 
    68 
    63         /*
    69         /*
    64         * Constructeur : Affiche la fenêtre de debug en lui passant en paramètre une instanciation de la
    70         * Constructeur : Affiche la fenêtre de debug en lui passant en paramètre une instanciation de la
    65         * classe KinectMain.
    71         * classe KinectMain.
    66         * Au départ, la kinect est éteinte.
    72         * Au départ, la kinect est éteinte.
    70             rm = new ResourceManager("Trakers.Properties.resources", Assembly.GetExecutingAssembly());
    76             rm = new ResourceManager("Trakers.Properties.resources", Assembly.GetExecutingAssembly());
    71             InitializeComponent();
    77             InitializeComponent();
    72             kinectMain = main;
    78             kinectMain = main;
    73             on = true;
    79             on = true;
    74             closing = false;
    80             closing = false;
       
    81             refreshImage = true;
       
    82 
       
    83             try
       
    84             {
       
    85                 //On instancie le timer à N ms.
       
    86                 _timer = new System.Timers.Timer(kinectMain.getTimerElapsing());
       
    87                 //Dès que le timer est expiré, on appelle _timer_Elapsed.
       
    88                 _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
       
    89             }
       
    90             catch (Exception){}
       
    91         }
       
    92 
       
    93         /*
       
    94          * Méthode appelée à l'expiration du timer pour les gestures et modes.
       
    95          */
       
    96         public void _timer_Elapsed(object sender, ElapsedEventArgs e)
       
    97         {
       
    98             //On débloque le raffraichissement de l'image.
       
    99             refreshImage = true;
       
   100             //On arrête le timer.
       
   101             _timer.Stop();
    75         }
   102         }
    76 
   103 
    77         /*
   104         /*
    78         * Getter pour le membre indiquant la fermeture de la fenêtre de debug.
   105         * Getter pour le membre indiquant la fermeture de la fenêtre de debug.
    79         */
   106         */
   132         /*
   159         /*
   133         *  Récupère le flux video et met à jour le rendu visuel de debug.
   160         *  Récupère le flux video et met à jour le rendu visuel de debug.
   134         */
   161         */
   135         public void RefreshVideo(AllFramesReadyEventArgs e)
   162         public void RefreshVideo(AllFramesReadyEventArgs e)
   136         {
   163         {
   137             bool receivedData = false;
   164             if (refreshImage)
   138             ColorImageFrame colorImageFrameData;
   165             {
   139             using (colorImageFrameData = e.OpenColorImageFrame())
   166                 bool receivedData = false;
   140             {
   167                 ColorImageFrame colorImageFrameData;
   141                 //Si on ne reçoit pas de trames de la kinect.
   168                 using (colorImageFrameData = e.OpenColorImageFrame())
   142                 if (colorImageFrameData == null)
       
   143                 {
   169                 {
   144                     //L'image est supprimée.
   170                     //Si on ne reçoit pas de trames de la kinect.
   145                     DebugImage.Source = null;
   171                     if (colorImageFrameData == null)
       
   172                     {
       
   173                         //L'image est supprimée.
       
   174                         DebugImage.Source = null;
       
   175                     }
       
   176                     //Si le tableau stockant l'image en cours est nul.
       
   177                     if (colorPixelData == null)
       
   178                         //On alloue un nouveau tableau.
       
   179                         colorPixelData = new byte[colorImageFrameData.PixelDataLength];
       
   180                     else
       
   181                     {
       
   182                         try
       
   183                         {
       
   184                             //Sinon on met à jour le tableau en copiant le contenu de la trame dans le tableau.
       
   185                             colorImageFrameData.CopyPixelDataTo(colorPixelData);
       
   186                             receivedData = true;
       
   187                         }
       
   188                         catch (Exception) { }
       
   189                     }
   146                 }
   190                 }
   147                 //Si le tableau stockant l'image en cours est nul.
   191                 //Si on a des données dans le tableau et que la kinect est allumée.
   148                 if (colorPixelData == null)
   192                 if (receivedData && on)
   149                     //On alloue un nouveau tableau.
       
   150                     colorPixelData = new byte[colorImageFrameData.PixelDataLength];
       
   151                 else
       
   152                 {
   193                 {
   153                     try
   194                     //On met à jour l'image de la caméra.
   154                     {
   195                     DebugImage.Source = BitmapSource.Create(colorImageFrameData.Width, colorImageFrameData.Height, 96, 96, PixelFormats.Bgr32, null, colorPixelData, colorImageFrameData.Width * colorImageFrameData.BytesPerPixel);
   155                         //Sinon on met à jour le tableau en copiant le contenu de la trame dans le tableau.
   196                     DebugImage.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
   156                         colorImageFrameData.CopyPixelDataTo(colorPixelData);
   197                     DebugImage.VerticalAlignment = System.Windows.VerticalAlignment.Center;
   157                         receivedData = true;
   198                     DebugImage.Stretch = Stretch.Fill;
   158                     }
   199                     //On annule l'image associée aux gestures.
   159                     catch (Exception){}
   200                     Gestures.Source = null;
   160                 }
   201                 }
   161             }
       
   162             //Si on a des données dans le tableau et que la kinect est allumée.
       
   163             if (receivedData && on)
       
   164             {
       
   165                 //Exemples de modifications de l'affichage :
       
   166                 //Zombies apocalypse.
       
   167                 //Black & White movie.
       
   168                 /*for (int i = 0; i < colorPixelData.Length; i += colorImageFrameData.BytesPerPixel)
       
   169                 {
       
   170                     byte gray = Math.Min(colorPixelData[i], colorPixelData[i + 1]);
       
   171                     gray = Math.Min(gray, colorPixelData[i + 2]);
       
   172                     colorPixelData[i] = gray;
       
   173                     colorPixelData[i + 1] = gray;
       
   174 
       
   175                     colorPixelData[i] = colorPixelData[i + 1];
       
   176                     colorPixelData[i + 1] = colorPixelData[i];
       
   177                     colorPixelData[i + 2] = (byte)~colorPixelData[i + 2];
       
   178                 }*/
       
   179 
       
   180                 //On met à jour l'image de la caméra.
       
   181                 DebugImage.Source = BitmapSource.Create(colorImageFrameData.Width, colorImageFrameData.Height, 96, 96, PixelFormats.Bgr32, null, colorPixelData, colorImageFrameData.Width * colorImageFrameData.BytesPerPixel);
       
   182                 DebugImage.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
       
   183                 DebugImage.VerticalAlignment = System.Windows.VerticalAlignment.Center;
       
   184                 DebugImage.Stretch = Stretch.Fill;
       
   185             }
   202             }
   186         }
   203         }
   187 
   204 
   188         /*
   205         /*
   189         * Affiche la distance de l'utilisateur dans le rendu visuel.
   206         * Affiche la distance de l'utilisateur dans le rendu visuel.
   190         * Sous forme de nombre en m et de rectangles changeant de couleur en fonction de la distance.
   207         * Sous forme de nombre en m et de rectangles changeant de couleur en fonction de la distance.
   191         */
   208         */
   192         public void showDistance(float distance)
   209         public void showDistance(float proximity)
   193         {
   210         {
   194             DistanceLbl.Content = "Distance : " + distance;
   211             DistanceLbl.Content = "Proximity : " + proximity + "%";
   195 
   212 
   196             if (distance > 0 && distance < 1)
   213             if (proximity == 0)
   197             {
   214             {
   198                 R1.Fill = System.Windows.Media.Brushes.Red;
   215                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
   199                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
   216                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
   200                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
   217                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
   201                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
   218                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
   202             }
   219                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
   203             else if (distance > 1 && distance < 2)
   220                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
   204             {
   221                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
   205                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
   222                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
   206                 R2.Fill = System.Windows.Media.Brushes.Orange;
   223                 R8.Fill = System.Windows.Media.Brushes.DarkGray;
   207                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
   224                 R9.Fill = System.Windows.Media.Brushes.DarkGray;
   208                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
   225                 R10.Fill = System.Windows.Media.Brushes.DarkGray;
   209             }
   226             }
   210             else if (distance > 2 && distance < 3)
   227             else if (proximity > 0 && proximity < 10)
   211             {
   228             {
       
   229                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   230                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   231                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   232                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   233                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   234                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   235                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   236                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   237                 R8.Fill = System.Windows.Media.Brushes.DarkGray;
       
   238                 R9.Fill = System.Windows.Media.Brushes.DarkGray;
       
   239                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   240             }
       
   241             else if (proximity > 10 && proximity < 20)
       
   242             {
       
   243                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   244                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   245                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   246                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   247                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   248                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   249                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   250                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   251                 R8.Fill = System.Windows.Media.Brushes.DarkGray;
       
   252                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   253                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   254             }
       
   255             else if (proximity > 20 && proximity < 30)
       
   256             {
       
   257                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   258                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   259                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   260                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   261                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   262                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   263                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   264                 R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   265                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   266                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   267                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   268             }
       
   269             else if (proximity > 30 && proximity < 40)
       
   270             {
       
   271                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   272                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   273                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   274                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   275                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   276                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   277                 R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   278                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   279                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   280                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   281                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   282             }
       
   283             else if (proximity > 40 && proximity < 50)
       
   284             {
       
   285                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   286                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   287                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   288                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   289                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   290                 R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   291                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   292                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   293                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   294                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   295                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   296             }
       
   297             else if (proximity > 50 && proximity < 60)
       
   298             {
       
   299                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   300                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   301                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   302                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   303                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   304                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   305                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   306                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   307                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   308                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   309                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   310             }
       
   311             else if (proximity > 60 && proximity < 70)
       
   312             {
       
   313                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   314                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   315                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   316                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   317                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   318                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   319                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   320                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   321                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   322                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   323                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   324             }
       
   325             else if (proximity > 70 && proximity < 80)
       
   326             {
       
   327                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
   212                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
   328                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
   213                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
   329                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
   214                 R3.Fill = System.Windows.Media.Brushes.Yellow;
   330                 R3.Fill = System.Windows.Media.Brushes.Yellow;
   215                 R4.Fill = System.Windows.Media.Brushes.DarkGray;
   331                 R4.Fill = System.Windows.Media.Brushes.Yellow;
   216             }
   332                 R5.Fill = System.Windows.Media.Brushes.Orange;
   217             else if (distance > 3 && distance < 4)
   333                 R6.Fill = System.Windows.Media.Brushes.Orange;
   218             {
   334                 R7.Fill = System.Windows.Media.Brushes.Orange;
   219                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
   335                 R8.Fill = System.Windows.Media.Brushes.Red;
   220                 R2.Fill = System.Windows.Media.Brushes.DarkGray;
   336                 R9.Fill = System.Windows.Media.Brushes.Red;
   221                 R3.Fill = System.Windows.Media.Brushes.DarkGray;
   337                 R10.Fill = System.Windows.Media.Brushes.Red;
   222                 R4.Fill = System.Windows.Media.Brushes.White;
   338             }
   223             }
   339             else if (proximity > 80 && proximity < 90)
   224         }
   340             {
   225 
   341                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
   226         public void showProximity(UserPositionEventArgs e)
   342                 R1.Fill = System.Windows.Media.Brushes.DarkGray;
   227         {
   343                 R2.Fill = System.Windows.Media.Brushes.Yellow;
   228 
   344                 R3.Fill = System.Windows.Media.Brushes.Yellow;
       
   345                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   346                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   347                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   348                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   349                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   350                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   351                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   352             }
       
   353             else if (proximity > 90 && proximity < 100)
       
   354             {
       
   355                 R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   356                 R1.Fill = System.Windows.Media.Brushes.Yellow;
       
   357                 R2.Fill = System.Windows.Media.Brushes.Yellow;
       
   358                 R3.Fill = System.Windows.Media.Brushes.Yellow;
       
   359                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   360                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   361                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   362                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   363                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   364                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   365                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   366             }
       
   367             else if (proximity == 100)
       
   368             {
       
   369                 R0.Fill = System.Windows.Media.Brushes.Green;
       
   370                 R1.Fill = System.Windows.Media.Brushes.Yellow;
       
   371                 R2.Fill = System.Windows.Media.Brushes.Yellow;
       
   372                 R3.Fill = System.Windows.Media.Brushes.Yellow;
       
   373                 R4.Fill = System.Windows.Media.Brushes.Yellow;
       
   374                 R5.Fill = System.Windows.Media.Brushes.Orange;
       
   375                 R6.Fill = System.Windows.Media.Brushes.Orange;
       
   376                 R7.Fill = System.Windows.Media.Brushes.Orange;
       
   377                 R8.Fill = System.Windows.Media.Brushes.Red;
       
   378                 R9.Fill = System.Windows.Media.Brushes.Red;
       
   379                 R10.Fill = System.Windows.Media.Brushes.Red;
       
   380             }
   229         }
   381         }
   230 
   382 
   231         /*
   383         /*
   232         * Affiche la détection de la main droite via un label.
   384         * Affiche la détection de la main droite via un label.
   233         */
   385         */
   253         /*
   405         /*
   254         * Dessine les noeuds du squelette dans le rendu visuel.
   406         * Dessine les noeuds du squelette dans le rendu visuel.
   255         */
   407         */
   256         public void drawJoints(JointCollection joints, Skeleton first)
   408         public void drawJoints(JointCollection joints, Skeleton first)
   257         {
   409         {
   258             //On enlève tout élément du Canvas à part l'image, de manière à mettre à jour la position du squelette. 
   410             if (refreshImage)
   259             DebugCanvas.Children.RemoveRange(1, DebugCanvas.Children.Count - 1);
   411             {
   260 
   412                 //On enlève tout élément du Canvas à part l'image, de manière à mettre à jour la position du squelette. 
   261             //Pour chaque noeud.
   413                 DebugCanvas.Children.RemoveRange(1, DebugCanvas.Children.Count - 1);
   262             foreach (Joint joint in first.Joints)
   414 
   263             {
   415                 //Pour chaque noeud.
   264                 //On crée une ellipse de taille 20 et de largeur 20.
   416                 foreach (Joint joint in first.Joints)
   265                 Ellipse node = new Ellipse();
   417                 {
   266                 node.Height = 20;
   418                     //On crée une ellipse de taille 20 et de largeur 20.
   267                 node.Width = 20;
   419                     Ellipse node = new Ellipse();
   268 
   420                     node.Height = 20;
   269                 //S'il s'agit d'un noeud de tête, on le colorie en rouge, sinon en bleu.
   421                     node.Width = 20;
   270                 if (joint.JointType == JointType.Head)
   422 
   271                     node.Fill = System.Windows.Media.Brushes.Red;
   423                     //S'il s'agit d'un noeud de tête, on le colorie en rouge, sinon en bleu.
   272                 else if(joint.JointType == JointType.ShoulderCenter)
   424                     if (joint.JointType == JointType.Head)
   273                     node.Fill = System.Windows.Media.Brushes.Green;
   425                         node.Fill = System.Windows.Media.Brushes.Red;
   274                 else if(joint.JointType == JointType.HipCenter)
   426                     else if (joint.JointType == JointType.ShoulderCenter)
   275                     node.Fill = System.Windows.Media.Brushes.Green;
   427                         node.Fill = System.Windows.Media.Brushes.Green;
   276                 else if (joint.JointType == JointType.HandRight)
   428                     else if (joint.JointType == JointType.HipCenter)
   277                     node.Fill = System.Windows.Media.Brushes.Red;
   429                         node.Fill = System.Windows.Media.Brushes.Green;
   278                 else
   430                     else if (joint.JointType == JointType.HandRight)
   279                     node.Fill = System.Windows.Media.Brushes.Blue;
   431                         node.Fill = System.Windows.Media.Brushes.Red;
   280 
   432                     else
   281                 //On met à la bonne échelle les coordonnées des positions des noeuds.
   433                         node.Fill = System.Windows.Media.Brushes.Blue;
   282                 Joint scaledJoint = Coding4Fun.Kinect.Wpf.SkeletalExtensions.ScaleTo(joint, 600, 400, 0.75f, 0.75f);
   434 
   283 
   435                     //On met à la bonne échelle les coordonnées des positions des noeuds.
   284                 //On positionne le noeud dans le Canvas, et on l'ajoute.
   436                     Joint scaledJoint = Coding4Fun.Kinect.Wpf.SkeletalExtensions.ScaleTo(joint, 600, 400, 0.75f, 0.75f);
   285                 Canvas.SetLeft(node, scaledJoint.Position.X);
   437 
   286                 Canvas.SetTop(node, scaledJoint.Position.Y);
   438                     //On positionne le noeud dans le Canvas, et on l'ajoute.
   287                 DebugCanvas.Children.Add(node);
   439                     Canvas.SetLeft(node, scaledJoint.Position.X);
       
   440                     Canvas.SetTop(node, scaledJoint.Position.Y);
       
   441                     DebugCanvas.Children.Add(node);
       
   442                 }
   288             }
   443             }
   289         }
   444         }
   290 
   445 
   291         /*
   446         /*
   292         * Dessine un os, en ayant en paramètres deux noeuds.
   447         * Dessine un os, en ayant en paramètres deux noeuds.
   300             line.X2 = j2.Position.X;
   455             line.X2 = j2.Position.X;
   301             line.Y1 = j1.Position.Y;
   456             line.Y1 = j1.Position.Y;
   302             line.Y2 = j2.Position.Y;
   457             line.Y2 = j2.Position.Y;
   303             line.StrokeThickness = 8;
   458             line.StrokeThickness = 8;
   304             DebugCanvas.Children.Add(line);
   459             DebugCanvas.Children.Add(line);
   305             ExceptionLbl.Content = DebugCanvas.Children.Count;
       
   306         }
   460         }
   307 
   461 
   308         /*
   462         /*
   309         * Dessine le squelette (ensemble des os), en ayant en paramètres tous les noeuds.
   463         * Dessine le squelette (ensemble des os), en ayant en paramètres tous les noeuds.
   310         */
   464         */
   311         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)
   465         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)
   312         {
   466         {
   313             //On met les noeuds deux par deux en fonction de leur position dans le squelette.
   467             if (refreshImage)
   314             drawBone(head, shoulderCenter);
   468             {
   315             drawBone(shoulderCenter, shoulderLeft);
   469                 //On met les noeuds deux par deux en fonction de leur position dans le squelette.
   316             drawBone(shoulderLeft, elbowLeft);
   470                 drawBone(head, shoulderCenter);
   317             drawBone(elbowLeft, wristLeft);
   471                 drawBone(shoulderCenter, shoulderLeft);
   318             drawBone(wristLeft, handLeft);
   472                 drawBone(shoulderLeft, elbowLeft);
   319             drawBone(shoulderCenter, shoulderRight);
   473                 drawBone(elbowLeft, wristLeft);
   320             drawBone(shoulderRight, elbowRight);
   474                 drawBone(wristLeft, handLeft);
   321             drawBone(elbowRight, wristRight);
   475                 drawBone(shoulderCenter, shoulderRight);
   322             drawBone(wristRight, handRight);
   476                 drawBone(shoulderRight, elbowRight);
   323             drawBone(shoulderCenter, spine);
   477                 drawBone(elbowRight, wristRight);
   324             drawBone(spine, hipCenter);
   478                 drawBone(wristRight, handRight);
   325             drawBone(hipCenter, hipLeft);
   479                 drawBone(shoulderCenter, spine);
   326             drawBone(hipLeft, kneeLeft);
   480                 drawBone(spine, hipCenter);
   327             drawBone(kneeLeft, ankleLeft);
   481                 drawBone(hipCenter, hipLeft);
   328             drawBone(ankleLeft, footLeft);
   482                 drawBone(hipLeft, kneeLeft);
   329             drawBone(hipCenter, hipRight);
   483                 drawBone(kneeLeft, ankleLeft);
   330             drawBone(hipRight, kneeRight);
   484                 drawBone(ankleLeft, footLeft);
   331             drawBone(kneeRight, ankleRight);
   485                 drawBone(hipCenter, hipRight);
   332             drawBone(ankleRight, footRight);
   486                 drawBone(hipRight, kneeRight);
   333         }
   487                 drawBone(kneeRight, ankleRight);
   334 
   488                 drawBone(ankleRight, footRight);
       
   489             }
       
   490         }
       
   491 
       
   492         /*
       
   493         * Cache le squelette et le reste de l'interface à part l'image.
       
   494         */
   335         public void hideSkeleton()
   495         public void hideSkeleton()
   336         {
   496         {
       
   497             //On vide le canvas mais en gardant l'image.
   337             if(DebugCanvas.Children.Count > 1)
   498             if(DebugCanvas.Children.Count > 1)
   338                 DebugCanvas.Children.RemoveRange(1, DebugCanvas.Children.Count - 1);
   499                 DebugCanvas.Children.RemoveRange(1, DebugCanvas.Children.Count - 1);
       
   500             //On colore en gris tous les indicateurs.
       
   501             R0.Fill = System.Windows.Media.Brushes.DarkGray;
       
   502             R1.Fill = System.Windows.Media.Brushes.DarkGray;
       
   503             R2.Fill = System.Windows.Media.Brushes.DarkGray;
       
   504             R3.Fill = System.Windows.Media.Brushes.DarkGray;
       
   505             R4.Fill = System.Windows.Media.Brushes.DarkGray;
       
   506             R5.Fill = System.Windows.Media.Brushes.DarkGray;
       
   507             R6.Fill = System.Windows.Media.Brushes.DarkGray;
       
   508             R7.Fill = System.Windows.Media.Brushes.DarkGray;
       
   509             R8.Fill = System.Windows.Media.Brushes.DarkGray;
       
   510             R9.Fill = System.Windows.Media.Brushes.DarkGray;
       
   511             R10.Fill = System.Windows.Media.Brushes.DarkGray;
       
   512             LeftHand.Background = System.Windows.Media.Brushes.DarkGray;
       
   513             RightHand.Background = System.Windows.Media.Brushes.DarkGray;
   339         }
   514         }
   340 
   515 
   341         /*
   516         /*
   342         * Affiche la position de la main gauche dans le rendu visuel.
   517         * Affiche la position de la main gauche dans le rendu visuel.
   343         */
   518         */
   350         * Affiche la position de la main gauche dans le rendu visuel.
   525         * Affiche la position de la main gauche dans le rendu visuel.
   351         */
   526         */
   352         public void showRightHandCoord(String coord)
   527         public void showRightHandCoord(String coord)
   353         {
   528         {
   354             RightHand.Content = coord;
   529             RightHand.Content = coord;
   355         }
       
   356 
       
   357         public void showSwipe(SwipeEventArgs e)
       
   358         {
       
   359             if(e.direction == Tracking.Gestures.SwipeDetector.Direction.LEFT)
       
   360                 ExceptionLbl.Background = System.Windows.Media.Brushes.Red;
       
   361             else if(e.direction == Tracking.Gestures.SwipeDetector.Direction.RIGHT)
       
   362                 ExceptionLbl.Background = System.Windows.Media.Brushes.Purple;
       
   363         }
       
   364 
       
   365         public void showPush(PushEventArgs e)
       
   366         {
       
   367             if (e.direction == Tracking.Gestures.PushDetector.Direction.PUSH)
       
   368             {
       
   369                 if(e.hand == Tracking.Gestures.PushDetector.Hand.LEFT)
       
   370                     LeftHand.Background = System.Windows.Media.Brushes.White;
       
   371                 else if(e.hand == Tracking.Gestures.PushDetector.Hand.RIGHT)
       
   372                     RightHand.Background = System.Windows.Media.Brushes.White;
       
   373                 else
       
   374                 {
       
   375                     LeftHand.Background = System.Windows.Media.Brushes.White;
       
   376                     RightHand.Background = System.Windows.Media.Brushes.White;
       
   377                 }
       
   378             }
       
   379             else
       
   380             {
       
   381                 if (e.hand == Tracking.Gestures.PushDetector.Hand.LEFT)
       
   382                     LeftHand.Background = System.Windows.Media.Brushes.Black;
       
   383                 else if (e.hand == Tracking.Gestures.PushDetector.Hand.RIGHT)
       
   384                     RightHand.Background = System.Windows.Media.Brushes.Black;
       
   385                 else
       
   386                 {
       
   387                     LeftHand.Background = System.Windows.Media.Brushes.Black;
       
   388                     RightHand.Background = System.Windows.Media.Brushes.Black;
       
   389                 }
       
   390             }
       
   391         }
   530         }
   392 
   531 
   393         /*
   532         /*
   394          * Méthode associée à l'événement : Ouvrir la fenêtre de paramétrage via le menu.
   533          * Méthode associée à l'événement : Ouvrir la fenêtre de paramétrage via le menu.
   395          */
   534          */
   416             //On éteint la Kinect (pour éviter qu'elle reste allumée même lorsque le programme est éteint).
   555             //On éteint la Kinect (pour éviter qu'elle reste allumée même lorsque le programme est éteint).
   417             kinectMain.KinectClose();
   556             kinectMain.KinectClose();
   418             Application.Current.Shutdown();
   557             Application.Current.Shutdown();
   419         }
   558         }
   420 
   559 
       
   560         /*
       
   561          * Permet d'initialiser la Kinect dès que la fenêtre est lancée.
       
   562          */
   421         private void Window_Loaded(object sender, RoutedEventArgs e)
   563         private void Window_Loaded(object sender, RoutedEventArgs e)
   422         {
   564         {
   423             kinectMain.KinectInitialization();
   565             kinectMain.KinectInitialization();
       
   566         }
       
   567 
       
   568         /*
       
   569          * Méthode d'affichage des gestures.
       
   570          */
       
   571         public void showGesture(String gesture)
       
   572         {
       
   573             if (refreshImage)
       
   574             {
       
   575                 refreshImage = false;
       
   576                 _timer.Start();
       
   577                 Bitmap bitmap = null;
       
   578                 //S'il s'agit de telle ou telle gesture, on prend l'image correspondante dans les ressources,
       
   579                 //on la convertit et on l'affiche.
       
   580                 switch (gesture)
       
   581                 {
       
   582                     case "SWIPE-LEFT": bitmap = (Bitmap)rm.GetObject("swipe_left");
       
   583                         break;
       
   584                     case "SWIPE-RIGHT": bitmap = (Bitmap)rm.GetObject("swipe_right");
       
   585                         break;
       
   586                     case "PUSH-RIGHT": bitmap = (Bitmap)rm.GetObject("push_right");
       
   587                         break;
       
   588                     case "PUSH-LEFT": bitmap = (Bitmap)rm.GetObject("push_left");
       
   589                         break;
       
   590                     case "PUSH-BOTH": bitmap = (Bitmap)rm.GetObject("push_both");
       
   591                         break;
       
   592                     case "PULL-RIGHT": bitmap = (Bitmap)rm.GetObject("pull_right");
       
   593                         break;
       
   594                     case "PULL-LEFT": bitmap = (Bitmap)rm.GetObject("pull_left");
       
   595                         break;
       
   596                     case "PULL-BOTH": bitmap = (Bitmap)rm.GetObject("pull_both");
       
   597                         break;
       
   598                 }
       
   599                 Gestures.Source = CreateBitmapSourceFromBitmap(bitmap);
       
   600             }
       
   601 
       
   602             DebugImage.Source = null;
       
   603             hideSkeleton();
       
   604         }
       
   605 
       
   606         /*
       
   607          * Méthode d'indication de raffraichissement de l'image ("on la raffraichit ou pas ?").
       
   608          */
       
   609         public void setRefreshImage(bool refresh)
       
   610         {
       
   611             refreshImage = refresh;
       
   612         }
       
   613 
       
   614         /*
       
   615          * Méthode de conversion de Bitmap (des ressources) en BitmapSource (du debug).
       
   616          */
       
   617         public static BitmapSource CreateBitmapSourceFromBitmap(Bitmap bitmap)
       
   618         {
       
   619             if (bitmap == null)
       
   620                 return null;
       
   621 
       
   622             return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       
   623                 bitmap.GetHbitmap(),
       
   624                 IntPtr.Zero,
       
   625                 Int32Rect.Empty,
       
   626                 BitmapSizeOptions.FromEmptyOptions());
   424         }
   627         }
   425     }
   628     }
   426 }
   629 }