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



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014