Main MRPT website > C++ reference
MRPT logo

CSetOfObjects.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef opengl_CSetOfObjects_H
00029 #define opengl_CSetOfObjects_H
00030 
00031 #include <mrpt/opengl/CRenderizable.h>
00032 
00033 namespace mrpt
00034 {
00035         namespace opengl
00036         {
00037                 class OPENGL_IMPEXP CSetOfObjects;
00038 
00039                 /** A list of objects pointers, automatically managing memory free at destructor, and managing copies correctly.
00040                   */
00041                 typedef std::deque<CRenderizablePtr> CListOpenGLObjects;
00042 
00043                 // This must be added to any CSerializable derived class:
00044                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CSetOfObjects, CRenderizable, OPENGL_IMPEXP )
00045 
00046                 /** A set of object, which are referenced to the coordinates framework established in this object.
00047                   *  It can be established a hierarchy of "CSetOfObjects", where the coordinates framework of each
00048                   *   one will be referenced to the parent's one.
00049                   *     The list of child objects is accessed directly as in the class "COpenGLScene"
00050                   *  \sa opengl::COpenGLScene
00051                   */
00052                 class OPENGL_IMPEXP CSetOfObjects : public CRenderizable
00053                 {
00054                         DEFINE_SERIALIZABLE( CSetOfObjects )
00055 
00056                 protected:
00057                         /** The list of child objects.
00058                           *  Objects are automatically deleted when calling "clear" or in the destructor.
00059                           */
00060                         CListOpenGLObjects              m_objects;
00061 
00062                 public:
00063 
00064                         typedef CListOpenGLObjects::const_iterator      const_iterator;
00065                         typedef CListOpenGLObjects::iterator            iterator;
00066 
00067                         inline const_iterator begin() const { return  m_objects.begin(); }
00068                         inline const_iterator end() const { return  m_objects.end(); }
00069                         inline iterator begin() { return  m_objects.begin(); }
00070                         inline iterator end() { return  m_objects.end(); }
00071 
00072                         /** Inserts a set of objects into the list.
00073                           */
00074                         template<class T> inline void insertCollection(const T &objs)   {
00075                                 insert(objs.begin(),objs.end());
00076                         }
00077                         /** Insert a new object to the list.
00078                           */
00079                         void insert( const CRenderizablePtr &newObject );
00080 
00081                         /** Inserts a set of objects, bounded by iterators, into the list.
00082                           */
00083                         template<class T_it> inline void insert(const T_it &begin,const T_it &end)      {
00084                                 for (T_it it=begin;it!=end;it++) insert(*it);
00085                         }
00086 
00087                         /** Render child objects.
00088                           */
00089                         void  render() const;
00090 
00091                         /** Clear the list of objects in the scene, deleting objects' memory.
00092                           */
00093                         void  clear();
00094 
00095                         /** Returns number of objects.  */
00096                         size_t size()  {  return m_objects.size(); }
00097 
00098                         /** Returns true if there are no objects.  */
00099                         inline bool empty() const { return m_objects.empty(); }
00100 
00101                         /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
00102                           */
00103                         void  initializeAllTextures();
00104 
00105                         /** Returns the first object with a given name, or a NULL pointer if not found.
00106                           */
00107                         CRenderizablePtr getByName( const std::string &str );
00108 
00109                          /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found.
00110                            *  Example:
00111                            * \code
00112                                         CSpherePtr obs = myscene.getByClass<CSphere>();
00113                            * \endcode
00114                            * By default (ith=0), the first observation is returned.
00115                            */
00116                          template <typename T>
00117                          typename T::SmartPtr getByClass( const size_t &ith = 0 ) const
00118                          {
00119                                 MRPT_START;
00120                                 size_t  foundCount = 0;
00121                                 const mrpt::utils::TRuntimeClassId*     class_ID = T::classinfo;
00122                                 for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it)
00123                                         if (  (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) )
00124                                                 if (foundCount++ == ith)
00125                                                         return typename T::SmartPtr(*it);
00126 
00127                                 // If not found directly, search recursively:
00128                                 for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it)
00129                                 {
00130                                         if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl))
00131                                         {
00132                                                 typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith);
00133                                                 if (o) return o;
00134                                         }
00135                                 }
00136 
00137                                 return typename T::SmartPtr();  // Not found: return empty smart pointer
00138                                 MRPT_END;
00139                          }
00140 
00141 
00142                         /** Removes the given object from the scene (it also deletes the object to free its memory).
00143                           */
00144                         void removeObject( const CRenderizablePtr &obj );
00145 
00146                         /** Retrieves a list of all objects in text form.
00147                           */
00148                         void dumpListOfObjects( utils::CStringList  &lst );
00149 
00150                         /** Ray tracing
00151                           */
00152                         virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
00153 
00154                         virtual CRenderizable& setColor(const mrpt::utils::TColorf &c);
00155                         virtual CRenderizable& setColor(double r,double g,double b,double a=1);
00156                         virtual CRenderizable& setColorR(const double r);
00157                         virtual CRenderizable& setColorG(const double g);
00158                         virtual CRenderizable& setColorB(const double b);
00159                         virtual CRenderizable& setColorA(const double a);
00160 
00161                         bool contains(const CRenderizablePtr &obj) const;
00162 
00163 
00164                 private:
00165                         /** Default constructor
00166                           */
00167                         CSetOfObjects( );
00168 
00169                         /** Private, virtual destructor: only can be deleted from smart pointers */
00170                         virtual ~CSetOfObjects();
00171                 };
00172                 /** Inserts an object into the list. Allows call chaining.
00173                   * \sa mrpt::opengl::CSetOfObjects::insert
00174                   */
00175                 inline CSetOfObjectsPtr &operator<<(CSetOfObjectsPtr &s,const CRenderizablePtr &r)      {
00176                         s->insert(r);
00177                         return s;
00178                 }
00179                 /** Inserts a set of objects into the list. Allows call chaining.
00180                   * \sa mrpt::opengl::CSetOfObjects::insert
00181                   */
00182                 template<class T> inline CSetOfObjectsPtr &operator<<(CSetOfObjectsPtr &o,const std::vector<T> &v)      {
00183                         o->insertCollection(v);
00184                         return o;
00185                 }
00186 
00187 
00188         } // end namespace
00189 
00190 } // End of namespace
00191 
00192 
00193 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011