14 std::vector<STL::Triangle>
STL::read(
const std::string& filename) {
15 std::ifstream file(filename, std::ios::in | std::ios::binary);
16 assert_msg(file,
"File '%s' could not be opened: %s", filename, strerror(errno));
17 constexpr
int HEADER_SIZE = 80;
18 file.seekg(HEADER_SIZE);
19 assert_msg(file,
"Error reading header in file '%s': %s", filename, strerror(errno));
20 uint32_t num_triangles;
21 file.read(
reinterpret_cast<char*
>(&num_triangles),
sizeof(uint32_t));
22 assert_msg(file,
"Error reading number of triangles in file '%s': %s", filename,
24 std::vector<Triangle> triangles(num_triangles);
25 constexpr
int TRIANGLE_BYTE_SIZE = 4 * 4 * 3;
26 constexpr
int ATTRIBUTE_BYTE_SIZE = 2;
27 for (uint64_t i = 0; i < num_triangles; ++i) {
28 file.read(
reinterpret_cast<char*
>(&triangles[i]), TRIANGLE_BYTE_SIZE);
29 assert_msg(file,
"Error reading triangle %d in file '%s': %s", i, filename,
31 file.read(
reinterpret_cast<char*
>(&triangles[i].attribute), ATTRIBUTE_BYTE_SIZE);
32 assert_msg(file,
"Error reading attribute %d in file '%s': %s", i, filename,
39 return os <<
"P(" << p.
x <<
", " << p.
y <<
", " << p.
z <<
")";
42 return os <<
"T(" << v.
normal <<
", " << v.
p1 <<
", " << v.
p2 <<
", " << v.
p3 <<
")";