-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathofApp.cpp
More file actions
371 lines (296 loc) · 9.38 KB
/
ofApp.cpp
File metadata and controls
371 lines (296 loc) · 9.38 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "ofApp.h"
/*
If you are struggling to get the device to connect ( especially Windows Users )
please look at the ReadMe: in addons/ofxKinect/README.md
*/
//--------------------------------------------------------------
void ofApp::setup() {
ofSetLogLevel(OF_LOG_VERBOSE);
// enable depth->video image calibration
kinect.setRegistration(true);
kinect.init();
//kinect.init(true); // shows infrared instead of RGB video image
//kinect.init(false, false); // disable video image (faster fps)
kinect.open(); // opens first available kinect
//kinect.open(1); // open a kinect by id, starting with 0 (sorted by serial # lexicographically))
//kinect.open("A00362A08602047A"); // open a kinect using it's unique serial #
// print the intrinsic IR sensor values
if(kinect.isConnected()) {
ofLogNotice() << "sensor-emitter dist: " << kinect.getSensorEmitterDistance() << "cm";
ofLogNotice() << "sensor-camera dist: " << kinect.getSensorCameraDistance() << "cm";
ofLogNotice() << "zero plane pixel size: " << kinect.getZeroPlanePixelSize() << "mm";
ofLogNotice() << "zero plane dist: " << kinect.getZeroPlaneDistance() << "mm";
}
#ifdef USE_TWO_KINECTS
kinect2.init();
kinect2.open();
#endif
colorImg.allocate(kinect.width, kinect.height);
grayImage.allocate(kinect.width, kinect.height);
grayThreshNear.allocate(kinect.width, kinect.height);
grayThreshFar.allocate(kinect.width, kinect.height);
nearThreshold = 230;
farThreshold = 70;
bThreshWithOpenCV = true;
ofSetFrameRate(60);
// zero the tilt on startup
angle = 0;
kinect.setCameraTiltAngle(angle);
// start from the front
bDrawPointCloud = false;
gui.setup(); // most of the time you don't need a name
gui.add(bTopVP.setup("top view", false));
gui.add(bSideVP.setup("side view", false));
gui.add(b3DVP.setup("3D view", false));
gui.add(bUseColor.setup("use kinect color", false));
gui.add(k2Dolly.setup("kinect2 dolly (in/out)", 0.0,-2.0,2.0));
gui.add(k2Truck.setup("kinect2 truck (side-to-side)", 0.0,-2.0,2.0));
gui.add(k2Boom.setup("kinect2 boom (up-down)", 0.0,-2.0,2.0));
gui.add(k2Rotate.setup("kinect2 rotate", 0.0,-1.0,1.0));
gui.add(k2Tilt.setup("kinect2 tilt", 0.0,-1.0,1.0));
gui.add(k2FreezeMotion.setup("freeze Motion", false));
gui.add(k2RotatePoint.setup("kinect2 rotate point", 0,0,5000));
}
//--------------------------------------------------------------
void ofApp::update() {
if (bTopVP){
bSideVP=false;
b3DVP = false;
}
if (bSideVP){
bTopVP=false;
b3DVP = false;
}
if(k2FreezeMotion){
k2Dolly = 0.0;
k2Truck = 0.0;
k2Boom = 0.0;
k2Rotate = 0.0;
k2Tilt = 0.0;
k2FreezeMotion = false;
}
ofBackground(100, 100, 100);
kinect.update();
#ifdef USE_TWO_KINECTS
kinect2.update();
#endif
}
//--------------------------------------------------------------
void ofApp::draw() {
ofSetColor(255, 255, 255);
easyCam.begin();
if (bTopVP){
//easyCam.enableOrtho();
ofRotateX(90);
}
if (bSideVP){
//easyCam.enableOrtho();
ofRotateY(90);
}
ofSetColor(100,200,100);
drawThePointCloud1();
#ifdef USE_TWO_KINECTS
kinect2cam.dolly(k2Dolly);
kinect2cam.truck(k2Truck);
kinect2cam.boom(k2Boom);
kinect2cam.rotate(k2Rotate,ofPoint(0,k2RotatePoint,0));
kinect2cam.tilt(k2Tilt);
kinect2cam.transformGL();
drawThePointCloud2(); // this is now done in local space relative to the node
kinect2cam.restoreTransformGL();
#endif
easyCam.end();
// draw instructions
ofSetColor(255, 255, 255);
stringstream reportStream;
if(kinect.hasAccelControl()) {
reportStream << "accel is: " << ofToString(kinect.getMksAccel().x, 2) << " / "
<< ofToString(kinect.getMksAccel().y, 2) << " / "
<< ofToString(kinect.getMksAccel().z, 2) << endl;
} else {
reportStream << "Note: this is a newer Xbox Kinect or Kinect For Windows device," << endl
<< "motor / led / accel controls are not currently supported" << endl << endl;
}
reportStream << "press p to switch between images and point cloud, rotate the point cloud with the mouse" << endl;
if(kinect.hasCamTiltControl()) {
reportStream << "press UP and DOWN to change the tilt angle: " << angle << " degrees" << endl
<< "press 1-5 & 0 to change the led mode" << endl;
}
ofDrawBitmapString(reportStream.str(), 20, 652);
gui.draw();
}
void ofApp::drawThePointCloud1() {
int w = 640;
int h = 480;
ofMesh mesh;
mesh.setMode(OF_PRIMITIVE_POINTS);
int step = 2;
for(int y = 0; y < h; y += step) {
for(int x = 0; x < w; x += step) {
if(kinect.getDistanceAt(x, y) > 0) {
if (bUseColor){
mesh.addColor(ofColor(150,150,150));
}else {
mesh.addColor(kinect.getColorAt(x,y));
}
mesh.addVertex(kinect.getWorldCoordinateAt(x, y));
}
}
}
glPointSize(3);
ofPushMatrix();
// the projected points are 'upside down' and 'backwards'
ofScale(1, -1, -1);
ofTranslate(0, 0, -1000); // center the points a bit
ofLine(ofPoint(0,0,0), ofPoint(0,0,5000));
ofDrawBox(0,0,0,100,20,20);
ofEnableDepthTest();
mesh.drawVertices();
ofDisableDepthTest();
ofPopMatrix();
}
void ofApp::drawThePointCloud2() {
int w = 640;
int h = 480;
ofMesh mesh;
mesh.setMode(OF_PRIMITIVE_POINTS);
int step = 2;
for(int y = 0; y < h; y += step) {
for(int x = 0; x < w; x += step) {
if(kinect2.getDistanceAt(x, y) > 0) {
if (bUseColor){
mesh.addColor(ofColor(200,200,50));
}else {
mesh.addColor(kinect2.getColorAt(x,y));
}
mesh.addVertex(kinect2.getWorldCoordinateAt(x, y));
}
}
}
glPointSize(3);
ofPushMatrix();
// the projected points are 'upside down' and 'backwards'
ofScale(1, -1, -1);
ofTranslate(0, 0, -1000); // center the points a bit
ofEnableDepthTest();
ofSetColor(200,200,0);
ofLine(ofPoint(0,0,0), ofPoint(0,0,5000));
ofDrawSphere(ofPoint(0,0,k2RotatePoint), 10);
ofDrawBox(0,0,0,100,20,20);
mesh.drawVertices();
ofDisableDepthTest();
ofPopMatrix();
}
//--------------------------------------------------------------
void ofApp::exit() {
kinect.setCameraTiltAngle(0); // zero the tilt on exit
kinect.close();
#ifdef USE_TWO_KINECTS
kinect2.close();
#endif
}
//--------------------------------------------------------------
void ofApp::keyPressed (int key) {
switch (key) {
case 's':
//if(key == 's'){
gui.saveToFile("settings.xml");
ofxSaveCamera(kinect2cam, "kinect 2 settings");
break;
//}
//else if(key == 'l'){
case 'l':
gui.loadFromFile("settings.xml");
ofxLoadCamera(kinect2cam, "kinect 2 settings");
break;
//}
/*
case ' ':
bThreshWithOpenCV = !bThreshWithOpenCV;
break;
case'p':
bDrawPointCloud = !bDrawPointCloud;
break;
case '>':
case '.':
farThreshold ++;
if (farThreshold > 255) farThreshold = 255;
break;
case '<':
case ',':
farThreshold --;
if (farThreshold < 0) farThreshold = 0;
break;
case '+':
case '=':
nearThreshold ++;
if (nearThreshold > 255) nearThreshold = 255;
break;
case '-':
nearThreshold --;
if (nearThreshold < 0) nearThreshold = 0;
break;
case 'w':
kinect.enableDepthNearValueWhite(!kinect.isDepthNearValueWhite());
break;
case 'o':
kinect.setCameraTiltAngle(angle); // go back to prev tilt
kinect.open();
break;
case 'c':
kinect.setCameraTiltAngle(0); // zero the tilt
kinect.close();
break;
case '1':
kinect.setLed(ofxKinect::LED_GREEN);
break;
case '2':
kinect.setLed(ofxKinect::LED_YELLOW);
break;
case '3':
kinect.setLed(ofxKinect::LED_RED);
break;
case '4':
kinect.setLed(ofxKinect::LED_BLINK_GREEN);
break;
case '5':
kinect.setLed(ofxKinect::LED_BLINK_YELLOW_RED);
break;
case '0':
kinect.setLed(ofxKinect::LED_OFF);
break;
case OF_KEY_UP:
angle++;
if(angle>30) angle=30;
kinect.setCameraTiltAngle(angle);
break;
case OF_KEY_DOWN:
angle--;
if(angle<-30) angle=-30;
kinect.setCameraTiltAngle(angle);
break;
*/
}
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button)
{
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button)
{
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button)
{
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h)
{
}