点云:点云读写——libLAS,VS2010,Qt4.8.6

虽然libLAS已经被PDAL取代,但是不可否认,它是一个很nice的库。

libLAS库可以通过OSGeo4W下载。

一、配置环境

分享给有需要的人,代码质量勿喷。

二、读取点云并存储

//libLAS
#include "liblas\liblas.hpp"
#include "liblas\reader.hpp"
#include "liblas\writer.hpp"


//read las and store points to mhPoints
void libLAStest::ReadLas(const QString &xjLasPath, QMultiHash<int, xjPoint> &mhPoints)
{
	std::ifstream ifs;
	ifs.open(xjLasPath.toStdString(),std::ios::in|std::ios::binary);
	liblas::ReaderFactory f;
	liblas::Reader xjReader = f.CreateWithStream(ifs);
	liblas::Header const& xjHeader = xjReader.GetHeader();

#pragma region
	QStringList xjInfo;
	//count
	int pCount = xjHeader.GetPointRecordsCount();
	xjInfo.append("points count: " + QString::number(pCount));
	//range
	double maxX =xjHeader.GetMaxX();
	double minX = xjHeader.GetMinX();
	double maxY = xjHeader.GetMaxY();
	double minY = xjHeader.GetMinY();
	double maxZ = xjHeader.GetMaxZ();
	double minZ = xjHeader.GetMinZ();
	double delatX = maxX-minX;
	double delatY = maxY-minY;
	double delatZ = maxZ-minZ;
	xjInfo.append("range X: " + QString::number(delatX));
	xjInfo.append("range Y: " + QString::number(delatY));
	xjInfo.append("range Z: " + QString::number(delatZ));
	xjInfo.append("-----------");
	//first point's information
	xjInfo.append( "first point's information:");
	if( xjReader.ReadPointAt(0))
	{
		liblas::Point const & p =xjReader.GetPoint();
		xjInfo.append("X = " + QString::number(p.GetX(),'f',4));
		xjInfo.append("Y = " + QString::number(p.GetY(),'f',4));
		xjInfo.append("Z = " + QString::number(p.GetZ(),'f',4));
		xjInfo.append("Red = " + QString::number(p.GetColor().GetRed() >> 8));
		xjInfo.append("Green = " + QString::number(p.GetColor().GetGreen()>>8));
		xjInfo.append("Blue = " + QString::number(p.GetColor().GetBlue()>>8));
		xjInfo.append("Intensity = " + QString::number(p.GetIntensity()));
		xjInfo.append("PointSourceID = " + QString::number(p.GetPointSourceID()));
		xjInfo.append("GPStime = " + QString::number(p.GetTime()));
	}
	ui.listWidget->addItems(xjInfo);
#pragma endregion

	//while( xjReader.ReadNextPoint() )
	//{
	//	liblas::Point const & p =xjReader.GetPoint();
	//	//...
	//}
	for(unsigned long i = 0;i <pCount; i++)
	{
		if( xjReader.ReadPointAt(i))
		{
			liblas::Point const & p =xjReader.GetPoint();
			xjPoint xjP;
			xjP.x =p.GetX();
			xjP.y = p.GetY();
			xjP.z = p.GetZ();
			xjP.r = p.GetColor().GetRed();
			xjP.g = p.GetColor().GetGreen();
			xjP.b = p.GetColor().GetBlue();
			xjP.intensity = p.GetIntensity();
			xjP.pointSourceID = p.GetPointSourceID();
			xjP.GPStime = p.GetTime();

			mhPoints.insert(i,xjP);
		}
	}
}

三、写las

//write las
void libLAStest::WriteLas(const QString &xjSaveLasPath, const QMultiHash<int, xjPoint> &mhPoints)
{
	std::ofstream ofs;
	if (!liblas::Create(ofs,xjSaveLasPath.toStdString()))
		return;

	int key = mhPoints.constBegin().key();
	xjPoint ps = mhPoints.value(key);
	double xoffset = floor(ps.x);
	double yoffset = floor(ps.y);
	double zoffset = floor(ps.z);

	liblas::Header header;
	header.SetVersionMajor(1);
	header.SetVersionMinor(2);
	header.SetOffset(xoffset,yoffset,zoffset);
	header.SetDataFormatId(liblas::ePointFormat3);
	header.SetPointRecordsCount(mhPoints.size());
	header.SetScale(0.0001,0.0001,0.0001);

	liblas::Writer writer(ofs,header);

	double Max_x , Max_y , Max_z;
	double Min_x , Min_y , Min_z;
	Max_x = Min_x = ps.x ;
	Max_y = Min_y = ps.y ;
	Max_z = Min_z = ps.z ;
	for (int i=0;i<mhPoints.size();i++)
	{
		xjPoint p = mhPoints.value(i);

		liblas::Point point;
		point.SetRawX((p.x-xoffset)*10000);
		point.SetRawY((p.y-yoffset)*10000);
		point.SetRawZ((p.z-zoffset)*10000);

		liblas::Color color;
		color.SetRed(p.r);
		color.SetGreen(p.g);
		color.SetBlue(p.b);
		point.SetColor(color);

		point.SetIntensity(p.intensity);
		point.SetPointSourceID(p.pointSourceID);
		point.SetTime(p.GPStime);

		writer.WritePoint(point);

		if(p.x > Max_x)Max_x = p.x;
		if(p.y > Max_y)Max_y = p.y;
		if(p.z > Max_z)Max_z = p.z;
		if(p.x < Min_x)Min_x = p.x;
		if(p.y < Min_y)Min_y = p.y;
		if(p.z < Min_z)Min_z = p.z;
	}
	
	header.SetMax(Max_x ,Max_y ,Max_z);
	header.SetMin(Min_x ,Min_y ,Min_z);

	writer.SetHeader(header);
	writer.WriteHeader();
	ofs.close();
}

四、button功能

void libLAStest::on_btnLas_clicked()
{
	QString xjLasPath = QFileDialog::getOpenFileName(this, "select LAS", "D:/", "*.las");
	ui.lineEdit->setText(xjLasPath);

	
	QMultiHash<int, xjPoint> mhPoints;
	ReadLas(xjLasPath, mhPoints);

	QString xjSaveLasPath = QCoreApplication::applicationDirPath() + "/save.las";
	WriteLas(xjSaveLasPath, mhPoints);
	ui.listWidget->addItem("-------------");
	ui.listWidget->addItem("the new las file have save to");
	ui.listWidget->addItem(xjSaveLasPath);
}

发布了61 篇原创文章 · 获赞 56 · 访问量 7万+
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览