整体实现思路是:首先获取文件夹下所有文件名,然后遍历打开文件用PCL从中获取坐标
程序所需头文件:
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <vector>
#include <cstring>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <ros/ros.h>
void getFiles(string path, vector<string>& filenames)
{
DIR *pDir;
struct dirent* ptr;
if(!(pDir = opendir(path.c_str()))){
cout<<"Folder doesn't Exist!"<<endl;
return;
}
while((ptr = readdir(pDir))!=0) {
if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){
filenames.push_back(path + "/" + ptr->d_name);
}
}
closedir(pDir);
}
void Get_position_data(float *position_x, float *position_y, int &position_num) {
string filePath = "/home/user/pcd";
vector<string> files;
getFiles(filePath, files);
for (int i=0; i<(int)files.size(); i++) {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if(pcl::io::loadPCDFile<pcl::PointXYZ>(files[i], *cloud)==-1) {
PCL_ERROR("Couldn't read file test_pcd.pcd\n");
return ;
}
for(size_t t=0; t<cloud->points.size(); ++t) {
position_x[position_num] = cloud->points[t].x;
position_y[position_num] = cloud->points[t].y;
position_num++;
}
}
return ;
}
使用方式:调用Get_position_data()即可