Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include <mrpt/gui.h>
12 #include <mrpt/system/CTicTac.h>
13 #include <mrpt/opengl.h>
14 #include <mrpt/opengl/CPlanarLaserScan.h> // This class is in mrpt-maps
15 #include <mrpt/img/TColor.h>
16 #include <iostream>
17 
18 using namespace mrpt;
19 using namespace mrpt::obs;
20 using namespace mrpt::opengl;
21 using namespace mrpt::hwdrivers;
22 using namespace mrpt::img;
23 using namespace std;
24 
25 const float vert_FOV = DEG2RAD(4.0);
26 
27 // This demo records from an OpenNI2 device, convert observations to 2D scans
28 // and runs 2d-icp-slam with them.
29 
30 int main(int argc, char** argv)
31 {
32  try
33  {
34  if (argc > 2)
35  {
36  cerr << "Usage: " << argv[0] << " <sensor_id/sensor_serial\n";
37  cerr << "Example: " << argv[0] << " 0 \n";
38  return 1;
39  }
40 
41  // const unsigned sensor_id = 0;
42  COpenNI2Sensor rgbd_sensor;
43  // rgbd_sensor.loadConfig_sensorSpecific(const
44  // mrpt::config::CConfigFileBase &configSource, const std::string
45  // &iniSection );
46 
47  unsigned sensor_id_or_serial = 0;
48  if (argc == 2)
49  {
50  sensor_id_or_serial = atoi(argv[1]);
51  if (sensor_id_or_serial > 10)
52  rgbd_sensor.setSerialToOpen(sensor_id_or_serial);
53  else
54  rgbd_sensor.setSensorIDToOpen(sensor_id_or_serial);
55  }
56 
57  // Open:
58  // cout << "Calling COpenNI2Sensor::initialize()...";
59  rgbd_sensor.initialize();
60 
61  if (rgbd_sensor.getNumDevices() == 0) return 0;
62 
63  cout << "OK " << rgbd_sensor.getNumDevices() << " available devices."
64  << endl;
65  cout << "\nUse device " << sensor_id_or_serial << endl << endl;
66 
67  // Create window and prepare OpenGL object in the scene:
68  // --------------------------------------------------------
69  mrpt::gui::CDisplayWindow3D win3D("OpenNI2 3D view", 800, 600);
70 
71  win3D.setCameraAzimuthDeg(140);
72  win3D.setCameraElevationDeg(20);
73  win3D.setCameraZoom(8.0);
74  win3D.setFOV(90);
75  win3D.setCameraPointingToPoint(2.5, 0, 0);
76 
78  mrpt::make_aligned_shared<mrpt::opengl::CPointCloudColoured>();
79  gl_points->setPointSize(2.5);
80 
81  // The 2D "laser scan" OpenGL object:
83  mrpt::make_aligned_shared<mrpt::opengl::CPlanarLaserScan>();
84  gl_2d_scan->enablePoints(true);
85  gl_2d_scan->enableLine(true);
86  gl_2d_scan->enableSurface(true);
87  gl_2d_scan->setSurfaceColor(0, 0, 1, 0.3); // RGBA
88 
90  viewInt; // Extra viewports for the RGB images.
91  {
92  mrpt::opengl::COpenGLScene::Ptr& scene = win3D.get3DSceneAndLock();
93 
94  // Create the Opengl object for the point cloud:
95  scene->insert(gl_points);
96  scene->insert(
97  mrpt::make_aligned_shared<mrpt::opengl::CGridPlaneXY>());
99  scene->insert(gl_2d_scan);
100 
101  const double aspect_ratio = 480.0 / 640.0;
102  const int VW_WIDTH =
103  400; // Size of the viewport into the window, in pixel units.
104  const int VW_HEIGHT = aspect_ratio * VW_WIDTH;
105 
106  // Create an extra opengl viewport for the RGB image:
107  viewInt = scene->createViewport("view2d_int");
108  viewInt->setViewportPosition(5, 30, VW_WIDTH, VW_HEIGHT);
109  win3D.addTextMessage(
110  10, 30 + VW_HEIGHT + 10, "Intensity data", TColorf(1, 1, 1), 2,
112 
113  win3D.addTextMessage(
114  5, 5, format("'o'/'i'-zoom out/in, ESC: quit"),
115  TColorf(0, 0, 1), 110, MRPT_GLUT_BITMAP_HELVETICA_18);
116 
117  win3D.unlockAccess3DScene();
118  win3D.repaint();
119  }
120 
121  // Grab frames continuously and show
122  //========================================================================================
123 
124  bool bObs = false, bError = true;
126 
127  while (!win3D.keyHit()) // Push any key to exit // win3D.isOpen()
128  {
129  // cout << "Get new observation\n";
131  mrpt::make_aligned_shared<CObservation3DRangeScan>();
132  rgbd_sensor.getNextObservation(*newObs, bObs, bError);
133 
134  CObservation2DRangeScan::Ptr obs_2d; // The equivalent 2D scan
135 
136  if (bObs && !bError && newObs &&
137  newObs->timestamp != INVALID_TIMESTAMP &&
138  newObs->timestamp != last_obs_tim)
139  {
140  // It IS a new observation:
141  last_obs_tim = newObs->timestamp;
142 
143  // Convert ranges to an equivalent 2D "fake laser" scan:
144  if (newObs->hasRangeImage)
145  {
146  // Convert to scan:
147  obs_2d =
148  mrpt::make_aligned_shared<CObservation2DRangeScan>();
149 
151  p2s.angle_sup = .5f * vert_FOV;
152  p2s.angle_inf = .5f * vert_FOV;
153  p2s.sensorLabel = "KINECT_2D_SCAN";
154  newObs->convertTo2DScan(*obs_2d, p2s);
155  }
156 
157  // Update visualization ---------------------------------------
158 
159  win3D.get3DSceneAndLock();
160 
161  // Estimated grabbing rate:
162  win3D.addTextMessage(
163  -350, -13,
164  format(
165  "Timestamp: %s",
167  .c_str()),
168  TColorf(0.6, 0.6, 0.6), "mono", 10, mrpt::opengl::FILL,
169  100);
170 
171  // Show intensity image:
172  if (newObs->hasIntensityImage)
173  {
174  viewInt->setImageView(
175  newObs->intensityImage); // This is not "_fast" since
176  // the intensity image may be
177  // needed later on.
178  }
179  win3D.unlockAccess3DScene();
180 
181  // -------------------------------------------------------
182  // Create 3D points from RGB+D data
183  //
184  // There are several methods to do this.
185  // Switch the #if's to select among the options:
186  // See also:
187  // http://www.mrpt.org/Generating_3D_point_clouds_from_RGB_D_observations
188  // -------------------------------------------------------
189  if (newObs->hasRangeImage)
190  {
191  // Pathway: RGB+D --> XYZ+RGB opengl
192  win3D.get3DSceneAndLock();
195  newObs->project3DPointsFromDepthImageInto(
196  *gl_points, pp /* without obs.sensorPose */);
197  win3D.unlockAccess3DScene();
198  }
199 
200  // And load scan in the OpenGL object:
201  gl_2d_scan->setScan(*obs_2d);
202 
203  win3D.repaint();
204  } // end update visualization:
205  }
206 
207  cout << "\nClosing RGBD sensor...\n";
208 
209  return 0;
210  }
211  catch (std::exception& e)
212  {
213  std::cout << "MRPT exception caught: " << e.what() << std::endl;
214  return -1;
215  }
216  catch (...)
217  {
218  printf("Untyped exception!!");
219  return -1;
220  }
221 }
mrpt::hwdrivers::COpenNI2Generic::getNumDevices
int getNumDevices() const
The number of available devices at initialization.
Definition: COpenNI2Generic.cpp:155
mrpt::obs::T3DPointsTo2DScanParams::sensorLabel
std::string sensorLabel
The sensor label that will have the newly created observation.
Definition: CObservation3DRangeScan.h:63
mrpt::system::dateTimeLocalToString
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS....
Definition: datetime.cpp:270
mrpt::obs::T3DPointsProjectionParams
Used in CObservation3DRangeScan::project3DPointsFromDepthImageInto()
Definition: CObservation3DRangeScan.h:30
mrpt::hwdrivers::COpenNI2Sensor::initialize
virtual void initialize()
Initializes the 3D camera - should be invoked after calling loadConfig() or setting the different par...
Definition: COpenNI2Sensor.cpp:93
INVALID_TIMESTAMP
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:15
COpenNI2Sensor.h
mrpt::hwdrivers::COpenNI2Sensor::getNextObservation
void getNextObservation(mrpt::obs::CObservation3DRangeScan &out_obs, bool &there_is_obs, bool &hardware_error)
The main data retrieving function, to be called after calling loadConfig() and initialize().
Definition: COpenNI2Sensor.cpp:285
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
mrpt::obs::T3DPointsTo2DScanParams::angle_sup
double angle_sup
(Default=5 degrees) [Only if use_origin_sensor_pose=false] The upper & lower half-FOV angle (in radia...
Definition: CObservation3DRangeScan.h:66
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::hwdrivers::COpenNI2Sensor::setSerialToOpen
void setSerialToOpen(const unsigned serial)
Set the serial number of the device to open.
Definition: COpenNI2Sensor.h:236
mrpt::opengl::CPointCloudColoured::Ptr
std::shared_ptr< CPointCloudColoured > Ptr
Definition: CPointCloudColoured.h:49
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_12
@ MRPT_GLUT_BITMAP_HELVETICA_12
Definition: opengl_fonts.h:31
mrpt::obs::T3DPointsTo2DScanParams
Used in CObservation3DRangeScan::convertTo2DScan()
Definition: CObservation3DRangeScan.h:60
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_18
@ MRPT_GLUT_BITMAP_HELVETICA_18
Definition: opengl_fonts.h:32
mrpt::system::TTimeStamp
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1,...
Definition: datetime.h:31
vert_FOV
const float vert_FOV
Definition: vision_stereo_rectify/test.cpp:25
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::obs::T3DPointsTo2DScanParams::angle_inf
double angle_inf
Definition: CObservation3DRangeScan.h:66
mrpt::img
Definition: CCanvas.h:17
mrpt::hwdrivers::COpenNI2Sensor::setSensorIDToOpen
void setSensorIDToOpen(const unsigned sensor_id)
Set the sensor_id of the device to open.
Definition: COpenNI2Sensor.h:245
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
mrpt::opengl::CPlanarLaserScan::Ptr
std::shared_ptr< CPlanarLaserScan > Ptr
Definition: CPlanarLaserScan.h:59
mrpt::opengl::FILL
@ FILL
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
mrpt::obs::CObservation2DRangeScan::Ptr
std::shared_ptr< CObservation2DRangeScan > Ptr
Definition: CObservation2DRangeScan.h:58
mrpt::opengl::stock_objects::CornerXYZ
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
Definition: StockObjects.cpp:209
opengl.h
CTicTac.h
mrpt::obs::CObservation3DRangeScan::Ptr
std::shared_ptr< CObservation3DRangeScan > Ptr
Definition: CObservation3DRangeScan.h:226
gui.h
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
CPlanarLaserScan.h
mrpt::obs::T3DPointsProjectionParams::takeIntoAccountSensorPoseOnRobot
bool takeIntoAccountSensorPoseOnRobot
(Default: false) If false, local (sensor-centric) coordinates of points are generated.
Definition: CObservation3DRangeScan.h:36
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< COpenGLViewport > Ptr
Definition: COpenGLViewport.h:63
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
TColor.h
mrpt::hwdrivers::COpenNI2Sensor
A class for grabing "range images", intensity images (either RGB or IR) and other information from an...
Definition: COpenNI2Sensor.h:219
mrpt::DEG2RAD
double DEG2RAD(const double x)
Degrees to radians.
Definition: core/include/mrpt/core/bits_math.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST