-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinect OSX Flipped Curser Control
301 lines (245 loc) · 7.05 KB
/
Kinect OSX Flipped Curser Control
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* --------------------------------------------------------------------------
* SimpleOpenNI Hands3d Test
* Processing Wrapper for the OpenNI/Kinect library
* http://code.google.com/p/simple-openni
* --------------------------------------------------------------------------
* programmed by Sam Hessenauer, start Date: March 2013
Based off of Max Rheiner / Interaction Design / zhdk / http://iad.zhdk.ch/
* ----------------------------------------------------------------------------
* This demos shows how to use the gesture/hand generator to control a mouse in
backgrounded applications, with the right side of the screen being a click.
Next steps are to add gesture recognition to simple click, right click, and hold click.
* ----------------------------------------------------------------------------
*/
import SimpleOpenNI.*;
import java.awt.Robot;
import java.awt.AWTException;
//import processing.opengl.*;
//CLICK TESt
import java.awt.event.InputEvent;
//end click test
SimpleOpenNI context;
Robot robby;
float zoomF =0.5f;
float rotX = radians(180); // by default rotate the hole scene 180deg around the x-axis,
// the data from openni comes upside down
float rotY = radians(0);
boolean handsTrackFlag = false;
PVector handVec = new PVector();
ArrayList handVecList = new ArrayList();
int handVecListSize = 30;
String lastGesture = "";
//test curser flip
float curserx;
float curserxtwo;
float curserytwo;
void setup()
{
size(1920,1080,P3D); // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem
//size(1024,768,OPENGL);
//ROBOT TEST
try
{
robby = new Robot();
}
catch (AWTException e)
{
println("Robot class not supported by your system!");
exit();
}
//eND ROBOT
context = new SimpleOpenNI(this);
// disable mirror
context.setMirror(true);
// enable depthMap generation
if(context.enableDepth() == false)
{
println("Can't open the depthMap, maybe the camera is not connected!");
exit();
return;
}
// enable hands + gesture generation
context.enableGesture();
context.enableHands();
// add focus gestures / here i do have some problems on the mac, i only recognize raiseHand ? Maybe cpu performance ?
context.addGesture("Wave");
context.addGesture("Click");
context.addGesture("RaiseHand");
// set how smooth the hand capturing should be
//context.setSmoothingHands(.5);
stroke(255,255,255);
smooth();
perspective(radians(45),
float(width)/float(height),
10.0f,150000.0f);
}
void draw()
{
// update the cam
context.update();
background(0,0,0);
// set the scene pos
translate(width/2, height/2, 0);
rotateX(rotX);
rotateY(rotY);
scale(zoomF);
// draw the 3d point depth map
int[] depthMap = context.depthMap();
int steps = 3; // to speed up the drawing, draw every third point
int index;
PVector realWorldPoint;
translate(0,0,-1000); // set the rotation center of the scene 1000 infront of the camera
stroke(200);
for(int y=0;y < context.depthHeight();y+=steps)
{
for(int x=0;x < context.depthWidth();x+=steps)
{
index = x + y * context.depthWidth();
if(depthMap[index] > 0)
{
// draw the projected point
realWorldPoint = context.depthMapRealWorld()[index];
point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z);
}
}
}
// draw the tracked hand
if(handsTrackFlag)
{
pushStyle();
stroke(255,0,0,200);
noFill();
Iterator itr = handVecList.iterator();
beginShape();
while( itr.hasNext() )
{
PVector p = (PVector) itr.next();
PVector q = (PVector) handVecList.get(0);
/*
if(p.x >= 1920/2){
float a = p.x -1920/2;
curserx = 1920/2 - a;
}
else if (p.x < 1920/2){
float b = 1920/2 - p.x;
curserx = 1920/2 - b;
}
*/
//ROBOT TEST
curserxtwo = map(q.x, -width/2,width/2, 0, width);
curserytwo = map(q.y, 627, -830, 0, 1080);
robby.mouseMove(int(curserxtwo), int(curserytwo));
//END ROBOT TEST
vertex(p.x,p.y,p.z);
}
endShape();
stroke(255,0,0);
strokeWeight(4);
//TEST
if(handVec.x >= width/2){
float a = handVec.x -width/2;
curserx = width/2 - a;
point(curserx,handVec.y,handVec.z);
}
else if (handVec.x < width/2){
float b = width/2 - handVec.x;
curserx = width/2 - b;
}
point(curserx,handVec.y,handVec.z);
popStyle();
}
// draw the kinect cam
context.drawCamFrustum();
//WOW
//TEST mouse click
//This part isnt working
//if(q.x > 500){
if(curserxtwo > 500){
try{
robby.mousePress(InputEvent.BUTTON3_MASK);
robby.mouseRelease(InputEvent.BUTTON3_MASK);
println("Mousepressed");
}
finally{}
/* catch(AWTException e){
e.printStackTrace();
}
*/
}
}//END DRAW
// -----------------------------------------------------------------
// hand events
void onCreateHands(int handId,PVector pos,float time)
{
println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);
handsTrackFlag = true;
handVec = pos;
handVecList.clear();
handVecList.add(pos);
}
void onUpdateHands(int handId,PVector pos,float time)
{
// println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
handVec = pos;
handVecList.add(0,pos);
if(handVecList.size() >= handVecListSize)
{ // remove the last point
handVecList.remove(handVecList.size()-1);
}
}
void onDestroyHands(int handId,float time)
{
println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
handsTrackFlag = false;
context.addGesture(lastGesture);
}
// -----------------------------------------------------------------
// gesture events
void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition)
{
println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);
lastGesture = strGesture;
context.removeGesture(strGesture);
context.startTrackingHands(endPosition);
}
void onProgressGesture(String strGesture, PVector position,float progress)
{
//println("onProgressGesture - strGesture: " + strGesture + ", position: " + position + ", progress:" + progress);
}
//END TEST MOUSE CLICK
// -----------------------------------------------------------------
// Keyboard event
void keyPressed()
{
switch(key)
{
case ' ':
context.setMirror(!context.mirror());
break;
}
switch(keyCode)
{
case LEFT:
rotY += 0.1f;
break;
case RIGHT:
rotY -= 0.1f;
break;
case UP:
if(keyEvent.isShiftDown())
zoomF += 0.01f;
else
rotX += 0.1f;
break;
case DOWN:
if(keyEvent.isShiftDown())
{
zoomF -= 0.01f;
if(zoomF < 0.01)
zoomF = 0.01;
}
else
rotX -= 0.1f;
break;
}
}