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 
10 /**
11  * rayTrace
12  * Ray tracing is a technique for generating an image by tracing the path of
13  * light through pixels in an image plane and simulating the effects of its
14  * encounters with virtual objects
15  *
16  */
17 
18 #include <mrpt/gui.h>
19 #include <mrpt/random.h>
20 #include <mrpt/poses/CPose3D.h>
21 #include <mrpt/poses/CPoint3D.h>
22 #include <mrpt/system/CTicTac.h>
24 #include <mrpt/opengl/CAxis.h>
25 #include <mrpt/opengl/CDisk.h>
26 #include <mrpt/opengl/CCylinder.h>
27 #include <mrpt/opengl/CSphere.h>
30 #include <mrpt/opengl/CEllipsoid.h>
32 #include <iostream>
33 
34 #define COLORR 1.0f
35 #define COLORG 0.0f
36 #define COLORB 0.0f
37 
38 #define GRID_R 1.0f
39 #define GRID_G 1.0f
40 #define GRID_B 1.0f
41 
42 using namespace std;
43 using namespace mrpt;
44 using namespace mrpt::gui;
45 using namespace mrpt::opengl;
46 using namespace mrpt::poses;
47 using namespace mrpt::math;
48 using namespace mrpt::random;
49 using namespace mrpt::serialization;
50 using namespace mrpt::system;
51 
53 
54 // Increase this values to get more precision. It will also increase run time.
55 const size_t HOW_MANY_YAWS = 150;
56 const size_t HOW_MANY_PITCHS = 75;
57 
58 const float RANDOM_POSE_DISTANCE = 10;
59 
60 inline double MYRAND1()
61 {
62  // return static_cast<float>(rand()%prec)/prec;
63  return getRandomGenerator().drawUniform(0, 1);
64 }
65 
66 inline double MYRANDG(double scale, double shift = 0)
67 {
68  // return shift+(static_cast<float>(rand()%prec)/prec)*scale;
69  return shift + scale * getRandomGenerator().drawUniform(0, 1);
70 }
71 
73 {
74  return CPose3D(
78  MYRAND1(), MYRAND1());
79 }
80 
81 /**
82  * Call configRandom given the address of an object and assign random pose and
83  * color to it
84  */
86 {
87  obj->setColor(MYRAND1(), MYRAND1(), MYRAND1(), MYRANDG(0.75, 0.25));
88  obj->setPose(randomPose());
89 }
90 
91 void guideLines(const CPose3D& base, CSetOfLines::Ptr& lines, float dist)
92 {
93  CPoint3D pDist = CPoint3D(dist, 0, 0);
94  CPoint3D pps[4];
95  pps[0] = base + pDist;
96  pps[1] = base + CPose3D(0, 0, 0, 0, -M_PI / 2, 0) + pDist;
97  pps[2] = base + CPose3D(0, 0, 0, -M_PI / 2, 0, 0) + pDist;
98  pps[3] = base + CPose3D(0, 0, 0, M_PI / 2, 0, 0) + pDist;
99  for (size_t i = 0; i < 4; i++)
100  lines->appendLine(
101  base.x(), base.y(), base.z(), pps[i].x(), pps[i].y(), pps[i].z());
102  lines->setLineWidth(5);
103  lines->setColor(0, 0, 1);
104 }
105 
106 // Add objects at your will to check results
108 {
109  // create object, give it a random pose/color, insert it in the world
110  CDisk::Ptr dsk = mrpt::make_aligned_shared<CDisk>();
111  dsk->setDiskRadius(MYRANDG(5, 5), MYRANDG(5));
112  configRandom(dsk);
113  world->insert(dsk);
114 
115  CSphere::Ptr sph = mrpt::make_aligned_shared<CSphere>(MYRANDG(5, 1));
116  configRandom(sph);
117  world->insert(sph);
118 
119  CTexturedPlane::Ptr pln = mrpt::make_aligned_shared<CTexturedPlane>(
120  MYRANDG(10, -10), MYRANDG(10), MYRANDG(10, -10), MYRANDG(10));
121  configRandom(pln);
122  world->insert(pln);
123 
124  for (size_t i = 0; i < 5; i++)
125  {
126  CPolyhedron::Ptr poly =
127  CPolyhedron::CreateRandomPolyhedron(MYRANDG(2, 2));
128  configRandom(poly);
129  world->insert(poly);
130  }
131 
132  CCylinder::Ptr cil = mrpt::make_aligned_shared<CCylinder>(
133  MYRANDG(3.0, 3.0), MYRANDG(3.0, 1.0), MYRANDG(2.0f, 3.0f), 50, 1);
134  configRandom(cil);
135  world->insert(cil);
136 
137  CEllipsoid::Ptr ell = mrpt::make_aligned_shared<CEllipsoid>();
138  CMatrixDouble md = CMatrixDouble(3, 3);
139  for (size_t i = 0; i < 3; i++) md(i, i) = MYRANDG(8.0, 1.0);
140  for (size_t i = 0; i < 3; i++)
141  {
142  size_t ii = (i + 1) % 3;
143  md(i, ii) = md(ii, i) = MYRANDG(sqrt(md(i, i) * md(ii, ii)));
144  }
145  ell->setCovMatrix(md);
146  configRandom(std::dynamic_pointer_cast<CRenderizable>(ell));
147  world->insert(ell);
148 }
149 
150 void display()
151 {
152  CDisplayWindow3D window("Ray trace demo", 640, 480);
153  window.setPos(10, 10);
154  std::this_thread::sleep_for(20ms);
155  COpenGLScene::Ptr scene1 = mrpt::make_aligned_shared<COpenGLScene>();
156  // COpenGLScene::Ptr &scene1=window.get3DSceneAndLock();
158  mrpt::make_aligned_shared<CGridPlaneXY>(-20, 20, -20, 20, 0, 1);
159  plane1->setColor(GRID_R, GRID_G, GRID_B);
160  scene1->insert(plane1);
161  scene1->insert(
162  mrpt::make_aligned_shared<CAxis>(-5, -5, -5, 5, 5, 5, 2.5, 3, true));
163  CSetOfObjects::Ptr world = mrpt::make_aligned_shared<CSetOfObjects>();
164  generateObjects(world);
165  scene1->insert(world);
166  CPose3D basePose = randomPose();
168  mrpt::make_aligned_shared<CAngularObservationMesh>();
169  CTicTac t;
170  t.Tic();
171  CAngularObservationMesh::trace2DSetOfRays(
172  scene1, basePose, aom,
173  CAngularObservationMesh::TDoubleRange::CreateFromAmount(
174  -M_PI / 2, 0, HOW_MANY_PITCHS),
175  CAngularObservationMesh::TDoubleRange::CreateFromAperture(
176  M_PI, HOW_MANY_YAWS));
177  cout << "Elapsed time: " << t.Tac() << " seconds.\n";
178  aom->setColor(0, 1, 0);
179  aom->setWireframe(true);
180  // Comment to stop showing traced rays and scan range guidelines.
181  CSetOfLines::Ptr traced = mrpt::make_aligned_shared<CSetOfLines>();
182  CSetOfLines::Ptr guides = mrpt::make_aligned_shared<CSetOfLines>();
183  aom->getTracedRays(traced);
184  traced->setLineWidth(1.5);
185  traced->setColor(1, 0, 0);
186  guideLines(basePose, guides, 10);
187  scene1->insert(traced);
188  scene1->insert(guides);
189 
190  // Uncomment to show also traced rays who got lost.
191  /*
192  CSetOfLines::Ptr untraced=mrpt::make_aligned_shared<CSetOfLines>();
193  aom->getUntracedRays(untraced,20);
194  untraced->setLineWidth(1);
195  untraced->setColor(1,1,1,0.5);
196  scene1->insert(untraced);
197  */
198  CSphere::Ptr point = mrpt::make_aligned_shared<CSphere>(0.2);
199  point->setColor(0, 1, 0);
200  point->setPose(basePose);
201  scene1->insert(point);
202  CDisplayWindow3D window2("Observed mesh", 640, 480);
203  window2.setPos(660, 10);
204  std::this_thread::sleep_for(20ms);
205  window.get3DSceneAndLock() = scene1;
206  window.unlockAccess3DScene();
207  window.setCameraElevationDeg(25.0f);
208  COpenGLScene::Ptr& scene2 = window2.get3DSceneAndLock();
209  scene2->insert(aom);
211  mrpt::make_aligned_shared<CGridPlaneXY>(-20, 20, -20, 20, 0, 1);
212  plane2->setColor(GRID_R, GRID_G, GRID_B);
213  scene2->insert(plane2);
214  scene2->insert(
215  mrpt::make_aligned_shared<CAxis>(-5, -5, -5, 5, 5, 5, 2.5, 3, true));
216  window2.unlockAccess3DScene();
217  window2.setCameraElevationDeg(25.0f);
218 
219  window.waitForKey();
220 }
221 
222 int main()
223 {
225  try
226  {
227  display();
228  return 0;
229  }
230  catch (const exception& e)
231  {
232  cout << "Error: " << e.what() << '.' << endl;
234  return -1;
235  }
236  catch (...)
237  {
238  cout << "Unknown Error.\n";
239  return -1;
240  }
241 }
A namespace of pseudo-random numbers generators of diferent distributions.
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
A mesh built from a set of 2D laser scan observations.
GLdouble GLdouble t
Definition: glext.h:3689
const double GRID_B
const double GRID_R
const float RANDOM_POSE_DISTANCE
double MYRANDG(double scale, double shift=0)
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:6502
void configRandom(const CRenderizable::Ptr &obj)
Call configRandom given the address of an object and assign random pose and color to it...
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
void guideLines(const CPose3D &base, CSetOfLines::Ptr &lines, float dist)
A high-performance stopwatch, with typical resolution of nanoseconds.
STL namespace.
void generateObjects(CSetOfObjects::Ptr &world)
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
This base provides a set of functions for maths stuff.
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
CPose3D randomPose()
const double GRID_G
A class used to store a 3D point.
Definition: CPoint3D.h:31
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
void pause(const std::string &msg=std::string("Press any key to continue...")) noexcept
Shows the message "Press any key to continue" (or other custom message) to the current standard outpu...
Definition: os.cpp:428
const size_t HOW_MANY_YAWS
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
double MYRAND1(size_t prec=64)
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
const size_t HOW_MANY_PITCHS
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: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020