00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 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 // All these are needed for the auxiliary methods posePDF2opengl() 00034 #include <mrpt/poses/CPointPDF.h> 00035 #include <mrpt/poses/CPosePDF.h> 00036 #include <mrpt/poses/CPose3DPDF.h> 00037 #include <mrpt/poses/CPose3DQuatPDF.h> 00038 00039 namespace mrpt 00040 { 00041 namespace opengl 00042 { 00043 class OPENGL_IMPEXP CSetOfObjects; 00044 00045 // This must be added to any CSerializable derived class: 00046 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CSetOfObjects, CRenderizable, OPENGL_IMPEXP ) 00047 00048 /** A set of object, which are referenced to the coordinates framework established in this object. 00049 * It can be established a hierarchy of "CSetOfObjects", where the coordinates framework of each 00050 * one will be referenced to the parent's one. 00051 * The list of child objects is accessed directly as in the class "COpenGLScene" 00052 * \sa opengl::COpenGLScene 00053 * \ingroup mrpt_opengl_grp 00054 */ 00055 class OPENGL_IMPEXP CSetOfObjects : public CRenderizable 00056 { 00057 DEFINE_SERIALIZABLE( CSetOfObjects ) 00058 00059 protected: 00060 /** The list of child objects. 00061 * Objects are automatically deleted when calling "clear" or in the destructor. 00062 */ 00063 CListOpenGLObjects m_objects; 00064 00065 public: 00066 00067 typedef CListOpenGLObjects::const_iterator const_iterator; 00068 typedef CListOpenGLObjects::iterator iterator; 00069 00070 inline const_iterator begin() const { return m_objects.begin(); } 00071 inline const_iterator end() const { return m_objects.end(); } 00072 inline iterator begin() { return m_objects.begin(); } 00073 inline iterator end() { return m_objects.end(); } 00074 00075 /** Inserts a set of objects into the list. 00076 */ 00077 template<class T> inline void insertCollection(const T &objs) { 00078 insert(objs.begin(),objs.end()); 00079 } 00080 /** Insert a new object to the list. 00081 */ 00082 void insert( const CRenderizablePtr &newObject ); 00083 00084 /** Inserts a set of objects, bounded by iterators, into the list. 00085 */ 00086 template<class T_it> inline void insert(const T_it &begin,const T_it &end) { 00087 for (T_it it=begin;it!=end;it++) insert(*it); 00088 } 00089 00090 /** Render child objects. 00091 */ 00092 void render() const; 00093 00094 /** Clear the list of objects in the scene, deleting objects' memory. 00095 */ 00096 void clear(); 00097 00098 /** Returns number of objects. */ 00099 size_t size() { return m_objects.size(); } 00100 00101 /** Returns true if there are no objects. */ 00102 inline bool empty() const { return m_objects.empty(); } 00103 00104 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) 00105 */ 00106 void initializeAllTextures(); 00107 00108 /** Returns the first object with a given name, or a NULL pointer if not found. 00109 */ 00110 CRenderizablePtr getByName( const std::string &str ); 00111 00112 /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found. 00113 * Example: 00114 * \code 00115 CSpherePtr obs = myscene.getByClass<CSphere>(); 00116 * \endcode 00117 * By default (ith=0), the first observation is returned. 00118 */ 00119 template <typename T> 00120 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const 00121 { 00122 MRPT_START 00123 size_t foundCount = 0; 00124 const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo; 00125 for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it) 00126 if ( (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) ) 00127 if (foundCount++ == ith) 00128 return typename T::SmartPtr(*it); 00129 00130 // If not found directly, search recursively: 00131 for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it) 00132 { 00133 if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl)) 00134 { 00135 typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith); 00136 if (o) return o; 00137 } 00138 } 00139 00140 return typename T::SmartPtr(); // Not found: return empty smart pointer 00141 MRPT_END 00142 } 00143 00144 00145 /** Removes the given object from the scene (it also deletes the object to free its memory). 00146 */ 00147 void removeObject( const CRenderizablePtr &obj ); 00148 00149 /** Retrieves a list of all objects in text form. 00150 */ 00151 void dumpListOfObjects( utils::CStringList &lst ); 00152 00153 /** Ray tracing 00154 */ 00155 virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const; 00156 00157 virtual CRenderizable& setColor_u8(const mrpt::utils::TColor &c); 00158 virtual CRenderizable& setColorR_u8(const uint8_t r); 00159 virtual CRenderizable& setColorG_u8(const uint8_t g); 00160 virtual CRenderizable& setColorB_u8(const uint8_t b); 00161 virtual CRenderizable& setColorA_u8(const uint8_t a); 00162 00163 bool contains(const CRenderizablePtr &obj) const; 00164 00165 00166 /** @name pose_pdf -> 3d objects auxiliary templates 00167 @{ */ 00168 // The reason this code is here is to exploit C++'s "T::template function()" in order to 00169 // define the members getAs3DObject() in several classes in mrpt-base with its argument 00170 // being a class (CSetOfObjects) which is actually declared here, in mrpt-opengl. 00171 // Implementations are in "pose_pdfs.cpp", not in "CSetOfObjects" (historic reasons...) 00172 00173 /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call 00174 * mrpt::poses::CPosePDF::getAs3DObject */ 00175 static CSetOfObjectsPtr posePDF2opengl(const mrpt::poses::CPosePDF &o); 00176 00177 /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call 00178 * mrpt::poses::CPointPDF::getAs3DObject */ 00179 static CSetOfObjectsPtr posePDF2opengl(const mrpt::poses::CPointPDF &o); 00180 00181 /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call 00182 * mrpt::poses::CPose3DPDF::getAs3DObject */ 00183 static CSetOfObjectsPtr posePDF2opengl(const mrpt::poses::CPose3DPDF &o); 00184 00185 /** Returns a representation of a the PDF - this is just an auxiliary function, it's more natural to call 00186 * mrpt::poses::CPose3DQuatPDF::getAs3DObject */ 00187 static CSetOfObjectsPtr posePDF2opengl(const mrpt::poses::CPose3DQuatPDF &o); 00188 00189 /** @} */ 00190 00191 private: 00192 /** Default constructor 00193 */ 00194 CSetOfObjects( ); 00195 00196 /** Private, virtual destructor: only can be deleted from smart pointers */ 00197 virtual ~CSetOfObjects(); 00198 }; 00199 /** Inserts an object into the list. Allows call chaining. 00200 * \sa mrpt::opengl::CSetOfObjects::insert 00201 */ 00202 inline CSetOfObjectsPtr &operator<<(CSetOfObjectsPtr &s,const CRenderizablePtr &r) { 00203 s->insert(r); 00204 return s; 00205 } 00206 /** Inserts a set of objects into the list. Allows call chaining. 00207 * \sa mrpt::opengl::CSetOfObjects::insert 00208 */ 00209 template<class T> inline CSetOfObjectsPtr &operator<<(CSetOfObjectsPtr &o,const std::vector<T> &v) { 00210 o->insertCollection(v); 00211 return o; 00212 } 00213 00214 00215 } // end namespace 00216 00217 } // End of namespace 00218 00219 00220 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |