ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
DrawGeometry.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 // clang-format off
10 // clang-format on
12 
13 #include <Logging.h>
14 #include <ecvMesh.h>
15 #include <ecvPointCloud.h>
16 
17 #include <Eigen/Core>
18 
27 
28 namespace cloudViewer {
29 namespace visualization {
30 using namespace cloudViewer;
31 
33  const std::vector<std::shared_ptr<const ccHObject>> &geometry_ptrs,
34  const std::string &window_name /* = "cloudViewer"*/,
35  int width /* = 640*/,
36  int height /* = 480*/,
37  int left /* = 50*/,
38  int top /* = 50*/,
39  bool point_show_normal /* = false */,
40  bool mesh_show_wireframe /* = false */,
41  bool mesh_show_back_face /* = false */,
42  Eigen::Vector3d *lookat /* = nullptr */,
43  Eigen::Vector3d *up /* = nullptr */,
44  Eigen::Vector3d *front /* = nullptr */,
45  double *zoom /* = nullptr */) {
46  Visualizer visualizer;
47  if (!visualizer.CreateVisualizerWindow(window_name, width, height, left,
48  top)) {
49  utility::LogWarning("[DrawGeometries] Failed creating OpenGL window.");
50  return false;
51  }
52  visualizer.GetRenderOption().point_show_normal_ = point_show_normal;
53  visualizer.GetRenderOption().mesh_show_wireframe_ = mesh_show_wireframe;
54  visualizer.GetRenderOption().mesh_show_back_face_ = mesh_show_back_face;
55  for (const auto &geometry_ptr : geometry_ptrs) {
56  if (!visualizer.AddGeometry(geometry_ptr)) {
57  utility::LogWarning("[DrawGeometries] Failed adding geometry.");
59  "[DrawGeometries] Possibly due to bad geometry or wrong "
60  "geometry type.");
61  return false;
62  }
63  }
64 
65  ViewControl &view_control = visualizer.GetViewControl();
66  if (lookat != nullptr) {
67  view_control.SetLookat(*lookat);
68  }
69  if (up != nullptr) {
70  view_control.SetUp(*up);
71  }
72  if (front != nullptr) {
73  view_control.SetFront(*front);
74  }
75  if (zoom != nullptr) {
76  view_control.SetZoom(*zoom);
77  }
78 
79  visualizer.Run();
80  visualizer.DestroyVisualizerWindow();
81  return true;
82 }
83 
85  const std::vector<std::shared_ptr<const ccHObject>> &geometry_ptrs,
86  const std::string &window_name /* = "cloudViewer"*/,
87  int width /* = 640*/,
88  int height /* = 480*/,
89  int left /* = 50*/,
90  int top /* = 50*/,
91  const std::string &json_filename /* = ""*/) {
93  if (!visualizer.CreateVisualizerWindow(window_name, width, height, left,
94  top)) {
96  "[DrawGeometriesWithCustomAnimation] Failed creating OpenGL "
97  "window.");
98  return false;
99  }
100  for (const auto &geometry_ptr : geometry_ptrs) {
101  if (!visualizer.AddGeometry(geometry_ptr)) {
103  "[DrawGeometriesWithCustomAnimation] Failed adding "
104  "geometry.");
106  "[DrawGeometriesWithCustomAnimation] Possibly due to bad "
107  "geometry or wrong geometry type.");
108  return false;
109  }
110  }
111  auto &view_control =
113  if (!json_filename.empty()) {
114  if (!view_control.LoadTrajectoryFromJsonFile(json_filename)) {
116  "[DrawGeometriesWithCustomAnimation] Failed loading json "
117  "file.");
119  "[DrawGeometriesWithCustomAnimation] Possibly due to bad "
120  "file or file does not contain trajectory.");
121  return false;
122  }
123  visualizer.UpdateWindowTitle();
124  }
125  visualizer.Run();
126  visualizer.DestroyVisualizerWindow();
127  return true;
128 }
129 
131  const std::vector<std::shared_ptr<const ccHObject>> &geometry_ptrs,
132  std::function<bool(Visualizer *)> callback_func,
133  const std::string &window_name /* = "cloudViewer"*/,
134  int width /* = 640*/,
135  int height /* = 480*/,
136  int left /* = 50*/,
137  int top /* = 50*/) {
138  Visualizer visualizer;
139  if (!visualizer.CreateVisualizerWindow(window_name, width, height, left,
140  top)) {
142  "[DrawGeometriesWithAnimationCallback] Failed creating OpenGL "
143  "window.");
144  return false;
145  }
146  for (const auto &geometry_ptr : geometry_ptrs) {
147  if (!visualizer.AddGeometry(geometry_ptr)) {
149  "[DrawGeometriesWithAnimationCallback] Failed adding "
150  "geometry.");
152  "[DrawGeometriesWithAnimationCallback] Possibly due to bad "
153  "geometry or wrong geometry type.");
154  return false;
155  }
156  }
157  visualizer.RegisterAnimationCallback(callback_func);
158  visualizer.Run();
159  visualizer.DestroyVisualizerWindow();
160  return true;
161 }
162 
164  const std::vector<std::shared_ptr<const ccHObject>> &geometry_ptrs,
165  const std::map<int, std::function<bool(Visualizer *)>> &key_to_callback,
166  const std::string &window_name /* = "cloudViewer"*/,
167  int width /* = 640*/,
168  int height /* = 480*/,
169  int left /* = 50*/,
170  int top /* = 50*/) {
171  VisualizerWithKeyCallback visualizer;
172  if (!visualizer.CreateVisualizerWindow(window_name, width, height, left,
173  top)) {
175  "[DrawGeometriesWithKeyCallbacks] Failed creating OpenGL "
176  "window.");
177  return false;
178  }
179  for (const auto &geometry_ptr : geometry_ptrs) {
180  if (!visualizer.AddGeometry(geometry_ptr)) {
182  "[DrawGeometriesWithKeyCallbacks] Failed adding "
183  "geometry.");
185  "[DrawGeometriesWithKeyCallbacks] Possibly due to bad "
186  "geometry or wrong geometry type.");
187  return false;
188  }
189  }
190  for (auto key_func_pair : key_to_callback) {
191  visualizer.RegisterKeyCallback(key_func_pair.first,
192  key_func_pair.second);
193  }
194  visualizer.Run();
195  visualizer.DestroyVisualizerWindow();
196  return true;
197 }
198 
200  const std::vector<std::shared_ptr<const ccHObject>> &geometry_ptrs,
201  const std::string &window_name /* = "cloudViewer"*/,
202  int width /* = 640*/,
203  int height /* = 480*/,
204  int left /* = 50*/,
205  int top /* = 50*/) {
206  VisualizerWithEditing visualizer;
207  if (!visualizer.CreateVisualizerWindow(window_name, width, height, left,
208  top)) {
210  "[DrawGeometriesWithEditing] Failed creating OpenGL window.");
211  return false;
212  }
213  for (const auto &geometry_ptr : geometry_ptrs) {
214  if (!visualizer.AddGeometry(geometry_ptr)) {
216  "[DrawGeometriesWithEditing] Failed adding geometry.");
218  "[DrawGeometriesWithEditing] Possibly due to bad geometry "
219  "or wrong geometry type.");
220  return false;
221  }
222  }
223  visualizer.Run();
224  visualizer.DestroyVisualizerWindow();
225  return true;
226 }
227 
229  const std::vector<std::shared_ptr<const ccHObject>> &geometry_ptrs,
230  const std::string &window_name /* = "cloudViewer"*/,
231  int width /* = 640*/,
232  int height /* = 480*/,
233  int left /* = 50*/,
234  int top /* = 50*/) {
236  if (!visualizer.CreateVisualizerWindow(window_name, width, height, left,
237  top)) {
239  "[DrawGeometriesWithVertexSelection] Failed creating OpenGL "
240  "window.");
241  return false;
242  }
243  for (const auto &geometry_ptr : geometry_ptrs) {
244  if (!visualizer.AddGeometry(geometry_ptr)) {
246  "[DrawGeometriesWithVertexSelection] Failed adding "
247  "geometry.");
249  "[DrawGeometriesWithVertexSelection] Possibly due to bad "
250  "geometry or wrong geometry type.");
251  return false;
252  }
253  }
254  visualizer.Run();
255  visualizer.DestroyVisualizerWindow();
256  return true;
257 }
258 
259 } // namespace visualization
260 } // namespace cloudViewer
int width
int height
bool mesh_show_back_face_
Whether to show back faces for TriangleMesh.
Definition: RenderOption.h:190
bool point_show_normal_
Whether to show normal for PointCloud.
Definition: RenderOption.h:182
View controller for visualizer.
Definition: ViewControl.h:25
void SetFront(const Eigen::Vector3d &front)
void SetUp(const Eigen::Vector3d &up)
void SetLookat(const Eigen::Vector3d &lookat)
bool AddGeometry(std::shared_ptr< const ccHObject > geometry_ptr, bool reset_bounding_box=true) override
Function to add geometry to the scene and create corresponding shaders.
Visualizer with custom key callack capabilities.
void RegisterKeyCallback(int key, std::function< bool(Visualizer *)> callback)
bool AddGeometry(std::shared_ptr< const ccHObject > geometry_ptr, bool reset_bounding_box=true) override
Function to add geometry to the scene and create corresponding shaders.
The main Visualizer class.
Definition: Visualizer.h:45
RenderOption & GetRenderOption()
Function to retrieve the associated RenderOption.
Definition: Visualizer.h:177
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
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
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
Definition: Visualizer.h:175
#define LogWarning(...)
Definition: Logging.h:72
bool DrawGeometriesWithVertexSelection(const std::vector< std::shared_ptr< const ccHObject >> &geometry_ptrs, const std::string &window_name, int width, int height, int left, int top)
bool DrawGeometriesWithKeyCallbacks(const std::vector< std::shared_ptr< const ccHObject >> &geometry_ptrs, const std::map< int, std::function< bool(Visualizer *)>> &key_to_callback, const std::string &window_name, int width, int height, int left, int top)
Function to draw a list of geometry.
bool DrawGeometriesWithCustomAnimation(const std::vector< std::shared_ptr< const ccHObject >> &geometry_ptrs, const std::string &window_name, int width, int height, int left, int top, const std::string &json_filename)
Function to draw a list of geometry objects with a GUI that supports animation.
bool DrawGeometriesWithEditing(const std::vector< std::shared_ptr< const ccHObject >> &geometry_ptrs, const std::string &window_name, int width, int height, int left, int top)
Function to draw a list of geometry.
bool DrawGeometries(const std::vector< std::shared_ptr< const ccHObject >> &geometry_ptrs, const std::string &window_name, int width, int height, int left, int top, bool point_show_normal, bool mesh_show_wireframe, bool mesh_show_back_face, Eigen::Vector3d *lookat, Eigen::Vector3d *up, Eigen::Vector3d *front, double *zoom)
Function to draw a list of geometry objects.
bool DrawGeometriesWithAnimationCallback(const std::vector< std::shared_ptr< const ccHObject >> &geometry_ptrs, std::function< bool(Visualizer *)> callback_func, const std::string &window_name, int width, int height, int left, int top)
Function to draw a list of geometry objects with a customized animation callback function.
Generic file read and write utility for python interface.