24 #ifndef READ_OFF_MESH_HPP_INCLUDED
25 #define READ_OFF_MESH_HPP_INCLUDED
27 #include "../core/mesh.hpp"
28 #include "../library/lib_element.hpp"
29 #include "../util/type2tag.hpp"
40 template <
class Tag1,
class...Tags>
41 struct nvert2elem_id_impl
43 typedef typename tag2type<Tag1>::type
elem_t;
44 static unsigned eval(
unsigned nvert)
46 if (nvert == elem_t::num_nodes)
48 return nvert2elem_id_impl<Tags...>::eval(nvert);
53 struct nvert2elem_id_impl<Tag1>
55 typedef typename tag2type<Tag1>::type
elem_t;
56 static unsigned eval(
unsigned nvert)
58 if (nvert == elem_t::num_nodes)
60 throw std::runtime_error(
"Invalid number of vertices for current element tag vector");
72 template <
class...Tags>
75 return internal::nvert2elem_id_impl<Tags...>::eval(nvert);
78 typedef Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> uMatrix;
79 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
dMatrix;
81 template <
class...Tags>
82 void read_off_data(std::istream &is,
dMatrix &nodes, uMatrix &elements, Tags...tags)
86 if (!(is >> header) || header !=
"OFF")
87 throw std::runtime_error(
"Possibly invalid off file");
90 unsigned nNodes, nElements, nEdges;
91 if (!(is >> nNodes >> nElements >> nEdges))
92 throw std::runtime_error(
"Error reading number of mesh entries");
94 nodes.resize(nNodes, 3);
95 elements.resize(nElements, 5);
98 for (
unsigned i = 0; i < nNodes; ++i)
99 if (!(is >> nodes(i, 0) >> nodes(i, 1) >> nodes(i, 2)))
100 throw std::runtime_error(
"Error reading mesh nodes");
103 for (
unsigned i = 0; i < nElements; ++i)
107 throw std::runtime_error(
"Error reading mesh elements");
108 for (
unsigned c = 0; c < nvert; ++c)
109 if (!(is >> elements(i, c + 1)))
110 throw std::runtime_error(
"Error reading mesh elements");
111 elements(i, 0) = nvert2elem_id(nvert, tags...);
115 template <
class...Tags>
116 void read_off_data(std::string
const &fname,
dMatrix &nodes, uMatrix &elements, Tags...tags)
118 std::ifstream ifs(fname);
119 read_off_data(ifs, nodes, elements, tags...);
130 template <
class...Tags>
131 mesh<tmp::vector<typename tag2type<Tags>::type...> >
137 read_off_data(is, nodes, elements, tags...);
151 template <
class...Tags>
152 mesh<tmp::vector<typename tag2type<Tags>::type...> >
156 std::ifstream is(fname);
158 throw std::runtime_error(
"Error reading mesh file");
165 #endif // READ_OFF_MESH_HPP_INCLUDED