PDAL库读写多种格式的点云

7 篇文章 3 订阅
2 篇文章 1 订阅

 

// PDALTest.cpp : 定义控制台应用程序的入口点。
//
#pragma execution_character_set("utf-8")  //解决中文

#include "stdafx.h"

#include <iostream>

#include <memory>
#include <pdal/PointTable.hpp>
#include <pdal/PointView.hpp>
#include <pdal/Options.hpp>
#include <pdal/io/BufferReader.hpp>
#include <pdal/StageFactory.hpp>
#include <pdal/Stage.hpp>

#include <pdal/Filter.hpp>
#include <pdal/filters/DividerFilter.hpp>



#include <pdal/io/LasReader.hpp>
#include <pdal/io/LasHeader.hpp>

#include <pdal/io/PlyReader.hpp>
#include <pdal/io/PlyWriter.hpp>

#include <pdal/io/TextReader.hpp>
#include <pdal/io/TextWriter.hpp>

#include <pdal/PluginDirectory.hpp>
#include <pdal/PluginManager.hpp>

using namespace std;

struct las_Point
{
	double x;
	double y;
	double z;
	int intensity;
};

vector<las_Point> pointCloud;

void fillView(pdal::PointViewPtr view)
{
	for (int i = 0; i < pointCloud.size(); ++i)
	{
		view->setField(pdal::Dimension::Id::X, i, pointCloud[i].x);
		view->setField(pdal::Dimension::Id::Y, i, pointCloud[i].y);
		view->setField(pdal::Dimension::Id::Z, i, pointCloud[i].z);
		view->setField(pdal::Dimension::Id::Intensity, i, pointCloud[i].intensity);
	}
}

//pdal库读写ply
void WriteText()
{
	using namespace pdal;
	using namespace pdal::Dimension;

	std::string fileName = "d:/pdal_test.txt";

	Options xjOptions;
	xjOptions.add("filename", fileName);

	PointTable table;
	table.layout()->registerDim(Dimension::Id::X);
	table.layout()->registerDim(Dimension::Id::Y);
	table.layout()->registerDim(Dimension::Id::Z);
	table.layout()->registerDim(Dimension::Id::Intensity);
	
	PointViewPtr view(new PointView(table));
	fillView(view);
	BufferReader xjBufferReader;
	xjBufferReader.addView(view);

	StageFactory factory;
	std::string r_drivername = factory.inferReaderDriver(fileName);
	std::string w_drivername = factory.inferWriterDriver(fileName);
	Stage *writer = factory.createStage(w_drivername);
	//Stage *writer = factory.createStage("writers.e57");
	writer->addOptions(xjOptions);

	writer->setInput(xjBufferReader);
	writer->setOptions(xjOptions);
	writer->prepare(table);

	writer->execute(table);
}

//pdal库读写ply
void readText()
{
	using namespace pdal;
	using namespace pdal::Dimension;

	pdal::TextReader text_reader;
	string xjFileName = "d:/pdal_test.txt";
	pdal::Option las_opt("filename", xjFileName);//参数1:"filename"(键)
	pdal::Options las_opts;
	las_opts.add(las_opt);
	las_opts.add("skip", 1);   //忽略第一行 ,一定要设置
	las_opts.add("header", "X, Y, Z, Intensity");  //读取的文件头,一定要设置
	pdal::PointTable table;

	text_reader.setOptions(las_opts);
	text_reader.prepare(table);

	pdal::PointViewSet point_view_set = text_reader.execute(table);
	pdal::PointViewPtr point_view = *point_view_set.begin();
	pdal::Dimension::IdList dims = point_view->dims();

	for (pdal::PointId idx = 0; idx < point_view->size(); ++idx)
	{
		las_Point cur_point;
		cur_point.x = point_view->getFieldAs<double>(pdal::Dimension::Id::X, idx);
		cur_point.y = point_view->getFieldAs<double>(pdal::Dimension::Id::Y, idx);
		cur_point.z = point_view->getFieldAs<double>(pdal::Dimension::Id::Z, idx);
		int Pintensity = point_view->getFieldAs<int>(pdal::Dimension::Id::Intensity, idx);
		int PointSourceID = point_view->getFieldAs<int>(pdal::Dimension::Id::PointSourceId, idx);
		double GPStime = point_view->getFieldAs<double>(pdal::Dimension::Id::GpsTime, idx);
		int red = point_view->getFieldAs<int>(pdal::Dimension::Id::Red, idx);
		int green = point_view->getFieldAs<int>(pdal::Dimension::Id::Green, idx);
		int blue = point_view->getFieldAs<int>(pdal::Dimension::Id::Blue, idx);

		pointCloud.push_back(cur_point);
	}
}

