从pcl :: PointCloud创建pcl :: PointCloud :: Ptr [英] Create a pcl::PointCloud::Ptr from a pcl::PointCloud

查看:1702
本文介绍了从pcl :: PointCloud创建pcl :: PointCloud :: Ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可行.我有一个功能:

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

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

返回点云.我想知道是否有可能把这个点云,并指向它的副本. pcl这样指向云:

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>)

我尝试这样做:

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

这会导致一个非常明显的错误,即. createPointCloud不会返回指向云的指针.

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

我也尝试过这个:

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

和这个:

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?

以下两个答案都是正确的,我已将乔纳森(Jonathon)的第一次打入正确时机授予他.

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

推荐答案

我知道这很旧,可能对OP不再有用,但是其他用户可能会偶然发现它.我建议这样做如下:

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);

乔纳森的回答之所以危险是因为Pointcloud :: Ptr是boost :: shared_ptr的类型定义,它暗示了所指向对象的所有权.但是,在他的回答中,该对象实际上是一个局部变量,这意味着它在仍然有引用的情况下可能会超出范围,并且shared_ptr最终将对其进行调用delete,这是未定义的行为.

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.

另一方面,使用make_shared()深度复制云.该程序可以正常运行,但是如果您不需要多余的副本,那将不是最佳选择.

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.

这篇关于从pcl :: PointCloud创建pcl :: PointCloud :: Ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