Main MRPT website > C++ reference
MRPT logo
CGeneralizedCylinder.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef opengl_CGeneralizedCylinder_H
29 #define opengl_CGeneralizedCylinder_H
30 
34 #include <mrpt/math/geometry.h>
36 
37 namespace mrpt {
38 namespace opengl {
39  using namespace std;
40  using namespace mrpt::math;
42  // This must be added to any CSerializable derived class:
44  /**
45  * 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.
46  * \ingroup mrpt_opengl_grp
47  */
50  public:
51  /**
52  * Auxiliary struct holding any quadrilateral, represented by foour points.
53  */
55  private:
56  /**
57  * Automatically compute a vector normal to this quadrilateral.
58  */
59  void calculateNormal();
60  public:
61  /**
62  * Quadrilateral`'s points.
63  */
64  TPoint3D points[4];
65  /**
66  * Normal vector.
67  */
68  double normal[3];
69  /**
70  * Given a polygon with 4 already positions allocated, this method fills it with the quadrilateral points.
71  * \sa mrpt::math::TPolygon3D
72  */
73  inline void getAsPolygonUnsafe(mrpt::math::TPolygon3D &vec) const {
74  vec[0]=points[0];
75  vec[1]=points[1];
76  vec[2]=points[2];
77  vec[3]=points[3];
78  }
79  /**
80  * Constructor from 4 points.
81  */
82  TQuadrilateral(const TPoint3D &p1,const TPoint3D &p2,const TPoint3D &p3,const TPoint3D &p4) {
83  points[0]=p1;
84  points[1]=p2;
85  points[2]=p3;
86  points[3]=p4;
87  calculateNormal();
88  }
89  /**
90  * Construction from any array of four compatible objects.
91  */
92  template<class T> TQuadrilateral(const T (&p)[4]) {
93  for (int i=0;i<4;i++) points[i]=p[i];
94  calculateNormal();
95  }
96  /**
97  * Empty constructor. Initializes to garbage.
98  */
100  /**
101  * Destructor.
102  */
104  };
105  protected:
106  /**
107  * Cylinder's axis. It's represented as a pose because it holds the angle to get to the next pose.
108  */
109  vector<CPose3D> axis;
110  /**
111  * Object's generatrix, that is, profile which will be extruded.
112  */
113  vector<TPoint3D> generatrix;
114  /**
115  * Mutable object with mesh information, used to avoid repeated computations.
116  */
117  mutable std::vector<TQuadrilateral> mesh;
118  /**
119  * Mutable object with the cylinder's points, used to avoid repeated computations.
120  */
122  /**
123  * Mutable flag which tells if recalculations are needed.
124  */
125  mutable bool meshUpToDate;
126  /**
127  * Mutable set of data used in ray tracing.
128  * \sa mrpt::math::TPolygonWithPlane
129  */
130  mutable vector<TPolygonWithPlane> polys;
131  /**
132  * Mutable flag telling whether ray tracing temporary data must be recalculated or not.
133  */
134  mutable bool polysUpToDate;
135  /**
136  * Boolean variable which determines if the profile is closed at each section.
137  */
138  bool closed;
139  /**
140  * Flag to determine whether the object is fully visible or only some sections are.
141  */
143  /**
144  * First visible section, if fullyVisible is set to false.
145  * \sa fullyVisible,lastSection
146  */
147  size_t firstSection;
148  /**
149  * Last visible section, if fullyVisible is set to false.
150  * \sa fullyVisible,firstSection
151  */
152  size_t lastSection;
153  public:
154  /**
155  * Creation of generalized cylinder from axis and generatrix
156  */
157  static CGeneralizedCylinderPtr Create(const std::vector<TPoint3D> &axis,const std::vector<TPoint3D> &generatrix) {
158  return CGeneralizedCylinderPtr(new CGeneralizedCylinder(axis,generatrix));
159  }
160  /**
161  * Render.
162  * \sa mrpt::opengl::CRenderizable
163  */
164  void render_dl() const;
165  /**
166  * Ray tracing.
167  * \sa mrpt::opengl::CRenderizable.
168  */
169  virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
170  /**
171  * Get axis's spatial coordinates.
172  */
173  inline void getAxis(std::vector<TPoint3D> &a) const {
174  //a=axis;
175  size_t N=axis.size();
176  a.resize(N);
177  for (size_t i=0;i<N;i++) {
178  a[i].x=axis[i].x();
179  a[i].y=axis[i].y();
180  a[i].z=axis[i].z();
181  }
182  }
183  /**
184  * Get axis, including angular coordinates.
185  */
186  inline void getAxis(std::vector<CPose3D> &a) const {
187  a=axis;
188  }
189  /**
190  * Set the axis points.
191  */
192  inline void setAxis(const std::vector<TPoint3D> &a) {
193  generatePoses(a,axis);
194  meshUpToDate=false;
195  fullyVisible=true;
197  }
198  /**
199  * Get cylinder's profile.
200  */
201  inline void getGeneratrix(std::vector<TPoint3D> &g) const {
202  g=generatrix;
203  }
204  /**
205  * Set cylinder's profile.
206  */
207  inline void setGeneratrix(const std::vector<TPoint3D> g) {
208  generatrix=g;
209  meshUpToDate=false;
211  }
212  /**
213  * Returns true if each section is a closed polygon.
214  */
215  inline bool isClosed() const {
216  return closed;
217  }
218  /**
219  * Set whether each section is a closed polygon or not.
220  */
221  inline void setClosed(bool c=true) {
222  closed=c;
223  meshUpToDate=false;
225  }
226  /**
227  * Get a polyhedron containing the starting point of the cylinder (its "base").
228  * \sa getEnd,mrpt::opengl::CPolyhedron
229  */
230  void getOrigin(CPolyhedronPtr &poly) const;
231  /**
232  * Get a polyhedron containing the ending point of the cylinder (its "base").
233  * \sa getOrigin,mrpt::opengl::CPolyhedron
234  */
235  void getEnd(CPolyhedronPtr &poly) const;
236  /**
237  * Get the cylinder as a set of polygons in 3D.
238  * \sa mrpt::math::TPolygon3D
239  */
240  void generateSetOfPolygons(std::vector<TPolygon3D> &res) const;
241  /**
242  * Get a polyhedron consisting of a set of closed sections of the cylinder.
243  * \sa mrpt::opengl::CPolyhedron
244  */
245  void getClosedSection(size_t index1,size_t index2,CPolyhedronPtr &poly) const;
246  /**
247  * Get a polyhedron consisting of a single section of the cylinder.
248  * \sa mrpt::opengl::CPolyhedron
249  */
250  inline void getClosedSection(size_t index,CPolyhedronPtr &poly) const {
251  getClosedSection(index,index,poly);
252  }
253  /**
254  * Get the number of sections in this cylinder.
255  */
256  inline size_t getNumberOfSections() const {
257  return axis.size()?(axis.size()-1):0;
258  }
259  /**
260  * Get how many visible sections are in the cylinder.
261  */
262  inline size_t getVisibleSections() const {
263  return fullyVisible?getNumberOfSections():(lastSection-firstSection);
264  }
265  /**
266  * Gets the cylinder's visible sections.
267  */
268  void getVisibleSections(size_t &first,size_t &last) const {
269  if (fullyVisible) {
270  first=0;
271  last=getNumberOfSections();
272  } else {
273  first=firstSection;
274  last=lastSection;
275  }
276  }
277  /**
278  * Sets all sections visible.
279  */
280  inline void setAllSectionsVisible() {
281  fullyVisible=true;
283  }
284  /**
285  * Hides all sections.
286  */
287  inline void setAllSectionsInvisible(size_t pointer=0) {
288  fullyVisible=false;
289  firstSection=pointer;
290  lastSection=pointer;
292  }
293  /**
294  * Sets which sections are visible.
295  * \throw std::logic_error on wrongly defined bounds.
296  */
297  inline void setVisibleSections(size_t first,size_t last) {
298  fullyVisible=false;
299  if (first>last||last>getNumberOfSections()) throw std::logic_error("Wrong bound definition");
300  firstSection=first;
301  lastSection=last;
303  }
304  /**
305  * Adds another visible section at the start of the cylinder. The cylinder must have an invisble section to display.
306  * \throw std::logic_error if there is no section to add to the displaying set.
307  * \sa addVisibleSectionAtEnd,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
308  */
309  inline void addVisibleSectionAtStart() {
310  if (fullyVisible||firstSection==0) throw std::logic_error("No more sections");
311  firstSection--;
313  }
314  /**
315  * Adds another visible section at the end of the cylinder. The cylinder must have an invisible section to display.
316  * \throw std::logic_error if there is no section to add to the displaying set.
317  * \sa addVisibleSectionAtStart,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
318  */
319  inline void addVisibleSectionAtEnd() {
320  if (fullyVisible||lastSection==getNumberOfSections()) throw std::logic_error("No more sections");
321  lastSection++;
323  }
324  /**
325  * Removes a visible section from the start of the currently visible set.
326  * \throw std::logic_error if there are no visible sections.
327  * \sa addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtEnd
328  */
329  void removeVisibleSectionAtStart();
330  /**
331  * Removes a visible section from the ending of the currently visible set.
332  * \throw std::logic_error when there is no such section.
333  * \sa addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtStart
334  */
335  void removeVisibleSectionAtEnd();
336  /**
337  * Gets the axis pose of the first section, returning false if there is no such pose.
338  */
339  bool getFirstSectionPose(mrpt::poses::CPose3D &p);
340  /**
341  * Gets the axis pose of the last section, returning false if there is no such pose.
342  */
343  bool getLastSectionPose(mrpt::poses::CPose3D &p);
344  /**
345  * Gets the axis pose of the first visible section, returning false if there is no such pose.
346  */
347  bool getFirstVisibleSectionPose(mrpt::poses::CPose3D &p);
348  /**
349  * Gets the axis pose of the last section, returning false if there is no such pose.
350  */
351  bool getLastVisibleSectionPose(mrpt::poses::CPose3D &p);
352  /**
353  * Updates the mutable set of polygons used in ray tracing.
354  */
355  void updatePolys() const;
356  private:
357  /**
358  * Updates the axis, transforming each point into a pose pointing to the next section.
359  */
360  void generatePoses(const std::vector<TPoint3D> &pIn,std::vector<CPose3D> &pOut);
361  /**
362  * Updates the mutable mesh.
363  */
364  void updateMesh() const;
365  /**
366  * Given a vector of polyhedrons, gets the starting and ending iterators to the section to be actually rendered.
367  */
368  void getMeshIterators(const vector<TQuadrilateral> &m,vector<TQuadrilateral>::const_iterator &begin,vector<TQuadrilateral>::const_iterator &end) const;
369  /**
370  * Basic constructor with default initialization.
371  */
372  CGeneralizedCylinder():axis(),generatrix(),mesh(),meshUpToDate(false),polysUpToDate(false),closed(false),fullyVisible(true) {}
373  /**
374  * Constructor with axis and generatrix.
375  */
376  CGeneralizedCylinder(const std::vector<TPoint3D> &a,const std::vector<TPoint3D> &g):generatrix(g),mesh(),meshUpToDate(false),polysUpToDate(false),closed(false),fullyVisible(true) {
377  generatePoses(a,axis);
378  }
379  /**
380  * Destructor.
381  */
382  virtual ~CGeneralizedCylinder() {};
383  };
384 }
385 }
386 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013