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

PLYReader fails to read without extra line at the end nor comment #3487

Closed
jasjuang opened this issue Nov 25, 2019 · 2 comments · Fixed by #3542
Closed

PLYReader fails to read without extra line at the end nor comment #3487

jasjuang opened this issue Nov 25, 2019 · 2 comments · Fixed by #3542

Comments

@jasjuang
Copy link
Contributor

jasjuang commented Nov 25, 2019

Your Environment

  • Operating System and version: Ubuntu 18.04
  • Compiler: GCC 7
  • PCL Version: Latest master

Context

PLYReader fails to read ply file in a few simple scenarios.

Code to Reproduce

int main(){
  pcl::PointCloud<pcl::PointXYZRGB> cld;

  pcl::PLYReader Reader;
  Reader.read("test1.ply", cld); // success
  // Reader.read("test2.ply", cld); // fail
  // Reader.read("test3.ply", cld); // fail
  // Reader.read("test4.ply", cld); // fail

  return 0;
}

test1.ply is the only scenario that it will read successfully

ply
format ascii 1.0
comment PCL generated
element vertex 3
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
element camera 1
property float view_px
property float view_py
property float view_pz
property float x_axisx
property float x_axisy
property float x_axisz
property float y_axisx
property float y_axisy
property float y_axisz
property float z_axisx
property float z_axisy
property float z_axisz
property float focal
property float scalex
property float scaley
property float centerx
property float centery
property int viewportx
property int viewporty
property float k1
property float k2
end_header
127.97639 -267.0896 -900.87177 66 107 172
128.3494 -267.11069 -900.89008 65 103 172
128.06009 -267.20074 -901.63428 68 120 175
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 3 1 0 0

If we simply delete the last empty line from test1.ply to become test2.ply it fails

ply
format ascii 1.0
comment PCL generated
element vertex 3
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
element camera 1
property float view_px
property float view_py
property float view_pz
property float x_axisx
property float x_axisy
property float x_axisz
property float y_axisx
property float y_axisy
property float y_axisz
property float z_axisx
property float z_axisy
property float z_axisz
property float focal
property float scalex
property float scaley
property float centerx
property float centery
property int viewportx
property int viewporty
property float k1
property float k2
end_header
127.97639 -267.0896 -900.87177 66 107 172
128.3494 -267.11069 -900.89008 65 103 172
128.06009 -267.20074 -901.63428 68 120 175
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 3 1 0 0

If we add a comment at the end as shown in test3.ply it also fails

ply
format ascii 1.0
comment PCL generated
element vertex 3
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
element camera 1
property float view_px
property float view_py
property float view_pz
property float x_axisx
property float x_axisy
property float x_axisz
property float y_axisx
property float y_axisy
property float y_axisz
property float z_axisx
property float z_axisy
property float z_axisz
property float focal
property float scalex
property float scaley
property float centerx
property float centery
property int viewportx
property int viewporty
property float k1
property float k2
end_header
127.97639 -267.0896 -900.87177 66 107 172
128.3494 -267.11069 -900.89008 65 103 172
128.06009 -267.20074 -901.63428 68 120 175
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 3 1 0 0
comment test

Even if we add another empty line after the comment it still fails as shown in test4.ply

ply
format ascii 1.0
comment PCL generated
element vertex 3
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
element camera 1
property float view_px
property float view_py
property float view_pz
property float x_axisx
property float x_axisy
property float x_axisz
property float y_axisx
property float y_axisy
property float y_axisz
property float z_axisx
property float z_axisy
property float z_axisz
property float focal
property float scalex
property float scaley
property float centerx
property float centery
property int viewportx
property int viewporty
property float k1
property float k2
end_header
127.97639 -267.0896 -900.87177 66 107 172
128.3494 -267.11069 -900.89008 65 103 172
128.06009 -267.20074 -901.63428 68 120 175
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 3 1 0 0
comment test

I can open all 4 scenarios without a problem in meshlab, and I think it should be reasonable to expect PCL to be able to handle this. For the 3 failure cases, the error message is all the same as below

[pcl::PLYReader] /home/jasjuang/test2.ply:37: parse error
[pcl::PLYReader::read] problem parsing header!
[pcl::PLYReader::read] problem parsing header!

When I look into the source code here, there are multiple places that will trigger "parse error", can someone give me pointers on where should I look into in order to fix this problem?

@taketwo
Copy link
Member

taketwo commented Nov 26, 2019

I have virtually the same environment, but test2.ply does not fail for me.

Tests 3 and 4 indeed fail, however, I'm not sure if this is a bug. According to Wikipedia:

Comments may be placed in the header by using the word comment at the start of the line. Everything from there until the end of the line should then be ignored.

For me it reads as if comments are only allowed in the header, not in the "body" of the file.

@jasjuang
Copy link
Contributor Author

@taketwo are you creating test2.ply through vim? If you paste it in vim, even though there's no extra line at the end, after you wq it will create an extra line for you. If you create test2.ply through sublime, you will be able to recreate the problem.

It does sound like comment is intended for the header only, but somehow Meshlab is able to read it correctly so I think there is still a chance that this is valid. The reason I need this is that I want to embed a unit in the comment, for example,

comment unit m

or

comment unit cm

or

comment unit mm

Currently, I am doing it in a hacky way like the below

  pcl::PointCloud<pcl::PointXYZRGB> cld;
  
  // some processing

  pcl::PLYWriter plywriter;
  plywriter.write("test.ply", cld);

  std::ofstream plystream;
  plystream.open("test.ply", std::ios_base::app);

  plystream << "comment unit mm";

Appending at the end is the only way to not having to write the file 2 times.

Do you think it's easier to change the plywriter write function to support writing custom comments, or is it easier to make plyreader to not fail if there's a comment at the end?

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 a pull request may close this issue.

2 participants