PolyFEM
Loading...
Searching...
No Matches
CellConnectivity.cpp
Go to the documentation of this file.
2
4
5#include <algorithm>
6#include <cassert>
7#include <utility>
8
9namespace polyfem::mesh
10{
11 namespace
12 {
13 // Local facets of the standard cells. The tables are geogram's
14 // MeshCellDescriptors (https://github.com/BrunoLevy/geogram,
15 // src/lib/geogram/mesh/mesh.cpp, BSD-3-Clause license).
16 const CellDescriptor tet_descriptor = {
17 4, // n_vertices
18 4, // n_facets
19 {3, 3, 3, 3}, // n_facet_vertices
20 { // facet_vertex
21 {1, 3, 2},
22 {0, 2, 3},
23 {3, 1, 0},
24 {0, 1, 2}}};
25
26 const CellDescriptor hex_descriptor = {
27 8, // n_vertices
28 6, // n_facets
29 {4, 4, 4, 4, 4, 4}, // n_facet_vertices
30 { // facet_vertex
31 {0, 2, 6, 4},
32 {3, 1, 5, 7},
33 {1, 0, 4, 5},
34 {2, 3, 7, 6},
35 {1, 3, 2, 0},
36 {4, 6, 7, 5}}};
37
38 const CellDescriptor prism_descriptor = {
39 6, // n_vertices
40 5, // n_facets
41 {3, 3, 4, 4, 4}, // n_facet_vertices
42 { // facet_vertex
43 {0, 1, 2},
44 {3, 5, 4},
45 {0, 3, 4, 1},
46 {0, 2, 5, 3},
47 {1, 4, 5, 2}}};
48
49 const CellDescriptor pyramid_descriptor = {
50 5, // n_vertices
51 5, // n_facets
52 {4, 3, 3, 3, 3}, // n_facet_vertices
53 { // facet_vertex
54 {0, 1, 2, 3},
55 {0, 4, 1},
56 {0, 3, 4},
57 {2, 4, 3},
58 {2, 1, 4}}};
59
60 bool triangles_equal(int v1, int v2, int v3, int w1, int w2, int w3)
61 {
62 return (v1 == w1 && v2 == w2 && v3 == w3)
63 || (v1 == w2 && v2 == w3 && v3 == w1)
64 || (v1 == w3 && v2 == w1 && v3 == w2);
65 }
66
68 int find_tet_facet(const std::array<int, 4> &t, int v1, int v2, int v3)
69 {
70 for (int lf = 0; lf < 4; ++lf)
71 {
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]]))
74 return lf;
75 }
76 return -1;
77 }
78
80 bool facets_match(const StandardCell &c1, int f1, const StandardCell &c2, int f2)
81 {
82 const int nbv = c1.n_facet_vertices(f1);
83 if (c2.n_facet_vertices(f2) != nbv)
84 return false;
85 for (int offset = 0; offset < nbv; ++offset)
86 {
87 bool match = true;
88 for (int v1 = 0; v1 < nbv; ++v1)
89 {
90 const int v2 = (nbv - v1 + offset) % nbv;
91 if (c1.facet_vertex(f1, v1) != c2.facet_vertex(f2, v2))
92 {
93 match = false;
94 break;
95 }
96 }
97 if (match)
98 return true;
99 }
100 return false;
101 }
102
104 int find_cell_facet(const StandardCell &c1, const StandardCell &c2, int f2)
105 {
106 for (int f1 = 0; f1 < c1.n_facets(); ++f1)
107 {
108 if (facets_match(c1, f1, c2, f2))
109 return f1;
110 }
111 return -1;
112 }
113
114 int find_cell_vertex(const StandardCell &c, int v)
115 {
116 for (int lv = 0; lv < c.descriptor().n_vertices; ++lv)
117 {
118 if (c.vertices[lv] == v)
119 return lv;
120 }
121 return -1;
122 }
123
125 bool triangular_facet_matches_quad_facet(const StandardCell &c1, int lf1, const StandardCell &c2, int lf2)
126 {
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);
138 }
139
140 void connect_tets(const std::vector<StandardCell> &cells, const int n_vertices, std::vector<std::array<int, 6>> &adjacent)
141 {
142 // Compact copy of the tets (4 vertices each) for cache-friendly access.
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];
147
148 // Chain tet corners around vertices.
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)
152 {
153 for (int lv = 0; lv < 4; ++lv)
154 {
155 const int v = tets[t][lv];
156 next_tet_corner_around_vertex[4 * t + lv] = v2c[v];
157 v2c[v] = 4 * t + lv;
158 }
159 }
160
161 for (int t1 = 0; t1 < tets.size(); ++t1)
162 {
163 for (int lf1 = 0; lf1 < 4; ++lf1)
164 {
165 if (adjacent[t1][lf1] != -1)
166 continue;
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])
172 {
173 const int t2 = c2 / 4;
174 const int lf2 = find_tet_facet(tets[t2], v3, v2, v1);
175 if (lf2 != -1)
176 {
177 adjacent[t1][lf1] = t2;
178 adjacent[t2][lf2] = t1;
179 break;
180 }
181 }
182 }
183 }
184 }
185 } // namespace
186
188 {
189 switch (type)
190 {
192 return tet_descriptor;
194 return hex_descriptor;
196 return prism_descriptor;
198 return pyramid_descriptor;
199 }
200 assert(false);
201 return tet_descriptor;
202 }
203
204 std::vector<std::array<int, 6>> connect_cells(const std::vector<StandardCell> &cells, const int n_vertices)
205 {
206 std::array<int, 6> unconnected;
207 unconnected.fill(-1);
208 std::vector<std::array<int, 6>> adjacent(cells.size(), unconnected);
209
210 const bool all_tets = std::all_of(cells.begin(), cells.end(), [](const StandardCell &c) {
211 return c.type == StandardCellType::TET;
212 });
213 if (all_tets)
214 {
215 connect_tets(cells, n_vertices, adjacent);
216 return adjacent;
217 }
218
219 // Chain cells around vertices.
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)
223 {
224 for (int lv = 0; lv < cells[c].descriptor().n_vertices; ++lv)
225 {
226 const int v = cells[c].vertices[lv];
227 next_cell_around_vertex[c][lv] = v2cell[v];
228 v2cell[v] = c;
229 }
230 }
231 const auto next_cell = [&](const int c, const int v) {
232 return next_cell_around_vertex[c][find_cell_vertex(cells[c], v)];
233 };
234
235 // Glue each unconnected facet (c1, lf1) to the first matching facet of
236 // a cell incident to its first vertex.
237 for (int c1 = 0; c1 < cells.size(); ++c1)
238 {
239 for (int lf1 = 0; lf1 < cells[c1].n_facets(); ++lf1)
240 {
241 if (adjacent[c1][lf1] != -1)
242 continue;
243 const int v1 = cells[c1].facet_vertex(lf1, 0);
244 for (int c2 = v2cell[v1]; c2 != -1; c2 = next_cell(c2, v1))
245 {
246 const int lf2 = find_cell_facet(cells[c2], cells[c1], lf1);
247 if (lf2 != -1)
248 {
249 adjacent[c1][lf1] = c2;
250 adjacent[c2][lf2] = c1;
251 break;
252 }
253 }
254 }
255 }
256
257 // Reject non-conforming quad/triangle interfaces.
258 std::vector<std::pair<int, int>> matches;
259 for (int c1 = 0; c1 < cells.size(); ++c1)
260 {
261 if (cells[c1].type == StandardCellType::TET)
262 continue;
263 for (int lf1 = 0; lf1 < cells[c1].n_facets(); ++lf1)
264 {
265 if (cells[c1].n_facet_vertices(lf1) != 4 || adjacent[c1][lf1] != -1)
266 continue;
267
268 matches.clear();
269 for (int lv1 = 0; lv1 < 4; ++lv1)
270 {
271 const int v1 = cells[c1].facet_vertex(lf1, lv1);
272 for (int c2 = v2cell[v1]; c2 != -1; c2 = next_cell(c2, v1))
273 {
274 if (c2 == c1 || cells[c2].type == StandardCellType::HEX)
275 continue;
276 for (int lf2 = 0; lf2 < cells[c2].n_facets(); ++lf2)
277 {
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);
281 }
282 }
283 }
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());
291 }
292 }
293
294 return adjacent;
295 }
296} // namespace polyfem::mesh
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)
Definition Logger.cpp:73
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