#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);
}
注意:如果你是windows用户,可以配置好include,lib直接粘贴代码。如果你是CMakeLists.txt可以用下面编译,注意这个代码适合ubuntu18.04:
cmake_minimum_required (VERSION 3.10)
project(pcl_test)
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11")
find_package(Boost REQUIRED COMPONENTS
thread
)
if(NOT Boost_FOUND)
message("NOT found Boost")
endif()
include_directories(${Boost_INCLUDE_DIRS})
find_package( PCL REQUIRED )
include_directories( ${PCL_INCLUDE_DIRS} )
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(main main.cpp)
target_link_libraries(main ${Boost_LIBRARIES} ${PCL_LIBRARIES})
如果您是ubuntu20.04则需要这么做
cmake_minimum_required (VERSION 3.10)
project(pcl_test)
SET(CMAKE_CXX_STANDARD 14)
find_package(Boost REQUIRED COMPONENTS
thread
)
if(NOT Boost_FOUND)
message("NOT found Boost")
endif()
include_directories(${Boost_INCLUDE_DIRS})
find_package( PCL REQUIRED )
include_directories( ${PCL_INCLUDE_DIRS} )
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(main main.cpp)
target_link_libraries(main ${Boost_LIBRARIES} ${PCL_LIBRARIES} pthread boost_thread)
特别注意:经过测试ubuntu18.04和ubuntu20.4如果使用apt安装的pcl运行测试代码没有问题,但是到ubuntu22.04就会出现segment fault错误,原因在于viewer->spinOnce(100)这句代码会出崩溃,这个和vtk版本有关系,或者说pcl和vtk不兼容导致,详情参考https://github.com/PointCloudLibrary/pcl/issues/5237
这个issue已经说明原因,目前解决方法就是源码重新编译但是这个办法显然耗时费力,你可以把viewer->spinOnce(100)改成viewer->spin()即可正常运行,但是关闭窗口会报错segment fault,但是这个不影响整体代码。可以暂时这么用,因此推荐大家还是安装ubuntu18.04最为稳妥。