Debian Codegraph @

interfaces/qkinect/QKinect.h

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.h
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 #ifndef __QKINECT_H__
34 #define __QKINECT_H__
35 #include <QObject>
36 #include <QThread>
37 #include <stdexcept>
38 #include <QMutex>
39 #include <QMutexLocker>
40 #include <vector>
41 #include <libfreenect.h>
42
43 #define FREENECT_FRAME_W 640
44 #define FREENECT_FRAME_H 480
45 #define FREENECT_FRAME_PIX (FREENECT_FRAME_H*FREENECT_FRAME_W) //width*height pixels in the image
46 #define FREENECT_VIDEO_RGB_SIZE (FREENECT_FRAME_PIX*3) //3 bytes per pixel
47
48 //----------------------------------------------------------------------------------------------------------------------
49 /// @class QKinectProcessEvents "QKinect.h"
50 /// @author Jonathan Macey
51 /// @version 1.0
52 /// @date 20/12/10 Inital commit
53 /// @brief this is the process event thread class
54 /// it needs to be a class so we can use the Qt thread system
55 //----------------------------------------------------------------------------------------------------------------------
56
57 class QKinectProcessEvents : public QThread
58 {
59 public :
60 //----------------------------------------------------------------------------------------------------------------------
61 /// @brief ctor where we pass in the context of the kinect
62 /// @param [in] _ctx the context of the current kinect device
63 //----------------------------------------------------------------------------------------------------------------------
64 inline QKinectProcessEvents(
65 freenect_context *_ctx
66 )
67 {m_ctx=_ctx;}
68 //----------------------------------------------------------------------------------------------------------------------
69 /// @brief sets the thread active this will loop the run thread
70 /// with a while(m_active) setting this will end the thread loop
71 //----------------------------------------------------------------------------------------------------------------------
72 inline void setActive(){m_active=true;}
73 //----------------------------------------------------------------------------------------------------------------------
74 /// @brief sets the thread active must call QThread::start again to make this
75 /// work if the thread has been de-activated
76 //----------------------------------------------------------------------------------------------------------------------
77 inline void setInActive(){m_active=false;}
78 protected :
79 //----------------------------------------------------------------------------------------------------------------------
80 /// @brief the actual thread main loop, this is not callable and the
81 /// QThread::start method of QThread must be called to activate the loop
82 //----------------------------------------------------------------------------------------------------------------------
83 void run();
84
85 private :
86 //----------------------------------------------------------------------------------------------------------------------
87 /// @brief a flag to indicate if the loop is to be active
88 /// set true in the ctor
89 //----------------------------------------------------------------------------------------------------------------------
90 bool m_active;
91 //----------------------------------------------------------------------------------------------------------------------
92 /// @brief the context of the kinect device, this must
93 /// be set before the thread is run with QThread::start
94 //----------------------------------------------------------------------------------------------------------------------
95 freenect_context *m_ctx;
96
97 };
98
99 //----------------------------------------------------------------------------------------------------------------------
100 /// @class QKinect "QKinect.h"
101 /// @author Jonathan Macey
102 /// @version 1.0
103 /// @date 20/12/10 Inital commit
104 /// @brief this class wraps the libfreenect library as a QObject
105 /// this allows us to use singals and slots to communicate with the class
106 /// from other Qt GUI elements. This class uses the Singleton pattern so must
107 /// be accessed via the instance method
108 //----------------------------------------------------------------------------------------------------------------------
109
110 class QKinect : public QObject
111 {
112 // must include this macro so we inherit all the core QT features such as singnals and slots
113 Q_OBJECT
114 public :
115
116 //----------------------------------------------------------------------------------------------------------------------
117 /// @brief get the instance of the QKinect object this will call the
118 /// init method of the class if the instance doesn't exist. This is because
119 /// this class will construct some threads so we need to create an instance
120 /// first then pass that to the other classes in the thread
121 /// @returns an instance of the QKinect object
122 //----------------------------------------------------------------------------------------------------------------------
123 static QKinect *instance();
124 //----------------------------------------------------------------------------------------------------------------------
125 /// @brief we can set the user device number here by default it will be 1
126 /// @param[in] _m the number we wish to set
127 //----------------------------------------------------------------------------------------------------------------------
128 inline void setUserDeviceNumber(
129 int _m
130 )
131 {m_userDeviceNumber=_m;}
132 //----------------------------------------------------------------------------------------------------------------------
133 /// @brief method to shutdown our device and close
134 //----------------------------------------------------------------------------------------------------------------------
135 void shutDownKinect();
136
137 public slots :
138 //----------------------------------------------------------------------------------------------------------------------
139 /// @brief slot to change the angle of the Kinect
140 /// @param[in] _angle the angle to set will be constrained to -/+ 30.0
141 //----------------------------------------------------------------------------------------------------------------------
142 void setAngle(
143 double _angle
144 );
145 //----------------------------------------------------------------------------------------------------------------------
146 /// @brief slot to turn off the led
147 //----------------------------------------------------------------------------------------------------------------------
148 void setLedOff();
149 //----------------------------------------------------------------------------------------------------------------------
150 /// @brief slot to enable the red LED
151 //----------------------------------------------------------------------------------------------------------------------
152 void setRedLed();
153 //----------------------------------------------------------------------------------------------------------------------
154 /// @brief slot to enable the green LED
155 //----------------------------------------------------------------------------------------------------------------------
156 void setGreenLed();
157 //----------------------------------------------------------------------------------------------------------------------
158 /// @brief slot to enable the yellow LED
159 //----------------------------------------------------------------------------------------------------------------------
160 void setYellowLed();
161 //----------------------------------------------------------------------------------------------------------------------
162 /// @brief slot to flash the red led
163 //----------------------------------------------------------------------------------------------------------------------
164 void setRedLedFlash();
165 //----------------------------------------------------------------------------------------------------------------------
166 /// @brief slot to flash the green led
167 //----------------------------------------------------------------------------------------------------------------------
168 void setGreenLedFlash();
169 //----------------------------------------------------------------------------------------------------------------------
170 /// @brief slot to flash the yellow led
171 //----------------------------------------------------------------------------------------------------------------------
172 void setYellowLedFlash();
173 //----------------------------------------------------------------------------------------------------------------------
174 /// @brief slot to set the video mode
175 /// @brief param _mode the video mode as an int index
176 /// FREENECT_VIDEO_RGB = 0,
177 /// FREENECT_VIDEO_YUV_RGB = 1,
178 /// FREENECT_VIDEO_IR_8BIT = 2,
179
180 //----------------------------------------------------------------------------------------------------------------------
181 void setVideoMode(
182 int _mode
183 );
184 //----------------------------------------------------------------------------------------------------------------------
185 /// @brief reset the tilt angle
186 //----------------------------------------------------------------------------------------------------------------------
187 void resetAngle();
188 //----------------------------------------------------------------------------------------------------------------------
189 /// @brief getDepth buffer and convert it to an RGB colour representation
190 /// this code is based on the sample implementation glview.c / cppview.cpp
191 /// @param[out] o_buffer the buffer to fill
192 //----------------------------------------------------------------------------------------------------------------------
193 bool getDepth(
194 std::vector<uint8_t> &o_buffer
195 );
196 //----------------------------------------------------------------------------------------------------------------------
197 /// @brief getDepth buffer as the raw values converted to uint8_t not sure if this will
198 /// be much use but have used it in some tracking examples
199 /// @param[out] o_buffer the buffer to fill
200 //----------------------------------------------------------------------------------------------------------------------
201 bool getDepthRaw(
202 std::vector<uint8_t> &o_buffer
203 );
204 //----------------------------------------------------------------------------------------------------------------------
205 /// @brief getDepth buffer as the raw 16 Bit values
206 /// this is useful for generating point cloud renders as we have the full depth range
207 /// @param[out] o_buffer the buffer to fill
208 //----------------------------------------------------------------------------------------------------------------------
209 bool getDepth16Bit(
210 std::vector<uint16_t> &o_buffer
211 );
212 //----------------------------------------------------------------------------------------------------------------------
213 /// @brief get the RGB buffer
214 /// @param [out] o_buffer the rgb values
215 //----------------------------------------------------------------------------------------------------------------------
216 bool getRGB(
217 std::vector<uint8_t> &o_buffer
218 );
219 //----------------------------------------------------------------------------------------------------------------------
220 /// @brief start the Depth buffer grabbing subsystem
221 //----------------------------------------------------------------------------------------------------------------------
222 void startDepth();
223 //----------------------------------------------------------------------------------------------------------------------
224 /// @brief stop the Depth buffer grabbing subsystem
225 //----------------------------------------------------------------------------------------------------------------------
226 void stopDepth();
227 //----------------------------------------------------------------------------------------------------------------------
228 /// @brief start the Video buffer grabbing subsystem
229 //----------------------------------------------------------------------------------------------------------------------
230 void startVideo();
231 //----------------------------------------------------------------------------------------------------------------------
232 /// @brief stop the Depth buffer grabbing subsystem
233 //----------------------------------------------------------------------------------------------------------------------
234 void stopVideo();
235 //----------------------------------------------------------------------------------------------------------------------
236 /// @brief convenience method to toggle the video mode with a bool to indicate
237 /// mode, useful for connecting to buttons
238 //----------------------------------------------------------------------------------------------------------------------
239 void toggleVideoState(
240 bool _mode
241 );
242 //----------------------------------------------------------------------------------------------------------------------
243 /// @brief convenience method to toggle the depth mode with a bool to indicate
244 /// mode, useful for connecting to buttons
245 //----------------------------------------------------------------------------------------------------------------------
246 void toggleDepthState(
247 bool _mode
248 );
249 //----------------------------------------------------------------------------------------------------------------------
250 /// @brief return a pointer to the active device context
251 /// @returns the current active contects
252 //----------------------------------------------------------------------------------------------------------------------
253 inline freenect_context *getContext(){return m_ctx;}
254
255 private :
256 //----------------------------------------------------------------------------------------------------------------------
257 /// @brief private ctor as we are a singleton class
258 //----------------------------------------------------------------------------------------------------------------------
259 QKinect();
260 //----------------------------------------------------------------------------------------------------------------------
261 /// @brief private dtor as we are a singleton class
262 //----------------------------------------------------------------------------------------------------------------------
263 ~QKinect();
264 //----------------------------------------------------------------------------------------------------------------------
265 /// @brief private copy ctor as we are a singleton class
266 //----------------------------------------------------------------------------------------------------------------------
267 Q_DISABLE_COPY(QKinect)
268 //----------------------------------------------------------------------------------------------------------------------
269 /// @brief private init method, this makes the instance method thread safe
270 /// as all the initialisations will be done here
271 //----------------------------------------------------------------------------------------------------------------------
272 void init();
273 //----------------------------------------------------------------------------------------------------------------------
274 /// @brief our instance of the Kinect device
275 //----------------------------------------------------------------------------------------------------------------------
276 static QKinect *s_instance;
277 //----------------------------------------------------------------------------------------------------------------------
278 /// @brief the contex for the device
279 //----------------------------------------------------------------------------------------------------------------------
280 freenect_context *m_ctx;
281 //----------------------------------------------------------------------------------------------------------------------
282 /// @brief our device pointer
283 //----------------------------------------------------------------------------------------------------------------------
284 freenect_device *m_dev;
285 //----------------------------------------------------------------------------------------------------------------------
286 /// @brief user device number
287 //----------------------------------------------------------------------------------------------------------------------
288 int m_userDeviceNumber;
289 //----------------------------------------------------------------------------------------------------------------------
290 /// @brief function for depth callback this hooks into the libfreenect callback system
291 /// @param[in] _dev the device we are querying
292 /// @param[out] the actual depth data returned from the device
293 /// @param _timestamp the time stamp for the grab (not used at present)
294 //----------------------------------------------------------------------------------------------------------------------
295 void depthFunc(
296 freenect_device *_dev,
297 void *o_depth,
298 uint32_t _timestamp
299 );
300 //----------------------------------------------------------------------------------------------------------------------
301 /// @brief function for Video callback this hooks into the libfreenect callback system
302 /// @param[in] _dev the device we are querying
303 /// @param[out] o_rgb the actual video data returned from the device
304 /// @param _timestamp the time stamp for the grab (not used at present)
305 //----------------------------------------------------------------------------------------------------------------------
306 void rgbFunc(
307 freenect_device *_dev,
308 void *o_rgb,
309 uint32_t _timestamp
310 );
311 //----------------------------------------------------------------------------------------------------------------------
312 /// @brief our depth buffer for the current depth frames will be filled
313 /// from the depth callback, this data is converted into an RGB representation
314 /// with Red being closest and blue far away, this is done via a gamma table
315 /// and converting the depth from 16 bit to 8 bit RGB useful for OpenCV like
316 /// motion tracking
317 //----------------------------------------------------------------------------------------------------------------------
318 std::vector<uint8_t> m_bufferDepth;
319 //----------------------------------------------------------------------------------------------------------------------
320 /// @brief our video buffer for the current rgb video frames will be filled
321 /// from the rgb callback
322 //----------------------------------------------------------------------------------------------------------------------
323 std::vector<uint8_t> m_bufferVideo;
324 //----------------------------------------------------------------------------------------------------------------------
325 /// @brief our depth buffer for the current depth frames will be filled
326 /// from the depth callback this is the unaltered data converted to 8 bit
327 /// not really useful so may get rid of at a later date just legacy from
328 /// testing the other examples
329 //----------------------------------------------------------------------------------------------------------------------
330 std::vector<uint8_t> m_bufferDepthRaw;
331 //----------------------------------------------------------------------------------------------------------------------
332 /// @brief the 16Bit raw depth values, this is good for voxel rendering of the
333 /// data
334 //----------------------------------------------------------------------------------------------------------------------
335 std::vector<uint16_t> m_bufferDepthRaw16;
336
337 //----------------------------------------------------------------------------------------------------------------------
338 /// @brief our gamma table this is filled in the ctor and used in the depth
339 /// callbacks, this is taken from the sample code glview.c cppview.cpp
340 //----------------------------------------------------------------------------------------------------------------------
341 std::vector<uint16_t> m_gamma;
342 //----------------------------------------------------------------------------------------------------------------------
343 /// @brief flag to indicate if there is a new rgb frame
344 //----------------------------------------------------------------------------------------------------------------------
345 bool m_newRgbFrame;
346 //----------------------------------------------------------------------------------------------------------------------
347 /// @brief flag to indicate if there is a new depth frame
348 //----------------------------------------------------------------------------------------------------------------------
349 bool m_newDepthFrame;
350 //----------------------------------------------------------------------------------------------------------------------
351 /// @brief flag to indicate if we need to stop the device
352 //----------------------------------------------------------------------------------------------------------------------
353 bool m_stopDevice;
354 //----------------------------------------------------------------------------------------------------------------------
355 /// @brief flag to indicate if the device is active
356 //----------------------------------------------------------------------------------------------------------------------
357 bool m_deviceActive;
358 //----------------------------------------------------------------------------------------------------------------------
359 /// @brief pointer to our thread process used to process the events
360 //----------------------------------------------------------------------------------------------------------------------
361 QKinectProcessEvents *m_process;
362 //----------------------------------------------------------------------------------------------------------------------
363 /// @brief our mutex used for the threaded processes, for ease we use a
364 /// QMutexLocker to automagically control our mutex state
365 //----------------------------------------------------------------------------------------------------------------------
366 QMutex m_mutex;
367 //----------------------------------------------------------------------------------------------------------------------
368 /// @brief method to grab the depth passed to the depthCallback function
369 /// as we are hooking into a C lib we need to do it this way as the callback
370 /// functions must be static (see below)
371 /// @param[out] _depth the depth data
372 /// @param[in] _timestamp the timestamp of the operation (not used)
373 //----------------------------------------------------------------------------------------------------------------------
374 void grabDepth(
375 void *_depth,
376 uint32_t timestamp
377 );
378 //----------------------------------------------------------------------------------------------------------------------
379 /// @brief method to grab the Video passed to the depthCallback function
380 /// as we are hooking into a C lib we need to do it this way as the callback
381 /// functions must be static (see below)
382 /// @param[out] _video the depth data
383 /// @param[in] _timestamp the timestamp of the operation (not used)
384 //----------------------------------------------------------------------------------------------------------------------
385 void grabVideo(
386 void *_video,
387 uint32_t timestamp
388 );
389 //----------------------------------------------------------------------------------------------------------------------
390 /// @brief this hooks into the feenet callback system an is the main way of
391 /// getting the data from the kinect this is not called directly
392 /// @param[in] _dev this is not used as we use the class m_dev
393 /// @param[out] _video the depth data
394 /// @param[in] _timestamp the timestamp of the operation (not used)
395 //----------------------------------------------------------------------------------------------------------------------
396 static inline void depthCallback(
397 freenect_device *,
398 void *_depth,
399 uint32_t _timestamp=0
400 )
401 {
402 /// get an instance of our device
403 QKinect *kinect=QKinect::instance();
404 /// then call the grab method to fill the depth buffer and return it
405 kinect->grabDepth(_depth,_timestamp);
406 }
407 //----------------------------------------------------------------------------------------------------------------------
408 /// @brief this hooks into the feenet callback system an is the main way of
409 /// getting the data from the kinect this is not called directly
410 /// @param[in] _dev this is not used as we use the class m_dev
411 /// @param[out] _video the depth data
412 /// @param[in] _timestamp the timestamp of the operation (not used)
413 //----------------------------------------------------------------------------------------------------------------------
414
415 static inline void videoCallback(
416 freenect_device *,
417 void *_video,
418 uint32_t _timestamp=0
419 )
420 {
421 /// get an instance of our device
422 QKinect *kinect=QKinect::instance();
423 /// then fill the video buffer
424 kinect->grabVideo(_video, _timestamp);
425 }
426
427 };
428
429
430
431 #endif