-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve tensor point cloud io #5404
Conversation
Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 5 of 6 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 12 unresolved discussions (waiting on @ZhengyuDiao)
cpp/open3d/t/io/file_format/FileTXT.cpp
line 59 at r2 (raw file):
int64_t num_points = file.GetNumLines(); int64_t num_elements_per_line = line_template.size();
num_elements_per_line -> num_attributes
if (line_template == "xyz") {
num_attributes = 3;
} else if (line_template == "xyzi") {
num_attributes = 4;
} else if (line_template == "xyzn") {
num_attributes = 6;
}
...
cpp/open3d/t/io/file_format/FileTXT.cpp
line 66 at r2 (raw file):
int i = 0; double x, y, z, attr, attr_x, attr_y, attr_z;
double line_attributes[num_attributes];
// or
std::vector<double> line_attributes(num_attributes);
cpp/open3d/t/io/file_format/FileTXT.cpp
line 70 at r2 (raw file):
// Read TXT to buffer while ((line_buffer = file.ReadLine())) {
while (line_buffer = file.ReadLine())
Code quote:
while ((line_buffer = file.ReadLine()))
cpp/open3d/t/io/file_format/FileTXT.cpp
line 95 at r2 (raw file):
utility::LogWarning("Read TXT failed at line: {}", line_buffer); return false; }
std::memcpy(&pcd_buffer_ptr[num_attributes * i], line_attributes, num_attributes * sizeof(double));
cpp/open3d/t/io/file_format/FileTXT.cpp
line 103 at r2 (raw file):
// Buffer to point cloud pointcloud.SetPointPositions(pcd_buffer.Slice(1, 0, 3, 1)); if (line_template == std::vector<std::string>{"x", "y", "z"})
if (line_template == "xyz")
cpp/open3d/t/io/file_format/FileTXT.cpp
line 104 at r2 (raw file):
pointcloud.SetPointPositions(pcd_buffer.Slice(1, 0, 3, 1)); if (line_template == std::vector<std::string>{"x", "y", "z"}) ;
{
// No additional attributes;
}
cpp/open3d/t/io/file_format/FileTXT.cpp
line 106 at r2 (raw file):
; else if (line_template == std::vector<std::string>{"x", "y", "z", "i"}) { pointcloud.SetPointAttr("intensities", pcd_buffer.Slice(1, 3, 4, 1));
pcd_buffer.Slice(1, 3, 4, 1).Contiguous()
cpp/open3d/t/io/file_format/FileTXT.cpp
line 125 at r2 (raw file):
const open3d::io::ReadPointCloudOption ¶ms) { try { std::string formatTXT =
format_txt
cpp/open3d/t/io/file_format/FileTXT.cpp
line 129 at r2 (raw file):
if (formatTXT == "xyz") { return ReadFileTXT(filename, pointcloud, std::vector<std::string>{"x", "y", "z"}, params);
{"x", "y", "z"}
without std::vector<std::string>
already works
cpp/open3d/t/io/file_format/FileTXT.cpp
line 166 at r2 (raw file):
} utility::CountingProgressReporter reporter(params.update_progress); const core::Tensor &points = pointcloud.GetPointPositions();
What if the point cloud is CUDA? Need to call .To(Device("CPU:0"))
.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 181 at r2 (raw file):
for (int i = 0; i < points.GetShape(0); i++) { if (fprintf(file.GetFILE(), "%.10f %.10f %.10f\n", points[i][0].Item<double>(),
Do not use Item
as it is slow. First, get the buffer of points
and use pointers.
Code quote:
pcd_buffer_ptr
cpp/open3d/t/io/file_format/FileTXT.cpp
line 181 at r2 (raw file):
for (int i = 0; i < points.GetShape(0); i++) { if (fprintf(file.GetFILE(), "%.10f %.10f %.10f\n", points[i][0].Item<double>(),
What if the tensor is not double? This may be wrong.
Code quote:
Item<double>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 5 of 6 files reviewed, 12 unresolved discussions (waiting on @yxlao)
cpp/open3d/t/io/file_format/FileTXT.cpp
line 59 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
num_elements_per_line -> num_attributes
if (line_template == "xyz") { num_attributes = 3; } else if (line_template == "xyzi") { num_attributes = 4; } else if (line_template == "xyzn") { num_attributes = 6; } ...
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 66 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
double line_attributes[num_attributes]; // or std::vector<double> line_attributes(num_attributes);
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 70 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
while (line_buffer = file.ReadLine())
I try , but the compiler would report an error if removing brackets.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 95 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
std::memcpy(&pcd_buffer_ptr[num_attributes * i], line_attributes, num_attributes * sizeof(double));
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 103 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
if (line_template == "xyz")
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 104 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
{ // No additional attributes; }
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 106 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
pcd_buffer.Slice(1, 3, 4, 1).Contiguous()
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 125 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
format_txt
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 129 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
{"x", "y", "z"}
withoutstd::vector<std::string>
already works
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 166 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
What if the point cloud is CUDA? Need to call
.To(Device("CPU:0"))
.
We just get the value of pointcloud and write txt file, still need to convert it to cpu?
cpp/open3d/t/io/file_format/FileTXT.cpp
line 181 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
Do not use
Item
as it is slow. First, get the buffer ofpoints
and use pointers.
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 181 at r2 (raw file):
Previously, yxlao (Yixing Lao) wrote…
What if the tensor is not double? This may be wrong.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r3, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @yxlao and @ZhengyuDiao)
cpp/open3d/t/io/file_format/FileTXT.cpp
line 82 at r3 (raw file):
int i = 0; double *line_attr_ptr = (double *)malloc(num_attributes * sizeof(double));
malloc
needs free. Prefer not to use malloc
when possible. E.g. we can use vector
and get data
from it, or just use an array.
std::vector<float> line_attrs(num_attributes);
float *line_attr_ptr = line_attrs.data();
num_attributes = 3; | ||
} else if (format_txt == "xyzi") { | ||
num_attributes = 4; | ||
} else if ((format_txt == "xyzn") || (format_txt == "xyzrgb")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else if (format_txt == "xyzn") {
num_attributes = 6;
} else if (format_txt == "xyzrgb") {
num_attributes = 6;
is more clear
} else if ((format_txt == "xyzn") || (format_txt == "xyzrgb")) { | ||
num_attributes = 6; | ||
} else { | ||
utility::LogWarning("The format of TXT is not supported"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Print the name as well:
utility::LogWarning("The format of {} is not supported", format_txt);
Same for the rest.
return false; | ||
} | ||
|
||
core::Tensor pcd_buffer({num_points, num_attributes}, core::Float64); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about it again, it is best to keep everything Float32
by default. This is because all creation APIs of the tensor TriangleMesh and PointCould uses Float32. Could you change everything here to float32?
double *pcd_buffer_ptr = pcd_buffer.GetDataPtr<double>(); | ||
|
||
int i = 0; | ||
double *line_attr_ptr = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By vector, you can do something like this. This will avoid malloc and free.
std::vector<float> line_attrs(num_attributes);
float *line_attr_ptr = line_attrs.data();
utility::filesystem::GetFileExtensionInLowerCase(filename); | ||
|
||
int64_t num_points = file.GetNumLines(); | ||
int64_t num_attributes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: since we are using attr
in the rest of the code, we can also rename this to num_attrs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 5 of 6 files reviewed, 6 unresolved discussions (waiting on @yxlao)
cpp/open3d/t/io/file_format/FileTXT.cpp
line 70 at r3 (raw file):
Previously, yxlao (Yixing Lao) wrote…
} else if (format_txt == "xyzn") { num_attributes = 6; } else if (format_txt == "xyzrgb") { num_attributes = 6;
is more clear
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 73 at r3 (raw file):
Previously, yxlao (Yixing Lao) wrote…
Print the name as well:
utility::LogWarning("The format of {} is not supported", format_txt);
Same for the rest.
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 81 at r3 (raw file):
Previously, yxlao (Yixing Lao) wrote…
By vector, you can do something like this. This will avoid malloc and free.
std::vector<float> line_attrs(num_attributes); float *line_attr_ptr = line_attrs.data();
Done.
cpp/open3d/t/io/file_format/FileTXT.cpp
line 82 at r3 (raw file):
Previously, yxlao (Yixing Lao) wrote…
malloc
needs free. Prefer not to usemalloc
when possible. E.g. we can usevector
and getdata
from it, or just use an array.
Done.
Now the other files io in tensor module use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r4.
Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @ZhengyuDiao)
This change is