Open3D (C++ API)  0.18.0
Visualizer.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Avoid warning caused by redefinition of APIENTRY macro
11 // defined also in glfw3.h
12 #ifdef _WIN32
13 #include <windows.h>
14 #endif
15 
16 #include <GL/glew.h>
17 #include <GLFW/glfw3.h>
18 
19 #include <Eigen/Core>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <unordered_set>
24 
30 
31 namespace open3d {
32 
33 namespace geometry {
34 class TriangleMesh;
35 class Image;
36 } // namespace geometry
37 
38 namespace visualization {
39 
40 class GLFWContext;
41 
45 class Visualizer {
46 public:
47  struct MouseControl {
48  public:
51  bool is_control_key_down = false;
52  bool is_shift_key_down = false;
53  bool is_alt_key_down = false;
54  bool is_super_key_down = false;
55  double mouse_position_x = 0.0;
56  double mouse_position_y = 0.0;
57  };
58 
59 public:
60  Visualizer();
61  virtual ~Visualizer();
62  Visualizer(Visualizer &&) = delete;
63  Visualizer(const Visualizer &) = delete;
64  Visualizer &operator=(const Visualizer &) = delete;
65 
66 public:
77  bool CreateVisualizerWindow(const std::string &window_name = "Open3D",
78  const int width = 640,
79  const int height = 480,
80  const int left = 50,
81  const int top = 50,
82  const bool visible = true);
83 
88 
95  std::function<bool(Visualizer *)> callback_func);
96 
100  void Run();
101 
103  void Close();
104 
110  bool WaitEvents();
111 
117  bool PollEvents();
118 
133  virtual bool AddGeometry(
134  std::shared_ptr<const geometry::Geometry> geometry_ptr,
135  bool reset_bounding_box = true);
136 
147  virtual bool RemoveGeometry(
148  std::shared_ptr<const geometry::Geometry> geometry_ptr,
149  bool reset_bounding_box = true);
150 
154  virtual bool ClearGeometries();
155 
162  virtual bool UpdateGeometry(
163  std::shared_ptr<const geometry::Geometry> geometry_ptr = nullptr);
164  virtual bool HasGeometry() const;
165 
167  virtual void UpdateRender();
168 
170  virtual void SetFullScreen(bool fullscreen);
171  virtual void ToggleFullScreen();
172  virtual bool IsFullScreen();
173 
174  virtual void PrintVisualizerHelp();
175  virtual void UpdateWindowTitle();
176  virtual void BuildUtilities();
177 
185  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
186  bool do_render = true);
191  void CaptureScreenImage(const std::string &filename = "",
192  bool do_render = true);
196  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
197  bool do_render = true);
203  void CaptureDepthImage(const std::string &filename = "",
204  bool do_render = true,
205  double depth_scale = 1000.0);
212  void CaptureDepthPointCloud(const std::string &filename = "",
213  bool do_render = true,
214  bool convert_to_world_coordinate = false);
215  void CaptureRenderOption(const std::string &filename = "");
216 
218  void ResetViewPoint(bool reset_bounding_box = false);
219 
220  const std::string &GetWindowName() const { return window_name_; }
221 
223  std::string GetViewStatus();
224 
226  void SetViewStatus(const std::string &view_status_str);
227 
228 protected:
230  virtual bool InitOpenGL();
231 
233  virtual bool InitViewControl();
234 
236  virtual bool InitRenderOption();
237 
241  virtual void Render(bool render_screen = false);
242 
245 
248 
250  virtual void WindowRefreshCallback(GLFWwindow *window);
251  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
252  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
253  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
254  virtual void MouseButtonCallback(GLFWwindow *window,
255  int button,
256  int action,
257  int mods);
258  virtual void KeyPressCallback(
259  GLFWwindow *window, int key, int scancode, int action, int mods);
261  virtual void WindowCloseCallback(GLFWwindow *window);
262 
263 protected:
264  // window
265  GLFWwindow *window_ = NULL;
266  std::string window_name_ = "Open3D";
267 
269  std::shared_ptr<GLFWContext> glfw_context_ = nullptr;
270 
271  Eigen::Vector2i saved_window_size_ = Eigen::Vector2i::Zero();
272  Eigen::Vector2i saved_window_pos_ = Eigen::Vector2i::Zero();
273  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
274  // Auxiliary internal backup of the callback function.
275  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
276  // so that even if user calls RegisterAnimationCallback() within the
277  // callback function it is still safe.
279  nullptr;
280 
281  // control
283  bool is_redraw_required_ = true;
284  bool is_initialized_ = false;
285  GLuint vao_id_ = 0;
286 
287  // render targets for "capture_screen_float_buffer" and
288  // "capture_screen_image" in offscreen render mode
289  GLuint render_fbo_ = 0;
290  GLuint render_rgb_tex_ = 0;
292 
293  // view control
294  std::unique_ptr<ViewControl> view_control_ptr_;
295 
296  // rendering properties
297  std::unique_ptr<RenderOption> render_option_ptr_;
298 
299  // geometry to be rendered
300  std::unordered_set<std::shared_ptr<const geometry::Geometry>>
302 
303  // geometry renderers
304  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
306 
307  // utilities owned by the Visualizer
308  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
309 
310  // utility renderers
311  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
312  // map's key is the renderer for which the RenderOption applies
313  // (should be something in utility_renderer_ptrs_)
314  std::unordered_map<std::shared_ptr<glsl::GeometryRenderer>, RenderOption>
316 
317  // coordinate frame
318  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
319  std::shared_ptr<glsl::CoordinateFrameRenderer>
321 
322 #ifdef __APPLE__
323  // MacBook with Retina display does not have a 1:1 mapping from screen
324  // coordinates to pixels. Thus we hack it back.
325  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
326  double pixel_to_screen_coordinate_ = 1.0;
327 #endif //__APPLE__
328 };
329 
330 } // namespace visualization
331 } // namespace open3d
Defines rendering options for visualizer.
Definition: RenderOption.h:20
View controller for visualizer.
Definition: ViewControl.h:23
The main Visualizer class.
Definition: Visualizer.h:45
void CaptureRenderOption(const std::string &filename="")
Definition: VisualizerRender.cpp:502
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:181
bool CreateVisualizerWindow(const std::string &window_name="Open3D", const int width=640, const int height=480, const int left=50, const int top=50, const bool visible=true)
Function to create a window and initialize GLFW.
Definition: Visualizer.cpp:85
virtual void BuildUtilities()
Definition: Visualizer.cpp:256
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:297
virtual void SetFullScreen(bool fullscreen)
Functions to change between fullscreen and windowed modes.
Definition: Visualizer.cpp:468
virtual bool ClearGeometries()
Definition: Visualizer.cpp:440
bool is_initialized_
Definition: Visualizer.h:284
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:294
GLuint vao_id_
Definition: Visualizer.h:285
GLuint render_fbo_
Definition: Visualizer.h:289
bool PollEvents()
Definition: Visualizer.cpp:308
std::shared_ptr< GLFWContext > glfw_context_
Shared GLFW context.
Definition: Visualizer.h:269
void CaptureDepthImage(const std::string &filename="", bool do_render=true, double depth_scale=1000.0)
Definition: VisualizerRender.cpp:337
GLuint render_depth_stencil_rbo_
Definition: Visualizer.h:291
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:308
virtual void UpdateRender()
Function to inform render needed to be updated.
Definition: Visualizer.cpp:464
std::function< bool(Visualizer *)> animation_callback_func_in_loop_
Definition: Visualizer.h:278
Eigen::Vector2i saved_window_pos_
Definition: Visualizer.h:272
void CaptureDepthPointCloud(const std::string &filename="", bool do_render=true, bool convert_to_world_coordinate=false)
Function to capture and save local point cloud.
Definition: VisualizerRender.cpp:420
virtual bool InitOpenGL()
Function to initialize OpenGL.
Definition: VisualizerRender.cpp:29
virtual bool IsFullScreen()
Definition: Visualizer.cpp:496
Visualizer(const Visualizer &)=delete
void CaptureScreenImage(const std::string &filename="", bool do_render=true)
Function to capture and save a screen image.
Definition: VisualizerRender.cpp:219
void Run()
Function to activate the window.
Definition: Visualizer.cpp:274
virtual bool UpdateGeometry(std::shared_ptr< const geometry::Geometry > geometry_ptr=nullptr)
Function to update geometry.
Definition: Visualizer.cpp:450
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:320
virtual bool AddGeometry(std::shared_ptr< const geometry::Geometry > geometry_ptr, bool reset_bounding_box=true)
Function to add geometry to the scene and create corresponding shaders.
Definition: Visualizer.cpp:321
Visualizer(Visualizer &&)=delete
virtual void MouseMoveCallback(GLFWwindow *window, double x, double y)
Definition: VisualizerCallback.cpp:25
virtual void Render(bool render_screen=false)
Definition: VisualizerRender.cpp:69
virtual void WindowResizeCallback(GLFWwindow *window, int w, int h)
Definition: VisualizerCallback.cpp:20
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
Definition: Visualizer.h:179
std::unordered_map< std::shared_ptr< glsl::GeometryRenderer >, RenderOption > utility_renderer_opts_
Definition: Visualizer.h:315
virtual void WindowCloseCallback(GLFWwindow *window)
Function to notify the window to be closed.
Definition: VisualizerCallback.cpp:346
std::shared_ptr< geometry::Image > CaptureDepthFloatBuffer(bool do_render=true)
Definition: VisualizerRender.cpp:273
virtual bool InitViewControl()
Function to initialize ViewControl.
Definition: Visualizer.cpp:239
bool WaitEvents()
Function to process the event queue and return if the window is closed.
Definition: Visualizer.cpp:295
virtual bool InitRenderOption()
Function to initialize RenderOption.
Definition: Visualizer.cpp:245
std::string GetViewStatus()
Get the current view status as a json string of ViewTrajectory.
Definition: VisualizerRender.cpp:157
std::unordered_set< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:301
std::string window_name_
Definition: Visualizer.h:266
virtual void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
Definition: VisualizerCallback.cpp:68
void SetViewStatus(const std::string &view_status_str)
Set the current view status from a json string of ViewTrajectory.
Definition: VisualizerRender.cpp:171
const std::string & GetWindowName() const
Definition: Visualizer.h:220
void CopyViewStatusFromClipboard()
Apply the view point from clipboard.
Definition: VisualizerRender.cpp:149
virtual ~Visualizer()
Definition: Visualizer.cpp:77
void DestroyVisualizerWindow()
Function to destroy a window.
Definition: Visualizer.cpp:220
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:311
virtual bool RemoveGeometry(std::shared_ptr< const geometry::Geometry > geometry_ptr, bool reset_bounding_box=true)
Function to remove geometry from the scene.
Definition: Visualizer.cpp:416
virtual void MouseScrollCallback(GLFWwindow *window, double x, double y)
Definition: VisualizerCallback.cpp:63
GLuint render_rgb_tex_
Definition: Visualizer.h:290
virtual void UpdateWindowTitle()
Definition: Visualizer.cpp:250
void RegisterAnimationCallback(std::function< bool(Visualizer *)> callback_func)
Function to register a callback function for animation.
Definition: Visualizer.cpp:234
std::function< bool(Visualizer *)> animation_callback_func_
Definition: Visualizer.h:273
virtual void WindowRefreshCallback(GLFWwindow *window)
Callback functions.
Definition: VisualizerCallback.cpp:13
Eigen::Vector2i saved_window_size_
Definition: Visualizer.h:271
virtual void ToggleFullScreen()
Definition: Visualizer.cpp:488
MouseControl mouse_control_
Definition: Visualizer.h:282
GLFWwindow * window_
Definition: Visualizer.h:265
virtual void PrintVisualizerHelp()
Definition: Visualizer.cpp:500
void Close()
Function to to notify the window to be closed.
Definition: Visualizer.cpp:290
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:318
std::shared_ptr< geometry::Image > CaptureScreenFloatBuffer(bool do_render=true)
Function to capture screen and store RGB in a float buffer.
Definition: VisualizerRender.cpp:182
virtual bool HasGeometry() const
Definition: Visualizer.cpp:466
bool is_redraw_required_
Definition: Visualizer.h:283
Visualizer()
Definition: Visualizer.cpp:75
virtual void KeyPressCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: VisualizerCallback.cpp:102
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:305
Visualizer & operator=(const Visualizer &)=delete
void CopyViewStatusToClipboard()
Copy the current view status to clipboard.
Definition: VisualizerRender.cpp:145
void ResetViewPoint(bool reset_bounding_box=false)
Function to reset view point.
Definition: VisualizerRender.cpp:126
int width
Definition: FilePCD.cpp:52
int height
Definition: FilePCD.cpp:53
Definition: PinholeCameraIntrinsic.cpp:16
double mouse_position_x
Definition: Visualizer.h:55
bool is_control_key_down
Definition: Visualizer.h:51
bool is_mouse_left_button_down
Definition: Visualizer.h:49
double mouse_position_y
Definition: Visualizer.h:56
bool is_mouse_middle_button_down
Definition: Visualizer.h:50
bool is_super_key_down
Definition: Visualizer.h:54
bool is_shift_key_down
Definition: Visualizer.h:52
bool is_alt_key_down
Definition: Visualizer.h:53