C++读txt文件数据(只有X和Z坐标)存储三维点最后显示

实习的第二天接到了一个小任务,其中一个环节是将一堆只有x和z坐标的数据从txt文件中读出保存成点(xyz)的格式,数据是激光扫描所得,数据的y坐标需要后续的计算处理,下面的程序也是写了好半天才写出来,毕竟是刚刚开始工作,以后的效率一定会提上去的。

#include<iostream>
#include<sstream>
#include<string>
#include<fstream>
#include<vector>
#include<pcl/io/io.h>
#include<pcl/point_types.h>
#include<pcl/io/pcd_io.h>
#include<pcl/visualization/cloud_viewer.h>
using namespace std;
//创建坐标点 x y z 
typedef struct Point
{
	double x;
	double y;
	double z;
};
//按照字符串之间的空格进行字符切割
void split(const string& s, vector<string>& elems, char delim = ' ')
{
	stringstream ss;
	ss.str(s);   //str返回临时指针
	string ele;
	while (getline(ss, ele, delim))
	{
		elems.push_back(ele);//将切割后的单字符元素传入集合elems中
	}
}
int main()
{
	int row = 218;//修改 列
	ifstream Read;
	Read.open("C:\\Users\\asus\\Desktop\\程序\\demo1.txt");
	string String; //创建一个字符串 储存每行的字符串
	Point p;
	vector<Point> VecP;
	while (getline(Read, String))   //从文件中读的每一行传入String中
	{
		vector<string> strs;  //储存每行切割后的元素
		split(String, strs);  //自定义函数,将每行字符串 切割成字符
		string s = strs[0];  //每行的第一列为 x 
		double numberx;       //格式转换
		stringstream ss;
		ss << s;
		ss >> numberx;
		p.x = numberx;
		//遍历每一行的所有元素 
		for (int j = 1; j < row; j++)  //row:218 从第j列开始
		{
			string s = strs[j]; //格式转换
			double numberz;
			stringstream ss;
			ss << s;
			ss >> numberz;
			p.z = numberz;
			p.y = 0;
			VecP.push_back(p);
		}
	}

	/****************************************************/
	//显示  X  Z  点云
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	for (int i = 0; i < VecP.size(); i++)
	{
		pcl::PointXYZ pt;
		pt.x = VecP[i].x;
		pt.y = VecP[i].y;
		pt.z = VecP[i].z;
		cloud->push_back(pt);
	}
	pcl::visualization::CloudViewer viewer("dianyun");
	viewer.showCloud(cloud);
	/****************************************************/
	//cout << VecP[869].x << "  " << VecP[869].y << "  " << VecP[869].z << endl;
        //测试
	cout << VecP.size() << endl;
	system("pause");
	return 0;
}

点云显示是这个样子的,大概是一辆装有线缆的装载车的俯视图:

周一就已经开始实习了,打算实习半年这个样子,实习的过程中一定会接触到项目的,一定好好学习,提高自己,虽然加班到十点这一点我非常不能接受,可是谁让我是个菜鸟呢,趁着年轻多学习!!

猜你喜欢

转载自blog.csdn.net/qinlele1994/article/details/88673843