Question

I would like to know if this is possible. I have a function:

 pcl::PointCloud<pcl::PointXYZRGB> createPointCloud(std::Vector<Nodes> input)

which returns a point cloud. I would like to know if it is possible to take this point cloud, and make a pointer to a copy of it. pcl makes pointers to clouds like this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>)

I have tried doing this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(createPointCloud(nodeList))

This results in a pretty obvious error ie. createPointCloud doesnt return a pointer to a cloud.

I have also tried this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR = &createPointCloud(nodeList)

and this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(&createPointCloud(nodeList))

And this results in the compile error: "taking address of temporary"

Is the only option to have the function return a pointer type or is there a way to do what i am asking?

EDIT:

Both of the below answers are correct, I have awarded Jonathon the correct tick as he got in first this time.

Was it helpful?

Solution

I know this is old and probably of no more use to OP, but other users might stumble upon it. I would suggest doing it as follows:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
*cloudPTR = createPointCloud(nodeList);

The reason Jonathon's answer is dangerous is that Pointcloud::Ptr is a typedef for a boost::shared_ptr which implies ownership of the object pointed to. In his answer, however, the object is actually a local variable meaning that it might go out of scope while there are still references to it and that shared_ptr will eventually call delete on it, which is undefined behavior.

Using make_shared() on the other hand deep copies the cloud. The program will work correctly, but if you didn't need the extra copy, it is far from optimal.

OTHER TIPS

Yes, use the makeShared() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top