ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
visualizer.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 
9 
10 #include <Image.h>
11 
12 #include "pybind/docstring.h"
18 
19 namespace cloudViewer {
20 namespace visualization {
21 
22 // Functions have similar arguments, thus the arg docstrings may be shared
23 static const std::unordered_map<std::string, std::string>
25  {"callback_func", "The call back function."},
26  {"depth_scale",
27  "Scale depth value when capturing the depth image."},
28  {"do_render", "Set to ``True`` to do render."},
29  {"filename", "Path to file."},
30  {"geometry", "The ``Geometry`` object."},
31  {"height", "Height of window."},
32  {"left", "Left margin of the window to the screen."},
33  {"top", "Top margin of the window to the screen."},
34  {"visible", "Whether the window is visible."},
35  {"width", "Width of the window."},
36  {"window_name", "Window title name."},
37  {"convert_to_world_coordinate",
38  "Set to ``True`` to convert to world coordinates"},
39  {"reset_bounding_box",
40  "Set to ``False`` to keep current viewpoint"}};
41 
42 void pybind_visualizer(py::module &m) {
43  py::class_<Visualizer, PyVisualizer<>, std::shared_ptr<Visualizer>>
44  visualizer(m, "Visualizer", "The main Visualizer class.");
45  py::detail::bind_default_constructor<Visualizer>(visualizer);
46  visualizer
47  .def("__repr__",
48  [](const Visualizer &vis) {
49  return std::string("Visualizer with name ") +
50  vis.GetWindowName();
51  })
52  .def("create_window", &Visualizer::CreateVisualizerWindow,
53  "Function to create a window and initialize GLFW",
54  "window_name"_a = "CloudViewer", "width"_a = 1920,
55  "height"_a = 1080, "left"_a = 50, "top"_a = 50,
56  "visible"_a = true)
57  .def("destroy_window", &Visualizer::DestroyVisualizerWindow,
58  "Function to destroy a window. This function MUST be called "
59  "from the main thread.")
60  .def("register_animation_callback",
62  "Function to register a callback function for animation. The "
63  "callback function returns if UpdateGeometry() needs to be "
64  "run.",
65  "callback_func"_a)
66  .def("run", &Visualizer::Run,
67  "Function to activate the window. This function will block "
68  "the current thread until the window is closed.")
69  .def("close", &Visualizer::Close,
70  "Function to notify the window to be closed")
71  .def("reset_view_point", &Visualizer::ResetViewPoint,
72  "Function to reset view point", "reset_bounding_box"_a = false)
73  .def("update_geometry", &Visualizer::UpdateGeometry,
74  "Function to update geometry. This function must be called "
75  "when geometry has been changed. Otherwise the behavior of "
76  "Visualizer is undefined.",
77  "geometry"_a)
78  .def("update_renderer", &Visualizer::UpdateRender,
79  "Function to inform render needed to be updated")
80  .def("set_full_screen", &Visualizer::SetFullScreen,
81  "Function to change between fullscreen and windowed",
82  "fullscreen"_a)
83  .def("toggle_full_screen", &Visualizer::ToggleFullScreen,
84  "Function to toggle between fullscreen and windowed")
85  .def("is_full_screen", &Visualizer::IsFullScreen,
86  "Function to query whether in fullscreen mode")
87  .def("poll_events", &Visualizer::PollEvents,
88  "Function to poll events")
89  .def("add_geometry", &Visualizer::AddGeometry,
90  "Function to add geometry to the scene and create "
91  "corresponding shaders",
92  "geometry"_a, "reset_bounding_box"_a = true)
93  .def("remove_geometry", &Visualizer::RemoveGeometry,
94  "Function to remove geometry", "geometry"_a,
95  "reset_bounding_box"_a = true)
96  .def("clear_geometries", &Visualizer::ClearGeometries,
97  "Function to clear geometries from the visualizer")
98  .def("get_view_control", &Visualizer::GetViewControl,
99  "Function to retrieve the associated ``ViewControl``",
100  py::return_value_policy::reference_internal)
101  .def("get_render_option", &Visualizer::GetRenderOption,
102  "Function to retrieve the associated ``RenderOption``",
103  py::return_value_policy::reference_internal)
104  .def("capture_screen_float_buffer",
106  "Function to capture screen and store RGB in a float buffer",
107  "do_render"_a = false)
108  .def(
109  "capture_screen_image",
110  [](Visualizer &self, const fs::path &filename,
111  bool do_render) {
112  return self.CaptureScreenImage(filename.string(),
113  do_render);
114  },
115  "Function to capture and save a screen image", "filename"_a,
116  "do_render"_a = false)
117  .def("capture_depth_float_buffer",
119  "Function to capture depth in a float buffer",
120  "do_render"_a = false)
121  .def(
122  "capture_depth_image",
123  [](Visualizer &self, const fs::path &filename,
124  bool do_render, double depth_scale) {
125  self.CaptureDepthImage(filename.string(), do_render,
126  depth_scale);
127  },
128  "Function to capture and save a depth image", "filename"_a,
129  "do_render"_a = false, "depth_scale"_a = 1000.0)
130  .def(
131  "capture_depth_point_cloud",
132  [](Visualizer &self, const fs::path &filename,
133  bool do_render, bool convert_to_world_coordinate) {
134  self.CaptureDepthPointCloud(
135  filename.string(), do_render,
136  convert_to_world_coordinate);
137  },
138  "Function to capture and save local point cloud",
139  "filename"_a, "do_render"_a = false,
140  "convert_to_world_coordinate"_a = false)
141  .def("get_window_name", &Visualizer::GetWindowName);
142 
143  py::class_<VisualizerWithKeyCallback,
145  std::shared_ptr<VisualizerWithKeyCallback>>
146  visualizer_key(m, "VisualizerWithKeyCallback", visualizer,
147  "Visualizer with custom key callack capabilities.");
148  py::detail::bind_default_constructor<VisualizerWithKeyCallback>(
149  visualizer_key);
150  visualizer_key
151  .def("__repr__",
152  [](const VisualizerWithKeyCallback &vis) {
153  return std::string(
154  "VisualizerWithKeyCallback with name ") +
155  vis.GetWindowName();
156  })
157  .def("register_key_callback",
159  "Function to register a callback function for a key press "
160  "event",
161  "key"_a, "callback_func"_a)
162 
163  .def("register_key_action_callback",
165  "Function to register a callback function for a key action "
166  "event. The callback function takes `Visualizer`, `action` "
167  "and `mods` as input and returns a boolean indicating if "
168  "`UpdateGeometry()` needs to be run. The `action` can be one "
169  "of `GLFW_RELEASE` (0), `GLFW_PRESS` (1) or `GLFW_REPEAT` "
170  "(2), see `GLFW input interface "
171  "<https://www.glfw.org/docs/latest/group__input.html>`__. The "
172  "`mods` specifies the modifier key, see `GLFW modifier key "
173  "<https://www.glfw.org/docs/latest/group__mods.html>`__",
174  "key"_a, "callback_func"_a)
175 
176  .def("register_mouse_move_callback",
178  "Function to register a callback function for a mouse move "
179  "event. The callback function takes Visualizer, x and y mouse "
180  "position inside the window as input and returns a boolean "
181  "indicating if UpdateGeometry() needs to be run. `GLFW mouse "
182  "position <https://www.glfw.org/docs/latest/"
183  "input_guide.html#input_mouse>`__ for more details.",
184  "callback_func"_a)
185 
186  .def("register_mouse_scroll_callback",
188  "Function to register a callback function for a mouse scroll "
189  "event. The callback function takes Visualizer, x and y mouse "
190  "scroll offset as input and returns a boolean "
191  "indicating if UpdateGeometry() needs to be run. `GLFW mouse "
192  "scrolling <https://www.glfw.org/docs/latest/"
193  "input_guide.html#scrolling>`__ for more details.",
194  "callback_func"_a)
195 
196  .def("register_mouse_button_callback",
198  "Function to register a callback function for a mouse button "
199  "event. The callback function takes `Visualizer`, `button`, "
200  "`action` and `mods` as input and returns a boolean "
201  "indicating `UpdateGeometry()` needs to be run. The `action` "
202  "can be one of GLFW_RELEASE (0), GLFW_PRESS (1) or "
203  "GLFW_REPEAT (2), see `GLFW input interface "
204  "<https://www.glfw.org/docs/latest/group__input.html>`__. "
205  "The `mods` specifies the modifier key, see `GLFW modifier "
206  "key <https://www.glfw.org/docs/latest/group__mods.html>`__.",
207  "callback_func"_a);
208 
209  py::class_<VisualizerWithEditing, PyVisualizer<VisualizerWithEditing>,
210  std::shared_ptr<VisualizerWithEditing>>
211  visualizer_edit(m, "VisualizerWithEditing", visualizer,
212  "Visualizer with editing capabilities.");
213  py::detail::bind_default_constructor<VisualizerWithEditing>(
214  visualizer_edit);
215  visualizer_edit
216  .def(py::init<double, bool, const std::string &>(), "voxel_size"_a,
217  "use_dialog"_a, "directory"_a)
218  .def("__repr__",
219  [](const VisualizerWithEditing &vis) {
220  return std::string("VisualizerWithEditing with name ") +
221  vis.GetWindowName();
222  })
223  .def("get_picked_points", &VisualizerWithEditing::GetPickedPoints,
224  "Function to get picked points");
225 
228  std::shared_ptr<VisualizerWithVertexSelection>>
229  visualizer_vselect(
230  m, "VisualizerWithVertexSelection", visualizer,
231  "Visualizer with vertex selection capabilities.");
232  py::detail::bind_default_constructor<VisualizerWithVertexSelection>(
233  visualizer_vselect);
234  visualizer_vselect.def(py::init<>())
235  .def("__repr__",
236  [](const VisualizerWithVertexSelection &vis) {
237  return std::string(
238  "VisualizerWithVertexSelection with "
239  "name ") +
240  vis.GetWindowName();
241  })
242  .def("pick_points", &VisualizerWithVertexSelection::PickPoints,
243  "Function to pick points", "x"_a, "y"_a, "w"_a, "h"_a)
244  .def("get_picked_points",
246  "Function to get picked points")
247  .def("clear_picked_points",
249  "Function to clear picked points")
250  .def("add_picked_points",
252  "Function to add picked points", "indices"_a)
253  .def("remove_picked_points",
255  "Function to remove picked points", "indices"_a)
256  .def("register_selection_changed_callback",
258  RegisterSelectionChangedCallback,
259  "Registers a function to be called when selection changes",
260  "f"_a)
261  .def("register_selection_moving_callback",
263  RegisterSelectionMovingCallback,
264  "Registers a function to be called while selection moves. "
265  "Geometry's vertex values can be changed, but do not change"
266  "the number of vertices.",
267  "f"_a)
268  .def("register_selection_moved_callback",
270  "Registers a function to be called after selection moves",
271  "f"_a);
272 
273  py::class_<VisualizerWithVertexSelection::PickedPoint>
274  visualizer_vselect_pickedpoint(m, "PickedPoint");
275  visualizer_vselect_pickedpoint.def(py::init<>())
276  .def_readwrite("index",
278  .def_readwrite("coord",
280 
281  docstring::ClassMethodDocInject(m, "Visualizer", "add_geometry",
283  docstring::ClassMethodDocInject(m, "Visualizer", "remove_geometry",
285  docstring::ClassMethodDocInject(m, "Visualizer",
286  "capture_depth_float_buffer",
288  docstring::ClassMethodDocInject(m, "Visualizer", "capture_depth_image",
290  docstring::ClassMethodDocInject(m, "Visualizer",
291  "capture_depth_point_cloud",
293  docstring::ClassMethodDocInject(m, "Visualizer",
294  "capture_screen_float_buffer",
296  docstring::ClassMethodDocInject(m, "Visualizer", "capture_screen_image",
298  docstring::ClassMethodDocInject(m, "Visualizer", "close",
300  docstring::ClassMethodDocInject(m, "Visualizer", "create_window",
302  docstring::ClassMethodDocInject(m, "Visualizer", "destroy_window",
304  docstring::ClassMethodDocInject(m, "Visualizer", "get_render_option",
306  docstring::ClassMethodDocInject(m, "Visualizer", "get_view_control",
308  docstring::ClassMethodDocInject(m, "Visualizer", "get_window_name",
310  docstring::ClassMethodDocInject(m, "Visualizer", "poll_events",
312  docstring::ClassMethodDocInject(m, "Visualizer",
313  "register_animation_callback",
315  docstring::ClassMethodDocInject(m, "Visualizer", "reset_view_point",
317  docstring::ClassMethodDocInject(m, "Visualizer", "run",
319  docstring::ClassMethodDocInject(m, "Visualizer", "update_geometry",
321  docstring::ClassMethodDocInject(m, "Visualizer", "update_renderer",
323  docstring::ClassMethodDocInject(m, "Visualizer", "set_full_screen",
325  docstring::ClassMethodDocInject(m, "Visualizer", "toggle_full_screen",
327  docstring::ClassMethodDocInject(m, "Visualizer", "is_full_screen",
329 }
330 
331 void pybind_visualizer_method(py::module &m) {}
332 
333 } // namespace visualization
334 } // namespace cloudViewer
std::string filename
Visualizer with custom key callack capabilities.
void RegisterMouseScrollCallback(std::function< bool(Visualizer *, double, double)> callback)
void RegisterMouseMoveCallback(std::function< bool(Visualizer *, double, double)> callback)
void RegisterKeyActionCallback(int key, std::function< bool(Visualizer *, int, int)> callback)
void RegisterMouseButtonCallback(std::function< bool(Visualizer *, int, int, int)> callback)
void RegisterKeyCallback(int key, std::function< bool(Visualizer *)> callback)
std::vector< int > PickPoints(double x, double y, double w, double h)
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
std::shared_ptr< geometry::Image > CaptureDepthFloatBuffer(bool do_render=true)
const std::string & GetWindowName() const
Definition: Visualizer.h:215
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:177
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
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
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
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< geometry::Image > CaptureScreenFloatBuffer(bool do_render=true)
Function to capture screen and store RGB in a float buffer.
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
Definition: Visualizer.h:175
void ClassMethodDocInject(py::module &pybind_module, const std::string &class_name, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:27
static const std::string path
Definition: PointCloud.cpp:59
void pybind_visualizer_method(py::module &m)
Definition: visualizer.cpp:331
void pybind_visualizer(py::module &m)
Definition: visualizer.cpp:42
static const std::unordered_map< std::string, std::string > map_visualizer_docstrings
Definition: visualizer.cpp:24
Generic file read and write utility for python interface.