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 
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  // uint32_t s=1000000;
226  cout << "Random seed: " << s << '\n';
228  try
229  {
230  display();
231  return 0;
232  }
233  catch (const exception& e)
234  {
235  cout << "Error: " << e.what() << '.' << endl;
237  return -1;
238  }
239  catch (...)
240  {
241  cout << "Unknown Error.\n";
242  return -1;
243  }
244 }
mrpt::math::CMatrixDouble
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
Definition: CMatrixTemplateNumeric.h:144
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
mrpt::opengl::CSetOfLines::Ptr
std::shared_ptr< CSetOfLines > Ptr
Definition: CSetOfLines.h:35
GRID_B
const double GRID_B
Definition: vision_stereo_rectify/test.cpp:28
GRID_R
const double GRID_R
Definition: vision_stereo_rectify/test.cpp:26
s
GLdouble s
Definition: glext.h:3676
t
GLdouble GLdouble t
Definition: glext.h:3689
configRandom
void configRandom(const CRenderizable::Ptr &obj)
Call configRandom given the address of an object and assign random pose and color to it.
Definition: vision_stereo_rectify/test.cpp:85
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::opengl::CTexturedPlane::Ptr
std::shared_ptr< CTexturedPlane > Ptr
Definition: CTexturedPlane.h:24
CSphere.h
mrpt::opengl::CSphere::Ptr
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
RANDOM_POSE_DISTANCE
const float RANDOM_POSE_DISTANCE
Definition: vision_stereo_rectify/test.cpp:58
generateObjects
void generateObjects(CSetOfObjects::Ptr &world)
Definition: vision_stereo_rectify/test.cpp:107
MYRANDG
double MYRANDG(double scale, double shift=0)
Definition: vision_stereo_rectify/test.cpp:66
guideLines
void guideLines(const CPose3D &base, CSetOfLines::Ptr &lines, float dist)
Definition: vision_stereo_rectify/test.cpp:91
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::opengl::CCylinder::Ptr
std::shared_ptr< CCylinder > Ptr
Definition: CCylinder.h:34
mrpt::opengl::CRenderizable::Ptr
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
CEllipsoid.h
mrpt::opengl::CAngularObservationMesh::Ptr
std::shared_ptr< CAngularObservationMesh > Ptr
Definition: CAngularObservationMesh.h:46
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
random.h
mrpt::random::CRandomGenerator::randomize
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Definition: RandomGenerator.cpp:32
mrpt::math::CMatrixTemplateNumeric< double >
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
M_PI
#define M_PI
Definition: core/include/mrpt/core/bits_math.h:38
randomPose
CPose3D randomPose()
Definition: vision_stereo_rectify/test.cpp:72
mrpt::poses::CPoseOrPoint::y
double y() const
Definition: CPoseOrPoint.h:144
mrpt::random::CRandomGenerator::drawUniform
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm,...
Definition: RandomGenerators.h:111
GRID_G
const double GRID_G
Definition: vision_stereo_rectify/test.cpp:27
mrpt::opengl::CPolyhedron::Ptr
std::shared_ptr< CPolyhedron > Ptr
Definition: CPolyhedron.h:46
CDisk.h
mrpt::poses::CPoseOrPoint::x
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::opengl::CAngularObservationMesh
A mesh built from a set of 2D laser scan observations.
Definition: CAngularObservationMesh.h:44
CAngularObservationMesh.h
mrpt::opengl::CSetOfObjects::Ptr
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:30
mrpt::serialization
Definition: aligned_serialization.h:14
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
CPoint3D.h
HOW_MANY_YAWS
const size_t HOW_MANY_YAWS
Definition: vision_stereo_rectify/test.cpp:55
scale
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:6503
CPose3D.h
mrpt::system::getCurrentTime
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.cpp:74
CAxis.h
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
CTicTac.h
display
void display()
Definition: vision_stereo_rectify/test.cpp:33
mrpt::opengl::CDisk::Ptr
std::shared_ptr< CDisk > Ptr
Definition: CDisk.h:35
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
CCylinder.h
gui.h
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
MYRAND1
double MYRAND1(size_t prec=64)
Definition: vision_stereo_rectify/test.cpp:35
mrpt::random
A namespace of pseudo-random numbers generators of diferent distributions.
Definition: random_shuffle.h:17
mrpt::opengl::CEllipsoid::Ptr
std::shared_ptr< CEllipsoid > Ptr
Definition: CEllipsoid.h:49
HOW_MANY_PITCHS
const size_t HOW_MANY_PITCHS
Definition: vision_stereo_rectify/test.cpp:56
mrpt::poses::CPoint3D
A class used to store a 3D point.
Definition: CPoint3D.h:33
CGridPlaneXY.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
uint32_t
unsigned __int32 uint32_t
Definition: rptypes.h:47
CPolyhedron.h
CTexturedPlane.h
mrpt::system::pause
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
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