16 const CellDescriptor tet_descriptor = {
26 const CellDescriptor hex_descriptor = {
38 const CellDescriptor prism_descriptor = {
49 const CellDescriptor pyramid_descriptor = {
60 bool triangles_equal(
int v1,
int v2,
int v3,
int w1,
int w2,
int w3)
62 return (v1 == w1 && v2 == w2 && v3 == w3)
63 || (v1 == w2 && v2 == w3 && v3 == w1)
64 || (v1 == w3 && v2 == w1 && v3 == w2);
68 int find_tet_facet(
const std::array<int, 4> &t,
int v1,
int v2,
int v3)
70 for (
int lf = 0; lf < 4; ++lf)
72 const int *fv = tet_descriptor.facet_vertex[lf];
73 if (triangles_equal(v1, v2, v3, t[fv[0]], t[fv[1]], t[fv[2]]))
80 bool facets_match(
const StandardCell &c1,
int f1,
const StandardCell &c2,
int f2)
82 const int nbv =
c1.n_facet_vertices(f1);
83 if (
c2.n_facet_vertices(f2) != nbv)
85 for (
int offset = 0; offset < nbv; ++offset)
88 for (
int v1 = 0; v1 < nbv; ++v1)
90 const int v2 = (nbv - v1 + offset) % nbv;
91 if (
c1.facet_vertex(f1, v1) !=
c2.facet_vertex(f2, v2))
104 int find_cell_facet(
const StandardCell &c1,
const StandardCell &c2,
int f2)
106 for (
int f1 = 0; f1 <
c1.n_facets(); ++f1)
108 if (facets_match(c1, f1, c2, f2))
114 int find_cell_vertex(
const StandardCell &c,
int v)
116 for (
int lv = 0; lv < c.descriptor().n_vertices; ++lv)
118 if (c.vertices[lv] == v)
125 bool triangular_facet_matches_quad_facet(
const StandardCell &c1,
int lf1,
const StandardCell &c2,
int lf2)
127 const int v1 =
c1.facet_vertex(lf1, 0);
128 const int v2 =
c1.facet_vertex(lf1, 1);
129 const int v3 =
c1.facet_vertex(lf1, 2);
130 const int w1 =
c2.facet_vertex(lf2, 0);
131 const int w2 =
c2.facet_vertex(lf2, 1);
132 const int w3 =
c2.facet_vertex(lf2, 2);
133 const int w4 =
c2.facet_vertex(lf2, 3);
134 return triangles_equal(v1, v2, v3, w4, w3, w2)
135 || triangles_equal(v1, v2, v3, w3, w2, w1)
136 || triangles_equal(v1, v2, v3, w2, w1, w4)
137 || triangles_equal(v1, v2, v3, w1, w4, w3);
140 void connect_tets(
const std::vector<StandardCell> &cells,
const int n_vertices, std::vector<std::array<int, 6>> &adjacent)
143 std::vector<std::array<int, 4>> tets(cells.size());
144 for (
int t = 0; t < cells.size(); ++t)
145 for (
int lv = 0; lv < 4; ++lv)
146 tets[t][lv] = cells[t].vertices[lv];
149 std::vector<int> next_tet_corner_around_vertex(4 * tets.size(), -1);
150 std::vector<int> v2c(n_vertices, -1);
151 for (
int t = 0; t < tets.size(); ++t)
153 for (
int lv = 0; lv < 4; ++lv)
155 const int v = tets[t][lv];
156 next_tet_corner_around_vertex[4 * t + lv] = v2c[v];
161 for (
int t1 = 0; t1 < tets.size(); ++t1)
163 for (
int lf1 = 0; lf1 < 4; ++lf1)
165 if (adjacent[t1][lf1] != -1)
167 const int *fv = tet_descriptor.facet_vertex[lf1];
168 const int v1 = tets[t1][fv[0]];
169 const int v2 = tets[t1][fv[1]];
170 const int v3 = tets[t1][fv[2]];
171 for (
int c2 = v2c[v1];
c2 != -1;
c2 = next_tet_corner_around_vertex[
c2])
173 const int t2 =
c2 / 4;
174 const int lf2 = find_tet_facet(tets[t2], v3, v2, v1);
177 adjacent[t1][lf1] = t2;
178 adjacent[t2][lf2] = t1;
192 return tet_descriptor;
194 return hex_descriptor;
196 return prism_descriptor;
198 return pyramid_descriptor;
201 return tet_descriptor;
204 std::vector<std::array<int, 6>>
connect_cells(
const std::vector<StandardCell> &cells,
const int n_vertices)
206 std::array<int, 6> unconnected;
207 unconnected.fill(-1);
208 std::vector<std::array<int, 6>> adjacent(cells.size(), unconnected);
210 const bool all_tets = std::all_of(cells.begin(), cells.end(), [](
const StandardCell &c) {
211 return c.type == StandardCellType::TET;
215 connect_tets(cells, n_vertices, adjacent);
220 std::vector<std::array<int, 8>> next_cell_around_vertex(cells.size());
221 std::vector<int> v2cell(n_vertices, -1);
222 for (
int c = 0; c < cells.size(); ++c)
224 for (
int lv = 0; lv < cells[c].descriptor().n_vertices; ++lv)
226 const int v = cells[c].vertices[lv];
227 next_cell_around_vertex[c][lv] = v2cell[v];
231 const auto next_cell = [&](
const int c,
const int v) {
232 return next_cell_around_vertex[c][find_cell_vertex(cells[c], v)];
237 for (
int c1 = 0; c1 < cells.size(); ++c1)
239 for (
int lf1 = 0; lf1 < cells[c1].n_facets(); ++lf1)
241 if (adjacent[c1][lf1] != -1)
244 for (
int c2 = v2cell[v1]; c2 != -1; c2 = next_cell(c2, v1))
246 const int lf2 = find_cell_facet(cells[c2], cells[c1], lf1);
249 adjacent[c1][lf1] = c2;
250 adjacent[c2][lf2] = c1;
258 std::vector<std::pair<int, int>> matches;
259 for (
int c1 = 0; c1 < cells.size(); ++c1)
263 for (
int lf1 = 0; lf1 < cells[c1].n_facets(); ++lf1)
265 if (cells[c1].n_facet_vertices(lf1) != 4 || adjacent[c1][lf1] != -1)
269 for (
int lv1 = 0; lv1 < 4; ++lv1)
271 const int v1 = cells[c1].facet_vertex(lf1, lv1);
272 for (
int c2 = v2cell[v1]; c2 != -1; c2 = next_cell(c2, v1))
276 for (
int lf2 = 0; lf2 < cells[c2].n_facets(); ++lf2)
278 if (cells[c2].n_facet_vertices(lf2) == 3
279 && triangular_facet_matches_quad_facet(cells[c2], lf2, cells[c1], lf1))
280 matches.emplace_back(c2, lf2);
284 std::sort(matches.begin(), matches.end());
285 matches.erase(std::unique(matches.begin(), matches.end()), matches.end());
286 if (matches.size() >= 2)
288 "Cell {} facet {}: a quadrilateral facet is glued to {} triangular facets; "
289 "non-conforming quad/triangle interfaces are not supported.",
290 c1, lf1, matches.size());
std::vector< std::array< int, 6 > > connect_cells(const std::vector< StandardCell > &cells, const int n_vertices)
Computes cell-to-cell adjacency across facets.
StandardCellType
Standard volume cell types with a fixed local facet layout.
const CellDescriptor & cell_descriptor(const StandardCellType type)
Local vertex and facet conventions of each cell type.
void log_and_throw_error(const std::string &msg)
Local facets of a standard cell: for each facet, its local vertices, ordered so that the facet normal...
A standard cell referencing global vertex indices.
int facet_vertex(const int lf, const int lv) const