210 assert(M.vertices.dimension() == 3);
213 const int nv = M.vertices.nb();
216 for (
int i = 0; i < nv; ++i)
227 if (M.cells.nb() == 0)
230 bool last_isolated =
true;
234 for (
int i = 0; i < (int)M.facets.nb(); ++i)
239 face.
vs.resize(M.facets.nb_vertices(i));
240 for (
int j = 0; j < (int)M.facets.nb_vertices(i); ++j)
242 face.
vs[j] = M.facets.vertex(i, j);
243 if ((
int)face.
vs[j] == nv - 1)
245 last_isolated =
false;
252 for (
int i = 0; i < 1; ++i)
257 int nf = M.facets.nb();
260 for (
int j = 0; j < nf; ++j)
265 for (
auto fid : cell.
fs)
269 sort(cell.
vs.begin(), cell.
vs.end());
270 cell.
vs.erase(unique(cell.
vs.begin(), cell.
vs.end()), cell.
vs.end());
272 for (
int j = 0; j < nf; ++j)
279 cell.
v_in_Kernel.push_back(M.vertices.point(nv - 1)[0]);
280 cell.
v_in_Kernel.push_back(M.vertices.point(nv - 1)[1]);
281 cell.
v_in_Kernel.push_back(M.vertices.point(nv - 1)[2]);
286 Eigen::RowVector3d p(0, 0, 0);
287 for (
int v : cell.
vs)
298 for (
int i = 0; i < 1; ++i)
322 auto opposite_cell_facet = [&M](
int c,
int cf) {
323 GEO::index_t c2 = M.cell_facets.adjacent_cell(cf);
324 if (c2 == GEO::NO_FACET)
328 for (
int lf = 0; lf < (int)M.cells.nb_facets(c2); ++lf)
330 if (c == (
int)M.cells.adjacent(c2, lf))
332 return (
int)M.cells.facet(c2, lf);
339 std::vector<int> cell_facet_to_facet(M.cell_facets.nb(), -1);
342 int facet_counter = 0;
345 for (
int c = 0; c < (int)M.cells.nb(); ++c)
349 cell.
hex = (M.cells.type(c) == GEO::MESH_HEX);
351 is_hex = is_hex && cell.
hex;
353 int nf = M.cells.nb_facets(c);
356 for (
int lf = 0; lf < nf; ++lf)
358 int cf = M.cells.facet(c, lf);
359 int cf2 = opposite_cell_facet(c, cf);
360 if (cf2 < 0 || cell_facet_to_facet[cf2] < 0)
364 assert(face.
vs.empty());
365 face.
vs.resize(M.cells.facet_nb_vertices(c, lf));
366 for (
int lv = 0; lv < (int)M.cells.facet_nb_vertices(c, lf); ++lv)
368 face.
vs[lv] = M.cells.facet_vertex(c, lf, lv);
371 cell.
fs[lf] = face.
id = facet_counter;
372 cell_facet_to_facet[cf] = facet_counter;
377 cell.
fs[lf] = cell_facet_to_facet[cf2];
382 for (
auto fid : cell.
fs)
386 sort(cell.
vs.begin(), cell.
vs.end());
387 cell.
vs.erase(unique(cell.
vs.begin(), cell.
vs.end()), cell.
vs.end());
390 Eigen::RowVector3d p(0, 0, 0);
391 for (
int v : cell.
vs)
466 assert(
F.cols() == 4 ||
F.cols() == 5 ||
F.cols() == 6 ||
F.cols() == 8);
472 M.vertices.create_vertices((
int)
V.rows());
473 for (
int i = 0; i < (int)M.vertices.nb(); ++i)
475 GEO::vec3 &p = M.vertices.point(i);
481 static const std::vector<int> permute_tet = {0, 1, 2, 3};
482 static const std::vector<int> permute_pyramid = {0, 1, 2, 3, 4};
483 static const std::vector<int> permute_prism = {0, 1, 2, 3, 4, 5};
485 static const std::vector<int> permute_hex = {1, 0, 2, 3, 5, 4, 6, 7};
487 auto add_cell = [&](
int c, GEO::MeshCellType t,
int nv,
const std::vector<int> &perm) {
488 GEO::index_t cid = M.cells.create_cells(1, t);
489 for (
int lv = 0; lv < nv; ++lv)
491 int vi =
F(c, perm[lv]);
492 assert(vi >= 0 && vi <
V.rows());
493 M.cells.set_vertex(cid, lv, GEO::index_t(vi));
497 for (
int c = 0; c <
F.rows(); ++c)
500 for (nV = 0; nV <
F.cols(); ++nV)
507 for (
int k = nV; k <
F.cols(); ++k)
509 assert(
F(c, k) == -1);
513 add_cell(c, GEO::MESH_TET, 4, permute_tet);
515 add_cell(c, GEO::MESH_PYRAMID, 5, permute_pyramid);
517 add_cell(c, GEO::MESH_PRISM, 6, permute_prism);
519 add_cell(c, GEO::MESH_HEX, 8, permute_hex);
540 const auto attach_p2 = [&](
const Navigation3D::Index &index,
const std::vector<int> &nodes_ids) {
543 if (n.nodes.size() > 0)
549 const int n_v1 = index.
vertex;
554 if ((n_v1 == nodes_ids[0] && n_v2 == nodes_ids[1]) || (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[1]))
556 else if ((n_v1 == nodes_ids[1] && n_v2 == nodes_ids[2]) || (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[2]))
558 else if ((n_v1 == nodes_ids[2] && n_v2 == nodes_ids[3]) || (n_v2 == nodes_ids[2] && n_v1 == nodes_ids[3]))
561 else if ((n_v1 == nodes_ids[0] && n_v2 == nodes_ids[3]) || (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[3]))
563 else if ((n_v1 == nodes_ids[0] && n_v2 == nodes_ids[2]) || (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[2]))
568 n.nodes.resize(1, 3);
569 n.nodes <<
V(nodes_ids[node_index], 0),
V(nodes_ids[node_index], 1),
V(nodes_ids[node_index], 2);
570 n.nodes_ids.push_back(nodes_ids[node_index]);
573 const auto attach_p3 = [&](
const Navigation3D::Index &index,
const std::vector<int> &nodes_ids) {
576 if (n.nodes.size() > 0)
582 const int n_v1 = index.
vertex;
587 if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[1])
592 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[1])
597 else if (n_v1 == nodes_ids[1] && n_v2 == nodes_ids[2])
602 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[2])
607 else if (n_v1 == nodes_ids[2] && n_v2 == nodes_ids[3])
612 else if (n_v2 == nodes_ids[2] && n_v1 == nodes_ids[3])
618 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[3])
623 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[3])
628 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[2])
633 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[2])
639 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[3])
650 n.nodes.resize(2, 3);
651 n.nodes.row(0) <<
V(nodes_ids[node_index1], 0),
V(nodes_ids[node_index1], 1),
V(nodes_ids[node_index1], 2);
652 n.nodes.row(1) <<
V(nodes_ids[node_index2], 0),
V(nodes_ids[node_index2], 1),
V(nodes_ids[node_index2], 2);
653 n.nodes_ids.push_back(nodes_ids[node_index1]);
654 n.nodes_ids.push_back(nodes_ids[node_index2]);
657 const auto attach_p3_face = [&](
const Navigation3D::Index &index,
const std::vector<int> &nodes_ids,
int id) {
659 if (n.nodes.size() <= 0)
664 n.nodes.resize(1, 3);
665 n.nodes <<
V(nodes_ids[
id], 0),
V(nodes_ids[
id], 1),
V(nodes_ids[
id], 2);
666 n.nodes_ids.push_back(nodes_ids[
id]);
670 const auto attach_p4 = [&](
const Navigation3D::Index &index,
const std::vector<int> &nodes_ids) {
673 if (n.nodes.size() > 0)
679 const int n_v1 = index.
vertex;
685 if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[1])
691 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[1])
698 else if (n_v1 == nodes_ids[1] && n_v2 == nodes_ids[2])
704 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[2])
711 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[2])
717 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[2])
724 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[3])
730 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[3])
737 else if (n_v2 == nodes_ids[2] && n_v1 == nodes_ids[3])
743 else if (n_v1 == nodes_ids[2] && n_v2 == nodes_ids[3])
750 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[3])
756 else if (n_v1 == nodes_ids[1] && n_v2 == nodes_ids[3])
767 n.nodes.resize(3, 3);
768 n.nodes.row(0) <<
V(nodes_ids[node_index1], 0),
V(nodes_ids[node_index1], 1),
V(nodes_ids[node_index1], 2);
769 n.nodes.row(1) <<
V(nodes_ids[node_index2], 0),
V(nodes_ids[node_index2], 1),
V(nodes_ids[node_index2], 2);
770 n.nodes.row(2) <<
V(nodes_ids[node_index3], 0),
V(nodes_ids[node_index3], 1),
V(nodes_ids[node_index3], 2);
771 n.nodes_ids.push_back(nodes_ids[node_index1]);
772 n.nodes_ids.push_back(nodes_ids[node_index2]);
773 n.nodes_ids.push_back(nodes_ids[node_index3]);
776 const auto attach_p4_face = [&](
const Navigation3D::Index &index,
const std::vector<int> &nodes_ids) {
778 if (n.nodes.size() <= 0)
784 std::array<int, 3> vid = {{n.v1, n.v2, n.v3}};
785 std::sort(vid.begin(), vid.end());
787 std::array<int, 3> c1 = {{nodes_ids[0], nodes_ids[1], nodes_ids[2]}};
788 std::array<int, 3> c2 = {{nodes_ids[0], nodes_ids[1], nodes_ids[3]}};
789 std::array<int, 3> c3 = {{nodes_ids[0], nodes_ids[2], nodes_ids[3]}};
790 std::array<int, 3> c4 = {{nodes_ids[1], nodes_ids[2], nodes_ids[3]}};
792 std::sort(c1.begin(), c1.end());
793 std::sort(c2.begin(), c2.end());
794 std::sort(c3.begin(), c3.end());
795 std::sort(c4.begin(), c4.end());
851 n.nodes.resize(3, 3);
852 assert(
id + index0 < nodes_ids.size());
853 assert(
id + index1 < nodes_ids.size());
854 assert(
id + index2 < nodes_ids.size());
855 n.nodes.row(0) <<
V(nodes_ids[
id + index0], 0),
V(nodes_ids[
id + index0], 1),
V(nodes_ids[
id + index0], 2);
856 n.nodes.row(1) <<
V(nodes_ids[
id + index1], 0),
V(nodes_ids[
id + index1], 1),
V(nodes_ids[
id + index1], 2);
857 n.nodes.row(2) <<
V(nodes_ids[
id + index2], 0),
V(nodes_ids[
id + index2], 1),
V(nodes_ids[
id + index2], 2);
858 n.nodes_ids.push_back(nodes_ids[
id + index0]);
859 n.nodes_ids.push_back(nodes_ids[
id + index1]);
860 n.nodes_ids.push_back(nodes_ids[
id + index2]);
864 const auto attach_p4_cell = [&](
const Navigation3D::Index &index,
const std::vector<int> &nodes_ids) {
866 assert(nodes_ids.size() == 35);
867 assert(n.nodes.size() == 0);
869 if (n.nodes.size() <= 0)
875 n.nodes.resize(1, 3);
877 n.nodes <<
V(nodes_ids[34], 0),
V(nodes_ids[34], 1),
V(nodes_ids[34], 2);
878 n.nodes_ids.push_back(nodes_ids[34]);
882 assert(nodes.size() ==
n_cells());
884 for (
int c = 0; c <
n_cells(); ++c)
888 const auto &nodes_ids = nodes[c];
890 if (nodes_ids.size() == 4)
896 else if (nodes_ids.size() == 10)
900 for (
int le = 0; le < 3; ++le)
902 attach_p2(index, nodes_ids);
907 attach_p2(index, nodes_ids);
910 attach_p2(index, nodes_ids);
913 attach_p2(index, nodes_ids);
916 else if (nodes_ids.size() == 20)
920 for (
int le = 0; le < 3; ++le)
922 attach_p3(index, nodes_ids);
928 attach_p3(index, nodes_ids);
931 attach_p3(index, nodes_ids);
934 attach_p3(index, nodes_ids);
939 std::array<int, 4> indices;
942 std::array<int, 3> f16 = {{nodes_ids[0], nodes_ids[1], nodes_ids[2]}};
943 std::array<int, 3> f17 = {{nodes_ids[3], nodes_ids[1], nodes_ids[0]}};
944 std::array<int, 3> f18 = {{nodes_ids[0], nodes_ids[2], nodes_ids[3]}};
945 std::array<int, 3> f19 = {{nodes_ids[1], nodes_ids[2], nodes_ids[3]}};
946 std::sort(f16.begin(), f16.end());
947 std::sort(f17.begin(), f17.end());
948 std::sort(f18.begin(), f18.end());
949 std::sort(f19.begin(), f19.end());
960 std::sort(f0.begin(), f0.end());
961 std::sort(f1.begin(), f1.end());
962 std::sort(f2.begin(), f2.end());
963 std::sort(f3.begin(), f3.end());
965 const std::array<std::array<int, 3>, 4>
faces = {{f0, f1, f2, f3}};
966 const std::array<std::array<int, 3>, 4> nodes = {{f16, f17, f18, f19}};
967 for (
int i = 0; i < 4; ++i)
969 const auto &f =
faces[i];
971 for (
int j = 0; j < 4; ++j)
985 attach_p3_face(index, nodes_ids, indices[0]);
986 attach_p3_face(
switch_face(index), nodes_ids, indices[1]);
992 else if (nodes_ids.size() == 35)
995 for (
int le = 0; le < 3; ++le)
997 attach_p4(index, nodes_ids);
1003 attach_p4(index, nodes_ids);
1006 attach_p4(index, nodes_ids);
1009 attach_p4(index, nodes_ids);
1015 attach_p4_face(index, nodes_ids);