PolyFEM
Loading...
Searching...
No Matches
MeshProcessing3D.cpp
Go to the documentation of this file.
3
4#include <Eigen/Dense>
5
6#include <algorithm>
7#include <functional>
8#include <map>
9#include <set>
10#include <queue>
11#include <iterator>
12#include <cassert>
13#include <limits>
14
15using namespace polyfem::mesh;
16using namespace polyfem;
17using namespace std;
18using namespace Eigen;
19
20namespace
21{
25 template <typename T, typename Visit>
26 void fill_rows(JaggedArray<T> &rows, const int n_rows, const Visit &visit)
27 {
28 std::vector<uint32_t> counts(n_rows, 0);
29 visit([&](const int r, const T &) { ++counts[r]; });
30 rows.assign_sizes(counts);
31 std::fill(counts.begin(), counts.end(), 0);
32 visit([&](const int r, const T &value) { rows[r][counts[r]++] = value; });
33 }
34
37 struct RefinedMesh
38 {
39 struct Cell
40 {
41 std::vector<uint32_t> vs;
42 std::vector<uint32_t> fs;
43 std::vector<uint8_t> fs_flag;
44 bool hex = false;
45 std::array<double, 3> kernel;
46 };
47
48 MeshType type;
49 std::vector<std::array<double, 3>> points;
50 std::vector<std::vector<uint32_t>> faces;
51 std::vector<Cell> cells;
52
53 Mesh3DStorage to_storage() const
54 {
56 M.type = type;
57 M.points.resize(3, points.size());
58 for (size_t v = 0; v < points.size(); ++v)
59 for (int j = 0; j < 3; j++)
60 M.points(j, v) = points[v][j];
61 for (const auto &f : faces)
62 M.face_vs.push_back(f);
63 for (const auto &c : cells)
64 {
65 M.cell_vs.push_back(c.vs);
66 M.cell_input_vs.push_back_filled(0, 0);
67 M.cell_fs.push_back(c.fs);
68 M.cell_fs_flag.push_back(c.fs_flag);
69 M.cell_hex.push_back(c.hex);
70 M.cell_kernel.push_back(c.kernel);
71 }
72 return M;
73 }
74 };
75
77 bool hex_scaled_jacobian(const Eigen::MatrixXd &points, const std::vector<std::vector<uint32_t>> &hexes, Mesh_Quality &mq)
78 {
79 using namespace MeshProcessing3D;
80 mq.ave_Jacobian = 0;
81 mq.min_Jacobian = 1;
82 mq.deviation_Jacobian = 0;
83 mq.V_Js.resize(hexes.size() * 8);
84 mq.V_Js.setZero();
85 mq.H_Js.resize(hexes.size());
86 mq.H_Js.setZero();
87
88 for (uint32_t i = 0; i < hexes.size(); i++)
89 {
90 double hex_minJ = 1;
91 for (uint32_t j = 0; j < 8; j++)
92 {
93 uint32_t v0, v1, v2, v3;
94 v0 = hex_tetra_table[j][0];
95 v1 = hex_tetra_table[j][1];
96 v2 = hex_tetra_table[j][2];
97 v3 = hex_tetra_table[j][3];
98
99 Vector3d c0 = points.col(hexes[i][v0]);
100 Vector3d c1 = points.col(hexes[i][v1]);
101 Vector3d c2 = points.col(hexes[i][v2]);
102 Vector3d c3 = points.col(hexes[i][v3]);
103
104 double jacobian_value = a_jacobian(c0, c1, c2, c3);
105
106 if (hex_minJ > jacobian_value)
107 hex_minJ = jacobian_value;
108
109 uint32_t id = 8 * i + j;
110 mq.V_Js[id] = jacobian_value;
111 }
112 mq.H_Js[i] = hex_minJ;
113 mq.ave_Jacobian += hex_minJ;
114 if (mq.min_Jacobian > hex_minJ)
115 mq.min_Jacobian = hex_minJ;
116 }
117 mq.ave_Jacobian /= hexes.size();
118 for (int i = 0; i < mq.H_Js.size(); i++)
119 mq.deviation_Jacobian += (mq.H_Js[i] - mq.ave_Jacobian) * (mq.H_Js[i] - mq.ave_Jacobian);
120 mq.deviation_Jacobian /= hexes.size();
121
122 return true;
123 }
124
128 void build_edges(Mesh3DStorage &hmi, const bool mark_boundary)
129 {
130 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>> temp;
131 temp.reserve(hmi.face_vs.n_entries());
132 std::vector<uint32_t> face_sizes(hmi.n_faces());
133 for (uint32_t i = 0; i < hmi.n_faces(); ++i)
134 {
135 const auto fvs = hmi.face_vs[i];
136 const int vn = fvs.size();
137 for (uint32_t j = 0; j < vn; ++j)
138 {
139 uint32_t v0 = fvs[j], v1 = fvs[(j + 1) % vn];
140 if (v0 > v1)
141 std::swap(v0, v1);
142 temp.push_back(std::make_tuple(v0, v1, i, j));
143 }
144 face_sizes[i] = vn;
145 }
146 hmi.face_es.assign_sizes(face_sizes);
147 std::sort(temp.begin(), temp.end());
148
149 hmi.edge_vs.clear();
150 hmi.edge_vs.reserve(temp.size() / 2);
151 hmi.edge_boundary.clear();
152 hmi.edge_boundary.reserve(temp.size() / 2);
153 uint32_t E_num = 0;
154 for (uint32_t i = 0; i < temp.size(); ++i)
155 {
156 if (i == 0 || (i != 0 && (std::get<0>(temp[i]) != std::get<0>(temp[i - 1]) || std::get<1>(temp[i]) != std::get<1>(temp[i - 1]))))
157 {
158 E_num++;
159 hmi.edge_vs.push_back({{std::get<0>(temp[i]), std::get<1>(temp[i])}});
160 hmi.edge_boundary.push_back(mark_boundary);
161 }
162 else if (mark_boundary)
163 hmi.edge_boundary[E_num - 1] = false;
164
165 hmi.face_es[std::get<2>(temp[i])][std::get<3>(temp[i])] = E_num - 1;
166 }
167 }
168} // namespace
169
171{
172 const int n_vertices = hmi.n_vertices();
173 if (hmi.type == MeshType::TRI || hmi.type == MeshType::QUA || hmi.type == MeshType::H_SUR)
174 {
175 build_edges(hmi, true);
176 // boundary
177 hmi.vertex_boundary.assign(n_vertices, false);
178 for (uint32_t i = 0; i < hmi.n_edges(); ++i)
179 if (hmi.edge_boundary[i])
180 hmi.vertex_boundary[hmi.edge_vs[i][0]] = hmi.vertex_boundary[hmi.edge_vs[i][1]] = true;
181 }
182 else if (hmi.type == MeshType::HEX)
183 {
184 // Faces from the hexes (cell_vs in navigation order).
185 const int n_cells = hmi.cell_vs.size();
186 std::vector<std::array<uint32_t, 4>> total_fs(n_cells * 6);
187 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>> tempF(n_cells * 6);
188 std::array<uint32_t, 4> vs;
189 for (uint32_t i = 0; i < n_cells; ++i)
190 {
191 const auto hvs = hmi.cell_vs[i];
192 for (short j = 0; j < 6; j++)
193 {
194 for (short k = 0; k < 4; k++)
195 vs[k] = hvs[hex_face_table[j][k]];
196 uint32_t id = 6 * i + j;
197 total_fs[id] = vs;
198 std::sort(vs.begin(), vs.end());
199 tempF[id] = std::make_tuple(vs[0], vs[1], vs[2], vs[3], id, i, j);
200 }
201 }
202 hmi.cell_fs.assign_sizes(std::vector<uint32_t>(n_cells, 6));
203 std::sort(tempF.begin(), tempF.end());
204 hmi.face_vs.clear();
205 hmi.face_vs.reserve(tempF.size() / 3, 4 * (tempF.size() / 3));
206 hmi.face_boundary.clear();
207 uint32_t F_num = 0;
208 for (uint32_t i = 0; i < tempF.size(); ++i)
209 {
210 if (i == 0 || (i != 0 && (std::get<0>(tempF[i]) != std::get<0>(tempF[i - 1]) || std::get<1>(tempF[i]) != std::get<1>(tempF[i - 1]) || std::get<2>(tempF[i]) != std::get<2>(tempF[i - 1]) || std::get<3>(tempF[i]) != std::get<3>(tempF[i - 1]))))
211 {
212 F_num++;
213 hmi.face_vs.push_back(total_fs[std::get<4>(tempF[i])]);
214 hmi.face_boundary.push_back(true);
215 }
216 else
217 hmi.face_boundary[F_num - 1] = false;
218
219 hmi.cell_fs[std::get<5>(tempF[i])][std::get<6>(tempF[i])] = F_num - 1;
220 }
221
222 build_edges(hmi, false);
223 // boundary
224 hmi.vertex_boundary.assign(n_vertices, false);
225 for (uint32_t i = 0; i < hmi.n_faces(); ++i)
226 if (hmi.face_boundary[i])
227 for (uint32_t j = 0; j < 4; ++j)
228 {
229 uint32_t eid = hmi.face_es[i][j];
230 hmi.edge_boundary[eid] = true;
231 hmi.vertex_boundary[hmi.edge_vs[eid][0]] = hmi.vertex_boundary[hmi.edge_vs[eid][1]] = true;
232 }
233 }
234 else if (hmi.type == MeshType::HYB || hmi.type == MeshType::TET)
235 {
236 // A face is on the boundary if it belongs to a single cell.
237 hmi.face_boundary.assign(hmi.n_faces(), false);
238 for (int c = 0; c < hmi.n_cells(); ++c)
239 for (const auto f : hmi.cell_fs[c])
240 hmi.face_boundary[f] = !hmi.face_boundary[f];
241
242 build_edges(hmi, false);
243 // boundary
244 hmi.vertex_boundary.assign(n_vertices, false);
245 for (uint32_t i = 0; i < hmi.n_faces(); ++i)
246 if (hmi.face_boundary[i])
247 for (uint32_t j = 0; j < hmi.face_vs.row_size(i); ++j)
248 {
249 uint32_t eid = hmi.face_es[i][j];
250 hmi.edge_boundary[eid] = true;
251 hmi.vertex_boundary[hmi.face_vs[i][j]] = true;
252 }
253 }
254
255 const int n_edges = hmi.n_edges(), n_faces = hmi.n_faces(), n_cells = hmi.n_cells();
256
257 // f_nhs
258 fill_rows(hmi.face_hs, n_faces, [&](const auto &emit) {
259 for (uint32_t i = 0; i < n_cells; i++)
260 for (const auto f : hmi.cell_fs[i])
261 emit(f, i);
262 });
263 // e_nfs, v_nfs
264 fill_rows(hmi.edge_fs, n_edges, [&](const auto &emit) {
265 for (uint32_t i = 0; i < n_faces; i++)
266 for (const auto e : hmi.face_es[i])
267 emit(e, i);
268 });
269 fill_rows(hmi.vertex_fs, n_vertices, [&](const auto &emit) {
270 for (uint32_t i = 0; i < n_faces; i++)
271 for (const auto v : hmi.face_vs[i])
272 emit(v, i);
273 });
274 // v_nes, v_nvs
275 fill_rows(hmi.vertex_es, n_vertices, [&](const auto &emit) {
276 for (uint32_t i = 0; i < n_edges; i++)
277 {
278 emit(hmi.edge_vs[i][0], i);
279 emit(hmi.edge_vs[i][1], i);
280 }
281 });
282 fill_rows(hmi.vertex_vs, n_vertices, [&](const auto &emit) {
283 for (uint32_t i = 0; i < n_edges; i++)
284 {
285 emit(hmi.edge_vs[i][0], hmi.edge_vs[i][1]);
286 emit(hmi.edge_vs[i][1], hmi.edge_vs[i][0]);
287 }
288 });
289 // e_nhs
290 hmi.edge_hs.clear();
291 hmi.edge_hs.reserve(n_edges, 6 * n_edges);
292 std::vector<uint32_t> nhs;
293 for (uint32_t i = 0; i < n_edges; i++)
294 {
295 nhs.clear();
296 for (const auto nfid : hmi.edge_fs[i])
297 nhs.insert(nhs.end(), hmi.face_hs[nfid].begin(), hmi.face_hs[nfid].end());
298 std::sort(nhs.begin(), nhs.end());
299 nhs.erase(std::unique(nhs.begin(), nhs.end()), nhs.end());
300 hmi.edge_hs.push_back(nhs);
301 }
302 fill_rows(hmi.cell_es, n_cells, [&](const auto &emit) {
303 for (uint32_t i = 0; i < n_edges; i++)
304 for (const auto c : hmi.edge_hs[i])
305 emit(c, i);
306 });
307
308 // Local index of each face in its cells (aligned with face_hs).
309 const auto build_face_slots = [&]() {
310 fill_rows(hmi.face_hs_slot, n_faces, [&](const auto &emit) {
311 for (uint32_t i = 0; i < n_cells; i++)
312 {
313 // Refined polyhedra can have many faces.
314 if (hmi.cell_fs.row_size(i) > std::numeric_limits<uint16_t>::max() + 1)
315 log_and_throw_error("Cell {} has {} faces, more than supported.", i, hmi.cell_fs.row_size(i));
316 for (uint32_t j = 0; j < hmi.cell_fs.row_size(i); j++)
317 emit(hmi.cell_fs[i][j], uint16_t(j));
318 }
319 });
320 };
321
322 // v_nhs; ordering fs for hex
323 if (hmi.type != MeshType::HYB && hmi.type != MeshType::TET)
324 {
325 build_face_slots();
326 return;
327 }
328
329 JaggedArray<uint32_t> cell_vs;
330 cell_vs.reserve(n_cells, hmi.cell_input_vs.n_entries());
331 vector<uint32_t> vs, new_vs;
332 for (uint32_t i = 0; i < n_cells; i++)
333 {
334 vs.clear();
335 for (const auto fid : hmi.cell_fs[i])
336 vs.insert(vs.end(), hmi.face_vs[fid].begin(), hmi.face_vs[fid].end());
337 sort(vs.begin(), vs.end());
338 vs.erase(unique(vs.begin(), vs.end()), vs.end());
339
340 bool degree3 = true;
341 for (auto vid : vs)
342 {
343 int nv = 0;
344 for (auto nvid : hmi.vertex_vs[vid])
345 if (find(vs.begin(), vs.end(), nvid) != vs.end())
346 nv++;
347 if (nv != 3)
348 {
349 degree3 = false;
350 break;
351 }
352 }
353
354 // Eight vertices of degree 3 do not make a hex on their own (faces
355 // 3, 3, 5, 5, 4, 4 also have them); the reordering below needs six quads.
356 const auto all_quads = [&]() {
357 if (hmi.cell_fs.row_size(i) != 6)
358 return false;
359 for (const auto fid : hmi.cell_fs[i])
360 if (hmi.face_vs.row_size(fid) != 4)
361 return false;
362 return true;
363 };
364 if (hmi.cell_hex[i] && (vs.size() != 8 || !degree3 || !all_quads()))
365 hmi.cell_hex[i] = false;
366
367 if (hmi.cell_hex[i])
368 {
369 const auto hfs = hmi.cell_fs[i];
370 const auto hfs_flag = hmi.cell_fs_flag[i];
371 int top_fid = hfs[0];
372 new_vs = hmi.face_vs[top_fid].to_vector();
373
374 std::set<uint32_t> s_model(vs.begin(), vs.end());
375 std::set<uint32_t> s_pattern(hmi.face_vs[top_fid].begin(), hmi.face_vs[top_fid].end());
376 vector<uint32_t> vs_left;
377 std::set_difference(s_model.begin(), s_model.end(), s_pattern.begin(), s_pattern.end(), std::back_inserter(vs_left));
378
379 for (auto vid : hmi.face_vs[top_fid])
380 for (auto nvid : hmi.vertex_vs[vid])
381 if (find(vs_left.begin(), vs_left.end(), nvid) != vs_left.end())
382 {
383 new_vs.push_back(nvid);
384 break;
385 }
386
387 function<int(vector<uint32_t> &, int &)> WHICH_F = [&](vector<uint32_t> &vs0, int &f_flag) -> int {
388 int which_f = -1;
389 sort(vs0.begin(), vs0.end());
390 for (uint32_t j = 0; j < hfs.size(); j++)
391 {
392 auto fid = hfs[j];
393 vector<uint32_t> vs1 = hmi.face_vs[fid].to_vector();
394 sort(vs1.begin(), vs1.end());
395 if (vs0.size() == vs1.size() && std::equal(vs0.begin(), vs0.end(), vs1.begin()))
396 {
397 f_flag = hfs_flag[j];
398 which_f = fid;
399 break;
400 }
401 }
402 return which_f;
403 };
404
405 vector<uint32_t> fs;
406 vector<uint8_t> fs_flag;
407 fs_flag.push_back(hfs_flag[0]);
408 fs.push_back(top_fid);
409 vector<uint32_t> vs_temp;
410
411 vs_temp.insert(vs_temp.end(), new_vs.begin() + 4, new_vs.end());
412 int f_flag = -1;
413 int bottom_fid = WHICH_F(vs_temp, f_flag);
414 fs_flag.push_back(f_flag);
415 fs.push_back(bottom_fid);
416
417 vs_temp = {new_vs[0], new_vs[1], new_vs[4], new_vs[5]};
418 f_flag = -1;
419 int front_fid = WHICH_F(vs_temp, f_flag);
420 fs_flag.push_back(f_flag);
421 fs.push_back(front_fid);
422
423 vs_temp = {new_vs[2], new_vs[3], new_vs[6], new_vs[7]};
424 f_flag = -1;
425 int back_fid = WHICH_F(vs_temp, f_flag);
426 fs_flag.push_back(f_flag);
427 fs.push_back(back_fid);
428
429 vs_temp = {new_vs[1], new_vs[2], new_vs[5], new_vs[6]};
430 f_flag = -1;
431 int left_fid = WHICH_F(vs_temp, f_flag);
432 fs_flag.push_back(f_flag);
433 fs.push_back(left_fid);
434
435 vs_temp = {new_vs[3], new_vs[0], new_vs[7], new_vs[4]};
436 f_flag = -1;
437 int right_fid = WHICH_F(vs_temp, f_flag);
438 fs_flag.push_back(f_flag);
439 fs.push_back(right_fid);
440
441 assert(fs.size() == hfs.size());
442 std::copy(fs.begin(), fs.end(), hfs.begin());
443 std::copy(fs_flag.begin(), fs_flag.end(), hfs_flag.begin());
444 cell_vs.push_back(new_vs);
445 }
446 else
447 cell_vs.push_back(vs);
448 }
449 hmi.cell_vs = std::move(cell_vs);
450 fill_rows(hmi.vertex_hs, n_vertices, [&](const auto &emit) {
451 for (uint32_t i = 0; i < n_cells; i++)
452 for (const auto v : hmi.cell_vs[i])
453 emit(v, i);
454 });
455 build_face_slots();
456}
457
459{
460 // connected components
461 vector<bool> H_tag(hmi.n_cells(), false);
462 vector<vector<uint32_t>> Groups;
463 while (true)
464 {
465 vector<uint32_t> group;
466 int shid = -1;
467 for (uint32_t i = 0; i < H_tag.size(); i++)
468 if (!H_tag[i])
469 {
470 shid = i;
471 break;
472 }
473 if (shid == -1)
474 break;
475 group.push_back(shid);
476 H_tag[shid] = true;
477 vector<uint32_t> group_ = group;
478 while (true)
479 {
480 vector<uint32_t> pool;
481 for (auto hid : group_)
482 {
483 vector<vector<uint32_t>> Fvs(6), fvs_sorted;
484 for (uint32_t i = 0; i < 6; i++)
485 for (uint32_t j = 0; j < 4; j++)
486 Fvs[i].push_back(hmi.cell_vs[hid][hex_face_table[i][j]]);
487 fvs_sorted = Fvs;
488 for (auto &vs : fvs_sorted)
489 sort(vs.begin(), vs.end());
490
491 for (auto fid : hmi.cell_fs[hid])
492 if (!hmi.face_boundary[fid])
493 {
494 int nhid = hmi.face_hs[fid][0];
495 if (nhid == hid)
496 nhid = hmi.face_hs[fid][1];
497
498 if (!H_tag[nhid])
499 {
500 pool.push_back(nhid);
501 H_tag[nhid] = true;
502
503 vector<uint32_t> fvs = hmi.face_vs[fid].to_vector();
504 sort(fvs.begin(), fvs.end());
505
506 int f_ind = -1;
507 for (uint32_t i = 0; i < 6; i++)
508 if (std::equal(fvs.begin(), fvs.end(), fvs_sorted[i].begin()))
509 {
510 f_ind = i;
511 break;
512 }
513 vector<uint32_t> hvs = hmi.cell_vs[nhid].to_vector();
514 vector<uint32_t> topvs = Fvs[f_ind];
515 std::reverse(topvs.begin(), topvs.end());
516 vector<uint32_t> bottomvs;
517 for (uint32_t i = 0; i < 4; i++)
518 {
519 for (auto nvid : hmi.vertex_vs[topvs[i]])
520 if (nvid != topvs[(i + 3) % 4] && nvid != topvs[(i + 1) % 4]
521 && std::find(hvs.begin(), hvs.end(), nvid) != hvs.end())
522 {
523 bottomvs.push_back(nvid);
524 break;
525 }
526 }
527 const auto new_vs = hmi.cell_vs[nhid];
528 assert(topvs.size() + bottomvs.size() == new_vs.size());
529 std::copy(bottomvs.begin(), bottomvs.end(), std::copy(topvs.begin(), topvs.end(), new_vs.begin()));
530 }
531 }
532 }
533 if (pool.size())
534 {
535 group_ = pool;
536 group.insert(group.end(), pool.begin(), pool.end());
537 }
538 else
539 break;
540 }
541 Groups.push_back(group);
542 }
543 // direction
544 for (const auto &group : Groups)
545 {
546 Mesh_Quality mq1, mq2;
547 std::vector<std::vector<uint32_t>> hexes;
548 hexes.reserve(group.size());
549 for (auto hid : group)
550 hexes.push_back(hmi.cell_vs[hid].to_vector());
551 hex_scaled_jacobian(hmi.points, hexes, mq1);
552 if (mq1.min_Jacobian > 0)
553 continue;
554 for (auto &h : hexes)
555 {
556 swap(h[1], h[3]);
557 swap(h[5], h[7]);
558 }
559 hex_scaled_jacobian(hmi.points, hexes, mq2);
560 if (mq2.ave_Jacobian > mq1.ave_Jacobian)
561 {
562 for (size_t k = 0; k < group.size(); ++k)
563 std::copy(hexes[k].begin(), hexes[k].end(), hmi.cell_vs[group[k]].begin());
564 }
565 }
566}
567
568double MeshProcessing3D::a_jacobian(Vector3d &v0, Vector3d &v1, Vector3d &v2, Vector3d &v3)
569{
570 Matrix3d Jacobian;
571
572 Jacobian.col(0) = v1 - v0;
573 Jacobian.col(1) = v2 - v0;
574 Jacobian.col(2) = v3 - v0;
575
576 double norm1 = Jacobian.col(0).norm();
577 double norm2 = Jacobian.col(1).norm();
578 double norm3 = Jacobian.col(2).norm();
579
580 double scaled_jacobian = Jacobian.determinant();
581 if (std::abs(norm1) < Jacobian_Precision || std::abs(norm2) < Jacobian_Precision || std::abs(norm3) < Jacobian_Precision)
582 {
583 return scaled_jacobian;
584 }
585 scaled_jacobian /= norm1 * norm2 * norm3;
586 return scaled_jacobian;
587}
588
590{
591 // Reorients the hexes (only) consistently: build a hex-only mesh, propagate
592 // the orientation across shared faces, and copy the vertex orders back.
593 if (std::none_of(hmi.cell_hex.begin(), hmi.cell_hex.end(), [](const uint8_t hex) { return hex; }))
594 return;
595
596 // reorder_hex_mesh_propogation only uses the connectivity rebuilt below
597 // (vertex neighbors and face/element adjacency) and the points.
598 Mesh3DStorage mesh;
599 mesh.type = MeshType::HEX;
600 mesh.points = hmi.points;
601 vector<int> Ele_map_reverse;
602 for (int c = 0; c < hmi.n_cells(); ++c)
603 {
604 if (!hmi.cell_hex[c])
605 continue;
606 mesh.cell_vs.push_back(hmi.cell_vs[c]);
607 Ele_map_reverse.push_back(c);
608 }
609
610 build_connectivity(mesh);
612
613 for (int c = 0; c < mesh.cell_vs.size(); ++c)
614 {
615 const auto vs = mesh.cell_vs[c];
616 std::copy(vs.begin(), vs.end(), hmi.cell_vs[Ele_map_reverse[c]].begin());
617 }
618}
619
620void MeshProcessing3D::refine_catmul_clark_polar(Mesh3DStorage &M, int iter, std::vector<int> &Parents)
621{
622
623 for (int i = 0; i < iter; i++)
624 {
625
626 std::vector<int> Refinement_Levels;
627 ele_subdivison_levels(M, Refinement_Levels);
628
629 RefinedMesh M_;
630 M_.type = MeshType::HYB;
631
632 vector<int> E2V(M.n_edges()), F2V(M.n_faces()), Ele2V(M.n_cells());
633
634 const auto add_vertex = [&](const std::array<double, 3> &p) {
635 M_.points.push_back(p);
636 return int(M_.points.size()) - 1;
637 };
638
639 for (int v = 0; v < M.n_vertices(); ++v)
640 add_vertex({{M.points(0, v), M.points(1, v), M.points(2, v)}});
641
642 for (int e = 0; e < M.n_edges(); ++e)
643 {
644 Vector3d center;
645 center.setZero();
646 for (auto vid : M.edge_vs[e])
647 center += M.points.col(vid);
648 center /= M.edge_vs[e].size();
649 E2V[e] = add_vertex({{center[0], center[1], center[2]}});
650 }
651 for (int f = 0; f < M.n_faces(); ++f)
652 {
653 Vector3d center;
654 center.setZero();
655 for (auto vid : M.face_vs[f])
656 center += M.points.col(vid);
657 center /= M.face_vs[f].size();
658 F2V[f] = add_vertex({{center[0], center[1], center[2]}});
659 }
660 for (int c = 0; c < M.n_cells(); ++c)
661 {
662 if (!M.cell_hex[c])
663 continue;
664 Ele2V[c] = add_vertex(M.cell_kernel[c]);
665 }
666 // new elements
667 std::vector<std::array<uint32_t, 4>> total_fs;
668 total_fs.reserve(M.n_cells() * 8 * 6);
669 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>> tempF;
670 tempF.reserve(M.n_cells() * 8 * 6);
671 std::array<uint32_t, 4> vs;
672
673 int elen = 0, fn = 0;
674 for (int c = 0; c < M.n_cells(); ++c)
675 {
676 const auto ele_vs_in = M.cell_vs[c];
677 const auto ele_fs = M.cell_fs[c];
678 if (M.cell_hex[c])
679 {
680 for (auto vid : ele_vs_in)
681 {
682 // top 4 vs
683 vector<int> top_vs(4);
684 top_vs[0] = vid;
685 int fid = -1;
686 for (auto nfid : M.vertex_fs[vid])
687 if (find(ele_fs.begin(), ele_fs.end(), nfid) != ele_fs.end())
688 {
689 fid = nfid;
690 break;
691 }
692 assert(fid != -1);
693 top_vs[2] = F2V[fid];
694
695 int v_ind = find(M.face_vs[fid].begin(), M.face_vs[fid].end(), vid) - M.face_vs[fid].begin();
696 int e_pre = M.face_es[fid][(v_ind - 1 + 4) % 4];
697 int e_aft = M.face_es[fid][v_ind];
698 top_vs[1] = E2V[e_pre];
699 top_vs[3] = E2V[e_aft];
700 // bottom 4 vs
701 vector<int> bottom_vs(4);
702
703 const auto nvs = M.vertex_vs[vid];
704 int e_per = -1;
705 for (auto nvid : nvs)
706 if (find(ele_vs_in.begin(), ele_vs_in.end(), nvid) != ele_vs_in.end())
707 {
708 if (nvid != M.edge_vs[e_pre][0] && nvid != M.edge_vs[e_pre][1] && nvid != M.edge_vs[e_aft][0] && nvid != M.edge_vs[e_aft][1])
709 {
710 vector<uint32_t> sharedes, es0 = M.vertex_es[vid].to_vector(), es1 = M.vertex_es[nvid].to_vector();
711 sort(es0.begin(), es0.end());
712 sort(es1.begin(), es1.end());
713 set_intersection(es0.begin(), es0.end(), es1.begin(), es1.end(), back_inserter(sharedes));
714 assert(sharedes.size());
715 e_per = sharedes[0];
716 break;
717 }
718 }
719
720 assert(e_per != -1);
721 bottom_vs[0] = E2V[e_per];
722 bottom_vs[2] = Ele2V[c];
723
724 int f_pre = -1;
725 vector<uint32_t> sharedfs, fs0 = M.edge_fs[e_pre].to_vector(), fs1 = M.edge_fs[e_per].to_vector();
726 sort(fs0.begin(), fs0.end());
727 sort(fs1.begin(), fs1.end());
728 set_intersection(fs0.begin(), fs0.end(), fs1.begin(), fs1.end(), back_inserter(sharedfs));
729 for (auto sfid : sharedfs)
730 if (find(ele_fs.begin(), ele_fs.end(), sfid) != ele_fs.end())
731 {
732 f_pre = sfid;
733 break;
734 }
735 assert(f_pre != -1);
736
737 int f_aft = -1;
738 sharedfs.clear();
739 fs0 = M.edge_fs[e_aft].to_vector();
740 fs1 = M.edge_fs[e_per].to_vector();
741 sort(fs0.begin(), fs0.end());
742 sort(fs1.begin(), fs1.end());
743 set_intersection(fs0.begin(), fs0.end(), fs1.begin(), fs1.end(), back_inserter(sharedfs));
744 for (auto sfid : sharedfs)
745 if (find(ele_fs.begin(), ele_fs.end(), sfid) != ele_fs.end())
746 {
747 f_aft = sfid;
748 break;
749 }
750 assert(f_aft != -1);
751
752 bottom_vs[1] = F2V[f_pre];
753 bottom_vs[3] = F2V[f_aft];
754
755 vector<int> ele_vs = top_vs;
756 ele_vs.insert(ele_vs.end(), bottom_vs.begin(), bottom_vs.end());
757 // fs
758 for (short j = 0; j < 6; j++)
759 {
760 for (short k = 0; k < 4; k++)
761 vs[k] = ele_vs[hex_face_table[j][k]];
762 total_fs.push_back(vs);
763 std::sort(vs.begin(), vs.end());
764 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], vs[3], fn++, elen, j));
765 }
766 // new ele
767 RefinedMesh::Cell ele_;
768 elen++;
769 ele_.fs.resize(6, -1);
770 ele_.fs_flag.resize(6, 1);
771 ele_.hex = true;
772
773 Vector3d center;
774 center.setZero();
775 for (auto evid : ele_vs)
776 for (int j = 0; j < 3; j++)
777 center[j] += M_.points[evid][j];
778 center /= ele_vs.size();
779 for (int j = 0; j < 3; j++)
780 ele_.kernel[j] = center[j];
781
782 M_.cells.push_back(std::move(ele_));
783 Parents.push_back(c);
784 }
785 }
786 else
787 {
788 const std::array<double, 3> kernel = M.cell_kernel[c];
789 const int level = Refinement_Levels[c];
790 // local_V2V
791 std::vector<std::vector<int>> local_V2Vs;
792 std::map<int, int> local_vi_map;
793 for (auto vid : ele_vs_in)
794 {
795 std::vector<int> v2v;
796 v2v.push_back(vid);
797 for (int r = 0; r < level; r++)
798 {
799 std::array<double, 3> p;
800 for (int j = 0; j < 3; j++)
801 p[j] = M_.points[vid][j] + (kernel[j] - M_.points[vid][j]) * (r + 1.0) / (double)(level + 1);
802 v2v.push_back(add_vertex(p));
803 }
804 local_vi_map[vid] = local_V2Vs.size();
805 local_V2Vs.push_back(v2v);
806 }
807 // local_E2V
808 vector<uint32_t> es;
809 for (auto fid : ele_fs)
810 es.insert(es.end(), M.face_es[fid].begin(), M.face_es[fid].end());
811 sort(es.begin(), es.end());
812 es.erase(unique(es.begin(), es.end()), es.end());
813
814 std::vector<std::vector<int>> local_E2Vs;
815 std::map<int, int> local_ei_map;
816 for (auto eid : es)
817 {
818 std::vector<int> e2v;
819 e2v.push_back(E2V[eid]);
820 for (int r = 0; r < level; r++)
821 {
822 Vector3d center;
823 center.setZero();
824 for (auto vid : M.edge_vs[eid])
825 for (int j = 0; j < 3; j++)
826 center[j] += M_.points[local_V2Vs[local_vi_map[vid]][r + 1]][j];
827 center /= M.edge_vs[eid].size();
828 e2v.push_back(add_vertex({{center[0], center[1], center[2]}}));
829 }
830 local_ei_map[eid] = local_E2Vs.size();
831 local_E2Vs.push_back(e2v);
832 }
833 // local_F2V
834 std::vector<std::vector<int>> local_F2Vs;
835 std::map<int, int> local_fi_map;
836 for (auto fid : ele_fs)
837 {
838 std::vector<int> f2v;
839 f2v.push_back(F2V[fid]);
840 for (int r = 0; r < level; r++)
841 {
842 Vector3d center;
843 center.setZero();
844 for (auto vid : M.face_vs[fid])
845 for (int j = 0; j < 3; j++)
846 center[j] += M_.points[local_V2Vs[local_vi_map[vid]][r + 1]][j];
847 center /= M.face_vs[fid].size();
848 f2v.push_back(add_vertex({{center[0], center[1], center[2]}}));
849 }
850 local_fi_map[fid] = local_F2Vs.size();
851 local_F2Vs.push_back(f2v);
852 }
853 // polyhedron fs
854 int local_fn = 0;
855 for (auto fid : ele_fs)
856 {
857 const auto fvs = M.face_vs[fid];
858 const auto fes = M.face_es[fid];
859 int fvn = fvs.size();
860 for (uint32_t j = 0; j < fvn; j++)
861 {
862 vs[0] = local_E2Vs[local_ei_map[fes[(j - 1 + fvn) % fvn]]][level];
863 vs[1] = local_V2Vs[local_vi_map[fvs[j]]][level];
864 vs[2] = local_E2Vs[local_ei_map[fes[j]]][level];
865 vs[3] = local_F2Vs[local_fi_map[fid]][level];
866
867 total_fs.push_back(vs);
868 std::sort(vs.begin(), vs.end());
869 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], vs[3], fn++, elen, local_fn++));
870 }
871 }
872 // polyhedron
873 RefinedMesh::Cell ele_;
874 elen++;
875 ele_.fs.resize(local_fn, -1);
876 ele_.fs_flag.resize(local_fn, 1);
877 ele_.hex = false;
878 ele_.kernel = kernel;
879 M_.cells.push_back(std::move(ele_));
880 Parents.push_back(c);
881 // hex
882 for (int r = 0; r < level; r++)
883 {
884 for (auto fid : ele_fs)
885 {
886 const auto fvs = M.face_vs[fid];
887 const auto fes = M.face_es[fid];
888 int fvn = fvs.size();
889 for (uint32_t j = 0; j < fvn; j++)
890 {
891 vector<int> ele_vs(8);
892 ele_vs[0] = local_E2Vs[local_ei_map[fes[(j - 1 + fvn) % fvn]]][r + 1];
893 ele_vs[1] = local_V2Vs[local_vi_map[fvs[j]]][r + 1];
894 ele_vs[2] = local_E2Vs[local_ei_map[fes[j]]][r + 1];
895 ele_vs[3] = local_F2Vs[local_fi_map[fid]][r + 1];
896
897 ele_vs[4] = local_E2Vs[local_ei_map[fes[(j - 1 + fvn) % fvn]]][r];
898 ele_vs[5] = local_V2Vs[local_vi_map[fvs[j]]][r];
899 ele_vs[6] = local_E2Vs[local_ei_map[fes[j]]][r];
900 ele_vs[7] = local_F2Vs[local_fi_map[fid]][r];
901
902 // fs
903 for (short j = 0; j < 6; j++)
904 {
905 for (short k = 0; k < 4; k++)
906 vs[k] = ele_vs[hex_face_table[j][k]];
907 total_fs.push_back(vs);
908 std::sort(vs.begin(), vs.end());
909 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], vs[3], fn++, elen, j));
910 }
911 // hex
912 RefinedMesh::Cell hex;
913 elen++;
914 hex.fs.resize(6, -1);
915 hex.fs_flag.resize(6, 1);
916 hex.hex = true;
917
918 Vector3d center;
919 center.setZero();
920 for (auto vid : ele_vs)
921 for (int j = 0; j < 3; j++)
922 center[j] += M_.points[vid][j];
923 center /= ele_vs.size();
924 for (int j = 0; j < 3; j++)
925 hex.kernel[j] = center[j];
926 M_.cells.push_back(std::move(hex));
927 Parents.push_back(c);
928 }
929 }
930 }
931 }
932 }
933 // Fs
934 std::sort(tempF.begin(), tempF.end());
935 M_.faces.reserve(tempF.size() / 3);
936 uint32_t F_num = 0;
937 for (uint32_t i = 0; i < tempF.size(); ++i)
938 {
939 if (i == 0 || (i != 0 && (std::get<0>(tempF[i]) != std::get<0>(tempF[i - 1]) || std::get<1>(tempF[i]) != std::get<1>(tempF[i - 1]) || std::get<2>(tempF[i]) != std::get<2>(tempF[i - 1]) || std::get<3>(tempF[i]) != std::get<3>(tempF[i - 1]))))
940 {
941 F_num++;
942 const auto &fvs = total_fs[std::get<4>(tempF[i])];
943 M_.faces.emplace_back(fvs.begin(), fvs.end());
944 }
945
946 M_.cells[std::get<5>(tempF[i])].fs[std::get<6>(tempF[i])] = F_num - 1;
947 }
948
949 Mesh3DStorage refined = M_.to_storage();
950 build_connectivity(refined);
951 orient_volume_mesh(refined);
952 build_connectivity(refined);
953
954 M = std::move(refined);
955 }
956}
957
959{
960 for (int i = 0; i < iter; i++)
961 {
962 RefinedMesh M_;
963 M_.type = MeshType::TET;
964
965 vector<int> E2V(M.n_edges());
966
967 M_.points.reserve(M.n_vertices() + M.n_edges());
968 for (int v = 0; v < M.n_vertices(); ++v)
969 M_.points.push_back({{M.points(0, v), M.points(1, v), M.points(2, v)}});
970
971 for (int e = 0; e < M.n_edges(); ++e)
972 {
973 Vector3d center;
974 center.setZero();
975 for (auto vid : M.edge_vs[e])
976 center += M.points.col(vid);
977 center /= M.edge_vs[e].size();
978 M_.points.push_back({{center[0], center[1], center[2]}});
979 E2V[e] = M_.points.size() - 1;
980 }
981
982 // The edge shared by two vertices (the edges around a vertex are sorted).
983 auto shared_edge = [&](int v0, int v1, int &e) -> bool {
984 const auto es0 = M.vertex_es[v0], es1 = M.vertex_es[v1];
985 auto it0 = es0.begin(), it1 = es1.begin();
986 while (it0 != es0.end() && it1 != es1.end())
987 {
988 if (*it0 < *it1)
989 ++it0;
990 else if (*it1 < *it0)
991 ++it1;
992 else
993 {
994 e = *it0;
995 return true;
996 }
997 }
998 return false;
999 };
1000
1001 const auto kernel_of = [&](const std::vector<uint32_t> &vs) {
1002 Vector3d center;
1003 center.setZero();
1004 for (const auto &evid : vs)
1005 for (int j = 0; j < 3; j++)
1006 center[j] += M_.points[evid][j];
1007 center /= vs.size();
1008 return std::array<double, 3>{{center[0], center[1], center[2]}};
1009 };
1010
1011 std::vector<bool> e_flag(M.n_edges(), false);
1012
1013 M_.cells.reserve(8 * M.n_cells());
1014 for (int c = 0; c < M.n_cells(); ++c)
1015 { // 1 --> 8
1016 const auto ele_vs = M.cell_vs[c];
1017 const auto ele_fs = M.cell_fs[c];
1018
1019 for (short i = 0; i < 4; i++)
1020 { // four corners
1021 RefinedMesh::Cell ele_;
1022 ele_.fs.resize(4, -1);
1023 ele_.fs_flag.resize(4, 1);
1024 ele_.hex = false;
1025
1026 ele_.vs.push_back(ele_vs[i]);
1027 for (short j = 0; j < 4; j++)
1028 {
1029 if (j == i)
1030 continue;
1031 int v0 = ele_vs[i], v1 = ele_vs[j];
1032 int e = -1;
1033 if (shared_edge(v0, v1, e))
1034 ele_.vs.push_back(E2V[e]);
1035 }
1036 ele_.kernel = kernel_of(ele_.vs);
1037 M_.cells.push_back(std::move(ele_));
1038 }
1039
1040 // 6 edges
1041 std::vector<int> edges(6);
1042 for (int i = 0; i < 6; i++)
1043 {
1044 int v0 = ele_vs[tet_edges[i][0]], v1 = ele_vs[tet_edges[i][1]];
1045 int e = -1;
1046 if (shared_edge(v0, v1, e))
1047 edges[i] = e;
1048 }
1049 // Split the inner octahedron along its shortest diagonal, i.e. between the midpoints
1050 // of two opposite edges (k, 5 - k). A fixed choice of diagonal makes the tets
1051 // degenerate under repeated refinement, since build_connectivity sorts ele.vs.
1052 int diag = 0;
1053 double min_length = std::numeric_limits<double>::max();
1054 for (int k = 0; k < 3; ++k)
1055 {
1056 const double length = (Map<const Vector3d>(M_.points[E2V[edges[k]]].data()) - Map<const Vector3d>(M_.points[E2V[edges[5 - k]]].data())).squaredNorm();
1057 if (length < min_length)
1058 {
1059 min_length = length;
1060 diag = k;
1061 }
1062 }
1063 int lv0 = E2V[edges[diag]], lv1 = E2V[edges[5 - diag]];
1064 e_flag[edges[diag]] = true;
1065 e_flag[edges[5 - diag]] = true;
1066 for (short i = 0; i < 4; i++)
1067 { // four faces
1068 RefinedMesh::Cell ele_;
1069 ele_.fs.resize(4, -1);
1070 ele_.fs_flag.resize(4, 1);
1071 ele_.hex = false;
1072
1073 ele_.vs.push_back(lv0);
1074 ele_.vs.push_back(lv1);
1075 for (short j = 0; j < 3; j++)
1076 {
1077 int c_e = M.face_es[ele_fs[i]][j];
1078 if (e_flag[c_e])
1079 continue;
1080 ele_.vs.push_back(E2V[c_e]);
1081 }
1082 ele_.kernel = kernel_of(ele_.vs);
1083 M_.cells.push_back(std::move(ele_));
1084 }
1085 e_flag[edges[diag]] = e_flag[edges[5 - diag]] = false;
1086 }
1087
1088 const auto point = [&](const uint32_t v) { return Vector3d(M_.points[v][0], M_.points[v][1], M_.points[v][2]); };
1089
1090 // orient tets
1091 const auto t = M.cell_vs[0];
1092 Vector3d c0 = M.points.col(t[0]);
1093 Vector3d c1 = M.points.col(t[1]);
1094 Vector3d c2 = M.points.col(t[2]);
1095 Vector3d c3 = M.points.col(t[3]);
1096 bool signed_volume = a_jacobian(c0, c1, c2, c3) > 0 ? true : false;
1097
1098 for (auto &ele : M_.cells)
1099 {
1100 c0 = point(ele.vs[0]);
1101 c1 = point(ele.vs[1]);
1102 c2 = point(ele.vs[2]);
1103 c3 = point(ele.vs[3]);
1104 bool sign = a_jacobian(c0, c1, c2, c3) > 0 ? true : false;
1105 if (sign != signed_volume)
1106 std::swap(ele.vs[1], ele.vs[3]);
1107 }
1108
1109 // Fs
1110 std::vector<std::array<uint32_t, 3>> total_fs;
1111 total_fs.reserve(M_.cells.size() * 4);
1112 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>> tempF;
1113 tempF.reserve(M_.cells.size() * 4);
1114 std::array<uint32_t, 3> vs;
1115 for (uint32_t c = 0; c < M_.cells.size(); ++c)
1116 {
1117 const auto &ele = M_.cells[c];
1118 for (int i = 0; i < 4; i++)
1119 {
1120 vs[0] = ele.vs[tet_faces[i][0]];
1121 vs[1] = ele.vs[tet_faces[i][1]];
1122 vs[2] = ele.vs[tet_faces[i][2]];
1123 total_fs.push_back(vs);
1124 std::sort(vs.begin(), vs.end());
1125 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], c * 4 + i, c, i));
1126 }
1127 }
1128 std::sort(tempF.begin(), tempF.end());
1129 M_.faces.reserve(tempF.size() / 3);
1130 uint32_t F_num = 0;
1131 for (uint32_t i = 0; i < tempF.size(); ++i)
1132 {
1133 if (i == 0 || (i != 0 && (std::get<0>(tempF[i]) != std::get<0>(tempF[i - 1]) || std::get<1>(tempF[i]) != std::get<1>(tempF[i - 1]) || std::get<2>(tempF[i]) != std::get<2>(tempF[i - 1]))))
1134 {
1135 F_num++;
1136 const auto &fvs = total_fs[std::get<3>(tempF[i])];
1137 M_.faces.emplace_back(fvs.begin(), fvs.end());
1138 }
1139
1140 M_.cells[std::get<4>(tempF[i])].fs[std::get<5>(tempF[i])] = F_num - 1;
1141 }
1142
1143 Mesh3DStorage refined = M_.to_storage();
1144 build_connectivity(refined);
1145 orient_volume_mesh(refined);
1146 build_connectivity(refined);
1147
1148 M = std::move(refined);
1149 }
1150}
1151
1153{
1154
1155 vector<bool> flag(hmi.n_faces(), true);
1156 flag[0] = false;
1157
1158 std::queue<uint32_t> pf_temp;
1159 pf_temp.push(0);
1160 while (!pf_temp.empty())
1161 {
1162 uint32_t fid = pf_temp.front();
1163 pf_temp.pop();
1164 const auto fvs = hmi.face_vs[fid];
1165 for (auto eid : hmi.face_es[fid])
1166 for (auto nfid : hmi.edge_fs[eid])
1167 {
1168 if (!flag[nfid])
1169 continue;
1170 uint32_t v0 = hmi.edge_vs[eid][0], v1 = hmi.edge_vs[eid][1];
1171 int32_t v0_pos = std::find(fvs.begin(), fvs.end(), v0) - fvs.begin();
1172 int32_t v1_pos = std::find(fvs.begin(), fvs.end(), v1) - fvs.begin();
1173
1174 if ((v0_pos + 1) % fvs.size() != v1_pos)
1175 swap(v0, v1);
1176
1177 const auto nfvs = hmi.face_vs[nfid];
1178 int32_t v0_pos_ = std::find(nfvs.begin(), nfvs.end(), v0) - nfvs.begin();
1179 int32_t v1_pos_ = std::find(nfvs.begin(), nfvs.end(), v1) - nfvs.begin();
1180
1181 if ((v0_pos_ + 1) % nfvs.size() == v1_pos_)
1182 std::reverse(nfvs.begin(), nfvs.end());
1183
1184 pf_temp.push(nfid);
1185 flag[nfid] = false;
1186 }
1187 }
1188 double res = 0;
1189 Vector3d ori;
1190 ori.setZero();
1191 for (int f = 0; f < hmi.n_faces(); ++f)
1192 {
1193 const auto fvs = hmi.face_vs[f];
1194 Vector3d center;
1195 center.setZero();
1196 for (auto vid : fvs)
1197 center += hmi.points.col(vid);
1198 center /= fvs.size();
1199
1200 for (uint32_t j = 0; j < fvs.size(); j++)
1201 {
1202 Vector3d x = hmi.points.col(fvs[j]) - ori, y = hmi.points.col(fvs[(j + 1) % fvs.size()]) - ori, z = center - ori;
1203 res += -((x[0] * y[1] * z[2] + x[1] * y[2] * z[0] + x[2] * y[0] * z[1]) - (x[2] * y[1] * z[0] + x[1] * y[0] * z[2] + x[0] * y[2] * z[1]));
1204 }
1205 }
1206 if (res > 0)
1207 {
1208 for (uint32_t i = 0; i < hmi.n_faces(); i++)
1209 {
1210 const auto fvs = hmi.face_vs[i];
1211 std::reverse(fvs.begin(), fvs.end());
1212 }
1213 }
1214}
1215
1217{
1218 // surface orienting
1219 Mesh3DStorage M_sur;
1220 M_sur.type = MeshType::H_SUR;
1221 int bvn = 0;
1222 for (int v = 0; v < hmi.n_vertices(); ++v)
1223 if (hmi.vertex_boundary[v])
1224 bvn++;
1225 M_sur.points.resize(3, bvn);
1226 bvn = 0;
1227 vector<int> V_map(hmi.n_vertices(), -1), V_map_reverse;
1228 for (int v = 0; v < hmi.n_vertices(); ++v)
1229 if (hmi.vertex_boundary[v])
1230 {
1231 M_sur.points.col(bvn++) = hmi.points.col(v);
1232 V_map[v] = V_map_reverse.size();
1233 V_map_reverse.push_back(v);
1234 }
1235 std::vector<uint32_t> boundary_fvs;
1236 for (int f = 0; f < hmi.n_faces(); ++f)
1237 if (hmi.face_boundary[f])
1238 {
1239 boundary_fvs.clear();
1240 for (const auto v : hmi.face_vs[f])
1241 boundary_fvs.push_back(V_map[v]);
1242 M_sur.face_vs.push_back(boundary_fvs);
1243 }
1244 build_connectivity(M_sur);
1245 orient_surface_mesh(M_sur);
1246
1247 int fn_ = 0;
1248 for (int f = 0; f < hmi.n_faces(); ++f)
1249 if (hmi.face_boundary[f])
1250 {
1251 const auto fvs = hmi.face_vs[f];
1252 for (int j = 0; j < fvs.size(); j++)
1253 fvs[j] = V_map_reverse[M_sur.face_vs[fn_][j]];
1254 fn_++;
1255 }
1256 // volume orienting
1257 vector<bool> F_tag(hmi.n_faces(), true);
1258 std::vector<short> F_visit(hmi.n_faces(), 0); // 0 un-visited, 1 visited once, 2 visited twice
1259 for (uint32_t j = 0; j < hmi.n_faces(); j++)
1260 if (hmi.face_boundary[j])
1261 {
1262 F_visit[j]++;
1263 }
1264 std::vector<bool> F_state(hmi.n_faces(), false); // false is the reverse direction, true is the same direction
1265 std::vector<bool> P_visit(hmi.n_cells(), false);
1266 while (true)
1267 {
1268 std::vector<uint32_t> candidates;
1269 for (uint32_t j = 0; j < F_visit.size(); j++)
1270 if (F_visit[j] == 1)
1271 candidates.push_back(j);
1272 if (!candidates.size())
1273 break;
1274 for (auto ca : candidates)
1275 {
1276 if (F_visit[ca] == 2)
1277 continue;
1278 uint32_t pid = hmi.face_hs[ca][0];
1279 if (P_visit[pid])
1280 if (hmi.face_hs.row_size(ca) == 2)
1281 pid = hmi.face_hs[ca][1];
1282 if (P_visit[pid])
1283 {
1284 logger().error("bug");
1285 }
1286 const auto fs = hmi.cell_fs[pid];
1287 for (auto fid : fs)
1288 F_tag[fid] = false;
1289
1290 uint32_t start_f = ca;
1291 F_tag[start_f] = true;
1292 F_visit[ca]++;
1293 if (F_state[ca])
1294 F_state[ca] = false;
1295 else
1296 F_state[ca] = true;
1297
1298 std::queue<uint32_t> pf_temp;
1299 pf_temp.push(start_f);
1300 while (!pf_temp.empty())
1301 {
1302 uint32_t fid = pf_temp.front();
1303 pf_temp.pop();
1304 const auto fvs = hmi.face_vs[fid];
1305 for (auto eid : hmi.face_es[fid])
1306 for (auto nfid : hmi.edge_fs[eid])
1307 {
1308
1309 if (F_tag[nfid])
1310 continue;
1311 uint32_t v0 = hmi.edge_vs[eid][0], v1 = hmi.edge_vs[eid][1];
1312 int32_t v0_pos = std::find(fvs.begin(), fvs.end(), v0) - fvs.begin();
1313 int32_t v1_pos = std::find(fvs.begin(), fvs.end(), v1) - fvs.begin();
1314
1315 if ((v0_pos + 1) % fvs.size() != v1_pos)
1316 std::swap(v0, v1);
1317
1318 const auto nfvs = hmi.face_vs[nfid];
1319 int32_t v0_pos_ = std::find(nfvs.begin(), nfvs.end(), v0) - nfvs.begin();
1320 int32_t v1_pos_ = std::find(nfvs.begin(), nfvs.end(), v1) - nfvs.begin();
1321
1322 if (F_state[fid])
1323 {
1324 if ((v0_pos_ + 1) % nfvs.size() == v1_pos_)
1325 F_state[nfid] = false;
1326 else
1327 F_state[nfid] = true;
1328 }
1329 else if (!F_state[fid])
1330 {
1331 if ((v0_pos_ + 1) % nfvs.size() == v1_pos_)
1332 F_state[nfid] = true;
1333 else
1334 F_state[nfid] = false;
1335 }
1336
1337 F_visit[nfid]++;
1338
1339 pf_temp.push(nfid);
1340 F_tag[nfid] = true;
1341 }
1342 }
1343 P_visit[pid] = true;
1344 const auto fs_flag = hmi.cell_fs_flag[pid];
1345 for (uint32_t j = 0; j < fs.size(); j++)
1346 fs_flag[j] = F_state[fs[j]];
1347 }
1348 }
1349}
1350
1351void MeshProcessing3D::ele_subdivison_levels(const Mesh3DStorage &hmi, std::vector<int> &Ls)
1352{
1353
1354 Ls.clear();
1355 Ls.resize(hmi.n_cells(), 1);
1356 std::vector<double> volumes(hmi.n_cells(), 0);
1357
1358 auto compute_volume = [&](const int id, double &vol) {
1359 Vector3d ori;
1360 ori.setZero();
1361 for (auto f : hmi.cell_fs[id])
1362 {
1363 const auto fvs = hmi.face_vs[f];
1364 Vector3d center;
1365 center.setZero();
1366 for (auto vid : fvs)
1367 center += hmi.points.col(vid);
1368 center /= fvs.size();
1369
1370 for (uint32_t j = 0; j < fvs.size(); j++)
1371 {
1372 Vector3d x = hmi.points.col(fvs[j]) - ori, y = hmi.points.col(fvs[(j + 1) % fvs.size()]) - ori, z = center - ori;
1373 vol += -((x[0] * y[1] * z[2] + x[1] * y[2] * z[0] + x[2] * y[0] * z[1]) - (x[2] * y[1] * z[0] + x[1] * y[0] * z[2] + x[0] * y[2] * z[1]));
1374 }
1375 }
1376 vol = std::abs(vol);
1377 };
1378
1379 for (int c = 0; c < hmi.n_cells(); ++c)
1380 compute_volume(c, volumes[c]);
1381
1382 double ave_volume = 0;
1383 for (const auto &v : volumes)
1384 ave_volume += v;
1385 ave_volume /= volumes.size();
1386 for (int i = 0; i < Ls.size(); i++)
1387 if (!hmi.cell_hex[i])
1388 {
1389 Ls[i] = volumes[i] / ave_volume;
1390 if (Ls[i] < 1)
1391 Ls[i] = 1;
1392 }
1393}
1394
1396{
1397 if (other.type != type)
1399
1400 const int n_v = n_vertices();
1401 const int n_f = n_faces();
1402
1403 assert(points.rows() == other.points.rows());
1404 points.conservativeResize(points.rows(), n_v + other.points.cols());
1405 points.rightCols(other.points.cols()) = other.points;
1406
1407 face_vs.append(other.face_vs, n_v);
1408 face_boundary.insert(face_boundary.end(), other.face_boundary.begin(), other.face_boundary.end());
1409
1410 cell_vs.append(other.cell_vs, n_v);
1412 cell_fs.append(other.cell_fs, n_f);
1414 cell_hex.insert(cell_hex.end(), other.cell_hex.begin(), other.cell_hex.end());
1415 cell_kernel.insert(cell_kernel.end(), other.cell_kernel.begin(), other.cell_kernel.end());
1416}
1417
1419{
1420 return points.size() * sizeof(double)
1422 + vertex_boundary.capacity()
1423 + edge_vs.capacity() * sizeof(std::array<uint32_t, 2>) + edge_fs.memory() + edge_hs.memory() + edge_boundary.capacity()
1426 + cell_hex.capacity() + cell_kernel.capacity() * sizeof(std::array<double, 3>);
1427}
#define Jacobian_Precision
Eigen::RowVectorXd point
std::vector< Eigen::VectorXi > faces
int y
int z
int x
Rows of different lengths stored contiguously (compressed rows): row i is data[offsets[i],...
int size() const
Number of rows (a moved-from array has none).
int row_size(const int i) const
void push_back(It begin, It end)
Appends a row.
void append(const JaggedArray &other, const T shift)
Appends the rows of other, adding shift to every entry.
void reserve(const size_t n_rows, const size_t n_entries)
void assign_sizes(const std::vector< uint32_t > &sizes, const T &value=T())
Replaces the content with rows of the given sizes, filled with value.
size_t n_entries() const
Total number of entries.
Topology of a conforming 3D mesh (used by CMesh3D), stored as flat arrays indexed by entity id.
JaggedArray< uint8_t > cell_fs_flag
JaggedArray< uint32_t > cell_input_vs
input order (rows may be empty)
std::vector< uint8_t > vertex_boundary
Eigen::MatrixXd points
Vertex positions, 3 x #vertices.
size_t memory() const
Bytes allocated by the storage.
std::vector< uint8_t > cell_hex
JaggedArray< uint32_t > face_vs
JaggedArray< uint32_t > vertex_fs
JaggedArray< uint32_t > face_es
JaggedArray< uint32_t > vertex_es
JaggedArray< uint32_t > face_hs
JaggedArray< uint32_t > edge_fs
std::vector< uint8_t > face_boundary
JaggedArray< uint32_t > cell_vs
navigation order (see above)
JaggedArray< uint32_t > cell_es
std::vector< std::array< double, 3 > > cell_kernel
A point from which the cell is star-shaped.
JaggedArray< uint32_t > vertex_hs
JaggedArray< uint32_t > vertex_vs
JaggedArray< uint32_t > edge_hs
void append(const Mesh3DStorage &other)
Appends the input topology of other (shifting its ids).
JaggedArray< uint16_t > face_hs_slot
std::vector< std::array< uint32_t, 2 > > edge_vs
JaggedArray< uint32_t > cell_fs
std::vector< uint8_t > edge_boundary
enable_if_t< t==0 > sort(Eigen::Matrix< T, 3, 3 > &U, Eigen::Matrix< T, 3, 1 > &sigma, Eigen::Matrix< T, 3, 3 > &V)
Helper function of 3X3 SVD for sorting singular values.
M
Definition eigs.py:94
void reorder_hex_mesh_propogation(Mesh3DStorage &hmi)
void global_orientation_hexes(Mesh3DStorage &hmi)
double a_jacobian(Eigen::Vector3d &v0, Eigen::Vector3d &v1, Eigen::Vector3d &v2, Eigen::Vector3d &v3)
void refine_red_refinement_tet(Mesh3DStorage &M, int iter)
void orient_surface_mesh(Mesh3DStorage &hmi)
void build_connectivity(Mesh3DStorage &hmi)
void orient_volume_mesh(Mesh3DStorage &hmi)
void ele_subdivison_levels(const Mesh3DStorage &hmi, std::vector< int > &Ls)
void refine_catmul_clark_polar(Mesh3DStorage &M, int iter, std::vector< int > &Parents)
int find(const Eigen::VectorXi &vec, int x)
Definition NCMesh2D.cpp:369
double signed_volume(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F)
Compute the signed volume of a surface mesh.
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44