MRPT  2.0.4
CSetOfObjects.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-2020, 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/poses/poses_frwds.h> // All these are needed for the auxiliary methods posePDF2opengl()
13 
14 namespace mrpt::opengl
15 {
16 /** A set of object, which are referenced to the coordinates framework
17  *established in this object.
18  * It can be established a hierarchy of "CSetOfObjects", where the coordinates
19  *framework of each
20  * one will be referenced to the parent's one.
21  * The list of child objects is accessed directly as in the class
22  *"COpenGLScene"
23  * \sa opengl::COpenGLScene
24  * \ingroup mrpt_opengl_grp
25  */
27 {
29 
30  protected:
31  /** The list of child objects.
32  * Objects are automatically deleted when calling "clear" or in the
33  * destructor.
34  */
36 
37  public:
38  CSetOfObjects() = default;
39  virtual ~CSetOfObjects() override = default;
40 
41  using const_iterator = CListOpenGLObjects::const_iterator;
42  using iterator = CListOpenGLObjects::iterator;
43 
44  inline const_iterator begin() const { return m_objects.begin(); }
45  inline const_iterator end() const { return m_objects.end(); }
46  inline iterator begin() { return m_objects.begin(); }
47  inline iterator end() { return m_objects.end(); }
48  /** Inserts a set of objects into the list.
49  */
50  template <class T>
51  inline void insertCollection(const T& objs)
52  {
53  insert(objs.begin(), objs.end());
54  }
55  /** Insert a new object to the list.
56  */
57  void insert(const CRenderizable::Ptr& newObject);
58 
59  /** Inserts a set of objects, bounded by iterators, into the list.
60  */
61  template <class T_it>
62  inline void insert(const T_it& begin, const T_it& end)
63  {
64  for (T_it it = begin; it != end; it++) insert(*it);
65  }
66 
67  shader_list_t requiredShaders() const override
68  {
69  // For this container class, it actually doesn't matter the type of
70  // shader, since each children will register using the adequate shader
71  // ID. But the method is virtual, so we must provide an arbitrary one
72  // here anyway:
74  }
75  void render(const RenderContext& rc) const override;
76  void renderUpdateBuffers() const override;
78  const mrpt::opengl::TRenderMatrices& state,
79  RenderQueue& rq) const override;
80  void freeOpenGLResources() override
81  {
82  for (auto& o : m_objects)
83  if (o) o->freeOpenGLResources();
84  }
85  void initializeTextures() const override
86  {
87  for (const auto& o : m_objects)
88  if (o) o->initializeTextures();
89  }
90 
91  /** Clear the list of objects in the scene, deleting objects' memory.
92  */
93  void clear();
94 
95  /** Returns number of objects. */
96  size_t size() { return m_objects.size(); }
97  /** Returns true if there are no objects. */
98  inline bool empty() const { return m_objects.empty(); }
99 
100  /** Returns the first object with a given name, or a nullptr pointer if not
101  * found.
102  */
103  CRenderizable::Ptr getByName(const std::string& str);
104 
105  /** Returns the i'th object of a given class (or of a descendant class), or
106  nullptr (an empty smart pointer) if not found.
107  * Example:
108  * \code
109  CSphere::Ptr obs = myscene.getByClass<CSphere>();
110  * \endcode
111  * By default (ith=0), the first observation is returned.
112  */
113  template <typename T>
114  typename T::Ptr getByClass(size_t ith = 0) const;
115 
116  /** Removes the given object from the scene (it also deletes the object to
117  * free its memory).
118  */
119  void removeObject(const CRenderizable::Ptr& obj);
120 
121  /** Retrieves a list of all objects in text form */
122  void dumpListOfObjects(std::vector<std::string>& lst);
123 
124  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
125 
126  CRenderizable& setColor_u8(const mrpt::img::TColor& c) override;
127  CRenderizable& setColorR_u8(const uint8_t r) override;
128  CRenderizable& setColorG_u8(const uint8_t g) override;
129  CRenderizable& setColorB_u8(const uint8_t b) override;
130  CRenderizable& setColorA_u8(const uint8_t a) override;
131  bool contains(const CRenderizable::Ptr& obj) const;
132  void getBoundingBox(
134  mrpt::math::TPoint3D& bb_max) const override;
135 
136  /** @name pose_pdf -> 3d objects auxiliary templates
137  @{ */
138  // The reason this code is here is to exploit C++'s "T::template function()"
139  // in order to
140  // define the members getAs3DObject() in several classes in mrpt-base with
141  // its argument
142  // being a class (CSetOfObjects) which is actually declared here, in
143  // mrpt-opengl.
144  // Implementations are in "pose_pdfs.cpp", not in "CSetOfObjects" (historic
145  // reasons...)
146 
147  /** Returns a representation of a the PDF - this is just an auxiliary
148  * function, it's more natural to call
149  * mrpt::poses::CPosePDF::getAs3DObject */
151 
152  /** Returns a representation of a the PDF - this is just an auxiliary
153  * function, it's more natural to call
154  * mrpt::poses::CPointPDF::getAs3DObject */
156 
157  /** Returns a representation of a the PDF - this is just an auxiliary
158  * function, it's more natural to call
159  * mrpt::poses::CPose3DPDF::getAs3DObject */
161 
162  /** Returns a representation of a the PDF - this is just an auxiliary
163  * function, it's more natural to call
164  * mrpt::poses::CPose3DQuatPDF::getAs3DObject */
166  const mrpt::poses::CPose3DQuatPDF& o);
167 
168  /** @} */
169 };
170 /** Inserts an object into the list. Allows call chaining.
171  * \sa mrpt::opengl::CSetOfObjects::insert
172  */
175 {
176  s->insert(r);
177  return s;
178 }
179 /** Inserts a set of objects into the list. Allows call chaining.
180  * \sa mrpt::opengl::CSetOfObjects::insert
181  */
182 template <class T>
184  CSetOfObjects::Ptr& o, const std::vector<T>& v)
185 {
186  o->insertCollection(v);
187  return o;
188 }
189 
190 // Implementation: (here because it needs the _POST macro defining the
191 // Smart::Ptr)
192 template <typename T>
193 typename T::Ptr CSetOfObjects::getByClass(size_t ith) const
194 {
195  MRPT_START
196  size_t foundCount = 0;
197  const auto class_ID = &T::GetRuntimeClassIdStatic();
198  for (const auto& o : m_objects)
199  if (o && o->GetRuntimeClass()->derivedFrom(class_ID))
200  if (foundCount++ == ith) return std::dynamic_pointer_cast<T>(o);
201 
202  // If not found directly, search recursively:
203  for (const auto& o : m_objects)
204  {
205  if (o && o->GetRuntimeClass() ==
207  {
208  typename T::Ptr obj = std::dynamic_pointer_cast<CSetOfObjects>(o)
209  ->template getByClass<T>(ith);
210  if (obj) return obj;
211  }
212  }
213 
214  return typename T::Ptr(); // Not found: return empty smart pointer
215  MRPT_END
216 }
217 } // namespace mrpt::opengl
void clear()
Clear the list of objects in the scene, deleting objects&#39; memory.
#define MRPT_START
Definition: exceptions.h:241
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:26
void initializeTextures() const override
Initializes all textures (loads them into opengl memory).
Definition: CSetOfObjects.h:85
void insertCollection(const T &objs)
Inserts a set of objects into the list.
Definition: CSetOfObjects.h:51
CListOpenGLObjects::const_iterator const_iterator
Definition: CSetOfObjects.h:41
std::deque< CRenderizable::Ptr > CListOpenGLObjects
A list of smart pointers to renderizable objects.
bool contains(const CRenderizable::Ptr &obj) const
CListOpenGLObjects m_objects
The list of child objects.
Definition: CSetOfObjects.h:35
std::shared_ptr< mrpt::opengl ::CSetOfObjects > Ptr
Definition: CSetOfObjects.h:28
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Simulation of ray-trace, given a pose.
COpenGLScene::Ptr & operator<<(COpenGLScene::Ptr &s, const CRenderizable::Ptr &r)
Inserts an openGL object into a scene.
Definition: COpenGLScene.h:252
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
bool empty() const
Returns true if there are no objects.
Definition: CSetOfObjects.h:98
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:50
CRenderizable & setColorR_u8(const uint8_t r) override
Color components in the range [0,255].
std::vector< shader_id_t > shader_list_t
A list of shader IDs.
Definition: Shader.h:26
T::Ptr getByClass(size_t ith=0) const
Returns the i&#39;th object of a given class (or of a descendant class), or nullptr (an empty smart point...
CRenderizable & setColorG_u8(const uint8_t g) override
Color components in the range [0,255].
void insert(const T_it &begin, const T_it &end)
Inserts a set of objects, bounded by iterators, into the list.
Definition: CSetOfObjects.h:62
const_iterator end() const
Definition: CSetOfObjects.h:45
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
size_t size()
Returns number of objects.
Definition: CSetOfObjects.h:96
CRenderizable & setColorA_u8(const uint8_t a) override
Color components in the range [0,255].
static constexpr shader_id_t WIREFRAME
virtual ~CSetOfObjects() override=default
Rendering state related to the projection and model-view matrices.
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:38
const_iterator begin() const
Definition: CSetOfObjects.h:44
CListOpenGLObjects::iterator iterator
Definition: CSetOfObjects.h:42
void removeObject(const CRenderizable::Ptr &obj)
Removes the given object from the scene (it also deletes the object to free its memory).
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:105
static CSetOfObjects::Ptr posePDF2opengl(const mrpt::poses::CPosePDF &o)
Returns a representation of a the PDF - this is just an auxiliary function, it&#39;s more natural to call...
Definition: pose_pdfs.cpp:44
void freeOpenGLResources() override
Free opengl buffers.
Definition: CSetOfObjects.h:80
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form.
Declares a class that represents a Probability Density Function (PDF) of a 3D pose (6D actually)...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
#define MRPT_END
Definition: exceptions.h:245
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
const auto bb_max
void enqueForRenderRecursive(const mrpt::opengl::TRenderMatrices &state, RenderQueue &rq) const override
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
const auto bb_min
A RGB color - 8bit.
Definition: TColor.h:25
CRenderizable & setColor_u8(const mrpt::img::TColor &c) override
Declares a class that represents a Probability Distribution function (PDF) of a 3D point (x...
Definition: CPointPDF.h:36
CRenderizable::Ptr getByName(const std::string &str)
Returns the first object with a given name, or a nullptr pointer if not found.
void insert(const CRenderizable::Ptr &newObject)
Insert a new object to the list.
CRenderizable & setColorB_u8(const uint8_t b) override
Color components in the range [0,255].
Declares a class that represents a Probability Density Function (PDF) of a 3D pose (6D actually)...
Definition: CPose3DPDF.h:39
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...
shader_list_t requiredShaders() const override
Returns the ID of the OpenGL shader program required to render this class.
Definition: CSetOfObjects.h:67
std::map< shader_id_t, std::multimap< float, RenderQueueElement > > RenderQueue
A queue for rendering, sorted by shader program to minimize changes of OpenGL shader programs while r...
Definition: RenderQueue.h:46



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 1e710274b Thu May 28 15:30:14 2020 +0200 at jue may 28 15:45:10 CEST 2020