下面是基于1.8.1版本的pcl库编写的C++代码,实现了以已知点p为起点,在搜索半径为r内的点中随机选取一个点,并重复搜索直到ye不再有新的变更,并输出ye的代码。

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>

typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int main()
{
    // 读取点云数据
    PointCloudT::Ptr cloud(new PointCloudT);
    pcl::io::loadPCDFile("your_point_cloud.pcd", *cloud);

    // 搜索参数设置
    float radius = 0.1; // 搜索半径
    int maxIterations = 100; // 最大迭代次数

    // 初始化搜索器
    pcl::search::KdTree<PointT>::Ptr tree(new pcl::search::KdTree<PointT>);
    tree->setInputCloud(cloud);

    // 已知起点p
    PointT p;
    p.x = 0.0;
    p.y = 0.0;
    p.z = 0.0;

    // 创建指针ye,并将起点p加入
    std::vector<int> ye;
    ye.push_back(0);

    // 随机数生成器
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, ye.size() - 1);

    int iteration = 0;
    bool hasNewPoints = true;

    while (hasNewPoints && iteration < maxIterations) {
        hasNewPoints = false;

        // 在ye内随机选取一个点
        int randomIndex = dis(gen);
        int randomPointIndex = ye[randomIndex];

        // 搜索半径内的点
        std::vector<int> indices;
        std::vector<float> distances;
        tree->radiusSearch(randomPointIndex, radius, indices, distances);

        // 将搜索到的点加入ye
        for (int i = 0; i < indices.size(); ++i) {
            int index = indices[i];
            if (std::find(ye.begin(), ye.end(), index) == ye.end()) {
                ye.push_back(index);
                hasNewPoints = true;
            }
        }

        iteration++;
    }

    // 输出ye的代码
    std::cout << "std::vector<int> ye = {";
    for (int i = 0; i < ye.size() - 1; ++i) {
        std::cout << ye[i] << ", ";
    }
    std::cout << ye[ye.size() - 1] << "};" << std::endl;

    return 0;
}

请注意替换代码中的your_point_cloud.pcd为你自己的点云文件路径。此代码会输出一个名为ye的std::vector<int>,其中包含了搜索半径内所有点的索引。

基于181pcl库写一段以已知点p为起点搜索半径为r内的点加入指针ye再在ye内随机选一点重复搜索直到ye不再有新的变更输出ye的c++代码

原文地址: https://www.cveoy.top/t/topic/hVy2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录