当前位置:网站首页>K-d tree and octree of PCL
K-d tree and octree of PCL
2022-07-02 10:56:00 【AICVer】
utilize k-d tree Realize fast neighborhood search
#include <iostream>
#include <vector> // The dynamic array vector The header file
#include <ctime> // System time header file
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h> //k Nearest neighbor search header file
int main(int argc, char** argv)
{
srand(time(NULL)); // System random number seed
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // Initialize a point cloud with random numbers
cloud->width = 1000;
cloud->height = 1;
cloud->points.resize(cloud->width * cloud->height);
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
}
pcl::KdTreeFLANN<pcl::PointXYZ>kdtree; // establish kd tree
kdtree.setInputCloud(cloud); // Set up kd tree Search scope for
pcl::PointXYZ searchpoint; // Set the search center , And assign a random value to the store
searchpoint.x = 1024 * rand() / (RAND_MAX + 1.0f);
searchpoint.y = 1024 * rand() / (RAND_MAX + 1.0f);
searchpoint.z = 1024 * rand() / (RAND_MAX + 1.0f);
int K = 10; // Set search k The number of nearest neighbors is 10
std::vector<int>pointIdxNKNSearch(K); // Set the index of the nearest neighbor of the query point
std::vector<float>pointNKNSquareDistance(K); // Store the square distance corresponding to the nearest neighbor
std::cout << "K nearest neightbor searchpoint at (" << searchpoint.x << " " << searchpoint.y << " " << searchpoint.z << ") with K = " << K << std::endl;
if (kdtree.nearestKSearch(searchpoint, K, pointIdxNKNSearch, pointNKNSquareDistance) > 0)// If kd tree If there is a nearest neighbor, output , If it does not exist, it will not output
{
for (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:" << pointNKNSquareDistance[i] << ")" << std::endl;
}
}
else
{
std::cout << "NO K nearested point detected" << std::endl;
}
std::cout << "\n" << std::endl;
std::vector<int>pointIdxRadiusSearch; // Set to search the nearest neighbor within the radius
std::vector<float>pointRadiusSquareDistance; // The square distance corresponding to the nearest neighbor in the radius
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, pointRadiusSquareDistance) > 0)// Output the searched nearest neighbor and the corresponding radius information
{
for (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:" << pointRadiusSquareDistance[i] << ")" << std::endl;
}
}
else
{
std::cout << "No enough points detected in Radius Search!" << std::endl;
}
return(0);
}
Use octree to search
#include<pcl/point_cloud.h>
#include<pcl/octree/octree_search.h>
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
int main(int argc, char** argv)
{
// Use system time to seed random numbers
srand((unsigned int)time(NULL));
// Create a PointXYZ Type point cloud pointer
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
// Initialize point cloud data
cloud->width = 1000;// Wide for 1000
cloud->height = 1;// High for 1, Description is disordered point cloud
cloud->points.resize(cloud->width * cloud->height);
// Fill the data with random numbers
for (size_t i = 0; i < cloud->size(); ++i)
{
//PointCloud Class [] Operator is overloaded , The return is right points References to
// (*cloud)[i].x Equate to cloud->points[i].x
(*cloud)[i].x = 1024.0f * rand() / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024.0f * rand() / (RAND_MAX + 1.0f);// Progressive writing
cloud->points[i].z = 1024.0f * rand() / (RAND_MAX + 1.0f);// Progressive writing
}
// Create an octree object
float resolution = 128.0f;// Set resolution
pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree(resolution);
// Set point cloud input , Will be in cloud Mid search
octree.setInputCloud(cloud);
octree.addPointsFromInputCloud();
// Set the searched point , Fill with random numbers
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);
// Voxel inner nearest neighbor search
// Use vector Store search result subscripts
vector<int> pointIdxVec;// Save subscript
if (octree.voxelSearch(searchPoint, pointIdxVec))
{
cout << "Neighbors within voxel search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ")"
<< endl;
for (size_t i = 0; i < pointIdxVec.size(); ++i)
{
cout << " " << cloud->points[pointIdxVec[i]].x
<< " " << cloud->points[pointIdxVec[i]].x
<< " " << cloud->points[pointIdxVec[i]].z
<< endl;
}
}
// Start k Nearest neighbor search
int K = 10;
// Use two vector Store search results
vector<int> pointIdxNKNSearch(K);// Save subscript
vector<float> pointNKNSquaredDistance(K);// Save the square of the distance
cout << "K nearest neighbor search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with K = " << K << endl;
/** * Suppose our KdTree Return more than 0 The nearest neighbor , * Then it prints out all 10 Random separation searchPoint The location of the nearest neighbor , * These are stored in what we created before vector in . */
if (octree.nearestKSearch(searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
{
for (size_t i = 0; i < pointIdxNKNSearch.size(); ++i)
{
cout << " " << cloud->points[pointIdxNKNSearch[i]].x
<< " " << cloud->points[pointIdxNKNSearch[i]].x
<< " " << cloud->points[pointIdxNKNSearch[i]].z
<< "( squared distance: " << pointNKNSquaredDistance[i] << " )" << endl;
}
}
// Radius based neighborhood search
// The search results are saved in two arrays , One is the subscript , One is distance
vector<int> pointIdxRadiusSearch;
vector<float> pointRadiusSquaredDistance;
// Set the search radius , Random value
float radius = 256.0f* rand() / (RAND_MAX + 1.0f);
cout << "Neighbors within radius search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with radius=" << radius << endl;
/** * If our KdTree Returns more than... Within the specified radius 0 A neighbor , It will print out the coordinates of these points stored in the vector . */
if (octree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
{
for (size_t i = 0; i < pointIdxRadiusSearch.size(); ++i)
{
cout << " " << cloud->points[pointIdxRadiusSearch[i]].x
<< " " << cloud->points[pointIdxRadiusSearch[i]].x
<< " " << cloud->points[pointIdxRadiusSearch[i]].z
<< "( squared distance: " << pointRadiusSquaredDistance[i] << " )" << endl;
}
}
return 0;
}
边栏推荐
- Retrofit's callback hell is really vulnerable in kotlin synergy mode!
- 华为联机对战服务玩家掉线重连案例总结
- MYSQL环境配置
- Mongodb quickly get started with some simple operations of mongodb command line
- [SUCTF2018]followme
- From Read and save in bag file Jpg pictures and PCD point cloud
- 2022爱分析· 国央企数字化厂商全景报告
- Solution of mysql8 forgetting password file in Windows Environment
- Pywin32打开指定窗口
- UVM——Callback
猜你喜欢

The nanny level tutorial of flutter environment configuration makes the doctor green to the end

首份中国企业敏捷实践白皮书发布| 附完整下载

Kustomize使用手册

STM32 and motor development (upper system)

JSP webshell免杀——webshell免杀

axis设备的rtsp setup头中的url不能带参

Use WinDbg to statically analyze dump files (summary of practical experience)

【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?

Disassembling Meitu SaaS: driving the plane to change the engine

华为AppLinking中统一链接的创建和使用
随机推荐
(五)APA场景搭建之挡位控制设置
如何使用IDE自动签名调试鸿蒙应用
PCL 投影点云
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
正则及常用公式
JS settimeout() and interview questions
Windows环境MySQL8忘记密码文件解决方案
UWA报告使用小技巧,你get了吗?(第四弹)
js promise.all
Mongodb quickly get started with some simple operations of mongodb command line
MySQL environment configuration
Flutter——Canvas自定义曲线图
Flink submitter
Kustomize使用手册
Sus system availability scale
JSP webshell free -- webshell free
Solutions to a series of problems in sqoop job creation
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
【TS】1368- 秒懂 TypeScript 泛型工具类型!
12.进程同步与信号量