59 bool _is_online,
const string& _rawlog_file =
string(),
60 bool _generate_3D_pointcloud_in_this_thread =
false)
61 : is_online(_is_online),
62 rawlog_file(_rawlog_file),
63 generate_3D_pointcloud_in_this_thread(
64 _generate_3D_pointcloud_in_this_thread),
73 const string rawlog_file;
76 const bool generate_3D_pointcloud_in_this_thread;
101 typedef std::unique_ptr<CKinect>
118 kinect->enableGrabRGB(
true);
119 kinect->enableGrabDepth(
true);
120 kinect->enableGrabAccelerometers(
false);
121 kinect->enableGrab3DPoints(
p.generate_3D_pointcloud_in_this_thread);
124 cout <<
"Calling CKinect::initialize()...";
125 kinect->initialize();
132 if (!dataset.
open(
p.rawlog_file))
133 throw std::runtime_error(
134 "Couldn't open rawlog dataset file for input...");
137 CImage::setImagesPathBase(
138 CRawlog::detectImagesDirectory(
p.rawlog_file));
149 bool there_is_obs =
true, hard_error =
false;
155 kinect->getNextObservation(*obs, there_is_obs, hard_error);
158 throw std::runtime_error(
159 "Sensor returned 'hardware error'");
160 else if (there_is_obs)
163 std::atomic_store(&
p.new_obs, obs);
176 catch (std::exception& e)
178 throw std::runtime_error(
180 "\nError reading from dataset file (EOF?):\n") +
188 std::dynamic_pointer_cast<CObservation3DRangeScan>(obs);
199 #ifndef FAKE_KINECT_FPS_RATE
200 const double At_dataset =
203 const double At_dataset = 1.0 / FAKE_KINECT_FPS_RATE;
206 my_last_read_obs_tim, now_tim);
208 const double need_to_wait_ms =
209 1000. * (At_dataset - At_actual);
212 if (need_to_wait_ms > 0)
213 std::this_thread::sleep_for(
214 std::chrono::duration<double, std::milli>(
219 std::atomic_store(&
p.new_obs, obs3D);
221 dataset_prev_tim = cur_tim;
229 p.Hz = nImgs / tictac.
Tac();
235 catch (std::exception& e)
237 cout <<
"Exception in Kinect thread: " << e.what() << endl;
246 bool is_online,
const string& rawlog_file =
string())
251 is_online, rawlog_file,
260 cout <<
"Waiting for sensor initialization...\n";
267 std::this_thread::sleep_for(10ms);
268 }
while (!thrPar.quit);
271 if (thrPar.quit)
return;
272 cout <<
"OK! Sensor started to emit observations.\n";
278 win3D.setCameraAzimuthDeg(140);
279 win3D.setCameraElevationDeg(20);
280 win3D.setCameraZoom(8.0);
282 win3D.setCameraPointingToPoint(2.5, 0, 0);
285 mrpt::make_aligned_shared<mrpt::opengl::CPointCloudColoured>();
286 gl_points->setPointSize(2.5);
294 scene->insert(gl_points);
295 scene->insert(mrpt::make_aligned_shared<mrpt::opengl::CGridPlaneXY>());
298 const double aspect_ratio = 480.0 / 640.0;
301 const int VW_HEIGHT = aspect_ratio * VW_WIDTH;
304 viewInt = scene->createViewport(
"view2d_int");
305 viewInt->setViewportPosition(5, 30, VW_WIDTH, VW_HEIGHT);
306 win3D.addTextMessage(
307 10, 30 + VW_HEIGHT + 10,
"Intensity data",
TColorf(1, 1, 1), 2,
310 win3D.addTextMessage(
311 5, 5,
format(
"'o'/'i'-zoom out/in, ESC: quit"),
TColorf(0, 0, 1),
314 win3D.unlockAccess3DScene();
320 while (win3D.isOpen() && !thrPar.quit)
324 newObs->timestamp != last_obs_tim)
327 last_obs_tim = newObs->timestamp;
331 win3D.get3DSceneAndLock();
334 win3D.addTextMessage(
340 win3D.addTextMessage(
345 if (newObs->hasIntensityImage)
347 viewInt->setImageView(newObs->intensityImage);
354 win3D.unlockAccess3DScene();
364 if (newObs->hasRangeImage)
367 logger.
enter(
"RGBD->3D");
371 static pcl::PointCloud<pcl::PointXYZ> cloud;
372 logger.
enter(
"RGBD->3D.projectInto");
373 newObs->project3DPointsFromDepthImageInto(cloud,
false );
374 logger.
leave(
"RGBD->3D.projectInto");
376 win3D.get3DSceneAndLock();
377 logger.
enter(
"RGBD->3D.load in OpenGL");
378 gl_points->loadFromPointsMap(&cloud);
379 logger.
leave(
"RGBD->3D.load in OpenGL");
380 win3D.unlockAccess3DScene();
385 static pcl::PointCloud<pcl::PointXYZRGB> cloud;
386 logger.
enter(
"RGBD->3D.projectInto");
387 newObs->project3DPointsFromDepthImageInto(cloud,
false );
388 logger.
leave(
"RGBD->3D.projectInto");
390 win3D.get3DSceneAndLock();
391 logger.
enter(
"RGBD->3D.load in OpenGL");
392 gl_points->loadFromPointsMap(&cloud);
393 logger.
leave(
"RGBD->3D.load in OpenGL");
394 win3D.unlockAccess3DScene();
399 win3D.get3DSceneAndLock();
400 logger.
enter(
"RGBD->3D.projectInto");
404 newObs->project3DPointsFromDepthImageInto(*gl_points, pp);
406 logger.
leave(
"RGBD->3D.projectInto");
407 win3D.unlockAccess3DScene();
413 win3D.get3DSceneAndLock();
414 logger.
enter(
"RGBD->3D.projectInto");
415 newObs->project3DPointsFromDepthImageInto(*gl_points,
false , &globalPose);
416 logger.
leave(
"RGBD->3D.projectInto");
417 win3D.unlockAccess3DScene();
424 if (!newObs->hasPoints3D)
426 logger.
enter(
"RGBD->3D.projectInto");
427 newObs->project3DPointsFromDepthImage();
428 logger.
leave(
"RGBD->3D.projectInto");
431 CColouredPointsMap pntsMap;
432 pntsMap.colorScheme.scheme = CColouredPointsMap::cmFromIntensityImage;
433 pntsMap.loadFromRangeScan(*newObs);
435 win3D.get3DSceneAndLock();
436 logger.
enter(
"RGBD->3D.load in OpenGL");
437 gl_points->loadFromPointsMap(&pntsMap);
438 logger.
leave(
"RGBD->3D.load in OpenGL");
439 win3D.unlockAccess3DScene();
442 logger.
leave(
"RGBD->3D");
452 const int key = tolower(win3D.getPushedKey());
458 win3D.setCameraZoom(win3D.getCameraZoom() * 1.2);
462 win3D.setCameraZoom(win3D.getCameraZoom() / 1.2);
473 std::this_thread::sleep_for(1ms);
476 cout <<
"Waiting for grabbing thread to exit...\n";
482 int main(
int argc,
char** argv)
487 if (argc != 1 && argc != 2)
490 << argv[0] <<
" --> Online grab from sensor\n"
491 << argv[0] <<
" [DATASET.rawlog] --> Offline from dataset\n";
498 cout <<
"Using online operation" << endl;
504 cout <<
"Using offline operation with: " << argv[1] << endl;
508 std::this_thread::sleep_for(50ms);
511 catch (std::exception& e)
513 std::cout <<
"EXCEPCION: " << e.what() << std::endl;
518 printf(
"Another exception!!");