针对于批量处理问题的解决,根据需要将txt格式数据转换为pcd格式
适用于txt文件名按照顺序排列的文件夹
电脑存放位置:F:\muchlyBackup\muchlyBackup\BatchProcessingFunctionTxtTOPcd
//批量将 txt 转换成 pcd 格式 适用于txt文件名按照顺序排列的文件夹
#include<iostream>
#include<fstream>
#include <string>
#include <vector>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
using namespace std;
typedef struct txtPoint_3D{
double x;
double y;
double z;
}txtPoint_3D;
/*
从 txt 格式的文件中读取点云数据,然后再存储为pcd格式文件
readPath 读取 txt 格式文件路径
savePath 写入 pcd 格式文件路径
*/
void txtTOpcd(const char* readPath,const char* savePath)//为什么 char* 类型,因为路径本身就是c语言形式的字符串,进行类型转换时的需要。
{
FILE *fp_txt;
txtPoint_3D txtPoint; ///中间变量,用于存放单行数据的变量
vector<txtPoint_3D> txtPoints; ///用于存放整个txt文档内的数据变量
if ((fp_txt = fopen(readPath, "r")) !=NULL) ///if (fp_txt = fopen(readPath, "r")) //可以简化NULL==0
{
while (fscanf(fp_txt, "%lf;%lf;%lf", &txtPoint.x, &txtPoint.y, &txtPoint.z) != EOF) ///判断是否读到最后一行
{
txtPoints.push_back(txtPoint); ///读一行放进入
}
}
///将txt文档中的全部数据放到 pcd 中,先给这个cloud 分配空间大小 再放入
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
cloud->width = txtPoints.size();
cloud->height = 1;
cloud->is_dense = false;
cloud->points.resize(cloud->width * cloud->height);
for (size_t i = 0; i < cloud->points.size(); ++i){
cloud->points[i].x = txtPoints[i].x;
cloud->points[i].y = txtPoints[i].y;
cloud->points[i].z = txtPoints[i].z;
}
pcl::io::savePCDFileASCII(savePath, *cloud);
}
int main()
{
for (int i = 0; i < 6; ++i)
{
cout << i << endl;
string a = "home\\2-" + to_string(i) + ".txt";
string b = "home01\\2-" + to_string(i) + ".pcd";
/*strcpy(readPath, a.c_str());
strcpy(savePath, b.c_str());*/
const char* readPath = a.c_str();
const char* savePath = b.c_str();
txtTOpcd(readPath, savePath);
}
return 0;
}