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 |
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  /**
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>
82  {
83  for (int i = 0; i < 4; i++) points[i] = p[i];
85  }
86  /**
87  * Empty constructor. Initializes to garbage.
88  */
90  /**
91  * Destructor.
92  */
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. */
105  /** Mutable object with the cylinder's points, used to avoid repeated
106  * computations. */
109  /** Mutable flag which tells if recalculations are needed. */
110  mutable bool meshUpToDate{false};
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{false};
119  /** Boolean variable which determines if the profile is closed at each
120  * section. */
121  bool closed{false};
122  /** Flag to determine whether the object is fully visible or only some
123  * sections are. */
124  bool fullyVisible{true};
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  * Render.
139  * \sa mrpt::opengl::CRenderizable
140  */
141  void render_dl() const override;
142  /**
143  * Ray tracing.
144  * \sa mrpt::opengl::CRenderizable.
145  */
146  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
147  /**
148  * Get axis's spatial coordinates.
149  */
150  inline void getAxis(std::vector<mrpt::math::TPoint3D>& a) const
151  {
152  // a=axis;
153  size_t N = axis.size();
154  a.resize(N);
155  for (size_t i = 0; i < N; i++)
156  {
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<mrpt::poses::CPose3D>& a) const
166  {
167  a = axis;
168  }
169  /**
170  * Set the axis points.
171  */
172  inline void setAxis(const std::vector<mrpt::math::TPoint3D>& a)
173  {
174  generatePoses(a, axis);
175  meshUpToDate = false;
176  fullyVisible = true;
178  }
179  /**
180  * Get cylinder's profile.
181  */
182  inline void getGeneratrix(std::vector<mrpt::math::TPoint3D>& g) const
183  {
184  g = generatrix;
185  }
186  /**
187  * Set cylinder's profile.
188  */
189  inline void setGeneratrix(const std::vector<mrpt::math::TPoint3D>& g)
190  {
191  generatrix = g;
192  meshUpToDate = false;
194  }
195  /**
196  * Returns true if each section is a closed polygon.
197  */
198  inline bool isClosed() const { return closed; }
199  /**
200  * Set whether each section is a closed polygon or not.
201  */
202  inline void setClosed(bool c = true)
203  {
204  closed = c;
205  meshUpToDate = false;
207  }
208  /**
209  * Get a polyhedron containing the starting point of the cylinder (its
210  * "base").
211  * \sa getEnd,mrpt::opengl::CPolyhedron
212  */
213  void getOrigin(CPolyhedron::Ptr& poly) const;
214  /**
215  * Get a polyhedron containing the ending point of the cylinder (its
216  * "base").
217  * \sa getOrigin,mrpt::opengl::CPolyhedron
218  */
219  void getEnd(CPolyhedron::Ptr& poly) const;
220  /**
221  * Get the cylinder as a set of polygons in 3D.
222  * \sa mrpt::math::TPolygon3D
223  */
224  void generateSetOfPolygons(std::vector<mrpt::math::TPolygon3D>& res) const;
225  /**
226  * Get a polyhedron consisting of a set of closed sections of the cylinder.
227  * \sa mrpt::opengl::CPolyhedron
228  */
229  void getClosedSection(
230  size_t index1, size_t index2, CPolyhedron::Ptr& poly) const;
231  /**
232  * Get a polyhedron consisting of a single section of the cylinder.
233  * \sa mrpt::opengl::CPolyhedron
234  */
235  inline void getClosedSection(size_t index, CPolyhedron::Ptr& poly) const
236  {
237  getClosedSection(index, index, poly);
238  }
239  /**
240  * Get the number of sections in this cylinder.
241  */
242  inline size_t getNumberOfSections() const
243  {
244  return axis.size() ? (axis.size() - 1) : 0;
245  }
246  /**
247  * Get how many visible sections are in the cylinder.
248  */
249  inline size_t getVisibleSections() const
250  {
253  }
254  /**
255  * Gets the cylinder's visible sections.
256  */
257  void getVisibleSections(size_t& first, size_t& last) const
258  {
259  if (fullyVisible)
260  {
261  first = 0;
262  last = getNumberOfSections();
263  }
264  else
265  {
266  first = firstSection;
267  last = lastSection;
268  }
269  }
270  /**
271  * Sets all sections visible.
272  */
273  inline void setAllSectionsVisible()
274  {
275  fullyVisible = true;
277  }
278  /**
279  * Hides all sections.
280  */
281  inline void setAllSectionsInvisible(size_t pointer = 0)
282  {
283  fullyVisible = false;
284  firstSection = pointer;
285  lastSection = pointer;
287  }
288  /**
289  * Sets which sections are visible.
290  * \throw std::logic_error on wrongly defined bounds.
291  */
292  inline void setVisibleSections(size_t first, size_t last)
293  {
294  fullyVisible = false;
295  if (first > last || last > getNumberOfSections())
296  throw std::logic_error("Wrong bound definition");
297  firstSection = first;
298  lastSection = last;
300  }
301  /**
302  * Adds another visible section at the start of the cylinder. The cylinder
303  * must have an invisble section to display.
304  * \throw std::logic_error if there is no section to add to the displaying
305  * set.
306  * \sa
308  */
310  {
311  if (fullyVisible || firstSection == 0)
312  throw std::logic_error("No more sections");
313  firstSection--;
315  }
316  /**
317  * Adds another visible section at the end of the cylinder. The cylinder
318  * must have an invisible section to display.
319  * \throw std::logic_error if there is no section to add to the displaying
320  * set.
321  * \sa
323  */
325  {
327  throw std::logic_error("No more sections");
328  lastSection++;
330  }
331  /**
332  * Removes a visible section from the start of the currently visible set.
333  * \throw std::logic_error if there are no visible sections.
334  * \sa
336  */
338  /**
339  * Removes a visible section from the ending of the currently visible set.
340  * \throw std::logic_error when there is no such section.
341  * \sa
343  */
345  /**
346  * Gets the axis pose of the first section, returning false if there is no
347  * such pose.
348  */
350  /**
351  * Gets the axis pose of the last section, returning false if there is no
352  * such pose.
353  */
355  /**
356  * Gets the axis pose of the first visible section, returning false if
357  * there is no such pose.
358  */
360  /**
361  * Gets the axis pose of the last section, returning false if there is no
362  * such pose.
363  */
365  /**
366  * Updates the mutable set of polygons used in ray tracing.
367  */
368  void updatePolys() const;
369
370  /** Evaluates the bounding box of this object (including possible children)
371  * in the coordinate frame of the object parent. */
372  void getBoundingBox(
374  mrpt::math::TPoint3D& bb_max) const override;
375
376  private:
377  /**
378  * Updates the axis, transforming each point into a pose pointing to the
379  * next section.
380  */
381  void generatePoses(
382  const std::vector<mrpt::math::TPoint3D>& pIn,
383  std::vector<mrpt::poses::CPose3D>& pOut);
384  /**
385  * Updates the mutable mesh.
386  */
387  void updateMesh() const;
388  /**
389  * Given a vector of polyhedrons, gets the starting and ending iterators to
390  * the section to be actually rendered.
391  */
392  void getMeshIterators(
396
397  public:
398  /**
399  * Basic constructor with default initialization.
400  */
402  /**
403  * Constructor with axis and generatrix.
404  */
406  const std::vector<mrpt::math::TPoint3D>& a,
407  const std::vector<mrpt::math::TPoint3D>& g)
408  : generatrix(g),
409  mesh(),
410  meshUpToDate(false),
411  polysUpToDate(false),
412  closed(false),
413  fullyVisible(true)
414  {
415  generatePoses(a, axis);
416  }
417  /**
418  * Destructor.
419  */
420  ~CGeneralizedCylinder() override = default;
421 };
422 } // 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) ...
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...
Adds another visible section at the start of the cylinder.
Mutable object with mesh information, used to avoid repeated computations.
bool meshUpToDate
Mutable flag which tells if recalculations are needed.
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]
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
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
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. ...
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.
This template class provides the basic functionality for a general 2D any-size, resizable container o...