Skip to content
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

Merged
merged 14 commits into from
Aug 10, 2022
Merged

Improve tensor point cloud io #5404

merged 14 commits into from
Aug 10, 2022

Conversation

ZhengyuDiao
Copy link
Contributor

@ZhengyuDiao ZhengyuDiao commented Aug 4, 2022

This change is Reviewable

@update-docs
Copy link

update-docs bot commented Aug 4, 2022

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

Copy link
Collaborator

@yxlao yxlao left a 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 &params) {
    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>

Copy link
Contributor Author

@ZhengyuDiao ZhengyuDiao left a 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"} without std::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 of points 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.

Copy link
Collaborator

@yxlao yxlao left a 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")) {
Copy link
Collaborator

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");
Copy link
Collaborator

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);
Copy link
Collaborator

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 =
Copy link
Collaborator

@yxlao yxlao Aug 6, 2022

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;
Copy link
Collaborator

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

Copy link
Contributor Author

@ZhengyuDiao ZhengyuDiao left a 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 use malloc when possible. E.g. we can use vector and get data from it, or just use an array.

Done.

@ZhengyuDiao
Copy link
Contributor Author

ZhengyuDiao commented Aug 7, 2022

Now the other files io in tensor module use double as default, so the tests of io also use double as default. If I use float as default in FileTXT, it cannot pass the tests.
I think we may keep double as default this time and modify the default dtype from double to float in the relevant files in next pr. What do you think about it? @yxlao

Copy link
Collaborator

@yxlao yxlao left a 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)

@yxlao yxlao merged commit b40282c into master Aug 10, 2022
@yxlao yxlao deleted the zhengyu/t_pcd_io branch August 10, 2022 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants