ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ViewGeometry.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 "CloudViewer.h"
9 
10 void PrintHelp() {
11  using namespace cloudViewer;
12  PrintCloudViewerVersion();
13 
14  // clang-format off
15  utility::LogInfo("Usage:");
16  utility::LogInfo(" > ViewGeometry [options]");
17  utility::LogInfo(" Open a window to view geometry.");
18  utility::LogInfo("");
19  utility::LogInfo("Basic options:");
20  utility::LogInfo(" --help, -h : Print help information.");
21  utility::LogInfo(" --mesh file : Add a triangle mesh from file.");
22  utility::LogInfo(" --pointcloud file : Add a point cloud from file.");
23  utility::LogInfo(" --lineset file : Add a line set from file.");
24  utility::LogInfo(" --voxelgrid file : Add a voxel grid from file.");
25  utility::LogInfo(" --image file : Add an image from file.");
26  utility::LogInfo(" --depth file : Add a point cloud converted from a depth image.");
27  utility::LogInfo(" --depth_camera file : Use with --depth, read a json file that stores");
28  utility::LogInfo(" the camera parameters.");
29  utility::LogInfo(" --show_frame : Add a coordinate frame.");
30  utility::LogInfo(" --verbose n : Set verbose level (0-4).");
31  utility::LogInfo("");
32  utility::LogInfo("Animation options:");
33  utility::LogInfo(" --render_option file : Read a json file of rendering settings.");
34  utility::LogInfo(" --view_trajectory file : Read a json file of view trajectory.");
35  utility::LogInfo(" --camera_trajectory file : Read a json file of camera trajectory.");
36  utility::LogInfo(" --auto_recording [i|d] : Automatically plays the animation, record");
37  utility::LogInfo(" images (i) or depth images (d). Exits when");
38  utility::LogInfo(" animation ends.");
39  utility::LogInfo("");
40  utility::LogInfo("Window options:");
41  utility::LogInfo(" --window_name name : Set window name.");
42  utility::LogInfo(" --height n : Set window height.");
43  utility::LogInfo(" --width n : Set window width.");
44  utility::LogInfo(" --top n : Set window top edge.");
45  utility::LogInfo(" --left n : Set window left edge.");
46  // clang-format on
47 }
48 
49 int main(int argc, char **argv) {
50  using namespace cloudViewer;
51  using namespace cloudViewer::utility::filesystem;
52 
53  int verbose = utility::GetProgramOptionAsInt(argc, argv, "--verbose", 5);
55  if (argc <= 1 || utility::ProgramOptionExists(argc, argv, "--help") ||
56  utility::ProgramOptionExists(argc, argv, "-h")) {
57  PrintHelp();
58  return 0;
59  }
60 
61  std::vector<std::shared_ptr<ccHObject>> geometry_ptrs;
62  int width = utility::GetProgramOptionAsInt(argc, argv, "--width", 1920);
63  int height = utility::GetProgramOptionAsInt(argc, argv, "--height", 1080);
64  int top = utility::GetProgramOptionAsInt(argc, argv, "--top", 200);
65  int left = utility::GetProgramOptionAsInt(argc, argv, "--left", 200);
66  std::string window_name = utility::GetProgramOptionAsString(
67  argc, argv, "--window_name", "ViewGeometry");
68  std::string mesh_filename =
69  utility::GetProgramOptionAsString(argc, argv, "--mesh");
70  std::string pcd_filename =
71  utility::GetProgramOptionAsString(argc, argv, "--pointcloud");
72  std::string lineset_filename =
73  utility::GetProgramOptionAsString(argc, argv, "--lineset");
74  std::string voxelgrid_filename =
75  utility::GetProgramOptionAsString(argc, argv, "--voxelgrid");
76  std::string image_filename =
77  utility::GetProgramOptionAsString(argc, argv, "--image");
78  std::string depth_filename =
79  utility::GetProgramOptionAsString(argc, argv, "--depth");
80  std::string depth_parameter_filename =
81  utility::GetProgramOptionAsString(argc, argv, "--depth_camera");
82  std::string render_filename =
83  utility::GetProgramOptionAsString(argc, argv, "--render_option");
84  std::string view_filename =
85  utility::GetProgramOptionAsString(argc, argv, "--view_trajectory");
86  std::string camera_filename = utility::GetProgramOptionAsString(
87  argc, argv, "--camera_trajectory");
88  bool show_coordinate_frame =
89  utility::ProgramOptionExists(argc, argv, "--show_frame");
90 
92  if (!visualizer.CreateVisualizerWindow(window_name, width, height, left,
93  top)) {
94  utility::LogWarning("Failed creating OpenGL window.");
95  return 0;
96  }
97 
98  if (!mesh_filename.empty()) {
99  auto mesh_ptr = io::CreateMeshFromFile(mesh_filename);
101  if (!visualizer.AddGeometry(mesh_ptr)) {
102  utility::LogWarning("Failed adding triangle mesh.");
103  return 1;
104  }
105  }
106  if (!pcd_filename.empty()) {
107  auto pointcloud_ptr = io::CreatePointCloudFromFile(pcd_filename);
108  if (!visualizer.AddGeometry(pointcloud_ptr)) {
109  utility::LogWarning("Failed adding point cloud.");
110  return 1;
111  }
112  if (pointcloud_ptr->size() > 5000000) {
113  visualizer.GetRenderOption().point_size_ = 1.0;
114  }
115  }
116  if (!lineset_filename.empty()) {
117  auto lineset_ptr = io::CreateLineSetFromFile(lineset_filename);
118  if (!visualizer.AddGeometry(lineset_ptr)) {
119  utility::LogWarning("Failed adding line set.");
120  return 1;
121  }
122  }
123  if (!voxelgrid_filename.empty()) {
124  auto voxelgrid_ptr = io::CreateVoxelGridFromFile(voxelgrid_filename);
125  if (!visualizer.AddGeometry(voxelgrid_ptr)) {
126  utility::LogWarning("Failed adding voxel grid.");
127  return 1;
128  }
129  }
130  if (!image_filename.empty()) {
131  auto image_ptr = io::CreateImageFromFile(image_filename);
132  if (!visualizer.AddGeometry(image_ptr)) {
133  utility::LogWarning("Failed adding image.");
134  return 1;
135  }
136  }
137  if (!depth_filename.empty()) {
139  if (depth_parameter_filename.empty() ||
140  !io::ReadIJsonConvertible(depth_parameter_filename, parameters)) {
142  "Failed to read intrinsic parameters for depth image.");
143  utility::LogWarning("Using default value for Primesense camera.");
144  parameters.intrinsic_.SetIntrinsics(640, 480, 525.0, 525.0, 319.5,
145  239.5);
146  }
147  auto image_ptr = io::CreateImageFromFile(depth_filename);
149  *image_ptr, parameters.intrinsic_, parameters.extrinsic_);
150  if (pointcloud_ptr == nullptr) {
151  utility::LogWarning("Failed creating from depth image.");
152  return 1;
153  }
154  if (!visualizer.AddGeometry(pointcloud_ptr)) {
155  utility::LogWarning("Failed adding depth image.");
156  return 1;
157  }
158  }
159 
160  if (!visualizer.HasGeometry()) {
161  utility::LogWarning("No geometry to render!");
162  visualizer.DestroyVisualizerWindow();
163  return 1;
164  }
165 
166  if (!render_filename.empty()) {
167  if (!io::ReadIJsonConvertible(render_filename,
168  visualizer.GetRenderOption())) {
169  utility::LogWarning("Failed loading rendering settings.");
170  return 1;
171  }
172  }
173 
174  if (!view_filename.empty()) {
175  auto &view_control =
177  visualizer.GetViewControl();
178  if (!view_control.LoadTrajectoryFromJsonFile(view_filename)) {
179  utility::LogWarning("Failed loading view trajectory.");
180  return 1;
181  }
182  } else if (!camera_filename.empty()) {
183  camera::PinholeCameraTrajectory camera_trajectory;
184  if (!io::ReadIJsonConvertible(camera_filename, camera_trajectory)) {
185  utility::LogWarning("Failed loading camera trajectory.");
186  return 1;
187  } else {
188  auto &view_control =
190  visualizer.GetViewControl();
191  if (!view_control.LoadTrajectoryFromCameraTrajectory(
192  camera_trajectory)) {
194  "Failed converting camera trajectory to view "
195  "trajectory.");
196  return 1;
197  }
198  }
199  }
200 
201  visualizer.GetRenderOption().show_coordinate_frame_ = show_coordinate_frame;
202 
203  if (utility::ProgramOptionExists(argc, argv, "--auto_recording")) {
204  std::string mode = utility::GetProgramOptionAsString(
205  argc, argv, "--auto_recording");
206  if (mode == "i") {
207  visualizer.Play(true, false, true);
208  } else if (mode == "d") {
209  visualizer.Play(true, true, true);
210  } else {
211  visualizer.Play(true, false, true);
212  }
213  visualizer.Run();
214  } else {
215  visualizer.Run();
216  }
217  visualizer.DestroyVisualizerWindow();
218 
219  return 0;
220 }
int width
ccPointCloud * pointcloud_ptr
int height
int main(int argc, char **argv)
void PrintHelp()
ccMesh & ComputeVertexNormals(bool normalized=true)
Function to compute vertex normals, usually called before rendering.
static std::shared_ptr< ccPointCloud > CreateFromDepthImage(const cloudViewer::geometry::Image &depth, const cloudViewer::camera::PinholeCameraIntrinsic &intrinsic, const Eigen::Matrix4d &extrinsic=Eigen::Matrix4d::Identity(), double depth_scale=1000.0, double depth_trunc=1000.0, int stride=1, bool project_valid_depth_only=true)
Factory function to create a pointcloud from a depth image and a camera model.
unsigned size() const override
Definition: PointCloudTpl.h:38
void SetIntrinsics(int width, int height, double fx, double fy, double cx, double cy)
Set camera intrinsic parameters.
Contains both intrinsic and extrinsic pinhole camera parameters.
PinholeCameraIntrinsic intrinsic_
PinholeCameraIntrinsic object.
Eigen::Matrix4d_u extrinsic_
Camera extrinsic parameters.
double point_size_
Point size for PointCloud.
Definition: RenderOption.h:178
bool show_coordinate_frame_
Whether to show coordinate frame.
Definition: RenderOption.h:206
void Play(bool recording=false, bool recording_depth=false, bool close_window_when_animation_ends=false)
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
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
ccMesh * mesh_ptr
#define LogWarning(...)
Definition: Logging.h:72
#define LogInfo(...)
Definition: Logging.h:81
std::shared_ptr< ccMesh > CreateMeshFromFile(const std::string &filename, bool print_progress)
std::shared_ptr< geometry::Image > CreateImageFromFile(const std::string &filename)
std::shared_ptr< geometry::LineSet > CreateLineSetFromFile(const std::string &filename, const std::string &format="auto", bool print_progress=false)
std::shared_ptr< geometry::VoxelGrid > CreateVoxelGridFromFile(const std::string &filename, const std::string &format="auto", bool print_progress=false)
std::shared_ptr< ccPointCloud > CreatePointCloudFromFile(const std::string &filename, const std::string &format, bool print_progress)
bool ReadIJsonConvertible(const std::string &filename, cloudViewer::utility::IJsonConvertible &object)
int GetProgramOptionAsInt(int argc, char **argv, const std::string &option, const int default_value=0)
Definition: Console.cpp:31
void SetVerbosityLevel(VerbosityLevel level)
Definition: Logging.cpp:89
bool ProgramOptionExists(int argc, char **argv, const std::string &option)
Definition: Console.cpp:100
std::string GetProgramOptionAsString(int argc, char **argv, const std::string &option, const std::string &default_value="")
Definition: Console.cpp:19
Generic file read and write utility for python interface.