-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproyecto_final_kinect4win.pde
261 lines (218 loc) · 6.58 KB
/
proyecto_final_kinect4win.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Kinect interaction with proscene
* by Fabián Monsalve and Rafael Díez.
*
*/
import remixlab.proscene.*;
import remixlab.proscene.*;
import remixlab.bias.*;
import remixlab.bias.event.*;
import remixlab.dandelion.geom.*;
import remixlab.dandelion.core.*;
import kinect4WinSDK.Kinect;
import kinect4WinSDK.SkeletonData;
Scene mainScene;
PGraphics canvas, kinectCanvas;
InteractiveFrame iFrame;
boolean firstPerson;
HIDAgent hidAgent;
static int SN_ID;
// Kinect Library object
KinectTrack kinectAgent;
float maxThreshX = 320;
float maxThreshY = 240;
float centerThreshX = (maxThreshX)/2;
float centerThreshY = (maxThreshY)/2;
float centerThreshZ = 10500;
float centerOffsetX = 50;
float centerOffsetY = 30;
float centerOffsetZ = 1000;
float defaultZPosition = 0;
float defaultXPosition = 0;
float defaultYPosition = 0;
float defaultYRotate = 0;
float defaultZRotate = 0;
float rightHandX = 0;
float rightHandY = 0;
float rightHandZ = 0;
float leftHandX = 0;
float leftHandY = 0;
void setup() {
size(1024, 720, P3D);
canvas = createGraphics(width, height, P3D);
mainScene = new Scene(this, (PGraphics3D) canvas);
iFrame = new InteractiveFrame(mainScene);
iFrame.translate(30, 30);
//toggleFirstPerson();
hidAgent = new HIDAgent(mainScene);
kinectAgent = new KinectTrack(this);
// we bound some frame DOF5 actions to the gesture on both frames
mainScene.eyeFrame().setMotionBinding(SN_ID, "translateRotateXYZ");
// manipulate Iframe, when shift default grabber
iFrame.setMotionBinding(SN_ID, "translateRotateXYZ");
hidAgent.addGrabber(iFrame);
smooth();
kinectAgent.setUpBodyData();
}
void draw() {
mainScene.beginDraw();
background(0);
// Save the current model view matrix
pushMatrix();
// Multiply matrix to get in the frame coordinate system.
// applyMatrix(mainScene.toPMatrix(iFrame.matrix())); //is possible but inefficient
iFrame.applyTransformation();//very efficient
// Draw an axis using the mainScene static function
mainScene.drawAxes(20);
// Draw a second torus
if (mainScene.motionAgent().defaultGrabber() == iFrame) {
mainScene.pg().fill(0, 255, 255);
mainScene.drawTorusSolenoid();
}
else if (iFrame.grabsInput()) {
mainScene.pg().fill(255, 0, 0);
mainScene.drawTorusSolenoid();
}
else {
mainScene.pg().fill(0, 0, 255, 150);
mainScene.drawTorusSolenoid();
}
popMatrix();
mainScene.endDraw();
mainScene.display();
image(kinectAgent.kinect.GetDepth(), 0, 0, maxThreshX, maxThreshY);
for (int i=0; i<kinectAgent.bodies.size (); i++)
{
rightHandX = kinectAgent.getRightHandPosition(kinectAgent.bodies.get(i)).x * maxThreshX;
rightHandY = kinectAgent.getRightHandPosition(kinectAgent.bodies.get(i)).y * maxThreshY;
rightHandZ = kinectAgent.getRightHandPosition(kinectAgent.bodies.get(i)).z;
leftHandX = kinectAgent.getLeftHandPosition(kinectAgent.bodies.get(i)).x * maxThreshX;
leftHandY = kinectAgent.getLeftHandPosition(kinectAgent.bodies.get(i)).y * maxThreshY;
processKinectRightHand(rightHandX, rightHandY, rightHandZ);
processKinectLeftHand(leftHandX, leftHandY);
}
}
// processing translation movement
public void processKinectRightHand(float x , float y, float z){
// processing x translatation
if(x > (centerThreshX + centerOffsetX))
defaultXPosition = 0.3;
if(x < (centerThreshX - centerOffsetX))
defaultXPosition = -0.3;
if(x > (centerThreshX - centerOffsetX) && x < (centerThreshX + centerOffsetX))
defaultXPosition = 0;
// processing y translatation
if(y > (centerThreshY + centerOffsetY))
defaultYPosition = 0.3;
if(y < (centerThreshY - centerOffsetY))
defaultYPosition = -0.3;
if(y > (centerThreshY - centerOffsetY) && y < (centerThreshY + centerOffsetY))
defaultYPosition = 0;
// processing z translatation
if(z > (centerThreshZ + centerOffsetZ)){
defaultZPosition = -0.3;
fill(255,246,2);
noStroke();
ellipse(x, y, 10, 10);
}
else if(z < (centerThreshZ - centerOffsetZ)){
defaultZPosition = 0.3;
fill(255,0,0);
noStroke();
ellipse(x, y, 50, 50);
}
else if(z > (centerThreshZ - centerOffsetZ) && y < (centerThreshZ + centerOffsetZ)){
defaultZPosition = 0;
fill(0,255,0);
noStroke();
ellipse(x, y, 25, 25);
}
}
// processing rotation movement
public void processKinectLeftHand(float x , float y){
fill(150,0,255);
noStroke();
ellipse(x, y, 25, 25);
// processing x Rotation (proxy of Z rotation, since x is actually 0)
if(x > (centerThreshX + centerOffsetX))
defaultZRotate = 0.3;
if(x < (centerThreshX - centerOffsetX))
defaultZRotate = -0.3;
if(x > (centerThreshX - centerOffsetX) && x < (centerThreshX + centerOffsetX))
defaultZRotate = 0;
// processing y Rotation
if(y > (centerThreshY + centerOffsetY))
defaultYRotate = 0.3;
if(y < (centerThreshY - centerOffsetY))
defaultYRotate = -0.3;
if(y > (centerThreshY - centerOffsetY) && y < (centerThreshY + centerOffsetY))
defaultYRotate = 0;
}
public void keyPressed() {
// flip the scene
if ( key == 'y')
mainScene.flip();
// shift to manipulate the Iframe
if ( key == 'i')
mainScene.inputHandler().shiftDefaultGrabber(mainScene.eyeFrame(), iFrame);
// adding more sensitivity to the DOF5
if(key == '+'){
float [] sens = hidAgent.getSens();
for (int i = 0; i < sens.length; i++) {
if(i != 3){
sens[i] += 1;
}
}
hidAgent.setSens(sens);
}
// subtracting sensitivity to the DOF5
if(key == '-'){
float [] sens = hidAgent.getSens();
for (int i = 0; i < sens.length; i++) {
if(i != 3 && sens[i] > 0){
sens[i] -= 1;
}
}
hidAgent.setSens(sens);
}
}
// kinect4WinSDK updating default methods
void appearEvent(SkeletonData _s)
{
if (_s.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED)
{
return;
}
synchronized(kinectAgent.bodies) {
kinectAgent.bodies.add(_s);
}
}
void disappearEvent(SkeletonData _s)
{
synchronized(kinectAgent.bodies) {
for (int i=kinectAgent.bodies.size ()-1; i>=0; i--)
{
if (_s.dwTrackingID == kinectAgent.bodies.get(i).dwTrackingID)
{
kinectAgent.bodies.remove(i);
}
}
}
}
void moveEvent(SkeletonData _b, SkeletonData _a)
{
if (_a.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED)
{
return;
}
synchronized(kinectAgent.bodies) {
for (int i=kinectAgent.bodies.size ()-1; i>=0; i--)
{
if (_b.dwTrackingID == kinectAgent.bodies.get(i).dwTrackingID)
{
kinectAgent.bodies.get(i).copy(_a);
break;
}
}
}
}