用for循环输出点云ply文件的所有点的三维坐标的C++代码
下面是一个用C++编写的示例代码,用于读取并输出PLY文件中所有点的三维坐标:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct Point {
float x, y, z;
};
int main() {
std::string filename = "example.ply";
std::ifstream file(filename, std::ios::binary);
if (!file) {
std::cout << "Unable to open file: " << filename << std::endl;
return 1;
}
std::string line;
std::vector<Point> points;
bool isDataSection = false;
while (std::getline(file, line)) {
if (!isDataSection && line.find("end_header") != std::string::npos) {
isDataSection = true;
} else if (isDataSection) {
Point point;
file >> point.x >> point.y >> point.z;
points.push_back(point);
}
}
file.close();
for (const auto& point : points) {
std::cout << "x: " << point.x << ", y: " << point.y << ", z: " << point.z << std::endl;
}
return 0;
}
请注意,代码假定PLY文件的数据部分以end_header为标志开始。此外,还需要将example.ply替换为实际的PLY文件名
原文地址: http://www.cveoy.top/t/topic/hIC5 著作权归作者所有。请勿转载和采集!