38 int64_t num_points = 0;
39 const char *line_buffer;
40 if ((line_buffer = file.
ReadLine())) {
42 if (std::is_same<int64_t, long>::value) {
47 sscanf(line_buffer,
format, &num_points);
51 "Read PTS failed: number of points must be >= 0.");
53 }
else if (num_points == 0) {
61 int64_t start_pos = ftell(file.
GetFILE());
63 float *points_ptr =
nullptr;
64 float *intensities_ptr =
nullptr;
65 uint8_t *colors_ptr =
nullptr;
66 size_t num_fields = 0;
68 if ((line_buffer = file.
ReadLine())) {
72 if (num_fields == 7) {
86 else if (num_fields == 6) {
95 else if (num_fields == 4) {
102 intensities_ptr = pointcloud.
GetPointAttr(
"intensities")
103 .GetDataPtr<
float>();
106 else if (num_fields == 3) {
118 fseek(file.
GetFILE(), start_pos, 0);
121 while (idx < num_points && (line_buffer = file.
ReadLine())) {
125 if (num_fields == 7 && (sscanf(line_buffer,
"%f %f %f %f %d %d %d",
126 &x, &y, &z, &i, &r, &g, &b) == 7)) {
127 points_ptr[3 * idx + 0] = x;
128 points_ptr[3 * idx + 1] = y;
129 points_ptr[3 * idx + 2] = z;
130 intensities_ptr[idx] = i;
131 colors_ptr[3 * idx + 0] = r;
132 colors_ptr[3 * idx + 1] = g;
133 colors_ptr[3 * idx + 2] = b;
136 else if (num_fields == 6 &&
137 (sscanf(line_buffer,
"%f %f %f %d %d %d", &x, &y, &z, &r,
139 points_ptr[3 * idx + 0] = x;
140 points_ptr[3 * idx + 1] = y;
141 points_ptr[3 * idx + 2] = z;
142 colors_ptr[3 * idx + 0] = r;
143 colors_ptr[3 * idx + 1] = g;
144 colors_ptr[3 * idx + 2] = b;
147 else if (num_fields == 4 && (sscanf(line_buffer,
"%f %f %f %f", &x,
149 points_ptr[3 * idx + 0] = x;
150 points_ptr[3 * idx + 1] = y;
151 points_ptr[3 * idx + 2] = z;
152 intensities_ptr[idx] = i;
155 else if (num_fields == 3 &&
156 sscanf(line_buffer,
"%f %f %f", &x, &y, &z) == 3) {
157 points_ptr[3 * idx + 0] = x;
158 points_ptr[3 * idx + 1] = y;
159 points_ptr[3 * idx + 2] = z;
165 if (idx % 1000 == 0) {
172 }
catch (
const std::exception &e) {
186 color_values = color_in;
203 int64_t num_points = 0;
225 if (fprintf(file.
GetFILE(),
"%zu\r\n", (
size_t)num_points) < 0) {
231 const float *points_ptr =
nullptr;
232 const float *intensities_ptr =
nullptr;
233 const uint8_t *colors_ptr =
nullptr;
236 if (num_points > 0) {
246 colors_ptr =
colors.GetDataPtr<uint8_t>();
247 intensities_ptr = pointcloud.
GetPointAttr(
"intensities")
249 .GetDataPtr<
float>();
250 for (
int i = 0; i < num_points; i++) {
252 "%.10f %.10f %.10f %.10f %d %d %d\r\n",
253 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
254 points_ptr[3 * i + 2], intensities_ptr[i],
255 colors_ptr[3 * i + 0], colors_ptr[3 * i + 1],
256 colors_ptr[3 * i + 2]) < 0) {
258 "Write PTS failed: unable to write file: {}",
271 colors_ptr =
colors.GetDataPtr<uint8_t>();
272 for (
int i = 0; i < num_points; i++) {
273 if (fprintf(file.
GetFILE(),
"%.10f %.10f %.10f %d %d %d\r\n",
274 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
275 points_ptr[3 * i + 2], colors_ptr[3 * i + 0],
276 colors_ptr[3 * i + 1], colors_ptr[3 * i + 2]) < 0) {
278 "Write PTS failed: unable to write file: {}",
290 intensities_ptr = pointcloud.
GetPointAttr(
"intensities")
292 .GetDataPtr<
float>();
293 for (
int i = 0; i < num_points; i++) {
294 if (fprintf(file.
GetFILE(),
"%.10f %.10f %.10f %.10f\r\n",
295 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
296 points_ptr[3 * i + 2], intensities_ptr[i]) < 0) {
298 "Write PTS failed: unable to write file: {}",
310 for (
int i = 0; i < num_points; i++) {
311 if (fprintf(file.
GetFILE(),
"%.10f %.10f %.10f\r\n",
312 points_ptr[3 * i + 0], points_ptr[3 * i + 1],
313 points_ptr[3 * i + 2]) < 0) {
315 "Write PTS failed: unable to write file: {}",
328 }
catch (
const std::exception &e) {
filament::Texture::InternalFormat format
cmdLineReadable * params[]
#define AssertTensorShape(tensor,...)
int64_t GetLength() const
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Tensor Clip(Scalar min_val, Scalar max_val) const
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Tensor To(Dtype dtype, bool copy=false) const
A point cloud contains a list of 3D points.
bool HasPointPositions() const
void SetPointColors(const core::Tensor &value)
Set the value of the "colors" attribute. Convenience function.
bool HasPointAttr(const std::string &key) const
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.
bool IsEmpty() const override
Returns !HasPointPositions().
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)
core::Tensor & GetPointColors()
Get the value of the "colors" attribute. Convenience function.
bool HasPointColors() const
void SetTotal(int64_t total)
bool Update(int64_t count)
bool Open(const std::string &filename, const std::string &mode)
Open a file.
FILE * GetFILE()
Returns the underlying C FILE pointer.
Helper functions for the ml ops.
bool ReadPointCloudFromPTS(const std::string &filename, geometry::PointCloud &pointcloud, const ReadPointCloudOption ¶ms)
bool WritePointCloudToPTS(const std::string &filename, const geometry::PointCloud &pointcloud, const WritePointCloudOption ¶ms)
core::Tensor ConvertColorTensorToUint8(const core::Tensor &color_in)
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters=" ", bool trim_empty_str=true)
Generic file read and write utility for python interface.
Optional parameters to ReadPointCloud.
Optional parameters to WritePointCloud.