|
0
|
1 |
/* |
|
|
2 |
* Projet : KINECT PROJECTS |
|
|
3 |
* Module : MIDDLEWARE |
|
|
4 |
* Sous-Module : Communication |
|
|
5 |
* Classe : Server |
|
|
6 |
* |
|
|
7 |
* Auteur : alexandre.bastien@iri.centrepompidou.fr |
|
|
8 |
* |
|
|
9 |
* Fonctionnalités : Reçoit des notifications du module sous-module Tracking. |
|
|
10 |
* Traduit les notifications sous forme de messages OSC et les envoie au Front Atelier. |
|
|
11 |
* Forme des messages : |
|
|
12 |
* - Notification de main dans le champ de recherche : Point3D indiquant la position de la main dans l'espace. |
|
|
13 |
*/ |
|
|
14 |
|
|
|
15 |
using System; |
|
|
16 |
using System.Collections.Generic; |
|
|
17 |
using System.Linq; |
|
|
18 |
using System.Text; |
|
|
19 |
using System.IO; |
|
|
20 |
using System.Net; |
|
|
21 |
using System.Net.Sockets; |
|
|
22 |
using System.Threading; |
|
|
23 |
using Tuio; |
|
|
24 |
using System.Windows; |
|
|
25 |
using Microsoft.Kinect; |
|
|
26 |
using Trakers.Tracking; |
|
|
27 |
using System.Windows.Media.Media3D; |
|
|
28 |
using Trakers.Tracking.Events; |
|
|
29 |
|
|
|
30 |
namespace Trakers.Communication |
|
|
31 |
{ |
|
|
32 |
public class Server |
|
|
33 |
{ |
|
|
34 |
//Serveur TUIO, provenant de la DLL TuioServer créé par Bespoke. |
|
|
35 |
private TuioServer server; |
|
|
36 |
|
|
|
37 |
//Permet de savoir si un curseur pour la main gauche/droite a été créé. |
|
|
38 |
private bool leftHandCursorCreated; |
|
|
39 |
private bool rightHandCursorCreated; |
|
|
40 |
|
|
|
41 |
/* |
|
|
42 |
* Constructeur : On initialise le serveur avec une adresse et un port, au début les curseurs |
|
|
43 |
* ne sont pas créés et on indique au ThreadPool une fonction de callback de manière à vérifier |
|
|
44 |
* s'il reçoit des notifications. |
|
|
45 |
*/ |
|
|
46 |
public Server(String host, int port) |
|
|
47 |
{ |
|
|
48 |
server = new TuioServer(host, port); |
|
|
49 |
leftHandCursorCreated = false; |
|
|
50 |
rightHandCursorCreated = false; |
|
|
51 |
ThreadPool.QueueUserWorkItem(ThreadPoolCallback); |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
/* |
|
|
55 |
* Getter du serveur. |
|
|
56 |
*/ |
|
|
57 |
public TuioServer getServer() |
|
|
58 |
{ |
|
|
59 |
return server; |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/* |
|
|
63 |
* Méthode appelée lors d'une notification de type : main gauche entrée dans le champ. |
|
|
64 |
*/ |
|
|
65 |
public void LeftHandTracked(object sender, LeftHandTrackedEventArgs e) |
|
|
66 |
{ |
|
|
67 |
//Si le curseur de la main gauche n'est pas créé, alors on le crée. |
|
|
68 |
if (!leftHandCursorCreated) |
|
|
69 |
{ |
|
|
70 |
server.AddTuioCursor(0, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
71 |
leftHandCursorCreated = true; |
|
|
72 |
} |
|
|
73 |
//S'il existe, on le met simplement à jour. |
|
|
74 |
else |
|
|
75 |
{ |
|
|
76 |
server.UpdateTuioCursor(0, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
77 |
} |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
/* |
|
|
81 |
* Méthode appelée lors d'une notification de type : main droite entrée dans le champ. |
|
|
82 |
*/ |
|
|
83 |
public void RightHandTracked(object sender, RightHandTrackedEventArgs e) |
|
|
84 |
{ |
|
|
85 |
//Si le curseur de la main droite n'est pas créé, alors on le crée. |
|
|
86 |
if (!rightHandCursorCreated) |
|
|
87 |
{ |
|
|
88 |
server.AddTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
89 |
rightHandCursorCreated = true; |
|
|
90 |
} |
|
|
91 |
//S'il existe, on le met simplement à jour. |
|
|
92 |
else |
|
|
93 |
{ |
|
|
94 |
server.UpdateTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
95 |
} |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
/* |
|
|
99 |
* Méthode appelée lors d'une notification de type : main gauche sortie du champ. |
|
|
100 |
*/ |
|
|
101 |
public void LeftHandQuit(object sender, LeftHandQuitEventArgs e) |
|
|
102 |
{ |
|
|
103 |
//Si le curseur de la main gauche existe, alors on le supprime. |
|
|
104 |
if (leftHandCursorCreated) |
|
|
105 |
{ |
|
|
106 |
server.DeleteTuioCursor(0); |
|
|
107 |
leftHandCursorCreated = false; |
|
|
108 |
} |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
/* |
|
|
112 |
* Méthode appelée lors d'une notification de type : main droite sortie du champ. |
|
|
113 |
*/ |
|
|
114 |
public void RightHandQuit(object sender, RightHandQuitEventArgs e) |
|
|
115 |
{ |
|
|
116 |
//Si le curseur de la main droite existe, alors on le supprime. |
|
|
117 |
if (rightHandCursorCreated) |
|
|
118 |
{ |
|
|
119 |
server.DeleteTuioCursor(1); |
|
|
120 |
rightHandCursorCreated = false; |
|
|
121 |
} |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
/* |
|
|
125 |
* Permet de convertir un point de position de noeud en Point3D. |
|
|
126 |
*/ |
|
|
127 |
private Point3D SkeletonPointToPoint3D(SkeletonPoint p) |
|
|
128 |
{ |
|
|
129 |
return new Point3D((double)p.X, (double)p.Y, (double)p.Z); |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
/* |
|
|
133 |
* Méthode de callback vérifiant toutes les 25 ms les nouvelles notifications. |
|
|
134 |
* Il est à noter que si le temps de rafraîchissement des trop rapide, les messages n'ont pas |
|
|
135 |
* le temps d'être envoyés. |
|
|
136 |
*/ |
|
|
137 |
private void ThreadPoolCallback(Object threadContext) |
|
|
138 |
{ |
|
|
139 |
while (true) |
|
|
140 |
{ |
|
|
141 |
//On initialise le message OSC. |
|
|
142 |
server.InitFrame(); |
|
|
143 |
//On l'envoie au client (au host et au port spécifiés dans le constructeur). |
|
|
144 |
server.CommitFrame(); |
|
|
145 |
//On attend 25 ms. |
|
|
146 |
Thread.Sleep(25); |
|
|
147 |
} |
|
|
148 |
} |
|
|
149 |
} |
|
|
150 |
} |