目录
目的
点云文件转为八叉树文件
代码
在一个文件夹中新建两个文件,pcd2bt.cpp和CMakeLists.txt,分别写入:
pcd2bt.cpp
#include <iostream>
#include <assert.h>
//pcl
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
//octomap
#include <octomap/octomap.h>
using namespace std;
int main( int argc, char** argv )
{
if (argc != 3)
{
cout<<"error"<<endl;
return -1;
}
string input_file = argv[1];
string output_file = argv[2];
pcl::PointCloud<pcl::PointXYZI> cloud;
pcl::io::loadPCDFile<pcl::PointXYZI> ( input_file, cloud );
cout<<"point cloud loaded, piont size = "<<cloud.points.size()<<endl;
//声明octomap变量
cout<<"copy data into octomap..."<<endl;
// 创建八叉树对象,参数为分辨率,这里设成了0.05
octomap::OcTree tree( 0.05 );
for (auto p:cloud.points)
{
// 将点云里的点插入到octomap中
tree.updateNode( octomap::point3d(p.x, p.y, p.z), true );
}
// 更新octomap
tree.updateInnerOccupancy();
// 存储octomap
tree.writeBinary(output_file);
cout<<"done."<<endl;
return 0;
}
CMakeLists.txt文件内容
cmake_minimum_required(VERSION 2.8)
project(my_project_pcd2bt)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")
# 设置编译类型为Release
# 添加PCL库的依赖
find_package(PCL REQUIRED )
# 添加octomap库的依赖
find_package(octomap REQUIRED)
# 添加头文件路径
include_directories(${PCL_INCLUDE_DIRS})
include_directories(${OCTOMAP_INCLUDE_DIRS})
# 添加链接库路径
link_directories(${PCL_LIBRARY_DIRS})
link_directories(${OCTOMAP_LIBRARY_DIRS})
# 编译可执行文件
add_executable(pcd2bt pcd2bt.cpp)
# 链接依赖库
target_link_libraries(pcd2bt ${PCL_LIBRARIES})
target_link_libraries(pcd2bt ${OCTOMAP_LIBRARIES})
代码修改
pcd2bt.cpp文件中,根据需要修改<pcl::PointXYZI>、分辨率等。
编译
执行命令
在这个文件夹中,打开终端。依次执行:
mkdir build
cd build
cmake ..
make
终端显示内容
cmake ..通过,终端最后显示
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/myhome/xxx/pcd2bt/build
make通过,终端最后显示
Scanning dependencies of target pcd2bt
[ 50%] Building CXX object CMakeFiles/pcd2bt.dir/pcd2bt.cpp.o
[100%] Linking CXX executable pcd2bt
[100%] Built target pcd2bt
使用程序
在build文件夹下,打开终端。依次执行:
./pcd2bt /usr/myhome/xxx.pcd xxx.bt
xxx.pcd修改为所需转换的pcd文件的路径。
xxx.bt修改为所需保存的bt文件的路径。
参考