循环读取存储格式为32位单精度浮点数的二进制三维点数据文件 转换为点云数据并保存
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/impl/point_types.hpp>
#include <pcl/point_cloud.h>
#include <fstream>
#include <vector>
using namespace std;
// int main()
// {
// BYTE s[4];
// // s[3] = 0x00;
// // s[2] = 0x00;
// // s[1] = 0x10;
// // s[0] = 0x2c;
// //ff8f 0843
// s[3] = 0x43;
// s[2] = 0x77;
// s[1] = 0xb1;
// s[0] = 0x01;
// float *pf = (float *)s;
// printf("10进制%g\n", *pf);
// }
class Point_3d
{
public:
float m_x;
float m_y;
float m_z;
public:
Point_3d(float &x, float &y, float &z) : m_x(x), m_y(y), m_z(z)
{
}
};
vector<Point_3d> get_fs()
{
ifstream ifs("scan.bin", std::ios::binary);
float xyz[3] = {};
// float xyz_1[3] = {};
vector<Point_3d> pts;
if (!ifs)
{
cerr << "open error !" << endl;
abort();
}
while (!ifs.eof())
{
ifs.read((char *)xyz, sizeof(xyz));
Point_3d pt(xyz[0], xyz[1], xyz[2]);
pts.push_back(pt);
// ifs.seekg(sizeof(xyz), ios::cur);
// ifs.read((char *)xyz_1, sizeof(xyz_1));
}
ifs.clear();
ifs.close();
return pts;
}
int main(int argc, char **argv)
{
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
// Fill in the cloud data
cloud.width = 1000;
cloud.height = 1000;
cloud.is_dense = false;
cloud.points.resize(cloud.width * cloud.height);
cloud_ptr->width = cloud.width;
cloud_ptr->height = cloud.height;
cloud_ptr->is_dense = cloud.is_dense;
// cloud_ptr->points.resize (cloud.width * cloud.height);
// for (size_t i = 0; i < cloud.points.size (); ++i)
// {
// // cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
// // cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
// // cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
// pcl::PointXYZ p;
// p.x = 1024 * rand () / (RAND_MAX + 1.0f);
// p.y = 1024 * rand () / (RAND_MAX + 1.0f);
// p.z = 1024 * rand () / (RAND_MAX + 1.0f);
// cloud.points[i].x = p.x;
// cloud.points[i].y = p.y;
// cloud.points[i].z = p.z;
// cloud_ptr->points.push_back(p);
// }
vector<Point_3d> pts = get_fs();
for (int i = 0; i < pts.size(); i++)
{
cloud.points[i].x = pts[i].m_x;
cloud.points[i].y = pts[i].m_y;
cloud.points[i].z = pts[i].m_z;
pcl::PointXYZ p;
p.x = pts[i].m_x;
p.y = pts[i].m_y;
p.z = pts[i].m_z;
cloud_ptr->points.push_back(p);
}
std::cerr << "Saving to pcd file " << std::endl;
pcl::io::savePCDFileASCII("test_pcd0.pcd", cloud);
pcl::io::savePCDFile("test_pcd1.pcd", *cloud_ptr);
std::cerr << "Saved " << cloud.points.size() << " data points to test_pcd.pcd." << std::endl;
std::cerr << "Saving to ply file " << std::endl;
pcl::io::savePLYFile("test_ply.ply", cloud);
for (size_t i = 0; i < cloud.points.size(); ++i)
{
// std::cerr << " " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;
std::cerr << " " << cloud_ptr->points[i].x << " " << cloud_ptr->points[i].y << " " << cloud_ptr->points[i].z << std::endl;
}
return (0);
}
//采用CPP模式读二进制文件
// void DataRead_CPPMode()
// {
// double pos[200];
// ifstream f("binary.dat", ios::binary);
// if(!f)
// {
// cout << "读取文件失败" <<endl;
// return;
// }
// f.read((char*)pos,200*sizeof(double));
// for(int i = 0; i < 200; i++)
// cout << pos[i] <<endl;
// f.close();
// }
点云图效果