读取所有文件
#include<iostream>
#include<string>
#include<vector>
#include<io.h>
using namespace std;
void getFiles(string path, vector<string>&files)
{
//文件句柄
//千万不要用long,运行的时候会暴内存异常 要用intptr_t
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib &_A_SUBDIR))
{
//遇到文件夹跳进文件夹直到搜素到文件
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
接收数据
struct PCD
{
PointCloud::Ptr cloud;
std::string f_name;
PCD() : cloud(new PointCloud) {};
};
void loadData(std::vector<std::string>&argv, std::vector<PCD, Eigen::aligned_allocator<PCD> > &models)
{
std::string extension(".pcd");
// Suppose the first argument is the actual test model
for (int i = 0; i < argv.size(); i++)
{
std::string fname = argv[i];
// Needs to be at least 5: .plot
if (fname.size() <= extension.size())
continue;
std::transform(fname.begin(), fname.end(), fname.begin(), (int(*)(int))tolower);
//check that the argument is a pcd file
if (fname.compare(fname.size() - extension.size(), extension.size(), extension) == 0)
{
// Load the cloud and saves it into the global list of models
PCD m;
m.f_name = argv[1];
std::cout << "m.name: " << m.f_name << endl;
pcl::io::loadPCDFile(argv[1], *m.cloud);
//remove NAN points from the cloud
std::vector<int> indices;
pcl::removeNaNFromPointCloud(*m.cloud, *m.cloud, indices);
models.push_back(m);
}
}
}
int main()
{
const char *files_path = "C:\\Users\\xx\\Desktop\\pcdtest";
std::vector<string>files;
getFiles(files_path, files);
// Load data
std::vector<PCD, Eigen::aligned_allocator<PCD> > data;
loadData( files, data);
}