Create a pcl::PointCloud::Ptr from a pcl::PointCloud
Asked Answered
U

2

12

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.

Underground answered 17/5, 2012 at 22:38 Comment(0)
P
9

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.

Postnatal answered 20/7, 2017 at 15:57 Comment(1)
Which pcl header file needs to be included for this one?Monometallism
L
14

Yes, use the makeShared() method.

Lengthways answered 17/5, 2012 at 22:44 Comment(9)
he's not using std::shared_ptr. he's using a library specific RAII ptr class. std::make_shared() will not work with it.Chromous
@johnathon Where did I mention std:: anything? Follow the link.Lengthways
This might work as well, I have used @jonathon's answer though.Underground
Ptr pcl::PointCloud< PointT >::makeShared is not make_shared(), ergo the reference to the std lib.Chromous
@Ben, if you look at the code, it's just a convenience wrapper for Ptr (new PointCloud<PointT> (*this))Lengthways
I have tried using this method and it works a treat, @jonathon, you should remove your down vote. Both methods work.Underground
@Lengthways appologuise for the down vote, it's been removed. Sorry, make_shared() which is what you had is straight out of the std lib, easy to get confused if your accustom to using it in code as much as i am though.Chromous
@johnathon, OK, we can be friends now. ;-)Lengthways
Very understandable @jonathon, thanks for both your input, they are both equally correct answers, who am i supposed to give the tick to?Underground
P
9

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.

Postnatal answered 20/7, 2017 at 15:57 Comment(1)
Which pcl header file needs to be included for this one?Monometallism

© 2022 - 2024 — McMap. All rights reserved.