-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
387 lines (285 loc) · 12 KB
/
main.cpp
File metadata and controls
387 lines (285 loc) · 12 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <pcl/common/common_headers.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <librealsense/rs.hpp>
#include <iostream>
#include <chrono>
//===================================================================
// Global data
//===================================================================
#define NOISY 3.5 // Remove points past NOISY meters
#define FPS_MILLI 500 // Update fps every 0.5 seconds
// time typdefs for fps
typedef std::chrono::milliseconds TIME_IN_MILLI;
typedef std::chrono::duration<double, std::milli> TIME_DIFF;
typedef std::chrono::high_resolution_clock::time_point TIME_POINT;
//===================================================================
// Function declarations
//===================================================================
// Creates the window viewer displaying the point cloud
std::shared_ptr<pcl::visualization::PCLVisualizer> createPointCloudViewer( pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud );
// Looks at RS context info
int printRSContextInfo( rs::context *rsContext );
// Configures RS streams
int configureRSStreams( rs::device *rsCamera );
// Prints the FPS info to the viewer
int printTimeLoop( TIME_POINT &t0, TIME_POINT &t1, TIME_POINT &t2,int &frames, int totalframes, double &fps, double &totalfps, std::shared_ptr<pcl::visualization::PCLVisualizer> v );
// Generats the current frames point cloud data
int generatePointCloud( rs::device *dev, pcl::PointCloud<pcl::PointXYZRGB>::Ptr );
//===================================================================
// Main
//===================================================================
int main( int argc, char** argv ) try
{
int err = -1;
int const BOOST_WAIT_TIME = 1;
// Frame Numbers
int frames = 0;
int totalframes = 0;
// FPS
double fps = 0;
double totalfps = 0;
// Time Points used for displaying FPS
TIME_POINT timePoint0;
TIME_POINT timePoint1;
TIME_POINT timePoint2;
timePoint0 = timePoint1 = timePoint2 = std::chrono::high_resolution_clock::now( );
// ==== Cloud Setup ====
pcl::PointCloud<pcl::PointXYZRGB>::Ptr rsCloudPtr( new pcl::PointCloud<pcl::PointXYZRGB> );
std::shared_ptr<pcl::visualization::PCLVisualizer> pclVisualizer;
// Create the PCL viewer/window
pclVisualizer = createPointCloudViewer( rsCloudPtr );
//rs::log_to_console( rs::log_severity::warn );
// Create the RS context and display info about it
rs::context rsContext;
printRSContextInfo( &rsContext );
// Create the RS camera and configure streaming and start the streams
rs::device * rsCamera = rsContext.get_device( 0 );
configureRSStreams( rsCamera );
while( !pclVisualizer->wasStopped( ) )
{
// ==== Timing ====
++frames;
++totalframes;
err = printTimeLoop( timePoint0, timePoint1, timePoint2, frames, totalframes, fps, totalfps, pclVisualizer );
if( err != EXIT_SUCCESS )
{
std::cout << "Error in printTimeLoop( )\n" << std::endl;
return err;
}
// ==== Data Grab ====
err = generatePointCloud( rsCamera, rsCloudPtr );
if( err != EXIT_SUCCESS )
{
std::cout << "Error in getFrame( )\n" << std::endl;
return err;
}
// ==== Update Viewer Cloud State and display it ====
pclVisualizer->updatePointCloud( rsCloudPtr, "sample cloud" );
pclVisualizer->spinOnce( 1 );
//boost::this_thread::sleep( boost::posix_time::microseconds( BOOST_WAIT_TIME ) );
}
pclVisualizer->close( );
return EXIT_SUCCESS;
}
catch( const rs::error & e )
{
std::cerr << "RealSense error calling " << e.get_failed_function( ) << "(" << e.get_failed_args( ) << "):\n " << e.what( ) << std::endl;
return EXIT_FAILURE;
}
catch( const std::exception & e )
{
std::cerr << e.what( ) << std::endl;
return EXIT_FAILURE;
}
//===================================================================
// Create the viewer window where the point cloud is rendered
//===================================================================
std::shared_ptr<pcl::visualization::PCLVisualizer> createPointCloudViewer( pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud )
{
// Open 3D viewer and add point cloud
std::shared_ptr<pcl::visualization::PCLVisualizer> viewer( new pcl::visualization::PCLVisualizer( "LibRealSense PCL Viewer" ) );
viewer->setBackgroundColor( 0.251, 0.251, 0.251 ); // Floral white 1, 0.98, 0.94 | Misty Rose 1, 0.912, 0.9 |
viewer->addPointCloud<pcl::PointXYZRGB>( cloud, "sample cloud" );
viewer->setPointCloudRenderingProperties( pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud" );
viewer->addCoordinateSystem( 1.0 );
viewer->initCameraParameters( );
viewer->setShowFPS( false );
return( viewer );
}
//===================================================================
// provides context info and will exit if no devices
//===================================================================
int printRSContextInfo( rs::context *c )
{
printf( "There are %d connected RealSense devices.\n", c->get_device_count( ) );
if( c->get_device_count( ) == 0 )
throw std::runtime_error( "No device detected. Is it plugged in?" );
return EXIT_SUCCESS;
}
//===================================================================
// stream config & enabling for device
//===================================================================
int configureRSStreams( rs::device *rsCamera )
{
std::cout << "Configuring RS streaming " << rsCamera->get_name( ) << "... ";
rsCamera->enable_stream( rs::stream::depth, rs::preset::best_quality );
rsCamera->enable_stream( rs::stream::color, rs::preset::best_quality );
rsCamera->start( );
std::cout << "RS streaming enabled and running.\n";
return EXIT_SUCCESS;
}
//===================================================================
// calculates time stats and prints them on the viewer
//===================================================================
int printTimeLoop( TIME_POINT &t0, TIME_POINT &t1, TIME_POINT &t2,
int &frames, int totalframes, double &fps,
double &totalfps, std::shared_ptr<pcl::visualization::PCLVisualizer> v )
{
TIME_IN_MILLI zero_ms{0};
TIME_DIFF fp_ms;
TIME_DIFF overall;
fp_ms = overall = zero_ms;
t2 = std::chrono::high_resolution_clock::now( );
fp_ms = ( t2 - t1 );
if( fp_ms.count( ) > FPS_MILLI )
{
fps = frames / fp_ms.count( );
fps *= 1000.0;
frames = 0;
t1 = t2;
}
overall = ( t2 - t0 );
totalfps = overall.count( );
totalfps /= 1000.0;
if( ( totalframes % 10 ) == 0 )
{
char time_buffer[ 8 ], fps_buffer[ 8 ];
std::stringstream ss;
sprintf( time_buffer, "%4.2f", totalfps );
sprintf( fps_buffer, "%4.2f", fps );
ss << "FPS: " << fps_buffer << " Total Frames Processed: " << totalframes << " Time: " << time_buffer;
v->removeShape( "text", 0 );
v->addText( ss.str( ), 10, 0, "text" );
}
return EXIT_SUCCESS;
}
//===================================================================
// Get the raw data and build the current frames cloud
//===================================================================
int generatePointCloud( rs::device *dev, pcl::PointCloud<pcl::PointXYZRGB>::Ptr rs_cloud_ptr )
{
// Wait for new frame data
if( dev->is_streaming( ) )
dev->wait_for_frames();
// Retrieve our images
const uint16_t * depth_image = ( const uint16_t * )dev->get_frame_data( rs::stream::depth );
const uint8_t * color_image = ( const uint8_t * )dev->get_frame_data( rs::stream::color );
// Retrieve camera parameters for mapping between depth and color
rs::intrinsics depth_intrin = dev->get_stream_intrinsics( rs::stream::depth );
rs::extrinsics depth_to_color = dev->get_extrinsics( rs::stream::depth, rs::stream::color );
rs::intrinsics color_intrin = dev->get_stream_intrinsics( rs::stream::color );
float scale = dev->get_depth_scale( );
// Depth dimension helpers
int dw = 0;
int dh = 0;
int dwh = 0;
dw = depth_intrin.width;
dh = depth_intrin.height;
dwh = dw * dh;
// Set the cloud up to be used
rs_cloud_ptr->clear( );
rs_cloud_ptr->is_dense = false;
rs_cloud_ptr->resize( dwh );
// Iterate the data space
// First, iterate across columns
for( int dy = 0; dy < dh; dy++ )
{
// Second, iterate across rows
for( int dx = 0; dx < dw; dx++ )
{
uint i = dy * dw + dx;
uint16_t depth_value = depth_image[ i ];
if( depth_value == 0 )
continue;
rs::float2 depth_pixel = { (float)dx, (float)dy };
float depth_in_meters = depth_value * scale;
rs::float3 depth_point = depth_intrin.deproject( depth_pixel, depth_in_meters );
rs::float3 color_point = depth_to_color.transform(depth_point);
rs::float2 color_pixel = color_intrin.project(color_point);
const int cx = ( int )std::round( color_pixel.x );
const int cy = ( int )std::round( color_pixel.y );
static const float nan = std::numeric_limits<float>::quiet_NaN( );
// Set up logic to remove bad points
bool depth_fail = true;
bool color_fail = true;
depth_fail = ( depth_point.z > NOISY );
color_fail = ( cx < 0 || cy < 0 || cx > color_intrin.width || cy > color_intrin.height );
// ==== Cloud Input Pointers ====
// XYZ input access to cloud
float *dp_x;
float *dp_y;
float *dp_z;
dp_x = &( rs_cloud_ptr->points[ i ].x );
dp_y = &( rs_cloud_ptr->points[ i ].y );
dp_z = &( rs_cloud_ptr->points[ i ].z );
// RGB input access to cloud
uint8_t *cp_r;
uint8_t *cp_g;
uint8_t *cp_b;
cp_r = &( rs_cloud_ptr->points[ i ].r );
cp_g = &( rs_cloud_ptr->points[ i ].g );
cp_b = &( rs_cloud_ptr->points[ i ].b );
// ==== Cloud Input Data ====
// Set up depth point data
float real_x = 0;
float real_y = 0;
float real_z = 0;
float adjusted_x = 0;
float adjusted_y = 0;
float adjusted_z = 0;
real_x = depth_point.x;
real_y = depth_point.y;
real_z = depth_point.z;
// Adjust point to coordinates
adjusted_x = -1 * real_x;
adjusted_y = -1 * real_y;
adjusted_z = real_z;
// Set up color point data
const uint8_t *offset = ( color_image + ( cy * color_intrin.width + cx ) * 3 );
uint8_t raw_r = 0;
uint8_t raw_g = 0;
uint8_t raw_b = 0;
uint8_t adjusted_r = 0;
uint8_t adjusted_g = 0;
uint8_t adjusted_b = 0;
raw_r = *( offset );
raw_g = *( offset + 1 );
raw_b = *( offset + 2 );
// Adjust color arbitrarily
adjusted_r = raw_r;
adjusted_g = raw_g;
adjusted_b = raw_b;
// ==== Cloud Point Evaluation ====
// If bad point, remove & skip
if( depth_fail || color_fail )
{
*dp_x = *dp_y = *dp_z = (float) nan;
*cp_r = *cp_g = *cp_b = 0;
continue;
}
// If valid point, add data to cloud
else
{
// Fill in cloud depth
*dp_x = adjusted_x;
*dp_y = adjusted_y;
*dp_z = adjusted_z;
// Fill in cloud color
*cp_r = adjusted_r;
*cp_g = adjusted_g;
*cp_b = adjusted_b;
}
}
}
return EXIT_SUCCESS;
}