ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Visualizer.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Avoid warning caused by redefinition of APIENTRY macro
11 // defined also in glfw3.h
12 #ifdef _WIN32
13 #include <windows.h>
14 #endif
15 
16 #include <GL/glew.h> // Make sure glew.h is included before gl.h
17 #include <GLFW/glfw3.h>
18 
19 #include <Eigen/Core>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <unordered_set>
24 
25 #include "visualization/shader/GeometryRenderer.h"
29 
30 class ccMesh;
31 class ccHObject;
32 namespace cloudViewer {
33 
34 namespace geometry {
35 class Image;
36 } // namespace geometry
37 
38 namespace visualization {
39 
40 class GLFWContext;
41 
45 class Visualizer {
46 public:
47  struct MouseControl {
48  public:
51  bool is_control_key_down = false;
52  bool is_shift_key_down = false;
53  bool is_alt_key_down = false;
54  bool is_super_key_down = false;
55  double mouse_position_x = 0.0;
56  double mouse_position_y = 0.0;
57  };
58 
59 public:
60  Visualizer();
61  virtual ~Visualizer();
62  Visualizer(Visualizer &&) = delete;
63  Visualizer(const Visualizer &) = delete;
64  Visualizer &operator=(const Visualizer &) = delete;
65 
66 public:
77  bool CreateVisualizerWindow(const std::string &window_name = "CloudViewer",
78  const int width = 640,
79  const int height = 480,
80  const int left = 50,
81  const int top = 50,
82  const bool visible = true);
83 
88 
95  std::function<bool(Visualizer *)> callback_func);
96 
100  void Run();
101 
103  void Close();
104 
110  bool WaitEvents();
111 
117  bool PollEvents();
118 
132  virtual bool AddGeometry(std::shared_ptr<const ccHObject> geometry_ptr,
133  bool reset_bounding_box = true);
134 
144  virtual bool RemoveGeometry(std::shared_ptr<const ccHObject> geometry_ptr,
145  bool reset_bounding_box = true);
146 
150  virtual bool ClearGeometries();
151 
158  virtual bool UpdateGeometry(
159  std::shared_ptr<const ccHObject> geometry_ptr = nullptr);
160  virtual bool HasGeometry() const;
161 
163  virtual void UpdateRender();
164 
166  virtual void SetFullScreen(bool fullscreen);
167  virtual void ToggleFullScreen();
168  virtual bool IsFullScreen();
169 
170  virtual void PrintVisualizerHelp();
171  virtual void UpdateWindowTitle();
172  virtual void BuildUtilities();
173 
181  std::shared_ptr<geometry::Image> CaptureScreenFloatBuffer(
182  bool do_render = true);
187  void CaptureScreenImage(const std::string &filename = "",
188  bool do_render = true);
192  std::shared_ptr<geometry::Image> CaptureDepthFloatBuffer(
193  bool do_render = true);
199  void CaptureDepthImage(const std::string &filename = "",
200  bool do_render = true,
201  double depth_scale = 1000.0);
208  void CaptureDepthPointCloud(const std::string &filename = "",
209  bool do_render = true,
210  bool convert_to_world_coordinate = false);
211  void CaptureRenderOption(const std::string &filename = "");
213  void ResetViewPoint(bool reset_bounding_box = false);
214 
215  const std::string &GetWindowName() const { return window_name_; }
216 
217 protected:
219  virtual bool InitOpenGL();
220 
222  virtual bool InitViewControl();
223 
225  virtual bool InitRenderOption();
226 
230  virtual void Render(bool render_screen = false);
231 
233 
235 
236  // callback functions
237  virtual void WindowRefreshCallback(GLFWwindow *window);
238  virtual void WindowResizeCallback(GLFWwindow *window, int w, int h);
239  virtual void MouseMoveCallback(GLFWwindow *window, double x, double y);
240  virtual void MouseScrollCallback(GLFWwindow *window, double x, double y);
241  virtual void MouseButtonCallback(GLFWwindow *window,
242  int button,
243  int action,
244  int mods);
245  virtual void KeyPressCallback(
246  GLFWwindow *window, int key, int scancode, int action, int mods);
248  virtual void WindowCloseCallback(GLFWwindow *window);
249 
250 protected:
251  // window
252  GLFWwindow *window_ = nullptr;
253  std::string window_name_ = "CloudViewer";
254 
256  std::shared_ptr<GLFWContext> glfw_context_ = nullptr;
257 
258  Eigen::Vector2i saved_window_size_ = Eigen::Vector2i::Zero();
259  Eigen::Vector2i saved_window_pos_ = Eigen::Vector2i::Zero();
260  std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
261  // Auxiliary internal backup of the callback function.
262  // It copies animation_callback_func_ in each PollEvent() or WaitEvent()
263  // so that even if user calls RegisterAnimationCallback() within the
264  // callback function it is still safe.
266  nullptr;
267 
268  // control
270  bool is_redraw_required_ = true;
271  bool is_initialized_ = false;
272  GLuint vao_id_ = 0;
273 
274  // render targets for "capture_screen_float_buffer" and
275  // "capture_screen_image" in offscreen render mode
276  GLuint render_fbo_ = 0;
277  GLuint render_rgb_tex_ = 0;
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 ccHObject>> geometry_ptrs_;
288 
289  // geometry renderers
290  std::unordered_set<std::shared_ptr<glsl::GeometryRenderer>>
292 
293  // utilities owned by the Visualizer
294  std::vector<std::shared_ptr<const ccHObject>> utility_ptrs_;
295 
296  // utility renderers
297  std::vector<std::shared_ptr<glsl::GeometryRenderer>> utility_renderer_ptrs_;
298  // map's key is the renderer for which the RenderOption applies
299  // (should be something in utility_renderer_ptrs_)
300  std::unordered_map<std::shared_ptr<glsl::GeometryRenderer>, RenderOption>
302 
303  // coordinate frame
304  std::shared_ptr<ccMesh> coordinate_frame_mesh_ptr_;
305  std::shared_ptr<glsl::CoordinateFrameRenderer>
307 
308 #ifdef __APPLE__
309  // MacBook with Retina display does not have a 1:1 mapping from screen
310  // coordinates to pixels. Thus we hack it back.
311  // http://www.glfw.org/faq.html#why-is-my-output-in-the-lower-left-corner-of-the-window
312  double pixel_to_screen_coordinate_ = 1.0;
313 #endif //__APPLE__
314 };
315 
316 } // namespace visualization
317 } // namespace cloudViewer
std::string filename
int width
int height
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
Triangular mesh.
Definition: ecvMesh.h:35
Defines rendering options for visualizer.
Definition: RenderOption.h:20
View controller for visualizer.
Definition: ViewControl.h:25
The main Visualizer class.
Definition: Visualizer.h:45
virtual bool UpdateGeometry(std::shared_ptr< const ccHObject > geometry_ptr=nullptr)
Function to update geometry.
Definition: Visualizer.cpp:466
virtual bool InitRenderOption()
Function to initialize RenderOption.
Definition: Visualizer.cpp:260
virtual void KeyPressCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
std::function< bool(Visualizer *)> animation_callback_func_
Definition: Visualizer.h:260
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
Definition: Visualizer.h:297
bool WaitEvents()
Function to process the event queue and return if the window is closed.
Definition: Visualizer.cpp:310
std::shared_ptr< geometry::Image > CaptureDepthFloatBuffer(bool do_render=true)
virtual bool InitViewControl()
Function to initialize ViewControl.
Definition: Visualizer.cpp:254
void CaptureDepthPointCloud(const std::string &filename="", bool do_render=true, bool convert_to_world_coordinate=false)
Function to capture and save local point cloud.
virtual void WindowRefreshCallback(GLFWwindow *window)
const std::string & GetWindowName() const
Definition: Visualizer.h:215
virtual bool InitOpenGL()
Function to initialize OpenGL.
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:177
std::shared_ptr< ccMesh > coordinate_frame_mesh_ptr_
Definition: Visualizer.h:304
void CaptureScreenImage(const std::string &filename="", bool do_render=true)
Function to capture and save a screen image.
virtual bool RemoveGeometry(std::shared_ptr< const ccHObject > geometry_ptr, bool reset_bounding_box=true)
Function to remove geometry from the scene.
Definition: Visualizer.cpp:433
virtual void UpdateRender()
Function to inform render needed to be updated.
Definition: Visualizer.cpp:479
virtual void MouseScrollCallback(GLFWwindow *window, double x, double y)
std::unordered_set< std::shared_ptr< const ccHObject > > geometry_ptrs_
Definition: Visualizer.h:287
std::unordered_map< std::shared_ptr< glsl::GeometryRenderer >, RenderOption > utility_renderer_opts_
Definition: Visualizer.h:301
virtual void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
std::unique_ptr< RenderOption > render_option_ptr_
Definition: Visualizer.h:284
virtual void MouseMoveCallback(GLFWwindow *window, double x, double y)
void ResetViewPoint(bool reset_bounding_box=false)
Function to reset view point.
virtual bool AddGeometry(std::shared_ptr< const ccHObject > geometry_ptr, bool reset_bounding_box=true)
Function to add geometry to the scene and create corresponding shaders.
Definition: Visualizer.cpp:336
Visualizer(const Visualizer &)=delete
void DestroyVisualizerWindow()
Function to destroy a window.
Definition: Visualizer.cpp:235
void Run()
Function to activate the window.
Definition: Visualizer.cpp:289
virtual void SetFullScreen(bool fullscreen)
Functions to change between fullscreen and windowed modes.
Definition: Visualizer.cpp:483
void Close()
Function to to notify the window to be closed.
Definition: Visualizer.cpp:305
std::function< bool(Visualizer *)> animation_callback_func_in_loop_
Definition: Visualizer.h:265
void RegisterAnimationCallback(std::function< bool(Visualizer *)> callback_func)
Function to register a callback function for animation.
Definition: Visualizer.cpp:249
bool CreateVisualizerWindow(const std::string &window_name="CloudViewer", const int width=640, const int height=480, const int left=50, const int top=50, const bool visible=true)
Function to create a window and initialize GLFW.
Definition: Visualizer.cpp:95
std::shared_ptr< GLFWContext > glfw_context_
Shared GLFW context.
Definition: Visualizer.h:256
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
Definition: Visualizer.h:291
std::shared_ptr< geometry::Image > CaptureScreenFloatBuffer(bool do_render=true)
Function to capture screen and store RGB in a float buffer.
virtual void Render(bool render_screen=false)
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:281
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
Definition: Visualizer.h:175
void CaptureRenderOption(const std::string &filename="")
virtual void WindowResizeCallback(GLFWwindow *window, int w, int h)
std::vector< std::shared_ptr< const ccHObject > > utility_ptrs_
Definition: Visualizer.h:294
Visualizer & operator=(const Visualizer &)=delete
void CaptureDepthImage(const std::string &filename="", bool do_render=true, double depth_scale=1000.0)
virtual void WindowCloseCallback(GLFWwindow *window)
Function to notify the window to be closed.
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
Definition: Visualizer.h:306
Generic file read and write utility for python interface.
Eigen::Matrix< Index, 2, 1 > Vector2i
Definition: knncpp.h:29