看了下liblas的文档,但是没有一次调通,特意记录如下:
//读入.las,写点云
void writePointCloudFromLas(const char* strInputLasName, const char* strOutPutPointCloudName)
{
//打开las文件
std::ifstream ifs;
ifs.open(strInputLasName, std::ios::in | std::ios::binary);
liblas::ReaderFactory readerFactory;
liblas::Reader reader = readerFactory.CreateWithStream(ifs);
//写点云
pcl::PointCloud<pcl::PointXYZ> cloudOutput;
cloudOutput.clear();
while (reader.ReadNextPoint())
{
double x = reader.GetPoint().GetX();
double y = reader.GetPoint().GetY();
double z = reader.GetPoint().GetZ();
pcl::PointXYZ thePt(x, y, z);
cloudOutput.push_back(thePt);
}
cloudOutput.width = cloudOutput.size();
cloudOutput.height = 1;
cloudOutput.is_dense = false;
cloudOutput.resize(cloudOutput.width * cloudOutput.height);
pcl::io::savePCDFileASCII("E:\\DEM-2016_2.pcd", cloudOutput);
cloudOutput.clear();
}
//读入点云,写.las
void writeLasFromPointCloud(const char* strInputPointCloudName, const char* strOutLasName)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader pcdreader;
pcdreader.read(strInputPointCloudName, *cloud);
std::cout << "总数:" << cloud->points.size() << std::endl;
//写liblas,
std::ios::openmode m = std::ios::out | std::ios::in | std::ios::binary | std::ios::ate;
std::ofstream ofs;
if (!liblas::Create(ofs, strOutLasName))
{
std::cout << "无法创建.las" << std::endl;
return;
}
ofs.close();
std::ofstream * ofs2 = new std::ofstream(strOutLasName, m);
if (!ofs2->is_open())
{
std::cout << "打不开.las" << std::endl;
return;
}
else
{
std::cout << "能打开.las" << std::endl;
}
liblas::Header header;
liblas::Writer writer(*ofs2, header);
liblas::Point point(&header);
for (size_t i = 0; i < cloud->points.size(); i++)
{
double x = cloud->points[i].x;
double y = cloud->points[i].y;
double z = cloud->points[i].z;
point.SetX(x);
point.SetY(y);
point.SetZ(z);
writer.WritePoint(point);
std::cout << x << "," << y << "," << z << std::endl;
}
}