#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include<pcl/PCLPointCloud2.h>
#include<iostream>
#include<string>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/io/vtk_io.h>
#include "map_displayer.h"
#include <lasreaditem.hpp>
#include <lasreader_las.hpp>
#include <laswriter_las.hpp>
using namespace pcl;
using namespace pcl;
using namespace pcl::io;
using namespace std;
using namespace fusionpos;
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "gdi32.lib")
class A {
public:
A() : mapPointCloud(new pcl::PointCloud<PointXYZI>) {
clock_t start = clock();
pcl::io::loadPCDFile<PointXYZI>("C:\\Users\\y00444723\\Desktop\\pcd\\guomao.pcd", *mapPointCloud);
clock_t endOne = clock();
cout << "Load time:" << (double)(endOne - start) / CLOCKS_PER_SEC << "s" << endl;
};
~A() {
clock_t start1 = clock();
pcl::io::savePCDFileBinary("C:\\Users\\y00444723\\Desktop\\pcd\\guomao_save.pcd", *mapPointCloud);
clock_t endOne1 = clock();
cout << "Save time:" << (double)(endOne1 - start1) / CLOCKS_PER_SEC << "s" << endl;
};
public:
PointCloudPtr mapPointCloud;
};
int PCDtoPLYconvertor(string & input_filename, string& output_filename)
{
clock_t start, end;
pcl::PCLPointCloud2 cloud;
start = clock();
if (loadPCDFile(input_filename, cloud) < 0)
{
cout << "Error: cannot load the PCD file!!!" << endl;
return -1;
}
end = clock();
cout << "loadPCDFile load time " << end - start << endl;
PLYWriter writer;
start = clock();
writer.write(output_filename, cloud, Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), true, true);
end = clock();
cout <<"pcd2ply PLYWriter time " << end - start << endl;
return 0;
}
int pcd2Las(string& pcdFilePath, string& lasFilePath)
{
pcl::PointCloud<pcl::PointXYZI>::Ptr pcdCloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PCDReader pcdreader;
pcdreader.read(pcdFilePath.c_str(), *pcdCloud);
if (lasFilePath.empty() || lasFilePath.find_last_of(".") < 0)
{
//printf(" Error in lasread: input file is valid.");
return -1;
}
int pos = lasFilePath.find_last_of(".");
std::string ext = lasFilePath.substr(pos);
if (ext != ".las")
{
//printf(" Error in laswrite: %s is not *.las file.\n", output_lasfile.c_str());
return -1;
}
LASwriteOpener laswriteopener;
laswriteopener.set_file_name(lasFilePath.c_str());
if (!laswriteopener.active())
{
//printf(" Error in \"laswrite\": output lasfile is not specified.\n");
return -1;
}
LASheader lasheader;
lasheader.x_scale_factor = 1.0;
lasheader.y_scale_factor = 1.0;
lasheader.z_scale_factor = 1.0;
lasheader.x_offset = 0.0;
lasheader.y_offset = 0.0;
lasheader.z_offset = 0.0;
lasheader.point_data_format = 1;
lasheader.point_data_record_length = 28;
LASpoint laspoint;
laspoint.init(&lasheader, lasheader.point_data_format, lasheader.point_data_record_length, 0);
LASwriter* laswriter = NULL;
laswriter = laswriteopener.open(&lasheader);
if (laswriter == 0)
{
printf(" Error in \"laswrite\": could not open laswriter.\n");
return 0;
}
cout << "point num: " << pcdCloud->points.size() << endl;
// write points
if (pcdCloud->points.size() > 0)
{
for (size_t i = 0; i < pcdCloud->points.size(); ++i)
{
double x, y, z;
unsigned short intensity;
x = (pcdCloud->points[i].x - lasheader.x_offset) * lasheader.x_scale_factor;
y = (pcdCloud->points[i].y - lasheader.y_offset) * lasheader.y_scale_factor;
z = (pcdCloud->points[i].z - lasheader.z_offset) * lasheader.z_scale_factor;
intensity = pcdCloud->points[i].intensity;
// populate the point
laspoint.set_x(x);
laspoint.set_y(y);
laspoint.set_z(z);
laspoint.set_intensity(intensity);
/*laspoint.set_intensity((U16)i);
laspoint.set_gps_time(0.0006*i);*/
// write the point
laswriter->write_point(&laspoint);
// add it to the inventory
laswriter->update_inventory(&laspoint);
}
// update the header
laswriter->update_header(&lasheader, TRUE);
laswriter->close();
delete laswriter;
laswriter = NULL;
return 1;
} else {
printf(" Error in \"laswrite\": output pointset is empty.\n");
return 0;
}
return 0;
}
int LoadPLY(const string & plyPath)
{
clock_t start, end;
start = clock();
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPLYFile<pcl::PointXYZ>(plyPath.c_str(), *cloud) == -1) //* load the file
{
PCL_ERROR("Couldn't read file test_pcd.pcd \n");
return (-1);
}
end = clock();
cout << "LoadPLY time: " << end - start << endl;
}
int LoadLas(const string& lasPath)
{
//打开las文件
LASreadOpener lasreadopener;
clock_t start, end;
start = clock();
lasreadopener.set_file_name(lasPath.c_str());
LASreader* lasreader = lasreadopener.open();
end = clock();
cout << "LoadLas open time: " << end - start << endl;
int loop_time = 0;
while (lasreader->read_point())
{
//std::cout << lasreader->point.get_x() << " "
// << lasreader->point.get_y() << " "
// << lasreader->point.get_z() << std::endl;
loop_time++;
}
start = clock();
cout << "las read all_point time: " << start - end << endl;
lasreader->close();
delete lasreader;
return 1;
}
int main()
{
//MapDisplayer mapDisplayer;
//Eigen::Vector3d origLLH(116.483461736179, 39.944678684042, 28.2172);
//mapDisplayer.SetOrigLLH(origLLH);
//std::string mapPCDFilePath = "C:\\Users\\y00444723\\Desktop\\pcd\\guomao.pcd"; // "/home/cly/guomaopart2pcd/guomao.pcd";
//mapDisplayer.LoadMapCloud(mapPCDFilePath);
//// mapDisplayer.LoadOptimizedPos();
//Eigen::Vector3d curLLH(116.483461736179, 39.944678684042, 28.2172);
//double radius = 10;
//mapDisplayer.Display(curLLH, radius);
string input_filename = "C:\\Users\\y00444723\\Desktop\\pcd\\guomao.pcd";
string output_filename = "C:\\Users\\y00444723\\Desktop\\pcd\\guomao.ply";
string lasFilePath = "C:\\Users\\y00444723\\Desktop\\pcd\\guomao.las";
clock_t start, end;
start = clock();
int result = pcd2Las(input_filename, lasFilePath);
end = clock();
cout << "pcd2Las write time " << end - start << endl;
int A = LoadLas(lasFilePath);
A = LoadPLY(output_filename);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
start = clock();
pcl::PointCloud<pcl::PointXYZ>::Ptr pcdCloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader pcdreader;
pcdreader.read(input_filename.c_str(), *pcdCloud);
end = clock();
cout <<"PCDReader load time " << end - start << endl;
PCDtoPLYconvertor(input_filename, output_filename);
pcl::PolygonMesh mesh;
start = clock();;
if (pcl::io::loadPolygonFile("C:\\Users\\y00444723\\Desktop\\pcd\\guomao.ply", mesh)) {
PCL_ERROR("Couldnot read file.\n");
system("pause");
return(-1);
}
end = clock();
cout << "loadPolygonFile load mesh time " << end - start << endl;
return 0;
}