Creating PointCloud2 in ROS2

Is there a way to create and fill a PointCloud2 message in ROS2?

You can certainly create and manipulate a PointCloud2:

#include "sensor_msgs/msg/point_cloud2.hpp"

int main(int argc, char * argv[])
{
  sensor_msgs::msg::PointCloud2 point_cloud2_msg;
  point_cloud2_msg.data.reserve(10);
  point_cloud2_msg.fields.reserve(10);
  // ...
}

But I don’t know what you mean by fill. You mean from a PCL type or OpenCV type?

Sorry, I mean actually adding the points. In Ros1, I think the easiest way to create a PointCloud2, was to create a pcl::PointCloud and convert it to PointCloud2, since the pcl::PointCloud allowed you to push_back points.

Now I’m wondering how to go from a list of points to a PointCloud2, preferably without just copying pcl::toROSMsg

Thanks for the reply.

Unfortunately we don’t have anything like that at the moment. You might be able to use this header from ROS 1 as a starting point: