20 using face_type = Eigen::Matrix<index_t, 1, 3>;
21 using faces_type = Eigen::Matrix<index_t, Eigen::Dynamic, 3, Eigen::RowMajor>;
23 surface(
const vertices_type& vertices,
const faces_type& faces)
24 : vertices_(vertices.rows(), 3), faces_(faces.rows(), 3) {
25 std::vector<index_t> vi_map(vertices.rows(), -1);
28 auto v_it = vertices_.rowwise().begin();
29 auto f_it = faces_.rowwise().begin();
30 index_t n_vertices = 0;
33 for (
auto face : faces.rowwise()) {
34 for (
auto i = 0; i < 3; i++) {
35 auto& vi = vi_map.at(face(i));
38 *v_it++ = vertices.row(face(i));
47 vertices_.conservativeResize(n_vertices, 3);
48 faces_.conservativeResize(n_faces, 3);
51 void export_obj(
const std::string& filename)
const {
52 std::ofstream ofs(filename);
54 throw std::runtime_error(
"Failed to open file '" + filename +
"'.");
57 for (
auto v : vertices_.rowwise()) {
58 ofs <<
"v " << numeric::to_string(v(0)) <<
' ' << numeric::to_string(v(1)) <<
' '
59 << numeric::to_string(v(2)) <<
'\n';
62 for (
auto f : faces_.rowwise()) {
63 ofs <<
"f " << f(0) + 1 <<
' ' << f(1) + 1 <<
' ' << f(2) + 1 <<
'\n';
67 const faces_type& faces()
const {
return faces_; }
69 const vertices_type& vertices()
const {
return vertices_; }
72 vertices_type vertices_;