当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
STM32 and motor development (upper system)
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
使用Windbg静态分析dump文件(实战经验总结)
《MySQL 8 DBA基础教程》简介
Thanos Receiver
华为游戏初始化init失败,返回错误码907135000
从MediaRecord录像中读取H264参数
js数组常用方法
LeetCode+ 76 - 80 暴搜专题
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
随机推荐
Database dictionary Navicat automatic generation version
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
Pywin32 opens the specified window
2022-06-17
HDU1228 A + B(map映射)
大华设备播放过程中设置播放速度
Point cloud projection picture
KS009基于SSH实现宠物管理系统
从.bag文件中读取并保存.jpg图片和.pcd点云
4.随机变量
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
AppGallery Connect场景化开发实战—图片存储分享
《MySQL 8 DBA基础教程》简介
Kustomize使用手册
MYSQL关键字
MYSQL环境配置
Hdu1236 ranking (structure Sorting)
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
软件产品管理系统有哪些?12个最佳产品管理工具盘点