PolyFEM
Loading...
Searching...
No Matches
NCMesh3D.cpp
Go to the documentation of this file.
4
6
7#include <igl/writeMESH.h>
8
9#include <fstream>
10
11using namespace polyfem::utils;
12
13namespace polyfem
14{
15 namespace mesh
16 {
17 void NCMesh3D::remove_elements(const std::vector<bool> &keep)
18 {
19 assert(keep.size() == n_cells());
20
21 std::vector<int> old_node_ids(vertices.size(), -1);
22 if (!node_ids_.empty())
23 for (int v = 0; v < n_vertices(); ++v)
24 old_node_ids[valid_to_all_vertex(v)] = node_ids_[v];
25 std::vector<int> old_boundary_ids(faces.size(), -1);
26 if (has_boundary_ids())
27 for (int f = 0; f < n_faces(); ++f)
28 old_boundary_ids[valid_to_all_face(f)] = boundary_ids_[f];
29
30 for (int e = 0; e < keep.size(); ++e)
31 {
32 if (keep[e])
33 continue;
34 const int full_id = valid_to_all_elem(e);
35 auto &element = elements[full_id];
36 element.is_ghost = true;
37 for (const int face : element.faces)
38 faces[face].remove_element(full_id);
39 for (const int edge : element.edges)
40 edges[edge].remove_element(full_id);
41 for (const int vertex : element.vertices)
42 vertices[vertex].remove_element(full_id);
43 --n_elements;
44 }
47
48 if (!node_ids_.empty())
49 {
50 node_ids_.resize(n_vertices());
51 for (int v = 0; v < n_vertices(); ++v)
52 node_ids_[v] = old_node_ids[valid_to_all_vertex(v)];
53 }
54 if (has_boundary_ids())
55 {
56 boundary_ids_.resize(n_faces());
57 for (int f = 0; f < n_faces(); ++f)
58 {
59 const int old_id = old_boundary_ids[valid_to_all_face(f)];
60 boundary_ids_[f] = old_id < 0 ? get_default_boundary_id(f) : old_id;
61 faces[valid_to_all_face(f)].boundary_id = boundary_ids_[f];
62 }
63 }
64 }
65
66 int NCMesh3D::face_edge(const int f_id, const int le_id) const
67 {
68 const int v0 = faces[valid_to_all_face(f_id)].vertices(le_id);
69 const int v1 = faces[valid_to_all_face(f_id)].vertices((le_id + 1) % 3);
70 const int e_id = find_edge(v0, v1);
71 return all_to_valid_edge(e_id);
72 }
73
74 void NCMesh3D::refine(const int n_refinement, const double t)
75 {
76 if (n_refinement <= 0)
77 return;
78 for (int level = 0; level < n_refinement; ++level)
79 {
81 std::vector<bool> refine_mask(elements.size(), false);
82 for (int i = 0; i < elements.size(); i++)
83 if (elements[i].is_valid())
84 refine_mask[i] = true;
85
86 for (int i = 0; i < refine_mask.size(); i++)
87 if (refine_mask[i])
89 }
90 // refine_element() invalidates the id maps: rebuild them so that the
91 // mesh can be queried right away (e.g. to apply selections).
94 }
95
97 {
98 node_ids_.clear();
99 geometry_ids_.clear();
100 if (has_body_ids())
101 {
102 body_ids_.resize(n_cells());
103 for (int e = 0; e < body_ids_.size(); ++e)
104 body_ids_[e] = elements[valid_to_all_elem(e)].body_id;
105 }
106 if (has_boundary_ids())
107 {
108 boundary_ids_.resize(n_faces());
109 for (int b = 0; b < boundary_ids_.size(); ++b)
110 boundary_ids_[b] = faces[valid_to_all_face(b)].boundary_id;
111 }
112 }
113
115 {
116 log_and_throw_error("NCMesh3D: the mesh was modified without calling prepare_mesh().");
117 }
118
119 bool NCMesh3D::is_boundary_element(const int element_global_id) const
120 {
121 // A boundary face has boundary vertices, so checking the vertices is enough.
122 for (int lv = 0; lv < n_cell_vertices(element_global_id); lv++)
123 if (is_boundary_vertex(cell_vertex(element_global_id, lv)))
124 return true;
125
126 return false;
127 }
128
130 {
131 if (data.has_polyhedral_topology())
132 return false;
133 const Eigen::MatrixXd &V = data.vertices;
134 const Eigen::MatrixXi &F = data.elements;
135 if (F.cols() != 4 || F.minCoeff() < 0)
136 return false;
137 n_elements = 0;
138 elements.clear();
139 vertices.clear();
140 edges.clear();
141 midpointMap.clear();
142 edgeMap.clear();
143 faceMap.clear();
144 refineHistory.clear();
145 index_prepared = false;
146 adj_prepared = false;
147
148 vertices.reserve(V.rows());
149 for (int i = 0; i < V.rows(); i++)
150 {
151 vertices.emplace_back(V.row(i));
152 }
153 for (int i = 0; i < F.rows(); i++)
154 {
155 add_element(F.row(i), -1);
156 }
157
158 prepare_mesh();
159
160 return true;
161 }
162
163 void NCMesh3D::attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector<std::vector<int>> &nodes)
164 {
165 for (int f = 0; f < n_cells(); ++f)
166 if (nodes[f].size() != 4)
167 throw std::runtime_error("NCMesh doesn't support high order mesh!");
168 }
169
171 {
172 polyfem::RowVectorNd min, max;
173 bounding_box(min, max);
174
175 auto extent = max - min;
176 double scale = extent.maxCoeff();
177
178 for (auto &v : vertices)
179 v.pos = (v.pos - min.transpose()) / scale;
180 }
181
190 double NCMesh3D::edge_length(const int gid) const
191 {
192 const int v1 = edge_vertex(gid, 0);
193 const int v2 = edge_vertex(gid, 1);
194
195 return (point(v1) - point(v2)).norm();
196 }
197
198 RowVectorNd NCMesh3D::point(const int global_index) const
199 {
200 return vertices[valid_to_all_vertex(global_index)].pos.transpose();
201 }
202
203 void NCMesh3D::set_point(const int global_index, const RowVectorNd &p)
204 {
205 vertices[valid_to_all_vertex(global_index)].pos = p;
206 }
207
209 {
210 const int v1 = edge_vertex(e, 0);
211 const int v2 = edge_vertex(e, 1);
212
213 return 0.5 * (point(v1) + point(v2));
214 }
216 {
217 const int v1 = face_vertex(f, 0);
218 const int v2 = face_vertex(f, 1);
219 const int v3 = face_vertex(f, 2);
220
221 return (point(v1) + point(v2) + point(v3)) / 3.;
222 }
224 {
225 RowVectorNd bary = RowVectorNd::Zero(3);
226 for (int lv = 0; lv < n_cell_vertices(c); ++lv)
227 bary += point(cell_vertex(c, lv));
228 return bary / n_cell_vertices(c);
229 }
230
232 {
233 min = vertices[0].pos;
234 max = vertices[0].pos;
235
236 for (const auto &v : vertices)
237 {
238 for (int d = 0; d < 3; d++)
239 {
240 if (v.pos[d] > max[d])
241 max[d] = v.pos[d];
242 if (v.pos[d] < min[d])
243 min[d] = v.pos[d];
244 }
245 }
246 }
247
248 void NCMesh3D::compute_boundary_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &, bool)> &marker)
249 {
250 boundary_ids_.resize(n_faces());
251 std::fill(boundary_ids_.begin(), boundary_ids_.end(), -1);
252
253 for (int f = 0; f < n_faces(); ++f)
254 {
255 const bool is_boundary = is_boundary_face(f);
256 std::vector<int> vs(n_face_vertices(f));
257 const auto p = face_barycenter(f);
258
259 for (int vid = 0; vid < vs.size(); ++vid)
260 vs[vid] = face_vertex(f, vid);
261
262 std::sort(vs.begin(), vs.end());
263 boundary_ids_[f] = marker(f, vs, p, is_boundary);
264
265 faces[valid_to_all_face(f)].boundary_id = boundary_ids_[f];
266 }
267 }
268
269 void NCMesh3D::compute_body_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &)> &marker)
270 {
271 body_ids_.resize(n_cells());
272 std::fill(body_ids_.begin(), body_ids_.end(), -1);
273
274 for (int e = 0; e < n_cells(); ++e)
275 {
276 const auto bary = cell_barycenter(e);
277 body_ids_[e] = marker(e, element_vertices(e), bary);
278 elements[valid_to_all_elem(e)].body_id = body_ids_[e];
279 }
280 }
281 void NCMesh3D::set_boundary_ids(const std::vector<int> &boundary_ids)
282 {
283 assert(boundary_ids.size() == n_faces());
284 Mesh::set_boundary_ids(boundary_ids);
285 for (int i = 0; i < boundary_ids.size(); i++)
286 {
287 faces[valid_to_all_face(i)].boundary_id = boundary_ids[i];
288 }
289 }
290 void NCMesh3D::set_body_ids(const std::vector<int> &body_ids)
291 {
292 assert(body_ids.size() == n_cells());
293 Mesh::set_body_ids(body_ids);
294 for (int i = 0; i < body_ids.size(); i++)
295 {
296 elements[valid_to_all_elem(i)].body_id = body_ids[i];
297 }
298 }
299
300 // void NCMesh3D::triangulate_faces(Eigen::MatrixXi &tris, Eigen::MatrixXd &pts, std::vector<int> &ranges) const
301 // {
302 // ranges.clear();
303
304 // std::vector<Eigen::MatrixXi> local_tris(n_cells());
305 // std::vector<Eigen::MatrixXd> local_pts(n_cells());
306 // Eigen::MatrixXi tets;
307
308 // int total_tris = 0;
309 // int total_pts = 0;
310
311 // ranges.push_back(0);
312
313 // Eigen::MatrixXd face_barys;
314 // face_barycenters(face_barys);
315
316 // Eigen::MatrixXd cell_barys;
317 // cell_barycenters(cell_barys);
318
319 // for (std::size_t e = 0; e < n_cells(); ++e)
320 // {
321 // const int n_vertices = n_cell_vertices(e);
322 // const int n_faces = n_cell_faces(e);
323
324 // Eigen::MatrixXd local_pt(n_vertices + n_faces, 3);
325
326 // std::map<int, int> global_to_local;
327
328 // for (int i = 0; i < n_vertices; ++i)
329 // {
330 // const int global_index = cell_vertex(e, i);
331 // local_pt.row(i) = point(global_index);
332 // global_to_local[global_index] = i;
333 // }
334
335 // int n_local_faces = 0;
336 // for (int i = 0; i < n_faces; ++i)
337 // {
338 // const int f_id = cell_face(e, i);
339 // n_local_faces += n_face_vertices(f_id);
340
341 // local_pt.row(n_vertices + i) = face_barys.row(f_id);
342 // }
343
344 // Eigen::MatrixXi local_faces(n_local_faces, 3);
345
346 // int face_index = 0;
347 // for (int i = 0; i < n_faces; ++i)
348 // {
349 // const int f_id = cell_face(e, i);
350
351 // const Eigen::RowVector3d e0 = (point(face_vertex(f_id, 0)) - local_pt.row(n_vertices + i));
352 // const Eigen::RowVector3d e1 = (point(face_vertex(f_id, 1)) - local_pt.row(n_vertices + i));
353 // const Eigen::RowVector3d normal = e0.cross(e1);
354 // // const Eigen::RowVector3d check_dir = (node_from_element(e)-p);
355 // const Eigen::RowVector3d check_dir = (cell_barys.row(e) - point(face_vertex(f_id, 1)));
356
357 // const bool reverse_order = normal.dot(check_dir) > 0;
358
359 // for (int j = 0; j < n_face_vertices(f_id); ++j)
360 // {
361 // const int jp = (j + 1) % n_face_vertices(f_id);
362 // if (reverse_order)
363 // {
364 // local_faces(face_index, 0) = global_to_local[face_vertex(f_id, jp)];
365 // local_faces(face_index, 1) = global_to_local[face_vertex(f_id, j)];
366 // }
367 // else
368 // {
369 // local_faces(face_index, 0) = global_to_local[face_vertex(f_id, j)];
370 // local_faces(face_index, 1) = global_to_local[face_vertex(f_id, jp)];
371 // }
372 // local_faces(face_index, 2) = n_vertices + i;
373
374 // ++face_index;
375 // }
376 // }
377
378 // local_pts[e] = local_pt;
379 // local_tris[e] = local_faces;
380
381 // total_tris += local_tris[e].rows();
382 // total_pts += local_pts[e].rows();
383
384 // ranges.push_back(total_tris);
385
386 // assert(local_pts[e].rows() == local_pt.rows());
387 // }
388
389 // tris.resize(total_tris, 3);
390 // pts.resize(total_pts, 3);
391
392 // int tri_index = 0;
393 // int pts_index = 0;
394 // for (std::size_t i = 0; i < local_tris.size(); ++i)
395 // {
396 // tris.block(tri_index, 0, local_tris[i].rows(), local_tris[i].cols()) = local_tris[i].array() + pts_index;
397 // tri_index += local_tris[i].rows();
398
399 // pts.block(pts_index, 0, local_pts[i].rows(), local_pts[i].cols()) = local_pts[i];
400 // pts_index += local_pts[i].rows();
401 // }
402 // }
403
404 RowVectorNd NCMesh3D::kernel(const int cell_id) const
405 {
406 assert(false);
407 return RowVectorNd();
408 }
410 {
412
413 idx.element = hi;
414
415 idx.element_patch = lf;
416 idx.face = cell_face(hi, lf);
417
418 if (lv >= 4)
419 lv = lv % 4;
420 idx.face_corner = lv;
421 idx.vertex = face_vertex(idx.face, idx.face_corner);
422
423 idx.edge = face_edge(idx.face, idx.face_corner);
424
425 return idx;
426 }
428 {
430
431 idx.element = hi;
432 idx.element_patch = 0;
433 idx.face = cell_face(hi, 0);
434 idx.face_corner = 0;
435 idx.vertex = face_vertex(idx.face, idx.face_corner);
436 idx.edge = face_edge(idx.face, 0);
437
438 return idx;
439 }
440
442 {
444
445 idx.element = hi;
446 idx.vertex = v0;
448
449 for (int i = 0; i < 4; i++)
450 {
451 const int f_id = cell_face(idx.element, i);
452 for (int j = 0; j < 3; j++)
453 {
454 const int e_id = face_edge(f_id, j);
455 if (idx.edge == e_id)
456 {
457 idx.element_patch = i;
458 idx.face = f_id;
459
460 for (int d = 0; d < n_face_vertices(f_id); d++)
461 if (face_vertex(f_id, d) == idx.vertex)
462 idx.face_corner = d;
463
464 assert(switch_vertex(idx).vertex == v1);
465
466 return idx;
467 }
468 }
469 }
470
471 assert(false);
472 return idx;
473 }
474 Navigation3D::Index NCMesh3D::get_index_from_element_face(int hi, int v0, int v1, int v2) const
475 {
477
478 idx.element = hi;
479 idx.vertex = v0;
481
482 for (int d = 0; d < n_face_vertices(idx.face); d++)
483 if (face_vertex(idx.face, d) == idx.vertex)
484 idx.face_corner = d;
485
486 int v0_ = v0, v1_ = v1;
487
488 // let v0_ < v1_
489 if (v0_ > v1_)
490 std::swap(v0_, v1_);
491
492 for (int i = 0; i < 4; i++)
493 {
494 const int fid = cell_face(idx.element, i);
495 if (fid != idx.face)
496 continue;
497
498 idx.element_patch = i;
499
500 for (int j = 0; j < 3; j++)
501 {
502 const int eid = face_edge(fid, j);
503 const int ev0 = edge_vertex(eid, 0);
504 const int ev1 = edge_vertex(eid, 1);
505
506 if ((ev0 == v0_ && ev1 == v1_) || (ev0 == v1_ && ev1 == v0_))
507 {
508 idx.edge = eid;
509
510 assert(switch_vertex(idx).vertex == v1);
511 assert(switch_vertex(switch_edge(idx)).vertex == v2);
512
513 return idx;
514 }
515 }
516 }
517
518 assert(false);
519 return idx;
520 }
521
522 std::vector<uint32_t> NCMesh3D::vertex_neighs(const int v_gid) const
523 {
524 std::vector<uint32_t> hs;
525 for (auto h : vertices[valid_to_all_vertex(v_gid)].elem_list)
526 hs.push_back(all_to_valid_elem(h));
527
528 return hs;
529 }
530 std::vector<uint32_t> NCMesh3D::edge_neighs(const int e_gid) const
531 {
532 std::vector<uint32_t> hs;
533 for (auto h : edges[valid_to_all_edge(e_gid)].elem_list)
534 hs.push_back(all_to_valid_elem(h));
535
536 return hs;
537 }
538
540 {
541 if (idx.vertex == edge_vertex(idx.edge, 0))
542 idx.vertex = edge_vertex(idx.edge, 1);
543 else
544 idx.vertex = edge_vertex(idx.edge, 0);
545
546 for (int d = 0; d < n_face_vertices(idx.face); d++)
547 if (face_vertex(idx.face, d) == idx.vertex)
548 idx.face_corner = d;
549
550 return idx;
551 }
553 {
554 if (idx.edge == face_edge(idx.face, idx.face_corner))
555 idx.edge = face_edge(idx.face, (idx.face_corner + 2) % 3);
556 else
557 idx.edge = face_edge(idx.face, idx.face_corner);
558 return idx;
559 }
561 {
562 for (int i = 0; i < 4; i++)
563 {
564 const int fid = cell_face(idx.element, i);
565 if (fid == idx.face)
566 continue;
567 if (idx.edge == face_edge(fid, 0) || idx.edge == face_edge(fid, 1) || idx.edge == face_edge(fid, 2))
568 {
569 idx.face = fid;
570 idx.element_patch = i;
571
572 for (int d = 0; d < n_face_vertices(fid); d++)
573 if (face_vertex(fid, d) == idx.vertex)
574 idx.face_corner = d;
575
576 break;
577 }
578 }
579 return idx;
580 }
582 {
583 const auto &face = faces[valid_to_all_face(idx.face)];
584 if (face.n_elem() != 2)
585 {
586 idx.element = -1;
587 return idx;
588 }
589 if (all_to_valid_elem(face.get_element()) == idx.element)
590 idx.element = all_to_valid_elem(face.find_opposite_element(face.get_element()));
591 else
592 idx.element = all_to_valid_elem(face.get_element());
593
594 for (int f = 0; f < n_cell_faces(idx.element); f++)
595 if (cell_face(idx.element, f) == idx.face)
596 idx.element_patch = f;
597 return idx;
598 }
599
608
609 void NCMesh3D::get_vertex_elements_neighs(const int v_id, std::vector<int> &ids) const
610 {
611 ids.clear();
612 for (auto h : vertices[valid_to_all_vertex(v_id)].elem_list)
613 ids.push_back(all_to_valid_elem(h));
614 }
615 void NCMesh3D::get_edge_elements_neighs(const int e_id, std::vector<int> &ids) const
616 {
617 ids.clear();
618 for (auto h : edges[valid_to_all_edge(e_id)].elem_list)
619 ids.push_back(all_to_valid_elem(h));
620 }
621 void NCMesh3D::get_face_elements_neighs(const int f_id, std::vector<int> &ids) const
622 {
623 ids.clear();
624 for (auto h : faces[valid_to_all_face(f_id)].elem_list)
625 ids.push_back(all_to_valid_elem(h));
626 }
627
628 void NCMesh3D::refine_element(int id_full)
629 {
630 if (elements[id_full].is_not_valid())
631 throw std::runtime_error("Cannot refine an invalid element!");
632
633 const auto v = elements[id_full].vertices;
634 elements[id_full].is_refined = true;
635 n_elements--;
636
637 for (int f = 0; f < elements[id_full].faces.size(); f++)
638 faces[elements[id_full].faces(f)].remove_element(id_full);
639
640 for (int e = 0; e < elements[id_full].edges.size(); e++)
641 edges[elements[id_full].edges(e)].remove_element(id_full);
642
643 for (int i = 0; i < v.size(); i++)
644 vertices[v(i)].remove_element(id_full);
645
646 if (elements[id_full].children(0) >= 0)
647 {
648 for (int c = 0; c < elements[id_full].children.size(); c++)
649 {
650 const int child_id = elements[id_full].children(c);
651 auto &elem = elements[child_id];
652 elem.is_ghost = false;
653 n_elements++;
654
655 for (int f = 0; f < elem.faces.size(); f++)
656 faces[elem.faces(f)].add_element(child_id);
657
658 for (int e = 0; e < elem.edges.size(); e++)
659 edges[elem.edges(e)].add_element(child_id);
660
661 for (int v = 0; v < elem.vertices.size(); v++)
662 vertices[elem.vertices(v)].add_element(child_id);
663 }
664 }
665 else
666 {
667 // create mid-points if not exist
668 const int v1 = v[0];
669 const int v2 = v[1];
670 const int v3 = v[2];
671 const int v4 = v[3];
672 const int v5 = get_vertex(Eigen::Vector2i(v1, v2));
673 const int v8 = get_vertex(Eigen::Vector2i(v3, v2));
674 const int v6 = get_vertex(Eigen::Vector2i(v1, v3));
675 const int v7 = get_vertex(Eigen::Vector2i(v1, v4));
676 const int v9 = get_vertex(Eigen::Vector2i(v4, v2));
677 const int v10 = get_vertex(Eigen::Vector2i(v3, v4));
678
679 // inherite line singularity flag from parent edge
680 for (int i = 0; i < v.size(); i++)
681 for (int j = 0; j < i; j++)
682 {
683 int mid_id = find_vertex(v[i], v[j]);
684 int edge_id = find_edge(v[i], v[j]);
685 int edge1 = get_edge(v[i], mid_id);
686 int edge2 = get_edge(v[j], mid_id);
687 edges[edge1].boundary_id = edges[edge_id].boundary_id;
688 edges[edge2].boundary_id = edges[edge_id].boundary_id;
689 }
690
691 for (int i = 0; i < v.size(); i++)
692 for (int j = 0; j < i; j++)
693 for (int k = 0; k < j; k++)
694 {
695 int fid = find_face(v[i], v[j], v[k]);
696 int vij = find_vertex(v[i], v[j]);
697 int vjk = find_vertex(v[j], v[k]);
698 int vik = find_vertex(v[i], v[k]);
699 int facei = get_face(v[i], vij, vik);
700 int facej = get_face(v[j], vjk, vij);
701 int facek = get_face(v[k], vjk, vik);
702 int facem = get_face(vij, vjk, vik);
703 faces[facei].boundary_id = faces[fid].boundary_id;
704 faces[facej].boundary_id = faces[fid].boundary_id;
705 faces[facek].boundary_id = faces[fid].boundary_id;
706 faces[facem].boundary_id = faces[fid].boundary_id;
707 }
708
709 // create children
710 elements[id_full].children(0) = elements.size();
711 add_element(Eigen::Vector4i(v1, v5, v6, v7), id_full);
712 elements[id_full].children(1) = elements.size();
713 add_element(Eigen::Vector4i(v5, v2, v8, v9), id_full);
714 elements[id_full].children(2) = elements.size();
715 add_element(Eigen::Vector4i(v6, v8, v3, v10), id_full);
716 elements[id_full].children(3) = elements.size();
717 add_element(Eigen::Vector4i(v7, v9, v10, v4), id_full);
718 elements[id_full].children(4) = elements.size();
719 add_element(Eigen::Vector4i(v5, v6, v7, v9), id_full);
720 elements[id_full].children(5) = elements.size();
721 add_element(Eigen::Vector4i(v5, v9, v8, v6), id_full);
722 elements[id_full].children(6) = elements.size();
723 add_element(Eigen::Vector4i(v6, v7, v9, v10), id_full);
724 elements[id_full].children(7) = elements.size();
725 add_element(Eigen::Vector4i(v6, v10, v9, v8), id_full);
726 }
727
728 refineHistory.push_back(id_full);
729
730 index_prepared = false;
731 adj_prepared = false;
732 }
733 void NCMesh3D::refine_elements(const std::vector<int> &ids)
734 {
735 std::vector<int> full_ids(ids.size());
736 for (int i = 0; i < ids.size(); i++)
737 full_ids[i] = valid_to_all_elem(ids[i]);
738
739 for (int i : full_ids)
741
742 prepare_mesh();
744 }
745
747 {
748 const int parent_id = elements[id_full].parent;
749 if (parent_id < 0)
750 log_and_throw_error("NCMesh3D: cannot coarsen element {}, it has no parent.", id_full);
751 auto &parent = elements[parent_id];
752
753 for (int i = 0; i < parent.children.size(); i++)
754 if (elements[parent.children(i)].is_not_valid())
755 throw std::runtime_error("Invalid siblings in coarsening!");
756
757 // remove elements
758 for (int c = 0; c < parent.children.size(); c++)
759 {
760 auto &elem = elements[parent.children(c)];
761 elem.is_ghost = true;
762 n_elements--;
763
764 for (int f = 0; f < elem.faces.size(); f++)
765 faces[elem.faces(f)].remove_element(parent.children(c));
766
767 for (int e = 0; e < elem.edges.size(); e++)
768 edges[elem.edges(e)].remove_element(parent.children(c));
769
770 for (int v = 0; v < elem.vertices.size(); v++)
771 vertices[elem.vertices(v)].remove_element(parent.children(c));
772 }
773
774 // add element
775 parent.is_refined = false;
776 n_elements++;
777
778 for (int f = 0; f < parent.faces.size(); f++)
779 faces[parent.faces(f)].add_element(parent_id);
780
781 for (int e = 0; e < parent.edges.size(); e++)
782 edges[parent.edges(e)].add_element(parent_id);
783
784 for (int v = 0; v < parent.vertices.size(); v++)
785 vertices[parent.vertices(v)].add_element(parent_id);
786
787 refineHistory.push_back(parent_id);
788
789 index_prepared = false;
790 adj_prepared = false;
791 }
792
794 {
795 for (auto &face : faces)
796 {
797 if (face.n_elem() == 1)
798 face.isboundary = true;
799 else
800 face.isboundary = false;
801 }
802
803 for (auto &face : faces)
804 {
805 if (face.leader >= 0 && face.n_elem() > 0 && faces[face.leader].n_elem() > 0)
806 {
807 face.isboundary = false;
808 faces[face.leader].isboundary = false;
809 }
810 }
811
812 for (auto &vert : vertices)
813 vert.isboundary = false;
814
815 for (auto &edge : edges)
816 edge.isboundary = false;
817
818 for (auto &face : faces)
819 {
820 if (face.isboundary && face.n_elem())
821 {
822 for (int j = 0; j < 3; j++)
823 vertices[face.vertices(j)].isboundary = true;
824
825 for (int j = 0; j < 3; j++)
826 for (int i = 0; i < j; i++)
827 edges[find_edge(face.vertices(i), face.vertices(j))].isboundary = true;
828 }
829 }
830 }
831
833 {
834 // Count the valid entities from the topology (not from the maps being rebuilt).
835 index_prepared = false;
836 all_to_valid_elemMap.assign(elements.size(), -1);
838
839 for (int i = 0, e = 0; i < elements.size(); i++)
840 {
841 if (elements[i].is_not_valid())
842 continue;
845 e++;
846 }
847
848 const int n_verts = n_vertices();
849
850 all_to_valid_vertexMap.assign(vertices.size(), -1);
851 valid_to_all_vertexMap.resize(n_verts);
852
853 for (int i = 0, j = 0; i < vertices.size(); i++)
854 {
855 if (vertices[i].n_elem() == 0)
856 continue;
859 j++;
860 }
861
862 all_to_valid_edgeMap.assign(edges.size(), -1);
864
865 for (int i = 0, j = 0; i < edges.size(); i++)
866 {
867 if (edges[i].n_elem() == 0)
868 continue;
871 j++;
872 }
873
874 all_to_valid_faceMap.assign(faces.size(), -1);
876
877 for (int i = 0, j = 0; i < faces.size(); i++)
878 {
879 if (faces[i].n_elem() == 0)
880 continue;
883 j++;
884 }
885 index_prepared = true;
886 }
887
888 void NCMesh3D::append(const Mesh &mesh)
889 {
890 assert(typeid(mesh) == typeid(NCMesh3D));
891 const NCMesh3D &other = dynamic_cast<const NCMesh3D &>(mesh);
892 const bool had_boundary_ids = has_boundary_ids(), other_has_boundary_ids = other.has_boundary_ids();
893 const bool had_body_ids = has_body_ids(), other_has_body_ids = other.has_body_ids();
894
895 Mesh::append(mesh);
896
897 // Merge the whole structure of the other mesh (refinement hierarchy,
898 // hanging nodes and per-entity labels included), offsetting its ids.
899 const int v_off = vertices.size(), e_off = edges.size(), f_off = faces.size(), el_off = elements.size();
900 const auto offset = [](Eigen::VectorXi ids, const int off) {
901 for (int i = 0; i < ids.size(); ++i)
902 if (ids[i] >= 0)
903 ids[i] += off;
904 return ids;
905 };
906 const auto offset_set = [](const SortedIds &ids, const int off) {
907 SortedIds result;
908 for (const int id : ids)
909 result.insert(id + off);
910 return result;
911 };
912 const auto copy_boundaries = [&](const std::vector<ncBoundary> &from, std::vector<ncBoundary> &to) {
913 to.reserve(to.size() + from.size());
914 for (const ncBoundary &b : from)
915 {
916 ncBoundary copy = b;
917 copy.vertices = offset(b.vertices, v_off);
918 copy.elem_list = offset_set(b.elem_list, el_off);
919 copy.global_ids.clear();
920 to.push_back(std::move(copy)); // leaders/followers are rebuilt by prepare_mesh()
921 }
922 };
923
924 vertices.reserve(v_off + other.vertices.size());
925 for (const ncVert &v : other.vertices)
926 {
927 vertices.push_back(v); // edge/face/weight are rebuilt by prepare_mesh()
928 vertices.back().elem_list = offset_set(v.elem_list, el_off);
929 }
930 copy_boundaries(other.edges, edges);
931 copy_boundaries(other.faces, faces);
932
933 elements.reserve(el_off + other.elements.size());
934 for (const ncElem &el : other.elements)
935 {
936 ncElem copy = el;
937 copy.geom_vertices = offset(el.geom_vertices, v_off);
938 copy.vertices = offset(el.vertices, v_off);
939 copy.edges = offset(el.edges, e_off);
940 copy.faces = offset(el.faces, f_off);
941 copy.children = offset(el.children, el_off);
942 if (copy.parent >= 0)
943 copy.parent += el_off;
944 elements.push_back(std::move(copy));
945 }
946
947 for (const auto &[key, value] : other.midpointMap)
948 midpointMap[Eigen::Vector2i(key.array() + v_off)] = value + v_off;
949 for (const auto &[key, value] : other.edgeMap)
950 edgeMap[Eigen::Vector2i(key.array() + v_off)] = value + e_off;
951 for (const auto &[key, value] : other.faceMap)
952 faceMap[Eigen::Vector3i(key.array() + v_off)] = value + f_off;
953 for (const int id : other.refineHistory)
954 refineHistory.push_back(id + el_off);
955 n_elements += other.n_elements;
956
957 index_prepared = false;
958 adj_prepared = false;
959 prepare_mesh();
960
961 // The per-entity labels are authoritative; entities of a mesh that had
962 // no labels get the defaults, and the base-class arrays mirror them.
963 if (had_boundary_ids || other_has_boundary_ids)
964 {
965 boundary_ids_.resize(n_faces());
966 for (int f = 0; f < n_faces(); ++f)
967 {
969 const bool labeled = valid_to_all_face(f) < f_off ? had_boundary_ids : other_has_boundary_ids;
970 if (!labeled)
972 boundary_ids_[f] = face.boundary_id;
973 }
974 }
975 if (had_body_ids || other_has_body_ids)
976 {
977 body_ids_.resize(n_cells());
978 for (int c = 0; c < n_cells(); ++c)
979 {
980 ncElem &element = elements[valid_to_all_elem(c)];
981 const bool labeled = valid_to_all_elem(c) < el_off ? had_body_ids : other_has_body_ids;
982 if (!labeled)
983 element.body_id = 0;
984 body_ids_[c] = element.body_id;
985 }
986 }
987 }
988
989 std::unique_ptr<Mesh> NCMesh3D::copy() const
990 {
991 return std::make_unique<NCMesh3D>(*this);
992 }
993
994 int NCMesh3D::find_vertex(Eigen::Vector2i v) const
995 {
996 std::sort(v.data(), v.data() + v.size());
997 auto search = midpointMap.find(v);
998 if (search != midpointMap.end())
999 return search->second;
1000 else
1001 return -1;
1002 }
1003 int NCMesh3D::get_vertex(Eigen::Vector2i v)
1004 {
1005 std::sort(v.data(), v.data() + v.size());
1006 int id = find_vertex(v);
1007 if (id < 0)
1008 {
1009 Eigen::VectorXd v_mid = (vertices[v[0]].pos + vertices[v[1]].pos) / 2.;
1010 id = vertices.size();
1011 vertices.emplace_back(v_mid);
1012 midpointMap.emplace(v, id);
1013 }
1014 return id;
1015 }
1016 int NCMesh3D::find_edge(Eigen::Vector2i v) const
1017 {
1018 std::sort(v.data(), v.data() + v.size());
1019 auto search = edgeMap.find(v);
1020 if (search != edgeMap.end())
1021 return search->second;
1022 else
1023 return -1;
1024 }
1025 int NCMesh3D::get_edge(Eigen::Vector2i v)
1026 {
1027 std::sort(v.data(), v.data() + v.size());
1028 int id = find_edge(v);
1029 if (id < 0)
1030 {
1031 edges.emplace_back(v);
1032 id = edges.size() - 1;
1033 edgeMap.emplace(v, id);
1034 }
1035 return id;
1036 }
1037 int NCMesh3D::find_face(Eigen::Vector3i v) const
1038 {
1039 std::sort(v.data(), v.data() + v.size());
1040 auto search = faceMap.find(v);
1041 if (search != faceMap.end())
1042 return search->second;
1043 else
1044 return -1;
1045 }
1046 int NCMesh3D::get_face(Eigen::Vector3i v)
1047 {
1048 std::sort(v.data(), v.data() + v.size());
1049 int id = find_face(v);
1050 if (id < 0)
1051 {
1052 faces.emplace_back(v);
1053 id = faces.size() - 1;
1054 faceMap.emplace(v, id);
1055 }
1056 return id;
1057 }
1058 void NCMesh3D::traverse_edge(Eigen::Vector2i v, double p1, double p2, int depth, std::vector<follower_edge> &list) const
1059 {
1060 int v_mid = find_vertex(v);
1061 std::vector<follower_edge> list1, list2;
1062 if (v_mid >= 0)
1063 {
1064 double p_mid = (p1 + p2) / 2;
1065 traverse_edge(Eigen::Vector2i(v[0], v_mid), p1, p_mid, depth + 1, list1);
1066 list.insert(
1067 list.end(),
1068 std::make_move_iterator(list1.begin()),
1069 std::make_move_iterator(list1.end()));
1070 traverse_edge(Eigen::Vector2i(v_mid, v[1]), p_mid, p2, depth + 1, list2);
1071 list.insert(
1072 list.end(),
1073 std::make_move_iterator(list2.begin()),
1074 std::make_move_iterator(list2.end()));
1075 }
1076 if (depth > 0)
1077 {
1078 int follower_id = find_edge(v);
1079 if (follower_id >= 0 && edges[follower_id].n_elem() > 0)
1080 list.emplace_back(follower_id, p1, p2);
1081 }
1082 }
1084 {
1085 for (auto &edge : edges)
1086 {
1087 edge.leader = -1;
1088 edge.followers.clear();
1089 edge.weights.setConstant(-1);
1090 }
1091
1092 for (int e_id = 0; e_id < edges.size(); e_id++)
1093 {
1094 auto &edge = edges[e_id];
1095 if (edge.n_elem() == 0)
1096 continue;
1097 std::vector<follower_edge> followers;
1098 traverse_edge(edge.vertices, 0, 1, 0, followers);
1099 for (auto &s : followers)
1100 {
1101 if (edges[s.id].leader >= 0 && std::abs(edges[s.id].weights(1) - edges[s.id].weights(0)) < std::abs(s.p2 - s.p1))
1102 continue;
1103 edge.followers.push_back(s.id);
1104 edges[s.id].leader = e_id;
1105 edges[s.id].weights << s.p1, s.p2;
1106 }
1107 }
1108
1109 // In 3d, it's possible for one edge to have both leader and follower edges, but we don't care this case.
1110 for (auto &edge : edges)
1111 if (edge.leader >= 0 && edge.followers.size())
1112 edge.followers.clear();
1113 }
1114 void NCMesh3D::traverse_face(int v1, int v2, int v3, Eigen::Vector2d p1, Eigen::Vector2d p2, Eigen::Vector2d p3, int depth, std::vector<follower_face> &face_list, std::vector<int> &edge_list) const
1115 {
1116 int v12 = find_vertex(Eigen::Vector2i(v1, v2));
1117 int v23 = find_vertex(Eigen::Vector2i(v3, v2));
1118 int v31 = find_vertex(Eigen::Vector2i(v1, v3));
1119 std::vector<follower_face> list1, list2, list3, list4;
1120 std::vector<int> list1_, list2_, list3_, list4_;
1121 if (depth > 0)
1122 {
1123 edge_list.push_back(find_edge(v1, v2));
1124 edge_list.push_back(find_edge(v3, v2));
1125 edge_list.push_back(find_edge(v1, v3));
1126 }
1127 if (v12 >= 0 && v23 >= 0 && v31 >= 0)
1128 {
1129 auto p12 = (p1 + p2) / 2, p23 = (p2 + p3) / 2, p31 = (p1 + p3) / 2;
1130 traverse_face(v1, v12, v31, p1, p12, p31, depth + 1, list1, list1_);
1131 traverse_face(v12, v2, v23, p12, p2, p23, depth + 1, list2, list2_);
1132 traverse_face(v31, v23, v3, p31, p23, p3, depth + 1, list3, list3_);
1133 traverse_face(v12, v23, v31, p12, p23, p31, depth + 1, list4, list4_);
1134
1135 face_list.insert(face_list.end(), std::make_move_iterator(list1.begin()), std::make_move_iterator(list1.end()));
1136 face_list.insert(face_list.end(), std::make_move_iterator(list2.begin()), std::make_move_iterator(list2.end()));
1137 face_list.insert(face_list.end(), std::make_move_iterator(list3.begin()), std::make_move_iterator(list3.end()));
1138 face_list.insert(face_list.end(), std::make_move_iterator(list4.begin()), std::make_move_iterator(list4.end()));
1139
1140 edge_list.insert(edge_list.end(), std::make_move_iterator(list1_.begin()), std::make_move_iterator(list1_.end()));
1141 edge_list.insert(edge_list.end(), std::make_move_iterator(list2_.begin()), std::make_move_iterator(list2_.end()));
1142 edge_list.insert(edge_list.end(), std::make_move_iterator(list3_.begin()), std::make_move_iterator(list3_.end()));
1143 edge_list.insert(edge_list.end(), std::make_move_iterator(list4_.begin()), std::make_move_iterator(list4_.end()));
1144 }
1145 if (depth > 0)
1146 {
1147 int follower_id = find_face(Eigen::Vector3i(v1, v2, v3));
1148 if (follower_id >= 0 && faces[follower_id].n_elem() > 0)
1149 face_list.emplace_back(follower_id, p1, p2, p3);
1150 }
1151 }
1153 {
1154 Eigen::Matrix<int, 4, 3> fv;
1155 fv.row(0) << 0, 1, 2;
1156 fv.row(1) << 0, 1, 3;
1157 fv.row(2) << 1, 2, 3;
1158 fv.row(3) << 2, 0, 3;
1159
1160 for (auto &face : faces)
1161 {
1162 face.leader = -1;
1163 face.followers.clear();
1164 }
1165
1166 for (auto &edge : edges)
1167 {
1168 edge.leader_face = -1;
1169 }
1170
1171 for (int f_id = 0; f_id < faces.size(); f_id++)
1172 {
1173 auto &face = faces[f_id];
1174 if (face.n_elem() == 0)
1175 continue;
1176 std::vector<follower_face> followers;
1177 std::vector<int> interior_edges;
1178 traverse_face(face.vertices(0), face.vertices(1), face.vertices(2), Eigen::Vector2d(0, 0), Eigen::Vector2d(1, 0), Eigen::Vector2d(0, 1), 0, followers, interior_edges); // order is important
1179 for (auto &s : followers)
1180 {
1181 faces[s.id].leader = f_id;
1182 face.followers.push_back(s.id);
1183 }
1184 for (int s : interior_edges)
1185 if (s >= 0 && edges[s].leader < 0 && edges[s].n_elem() > 0)
1186 edges[s].leader_face = f_id;
1187 }
1188 }
1190 {
1191 for (auto &vert : vertices)
1192 {
1193 vert.edge = -1;
1194 vert.face = -1;
1195 }
1196
1197 for (auto &small_edge : edges)
1198 {
1199 // invalid edges
1200 if (small_edge.n_elem() == 0)
1201 continue;
1202
1203 // not follower edges
1204 int large_edge = small_edge.leader;
1205 if (large_edge < 0)
1206 continue;
1207 assert(edges[large_edge].leader < 0);
1208
1209 // follower edges
1210 for (int j = 0; j < 2; j++)
1211 {
1212 const int v_id = small_edge.vertices(j);
1213 // hanging nodes
1214 if (v_id != edges[large_edge].vertices(0) && v_id != edges[large_edge].vertices(1))
1215 vertices[v_id].edge = large_edge;
1216 }
1217 }
1218
1219 for (auto &small_face : faces)
1220 {
1221 // invalid faces
1222 if (small_face.n_elem() == 0)
1223 continue;
1224
1225 // not follower faces
1226 int large_face = small_face.leader;
1227 if (large_face < 0)
1228 continue;
1229
1230 // follower faces
1231 for (int j = 0; j < 3; j++)
1232 {
1233 const int v_id = small_face.vertices(j);
1234 // hanging nodes
1235 if (v_id != faces[large_face].vertices(0) && v_id != faces[large_face].vertices(1) && v_id != faces[large_face].vertices(2))
1236 vertices[v_id].face = large_face;
1237 }
1238 }
1239 }
1240 std::array<int, 4> NCMesh3D::get_ordered_vertices_from_tet(const int element_index) const
1241 {
1242 return std::array<int, 4>({{cell_vertex(element_index, 0), cell_vertex(element_index, 1), cell_vertex(element_index, 2), cell_vertex(element_index, 3)}});
1243 }
1244 int NCMesh3D::add_element(Eigen::Vector4i v, int parent)
1245 {
1246 const int id = elements.size();
1247 const int level = (parent < 0) ? 0 : elements[parent].level + 1;
1248
1249 Eigen::Vector3d e1 = vertices[v[1]].pos - vertices[v[0]].pos;
1250 Eigen::Vector3d e2 = vertices[v[2]].pos - vertices[v[0]].pos;
1251 Eigen::Vector3d e3 = vertices[v[3]].pos - vertices[v[0]].pos;
1252 if ((e1.cross(e2)).dot(e3) < 0)
1253 std::swap(v[2], v[3]);
1254
1255 e2 = vertices[v[2]].pos - vertices[v[0]].pos;
1256 e3 = vertices[v[3]].pos - vertices[v[0]].pos;
1257 assert((e1.cross(e2)).dot(e3) > 0);
1258
1259 elements.emplace_back(3, v, level, parent);
1260
1261 if (parent >= 0)
1262 elements[id].body_id = elements[parent].body_id;
1263
1264 // add faces if not exist
1265 const int face012 = get_face(v[0], v[1], v[2]);
1266 const int face013 = get_face(v[0], v[1], v[3]);
1267 const int face123 = get_face(v[1], v[2], v[3]);
1268 const int face203 = get_face(v[2], v[0], v[3]);
1269
1270 faces[face012].add_element(id);
1271 faces[face013].add_element(id);
1272 faces[face123].add_element(id);
1273 faces[face203].add_element(id);
1274
1275 elements[id].faces << face012, face013, face123, face203;
1276
1277 // add edges if not exist
1278 const int edge01 = get_edge(v[0], v[1]);
1279 const int edge12 = get_edge(v[1], v[2]);
1280 const int edge20 = get_edge(v[2], v[0]);
1281 const int edge03 = get_edge(v[0], v[3]);
1282 const int edge13 = get_edge(v[1], v[3]);
1283 const int edge23 = get_edge(v[2], v[3]);
1284
1285 edges[edge01].add_element(id);
1286 edges[edge12].add_element(id);
1287 edges[edge20].add_element(id);
1288 edges[edge03].add_element(id);
1289 edges[edge13].add_element(id);
1290 edges[edge23].add_element(id);
1291
1292 elements[id].edges << edge01, edge12, edge20, edge03, edge13, edge23;
1293
1294 n_elements++;
1295
1296 for (int i = 0; i < v.size(); i++)
1297 vertices[v[i]].add_element(id);
1298
1299 index_prepared = false;
1300 adj_prepared = false;
1301
1302 return id;
1303 }
1304 } // namespace mesh
1305} // namespace polyfem
int V
Eigen::RowVectorXd point
int edge_id
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
Eigen::MatrixXi elements
Definition MeshData.hpp:22
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
std::vector< ElementType > elements_tag_
list of element types
Definition Mesh.hpp:718
bool has_boundary_ids() const
checks if surface selections are available
Definition Mesh.hpp:557
virtual void set_body_ids(const std::vector< int > &body_ids)
Set the volume sections.
Definition Mesh.hpp:477
virtual bool has_body_ids() const
checks if volumes selections are available
Definition Mesh.hpp:561
std::vector< int > geometry_ids_
list of geometry labels, one per top-dimensional element
Definition Mesh.hpp:726
std::vector< int > boundary_ids_
list of surface labels
Definition Mesh.hpp:722
void clear_higher_order_data()
Definition Mesh.cpp:715
std::vector< int > node_ids_
list of node labels, one per vertex
Definition Mesh.hpp:720
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 filter_element_data(const std::vector< bool > &keep)
Definition Mesh.cpp:49
virtual void append(const Mesh &mesh)
appends a new mesh to the end of this
Definition Mesh.cpp:474
virtual void set_boundary_ids(const std::vector< int > &boundary_ids)
Set the boundary selection from a vector.
Definition Mesh.hpp:469
Non-conforming 3D tetrahedral mesh with hanging nodes, for adaptive h-refinement: refine_element() sp...
Definition NCMesh3D.hpp:34
void remove_elements(const std::vector< bool > &keep) override
Remove all top-dimensional elements whose mask entry is false.
Definition NCMesh3D.cpp:17
int find_vertex(Eigen::Vector2i v) const
Definition NCMesh3D.cpp:994
bool is_boundary_element(const int element_global_id) const override
is cell boundary
Definition NCMesh3D.cpp:119
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 NCMesh3D.cpp:269
void update_labels_after_refinement()
After a refinement: rebuild the body/boundary id arrays from the per-entity labels (inherited by the ...
Definition NCMesh3D.cpp:96
std::vector< int > all_to_valid_elemMap
Definition NCMesh3D.hpp:490
int n_faces() const override
number of faces
Definition NCMesh3D.hpp:210
int n_cell_vertices(const int c_id) const override
number of vertices of a cell
Definition NCMesh3D.hpp:244
int valid_to_all_edge(const int id) const
Definition NCMesh3D.hpp:421
Navigation3D::Index next_around_edge(Navigation3D::Index idx) const override
Definition NCMesh3D.cpp:600
int face_edge(const int f_id, const int le_id) const
Definition NCMesh3D.cpp:66
void stale_index_error() const
Throws: the id maps are stale (prepare_mesh() was not called after editing the mesh).
Definition NCMesh3D.cpp:114
int n_cell_faces(const int c_id) const override
Definition NCMesh3D.hpp:246
void traverse_edge(Eigen::Vector2i v, double p1, double p2, int depth, std::vector< follower_edge > &list) const
std::unordered_map< Eigen::Vector2i, int, ArrayHasher2D > midpointMap
Definition NCMesh3D.hpp:486
int n_cells() const override
number of cells
Definition NCMesh3D.hpp:207
void build_element_vertex_adjacency()
void prepare_mesh() override
method used to finalize the mesh.
Definition NCMesh3D.hpp:377
std::vector< ncBoundary > edges
Definition NCMesh3D.hpp:483
int valid_to_all_face(const int id) const
Definition NCMesh3D.hpp:423
void bounding_box(RowVectorNd &min, RowVectorNd &max) const override
computes the bbox of the mesh
Definition NCMesh3D.cpp:231
void append(const Mesh &mesh) override
appends a new mesh to the end of this
Definition NCMesh3D.cpp:888
std::vector< int > refineHistory
Definition NCMesh3D.hpp:495
double edge_length(const int gid) const override
edge length
Definition NCMesh3D.cpp:190
int n_edges() const override
number of edges
Definition NCMesh3D.hpp:220
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 NCMesh3D.cpp:248
int get_edge(Eigen::Vector2i v)
void set_point(const int global_index, const RowVectorNd &p) override
Set the point.
Definition NCMesh3D.cpp:203
int add_element(Eigen::Vector4i v, int parent=-1)
int get_face(Eigen::Vector3i v)
void attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector< std::vector< int > > &nodes) override
attach high order nodes
Definition NCMesh3D.cpp:163
int n_face_vertices(const int f_id) const override
number of vertices of a face
Definition NCMesh3D.hpp:241
bool index_prepared
Whether the valid <-> all id maps match the current topology.
Definition NCMesh3D.hpp:471
RowVectorNd cell_barycenter(const int c) const override
cell barycenter
Definition NCMesh3D.cpp:223
std::vector< uint32_t > edge_neighs(const int e_gid) const override
Definition NCMesh3D.cpp:530
int all_to_valid_elem(const int id) const
Definition NCMesh3D.hpp:424
int n_vertices() const override
number of vertices
Definition NCMesh3D.hpp:230
std::vector< ncVert > vertices
Definition NCMesh3D.hpp:482
Navigation3D::Index get_index_from_element_edge(int hi, int v0, int v1) const override
Definition NCMesh3D.cpp:441
std::unordered_map< Eigen::Vector2i, int, ArrayHasher2D > edgeMap
Definition NCMesh3D.hpp:487
void traverse_face(int v1, int v2, int v3, Eigen::Vector2d p1, Eigen::Vector2d p2, Eigen::Vector2d p3, int depth, std::vector< follower_face > &face_list, std::vector< int > &edge_list) const
void refine(const int n_refinement, const double t) override
refine the mesh
Definition NCMesh3D.cpp:74
void normalize() override
normalize the mesh
Definition NCMesh3D.cpp:170
std::vector< int > all_to_valid_vertexMap
Definition NCMesh3D.hpp:491
int all_to_valid_edge(const int id) const
Definition NCMesh3D.hpp:420
bool build_topology(const MeshData &data) override
Construct only the dimension-specific topology.
Definition NCMesh3D.cpp:129
void get_face_elements_neighs(const int f_id, std::vector< int > &ids) const
Definition NCMesh3D.cpp:621
std::unique_ptr< Mesh > copy() const override
Create a copy of the mesh.
Definition NCMesh3D.cpp:989
int valid_to_all_elem(const int id) const
Definition NCMesh3D.hpp:425
void refine_element(int id_full)
Definition NCMesh3D.cpp:628
Navigation3D::Index switch_vertex(Navigation3D::Index idx) const override
Definition NCMesh3D.cpp:539
RowVectorNd kernel(const int cell_id) const override
Definition NCMesh3D.cpp:404
bool is_boundary_vertex(const int vertex_global_id) const override
is vertex boundary
Definition NCMesh3D.hpp:257
int cell_face(const int c_id, const int lf_id) const override
Definition NCMesh3D.hpp:248
void compute_elements_tag() override
compute element types, see ElementType
Definition NCMesh3D.cpp:182
RowVectorNd point(const int global_index) const override
point coordinates
Definition NCMesh3D.cpp:198
std::vector< int > all_to_valid_edgeMap
Definition NCMesh3D.hpp:492
int find_edge(Eigen::Vector2i v) const
Navigation3D::Index get_index_from_element(int hi, int lf, int lv) const override
Definition NCMesh3D.cpp:409
bool is_boundary_face(const int face_global_id) const override
is face boundary
Definition NCMesh3D.hpp:259
int cell_vertex(const int f_id, const int lv_id) const override
id of the vertex of a cell
Definition NCMesh3D.hpp:247
std::vector< int > all_to_valid_faceMap
Definition NCMesh3D.hpp:493
void coarsen_element(int id_full)
Definition NCMesh3D.cpp:746
Navigation3D::Index next_around_face(Navigation3D::Index idx) const override
Definition NCMesh3D.cpp:604
std::vector< int > valid_to_all_elemMap
Definition NCMesh3D.hpp:490
std::vector< int > valid_to_all_edgeMap
Definition NCMesh3D.hpp:492
RowVectorNd face_barycenter(const int f) const override
face barycenter
Definition NCMesh3D.cpp:215
Navigation3D::Index switch_element(Navigation3D::Index idx) const override
Definition NCMesh3D.cpp:581
void get_vertex_elements_neighs(const int v_id, std::vector< int > &ids) const override
Definition NCMesh3D.cpp:609
std::vector< ncElem > elements
Definition NCMesh3D.hpp:481
std::unordered_map< Eigen::Vector3i, int, ArrayHasher3D > faceMap
Definition NCMesh3D.hpp:488
RowVectorNd edge_barycenter(const int e) const override
edge barycenter
Definition NCMesh3D.cpp:208
std::vector< int > valid_to_all_faceMap
Definition NCMesh3D.hpp:493
std::vector< uint32_t > vertex_neighs(const int v_gid) const override
Definition NCMesh3D.cpp:522
std::vector< int > valid_to_all_vertexMap
Definition NCMesh3D.hpp:491
std::array< int, 4 > get_ordered_vertices_from_tet(const int element_index) const override
void update_elements_tag() override
Update elements types.
Definition NCMesh3D.cpp:186
int valid_to_all_vertex(const int id) const
Definition NCMesh3D.hpp:419
int edge_vertex(const int e_id, const int lv_id) const override
id of the edge vertex
Definition NCMesh3D.hpp:253
void refine_elements(const std::vector< int > &ids)
Definition NCMesh3D.cpp:733
int get_vertex(Eigen::Vector2i v)
int find_face(Eigen::Vector3i v) const
void get_edge_elements_neighs(const int e_id, std::vector< int > &ids) const override
Definition NCMesh3D.cpp:615
Navigation3D::Index switch_face(Navigation3D::Index idx) const override
Definition NCMesh3D.cpp:560
int face_vertex(const int f_id, const int lv_id) const override
id of the face vertex
Definition NCMesh3D.hpp:251
int all_to_valid_face(const int id) const
Definition NCMesh3D.hpp:422
Navigation3D::Index get_index_from_element_face(int hi, int v0, int v1, int v2) const override
Definition NCMesh3D.cpp:474
void set_body_ids(const std::vector< int > &body_ids) override
Set the volume sections.
Definition NCMesh3D.cpp:290
void set_boundary_ids(const std::vector< int > &boundary_ids) override
Set the boundary selection from a vector.
Definition NCMesh3D.cpp:281
std::vector< ncBoundary > faces
Definition NCMesh3D.hpp:484
Navigation3D::Index switch_edge(Navigation3D::Index idx) const override
Definition NCMesh3D.cpp:552
Set of ids stored as a sorted vector: iterates in increasing order like std::set<int>,...
void insert(const int id)
std::tuple< bool, int, Tree > is_valid(const int dim, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXd &u, const double threshold, const unsigned max_iter)
Definition Jacobian.cpp:201
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
InlineVector< int, 4 > geom_vertices
Definition NCMesh3D.hpp:182