C++ 从 pcl::PointCloud 创建 pcl::PointCloud::Ptr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10644429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:17:47  来源:igfitidea点击:

Create a pcl::PointCloud::Ptr from a pcl::PointCloud

c++pointerspoint-cloud-library

提问by Fantastic Mr Fox

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 像这样指向云:

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.

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

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.

以下两个答案都是正确的,我已经给了乔纳森正确的勾号,因为他这次是第一个进来的。

采纳答案by patatahooligan

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:

我知道这是旧的,可​​能不再用于 OP,但其他用户可能会偶然发现它。我建议这样做:

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.

Jonathon 的答案很危险的原因是 Pointcloud::Ptr 是 boost::shared_ptr 的 typedef,这意味着指向的对象的所有权。然而,在他的回答中,该对象实际上是一个局部变量,这意味着它可能会超出范围,而仍然有对它的引用,并且 shared_ptr 最终会对其调用 delete,这是未定义的行为。

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.

另一方面,使用 make_shared() 深度复制云。该程序将正常运行,但如果您不需要额外的副本,则远非最佳。

回答by ergosys

Yes, use the makeShared()method.

是的,使用makeShared()方法。