ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
VisualizerCallback.cpp
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 #include <Logging.h>
9 
10 #include "Visualizer.h"
11 
12 namespace cloudViewer {
13 namespace visualization {
14 
15 using namespace cloudViewer;
16 
17 void Visualizer::WindowRefreshCallback(GLFWwindow *window) {
18  if (is_redraw_required_) {
19  Render();
20  is_redraw_required_ = false;
21  }
22 }
23 
24 void Visualizer::WindowResizeCallback(GLFWwindow *window, int w, int h) {
25  view_control_ptr_->ChangeWindowSize(w, h);
26  is_redraw_required_ = true;
27 }
28 
29 void Visualizer::MouseMoveCallback(GLFWwindow *window, double x, double y) {
30 #ifdef __APPLE__
31  x /= pixel_to_screen_coordinate_;
32  y /= pixel_to_screen_coordinate_;
33 #endif
40  } else if (mouse_control_.is_shift_key_down) {
42  } else if (mouse_control_.is_alt_key_down) {
43  view_control_ptr_->CameraLocalRotate(
48  } else {
53  }
54  is_redraw_required_ = true;
55  }
61  is_redraw_required_ = true;
62  }
65 }
66 
67 void Visualizer::MouseScrollCallback(GLFWwindow *window, double x, double y) {
68  view_control_ptr_->Scale(y);
69  is_redraw_required_ = true;
70 }
71 
72 void Visualizer::MouseButtonCallback(GLFWwindow *window,
73  int button,
74  int action,
75  int mods) {
76  double x, y;
77  glfwGetCursorPos(window, &x, &y);
78 #ifdef __APPLE__
79  x /= pixel_to_screen_coordinate_;
80  y /= pixel_to_screen_coordinate_;
81 #endif
84  if (action == GLFW_PRESS) {
85  if (button == GLFW_MOUSE_BUTTON_LEFT) {
86  view_control_ptr_->ResetCameraLocalRotate();
87 
89  mouse_control_.is_control_key_down = (mods & GLFW_MOD_CONTROL) != 0;
90  mouse_control_.is_shift_key_down = (mods & GLFW_MOD_SHIFT) != 0;
91  mouse_control_.is_alt_key_down = (mods & GLFW_MOD_ALT) != 0;
92  mouse_control_.is_super_key_down = (mods & GLFW_MOD_SUPER) != 0;
93  } else if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
95  }
96  } else {
103  }
104 }
105 
107  GLFWwindow *window, int key, int scancode, int action, int mods) {
108  if (action == GLFW_RELEASE) {
109  return;
110  }
111 
112  switch (key) {
113  case GLFW_KEY_ENTER:
114  if (mods & GLFW_MOD_ALT) {
115  if (IsFullScreen()) {
116  SetFullScreen(false);
117  } else {
118  SetFullScreen(true);
119  };
120  }
121  break;
122  case GLFW_KEY_LEFT_BRACKET:
123  view_control_ptr_->ChangeFieldOfView(-1.0);
124  utility::LogDebug("[Visualizer] Field of view set to {:.2f}.",
125  view_control_ptr_->GetFieldOfView());
126  break;
127  case GLFW_KEY_RIGHT_BRACKET:
128  view_control_ptr_->ChangeFieldOfView(1.0);
129  utility::LogDebug("[Visualizer] Field of view set to {:.2f}.",
130  view_control_ptr_->GetFieldOfView());
131  break;
132  case GLFW_KEY_R:
133  ResetViewPoint();
134  utility::LogDebug("[Visualizer] reset view point.");
135  break;
136  case GLFW_KEY_C:
137  if (mods & GLFW_MOD_CONTROL || mods & GLFW_MOD_SUPER) {
139  }
140  break;
141  case GLFW_KEY_V:
142  if (mods & GLFW_MOD_CONTROL || mods & GLFW_MOD_SUPER) {
144  }
145  break;
146  case GLFW_KEY_ESCAPE:
147  case GLFW_KEY_Q:
148  Close();
149  break;
150  case GLFW_KEY_H:
152  break;
153  case GLFW_KEY_P:
154  case GLFW_KEY_PRINT_SCREEN:
156  break;
157  case GLFW_KEY_D:
159  break;
160  case GLFW_KEY_O:
162  break;
163  case GLFW_KEY_L:
164  render_option_ptr_->ToggleLightOn();
165  utility::LogDebug("[Visualizer] Lighting {}.",
166  render_option_ptr_->light_on_ ? "ON" : "OFF");
167  break;
168  case GLFW_KEY_EQUAL:
169  if (mods & GLFW_MOD_SHIFT) {
170  render_option_ptr_->ChangeLineWidth(1.0);
171  utility::LogDebug("[Visualizer] Line width set to {:.2f}.",
172  render_option_ptr_->line_width_);
173  } else {
174  render_option_ptr_->ChangePointSize(1.0);
175  if (render_option_ptr_->point_show_normal_) {
176  UpdateGeometry();
177  }
178  utility::LogDebug("[Visualizer] Point size set to {:.2f}.",
179  render_option_ptr_->point_size_);
180  }
181  break;
182  case GLFW_KEY_MINUS:
183  if (mods & GLFW_MOD_SHIFT) {
184  render_option_ptr_->ChangeLineWidth(-1.0);
185  utility::LogDebug("[Visualizer] Line width set to {:.2f}.",
186  render_option_ptr_->line_width_);
187  } else {
188  render_option_ptr_->ChangePointSize(-1.0);
189  if (render_option_ptr_->point_show_normal_) {
190  UpdateGeometry();
191  }
192  utility::LogDebug("[Visualizer] Point size set to {:.2f}.",
193  render_option_ptr_->point_size_);
194  }
195  break;
196  case GLFW_KEY_N:
197  render_option_ptr_->TogglePointShowNormal();
198  if (render_option_ptr_->point_show_normal_) {
199  UpdateGeometry();
200  }
202  "[Visualizer] Point normal rendering {}.",
203  render_option_ptr_->point_show_normal_ ? "ON" : "OFF");
204  break;
205  case GLFW_KEY_S:
206  render_option_ptr_->ToggleShadingOption();
207  UpdateGeometry();
209  "[Visualizer] Mesh shading mode is {}.",
210  render_option_ptr_->mesh_shade_option_ ==
212  ? "FLAT"
213  : "SMOOTH");
214  break;
215  case GLFW_KEY_W:
216  render_option_ptr_->ToggleMeshShowWireframe();
218  "[Visualizer] Mesh wireframe rendering {}.",
219  render_option_ptr_->mesh_show_wireframe_ ? "ON" : "OFF");
220  break;
221  case GLFW_KEY_B:
222  render_option_ptr_->ToggleMeshShowBackFace();
224  "[Visualizer] Mesh back face rendering {}.",
225  render_option_ptr_->mesh_show_back_face_ ? "ON" : "OFF");
226  break;
227  case GLFW_KEY_I:
228  render_option_ptr_->ToggleInterpolationOption();
229  UpdateGeometry();
231  "[Visualizer] geometry::Image interpolation mode is {}.",
232  render_option_ptr_->interpolation_option_ ==
234  Nearest
235  ? "NEARST"
236  : "LINEAR");
237  break;
238  case GLFW_KEY_T:
239  render_option_ptr_->ToggleImageStretchOption();
241  "[Visualizer] geometry::Image stretch mode is #{}.",
242  int(render_option_ptr_->image_stretch_option_));
243  break;
244  case GLFW_KEY_0:
245  if (mods & GLFW_MOD_CONTROL) {
246  render_option_ptr_->mesh_color_option_ =
248  UpdateGeometry();
249  utility::LogDebug("[Visualizer] Mesh color set to DEFAULT.");
250  } else if (mods & GLFW_MOD_SHIFT) {
252  UpdateGeometry();
253  utility::LogDebug("[Visualizer] Color map set to GRAY.");
254  } else {
255  render_option_ptr_->point_color_option_ =
257  UpdateGeometry();
258  utility::LogDebug("[Visualizer] Point color set to DEFAULT.");
259  }
260  break;
261  case GLFW_KEY_1:
262  if (mods & GLFW_MOD_CONTROL) {
263  render_option_ptr_->mesh_color_option_ =
265  UpdateGeometry();
266  utility::LogDebug("[Visualizer] Mesh color set to COLOR.");
267  } else if (mods & GLFW_MOD_SHIFT) {
269  UpdateGeometry();
270  utility::LogDebug("[Visualizer] Color map set to JET.");
271  } else {
272  render_option_ptr_->point_color_option_ =
274  UpdateGeometry();
275  utility::LogDebug("[Visualizer] Point color set to COLOR.");
276  }
277  break;
278  case GLFW_KEY_2:
279  if (mods & GLFW_MOD_CONTROL) {
280  render_option_ptr_->mesh_color_option_ =
282  UpdateGeometry();
283  utility::LogDebug("[Visualizer] Mesh color set to X.");
284  } else if (mods & GLFW_MOD_SHIFT) {
286  UpdateGeometry();
287  utility::LogDebug("[Visualizer] Color map set to SUMMER.");
288  } else {
289  render_option_ptr_->point_color_option_ =
291  UpdateGeometry();
292  utility::LogDebug("[Visualizer] Point color set to X.");
293  }
294  break;
295  case GLFW_KEY_3:
296  if (mods & GLFW_MOD_CONTROL) {
297  render_option_ptr_->mesh_color_option_ =
299  UpdateGeometry();
300  utility::LogDebug("[Visualizer] Mesh color set to Y.");
301  } else if (mods & GLFW_MOD_SHIFT) {
303  UpdateGeometry();
304  utility::LogDebug("[Visualizer] Color map set to WINTER.");
305  } else {
306  render_option_ptr_->point_color_option_ =
308  UpdateGeometry();
309  utility::LogDebug("[Visualizer] Point color set to Y.");
310  }
311  break;
312  case GLFW_KEY_4:
313  if (mods & GLFW_MOD_CONTROL) {
314  render_option_ptr_->mesh_color_option_ =
316  UpdateGeometry();
317  utility::LogDebug("[Visualizer] Mesh color set to Z.");
318  } else if (mods & GLFW_MOD_SHIFT) {
320  UpdateGeometry();
321  utility::LogDebug("[Visualizer] Color map set to HOT.");
322  } else {
323  render_option_ptr_->point_color_option_ =
325  UpdateGeometry();
326  utility::LogDebug("[Visualizer] Point color set to Z.");
327  }
328  break;
329  case GLFW_KEY_9:
330  if (mods & GLFW_MOD_CONTROL) {
331  render_option_ptr_->mesh_color_option_ =
333  UpdateGeometry();
334  utility::LogDebug("[Visualizer] Mesh color set to NORMAL.");
335  } else if (mods & GLFW_MOD_SHIFT) {
336  } else {
337  render_option_ptr_->point_color_option_ =
339  UpdateGeometry();
340  utility::LogDebug("[Visualizer] Point color set to NORMAL.");
341  }
342  break;
343  default:
344  break;
345  }
346 
347  is_redraw_required_ = true;
348 }
349 
350 void Visualizer::WindowCloseCallback(GLFWwindow *window) {
351  // happens when user click the close icon to close the window
352 }
353 
354 } // namespace visualization
355 } // namespace cloudViewer
virtual bool UpdateGeometry(std::shared_ptr< const ccHObject > geometry_ptr=nullptr)
Function to update geometry.
Definition: Visualizer.cpp:466
virtual void KeyPressCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
virtual void WindowRefreshCallback(GLFWwindow *window)
void CaptureScreenImage(const std::string &filename="", bool do_render=true)
Function to capture and save a screen image.
virtual void MouseScrollCallback(GLFWwindow *window, double x, double y)
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 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
virtual void Render(bool render_screen=false)
std::unique_ptr< ViewControl > view_control_ptr_
Definition: Visualizer.h:281
void CaptureRenderOption(const std::string &filename="")
virtual void WindowResizeCallback(GLFWwindow *window, int w, int h)
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.
#define LogDebug(...)
Definition: Logging.h:90
void SetGlobalColorMap(ColorMap::ColorMapOption option)
Definition: ColorMap.cpp:88
Generic file read and write utility for python interface.