Open3D (C++ API)  0.12.0
Visualizer.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 // Avoid warning caused by redefinition of APIENTRY macro
30 // defined also in glfw3.h
31 #ifdef _WIN32
32 #include <windows.h>
33 #endif
34 
35 #include <GL/glew.h>
36 #include <GLFW/glfw3.h>
37 
38 #include <Eigen/Core>
39 #include <memory>
40 #include <string>
41 #include <unordered_map>
42 #include <unordered_set>
43 
49 
50 namespace open3d {
51 
52 namespace geometry {
53 class TriangleMesh;
54 class Image;
55 } // namespace geometry
56 
57 namespace visualization {
58 
62 class Visualizer {
63 public:
64  struct MouseControl {
65  public:
66  bool is_mouse_left_button_down = false;
67  bool is_mouse_middle_button_down = false;
68  bool is_control_key_down = false;
69  bool is_shift_key_down = false;
70  bool is_alt_key_down = false;
71  bool is_super_key_down = false;
72  double mouse_position_x = 0.0;
73  double mouse_position_y = 0.0;
74  };
75 
76 public:
77  Visualizer();
78  virtual ~Visualizer();
79  Visualizer(Visualizer &&) = delete;
80  Visualizer(const Visualizer &) = delete;
81  Visualizer &operator=(const Visualizer &) = delete;
82 
83 public:
94  bool CreateVisualizerWindow(const std::string &window_name = "Open3D",
95  const int width = 640,
96  const int height = 480,
97  const int left = 50,
98  const int top = 50,
99  const bool visible = true);
100 
104  void DestroyVisualizerWindow();
105 
111  void RegisterAnimationCallback(
112  std::function<bool(Visualizer *)> callback_func);
113 
117  void Run();
118 
120  void Close();
121 
127  bool WaitEvents();
128 
134  bool PollEvents();
135 
149  virtual bool AddGeometry(
150  std::shared_ptr<const geometry::Geometry> geometry_ptr,
151  bool reset_bounding_box = true);
152 
162  virtual bool RemoveGeometry(
163  std::shared_ptr<const geometry::Geometry> geometry_ptr,
164  bool reset_bounding_box = true);
165 
169  virtual bool ClearGeometries();
170 
177  virtual bool UpdateGeometry(
178  std::shared_ptr<const geometry::Geometry> geometry_ptr = nullptr);
179  virtual bool HasGeometry() const;
180 
182  virtual void UpdateRender();
183 
185  virtual void SetFullScreen(bool fullscreen);
186  virtual void ToggleFullScreen();
187  virtual bool IsFullScreen();
188 
189  virtual void PrintVisualizerHelp();
190  virtual void UpdateWindowTitle();
191  virtual void BuildUtilities();
192 
194  ViewControl &GetViewControl() { return *view_control_ptr_; }
196  RenderOption &GetRenderOption() { return *render_option_ptr_; }
200  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
201  bool do_render = true);
206  void CaptureScreenImage(const std::string &filename = "",
207  bool do_render = true);
211  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
212  bool do_render = true);
218  void CaptureDepthImage(const std::string &filename = "",
219  bool do_render = true,
220  double depth_scale = 1000.0);
227  void CaptureDepthPointCloud(const std::string &filename = "",
228  bool do_render = true,
229  bool convert_to_world_coordinate = false);
230  void CaptureRenderOption(const std::string &filename = "");
232  void ResetViewPoint(bool reset_bounding_box = false);
233 
234  const std::string &GetWindowName() const { return window_name_; }
235 
236 protected:
238  virtual bool InitOpenGL();
239 
241  virtual bool InitViewControl();
242 
244  virtual bool InitRenderOption();
245 
249  virtual void Render(bool render_screen = false);
250 
251  void CopyViewStatusToClipboard();
252 
253  void CopyViewStatusFromClipboard();
254 
255  // callback functions
256  virtual void WindowRefreshCallback(GLFWwindow *window);
257  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
258  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
259  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
260  virtual void MouseButtonCallback(GLFWwindow *window,
261  int button,
262  int action,
263  int mods);
264  virtual void KeyPressCallback(
265  GLFWwindow *window, int key, int scancode, int action, int mods);
267  virtual void WindowCloseCallback(GLFWwindow *window);
268 
269 protected:
270  // window
271  GLFWwindow *window_ = NULL;
272  std::string window_name_ = "Open3D";
273  Eigen::Vector2i saved_window_size_ = Eigen::Vector2i::Zero();
274  Eigen::Vector2i saved_window_pos_ = Eigen::Vector2i::Zero();
275  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
276  // Auxiliary internal backup of the callback function.
277  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
278  // so that even if user calls RegisterAnimationCallback() within the
279  // callback function it is still safe.
280  std::function<bool(Visualizer *)> animation_callback_func_in_loop_ =
281  nullptr;
282 
283  // control
285  bool is_redraw_required_ = true;
286  bool is_initialized_ = false;
287  GLuint vao_id_;
288 
289  // render targets for "capture_screen_float_buffer" and
290  // "capture_screen_image" in offscreen render mode
291  unsigned int render_fbo_;
292  unsigned int render_rgb_tex_;
294 
295  // view control
296  std::unique_ptr<ViewControl> view_control_ptr_;
297 
298  // rendering properties
299  std::unique_ptr<RenderOption> render_option_ptr_;
300 
301  // geometry to be rendered
302  std::unordered_set<std::shared_ptr<const geometry::Geometry>>
304 
305  // geometry renderers
306  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
308 
309  // utilities owned by the Visualizer
310  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
311 
312  // utility renderers
313  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
314  // map's key is the renderer for which the RenderOption applies
315  // (should be something in utility_renderer_ptrs_)
316  std::unordered_map<std::shared_ptr<glsl::GeometryRenderer>, RenderOption>
318 
319  // coordinate frame
320  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
321  std::shared_ptr<glsl::CoordinateFrameRenderer>
323 
324 #ifdef __APPLE__
325  // MacBook with Retina display does not have a 1:1 mapping from screen
326  // coordinates to pixels. Thus we hack it back.
327  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
328  double pixel_to_screen_coordinate_ = 1.0;
329 #endif //__APPLE__
330 };
331 
332 } // namespace visualization
333 } // namespace open3d
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:310
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:313
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:307
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:196
View controller for visualizer.
Definition: ViewControl.h:41
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:322
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:299
unsigned int render_rgb_tex_
Definition: Visualizer.h:292
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:320
unsigned int render_fbo_
Definition: Visualizer.h:291
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
Definition: Visualizer.h:194
Defines rendering options for visualizer.
Definition: RenderOption.h:39
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:296
GLuint vao_id_
Definition: Visualizer.h:287
std::unordered_set< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:303
const std::string & GetWindowName() const
Definition: Visualizer.h:234
Definition: PinholeCameraIntrinsic.cpp:35
The main Visualizer class.
Definition: Visualizer.h:62
MouseControl mouse_control_
Definition: Visualizer.h:284
int height
Definition: FilePCD.cpp:72
unsigned int render_depth_stencil_rbo_
Definition: Visualizer.h:293
std::unordered_map< std::shared_ptr< glsl::GeometryRenderer >, RenderOption > utility_renderer_opts_
Definition: Visualizer.h:317
int width
Definition: FilePCD.cpp:71