12#include <unordered_set>
21#include <Eigen/Geometry>
31 assert(
V.cols() == 3);
34 const double diff =
V.col(2).maxCoeff() -
V.col(2).minCoeff();
36 return fabs(diff) < tol;
49 for (
int lv = 0; lv < M.n_facet_vertices(f); ++lv)
53 return p / double(M.n_facet_vertices(f));
60 return M.create_vertex(p);
69 std::vector<ElementType> old_tags = element_tags;
71 element_tags.resize(M.n_facets());
74 const std::vector<bool> &is_boundary_vertex = M.boundary_vertex;
75 std::vector<bool> is_interface_vertex(M.n_vertices(),
false);
77 for (index_t f = 0; f < M.n_facets(); ++f)
79 if (M.n_facet_vertices(f) != 4 || (!old_tags.empty() && old_tags[f] == ElementType::INTERIOR_POLYTOPE))
82 for (index_t lv = 0; lv < M.n_facet_vertices(f); ++lv)
84 is_interface_vertex[M.facet_vertex(f, lv)] =
true;
94 std::vector<int> degree(M.n_vertices(), 0);
95 std::vector<bool> is_regular_vertex(M.n_vertices());
96 for (index_t f = 0; f < M.n_facets(); ++f)
98 if (M.n_facet_vertices(f) == 4)
101 for (index_t lv = 0; lv < M.n_facet_vertices(f); ++lv)
103 index_t v = M.facet_vertex(f, lv);
108 for (index_t v = 0; v < M.n_vertices(); ++v)
111 if (is_boundary_vertex[v] || is_interface_vertex[v])
113 is_regular_vertex[v] = (degree[v] <= 2);
117 is_regular_vertex[v] = (degree[v] == 4);
122 for (index_t f = 0; f < M.n_facets(); ++f)
124 assert(M.n_facet_vertices(f) > 2);
125 if (!old_tags.empty() && old_tags[f] == ElementType::INTERIOR_POLYTOPE)
128 if (M.n_facet_vertices(f) == 4)
133 bool is_boundary_facet =
false;
134 bool is_interface_facet =
false;
135 for (index_t lv = 0; lv < M.n_facet_vertices(f); ++lv)
137 if (is_boundary_vertex[M.facet_vertex(f, lv)])
139 is_boundary_facet =
true;
141 if (is_interface_vertex[M.facet_vertex(f, lv)])
143 is_interface_facet =
true;
148 if (is_boundary_facet || is_interface_facet)
153 bool is_singular =
false;
154 for (index_t lv = 0; lv < M.n_facet_vertices(f); ++lv)
156 if (!is_regular_vertex[M.facet_vertex(f, lv)])
163 if (is_interface_facet)
165 element_tags[f] = ElementType::INTERFACE_CUBE;
167 else if (is_singular)
169 element_tags[f] = ElementType::SIMPLE_SINGULAR_BOUNDARY_CUBE;
173 element_tags[f] = ElementType::REGULAR_BOUNDARY_CUBE;
179 int nb_singulars = 0;
180 for (index_t lv = 0; lv < M.n_facet_vertices(f); ++lv)
182 if (!is_regular_vertex[M.facet_vertex(f, lv)])
188 if (nb_singulars == 0)
190 element_tags[f] = ElementType::REGULAR_INTERIOR_CUBE;
192 else if (nb_singulars == 1)
194 element_tags[f] = ElementType::SIMPLE_SINGULAR_INTERIOR_CUBE;
198 element_tags[f] = ElementType::MULTI_SINGULAR_INTERIOR_CUBE;
208 const std::vector<bool> &boundary_vertices = M.boundary_vertex;
209 for (index_t lv = 0; lv < M.n_facet_vertices(f); ++lv)
211 if (boundary_vertices[M.facet_vertex(f, lv)])
213 tag = ElementType::BOUNDARY_POLYTOPE;
218 element_tags[f] = tag;
224 for (index_t f = 0; f < M.n_facets(); ++f)
226 if (M.n_facet_vertices(f) == 3)
228 element_tags[f] = ElementType::SIMPLEX;
240 inline double det_2d(
const double ax,
const double ay,
const double bx,
const double by)
242 return ax * by - ay * bx;
245 inline double triangle_signed_area_2d(
250 return 0.5 * det_2d(p2[0] - p1[0], p2[1] - p1[1], p3[0] - p1[0], p3[1] - p1[1]);
257 int v0 =
M.corner_vertex(
M.corners_begin(f));
259 for (
int c =
M.corners_begin(f) + 1; c + 1 <
M.corners_end(f); c++)
261 int v1 =
M.corner_vertex(c);
263 int v2 =
M.corner_vertex(c + 1);
265 result += triangle_signed_area_2d(p0, p1, p2);
274 std::vector<int> component;
275 int nb_components = M.connected_components(component);
276 std::vector<double> comp_signed_volume(nb_components, 0.0);
277 for (
int f = 0; f < M.n_facets(); ++f)
279 comp_signed_volume[component[f]] += signed_area(M, f);
281 for (
int f = 0; f < M.n_facets(); ++f)
283 if (comp_signed_volume[component[f]] < 0.0)
294 assert(
V.rows() == C.size());
295 int num_colors = C.maxCoeff() + 1;
296 Eigen::VectorXi count(num_colors);
298 for (
int i = 0; i < C.size(); ++i)
302 R.resize(num_colors + 1);
304 for (
int c = 0; c < num_colors; ++c)
306 R(c + 1) = R(c) + count(c);
309 Eigen::VectorXi remap(C.size());
310 for (
int i = 0; i < C.size(); ++i)
312 remap[i] = R(C(i)) + count[C(i)];
316 Eigen::MatrixXd NV(
V.rows(),
V.cols());
317 for (
int v = 0; v <
V.rows(); ++v)
319 NV.row(remap(v)) =
V.row(v);
323 for (
int f = 0; f <
F.rows(); ++f)
325 for (
int lv = 0; lv <
F.cols(); ++lv)
327 F(f, lv) = remap(
F(f, lv));
335 const Eigen::RowVector3d &interior_point,
const double distance, Eigen::MatrixXd &offset)
337 Eigen::MatrixXd normals = Eigen::MatrixXd::Zero(
V.rows(), 3);
338 Eigen::VectorXd incident_area = Eigen::VectorXd::Zero(
V.rows());
339 for (
int f = 0; f <
F.rows(); ++f)
341 const Eigen::RowVector3d a =
V.row(
F(f, 1)) -
V.row(
F(f, 0));
342 const Eigen::RowVector3d b =
V.row(
F(f, 2)) -
V.row(
F(f, 0));
343 const Eigen::RowVector3d normal = a.cross(b);
344 const double area = normal.norm();
345 for (
int j = 0; j < 3; ++j)
347 normals.row(
F(f, j)) += normal;
348 incident_area(
F(f, j)) += area;
353 for (
int v = 0; v <
V.rows(); ++v)
355 Eigen::RowVector3d direction = normals.row(v);
357 if (direction.norm() <= 64 * std::numeric_limits<double>::epsilon() * incident_area(v))
358 direction =
V.row(v) - interior_point;
359 const double length = direction.norm();
361 offset.row(v) += (distance / length) * direction;
378 inline Vec3 vec3(
const double *p) {
return {p[0], p[1], p[2]}; }
379 inline Vec3 operator-(
const Vec3 &a,
const Vec3 &b) {
return {a.x -
b.x, a.y -
b.y, a.z -
b.z}; }
380 inline Vec3 operator+(
const Vec3 &a,
const Vec3 &b) {
return {a.x +
b.x, a.y +
b.y, a.z +
b.z}; }
381 inline Vec3 operator*(
double s,
const Vec3 &v) {
return {s * v.x, s * v.y, s * v.z}; }
382 inline double dot(
const Vec3 &a,
const Vec3 &b) {
return a.x *
b.x + a.y *
b.y + a.z *
b.z; }
383 inline double length2(
const Vec3 &v) {
return v.x * v.x + v.y * v.y + v.z * v.z; }
384 inline Vec3
cross(
const Vec3 &a,
const Vec3 &b)
387 a.y *
b.z - a.z *
b.y,
388 a.z *
b.x - a.x *
b.z,
389 a.x *
b.y - a.y *
b.x};
391 inline double distance2(
const Vec3 &a,
const Vec3 &b)
393 const double dx = a.x -
b.x;
394 const double dy = a.y -
b.y;
395 const double dz = a.z -
b.z;
396 return dx * dx + dy * dy + dz * dz;
399 inline double tetra_signed_volume(
const Vec3 &p1,
const Vec3 &p2,
const Vec3 &p3,
const Vec3 &p4)
401 return dot(p2 - p1,
cross(p3 - p1, p4 - p1)) / 6.0;
404 double point_segment_squared_distance(
const Vec3 &
point,
const Vec3 &v0,
const Vec3 &v1)
406 const double l2 = distance2(v0, v1);
407 const double t = dot(
point - v0, v1 - v0);
408 if (t <= 0.0 || l2 == 0.0)
409 return distance2(
point, v0);
411 return distance2(
point, v1);
412 const double lambda1 = t / l2;
413 const double lambda0 = 1.0 - lambda1;
414 const Vec3 closest_point = lambda0 * v0 + lambda1 * v1;
415 return distance2(
point, closest_point);
418 double cos_angle(
const Vec3 &a,
const Vec3 &b)
420 const double lab = std::sqrt(length2(a) * length2(b));
421 double result = (lab > 1
e-50) ? (dot(a, b) / lab) : 1.0;
426 else if (result > 1.0)
434 double x1,
double y1,
double x2,
double y2,
double &twice_signed_area)
436 twice_signed_area = y1 * x2 - x1 * y2;
437 if (twice_signed_area > 0)
439 else if (twice_signed_area < 0)
458 bool point_in_triangle_2d(
459 double x0,
double y0,
double x1,
double y1,
460 double x2,
double y2,
double x3,
double y3,
461 double &a,
double &b,
double &c)
469 int signa = orientation(x2, y2, x3, y3, a);
472 int signb = orientation(x3, y3, x1, y1, b);
475 int signc = orientation(x1, y1, x2, y2, c);
478 double sum = a +
b + c;
490 inline int orient_2d_inexact(
491 const double p0x,
const double p0y,
492 const double p1x,
const double p1y,
493 const double p2x,
const double p2y)
495 double a11 = p1x - p0x;
496 double a12 = p1y - p0y;
498 double a21 = p2x - p0x;
499 double a22 = p2y - p0y;
501 double Delta = a11 * a22 - a12 * a21;
503 return (Delta > 0) ? 1 : ((Delta < 0) ? -1 : 0);
519 template <
int X = 0,
int Y = 1,
int Z = 2>
520 int intersect_ray_z(
const Eigen::MatrixXd &
V,
const Eigen::MatrixXi &
F,
int f,
const Eigen::RowVector3d &q,
double &
z)
522 const auto p1 =
V.row(
F(f, 0));
523 const auto p2 =
V.row(
F(f, 1));
524 const auto p3 =
V.row(
F(f, 2));
527 if (point_in_triangle_2d(
528 q[X], q[Y], p1[X], p1[Y], p2[X], p2[Y], p3[X], p3[Y], u, v, w))
530 z = u * p1[Z] + v * p2[Z] + w * p3[Z];
531 return orient_2d_inexact(p1[X], p1[Y], p2[X], p2[Y], p3[X], p3[Y]);
549 TriangleBVH(
const Eigen::MatrixXd &
V,
const Eigen::MatrixXi &
F)
550 : V_(
V),
F_(
F), boxes_(
F.rows()), order_(
F.rows())
552 std::vector<std::array<double, 3>> centroids(
F.rows());
553 for (
int f = 0;
f <
F.rows(); ++
f)
555 for (
int d = 0; d < 3; ++d)
557 const double a =
V(
F(f, 0), d),
b =
V(
F(f, 1), d), c =
V(
F(f, 2), d);
558 boxes_[
f].min[d] = std::min(a, std::min(b, c));
559 boxes_[
f].max[d] = std::max(a, std::max(b, c));
560 centroids[
f][d] = (a +
b + c) / 3;
563 std::iota(order_.begin(), order_.end(), 0);
564 nodes_.reserve(2 *
F.rows());
566 build(0,
F.rows(), centroids);
570 template <
typename Fn>
571 void for_each_in_xy_column(
const double x,
const double y, Fn &&fn)
const
575 std::vector<int> stack = {0};
576 while (!stack.empty())
578 const Node &node = nodes_[stack.back()];
580 if (!contains_xy(node.box,
x,
y))
584 for (
int i = node.begin; i < node.end; ++i)
585 if (contains_xy(boxes_[order_[i]],
x,
y))
590 stack.push_back(node.right);
591 stack.push_back(node.left);
597 double min_squared_distance(
const Eigen::RowVector3d &p,
const double margin)
const
599 double best = std::numeric_limits<double>::max();
602 std::vector<int> stack = {0};
603 while (!stack.empty())
605 const Node &node = nodes_[stack.back()];
607 if (box_squared_distance(node.box, p) > best + margin)
611 for (
int i = node.begin; i < node.end; ++i)
613 const int f = order_[i];
614 const Eigen::RowVector3d v0 = V_.row(
F_(f, 0)), v1 = V_.row(
F_(f, 1)), v2 = V_.row(
F_(f, 2));
623 const double dl = box_squared_distance(nodes_[node.left].box, p);
624 const double dr = box_squared_distance(nodes_[node.right].box, p);
627 stack.push_back(node.right);
628 stack.push_back(node.left);
632 stack.push_back(node.left);
633 stack.push_back(node.right);
643 std::array<double, 3> min, max;
650 int left = -1, right = -1;
653 static bool contains_xy(
const Box &box,
const double x,
const double y)
655 return box.min[0] <=
x &&
x <= box.max[0] && box.min[1] <=
y &&
y <= box.max[1];
658 static double box_squared_distance(
const Box &box,
const Eigen::RowVector3d &p)
661 for (
int d = 0; d < 3; ++d)
663 const double delta = std::max(0.0, std::max(box.min[d] - p[d], p[d] - box.max[d]));
664 result += delta * delta;
669 int build(
const int begin,
const int end,
const std::vector<std::array<double, 3>> ¢roids)
671 const int id = nodes_.size();
672 nodes_.emplace_back();
673 Box box = boxes_[order_[begin]];
674 for (
int i = begin + 1; i < end; ++i)
676 for (
int d = 0; d < 3; ++d)
678 box.min[d] = std::min(box.min[d], boxes_[order_[i]].min[d]);
679 box.max[d] = std::max(box.max[d], boxes_[order_[i]].max[d]);
682 nodes_[id].box = box;
683 nodes_[id].begin = begin;
684 nodes_[id].end = end;
685 if (end - begin <= 4)
689 for (
int d = 1; d < 3; ++d)
690 if (box.max[d] - box.min[d] > box.max[axis] - box.min[axis])
692 const int mid = begin + (end - begin) / 2;
693 std::nth_element(order_.begin() + begin, order_.begin() + mid, order_.begin() + end,
694 [&](
const int a,
const int b) { return centroids[a][axis] < centroids[b][axis]; });
695 const int left = build(begin, mid, centroids);
696 const int right = build(mid, end, centroids);
697 nodes_[id].left = left;
698 nodes_[id].right = right;
702 const Eigen::MatrixXd &V_;
703 const Eigen::MatrixXi &
F_;
704 std::vector<Box> boxes_;
705 std::vector<int> order_;
706 std::vector<Node> nodes_;
714 void compute_sign(
const Eigen::MatrixXd &
V,
const Eigen::MatrixXi &
F,
const TriangleBVH &bvh,
715 const Eigen::MatrixXd &P, Eigen::VectorXd &D)
717 assert(
P.cols() == 3);
718 assert(D.size() ==
P.rows());
720 for (
int k = 0; k <
P.rows(); ++k)
722 const Eigen::RowVector3d center =
P.row(k);
724 std::vector<std::pair<double, int>> inter;
725 bvh.for_each_in_xy_column(center[0], center[1], [&](
const int f) {
727 if (
int s = intersect_ray_z(
V,
F, f, center,
z))
729 inter.emplace_back(
z, s);
732 std::sort(inter.begin(), inter.end());
734 std::vector<double> reduced;
735 for (
int i = 0, s = 0; i < (int)inter.size(); ++i)
737 const int ds = inter[i].second;
739 if ((s == -1 && ds < 0) || (s == 0 && ds > 0))
741 reduced.push_back(inter[i].first);
746 for (
double z : reduced)
753 if (num_before % 2 == 1)
766 const Eigen::MatrixXd &P, Eigen::VectorXd &D)
768 assert(
V.cols() == 3);
769 assert(
F.cols() == 3);
770 assert(P.cols() == 3);
771 const TriangleBVH bvh(
V,
F);
776 if (
V.rows() > 0 && P.rows() > 0)
778 const Eigen::RowVector3d lo =
V.colwise().minCoeff().cwiseMin(P.colwise().minCoeff());
779 const Eigen::RowVector3d hi =
V.colwise().maxCoeff().cwiseMax(P.colwise().maxCoeff());
780 scale2 = (hi - lo).squaredNorm();
782 const double margin = 1e-10 * scale2;
785 for (
int i = 0; i < P.rows(); ++i)
786 D(i) = bvh.min_squared_distance(P.row(i), margin);
787 compute_sign(
V,
F, bvh, P, D);
794 assert(
F.cols() == 3);
795 assert(
V.cols() == 3);
796 std::array<Eigen::RowVector3d, 4> t;
797 t[3] = Eigen::RowVector3d::Zero(
V.cols());
798 double volume_total = 0;
799 for (
int f = 0; f <
F.rows(); ++f)
801 for (
int lv = 0; lv <
F.cols(); ++lv)
803 t[lv] =
V.row(
F(f, lv));
805 double vol = tetra_signed_volume(vec3(t[0].data()), vec3(t[1].data()), vec3(t[2].data()), vec3(t[3].data()));
808 return -volume_total;
817 for (
int f = 0; f <
F.rows(); ++f)
819 F.row(f) =
F.row(f).reverse().eval();
828 struct EdgeInterfacePrimitive
831 Eigen::Vector2d from;
835 struct FaceInterfacePrimitive
838 std::vector<Eigen::Vector3d> vertices;
843 return a.size() ==
b.size() && (a -
b).squaredNorm() <= 1
e-24;
846 bool overlapping_segments(
const EdgeInterfacePrimitive &first,
const EdgeInterfacePrimitive &second)
848 const Eigen::Vector2d direction = first.to - first.from;
849 const double length = direction.norm();
850 const double second_length = (second.to - second.from).
norm();
851 const double scale = std::max({1.0, length, second_length});
852 const double tolerance = 1
e-12 * scale;
853 if (length <= tolerance || second_length <= tolerance)
856 auto cross = [](
const Eigen::Vector2d &a,
const Eigen::Vector2d &
b) {
857 return a.x() *
b.y() - a.y() *
b.x();
859 if (std::abs(
cross(direction, second.from - first.from)) > tolerance * length
860 || std::abs(
cross(direction, second.to - first.from)) > tolerance * length)
863 const Eigen::Vector2d tangent = direction / length;
864 const double second_from = (second.from - first.from).dot(tangent);
865 const double second_to = (second.to - first.from).dot(tangent);
866 const double overlap_begin = std::max(0.0, std::min(second_from, second_to));
867 const double overlap_end = std::min(length, std::max(second_from, second_to));
868 return overlap_end - overlap_begin > tolerance;
871 Eigen::Vector3d face_normal(
const FaceInterfacePrimitive &face)
873 Eigen::Vector3d normal = Eigen::Vector3d::Zero();
874 for (
int i = 0; i < face.vertices.size(); ++i)
875 normal += face.vertices[i].cross(face.vertices[(i + 1) % face.vertices.size()]);
879 Eigen::Vector2d project_point(
const Eigen::Vector3d &
point,
const int dropped_axis)
881 if (dropped_axis == 0)
883 if (dropped_axis == 1)
888 bool point_in_convex_polygon(
889 const Eigen::Vector2d &
point,
890 const std::vector<Eigen::Vector2d> &polygon,
891 const double tolerance)
894 for (
int i = 0; i < polygon.size(); ++i)
896 const Eigen::Vector2d edge = polygon[(i + 1) % polygon.size()] - polygon[i];
897 const Eigen::Vector2d offset =
point - polygon[i];
898 const double cross = edge.x() * offset.y() - edge.y() * offset.x();
899 if (std::abs(cross) <= tolerance)
903 else if (sign * cross < 0)
909 bool overlapping_faces(
const FaceInterfacePrimitive &first,
const FaceInterfacePrimitive &second)
911 const Eigen::Vector3d first_normal = face_normal(first);
912 const Eigen::Vector3d second_normal = face_normal(second);
913 const double first_area_scale = first_normal.norm();
914 const double second_area_scale = second_normal.norm();
915 const double coordinate_scale = std::max({1.0,
916 first.vertices.front().
norm(),
917 second.vertices.front().norm()});
918 const double tolerance = 1
e-12 * coordinate_scale;
919 if (first_area_scale <= tolerance * tolerance || second_area_scale <= tolerance * tolerance)
922 const Eigen::Vector3d unit_normal = first_normal / first_area_scale;
923 if (unit_normal.cross(second_normal / second_area_scale).norm() > 1e-10)
926 if (std::abs(unit_normal.dot(
point - first.
vertices.front())) > tolerance)
929 Eigen::Index dropped_axis;
930 unit_normal.cwiseAbs().maxCoeff(&dropped_axis);
931 std::vector<Eigen::Vector2d> first_polygon, second_polygon;
932 first_polygon.reserve(first.vertices.size());
933 second_polygon.reserve(second.vertices.size());
935 first_polygon.push_back(project_point(
point, dropped_axis));
937 second_polygon.push_back(project_point(
point, dropped_axis));
939 auto contains = [tolerance](
const auto &container,
const auto &contained) {
940 return std::all_of(contained.begin(), contained.end(), [&](
const Eigen::Vector2d &
point) {
941 return point_in_convex_polygon(point, container, tolerance);
944 return contains(first_polygon, second_polygon) || contains(second_polygon, first_polygon);
948std::vector<std::pair<polyfem::mesh::Navigation::Index, polyfem::mesh::Navigation::Index>>
952 std::vector<EdgeInterfacePrimitive> first_edges, second_edges;
953 auto collect = [](
const Mesh2D &mesh,
auto &edges) {
954 for (
int f = 0; f < mesh.
n_faces(); ++f)
962 edges.push_back({index,
963 mesh.
point(index.vertex).head<2>(),
964 mesh.
point(opposite.vertex).head<2>()});
970 collect(first, first_edges);
971 collect(second, second_edges);
973 std::vector<std::pair<Index, Index>> result;
974 std::set<std::tuple<int, int, int, int, int, int>> visited_pairs;
975 for (
const auto &lhs : first_edges)
977 for (
const auto &rhs : second_edges)
979 if (!overlapping_segments(lhs, rhs))
981 Index second_index = rhs.index;
982 if ((lhs.to - lhs.from).dot(rhs.to - rhs.from) > 0)
984 const auto key = std::make_tuple(
985 lhs.index.face, lhs.index.edge, lhs.index.vertex,
986 second_index.face, second_index.edge, second_index.vertex);
987 if (visited_pairs.insert(key).second)
988 result.emplace_back(lhs.index, second_index);
994std::vector<std::pair<polyfem::mesh::Navigation3D::Index, polyfem::mesh::Navigation3D::Index>>
998 std::vector<FaceInterfacePrimitive> first_faces, second_faces;
999 auto collect = [](
const Mesh3D &mesh,
auto &
faces) {
1000 std::unordered_set<int> visited_faces;
1001 for (
int c = 0; c < mesh.
n_cells(); ++c)
1010 for (
int lv = 0; lv < vertices.size(); ++lv)
1012 faces.push_back({index, std::move(vertices)});
1016 collect(first, first_faces);
1017 collect(second, second_faces);
1019 std::vector<std::pair<Index, Index>> result;
1020 for (
const auto &lhs : first_faces)
1022 for (
const auto &rhs : second_faces)
1024 if (!overlapping_faces(lhs, rhs))
1026 Index second_index = rhs.index;
1027 for (
int lv = 0; lv < second.
n_face_vertices(second_index.face); ++lv)
1029 if (same_point(first.
point(lhs.index.vertex), second.
point(second_index.vertex)))
1033 result.emplace_back(lhs.index, second_index);
1044 bool approx_aligned(
const double *a_,
const double *b_,
const double *p_,
const double *q_,
double tol = 1e-6)
1046 const Vec3 a = vec3(a_), b = vec3(b_), p = vec3(p_), q = vec3(q_);
1047 double da = std::sqrt(point_segment_squared_distance(a, p, q));
1048 double db = std::sqrt(point_segment_squared_distance(b, p, q));
1049 double cos_theta = cos_angle(b - a, p - q);
1050 return (
da < tol && db < tol && std::abs(std::abs(cos_theta) - 1.0) < tol);
1058 const Eigen::MatrixXd &BV,
const Eigen::MatrixXi &BE, Eigen::MatrixXi &OE)
1060 assert(IV.cols() == 2 || IV.cols() == 3);
1061 assert(BV.cols() == 2 || BV.cols() == 3);
1062 typedef std::pair<int, int> Edge;
1063 std::vector<Edge> selected;
1064 for (
int e1 = 0; e1 < IE.rows(); ++e1)
1066 Eigen::RowVector3d a;
1068 a.head(IV.cols()) = IV.row(IE(e1, 0));
1069 Eigen::RowVector3d b;
1071 b.head(IV.cols()) = IV.row(IE(e1, 1));
1072 for (
int e2 = 0; e2 < BE.rows(); ++e2)
1074 Eigen::RowVector3d p;
1076 p.head(BV.cols()) = BV.row(BE(e2, 0));
1077 Eigen::RowVector3d q;
1079 q.head(BV.cols()) = BV.row(BE(e2, 1));
1080 if (approx_aligned(a.data(), b.data(), p.data(), q.data()))
1082 selected.emplace_back(IE(e1, 0), IE(e1, 1));
1088 OE.resize(selected.size(), 2);
1089 for (
int e = 0; e < OE.rows(); ++e)
1091 OE.row(e) << selected[e].first, selected[e].second;
1098 const Eigen::MatrixXi &tets,
1099 Eigen::MatrixXi &
faces)
1101 std::unordered_set<Eigen::Vector3i, HashMatrix> tri_to_tet(4 * tets.rows());
1102 for (
int i = 0; i < tets.rows(); i++)
1104 tri_to_tet.emplace(tets(i, 0), tets(i, 2), tets(i, 1));
1105 tri_to_tet.emplace(tets(i, 0), tets(i, 3), tets(i, 2));
1106 tri_to_tet.emplace(tets(i, 0), tets(i, 1), tets(i, 3));
1107 tri_to_tet.emplace(tets(i, 1), tets(i, 2), tets(i, 3));
1110 std::vector<Eigen::RowVector3i> faces_vector;
1111 for (
const auto &tri : tri_to_tet)
1114 bool is_surface_triangle =
1115 tri_to_tet.find(Eigen::Vector3i(tri[2], tri[1], tri[0])) == tri_to_tet.end()
1116 && tri_to_tet.find(Eigen::Vector3i(tri[1], tri[0], tri[2])) == tri_to_tet.end()
1117 && tri_to_tet.find(Eigen::Vector3i(tri[0], tri[2], tri[1])) == tri_to_tet.end();
1118 if (is_surface_triangle)
1120 faces_vector.emplace_back(tri[0], tri[1], tri[2]);
1124 faces.resize(faces_vector.size(), 3);
1125 for (
int i = 0; i <
faces.rows(); i++)
1127 faces.row(i) = faces_vector[i];
1132 const Eigen::MatrixXd &vertices,
1133 const Eigen::MatrixXi &tets,
1134 Eigen::MatrixXd &surface_vertices,
1135 Eigen::MatrixXi &tris)
1137 Eigen::MatrixXi full_tris;
1140 std::unordered_map<int, int> full_to_surface;
1141 std::vector<size_t> surface_to_full;
1142 for (
int i = 0; i < full_tris.rows(); i++)
1144 for (
int j = 0; j < full_tris.cols(); j++)
1146 if (full_to_surface.find(full_tris(i, j)) == full_to_surface.end())
1148 full_to_surface[full_tris(i, j)] = surface_to_full.size();
1149 surface_to_full.push_back(full_tris(i, j));
1154 surface_vertices.resize(surface_to_full.size(), 3);
1155 for (
int i = 0; i < surface_to_full.size(); i++)
1157 surface_vertices.row(i) = vertices.row(surface_to_full[i]);
1160 tris.resize(full_tris.rows(), full_tris.cols());
1161 for (
int i = 0; i < tris.rows(); i++)
1163 for (
int j = 0; j < tris.cols(); j++)
1165 tris(i, j) = full_to_surface[full_tris(i, j)];
1172 std::unordered_set<std::vector<int>,
HashVector> boundaries;
1174 auto insert = [&](std::vector<int> v) {
1175 std::sort(v.begin(), v.end());
1176 boundaries.insert(v);
1179 for (
int i = 0; i < cells.rows(); i++)
1181 const auto &cell = cells.row(i);
1182 if (cells.cols() == 3)
1184 insert({{cell(0), cell(1)}});
1185 insert({{cell(1), cell(2)}});
1186 insert({{cell(2), cell(0)}});
1188 else if (cells.cols() == 4 && dim == 2)
1190 insert({{cell(0), cell(1)}});
1191 insert({{cell(1), cell(2)}});
1192 insert({{cell(2), cell(3)}});
1193 insert({{cell(3), cell(0)}});
1195 else if (cells.cols() == 4 && dim == 3)
1197 insert({{cell(0), cell(2), cell(1)}});
1198 insert({{cell(0), cell(3), cell(2)}});
1199 insert({{cell(0), cell(1), cell(3)}});
1200 insert({{cell(1), cell(2), cell(3)}});
1202 else if (cells.cols() == 8)
1204 insert({{cell(0), cell(1), cell(2), cell(3)}});
1205 insert({{cell(1), cell(5), cell(6), cell(2)}});
1206 insert({{cell(5), cell(4), cell(7), cell(6)}});
1207 insert({{cell(0), cell(4), cell(7), cell(3)}});
1208 insert({{cell(0), cell(4), cell(5), cell(1)}});
1209 insert({{cell(2), cell(6), cell(7), cell(3)}});
1213 throw std::runtime_error(
"count_boundary_elements not implemented for polygons");
1217 return boundaries.size();
1222 typedef std::pair<int, int> Edge;
1224 if (M.n_edges() > 0)
1230 std::vector<std::pair<Edge, int>> e2c;
1231 for (
int f = 0; f < M.n_facets(); ++f)
1233 for (
int c = M.corners_begin(f); c < M.corners_end(f); ++c)
1235 int v = M.corner_vertex(c);
1236 int c2 = M.next_corner_around_facet(f, c);
1237 int v2 = M.corner_vertex(c2);
1238 e2c.emplace_back(std::make_pair(std::min(v, v2), std::max(v, v2)), c);
1241 std::sort(e2c.begin(), e2c.end());
1245 Edge prev_e(-1, -1);
1246 for (
const auto &kv : e2c)
1251 M.create_edge(e.first, e.second);
std::vector< Eigen::VectorXi > faces
2D mesh interface: the elements are the faces (polygons) and the boundary elements are the edges.
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
3D mesh interface: the elements are the cells (tetrahedra, hexahedra, prisms, pyramids or general pol...
virtual Navigation3D::Index get_index_from_element(int hi, int lf, int lv) const =0
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 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 face_vertex(const int f_id, const int lv_id) const =0
id of the face vertex
Minimal polygonal surface mesh used as the storage of CMesh2D.
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.
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.
PolygonMesh::Point mesh_vertex(const PolygonMesh &M, int v)
Retrieve a 3D vector with the position of a given vertex.
void offset_surface_vertices(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, const Eigen::RowVector3d &interior_point, double distance, Eigen::MatrixXd &offset)
Offset along area-weighted surface normals, using the direction away from an interior point where inc...
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 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_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.
double point_triangle_squared_distance(const double *p, const double *v0, const double *v1, const double *v2)
Squared distance between a point and a triangle.
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.
PolygonMesh::Point facet_barycenter(const PolygonMesh &M, int f)
void compute_element_tags(const PolygonMesh &M, std::vector< ElementType > &element_tags)
Compute the type of each facet in a surface mesh.
int mesh_create_vertex(PolygonMesh &M, const PolygonMesh::Point &p)
void orient_normals_2d(PolygonMesh &M)
Orient facets of a 2D mesh so that each connected component has positive volume.
void generate_edges(PolygonMesh &M)
Creates the edges of M (sorted by vertex pair) if it has none yet.
int count_faces(const int dim, const Eigen::MatrixXi &cells)
Count the number of boundary elements (triangles for tetmesh and edges for triangle mesh)
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd