MRPT  1.9.9
CGeneralizedCylinder.h
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 #ifndef opengl_CGeneralizedCylinder_H
10 #define opengl_CGeneralizedCylinder_H
11 
15 #include <mrpt/math/geometry.h>
18 
19 namespace mrpt::opengl
20 {
21 class CGeneralizedCylinder;
22 /**
23  * This object represents any figure obtained by extruding any profile along a
24  * given axis. The profile should lie over a x=0 plane, and the axis must be
25  * roughly perpendicular to this plane. In particular, it should be almost
26  * perpendicular to the Z axis.
27  * \ingroup mrpt_opengl_grp
28  */
30 {
32  public:
33  /**
34  * Auxiliary struct holding any quadrilateral, represented by foour points.
35  */
37  {
38  private:
39  /**
40  * Automatically compute a vector normal to this quadrilateral.
41  */
42  void calculateNormal();
43 
44  public:
45  /**
46  * Quadrilateral`'s points.
47  */
49  /**
50  * Normal vector.
51  */
52  double normal[3];
53  /**
54  * Given a polygon with 4 already positions allocated, this method
55  * fills it with the quadrilateral points.
56  * \sa mrpt::math::TPolygon3D
57  */
59  {
60  vec[0] = points[0];
61  vec[1] = points[1];
62  vec[2] = points[2];
63  vec[3] = points[3];
64  }
65  /**
66  * Constructor from 4 points.
67  */
69  const mrpt::math::TPoint3D& p1, const mrpt::math::TPoint3D& p2,
70  const mrpt::math::TPoint3D& p3, const mrpt::math::TPoint3D& p4)
71  {
72  points[0] = p1;
73  points[1] = p2;
74  points[2] = p3;
75  points[3] = p4;
77  }
78  /**
79  * Construction from any array of four compatible objects.
80  */
81  template <class T>
82  TQuadrilateral(const T (&p)[4])
83  {
84  for (int i = 0; i < 4; i++) points[i] = p[i];
86  }
87  /**
88  * Empty constructor. Initializes to garbage.
89  */
91  /**
92  * Destructor.
93  */
95  };
96 
97  protected:
98  /** Cylinder's axis. It's represented as a pose because it holds the angle
99  * to get to the next pose. */
101  /** Object's generatrix, that is, profile which will be extruded. */
102  std::vector<mrpt::math::TPoint3D> generatrix;
103  /** Mutable object with mesh information, used to avoid repeated
104  * computations. */
105  mutable std::vector<TQuadrilateral> mesh;
106  /** Mutable object with the cylinder's points, used to avoid repeated
107  * computations. */
109  /** Mutable flag which tells if recalculations are needed. */
110  mutable bool meshUpToDate;
111  /**
112  * Mutable set of data used in ray tracing.
113  * \sa mrpt::math::TPolygonWithPlane
114  */
115  mutable std::vector<mrpt::math::TPolygonWithPlane> polys;
116  /** Mutable flag telling whether ray tracing temporary data must be
117  * recalculated or not. */
118  mutable bool polysUpToDate;
119  /** Boolean variable which determines if the profile is closed at each
120  * section. */
121  bool closed;
122  /** Flag to determine whether the object is fully visible or only some
123  * sections are. */
125  /**
126  * First visible section, if fullyVisible is set to false.
127  * \sa fullyVisible,lastSection
128  */
129  size_t firstSection;
130  /**
131  * Last visible section, if fullyVisible is set to false.
132  * \sa fullyVisible,firstSection
133  */
134  size_t lastSection;
135 
136  public:
137  /**
138  * Creation of generalized cylinder from axis and generatrix
139  */
141  const std::vector<mrpt::math::TPoint3D>& axis,
142  const std::vector<mrpt::math::TPoint3D>& generatrix);
143  /**
144  * Render.
145  * \sa mrpt::opengl::CRenderizable
146  */
147  void render_dl() const override;
148  /**
149  * Ray tracing.
150  * \sa mrpt::opengl::CRenderizable.
151  */
152  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
153  /**
154  * Get axis's spatial coordinates.
155  */
156  inline void getAxis(std::vector<mrpt::math::TPoint3D>& a) const
157  {
158  // a=axis;
159  size_t N = axis.size();
160  a.resize(N);
161  for (size_t i = 0; i < N; i++)
162  {
163  a[i].x = axis[i].x();
164  a[i].y = axis[i].y();
165  a[i].z = axis[i].z();
166  }
167  }
168  /**
169  * Get axis, including angular coordinates.
170  */
172  {
173  a = axis;
174  }
175  /**
176  * Set the axis points.
177  */
178  inline void setAxis(const std::vector<mrpt::math::TPoint3D>& a)
179  {
180  generatePoses(a, axis);
181  meshUpToDate = false;
182  fullyVisible = true;
184  }
185  /**
186  * Get cylinder's profile.
187  */
188  inline void getGeneratrix(std::vector<mrpt::math::TPoint3D>& g) const
189  {
190  g = generatrix;
191  }
192  /**
193  * Set cylinder's profile.
194  */
195  inline void setGeneratrix(const std::vector<mrpt::math::TPoint3D>& g)
196  {
197  generatrix = g;
198  meshUpToDate = false;
200  }
201  /**
202  * Returns true if each section is a closed polygon.
203  */
204  inline bool isClosed() const { return closed; }
205  /**
206  * Set whether each section is a closed polygon or not.
207  */
208  inline void setClosed(bool c = true)
209  {
210  closed = c;
211  meshUpToDate = false;
213  }
214  /**
215  * Get a polyhedron containing the starting point of the cylinder (its
216  * "base").
217  * \sa getEnd,mrpt::opengl::CPolyhedron
218  */
219  void getOrigin(CPolyhedron::Ptr& poly) const;
220  /**
221  * Get a polyhedron containing the ending point of the cylinder (its
222  * "base").
223  * \sa getOrigin,mrpt::opengl::CPolyhedron
224  */
225  void getEnd(CPolyhedron::Ptr& poly) const;
226  /**
227  * Get the cylinder as a set of polygons in 3D.
228  * \sa mrpt::math::TPolygon3D
229  */
230  void generateSetOfPolygons(std::vector<mrpt::math::TPolygon3D>& res) const;
231  /**
232  * Get a polyhedron consisting of a set of closed sections of the cylinder.
233  * \sa mrpt::opengl::CPolyhedron
234  */
235  void getClosedSection(
236  size_t index1, size_t index2, CPolyhedron::Ptr& poly) const;
237  /**
238  * Get a polyhedron consisting of a single section of the cylinder.
239  * \sa mrpt::opengl::CPolyhedron
240  */
241  inline void getClosedSection(size_t index, CPolyhedron::Ptr& poly) const
242  {
243  getClosedSection(index, index, poly);
244  }
245  /**
246  * Get the number of sections in this cylinder.
247  */
248  inline size_t getNumberOfSections() const
249  {
250  return axis.size() ? (axis.size() - 1) : 0;
251  }
252  /**
253  * Get how many visible sections are in the cylinder.
254  */
255  inline size_t getVisibleSections() const
256  {
259  }
260  /**
261  * Gets the cylinder's visible sections.
262  */
263  void getVisibleSections(size_t& first, size_t& last) const
264  {
265  if (fullyVisible)
266  {
267  first = 0;
268  last = getNumberOfSections();
269  }
270  else
271  {
273  last = lastSection;
274  }
275  }
276  /**
277  * Sets all sections visible.
278  */
279  inline void setAllSectionsVisible()
280  {
281  fullyVisible = true;
283  }
284  /**
285  * Hides all sections.
286  */
287  inline void setAllSectionsInvisible(size_t pointer = 0)
288  {
289  fullyVisible = false;
293  }
294  /**
295  * Sets which sections are visible.
296  * \throw std::logic_error on wrongly defined bounds.
297  */
298  inline void setVisibleSections(size_t first, size_t last)
299  {
300  fullyVisible = false;
301  if (first > last || last > getNumberOfSections())
302  throw std::logic_error("Wrong bound definition");
304  lastSection = last;
306  }
307  /**
308  * Adds another visible section at the start of the cylinder. The cylinder
309  * must have an invisble section to display.
310  * \throw std::logic_error if there is no section to add to the displaying
311  * set.
312  * \sa
313  * addVisibleSectionAtEnd,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
314  */
316  {
317  if (fullyVisible || firstSection == 0)
318  throw std::logic_error("No more sections");
319  firstSection--;
321  }
322  /**
323  * Adds another visible section at the end of the cylinder. The cylinder
324  * must have an invisible section to display.
325  * \throw std::logic_error if there is no section to add to the displaying
326  * set.
327  * \sa
328  * addVisibleSectionAtStart,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
329  */
331  {
333  throw std::logic_error("No more sections");
334  lastSection++;
336  }
337  /**
338  * Removes a visible section from the start of the currently visible set.
339  * \throw std::logic_error if there are no visible sections.
340  * \sa
341  * addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtEnd
342  */
344  /**
345  * Removes a visible section from the ending of the currently visible set.
346  * \throw std::logic_error when there is no such section.
347  * \sa
348  * addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtStart
349  */
351  /**
352  * Gets the axis pose of the first section, returning false if there is no
353  * such pose.
354  */
356  /**
357  * Gets the axis pose of the last section, returning false if there is no
358  * such pose.
359  */
361  /**
362  * Gets the axis pose of the first visible section, returning false if
363  * there is no such pose.
364  */
366  /**
367  * Gets the axis pose of the last section, returning false if there is no
368  * such pose.
369  */
371  /**
372  * Updates the mutable set of polygons used in ray tracing.
373  */
374  void updatePolys() const;
375 
376  /** Evaluates the bounding box of this object (including possible children)
377  * in the coordinate frame of the object parent. */
378  void getBoundingBox(
379  mrpt::math::TPoint3D& bb_min,
380  mrpt::math::TPoint3D& bb_max) const override;
381 
382  private:
383  /**
384  * Updates the axis, transforming each point into a pose pointing to the
385  * next section.
386  */
387  void generatePoses(
388  const std::vector<mrpt::math::TPoint3D>& pIn,
390  /**
391  * Updates the mutable mesh.
392  */
393  void updateMesh() const;
394  /**
395  * Given a vector of polyhedrons, gets the starting and ending iterators to
396  * the section to be actually rendered.
397  */
398  void getMeshIterators(
399  const std::vector<TQuadrilateral>& m,
402 
403  public:
404  /**
405  * Basic constructor with default initialization.
406  */
408  : axis(),
409  generatrix(),
410  mesh(),
411  meshUpToDate(false),
412  polysUpToDate(false),
413  closed(false),
414  fullyVisible(true)
415  {
416  }
417  /**
418  * Constructor with axis and generatrix.
419  */
421  const std::vector<mrpt::math::TPoint3D>& a,
422  const std::vector<mrpt::math::TPoint3D>& g)
423  : generatrix(g),
424  mesh(),
425  meshUpToDate(false),
426  polysUpToDate(false),
427  closed(false),
428  fullyVisible(true)
429  {
430  generatePoses(a, axis);
431  }
432  /**
433  * Destructor.
434  */
436 };
437 }
438 #endif
439 
440 
void generateSetOfPolygons(std::vector< mrpt::math::TPolygon3D > &res) const
Get the cylinder as a set of polygons in 3D.
void calculateNormal()
Automatically compute a vector normal to this quadrilateral.
void generatePoses(const std::vector< mrpt::math::TPoint3D > &pIn, mrpt::aligned_std_vector< mrpt::poses::CPose3D > &pOut)
Updates the axis, transforming each point into a pose pointing to the next section.
void setAllSectionsVisible()
Sets all sections visible.
void getOrigin(CPolyhedron::Ptr &poly) const
Get a polyhedron containing the starting point of the cylinder (its "base").
void getAsPolygonUnsafe(mrpt::math::TPolygon3D &vec) const
Given a polygon with 4 already positions allocated, this method fills it with the quadrilateral point...
GLsizei const GLvoid * pointer
Definition: glext.h:3825
void getEnd(CPolyhedron::Ptr &poly) const
Get a polyhedron containing the ending point of the cylinder (its "base").
bool getFirstSectionPose(mrpt::poses::CPose3D &p)
Gets the axis pose of the first section, returning false if there is no such pose.
void setClosed(bool c=true)
Set whether each section is a closed polygon or not.
GLint * first
Definition: glext.h:3827
size_t getNumberOfSections() const
Get the number of sections in this cylinder.
void removeVisibleSectionAtStart()
Removes a visible section from the start of the currently visible set.
bool fullyVisible
Flag to determine whether the object is fully visible or only some sections are.
static Ptr Create(Args &&... args)
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:29
void addVisibleSectionAtStart()
Adds another visible section at the start of the cylinder.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
std::vector< TQuadrilateral > mesh
Mutable object with mesh information, used to avoid repeated computations.
bool meshUpToDate
Mutable flag which tells if recalculations are needed.
void getMeshIterators(const std::vector< TQuadrilateral > &m, std::vector< TQuadrilateral >::const_iterator &begin, std::vector< TQuadrilateral >::const_iterator &end) const
Given a vector of polyhedrons, gets the starting and ending iterators to the section to be actually r...
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
bool getLastSectionPose(mrpt::poses::CPose3D &p)
Gets the axis pose of the last section, returning false if there is no such pose. ...
CGeneralizedCylinder(const std::vector< mrpt::math::TPoint3D > &a, const std::vector< mrpt::math::TPoint3D > &g)
Constructor with axis and generatrix.
void getClosedSection(size_t index, CPolyhedron::Ptr &poly) const
Get a polyhedron consisting of a single section of the cylinder.
void removeVisibleSectionAtEnd()
Removes a visible section from the ending of the currently visible set.
GLsizei const GLfloat * points
Definition: glext.h:5339
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
void render_dl() const override
Render.
bool closed
Boolean variable which determines if the profile is closed at each section.
void setVisibleSections(size_t first, size_t last)
Sets which sections are visible.
void getGeneratrix(std::vector< mrpt::math::TPoint3D > &g) const
Get cylinder&#39;s profile.
GLuint index
Definition: glext.h:4054
const GLubyte * c
Definition: glext.h:6313
void setGeneratrix(const std::vector< mrpt::math::TPoint3D > &g)
Set cylinder&#39;s profile.
GLuint GLuint end
Definition: glext.h:3528
GLubyte g
Definition: glext.h:6279
void setAllSectionsInvisible(size_t pointer=0)
Hides all sections.
void getVisibleSections(size_t &first, size_t &last) const
Gets the cylinder&#39;s visible sections.
bool getFirstVisibleSectionPose(mrpt::poses::CPose3D &p)
Gets the axis pose of the first visible section, returning false if there is no such pose...
mrpt::math::CMatrixTemplate< mrpt::math::TPoint3D > pointsMesh
Mutable object with the cylinder&#39;s points, used to avoid repeated computations.
std::vector< mrpt::math::TPolygonWithPlane > polys
Mutable set of data used in ray tracing.
bool polysUpToDate
Mutable flag telling whether ray tracing temporary data must be recalculated or not.
void setAxis(const std::vector< mrpt::math::TPoint3D > &a)
Set the axis points.
void getAxis(std::vector< mrpt::math::TPoint3D > &a) const
Get axis&#39;s spatial coordinates.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
size_t lastSection
Last visible section, if fullyVisible is set to false.
This object represents any figure obtained by extruding any profile along a given axis...
size_t getVisibleSections() const
Get how many visible sections are in the cylinder.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
void updateMesh() const
Updates the mutable mesh.
std::vector< mrpt::math::TPoint3D > generatrix
Object&#39;s generatrix, that is, profile which will be extruded.
mrpt::aligned_std_vector< mrpt::poses::CPose3D > axis
Cylinder&#39;s axis.
void updatePolys() const
Updates the mutable set of polygons used in ray tracing.
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
TQuadrilateral(const T(&p)[4])
Construction from any array of four compatible objects.
void getClosedSection(size_t index1, size_t index2, CPolyhedron::Ptr &poly) const
Get a polyhedron consisting of a set of closed sections of the cylinder.
bool isClosed() const
Returns true if each section is a closed polygon.
CGeneralizedCylinder()
Basic constructor with default initialization.
void getAxis(mrpt::aligned_std_vector< mrpt::poses::CPose3D > &a) const
Get axis, including angular coordinates.
bool getLastVisibleSectionPose(mrpt::poses::CPose3D &p)
Gets the axis pose of the last section, returning false if there is no such pose. ...
GLuint res
Definition: glext.h:7268
Lightweight 3D point.
TQuadrilateral(const mrpt::math::TPoint3D &p1, const mrpt::math::TPoint3D &p2, const mrpt::math::TPoint3D &p3, const mrpt::math::TPoint3D &p4)
Constructor from 4 points.
Auxiliary struct holding any quadrilateral, represented by foour points.
void addVisibleSectionAtEnd()
Adds another visible section at the end of the cylinder.
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLfloat GLfloat p
Definition: glext.h:6305
const Scalar * const_iterator
Definition: eigen_plugins.h:27
3D polygon, inheriting from std::vector<TPoint3D>
size_t firstSection
First visible section, if fullyVisible is set to false.
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Ray tracing.



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