亲测可用
#include <iostream>
#include <fstream>
#include <vector>
struct Point3D {
float x, y, z;
};
void writePLY(const std::string& filename, const std::vector<Point3D>& points) {
std::ofstream ofs(filename);
if (!ofs.is_open()) {
std::cerr << "Failed to open file for writing: " << filename << std::endl;
return;
}
// PLY header
ofs << "ply\n";
ofs << "format ascii 1.0\n";
ofs << "element vertex " << points.size() << "\n";
ofs << "property float x\n";
ofs << "property float y\n";
ofs << "property float z\n";
ofs << "end_header\n";
// Points
for (const Point3D& p : points) {
ofs << p.x << " " << p.y << " " << p.z << "\n";
}
ofs.close();
}
int main() {
std::vector<Point3D> points = {
{1.0f, 2.0f, 3.0f},
{2.0f, 3.0f, 4.0f},
// ... 更多点数据
};
writePLY("output.ply", points);
return 0;
}