PolyFEM
Loading...
Searching...
No Matches
CMesh3D.cpp
Go to the documentation of this file.
5
7
8#include <igl/barycentric_coordinates.h>
9
10#include <unordered_map>
11
12using namespace polyfem::utils;
13
14namespace polyfem
15{
16 namespace mesh
17 {
18 void CMesh3D::remove_elements(const std::vector<bool> &keep)
19 {
20 assert(keep.size() == n_cells());
21
22 auto face_key = [this](const int f) {
23 std::vector<int> key(n_face_vertices(f));
24 for (int lv = 0; lv < key.size(); ++lv)
25 key[lv] = face_vertex(f, lv);
26 std::sort(key.begin(), key.end());
27 return key;
28 };
29 auto edge_key = [this](const int e) {
30 return std::pair<int, int>(
31 std::min(edge_vertex(e, 0), edge_vertex(e, 1)),
32 std::max(edge_vertex(e, 0), edge_vertex(e, 1)));
33 };
34
35 std::unordered_map<std::vector<int>, int, utils::HashVector> old_boundary_ids;
36 std::unordered_map<std::vector<int>, FaceNodes, utils::HashVector> old_face_nodes;
37 for (int f = 0; f < n_faces(); ++f)
38 {
39 const auto key = face_key(f);
40 if (has_boundary_ids())
41 old_boundary_ids[key] = boundary_ids_[f];
42 if (f < face_nodes_.size())
43 old_face_nodes[key] = face_nodes_[f];
44 }
45
46 std::unordered_map<std::pair<int, int>, EdgeNodes, utils::HashPair> old_edge_nodes;
47 for (int e = 0; e < n_edges() && e < edge_nodes_.size(); ++e)
48 old_edge_nodes[edge_key(e)] = edge_nodes_[e];
49
50 if (cell_nodes_.size() == keep.size())
51 {
52 std::vector<CellNodes> filtered;
53 for (int c = 0; c < keep.size(); ++c)
54 if (keep[c])
55 filtered.push_back(cell_nodes_[c]);
56 cell_nodes_ = std::move(filtered);
57 }
59
60 std::vector<bool> used_faces(mesh_.n_faces(), false);
61 for (int c = 0; c < keep.size(); ++c)
62 if (keep[c])
63 for (const int f : mesh_.cell_fs[c])
64 used_faces[f] = true;
65
66 std::vector<int> old_to_new_face(mesh_.n_faces(), -1);
68 for (int f = 0; f < used_faces.size(); ++f)
69 {
70 if (!used_faces[f])
71 continue;
72 old_to_new_face[f] = face_vs.size();
73 face_vs.push_back(mesh_.face_vs[f]);
74 }
75
76 JaggedArray<uint32_t> cell_vs, cell_input_vs, cell_fs;
77 JaggedArray<uint8_t> cell_fs_flag;
78 std::vector<uint8_t> cell_hex;
79 std::vector<std::array<double, 3>> cell_kernel;
80 std::vector<uint32_t> fs;
81 for (int c = 0; c < keep.size(); ++c)
82 {
83 if (!keep[c])
84 continue;
85 cell_vs.push_back(mesh_.cell_vs[c]);
86 cell_input_vs.push_back(mesh_.cell_input_vs[c]);
87 fs.clear();
88 for (const auto f : mesh_.cell_fs[c])
89 fs.push_back(old_to_new_face[f]);
90 cell_fs.push_back(fs);
91 cell_fs_flag.push_back(mesh_.cell_fs_flag[c]);
92 cell_hex.push_back(mesh_.cell_hex[c]);
93 cell_kernel.push_back(mesh_.cell_kernel[c]);
94 }
95 mesh_.face_vs = std::move(face_vs);
96 mesh_.cell_vs = std::move(cell_vs);
97 mesh_.cell_input_vs = std::move(cell_input_vs);
98 mesh_.cell_fs = std::move(cell_fs);
99 mesh_.cell_fs_flag = std::move(cell_fs_flag);
100 mesh_.cell_hex = std::move(cell_hex);
101 mesh_.cell_kernel = std::move(cell_kernel);
102
104
105 if (!old_boundary_ids.empty())
106 {
107 boundary_ids_.resize(n_faces());
108 for (int f = 0; f < n_faces(); ++f)
109 {
110 const auto it = old_boundary_ids.find(face_key(f));
111 boundary_ids_[f] = it == old_boundary_ids.end() ? get_default_boundary_id(f) : it->second;
112 }
113 }
114 else
115 boundary_ids_.clear();
116
117 face_nodes_.clear();
118 if (!old_face_nodes.empty())
119 {
120 face_nodes_.resize(n_faces());
121 for (int f = 0; f < n_faces(); ++f)
122 {
123 const auto it = old_face_nodes.find(face_key(f));
124 if (it != old_face_nodes.end())
125 face_nodes_[f] = it->second;
126 }
127 }
128
129 edge_nodes_.clear();
130 if (!old_edge_nodes.empty())
131 {
132 edge_nodes_.resize(n_edges());
133 for (int e = 0; e < n_edges(); ++e)
134 {
135 const auto it = old_edge_nodes.find(edge_key(e));
136 if (it != old_edge_nodes.end())
137 edge_nodes_[e] = it->second;
138 }
139 }
140
141 in_ordered_edges_.resize(n_edges(), 2);
142 for (int e = 0; e < n_edges(); ++e)
143 in_ordered_edges_.row(e) << edge_vertex(e, 0), edge_vertex(e, 1);
144 int max_face_size = 0;
145 for (int f = 0; f < n_faces(); ++f)
146 max_face_size = std::max(max_face_size, n_face_vertices(f));
147 in_ordered_faces_.setConstant(n_faces(), max_face_size, -1);
148 for (int f = 0; f < n_faces(); ++f)
149 for (int lv = 0; lv < n_face_vertices(f); ++lv)
150 in_ordered_faces_(f, lv) = face_vertex(f, lv);
152 }
153
154 void CMesh3D::refine(const int n_refinement, const double t)
155 {
156 if (n_refinement <= 0)
157 {
158 return;
159 }
160
161 // TODO refine high order mesh!
163 if (mesh_.type == MeshType::TET)
164 {
166 }
167 else
168 {
169 for (size_t i = 0; i < elements_tag().size(); ++i)
170 {
172 mesh_.cell_hex[i] = false;
173 }
174
175 std::vector<int> parent_nodes;
176 MeshProcessing3D::refine_catmul_clark_polar(mesh_, n_refinement, parent_nodes);
177 }
178
181
182 in_ordered_vertices_ = Eigen::VectorXi::LinSpaced(n_vertices(), 0, n_vertices() - 1);
183 assert(in_ordered_vertices_[0] == 0);
184 assert(in_ordered_vertices_[1] == 1);
185 assert(in_ordered_vertices_[2] == 2);
186 assert(in_ordered_vertices_[in_ordered_vertices_.size() - 1] == n_vertices() - 1);
187
188 in_ordered_edges_.resize(mesh_.n_edges(), 2);
189
190 for (int e = 0; e < (int)mesh_.n_edges(); ++e)
191 {
192 assert(mesh_.edge_vs[e].size() == 2);
193 for (int lv = 0; lv < 2; ++lv)
194 {
195 in_ordered_edges_(e, lv) = mesh_.edge_vs[e][lv];
196 }
197 }
198 assert(in_ordered_edges_.size() > 0);
199
201
202 for (int f = 0; f < (int)mesh_.n_faces(); ++f)
203 {
204 assert(in_ordered_faces_.cols() == mesh_.face_vs[f].size());
205
206 for (int lv = 0; lv < in_ordered_faces_.cols(); ++lv)
207 {
208 in_ordered_faces_(f, lv) = mesh_.face_vs[f][lv];
209 }
210 }
211 assert(in_ordered_faces_.size() > 0);
212 }
213
214 // Build the internal topology from standard cells (tets, pyramids, prisms, hexes).
215 bool CMesh3D::build_storage_from_cells(const Eigen::MatrixXd &V, const std::vector<StandardCell> &cells)
216 {
217 edge_nodes_.clear();
218 face_nodes_.clear();
219 cell_nodes_.clear();
220
221 assert(V.cols() == 3);
222
223 // Set vertices
224 const int nv = V.rows();
226 mesh_.points.resize(3, nv);
227 for (int i = 0; i < nv; ++i)
228 {
229 mesh_.points(0, i) = V(i, 0);
230 mesh_.points(1, i) = V(i, 1);
231 mesh_.points(2, i) = V(i, 2);
232 }
233
234 const bool all_tets = std::all_of(cells.begin(), cells.end(), [](const StandardCell &c) {
235 return c.type == StandardCellType::TET;
236 });
237
238 // Set cells
239 // MeshData guarantees at least one cell.
240 if (cells.empty())
241 log_and_throw_error("A volume mesh needs at least one cell.");
242 else
243 {
244 const std::vector<std::array<int, 6>> adjacent = connect_cells(cells, nv);
245
246 // Index of the cell facets (c, lf).
247 std::vector<int> first_cell_facet(cells.size() + 1, 0);
248 for (int c = 0; c < cells.size(); ++c)
249 first_cell_facet[c + 1] = first_cell_facet[c] + cells[c].n_facets();
250
251 // Cell facet of the neighbor across (c, lf) that is glued to c.
252 auto opposite_cell_facet = [&](int c, int lf) {
253 const int c2 = adjacent[c][lf];
254 if (c2 == -1)
255 {
256 return -1;
257 }
258 for (int lf2 = 0; lf2 < cells[c2].n_facets(); ++lf2)
259 {
260 if (c == adjacent[c2][lf2])
261 {
262 return first_cell_facet[c2] + lf2;
263 }
264 }
265 assert(false);
266 return -1;
267 };
268
269 std::vector<int> cell_facet_to_facet(first_cell_facet.back(), -1);
270
271 // Creates 1 hex or polyhedral element for each cell of the input mesh
272 int facet_counter = 0;
273 bool is_hex = true;
274 mesh_.cell_hex.reserve(cells.size());
275 mesh_.cell_kernel.reserve(cells.size());
276 std::vector<uint32_t> fs, face_vs, input_vs;
277 std::vector<uint8_t> fs_flag;
278 for (int c = 0; c < (int)cells.size(); ++c)
279 {
280 const bool hex = (cells[c].type == StandardCellType::HEX);
281 mesh_.cell_hex.push_back(hex);
282 is_hex = is_hex && hex;
283
284 int nf = cells[c].n_facets();
285 fs.assign(nf, 0);
286 fs_flag.clear();
287
288 for (int lf = 0; lf < nf; ++lf)
289 {
290 int cf = first_cell_facet[c] + lf;
291 int cf2 = opposite_cell_facet(c, lf);
292 if (cf2 < 0 || cell_facet_to_facet[cf2] < 0)
293 {
294 face_vs.resize(cells[c].n_facet_vertices(lf));
295 for (int lv = 0; lv < cells[c].n_facet_vertices(lf); ++lv)
296 {
297 face_vs[lv] = cells[c].facet_vertex(lf, lv);
298 }
299 mesh_.face_vs.push_back(face_vs);
300 fs_flag.push_back(0);
301 fs[lf] = facet_counter;
302 cell_facet_to_facet[cf] = facet_counter;
303 ++facet_counter;
304 }
305 else
306 {
307 fs[lf] = cell_facet_to_facet[cf2];
308 fs_flag.push_back(1);
309 }
310 }
313 mesh_.cell_vs.push_back_filled(0, 0); // built by prepare_mesh
314
315 const int n_cell_vertices = cells[c].descriptor().n_vertices;
316 input_vs.resize(n_cell_vertices);
317 for (int lv = 0; lv < n_cell_vertices; ++lv)
318 input_vs[lv] = cells[c].vertices[lv];
319 mesh_.cell_input_vs.push_back(input_vs);
320
321 // Compute a point in the kernel (assumes the barycenter is ok)
322 Eigen::RowVector3d p(0, 0, 0);
323 for (int v : input_vs)
324 {
325 p += mesh_.points.col(v).transpose();
326 }
327 p /= input_vs.size();
328 mesh_.cell_kernel.push_back({{p[0], p[1], p[2]}});
329 }
330 mesh_.type = is_hex ? MeshType::HEX : (all_tets ? MeshType::TET : MeshType::HYB);
331 }
332
334 // if (is_simplicial()) {
335 // MeshProcessing3D::orient_volume_mesh(mesh_);
336 // }
338 return true;
339 }
340
342 {
343 const Eigen::MatrixXd &V = data.vertices;
344 const Eigen::MatrixXi &F = data.elements;
345 edge_nodes_.clear();
346 face_nodes_.clear();
347 cell_nodes_.clear();
348
349 if (data.has_polyhedral_topology())
350 {
352 mesh_.points = V.transpose();
353
354 for (int i = 0; i < data.faces.size(); ++i)
355 mesh_.face_vs.push_back(data.faces[i]);
356
357 bool all_hex = true;
358 std::vector<uint32_t> input_vs, vs;
359 std::vector<uint8_t> fs_flag;
360 for (int i = 0; i < F.rows(); ++i)
361 {
363 fs_flag.clear();
364 for (const int o : data.cell_face_orientations[i])
365 fs_flag.push_back(o != 0);
367 mesh_.cell_hex.push_back(data.cell_is_hex[i]);
368 all_hex &= data.cell_is_hex[i];
369 input_vs.clear();
370 if (data.elements_are_ordered)
371 for (int j = 0; j < F.cols() && F(i, j) >= 0; ++j)
372 input_vs.push_back(F(i, j));
373 mesh_.cell_input_vs.push_back(input_vs);
374 vs.clear();
375 for (const int face : data.cell_faces[i])
376 vs.insert(vs.end(), mesh_.face_vs[face].begin(), mesh_.face_vs[face].end());
377 std::sort(vs.begin(), vs.end());
378 vs.erase(std::unique(vs.begin(), vs.end()), vs.end());
380 assert(data.cell_kernel_points.cols() == 3);
381 mesh_.cell_kernel.push_back({{data.cell_kernel_points(i, 0), data.cell_kernel_points(i, 1), data.cell_kernel_points(i, 2)}});
382 }
386 return true;
387 }
388 assert(F.cols() == 4 || F.cols() == 5 || F.cols() == 6 || F.cols() == 8);
389
390 static const std::vector<int> permute_tet = {0, 1, 2, 3};
391 static const std::vector<int> permute_pyramid = {0, 1, 2, 3, 4};
392 static const std::vector<int> permute_prism = {0, 1, 2, 3, 4, 5};
393 // MeshData uses the msh hex vertex ordering, the cell descriptors use
394 // geogram's (see CellConnectivity.hpp).
395 static const std::vector<int> permute_hex = {1, 0, 2, 3, 5, 4, 6, 7};
396
397 std::vector<StandardCell> cells;
398 cells.reserve(F.rows());
399 auto add_cell = [&](int c, StandardCellType t, int nv, const std::vector<int> &perm) {
400 StandardCell cell(t);
401 for (int lv = 0; lv < nv; ++lv)
402 {
403 int vi = F(c, perm[lv]);
404 assert(vi >= 0 && vi < V.rows());
405 cell.vertices[lv] = vi;
406 }
407 cells.push_back(cell);
408 };
409
410 for (int c = 0; c < F.rows(); ++c)
411 {
412 int nV;
413 for (nV = 0; nV < F.cols(); ++nV)
414 {
415 if (F(c, nV) == -1)
416 break;
417 }
418
419#ifndef NDEBUG
420 for (int k = nV; k < F.cols(); ++k)
421 {
422 assert(F(c, k) == -1);
423 }
424#endif
425 if (nV == 4)
426 add_cell(c, StandardCellType::TET, 4, permute_tet);
427 else if (nV == 5)
428 add_cell(c, StandardCellType::PYRAMID, 5, permute_pyramid);
429 else if (nV == 6)
430 add_cell(c, StandardCellType::PRISM, 6, permute_prism);
431 else if (nV == 8)
432 add_cell(c, StandardCellType::HEX, 8, permute_hex);
433 else
434 log_and_throw_error(fmt::format("Invalid number of vertices {}", nV));
435 }
436
437 if (!build_storage_from_cells(V, cells))
438 return false;
439
440 // cell_vertex() (and therefore to_mesh_data() and checkpoints)
441 // returns input_vs, which must be in the input (MeshData, i.e. Gmsh)
442 // order. Hex cells were permuted to the descriptor order
443 // above; restore their input order (the kernel point was already
444 // computed from the permuted order).
445 for (int c = 0; c < cells.size(); ++c)
446 {
447 if (cells[c].type != StandardCellType::HEX)
448 continue;
449 for (int lv = 0; lv < 8; ++lv)
450 mesh_.cell_input_vs[c][lv] = F(c, lv);
451 }
452 return true;
453 }
454
455 void CMesh3D::attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector<std::vector<int>> &nodes)
456 {
458 face_nodes_.clear();
459 cell_nodes_.clear();
460
461 edge_nodes_.resize(n_edges());
462 face_nodes_.resize(n_faces());
463 cell_nodes_.resize(n_cells());
464
465 orders_.setOnes(n_cells(), 1);
466
467 const auto attach_p2 = [&](const Navigation3D::Index &index, const std::vector<int> &nodes_ids) {
468 auto &n = edge_nodes_[index.edge];
469
470 if (n.nodes.size() > 0)
471 return;
472
473 n.v1 = index.vertex;
474 n.v2 = switch_vertex(index).vertex;
475
476 const int n_v1 = index.vertex;
477 const int n_v2 = switch_vertex(index).vertex;
478
479 int node_index = 0;
480
481 if ((n_v1 == nodes_ids[0] && n_v2 == nodes_ids[1]) || (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[1]))
482 node_index = 4;
483 else if ((n_v1 == nodes_ids[1] && n_v2 == nodes_ids[2]) || (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[2]))
484 node_index = 5;
485 else if ((n_v1 == nodes_ids[2] && n_v2 == nodes_ids[3]) || (n_v2 == nodes_ids[2] && n_v1 == nodes_ids[3]))
486 node_index = 8;
487
488 else if ((n_v1 == nodes_ids[0] && n_v2 == nodes_ids[3]) || (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[3]))
489 node_index = 7;
490 else if ((n_v1 == nodes_ids[0] && n_v2 == nodes_ids[2]) || (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[2]))
491 node_index = 6;
492 else
493 node_index = 9;
494
495 n.nodes.resize(1, 3);
496 n.nodes << V(nodes_ids[node_index], 0), V(nodes_ids[node_index], 1), V(nodes_ids[node_index], 2);
497 n.nodes_ids.push_back(nodes_ids[node_index]);
498 };
499
500 const auto attach_p3 = [&](const Navigation3D::Index &index, const std::vector<int> &nodes_ids) {
501 auto &n = edge_nodes_[index.edge];
502
503 if (n.nodes.size() > 0)
504 return;
505
506 n.v1 = index.vertex;
507 n.v2 = switch_vertex(index).vertex;
508
509 const int n_v1 = index.vertex;
510 const int n_v2 = switch_vertex(index).vertex;
511
512 int node_index1 = 0;
513 int node_index2 = 0;
514 if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[1])
515 {
516 node_index1 = 4;
517 node_index2 = 5;
518 }
519 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[1])
520 {
521 node_index1 = 5;
522 node_index2 = 4;
523 }
524 else if (n_v1 == nodes_ids[1] && n_v2 == nodes_ids[2])
525 {
526 node_index1 = 6;
527 node_index2 = 7;
528 }
529 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[2])
530 {
531 node_index1 = 7;
532 node_index2 = 6;
533 }
534 else if (n_v1 == nodes_ids[2] && n_v2 == nodes_ids[3])
535 {
536 node_index1 = 13;
537 node_index2 = 12;
538 }
539 else if (n_v2 == nodes_ids[2] && n_v1 == nodes_ids[3])
540 {
541 node_index1 = 12;
542 node_index2 = 13;
543 }
544
545 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[3])
546 {
547 node_index1 = 11;
548 node_index2 = 10;
549 }
550 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[3])
551 {
552 node_index1 = 10;
553 node_index2 = 11;
554 }
555 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[2])
556 {
557 node_index1 = 9;
558 node_index2 = 8;
559 }
560 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[2])
561 {
562 node_index1 = 8;
563 node_index2 = 9;
564 }
565
566 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[3])
567 {
568 node_index1 = 14;
569 node_index2 = 15;
570 }
571 else
572 {
573 node_index1 = 15;
574 node_index2 = 14;
575 }
576
577 n.nodes.resize(2, 3);
578 n.nodes.row(0) << V(nodes_ids[node_index1], 0), V(nodes_ids[node_index1], 1), V(nodes_ids[node_index1], 2);
579 n.nodes.row(1) << V(nodes_ids[node_index2], 0), V(nodes_ids[node_index2], 1), V(nodes_ids[node_index2], 2);
580 n.nodes_ids.push_back(nodes_ids[node_index1]);
581 n.nodes_ids.push_back(nodes_ids[node_index2]);
582 };
583
584 const auto attach_p3_face = [&](const Navigation3D::Index &index, const std::vector<int> &nodes_ids, int id) {
585 auto &n = face_nodes_[index.face];
586 if (n.nodes.size() <= 0)
587 {
588 n.v1 = face_vertex(index.face, 0);
589 n.v2 = face_vertex(index.face, 1);
590 n.v3 = face_vertex(index.face, 2);
591 n.nodes.resize(1, 3);
592 n.nodes << V(nodes_ids[id], 0), V(nodes_ids[id], 1), V(nodes_ids[id], 2);
593 n.nodes_ids.push_back(nodes_ids[id]);
594 }
595 };
596
597 const auto attach_p4 = [&](const Navigation3D::Index &index, const std::vector<int> &nodes_ids) {
598 auto &n = edge_nodes_[index.edge];
599
600 if (n.nodes.size() > 0)
601 return;
602
603 n.v1 = index.vertex;
604 n.v2 = switch_vertex(index).vertex;
605
606 const int n_v1 = index.vertex;
607 const int n_v2 = switch_vertex(index).vertex;
608
609 int node_index1 = 0;
610 int node_index2 = 0;
611 int node_index3 = 0;
612 if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[1])
613 {
614 node_index1 = 4;
615 node_index2 = 5;
616 node_index3 = 6;
617 }
618 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[1])
619 {
620 node_index1 = 6;
621 node_index2 = 5;
622 node_index3 = 4;
623 }
624
625 else if (n_v1 == nodes_ids[1] && n_v2 == nodes_ids[2])
626 {
627 node_index1 = 7;
628 node_index2 = 8;
629 node_index3 = 9;
630 }
631 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[2])
632 {
633 node_index1 = 9;
634 node_index2 = 8;
635 node_index3 = 7;
636 }
637
638 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[2])
639 {
640 node_index1 = 10;
641 node_index2 = 11;
642 node_index3 = 12;
643 }
644 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[2])
645 {
646 node_index1 = 12;
647 node_index2 = 11;
648 node_index3 = 10;
649 }
650
651 else if (n_v2 == nodes_ids[0] && n_v1 == nodes_ids[3])
652 {
653 node_index1 = 13;
654 node_index2 = 14;
655 node_index3 = 15;
656 }
657 else if (n_v1 == nodes_ids[0] && n_v2 == nodes_ids[3])
658 {
659 node_index1 = 15;
660 node_index2 = 14;
661 node_index3 = 13;
662 }
663
664 else if (n_v2 == nodes_ids[2] && n_v1 == nodes_ids[3])
665 {
666 node_index1 = 16;
667 node_index2 = 17;
668 node_index3 = 18;
669 }
670 else if (n_v1 == nodes_ids[2] && n_v2 == nodes_ids[3])
671 {
672 node_index1 = 18;
673 node_index2 = 17;
674 node_index3 = 16;
675 }
676
677 else if (n_v2 == nodes_ids[1] && n_v1 == nodes_ids[3])
678 {
679 node_index1 = 19;
680 node_index2 = 20;
681 node_index3 = 21;
682 }
683 else if (n_v1 == nodes_ids[1] && n_v2 == nodes_ids[3])
684 {
685 node_index1 = 21;
686 node_index2 = 20;
687 node_index3 = 19;
688 }
689 else
690 {
691 assert(false);
692 }
693
694 n.nodes.resize(3, 3);
695 n.nodes.row(0) << V(nodes_ids[node_index1], 0), V(nodes_ids[node_index1], 1), V(nodes_ids[node_index1], 2);
696 n.nodes.row(1) << V(nodes_ids[node_index2], 0), V(nodes_ids[node_index2], 1), V(nodes_ids[node_index2], 2);
697 n.nodes.row(2) << V(nodes_ids[node_index3], 0), V(nodes_ids[node_index3], 1), V(nodes_ids[node_index3], 2);
698 n.nodes_ids.push_back(nodes_ids[node_index1]);
699 n.nodes_ids.push_back(nodes_ids[node_index2]);
700 n.nodes_ids.push_back(nodes_ids[node_index3]);
701 };
702
703 const auto attach_p4_face = [&](const Navigation3D::Index &index, const std::vector<int> &nodes_ids) {
704 auto &n = face_nodes_[index.face];
705 if (n.nodes.size() <= 0)
706 {
707 n.v1 = face_vertex(index.face, 0);
708 n.v2 = face_vertex(index.face, 1);
709 n.v3 = face_vertex(index.face, 2);
710
711 std::array<int, 3> vid = {{n.v1, n.v2, n.v3}};
712 std::sort(vid.begin(), vid.end());
713
714 std::array<int, 3> c1 = {{nodes_ids[0], nodes_ids[1], nodes_ids[2]}}; // 22
715 std::array<int, 3> c2 = {{nodes_ids[0], nodes_ids[1], nodes_ids[3]}}; // 25
716 std::array<int, 3> c3 = {{nodes_ids[0], nodes_ids[2], nodes_ids[3]}}; // 28
717 std::array<int, 3> c4 = {{nodes_ids[1], nodes_ids[2], nodes_ids[3]}}; // 31
718
719 std::sort(c1.begin(), c1.end());
720 std::sort(c2.begin(), c2.end());
721 std::sort(c3.begin(), c3.end());
722 std::sort(c4.begin(), c4.end());
723
724 int id = 0;
725 int index0 = 0;
726 int index1 = 1;
727 int index2 = 2;
728 if (vid == c1)
729 {
730 id = 22;
731 n.v1 = nodes_ids[0];
732 n.v2 = nodes_ids[1];
733 n.v3 = nodes_ids[2];
734
735 index0 = 0;
736 index1 = 2;
737 index2 = 1; // ok
738 }
739 else if (vid == c2)
740 {
741 id = 25;
742 n.v1 = nodes_ids[0];
743 n.v2 = nodes_ids[1];
744 n.v3 = nodes_ids[3];
745
746 index0 = 0;
747 index1 = 1;
748 index2 = 2; // ok
749 }
750 else if (vid == c3)
751 {
752 id = 28;
753 n.v1 = nodes_ids[0];
754 n.v2 = nodes_ids[2];
755 n.v3 = nodes_ids[3];
756
757 index0 = 0;
758 index1 = 2;
759 index2 = 1; // ok
760 }
761 else if (vid == c4)
762 {
763 id = 31;
764 n.v1 = nodes_ids[1];
765 n.v2 = nodes_ids[2];
766 n.v3 = nodes_ids[3];
767
768 index0 = 1;
769 index1 = 2;
770 index2 = 0; // ok
771 }
772 else
773 {
774 // the face nees to be one of the 4 above
775 assert(false);
776 }
777
778 n.nodes.resize(3, 3);
779 assert(id + index0 < nodes_ids.size());
780 assert(id + index1 < nodes_ids.size());
781 assert(id + index2 < nodes_ids.size());
782 n.nodes.row(0) << V(nodes_ids[id + index0], 0), V(nodes_ids[id + index0], 1), V(nodes_ids[id + index0], 2);
783 n.nodes.row(1) << V(nodes_ids[id + index1], 0), V(nodes_ids[id + index1], 1), V(nodes_ids[id + index1], 2);
784 n.nodes.row(2) << V(nodes_ids[id + index2], 0), V(nodes_ids[id + index2], 1), V(nodes_ids[id + index2], 2);
785 n.nodes_ids.push_back(nodes_ids[id + index0]);
786 n.nodes_ids.push_back(nodes_ids[id + index1]);
787 n.nodes_ids.push_back(nodes_ids[id + index2]);
788 }
789 };
790
791 const auto attach_p4_cell = [&](const Navigation3D::Index &index, const std::vector<int> &nodes_ids) {
792 auto &n = cell_nodes_[index.element];
793 assert(nodes_ids.size() == 35);
794 assert(n.nodes.size() == 0);
795
796 if (n.nodes.size() <= 0)
797 {
798 n.v1 = cell_vertex(index.element, 0);
799 n.v2 = cell_vertex(index.element, 1);
800 n.v3 = cell_vertex(index.element, 2);
801 n.v4 = cell_vertex(index.element, 3);
802 n.nodes.resize(1, 3);
803
804 n.nodes << V(nodes_ids[34], 0), V(nodes_ids[34], 1), V(nodes_ids[34], 2);
805 n.nodes_ids.push_back(nodes_ids[34]);
806 }
807 };
808
809 assert(nodes.size() == n_cells());
810
811 for (int c = 0; c < n_cells(); ++c)
812 {
813 auto index = get_index_from_element(c);
814
815 const auto &nodes_ids = nodes[c];
816
817 // Only tetrahedra can be curved. Mesh::append lists the plain
818 // vertices of the linear hexes, prisms, pyramids and polyhedra it
819 // merges with curved tets, so a node list as long as the vertex
820 // list is linear.
821 if (n_cell_vertices(c) != 4)
822 {
823 if (nodes_ids.size() != size_t(n_cell_vertices(c)))
824 log_and_throw_error("Element {} is a curved cell with {} vertices and {} nodes; only tetrahedra can be curved.", c, n_cell_vertices(c), nodes_ids.size());
825 continue;
826 }
827
828 if (nodes_ids.size() == 4)
829 {
830 orders_(c) = 1;
831 continue;
832 }
833 // P2
834 else if (nodes_ids.size() == 10)
835 {
836 orders_(c) = 2;
837
838 for (int le = 0; le < 3; ++le)
839 {
840 attach_p2(index, nodes_ids);
841 index = next_around_face(index);
842 }
843
844 index = switch_vertex(switch_edge(switch_face(index)));
845 attach_p2(index, nodes_ids);
846
847 index = switch_edge(index);
848 attach_p2(index, nodes_ids);
849
850 index = switch_edge(switch_face(index));
851 attach_p2(index, nodes_ids);
852 }
853 // P3
854 else if (nodes_ids.size() == 20)
855 {
856 orders_(c) = 3;
857
858 for (int le = 0; le < 3; ++le)
859 {
860 attach_p3(index, nodes_ids);
861 index = next_around_face(index);
862 }
863
864 {
865 index = switch_vertex(switch_edge(switch_face(index)));
866 attach_p3(index, nodes_ids);
867
868 index = switch_edge(index);
869 attach_p3(index, nodes_ids);
870
871 index = switch_edge(switch_face(index));
872 attach_p3(index, nodes_ids);
873 }
874
875 {
876 index = get_index_from_element(c);
877 std::array<int, 4> indices;
878
879 {
880 std::array<int, 3> f16 = {{nodes_ids[0], nodes_ids[1], nodes_ids[2]}};
881 std::array<int, 3> f17 = {{nodes_ids[3], nodes_ids[1], nodes_ids[0]}};
882 std::array<int, 3> f18 = {{nodes_ids[0], nodes_ids[2], nodes_ids[3]}};
883 std::array<int, 3> f19 = {{nodes_ids[1], nodes_ids[2], nodes_ids[3]}};
884 std::sort(f16.begin(), f16.end());
885 std::sort(f17.begin(), f17.end());
886 std::sort(f18.begin(), f18.end());
887 std::sort(f19.begin(), f19.end());
888
889 auto tmp = index;
890 std::array<int, 3> f0 = {{face_vertex(tmp.face, 0), face_vertex(tmp.face, 1), face_vertex(tmp.face, 2)}};
891 tmp = switch_face(index);
892 std::array<int, 3> f1 = {{face_vertex(tmp.face, 0), face_vertex(tmp.face, 1), face_vertex(tmp.face, 2)}};
893 tmp = switch_face(next_around_face(index));
894 std::array<int, 3> f2 = {{face_vertex(tmp.face, 0), face_vertex(tmp.face, 1), face_vertex(tmp.face, 2)}};
896 std::array<int, 3> f3 = {{face_vertex(tmp.face, 0), face_vertex(tmp.face, 1), face_vertex(tmp.face, 2)}};
897
898 std::sort(f0.begin(), f0.end());
899 std::sort(f1.begin(), f1.end());
900 std::sort(f2.begin(), f2.end());
901 std::sort(f3.begin(), f3.end());
902
903 const std::array<std::array<int, 3>, 4> faces = {{f0, f1, f2, f3}};
904 const std::array<std::array<int, 3>, 4> nodes = {{f16, f17, f18, f19}};
905 for (int i = 0; i < 4; ++i)
906 {
907 const auto &f = faces[i];
908 bool found = false;
909 for (int j = 0; j < 4; ++j)
910 {
911 if (nodes[j] == f)
912 {
913 indices[i] = j + 16;
914 found = true;
915 break;
916 }
917 }
918
919 assert(found);
920 }
921 }
922
923 attach_p3_face(index, nodes_ids, indices[0]);
924 attach_p3_face(switch_face(index), nodes_ids, indices[1]);
925 attach_p3_face(switch_face(next_around_face(index)), nodes_ids, indices[2]);
926 attach_p3_face(switch_face(next_around_face(next_around_face(index))), nodes_ids, indices[3]);
927 }
928 }
929 // P4
930 else if (nodes_ids.size() == 35)
931 {
932 orders_(c) = 4;
933 for (int le = 0; le < 3; ++le)
934 {
935 attach_p4(index, nodes_ids);
936 index = next_around_face(index);
937 }
938
939 {
940 index = switch_vertex(switch_edge(switch_face(index)));
941 attach_p4(index, nodes_ids);
942
943 index = switch_edge(index);
944 attach_p4(index, nodes_ids);
945
946 index = switch_edge(switch_face(index));
947 attach_p4(index, nodes_ids);
948 }
949
950 {
951 index = get_index_from_element(c);
952
953 attach_p4_face(index, nodes_ids);
954 attach_p4_face(switch_face(index), nodes_ids);
955 attach_p4_face(switch_face(next_around_face(index)), nodes_ids);
956 attach_p4_face(switch_face(next_around_face(next_around_face(index))), nodes_ids);
957 }
958
959 attach_p4_cell(get_index_from_element(c), nodes_ids);
960 }
961 // unsupported
962 else
963 {
964 log_and_throw_error("Unsupported higher-order tetrahedron with {} nodes (supported: 4, 10, 20, 35).", nodes_ids.size());
965 }
966 }
967 }
968
970 {
971 const auto &V = mesh_.points;
972 min = V.rowwise().minCoeff().transpose();
973 max = V.rowwise().maxCoeff().transpose();
974 }
975
977 {
978 RowVectorNd minV, maxV;
979 bounding_box(minV, maxV);
980 auto &V = mesh_.points;
981 const auto shift = V.rowwise().minCoeff().eval();
982 const double scaling = 1.0 / (V.rowwise().maxCoeff() - V.rowwise().minCoeff()).maxCoeff();
983 V = (V.colwise() - shift) * scaling;
984
985 for (int i = 0; i < n_cells(); ++i)
986 {
987 for (int d = 0; d < 3; ++d)
988 {
989 auto val = mesh_.cell_kernel[i][d];
990 mesh_.cell_kernel[i][d] = (val - shift(d)) * scaling;
991 }
992 }
993
994 for (auto &n : edge_nodes_)
995 {
996 if (n.nodes.size() > 0)
997 n.nodes = (n.nodes.rowwise() - shift.transpose()) * scaling;
998 }
999 for (auto &n : face_nodes_)
1000 {
1001 if (n.nodes.size() > 0)
1002 n.nodes = (n.nodes.rowwise() - shift.transpose()) * scaling;
1003 }
1004 for (auto &n : cell_nodes_)
1005 {
1006 if (n.nodes.size() > 0)
1007 n.nodes = (n.nodes.rowwise() - shift.transpose()) * scaling;
1008 }
1010 Eigen::Matrix3d::Identity() * scaling,
1011 -shift * scaling);
1012
1013 logger().debug("-- bbox before normalization:");
1014 logger().debug(" min : {}", minV);
1015 logger().debug(" max : {}", maxV);
1016 logger().debug(" extent: {}", maxV - minV);
1017 bounding_box(minV, maxV);
1018 logger().debug("-- bbox after normalization:");
1019 logger().debug(" min : {}", minV);
1020 logger().debug(" max : {}", maxV);
1021 logger().debug(" extent: {}", maxV - minV);
1022
1023 // V.row(1) /= 100.;
1024
1025 // for(int i = 0; i < n_cells(); ++i)
1026 // {
1027 // mesh_.cell_kernel[i][1] /= 100.;
1028 // }
1029
1030 Eigen::MatrixXd p0, p1, p;
1031 get_edges(p0, p1);
1032 p = p0 - p1;
1033 logger().debug("-- edge length after normalization:");
1034 logger().debug(" min: ", p.rowwise().norm().minCoeff());
1035 logger().debug(" max: ", p.rowwise().norm().maxCoeff());
1036 logger().debug(" avg: ", p.rowwise().norm().mean());
1037 }
1038
1039 // void CMesh3D::triangulate_faces(Eigen::MatrixXi &tris, Eigen::MatrixXd &pts, std::vector<int> &ranges) const
1040 // {
1041 // ranges.clear();
1042
1043 // std::vector<Eigen::MatrixXi> local_tris(mesh_.n_cells());
1044 // std::vector<Eigen::MatrixXd> local_pts(mesh_.n_cells());
1045 // Eigen::MatrixXi tets;
1046
1047 // int total_tris = 0;
1048 // int total_pts = 0;
1049
1050 // ranges.push_back(0);
1051
1052 // Eigen::MatrixXd face_barys;
1053 // face_barycenters(face_barys);
1054
1055 // Eigen::MatrixXd cell_barys;
1056 // cell_barycenters(cell_barys);
1057
1058 // for (std::size_t e = 0; e < mesh_.n_cells(); ++e)
1059 // {
1060 // const Element &el = mesh_.elements[e];
1061
1062 // const int n_vertices = el.vs.size();
1063 // const int n_faces = el.fs.size();
1064
1065 // Eigen::MatrixXd local_pt(n_vertices + n_faces, 3);
1066
1067 // std::map<int, int> global_to_local;
1068
1069 // for (int i = 0; i < n_vertices; ++i)
1070 // {
1071 // const int global_index = el.vs[i];
1072 // local_pt.row(i) = mesh_.points.col(global_index).transpose();
1073 // global_to_local[global_index] = i;
1074 // }
1075
1076 // int n_local_faces = 0;
1077 // for (int i = 0; i < n_faces; ++i)
1078 // {
1079 // const Face &f = mesh_.faces[el.fs[i]];
1080 // n_local_faces += f.vs.size();
1081
1082 // local_pt.row(n_vertices + i) = face_barys.row(f.id); // node_from_face(f.id);
1083 // }
1084
1085 // Eigen::MatrixXi local_faces(n_local_faces, 3);
1086
1087 // int face_index = 0;
1088 // for (int i = 0; i < n_faces; ++i)
1089 // {
1090 // const Face &f = mesh_.faces[el.fs[i]];
1091 // const int n_face_vertices = f.vs.size();
1092
1093 // const Eigen::RowVector3d e0 = (point(f.vs[0]) - local_pt.row(n_vertices + i));
1094 // const Eigen::RowVector3d e1 = (point(f.vs[1]) - local_pt.row(n_vertices + i));
1095 // const Eigen::RowVector3d normal = e0.cross(e1);
1096 // // const Eigen::RowVector3d check_dir = (node_from_element(e)-p);
1097 // const Eigen::RowVector3d check_dir = (cell_barys.row(e) - point(f.vs[1]));
1098
1099 // const bool reverse_order = normal.dot(check_dir) > 0;
1100
1101 // for (int j = 0; j < n_face_vertices; ++j)
1102 // {
1103 // const int jp = (j + 1) % n_face_vertices;
1104 // if (reverse_order)
1105 // {
1106 // local_faces(face_index, 0) = global_to_local[f.vs[jp]];
1107 // local_faces(face_index, 1) = global_to_local[f.vs[j]];
1108 // }
1109 // else
1110 // {
1111 // local_faces(face_index, 0) = global_to_local[f.vs[j]];
1112 // local_faces(face_index, 1) = global_to_local[f.vs[jp]];
1113 // }
1114 // local_faces(face_index, 2) = n_vertices + i;
1115
1116 // ++face_index;
1117 // }
1118 // }
1119
1120 // local_pts[e] = local_pt;
1121 // local_tris[e] = local_faces;
1122
1123 // total_tris += local_tris[e].rows();
1124 // total_pts += local_pts[e].rows();
1125
1126 // ranges.push_back(total_tris);
1127
1128 // assert(local_pts[e].rows() == local_pt.rows());
1129 // }
1130
1131 // tris.resize(total_tris, 3);
1132 // pts.resize(total_pts, 3);
1133
1134 // int tri_index = 0;
1135 // int pts_index = 0;
1136 // for (std::size_t i = 0; i < local_tris.size(); ++i)
1137 // {
1138 // tris.block(tri_index, 0, local_tris[i].rows(), local_tris[i].cols()) = local_tris[i].array() + pts_index;
1139 // tri_index += local_tris[i].rows();
1140
1141 // pts.block(pts_index, 0, local_pts[i].rows(), local_pts[i].cols()) = local_pts[i];
1142 // pts_index += local_pts[i].rows();
1143 // }
1144 // }
1145
1146 bool CMesh3D::is_boundary_element(const int element_global_id) const
1147 {
1148 const auto &fs = mesh_.cell_fs[element_global_id];
1149
1150 for (auto f_id : fs)
1151 {
1152 if (is_boundary_face(f_id))
1153 return true;
1154 }
1155
1156 const auto &vs = mesh_.cell_vs[element_global_id];
1157
1158 for (auto v_id : vs)
1159 {
1160 if (is_boundary_vertex(v_id))
1161 return true;
1162 }
1163
1164 return false;
1165 }
1166
1167 void CMesh3D::compute_boundary_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &, bool)> &marker)
1168 {
1169 boundary_ids_.resize(n_faces());
1170
1171 for (int f = 0; f < n_faces(); ++f)
1172 {
1173 const bool is_boundary = is_boundary_face(f);
1174 std::vector<int> vs(n_face_vertices(f));
1175 for (int vid = 0; vid < vs.size(); ++vid)
1176 vs[vid] = face_vertex(f, vid);
1177
1178 const auto p = face_barycenter(f);
1179
1180 std::sort(vs.begin(), vs.end());
1181 boundary_ids_[f] = marker(f, vs, p, is_boundary);
1182 }
1183 }
1184
1185 void CMesh3D::compute_body_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &)> &marker)
1186 {
1187 body_ids_.resize(n_elements());
1188 std::fill(body_ids_.begin(), body_ids_.end(), -1);
1189
1190 for (int e = 0; e < n_elements(); ++e)
1191 {
1192 const auto bary = cell_barycenter(e);
1193 body_ids_[e] = marker(e, element_vertices(e), bary);
1194 }
1195 }
1196
1198 {
1200 for (auto &kernel : mesh_.cell_kernel)
1201 {
1202 VectorNd k(3);
1203 k << kernel[0], kernel[1], kernel[2];
1204 k = A * k + b;
1205 for (int d = 0; d < 3; ++d)
1206 kernel[d] = k[d];
1207 }
1208 }
1209
1210 void CMesh3D::set_point(const int global_index, const RowVectorNd &p)
1211 {
1212 mesh_.points.col(global_index) = p.transpose();
1213 }
1214
1215 RowVectorNd CMesh3D::point(const int global_index) const
1216 {
1217 RowVectorNd pt = mesh_.points.col(global_index).transpose();
1218 return pt;
1219 }
1220
1221 RowVectorNd CMesh3D::kernel(const int c) const
1222 {
1223 RowVectorNd pt(3);
1224 pt << mesh_.cell_kernel[c][0], mesh_.cell_kernel[c][1], mesh_.cell_kernel[c][2];
1225 return pt;
1226 }
1227
1229 {
1230 std::vector<ElementType> &ele_tag = elements_tag_;
1231 ele_tag.clear();
1232
1233 ele_tag.resize(mesh_.n_cells());
1234 for (auto &t : ele_tag)
1236
1237 // boundary flags
1238 std::vector<bool> bv_flag(mesh_.n_vertices(), false), be_flag(mesh_.n_edges(), false), bf_flag(mesh_.n_faces(), false);
1239 for (int f = 0; f < mesh_.n_faces(); ++f)
1240 if (mesh_.face_boundary[f])
1241 bf_flag[f] = true;
1242 else
1243 {
1244 for (auto nhid : mesh_.face_hs[f])
1245 if (!mesh_.cell_hex[nhid])
1246 bf_flag[f] = true;
1247 }
1248 for (uint32_t i = 0; i < mesh_.n_faces(); ++i)
1249 if (bf_flag[i])
1250 for (uint32_t j = 0; j < mesh_.face_vs[i].size(); ++j)
1251 {
1252 uint32_t eid = mesh_.face_es[i][j];
1253 be_flag[eid] = true;
1254 bv_flag[mesh_.face_vs[i][j]] = true;
1255 }
1256
1257 for (int c = 0; c < mesh_.n_cells(); ++c)
1258 {
1259 const auto ele_vs = mesh_.cell_vs[c];
1260 const auto ele_es = mesh_.cell_es[c];
1261 if (mesh_.cell_hex[c])
1262 {
1263 bool attaching_non_hex = false, on_boundary = false;
1264 ;
1265 for (auto vid : ele_vs)
1266 {
1267 for (auto eleid : mesh_.vertex_hs[vid])
1268 if (!mesh_.cell_hex[eleid])
1269 {
1270 attaching_non_hex = true;
1271 break;
1272 }
1273 if (mesh_.vertex_boundary[vid])
1274 {
1275 on_boundary = true;
1276 break;
1277 }
1278 if (on_boundary || attaching_non_hex)
1279 break;
1280 }
1281 if (attaching_non_hex)
1282 {
1283 ele_tag[c] = ElementType::INTERFACE_CUBE;
1284 continue;
1285 }
1286
1287 if (on_boundary)
1288 {
1290 // has no boundary edge--> singular
1291 bool boundary_edge = false, boundary_edge_singular = false, interior_edge_singular = false;
1292 int n_interior_edge_singular = 0;
1293 for (auto eid : ele_es)
1294 {
1295 int en = 0;
1296 if (be_flag[eid])
1297 {
1298 boundary_edge = true;
1299 for (auto nhid : mesh_.edge_hs[eid])
1300 if (mesh_.cell_hex[nhid])
1301 en++;
1302 if (en > 2)
1303 boundary_edge_singular = true;
1304 }
1305 else
1306 {
1307 for (auto nhid : mesh_.edge_hs[eid])
1308 if (mesh_.cell_hex[nhid])
1309 en++;
1310 if (en != 4)
1311 {
1312 interior_edge_singular = true;
1313 n_interior_edge_singular++;
1314 }
1315 }
1316 }
1317 if (!boundary_edge || boundary_edge_singular || n_interior_edge_singular > 1)
1318 continue;
1319
1320 bool has_singular_v = false, has_iregular_v = false;
1321 int n_in_irregular_v = 0;
1322 for (auto vid : ele_vs)
1323 {
1324 int vn = 0;
1325 if (bv_flag[vid])
1326 {
1327 int nh = 0;
1328 for (auto nhid : mesh_.vertex_hs[vid])
1329 if (mesh_.cell_hex[nhid])
1330 nh++;
1331 if (nh > 4)
1332 has_iregular_v = true;
1333 continue; // not sure the conditions
1334 }
1335 else
1336 {
1337 if (mesh_.vertex_hs[vid].size() != 8)
1338 n_in_irregular_v++;
1339 int n_irregular_e = 0;
1340 for (auto eid : mesh_.vertex_es[vid])
1341 {
1342 if (mesh_.edge_hs[eid].size() != 4)
1343 n_irregular_e++;
1344 }
1345 if (n_irregular_e != 0 && n_irregular_e != 2)
1346 {
1347 has_singular_v = true;
1348 break;
1349 }
1350 }
1351 }
1352 int n_irregular_e = 0;
1353 for (auto eid : ele_es)
1354 if (!be_flag[eid] && mesh_.edge_hs[eid].size() != 4)
1355 n_irregular_e++;
1356 if (has_singular_v)
1357 continue;
1358 if (!has_singular_v)
1359 {
1360 if (n_irregular_e == 1)
1361 {
1363 }
1364 else if (n_irregular_e == 0 && n_in_irregular_v == 0 && !has_iregular_v)
1366 else
1367 continue;
1368 }
1369 continue;
1370 }
1371
1372 // type 1
1373 bool has_irregular_v = false;
1374 for (auto vid : ele_vs)
1375 if (mesh_.vertex_hs[vid].size() != 8)
1376 {
1377 has_irregular_v = true;
1378 break;
1379 }
1380 if (!has_irregular_v)
1381 {
1383 continue;
1384 }
1385 // type 2
1386 bool has_singular_v = false;
1387 int n_irregular_v = 0;
1388 for (auto vid : ele_vs)
1389 {
1390 if (mesh_.vertex_hs[vid].size() != 8)
1391 n_irregular_v++;
1392 int n_irregular_e = 0;
1393 for (auto eid : mesh_.vertex_es[vid])
1394 {
1395 if (mesh_.edge_hs[eid].size() != 4)
1396 n_irregular_e++;
1397 }
1398 if (n_irregular_e != 0 && n_irregular_e != 2)
1399 {
1400 has_singular_v = true;
1401 break;
1402 }
1403 }
1404 if (!has_singular_v && n_irregular_v == 2)
1405 {
1407 continue;
1408 }
1409
1411 }
1412 else
1413 {
1414 ele_tag[c] = ElementType::INTERIOR_POLYTOPE;
1415 for (auto fid : mesh_.cell_fs[c])
1416 if (mesh_.face_boundary[fid])
1417 {
1418 ele_tag[c] = ElementType::BOUNDARY_POLYTOPE;
1419 break;
1420 }
1421 }
1422 }
1423
1424 // TODO correct?
1425 for (int c = 0; c < mesh_.n_cells(); ++c)
1426 {
1427 const int nv = mesh_.cell_vs.row_size(c);
1428 if (nv == 4)
1429 ele_tag[c] = ElementType::SIMPLEX;
1430 else if (nv == 5)
1431 ele_tag[c] = ElementType::PYRAMID;
1432 else if (nv == 6)
1433 ele_tag[c] = ElementType::PRISM;
1434 }
1435 }
1436
1437 double CMesh3D::quad_area(const int gid) const
1438 {
1439 const int n_vertices = n_face_vertices(gid);
1440 assert(n_vertices == 4);
1441
1442 const auto &vertices = mesh_.face_vs[gid];
1443
1444 const auto v1 = point(vertices[0]);
1445 const auto v2 = point(vertices[1]);
1446 const auto v3 = point(vertices[2]);
1447 const auto v4 = point(vertices[3]);
1448
1449 const Eigen::Vector3d e0 = (v2 - v1).transpose();
1450 const Eigen::Vector3d e1 = (v3 - v1).transpose();
1451
1452 const Eigen::Vector3d e2 = (v2 - v4).transpose();
1453 const Eigen::Vector3d e3 = (v3 - v4).transpose();
1454
1455 return e0.cross(e1).norm() / 2 + e2.cross(e3).norm() / 2;
1456 }
1457
1459 {
1460 const int v0 = mesh_.edge_vs[e][0];
1461 const int v1 = mesh_.edge_vs[e][1];
1462 return 0.5 * (point(v0) + point(v1));
1463 }
1464
1466 {
1467 const int n_vertices = n_face_vertices(f);
1468 RowVectorNd bary(3);
1469 bary.setZero();
1470
1471 const auto &vertices = mesh_.face_vs[f];
1472 for (int lv = 0; lv < n_vertices; ++lv)
1473 {
1474 bary += point(vertices[lv]);
1475 }
1476 return bary / n_vertices;
1477 }
1478
1480 {
1481 const int n_vertices = n_cell_vertices(c);
1482 RowVectorNd bary(3);
1483 bary.setZero();
1484
1485 const auto &vertices = mesh_.cell_vs[c];
1486 for (int lv = 0; lv < n_vertices; ++lv)
1487 {
1488 bary += point(vertices[lv]);
1489 }
1490 return bary / n_vertices;
1491 }
1492
1493 void CMesh3D::barycentric_coords(const RowVectorNd &p, const int el_id, Eigen::MatrixXd &coord) const
1494 {
1495 assert(is_simplex(el_id));
1496
1497 const auto indices = get_ordered_vertices_from_tet(el_id);
1498
1499 const auto A = point(indices[0]);
1500 const auto B = point(indices[1]);
1501 const auto C = point(indices[2]);
1502 const auto D = point(indices[3]);
1503
1504 igl::barycentric_coordinates(p, A, B, C, D, coord);
1505 }
1506
1507 void CMesh3D::append(const Mesh &mesh)
1508 {
1509 assert(typeid(mesh) == typeid(CMesh3D));
1510 Mesh::append(mesh);
1511
1512 const CMesh3D &mesh3d = dynamic_cast<const CMesh3D &>(mesh);
1513 mesh_.append(mesh3d.mesh_);
1514
1517 }
1518
1519 std::unique_ptr<Mesh> CMesh3D::copy() const
1520 {
1521 return std::make_unique<CMesh3D>(*this);
1522 }
1523
1524 } // namespace mesh
1525} // namespace polyfem
int V
double val
Eigen::RowVectorXd point
Conforming 3D mesh of tetrahedra, hexahedra, prisms, pyramids and general polyhedra,...
Definition CMesh3D.hpp:34
RowVectorNd cell_barycenter(const int c) const override
cell barycenter
Definition CMesh3D.cpp:1479
RowVectorNd edge_barycenter(const int e) const override
edge barycenter
Definition CMesh3D.cpp:1458
RowVectorNd point(const int vertex_id) const override
point coordinates
Definition CMesh3D.cpp:1215
bool build_topology(const MeshData &data) override
Construct only the dimension-specific topology.
Definition CMesh3D.cpp:341
void normalize() override
normalize the mesh
Definition CMesh3D.cpp:976
int edge_vertex(const int e_id, const int lv_id) const override
id of the edge vertex
Definition CMesh3D.hpp:64
void refine(const int n_refinement, const double t) override
refine the mesh
Definition CMesh3D.cpp:154
int n_edges() const override
number of edges
Definition CMesh3D.hpp:49
int face_vertex(const int f_id, const int lv_id) const override
id of the face vertex
Definition CMesh3D.hpp:63
void compute_boundary_ids(const std::function< int(const size_t, const std::vector< int > &, const RowVectorNd &, bool)> &marker) override
computes boundary selections based on a function
Definition CMesh3D.cpp:1167
RowVectorNd face_barycenter(const int f) const override
face barycenter
Definition CMesh3D.cpp:1465
int n_vertices() const override
number of vertices
Definition CMesh3D.hpp:50
Mesh3DStorage mesh_
Definition CMesh3D.hpp:149
bool is_boundary_face(const int face_global_id) const override
is face boundary
Definition CMesh3D.hpp:70
void apply_affine_transformation(const MatrixNd &A, const VectorNd &b) override
Also moves the kernel points of the elements, used by polyhedral quadrature and refinement.
Definition CMesh3D.cpp:1197
double quad_area(const int gid) const override
area of a quad face of an hex mesh
Definition CMesh3D.cpp:1437
Navigation3D::Index next_around_face(Navigation3D::Index idx) const override
Definition CMesh3D.hpp:110
bool is_boundary_vertex(const int vertex_global_id) const override
is vertex boundary
Definition CMesh3D.hpp:68
RowVectorNd kernel(const int cell_id) const override
Definition CMesh3D.cpp:1221
void attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector< std::vector< int > > &nodes) override
attach high order nodes
Definition CMesh3D.cpp:455
void set_point(const int global_index, const RowVectorNd &p) override
Set the point.
Definition CMesh3D.cpp:1210
void bounding_box(RowVectorNd &min, RowVectorNd &max) const override
computes the bbox of the mesh
Definition CMesh3D.cpp:969
bool build_storage_from_cells(const Eigen::MatrixXd &V, const std::vector< StandardCell > &cells)
Definition CMesh3D.cpp:215
void append(const Mesh &mesh) override
appends a new mesh to the end of this
Definition CMesh3D.cpp:1507
int n_cells() const override
number of cells
Definition CMesh3D.hpp:47
int n_faces() const override
number of faces
Definition CMesh3D.hpp:48
Navigation3D::Index switch_vertex(Navigation3D::Index idx) const override
Definition CMesh3D.hpp:103
Navigation3D::Index get_index_from_element(int hi, int lf, int lv) const override
Definition CMesh3D.hpp:93
std::unique_ptr< Mesh > copy() const override
Create a copy of the mesh.
Definition CMesh3D.cpp:1519
void remove_elements(const std::vector< bool > &keep) override
Remove all top-dimensional elements whose mask entry is false.
Definition CMesh3D.cpp:18
void compute_elements_tag() override
compute element types, see ElementType
Definition CMesh3D.cpp:1228
Navigation3D::Index switch_edge(Navigation3D::Index idx) const override
Definition CMesh3D.hpp:104
int n_cell_vertices(const int c_id) const override
number of vertices of a cell
Definition CMesh3D.hpp:53
Navigation3D::Index switch_face(Navigation3D::Index idx) const override
Definition CMesh3D.hpp:105
void barycentric_coords(const RowVectorNd &p, const int el_id, Eigen::MatrixXd &coord) const override
constructs barycentric coodiantes for a point p.
Definition CMesh3D.cpp:1493
bool is_boundary_element(const int element_global_id) const override
is cell boundary
Definition CMesh3D.cpp:1146
int n_face_vertices(const int f_id) const override
number of vertices of a face
Definition CMesh3D.hpp:52
int cell_vertex(const int c_id, const int lv_id) const override
id of the vertex of a cell
Definition CMesh3D.hpp:56
void compute_body_ids(const std::function< int(const size_t, const std::vector< int > &, const RowVectorNd &)> &marker) override
computes boundary selections based on a function
Definition CMesh3D.cpp:1185
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).
void push_back_filled(const size_t n, const T &value)
Appends a row of n copies of value.
int row_size(const int i) const
void push_back(It begin, It end)
Appends a row.
void get_edges(Eigen::MatrixXd &p0, Eigen::MatrixXd &p1) const override
Get all the edges.
Definition Mesh3D.cpp:32
virtual std::array< int, 4 > get_ordered_vertices_from_tet(const int element_index) const
Definition Mesh3D.cpp:527
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.
std::vector< uint8_t > cell_hex
JaggedArray< uint32_t > face_vs
JaggedArray< uint32_t > face_es
JaggedArray< uint32_t > vertex_es
JaggedArray< uint32_t > face_hs
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 > edge_hs
void append(const Mesh3DStorage &other)
Appends the input topology of other (shifting its ids).
std::vector< std::array< uint32_t, 2 > > edge_vs
JaggedArray< uint32_t > cell_fs
Class to store the high-order edge nodes.
Definition Mesh.hpp:81
Class to store the high-order face nodes.
Definition Mesh.hpp:90
Format-independent input used to construct a runtime Mesh.
Definition MeshData.hpp:12
Eigen::MatrixXd vertices
Definition MeshData.hpp:21
bool has_polyhedral_topology() const
Definition MeshData.hpp:19
std::vector< std::vector< int > > faces
Optional topology for arbitrary polyhedral cells.
Definition MeshData.hpp:38
bool elements_are_ordered
Whether each element row carries a format-defined local vertex order.
Definition MeshData.hpp:25
Eigen::MatrixXi elements
Definition MeshData.hpp:22
Eigen::MatrixXd cell_kernel_points
Definition MeshData.hpp:42
std::vector< std::vector< int > > cell_faces
Definition MeshData.hpp:39
std::vector< std::vector< int > > cell_face_orientations
Definition MeshData.hpp:40
std::vector< bool > cell_is_hex
Definition MeshData.hpp:41
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
Definition Mesh.hpp:181
Eigen::MatrixXi orders_
list of geometry orders, one per cell
Definition Mesh.hpp:728
std::vector< ElementType > elements_tag_
list of element types
Definition Mesh.hpp:718
void clear_data_lost_by_refinement()
Drop the labels and the higher-order geometry before a conforming refinement.
Definition Mesh.cpp:702
bool has_boundary_ids() const
checks if surface selections are available
Definition Mesh.hpp:557
Eigen::MatrixXi in_ordered_faces_
Order of the input faces, TODO: change to std::vector of Eigen::Vector.
Definition Mesh.hpp:754
bool is_simplex(const int el_id) const
checks if element is simplex
Definition Mesh.cpp:392
std::vector< int > boundary_ids_
list of surface labels
Definition Mesh.hpp:722
virtual void apply_affine_transformation(const MatrixNd &A, const VectorNd &b)
Apply an affine transformation to the vertex positions (and to every other stored position,...
Definition Mesh.cpp:687
std::vector< CellNodes > cell_nodes_
high-order nodes associates to cells
Definition Mesh.hpp:737
std::vector< int > element_vertices(const int el_id) const
list of vids of an element
Definition Mesh.hpp:249
std::vector< int > body_ids_
list of volume labels
Definition Mesh.hpp:724
virtual int get_default_boundary_id(const int primitive) const
Get the default boundary selection of an element (face in 3d, edge in 2d)
Definition Mesh.hpp:487
void transform_higher_order_data(const MatrixNd &A, const VectorNd &b)
Definition Mesh.cpp:723
std::vector< FaceNodes > face_nodes_
high-order nodes associates to faces
Definition Mesh.hpp:735
const std::vector< ElementType > & elements_tag() const
Returns the elements types.
Definition Mesh.hpp:444
std::vector< EdgeNodes > edge_nodes_
high-order nodes associates to edges
Definition Mesh.hpp:733
std::vector< std::vector< int > > faces() const
list of sorted faces.
Definition Mesh.cpp:423
void filter_element_data(const std::vector< bool > &keep)
Definition Mesh.cpp:49
Eigen::MatrixXi in_ordered_edges_
Order of the input edges.
Definition Mesh.hpp:752
virtual void append(const Mesh &mesh)
appends a new mesh to the end of this
Definition Mesh.cpp:474
Eigen::VectorXi in_ordered_vertices_
Order of the input vertices.
Definition Mesh.hpp:750
void refine_red_refinement_tet(Mesh3DStorage &M, int iter)
void refine_catmul_clark_polar(Mesh3DStorage &M, int iter, std::vector< int > &Parents)
void prepare_mesh(Mesh3DStorage &M)
std::vector< std::array< int, 6 > > connect_cells(const std::vector< StandardCell > &cells, const int n_vertices)
Computes cell-to-cell adjacency across facets.
@ REGULAR_INTERIOR_CUBE
Triangle/tet element.
@ REGULAR_BOUNDARY_CUBE
Quad/Hex incident to more than 1 singular vertices (should not happen in 2D)
@ MULTI_SINGULAR_BOUNDARY_CUBE
Quad incident to exactly 1 singular vertex (in 2D); hex incident to exactly 1 singular interior edge,...
@ PRISM
Boundary polytope.
@ MULTI_SINGULAR_INTERIOR_CUBE
Quad/hex incident to exactly 1 singular vertex (in 2D) or edge (in 3D)
@ SIMPLE_SINGULAR_INTERIOR_CUBE
Regular quad/hex inside a 3^n patch.
@ INTERFACE_CUBE
Boundary hex that is not regular nor SimpleSingularBoundaryCube.
@ INTERIOR_POLYTOPE
Quad/hex that is at the interface with a polytope (if a cube has both external boundary and and inter...
@ SIMPLE_SINGULAR_BOUNDARY_CUBE
Boundary quad/hex, where all boundary vertices/edges are incident to at most 2 quads/hexes.
@ BOUNDARY_POLYTOPE
Interior polytope.
StandardCellType
Standard volume cell types with a fixed local facet layout.
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > VectorNd
Definition Types.hpp:11
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 3, 3 > MatrixNd
Definition Types.hpp:14
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73
A standard cell referencing global vertex indices.
std::array< int, 8 > vertices