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 <string>
30 #include <memory>
31 
32 #include <GL/glew.h>
33 #include <GLFW/glfw3.h>
35 
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 
123  virtual bool UpdateGeometry();
124  virtual bool HasGeometry() const;
125 
127  virtual void UpdateRender();
128 
129  virtual void PrintVisualizerHelp();
130  virtual void UpdateWindowTitle();
131  virtual void BuildUtilities();
132 
133  ViewControl &GetViewControl() { return *view_control_ptr_; }
134  RenderOption &GetRenderOption() { return *render_option_ptr_; }
135  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
136  bool do_render = true);
137  void CaptureScreenImage(const std::string &filename = "",
138  bool do_render = true);
139  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
140  bool do_render = true);
141  void CaptureDepthImage(const std::string &filename = "",
142  bool do_render = true,
143  double depth_scale = 1000.0);
144  void CaptureDepthPointCloud(const std::string &filename = "",
145  bool do_render = true,
146  bool convert_to_world_coordinate = false);
147  void CaptureRenderOption(const std::string &filename = "");
148  void ResetViewPoint(bool reset_bounding_box = false);
149 
150  const std::string &GetWindowName() const { return window_name_; }
151 
152 protected:
154  virtual bool InitOpenGL();
155 
157  virtual bool InitViewControl();
158 
160  virtual bool InitRenderOption();
161 
165  virtual void Render();
166 
167  void CopyViewStatusToClipboard();
168 
169  void CopyViewStatusFromClipboard();
170 
171  // callback functions
172  virtual void WindowRefreshCallback(GLFWwindow *window);
173  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
174  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
175  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
176  virtual void MouseButtonCallback(GLFWwindow *window,
177  int button,
178  int action,
179  int mods);
180  virtual void KeyPressCallback(
181  GLFWwindow *window, int key, int scancode, int action, int mods);
182  virtual void WindowCloseCallback(GLFWwindow *window);
183 
184 protected:
185  // window
186  GLFWwindow *window_ = NULL;
187  std::string window_name_ = "Open3D";
188  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
189  // Auxiliary internal backup of the callback function.
190  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
191  // so that even if user calls RegisterAnimationCallback() within the
192  // callback function it is still safe.
193  std::function<bool(Visualizer *)> animation_callback_func_in_loop_ =
194  nullptr;
195 
196  // control
198  bool is_redraw_required_ = true;
199  bool is_initialized_ = false;
200  GLuint vao_id_;
201 
202  // view control
203  std::unique_ptr<ViewControl> view_control_ptr_;
204 
205  // rendering properties
206  std::unique_ptr<RenderOption> render_option_ptr_;
207 
208  // geometry to be rendered
209  std::vector<std::shared_ptr<const geometry::Geometry>> geometry_ptrs_;
210 
211  // geometry renderers
212  std::vector<std::shared_ptr<glsl::GeometryRenderer>>
214 
215  // utilities owned by the Visualizer
216  std::vector<std::shared_ptr<const geometry::Geometry>> utility_ptrs_;
217 
218  // utility renderers
219  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
220 
221  // coordinate frame
222  std::shared_ptr<geometry::TriangleMesh> coordinate_frame_mesh_ptr_;
223  std::shared_ptr<glsl::CoordinateFrameRenderer>
225 
226 #ifdef __APPLE__
227  // MacBook with Retina display does not have a 1:1 mapping from screen
228  // coordinates to pixels. Thus we hack it back.
229  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
230  double pixel_to_screen_coordinate_ = 1.0;
231 #endif //__APPLE__
232 };
233 
234 } // namespace visualization
235 } // namespace open3d
std::vector< std::shared_ptr< const geometry::Geometry > > utility_ptrs_
Definition: Visualizer.h:216
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:219
std::vector< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:213
RenderOption & GetRenderOption()
Definition: Visualizer.h:134
Definition: ViewControl.h:38
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:224
std::vector< std::shared_ptr< const geometry::Geometry > > geometry_ptrs_
Definition: Visualizer.h:209
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:206
std::shared_ptr< geometry::TriangleMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:222
ViewControl & GetViewControl()
Definition: Visualizer.h:133
Definition: RenderOption.h:36
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:203
GLuint vao_id_
Definition: Visualizer.h:200
const std::string & GetWindowName() const
Definition: Visualizer.h:150
Definition: PinholeCameraIntrinsic.cpp:33
Definition: Visualizer.h:50
MouseControl mouse_control_
Definition: Visualizer.h:197
int height
Definition: FilePCD.cpp:68
int width
Definition: FilePCD.cpp:67