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 
9 
10 #include <FileSystem.h>
11 #include <Helper.h>
12 #include <Logging.h>
13 #include <ProgressReporters.h>
14 #include <ecvPointCloud.h>
15 
16 #include <iostream>
17 #include <unordered_map>
18 
21 
22 namespace cloudViewer {
23 namespace t {
24 namespace io {
25 
26 static const std::unordered_map<
27  std::string,
28  std::function<bool(const std::string &,
32  {"npz", ReadPointCloudFromNPZ},
33  {"xyz", ReadPointCloudFromTXT},
34  {"xyzd", ReadPointCloudFromTXT},
35  {"xyzi", ReadPointCloudFromTXT},
36  {"xyzn", ReadPointCloudFromTXT},
37  {"xyzrgb", ReadPointCloudFromTXT},
38  {"pcd", ReadPointCloudFromPCD},
39  {"ply", ReadPointCloudFromPLY},
40  {"splat", ReadPointCloudFromSPLAT},
41  {"pts", ReadPointCloudFromPTS},
42  };
43 
44 static const std::unordered_map<
45  std::string,
46  std::function<bool(const std::string &,
47  const geometry::PointCloud &,
50  {"npz", WritePointCloudToNPZ},
51  {"xyz", WritePointCloudToTXT},
52  {"xyzd", WritePointCloudToTXT},
53  {"xyzi", WritePointCloudToTXT},
54  {"xyzn", WritePointCloudToTXT},
55  {"xyzrgb", WritePointCloudToTXT},
56  {"pcd", WritePointCloudToPCD},
57  {"ply", WritePointCloudToPLY},
58  {"splat", WritePointCloudToSPLAT},
59  {"pts", WritePointCloudToPTS},
60  };
61 
62 std::shared_ptr<geometry::PointCloud> CreatePointCloudFromFile(
63  const std::string &filename,
64  const std::string &format,
65  bool print_progress) {
66  auto pointcloud = std::make_shared<geometry::PointCloud>();
67  ReadPointCloud(filename, *pointcloud,
68  {format, false, false, print_progress});
69  return pointcloud;
70 }
71 
72 bool ReadPointCloud(const std::string &filename,
73  geometry::PointCloud &pointcloud,
75  std::string format = params.format;
76  if (format == "auto") {
78  }
79 
80  utility::LogDebug("Format {} File {}", params.format, filename);
81 
82  bool success = false;
84  if (map_itr == file_extension_to_pointcloud_read_function.end()) {
86  "Read geometry::PointCloud failed: unknown file extension for "
87  "{} (format: {}).",
88  filename, params.format);
89  return false;
90  } else {
91  success = map_itr->second(filename, pointcloud, params);
92  if (params.remove_nan_points || params.remove_infinite_points) {
94  "remove_nan_points and remove_infinite_points options are "
95  "unimplemented.");
96  return false;
97  }
98  }
99 
101  "Read t::geometry::PointCloud with following attributes: ");
102  for (auto &kv : pointcloud.GetPointAttr()) {
103  utility::LogDebug(" {} [shape: {}, stride: {}, {}]", kv.first,
104  kv.second.GetShape().ToString(),
105  kv.second.GetStrides().ToString(),
106  kv.second.GetDtype().ToString());
107  }
108 
109  return success;
110 }
111 
112 bool ReadPointCloud(const std::string &filename,
113  geometry::PointCloud &pointcloud,
114  const std::string &file_format,
115  bool remove_nan_points,
116  bool remove_infinite_points,
117  bool print_progress) {
118  std::string format = file_format;
119  if (format == "auto") {
121  }
122 
124  p.format = format;
125  p.remove_nan_points = remove_nan_points;
126  p.remove_infinite_points = remove_infinite_points;
127  utility::ConsoleProgressUpdater progress_updater(
128  std::string("Reading ") + utility::ToUpper(format) +
129  " file: " + filename,
130  print_progress);
131  p.update_progress = progress_updater;
132  return ReadPointCloud(filename, pointcloud, p);
133 }
134 
135 bool WritePointCloud(const std::string &filename,
136  const geometry::PointCloud &pointcloud,
138  std::string format =
141  if (map_itr == file_extension_to_pointcloud_write_function.end()) {
142  // Use legacy writer with legacy point cloud conversion
143  cloudViewer::geometry::PointCloud legacy = pointcloud.ToLegacy();
145  }
146 
147  bool success = map_itr->second(
148  filename, pointcloud.To(core::Device("CPU:0")), params);
149  if (!pointcloud.IsEmpty()) {
150  utility::LogDebug("Write geometry::PointCloud: {:d} vertices.",
151  (int)pointcloud.GetPointPositions().GetLength());
152  } else {
153  utility::LogDebug("Write geometry::PointCloud: 0 vertices.");
154  }
155  return success;
156 }
157 
158 bool WritePointCloud(const std::string &filename,
159  const geometry::PointCloud &pointcloud,
160  bool write_ascii /* = false*/,
161  bool compressed /* = false*/,
162  bool print_progress) {
164  p.write_ascii =
166  p.compressed =
168  std::string format =
170  utility::ConsoleProgressUpdater progress_updater(
171  std::string("Writing ") + utility::ToUpper(format) +
172  " file: " + filename,
173  print_progress);
174  p.update_progress = progress_updater;
175  return WritePointCloud(filename, pointcloud, p);
176 }
177 
178 bool ReadPointCloudFromNPZ(const std::string &filename,
179  geometry::PointCloud &pointcloud,
180  const ReadPointCloudOption &params) {
181  // Required checks are performed in the pointcloud constructor itself.
182  pointcloud = geometry::PointCloud(ReadNpz(filename));
183  return true;
184 }
185 
186 bool WritePointCloudToNPZ(const std::string &filename,
187  const geometry::PointCloud &pointcloud,
188  const WritePointCloudOption &params) {
189  if (bool(params.write_ascii)) {
190  utility::LogError("PointCloud can't be saved in ASCII format as .npz.");
191  }
192  // TODO: When open3d NPZ io supports compression in future, update this.
193  if (bool(params.compressed)) {
195  "PointCloud can't be saved in compressed format as .npz.");
196  }
197 
198  WriteNpz(filename, pointcloud.GetPointAttr());
199  utility::LogDebug("Saved pointcloud has the following attributes:");
200  for (auto &kv : pointcloud.GetPointAttr()) {
201  utility::LogDebug(" {} [shape: {}, stride: {}, {}]", kv.first,
202  kv.second.GetShape().ToString(),
203  kv.second.GetStrides().ToString(),
204  kv.second.GetDtype().ToString());
205  }
206 
207  return true;
208 }
209 
210 } // namespace io
211 } // namespace t
212 } // 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.)
int64_t GetLength() const
Definition: Tensor.h:1125
A point cloud contains a list of 3D points.
Definition: PointCloud.h:82
core::Tensor & GetPointPositions()
Get the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:124
bool IsEmpty() const override
Returns !HasPointPositions().
Definition: PointCloud.h:257
PointCloud To(const core::Device &device, bool copy=false) const
Definition: PointCloud.cpp:112
cloudViewer::geometry::PointCloud ToLegacy() const
Convert to a legacy CloudViewer PointCloud.
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
Definition: PointCloud.h:111
#define LogWarning(...)
Definition: Logging.h:72
#define LogError(...)
Definition: Logging.h:60
#define LogDebug(...)
Definition: Logging.h:90
Helper functions for the ml ops.
::ccPointCloud PointCloud
Definition: PointCloud.h:19
bool WritePointCloud(const std::string &filename, const ccPointCloud &pointcloud, const WritePointCloudOption &params)
static const std::unordered_map< std::string, std::function< bool(const std::string &, geometry::PointCloud &, const cloudViewer::io::ReadPointCloudOption &)> > file_extension_to_pointcloud_read_function
bool ReadPointCloudFromTXT(const std::string &filename, geometry::PointCloud &pointcloud, const cloudViewer::io::ReadPointCloudOption &params)
Definition: FileTXT.cpp:27
std::unordered_map< std::string, core::Tensor > ReadNpz(const std::string &file_name)
Definition: NumpyIO.cpp:675
bool WritePointCloud(const std::string &filename, const geometry::PointCloud &pointcloud, const cloudViewer::io::WritePointCloudOption &params)
bool ReadPointCloudFromPTS(const std::string &filename, geometry::PointCloud &pointcloud, const ReadPointCloudOption &params)
Definition: FilePTS.cpp:23
bool WritePointCloudToTXT(const std::string &filename, const geometry::PointCloud &pointcloud, const cloudViewer::io::WritePointCloudOption &params)
Definition: FileTXT.cpp:132
bool WritePointCloudToPLY(const std::string &filename, const geometry::PointCloud &pointcloud, const cloudViewer::io::WritePointCloudOption &params)
Definition: FilePLY.cpp:355
std::shared_ptr< geometry::PointCloud > CreatePointCloudFromFile(const std::string &filename, const std::string &format, bool print_progress)
void WriteNpz(const std::string &file_name, const std::unordered_map< std::string, core::Tensor > &tensor_map)
Definition: NumpyIO.cpp:759
bool WritePointCloudToPTS(const std::string &filename, const geometry::PointCloud &pointcloud, const WritePointCloudOption &params)
Definition: FilePTS.cpp:191
bool WritePointCloudToPCD(const std::string &filename, const geometry::PointCloud &pointcloud, const WritePointCloudOption &params)
Definition: FilePCD.cpp:1178
bool ReadPointCloudFromSPLAT(const std::string &filename, geometry::PointCloud &pointcloud, const cloudViewer::io::ReadPointCloudOption &params)
Definition: FileSPLAT.cpp:84
bool ReadPointCloudFromPCD(const std::string &filename, t::geometry::PointCloud &pointcloud, const ReadPointCloudOption &params)
Definition: FilePCD.cpp:1140
bool WritePointCloudToSPLAT(const std::string &filename, const geometry::PointCloud &pointcloud, const cloudViewer::io::WritePointCloudOption &params)
Definition: FileSPLAT.cpp:207
bool ReadPointCloudFromNPZ(const std::string &filename, geometry::PointCloud &pointcloud, const ReadPointCloudOption &params)
static const std::unordered_map< std::string, std::function< bool(const std::string &, const geometry::PointCloud &, const cloudViewer::io::WritePointCloudOption &)> > file_extension_to_pointcloud_write_function
bool ReadPointCloud(const std::string &filename, geometry::PointCloud &pointcloud, const cloudViewer::io::ReadPointCloudOption &params)
bool ReadPointCloudFromPLY(const std::string &filename, geometry::PointCloud &pointcloud, const cloudViewer::io::ReadPointCloudOption &params)
Definition: FilePLY.cpp:176
bool WritePointCloudToNPZ(const std::string &filename, const geometry::PointCloud &pointcloud, const WritePointCloudOption &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