Medusa  1.1
Coordinate Free Mehless Method implementation
STL.cpp
Go to the documentation of this file.
1 #include <medusa/bits/io/STL.hpp>
2 
8 #include <fstream>
9 #include <stdexcept>
11 
12 namespace mm {
13 
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,
23  strerror(errno));
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,
30  strerror(errno));
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,
33  strerror(errno));
34  }
35  return triangles;
36 }
37 
38 std::ostream& operator<<(std::ostream& os, const STL::Point& p) {
39  return os << "P(" << p.x << ", " << p.y << ", " << p.z << ")";
40 }
41 std::ostream& operator<<(std::ostream& os, const STL::Triangle& v) {
42  return os << "T(" << v.normal << ", " << v.p1 << ", " << v.p2 << ", " << v.p3 << ")";
43 }
44 
45 } // namespace mm
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::STL::Triangle::normal
Point normal
triangle normal
Definition: STL_fwd.hpp:32
mm::STL::read
static std::vector< Triangle > read(const std::string &filename)
Read a binary STL file.
Definition: STL.cpp:14
mm::operator<<
std::ostream & operator<<(std::ostream &os, const Gaussian< S > &b)
Output basic information about given Gaussian RBF.
Definition: Gaussian.hpp:37
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::STL::Triangle::p1
Point p1
first point
Definition: STL_fwd.hpp:32
mm::STL::Triangle::p3
Point p3
third point
Definition: STL_fwd.hpp:33
mm::STL::Triangle::p2
Point p2
second point
Definition: STL_fwd.hpp:32
mm::STL::Point::z
float z
z coordinate
Definition: STL_fwd.hpp:29
assert.hpp
STL.hpp
mm::STL::Point
Holds one 3d Point in a STL file.
Definition: STL_fwd.hpp:29
mm::STL::Point::y
float y
y coordinate
Definition: STL_fwd.hpp:29
mm::STL::Point::x
float x
x coordinate
Definition: STL_fwd.hpp:29
mm::STL::Triangle
Holds one STL triangle, which consists of three points, a normal and an attribute.
Definition: STL_fwd.hpp:31