8#include <igl/read_triangle_mesh.h>
23 Eigen::MatrixXi padded_elements(
const std::vector<std::vector<int>> &elements)
26 for (
const auto &element : elements)
27 width = std::max(width, int(element.size()));
28 Eigen::MatrixXi result;
29 result.setConstant(elements.size(), width, -1);
30 for (
int i = 0; i < elements.size(); ++i)
31 for (
int j = 0; j < elements[i].size(); ++j)
32 result(i, j) = elements[i][j];
39 MeshData polygon_soup_to_mesh_data(
40 const Eigen::MatrixXd &vertices3d,
const std::vector<std::vector<int>> &facets)
43 return MeshData(vertices3d.leftCols(2), padded_elements(facets));
47 std::set<int> used_vertices;
48 for (
const auto &face : facets)
49 used_vertices.insert(face.begin(), face.end());
50 std::vector<std::vector<int>> elements(1);
51 elements[0].assign(used_vertices.begin(), used_vertices.end());
52 MeshData data(vertices3d, padded_elements(elements));
54 data.cell_faces.resize(1);
55 data.cell_faces[0].resize(data.faces.size());
56 std::iota(data.cell_faces[0].begin(), data.cell_faces[0].end(), 0);
57 data.cell_face_orientations = {std::vector<int>(data.faces.size(), 1)};
58 data.cell_is_hex = {
false};
59 data.cell_kernel_points.resize(1, 3);
60 const int last = vertices3d.rows() - 1;
61 if (used_vertices.find(last) == used_vertices.end())
62 data.cell_kernel_points.row(0) = data.vertices.row(last);
65 data.cell_kernel_points.setZero();
66 for (
const int vertex : elements[0])
67 data.cell_kernel_points.row(0) += data.
vertices.row(vertex);
68 data.cell_kernel_points.row(0) /= elements[0].size();
74 std::vector<std::string> split_fields(
const std::string &line)
76 std::vector<std::string> fields;
77 size_t begin = line.find_first_not_of(
" \t\r\n");
78 while (begin != std::string::npos)
80 const size_t end = line.find_first_of(
" \t\r\n", begin);
81 fields.push_back(line.substr(begin, end == std::string::npos ? std::string::npos : end - begin));
82 begin = end == std::string::npos ? end : line.find_first_not_of(
" \t\r\n", end);
87 double parse_obj_double(
const std::string &field,
const int line,
const std::string &description)
91 const double value = std::strtod(field.c_str(), &end);
92 if (end == field.c_str() || *end !=
'\0' || errno != 0)
99 int parse_obj_corner(
const std::string &field,
const int n_vertices,
const int line,
const std::string &description)
103 const long long index = std::strtoll(field.c_str(), &end, 10);
104 if (end == field.c_str() || (*end !=
'\0' && *end !=
'/') || errno != 0)
106 const long long vertex = index < 0 ? n_vertices + 1 + index : index;
107 if (vertex < 1 || vertex > n_vertices)
108 log_and_throw_error(
"{}:{}: face corner '{}' references an invalid vertex.", description, line, field);
109 return int(vertex - 1);
113 void finalize_surface(SurfaceMeshData &surface,
const std::string &description)
115 const int n_vertices = surface.vertices.rows();
116 for (
const Eigen::MatrixXi *ids : {&surface.edges, &surface.faces})
117 if (ids->size() > 0 && (ids->minCoeff() < 0 || ids->maxCoeff() >= n_vertices))
118 log_and_throw_error(
"{} references an invalid vertex: expected indices in [0, {}), got [{}, {}].",
119 description, n_vertices, ids->minCoeff(), ids->maxCoeff());
121 std::vector<bool> used(n_vertices,
false);
122 for (
int i = 0; i < surface.edges.rows(); ++i)
123 for (
int j = 0; j < surface.edges.cols(); ++j)
124 used[surface.edges(i, j)] =
true;
125 for (
int i = 0; i < surface.faces.rows(); ++i)
126 for (
int j = 0; j < surface.faces.cols(); ++j)
127 used[surface.faces(i, j)] =
true;
128 const int count = std::count(used.begin(), used.end(),
false);
129 surface.points.resize(count);
130 for (
int i = 0,
point = 0; i < used.size(); ++i)
132 surface.points[
point++] = i;
138 Eigen::MatrixXd vertices;
139 Eigen::MatrixXi cells;
140 std::vector<std::vector<int>> higher_order_connectivity;
141 std::vector<std::vector<double>>
weights;
142 std::vector<int> body_ids;
143 std::vector<std::vector<int>> boundary_elements;
144 std::vector<int> boundary_ids;
146 path.string(), vertices, cells, higher_order_connectivity,
weights,
147 body_ids, boundary_elements, boundary_ids))
150 MeshData data(std::move(vertices), std::move(cells));
151 data.
body_ids = std::move(body_ids);
166 int vertex_count = 0;
168 int encoded_cell_count = 0;
169 if (!(input >> vertex_count >> face_count >> encoded_cell_count)
170 || vertex_count <= 0 || face_count <= 0 || encoded_cell_count <= 0 || encoded_cell_count % 3 != 0)
172 const int cell_count = encoded_cell_count / 3;
174 Eigen::MatrixXd vertices(vertex_count, 3);
175 for (
int i = 0; i < vertex_count; ++i)
176 if (!(input >> vertices(i, 0) >> vertices(i, 1) >> vertices(i, 2)))
179 std::vector<std::vector<int>>
faces(face_count);
180 for (
auto &face :
faces)
183 if (!(input >> size) || size < 3)
186 for (
int &vertex : face)
188 if (!(input >> vertex))
190 if (vertex < 0 || vertex >= vertex_count)
191 log_and_throw_error(
"Invalid HYBRID face vertex {} in {}: expected an index in [0, {}).", vertex, description, vertex_count);
195 std::vector<std::vector<int>> cell_faces(cell_count);
196 std::vector<std::vector<int>> orientations(cell_count);
197 std::vector<std::vector<int>> elements(cell_count);
198 for (
int i = 0; i < cell_count; ++i)
201 if (!(input >> face_size) || face_size <= 0)
203 cell_faces[i].resize(face_size);
204 for (
int &face : cell_faces[i])
205 if (!(input >> face) || face < 0 || face >= face_count)
208 int orientation_size = 0;
209 if (!(input >> orientation_size) || orientation_size != face_size)
211 orientations[i].resize(orientation_size);
212 for (
int &orientation : orientations[i])
213 if (!(input >> orientation))
216 std::set<int> vertices_in_cell;
217 for (
const int face : cell_faces[i])
218 vertices_in_cell.insert(
faces[face].begin(),
faces[face].end());
219 elements[i].assign(vertices_in_cell.begin(), vertices_in_cell.end());
222 std::vector<bool> cell_is_hex(cell_count);
223 for (
int i = 0; i < cell_count; ++i)
226 if (!(input >> value))
228 cell_is_hex[i] = value != 0;
231 Eigen::MatrixXd kernels(cell_count, 3);
237 if (marker !=
"KERNEL" || !(input >> count) || count != cell_count)
239 for (
int i = 0; i < cell_count; ++i)
240 if (!(input >> kernels(i, 0) >> kernels(i, 1) >> kernels(i, 2)))
248 for (
int i = 0; i < cell_count; ++i)
250 for (
const int vertex : elements[i])
251 kernels.row(i) += vertices.row(vertex);
252 kernels.row(i) /= elements[i].size();
255 MeshData data(std::move(vertices), padded_elements(elements));
273 std::vector<Eigen::RowVector3d> vertices;
274 std::vector<std::vector<int>> facets;
275 std::string line, next;
277 while (std::getline(input, line))
280 if (line.empty() || !std::isprint(
static_cast<unsigned char>(line[0])))
282 while (!line.empty() && line.back() ==
'\\' && std::getline(input, next))
289 const std::vector<std::string> fields = split_fields(line);
292 if (fields[0] ==
"v")
294 Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
295 for (
int c = 0; c < 3 && c + 1 < fields.size(); ++c)
296 p[c] = parse_obj_double(fields[c + 1], line_number, description);
297 vertices.push_back(p);
299 else if (fields[0] ==
"f")
301 if (fields.size() < 3)
303 std::vector<int> facet(fields.size() - 1);
304 for (
int i = 1; i < fields.size(); ++i)
305 facet[i - 1] = parse_obj_corner(fields[i], vertices.size(), line_number, description);
306 facets.push_back(std::move(facet));
310 Eigen::MatrixXd
V(vertices.size(), 3);
311 for (
int i = 0; i <
V.rows(); ++i)
312 V.row(i) = vertices[i];
313 return polygon_soup_to_mesh_data(
V, facets);
319 Eigen::MatrixXi cells;
320 std::vector<std::vector<int>> elements;
321 std::vector<std::vector<double>>
weights;
322 std::vector<int> body_ids;
326 if (cells.cols() == 1)
328 else if (cells.cols() == 2)
329 result.
edges = cells;
330 else if (cells.cols() == 3)
331 result.
faces = cells;
332 else if (cells.cols() == 4 && result.
vertices.cols() == 3 && keep_volume)
340 else if (cells.cols() == 4 && result.
vertices.cols() == 3)
342 Eigen::MatrixXd surface_vertices;
345 result.
vertices = std::move(surface_vertices);
350 finalize_surface(result, path.string());
359 finalize_surface(result, path.string());
364 const std::filesystem::path &path,
368 if (!igl::read_triangle_mesh(path.string(), result.
vertices, result.
faces))
370 finalize_surface(result, path.string());
std::vector< Eigen::VectorXi > faces
static bool load(const std::string &path, Eigen::MatrixXd &vertices, Eigen::MatrixXi &cells, std::vector< std::vector< int > > &elements, std::vector< std::vector< double > > &weights, std::vector< int > &body_ids)
static bool read(const std::string obj_file_name, std::vector< std::vector< double > > &V, std::vector< std::vector< double > > &TC, std::vector< std::vector< double > > &N, std::vector< std::vector< int > > &F, std::vector< std::vector< int > > &FTC, std::vector< std::vector< int > > &FN, std::vector< std::vector< int > > &L)
Read a mesh from an ascii obj file.
Format-independent input used to construct a runtime Mesh.
Eigen::MatrixXd higher_order_nodes
std::vector< std::vector< int > > higher_order_connectivity
std::vector< std::vector< int > > boundary_elements
std::vector< std::vector< int > > faces
Optional topology for arbitrary polyhedral cells.
std::vector< int > body_ids
bool elements_are_ordered
Whether each element row carries a format-defined local vertex order.
std::vector< int > boundary_ids
Eigen::MatrixXd cell_kernel_points
std::vector< std::vector< int > > cell_faces
std::vector< std::vector< int > > cell_face_orientations
std::vector< std::vector< double > > higher_order_weights
std::vector< bool > cell_is_hex
static MeshData read_hybrid(std::istream &input, const std::string &description)
static MeshData read_msh(const std::filesystem::path &path)
static SurfaceMeshData read_msh_surface(const std::filesystem::path &path, bool keep_volume=false)
static bool read_triangle_surface(const std::filesystem::path &path, SurfaceMeshData &result)
static MeshData read_obj(std::istream &input, const std::string &description)
Reads a 2D polygonal mesh (planar OBJ) or a single polyhedron given by its boundary (non-planar OBJ).
static SurfaceMeshData read_obj_surface(const std::filesystem::path &path)
Eigen::MatrixXi tets
Volumetric cells indexing vertices; only filled when a 3D tet mesh is read with keep_volume.
void find_triangle_surface_from_tets(const Eigen::MatrixXi &tets, Eigen::MatrixXi &faces)
Boundary triangles of a tet mesh, indexing the tets' vertices.
bool is_planar(const Eigen::MatrixXd &V, const double tol=1e-5)
Determine if the given V x (2|3) vertices are planar (2D or z-range below tol).
void extract_triangle_surface_from_tets(const Eigen::MatrixXd &vertices, const Eigen::MatrixXi &tets, Eigen::MatrixXd &surface_vertices, Eigen::MatrixXi &tris)
Extract triangular surface from a tetmesh.
void log_and_throw_error(const std::string &msg)