43 std::string format_txt =
49 if (format_txt ==
"xyz") {
51 }
else if (format_txt ==
"xyzd") {
53 }
else if (format_txt ==
"xyzi") {
55 }
else if (format_txt ==
"xyzn") {
57 }
else if (format_txt ==
"xyzrgb") {
66 float *pcd_buffer_ptr = pcd_buffer.
GetDataPtr<
float>();
69 std::vector<float> line_attrs(num_attrs);
70 float *line_attr_ptr = line_attrs.data();
71 const char *line_buffer;
74 while ((line_buffer = file.
ReadLine())) {
76 (sscanf(line_buffer,
"%f %f %f", &line_attr_ptr[0],
77 &line_attr_ptr[1], &line_attr_ptr[2]) == 3)) {
78 std::memcpy(&pcd_buffer_ptr[num_attrs * i], line_attr_ptr,
79 num_attrs *
sizeof(
float));
80 }
else if (num_attrs == 4 &&
81 (sscanf(line_buffer,
"%f %f %f %f", &line_attr_ptr[0],
82 &line_attr_ptr[1], &line_attr_ptr[2],
83 &line_attr_ptr[3]) == 4)) {
84 std::memcpy(&pcd_buffer_ptr[num_attrs * i], line_attr_ptr,
85 num_attrs *
sizeof(
float));
86 }
else if (num_attrs == 6 &&
87 (sscanf(line_buffer,
"%f %f %f %f %f %f",
88 &line_attr_ptr[0], &line_attr_ptr[1],
89 &line_attr_ptr[2], &line_attr_ptr[3],
90 &line_attr_ptr[4], &line_attr_ptr[5]) == 6)) {
91 std::memcpy(&pcd_buffer_ptr[num_attrs * i], line_attr_ptr,
92 num_attrs *
sizeof(
float));
97 if (++i % 1000 == 0) {
104 if (format_txt ==
"xyz") {
106 }
else if (format_txt ==
"xyzd") {
108 pcd_buffer.Slice(1, 3, 4, 1).Contiguous());
109 }
else if (format_txt ==
"xyzi") {
111 pcd_buffer.Slice(1, 3, 4, 1).Contiguous());
112 }
else if (format_txt ==
"xyzn") {
114 pcd_buffer.Slice(1, 3, 6, 1).Contiguous());
115 }
else if (format_txt ==
"xyzrgb") {
117 pcd_buffer.Slice(1, 3, 6, 1).Contiguous());
126 }
catch (
const std::exception &e) {
145 std::string format_txt =
149 if (!
points.GetShape().IsCompatible({utility::nullopt, 3})) {
151 "Write TXT failed: Shape of points is {}, but it should "
157 int64_t num_points =
points.GetShape(0);
161 if (format_txt ==
"xyz") {
163 len_attr = num_points;
164 }
else if (format_txt ==
"xyzd") {
165 len_attr = pointcloud.
GetPointAttr(
"dopplers").GetLength();
166 }
else if (format_txt ==
"xyzi") {
167 len_attr = pointcloud.
GetPointAttr(
"intensities").GetLength();
168 }
else if (format_txt ==
"xyzn") {
169 len_attr = pointcloud.
GetPointAttr(
"normals").GetLength();
170 }
else if (format_txt ==
"xyzrgb") {
171 len_attr = pointcloud.
GetPointAttr(
"colors").GetLength();
178 if (len_attr != num_points) {
180 "Write TXT failed: Points ({}) and attributes ({}) have "
181 "different lengths.",
182 num_points, len_attr);
186 const float *points_ptr =
points.GetDataPtr<
float>();
187 const float *attr_ptr =
nullptr;
189 if (format_txt ==
"xyz") {
190 for (
int i = 0; i < num_points; i++) {
191 if (fprintf(file.
GetFILE(),
"%.10f %.10f %.10f\n",
192 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
193 points_ptr[3 * i + 2]) < 0) {
195 "Write TXT failed: unable to write file: {}",
203 }
else if (format_txt ==
"xyzd") {
204 attr_ptr = pointcloud.
GetPointAttr(
"dopplers").GetDataPtr<
float>();
205 for (
int i = 0; i < num_points; i++) {
206 if (fprintf(file.
GetFILE(),
"%.10f %.10f %.10f %.10f\n",
207 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
208 points_ptr[3 * i + 2], attr_ptr[i]) < 0) {
210 "Write TXT failed: unable to write file: {}",
218 }
else if (format_txt ==
"xyzi") {
220 pointcloud.
GetPointAttr(
"intensities").GetDataPtr<
float>();
221 for (
int i = 0; i < num_points; i++) {
222 if (fprintf(file.
GetFILE(),
"%.10f %.10f %.10f %.10f\n",
223 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
224 points_ptr[3 * i + 2], attr_ptr[i]) < 0) {
226 "Write TXT failed: unable to write file: {}",
234 }
else if (format_txt ==
"xyzn") {
235 attr_ptr = pointcloud.
GetPointAttr(
"normals").GetDataPtr<
float>();
236 for (
int i = 0; i < num_points; i++) {
238 "%.10f %.10f %.10f %.10f %.10f %.10f\n",
239 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
240 points_ptr[3 * i + 2], attr_ptr[3 * i + 0],
241 attr_ptr[3 * i + 1], attr_ptr[3 * i + 2]) < 0) {
243 "Write TXT failed: unable to write file: {}",
251 }
else if (format_txt ==
"xyzrgb") {
252 attr_ptr = pointcloud.
GetPointAttr(
"colors").GetDataPtr<
float>();
253 for (
int i = 0; i < num_points; i++) {
255 "%.10f %.10f %.10f %.10f %.10f %.10f\n",
256 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
257 points_ptr[3 * i + 2], attr_ptr[3 * i + 0],
258 attr_ptr[3 * i + 1], attr_ptr[3 * i + 2]) < 0) {
260 "Write TXT failed: unable to write file: {}",
277 }
catch (
const std::exception &e) {
cmdLineReadable * params[]
A point cloud contains a list of 3D points.
void SetPointPositions(const core::Tensor &value)
Set the value of the "positions" attribute. Convenience function.
core::Tensor & GetPointPositions()
Get the value of the "positions" attribute. Convenience function.
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
PointCloud & Clear() override
Clear all data in the point cloud.
void SetPointAttr(const std::string &key, const core::Tensor &value)
void SetTotal(int64_t total)
bool Update(int64_t count)
bool Open(const std::string &filename, const std::string &mode)
Open a file.
int64_t CurPos()
Returns current position in the file (ftell).
FILE * GetFILE()
Returns the underlying C FILE pointer.
int64_t GetFileSize()
Returns the file size in bytes.
int64_t GetNumLines()
Returns the number of lines in the file.
static const std::string path
bool ReadPointCloudFromTXT(const std::string &filename, geometry::PointCloud &pointcloud, const cloudViewer::io::ReadPointCloudOption ¶ms)
bool WritePointCloudToTXT(const std::string &filename, const geometry::PointCloud &pointcloud, const cloudViewer::io::WritePointCloudOption ¶ms)
cloudViewer::io::FileGeometry ReadFileGeometryTypeTXT(const std::string &path)
std::string GetFileExtensionInLowerCase(const std::string &filename)
Generic file read and write utility for python interface.
Optional parameters to ReadPointCloud.
Optional parameters to WritePointCloud.