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 
13 #include <mrpt/opengl/CAxis.h>
15 #include <iostream>
16 
17 using namespace std;
18 using namespace mrpt;
19 using namespace mrpt::gui;
20 using namespace mrpt::opengl;
21 using namespace mrpt::poses;
22 using namespace mrpt::math;
24 using namespace mrpt::system;
25 
26 const double GRID_R = 1.0;
27 const double GRID_G = 1.0;
28 const double GRID_B = 1.0;
29 
30 // Comment to increase randomness of poses
31 #define PAIRED_RANDOM_POSES
32 
33 #define USE_THREADS
34 
35 inline double MYRAND1(size_t prec = 64)
36 {
37  return static_cast<double>(rand() % prec) / static_cast<double>(prec - 1);
38 }
39 
40 void randomColor(const CRenderizable::Ptr& obj, double alpha)
41 {
42  obj->setColor(MYRAND1(), MYRAND1(), MYRAND1(), alpha);
43 }
44 
45 #ifdef USE_THREADS
46 class PIThreadParam
47 {
48  public:
49  const pair<CPolyhedron::Ptr, CPolyhedron::Ptr>* polys;
50  vector<TSegment3D> intersection;
51  PIThreadParam(const pair<CPolyhedron::Ptr, CPolyhedron::Ptr>& p)
52  : polys(&p), intersection()
53  {
54  }
55  PIThreadParam() : polys(nullptr), intersection() {}
56  inline static PIThreadParam createObject(
57  const pair<CPolyhedron::Ptr, CPolyhedron::Ptr>& p)
58  {
59  return PIThreadParam(p);
60  }
61 };
62 
64 {
65  vector<TObject3D> ints;
66  CPolyhedron::getIntersection(p.polys->first, p.polys->second, ints);
67  TObject3D::getSegments(ints, p.intersection);
68 }
69 
70 inline std::thread piCreateThread(PIThreadParam& p)
71 {
72  return std::thread(&piThreadFunction, std::ref(p));
73 }
74 
76 {
77  public:
78  vector<TSegment3D>& sgms;
79  AggregatorFunctor(vector<TSegment3D>& s) : sgms(s) {}
80  inline void operator()(const PIThreadParam& p)
81  {
82  sgms.insert(sgms.end(), p.intersection.begin(), p.intersection.end());
83  }
84 };
85 #endif
86 
88  const vector<pair<CPolyhedron::Ptr, CPolyhedron::Ptr>>& v)
89 {
90  vector<TSegment3D> sgms;
91 #ifdef USE_THREADS
92  vector<PIThreadParam> pars(v.size());
93  vector<std::thread> threads(v.size());
94  transform(v.begin(), v.end(), pars.begin(), &PIThreadParam::createObject);
95  transform(pars.begin(), pars.end(), threads.begin(), &piCreateThread);
96  for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
97  for_each(pars.begin(), pars.end(), AggregatorFunctor(sgms));
98 #else
99  vector<TObject3D> ints, TMP;
100  for (vector<pair<CPolyhedron::Ptr, CPolyhedron::Ptr>>::const_iterator it =
101  v.begin();
102  it != v.end(); ++it)
103  {
104  CPolyhedron::getIntersection(it->first, it->second, TMP);
105  ints.insert(ints.end(), TMP.begin(), TMP.end());
106  }
107  TObject3D::getSegments(ints, sgms);
108 #endif
109  CSetOfLines::Ptr lns = mrpt::make_aligned_shared<CSetOfLines>(sgms);
110  lns->setLineWidth(9);
111  randomColor(lns, 1.0);
112  return lns;
113 }
114 
115 inline double randomAngle(size_t prec = 64) { return MYRAND1(prec) * M_PI; }
116 inline double randomZ(double space = 25, size_t prec = 64)
117 {
118  return space * (MYRAND1(prec) - 0.5);
119 }
120 
121 pair<CPolyhedron::Ptr, CPolyhedron::Ptr> addPairOfPolys(
123  double x, double y)
124 {
125  p1->makeConvexPolygons();
126  p2->makeConvexPolygons();
127 #ifdef PAIRED_RANDOM_POSES
128  CPose3D pose =
130  p1->setPose(pose);
131  p2->setPose(pose);
132 #else
133  CPose3D pose1 =
135  CPose3D pose2 =
137  p1->setPose(pose1);
138  p2->setPose(pose2);
139 #endif
140  randomColor(p1, 0.5);
141  randomColor(p2, 0.5);
142  objs->insert(p1);
143  objs->insert(p2);
144  return make_pair(p1, p2);
145 }
146 
147 void display()
148 {
149  CDisplayWindow3D window("Polyhedra Intersection demo", 640, 480);
150  window.resize(640, 480);
151  COpenGLScene::Ptr scene1 = mrpt::make_aligned_shared<COpenGLScene>();
153  mrpt::make_aligned_shared<CGridPlaneXY>(-25, 25, -25, 25, 0, 1);
154  plane1->setColor(GRID_R, GRID_G, GRID_B);
155  scene1->insert(plane1);
156  scene1->insert(
157  mrpt::make_aligned_shared<CAxis>(-5, -5, -5, 5, 5, 5, 2.5, 3, true));
158  CSetOfObjects::Ptr objs = mrpt::make_aligned_shared<CSetOfObjects>();
159  vector<pair<CPolyhedron::Ptr, CPolyhedron::Ptr>> polys;
160  polys.reserve(16);
161  // Addition of polyhedra. Add more polyhedra at wish, but try to avoid
162  // intersections with other pairs, for better visualization.
163  polys.push_back(
165  CPolyhedron::CreateHexahedron(10),
166  CPolyhedron::CreateOctahedron(10), objs, -12.5, -12.5));
167  polys.push_back(
169  CPolyhedron::CreateIcosahedron(10),
170  CPolyhedron::CreateDodecahedron(10), objs, -12.5, 12.5));
171  polys.push_back(
173  CPolyhedron::CreateRhombicuboctahedron(10),
174  CPolyhedron::CreateCuboctahedron(10), objs, 12.5, 12.5));
175  polys.push_back(
177  CPolyhedron::CreateArchimedeanRegularAntiprism(4, 9),
178  CPolyhedron::CreateRegularDoublePyramid(9, 10, 15, 6), objs, 12.5,
179  -12.5));
180  polys.push_back(
182  CPolyhedron::CreateCuboctahedron(10),
183  CPolyhedron::CreateRhombicDodecahedron(10), objs, -37.5, -37.5));
184  polys.push_back(
186  CPolyhedron::CreateRhombicuboctahedron(10),
187  CPolyhedron::CreateDeltoidalIcositetrahedron(10), objs, -37.5,
188  -12.5));
189  polys.push_back(
191  CPolyhedron::CreateIcosidodecahedron(10),
192  CPolyhedron::CreateRhombicTriacontahedron(10), objs, -37.5, 12.5));
193  polys.push_back(
195  CPolyhedron::CreateRhombicosidodecahedron(10),
196  CPolyhedron::CreateDeltoidalHexecontahedron(10), objs, -37.5,
197  37.5));
198  polys.push_back(
200  CPolyhedron::CreateTruncatedTetrahedron(10),
201  CPolyhedron::CreateTriakisTetrahedron(10), objs, -12.5, -37.5));
202  polys.push_back(
204  CPolyhedron::CreateTruncatedHexahedron(10),
205  CPolyhedron::CreateTriakisOctahedron(10), objs, -12.5, 37.5));
206  polys.push_back(
208  CPolyhedron::CreateTruncatedOctahedron(10),
209  CPolyhedron::CreateTetrakisHexahedron(10), objs, 12.5, -37.5));
210  polys.push_back(
212  CPolyhedron::CreateTruncatedDodecahedron(10),
213  CPolyhedron::CreateTriakisIcosahedron(10), objs, 12.5, 37.5));
214  polys.push_back(
216  CPolyhedron::CreateTruncatedIcosahedron(10),
217  CPolyhedron::CreatePentakisDodecahedron(10), objs, 37.5, -37.5));
218  polys.push_back(
220  CPolyhedron::CreateRandomPolyhedron(10),
221  CPolyhedron::CreateRandomPolyhedron(10), objs, 37.5, -12.5));
222  polys.push_back(
224  CPolyhedron::CreateDodecahedron(10),
225  CPolyhedron::CreateDeltoidalHexecontahedron(10), objs, 37.5, 12.5));
226  polys.push_back(
228  CPolyhedron::CreateTriakisIcosahedron(10),
229  CPolyhedron::CreatePentakisDodecahedron(10), objs, 37.5, 37.5));
230  objs << getIntersections(polys);
231 
232  scene1->insert(objs);
233  window.get3DSceneAndLock() = scene1;
234  window.unlockAccess3DScene();
235  window.setCameraElevationDeg(25.0f);
236  window.forceRepaint();
237  window.waitForKey();
238 }
239 
240 int main()
241 {
242  srand(
245  try
246  {
247  display();
248  return 0;
249  }
250  catch (exception& e)
251  {
252  cout << "Error: " << e.what() << '.' << endl;
253  return -1;
254  }
255  catch (...)
256  {
257  cout << "Unknown Error.\n";
258  return -1;
259  }
260 }
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
getIntersections
CSetOfLines::Ptr getIntersections(const vector< pair< CPolyhedron::Ptr, CPolyhedron::Ptr >> &v)
Definition: vision_stereo_rectify/test.cpp:87
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
piThreadFunction
void piThreadFunction(PIThreadParam &p)
Definition: vision_stereo_rectify/test.cpp:63
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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
randomColor
void randomColor(const CRenderizable::Ptr &obj, double alpha)
Definition: vision_stereo_rectify/test.cpp:40
mrpt::system::extractDayTimeFromTimestamp
double extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
Definition: datetime.cpp:291
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
transform
GLuint GLenum GLenum transform
Definition: glext.h:6975
addPairOfPolys
pair< CPolyhedron::Ptr, CPolyhedron::Ptr > addPairOfPolys(CPolyhedron::Ptr p1, CPolyhedron::Ptr p2, CSetOfObjects::Ptr &objs, double x, double y)
Definition: vision_stereo_rectify/test.cpp:121
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
alpha
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3525
randomZ
double randomZ(double space=25, size_t prec=64)
Definition: vision_stereo_rectify/test.cpp:116
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
CDisplayWindow3D.h
mrpt::system::getCurrentLocalTime
mrpt::system::TTimeStamp getCurrentLocalTime()
Returns the current (local) time.
Definition: datetime.cpp:174
AggregatorFunctor
Definition: vision_stereo_rectify/test.cpp:75
v
const GLdouble * v
Definition: glext.h:3678
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
M_PI
#define M_PI
Definition: core/include/mrpt/core/bits_math.h:38
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
PIThreadParam
Definition: vision_stereo_rectify/test.cpp:46
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::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
piCreateThread
std::thread piCreateThread(PIThreadParam &p)
Definition: vision_stereo_rectify/test.cpp:70
randomAngle
double randomAngle(size_t prec=64)
Definition: vision_stereo_rectify/test.cpp:115
CAxis.h
display
void display()
Definition: vision_stereo_rectify/test.cpp:33
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
ref
GLenum GLint ref
Definition: glext.h:4050
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
PIThreadParam::createObject
static PIThreadParam createObject(const pair< CPolyhedron::Ptr, CPolyhedron::Ptr > &p)
Definition: vision_stereo_rectify/test.cpp:56
CGridPlaneXY.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
CPolyhedron.h
x
GLenum GLint x
Definition: glext.h:3538
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