-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOKPCL.cpp
More file actions
201 lines (188 loc) · 6.4 KB
/
Copy pathOKPCL.cpp
File metadata and controls
201 lines (188 loc) · 6.4 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
#include<iostream>
#include<libfreenect.hpp>
#include<pthread.h>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<vector>
#include<ctime>
#include<boost/thread/thread.hpp>
#include"boost/lexical_cast.hpp"
#include"pcl/common/common_headers.h"
#include"pcl/io/pcd_io.h"
#include"pcl/visualization/pcl_visualizer.h"
#include"pcl/console/parse.h"
#include"pcl/point_types.h"
class Mutex {
public:
Mutex() {
pthread_mutex_init( &m_mutex, NULL );
}
void lock() {
pthread_mutex_lock( &m_mutex );
}
void unlock() {
pthread_mutex_unlock( &m_mutex );
}
class ScopedLock
{
Mutex & _mutex;
public:
ScopedLock(Mutex & mutex): _mutex(mutex)
{
_mutex.lock();
}
~ScopedLock()
{
_mutex.unlock();
}
};
private:
pthread_mutex_t m_mutex;
};
///Kinect Hardware Connection Class
/* thanks to Yoda---- from IRC */
class MyFreenectDevice : public Freenect::FreenectDevice {
public:
MyFreenectDevice(freenect_context *_ctx, int _index):
Freenect::FreenectDevice(_ctx, _index),
depth(freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM,FREENECT_DEPTH_REGISTERED).bytes),
m_buffer_video(freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM,FREENECT_VIDEO_RGB).bytes),
m_new_rgb_frame(false), m_new_depth_frame(false)
{
}
//~MyFreenectDevice(){}
// Do not call directly even in child
void VideoCallback(void* _rgb, uint32_t timestamp) {
Mutex::ScopedLock lock(m_rgb_mutex);
uint8_t* rgb = static_cast<uint8_t*>(_rgb);
std::copy(rgb, rgb+getVideoBufferSize(), m_buffer_video.begin());
m_new_rgb_frame = true;
};
// Do not call directly even in child
void DepthCallback(void* _depth, uint32_t timestamp) {
Mutex::ScopedLock lock(m_depth_mutex);
depth.clear();
uint16_t* call_depth = static_cast<uint16_t*>(_depth);
for (size_t i = 0; i < 640*480 ; i++) depth.push_back(call_depth[i]);
m_new_depth_frame = true;
}
bool getRGB(std::vector<uint8_t>&buffer) {
//printf("Getting RGB!\n");
Mutex::ScopedLock lock(m_rgb_mutex);
if (!m_new_rgb_frame) {
//printf("No new RGB Frame.\n");
return false;
}
buffer.swap(m_buffer_video);
m_new_rgb_frame = false;
return true;
}
bool getDepth(std::vector<uint16_t>&buffer) {
Mutex::ScopedLock lock(m_depth_mutex);
if (!m_new_depth_frame) return false;
buffer.swap(depth);
m_new_depth_frame = false;
return true;
}
private:
std::vector<uint16_t> depth;
std::vector<uint8_t> m_buffer_video;
Mutex m_rgb_mutex;
Mutex m_depth_mutex;
bool m_new_rgb_frame;
bool m_new_depth_frame;
};
///Start the PCL/OK Bridging
//OK
Freenect::Freenect freenect;
MyFreenectDevice* device;
freenect_video_format requested_format(FREENECT_VIDEO_RGB);
double freenect_angle(0);
int got_frames(0),window(0);
int g_argc;
char **g_argv;
int user_data = 0;
//PCL
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
unsigned int cloud_id = 0;
///Keyboard Event Tracking
void keyboardEventOccurred (const pcl::visualization::KeyboardEvent &event,void* viewer_void)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = *static_cast<boost::shared_ptr<pcl::visualization::PCLVisualizer> *> (viewer_void);
if (event.getKeySym () == "c"&&event.keyDown ())
{
std::cout <<"c was pressed => capturing a pointcloud"<< std::endl;
std::string filename = "KinectCap";
filename.append(boost::lexical_cast<std::string>(cloud_id));
filename.append(".pcd");
pcl::io::savePCDFileASCII (filename, *cloud);
cloud_id++;
}
}
int main (int argc, char** argv)
{
//More Kinect Setup
static std::vector<uint16_t> mdepth(640*480);
static std::vector<uint8_t> mrgb(640*480*4);
// Fill in the cloud data
cloud->width = 640;
cloud->height = 480;
cloud->is_dense = false;
cloud->points.resize (cloud->width * cloud->height);
// Create and setup the viewer
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->registerKeyboardCallback (keyboardEventOccurred, (void*)&viewer);
viewer->setBackgroundColor (0, 0, 0);
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, "Kinect Cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE,1, "Kinect Cloud");
viewer->addCoordinateSystem (1.0, 0);
viewer->initCameraParameters ();
device = &freenect.createDevice<MyFreenectDevice>(0);
device->startVideo();
device->startDepth();
boost::this_thread::sleep (boost::posix_time::seconds (1));
//Grab until clean returns
int DepthCount = 0;
while (DepthCount == 0) {
device->updateState();
device->getDepth(mdepth);
device->getRGB(mrgb);
for (size_t i = 0;i < 480*640;i++) DepthCount+=mdepth[i];
}
device->setVideoFormat(requested_format);
//--------------------
// -----Main loop-----
//--------------------
double x = NULL;
double y = NULL;
int iRealDepth = 0;
while (!viewer->wasStopped ())
{
device->updateState();
device->getDepth(mdepth);
device->getRGB(mrgb);
size_t i = 0;
size_t cinput = 0;
for (size_t v=0 ; v<480 ; v++)
{
for ( size_t u=0 ; u<640 ; u++, i++)
{
iRealDepth = mdepth[i];
freenect_camera_to_world(device->getDevicePtr(), u, v, iRealDepth, &x, &y);
cloud->points[i].x = x;
cloud->points[i].y = y;
cloud->points[i].z = iRealDepth;
cloud->points[i].r = mrgb[i*3];
cloud->points[i].g = mrgb[(i*3)+1];
cloud->points[i].b = mrgb[(i*3)+2];
}
}
viewer->updatePointCloud (cloud, "Kinect Cloud");
viewer->spinOnce ();
}
printf("render loop finished\n");
device->stopVideo();
device->stopDepth();
return 0;
}