通过调用pcl相关库及函数,将pcd、obj、las、ply格式3D文件统一转换成ply点云格式文件进行存储及显示,调用OpenGL相关函数进行三维显示及鼠标、键盘的简单操作。
1、transform.h(建立一个抽象类Modelloader,由三个子类分别继承,使其读取转换不同的3D文件)
#include <iostream>
#include <cstdlib>
#include<string>
#include <liblas/liblas.hpp>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include<pcl/PCLPointCloud2.h>
#include <pcl/io/obj_io.h>
using namespace pcl;
using namespace pcl::io;
using namespace std;
class ModelLoader {
public:
string filename;
string savefilename;
ModelLoader(string filename="", string savefilename="") {
this->filename = filename;
this->savefilename = savefilename;
}
virtual void TO_PLY(){}
~ModelLoader() {
this->filename = "";
}
};
class LasLoader:public ModelLoader {
public:
LasLoader(string filename="",string savefilename="") :ModelLoader(filename,savefilename) {}
void TO_PLY();
};
class PCDLoader :public ModelLoader {
public:
PCDLoader(string filename = "", string savefilename = "") :ModelLoader(filename, savefilename) {}
void TO_PLY();
};
class OBJLoader :public ModelLoader {
public:
OBJLoader(string filename = "", string savefilename = "") :ModelLoader(filename, savefilename) {}
void TO_PLY();
};
2、LAS_to_PLY.cpp
#include "transform.h"
void LasLoader::TO_PLY()
{
std::ifstream ifs;
ifs.open(this->filename, ios::in | ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
unsigned long int nbPoints = reader.GetHeader().GetPointRecordsCount();
pcl::PointCloud<pcl::PointXYZRGB> cloud;
cloud.width = nbPoints;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize(cloud.width * cloud.height);
int i = 0;
uint16_t r1, g1, b1;
int r2, g2, b2;
uint32_t rgb;
while (reader.ReadNextPoint())
{
cloud.points[i].x = (reader.GetPoint().GetX());
cloud.points[i].y = (reader.GetPoint().GetY());
cloud.points[i].z = (reader.GetPoint().GetZ());
r1 = (reader.GetPoint().GetColor().GetRed());
g1 = (reader.GetPo