MRPT  2.0.4
CDisplayWindow3D.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/img/CImage.h>
15 #include <mrpt/system/datetime.h>
16 
17 #include <mutex>
18 
19 namespace mrpt::gui
20 {
21 class C3DWindowDialog;
22 class CMyGLCanvas_DisplayWindow3D;
23 
24 /** A graphical user interface (GUI) for efficiently rendering 3D scenes in
25  * real-time.
26  * This class always contains internally an instance of opengl::COpenGLScene,
27  * which
28  * the objects, viewports, etc. to be rendered.
29  *
30  * Images can be grabbed automatically to disk for easy creation of videos.
31  * See CDisplayWindow3D::grabImagesStart (and for creating videos,
32  * mrpt::vision::CVideoFileWriter).
33  *
34  * A short-cut for displaying 2D images (using the OpenGL rendering hardware)
35  * is available
36  * through \a setImageView() . Internally, these methods call methods
37  * in the "main" viewport of the window (see \a COpenGLViewport).
38  *
39  * Since the 3D rendering is performed in a detached thread, especial care
40  * must be taken
41  * when updating the 3D scene to be rendered. The process involves an
42  * internal critical section
43  * and it must always consist of these steps:
44  *
45  * \code
46  * mrpt::gui::CDisplayWindow3D win("My window");
47  *
48  * // Adquire the scene:
49  * mrpt::opengl::COpenGLScene::Ptr &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
64  * 3D window, a new object
65  * of class COpenGLScene, then locking the window only for replacing the smart
66  * pointer. This may be
67  * advantageous is generating the 3D scene takes a long time, since while the
68  * window
69  * is locked it will not be responsive to the user input or window redraw.
70  *
71  * It is safer against exceptions to use the auxiliary class
72  * CDisplayWindow3DLocker.
73  * \code
74  * mrpt::gui::CDisplayWindow3D win("My window");
75  * // ...
76  * { // The scene is adquired in this scope
77  * mrpt::opengl::COpenGLScene::Ptr ptrScene;
78  * mrpt::gui::CDisplayWindow3DLocker locker(win,ptrScene);
79  * //...
80  *
81  * } // scene is unlocked upon dtor of `locker`
82  * \endcode
83  *
84  * Notice however that a copy of the smart pointer is made, so replacement of
85  * the entire scene
86  * via `operator =` is not possible if using this method. Still, in general it
87  * should be preferred because
88  * the mutexes are automatically released in case of unexpected exceptions.
89  *
90  * The window can also display a set of 2D text messages overlapped to the 3D
91  * scene.
92  * See CDisplayWindow3D::addTextMessage
93  *
94  * For a list of supported events with the observer/observable pattern, see
95  * the discussion in mrpt::gui::CBaseGUIWindow.
96  * In addition to those events, this class introduces
97  * mrpt::gui::mrptEvent3DWindowGrabImageFile
98  *
99  * ** CDisplayWindow3D mouse view navigation cheatsheet **
100  * - <b>Orbit camera</b>: Left-button pressed + move
101  * - <b>Zoom in / out</b>:
102  * - Mouse scroll wheel, or
103  * - SHIFT+Left-button pressed + move up/down
104  * - <b>Look around (pivot camera)</b>: CTRL+Left-button pressed + move
105  * up/down
106  * - <b>Pan (XY plane)</b>: Right-button pressed + move
107  * - <b>Move camera along Z axis</b>: SHIFT+Left-button pressed + move
108  * left/right
109  *
110  *
111  * ![mrpt::gui::CDisplayWindow3D screenshot](preview_CDisplayWindow3D.png)
112  *
113  * \sa The example /samples/display3D, the <a
114  * href="http://www.mrpt.org/Tutorial_3D_Scenes" > tutorial only</a>.
115  * \ingroup mrpt_gui_grp
116  */
118 {
119  public:
122 
123  protected:
124  friend class C3DWindowDialog;
126 
127  /** Internal OpenGL object (see general discussion in about usage of this
128  * object) */
130  /** Critical section for accesing m_3Dscene */
131  mutable std::recursive_timed_mutex m_csAccess3DScene;
132 
133  /** Throws an exception on initialization error */
134  void createOpenGLContext();
135 
138 
139  std::string m_grab_imgs_prefix;
140  unsigned int m_grab_imgs_idx{0};
141 
142  bool m_is_capturing_imgs{false};
144  mutable std::mutex m_last_captured_img_cs;
145 
146  void doRender();
147 
149 
150  /** \sa getRenderingFPS */
151  double m_last_FPS{10};
152 
153  void internalSetMinMaxRange();
154 
155  public:
156  /** Constructor */
158  const std::string& windowCaption = std::string(),
159  unsigned int initialWindowWidth = 400,
160  unsigned int initialWindowHeight = 300);
161 
162  /** Class factory returning a smart pointer */
164  const std::string& windowCaption, unsigned int initialWindowWidth = 400,
165  unsigned int initialWindowHeight = 300);
166 
167  /** Destructor */
168  ~CDisplayWindow3D() override;
169 
170  /** Gets a reference to the smart shared pointer that holds the internal
171  * scene (carefuly read introduction in gui::CDisplayWindow3D before use!)
172  * This also locks the critical section for accesing the scene, thus the
173  * window will not be repainted until it is unlocked.
174  * \note It is safer to use mrpt::gui::CDisplayWindow3DLocker instead.*/
176 
177  /** Unlocks the access to the internal 3D scene. It is safer to use
178  * mrpt::gui::CDisplayWindow3DLocker instead.
179  * Typically user will want to call forceRepaint after updating the scene.
180  */
181  void unlockAccess3DScene();
182 
183  /** Repaints the window. forceRepaint, repaint and updateWindow are all
184  * aliases of the same method */
185  void forceRepaint();
186  /** Repaints the window. forceRepaint, repaint and updateWindow are all
187  * aliases of the same method */
188  void repaint() { forceRepaint(); }
189  /** Repaints the window. forceRepaint, repaint and updateWindow are all
190  * aliases of the same method */
192  /** Return the camera field of view (in degrees) (used for gluPerspective)
193  */
194  float getFOV() const;
195  /** Changes the camera min clip range (z) (used for gluPerspective). The
196  * window is not updated with this method, call "forceRepaint" to update the
197  * 3D view. */
198  void setMinRange(float new_min);
199  /** Changes the camera max clip range (z) (used for gluPerspective. The
200  * window is not updated with this method, call "forceRepaint" to update the
201  * 3D view. */
202  void setMaxRange(float new_max);
203  /** Changes the camera field of view (in degrees) (used for gluPerspective).
204  * The window is not updated with this method, call "forceRepaint" to update
205  * the 3D view. */
206  void setFOV(float v);
207  /** Resizes the window, stretching the image to fit into the display area.
208  */
209  void resize(unsigned int width, unsigned int height) override;
210  /** Changes the position of the window on the screen. */
211  void setPos(int x, int y) override;
212  /** Changes the window title. */
213  void setWindowTitle(const std::string& str) override;
214  /** Changes the camera parameters programmatically */
215  void setCameraElevationDeg(float deg);
216  /** Changes the camera parameters programmatically */
217  void setCameraAzimuthDeg(float deg);
218  /** Changes the camera parameters programmatically */
219  void setCameraPointingToPoint(float x, float y, float z);
220  /** Changes the camera parameters programmatically */
221  void setCameraZoom(float zoom);
222  /** Sets the camera as projective, or orthogonal. */
223  void setCameraProjective(bool isProjective);
224  /** Get camera parameters programmatically */
225  float getCameraElevationDeg() const;
226  /** Get camera parameters programmatically */
227  float getCameraAzimuthDeg() const;
228  /** Get camera parameters programmatically */
229  void getCameraPointingToPoint(float& x, float& y, float& z) const;
230  /** Get camera parameters programmatically */
231  float getCameraZoom() const;
232  /** Sets the camera as projective, or orthogonal */
233  bool isCameraProjective() const;
234  /** If set to true (default = false), the mouse-based scene navigation will
235  * be disabled and the camera position will be determined by the opengl
236  * viewports in the 3D scene */
237  void useCameraFromScene(bool useIt = true);
238  /** Gets the 3D ray for the direction line of the pixel where the mouse
239  * cursor is at. \return False if the window is closed. \sa
240  * getLastMousePosition */
242  /** Gets the last x,y pixel coordinates of the mouse. \return False if the
243  * window is closed. \sa getLastMousePositionRay */
244  bool getLastMousePosition(int& x, int& y) const override;
245  /** Set cursor style to default (cursorIsCross=false) or to a cross
246  * (cursorIsCross=true) \sa getLastMousePositionRay */
247  void setCursorCross(bool cursorIsCross) override;
248 
249  /** Start to save rendered images to disk.
250  * Images will be saved independently as png files, depending on
251  * the template path passed to this method. For example:
252  *
253  * path_prefix: "./video_"
254  *
255  * Will generate "./video_000001.png", etc.
256  *
257  * If this feature is enabled, the window will emit events of the type
258  * mrpt::gui::mrptEvent3DWindowGrabImageFile() which you can subscribe to.
259  *
260  * \sa grabImagesStop
261  */
262  void grabImagesStart(
263  const std::string& grab_imgs_prefix = std::string("video_"));
264 
265  /** Stops image grabbing started by grabImagesStart
266  * \sa grabImagesStart
267  */
268  void grabImagesStop();
269 
270  /** Enables the grabbing of CImage objects from screenshots of the window.
271  * \sa getLastWindowImage
272  */
273  void captureImagesStart();
274 
275  /** Stop image grabbing
276  * \sa captureImagesStart
277  */
278  void captureImagesStop();
279 
280  /** Retrieve the last captured image from the window.
281  * You MUST CALL FIRST captureImagesStart to enable image grabbing.
282  * \return false if there was no time yet for grabbing any image (then, the
283  * output image is undefined).
284  * \sa captureImagesStart, getLastWindowImagePtr
285  */
286  bool getLastWindowImage(mrpt::img::CImage& out_img) const;
287 
288  /** Retrieve the last captured image from the window, as a smart pointer.
289  * This method is more efficient than getLastWindowImage since only a copy
290  * of the pointer is performed, while
291  * getLastWindowImage would copy the entire image.
292  *
293  * You MUST CALL FIRST captureImagesStart to enable image grabbing.
294  * \Note If there was no time yet for grabbing any image, an empty smart
295  * pointer will be returned.
296  * \sa captureImagesStart, getLastWindowImage
297  */
299 
300  /** Increments by one the image counter and return the next image file name
301  * (Users normally don't want to call this method).
302  * \sa grabImagesStart
303  */
304  std::string grabImageGetNextFile();
305 
306  bool isCapturingImgs() const { return m_is_capturing_imgs; }
307 
308  /** A shortcut for calling mrpt::opengl::COpenGLViewport::addTextMessage()
309  * in the "main" viewport of the 3D scene.
310  * \sa clearTextMessages, mrpt::opengl::COpenGLViewport::addTextMessage()
311  */
313  const double x_frac, const double y_frac, const std::string& text,
314  const size_t unique_index = 0,
315  const mrpt::opengl::TFontParams& fontParams =
317  {
318  if (!m_3Dscene) return;
319  auto gl_view = m_3Dscene->getViewport();
320  if (!gl_view) return;
321  gl_view->addTextMessage(x_frac, y_frac, text, unique_index, fontParams);
322  }
323 
324  /** Clear all text messages created with addTextMessage(). A shortcut for
325  * calling mrpt::opengl::COpenGLViewport::clearTextMessages().
326  *
327  * \sa addTextMessage
328  */
330  {
331  if (!m_3Dscene) return;
332  auto gl_view = m_3Dscene->getViewport();
333  if (!gl_view) return;
334  gl_view->clearTextMessages();
335  }
336 
337  /** Just updates the text of a given text message, without touching the
338  * other parameters. A shortcut for
339  * calling mrpt::opengl::COpenGLViewport::updateTextMessage()
340  *
341  * \return false if given ID doesn't exist.
342  */
343  bool updateTextMessage(const size_t unique_index, const std::string& text)
344  {
345  if (!m_3Dscene) return false;
346  auto gl_view = m_3Dscene->getViewport();
347  if (!gl_view) return false;
348  return gl_view->updateTextMessage(unique_index, text);
349  }
350 
351  /** Get the average Frames Per Second (FPS) value from the last 250
352  * rendering events */
353  double getRenderingFPS() const { return m_last_FPS; }
354  /** A short cut for getting the "main" viewport of the scene object, it is
355  * equivalent to:
356  * \code
357  * mrpt::opengl::COpenGLScene::Ptr &scene = win3D.get3DSceneAndLock();
358  * viewport = scene->getViewport("main");
359  * win3D.unlockAccess3DScene();
360  * \endcode
361  */
363 
364  /** Set the "main" viewport into "image view"-mode, where an image is
365  * efficiently drawn (fitting the viewport area) using an OpenGL textured
366  * quad.
367  * Call this method with the new image to update the displayed image (but
368  * recall to first lock the parent openglscene's critical section, then do
369  * the update, then release the lock, and then issue a window repaint).
370  * Internally, the texture is drawn using a mrpt::opengl::CTexturedPlane
371  * The viewport can be reverted to behave like a normal viewport by
372  * calling setNormalMode()
373  * \sa COpenGLViewport
374  * \note This method already locks/unlocks the 3D scene of the window, so
375  * the user must NOT call get3DSceneAndLock() / unlockAccess3DScene()
376  * before/after calling it.
377  */
378  void setImageView(const mrpt::img::CImage& img);
379 
380  /** Just like \a setImageView but moves the internal image memory instead of
381  * making a copy, so it's faster but empties the input image.
382  * \sa setImageView, COpenGLViewport
383  * \note This method already locks/unlocks the 3D scene of the window, so
384  * the user must NOT call get3DSceneAndLock() / unlockAccess3DScene()
385  * before/after calling it.
386  */
387  void setImageView(mrpt::img::CImage&& img);
388 
389  protected:
390  /** Set the rendering FPS (users don't call this, the method is for internal
391  * MRPT objects only) \sa getRenderingFPS */
392  void internal_setRenderingFPS(double FPS);
393  /** called by CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers */
394  void internal_emitGrabImageEvent(const std::string& fil);
395 
396 }; // End of class def.
397 
398 /** @name Events specific to CDisplayWindow3D
399  @{ */
400 
401 /** An event sent by a CDisplayWindow3D window when an image is saved after
402  * enabling this feature with CDisplayWindow3D::grabImagesStart()
403  *
404  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
405  * from the wxWidgets internal MRPT thread,
406  * so all your code in the handler must be thread safe.
407  */
409 {
410  protected:
411  /** Just to allow this class to be polymorphic */
412  void do_nothing() override {}
413 
414  public:
416  CDisplayWindow3D* obj, const std::string& _img_file)
417  : source_object(obj), img_file(_img_file)
418  {
419  }
420 
422  /** The absolute path of the file that has been just saved. */
423  const std::string& img_file;
424 }; // End of class def.
425 
426 /** @} */
427 
428 /** Auxiliary class for safely claiming the 3DScene of a
429  * mrpt::gui::CDisplayWindow3D.
430  * The mutex will be hold between ctor and dtor calls of objects of this class,
431  * safely releasing
432  * the lock upon exceptions. See example usage code in docs of
433  * mrpt::gui::CDisplayWindow3D
434  *
435  * \ingroup mrpt_gui_grp
436  * \note New in MRPT 1.5.0
437  */
439 {
440  public:
441  /** Acquires the lock of the 3D scene of the referenced window, and returns
442  * a copy of the smart pointer to it. */
445  /** Acquires the lock of the 3D scene of the referenced window. Use this
446  * signature when the scene object is not required. */
449 
450  private:
452 };
453 
454 } // namespace mrpt::gui
bool getLastMousePosition(int &x, int &y) const override
Gets the last x,y pixel coordinates of the mouse.
float getCameraAzimuthDeg() const
Get camera parameters programmatically.
bool getLastMousePositionRay(mrpt::math::TLine3D &ray) const
Gets the 3D ray for the direction line of the pixel where the mouse cursor is at. ...
An event sent by a CDisplayWindow3D window when an image is saved after enabling this feature with CD...
Auxiliary class for safely claiming the 3DScene of a mrpt::gui::CDisplayWindow3D. ...
void unlockAccess3DScene()
Unlocks the access to the internal 3D scene.
mrpt::img::CImage::Ptr getLastWindowImagePtr() const
Retrieve the last captured image from the window, as a smart pointer.
CDisplayWindow3DLocker(CDisplayWindow3D &win, mrpt::opengl::COpenGLScene::Ptr &out_scene_ptr)
Acquires the lock of the 3D scene of the referenced window, and returns a copy of the smart pointer t...
void setWindowTitle(const std::string &str) override
Changes the window title.
void clearTextMessages()
Clear all text messages created with addTextMessage().
mrpt::opengl::COpenGLScene::Ptr & get3DSceneAndLock()
Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introductio...
void grabImagesStart(const std::string &grab_imgs_prefix=std::string("video_"))
Start to save rendered images to disk.
void resize(unsigned int width, unsigned int height) override
Resizes the window, stretching the image to fit into the display area.
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
void setCameraPointingToPoint(float x, float y, float z)
Changes the camera parameters programmatically.
A description of a bitmapped or vectorized text font.
Definition: opengl_fonts.h:36
void createOpenGLContext()
Throws an exception on initialization error.
void setMaxRange(float new_max)
Changes the camera max clip range (z) (used for gluPerspective.
static CDisplayWindow3D::Ptr Create(const std::string &windowCaption, unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Class factory returning a smart pointer.
void internal_emitGrabImageEvent(const std::string &fil)
called by CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers
void setCameraProjective(bool isProjective)
Sets the camera as projective, or orthogonal.
mrpt::void_ptr_noncopy m_GLRenderingContext
float getCameraZoom() const
Get camera parameters programmatically.
friend class CMyGLCanvas_DisplayWindow3D
mrpt::opengl::COpenGLViewport::Ptr getDefaultViewport()
A short cut for getting the "main" viewport of the scene object, it is equivalent to: ...
double getRenderingFPS() const
Get the average Frames Per Second (FPS) value from the last 250 rendering events. ...
void setMinRange(float new_min)
Changes the camera min clip range (z) (used for gluPerspective).
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
mrpt::void_ptr_noncopy m_DisplayDeviceContext
const std::string & img_file
The absolute path of the file that has been just saved.
void getCameraPointingToPoint(float &x, float &y, float &z) const
Get camera parameters programmatically.
void setPos(int x, int y) override
Changes the position of the window on the screen.
~CDisplayWindow3D() override
Destructor.
std::string grabImageGetNextFile()
Increments by one the image counter and return the next image file name (Users normally don&#39;t want to...
mrpt::gui::CDisplayWindow3D::Ptr win
void forceRepaint()
Repaints the window.
void setImageView(const mrpt::img::CImage &img)
Set the "main" viewport into "image view"-mode, where an image is efficiently drawn (fitting the view...
mrptEvent3DWindowGrabImageFile(CDisplayWindow3D *obj, const std::string &_img_file)
float getFOV() const
Return the camera field of view (in degrees) (used for gluPerspective)
std::recursive_timed_mutex m_csAccess3DScene
Critical section for accesing m_3Dscene.
void addTextMessage(const double x_frac, const double y_frac, const std::string &text, const size_t unique_index=0, const mrpt::opengl::TFontParams &fontParams=mrpt::opengl::TFontParams())
A shortcut for calling mrpt::opengl::COpenGLViewport::addTextMessage() in the "main" viewport of the ...
mrpt::system::TTimeStamp m_lastFullScreen
void grabImagesStop()
Stops image grabbing started by grabImagesStart.
bool isCameraProjective() const
Sets the camera as projective, or orthogonal.
void setCameraZoom(float zoom)
Changes the camera parameters programmatically.
bool updateTextMessage(const size_t unique_index, const std::string &text)
Just updates the text of a given text message, without touching the other parameters.
void updateWindow()
Repaints the window.
void setCursorCross(bool cursorIsCross) override
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) ...
bool getLastWindowImage(mrpt::img::CImage &out_img) const
Retrieve the last captured image from the window.
mrpt::img::CImage::Ptr m_last_captured_img
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void captureImagesStop()
Stop image grabbing.
CDisplayWindow3D(const std::string &windowCaption=std::string(), unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Constructor.
void repaint()
Repaints the window.
void internal_setRenderingFPS(double FPS)
Set the rendering FPS (users don&#39;t call this, the method is for internal MRPT objects only) ...
mrpt::opengl::COpenGLScene::Ptr m_3Dscene
Internal OpenGL object (see general discussion in about usage of this object)
The base class for GUI window classes based on wxWidgets.
void setFOV(float v)
Changes the camera field of view (in degrees) (used for gluPerspective).
void setCameraAzimuthDeg(float deg)
Changes the camera parameters programmatically.
void do_nothing() override
Just to allow this class to be polymorphic.
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148
void setCameraElevationDeg(float deg)
Changes the camera parameters programmatically.
void useCameraFromScene(bool useIt=true)
If set to true (default = false), the mouse-based scene navigation will be disabled and the camera po...
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
void captureImagesStart()
Enables the grabbing of CImage objects from screenshots of the window.
3D line, represented by a base point and a director vector.
Definition: TLine3D.h:19
float getCameraElevationDeg() const
Get camera parameters programmatically.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020