我有一个定义为 pcl::PointCloud<pcl::PointXYZI> cloud_xyzi_;
的点云.稍后,我从一个文件中读入数据并将其推回到这个点云中;一切正常。然而,我的任务要求我过滤点云,并且过滤方法需要指针。
我可以简单地用类似这样的东西替换上面的声明吗 pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_xyzi_p;
,在类的构造函数中初始化它,然后自己拥有一个指针,而不是创建一个指针只是为了将它传递给一个函数?当然改变.
至 ->
当我以其他方式使用它时。我问这个是因为我在尝试实现它时遇到了意想不到的行为。
这是我的删节类(class),当前的代码看起来既多余又效果不佳。
class P400toPointcloud {
public:
P400toPointcloud():
callback_counter_(0),
sensor_model_created_(false)
{
// Setup pointers for later use
cloud_xyzi_p = cloud_xyzi_.makeShared();
cloud_xyzi_fp = cloud_xyzi_f.makeShared();
}
~P400toPointcloud()
{
}
private:
pcl::PointCloud<pcl::PointXYZI> cloud_xyzi_;
pcl::PointCloud<pcl::PointXYZI> cloud_xyzi_f;
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_xyzi_p;
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_xyzi_fp;
std::vector<unsigned int> value_idx_;
};
最佳答案
构造函数中的代码与您可能期望的效果不同:
// Setup pointers for later use
cloud_xyzi_p = cloud_xyzi_.makeShared();
cloud_xyzi_fp = cloud_xyzi_f.makeShared();
调用makeShared()
为新点云分配内存,将旧点云的内容复制到里面,将指向新点云的指针包装到 boost::shared_ptr
中和返回。实际上,您得到了一个完全独立 的拷贝,可以在接受指针的函数中使用。对其所做的任何修改都不会影响原始云。
既然你知道你需要通过它的指针来处理点云,你应该只需要一个 pcl::PointCloud<pcl::PointXYZI>::Ptr
.但是不要忘记在构造函数中为它分配内存:
class P400toPointcloud {
public:
P400toPointcloud():
callback_counter_(0),
sensor_model_created_(false),
cloud_xyzi_(new pcl::PointCloud<pcl::PointXYZI>),
cloud_xyzi_f(new pcl::PointCloud<pcl::PointXYZI>)
{
// From now on you pass
// cloud_xyzi_ to functions that expect pointer to a point cloud
// *cloud_xyzi_ to functions that expect (raw) point cloud
}
~P400toPointcloud()
{
}
private:
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_xyzi_;
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_xyzi_f;
std::vector<unsigned int> value_idx_;
};
关于c++ - PCL 用指针替换点云?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28376386/