MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/gui.h>
12 #include <mrpt/img/TColor.h>
13 #include <mrpt/opengl.h>
14 #include <mrpt/system/CTicTac.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 
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(mrpt::opengl::CGridPlaneXY::Create());
125 
126  const double aspect_ratio = 480.0 / 640.0;
127  const int VW_WIDTH = 400; // Size of the viewport into the
128  // window, in pixel units.
129  const int VW_HEIGHT = aspect_ratio * VW_WIDTH;
130 
131  // Create an extra opengl viewport for the RGB image:
132  viewInt = scene->createViewport("view2d_int");
133  viewInt->setViewportPosition(5, 30, VW_WIDTH, VW_HEIGHT);
134  win3D.addTextMessage(
135  10, 30 + VW_HEIGHT + 10, "Intensity data", TColorf(1, 1, 1),
137 
138  win3D.addTextMessage(
139  5, 5, format("'o'/'i'-zoom out/in, ESC: quit"),
140  TColorf(0, 0, 1), 110, MRPT_GLUT_BITMAP_HELVETICA_18);
141 
142  win3D.unlockAccess3DScene();
143  win3D.repaint();
144  }
145 
146  // Grab frames continuously and show
147  //========================================================================================
148 
149  bool bObs = false, bError = true;
151 
152  while (!win3D.keyHit()) // Push any key to exit // win3D.isOpen()
153  {
154  // cout << "Get new observation\n";
157  rgbd_sensor.getNextObservation(*newObs, bObs, bError);
158 
159  if (bObs && !bError && newObs &&
160  newObs->timestamp != INVALID_TIMESTAMP &&
161  newObs->timestamp != last_obs_tim)
162  {
163  // It IS a new observation:
164  last_obs_tim = newObs->timestamp;
165 
166  // Update visualization
167  // ---------------------------------------
168 
169  win3D.get3DSceneAndLock();
170 
171  // Estimated grabbing rate:
172  win3D.addTextMessage(
173  -350, -13,
174  format(
175  "Timestamp: %s",
177  .c_str()),
178  TColorf(0.6, 0.6, 0.6), "mono", 10, mrpt::opengl::FILL,
179  100);
180 
181  // Show intensity image:
182  if (newObs->hasIntensityImage)
183  {
184  viewInt->setImageView(
185  newObs->intensityImage); // This is not "_fast"
186  // since the intensity
187  // image may be needed
188  // later on.
189  }
190  win3D.unlockAccess3DScene();
191 
192  // -------------------------------------------------------
193  // Create 3D points from RGB+D data
194  //
195  // There are several methods to do this.
196  // Switch the #if's to select among the options:
197  // See also:
198  // https://www.mrpt.org/Generating_3D_point_clouds_from_RGB_D_observations
199  // -------------------------------------------------------
200  if (newObs->hasRangeImage)
201  {
202 #if 0
203  static pcl::PointCloud<pcl::PointXYZRGB> cloud;
204  newObs->project3DPointsFromDepthImageInto(cloud, false /* without obs.sensorPose */);
205 
206  win3D.get3DSceneAndLock();
207  gl_points->loadFromPointsMap(&cloud);
208  win3D.unlockAccess3DScene();
209 #endif
210 
211 // Pathway: RGB+D --> XYZ+RGB opengl
212 #if 1
213  win3D.get3DSceneAndLock();
216  newObs->project3DPointsFromDepthImageInto(
217  *gl_points, pp /* without obs.sensorPose */);
218  win3D.unlockAccess3DScene();
219 #endif
220  }
221 
222  win3D.repaint();
223  } // end update visualization:
224  }
225  }
226 
227  cout << "\nClosing RGBD sensor...\n";
228 
229  return 0;
230  }
231  catch (const std::exception& e)
232  {
233  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
234  return -1;
235  }
236  catch (...)
237  {
238  printf("Untyped exception!!");
239  return -1;
240  }
241 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
int getNumDevices() const
The number of available devices at initialization.
static Ptr Create(Args &&... args)
A high-performance stopwatch, with typical resolution of nanoseconds.
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().
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.
Contains classes for various device interfaces.
STL namespace.
A class for grabing "range images", intensity images (either RGB or IR) and other information from an...
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
void setSerialToOpen(const unsigned serial)
Set the serial number of the device to open.
This class creates a window as a graphical user interface (GUI) for displaying images to the user...
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
This namespace contains representation of robot actions and observations.
mrpt::gui::CDisplayWindow3D::Ptr win
void setSensorIDToOpen(const unsigned sensor_id)
Set the sensor_id of the device to open.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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.
Definition: format.cpp:16
Used in CObservation3DRangeScan::project3DPointsFromDepthImageInto()
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:394
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...
Definition: exceptions.cpp:59
void initialize() override
Initializes the 3D camera - should be invoked after calling loadConfig() or setting the different par...
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
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.
Definition: datetime.cpp:170
renders glyphs as filled polygons
Definition: opengl_fonts.h:35
static Ptr Create(Args &&... args)
Definition: CGridPlaneXY.h:31
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:43
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.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019