ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I am making two assumptions in answering this question:

  1. You want to merge (NOT register) the two clouds. I.e. combine their points into one point object without transforming the points in any way.
  2. From the question and comments, you want to merge two clouds of the pcl::PCLPointCloud2 data type

For this, you have several options:

  • MergedCloud = Cloud1 + Cloud2
  • MergedCloud += Cloud1
  • pcl::PCLPointCloud2::concatenate(MergedCloud, Cloud1)
  • pcl::PCLPointCloud2::concatenate(Cloud1, Cloud2, MergedCloud)

Source: pcl::PCLPointCloud2 Struct Reference

I am making two assumptions an assumption in answering this question:

  1. question: You want to merge (NOT register) the two clouds. I.e. combine their points into one point object without transforming the points in any way.
  2. From the question

    Next, the answer depends on which "PointCloud2" datatype you are referring to. Both ROS and comments, you want to PCL (Point Cloud Library) have their own definitions of a data type with this name. The options available to you are different depending on which you are using.

    pcl::PCLPointCloud2

    To merge two clouds of the pcl::PCLPointCloud2 clouds, you have the following options:

    pcl::PCLPointCloud2 data type

For this, you have several options:

  • // Option 1 MergedCloud = Cloud1 + Cloud2
  • Cloud2 // Option 2 MergedCloud += Cloud1
  • Cloud1 // Option 3 pcl::PCLPointCloud2::concatenate(MergedCloud, Cloud1)
  • Cloud1) // Option 4 pcl::PCLPointCloud2::concatenate(Cloud1, Cloud2, MergedCloud)
MergedCloud)

Source: pcl::PCLPointCloud2 Struct Reference

sensor_msgs::PointCloud2

To merge two ROS sensor_msgs::PointCloud2 clouds, there aren't any pre-existing functions that will do this for you (unless I missed them) so you have to either convert the point cloud to a PCL type (plenty of online tutorials that will show you how to do that) or you have to do it yourself directly. In C++ this can be done as shown below:

Note: I am assuming here that you are using an unorganized point cloud, in which case the "width" is just the length of the 1D data array. Merging organised point clouds is very different and deserves its own Q&A.

// Merge metadata
MergedCloud.width += Cloud1.width;

// Re-size the merged data array to make space for the new points
uint64_t OriginalSize = MergedCloud.data.size();
MergedCloud.data.resize(MergedCloud.data.size() + Cloud1.data.size());

// Copy the new points from Cloud1 into the second half of the MergedCloud array
std::copy(
  Cloud1.data.begin(),
  Cloud1.data.end(),
  MergedCloud.data.begin() + OriginalSize);