测试环境:
qt5.12.9
ubuntu16.04
pcl为系统apt默认库
代码:
#include <iostream>
#include <vector>
#include <ctime>
#include <pcl/point_cloud.h>
#include <pcl/octree/octree.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std;
int main(int argc, char** argv)
{srand((unsigned int)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.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::octree::OctreePointCloudSearch<pcl::PointXYZ> octree(0.1);octree.setInputCloud(cloud);octree.addPointsFromInputCloud();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);//半径内近邻搜索vector<int>pointIdxRadiusSearch;vector<float>pointRadiusSquaredDistance;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 (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]].y<< " " << cloud->points[pointIdxRadiusSearch[i]].z<< " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << endl;}// 初始化点云可视化对象boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer(new pcl::visualization::PCLVisualizer("pcl cloud point"));viewer->setBackgroundColor(0, 0, 0); //设置背景颜色为黑色// 对点云着色可视化 (red).pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>target_color(cloud, 255, 0, 0);viewer->addPointCloud<pcl::PointXYZ>(cloud, target_color, "target cloud");viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "target cloud");// 等待直到可视化窗口关闭while (!viewer->wasStopped()){viewer->spinOnce(100);boost::this_thread::sleep(boost::posix_time::microseconds(1000));}return (0);
}
pro配置:
DEPATH += /usr/include/eigen3INCLUDEPATH += /usr/include/vtk-6.2LIBS += /usr/lib/x86_64-linux-gnu/libvtk*.soINCLUDEPATH += /usr/include/boostLIBS += /usr/lib/x86_64-linux-gnu/libboost_*.so