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