13#include <unordered_set>
18#include <igl/read_triangle_mesh.h>
20#include <geogram/mesh/mesh_io.h>
21#include <geogram/mesh/mesh_geometry.h>
22#include <geogram/basic/geometry.h>
23#include <geogram/mesh/mesh_preprocessing.h>
24#include <geogram/mesh/mesh_topology.h>
25#include <geogram/mesh/mesh_geometry.h>
26#include <geogram/mesh/mesh_repair.h>
27#include <geogram/mesh/mesh_AABB.h>
28#include <geogram/voronoi/CVT.h>
29#include <geogram/basic/logger.h>
37 if (M.vertices.dimension() == 2)
40 assert(M.vertices.dimension() == 3);
41 GEO::vec3 min_corner, max_corner;
42 GEO::get_bbox(M, &min_corner[0], &max_corner[0]);
43 const double diff = (max_corner[2] - min_corner[2]);
45 return fabs(diff) < tol;
52 for (index_t d = 0; d < std::min(3u, (index_t)M.vertices.dimension()); ++d)
54 if (M.vertices.double_precision())
56 p[d] = M.vertices.point_ptr(v)[d];
60 p[d] = M.vertices.single_precision_point_ptr(v)[d];
72 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
76 return p / M.facets.nb_vertices(f);
84 auto v = M.vertices.create_vertex();
85 for (index_t d = 0; d < std::min(3u, (index_t)M.vertices.dimension()); ++d)
87 if (M.vertices.double_precision())
89 M.vertices.point_ptr(v)[d] = p[d];
93 M.vertices.single_precision_point_ptr(v)[d] = (float)p[d];
105 std::vector<ElementType> old_tags = element_tags;
107 element_tags.resize(M.facets.nb());
110 GEO::Attribute<bool> is_boundary_vertex(M.vertices.attributes(),
"boundary_vertex");
111 std::vector<bool> is_interface_vertex(M.vertices.nb(),
false);
113 for (index_t f = 0; f < M.facets.nb(); ++f)
115 if (M.facets.nb_vertices(f) != 4 || (!old_tags.empty() && old_tags[f] == ElementType::INTERIOR_POLYTOPE))
118 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
120 is_interface_vertex[M.facets.vertex(f, lv)] =
true;
130 std::vector<int> degree(M.vertices.nb(), 0);
131 std::vector<bool> is_regular_vertex(M.vertices.nb());
132 for (index_t f = 0; f < M.facets.nb(); ++f)
134 if (M.facets.nb_vertices(f) == 4)
137 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
139 index_t v = M.facets.vertex(f, lv);
144 for (index_t v = 0; v < M.vertices.nb(); ++v)
147 if (is_boundary_vertex[v] || is_interface_vertex[v])
149 is_regular_vertex[v] = (degree[v] <= 2);
153 is_regular_vertex[v] = (degree[v] == 4);
158 for (index_t f = 0; f < M.facets.nb(); ++f)
160 assert(M.facets.nb_vertices(f) > 2);
161 if (!old_tags.empty() && old_tags[f] == ElementType::INTERIOR_POLYTOPE)
164 if (M.facets.nb_vertices(f) == 4)
169 bool is_boundary_facet =
false;
170 bool is_interface_facet =
false;
171 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
173 if (is_boundary_vertex[M.facets.vertex(f, lv)])
175 is_boundary_facet =
true;
177 if (is_interface_vertex[M.facets.vertex(f, lv)])
179 is_interface_facet =
true;
184 if (is_boundary_facet || is_interface_facet)
188 bool is_singular =
false;
189 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
191 index_t v = M.facets.vertex(f, lv);
192 if (is_boundary_vertex[v] || is_interface_vertex[v])
194 if (!is_regular_vertex[v])
202 if (!is_regular_vertex[v])
204 element_tags[f] = ElementType::UNDEFINED;
210 if (is_interface_facet)
212 element_tags[f] = ElementType::INTERFACE_CUBE;
214 else if (is_singular)
216 element_tags[f] = ElementType::SIMPLE_SINGULAR_BOUNDARY_CUBE;
220 element_tags[f] = ElementType::REGULAR_BOUNDARY_CUBE;
226 int nb_singulars = 0;
227 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
229 if (!is_regular_vertex[M.facets.vertex(f, lv)])
235 if (nb_singulars == 0)
237 element_tags[f] = ElementType::REGULAR_INTERIOR_CUBE;
239 else if (nb_singulars == 1)
241 element_tags[f] = ElementType::SIMPLE_SINGULAR_INTERIOR_CUBE;
245 element_tags[f] = ElementType::MULTI_SINGULAR_INTERIOR_CUBE;
255 GEO::Attribute<bool> boundary_vertices(M.vertices.attributes(),
"boundary_vertex");
256 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
258 if (boundary_vertices[M.facets.vertex(f, lv)])
260 tag = ElementType::BOUNDARY_POLYTOPE;
265 element_tags[f] = tag;
271 for (index_t f = 0; f < M.facets.nb(); ++f)
273 if (M.facets.nb_vertices(f) == 3)
275 element_tags[f] = ElementType::SIMPLEX;
286 double signed_area(
const GEO::Mesh &M, GEO::index_t f)
290 index_t v0 = M.facet_corners.vertex(M.facets.corners_begin(f));
291 const vec3 &p0 = Geom::mesh_vertex(M, v0);
293 M.facets.corners_begin(f) + 1;
294 c + 1 < M.facets.corners_end(f); c++)
296 index_t v1 = M.facet_corners.vertex(c);
298 index_t v2 = M.facet_corners.vertex(c + 1);
300 result += Geom::triangle_signed_area(vec2(&p0[0]), vec2(&p1[0]), vec2(&p2[0]));
310 vector<index_t> component;
311 index_t nb_components = get_connected_components(M, component);
312 vector<double> comp_signed_volume(nb_components, 0.0);
313 for (index_t f = 0; f < M.facets.nb(); ++f)
315 comp_signed_volume[component[f]] += signed_area(M, f);
317 for (index_t f = 0; f < M.facets.nb(); ++f)
319 if (comp_signed_volume[component[f]] < 0.0)
330 assert(
V.rows() == C.size());
331 int num_colors = C.maxCoeff() + 1;
332 Eigen::VectorXi count(num_colors);
334 for (
int i = 0; i < C.size(); ++i)
338 R.resize(num_colors + 1);
340 for (
int c = 0; c < num_colors; ++c)
342 R(c + 1) = R(c) + count(c);
345 Eigen::VectorXi remap(C.size());
346 for (
int i = 0; i < C.size(); ++i)
348 remap[i] = R(C(i)) + count[C(i)];
352 Eigen::MatrixXd NV(
V.rows(),
V.cols());
353 for (
int v = 0; v <
V.rows(); ++v)
355 NV.row(remap(v)) =
V.row(v);
359 for (
int f = 0; f <
F.rows(); ++f)
361 for (
int lv = 0; lv <
F.cols(); ++lv)
363 F(f, lv) = remap(
F(f, lv));
373 void compute_unsigned_distance_field(
const GEO::Mesh &M,
374 const GEO::MeshFacetsAABB &aabb_tree,
const Eigen::MatrixXd &P, Eigen::VectorXd &D)
376 assert(P.cols() == 3);
378#pragma omp parallel for
379 for (
int i = 0; i < P.rows(); ++i)
381 GEO::vec3 pos(P(i, 0), P(i, 1), P(i, 2));
382 double sq_dist = aabb_tree.squared_distance(pos);
390 double x1,
double y1,
double x2,
double y2,
double &twice_signed_area)
392 twice_signed_area = y1 * x2 - x1 * y2;
393 if (twice_signed_area > 0)
395 else if (twice_signed_area < 0)
414 bool point_in_triangle_2d(
415 double x0,
double y0,
double x1,
double y1,
416 double x2,
double y2,
double x3,
double y3,
417 double &a,
double &b,
double &c)
425 int signa = orientation(x2, y2, x3, y3, a);
428 int signb = orientation(x3, y3, x1, y1, b);
431 int signc = orientation(x1, y1, x2, y2, c);
434 double sum = a +
b + c;
435 geo_assert(sum != 0);
456 inline GEO::Sign orient_2d_inexact(GEO::vec2 p0, GEO::vec2 p1, GEO::vec2 p2)
458 double a11 = p1[0] - p0[0];
459 double a12 = p1[1] - p0[1];
461 double a21 = p2[0] - p0[0];
462 double a22 = p2[1] - p0[1];
464 double Delta = GEO::det2x2(
468 return GEO::geo_sgn(Delta);
483 template <
int X = 0,
int Y = 1,
int Z = 2>
484 int intersect_ray_z(
const GEO::Mesh &M, GEO::index_t f,
const GEO::vec3 &q,
double &
z)
488 index_t c =
M.facets.corners_begin(f);
489 const vec3 &p1 = Geom::mesh_vertex(M,
M.facet_corners.vertex(c++));
490 const vec3 &p2 = Geom::mesh_vertex(M,
M.facet_corners.vertex(c++));
491 const vec3 &p3 = Geom::mesh_vertex(M,
M.facet_corners.vertex(c));
494 if (point_in_triangle_2d(
495 q[X], q[Y], p1[X], p1[Y], p2[X], p2[Y], p3[X], p3[Y], u, v, w))
497 z = u * p1[Z] + v * p2[Z] + w * p3[Z];
498 auto sign = orient_2d_inexact(vec2(p1[X], p1[Y]), vec2(p2[X], p2[Y]), vec2(p3[X], p3[Y]));
516 void compute_sign(
const GEO::Mesh &M,
const GEO::MeshFacetsAABB &aabb_tree,
517 const Eigen::MatrixXd &P, Eigen::VectorXd &D)
519 assert(
P.cols() == 3);
520 assert(D.size() ==
P.rows());
522 GEO::vec3 min_corner, max_corner;
523 GEO::get_bbox(M, &min_corner[0], &max_corner[0]);
525#pragma omp parallel for
526 for (
int k = 0; k <
P.rows(); ++k)
528 GEO::vec3 center(
P(k, 0),
P(k, 1),
P(k, 2));
531 box.xyz_min[0] = box.xyz_max[0] = center[0];
532 box.xyz_min[1] = box.xyz_max[1] = center[1];
533 box.xyz_min[2] = min_corner[2];
534 box.xyz_max[2] = max_corner[2];
536 std::vector<std::pair<double, int>> inter;
537 auto action = [&
M, &inter, ¢er](GEO::index_t
f) {
539 if (
int s = intersect_ray_z(M, f, center,
z))
541 inter.emplace_back(
z, s);
544 aabb_tree.compute_bbox_facet_bbox_intersections(box, action);
545 std::sort(inter.begin(), inter.end());
547 std::vector<double> reduced;
548 for (
int i = 0, s = 0; i < (int)inter.size(); ++i)
550 const int ds = inter[i].second;
552 if ((s == -1 && ds < 0) || (s == 0 && ds > 0))
554 reduced.push_back(inter[i].first);
559 for (
double z : reduced)
566 if (num_before % 2 == 1)
582 M.vertices.create_vertices((
int)
V.rows());
583 for (
int i = 0; i < (int)M.vertices.nb(); ++i)
585 GEO::vec3 &p = M.vertices.point(i);
588 p[2] =
V.cols() >= 3 ?
V(i, 2) : 0;
593 M.facets.create_triangles((
int)
F.rows());
595 else if (
F.cols() == 4)
597 M.facets.create_quads((
int)
F.rows());
601 throw std::runtime_error(
"Mesh format not supported");
603 for (
int c = 0; c < (int)M.facets.nb(); ++c)
605 for (
int lv = 0; lv <
F.cols(); ++lv)
607 M.facets.set_vertex(c, lv,
F(c, lv));
644 V.resize(M.vertices.nb(), 3);
645 for (
int i = 0; i < (int)M.vertices.nb(); ++i)
647 GEO::vec3 p = M.vertices.point(i);
648 V.row(i) << p[0], p[1], p[2];
650 assert(M.facets.are_simplices());
651 F.resize(M.facets.nb(), 3);
652 for (
int c = 0; c < (int)M.facets.nb(); ++c)
654 for (
int lv = 0; lv < 3; ++lv)
656 F(c, lv) = M.facets.vertex(c, lv);
659 assert(M.cells.are_simplices());
660 T.resize(M.cells.nb(), 4);
661 for (
int c = 0; c < (int)M.cells.nb(); ++c)
663 for (
int lv = 0; lv < 4; ++lv)
665 T(c, lv) = M.cells.vertex(c, lv);
673 const Eigen::MatrixXd &P, Eigen::VectorXd &D)
677 GEO::MeshFacetsAABB aabb_tree(M);
678 compute_unsigned_distance_field(M, aabb_tree, P, D);
679 compute_sign(M, aabb_tree, P, D);
686 assert(
F.cols() == 3);
687 assert(
V.cols() == 3);
688 std::array<Eigen::RowVector3d, 4> t;
689 t[3] = Eigen::RowVector3d::Zero(
V.cols());
690 double volume_total = 0;
691 for (
int f = 0; f <
F.rows(); ++f)
693 for (
int lv = 0; lv <
F.cols(); ++lv)
695 t[lv] =
V.row(
F(f, lv));
697 double vol = GEO::Geom::tetra_signed_volume(t[0].data(), t[1].data(), t[2].data(), t[3].data());
700 return -volume_total;
709 for (
int f = 0; f <
F.rows(); ++f)
711 F.row(f) =
F.row(f).reverse().eval();
720 struct EdgeInterfacePrimitive
723 Eigen::Vector2d from;
727 struct FaceInterfacePrimitive
730 std::vector<Eigen::Vector3d> vertices;
735 return a.size() ==
b.size() && (a -
b).squaredNorm() <= 1
e-24;
738 bool overlapping_segments(
const EdgeInterfacePrimitive &first,
const EdgeInterfacePrimitive &second)
740 const Eigen::Vector2d direction = first.to - first.from;
741 const double length = direction.norm();
742 const double second_length = (second.to - second.from).
norm();
743 const double scale = std::max({1.0, length, second_length});
744 const double tolerance = 1
e-12 * scale;
745 if (length <= tolerance || second_length <= tolerance)
748 auto cross = [](
const Eigen::Vector2d &a,
const Eigen::Vector2d &
b) {
749 return a.x() *
b.y() - a.y() *
b.x();
751 if (std::abs(
cross(direction, second.from - first.from)) > tolerance * length
752 || std::abs(
cross(direction, second.to - first.from)) > tolerance * length)
755 const Eigen::Vector2d tangent = direction / length;
756 const double second_from = (second.from - first.from).dot(tangent);
757 const double second_to = (second.to - first.from).dot(tangent);
758 const double overlap_begin = std::max(0.0, std::min(second_from, second_to));
759 const double overlap_end = std::min(length, std::max(second_from, second_to));
760 return overlap_end - overlap_begin > tolerance;
763 Eigen::Vector3d face_normal(
const FaceInterfacePrimitive &face)
765 Eigen::Vector3d normal = Eigen::Vector3d::Zero();
766 for (
int i = 0; i < face.vertices.size(); ++i)
767 normal += face.vertices[i].cross(face.vertices[(i + 1) % face.vertices.size()]);
771 Eigen::Vector2d project_point(
const Eigen::Vector3d &
point,
const int dropped_axis)
773 if (dropped_axis == 0)
775 if (dropped_axis == 1)
780 bool point_in_convex_polygon(
781 const Eigen::Vector2d &
point,
782 const std::vector<Eigen::Vector2d> &polygon,
783 const double tolerance)
786 for (
int i = 0; i < polygon.size(); ++i)
788 const Eigen::Vector2d edge = polygon[(i + 1) % polygon.size()] - polygon[i];
789 const Eigen::Vector2d offset =
point - polygon[i];
790 const double cross = edge.x() * offset.y() - edge.y() * offset.x();
791 if (std::abs(
cross) <= tolerance)
795 else if (sign *
cross < 0)
801 bool overlapping_faces(
const FaceInterfacePrimitive &first,
const FaceInterfacePrimitive &second)
803 const Eigen::Vector3d first_normal = face_normal(first);
804 const Eigen::Vector3d second_normal = face_normal(second);
805 const double first_area_scale = first_normal.norm();
806 const double second_area_scale = second_normal.norm();
807 const double coordinate_scale = std::max({1.0,
808 first.vertices.front().
norm(),
809 second.vertices.front().norm()});
810 const double tolerance = 1
e-12 * coordinate_scale;
811 if (first_area_scale <= tolerance * tolerance || second_area_scale <= tolerance * tolerance)
814 const Eigen::Vector3d unit_normal = first_normal / first_area_scale;
815 if (unit_normal.cross(second_normal / second_area_scale).norm() > 1e-10)
818 if (std::abs(unit_normal.dot(
point - first.
vertices.front())) > tolerance)
821 Eigen::Index dropped_axis;
822 unit_normal.cwiseAbs().maxCoeff(&dropped_axis);
823 std::vector<Eigen::Vector2d> first_polygon, second_polygon;
824 first_polygon.reserve(first.vertices.size());
825 second_polygon.reserve(second.vertices.size());
827 first_polygon.push_back(project_point(
point, dropped_axis));
829 second_polygon.push_back(project_point(
point, dropped_axis));
831 auto contains = [tolerance](
const auto &container,
const auto &contained) {
832 return std::all_of(contained.begin(), contained.end(), [&](
const Eigen::Vector2d &
point) {
833 return point_in_convex_polygon(point, container, tolerance);
836 return contains(first_polygon, second_polygon) || contains(second_polygon, first_polygon);
840std::vector<std::pair<polyfem::mesh::Navigation::Index, polyfem::mesh::Navigation::Index>>
844 std::vector<EdgeInterfacePrimitive> first_edges, second_edges;
845 auto collect = [](
const Mesh2D &mesh,
auto &edges) {
846 for (
int f = 0; f < mesh.
n_faces(); ++f)
854 edges.push_back({index,
855 mesh.
point(index.vertex).head<2>(),
856 mesh.
point(opposite.vertex).head<2>()});
862 collect(first, first_edges);
863 collect(second, second_edges);
865 std::vector<std::pair<Index, Index>> result;
866 std::set<std::tuple<int, int, int, int, int, int>> visited_pairs;
867 for (
const auto &lhs : first_edges)
869 for (
const auto &rhs : second_edges)
871 if (!overlapping_segments(lhs, rhs))
873 Index second_index = rhs.index;
874 if ((lhs.to - lhs.from).dot(rhs.to - rhs.from) > 0)
876 const auto key = std::make_tuple(
877 lhs.index.face, lhs.index.edge, lhs.index.vertex,
878 second_index.face, second_index.edge, second_index.vertex);
879 if (visited_pairs.insert(key).second)
880 result.emplace_back(lhs.index, second_index);
886std::vector<std::pair<polyfem::mesh::Navigation3D::Index, polyfem::mesh::Navigation3D::Index>>
890 std::vector<FaceInterfacePrimitive> first_faces, second_faces;
891 auto collect = [](
const Mesh3D &mesh,
auto &
faces) {
892 std::unordered_set<int> visited_faces;
893 for (
int c = 0; c < mesh.
n_cells(); ++c)
902 for (
int lv = 0; lv < vertices.size(); ++lv)
904 faces.push_back({index, std::move(vertices)});
908 collect(first, first_faces);
909 collect(second, second_faces);
911 std::vector<std::pair<Index, Index>> result;
912 for (
const auto &lhs : first_faces)
914 for (
const auto &rhs : second_faces)
916 if (!overlapping_faces(lhs, rhs))
918 Index second_index = rhs.index;
921 if (same_point(first.
point(lhs.index.vertex), second.
point(second_index.vertex)))
925 result.emplace_back(lhs.index, second_index);
934 std::vector<int> vertex_l2g;
935 for (
int c = 0; c < mesh.
n_cells(); ++c)
941 auto poly = std::make_unique<GEO::Mesh>();
944 poly->vertices.create_vertices((triangulated ? nv + nf : nv) + 1);
946 vertex_l2g.reserve(nv);
947 for (
int lf = 0; lf < nf; ++lf)
949 GEO::vector<GEO::index_t> facet_vertices;
953 Eigen::RowVector3d p = mesh.
point(index.vertex);
954 if (vertex_g2l[index.vertex] < 0)
956 vertex_g2l[index.vertex] = vertex_l2g.size();
957 vertex_l2g.push_back(index.vertex);
959 int v1 = vertex_g2l[index.vertex];
960 facet_vertices.push_back(v1);
961 poly->vertices.point(v1) = GEO::vec3(p.data());
966 GEO::vec3 p(0, 0, 0);
967 for (GEO::index_t lv = 0; lv < facet_vertices.size(); ++lv)
969 p += poly->vertices.point(facet_vertices[lv]);
971 p /= facet_vertices.size();
972 int v0 = vertex_l2g.size();
973 vertex_l2g.push_back(0);
974 poly->vertices.point(v0) = p;
975 for (GEO::index_t lv = 0; lv < facet_vertices.size(); ++lv)
977 int v1 = facet_vertices[lv];
978 int v2 = facet_vertices[(lv + 1) % facet_vertices.size()];
979 poly->facets.create_triangle(v0, v1, v2);
984 poly->facets.create_polygon(facet_vertices);
988 Eigen::RowVector3d p = mesh.
kernel(c);
989 poly->vertices.point(nv) = GEO::vec3(p.data());
991 assert(vertex_l2g.size() ==
size_t(triangulated ? nv + nf : nv));
993 for (
int v : vertex_l2g)
998 poly->facets.compute_borders();
999 poly->facets.connect();
1001 polys.emplace_back(std::move(poly));
1031 M.vertices.create_vertices((
int)mesh.
n_vertices());
1032 for (
int i = 0; i < (int)M.vertices.nb(); ++i)
1034 auto pt = mesh.
point(i);
1035 GEO::vec3 &p = M.vertices.point(i);
1041 for (
int f = 0, lf = 0; f < mesh.
n_faces(); ++f)
1046 M.facets.create_polygon(nv);
1047 for (
int lv = 0; lv < nv; ++lv)
1049 M.facets.set_vertex(lf, lv, mesh.
face_vertex(f, lv));
1055 typedef std::array<int, 8> Vector8i;
1056 Vector8i g2p = {{0, 4, 1, 5, 3, 7, 2, 6}};
1057 for (
int c = 0; c < mesh.
n_cells(); ++c)
1063 for (
size_t k = 0; k < 8; ++k)
1065 lvg[k] = lvp[g2p[k]];
1067 std::reverse(lvg.begin(), lvg.end());
1069 lvg[0], lvg[1], lvg[2], lvg[3],
1070 lvg[4], lvg[5], lvg[6], lvg[7]);
1080 GEO::mesh_reorient(M);
1086 Eigen::MatrixXd &P, Eigen::MatrixXd *N,
int num_lloyd,
int num_newton)
1088 assert(num_samples > 3);
1091 GEO::CentroidalVoronoiTesselation CVT(&M);
1093 bool was_quiet = GEO::Logger::instance()->is_quiet();
1094 GEO::Logger::instance()->set_quiet(
true);
1095 CVT.compute_initial_sampling(num_samples);
1096 GEO::Logger::instance()->set_quiet(was_quiet);
1100 CVT.Lloyd_iterations(num_lloyd);
1105 CVT.Newton_iterations(num_newton);
1108 P.resize(3, num_samples);
1109 std::copy_n(CVT.embedding(0), 3 * num_samples, P.data());
1110 P.transposeInPlace();
1114 GEO::MeshFacetsAABB aabb(M);
1116 for (
int i = 0; i < num_samples; ++i)
1118 GEO::vec3 p(P(i, 0), P(i, 1), P(i, 2));
1119 GEO::vec3 nearest_point;
1121 auto f = aabb.nearest_facet(p, nearest_point, sq_dist);
1122 GEO::vec3 n = normalize(GEO::Geom::mesh_facet_normal(M, f));
1123 N->row(i) << n[0], n[1], n[2];
1133 bool approx_aligned(
const double *a_,
const double *b_,
const double *p_,
const double *q_,
double tol = 1e-6)
1135 using namespace GEO;
1136 vec3 a(a_), b(b_), p(p_), q(q_);
1137 double da = std::sqrt(Geom::point_segment_squared_distance(a, p, q));
1138 double db = std::sqrt(Geom::point_segment_squared_distance(b, p, q));
1139 double cos_theta = Geom::cos_angle(b - a, p - q);
1140 return (
da < tol && db < tol && std::abs(std::abs(cos_theta) - 1.0) < tol);
1148 const Eigen::MatrixXd &BV,
const Eigen::MatrixXi &BE, Eigen::MatrixXi &OE)
1150 assert(IV.cols() == 2 || IV.cols() == 3);
1151 assert(BV.cols() == 2 || BV.cols() == 3);
1152 typedef std::pair<int, int>
Edge;
1153 std::vector<Edge> selected;
1154 for (
int e1 = 0; e1 < IE.rows(); ++e1)
1156 Eigen::RowVector3d a;
1158 a.head(IV.cols()) = IV.row(IE(e1, 0));
1159 Eigen::RowVector3d b;
1161 b.head(IV.cols()) = IV.row(IE(e1, 1));
1162 for (
int e2 = 0; e2 < BE.rows(); ++e2)
1164 Eigen::RowVector3d p;
1166 p.head(BV.cols()) = BV.row(BE(e2, 0));
1167 Eigen::RowVector3d q;
1169 q.head(BV.cols()) = BV.row(BE(e2, 1));
1170 if (approx_aligned(a.data(), b.data(), p.data(), q.data()))
1172 selected.emplace_back(IE(e1, 0), IE(e1, 1));
1178 OE.resize(selected.size(), 2);
1179 for (
int e = 0; e < OE.rows(); ++e)
1181 OE.row(e) << selected[e].first, selected[e].second;
1188 const Eigen::MatrixXd &vertices,
1189 const Eigen::MatrixXi &codim_edges,
1190 const Eigen::MatrixXi &
faces,
1191 Eigen::VectorXi &codim_vertices)
1193 std::vector<bool> is_vertex_codim(vertices.rows(),
true);
1194 for (
int i = 0; i < codim_edges.rows(); i++)
1196 for (
int j = 0; j < codim_edges.cols(); j++)
1198 is_vertex_codim[codim_edges(i, j)] =
false;
1201 for (
int i = 0; i <
faces.rows(); i++)
1203 for (
int j = 0; j <
faces.cols(); j++)
1205 is_vertex_codim[
faces(i, j)] =
false;
1208 const auto n_codim_vertices = std::count(is_vertex_codim.begin(), is_vertex_codim.end(),
true);
1209 codim_vertices.resize(n_codim_vertices);
1210 for (
int i = 0, ci = 0; i < vertices.rows(); i++)
1212 if (is_vertex_codim[i])
1214 codim_vertices[ci++] = i;
1220 const Eigen::MatrixXi &tets,
1221 Eigen::MatrixXi &
faces)
1223 std::unordered_set<Eigen::Vector3i, HashMatrix> tri_to_tet(4 * tets.rows());
1224 for (
int i = 0; i < tets.rows(); i++)
1226 tri_to_tet.emplace(tets(i, 0), tets(i, 2), tets(i, 1));
1227 tri_to_tet.emplace(tets(i, 0), tets(i, 3), tets(i, 2));
1228 tri_to_tet.emplace(tets(i, 0), tets(i, 1), tets(i, 3));
1229 tri_to_tet.emplace(tets(i, 1), tets(i, 2), tets(i, 3));
1232 std::vector<Eigen::RowVector3i> faces_vector;
1233 for (
const auto &tri : tri_to_tet)
1236 bool is_surface_triangle =
1237 tri_to_tet.find(Eigen::Vector3i(tri[2], tri[1], tri[0])) == tri_to_tet.end()
1238 && tri_to_tet.find(Eigen::Vector3i(tri[1], tri[0], tri[2])) == tri_to_tet.end()
1239 && tri_to_tet.find(Eigen::Vector3i(tri[0], tri[2], tri[1])) == tri_to_tet.end();
1240 if (is_surface_triangle)
1242 faces_vector.emplace_back(tri[0], tri[1], tri[2]);
1246 faces.resize(faces_vector.size(), 3);
1247 for (
int i = 0; i <
faces.rows(); i++)
1249 faces.row(i) = faces_vector[i];
1254 const Eigen::MatrixXd &vertices,
1255 const Eigen::MatrixXi &tets,
1256 Eigen::MatrixXd &surface_vertices,
1257 Eigen::MatrixXi &tris)
1259 Eigen::MatrixXi full_tris;
1262 std::unordered_map<int, int> full_to_surface;
1263 std::vector<size_t> surface_to_full;
1264 for (
int i = 0; i < full_tris.rows(); i++)
1266 for (
int j = 0; j < full_tris.cols(); j++)
1268 if (full_to_surface.find(full_tris(i, j)) == full_to_surface.end())
1270 full_to_surface[full_tris(i, j)] = surface_to_full.size();
1271 surface_to_full.push_back(full_tris(i, j));
1276 surface_vertices.resize(surface_to_full.size(), 3);
1277 for (
int i = 0; i < surface_to_full.size(); i++)
1279 surface_vertices.row(i) = vertices.row(surface_to_full[i]);
1282 tris.resize(full_tris.rows(), full_tris.cols());
1283 for (
int i = 0; i < tris.rows(); i++)
1285 for (
int j = 0; j < tris.cols(); j++)
1287 tris(i, j) = full_to_surface[full_tris(i, j)];
1293 const std::string &mesh_path,
1294 Eigen::MatrixXd &vertices,
1295 Eigen::VectorXi &codim_vertices,
1296 Eigen::MatrixXi &codim_edges,
1297 Eigen::MatrixXi &
faces)
1299 vertices.resize(0, 0);
1300 codim_vertices.resize(0);
1301 codim_edges.resize(0, 0);
1304 std::string lowername = mesh_path;
1306 lowername.begin(), lowername.end(), lowername.begin(), ::tolower);
1310 Eigen::MatrixXi cells;
1311 std::vector<std::vector<int>> elements;
1312 std::vector<std::vector<double>>
weights;
1313 std::vector<int> body_ids;
1316 logger().error(
"Unable to load mesh: {}", mesh_path);
1320 if (cells.cols() == 1)
1321 codim_vertices = cells;
1322 else if (cells.cols() == 2)
1323 codim_edges = cells;
1324 else if (cells.cols() == 3)
1326 else if (cells.cols() == 4)
1328 if (vertices.cols() == 2)
1330 logger().error(
"read_surface_mesh not implemented for 2D quad meshes");
1336 assert(vertices.cols() == 3);
1337 Eigen::MatrixXd surface_vertices;
1339 vertices = surface_vertices;
1344 logger().error(
"read_surface_mesh not implemented for hexahedral and polygonal/polyhedral meshes");
1352 logger().error(
"Unable to load mesh: {}", mesh_path);
1356 else if (!igl::read_triangle_mesh(mesh_path, vertices,
faces))
1359 if (!GEO::mesh_load(mesh_path, mesh))
1361 logger().error(
"Unable to load mesh: {}", mesh_path);
1366 vertices.resize(mesh.vertices.nb(), dim);
1367 for (
int vi = 0; vi < mesh.vertices.nb(); vi++)
1369 const auto &v = mesh.vertices.point(vi);
1370 for (
int vj = 0; vj < dim; vj++)
1372 vertices(vi, vj) = v[vj];
1377 assert(mesh.facets.nb());
1378 int face_cols = mesh.facets.nb_vertices(0);
1379 faces.resize(mesh.facets.nb(), face_cols);
1380 for (
int fi = 0; fi < mesh.facets.nb(); fi++)
1382 assert(face_cols == mesh.facets.nb_vertices(fi));
1383 for (
int fj = 0; fj < mesh.facets.nb_vertices(fi); fj++)
1385 faces(fi, fj) = mesh.facets.vertex(fi, fj);
1397 std::unordered_set<std::vector<int>,
HashVector> boundaries;
1399 auto insert = [&](std::vector<int> v) {
1400 std::sort(v.begin(), v.end());
1401 boundaries.insert(v);
1404 for (
int i = 0; i < cells.rows(); i++)
1406 const auto &cell = cells.row(i);
1407 if (cells.cols() == 3)
1409 insert({{cell(0), cell(1)}});
1410 insert({{cell(1), cell(2)}});
1411 insert({{cell(2), cell(0)}});
1413 else if (cells.cols() == 4 && dim == 2)
1415 insert({{cell(0), cell(1)}});
1416 insert({{cell(1), cell(2)}});
1417 insert({{cell(2), cell(3)}});
1418 insert({{cell(3), cell(0)}});
1420 else if (cells.cols() == 4 && dim == 3)
1422 insert({{cell(0), cell(2), cell(1)}});
1423 insert({{cell(0), cell(3), cell(2)}});
1424 insert({{cell(0), cell(1), cell(3)}});
1425 insert({{cell(1), cell(2), cell(3)}});
1427 else if (cells.cols() == 8)
1429 insert({{cell(0), cell(1), cell(2), cell(3)}});
1430 insert({{cell(1), cell(5), cell(6), cell(3)}});
1431 insert({{cell(5), cell(4), cell(7), cell(6)}});
1432 insert({{cell(0), cell(4), cell(7), cell(3)}});
1433 insert({{cell(0), cell(4), cell(5), cell(1)}});
1434 insert({{cell(2), cell(6), cell(7), cell(3)}});
1438 throw std::runtime_error(
"count_boundary_elements not implemented for polygons");
1442 return boundaries.size();
1447 using namespace GEO;
1448 typedef std::pair<index_t, index_t>
Edge;
1450 if (M.edges.nb() > 0)
1455 if (M.cells.nb() != 0 && M.facets.nb() == 0)
1457 M.cells.compute_borders();
1461 std::vector<std::pair<Edge, index_t>> e2c;
1462 for (index_t f = 0; f < M.facets.nb(); ++f)
1464 for (index_t c = M.facets.corners_begin(f); c < M.facets.corners_end(f); ++c)
1466 index_t v = M.facet_corners.vertex(c);
1467 index_t c2 = M.facets.next_corner_around_facet(f, c);
1468 index_t v2 = M.facet_corners.vertex(c2);
1469 e2c.emplace_back(std::make_pair(std::min(v, v2), std::max(v, v2)), c);
1472 std::sort(e2c.begin(), e2c.end());
1476 Edge prev_e(-1, -1);
1477 for (
const auto &kv : e2c)
1482 M.edges.create_edge(e.first, e.second);
void find_codim_vertices(const Eigen::MatrixXd &vertices, const Eigen::MatrixXi &codim_edges, const Eigen::MatrixXi &faces, Eigen::VectorXi &codim_vertices)
void find_triangle_surface_from_tets(const Eigen::MatrixXi &tets, Eigen::MatrixXi &faces)
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.
Navigation::Index next_around_face(Navigation::Index idx) const
virtual Navigation::Index switch_vertex(Navigation::Index idx) const =0
virtual Navigation::Index get_index_from_face(int f, int lv=0) const =0
virtual Navigation3D::Index get_index_from_element(int hi, int lf, int lv) const =0
virtual RowVectorNd kernel(const int cell_id) const =0
std::array< int, 8 > get_ordered_vertices_from_hex(const int element_index) const
virtual int n_cell_faces(const int c_id) const =0
virtual int cell_face(const int c_id, const int lf_id) const =0
virtual Navigation3D::Index next_around_face(Navigation3D::Index idx) const =0
virtual int n_vertices() const =0
number of vertices
bool is_polytope(const int el_id) const
checks if element is polygon compatible
bool is_cube(const int el_id) const
checks if element is cube compatible
virtual RowVectorNd point(const int global_index) const =0
point coordinates
virtual bool is_boundary_face(const int face_global_id) const =0
is face boundary
virtual bool is_boundary_edge(const int edge_global_id) const =0
is edge boundary
virtual int n_cells() const =0
number of cells
virtual int n_faces() const =0
number of faces
virtual int n_face_vertices(const int f_id) const =0
number of vertices of a face
virtual int n_cell_vertices(const int c_id) const =0
number of vertices of a cell
virtual int face_vertex(const int f_id, const int lv_id) const =0
id of the face vertex
Eigen::Matrix< double, dim, 1 > cross(const Eigen::Matrix< double, dim, 1 > &x, const Eigen::Matrix< double, dim, 1 > &y)
Eigen::ArrayXd P(const int m, const int p, const Eigen::ArrayXd &z)
std::vector< std::pair< Navigation::Index, Navigation::Index > > compute_mesh_interface(const Mesh2D &first, const Mesh2D &second)
Pair coincident boundary edges, including nonconforming leader/follower edges.
bool is_planar(const GEO::Mesh &M, const double tol=1e-5)
Determine if the given mesh is planar (2D or tiny z-range).
void sample_surface(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, int num_samples, Eigen::MatrixXd &P, Eigen::MatrixXd *N=nullptr, int num_lloyd=10, int num_newton=10)
Samples points on a surface.
void orient_closed_surface(const Eigen::MatrixXd &V, Eigen::MatrixXi &F, bool positive=true)
Orient a triangulated surface to have positive volume.
double signed_volume(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F)
Compute the signed volume of a surface mesh.
void reorder_mesh(Eigen::MatrixXd &V, Eigen::MatrixXi &F, const Eigen::VectorXi &C, Eigen::VectorXi &R)
Reorder vertices of a mesh using color tags, so that vertices are ordered by increasing colors.
void orient_normals_2d(GEO::Mesh &M)
Orient facets of a 2D mesh so that each connected component has positive volume.
void extract_parent_edges(const Eigen::MatrixXd &IV, const Eigen::MatrixXi &IE, const Eigen::MatrixXd &BV, const Eigen::MatrixXi &BE, Eigen::MatrixXi &OE)
Extract a set of edges that are overlap with a set given set of parent edges, using vertices position...
ElementType
Type of Element, check [Poly-Spline Finite Element Method] for a complete description.
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.
GEO::vec3 mesh_vertex(const GEO::Mesh &M, GEO::index_t v)
Retrieve a 3D vector with the position of a given vertex.
GEO::index_t mesh_create_vertex(GEO::Mesh &M, const GEO::vec3 &p)
void to_geogram_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, GEO::Mesh &M)
Converts a triangle mesh to a Geogram mesh.
void signed_squared_distances(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, const Eigen::MatrixXd &P, Eigen::VectorXd &D)
Computes the signed squared distance from a list of points to a triangle mesh.
GEO::vec3 facet_barycenter(const GEO::Mesh &M, GEO::index_t f)
void from_geogram_mesh(const GEO::Mesh &M, Eigen::MatrixXd &V, Eigen::MatrixXi &F, Eigen::MatrixXi &T)
Extract simplices from a Geogram mesh.
void generate_edges(GEO::Mesh &M)
assing edges to M
bool read_surface_mesh(const std::string &mesh_path, Eigen::MatrixXd &vertices, Eigen::VectorXi &codim_vertices, Eigen::MatrixXi &codim_edges, Eigen::MatrixXi &faces)
read a surface mesh
void extract_polyhedra(const Mesh3D &mesh, std::vector< std::unique_ptr< GEO::Mesh > > &polys, bool triangulated=false)
Extract polyhedra from a 3D volumetric mesh.
int count_faces(const int dim, const Eigen::MatrixXi &cells)
Count the number of boundary elements (triangles for tetmesh and edges for triangle mesh)
void compute_element_tags(const GEO::Mesh &M, std::vector< ElementType > &element_tags)
Compute the type of each facet in a surface mesh.
bool endswith(const std::string &str, const std::string &suffix)
spdlog::logger & logger()
Retrieves the current logger.
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd