1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #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>);
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.0 * rand() / (RAND_MAX + 1.0f); cloud->points[i].y = 1024.0 * rand() / (RAND_MAX + 1.0f); cloud->points[i].z = 1024.0 * rand() / (RAND_MAX + 1.0f); }
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree; kdtree.setInputCloud(cloud);
pcl::PointXYZ searchPoint; searchPoint.x = 1024.0 * rand() / (RAND_MAX + 1.0f); searchPoint.y = 1024.0 * rand() / (RAND_MAX + 1.0f); searchPoint.z = 1024.0 * rand() / (RAND_MAX + 1.0f);
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){ std::cout << "idx\t" << "x\t" << "y\t" << "z\t" << " (squared distance)" << std::endl; for (size_t i=0; i < pointIdxNKNSearch.size(); ++i){ std::cout << i << cloud->points[pointIdxNKNSearch[i]].x << " " << cloud->points[pointIdxNKNSearch[i]].y << " " << cloud->points[pointIdxNKNSearch[i]].z << " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl; } }
std::vector<int> pointIdxRadiusSearch; std::vector<float> pointRadiusSquaredDistance;
float radius = 256.0f * rand() / (RAND_MAX + 1.0f); std::cout<<"--------------------------------------------------------"<<std::endl; std::cout<<"nearest neighbor within radius search at ("<<searchPoint.x<<","<<searchPoint.y<<","<<searchPoint.z<<") with radius="<<radius<<std::endl;
if (kdtree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0){ std::cout << "idx\t" << "x\t" << "y\t" << "z\t" << " (squared distance)" << std::endl; for (size_t i=0; i < pointIdxRadiusSearch.size(); ++i){ std::cout << i << cloud->points[pointIdxRadiusSearch[i]].x << " " << cloud->points[pointIdxRadiusSearch[i]].y << " " << cloud->points[pointIdxRadiusSearch[i]].z << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl; } }
return 0; }
|