Main MRPT website > C++ reference for MRPT 1.5.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-2017, 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 {
20 namespace opengl {
22  // This must be added to any CSerializable derived class:
24  /**
25  * This object represents any figure obtained by extruding any profile along a given axis. The profile should lie over a x=0 plane, and the axis must be roughly perpendicular to this plane. In particular, it should be almost perpendicular to the Z axis.
26  * \ingroup mrpt_opengl_grp
27  */
30  public:
31  /**
32  * Auxiliary struct holding any quadrilateral, represented by foour points.
33  */
35  private:
36  /**
37  * Automatically compute a vector normal to this quadrilateral.
38  */
39  void calculateNormal();
40  public:
41  /**
42  * Quadrilateral`'s points.
43  */
45  /**
46  * Normal vector.
47  */
48  double normal[3];
49  /**
50  * Given a polygon with 4 already positions allocated, this method fills it with the quadrilateral points.
51  * \sa mrpt::math::TPolygon3D
52  */
53  inline void getAsPolygonUnsafe(mrpt::math::TPolygon3D &vec) const {
54  vec[0]=points[0];
55  vec[1]=points[1];
56  vec[2]=points[2];
57  vec[3]=points[3];
58  }
59  /**
60  * Constructor from 4 points.
61  */
63  points[0]=p1;
64  points[1]=p2;
65  points[2]=p3;
66  points[3]=p4;
67  calculateNormal();
68  }
69  /**
70  * Construction from any array of four compatible objects.
71  */
72  template<class T> TQuadrilateral(const T (&p)[4]) {
73  for (int i=0;i<4;i++) points[i]=p[i];
74  calculateNormal();
75  }
76  /**
77  * Empty constructor. Initializes to garbage.
78  */
80  /**
81  * Destructor.
82  */
84  };
85  protected:
86  /** Cylinder's axis. It's represented as a pose because it holds the angle to get to the next pose. */
88  /** Object's generatrix, that is, profile which will be extruded. */
89  std::vector<mrpt::math::TPoint3D> generatrix;
90  /** Mutable object with mesh information, used to avoid repeated computations. */
91  mutable std::vector<TQuadrilateral> mesh;
92  /** Mutable object with the cylinder's points, used to avoid repeated computations. */
94  /** Mutable flag which tells if recalculations are needed. */
95  mutable bool meshUpToDate;
96  /**
97  * Mutable set of data used in ray tracing.
98  * \sa mrpt::math::TPolygonWithPlane
99  */
100  mutable std::vector<mrpt::math::TPolygonWithPlane> polys;
101  /** Mutable flag telling whether ray tracing temporary data must be recalculated or not. */
102  mutable bool polysUpToDate;
103  /** Boolean variable which determines if the profile is closed at each section. */
104  bool closed;
105  /** Flag to determine whether the object is fully visible or only some sections are. */
107  /**
108  * First visible section, if fullyVisible is set to false.
109  * \sa fullyVisible,lastSection
110  */
111  size_t firstSection;
112  /**
113  * Last visible section, if fullyVisible is set to false.
114  * \sa fullyVisible,firstSection
115  */
116  size_t lastSection;
117  public:
118  /**
119  * Creation of generalized cylinder from axis and generatrix
120  */
121  static CGeneralizedCylinderPtr Create(const std::vector<mrpt::math::TPoint3D> &axis,const std::vector<mrpt::math::TPoint3D> &generatrix);
122  /**
123  * Render.
124  * \sa mrpt::opengl::CRenderizable
125  */
126  void render_dl() const MRPT_OVERRIDE;
127  /**
128  * Ray tracing.
129  * \sa mrpt::opengl::CRenderizable.
130  */
131  bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const MRPT_OVERRIDE;
132  /**
133  * Get axis's spatial coordinates.
134  */
135  inline void getAxis(std::vector<mrpt::math::TPoint3D> &a) const {
136  //a=axis;
137  size_t N=axis.size();
138  a.resize(N);
139  for (size_t i=0;i<N;i++) {
140  a[i].x=axis[i].x();
141  a[i].y=axis[i].y();
142  a[i].z=axis[i].z();
143  }
144  }
145  /**
146  * Get axis, including angular coordinates.
147  */
149  a=axis;
150  }
151  /**
152  * Set the axis points.
153  */
154  inline void setAxis(const std::vector<mrpt::math::TPoint3D> &a) {
155  generatePoses(a,axis);
156  meshUpToDate=false;
157  fullyVisible=true;
159  }
160  /**
161  * Get cylinder's profile.
162  */
163  inline void getGeneratrix(std::vector<mrpt::math::TPoint3D> &g) const {
164  g=generatrix;
165  }
166  /**
167  * Set cylinder's profile.
168  */
169  inline void setGeneratrix(const std::vector<mrpt::math::TPoint3D> &g) {
170  generatrix=g;
171  meshUpToDate=false;
173  }
174  /**
175  * Returns true if each section is a closed polygon.
176  */
177  inline bool isClosed() const {
178  return closed;
179  }
180  /**
181  * Set whether each section is a closed polygon or not.
182  */
183  inline void setClosed(bool c=true) {
184  closed=c;
185  meshUpToDate=false;
187  }
188  /**
189  * Get a polyhedron containing the starting point of the cylinder (its "base").
190  * \sa getEnd,mrpt::opengl::CPolyhedron
191  */
192  void getOrigin(CPolyhedronPtr &poly) const;
193  /**
194  * Get a polyhedron containing the ending point of the cylinder (its "base").
195  * \sa getOrigin,mrpt::opengl::CPolyhedron
196  */
197  void getEnd(CPolyhedronPtr &poly) const;
198  /**
199  * Get the cylinder as a set of polygons in 3D.
200  * \sa mrpt::math::TPolygon3D
201  */
202  void generateSetOfPolygons(std::vector<mrpt::math::TPolygon3D> &res) const;
203  /**
204  * Get a polyhedron consisting of a set of closed sections of the cylinder.
205  * \sa mrpt::opengl::CPolyhedron
206  */
207  void getClosedSection(size_t index1,size_t index2,CPolyhedronPtr &poly) const;
208  /**
209  * Get a polyhedron consisting of a single section of the cylinder.
210  * \sa mrpt::opengl::CPolyhedron
211  */
212  inline void getClosedSection(size_t index,CPolyhedronPtr &poly) const {
213  getClosedSection(index,index,poly);
214  }
215  /**
216  * Get the number of sections in this cylinder.
217  */
218  inline size_t getNumberOfSections() const {
219  return axis.size()?(axis.size()-1):0;
220  }
221  /**
222  * Get how many visible sections are in the cylinder.
223  */
224  inline size_t getVisibleSections() const {
225  return fullyVisible?getNumberOfSections():(lastSection-firstSection);
226  }
227  /**
228  * Gets the cylinder's visible sections.
229  */
230  void getVisibleSections(size_t &first,size_t &last) const {
231  if (fullyVisible) {
232  first=0;
233  last=getNumberOfSections();
234  } else {
235  first=firstSection;
236  last=lastSection;
237  }
238  }
239  /**
240  * Sets all sections visible.
241  */
242  inline void setAllSectionsVisible() {
243  fullyVisible=true;
245  }
246  /**
247  * Hides all sections.
248  */
249  inline void setAllSectionsInvisible(size_t pointer=0) {
250  fullyVisible=false;
251  firstSection=pointer;
252  lastSection=pointer;
254  }
255  /**
256  * Sets which sections are visible.
257  * \throw std::logic_error on wrongly defined bounds.
258  */
259  inline void setVisibleSections(size_t first,size_t last) {
260  fullyVisible=false;
261  if (first>last||last>getNumberOfSections()) throw std::logic_error("Wrong bound definition");
262  firstSection=first;
263  lastSection=last;
265  }
266  /**
267  * Adds another visible section at the start of the cylinder. The cylinder must have an invisble section to display.
268  * \throw std::logic_error if there is no section to add to the displaying set.
269  * \sa addVisibleSectionAtEnd,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
270  */
271  inline void addVisibleSectionAtStart() {
272  if (fullyVisible||firstSection==0) throw std::logic_error("No more sections");
273  firstSection--;
275  }
276  /**
277  * Adds another visible section at the end of the cylinder. The cylinder must have an invisible section to display.
278  * \throw std::logic_error if there is no section to add to the displaying set.
279  * \sa addVisibleSectionAtStart,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
280  */
281  inline void addVisibleSectionAtEnd() {
282  if (fullyVisible||lastSection==getNumberOfSections()) throw std::logic_error("No more sections");
283  lastSection++;
285  }
286  /**
287  * Removes a visible section from the start of the currently visible set.
288  * \throw std::logic_error if there are no visible sections.
289  * \sa addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtEnd
290  */
291  void removeVisibleSectionAtStart();
292  /**
293  * Removes a visible section from the ending of the currently visible set.
294  * \throw std::logic_error when there is no such section.
295  * \sa addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtStart
296  */
297  void removeVisibleSectionAtEnd();
298  /**
299  * Gets the axis pose of the first section, returning false if there is no such pose.
300  */
301  bool getFirstSectionPose(mrpt::poses::CPose3D &p);
302  /**
303  * Gets the axis pose of the last section, returning false if there is no such pose.
304  */
305  bool getLastSectionPose(mrpt::poses::CPose3D &p);
306  /**
307  * Gets the axis pose of the first visible section, returning false if there is no such pose.
308  */
309  bool getFirstVisibleSectionPose(mrpt::poses::CPose3D &p);
310  /**
311  * Gets the axis pose of the last section, returning false if there is no such pose.
312  */
313  bool getLastVisibleSectionPose(mrpt::poses::CPose3D &p);
314  /**
315  * Updates the mutable set of polygons used in ray tracing.
316  */
317  void updatePolys() const;
318 
319  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
320  void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE;
321 
322  private:
323  /**
324  * Updates the axis, transforming each point into a pose pointing to the next section.
325  */
326  void generatePoses(const std::vector<mrpt::math::TPoint3D> &pIn, mrpt::aligned_containers<mrpt::poses::CPose3D>::vector_t &pOut);
327  /**
328  * Updates the mutable mesh.
329  */
330  void updateMesh() const;
331  /**
332  * Given a vector of polyhedrons, gets the starting and ending iterators to the section to be actually rendered.
333  */
334  void getMeshIterators(const std::vector<TQuadrilateral> &m,std::vector<TQuadrilateral>::const_iterator &begin,std::vector<TQuadrilateral>::const_iterator &end) const;
335  /**
336  * Basic constructor with default initialization.
337  */
338  CGeneralizedCylinder():axis(),generatrix(),mesh(),meshUpToDate(false),polysUpToDate(false),closed(false),fullyVisible(true) {}
339  /**
340  * Constructor with axis and generatrix.
341  */
342  CGeneralizedCylinder(const std::vector<mrpt::math::TPoint3D> &a,const std::vector<mrpt::math::TPoint3D> &g):generatrix(g),mesh(),meshUpToDate(false),polysUpToDate(false),closed(false),fullyVisible(true) {
343  generatePoses(a,axis);
344  }
345  /**
346  * Destructor.
347  */
348  virtual ~CGeneralizedCylinder() {};
349  };
351 }
352 }
353 #endif
void setAllSectionsVisible()
Sets all sections visible.
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:3702
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
void setClosed(bool c=true)
Set whether each section is a closed polygon or not.
bool BASE_IMPEXP traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons&#39; properties.
Definition: geometry.cpp:1989
GLint * first
Definition: glext.h:3703
void getAxis(mrpt::aligned_containers< mrpt::poses::CPose3D >::vector_t &a) const
Get axis, including angular coordinates.
size_t getNumberOfSections() const
Get the number of sections in this cylinder.
bool fullyVisible
Flag to determine whether the object is fully visible or only some sections are.
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:26
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) ...
STL namespace.
std::vector< TQuadrilateral > mesh
Mutable object with mesh information, used to avoid repeated computations.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
bool meshUpToDate
Mutable flag which tells if recalculations are needed.
CGeneralizedCylinder(const std::vector< mrpt::math::TPoint3D > &a, const std::vector< mrpt::math::TPoint3D > &g)
Constructor with axis and generatrix.
GLsizei const GLfloat * points
Definition: glext.h:4797
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
bool closed
Boolean variable which determines if the profile is closed at each section.
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
void setVisibleSections(size_t first, size_t last)
Sets which sections are visible.
void getClosedSection(size_t index, CPolyhedronPtr &poly) const
Get a polyhedron consisting of a single section of the cylinder.
void getGeneratrix(std::vector< mrpt::math::TPoint3D > &g) const
Get cylinder&#39;s profile.
GLuint index
Definition: glext.h:3891
const GLubyte * c
Definition: glext.h:5590
void setGeneratrix(const std::vector< mrpt::math::TPoint3D > &g)
Set cylinder&#39;s profile.
GLuint GLuint end
Definition: glext.h:3512
GLubyte g
Definition: glext.h:5575
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.
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.
class OPENGL_IMPEXP CGeneralizedCylinder
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#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:72
std::vector< mrpt::math::TPoint3D > generatrix
Object&#39;s generatrix, that is, profile which will be extruded.
TQuadrilateral(const T(&p)[4])
Construction from any array of four compatible objects.
mrpt::aligned_containers< mrpt::poses::CPose3D >::vector_t axis
Cylinder&#39;s axis.
bool isClosed() const
Returns true if each section is a closed polygon.
CGeneralizedCylinder()
Basic constructor with default initialization.
GLuint res
Definition: glext.h:6298
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.
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
void addVisibleSectionAtEnd()
Adds another visible section at the end of the cylinder.
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
GLfloat GLfloat p
Definition: glext.h:5587
std::vector< TYPE1, Eigen::aligned_allocator< TYPE1 > > vector_t
3D polygon, inheriting from std::vector<TPoint3D>
size_t firstSection
First visible section, if fullyVisible is set to false.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020