欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-17 17:25:12
...

文章目录


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

支付宝

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

微信

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

相关标签: 激光Slam