Pcl: [io] savePCDFileBinary doesn't check if (height*width) equals no. of points

Created on 22 Mar 2020  路  6Comments  路  Source: PointCloudLibrary/pcl

Describe the bug

pcl::io::savePCDFileBinary doesn't perform a check to see if
(height * width ) == no_of_points
before saving the file. Consequently, attempting to open the resultant PCD file in most applications will fail.

In contrast, pcl::io::savePCDFileASCII does perform this check, and throws EXCEPTION : [pcl::PCDWriter::writeASCII] Number of points different than width * height!

Context

Issue occurs when attempting to save in binary format, if the width*height value is not equal to the number of points in the cloud.

The following code snippet from pcd_io.hpp demonstrates how the ASCII variant handles this.
if (cloud.width * cloud.height != cloud.points.size ()) { throw pcl::IOException ("[pcl::PCDWriter::writeASCII] Number of points different than width * height!"); return (-1); }

Expected behavior

Throw the above mentioned exception, same as the ASCII variant.

As a side note, would it make more sense for the PCDWriter to actually attempt to rectify this issue by changing the width, and perhaps inform the user with a warning, rather than throwing an exception?
To a general user, I feel like that may be more convenient, but of course I'm not well-versed enough with the library to think of any situation why an exception would be more useful.

Current Behavior

No error thrown, output file is corrupt. (at least in the sense that most viewers won't open it due to the mismatch)

To Reproduce

  • Create a new point cloud and add some points
  • Keep the width and height values at 1
  • Save the cloud using the savePCDFileBinary method

Screenshots/Code snippets

Your Environment (please complete the following information):

  • OS: Ubuntu 18.04
  • Compiler: GCC 7.5
  • PCL Version: 1.9

Possible Solution

Simply adding the above check to the PCDWriter::writeBinary method should fix the issue. I'd be happy to put in a PR.

Additional context

bug todo io

All 6 comments

Please go ahead with a PR 馃槃

@haritha-j how did you end up with a point cloud where width x height doesn't equal pointcloud size in the first place?

@haritha-j how did you end up with a point cloud where width x height doesn't equal pointcloud size in the first place?

@larshg The short answer would be bad coding i guess :P

Basically I was directly manipulating the points vector of a point cloud to add / delete points, which of course changes the pointcloud size, but forgot to set the width at the end.

Well, it's a faulty API design. It provides direct access to the points container. :)

Maybe something to look into one day 馃槃

Marking this as stale due to 30 days of inactivity. It will be closed in 7 days if no further activity occurs.

Was this page helpful?
0 / 5 - 0 ratings