|
1 using System; |
|
2 using System.Collections.Generic; |
|
3 using System.Linq; |
|
4 using System.Text; |
|
5 using Microsoft.Kinect; |
|
6 |
|
7 namespace Trakers.Tracking.Gestures |
|
8 { |
|
9 public class SwipeDetector : GestureDetector |
|
10 { |
|
11 public enum Direction {LEFT, RIGHT, TOP, DOWN}; |
|
12 |
|
13 public SwipeDetector() : base() |
|
14 { |
|
15 gesturePeriod = (float)0.5; |
|
16 indexesPerSecond = 30; |
|
17 indexesToCheck = (int)(gesturePeriod * indexesPerSecond); |
|
18 } |
|
19 |
|
20 /* |
|
21 * Lit les noeuds de l'historique du squelette afin de détecter un Swipe left. |
|
22 * Règles : |
|
23 * Se fait avec la main droite. |
|
24 * Chaque nouvelle position de la main doit être à la gauche de la précédente. |
|
25 * Chaque nouvelle position de la main ne doit pas s'éloigner verticalement de plus d'une certaine distance. |
|
26 * Le geste doit mesurer horizontalement une certaine distance. |
|
27 */ |
|
28 public bool CheckForSwipeLeft() |
|
29 { |
|
30 //Crée un historique de squelette local, puisque l'historique est mis à jour toutes les ~1/30 s. |
|
31 //List<Skeleton> history = new List<Skeleton>(history); |
|
32 |
|
33 /*for (int i = 0; i < history.Count; i++) |
|
34 System.Console.Out.WriteLine(history[i].Joints.ElementAt(handRightID).Position.X); |
|
35 System.Console.Out.WriteLine("========================");*/ |
|
36 |
|
37 //Si il n'y a pas assez de positions dans l'historique local pour vérifier le geste. |
|
38 if (history.Count < indexesToCheck+1) |
|
39 return false; |
|
40 //Console.ReadLine(); |
|
41 |
|
42 //La distance de référence est ici la distance entre le milieu du dos et le milieu des épaules. |
|
43 refDistance = Math.Abs(history[0][spineID].Position.Y - history[0][shoulderCenterID].Position.Y); |
|
44 //On commence la position pour les indexesToCheck dernières postures (celle à l'index 0 étant la dernière). |
|
45 startPoint = history[history.Count - indexesToCheck][handRightID].Position; |
|
46 |
|
47 //De la position p1 à pn, on suit l'algorithme. |
|
48 Console.Out.WriteLine("================================="); |
|
49 for (int i = history.Count - indexesToCheck + 1; i < history.Count; i++) |
|
50 { |
|
51 System.Console.Out.WriteLine("hand:head " + history[i][handRightID].Position.Y + " <= " + history[i][headID].Position.Y + " && " |
|
52 + "hand:hip " + history[i][handRightID].Position.Y + " >= " + history[i][hipCenterID].Position.Y + " && " |
|
53 + "handX:handX-1 " + history[i][handRightID].Position.X + " <= " + history[i - 1][handRightID].Position.X + " && " |
|
54 + "hand0:handN " + Math.Abs(history[i][handRightID].Position.Y - startPoint.Y) + " <= " + refDistance / 2); |
|
55 |
|
56 //Si la position Y de la main est supérieure à celle de l'épaule |
|
57 //OU si la position Y de la main est inférieure à celle de la hanche |
|
58 //OU si la nouvelle position X de la main est à droite de la précédente |
|
59 //OU si la nouvelle position Y de la main est plus éloignée de la distance N par rapport à la première position Y |
|
60 //Alors on retourne faux. |
|
61 if (history[i][handRightID].Position.Y > history[i][headID].Position.Y || |
|
62 history[i][handRightID].Position.Y < history[i][hipCenterID].Position.Y || |
|
63 history[i][handRightID].Position.X > history[i - 1][handRightID].Position.X || |
|
64 Math.Abs(history[i][handRightID].Position.Y - startPoint.Y) > refDistance/2) |
|
65 return false; |
|
66 } |
|
67 Console.Out.WriteLine("================================="); |
|
68 Console.ReadLine(); |
|
69 //Si la distance horizontale du geste a été plus courte que la distance N |
|
70 //Alors on retourne faux. |
|
71 float dist = (history[history.Count - 1][handRightID].Position.X - history[history.Count - indexesToCheck][handRightID].Position.X); |
|
72 |
|
73 //System.Console.Out.WriteLine(history[0].Joints.ElementAt(handRightID).Position.X + " " + history[1].Joints.ElementAt(handRightID).Position.X + " " + history[2].Joints.ElementAt(handRightID).Position.X); |
|
74 //System.Console.Out.WriteLine(history[0].Joints.ElementAt(handRightID).Position.X + " " + history[1].Joints.ElementAt(handRightID).Position.X + " " + history[2].Joints.ElementAt(handRightID).Position.X); |
|
75 System.Console.Out.WriteLine(Math.Abs(history[0][handRightID].Position.X - history[history.Count - indexesToCheck][handRightID].Position.X) + " < " + refDistance*1.5); |
|
76 if (Math.Abs(history[0][handRightID].Position.X - history[history.Count - indexesToCheck][handRightID].Position.X) < refDistance*1.5) |
|
77 return false; |
|
78 |
|
79 history.Clear(); |
|
80 Console.Out.WriteLine("SWIPE LEF"); |
|
81 Console.ReadLine(); |
|
82 return true; |
|
83 } |
|
84 } |
|
85 } |