Main MRPT website > C++ reference
MRPT logo
CSetOfObjects.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_CSetOfObjects_H
29 #define opengl_CSetOfObjects_H
30 
32 
33 // All these are needed for the auxiliary methods posePDF2opengl()
34 #include <mrpt/poses/CPointPDF.h>
35 #include <mrpt/poses/CPosePDF.h>
36 #include <mrpt/poses/CPose3DPDF.h>
38 
39 namespace mrpt
40 {
41  namespace opengl
42  {
44 
45  // This must be added to any CSerializable derived class:
47 
48  /** A set of object, which are referenced to the coordinates framework established in this object.
49  * It can be established a hierarchy of "CSetOfObjects", where the coordinates framework of each
50  * one will be referenced to the parent's one.
51  * The list of child objects is accessed directly as in the class "COpenGLScene"
52  * \sa opengl::COpenGLScene
53  * \ingroup mrpt_opengl_grp
54  */
56  {
58 
59  protected:
60  /** The list of child objects.
61  * Objects are automatically deleted when calling "clear" or in the destructor.
62  */
63  CListOpenGLObjects m_objects;
64 
65  public:
66 
69 
70  inline const_iterator begin() const { return m_objects.begin(); }
71  inline const_iterator end() const { return m_objects.end(); }
72  inline iterator begin() { return m_objects.begin(); }
73  inline iterator end() { return m_objects.end(); }
74 
75  /** Inserts a set of objects into the list.
76  */
77  template<class T> inline void insertCollection(const T &objs) {
78  insert(objs.begin(),objs.end());
79  }
80  /** Insert a new object to the list.
81  */
82  void insert( const CRenderizablePtr &newObject );
83 
84  /** Inserts a set of objects, bounded by iterators, into the list.
85  */
86  template<class T_it> inline void insert(const T_it &begin,const T_it &end) {
87  for (T_it it=begin;it!=end;it++) insert(*it);
88  }
89 
90  /** Render child objects.
91  */
92  void render() const;
93 
94  /** Clear the list of objects in the scene, deleting objects' memory.
95  */
96  void clear();
97 
98  /** Returns number of objects. */
99  size_t size() { return m_objects.size(); }
100 
101  /** Returns true if there are no objects. */
102  inline bool empty() const { return m_objects.empty(); }
103 
104  /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
105  */
106  void initializeAllTextures();
107 
108  /** Returns the first object with a given name, or a NULL pointer if not found.
109  */
110  CRenderizablePtr getByName( const std::string &str );
111 
112  /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found.
113  * Example:
114  * \code
115  CSpherePtr obs = myscene.getByClass<CSphere>();
116  * \endcode
117  * By default (ith=0), the first observation is returned.
118  */
119  template <typename T>
120  typename T::SmartPtr getByClass( const size_t &ith = 0 ) const
121  {
122  MRPT_START
123  size_t foundCount = 0;
124  const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo;
125  for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it)
126  if ( (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) )
127  if (foundCount++ == ith)
128  return typename T::SmartPtr(*it);
129 
130  // If not found directly, search recursively:
131  for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it)
132  {
133  if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl))
134  {
135  typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith);
136  if (o) return o;
137  }
138  }
139 
140  return typename T::SmartPtr(); // Not found: return empty smart pointer
141  MRPT_END
142  }
143 
144 
145  /** Removes the given object from the scene (it also deletes the object to free its memory).
146  */
147  void removeObject( const CRenderizablePtr &obj );
148 
149  /** Retrieves a list of all objects in text form.
150  */
151  void dumpListOfObjects( utils::CStringList &lst );
152 
153  /** Ray tracing
154  */
155  virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
156 
157  virtual CRenderizable& setColor_u8(const mrpt::utils::TColor &c);
158  virtual CRenderizable& setColorR_u8(const uint8_t r);
159  virtual CRenderizable& setColorG_u8(const uint8_t g);
160  virtual CRenderizable& setColorB_u8(const uint8_t b);
161  virtual CRenderizable& setColorA_u8(const uint8_t a);
162 
163  bool contains(const CRenderizablePtr &obj) const;
164 
165 
166  /** @name pose_pdf -> 3d objects auxiliary templates
167  @{ */
168  // The reason this code is here is to exploit C++'s "T::template function()" in order to
169  // define the members getAs3DObject() in several classes in mrpt-base with its argument
170  // being a class (CSetOfObjects) which is actually declared here, in mrpt-opengl.
171  // Implementations are in "pose_pdfs.cpp", not in "CSetOfObjects" (historic reasons...)
172 
173  /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call
174  * mrpt::poses::CPosePDF::getAs3DObject */
176 
177  /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call
178  * mrpt::poses::CPointPDF::getAs3DObject */
180 
181  /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call
182  * mrpt::poses::CPose3DPDF::getAs3DObject */
184 
185  /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call
186  * mrpt::poses::CPose3DQuatPDF::getAs3DObject */
188 
189  /** @} */
190 
191  private:
192  /** Default constructor
193  */
194  CSetOfObjects( );
195 
196  /** Private, virtual destructor: only can be deleted from smart pointers */
197  virtual ~CSetOfObjects();
198  };
199  /** Inserts an object into the list. Allows call chaining.
200  * \sa mrpt::opengl::CSetOfObjects::insert
201  */
203  s->insert(r);
204  return s;
205  }
206  /** Inserts a set of objects into the list. Allows call chaining.
207  * \sa mrpt::opengl::CSetOfObjects::insert
208  */
209  template<class T> inline CSetOfObjectsPtr &operator<<(CSetOfObjectsPtr &o,const std::vector<T> &v) {
210  o->insertCollection(v);
211  return o;
212  }
213 
214 
215  } // end namespace
216 
217 } // End of namespace
218 
219 
220 #endif



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