Open3D (C++ API)  0.11.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 <memory>
39 #include <string>
40 #include <unordered_map>
41 #include <unordered_set>
42 
48 
49 namespace open3d {
50 
51 namespace geometry {
52 class TriangleMesh;
53 class Image;
54 } // namespace geometry
55 
56 namespace visualization {
57 
61 class Visualizer {
62 public:
63  struct MouseControl {
64  public:
65  bool is_mouse_left_button_down = false;
66  bool is_mouse_middle_button_down = false;
67  bool is_control_key_down = false;
68  bool is_shift_key_down = false;
69  bool is_alt_key_down = false;
70  bool is_super_key_down = false;
71  double mouse_position_x = 0.0;
72  double mouse_position_y = 0.0;
73  };
74 
75 public:
76  Visualizer();
77  virtual ~Visualizer();
78  Visualizer(Visualizer &&) = delete;
79  Visualizer(const Visualizer &) = delete;
80  Visualizer &operator=(const Visualizer &) = delete;
81 
82 public:
93  bool CreateVisualizerWindow(const std::string &window_name = "Open3D",
94  const int width = 640,
95  const int height = 480,
96  const int left = 50,
97  const int top = 50,
98  const bool visible = true);
99 
103  void DestroyVisualizerWindow();
104 
110  void RegisterAnimationCallback(
111  std::function<bool(Visualizer *)> callback_func);
112 
116  void Run();
117 
119  void Close();
120 
126  bool WaitEvents();
127 
133  bool PollEvents();
134 
148  virtual bool AddGeometry(
149  std::shared_ptr<const geometry::Geometry> geometry_ptr,
150  bool reset_bounding_box = true);
151 
161  virtual bool RemoveGeometry(
162  std::shared_ptr<const geometry::Geometry> geometry_ptr,
163  bool reset_bounding_box = true);
164 
168  virtual bool ClearGeometries();
169 
176  virtual bool UpdateGeometry(
177  std::shared_ptr<const geometry::Geometry> geometry_ptr = nullptr);
178  virtual bool HasGeometry() const;
179 
181  virtual void UpdateRender();
182 
183  virtual void PrintVisualizerHelp();
184  virtual void UpdateWindowTitle();
185  virtual void BuildUtilities();
186 
188  ViewControl &GetViewControl() { return *view_control_ptr_; }
190  RenderOption &GetRenderOption() { return *render_option_ptr_; }
194  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
195  bool do_render = true);
200  void CaptureScreenImage(const std::string &filename = "",
201  bool do_render = true);
205  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
206  bool do_render = true);
212  void CaptureDepthImage(const std::string &filename = "",
213  bool do_render = true,
214  double depth_scale = 1000.0);
221  void CaptureDepthPointCloud(const std::string &filename = "",
222  bool do_render = true,
223  bool convert_to_world_coordinate = false);
224  void CaptureRenderOption(const std::string &filename = "");
226  void ResetViewPoint(bool reset_bounding_box = false);
227 
228  const std::string &GetWindowName() const { return window_name_; }
229 
230 protected:
232  virtual bool InitOpenGL();
233 
235  virtual bool InitViewControl();
236 
238  virtual bool InitRenderOption();
239 
243  virtual void Render(bool render_screen = false);
244 
245  void CopyViewStatusToClipboard();
246 
247  void CopyViewStatusFromClipboard();
248 
249  // callback functions
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  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
268  // Auxiliary internal backup of the callback function.
269  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
270  // so that even if user calls RegisterAnimationCallback() within the
271  // callback function it is still safe.
272  std::function<bool(Visualizer *)> animation_callback_func_in_loop_ =
273  nullptr;
274 
275  // control
277  bool is_redraw_required_ = true;
278  bool is_initialized_ = false;
279  GLuint vao_id_;
280 
281  // render targets for "capture_screen_float_buffer" and
282  // "capture_screen_image" in offscreen render mode
283  unsigned int render_fbo_;
284  unsigned int render_rgb_tex_;
286 
287  // view control
288  std::unique_ptr<ViewControl> view_control_ptr_;
289 
290  // rendering properties
291  std::unique_ptr<RenderOption> render_option_ptr_;
292 
293  // geometry to be rendered
294  std::unordered_set<std::shared_ptr<const geometry::Geometry>>
296 
297  // geometry renderers
298  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
300 
301  // utilities owned by the Visualizer
302  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
303 
304  // utility renderers
305  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
306  // map's key is the renderer for which the RenderOption applies
307  // (should be something in utility_renderer_ptrs_)
308  std::unordered_map<std::shared_ptr<glsl::GeometryRenderer>, RenderOption>
310 
311  // coordinate frame
312  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
313  std::shared_ptr<glsl::CoordinateFrameRenderer>
315 
316 #ifdef __APPLE__
317  // MacBook with Retina display does not have a 1:1 mapping from screen
318  // coordinates to pixels. Thus we hack it back.
319  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
320  double pixel_to_screen_coordinate_ = 1.0;
321 #endif //__APPLE__
322 };
323 
324 } // namespace visualization
325 } // namespace open3d
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:302
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:305
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:299
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:190
View controller for visualizer.
Definition: ViewControl.h:41
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:314
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:291
unsigned int render_rgb_tex_
Definition: Visualizer.h:284
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:312
unsigned int render_fbo_
Definition: Visualizer.h:283
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
Definition: Visualizer.h:188
Defines rendering options for visualizer.
Definition: RenderOption.h:39
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:288
GLuint vao_id_
Definition: Visualizer.h:279
std::unordered_set< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:295
const std::string & GetWindowName() const
Definition: Visualizer.h:228
Definition: PinholeCameraIntrinsic.cpp:35
The main Visualizer class.
Definition: Visualizer.h:61
MouseControl mouse_control_
Definition: Visualizer.h:276
int height
Definition: FilePCD.cpp:72
unsigned int render_depth_stencil_rbo_
Definition: Visualizer.h:285
std::unordered_map< std::shared_ptr< glsl::GeometryRenderer >, RenderOption > utility_renderer_opts_
Definition: Visualizer.h:309
int width
Definition: FilePCD.cpp:71