10#include <unordered_set>
15#include <geogram/mesh/mesh_geometry.h>
16#include <geogram/basic/geometry.h>
17#include <geogram/mesh/mesh_preprocessing.h>
18#include <geogram/mesh/mesh_topology.h>
19#include <geogram/mesh/mesh_geometry.h>
20#include <geogram/mesh/mesh_repair.h>
21#include <geogram/mesh/mesh_AABB.h>
22#include <geogram/voronoi/CVT.h>
23#include <geogram/basic/logger.h>
30 if (M.vertices.dimension() == 2)
33 assert(M.vertices.dimension() == 3);
34 GEO::vec3 min_corner, max_corner;
35 GEO::get_bbox(M, &min_corner[0], &max_corner[0]);
36 const double diff = (max_corner[2] - min_corner[2]);
38 return fabs(diff) < tol;
45 for (index_t d = 0; d < std::min(3u, (index_t)M.vertices.dimension()); ++d)
47 if (M.vertices.double_precision())
49 p[d] = M.vertices.point_ptr(v)[d];
53 p[d] = M.vertices.single_precision_point_ptr(v)[d];
65 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
69 return p / M.facets.nb_vertices(f);
77 auto v = M.vertices.create_vertex();
78 for (index_t d = 0; d < std::min(3u, (index_t)M.vertices.dimension()); ++d)
80 if (M.vertices.double_precision())
82 M.vertices.point_ptr(v)[d] = p[d];
86 M.vertices.single_precision_point_ptr(v)[d] = (float)p[d];
98 std::vector<ElementType> old_tags = element_tags;
100 element_tags.resize(M.facets.nb());
103 GEO::Attribute<bool> is_boundary_vertex(M.vertices.attributes(),
"boundary_vertex");
104 std::vector<bool> is_interface_vertex(M.vertices.nb(),
false);
106 for (index_t f = 0; f < M.facets.nb(); ++f)
108 if (M.facets.nb_vertices(f) != 4 || (!old_tags.empty() && old_tags[f] == ElementType::INTERIOR_POLYTOPE))
111 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
113 is_interface_vertex[M.facets.vertex(f, lv)] =
true;
123 std::vector<int> degree(M.vertices.nb(), 0);
124 std::vector<bool> is_regular_vertex(M.vertices.nb());
125 for (index_t f = 0; f < M.facets.nb(); ++f)
127 if (M.facets.nb_vertices(f) == 4)
130 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
132 index_t v = M.facets.vertex(f, lv);
137 for (index_t v = 0; v < M.vertices.nb(); ++v)
140 if (is_boundary_vertex[v] || is_interface_vertex[v])
142 is_regular_vertex[v] = (degree[v] <= 2);
146 is_regular_vertex[v] = (degree[v] == 4);
151 for (index_t f = 0; f < M.facets.nb(); ++f)
153 assert(M.facets.nb_vertices(f) > 2);
154 if (!old_tags.empty() && old_tags[f] == ElementType::INTERIOR_POLYTOPE)
157 if (M.facets.nb_vertices(f) == 4)
162 bool is_boundary_facet =
false;
163 bool is_interface_facet =
false;
164 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
166 if (is_boundary_vertex[M.facets.vertex(f, lv)])
168 is_boundary_facet =
true;
170 if (is_interface_vertex[M.facets.vertex(f, lv)])
172 is_interface_facet =
true;
177 if (is_boundary_facet || is_interface_facet)
181 bool is_singular =
false;
182 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
184 index_t v = M.facets.vertex(f, lv);
185 if (is_boundary_vertex[v] || is_interface_vertex[v])
187 if (!is_regular_vertex[v])
195 if (!is_regular_vertex[v])
197 element_tags[f] = ElementType::UNDEFINED;
203 if (is_interface_facet)
205 element_tags[f] = ElementType::INTERFACE_CUBE;
207 else if (is_singular)
209 element_tags[f] = ElementType::SIMPLE_SINGULAR_BOUNDARY_CUBE;
213 element_tags[f] = ElementType::REGULAR_BOUNDARY_CUBE;
219 int nb_singulars = 0;
220 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
222 if (!is_regular_vertex[M.facets.vertex(f, lv)])
228 if (nb_singulars == 0)
230 element_tags[f] = ElementType::REGULAR_INTERIOR_CUBE;
232 else if (nb_singulars == 1)
234 element_tags[f] = ElementType::SIMPLE_SINGULAR_INTERIOR_CUBE;
238 element_tags[f] = ElementType::MULTI_SINGULAR_INTERIOR_CUBE;
248 GEO::Attribute<bool> boundary_vertices(M.vertices.attributes(),
"boundary_vertex");
249 for (index_t lv = 0; lv < M.facets.nb_vertices(f); ++lv)
251 if (boundary_vertices[M.facets.vertex(f, lv)])
253 tag = ElementType::BOUNDARY_POLYTOPE;
258 element_tags[f] = tag;
264 for (index_t f = 0; f < M.facets.nb(); ++f)
266 if (M.facets.nb_vertices(f) == 3)
268 element_tags[f] = ElementType::SIMPLEX;
279 double signed_area(
const GEO::Mesh &M, GEO::index_t f)
283 index_t v0 = M.facet_corners.vertex(M.facets.corners_begin(f));
284 const vec3 &p0 = Geom::mesh_vertex(M, v0);
286 M.facets.corners_begin(f) + 1;
287 c + 1 < M.facets.corners_end(f); c++)
289 index_t v1 = M.facet_corners.vertex(c);
291 index_t v2 = M.facet_corners.vertex(c + 1);
293 result += Geom::triangle_signed_area(vec2(&p0[0]), vec2(&p1[0]), vec2(&p2[0]));
303 vector<index_t> component;
304 index_t nb_components = get_connected_components(M, component);
305 vector<double> comp_signed_volume(nb_components, 0.0);
306 for (index_t f = 0; f < M.facets.nb(); ++f)
308 comp_signed_volume[component[f]] += signed_area(M, f);
310 for (index_t f = 0; f < M.facets.nb(); ++f)
312 if (comp_signed_volume[component[f]] < 0.0)
323 assert(
V.rows() == C.size());
324 int num_colors = C.maxCoeff() + 1;
325 Eigen::VectorXi count(num_colors);
327 for (
int i = 0; i < C.size(); ++i)
331 R.resize(num_colors + 1);
333 for (
int c = 0; c < num_colors; ++c)
335 R(c + 1) = R(c) + count(c);
338 Eigen::VectorXi remap(C.size());
339 for (
int i = 0; i < C.size(); ++i)
341 remap[i] = R(C(i)) + count[C(i)];
345 Eigen::MatrixXd NV(
V.rows(),
V.cols());
346 for (
int v = 0; v <
V.rows(); ++v)
348 NV.row(remap(v)) =
V.row(v);
352 for (
int f = 0; f <
F.rows(); ++f)
354 for (
int lv = 0; lv <
F.cols(); ++lv)
356 F(f, lv) = remap(
F(f, lv));
366 void compute_unsigned_distance_field(
const GEO::Mesh &M,
367 const GEO::MeshFacetsAABB &aabb_tree,
const Eigen::MatrixXd &P, Eigen::VectorXd &D)
369 assert(P.cols() == 3);
371#pragma omp parallel for
372 for (
int i = 0; i < P.rows(); ++i)
374 GEO::vec3 pos(P(i, 0), P(i, 1), P(i, 2));
375 double sq_dist = aabb_tree.squared_distance(pos);
383 double x1,
double y1,
double x2,
double y2,
double &twice_signed_area)
385 twice_signed_area = y1 * x2 - x1 * y2;
386 if (twice_signed_area > 0)
388 else if (twice_signed_area < 0)
407 bool point_in_triangle_2d(
408 double x0,
double y0,
double x1,
double y1,
409 double x2,
double y2,
double x3,
double y3,
410 double &a,
double &b,
double &c)
418 int signa = orientation(x2, y2, x3, y3, a);
421 int signb = orientation(x3, y3, x1, y1, b);
424 int signc = orientation(x1, y1, x2, y2, c);
427 double sum = a +
b + c;
428 geo_assert(sum != 0);
449 inline GEO::Sign orient_2d_inexact(GEO::vec2 p0, GEO::vec2 p1, GEO::vec2 p2)
451 double a11 = p1[0] - p0[0];
452 double a12 = p1[1] - p0[1];
454 double a21 = p2[0] - p0[0];
455 double a22 = p2[1] - p0[1];
457 double Delta = GEO::det2x2(
461 return GEO::geo_sgn(Delta);
476 template <
int X = 0,
int Y = 1,
int Z = 2>
477 int intersect_ray_z(
const GEO::Mesh &M, GEO::index_t f,
const GEO::vec3 &q,
double &
z)
481 index_t c =
M.facets.corners_begin(f);
482 const vec3 &p1 = Geom::mesh_vertex(M,
M.facet_corners.vertex(c++));
483 const vec3 &p2 = Geom::mesh_vertex(M,
M.facet_corners.vertex(c++));
484 const vec3 &p3 = Geom::mesh_vertex(M,
M.facet_corners.vertex(c));
487 if (point_in_triangle_2d(
488 q[X], q[Y], p1[X], p1[Y], p2[X], p2[Y], p3[X], p3[Y], u, v, w))
490 z = u * p1[Z] + v * p2[Z] + w * p3[Z];
491 auto sign = orient_2d_inexact(vec2(p1[X], p1[Y]), vec2(p2[X], p2[Y]), vec2(p3[X], p3[Y]));
509 void compute_sign(
const GEO::Mesh &M,
const GEO::MeshFacetsAABB &aabb_tree,
510 const Eigen::MatrixXd &P, Eigen::VectorXd &D)
512 assert(
P.cols() == 3);
513 assert(D.size() ==
P.rows());
515 GEO::vec3 min_corner, max_corner;
516 GEO::get_bbox(M, &min_corner[0], &max_corner[0]);
518#pragma omp parallel for
519 for (
int k = 0; k <
P.rows(); ++k)
521 GEO::vec3 center(
P(k, 0),
P(k, 1),
P(k, 2));
524 box.xyz_min[0] = box.xyz_max[0] = center[0];
525 box.xyz_min[1] = box.xyz_max[1] = center[1];
526 box.xyz_min[2] = min_corner[2];
527 box.xyz_max[2] = max_corner[2];
529 std::vector<std::pair<double, int>> inter;
530 auto action = [&
M, &inter, ¢er](GEO::index_t
f) {
532 if (
int s = intersect_ray_z(M, f, center,
z))
534 inter.emplace_back(
z, s);
537 aabb_tree.compute_bbox_facet_bbox_intersections(box, action);
538 std::sort(inter.begin(), inter.end());
540 std::vector<double> reduced;
541 for (
int i = 0, s = 0; i < (int)inter.size(); ++i)
543 const int ds = inter[i].second;
545 if ((s == -1 && ds < 0) || (s == 0 && ds > 0))
547 reduced.push_back(inter[i].first);
552 for (
double z : reduced)
559 if (num_before % 2 == 1)
575 M.vertices.create_vertices((
int)
V.rows());
576 for (
int i = 0; i < (int)M.vertices.nb(); ++i)
578 GEO::vec3 &p = M.vertices.point(i);
581 p[2] =
V.cols() >= 3 ?
V(i, 2) : 0;
586 M.facets.create_triangles((
int)
F.rows());
588 else if (
F.cols() == 4)
590 M.facets.create_quads((
int)
F.rows());
594 throw std::runtime_error(
"Mesh format not supported");
596 for (
int c = 0; c < (int)M.facets.nb(); ++c)
598 for (
int lv = 0; lv <
F.cols(); ++lv)
600 M.facets.set_vertex(c, lv,
F(c, lv));
637 V.resize(M.vertices.nb(), 3);
638 for (
int i = 0; i < (int)M.vertices.nb(); ++i)
640 GEO::vec3 p = M.vertices.point(i);
641 V.row(i) << p[0], p[1], p[2];
643 assert(M.facets.are_simplices());
644 F.resize(M.facets.nb(), 3);
645 for (
int c = 0; c < (int)M.facets.nb(); ++c)
647 for (
int lv = 0; lv < 3; ++lv)
649 F(c, lv) = M.facets.vertex(c, lv);
652 assert(M.cells.are_simplices());
653 T.resize(M.cells.nb(), 4);
654 for (
int c = 0; c < (int)M.cells.nb(); ++c)
656 for (
int lv = 0; lv < 4; ++lv)
658 T(c, lv) = M.cells.vertex(c, lv);
666 const Eigen::MatrixXd &P, Eigen::VectorXd &D)
670 GEO::MeshFacetsAABB aabb_tree(M);
671 compute_unsigned_distance_field(M, aabb_tree, P, D);
672 compute_sign(M, aabb_tree, P, D);
679 assert(
F.cols() == 3);
680 assert(
V.cols() == 3);
681 std::array<Eigen::RowVector3d, 4> t;
682 t[3] = Eigen::RowVector3d::Zero(
V.cols());
683 double volume_total = 0;
684 for (
int f = 0; f <
F.rows(); ++f)
686 for (
int lv = 0; lv <
F.cols(); ++lv)
688 t[lv] =
V.row(
F(f, lv));
690 double vol = GEO::Geom::tetra_signed_volume(t[0].data(), t[1].data(), t[2].data(), t[3].data());
693 return -volume_total;
702 for (
int f = 0; f <
F.rows(); ++f)
704 F.row(f) =
F.row(f).reverse().eval();
713 struct EdgeInterfacePrimitive
716 Eigen::Vector2d from;
720 struct FaceInterfacePrimitive
723 std::vector<Eigen::Vector3d> vertices;
728 return a.size() ==
b.size() && (a -
b).squaredNorm() <= 1
e-24;
731 bool overlapping_segments(
const EdgeInterfacePrimitive &first,
const EdgeInterfacePrimitive &second)
733 const Eigen::Vector2d direction = first.to - first.from;
734 const double length = direction.norm();
735 const double second_length = (second.to - second.from).
norm();
736 const double scale = std::max({1.0, length, second_length});
737 const double tolerance = 1
e-12 * scale;
738 if (length <= tolerance || second_length <= tolerance)
741 auto cross = [](
const Eigen::Vector2d &a,
const Eigen::Vector2d &
b) {
742 return a.x() *
b.y() - a.y() *
b.x();
744 if (std::abs(
cross(direction, second.from - first.from)) > tolerance * length
745 || std::abs(
cross(direction, second.to - first.from)) > tolerance * length)
748 const Eigen::Vector2d tangent = direction / length;
749 const double second_from = (second.from - first.from).dot(tangent);
750 const double second_to = (second.to - first.from).dot(tangent);
751 const double overlap_begin = std::max(0.0, std::min(second_from, second_to));
752 const double overlap_end = std::min(length, std::max(second_from, second_to));
753 return overlap_end - overlap_begin > tolerance;
756 Eigen::Vector3d face_normal(
const FaceInterfacePrimitive &face)
758 Eigen::Vector3d normal = Eigen::Vector3d::Zero();
759 for (
int i = 0; i < face.vertices.size(); ++i)
760 normal += face.vertices[i].cross(face.vertices[(i + 1) % face.vertices.size()]);
764 Eigen::Vector2d project_point(
const Eigen::Vector3d &
point,
const int dropped_axis)
766 if (dropped_axis == 0)
768 if (dropped_axis == 1)
773 bool point_in_convex_polygon(
774 const Eigen::Vector2d &
point,
775 const std::vector<Eigen::Vector2d> &polygon,
776 const double tolerance)
779 for (
int i = 0; i < polygon.size(); ++i)
781 const Eigen::Vector2d edge = polygon[(i + 1) % polygon.size()] - polygon[i];
782 const Eigen::Vector2d offset =
point - polygon[i];
783 const double cross = edge.x() * offset.y() - edge.y() * offset.x();
784 if (std::abs(cross) <= tolerance)
788 else if (sign * cross < 0)
794 bool overlapping_faces(
const FaceInterfacePrimitive &first,
const FaceInterfacePrimitive &second)
796 const Eigen::Vector3d first_normal = face_normal(first);
797 const Eigen::Vector3d second_normal = face_normal(second);
798 const double first_area_scale = first_normal.norm();
799 const double second_area_scale = second_normal.norm();
800 const double coordinate_scale = std::max({1.0,
801 first.vertices.front().
norm(),
802 second.vertices.front().norm()});
803 const double tolerance = 1
e-12 * coordinate_scale;
804 if (first_area_scale <= tolerance * tolerance || second_area_scale <= tolerance * tolerance)
807 const Eigen::Vector3d unit_normal = first_normal / first_area_scale;
808 if (unit_normal.cross(second_normal / second_area_scale).norm() > 1e-10)
811 if (std::abs(unit_normal.dot(
point - first.
vertices.front())) > tolerance)
814 Eigen::Index dropped_axis;
815 unit_normal.cwiseAbs().maxCoeff(&dropped_axis);
816 std::vector<Eigen::Vector2d> first_polygon, second_polygon;
817 first_polygon.reserve(first.vertices.size());
818 second_polygon.reserve(second.vertices.size());
820 first_polygon.push_back(project_point(
point, dropped_axis));
822 second_polygon.push_back(project_point(
point, dropped_axis));
824 auto contains = [tolerance](
const auto &container,
const auto &contained) {
825 return std::all_of(contained.begin(), contained.end(), [&](
const Eigen::Vector2d &
point) {
826 return point_in_convex_polygon(point, container, tolerance);
829 return contains(first_polygon, second_polygon) || contains(second_polygon, first_polygon);
833std::vector<std::pair<polyfem::mesh::Navigation::Index, polyfem::mesh::Navigation::Index>>
837 std::vector<EdgeInterfacePrimitive> first_edges, second_edges;
838 auto collect = [](
const Mesh2D &mesh,
auto &edges) {
839 for (
int f = 0; f < mesh.
n_faces(); ++f)
847 edges.push_back({index,
848 mesh.
point(index.vertex).head<2>(),
849 mesh.
point(opposite.vertex).head<2>()});
855 collect(first, first_edges);
856 collect(second, second_edges);
858 std::vector<std::pair<Index, Index>> result;
859 std::set<std::tuple<int, int, int, int, int, int>> visited_pairs;
860 for (
const auto &lhs : first_edges)
862 for (
const auto &rhs : second_edges)
864 if (!overlapping_segments(lhs, rhs))
866 Index second_index = rhs.index;
867 if ((lhs.to - lhs.from).dot(rhs.to - rhs.from) > 0)
869 const auto key = std::make_tuple(
870 lhs.index.face, lhs.index.edge, lhs.index.vertex,
871 second_index.face, second_index.edge, second_index.vertex);
872 if (visited_pairs.insert(key).second)
873 result.emplace_back(lhs.index, second_index);
879std::vector<std::pair<polyfem::mesh::Navigation3D::Index, polyfem::mesh::Navigation3D::Index>>
883 std::vector<FaceInterfacePrimitive> first_faces, second_faces;
884 auto collect = [](
const Mesh3D &mesh,
auto &
faces) {
885 std::unordered_set<int> visited_faces;
886 for (
int c = 0; c < mesh.
n_cells(); ++c)
895 for (
int lv = 0; lv < vertices.size(); ++lv)
897 faces.push_back({index, std::move(vertices)});
901 collect(first, first_faces);
902 collect(second, second_faces);
904 std::vector<std::pair<Index, Index>> result;
905 for (
const auto &lhs : first_faces)
907 for (
const auto &rhs : second_faces)
909 if (!overlapping_faces(lhs, rhs))
911 Index second_index = rhs.index;
914 if (same_point(first.
point(lhs.index.vertex), second.
point(second_index.vertex)))
918 result.emplace_back(lhs.index, second_index);
927 std::vector<int> vertex_l2g;
928 for (
int c = 0; c < mesh.
n_cells(); ++c)
934 auto poly = std::make_unique<GEO::Mesh>();
937 poly->vertices.create_vertices((triangulated ? nv + nf : nv) + 1);
939 vertex_l2g.reserve(nv);
940 for (
int lf = 0; lf < nf; ++lf)
942 GEO::vector<GEO::index_t> facet_vertices;
946 Eigen::RowVector3d p = mesh.
point(index.vertex);
947 if (vertex_g2l[index.vertex] < 0)
949 vertex_g2l[index.vertex] = vertex_l2g.size();
950 vertex_l2g.push_back(index.vertex);
952 int v1 = vertex_g2l[index.vertex];
953 facet_vertices.push_back(v1);
954 poly->vertices.point(v1) = GEO::vec3(p.data());
959 GEO::vec3 p(0, 0, 0);
960 for (GEO::index_t lv = 0; lv < facet_vertices.size(); ++lv)
962 p += poly->vertices.point(facet_vertices[lv]);
964 p /= facet_vertices.size();
965 int v0 = vertex_l2g.size();
966 vertex_l2g.push_back(0);
967 poly->vertices.point(v0) = p;
968 for (GEO::index_t lv = 0; lv < facet_vertices.size(); ++lv)
970 int v1 = facet_vertices[lv];
971 int v2 = facet_vertices[(lv + 1) % facet_vertices.size()];
972 poly->facets.create_triangle(v0, v1, v2);
977 poly->facets.create_polygon(facet_vertices);
981 Eigen::RowVector3d p = mesh.
kernel(c);
982 poly->vertices.point(nv) = GEO::vec3(p.data());
984 assert(vertex_l2g.size() ==
size_t(triangulated ? nv + nf : nv));
986 for (
int v : vertex_l2g)
991 poly->facets.compute_borders();
992 poly->facets.connect();
994 polys.emplace_back(std::move(poly));
1024 M.vertices.create_vertices((
int)mesh.
n_vertices());
1025 for (
int i = 0; i < (int)M.vertices.nb(); ++i)
1027 auto pt = mesh.
point(i);
1028 GEO::vec3 &p = M.vertices.point(i);
1034 for (
int f = 0, lf = 0; f < mesh.
n_faces(); ++f)
1039 M.facets.create_polygon(nv);
1040 for (
int lv = 0; lv < nv; ++lv)
1042 M.facets.set_vertex(lf, lv, mesh.
face_vertex(f, lv));
1048 typedef std::array<int, 8> Vector8i;
1049 Vector8i g2p = {{0, 4, 1, 5, 3, 7, 2, 6}};
1050 for (
int c = 0; c < mesh.
n_cells(); ++c)
1056 for (
size_t k = 0; k < 8; ++k)
1058 lvg[k] = lvp[g2p[k]];
1060 std::reverse(lvg.begin(), lvg.end());
1062 lvg[0], lvg[1], lvg[2], lvg[3],
1063 lvg[4], lvg[5], lvg[6], lvg[7]);
1073 GEO::mesh_reorient(M);
1079 Eigen::MatrixXd &P, Eigen::MatrixXd *N,
int num_lloyd,
int num_newton)
1081 assert(num_samples > 3);
1084 GEO::CentroidalVoronoiTesselation CVT(&M);
1086 bool was_quiet = GEO::Logger::instance()->is_quiet();
1087 GEO::Logger::instance()->set_quiet(
true);
1088 CVT.compute_initial_sampling(num_samples);
1089 GEO::Logger::instance()->set_quiet(was_quiet);
1093 CVT.Lloyd_iterations(num_lloyd);
1098 CVT.Newton_iterations(num_newton);
1101 P.resize(3, num_samples);
1102 std::copy_n(CVT.embedding(0), 3 * num_samples, P.data());
1103 P.transposeInPlace();
1107 GEO::MeshFacetsAABB aabb(M);
1109 for (
int i = 0; i < num_samples; ++i)
1111 GEO::vec3 p(P(i, 0), P(i, 1), P(i, 2));
1112 GEO::vec3 nearest_point;
1114 auto f = aabb.nearest_facet(p, nearest_point, sq_dist);
1115 GEO::vec3 n = normalize(GEO::Geom::mesh_facet_normal(M, f));
1116 N->row(i) << n[0], n[1], n[2];
1126 bool approx_aligned(
const double *a_,
const double *b_,
const double *p_,
const double *q_,
double tol = 1e-6)
1128 using namespace GEO;
1129 vec3 a(a_), b(b_), p(p_), q(q_);
1130 double da = std::sqrt(Geom::point_segment_squared_distance(a, p, q));
1131 double db = std::sqrt(Geom::point_segment_squared_distance(b, p, q));
1132 double cos_theta = Geom::cos_angle(b - a, p - q);
1133 return (
da < tol && db < tol && std::abs(std::abs(cos_theta) - 1.0) < tol);
1141 const Eigen::MatrixXd &BV,
const Eigen::MatrixXi &BE, Eigen::MatrixXi &OE)
1143 assert(IV.cols() == 2 || IV.cols() == 3);
1144 assert(BV.cols() == 2 || BV.cols() == 3);
1145 typedef std::pair<int, int>
Edge;
1146 std::vector<Edge> selected;
1147 for (
int e1 = 0; e1 < IE.rows(); ++e1)
1149 Eigen::RowVector3d a;
1151 a.head(IV.cols()) = IV.row(IE(e1, 0));
1152 Eigen::RowVector3d b;
1154 b.head(IV.cols()) = IV.row(IE(e1, 1));
1155 for (
int e2 = 0; e2 < BE.rows(); ++e2)
1157 Eigen::RowVector3d p;
1159 p.head(BV.cols()) = BV.row(BE(e2, 0));
1160 Eigen::RowVector3d q;
1162 q.head(BV.cols()) = BV.row(BE(e2, 1));
1163 if (approx_aligned(a.data(), b.data(), p.data(), q.data()))
1165 selected.emplace_back(IE(e1, 0), IE(e1, 1));
1171 OE.resize(selected.size(), 2);
1172 for (
int e = 0; e < OE.rows(); ++e)
1174 OE.row(e) << selected[e].first, selected[e].second;
1181 const Eigen::MatrixXi &tets,
1182 Eigen::MatrixXi &
faces)
1184 std::unordered_set<Eigen::Vector3i, HashMatrix> tri_to_tet(4 * tets.rows());
1185 for (
int i = 0; i < tets.rows(); i++)
1187 tri_to_tet.emplace(tets(i, 0), tets(i, 2), tets(i, 1));
1188 tri_to_tet.emplace(tets(i, 0), tets(i, 3), tets(i, 2));
1189 tri_to_tet.emplace(tets(i, 0), tets(i, 1), tets(i, 3));
1190 tri_to_tet.emplace(tets(i, 1), tets(i, 2), tets(i, 3));
1193 std::vector<Eigen::RowVector3i> faces_vector;
1194 for (
const auto &tri : tri_to_tet)
1197 bool is_surface_triangle =
1198 tri_to_tet.find(Eigen::Vector3i(tri[2], tri[1], tri[0])) == tri_to_tet.end()
1199 && tri_to_tet.find(Eigen::Vector3i(tri[1], tri[0], tri[2])) == tri_to_tet.end()
1200 && tri_to_tet.find(Eigen::Vector3i(tri[0], tri[2], tri[1])) == tri_to_tet.end();
1201 if (is_surface_triangle)
1203 faces_vector.emplace_back(tri[0], tri[1], tri[2]);
1207 faces.resize(faces_vector.size(), 3);
1208 for (
int i = 0; i <
faces.rows(); i++)
1210 faces.row(i) = faces_vector[i];
1215 const Eigen::MatrixXd &vertices,
1216 const Eigen::MatrixXi &tets,
1217 Eigen::MatrixXd &surface_vertices,
1218 Eigen::MatrixXi &tris)
1220 Eigen::MatrixXi full_tris;
1223 std::unordered_map<int, int> full_to_surface;
1224 std::vector<size_t> surface_to_full;
1225 for (
int i = 0; i < full_tris.rows(); i++)
1227 for (
int j = 0; j < full_tris.cols(); j++)
1229 if (full_to_surface.find(full_tris(i, j)) == full_to_surface.end())
1231 full_to_surface[full_tris(i, j)] = surface_to_full.size();
1232 surface_to_full.push_back(full_tris(i, j));
1237 surface_vertices.resize(surface_to_full.size(), 3);
1238 for (
int i = 0; i < surface_to_full.size(); i++)
1240 surface_vertices.row(i) = vertices.row(surface_to_full[i]);
1243 tris.resize(full_tris.rows(), full_tris.cols());
1244 for (
int i = 0; i < tris.rows(); i++)
1246 for (
int j = 0; j < tris.cols(); j++)
1248 tris(i, j) = full_to_surface[full_tris(i, j)];
1255 std::unordered_set<std::vector<int>,
HashVector> boundaries;
1257 auto insert = [&](std::vector<int> v) {
1258 std::sort(v.begin(), v.end());
1259 boundaries.insert(v);
1262 for (
int i = 0; i < cells.rows(); i++)
1264 const auto &cell = cells.row(i);
1265 if (cells.cols() == 3)
1267 insert({{cell(0), cell(1)}});
1268 insert({{cell(1), cell(2)}});
1269 insert({{cell(2), cell(0)}});
1271 else if (cells.cols() == 4 && dim == 2)
1273 insert({{cell(0), cell(1)}});
1274 insert({{cell(1), cell(2)}});
1275 insert({{cell(2), cell(3)}});
1276 insert({{cell(3), cell(0)}});
1278 else if (cells.cols() == 4 && dim == 3)
1280 insert({{cell(0), cell(2), cell(1)}});
1281 insert({{cell(0), cell(3), cell(2)}});
1282 insert({{cell(0), cell(1), cell(3)}});
1283 insert({{cell(1), cell(2), cell(3)}});
1285 else if (cells.cols() == 8)
1287 insert({{cell(0), cell(1), cell(2), cell(3)}});
1288 insert({{cell(1), cell(5), cell(6), cell(3)}});
1289 insert({{cell(5), cell(4), cell(7), cell(6)}});
1290 insert({{cell(0), cell(4), cell(7), cell(3)}});
1291 insert({{cell(0), cell(4), cell(5), cell(1)}});
1292 insert({{cell(2), cell(6), cell(7), cell(3)}});
1296 throw std::runtime_error(
"count_boundary_elements not implemented for polygons");
1300 return boundaries.size();
1305 using namespace GEO;
1306 typedef std::pair<index_t, index_t>
Edge;
1308 if (M.edges.nb() > 0)
1313 if (M.cells.nb() != 0 && M.facets.nb() == 0)
1315 M.cells.compute_borders();
1319 std::vector<std::pair<Edge, index_t>> e2c;
1320 for (index_t f = 0; f < M.facets.nb(); ++f)
1322 for (index_t c = M.facets.corners_begin(f); c < M.facets.corners_end(f); ++c)
1324 index_t v = M.facet_corners.vertex(c);
1325 index_t c2 = M.facets.next_corner_around_facet(f, c);
1326 index_t v2 = M.facet_corners.vertex(c2);
1327 e2c.emplace_back(std::make_pair(std::min(v, v2), std::max(v, v2)), c);
1330 std::sort(e2c.begin(), e2c.end());
1334 Edge prev_e(-1, -1);
1335 for (
const auto &kv : e2c)
1340 M.edges.create_edge(e.first, e.second);
void find_triangle_surface_from_tets(const Eigen::MatrixXi &tets, Eigen::MatrixXi &faces)
std::vector< Eigen::VectorXi > faces
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
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.
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd