1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
struct Triangle {
float x1, y1, z1;
float x2, y2, z2;
float x3, y3, z3;
};
std::vector<Triangle> readSTL(std::string filename) {
std::vector<Triangle> triangles;
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (!file) {
throw std::runtime_error("Failed to open file: " + filename);
}
char header[80];
file.read(header, 80);
uint32_t num_triangles;
file.read(reinterpret_cast<char*>(&num_triangles), sizeof(num_triangles));
for (uint32_t i = 0; i < num_triangles; i++) {
// Read vertex coordinates
float v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z;
file.read(reinterpret_cast<char*>(&v1x), sizeof(float));
file.read(reinterpret_cast<char*>(&v1y), sizeof(float));
file.read(reinterpret_cast<char*>(&v1z), sizeof(float));
file.read(reinterpret_cast<char*>(&v2x), sizeof(float));
file.read(reinterpret_cast<char*>(&v2y), sizeof(float));
file.read(reinterpret_cast<char*>(&v2z), sizeof(float));
file.read(reinterpret_cast<char*>(&v3x), sizeof(float));
file.read(reinterpret_cast<char*>(&v3y), sizeof(float));
file.read(reinterpret_cast<char*>(&v3z), sizeof(float));
uint16_t attr_byte_count;
file.read(reinterpret_cast<char*>(&attr_byte_count), sizeof(attr_byte_count));
// Create Triangle object
Triangle t = { v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z };
triangles.push_back(t);
}
file.close();
for (const auto& t : triangles) {
std::cout << "Triangle vertex coordinates: "
<< "(" << t.x1 << ", " << t.y1 << ", " << t.z1 << ") "
<< "(" << t.x2 << ", " << t.y2 << ", " << t.z2 << ") "
<< "(" << t.x3 << ", " << t.y3 << ", " << t.z3 << ")" << std::endl;
}
return triangles;
}
|