KinectV1Device.hpp
Go to the documentation of this file.
1//
2// Created by christoph on 22.08.19.
3//
4
5#ifndef VISIONX_KINECTV1DEVICE_HPP
6#define VISIONX_KINECTV1DEVICE_HPP
7
8#include <vector>
9
10#include <boost/thread/mutex.hpp>
11
12#include <libfreenect.hpp>
13
14class KinectV1Device : public Freenect::FreenectDevice
15{
16public:
17 KinectV1Device(freenect_context* _ctx, int _index) :
18 Freenect::FreenectDevice(_ctx, _index),
19 m_buffer_video(
20 freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes),
21 m_buffer_depth(
22 freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_REGISTERED).bytes /
23 2),
24 m_new_rgb_frame(false),
25 m_new_depth_frame(false)
26 {
27 setDepthFormat(FREENECT_DEPTH_REGISTERED);
28 }
29
30 // Do not call directly, even in child
31 void
32 VideoCallback(void* _rgb, uint32_t timestamp)
33 {
34 boost::mutex::scoped_lock lock(m_rgb_mutex);
35 uint8_t* rgb = static_cast<uint8_t*>(_rgb);
36 copy(rgb, rgb + getVideoBufferSize(), m_buffer_video.begin());
37 m_new_rgb_frame = true;
38 }
39
40 // Do not call directly, even in child
41 void
42 DepthCallback(void* _depth, uint32_t timestamp)
43 {
44 boost::mutex::scoped_lock lock(m_depth_mutex);
45 uint16_t* depth = static_cast<uint16_t*>(_depth);
46 copy(depth, depth + getDepthBufferSize() / 2, m_buffer_depth.begin());
47 m_new_depth_frame = true;
48 }
49
50 bool
51 getRGB(std::vector<uint8_t>& buffer)
52 {
53 boost::mutex::scoped_lock lock(m_rgb_mutex);
54
55 if (!m_new_rgb_frame)
56 {
57 return false;
58 }
59
60 buffer.swap(m_buffer_video);
61 m_new_rgb_frame = false;
62
63 return true;
64 }
65
66 bool
67 getDepth(std::vector<uint16_t>& buffer)
68 {
69 boost::mutex::scoped_lock lock(m_depth_mutex);
70
71 if (!m_new_depth_frame)
72 {
73 return false;
74 }
75
76 buffer.swap(m_buffer_depth);
77 m_new_depth_frame = false;
78
79 return true;
80 }
81
82private:
83 mutable boost::mutex m_rgb_mutex;
84 mutable boost::mutex m_depth_mutex;
85 std::vector<uint8_t> m_buffer_video;
86 std::vector<uint16_t> m_buffer_depth;
87 bool m_new_rgb_frame;
88 bool m_new_depth_frame;
89};
90
91#endif //VISIONX_KINECTV1DEVICE_HPP
std::string timestamp()
void VideoCallback(void *_rgb, uint32_t timestamp)
KinectV1Device(freenect_context *_ctx, int _index)
void DepthCallback(void *_depth, uint32_t timestamp)
bool getRGB(std::vector< uint8_t > &buffer)
bool getDepth(std::vector< uint16_t > &buffer)