• PCL——(5)kd-tree实现快速领域搜索


    @


    在这里插入图片描述

    #include <pcl/point_cloud.h>
    #include <pcl/kdtree/kdtree_flann.h>
    
    #include <iostream>
    #include <vector>
    #include <ctime>
    
    int main (int argc, char** argv)
    {
      srand (time (NULL));//用系统时间初始化随机种子
    
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    
      // Generate pointcloud data
     // 随机点云生成
      cloud->width=1000;                 //此处为点云数量
      cloud->height=1;                   //此处表示点云为无序点云
      cloud->points.resize (cloud->width * cloud->height);
      // //循环填充点云数据
      for (std::size_t i = 0; i < cloud->points.size (); ++i)
      {
        cloud->points[i].x = 1024.0f * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024.0f * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024.0f * rand () / (RAND_MAX + 1.0f);
      }
    
      pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;//创建kdtree对象
    
      kdtree.setInputCloud(cloud); // 设置搜索点云(空间)
    
      pcl::PointXYZ searchPoint;//定义需要查询的点并赋随机值
    
      searchPoint.x = 1024.0f * rand () / (RAND_MAX + 1.0f);
      searchPoint.y = 1024.0f * rand () / (RAND_MAX + 1.0f);
      searchPoint.z = 1024.0f * rand () / (RAND_MAX + 1.0f);
    
      // K nearest neighbor search
      int K = 10;
    
      std::vector<int> pointIdxNKNSearch(K);//存储查询点近邻索引
      std::vector<float> pointNKNSquaredDistance(K);//存储近邻点对应平方距离
    
      std::cout << "K nearest neighbor search at (" << searchPoint.x 
                << " " << searchPoint.y 
                << " " << searchPoint.z
                << ") with K=" << K << std::endl;
    
      if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 )
      {
       //打印出所有近邻坐标
        for (std::size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
          std::cout << "    "  <<   cloud->points[ pointIdxNKNSearch[i] ].x 
                    << " " << cloud->points[ pointIdxNKNSearch[i] ].y 
                    << " " << cloud->points[ pointIdxNKNSearch[i] ].z 
                    << " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
      }
    
      // Neighbors within radius search
      std::vector<int> pointIdxRadiusSearch; //存储近邻索引
      std::vector<float> pointRadiusSquaredDistance;  //存储近邻对应的平方距离
    
      float radius = 256.0f * rand () / (RAND_MAX + 1.0f);
    
      std::cout << "Neighbors within radius search at (" << searchPoint.x 
                << " " << searchPoint.y 
                << " " << searchPoint.z
                << ") with radius=" << radius << std::endl;
    
    
      if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 )
      {
        for (std::size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
          std::cout << "    "  <<   cloud->points[ pointIdxRadiusSearch[i] ].x 
                    << " " << cloud->points[ pointIdxRadiusSearch[i] ].y 
                    << " " << cloud->points[ pointIdxRadiusSearch[i] ].z 
                    << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
      }
    
    
      return 0;
    }
    

    打赏

    码字不易,如果对您有帮助,就打赏一下吧O(∩_∩)O

    支付宝

    微信

  • 相关阅读:
    线性表
    面试题
    进程与线程的区别
    双亲委派模型&动态生成Java类
    springboot优势
    Python3数据结构
    GC算法
    JVM的跨平台特性
    HashMap#put (K key, V value)过程白话文
    java内存模型
  • 原文地址:https://www.cnblogs.com/long5683/p/13275936.html
Copyright © 2020-2023  润新知