Open3D (C++ API)
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 #include <memory>
38 #include <string>
39 #include <unordered_map>
40 #include <unordered_set>
41 
47 
48 namespace open3d {
49 
50 namespace geometry {
51 class TriangleMesh;
52 class Image;
53 } // namespace geometry
54 
55 namespace visualization {
56 
60 class Visualizer {
61 public:
62  struct MouseControl {
63  public:
64  bool is_mouse_left_button_down = false;
65  bool is_mouse_middle_button_down = false;
66  bool is_control_key_down = false;
67  bool is_shift_key_down = false;
68  bool is_alt_key_down = false;
69  bool is_super_key_down = false;
70  double mouse_position_x = 0.0;
71  double mouse_position_y = 0.0;
72  };
73 
74 public:
75  Visualizer();
76  virtual ~Visualizer();
77  Visualizer(Visualizer &&) = delete;
78  Visualizer(const Visualizer &) = delete;
79  Visualizer &operator=(const Visualizer &) = delete;
80 
81 public:
92  bool CreateVisualizerWindow(const std::string &window_name = "Open3D",
93  const int width = 640,
94  const int height = 480,
95  const int left = 50,
96  const int top = 50,
97  const bool visible = true);
98 
102  void DestroyVisualizerWindow();
103 
109  void RegisterAnimationCallback(
110  std::function<bool(Visualizer *)> callback_func);
111 
115  void Run();
116 
118  void Close();
119 
125  bool WaitEvents();
126 
132  bool PollEvents();
133 
147  virtual bool AddGeometry(
148  std::shared_ptr<const geometry::Geometry> geometry_ptr,
149  bool reset_bounding_box = true);
150 
160  virtual bool RemoveGeometry(
161  std::shared_ptr<const geometry::Geometry> geometry_ptr,
162  bool reset_bounding_box = true);
163 
167  virtual bool ClearGeometries();
168 
175  virtual bool UpdateGeometry(
176  std::shared_ptr<const geometry::Geometry> geometry_ptr = nullptr);
177  virtual bool HasGeometry() const;
178 
180  virtual void UpdateRender();
181 
182  virtual void PrintVisualizerHelp();
183  virtual void UpdateWindowTitle();
184  virtual void BuildUtilities();
185 
187  ViewControl &GetViewControl() { return *view_control_ptr_; }
189  RenderOption &GetRenderOption() { return *render_option_ptr_; }
193  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
194  bool do_render = true);
199  void CaptureScreenImage(const std::string &filename = "",
200  bool do_render = true);
204  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
205  bool do_render = true);
211  void CaptureDepthImage(const std::string &filename = "",
212  bool do_render = true,
213  double depth_scale = 1000.0);
220  void CaptureDepthPointCloud(const std::string &filename = "",
221  bool do_render = true,
222  bool convert_to_world_coordinate = false);
223  void CaptureRenderOption(const std::string &filename = "");
225  void ResetViewPoint(bool reset_bounding_box = false);
226 
227  const std::string &GetWindowName() const { return window_name_; }
228 
229 protected:
231  virtual bool InitOpenGL();
232 
234  virtual bool InitViewControl();
235 
237  virtual bool InitRenderOption();
238 
242  virtual void Render();
243 
244  void CopyViewStatusToClipboard();
245 
246  void CopyViewStatusFromClipboard();
247 
248  // callback functions
249  virtual void WindowRefreshCallback(GLFWwindow *window);
250  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
251  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
252  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
253  virtual void MouseButtonCallback(GLFWwindow *window,
254  int button,
255  int action,
256  int mods);
257  virtual void KeyPressCallback(
258  GLFWwindow *window, int key, int scancode, int action, int mods);
260  virtual void WindowCloseCallback(GLFWwindow *window);
261 
262 protected:
263  // window
264  GLFWwindow *window_ = NULL;
265  std::string window_name_ = "Open3D";
266  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
267  // Auxiliary internal backup of the callback function.
268  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
269  // so that even if user calls RegisterAnimationCallback() within the
270  // callback function it is still safe.
271  std::function<bool(Visualizer *)> animation_callback_func_in_loop_ =
272  nullptr;
273 
274  // control
276  bool is_redraw_required_ = true;
277  bool is_initialized_ = false;
278  GLuint vao_id_;
279 
280  // view control
281  std::unique_ptr<ViewControl> view_control_ptr_;
282 
283  // rendering properties
284  std::unique_ptr<RenderOption> render_option_ptr_;
285 
286  // geometry to be rendered
287  std::unordered_set<std::shared_ptr<const geometry::Geometry>>
289 
290  // geometry renderers
291  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
293 
294  // utilities owned by the Visualizer
295  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
296 
297  // utility renderers
298  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
299  // map's key is the renderer for which the RenderOption applies
300  // (should be something in utility_renderer_ptrs_)
301  std::unordered_map<std::shared_ptr<glsl::GeometryRenderer>, RenderOption>
303 
304  // coordinate frame
305  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
306  std::shared_ptr<glsl::CoordinateFrameRenderer>
308 
309 #ifdef __APPLE__
310  // MacBook with Retina display does not have a 1:1 mapping from screen
311  // coordinates to pixels. Thus we hack it back.
312  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
313  double pixel_to_screen_coordinate_ = 1.0;
314 #endif //__APPLE__
315 };
316 
317 } // namespace visualization
318 } // namespace open3d
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:295
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:298
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:292
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:189
View controller for visualizer.
Definition: ViewControl.h:41
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:307
int Run(int argc, const char *argv[])
Definition: Open3DViewer.cpp:77
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:284
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:305
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
Definition: Visualizer.h:187
Defines rendering options for visualizer.
Definition: RenderOption.h:39
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:281
GLuint vao_id_
Definition: Visualizer.h:278
std::unordered_set< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:288
const std::string & GetWindowName() const
Definition: Visualizer.h:227
Definition: Open3DViewer.h:29
The main Visualizer class.
Definition: Visualizer.h:60
MouseControl mouse_control_
Definition: Visualizer.h:275
int height
Definition: FilePCD.cpp:70
std::unordered_map< std::shared_ptr< glsl::GeometryRenderer >, RenderOption > utility_renderer_opts_
Definition: Visualizer.h:302
int width
Definition: FilePCD.cpp:69