Main MRPT website > C++ reference for MRPT 1.5.6
COpenGLScene.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef opengl_COpenGLScene_H
10 #define opengl_COpenGLScene_H
11 
14 #include <mrpt/utils/CStringList.h>
15 
16 namespace mrpt
17 {
18  /** The namespace for 3D scene representation and rendering. See also the <a href="mrpt-opengl.html" > summary page</a> of the mrpt-opengl library for more info and thumbnails of many of the render primitive.
19  */
20  namespace opengl
21  {
22  // This must be added to any CSerializable derived class:
24 
25 
26  /** This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
27  * The class can be understood as a program to be run over OpenGL, containing a sequence of viewport definitions,
28  * rendering primitives, etc...
29  *
30  * It can contain from 1 up to any number of <b>Viewports</b>, each one
31  * associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection
32  * camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes.
33  *
34  * An object of COpenGLScene always contains at least one viewport (utils::COpenGLViewport), named "main". Optionally, any
35  * number of other viewports may exist. Viewports are referenced by their names, case-sensitive strings. Each viewport contains
36  * a different 3D scene (i.e. they render different objects), though a mechanism exist to share the same 3D scene by a number of
37  * viewports so memory is not wasted replicating the same objects (see COpenGLViewport::setCloneView ).
38  *
39  * The main rendering method, COpenGLScene::render(), assumes a viewport has been set-up for the entire target window. That
40  * method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth
41  * buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate
42  * member in COpenGLViewport.
43  *
44  * An object COpenGLScene can be saved to a ".3Dscene" file using CFileOutputStream, for posterior visualization from
45  * the standalone application <a href="http://www.mrpt.org/Application:SceneViewer" >SceneViewer</a>.
46  * It can be also displayed in real-time using gui::CDisplayWindow3D.
47  * \ingroup mrpt_opengl_grp
48  */
49  class OPENGL_IMPEXP COpenGLScene : public mrpt::utils::CSerializable
50  {
52  public:
53  /** Constructor
54  */
55  COpenGLScene();
56 
57  /** Destructor:
58  */
59  virtual ~COpenGLScene();
60 
61  /** Copy operator:
62  */
63  COpenGLScene & operator =( const COpenGLScene &obj );
64 
65  /** Copy constructor:
66  */
67  COpenGLScene( const COpenGLScene &obj );
68 
69  /**
70  * Inserts a set of objects into the scene, in the given viewport ("main" by default). Any iterable object will be accepted.
71  * \sa createViewport,getViewport
72  */
73  template<class T> inline void insertCollection(const T &objs,const std::string &vpn=std::string("main")) {
74  insert(objs.begin(),objs.end(),vpn);
75  }
76  /** Insert a new object into the scene, in the given viewport (by default, into the "main" viewport).
77  * The viewport must be created previously, an exception will be raised if the given name does not correspond to
78  * an existing viewport.
79  * \sa createViewport, getViewport
80  */
81  void insert( const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"));
82 
83  /**
84  * Inserts a set of objects into the scene, in the given viewport ("main" by default).
85  * \sa createViewport,getViewport
86  */
87  template<class T_it> inline void insert(const T_it &begin,const T_it &end,const std::string &vpn=std::string("main")) {
88  for (T_it it=begin;it!=end;it++) insert(*it,vpn);
89  }
90 
91  /**Creates a new viewport, adding it to the scene and returning a pointer to the new object.
92  * Names (case-sensitive) cannot be duplicated: if the name provided coincides with an already existing viewport, a pointer to the existing object will be returned.
93  * The first, default viewport, is named "main".
94  */
95  COpenGLViewportPtr createViewport( const std::string &viewportName );
96 
97  /** Returns the viewport with the given name, or NULL if it does not exist; note that the default viewport is named "main" and initially occupies the entire rendering area.
98  */
99  COpenGLViewportPtr getViewport( const std::string &viewportName = std::string("main") ) const;
100 
101  /** Render this scene */
102  void render() const;
103 
104  size_t viewportsCount() const { return m_viewports.size(); }
105 
106  /** Clear the list of objects and viewports in the scene, deleting objects' memory, and leaving just the default viewport with the default values.
107  */
108  void clear( bool createMainViewport = true );
109 
110  /** If disabled (default), the SceneViewer application will ignore the camera of the "main" viewport and keep the viewport selected by the user by hand; otherwise, the camera in the "main" viewport prevails.
111  * \sa followCamera
112  */
113  void enableFollowCamera( bool enabled ) { m_followCamera = enabled; }
114 
115  /** Return the value of "followCamera"
116  * \sa enableFollowCamera
117  */
118  bool followCamera() const { return m_followCamera; }
119 
120  /** Returns the first object with a given name, or NULL (an empty smart pointer) if not found.
121  */
122  CRenderizablePtr getByName( const std::string &str, const std::string &viewportName = std::string("main") );
123 
124  /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found.
125  * Example:
126  * \code
127  CSpherePtr obs = myscene.getByClass<CSphere>();
128  * \endcode
129  * By default (ith=0), the first observation is returned.
130  */
131  template <typename T>
132  typename T::Ptr getByClass( const size_t &ith = 0 ) const
133  {
134  MRPT_START
135  for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
136  {
137  typename T::Ptr o = (*it)->getByClass<T>(ith);
138  if (o.present()) return o;
139  }
140  return typename T::Ptr(); // 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, const std::string &viewportName = std::string("main") );
148 
149  /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
150  */
151  void initializeAllTextures();
152 
153  /** Retrieves a list of all objects in text form.
154  */
155  void dumpListOfObjects( mrpt::utils::CStringList &lst );
156 
157  /** Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D
158  * \sa loadFromFile
159  * \return false on any error.
160  */
161  bool saveToFile(const std::string &fil) const;
162 
163  /** Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
164  * \sa saveToFile
165  * \return false on any error.
166  */
167  bool loadFromFile(const std::string &fil);
168 
169  /** Traces a ray
170  */
171  bool traceRay(const mrpt::poses::CPose3D&o,double &dist) const;
172 
173  /** Evaluates the bounding box of the scene in the given viewport (default: "main"). */
174  void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max, const std::string &vpn=std::string("main") ) const;
175 
176  /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
177  * The passed function must accept only one argument of type "const mrpt::opengl::CRenderizablePtr &"
178  */
179  template <typename FUNCTOR>
180  void visitAllObjects( FUNCTOR functor) const
181  {
182  MRPT_START
183  for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
184  for (COpenGLViewport::const_iterator itO = (*it)->begin();itO!=(*it)->end();++itO)
185  internal_visitAllObjects(functor, *itO);
186  MRPT_END
187  }
188 
189  /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
190  * The passed function must accept a first argument of type "const mrpt::opengl::CRenderizablePtr &"
191  * and a second one of type EXTRA_PARAM
192  */
193  template <typename FUNCTOR,typename EXTRA_PARAM>
194  inline void visitAllObjects( FUNCTOR functor, const EXTRA_PARAM &userParam) const {
195  visitAllObjects( std::bind2nd(functor,userParam) );
196  }
197 
198  protected:
200 
201  typedef std::vector<COpenGLViewportPtr> TListViewports;
202 
203  TListViewports m_viewports; //!< The list of viewports, indexed by name.
204 
205 
206  template <typename FUNCTOR>
207  static void internal_visitAllObjects(FUNCTOR functor, const CRenderizablePtr &o)
208  {
209  functor(o);
210  if (IS_CLASS(o,CSetOfObjects))
211  {
212  CSetOfObjectsPtr obj = CSetOfObjectsPtr(o);
213  for (CSetOfObjects::const_iterator it=obj->begin();it!=obj->end();++it)
214  internal_visitAllObjects(functor,*it);
215  }
216  }
217 
218  };
220 
221  /** Inserts an openGL object into a scene. Allows call chaining. \sa mrpt::opengl::COpenGLScene::insert */
222  inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const CRenderizablePtr &r) {
223  s->insert(r);
224  return s;
225  }
226  /**Inserts any iterable collection of openGL objects into a scene, allowing call chaining. \sa mrpt::opengl::COpenGLScene::insert */
227  template <class T> inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const std::vector<T> &v) {
228  s->insert(v.begin(),v.end());
229  return s;
230  }
231  } // end namespace
232 
233 } // End of namespace
234 
235 
236 #endif
const GLdouble * v
Definition: glew.h:1296
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:32
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
size_t viewportsCount() const
Definition: COpenGLScene.h:104
bool BASE_IMPEXP traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons' properties.
Definition: geometry.cpp:1996
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:26
const Scalar * const_iterator
Definition: eigen_plugins.h:24
void visitAllObjects(FUNCTOR functor, const EXTRA_PARAM &userParam) const
Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied func...
Definition: COpenGLScene.h:194
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
GLdouble s
Definition: glew.h:1295
A class for storing a list of text lines.
Definition: CStringList.h:32
std::vector< COpenGLViewportPtr > TListViewports
Definition: COpenGLScene.h:201
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
GLuint GLuint end
Definition: glew.h:1167
#define MRPT_END
void enableFollowCamera(bool enabled)
If disabled (default), the SceneViewer application will ignore the camera of the "main" viewport and ...
Definition: COpenGLScene.h:113
T::Ptr getByClass(const size_t &ith=0) const
Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer)...
Definition: COpenGLScene.h:132
GLhandleARB obj
Definition: glew.h:3276
TListViewports m_viewports
The list of viewports, indexed by name.
Definition: COpenGLScene.h:203
static void internal_visitAllObjects(FUNCTOR functor, const CRenderizablePtr &o)
Definition: COpenGLScene.h:207
void insert(const T_it &begin, const T_it &end, const std::string &vpn=std::string("main"))
Inserts a set of objects into the scene, in the given viewport ("main" by default).
Definition: COpenGLScene.h:87
#define MRPT_START
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
GLsizei const GLcharARB ** string
Definition: glew.h:3293
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:103
CListOpenGLObjects::const_iterator const_iterator
CListOpenGLObjects::const_iterator const_iterator
Definition: CSetOfObjects.h:44
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:49
bool followCamera() const
Return the value of "followCamera".
Definition: COpenGLScene.h:118
GLdouble GLdouble GLdouble r
Definition: glew.h:1311
void visitAllObjects(FUNCTOR functor) const
Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied func...
Definition: COpenGLScene.h:180
Lightweight 3D point.
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018