ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
FileXYZI.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 <FileSystem.h>
9 #include <Logging.h>
10 #include <ProgressReporters.h>
11 
12 #include <cstdio>
13 
14 #include "core/Dtype.h"
15 #include "core/Tensor.h"
16 #include "core/TensorList.h"
17 #include "io/FileFormatIO.h"
18 #include "t/io/PointCloudIO.h"
19 
20 namespace cloudViewer {
21 namespace t {
22 namespace io {
23 
25  const std::string &path) {
27 }
28 
30  const std::string &filename,
31  geometry::PointCloud &pointcloud,
33  try {
35  if (!file.Open(filename, "r")) {
37  "Read XYZI failed: unable to open file: {}", filename);
38  return false;
39  }
41  params.update_progress);
42  reporter.SetTotal(file.GetFileSize());
43  int64_t num_points = file.GetNumLines();
44 
45  pointcloud.Clear();
46  core::Tensor points({num_points, 3}, core::Float64);
47  core::Tensor intensities({num_points, 1}, core::Float64);
48  double *points_ptr = points.GetDataPtr<double>();
49  double *intensities_ptr = intensities.GetDataPtr<double>();
50 
51  int i = 0;
52  double x, y, z, I;
53  const char *line_buffer;
54  while ((line_buffer = file.ReadLine())) {
55  if (sscanf(line_buffer, "%lf %lf %lf %lf", &x, &y, &z, &I) == 4) {
56  points_ptr[3 * i + 0] = x;
57  points_ptr[3 * i + 1] = y;
58  points_ptr[3 * i + 2] = z;
59  intensities_ptr[i] = I;
60  }
61  if (++i % 1000 == 0) {
62  reporter.Update(file.CurPos());
63  }
64  }
65  pointcloud.SetPoints(points);
66  pointcloud.SetPointAttr("intensities", intensities);
67  reporter.Finish();
68 
69  return true;
70  } catch (const std::exception &e) {
71  cloudViewer::utility::LogWarning("Read XYZ failed with exception: {}",
72  e.what());
73  return false;
74  }
75 }
76 
78  const std::string &filename,
79  const geometry::PointCloud &pointcloud,
81  if (!pointcloud.HasPointAttr("intensities")) {
82  return false;
83  }
84 
85  try {
87  if (!file.Open(filename, "w")) {
89  "Write XYZI failed: unable to open file: {}", filename);
90  return false;
91  }
93  params.update_progress);
94  const core::Tensor &points = pointcloud.GetPoints();
95  if (!points.GetShape().IsCompatible(
96  {cloudViewer::utility::nullopt, 3})) {
98  "Write XYZI failed: Shape of points is {}, but it should "
99  "be Nx3.",
100  points.GetShape());
101  return false;
102  }
103  const core::Tensor &intensities =
104  pointcloud.GetPointAttr("intensities");
105  if (points.GetShape(0) != intensities.GetShape(0)) {
107  "Write XYZI failed: Points ({}) and intensities ({}) have "
108  "different lengths.",
109  points.GetShape(0), intensities.GetShape(0));
110  return false;
111  }
112  reporter.SetTotal(points.GetShape(0));
113 
114  for (int i = 0; i < points.GetShape(0); i++) {
115  if (fprintf(file.GetFILE(), "%.10f %.10f %.10f %.10f\n",
116  points[i][0].Item<double>(),
117  points[i][1].Item<double>(),
118  points[i][2].Item<double>(),
119  intensities[i][0].Item<double>()) < 0) {
121  "Write XYZI failed: unable to write file: {}",
122  filename);
123  return false; // error happened during writing.
124  }
125  if (i % 1000 == 0) {
126  reporter.Update(i);
127  }
128  }
129  reporter.Finish();
130  return true;
131  } catch (const std::exception &e) {
132  cloudViewer::utility::LogWarning("Write XYZI failed with exception: {}",
133  e.what());
134  return false;
135  }
136 }
137 
138 } // namespace io
139 } // namespace t
140 } // namespace cloudViewer
std::string filename
int points
cmdLineReadable * params[]
SizeVector GetShape() const
Definition: Tensor.h:1127
A point cloud contains a list of 3D points.
Definition: PointCloud.h:82
void SetPoints(const core::Tensor &value)
Definition: PointCloud.h:156
bool HasPointAttr(const std::string &key) const
Definition: PointCloud.h:207
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
Definition: PointCloud.h:111
PointCloud & Clear() override
Clear all data in the point cloud.
Definition: PointCloud.h:251
void SetPointAttr(const std::string &key, const core::Tensor &value)
Definition: PointCloud.h:177
bool Open(const std::string &filename, const std::string &mode)
Open a file.
Definition: FileSystem.cpp:739
int64_t CurPos()
Returns current position in the file (ftell).
Definition: FileSystem.cpp:760
FILE * GetFILE()
Returns the underlying C FILE pointer.
Definition: FileSystem.h:264
int64_t GetFileSize()
Returns the file size in bytes.
Definition: FileSystem.cpp:772
int64_t GetNumLines()
Returns the number of lines in the file.
Definition: FileSystem.cpp:793
#define LogWarning(...)
Definition: Logging.h:72
const Dtype Float64
Definition: Dtype.cpp:43
static const std::string path
Definition: PointCloud.cpp:59
cloudViewer::io::FileGeometry ReadFileGeometryTypeXYZI(const std::string &path)
Definition: FileXYZI.cpp:24
bool WritePointCloudToXYZI(const std::string &filename, const geometry::PointCloud &pointcloud, const cloudViewer::io::WritePointCloudOption &params)
Definition: FileXYZI.cpp:77
bool ReadPointCloudFromXYZI(const std::string &filename, geometry::PointCloud &pointcloud, const cloudViewer::io::ReadPointCloudOption &params)
Definition: FileXYZI.cpp:29
Generic file read and write utility for python interface.
Optional parameters to ReadPointCloud.
Definition: FileIO.h:39
Optional parameters to WritePointCloud.
Definition: FileIO.h:77