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;
152 auto obs = CObservation3DRangeScan::Create();
153 kinect->getNextObservation(*obs, there_is_obs, hard_error);
156 throw std::runtime_error(
157 "Sensor returned 'hardware error'");
158 else if (there_is_obs)
161 std::atomic_store(&
p.new_obs, obs);
174 catch (
const std::exception& e)
176 throw std::runtime_error(
178 "\nError reading from dataset file (EOF?):\n") +
197 #ifndef FAKE_KINECT_FPS_RATE 198 const double At_dataset =
201 const double At_dataset = 1.0 / FAKE_KINECT_FPS_RATE;
204 my_last_read_obs_tim, now_tim);
206 const double need_to_wait_ms =
207 1000. * (At_dataset - At_actual);
210 if (need_to_wait_ms > 0)
211 std::this_thread::sleep_for(
212 std::chrono::duration<double, std::milli>(
217 std::atomic_store(&
p.new_obs, obs3D);
219 dataset_prev_tim = cur_tim;
227 p.Hz = nImgs / tictac.
Tac();
233 catch (
const std::exception& e)
235 cout <<
"Exception in Kinect thread: " << e.what() << endl;
244 bool is_online,
const string& rawlog_file =
string())
249 is_online, rawlog_file,
258 cout <<
"Waiting for sensor initialization...\n";
265 std::this_thread::sleep_for(10ms);
266 }
while (!thrPar.quit);
269 if (thrPar.quit)
return;
270 cout <<
"OK! Sensor started to emit observations.\n";
276 win3D.setCameraAzimuthDeg(140);
277 win3D.setCameraElevationDeg(20);
278 win3D.setCameraZoom(8.0);
280 win3D.setCameraPointingToPoint(2.5, 0, 0);
284 gl_points->setPointSize(2.5);
292 scene->insert(gl_points);
296 const double aspect_ratio = 480.0 / 640.0;
299 const int VW_HEIGHT = aspect_ratio * VW_WIDTH;
302 viewInt = scene->createViewport(
"view2d_int");
303 viewInt->setViewportPosition(5, 30, VW_WIDTH, VW_HEIGHT);
304 win3D.addTextMessage(
305 10, 30 + VW_HEIGHT + 10,
"Intensity data",
TColorf(1, 1, 1), 2,
308 win3D.addTextMessage(
309 5, 5,
format(
"'o'/'i'-zoom out/in, ESC: quit"),
TColorf(0, 0, 1),
312 win3D.unlockAccess3DScene();
318 while (win3D.isOpen() && !thrPar.quit)
322 newObs->timestamp != last_obs_tim)
325 last_obs_tim = newObs->timestamp;
329 win3D.get3DSceneAndLock();
332 win3D.addTextMessage(
338 win3D.addTextMessage(
343 if (newObs->hasIntensityImage)
345 viewInt->setImageView(newObs->intensityImage);
352 win3D.unlockAccess3DScene();
362 if (newObs->hasRangeImage)
365 logger.
enter(
"RGBD->3D");
369 static pcl::PointCloud<pcl::PointXYZ> cloud;
370 logger.
enter(
"RGBD->3D.projectInto");
371 newObs->project3DPointsFromDepthImageInto(cloud,
false );
372 logger.
leave(
"RGBD->3D.projectInto");
374 win3D.get3DSceneAndLock();
375 logger.
enter(
"RGBD->3D.load in OpenGL");
376 gl_points->loadFromPointsMap(&cloud);
377 logger.
leave(
"RGBD->3D.load in OpenGL");
378 win3D.unlockAccess3DScene();
383 static pcl::PointCloud<pcl::PointXYZRGB> cloud;
384 logger.
enter(
"RGBD->3D.projectInto");
385 newObs->project3DPointsFromDepthImageInto(cloud,
false );
386 logger.
leave(
"RGBD->3D.projectInto");
388 win3D.get3DSceneAndLock();
389 logger.
enter(
"RGBD->3D.load in OpenGL");
390 gl_points->loadFromPointsMap(&cloud);
391 logger.
leave(
"RGBD->3D.load in OpenGL");
392 win3D.unlockAccess3DScene();
397 win3D.get3DSceneAndLock();
398 logger.
enter(
"RGBD->3D.projectInto");
402 newObs->project3DPointsFromDepthImageInto(*gl_points, pp);
404 logger.
leave(
"RGBD->3D.projectInto");
405 win3D.unlockAccess3DScene();
411 win3D.get3DSceneAndLock();
412 logger.
enter(
"RGBD->3D.projectInto");
413 newObs->project3DPointsFromDepthImageInto(*gl_points,
false , &globalPose);
414 logger.
leave(
"RGBD->3D.projectInto");
415 win3D.unlockAccess3DScene();
422 if (!newObs->hasPoints3D)
424 logger.
enter(
"RGBD->3D.projectInto");
425 newObs->project3DPointsFromDepthImage();
426 logger.
leave(
"RGBD->3D.projectInto");
429 CColouredPointsMap pntsMap;
430 pntsMap.colorScheme.scheme = CColouredPointsMap::cmFromIntensityImage;
431 pntsMap.loadFromRangeScan(*newObs);
433 win3D.get3DSceneAndLock();
434 logger.
enter(
"RGBD->3D.load in OpenGL");
435 gl_points->loadFromPointsMap(&pntsMap);
436 logger.
leave(
"RGBD->3D.load in OpenGL");
437 win3D.unlockAccess3DScene();
440 logger.
leave(
"RGBD->3D");
450 const int key = tolower(win3D.getPushedKey());
456 win3D.setCameraZoom(win3D.getCameraZoom() * 1.2);
460 win3D.setCameraZoom(win3D.getCameraZoom() / 1.2);
471 std::this_thread::sleep_for(1ms);
474 cout <<
"Waiting for grabbing thread to exit...\n";
480 int main(
int argc,
char** argv)
485 if (argc != 1 && argc != 2)
488 << argv[0] <<
" --> Online grab from sensor\n" 489 << argv[0] <<
" [DATASET.rawlog] --> Offline from dataset\n";
496 cout <<
"Using online operation" << endl;
502 cout <<
"Using offline operation with: " << argv[1] << endl;
506 std::this_thread::sleep_for(50ms);
509 catch (
const std::exception& e)
516 printf(
"Another exception!!");
double Tac() noexcept
Stops the stopwatch.
static Ptr Create(Args &&... args)
double DEG2RAD(const double x)
Degrees to radians.
A high-performance stopwatch, with typical resolution of nanoseconds.
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement, as from a time-of-flight range camera or any other RGBD sensor.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Contains classes for various device interfaces.
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream, std::istream, std::ostream, std::stringstream.
#define ASSERT_(f)
Defines an assertion mechanism.
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
A class for grabing "range images", intensity images (either RGB or IR) and other information from an...
This namespace contains representation of robot actions and observations.
void Test_KinectOnlineOffline(bool is_online, const string &rawlog_file=string())
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void thread_grabbing(TThreadParam &p)
Used in CObservation3DRangeScan::project3DPointsFromDepthImageInto()
A RGB color - floats in the range [0,1].
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Classes for creating GUI windows for 2D and 3D visualization.
double timeDifference(const mrpt::system::TTimeStamp t_first, const mrpt::system::TTimeStamp t_later)
Returns the time difference from t1 to t2 (positive if t2 is posterior to t1), in seconds...
void Tic() noexcept
Starts the stopwatch.
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
renders glyphs as filled polygons
static Ptr Create(Args &&... args)
void enter(const std::string_view &func_name)
Start of a named section.
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
double leave(const std::string_view &func_name)
End of a named section.
bool takeIntoAccountSensorPoseOnRobot
(Default: false) If false, local (sensor-centric) coordinates of points are generated.
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.