main.cc 文件
#include <omp.h>
#include <ctime>
#include <vector>
#include <string>
#include <algorithm>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
#include <fstream>
#include <pcl/io/pcd_io.h>
#include <pcl/common/common_headers.h>
#include <sys/stat.h>
#include <pcl/io/boost.h>
#include <boost/program_options.hpp>
void convert_pcd_to_bin(std::string path_input, std::string path_output)
{
std::string datapath_in = path_input;
std::string datapath_out= path_output;
std::vector<boost::filesystem::path> paths(boost::filesystem::directory_iterator{datapath_in}, boost::filesystem::directory_iterator{});
sort(paths.begin(), paths.end());
auto pcd_iter = paths.begin();
int frames_all = paths.end() - paths.begin();
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
for (int i = 0; i < frames_all; i++) {
pcd_iter++;
std::string in_file = pcd_iter->string();
if (pcl::io::loadPCDFile<pcl::PointXYZI>(in_file, *cloud) == -1) {
std::string err = "Couldn't read file " + in_file;
PCL_ERROR(err.c_str());
return;// (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from "
<< in_file
<< " with the following fields: "
<< std::endl;
std::string out_file = datapath_out + std::to_string(i) + ".bin";
std::ofstream bin_file(out_file.c_str(), std::ios::out | std::ios::binary);
for (int j = 0; j < cloud->size(); j++) {
bin_file.write((char*)& cloud->at(j).x, sizeof(cloud->at(j).x));
bin_file.write((char*)& cloud->at(j).y, sizeof(cloud->at(j).y));
bin_file.write((char*)& cloud->at(j).z, sizeof(cloud->at(j).z));
bin_file.write((char*)& cloud->at(j).intensity, sizeof(cloud->at(j).intensity));
}
bin_file.close();
}
}
int main(int argc, char **argv)
{
if (argc <= 1) {
std::cout << "please input datapath dir and output datapath dir" << std::endl;
std::cout << "for example, run ./pcd2bin /home/pcds/ /home/bins/" << std::endl;
return 0;
}
std::string path_input = argv[1];
std::string path_output= argv[2];
std::fstream file_in;
file_in.open(path_input, std::ios::in);
if (!file_in) {
std::cout << "can not find the input path:" << path_input << std::endl;
return 0;
}
std::fstream file_out;
file_out.open(path_output, std::ios::in);
if (!file_out) {
std::cout << "can not find the output path:" << path_output << std::endl;
if (0 == mkdir(path_output.data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
std::cout << "successfully creat output path: " << path_output << std::endl;
} else {
std::cout << "create output path failed!" << std::endl;
return 0;
}
}
convert_pcd_to_bin(path_input, path_output);
return 0;
}
CMakeLists.txt 文件
cmake_minimum_required(VERSION 3.5)
project(pcd2bin)
find_package(PCL 1.2 REQUIRED)
# 加入Boost setting
find_package(Boost COMPONENTS program_options REQUIRED )
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(pcd2bin main.cc)
target_link_libraries (pcd2bin ${PCL_LIBRARIES} ${Boost_LIBRARIES})