您的位置:首页 > 编程语言 > C语言/C++

Qt工程中c++实现wrl到pcd格式转换

2017-01-05 09:34 288 查看
pcl1.8.0+QT5.7.0+vs2013 win7 x64环境配置

//读取文本型和二进制型点云数据
void onOpen()
{
//打开文件
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open PointCloud"), ".",
tr("Open PCD files(*.pcd);;Open WRL files(*.wrl)"));
//filename类型转换
std::string file_name = fileName.toStdString();
//获取文件名称以及扩展名
vector<string> tokens;
boost::char_separator<char> sep(".");
boost::tokenizer<boost::char_separator<char> > tok(file_name, sep);
tokens.clear();
std::copy(tok.begin(), tok.end(), std::back_inserter(tokens));

if (!fileName.isEmpty())
{
//若是其他类型
if (tokens[1] != "pcd")
{
//查找特定位置(所需点坐标)l为所在行数;l-35为点云个数
string fout_name = tokens[0] + ".pcd";
int linenum = 0;
int l;
ifstream in(file_name);
string line;
string s;
int i = 0;
string key = "texCoord DEF texcoord0 TextureCoordinate";

while (getline(in, line))
{
if (line.find(key) != string::npos)

{
// cout << linenum << " ";
l = linenum;
}
linenum++;
}
//写入
ofstream outfile(fout_name, ios::out | ios::trunc);
outfile << "# .PCD v.5 - Point Cloud Data file format" << endl;
outfile << "VERSION .5" << endl;
outfile << "FIELDS x y z" << endl;
outfile << "SIZE 4 4 4" << endl;
outfile << "TYPE F F F" << endl;
outfile << "COUNT 1 1 1" << endl;
outfile << "WIDTH " << l - 35 << endl;
outfile << "HEIGHT 1" << endl;
outfile << "POINTS " << l - 35 << endl;
outfile << "DATA ascii" << endl;

//添加点
ifstream inFile;
inFile.open(file_name);
while (getline(inFile, s, '\n'))
{

if (i > 32 && i < l - 2)
outfile << s << endl;
i++;
}
inFile.close();
outfile.close();

//显示
pcl::PCLPointCloud2 cloud2;
//pcl::PointCloud<Eigen::MatrixXf> cloud2;
Eigen::Vector4f origin;
Eigen::Quaternionf orientation;
int pcd_version;
int data_type;
unsigned int data_idx;
int offset = 0;
pcl::PCDReader rd;
rd.readHeader(fout_name, cloud2, origin, orientation, pcd_version, data_type, data_idx);

if (data_type == 0)
{
pcl::io::loadPCDFile(fout_name, *cloud);
}
else if (data_type == 2)
{
pcl::PCDReader reader;
reader.read<pcl::PointXYZ>(fout_name, *cloud);
}

viewer->updatePointCloud(cloud, "cloud");
viewer->resetCamera();
ui.qvtkWidget->update();

}

else
{

//sensor_msgs::PointCloud2 cloud2;
pcl::PCLPointCloud2 cloud2;
//pcl::PointCloud<Eigen::MatrixXf> cloud2;
Eigen::Vector4f origin;
Eigen::Quaternionf orientation;
int pcd_version;
int data_type;
unsigned int data_idx;
int offset = 0;
pcl::PCDReader rd;
rd.readHeader(file_name, cloud2, origin, orientation, pcd_version, data_type, data_idx);

if (data_type == 0)
{
pcl::io::loadPCDFile(fileName.toStdString(), *cloud);
}
else if (data_type == 2)
{
pcl::PCDReader reader;
reader.read<pcl::PointXYZ>(fileName.toStdString(), *cloud);
}

viewer->updatePointCloud(cloud, "cloud");
viewer->resetCamera();
ui.qvtkWidget->update();
}
}
}
运行结果









内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: