PCL点云库:Kd树
Kd树按空间划分生成叶子节点,各个叶子节点里存放点数据,其可以按半径搜索或邻区搜索。PCL中的Kd tree的基础数据结构使用了FLANN以便可以快速的进行邻区搜索。FLANN is a library for performing fast approximate nearest neighbor searches in high dimensional spaces。下面是一个最基本的例子,只寻找一个最近点:
#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)); //seeds rand() with the system time time_t begin,end;
begin = clock(); //开始计时
//-------------------------------------------------------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); // Generate pointcloud data
cloud->width = ;
cloud->height = ;
cloud->points.resize (cloud->width * cloud->height); // fills a PointCloud with random data
for (size_t i = ; 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);
} // creates kdtree object
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree; // sets our randomly created cloud as the input
kdtree.setInputCloud (cloud); //create a “searchPoint” which is assigned random coordinates
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 = ;
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; /***********************************************************************************************
template<typename PointT>
virtual int pcl::KdTree< PointT >::nearestKSearch ( const PointT & p_q,
int k,
std::vector< int > & k_indices,
std::vector< float > & k_sqr_distances
) const [pure virtual] Search for k-nearest neighbors for the given query point.
Parameters:
[in] the given query point
[in] k the number of neighbors to search for
[out] the resultant indices of the neighboring points
[out] the resultant squared distances to the neighboring points
Returns:
number of neighbors found
********************************************************************************************/
if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > )
{
for (size_t i = ; 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;
}
//--------------------------------------------------------------------------------------------
end = clock(); //结束计时
double Times = double(end - begin) / CLOCKS_PER_SEC; //将clock()函数的结果转化为以秒为单位的量 std::cout<<"time: "<<Times<<"s"<<std::endl; return ;
}
生成四十万个随机点,release版本下测试0.3s左右找到最近点,这比之前自己写的Kd树不知快到哪里去了。当然自己写只是为了更好的理解其中的原理,真要用的时候还得靠别人的轮子...

参考:
PCL点云库:Kd树的更多相关文章
- PCL点云库:ICP算法
ICP(Iterative Closest Point迭代最近点)算法是一种点集对点集配准方法.在VTK.PCL.MRPT.MeshLab等C++库或软件中都有实现,可以参见维基百科中的ICP Alg ...
- PCL点云库中的坐标系(CoordinateSystem)
博客转载自:https://blog.csdn.net/qq_33624918/article/details/80488590 引言 世上本没有坐标系,用的人多了,便定义了坐标系统用来定位.地理坐标 ...
- Windows下安装PCL点云库
原文链接:http://blog.csdn.net/u012337034/article/details/38270109 简介: 在Windows下安装PCL点云库的方法大概有两种: ...
- Windows 8 64位系统 在VS2010 32位软件上 搭建 PCL点云库 开发环境
Windows 8 64位系统 在VS2010 32位软件上 搭建 PCL点云库 开发环境 下载PCL For windows 软件包 到这个网站下载PCL-All-In-One Installer: ...
- python利用pybind11调用PCL点云库
2019年7月9日14:31:13 完成了一个简单的小例子,python生成点云数据,利用pybind11传给PCL显示. ubuntu 16.04 + Anaconda3 python3.6 + ...
- PCL点云库(Point Cloud Library)简介
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=29 什么是PCL PCL(Point Cloud Library)是在吸收了 ...
- PCL点云库:对点云进行变换(Using a matrix to transform a point cloud)
点云数据可以用ASCII码的形式存储在PCD文件中(关于该格式的描述可以参考链接:The PCD (Point Cloud Data) file format).为了生成三维点云数据,在excel中用 ...
- [PCL]1 PCL点云库安装
1.安装文件下载:官网,我还是比较喜欢别人编译好的安装包啊,哈哈. http://www.pointclouds.org/downloads/windows.html 2.傻瓜式安装(下面的依赖项都集 ...
- PCL点云库增加自定义数据类型
#include <pcl/filters/passthrough.h> #include <pcl/filters/impl/passthrough.hpp> // the ...
随机推荐
- javascript 原型链
浅谈JS原型链 原型链 ECMAScript中描述了原型链的概念.我们知道ECMAScript并不像C++,Java那样使用类,但是对象仍然可以通过多种方式创建,其中就有构造函数方式.每个构造函数都有 ...
- NPOI简单操作excel
本文仅当是个记录文件,仅供初学者参考. 首先得using几个npoi的空间名如下: using NPOI.HSSF.UserModel;using NPOI.HSSF.Util;using NPOI. ...
- Codeforces Round #350 (Div. 2) E 思维模拟
给出一个合法的括号串 有LRD三种操作 LR分别是左右移动当前位置 且合法 D为删除这个括号和里面的所有 当删除完成后 位置向右移动 如果不能移动 就向左 比赛都是很久远的事情了 写这道题也是一时兴起 ...
- URAL 2080 莫队
题意 有m种卡 给出卡的使用序列 要求每次从卡堆的顶部抽一张出来 刚好符合序列 输出初始 卡堆的排序 再输出每次抽出卡用后 卡插回卡堆的时候 这张卡上面有几张卡 初始排序很容易就可以搞出来 但是需要注 ...
- Eclipse new server : Cancnot create a server using the selected type
1.退出 eclipse 2.到[工程目录下]/.metadata/.plugins/org.eclipse.core.runtime 3.把org.eclipse.wst.server.core.p ...
- 一键发布ASP.NET Web安装程序
转载自:http://www.cnblogs.com/nangong/p/Web.html 前言:最近公司有个Web要发布,但是以前都是由实施到甲方去发布,配置,这几天有点闲,同事让我搞 ...
- c语言中的指针问题
“*”符号的作用在C语言中有两种: 1.声明该变量是指针,例如:int * p;//表示声明一个int类型的指针,变量名为p 2.在指针运算时,表示取这个地址上的内容,例如 temp = *p;// ...
- Stream Processing for Everyone with SQL and Apache Flink
Where did we come from? With the 0.9.0-milestone1 release, Apache Flink added an API to process rela ...
- xampp使用phpunit
1.将xampp/php的pear文件夹里面的phpunit文件夹复制到htdocs目录下 2.复制xampp/php的phpunit.bat到需要测试的目录 3.使用cmd命令切换至phpunit. ...
- AFNetworking的原理与基本使用
全称是AFNetworking 虽然运行效率没有ASI高,但是使用比ASI简单 是对NSURLConnection和NSURLSession的各自的一层包装 AFN的内部中的RunLoop AFN内部 ...