//pdal库读写ply
void readPly()
{
	using namespace pdal;
	using namespace pdal::Dimension;

	pdal::PlyReader ply_reader;
	string xjFileName = "d:/pdal_test.ply";
	pdal::Option las_opt("filename", xjFileName);//参数1:"filename"(键)
	pdal::Options las_opts;
	las_opts.add(las_opt);

	pdal::PointTable table;

	ply_reader.setOptions(las_opts);
	ply_reader.prepare(table);

	pdal::PointViewSet point_view_set = ply_reader.execute(table);
	pdal::PointViewPtr point_view = *point_view_set.begin();
	pdal::Dimension::IdList dims = point_view->dims();

	for (pdal::PointId idx = 0; idx < point_view->size(); ++idx)
	{
		las_Point cur_point;
		cur_point.x = point_view->getFieldAs<double>(pdal::Dimension::Id::X, idx);
		cur_point.y = point_view->getFieldAs<double>(pdal::Dimension::Id::Y, idx);
		cur_point.z = point_view->getFieldAs<double>(pdal::Dimension::Id::Z, idx);
		int Pintensity = point_view->getFieldAs<int>(pdal::Dimension::Id::Intensity, idx);
		int PointSourceID = point_view->getFieldAs<int>(pdal::Dimension::Id::PointSourceId, idx);
		double GPStime = point_view->getFieldAs<double>(pdal::Dimension::Id::GpsTime, idx);
		int red = point_view->getFieldAs<int>(pdal::Dimension::Id::Red, idx);
		int green = point_view->getFieldAs<int>(pdal::Dimension::Id::Green, idx);
		int blue = point_view->getFieldAs<int>(pdal::Dimension::Id::Blue, idx);

		pointCloud.push_back(cur_point);
	}
}

//pdal库读写ply
void WritePly()
{
	using namespace pdal;
	using namespace pdal::Dimension;

	pdal::PlyReader ply_reader;
	
	std::string fileName = "d:/pdal_test.ply";

	Options xjOptions;
	xjOptions.add("filename", fileName);
	xjOptions.add("storage_mode", "ascii");   //Valid values are ‘ascii’, ‘little endian’, ‘big endian’. [Default: “ascii”]
	//xjOptions.add("dims", "Y=int32_t, X,Red=char");  //默认所有维度为存储类型

	//xjOptions.add("faces", true);   //Write a mesh as faces in addition to writing points as vertices. [Default: false]
	//xjOptions.add("sized_types", true);
	xjOptions.add("precision", "g-style");
	
	PointTable table;
	table.layout()->registerDim(Dimension::Id::X);
	table.layout()->registerDim(Dimension::Id::Y);
	table.layout()->registerDim(Dimension::Id::Z);
	table.layout()->registerDim(Dimension::Id::Intensity);
	PointViewPtr view(new PointView(table));
	fillView(view);
	BufferReader xjBufferReader;
	xjBufferReader.addView(view);

	StageFactory factory;
	std::string r_drivername = factory.inferReaderDriver(fileName);
	std::string w_drivername = factory.inferWriterDriver(fileName);
	Stage *writer = factory.createStage(w_drivername);
	writer->addOptions(xjOptions);

	writer->setInput(xjBufferReader);
	writer->setOptions(xjOptions);
	writer->prepare(table);
	writer->execute(table);
}

