MRPT  1.9.9
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-2019, 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  * \sa The example /samples/display3D, the <a
111  * href="http://www.mrpt.org/Tutorial_3D_Scenes" > tutorial only</a>.
112  * \ingroup mrpt_gui_grp
113  */
115 {
116  public:
119 
120  protected:
121  friend class C3DWindowDialog;
123 
124  /** Internal OpenGL object (see general discussion in about usage of this
125  * object) */
127  /** Critical section for accesing m_3Dscene */
128  mutable std::recursive_mutex m_csAccess3DScene;
129 
130  /** Throws an exception on initialization error */
131  void createOpenGLContext();
132 
135 
137  unsigned int m_grab_imgs_idx{0};
138 
139  bool m_is_capturing_imgs{false};
141  mutable std::mutex m_last_captured_img_cs;
142 
143  void doRender();
144 
146 
147  /** \sa getRenderingFPS */
148  double m_last_FPS{10};
149 
150  void internalSetMinMaxRange();
151 
152  public:
153  /** Constructor */
155  const std::string& windowCaption = std::string(),
156  unsigned int initialWindowWidth = 400,
157  unsigned int initialWindowHeight = 300);
158 
159  /** Class factory returning a smart pointer */
161  const std::string& windowCaption, unsigned int initialWindowWidth = 400,
162  unsigned int initialWindowHeight = 300);
163 
164  /** Destructor */
165  ~CDisplayWindow3D() override;
166 
167  /** Gets a reference to the smart shared pointer that holds the internal
168  * scene (carefuly read introduction in gui::CDisplayWindow3D before use!)
169  * This also locks the critical section for accesing the scene, thus the
170  * window will not be repainted until it is unlocked.
171  * \note It is safer to use mrpt::gui::CDisplayWindow3DLocker instead.*/
173 
174  /** Unlocks the access to the internal 3D scene. It is safer to use
175  * mrpt::gui::CDisplayWindow3DLocker instead.
176  * Typically user will want to call forceRepaint after updating the scene.
177  */
178  void unlockAccess3DScene();
179 
180  /** Repaints the window. forceRepaint, repaint and updateWindow are all
181  * aliases of the same method */
182  void forceRepaint();
183  /** Repaints the window. forceRepaint, repaint and updateWindow are all
184  * aliases of the same method */
185  void repaint() { forceRepaint(); }
186  /** Repaints the window. forceRepaint, repaint and updateWindow are all
187  * aliases of the same method */
189  /** Return the camera field of view (in degrees) (used for gluPerspective)
190  */
191  float getFOV() const;
192  /** Changes the camera min clip range (z) (used for gluPerspective). The
193  * window is not updated with this method, call "forceRepaint" to update the
194  * 3D view. */
195  void setMinRange(double new_min);
196  /** Changes the camera max clip range (z) (used for gluPerspective. The
197  * window is not updated with this method, call "forceRepaint" to update the
198  * 3D view. */
199  void setMaxRange(double new_max);
200  /** Changes the camera field of view (in degrees) (used for gluPerspective).
201  * The window is not updated with this method, call "forceRepaint" to update
202  * the 3D view. */
203  void setFOV(float v);
204  /** Resizes the window, stretching the image to fit into the display area.
205  */
206  void resize(unsigned int width, unsigned int height) override;
207  /** Changes the position of the window on the screen. */
208  void setPos(int x, int y) override;
209  /** Changes the window title. */
210  void setWindowTitle(const std::string& str) override;
211  /** Changes the camera parameters programmatically */
212  void setCameraElevationDeg(float deg);
213  /** Changes the camera parameters programmatically */
214  void setCameraAzimuthDeg(float deg);
215  /** Changes the camera parameters programmatically */
216  void setCameraPointingToPoint(float x, float y, float z);
217  /** Changes the camera parameters programmatically */
218  void setCameraZoom(float zoom);
219  /** Sets the camera as projective, or orthogonal. */
220  void setCameraProjective(bool isProjective);
221  /** Get camera parameters programmatically */
222  float getCameraElevationDeg() const;
223  /** Get camera parameters programmatically */
224  float getCameraAzimuthDeg() const;
225  /** Get camera parameters programmatically */
226  void getCameraPointingToPoint(float& x, float& y, float& z) const;
227  /** Get camera parameters programmatically */
228  float getCameraZoom() const;
229  /** Sets the camera as projective, or orthogonal */
230  bool isCameraProjective() const;
231  /** If set to true (default = false), the mouse-based scene navigation will
232  * be disabled and the camera position will be determined by the opengl
233  * viewports in the 3D scene */
234  void useCameraFromScene(bool useIt = true);
235  /** Gets the 3D ray for the direction line of the pixel where the mouse
236  * cursor is at. \return False if the window is closed. \sa
237  * getLastMousePosition */
239  /** Gets the last x,y pixel coordinates of the mouse. \return False if the
240  * window is closed. \sa getLastMousePositionRay */
241  bool getLastMousePosition(int& x, int& y) const override;
242  /** Set cursor style to default (cursorIsCross=false) or to a cross
243  * (cursorIsCross=true) \sa getLastMousePositionRay */
244  void setCursorCross(bool cursorIsCross) override;
245 
246  /** Start to save rendered images to disk.
247  * Images will be saved independently as png files, depending on
248  * the template path passed to this method. For example:
249  *
250  * path_prefix: "./video_"
251  *
252  * Will generate "./video_000001.png", etc.
253  *
254  * If this feature is enabled, the window will emit events of the type
255  * mrpt::gui::mrptEvent3DWindowGrabImageFile() which you can subscribe to.
256  *
257  * \sa grabImagesStop
258  */
259  void grabImagesStart(
260  const std::string& grab_imgs_prefix = std::string("video_"));
261 
262  /** Stops image grabbing started by grabImagesStart
263  * \sa grabImagesStart
264  */
265  void grabImagesStop();
266 
267  /** Enables the grabbing of CImage objects from screenshots of the window.
268  * \sa getLastWindowImage
269  */
270  void captureImagesStart();
271 
272  /** Stop image grabbing
273  * \sa captureImagesStart
274  */
275  void captureImagesStop();
276 
277  /** Retrieve the last captured image from the window.
278  * You MUST CALL FIRST captureImagesStart to enable image grabbing.
279  * \return false if there was no time yet for grabbing any image (then, the
280  * output image is undefined).
281  * \sa captureImagesStart, getLastWindowImagePtr
282  */
283  bool getLastWindowImage(mrpt::img::CImage& out_img) const;
284 
285  /** Retrieve the last captured image from the window, as a smart pointer.
286  * This method is more efficient than getLastWindowImage since only a copy
287  * of the pointer is performed, while
288  * getLastWindowImage would copy the entire image.
289  *
290  * You MUST CALL FIRST captureImagesStart to enable image grabbing.
291  * \Note If there was no time yet for grabbing any image, an empty smart
292  * pointer will be returned.
293  * \sa captureImagesStart, getLastWindowImage
294  */
296 
297  /** Increments by one the image counter and return the next image file name
298  * (Users normally don't want to call this method).
299  * \sa grabImagesStart
300  */
302 
303  bool isCapturingImgs() const { return m_is_capturing_imgs; }
304  /** Add 2D text messages overlapped to the 3D rendered scene. The string
305  * will remain displayed in the 3D window
306  * until it's changed with subsequent calls to this same method, or all
307  * the texts are cleared with clearTextMessages().
308  *
309  * \param x The X position, interpreted as absolute pixels from the left
310  * if X>=1, absolute pixels from the left if X<0 or as a width factor if in
311  * the range [0,1[.
312  * \param y The Y position, interpreted as absolute pixels from the bottom
313  * if Y>=1, absolute pixels from the top if Y<0 or as a height factor if in
314  * the range [0,1[.
315  * \param text The text string to display.
316  * \param color The text color. For example: TColorf(1.0,1.0,1.0)
317  * \param unique_index An "index" for this text message, so that
318  * subsequent calls with the same index will overwrite this text message
319  * instead of creating new ones.
320  *
321  * You'll need to refresh the display manually with forceRepaint().
322  *
323  * \sa clearTextMessages
324  */
325  void addTextMessage(
326  const double x, const double y, const std::string& text,
327  const mrpt::img::TColorf& color = mrpt::img::TColorf(1.0, 1.0, 1.0),
328  const size_t unique_index = 0,
329  const mrpt::opengl::TOpenGLFont font =
331 
332  /** overload with more font parameters - refer to
333  * mrpt::opengl::gl_utils::glDrawText()
334  * Available fonts are enumerated at mrpt::opengl::gl_utils::glSetFont() */
335  void addTextMessage(
336  const double x_frac, const double y_frac, const std::string& text,
337  const mrpt::img::TColorf& color, const std::string& font_name,
338  const double font_size,
340  const size_t unique_index = 0, const double font_spacing = 1.5,
341  const double font_kerning = 0.1, const bool draw_shadow = false,
342  const mrpt::img::TColorf& shadow_color = mrpt::img::TColorf(0, 0, 0));
343 
344  /** Clear all text messages created with addTextMessage().
345  * You'll need to refresh the display manually with forceRepaint().
346  * \sa addTextMessage
347  */
348  void clearTextMessages();
349 
350  /** Get the average Frames Per Second (FPS) value from the last 250
351  * rendering events */
352  double getRenderingFPS() const { return m_last_FPS; }
353  /** A short cut for getting the "main" viewport of the scene object, it is
354  * equivalent to:
355  * \code
356  * mrpt::opengl::COpenGLScene::Ptr &scene = win3D.get3DSceneAndLock();
357  * viewport = scene->getViewport("main");
358  * win3D.unlockAccess3DScene();
359  * \endcode
360  */
362 
363  /** Set the "main" viewport into "image view"-mode, where an image is
364  * efficiently drawn (fitting the viewport area) using an OpenGL textured
365  * quad.
366  * Call this method with the new image to update the displayed image (but
367  * recall to first lock the parent openglscene's critical section, then do
368  * the update, then release the lock, and then issue a window repaint).
369  * Internally, the texture is drawn using a mrpt::opengl::CTexturedPlane
370  * The viewport can be reverted to behave like a normal viewport by
371  * calling setNormalMode()
372  * \sa COpenGLViewport
373  * \note This method already locks/unlocks the 3D scene of the window, so
374  * the user must NOT call get3DSceneAndLock() / unlockAccess3DScene()
375  * before/after calling it.
376  */
377  void setImageView(const mrpt::img::CImage& img);
378 
379  /** Just like \a setImageView but moves the internal image memory instead of
380  * making a copy, so it's faster but empties the input image.
381  * \sa setImageView, COpenGLViewport
382  * \note This method already locks/unlocks the 3D scene of the window, so
383  * the user must NOT call get3DSceneAndLock() / unlockAccess3DScene()
384  * before/after calling it.
385  */
387 
388  protected:
389  /** Set the rendering FPS (users don't call this, the method is for internal
390  * MRPT objects only) \sa getRenderingFPS */
391  void internal_setRenderingFPS(double FPS);
392  /** called by CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers */
393  void internal_emitGrabImageEvent(const std::string& fil);
394 
395 }; // End of class def.
396 
397 /** @name Events specific to CDisplayWindow3D
398  @{ */
399 
400 /** An event sent by a CDisplayWindow3D window when an image is saved after
401  * enabling this feature with CDisplayWindow3D::grabImagesStart()
402  *
403  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
404  * from the wxWidgets internal MRPT thread,
405  * so all your code in the handler must be thread safe.
406  */
408 {
409  protected:
410  /** Just to allow this class to be polymorphic */
411  void do_nothing() override {}
412 
413  public:
415  CDisplayWindow3D* obj, const std::string& _img_file)
416  : source_object(obj), img_file(_img_file)
417  {
418  }
419 
421  /** The absolute path of the file that has been just saved. */
423 }; // End of class def.
424 
425 /** @} */
426 
427 /** Auxiliary class for safely claiming the 3DScene of a
428  * mrpt::gui::CDisplayWindow3D.
429  * The mutex will be hold between ctor and dtor calls of objects of this class,
430  * safely releasing
431  * the lock upon exceptions. See example usage code in docs of
432  * mrpt::gui::CDisplayWindow3D
433  *
434  * \ingroup mrpt_gui_grp
435  * \note New in MRPT 1.5.0
436  */
438 {
439  public:
440  /** Acquires the lock of the 3D scene of the referenced window, and returns
441  * a copy of the smart pointer to it. */
444  /** Acquires the lock of the 3D scene of the referenced window. Use this
445  * signature when the scene object is not required. */
448 
449  private:
451 };
452 
453 } // 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.
std::recursive_mutex m_csAccess3DScene
Critical section for accesing m_3Dscene.
GLdouble GLdouble z
Definition: glext.h:3879
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.
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.
void createOpenGLContext()
Throws an exception on initialization error.
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.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
GLenum GLsizei width
Definition: glext.h:3535
friend class CMyGLCanvas_DisplayWindow3D
std::shared_ptr< CImage > Ptr
Definition: img/CImage.h:149
mrpt::opengl::COpenGLViewport::Ptr getDefaultViewport()
A short cut for getting the "main" viewport of the scene object, it is equivalent to: ...
GLuint color
Definition: glext.h:8459
double getRenderingFPS() const
Get the average Frames Per Second (FPS) value from the last 250 rendering events. ...
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.
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:22
void setMaxRange(double new_max)
Changes the camera max clip range (z) (used for gluPerspective.
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...
GLint GLvoid * img
Definition: glext.h:3769
TOpenGLFontStyle
Different style for vectorized font rendering.
Definition: opengl_fonts.h:33
mrpt::gui::CDisplayWindow3D::Ptr win
GLsizei const GLchar ** string
Definition: glext.h:4116
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)
const GLdouble * v
Definition: glext.h:3684
mrpt::system::TTimeStamp m_lastFullScreen
void grabImagesStop()
Stops image grabbing started by grabImagesStart.
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
bool isCameraProjective() const
Sets the camera as projective, or orthogonal.
void setCameraZoom(float zoom)
Changes the camera parameters programmatically.
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:37
void updateWindow()
Repaints the window.
void setCursorCross(bool cursorIsCross) override
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) ...
GLenum GLint GLint y
Definition: glext.h:3542
void setMinRange(double new_min)
Changes the camera min clip range (z) (used for gluPerspective).
bool getLastWindowImage(mrpt::img::CImage &out_img) const
Retrieve the last captured image from the window.
void addTextMessage(const double x, const double y, const std::string &text, const mrpt::img::TColorf &color=mrpt::img::TColorf(1.0, 1.0, 1.0), const size_t unique_index=0, const mrpt::opengl::TOpenGLFont font=mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_24)
Add 2D text messages overlapped to the 3D rendered scene.
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.
GLenum GLint x
Definition: glext.h:3542
void repaint()
Repaints the window.
GLenum GLsizei GLsizei height
Definition: glext.h:3558
void internal_setRenderingFPS(double FPS)
Set the rendering FPS (users don&#39;t call this, the method is for internal MRPT objects only) ...
void clearTextMessages()
Clear all text messages created with addTextMessage().
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.
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:147
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 1.9.9 Git: 034c2ee2a Tue Aug 20 02:15:02 2019 +0200 at mar ago 20 02:20:10 CEST 2019