C++ 读取PLY格式点云数据教程
以下是一个简单的C++程序,可以读取PLY格式的点云数据:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
struct Point {
float x, y, z;
};
struct PlyHeader {
int vertexCount;
};
int main(int argc, char* argv[]) {
if (argc < 2) {
cerr << 'Usage: ' << argv[0] << ' <ply file>' << endl;
return 1;
}
ifstream file(argv[1], ios::binary);
if (!file.is_open()) {
cerr << 'Error opening file ' << argv[1] << endl;
return 1;
}
string line;
PlyHeader header;
vector<Point> points;
// Read header
while (getline(file, line)) {
if (line == 'end_header') {
break;
}
if (line.substr(0, 14) == 'element vertex') {
header.vertexCount = stoi(line.substr(15));
}
}
// Read vertex data
float x, y, z;
for (int i = 0; i < header.vertexCount; ++i) {
file.read((char*)&x, sizeof(float));
file.read((char*)&y, sizeof(float));
file.read((char*)&z, sizeof(float));
points.push_back({ x, y, z });
}
file.close();
cout << 'Read ' << points.size() << ' points from file ' << argv[1] << endl;
return 0;
}
该程序假定PLY文件的第一个元素为顶点,并且顶点只包含x、y、z三个属性。如果PLY文件包含其他元素或属性,程序需要相应地进行修改。
原文地址: https://www.cveoy.top/t/topic/nNze 著作权归作者所有。请勿转载和采集!