diff --git a/sensor_msgs/include/sensor_msgs/impl/point_cloud2_iterator.h b/sensor_msgs/include/sensor_msgs/impl/point_cloud2_iterator.h index c8d7de8a..7ae8fddf 100644 --- a/sensor_msgs/include/sensor_msgs/impl/point_cloud2_iterator.h +++ b/sensor_msgs/include/sensor_msgs/impl/point_cloud2_iterator.h @@ -189,7 +189,7 @@ inline void PointCloud2Modifier::setPointCloud2Fields(int n_fields, ...) * internals of the PointCloud2 * @param n_fields the number of fields to add. The fields are given as * strings: "xyz" (3 floats), "rgb" (3 uchar stacked in a float), - * "rgba" (4 uchar stacked in a float) + * "rgba" (4 uchar stacked in a uint32) * @return void * * WARNING: THIS FUNCTION DOES ADD ANY NECESSARY PADDING TRANSPARENTLY @@ -201,6 +201,8 @@ inline void PointCloud2Modifier::setPointCloud2FieldsByString(int n_fields, ...) va_list vl; va_start(vl, n_fields); int offset = 0; + bool has_xyz = false; + bool has_rgb = false; for (int i = 0; i < n_fields; ++i) { // Create the corresponding PointFields std::string @@ -211,16 +213,24 @@ inline void PointCloud2Modifier::setPointCloud2FieldsByString(int n_fields, ...) offset = addPointField(cloud_msg_, "x", 1, sensor_msgs::PointField::FLOAT32, offset); offset = addPointField(cloud_msg_, "y", 1, sensor_msgs::PointField::FLOAT32, offset); offset = addPointField(cloud_msg_, "z", 1, sensor_msgs::PointField::FLOAT32, offset); - offset += sizeOfPointField(sensor_msgs::PointField::FLOAT32); + has_xyz = true; } else - if ((field_name == "rgb") || (field_name == "rgba")) { + if (field_name == "rgb") { offset = addPointField(cloud_msg_, field_name, 1, sensor_msgs::PointField::FLOAT32, offset); - offset += 3 * sizeOfPointField(sensor_msgs::PointField::FLOAT32); + has_rgb = true; + } else if (field_name == "rgba") { + offset = addPointField(cloud_msg_, field_name, 1, sensor_msgs::PointField::UINT32, offset); + has_rgb = true; } else throw std::runtime_error("Field " + field_name + " does not exist"); } va_end(vl); + if (has_xyz && !has_rgb) + offset += sizeOfPointField(sensor_msgs::PointField::FLOAT32); + else if (!has_xyz && has_rgb) + offset += 3 * sizeOfPointField(sensor_msgs::PointField::FLOAT32); + // Resize the point cloud accordingly cloud_msg_.point_step = offset; cloud_msg_.row_step = cloud_msg_.width * cloud_msg_.point_step;