Debian Codegraph @

interfaces/qkinect/QKinect.cpp

raw symbols SCIP
1 /*
2 * This file is part of the OpenKinect Project. http://www.openkinect.org
3 *
4 * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
5 * for details.
6 *
7 * This code is licensed to you under the terms of the Apache License, version
8 * 2.0, or, at your option, the terms of the GNU General Public License,
9 * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10 * or the following URLs:
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * http://www.gnu.org/licenses/gpl-2.0.txt
13 *
14 * If you redistribute this file in source form, modified or unmodified, you
15 * may:
16 * 1) Leave this header intact and distribute it under the same terms,
17 * accompanying it with the APACHE20 and GPL20 files, or
18 * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19 * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20 * In all cases you must keep the copyright notice intact and include a copy
21 * of the CONTRIB file.
22 *
23 * Binary distributions must follow the binary distribution requirements of
24 * either License.
25 */
26
27 //----------------------------------------------------------------------------------------------------------------------
28 /// @file QKinect.cpp
29 /// @brief a Qt wrapper for the OpenKinect project a lot of the code has been modified from
30 /// this project source here https://github.com/OpenKinect/libfreenect
31 //----------------------------------------------------------------------------------------------------------------------
32
33 #include "QKinect.h"
34 #include <QDebug>
35 #include <cstdlib>
36 #include <cmath>
37 #include <cassert>
38
39 //----------------------------------------------------------------------------------------------------------------------
40 QKinect *QKinect::s_instance = 0;// initialize pointer
41 //----------------------------------------------------------------------------------------------------------------------
42
43
44 //----------------------------------------------------------------------------------------------------------------------
45 void QKinectProcessEvents::run()
46 {
47 // loop while we are active and process the kinect event queue
48 while(m_active)
49 {
50 if(freenect_process_events(m_ctx) < 0)
51 {
52 throw std::runtime_error("Cannot process freenect events");
53 }
54 }
55 }
56
57 //----------------------------------------------------------------------------------------------------------------------
58 /// @brief Destructor ---------------------------------------------------------------------------------------------------------------
59 //----------------------------------------------------------------------------------------------------------------------
60 QKinect::~QKinect()
61 {
62 // dtor is not called directly
63 if (s_instance)
64 {
65 delete s_instance;
66 }
67 }
68 //----------------------------------------------------------------------------------------------------------------------
69 /// @brief Get instance --------------------------------------------------------------------------------------------------------------
70 //----------------------------------------------------------------------------------------------------------------------
71 QKinect* QKinect::instance()
72 {
73 // this is the main singleton code first check to see if we exist
74 if (s_instance==0 )
75 {
76 // we do so create an instance (this will validate the pointer so other
77 // methods called in the init function will have a valid pointer to use)
78 s_instance = new QKinect;
79 // now setup the actual class (with a valid pointer)
80 /// \note this could be made nicer to make it fully thread safe
81 s_instance->init();
82 }
83 // otherwise return the existing pointer
84 return s_instance;
85 }
86
87
88 //----------------------------------------------------------------------------------------------------------------------
89 QKinect::QKinect() : QObject(0)
90 {
91 // nothing to see here we just need a valid object pointer the init
92 // method does all the hard work
93 qDebug()<<"ctor called \n";
94 }
95 //----------------------------------------------------------------------------------------------------------------------
96 void QKinect::init()
97 {
98 // first see if we can init the kinect
99 if (freenect_init(&m_ctx, NULL) < 0)
100 {
101 qDebug()<<"freenect_init() failed\n";
102 return;
103 exit(EXIT_FAILURE);
104 }
105 /// set loggin level make this programmable at some stage
106 freenect_set_log_level(m_ctx, FREENECT_LOG_DEBUG);
107 /// see how many devices we have
108 int nr_devices = freenect_num_devices (m_ctx);
109 qDebug()<<"Number of devices found: "<<nr_devices<<"\n";
110 /// now allocate the buffers so we can fill them
111 m_userDeviceNumber = 0;
112 m_bufferDepth.resize(FREENECT_VIDEO_RGB_SIZE);
113 m_bufferVideo.resize(FREENECT_VIDEO_RGB_SIZE);
114 m_bufferDepthRaw.resize(FREENECT_FRAME_PIX);
115 m_bufferDepthRaw16.resize(FREENECT_FRAME_PIX);
116 m_gamma.resize(2048);
117 /// open the device at present hard coded to device 0 as I only
118 /// have 1 kinect
119 if (freenect_open_device(m_ctx, &m_dev, m_userDeviceNumber) < 0)
120 {
121 qDebug()<<"Could not open device\n";
122 return;
123 exit(EXIT_FAILURE);
124 }
125
126
127 /// build the gamma table used for the depth to rgb conversion
128 /// taken from the demo programs
129 for (int i=0; i<2048; ++i)
130 {
131 float v = i/2048.0;
132 v = std::pow(v, 3)* 6;
133 m_gamma[i] = v*6*256;
134 }
135 /// init our flags
136 m_newRgbFrame=false;
137 m_newDepthFrame=false;
138 m_deviceActive=true;
139 // set our video formats to RGB by default
140
141 #ifdef Q_OS_MAC
142 freenect_set_video_mode(m_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB));
143 freenect_set_depth_mode(m_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
144 #else
145 freenect_set_video_format(m_dev, FREENECT_VIDEO_RGB);
146 freenect_set_depth_format(m_dev, FREENECT_DEPTH_11BIT);
147 #endif
148
149 /// hook in the callbacks
150 freenect_set_depth_callback(m_dev, depthCallback);
151 freenect_set_video_callback(m_dev, videoCallback);
152 // start the video and depth sub systems
153 startVideo();
154 startDepth();
155 // set the thread to be active and start
156 m_process = new QKinectProcessEvents(m_ctx);
157 m_process->setActive();
158 m_process->start();
159 }
160
161 //----------------------------------------------------------------------------------------------------------------------
162 void QKinect::shutDownKinect()
163 {
164 /// stop the processing thread
165 m_process->setInActive();
166 /// stop the video and depth callbacks
167 freenect_stop_depth(m_dev);
168 freenect_stop_video(m_dev);
169 // close down our devices
170 freenect_close_device(m_dev);
171 freenect_shutdown(m_ctx);
172
173 }
174 //----------------------------------------------------------------------------------------------------------------------
175 void QKinect::setLedOff()
176 {
177 freenect_set_led(m_dev,LED_OFF);
178 }
179 //----------------------------------------------------------------------------------------------------------------------
180 void QKinect::setRedLed()
181 {
182 freenect_set_led(m_dev,LED_RED);
183 }
184 //----------------------------------------------------------------------------------------------------------------------
185 void QKinect::setGreenLed()
186 {
187 freenect_set_led(m_dev,LED_GREEN);
188 }
189 //----------------------------------------------------------------------------------------------------------------------
190 void QKinect::setYellowLed()
191 {
192 freenect_set_led(m_dev,LED_YELLOW);
193 }
194 //----------------------------------------------------------------------------------------------------------------------
195 void QKinect::setRedLedFlash()
196 {
197 freenect_set_led(m_dev,LED_BLINK_RED_YELLOW);
198 }
199 //----------------------------------------------------------------------------------------------------------------------
200 void QKinect::setGreenLedFlash()
201 {
202 freenect_set_led(m_dev,LED_BLINK_GREEN);
203 }
204 //----------------------------------------------------------------------------------------------------------------------
205 void QKinect::setYellowLedFlash()
206 {
207 freenect_set_led(m_dev,LED_YELLOW);
208 }
209 //----------------------------------------------------------------------------------------------------------------------
210 void QKinect::setVideoMode(
211 int _mode
212 )
213 {
214 freenect_video_format vm=FREENECT_VIDEO_RGB;
215 switch(_mode)
216 {
217 case 0 : { vm=FREENECT_VIDEO_RGB; break;}
218 case 1 : { vm=FREENECT_VIDEO_YUV_RGB; break;}
219 case 2 : { vm=FREENECT_VIDEO_IR_8BIT; break;}
220 /*
221 /// had issues with these modes so sticking to the 3 that work
222 case 1 : { vm=FREENECT_VIDEO_BAYER; break;}
223 case 2 : { vm=FREENECT_VIDEO_IR_8BIT; break;}
224 case 3 : { vm=FREENECT_VIDEO_IR_10BIT; break;}
225 case 4 : { vm=FREENECT_VIDEO_IR_10BIT_PACKED; break;}
226 case 5 : { vm=FREENECT_VIDEO_YUV_RGB; break;}
227 case 6 : { vm=FREENECT_VIDEO_YUV_RAW; break;}
228 */
229 default : qDebug()<<"index out of bounds for video mode\n";
230 vm=FREENECT_VIDEO_RGB;
231 break;
232 }
233 /// stop the video and set to new mode
234 freenect_stop_video(m_dev);
235
236 #ifdef Q_OS_MAC
237 freenect_set_video_mode(m_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, vm));
238 #else
239 freenect_set_video_format(m_dev, vm);
240 #endif
241
242 freenect_start_video(m_dev);
243 }
244 //----------------------------------------------------------------------------------------------------------------------
245 void QKinect::resetAngle()
246 {
247 freenect_set_tilt_degs(m_dev,0);
248 }
249
250 //----------------------------------------------------------------------------------------------------------------------
251 void QKinect::setAngle(
252 double _angle
253 )
254 {
255 // constrain the angle from -30 - +30
256 if(_angle > 30)
257 {
258 _angle = 30;
259 }
260 else if(_angle <-30)
261 {
262 _angle=-30;
263 }
264 freenect_set_tilt_degs(m_dev,_angle);
265 }
266
267
268 //----------------------------------------------------------------------------------------------------------------------
269 bool QKinect::getRGB(
270 std::vector<uint8_t> &o_buffer
271 )
272 {
273 /// this grabs the rgb data we first need to lock our mutex
274 /// this will be unlocked on destruction of the locker
275 QMutexLocker locker( &m_mutex );
276 // do we have a new frame?
277 if(m_newRgbFrame)
278 {
279 // if so swap out and fill
280 o_buffer.swap(m_bufferVideo);
281 m_newRgbFrame = false;
282 return true;
283 }
284 else
285 {
286 return false;
287 }
288 }
289
290
291 //----------------------------------------------------------------------------------------------------------------------
292 bool QKinect::getDepth(
293 std::vector<uint8_t> &o_buffer
294 )
295 {
296 // this fills the depth buffer, first lock our mutex
297 QMutexLocker locker( &m_mutex );
298 if(m_newDepthFrame)
299 {
300 // swap data
301 o_buffer.swap(m_bufferDepth);
302 m_newDepthFrame = false;
303 return true;
304 }
305 else
306 {
307 return false;
308 }
309 }
310 //----------------------------------------------------------------------------------------------------------------------
311 bool QKinect::getDepth16Bit(
312 std::vector<uint16_t> &o_buffer
313 )
314 {
315 /// fill the 16 Bit data value first lock mutex
316 QMutexLocker locker( &m_mutex );
317 if(m_newDepthFrame)
318 {
319 // fill our buffer if avaliable
320 o_buffer.swap( m_bufferDepthRaw16 );
321 m_newDepthFrame = false;
322 return true;
323 }
324 else
325 {
326 return false;
327 }
328 }
329
330
331 //----------------------------------------------------------------------------------------------------------------------
332 bool QKinect::getDepthRaw(
333 std::vector<uint8_t> &o_buffer
334 )
335 {
336 QMutexLocker locker( &m_mutex );
337 if(m_newDepthFrame)
338 {
339 o_buffer.swap(m_bufferDepthRaw);
340 m_newDepthFrame = false;
341 return true;
342 }
343 else
344 {
345 return false;
346 }
347 }
348 //----------------------------------------------------------------------------------------------------------------------
349 void QKinect::grabDepth(
350 void *_depth,
351 uint32_t
352 )
353 {
354 // this method fills all the different depth buffers at once
355 // modifed from the sample code glview and cppview.cpp
356 /// lock our mutex
357 QMutexLocker locker( &m_mutex );
358 // cast the void pointer to the unint16_t the data is actually in
359 uint16_t* depth = static_cast<uint16_t*>(_depth);
360
361 // now loop and fill data buffers
362 for( unsigned int i = 0 ; i < FREENECT_FRAME_PIX ; ++i)
363 {
364 // first our two raw buffers the first will lose precision and may well
365 // be removed in the next iterations
366 m_bufferDepthRaw[i]=depth[i];
367 m_bufferDepthRaw16[i]=depth[i];
368 // now get the index into the gamma table
369 int pval = m_gamma[depth[i]];
370 // get the lower bit
371 int lb = pval & 0xff;
372 // shift right by 8 and determine which colour value to fill the
373 // array with based on the position
374 switch (pval>>8)
375 {
376 case 0:
377 m_bufferDepth[3*i+0] = 255;
378 m_bufferDepth[3*i+1] = 255-lb;
379 m_bufferDepth[3*i+2] = 255-lb;
380 break;
381 case 1:
382 m_bufferDepth[3*i+0] = 255;
383 m_bufferDepth[3*i+1] = lb;
384 m_bufferDepth[3*i+2] = 0;
385 break;
386 case 2:
387 m_bufferDepth[3*i+0] = 255-lb;
388 m_bufferDepth[3*i+1] = 255;
389 m_bufferDepth[3*i+2] = 0;
390 break;
391 case 3:
392 m_bufferDepth[3*i+0] = 0;
393 m_bufferDepth[3*i+1] = 255;
394 m_bufferDepth[3*i+2] = lb;
395 break;
396 case 4:
397 m_bufferDepth[3*i+0] = 0;
398 m_bufferDepth[3*i+1] = 255-lb;
399 m_bufferDepth[3*i+2] = 255;
400 break;
401 case 5:
402 m_bufferDepth[3*i+0] = 0;
403 m_bufferDepth[3*i+1] = 0;
404 m_bufferDepth[3*i+2] = 255-lb;
405 break;
406 default:
407 m_bufferDepth[3*i+0] = 0;
408 m_bufferDepth[3*i+1] = 0;
409 m_bufferDepth[3*i+2] = 0;
410 break;
411 }
412 }
413 // flag we have a new frame
414 m_newDepthFrame = true;
415 }
416 //----------------------------------------------------------------------------------------------------------------------
417 void QKinect::grabVideo(
418 void *_video,
419 uint32_t
420 )
421 {
422 // lock our mutex and copy the data from the video buffer
423 QMutexLocker locker( &m_mutex );
424 uint8_t* rgb = static_cast<uint8_t*>(_video);
425 std::copy(rgb, rgb+FREENECT_VIDEO_RGB_SIZE, m_bufferVideo.begin());
426 m_newRgbFrame = true;
427 }
428
429 //----------------------------------------------------------------------------------------------------------------------
430 void QKinect::startVideo()
431 {
432 if(freenect_start_video(m_dev) < 0)
433 {
434 throw std::runtime_error("Cannot start RGB callback");
435 }
436 }
437 //----------------------------------------------------------------------------------------------------------------------
438 void QKinect::stopVideo()
439 {
440 if(freenect_stop_video(m_dev) < 0)
441 {
442 throw std::runtime_error("Cannot stop RGB callback");
443 }
444 }
445 //----------------------------------------------------------------------------------------------------------------------
446 void QKinect::startDepth()
447 {
448 if(freenect_start_depth(m_dev) < 0)
449 {
450 throw std::runtime_error("Cannot start depth callback");
451 }
452 }
453
454 //----------------------------------------------------------------------------------------------------------------------
455 void QKinect::stopDepth()
456 {
457 if(freenect_stop_depth(m_dev) < 0)
458 {
459 throw std::runtime_error("Cannot stop depth callback");
460 }
461 }
462
463 //----------------------------------------------------------------------------------------------------------------------
464 void QKinect::toggleVideoState(bool _mode)
465 {
466 if(_mode ==true)
467 {
468 startVideo();
469 }
470 else
471 {
472 stopVideo();
473 }
474 }
475 //----------------------------------------------------------------------------------------------------------------------
476 void QKinect::toggleDepthState(bool _mode)
477 {
478 if(_mode == true)
479 {
480 startDepth();
481 }
482 else
483 {
484 stopDepth();
485 }
486 }
487 //----------------------------------------------------------------------------------------------------------------------