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(addPairOfPolys(
164  CPolyhedron::CreateHexahedron(10), CPolyhedron::CreateOctahedron(10),
165  objs, -12.5, -12.5));
166  polys.push_back(addPairOfPolys(
167  CPolyhedron::CreateIcosahedron(10), CPolyhedron::CreateDodecahedron(10),
168  objs, -12.5, 12.5));
169  polys.push_back(addPairOfPolys(
170  CPolyhedron::CreateRhombicuboctahedron(10),
171  CPolyhedron::CreateCuboctahedron(10), objs, 12.5, 12.5));
172  polys.push_back(addPairOfPolys(
173  CPolyhedron::CreateArchimedeanRegularAntiprism(4, 9),
174  CPolyhedron::CreateRegularDoublePyramid(9, 10, 15, 6), objs, 12.5,
175  -12.5));
176  polys.push_back(addPairOfPolys(
177  CPolyhedron::CreateCuboctahedron(10),
178  CPolyhedron::CreateRhombicDodecahedron(10), objs, -37.5, -37.5));
179  polys.push_back(addPairOfPolys(
180  CPolyhedron::CreateRhombicuboctahedron(10),
181  CPolyhedron::CreateDeltoidalIcositetrahedron(10), objs, -37.5, -12.5));
182  polys.push_back(addPairOfPolys(
183  CPolyhedron::CreateIcosidodecahedron(10),
184  CPolyhedron::CreateRhombicTriacontahedron(10), objs, -37.5, 12.5));
185  polys.push_back(addPairOfPolys(
186  CPolyhedron::CreateRhombicosidodecahedron(10),
187  CPolyhedron::CreateDeltoidalHexecontahedron(10), objs, -37.5, 37.5));
188  polys.push_back(addPairOfPolys(
189  CPolyhedron::CreateTruncatedTetrahedron(10),
190  CPolyhedron::CreateTriakisTetrahedron(10), objs, -12.5, -37.5));
191  polys.push_back(addPairOfPolys(
192  CPolyhedron::CreateTruncatedHexahedron(10),
193  CPolyhedron::CreateTriakisOctahedron(10), objs, -12.5, 37.5));
194  polys.push_back(addPairOfPolys(
195  CPolyhedron::CreateTruncatedOctahedron(10),
196  CPolyhedron::CreateTetrakisHexahedron(10), objs, 12.5, -37.5));
197  polys.push_back(addPairOfPolys(
198  CPolyhedron::CreateTruncatedDodecahedron(10),
199  CPolyhedron::CreateTriakisIcosahedron(10), objs, 12.5, 37.5));
200  polys.push_back(addPairOfPolys(
201  CPolyhedron::CreateTruncatedIcosahedron(10),
202  CPolyhedron::CreatePentakisDodecahedron(10), objs, 37.5, -37.5));
203  polys.push_back(addPairOfPolys(
204  CPolyhedron::CreateRandomPolyhedron(10),
205  CPolyhedron::CreateRandomPolyhedron(10), objs, 37.5, -12.5));
206  polys.push_back(addPairOfPolys(
207  CPolyhedron::CreateDodecahedron(10),
208  CPolyhedron::CreateDeltoidalHexecontahedron(10), objs, 37.5, 12.5));
209  polys.push_back(addPairOfPolys(
210  CPolyhedron::CreateTriakisIcosahedron(10),
211  CPolyhedron::CreatePentakisDodecahedron(10), objs, 37.5, 37.5));
212  objs << getIntersections(polys);
213 
214  scene1->insert(objs);
215  window.get3DSceneAndLock() = scene1;
216  window.unlockAccess3DScene();
217  window.setCameraElevationDeg(25.0f);
218  window.forceRepaint();
219  window.waitForKey();
220 }
221 
222 int main()
223 {
224  srand((unsigned int)mrpt::system::extractDayTimeFromTimestamp(
225  mrpt::system::now()));
226  try
227  {
228  display();
229  return 0;
230  }
231  catch (exception& e)
232  {
233  cout << "Error: " << e.what() << '.' << endl;
234  return -1;
235  }
236  catch (...)
237  {
238  cout << "Unknown Error.\n";
239  return -1;
240  }
241 }
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3525
A mesh built from a set of 2D laser scan observations.
GLdouble GLdouble t
Definition: glext.h:3689
static PIThreadParam createObject(const pair< CPolyhedron::Ptr, CPolyhedron::Ptr > &p)
void randomColor(const CRenderizable::Ptr &obj, double alpha)
CSetOfLines::Ptr getIntersections(const vector< pair< CPolyhedron::Ptr, CPolyhedron::Ptr >> &v)
const double GRID_B
const double GRID_R
GLenum GLint ref
Definition: glext.h:4050
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:87
pair< CPolyhedron::Ptr, CPolyhedron::Ptr > addPairOfPolys(CPolyhedron::Ptr p1, CPolyhedron::Ptr p2, CSetOfObjects::Ptr &objs, double x, double y)
double extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
Definition: datetime.cpp:192
STL namespace.
GLdouble s
Definition: glext.h:3676
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
This base provides a set of functions for maths stuff.
double randomZ(double space=25, size_t prec=64)
const double GRID_G
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
std::thread piCreateThread(PIThreadParam &p)
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double randomAngle(size_t prec=64)
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
GLenum GLint GLint y
Definition: glext.h:3538
double MYRAND1(size_t prec=64)
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
GLenum GLint x
Definition: glext.h:3538
GLuint GLenum GLenum transform
Definition: glext.h:6975
GLfloat GLfloat p
Definition: glext.h:6305
const Scalar * const_iterator
Definition: eigen_plugins.h:27
void piThreadFunction(PIThreadParam &p)
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