【发布时间】:2020-05-10 20:42:57
【问题描述】:
我正在使用 PCL lib 进行 RANSAC 的基本实现。虽然,这里的问题仅与 C++ 概念有关。
我以两种方式迭代点云;一个工作完美,另一个迭代不到一半的点。我只是想了解两者之一不起作用的原因。
工作一:
for (int index=0; index < cloud->points.size(); index++)
{
float distance = abs(A * cloud->points[index].x + B * cloud->points[index].y + C * cloud->points[index].z + D) / sqrt(A * A + B * B + C * C);
// Check for the two points set above, if present ignore
if (set_inliers.count(index) > 0)
continue;
// If distance is smaller than threshold count it as inlier
if (distance <= distanceTol)
set_inliers.insert(index);
std::cout << "Point Number: " << index << std::endl;
}
不起作用的循环:
int index = 0;
for (auto elem : cloud->points)
{
float distance = abs(A * elem.x + B * elem.y + C * elem.z + D) / sqrt(A * A + B * B + C * C);
// Check for the two points set above, if present ignore
if (set_inliers.count(index) > 0)
continue;
// If distance is smaller than threshold count it as inlier
if (distance <= distanceTol)
set_inliers.insert(index);
std::cout << "Point Number: " << index << std::endl;
index++;
}
cloud->points 是一个向量(见下文)。因此,C++11 中引入的基于范围的循环应该可以工作,并且上面提到的两个循环应该是相同的,对吧?我想我在这里遗漏了一些东西。
变量详细信息:
在上面的代码中,var cloud 声明为:
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud
Ptr 是以下向量:
std::vector<pcl::PointXYZ, Eigen::aligned_allocator<pcl::PointXYZ>
cloud->points 定义为:
std::vector<PointT, Eigen::aligned_allocator<PointT> > pcl::PointCloud< PointT >::points
我在这里有一些理解问题,因此如果有人能提供帮助,那就太好了!
非常感谢!
【问题讨论】:
-
两个循环应该做同样的事情。一个区别是
auto elem : cloud->points将复制向量中的每个元素,因此您可以将其更改为auto& elem : cloud->points或const auto& elem : cloud->points来阻止它。如果这没有什么不同,我们真的需要minimal reproducible example。 -
这是什么意思:“循环不起作用”?为什么它不起作用?你有什么错误吗?还是什么?
-
您还需要在
continue;之前增加index,而不仅仅是在循环结束时。
标签: c++ c++11 vector shared-ptr