//pdal库读写las
void WriteLASPC()
{
	using namespace pdal;
	using namespace pdal::Dimension;

	std::string fileName = "d:/pdal_test.las";
	Options xjOptions;
	xjOptions.add("filename", fileName);
	
	double xoffset = 0.0, yoffset = 0.0, zoffset = 0.0;

	xjOptions.add("offset_x", xoffset);
	xjOptions.add("offset_y", yoffset);
	xjOptions.add("offset_z", zoffset);
	xjOptions.add("scale_x", 0.001);
	xjOptions.add("scale_y", 0.001);
	xjOptions.add("scale_z", 0.001);

	//xjOptions.add("type", "filters.splitter");
	//xjOptions.add("scale_z", "100");
	//xjOptions.add("scale_z", "638900.0");
	//xjOptions.add("scale_z", "835500.0");

	PointTable table;
	table.layout()->registerDim(Dimension::Id::X);
	table.layout()->registerDim(Dimension::Id::Y);
	table.layout()->registerDim(Dimension::Id::Z);
	//table.layout()->registerDim(Dimension::Id::Intensity);

	PointViewPtr view(new PointView(table));
	fillView(view);

	BufferReader xjBufferReader;
	xjBufferReader.addView(view);
	
	StageFactory factory;
	std::string r_drivername = factory.inferReaderDriver(fileName);
	std::string w_drivername = factory.inferWriterDriver(fileName);
	//Stage *writer = factory.createStage(w_drivername);
	Stage *writer = factory.createStage("writers.las");
	
	writer->setInput(xjBufferReader);
	writer->setOptions(xjOptions);
	writer->prepare(table);
	writer->execute(table);
}

//pdal库读写las
void readlasPC()
{
	string xjFileName = "F:\\pointCloud_data\\zhangjiang8\\pointcloud\\181013_030701-11-16-47-028.las";//中文路径可能报错
	pdal::Option las_opt("filename", xjFileName);//参数1:"filename"(键)
	pdal::Options las_opts;
	las_opts.add(las_opt);

	pdal::PointTable table;
	pdal::LasReader las_reader;



	las_reader.setOptions(las_opts);
	las_reader.prepare(table);

	pdal::PointViewSet point_view_set = las_reader.execute(table);

	pdal::PointViewPtr point_view = *point_view_set.begin();
	pdal::Dimension::IdList dims = point_view->dims();

	pdal::LasHeader las_header = las_reader.header();

	unsigned int PointCount = las_header.pointCount();
	//char* projstr = table.spatialRef().getWKT(pdal::SpatialReference::eCompoundOK).c_str();
	double scale_x = las_header.scaleX();
	double scale_y = las_header.scaleY();
	double scale_z = las_header.scaleZ();
	double offset_x = las_header.offsetX();
	double offset_y = las_header.offsetY();
	double offset_z = las_header.offsetZ();
	double xmin = las_header.minX();
	double xmax = las_header.maxX();
	double ymin = las_header.minY();
	double ymax = las_header.maxY();
	double zmin = las_header.minZ();
	double zmax = las_header.maxZ();

	unsigned int n_features = las_header.pointCount();
	int count = 0;
	for (pdal::PointId idx = 0; idx < point_view->size(); ++idx)
	{
		las_Point cur_point;
		cur_point.x = point_view->getFieldAs<double>(pdal::Dimension::Id::X, idx);
		cur_point.y = point_view->getFieldAs<double>(pdal::Dimension::Id::Y, idx);
		cur_point.z = point_view->getFieldAs<double>(pdal::Dimension::Id::Z, idx);
		cur_point.intensity = point_view->getFieldAs<int>(pdal::Dimension::Id::Intensity, idx);
		int PointSourceID = point_view->getFieldAs<int>(pdal::Dimension::Id::PointSourceId, idx);
		double GPStime = point_view->getFieldAs<double>(pdal::Dimension::Id::GpsTime, idx);
		int red = point_view->getFieldAs<int>(pdal::Dimension::Id::Red, idx);
		int green = point_view->getFieldAs<int>(pdal::Dimension::Id::Green, idx);
		int blue = point_view->getFieldAs<int>(pdal::Dimension::Id::Blue, idx);

		
		pointCloud.push_back(cur_point);
	}


	/*double deltaX = xmax - xmin;
	cout << "deltaX=" << fixed << setprecision(4) << deltaX << endl;

	double ymin = las_header.minY();
	double ymax = las_header.maxY();
	double deltaY = ymax - ymin;
	cout << "deltaY=" << fixed << setprecision(4) << deltaY << endl;

	double zmin = las_header.minZ();
	double zmax = las_header.maxZ();
	double deltaZ = zmax - zmin;
	cout << "deltaZ=" << fixed << setprecision(4) << deltaZ << endl;

	unsigned int n_features = las_header.pointCount();
	cout << "PointCount=" << n_features << endl;*/
}

int main()
{
	readlasPC();
	//readPly();
	//readText();

	//WriteLASPC();
	//WritePly();
	//WriteText();
	dividerLAS();

	system("pause");
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值