基于pcl库将点P1从stdvectorpclPointCloudpclPointXYZRGBPtr clouds;内删除的c++代码
下面是一个示例代码,用于将点P1从pcl::PointCloudpcl::PointXYZRGB对象中删除:
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
void removePointFromCloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud, pcl::PointXYZRGB point)
{
std::vector<pcl::PointXYZRGB, Eigen::aligned_allocator<pcl::PointXYZRGB>> newPoints;
for (const auto& p : cloud->points)
{
if (p.x != point.x || p.y != point.y || p.z != point.z)
{
newPoints.push_back(p);
}
}
cloud->points = newPoints;
cloud->width = newPoints.size();
cloud->height = 1;
cloud->is_dense = true;
}
int main()
{
std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> clouds;
// 创建点云对象
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
// 向点云中添加点
pcl::PointXYZRGB p1;
p1.x = 1.0;
p1.y = 2.0;
p1.z = 3.0;
cloud->push_back(p1);
// 将点云对象添加到vector中
clouds.push_back(cloud);
// 从点云中删除点p1
pcl::PointXYZRGB pointToRemove;
pointToRemove.x = 1.0;
pointToRemove.y = 2.0;
pointToRemove.z = 3.0;
for(auto it = clouds.begin(); it != clouds.end(); ++it)
{
removePointFromCloud(*it, pointToRemove);
}
return 0;
}
这段代码首先定义了一个函数removePointFromCloud,它接受一个pcl::PointCloud<pcl::PointXYZRGB>::Ptr指针和一个pcl::PointXYZRGB对象作为参数。该函数会遍历点云中的每个点,将不等于给定点P1的点添加到一个新的点云中,最后将新的点云赋值给原始的点云对象。
在main函数中,我们先创建一个pcl::PointCloud<pcl::PointXYZRGB>::Ptr指针,然后向其中添加一个点P1。接着,我们将该点云对象添加到vector中。最后,我们调用removePointFromCloud函数来删除点P1
原文地址: https://www.cveoy.top/t/topic/iHww 著作权归作者所有。请勿转载和采集!