MRPT  1.9.9
CGeneralizedCylinder.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/math/geometry.h>
16 #include <vector>
17 
18 namespace mrpt::opengl
19 {
20 class CGeneralizedCylinder;
21 /**
22  * This object represents any figure obtained by extruding any profile along a
23  * given axis. The profile should lie over a x=0 plane, and the axis must be
24  * roughly perpendicular to this plane. In particular, it should be almost
25  * perpendicular to the Z axis.
26  * \ingroup mrpt_opengl_grp
27  */
29 {
31  public:
32  /**
33  * Auxiliary struct holding any quadrilateral, represented by foour points.
34  */
36  {
37  private:
38  /**
39  * Automatically compute a vector normal to this quadrilateral.
40  */
41  void calculateNormal();
42 
43  public:
44  /**
45  * Quadrilateral`'s points.
46  */
48  /**
49  * Normal vector.
50  */
51  double normal[3];
52  /**
53  * Given a polygon with 4 already positions allocated, this method
54  * fills it with the quadrilateral points.
55  * \sa mrpt::math::TPolygon3D
56  */
58  {
59  vec[0] = points[0];
60  vec[1] = points[1];
61  vec[2] = points[2];
62  vec[3] = points[3];
63  }
64  /**
65  * Constructor from 4 points.
66  */
68  const mrpt::math::TPoint3D& p1, const mrpt::math::TPoint3D& p2,
69  const mrpt::math::TPoint3D& p3, const mrpt::math::TPoint3D& p4)
70  {
71  points[0] = p1;
72  points[1] = p2;
73  points[2] = p3;
74  points[3] = p4;
76  }
77  /**
78  * Construction from any array of four compatible objects.
79  */
80  template <class T>
81  TQuadrilateral(const T (&p)[4])
82  {
83  for (int i = 0; i < 4; i++) points[i] = p[i];
85  }
86  /**
87  * Empty constructor. Initializes to garbage.
88  */
89  TQuadrilateral() = default;
90  /**
91  * Destructor.
92  */
93  ~TQuadrilateral() = default;
94  };
95 
96  protected:
97  /** Cylinder's axis. It's represented as a pose because it holds the angle
98  * to get to the next pose. */
99  std::vector<mrpt::poses::CPose3D> axis;
100  /** Object's generatrix, that is, profile which will be extruded. */
101  std::vector<mrpt::math::TPoint3D> generatrix;
102  /** Mutable object with mesh information, used to avoid repeated
103  * computations. */
104  mutable std::vector<TQuadrilateral> mesh;
105  /** Mutable object with the cylinder's points, used to avoid repeated
106  * computations. */
108  /** Mutable flag which tells if recalculations are needed. */
109  mutable bool meshUpToDate{false};
110  /**
111  * Mutable set of data used in ray tracing.
112  * \sa mrpt::math::TPolygonWithPlane
113  */
114  mutable std::vector<mrpt::math::TPolygonWithPlane> polys;
115  /** Mutable flag telling whether ray tracing temporary data must be
116  * recalculated or not. */
117  mutable bool polysUpToDate{false};
118  /** Boolean variable which determines if the profile is closed at each
119  * section. */
120  bool closed{false};
121  /** Flag to determine whether the object is fully visible or only some
122  * sections are. */
123  bool fullyVisible{true};
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 
135  public:
136  /**
137  * Render.
138  * \sa mrpt::opengl::CRenderizable
139  */
140  void render_dl() const override;
141  /**
142  * Ray tracing.
143  * \sa mrpt::opengl::CRenderizable.
144  */
145  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
146  /**
147  * Get axis's spatial coordinates.
148  */
149  inline void getAxis(std::vector<mrpt::math::TPoint3D>& a) const
150  {
151  // a=axis;
152  size_t N = axis.size();
153  a.resize(N);
154  for (size_t i = 0; i < N; i++)
155  {
156  a[i].x = axis[i].x();
157  a[i].y = axis[i].y();
158  a[i].z = axis[i].z();
159  }
160  }
161  /**
162  * Get axis, including angular coordinates.
163  */
164  inline void getAxis(std::vector<mrpt::poses::CPose3D>& a) const
165  {
166  a = axis;
167  }
168  /**
169  * Set the axis points.
170  */
171  inline void setAxis(const std::vector<mrpt::math::TPoint3D>& a)
172  {
173  generatePoses(a, axis);
174  meshUpToDate = false;
175  fullyVisible = true;
177  }
178  /**
179  * Get cylinder's profile.
180  */
181  inline void getGeneratrix(std::vector<mrpt::math::TPoint3D>& g) const
182  {
183  g = generatrix;
184  }
185  /**
186  * Set cylinder's profile.
187  */
188  inline void setGeneratrix(const std::vector<mrpt::math::TPoint3D>& g)
189  {
190  generatrix = g;
191  meshUpToDate = false;
193  }
194  /**
195  * Returns true if each section is a closed polygon.
196  */
197  inline bool isClosed() const { return closed; }
198  /**
199  * Set whether each section is a closed polygon or not.
200  */
201  inline void setClosed(bool c = true)
202  {
203  closed = c;
204  meshUpToDate = false;
206  }
207  /**
208  * Get a polyhedron containing the starting point of the cylinder (its
209  * "base").
210  * \sa getEnd,mrpt::opengl::CPolyhedron
211  */
212  void getOrigin(CPolyhedron::Ptr& poly) const;
213  /**
214  * Get a polyhedron containing the ending point of the cylinder (its
215  * "base").
216  * \sa getOrigin,mrpt::opengl::CPolyhedron
217  */
218  void getEnd(CPolyhedron::Ptr& poly) const;
219  /**
220  * Get the cylinder as a set of polygons in 3D.
221  * \sa mrpt::math::TPolygon3D
222  */
223  void generateSetOfPolygons(std::vector<mrpt::math::TPolygon3D>& res) const;
224  /**
225  * Get a polyhedron consisting of a set of closed sections of the cylinder.
226  * \sa mrpt::opengl::CPolyhedron
227  */
228  void getClosedSection(
229  size_t index1, size_t index2, CPolyhedron::Ptr& poly) const;
230  /**
231  * Get a polyhedron consisting of a single section of the cylinder.
232  * \sa mrpt::opengl::CPolyhedron
233  */
234  inline void getClosedSection(size_t index, CPolyhedron::Ptr& poly) const
235  {
236  getClosedSection(index, index, poly);
237  }
238  /**
239  * Get the number of sections in this cylinder.
240  */
241  inline size_t getNumberOfSections() const
242  {
243  return axis.size() ? (axis.size() - 1) : 0;
244  }
245  /**
246  * Get how many visible sections are in the cylinder.
247  */
248  inline size_t getVisibleSections() const
249  {
252  }
253  /**
254  * Gets the cylinder's visible sections.
255  */
256  void getVisibleSections(size_t& first, size_t& last) const
257  {
258  if (fullyVisible)
259  {
260  first = 0;
261  last = getNumberOfSections();
262  }
263  else
264  {
265  first = firstSection;
266  last = lastSection;
267  }
268  }
269  /**
270  * Sets all sections visible.
271  */
272  inline void setAllSectionsVisible()
273  {
274  fullyVisible = true;
276  }
277  /**
278  * Hides all sections.
279  */
280  inline void setAllSectionsInvisible(size_t pointer = 0)
281  {
282  fullyVisible = false;
283  firstSection = pointer;
284  lastSection = pointer;
286  }
287  /**
288  * Sets which sections are visible.
289  * \throw std::logic_error on wrongly defined bounds.
290  */
291  inline void setVisibleSections(size_t first, size_t last)
292  {
293  fullyVisible = false;
294  if (first > last || last > getNumberOfSections())
295  throw std::logic_error("Wrong bound definition");
296  firstSection = first;
297  lastSection = last;
299  }
300  /**
301  * Adds another visible section at the start of the cylinder. The cylinder
302  * must have an invisble section to display.
303  * \throw std::logic_error if there is no section to add to the displaying
304  * set.
305  * \sa
306  * addVisibleSectionAtEnd,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
307  */
309  {
310  if (fullyVisible || firstSection == 0)
311  throw std::logic_error("No more sections");
312  firstSection--;
314  }
315  /**
316  * Adds another visible section at the end of the cylinder. The cylinder
317  * must have an invisible section to display.
318  * \throw std::logic_error if there is no section to add to the displaying
319  * set.
320  * \sa
321  * addVisibleSectionAtStart,removeVisibleSectionAtStart,removeVisibleSectionAtEnd
322  */
324  {
326  throw std::logic_error("No more sections");
327  lastSection++;
329  }
330  /**
331  * Removes a visible section from the start of the currently visible set.
332  * \throw std::logic_error if there are no visible sections.
333  * \sa
334  * addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtEnd
335  */
337  /**
338  * Removes a visible section from the ending of the currently visible set.
339  * \throw std::logic_error when there is no such section.
340  * \sa
341  * addVisibleSectionAtStart,addVisibleSectionAtEnd,removeVisibleSectionAtStart
342  */
344  /**
345  * Gets the axis pose of the first section, returning false if there is no
346  * such pose.
347  */
349  /**
350  * Gets the axis pose of the last section, returning false if there is no
351  * such pose.
352  */
354  /**
355  * Gets the axis pose of the first visible section, returning false if
356  * there is no such pose.
357  */
359  /**
360  * Gets the axis pose of the last section, returning false if there is no
361  * such pose.
362  */
364  /**
365  * Updates the mutable set of polygons used in ray tracing.
366  */
367  void updatePolys() const;
368 
369  /** Evaluates the bounding box of this object (including possible children)
370  * in the coordinate frame of the object parent. */
371  void getBoundingBox(
373  mrpt::math::TPoint3D& bb_max) const override;
374 
375  private:
376  /**
377  * Updates the axis, transforming each point into a pose pointing to the
378  * next section.
379  */
380  void generatePoses(
381  const std::vector<mrpt::math::TPoint3D>& pIn,
382  std::vector<mrpt::poses::CPose3D>& pOut);
383  /**
384  * Updates the mutable mesh.
385  */
386  void updateMesh() const;
387  /**
388  * Given a vector of polyhedrons, gets the starting and ending iterators to
389  * the section to be actually rendered.
390  */
391  void getMeshIterators(
392  const std::vector<TQuadrilateral>& m,
393  std::vector<TQuadrilateral>::const_iterator& begin,
394  std::vector<TQuadrilateral>::const_iterator& end) const;
395 
396  public:
397  /**
398  * Basic constructor with default initialization.
399  */
401  /**
402  * Constructor with axis and generatrix.
403  */
405  const std::vector<mrpt::math::TPoint3D>& a,
406  const std::vector<mrpt::math::TPoint3D>& g)
407  : generatrix(g),
408  mesh(),
409  meshUpToDate(false),
410  polysUpToDate(false),
411  closed(false),
412  fullyVisible(true)
413  {
414  generatePoses(a, axis);
415  }
416  /**
417  * Destructor.
418  */
419  ~CGeneralizedCylinder() override = default;
420 };
421 } // namespace mrpt::opengl
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 notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
mrpt::math::CMatrixDynamic< mrpt::math::TPoint3D_data > pointsMesh
Mutable object with the cylinder&#39;s points, used to avoid repeated computations.
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...
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.
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.
std::vector< mrpt::poses::CPose3D > axis
Cylinder&#39;s axis.
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...
void addVisibleSectionAtStart()
Adds another visible section at the start of the cylinder.
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...
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.
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
mrpt::math::TPoint3D points[4]
Quadrilateral`&#39;s points.
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.
void getAxis(std::vector< mrpt::poses::CPose3D > &a) const
Get axis, including angular coordinates.
void setGeneratrix(const std::vector< mrpt::math::TPoint3D > &g)
Set cylinder&#39;s profile.
void setAllSectionsInvisible(size_t pointer=0)
Hides all sections.
void generatePoses(const std::vector< mrpt::math::TPoint3D > &pIn, std::vector< mrpt::poses::CPose3D > &pOut)
Updates the axis, transforming each point into a pose pointing to the next section.
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...
~CGeneralizedCylinder() override=default
Destructor.
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.
const_iterator end() const
Definition: ts_hash_map.h:246
size_t lastSection
Last visible section, if fullyVisible is set to false.
const_iterator begin() const
Definition: ts_hash_map.h:240
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:85
void updateMesh() const
Updates the mutable mesh.
std::vector< mrpt::math::TPoint3D > generatrix
Object&#39;s generatrix, that is, profile which will be extruded.
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
const auto bb_max
TQuadrilateral(const T(&p)[4])
Construction from any array of four compatible objects.
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
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.
const auto bb_min
bool isClosed() const
Returns true if each section is a closed polygon.
CGeneralizedCylinder()
Basic constructor with default initialization.
bool getLastVisibleSectionPose(mrpt::poses::CPose3D &p)
Gets the axis pose of the last section, returning false if there is no such pose. ...
Lightweight 3D point.
Definition: TPoint3D.h:90
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.
3D polygon, inheriting from std::vector<TPoint3D>
Definition: TPolygon3D.h:18
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: 7e629e01a Sat Dec 14 00:05:55 2019 +0100 at sáb dic 14 00:15:10 CET 2019