ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PointCloudIO.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 "io/PointCloudIO.h"
9 
10 // CV_CORE_LIB
11 #include <FileSystem.h>
12 #include <Helper.h>
13 #include <Logging.h>
14 #include <ProgressReporters.h>
15 
16 // CV_DB_LIB
17 #include <ecvPointCloud.h>
18 
19 // CV_IO_LIB
20 #include <AutoIO.h>
21 
22 // SYSTEM
23 #include <iostream>
24 #include <unordered_map>
25 
26 namespace cloudViewer {
27 namespace io {
28 static const std::unordered_map<
29  std::string,
30  std::function<bool(const std::string &,
31  ccPointCloud &,
32  const ReadPointCloudOption &)>>
34  {"xyz", ReadPointCloudFromXYZ},
35  {"txt", ReadPointCloudFromXYZ},
36  {"xyzn", ReadPointCloudFromXYZN},
37  {"xyzrgb", ReadPointCloudFromXYZRGB},
38  {"ply", ReadPointCloudFromPLY},
39  {"pcd", ReadPointCloudFromPCD},
40  {"pts", ReadPointCloudFromPTS},
41  {"ptx", AutoReadEntity},
42  {"vtk", AutoReadEntity},
43  {"bin", AutoReadEntity},
44  };
45 
46 static const std::unordered_map<
47  std::string,
48  std::function<bool(const unsigned char *,
49  const size_t,
50  ccPointCloud &,
51  const ReadPointCloudOption &)>>
53  {"mem::xyz", ReadPointCloudInMemoryFromXYZ},
54  };
55 
56 static const std::unordered_map<
57  std::string,
58  std::function<bool(const std::string &,
59  const ccPointCloud &,
60  const WritePointCloudOption &)>>
62  {"xyz", WritePointCloudToXYZ},
63  {"txt", WritePointCloudToXYZ},
64  {"xyzn", WritePointCloudToXYZN},
65  {"xyzrgb", WritePointCloudToXYZRGB},
66  {"ply", WritePointCloudToPLY},
67  {"pcd", WritePointCloudToPCD},
68  {"pts", WritePointCloudToPTS},
69  {"ptx", AutoWriteEntity},
70  {"vtk", AutoWriteEntity},
71  {"bin", AutoWriteEntity},
72  };
73 
74 static const std::unordered_map<
75  std::string,
76  std::function<bool(unsigned char *&,
77  size_t &,
78  const ccPointCloud &,
79  const WritePointCloudOption &)>>
81  {"mem::xyz", WritePointCloudInMemoryToXYZ},
82  };
83 
84 std::shared_ptr<ccPointCloud> CreatePointCloudFromFile(
85  const std::string &filename,
86  const std::string &format,
87  bool print_progress) {
88  auto pointcloud = std::make_shared<ccPointCloud>("pointCloud");
89  ReadPointCloud(filename, *pointcloud, {format, true, true, print_progress});
90  return pointcloud;
91 }
92 
93 std::shared_ptr<ccPointCloud> CreatePointCloudFromMemory(
94  const unsigned char *buffer,
95  const size_t length,
96  const std::string &format,
97  bool print_progress) {
98  auto pointcloud = std::make_shared<ccPointCloud>();
99  ReadPointCloud(buffer, length, *pointcloud,
100  {format, true, true, print_progress});
101  return pointcloud;
102 }
103 
104 bool ReadPointCloud(const std::string &filename,
105  ccPointCloud &pointcloud,
106  const ReadPointCloudOption &params) {
107  std::string format = params.format;
108  if (format == "auto") {
110  filename);
111  }
112 
113  cloudViewer::utility::LogDebug("Format {} File {}", params.format,
114  filename);
115 
117  if (map_itr == file_extension_to_pointcloud_read_function.end()) {
119  "Read ccPointCloud failed: unknown file extension for "
120  "{} (format: {}).",
121  filename, params.format);
122  return false;
123  }
124  bool success = map_itr->second(filename, pointcloud, params);
125  cloudViewer::utility::LogDebug("Read ccPointCloud: {:d} vertices.",
126  pointcloud.size());
127  if (params.remove_nan_points || params.remove_infinite_points) {
128  pointcloud.RemoveNonFinitePoints(params.remove_nan_points,
129  params.remove_infinite_points);
130  }
131  return success;
132 }
133 
134 bool ReadPointCloud(const std::string &filename,
135  ccPointCloud &pointcloud,
136  const std::string &file_format,
137  bool remove_nan_points,
138  bool remove_infinite_points,
139  bool print_progress) {
140  std::string format = file_format;
141  if (format == "auto") {
143  filename);
144  }
145 
147  p.format = format;
148  p.remove_nan_points = remove_nan_points;
149  p.remove_infinite_points = remove_infinite_points;
151  std::string("Reading ") + cloudViewer::utility::ToUpper(format) +
152  " file: " + filename,
153  print_progress);
154  p.update_progress = progress_updater;
155  return ReadPointCloud(filename, pointcloud, p);
156 }
157 
158 bool ReadPointCloud(const unsigned char *buffer,
159  const size_t length,
160  ccPointCloud &pointcloud,
161  const ReadPointCloudOption &params) {
162  std::string format = params.format;
163  if (format == "auto") {
165  "Read ccPointCloud failed: unknown format for "
166  "(format: {}).",
167  params.format);
168  return false;
169  }
170 
171  cloudViewer::utility::LogDebug("Format {}", params.format);
172 
173  auto map_itr = in_memory_to_pointcloud_read_function.find(format);
174  if (map_itr == in_memory_to_pointcloud_read_function.end()) {
176  "Read ccPointCloud failed: unknown format for "
177  "(format: {}).",
178  params.format);
179  return false;
180  }
181  bool success = map_itr->second(buffer, length, pointcloud, params);
182  cloudViewer::utility::LogDebug("Read ccPointCloud: {} vertices.",
183  pointcloud.size());
184  if (params.remove_nan_points || params.remove_infinite_points) {
185  pointcloud.RemoveNonFinitePoints(params.remove_nan_points,
186  params.remove_infinite_points);
187  }
188  return success;
189 }
190 
191 bool WritePointCloud(const std::string &filename,
192  const ccPointCloud &pointcloud,
193  const WritePointCloudOption &params) {
194  std::string format =
196  filename);
198  if (map_itr == file_extension_to_pointcloud_write_function.end()) {
200  "Write ccPointCloud failed: unknown file extension {} "
201  "for file {}.",
202  format, filename);
203  return false;
204  }
205 
206  bool success = map_itr->second(filename, pointcloud, params);
207  cloudViewer::utility::LogDebug("Write ccPointCloud: {:d} vertices.",
208  pointcloud.size());
209  return success;
210 }
211 
212 bool WritePointCloud(const std::string &filename,
213  const ccPointCloud &pointcloud,
214  const std::string &file_format /* = "auto"*/,
215  bool write_ascii /* = false*/,
216  bool compressed /* = false*/,
217  bool print_progress) {
221  std::string format = file_format;
222  if (format == "auto") {
224  filename);
225  }
227  std::string("Writing ") + cloudViewer::utility::ToUpper(format) +
228  " file: " + filename,
229  print_progress);
230  p.update_progress = progress_updater;
231  return WritePointCloud(filename, pointcloud, p);
232 }
233 
234 bool WritePointCloud(unsigned char *&buffer,
235  size_t &length,
236  const ccPointCloud &pointcloud,
237  const WritePointCloudOption &params) {
238  std::string format = params.format;
239  if (format == "auto") {
241  "Write ccPointCloud failed: unknown format for "
242  "(format: {}).",
243  params.format);
244  return false;
245  }
246  auto map_itr = in_memory_to_pointcloud_write_function.find(format);
247  if (map_itr == in_memory_to_pointcloud_write_function.end()) {
249  "Write ccPointCloud failed: unknown format {}.", format);
250  return false;
251  }
252 
253  bool success = map_itr->second(buffer, length, pointcloud, params);
254  cloudViewer::utility::LogDebug("Write ccPointCloud: {} vertices.",
255  pointcloud.size());
256  return success;
257 }
258 
259 } // namespace io
260 } // namespace cloudViewer
IsAscii write_ascii
Compressed compressed
std::string filename
filament::Texture::InternalFormat format
cmdLineReadable * params[]
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
ccPointCloud & RemoveNonFinitePoints(bool remove_nan=true, bool remove_infinite=true)
Remove all points fromt he point cloud that have a nan entry, or infinite entries.
unsigned size() const override
Definition: PointCloudTpl.h:38
#define LogWarning(...)
Definition: Logging.h:72
#define LogDebug(...)
Definition: Logging.h:90
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
Helper functions for the ml ops.
bool ReadPointCloudFromPLY(const std::string &filename, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
bool ReadPointCloudFromXYZN(const std::string &filename, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
bool WritePointCloudToPCD(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
bool AutoReadEntity(const std::string &filename, ccHObject &entity, const ReadPointCloudOption &params)
static const std::unordered_map< std::string, std::function< bool(const std::string &, ccPointCloud &, const ReadPointCloudOption &)> > file_extension_to_pointcloud_read_function
bool WritePointCloudToPTS(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
bool ReadPointCloudFromPCD(const std::string &filename, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
static const std::unordered_map< std::string, std::function< bool(const std::string &, const ccPointCloud &, const WritePointCloudOption &)> > file_extension_to_pointcloud_write_function
bool ReadPointCloudFromXYZ(const std::string &filename, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
bool ReadPointCloud(const std::string &filename, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
bool ReadPointCloudFromXYZRGB(const std::string &filename, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
bool AutoWriteEntity(const std::string &filename, const ccHObject &entity, const WritePointCloudOption &params)
bool WritePointCloud(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
static const std::unordered_map< std::string, std::function< bool(const unsigned char *, const size_t, ccPointCloud &, const ReadPointCloudOption &)> > in_memory_to_pointcloud_read_function
bool WritePointCloudToXYZ(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
static const std::unordered_map< std::string, std::function< bool(unsigned char *&, size_t &, const ccPointCloud &, const WritePointCloudOption &)> > in_memory_to_pointcloud_write_function
bool WritePointCloudToPLY(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
bool WritePointCloudToXYZN(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
std::shared_ptr< ccPointCloud > CreatePointCloudFromFile(const std::string &filename, const std::string &format, bool print_progress)
bool WritePointCloudToXYZRGB(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
bool WritePointCloudInMemoryToXYZ(unsigned char *&buffer, size_t &length, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
bool ReadPointCloudFromPTS(const std::string &filename, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
std::shared_ptr< ccPointCloud > CreatePointCloudFromMemory(const unsigned char *buffer, const size_t length, const std::string &format, bool print_progress)
bool ReadPointCloudInMemoryFromXYZ(const unsigned char *buffer, const size_t length, ccPointCloud &pointcloud, const ReadPointCloudOption &params)
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:281
std::string ToUpper(const std::string &s)
Convert string to the upper case.
Definition: Helper.cpp:249
Generic file read and write utility for python interface.
Optional parameters to ReadPointCloud.
Definition: FileIO.h:39
bool remove_nan_points
Whether to remove all points that have nan.
Definition: FileIO.h:62
std::function< bool(double)> update_progress
Definition: FileIO.h:72
bool remove_infinite_points
Whether to remove all points that have +-inf.
Definition: FileIO.h:64
Optional parameters to WritePointCloud.
Definition: FileIO.h:77
std::function< bool(double)> update_progress
Definition: FileIO.h:135
update_progress(double percent) functor for ConsoleProgressBar