Open3D (C++ API)  0.17.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 
43 class Visualizer {
44 public:
45  struct MouseControl {
46  public:
49  bool is_control_key_down = false;
50  bool is_shift_key_down = false;
51  bool is_alt_key_down = false;
52  bool is_super_key_down = false;
53  double mouse_position_x = 0.0;
54  double mouse_position_y = 0.0;
55  };
56 
57 public:
58  Visualizer();
59  virtual ~Visualizer();
60  Visualizer(Visualizer &&) = delete;
61  Visualizer(const Visualizer &) = delete;
62  Visualizer &operator=(const Visualizer &) = delete;
63 
64 public:
75  bool CreateVisualizerWindow(const std::string &window_name = "Open3D",
76  const int width = 640,
77  const int height = 480,
78  const int left = 50,
79  const int top = 50,
80  const bool visible = true);
81 
86 
93  std::function<bool(Visualizer *)> callback_func);
94 
98  void Run();
99 
101  void Close();
102 
108  bool WaitEvents();
109 
115  bool PollEvents();
116 
130  virtual bool AddGeometry(
131  std::shared_ptr<const geometry::Geometry> geometry_ptr,
132  bool reset_bounding_box = true);
133 
143  virtual bool RemoveGeometry(
144  std::shared_ptr<const geometry::Geometry> geometry_ptr,
145  bool reset_bounding_box = true);
146 
150  virtual bool ClearGeometries();
151 
158  virtual bool UpdateGeometry(
159  std::shared_ptr<const geometry::Geometry> geometry_ptr = nullptr);
160  virtual bool HasGeometry() const;
161 
163  virtual void UpdateRender();
164 
166  virtual void SetFullScreen(bool fullscreen);
167  virtual void ToggleFullScreen();
168  virtual bool IsFullScreen();
169 
170  virtual void PrintVisualizerHelp();
171  virtual void UpdateWindowTitle();
172  virtual void BuildUtilities();
173 
176  const ViewControl &GetViewControl() const { return *view_control_ptr_; }
182  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
183  bool do_render = true);
188  void CaptureScreenImage(const std::string &filename = "",
189  bool do_render = true);
193  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
194  bool do_render = true);
200  void CaptureDepthImage(const std::string &filename = "",
201  bool do_render = true,
202  double depth_scale = 1000.0);
209  void CaptureDepthPointCloud(const std::string &filename = "",
210  bool do_render = true,
211  bool convert_to_world_coordinate = false);
212  void CaptureRenderOption(const std::string &filename = "");
214  void ResetViewPoint(bool reset_bounding_box = false);
215 
216  const std::string &GetWindowName() const { return window_name_; }
217 
218 protected:
220  virtual bool InitOpenGL();
221 
223  virtual bool InitViewControl();
224 
226  virtual bool InitRenderOption();
227 
231  virtual void Render(bool render_screen = false);
232 
234 
236 
237  // callback functions
238  virtual void WindowRefreshCallback(GLFWwindow *window);
239  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
240  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
241  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
242  virtual void MouseButtonCallback(GLFWwindow *window,
243  int button,
244  int action,
245  int mods);
246  virtual void KeyPressCallback(
247  GLFWwindow *window, int key, int scancode, int action, int mods);
249  virtual void WindowCloseCallback(GLFWwindow *window);
250 
251 protected:
252  // window
253  GLFWwindow *window_ = NULL;
254  std::string window_name_ = "Open3D";
255  Eigen::Vector2i saved_window_size_ = Eigen::Vector2i::Zero();
256  Eigen::Vector2i saved_window_pos_ = Eigen::Vector2i::Zero();
257  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
258  // Auxiliary internal backup of the callback function.
259  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
260  // so that even if user calls RegisterAnimationCallback() within the
261  // callback function it is still safe.
263  nullptr;
264 
265  // control
267  bool is_redraw_required_ = true;
268  bool is_initialized_ = false;
269  GLuint vao_id_ = 0;
270 
271  // render targets for "capture_screen_float_buffer" and
272  // "capture_screen_image" in offscreen render mode
273  GLuint render_fbo_ = 0;
274  GLuint render_rgb_tex_ = 0;
276 
277  // view control
278  std::unique_ptr<ViewControl> view_control_ptr_;
279 
280  // rendering properties
281  std::unique_ptr<RenderOption> render_option_ptr_;
282 
283  // geometry to be rendered
284  std::unordered_set<std::shared_ptr<const geometry::Geometry>>
286 
287  // geometry renderers
288  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
290 
291  // utilities owned by the Visualizer
292  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
293 
294  // utility renderers
295  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
296  // map's key is the renderer for which the RenderOption applies
297  // (should be something in utility_renderer_ptrs_)
298  std::unordered_map<std::shared_ptr<glsl::GeometryRenderer>, RenderOption>
300 
301  // coordinate frame
302  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
303  std::shared_ptr<glsl::CoordinateFrameRenderer>
305 
306 #ifdef __APPLE__
307  // MacBook with Retina display does not have a 1:1 mapping from screen
308  // coordinates to pixels. Thus we hack it back.
309  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
310  double pixel_to_screen_coordinate_ = 1.0;
311 #endif //__APPLE__
312 };
313 
314 } // namespace visualization
315 } // 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:43
void CaptureRenderOption(const std::string &filename="")
Definition: VisualizerRender.cpp:496
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:178
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:71
virtual void BuildUtilities()
Definition: Visualizer.cpp:231
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:281
virtual void SetFullScreen(bool fullscreen)
Functions to change between fullscreen and windowed modes.
Definition: Visualizer.cpp:443
virtual bool ClearGeometries()
Definition: Visualizer.cpp:415
bool is_initialized_
Definition: Visualizer.h:268
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:278
GLuint vao_id_
Definition: Visualizer.h:269
GLuint render_fbo_
Definition: Visualizer.h:273
bool PollEvents()
Definition: Visualizer.cpp:283
void CaptureDepthImage(const std::string &filename="", bool do_render=true, double depth_scale=1000.0)
Definition: VisualizerRender.cpp:331
GLuint render_depth_stencil_rbo_
Definition: Visualizer.h:275
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:292
virtual void UpdateRender()
Function to inform render needed to be updated.
Definition: Visualizer.cpp:439
std::function< bool(Visualizer *)> animation_callback_func_in_loop_
Definition: Visualizer.h:262
Eigen::Vector2i saved_window_pos_
Definition: Visualizer.h:256
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:414
virtual bool InitOpenGL()
Function to initialize OpenGL.
Definition: VisualizerRender.cpp:29
virtual bool IsFullScreen()
Definition: Visualizer.cpp:471
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:213
void Run()
Function to activate the window.
Definition: Visualizer.cpp:249
virtual bool UpdateGeometry(std::shared_ptr< const geometry::Geometry > geometry_ptr=nullptr)
Function to update geometry.
Definition: Visualizer.cpp:425
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:304
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:296
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:175
std::unordered_map< std::shared_ptr< glsl::GeometryRenderer >, RenderOption > utility_renderer_opts_
Definition: Visualizer.h:299
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:267
virtual bool InitViewControl()
Function to initialize ViewControl.
Definition: Visualizer.cpp:214
bool WaitEvents()
Function to process the event queue and return if the window is closed.
Definition: Visualizer.cpp:270
virtual bool InitRenderOption()
Function to initialize RenderOption.
Definition: Visualizer.cpp:220
std::unordered_set< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:285
std::string window_name_
Definition: Visualizer.h:254
virtual void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
Definition: VisualizerCallback.cpp:68
const std::string & GetWindowName() const
Definition: Visualizer.h:216
void CopyViewStatusFromClipboard()
Definition: VisualizerRender.cpp:159
virtual ~Visualizer()
Definition: Visualizer.cpp:64
void DestroyVisualizerWindow()
Function to destroy a window.
Definition: Visualizer.cpp:203
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:295
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:391
virtual void MouseScrollCallback(GLFWwindow *window, double x, double y)
Definition: VisualizerCallback.cpp:63
GLuint render_rgb_tex_
Definition: Visualizer.h:274
virtual void UpdateWindowTitle()
Definition: Visualizer.cpp:225
void RegisterAnimationCallback(std::function< bool(Visualizer *)> callback_func)
Function to register a callback function for animation.
Definition: Visualizer.cpp:209
std::function< bool(Visualizer *)> animation_callback_func_
Definition: Visualizer.h:257
virtual void WindowRefreshCallback(GLFWwindow *window)
Definition: VisualizerCallback.cpp:13
Eigen::Vector2i saved_window_size_
Definition: Visualizer.h:255
virtual void ToggleFullScreen()
Definition: Visualizer.cpp:463
MouseControl mouse_control_
Definition: Visualizer.h:266
GLFWwindow * window_
Definition: Visualizer.h:253
virtual void PrintVisualizerHelp()
Definition: Visualizer.cpp:475
const ViewControl & GetViewControl() const
Definition: Visualizer.h:176
void Close()
Function to to notify the window to be closed.
Definition: Visualizer.cpp:265
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:302
std::shared_ptr< geometry::Image > CaptureScreenFloatBuffer(bool do_render=true)
Function to capture screen and store RGB in a float buffer.
Definition: VisualizerRender.cpp:176
virtual bool HasGeometry() const
Definition: Visualizer.cpp:441
bool is_redraw_required_
Definition: Visualizer.h:267
Visualizer()
Definition: Visualizer.cpp:62
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:289
Visualizer & operator=(const Visualizer &)=delete
void CopyViewStatusToClipboard()
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:53
bool is_control_key_down
Definition: Visualizer.h:49
bool is_mouse_left_button_down
Definition: Visualizer.h:47
double mouse_position_y
Definition: Visualizer.h:54
bool is_mouse_middle_button_down
Definition: Visualizer.h:48
bool is_super_key_down
Definition: Visualizer.h:52
bool is_shift_key_down
Definition: Visualizer.h:50
bool is_alt_key_down
Definition: Visualizer.h:51