向量下标超出范围,PCL 八叉树

Vector subscript out of range, PCL octree

我正在尝试获取点云的最近邻居,但由于某种原因,我遇到了矢量下标超出范围的错误。当我尝试使用调试器弄清楚它是如何超出范围时,所有值看起来都很正常。

这是我正在使用的代码(我从 the official documentation 中获取)

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(cloud0);
pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree(128.0f);
octree.setInputCloud(cloud);
octree.addPointsFromInputCloud();

pcl::PointXYZ searchPoint;
searchPoint.x = 0;
searchPoint.y = 0;
searchPoint.z = 0;

std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;

float radius = 50;

std::cout << "Neighbors within radius search at (" << searchPoint.x
    << " " << searchPoint.y
    << " " << searchPoint.z
    << ") with radius=" << radius << std::endl;


if (octree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0) {
    for (size_t i = 0; i < pointIdxRadiusSearch.size(); ++i) {
        float x = cloud->points[pointIdxRadiusSearch[i]].x;
        float y = cloud->points[pointIdxRadiusSearch[i]].y;
        float z = cloud->points[pointIdxRadiusSearch[i]].z;
        std::cout << "    " << x
            << " " << y
            << " " << z
            << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
    }
}

更改
if (octree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0) {
    for (size_t i = 0; i < pointIdxRadiusSearch.size(); ++i) {
        float x = cloud->points[pointIdxRadiusSearch[i]].x;
        float y = cloud->points[pointIdxRadiusSearch[i]].y;
        float z = cloud->points[pointIdxRadiusSearch[i]].z;
        std::cout << "    " << x
            << " " << y
            << " " << z
            << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
    }
}

float min_distance = 10000000000000.0f;
pcl::PointXYZ nearestNeighbour;
if (octree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0) {
    for (size_t i = 0; i < pointRadiusSquaredDistance.size(); ++i) {
        if (pointRadiusSquaredDistance[i] < min_distance) {
            min_distance = pointRadiusSquaredDistance[i]; 
            float x = cloud->points[pointIdxRadiusSearch[i]].x;
            float y = cloud->points[pointIdxRadiusSearch[i]].y;
            float z = cloud->points[pointIdxRadiusSearch[i]].z;
            nearestNeighbour.x = x;
            nearestNeighbour.y = y;
            nearestNeighbour.z = z;
        }
    }
}
std::cout << "    " << nearestNeighbour.x << " " << nearestNeighbour.y << " " << nearestNeighbour.z << 
    " (squared distance: " << min_distance << ")" << std::endl;

似乎已经修复了向量超出范围的问题。