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 #include <GL/glew.h>
30 #include <GLFW/glfw3.h>
31 #include <memory>
32 #include <string>
33 #include <unordered_set>
34 
41 
42 namespace open3d {
43 
44 namespace geometry {
45 class TriangleMesh;
46 class Image;
47 } // namespace geometry
48 
49 namespace visualization {
50 class Visualizer {
51 public:
52  struct MouseControl {
53  public:
54  bool is_mouse_left_button_down = false;
55  bool is_mouse_middle_button_down = false;
56  bool is_control_key_down = false;
57  bool is_shift_key_down = false;
58  bool is_alt_key_down = false;
59  bool is_super_key_down = false;
60  double mouse_position_x = 0.0;
61  double mouse_position_y = 0.0;
62  };
63 
64 public:
65  Visualizer();
66  virtual ~Visualizer();
67  Visualizer(Visualizer &&) = delete;
68  Visualizer(const Visualizer &) = delete;
69  Visualizer &operator=(const Visualizer &) = delete;
70 
71 public:
74  bool CreateVisualizerWindow(const std::string &window_name = "Open3D",
75  const int width = 640,
76  const int height = 480,
77  const int left = 50,
78  const int top = 50,
79  const bool visible = true);
80 
83  void DestroyVisualizerWindow();
84 
87  void RegisterAnimationCallback(
88  std::function<bool(Visualizer *)> callback_func);
89 
92  void Run();
93 
95  void Close();
96 
100  bool WaitEvents();
101 
106  bool PollEvents();
107 
117  virtual bool AddGeometry(
118  std::shared_ptr<const geometry::Geometry> geometry_ptr);
119 
126  virtual bool RemoveGeometry(
127  std::shared_ptr<const geometry::Geometry> geometry_ptr);
128 
132  virtual bool UpdateGeometry();
133  virtual bool HasGeometry() const;
134 
136  virtual void UpdateRender();
137 
138  virtual void PrintVisualizerHelp();
139  virtual void UpdateWindowTitle();
140  virtual void BuildUtilities();
141 
142  ViewControl &GetViewControl() { return *view_control_ptr_; }
143  RenderOption &GetRenderOption() { return *render_option_ptr_; }
144  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
145  bool do_render = true);
146  void CaptureScreenImage(const std::string &filename = "",
147  bool do_render = true);
148  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
149  bool do_render = true);
150  void CaptureDepthImage(const std::string &filename = "",
151  bool do_render = true,
152  double depth_scale = 1000.0);
153  void CaptureDepthPointCloud(const std::string &filename = "",
154  bool do_render = true,
155  bool convert_to_world_coordinate = false);
156  void CaptureRenderOption(const std::string &filename = "");
157  void ResetViewPoint(bool reset_bounding_box = false);
158 
159  const std::string &GetWindowName() const { return window_name_; }
160 
161 protected:
163  virtual bool InitOpenGL();
164 
166  virtual bool InitViewControl();
167 
169  virtual bool InitRenderOption();
170 
174  virtual void Render();
175 
176  void CopyViewStatusToClipboard();
177 
178  void CopyViewStatusFromClipboard();
179 
180  // callback functions
181  virtual void WindowRefreshCallback(GLFWwindow *window);
182  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
183  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
184  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
185  virtual void MouseButtonCallback(GLFWwindow *window,
186  int button,
187  int action,
188  int mods);
189  virtual void KeyPressCallback(
190  GLFWwindow *window, int key, int scancode, int action, int mods);
191  virtual void WindowCloseCallback(GLFWwindow *window);
192 
193 protected:
194  // window
195  GLFWwindow *window_ = NULL;
196  std::string window_name_ = "Open3D";
197  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
198  // Auxiliary internal backup of the callback function.
199  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
200  // so that even if user calls RegisterAnimationCallback() within the
201  // callback function it is still safe.
202  std::function<bool(Visualizer *)> animation_callback_func_in_loop_ =
203  nullptr;
204 
205  // control
207  bool is_redraw_required_ = true;
208  bool is_initialized_ = false;
209  GLuint vao_id_;
210 
211  // view control
212  std::unique_ptr<ViewControl> view_control_ptr_;
213 
214  // rendering properties
215  std::unique_ptr<RenderOption> render_option_ptr_;
216 
217  // geometry to be rendered
218  std::unordered_set<std::shared_ptr<const geometry::Geometry>>
220 
221  // geometry renderers
222  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
224 
225  // utilities owned by the Visualizer
226  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
227 
228  // utility renderers
229  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
230 
231  // coordinate frame
232  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
233  std::shared_ptr<glsl::CoordinateFrameRenderer>
235 
236 #ifdef __APPLE__
237  // MacBook with Retina display does not have a 1:1 mapping from screen
238  // coordinates to pixels. Thus we hack it back.
239  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
240  double pixel_to_screen_coordinate_ = 1.0;
241 #endif //__APPLE__
242 };
243 
244 } // namespace visualization
245 } // namespace open3d
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:226
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:229
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:223
RenderOption & GetRenderOption()
Definition: Visualizer.h:143
Definition: ViewControl.h:38
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:234
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:215
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:232
ViewControl & GetViewControl()
Definition: Visualizer.h:142
Definition: RenderOption.h:36
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:212
GLuint vao_id_
Definition: Visualizer.h:209
std::unordered_set< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:219
const std::string & GetWindowName() const
Definition: Visualizer.h:159
Definition: PinholeCameraIntrinsic.cpp:34
Definition: Visualizer.h:50
MouseControl mouse_control_
Definition: Visualizer.h:206
int height
Definition: FilePCD.cpp:68
int width
Definition: FilePCD.cpp:67