Open3D (C++ API)
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 class Visualizer {
57 public:
58  struct MouseControl {
59  public:
60  bool is_mouse_left_button_down = false;
61  bool is_mouse_middle_button_down = false;
62  bool is_control_key_down = false;
63  bool is_shift_key_down = false;
64  bool is_alt_key_down = false;
65  bool is_super_key_down = false;
66  double mouse_position_x = 0.0;
67  double mouse_position_y = 0.0;
68  };
69 
70 public:
71  Visualizer();
72  virtual ~Visualizer();
73  Visualizer(Visualizer &&) = delete;
74  Visualizer(const Visualizer &) = delete;
75  Visualizer &operator=(const Visualizer &) = delete;
76 
77 public:
80  bool CreateVisualizerWindow(const std::string &window_name = "Open3D",
81  const int width = 640,
82  const int height = 480,
83  const int left = 50,
84  const int top = 50,
85  const bool visible = true);
86 
89  void DestroyVisualizerWindow();
90 
93  void RegisterAnimationCallback(
94  std::function<bool(Visualizer *)> callback_func);
95 
98  void Run();
99 
101  void Close();
102 
106  bool WaitEvents();
107 
112  bool PollEvents();
113 
123  virtual bool AddGeometry(
124  std::shared_ptr<const geometry::Geometry> geometry_ptr,
125  bool reset_bounding_box = true);
126 
133  virtual bool RemoveGeometry(
134  std::shared_ptr<const geometry::Geometry> geometry_ptr,
135  bool reset_bounding_box = true);
136 
140  virtual bool ClearGeometries();
141 
147  virtual bool UpdateGeometry(
148  std::shared_ptr<const geometry::Geometry> geometry_ptr = nullptr);
149  virtual bool HasGeometry() const;
150 
152  virtual void UpdateRender();
153 
154  virtual void PrintVisualizerHelp();
155  virtual void UpdateWindowTitle();
156  virtual void BuildUtilities();
157 
158  ViewControl &GetViewControl() { return *view_control_ptr_; }
159  RenderOption &GetRenderOption() { return *render_option_ptr_; }
160  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
161  bool do_render = true);
162  void CaptureScreenImage(const std::string &filename = "",
163  bool do_render = true);
164  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
165  bool do_render = true);
166  void CaptureDepthImage(const std::string &filename = "",
167  bool do_render = true,
168  double depth_scale = 1000.0);
169  void CaptureDepthPointCloud(const std::string &filename = "",
170  bool do_render = true,
171  bool convert_to_world_coordinate = false);
172  void CaptureRenderOption(const std::string &filename = "");
173  void ResetViewPoint(bool reset_bounding_box = false);
174 
175  const std::string &GetWindowName() const { return window_name_; }
176 
177 protected:
179  virtual bool InitOpenGL();
180 
182  virtual bool InitViewControl();
183 
185  virtual bool InitRenderOption();
186 
190  virtual void Render();
191 
192  void CopyViewStatusToClipboard();
193 
194  void CopyViewStatusFromClipboard();
195 
196  // callback functions
197  virtual void WindowRefreshCallback(GLFWwindow *window);
198  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
199  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
200  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
201  virtual void MouseButtonCallback(GLFWwindow *window,
202  int button,
203  int action,
204  int mods);
205  virtual void KeyPressCallback(
206  GLFWwindow *window, int key, int scancode, int action, int mods);
207  virtual void WindowCloseCallback(GLFWwindow *window);
208 
209 protected:
210  // window
211  GLFWwindow *window_ = NULL;
212  std::string window_name_ = "Open3D";
213  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
214  // Auxiliary internal backup of the callback function.
215  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
216  // so that even if user calls RegisterAnimationCallback() within the
217  // callback function it is still safe.
218  std::function<bool(Visualizer *)> animation_callback_func_in_loop_ =
219  nullptr;
220 
221  // control
223  bool is_redraw_required_ = true;
224  bool is_initialized_ = false;
225  GLuint vao_id_;
226 
227  // view control
228  std::unique_ptr<ViewControl> view_control_ptr_;
229 
230  // rendering properties
231  std::unique_ptr<RenderOption> render_option_ptr_;
232 
233  // geometry to be rendered
234  std::unordered_set<std::shared_ptr<const geometry::Geometry>>
236 
237  // geometry renderers
238  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
240 
241  // utilities owned by the Visualizer
242  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
243 
244  // utility renderers
245  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
246  // map's key is the renderer for which the RenderOption applies
247  // (should be something in utility_renderer_ptrs_)
248  std::unordered_map<std::shared_ptr<glsl::GeometryRenderer>, RenderOption>
250 
251  // coordinate frame
252  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
253  std::shared_ptr<glsl::CoordinateFrameRenderer>
255 
256 #ifdef __APPLE__
257  // MacBook with Retina display does not have a 1:1 mapping from screen
258  // coordinates to pixels. Thus we hack it back.
259  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
260  double pixel_to_screen_coordinate_ = 1.0;
261 #endif //__APPLE__
262 };
263 
264 } // namespace visualization
265 } // namespace open3d
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:242
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:245
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:239
RenderOption & GetRenderOption()
Definition: Visualizer.h:159
Definition: ViewControl.h:38
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:254
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:231
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:252
ViewControl & GetViewControl()
Definition: Visualizer.h:158
Definition: RenderOption.h:36
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:228
GLuint vao_id_
Definition: Visualizer.h:225
std::unordered_set< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:235
const std::string & GetWindowName() const
Definition: Visualizer.h:175
Definition: PinholeCameraIntrinsic.cpp:34
Definition: Visualizer.h:56
MouseControl mouse_control_
Definition: Visualizer.h:222
int height
Definition: FilePCD.cpp:69
std::unordered_map< std::shared_ptr< glsl::GeometryRenderer >, RenderOption > utility_renderer_opts_
Definition: Visualizer.h:249
int width
Definition: FilePCD.cpp:68