Pcl: How can I initialize pcl::PointCloud<pcl::PointXYZ> using std::vector<pcl::PointXYZ>?

Created on 8 May 2019  路  5Comments  路  Source: PointCloudLibrary/pcl

Hi, thank you for this perfect code about pointcloud.
I want to use a variable( std::vector) to initialize another variable (pcl::PointCloud). I could use the following code to accomplish it.

   std::vector<pcl::PointXYZ> pp(p1, p1 + 10);
pcl::PointCloud<pcl::PointXYZ> cloud;
for (int i = 0; i < 10;++i)
{
    cloud.push_back(pp[i]);
}

But I think this method is inefficient, is there a better way? Thank you for your reply.

Regards,

question

Most helpful comment

Note that it's not recommended to store PCL points in a vector without a custom aligned allocator. This may lead to weird segmentation faults. If you really want to have a plain vector of points (not a pcl::PointCloud), then use a special typedef from the point cloud class:

https://github.com/PointCloudLibrary/pcl/blob/8b8ec7b3a762870c73044c47b0cb4a144964ebb9/common/include/pcl/point_cloud.h#L425

If you do so, you can also just use assignment operator in your snippet: cloud.points = pp, though don't forget to update the cloud width and height afterwards.

All 5 comments

You should be able to use std::copy. pcl::PointCloud exposes the begin() and end() iterators, so as long as you resize it advance, the copy can happen in a single line invocation.

Note that it's not recommended to store PCL points in a vector without a custom aligned allocator. This may lead to weird segmentation faults. If you really want to have a plain vector of points (not a pcl::PointCloud), then use a special typedef from the point cloud class:

https://github.com/PointCloudLibrary/pcl/blob/8b8ec7b3a762870c73044c47b0cb4a144964ebb9/common/include/pcl/point_cloud.h#L425

If you do so, you can also just use assignment operator in your snippet: cloud.points = pp, though don't forget to update the cloud width and height afterwards.

@SergioRAgostinho
According to your instruction, I solve the problem. Thank you for your help.

@taketwo
Your idea works, thank you very much. I have a question. Is this method deep copy?

Regards,

This uses assignment operator of the standard vector. Thus yes, it makes a deep copy.

Was this page helpful?
0 / 5 - 0 ratings