zoukankan      html  css  js  c++  java
  • 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

    支付宝

    微信

  • 相关阅读:
    11111 Generalized Matrioshkas
    Uva 442 Matrix Chain Multiplication
    Uva 10815 Andy's First Dictionary
    Uva 537 Artificial Intelligence?
    Uva 340 MasterMind Hints
    SCAU 9508 诸葛给我牌(水泥题)
    Uva 10420 List of Conquests(排序水题)
    Uva 409 Excuses, Excuses!
    10/26
    11/2
  • 原文地址:https://www.cnblogs.com/long5683/p/13275936.html
Copyright © 2011-2022 走看看