The overall implementation idea is: first get all the file names under the folder, and then traverse and open the file to get the coordinates from it with PCL
The header files required by the program:
#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 ;
}
How to use: just call Get_position_data()