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/img/TColor.h>
15 #include <iostream>
16 
17 using namespace mrpt;
18 using namespace mrpt::obs;
19 using namespace mrpt::opengl;
20 using namespace mrpt::hwdrivers;
21 using namespace mrpt::system;
22 using namespace mrpt::img;
23 using namespace std;
24 
25 // This simple demo records form an OpenNI2 device into a rawlog as 3D
26 // observations. It's meant as a temporary workaround before OpenNI2 is
27 // integrated as a generic sensor so that it works with rawlog-grabber.
28 
29 int main(int argc, char** argv)
30 {
31  try
32  {
33  if (argc > 2)
34  {
35  cerr << "Usage: " << argv[0] << " <sensor_id/sensor_serial\n";
36  cerr << "Example: " << argv[0] << " 0 \n";
37  return 1;
38  }
39 
40  // const unsigned sensor_id = 0;
41  COpenNI2Sensor rgbd_sensor;
42  // rgbd_sensor.loadConfig_sensorSpecific(const
43  // mrpt::config::CConfigFileBase &configSource, const std::string
44  // &iniSection );
45 
46  unsigned sensor_id_or_serial = 0;
47  // const string configFile = std::string( argv[2] );
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  bool showImages = false;
68  if (showImages)
69  {
71 
72  CTicTac tictac;
73  tictac.Tic();
74  unsigned int nFrames = 0;
76  bool bObs = false, bError = true;
77  rgbd_sensor.getNextObservation(newObs, bObs, bError);
78 
79  while (!system::os::kbhit())
80  {
81  cout << "Get a new frame\n";
82  rgbd_sensor.getNextObservation(newObs, bObs, bError);
83 
84  double fps = ++nFrames / tictac.Tac();
85  // newObs->intensityImage.textOut(5,5,format("%.02f
86  // fps",fps),TColor(0x80,0x80,0x80) );
87  cout << "FPS: " << fps << endl;
88 
89  if (nFrames > 100)
90  {
91  tictac.Tic();
92  nFrames = 0;
93  }
94 
95  win.showImage(newObs.intensityImage);
96  std::this_thread::sleep_for(10ms);
97  }
98  }
99  else // Show point cloud and images
100  {
101  // Create window and prepare OpenGL object in the scene:
102  // --------------------------------------------------------
103  mrpt::gui::CDisplayWindow3D win3D("OpenNI2 3D view", 800, 600);
104 
105  win3D.setCameraAzimuthDeg(140);
106  win3D.setCameraElevationDeg(20);
107  win3D.setCameraZoom(8.0);
108  win3D.setFOV(90);
109  win3D.setCameraPointingToPoint(2.5, 0, 0);
110 
112  mrpt::make_aligned_shared<mrpt::opengl::CPointCloudColoured>();
113  gl_points->setPointSize(2.5);
114 
116  viewInt; // Extra viewports for the RGB images.
117  {
119  win3D.get3DSceneAndLock();
120 
121  // Create the Opengl object for the point cloud:
122  scene->insert(gl_points);
123  scene->insert(
124  mrpt::make_aligned_shared<mrpt::opengl::CGridPlaneXY>());
126 
127  const double aspect_ratio = 480.0 / 640.0;
128  const int VW_WIDTH = 400; // Size of the viewport into the
129  // window, in pixel units.
130  const int VW_HEIGHT = aspect_ratio * VW_WIDTH;
131 
132  // Create an extra opengl viewport for the RGB image:
133  viewInt = scene->createViewport("view2d_int");
134  viewInt->setViewportPosition(5, 30, VW_WIDTH, VW_HEIGHT);
135  win3D.addTextMessage(
136  10, 30 + VW_HEIGHT + 10, "Intensity data", TColorf(1, 1, 1),
138 
139  win3D.addTextMessage(
140  5, 5, format("'o'/'i'-zoom out/in, ESC: quit"),
141  TColorf(0, 0, 1), 110, MRPT_GLUT_BITMAP_HELVETICA_18);
142 
143  win3D.unlockAccess3DScene();
144  win3D.repaint();
145  }
146 
147  // Grab frames continuously and show
148  //========================================================================================
149 
150  bool bObs = false, bError = true;
152 
153  while (!win3D.keyHit()) // Push any key to exit // win3D.isOpen()
154  {
155  // cout << "Get new observation\n";
157  mrpt::make_aligned_shared<CObservation3DRangeScan>();
158  rgbd_sensor.getNextObservation(*newObs, bObs, bError);
159 
160  if (bObs && !bError && newObs &&
161  newObs->timestamp != INVALID_TIMESTAMP &&
162  newObs->timestamp != last_obs_tim)
163  {
164  // It IS a new observation:
165  last_obs_tim = newObs->timestamp;
166 
167  // Update visualization
168  // ---------------------------------------
169 
170  win3D.get3DSceneAndLock();
171 
172  // Estimated grabbing rate:
173  win3D.addTextMessage(
174  -350, -13,
175  format(
176  "Timestamp: %s",
178  .c_str()),
179  TColorf(0.6, 0.6, 0.6), "mono", 10, mrpt::opengl::FILL,
180  100);
181 
182  // Show intensity image:
183  if (newObs->hasIntensityImage)
184  {
185  viewInt->setImageView(
186  newObs->intensityImage); // This is not "_fast"
187  // since the intensity
188  // image may be needed
189  // later on.
190  }
191  win3D.unlockAccess3DScene();
192 
193  // -------------------------------------------------------
194  // Create 3D points from RGB+D data
195  //
196  // There are several methods to do this.
197  // Switch the #if's to select among the options:
198  // See also:
199  // http://www.mrpt.org/Generating_3D_point_clouds_from_RGB_D_observations
200  // -------------------------------------------------------
201  if (newObs->hasRangeImage)
202  {
203 #if 0
204  static pcl::PointCloud<pcl::PointXYZRGB> cloud;
205  newObs->project3DPointsFromDepthImageInto(cloud, false /* without obs.sensorPose */);
206 
207  win3D.get3DSceneAndLock();
208  gl_points->loadFromPointsMap(&cloud);
209  win3D.unlockAccess3DScene();
210 #endif
211 
212 // Pathway: RGB+D --> XYZ+RGB opengl
213 #if 1
214  win3D.get3DSceneAndLock();
217  newObs->project3DPointsFromDepthImageInto(
218  *gl_points, pp /* without obs.sensorPose */);
219  win3D.unlockAccess3DScene();
220 #endif
221  }
222 
223  win3D.repaint();
224  } // end update visualization:
225  }
226  }
227 
228  cout << "\nClosing RGBD sensor...\n";
229 
230  return 0;
231  }
232  catch (std::exception& e)
233  {
234  std::cout << "MRPT exception caught: " << e.what() << std::endl;
235  return -1;
236  }
237  catch (...)
238  {
239  printf("Untyped exception!!");
240  return -1;
241  }
242 }
mrpt::hwdrivers::COpenNI2Generic::getNumDevices
int getNumDevices() const
The number of available devices at initialization.
Definition: COpenNI2Generic.cpp:155
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
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::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
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
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
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
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::img
Definition: CCanvas.h:17
mrpt::obs::CObservation3DRangeScan::intensityImage
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage".
Definition: CObservation3DRangeScan.h:502
mrpt::obs::CObservation3DRangeScan
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement,...
Definition: CObservation3DRangeScan.h:224
mrpt::hwdrivers::COpenNI2Sensor::setSensorIDToOpen
void setSensorIDToOpen(const unsigned sensor_id)
Set the sensor_id of the device to open.
Definition: COpenNI2Sensor.h:245
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
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::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:30
mrpt::opengl::FILL
@ FILL
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
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
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::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



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