Fix incorrect offset calculation for PointCloud2 PointField fields #29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
While using the library to convert PCD data into ROS
PointCloud2
messages, I encountered an issue where theoffset
values for thePointField
fields were calculated incorrectly. This resulted in fields likeintensity
or other attributes being misaligned in the generatedPointCloud2
message, causing visualization tools like RViz to display incorrect or missing data.Expected Behavior
The
offset
for eachPointField
should reflect the cumulative byte position of the field within a point, considering the size and count of all preceding fields.Actual Behavior
The current implementation calculates
offset
usingi * type_.itemsize
, which assumes a fixed-size field alignment without considering the actual data layout or field sizes. This leads to incorrectoffset
values for fields that do not align sequentially (e.g., fields with different sizes or counts).Steps to Reproduce
x, y, z
(float32)intensity
(uint8)return_type
(uint8)channel
(uint16)PointCloud2
message in ROS.Example Code
Here is the problematic snippet:
Suggested Fix
The
offset
should be calculated as the cumulative sum of the sizes of all preceding fields, adjusted for the field'scount
. For example:Impact
This issue causes data misalignment in the
PointCloud2
message, leading to:Testing
Tested with PCD files containing fields of varying sizes (float32, uint8, uint16).
Verified the corrected offsets using RViz and CloudCompare.