Main MRPT website > C++ reference for MRPT 1.4.0
CDisplayWindow3D.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-2016, 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 CDisplayWindow3D_H
10 #define CDisplayWindow3D_H
11 
16 #include <mrpt/utils/CImage.h>
17 
18 /*---------------------------------------------------------------
19  Class
20  ---------------------------------------------------------------*/
21 namespace mrpt
22 {
23  namespace gui
24  {
25  class C3DWindowDialog;
26  class CMyGLCanvas_DisplayWindow3D;
27 
29 
30  /** A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
31  * This class always contains internally an instance of opengl::COpenGLScene, which
32  * the objects, viewports, etc. to be rendered.
33  *
34  * Images can be grabbed automatically to disk for easy creation of videos.
35  * See CDisplayWindow3D::grabImagesStart (and for creating videos, mrpt::vision::CVideoFileWriter).
36  *
37  * A short-cut for displaying 2D images (using the OpenGL rendering hardware) is available
38  * through \a setImageView() and \a setImageView_fast(). Internally, these methods call methods
39  * in the "main" viewport of the window (see \a COpenGLViewport).
40  *
41  * Since the 3D rendering is performed in a detached thread, especial care must be taken
42  * when updating the 3D scene to be rendered. The process involves an internal critical section
43  * and it must always consist of these steps:
44  *
45  * \code
46  * CDisplayWindow3D win("My window");
47  *
48  * // Adquire the scene:
49  * opengl::COpenGLScenePtr &ptrScene = win.get3DSceneAndLock();
50  *
51  * // Modify the scene:
52  * ptrScene->...
53  * // or replace by another scene:
54  * ptrScene = otherScene;
55  *
56  * // Unlock it, so the window can use it for redraw:
57  * win.unlockAccess3DScene();
58  *
59  * // Update window, if required
60  * win.forceRepaint();
61  * \endcode
62  *
63  * An alternative way of updating the scene is by creating, before locking the 3D window, a new object
64  * of class COpenGLScene, then locking the window only for replacing the smart pointer. This may be
65  * advantageous is generating the 3D scene takes a long time, since while the window
66  * is locked it will not be responsive to the user input or window redraw.
67  *
68  * The window can also display a set of 2D text messages overlapped to the 3D scene.
69  * See CDisplayWindow3D::addTextMessage
70  *
71  * For a list of supported events with the observer/observable pattern, see the discussion in mrpt::gui::CBaseGUIWindow.
72  * In addition to those events, this class introduces mrpt::gui::mrptEvent3DWindowGrabImageFile
73  *
74  *
75  * \sa The example /samples/display3D, the <a href="http://www.mrpt.org/Tutorial_3D_Scenes" > tutorial only</a>.
76  * \ingroup mrpt_gui_grp
77  */
79  {
80  // This must be added to any CObject derived class:
82 
83  protected:
84  friend class C3DWindowDialog;
85  friend class CMyGLCanvas_DisplayWindow3D;
86  /** Internal OpenGL object (see general discussion in about usage of this object)
87  */
88  opengl::COpenGLScenePtr m_3Dscene;
89 
90  /** Critical section for accesing m_3Dscene
91  */
93 
94  /** Throws an exception on initialization error
95  */
96  void createOpenGLContext();
97 
100 
101  std::string m_grab_imgs_prefix;
102  unsigned int m_grab_imgs_idx;
103 
105  mrpt::utils::CImagePtr m_last_captured_img;
107 
108  void doRender();
109 
111 
112  double m_last_FPS; //!< \sa getRenderingFPS
113 
114  void internalSetMinMaxRange();
115 
116  public:
117  /** Constructor */
119  const std::string &windowCaption = std::string(),
120  unsigned int initialWindowWidth = 400,
121  unsigned int initialWindowHeight = 300 );
122 
123  /** Class factory returning a smart pointer */
124  static CDisplayWindow3DPtr Create(
125  const std::string &windowCaption,
126  unsigned int initialWindowWidth = 400,
127  unsigned int initialWindowHeight = 300 );
128 
129  /** Destructor */
130  virtual ~CDisplayWindow3D();
131 
132  /** Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introduction in gui::CDisplayWindow3D before use!)
133  * This also locks the critical section for accesing the scene, thus the window will not be repainted until it is unlocked. */
134  opengl::COpenGLScenePtr & get3DSceneAndLock( );
135 
136  /** Unlocks the access to the internal 3D scene.
137  * Typically user will want to call forceRepaint after updating the scene. */
138  void unlockAccess3DScene();
139 
140  void forceRepaint(); //!< Repaints the window. forceRepaint, repaint and updateWindow are all aliases of the same method
141  void repaint() //!< Repaints the window. forceRepaint, repaint and updateWindow are all aliases of the same method
142  { forceRepaint(); }
143  void updateWindow() //!< Repaints the window. forceRepaint, repaint and updateWindow are all aliases of the same method
144  { forceRepaint(); }
145 
146  float getFOV() const; //!< Return the camera field of view (in degrees) (used for gluPerspective)
147  void setMinRange(double new_min);//!< Changes the camera min clip range (z) (used for gluPerspective). The window is not updated with this method, call "forceRepaint" to update the 3D view.
148  void setMaxRange(double new_max);//!< Changes the camera max clip range (z) (used for gluPerspective. The window is not updated with this method, call "forceRepaint" to update the 3D view.
149  void setFOV(float v);//!< Changes the camera field of view (in degrees) (used for gluPerspective). The window is not updated with this method, call "forceRepaint" to update the 3D view.
150  void resize( unsigned int width, unsigned int height) MRPT_OVERRIDE; //!< Resizes the window, stretching the image to fit into the display area.
151  void setPos( int x, int y ) MRPT_OVERRIDE;//!< Changes the position of the window on the screen.
152  void setWindowTitle( const std::string &str ) MRPT_OVERRIDE;//!< Changes the window title.
153  void setCameraElevationDeg( float deg );//!< Changes the camera parameters programatically
154  void setCameraAzimuthDeg( float deg );//!< Changes the camera parameters programatically
155  void setCameraPointingToPoint( float x,float y, float z );//!< Changes the camera parameters programatically
156  void setCameraZoom( float zoom );//!< Changes the camera parameters programatically
157  void setCameraProjective( bool isProjective );//!< Sets the camera as projective, or orthogonal.
158  float getCameraElevationDeg() const;//!< Get camera parameters programatically
159  float getCameraAzimuthDeg() const;//!< Get camera parameters programatically
160  void getCameraPointingToPoint( float &x,float &y, float &z ) const;//!< Get camera parameters programatically
161  float getCameraZoom() const;//!< Get camera parameters programatically
162  bool isCameraProjective() const;//!< Sets the camera as projective, or orthogonal
163  void useCameraFromScene(bool useIt = true);//!< If set to true (default = false), the mouse-based scene navigation will be disabled and the camera position will be determined by the opengl viewports in the 3D scene
164  bool getLastMousePositionRay(mrpt::math::TLine3D &ray) const;//!< Gets the 3D ray for the direction line of the pixel where the mouse cursor is at. \return False if the window is closed. \sa getLastMousePosition
165  virtual bool getLastMousePosition(int &x, int &y) const MRPT_OVERRIDE;//!< Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. \sa getLastMousePositionRay
166  virtual void setCursorCross(bool cursorIsCross) MRPT_OVERRIDE;//!< Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) \sa getLastMousePositionRay
167 
168  /** Start to save rendered images to disk.
169  * Images will be saved independently as png files, depending on
170  * the template path passed to this method. For example:
171  *
172  * path_prefix: "./video_"
173  *
174  * Will generate "./video_000001.png", etc.
175  *
176  * If this feature is enabled, the window will emit events of the type mrpt::gui::mrptEvent3DWindowGrabImageFile() which you can subscribe to.
177  *
178  * \sa grabImagesStop
179  */
180  void grabImagesStart( const std::string &grab_imgs_prefix = std::string("video_") );
181 
182  /** Stops image grabbing started by grabImagesStart
183  * \sa grabImagesStart
184  */
185  void grabImagesStop();
186 
187  /** Enables the grabbing of CImage objects from screenshots of the window.
188  * \sa getLastWindowImage
189  */
190  void captureImagesStart();
191 
192  /** Stop image grabbing
193  * \sa captureImagesStart
194  */
195  void captureImagesStop();
196 
197  /** Retrieve the last captured image from the window.
198  * You MUST CALL FIRST captureImagesStart to enable image grabbing.
199  * \return false if there was no time yet for grabbing any image (then, the output image is undefined).
200  * \sa captureImagesStart, getLastWindowImagePtr
201  */
202  bool getLastWindowImage( mrpt::utils::CImage &out_img) const;
203 
204  /** Retrieve the last captured image from the window, as a smart pointer.
205  * This method is more efficient than getLastWindowImage since only a copy of the pointer is performed, while
206  * getLastWindowImage would copy the entire image.
207  *
208  * You MUST CALL FIRST captureImagesStart to enable image grabbing.
209  * \Note If there was no time yet for grabbing any image, an empty smart pointer will be returned.
210  * \sa captureImagesStart, getLastWindowImage
211  */
212  mrpt::utils::CImagePtr getLastWindowImagePtr() const;
213 
214  /** Increments by one the image counter and return the next image file name (Users normally don't want to call this method).
215  * \sa grabImagesStart
216  */
217  std::string grabImageGetNextFile();
218 
219  bool isCapturingImgs() const { return m_is_capturing_imgs; }
220 
221 
222  /** Add 2D text messages overlapped to the 3D rendered scene. The string will remain displayed in the 3D window
223  * until it's changed with subsequent calls to this same method, or all the texts are cleared with clearTextMessages().
224  *
225  * \param x The X position, interpreted as absolute pixels from the left if X>=1, absolute pixels from the left if X<0 or as a width factor if in the range [0,1[.
226  * \param y The Y position, interpreted as absolute pixels from the bottom if Y>=1, absolute pixels from the top if Y<0 or as a height factor if in the range [0,1[.
227  * \param text The text string to display.
228  * \param color The text color. For example: TColorf(1.0,1.0,1.0)
229  * \param unique_index An "index" for this text message, so that subsequent calls with the same index will overwrite this text message instead of creating new ones.
230  *
231  * You'll need to refresh the display manually with forceRepaint().
232  *
233  * \sa clearTextMessages
234  */
235  void addTextMessage(
236  const double x,
237  const double y,
238  const std::string &text,
239  const mrpt::utils::TColorf &color = mrpt::utils::TColorf(1.0,1.0,1.0),
240  const size_t unique_index = 0,
242  );
243 
244  /** \overload with more font parameters - refer to mrpt::opengl::gl_utils::glDrawText()
245  * Available fonts are enumerated at mrpt::opengl::gl_utils::glSetFont() */
246  void addTextMessage(
247  const double x_frac,
248  const double y_frac,
249  const std::string &text,
250  const mrpt::utils::TColorf &color,
251  const std::string &font_name,
252  const double font_size,
254  const size_t unique_index = 0,
255  const double font_spacing = 1.5,
256  const double font_kerning = 0.1,
257  const bool draw_shadow = false,
258  const mrpt::utils::TColorf &shadow_color = mrpt::utils::TColorf(0,0,0)
259  );
260 
261  /** Clear all text messages created with addTextMessage().
262  * You'll need to refresh the display manually with forceRepaint().
263  * \sa addTextMessage
264  */
265  void clearTextMessages();
266 
267  /** Get the average Frames Per Second (FPS) value from the last 250 rendering events */
268  double getRenderingFPS() const { return m_last_FPS; }
269 
270  /** A short cut for getting the "main" viewport of the scene object, it is equivalent to:
271  * \code
272  * mrpt::opengl::COpenGLScenePtr &scene = win3D.get3DSceneAndLock();
273  * viewport = scene->getViewport("main");
274  * win3D.unlockAccess3DScene();
275  * \endcode
276  */
277  mrpt::opengl::COpenGLViewportPtr getDefaultViewport();
278 
279  /** Set the "main" viewport into "image view"-mode, where an image is efficiently drawn (fitting the viewport area) using an OpenGL textured quad.
280  * Call this method with the new image to update the displayed image (but recall to first lock the parent openglscene's critical section, then do the update, then release the lock, and then issue a window repaint).
281  * Internally, the texture is drawn using a mrpt::opengl::CTexturedPlane
282  * The viewport can be reverted to behave like a normal viewport by calling setNormalMode()
283  * \sa setImageView_fast, COpenGLViewport
284  */
285  void setImageView(const mrpt::utils::CImage &img);
286 
287  /** Just like \a setImageView but moves the internal image memory instead of making a copy, so it's faster but empties the input image.
288  * \sa setImageView, COpenGLViewport
289  */
290  void setImageView_fast(mrpt::utils::CImage &img);
291 
292 
293  protected:
294  void internal_setRenderingFPS(double FPS); //!< Set the rendering FPS (users don't call this, the method is for internal MRPT objects only) \sa getRenderingFPS
295  void internal_emitGrabImageEvent(const std::string &fil); //!< called by CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers
296 
297  }; // End of class def.
299 
300 
301  /** @name Events specific to CDisplayWindow3D
302  @{ */
303 
304  /** An event sent by a CDisplayWindow3D window when an image is saved after enabling this feature with CDisplayWindow3D::grabImagesStart()
305  *
306  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked from the wxWidgets internal MRPT thread,
307  * so all your code in the handler must be thread safe.
308  */
309  class GUI_IMPEXP mrptEvent3DWindowGrabImageFile : public mrpt::utils::mrptEvent
310  {
311  protected:
312  virtual void do_nothing() MRPT_OVERRIDE { } //!< Just to allow this class to be polymorphic
313  public:
315  CDisplayWindow3D *obj,
316  const std::string &_img_file
317  ) : source_object(obj), img_file(_img_file) { }
318 
320  const std::string &img_file; //!< The absolute path of the file that has been just saved.
321  }; // End of class def.
322 
323  /** @} */
324 
325 
326  } // End of namespace
327 } // End of namespace
328 
329 #endif
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1,...
Definition: datetime.h:30
virtual void do_nothing() MRPT_OVERRIDE
Just to allow this class to be polymorphic.
mrpt::utils::CImagePtr m_last_captured_img
An event sent by a CDisplayWindow3D window when an image is saved after enabling this feature with CD...
This class provides simple critical sections functionality.
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
double getRenderingFPS() const
Get the average Frames Per Second (FPS) value from the last 250 rendering events.
const std::string & img_file
The absolute path of the file that has been just saved.
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:26
TOpenGLFontStyle
Different style for vectorized font rendering.
Definition: opengl_fonts.h:37
#define DEFINE_MRPT_OBJECT(class_name)
This declaration must be inserted in all CObject classes definition, within the class declaration.
Definition: CObject.h:167
#define DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
Definition: CObject.h:172
#define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
Definition: CObject.h:171
synch::CCriticalSection m_csAccess3DScene
Critical section for accesing m_3Dscene.
mrptEvent3DWindowGrabImageFile(CDisplayWindow3D *obj, const std::string &_img_file)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp m_lastFullScreen
mrpt::utils::void_ptr_noncopy m_GLRenderingContext
synch::CCriticalSection m_last_captured_img_cs
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:40
A RGB color - floats in the range [0,1].
Definition: TColor.h:52
mrpt::utils::void_ptr_noncopy m_DisplayDeviceContext
opengl::COpenGLScenePtr m_3Dscene
Internal OpenGL object (see general discussion in about usage of this object)
The base class for GUI window classes.
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
3D line, represented by a base point and a director vector.



Page generated by Doxygen 1.8.15 for MRPT 1.4.0 SVN: at Sun Mar 24 23:21:48 UTC 2019