PolyFEM
Loading...
Searching...
No Matches
NCMesh2D.cpp
Go to the documentation of this file.
2
4
5#include <igl/writeOBJ.h>
6
8
9namespace polyfem
10{
11 namespace mesh
12 {
13 void NCMesh2D::remove_elements(const std::vector<bool> &keep)
14 {
15 assert(keep.size() == n_faces());
16
17 std::vector<int> old_node_ids(vertices.size(), -1);
18 if (!node_ids_.empty())
19 for (int v = 0; v < n_vertices(); ++v)
20 old_node_ids[valid_to_all_vertex(v)] = node_ids_[v];
21 std::vector<int> old_boundary_ids(edges.size(), -1);
22 if (has_boundary_ids())
23 for (int e = 0; e < n_edges(); ++e)
24 old_boundary_ids[valid_to_all_edge(e)] = boundary_ids_[e];
25
26 for (int e = 0; e < keep.size(); ++e)
27 {
28 if (keep[e])
29 continue;
30 const int full_id = valid_to_all_elem(e);
31 auto &element = elements[full_id];
32 element.is_ghost = true;
33 for (const int edge : element.edges)
34 edges[edge].remove_element(full_id);
35 for (const int vertex : element.vertices)
36 --vertices[vertex].n_elem;
37 --n_elements;
38 }
41
42 if (!node_ids_.empty())
43 {
44 node_ids_.resize(n_vertices());
45 for (int v = 0; v < n_vertices(); ++v)
46 node_ids_[v] = old_node_ids[valid_to_all_vertex(v)];
47 }
48 if (has_boundary_ids())
49 {
50 boundary_ids_.resize(n_edges());
51 for (int e = 0; e < n_edges(); ++e)
52 {
53 const int old_id = old_boundary_ids[valid_to_all_edge(e)];
54 boundary_ids_[e] = old_id < 0 ? get_default_boundary_id(e) : old_id;
55 edges[valid_to_all_edge(e)].boundary_id = boundary_ids_[e];
56 }
57 }
58 }
59
60 bool NCMesh2D::is_boundary_element(const int element_global_id) const
61 {
62 for (int le = 0; le < n_face_vertices(element_global_id); le++)
63 if (is_boundary_edge(face_edge(element_global_id, le)))
64 return true;
65
66 return false;
67 }
68
69 void NCMesh2D::refine(const int n_refinement, const double t)
70 {
71 if (n_refinement <= 0)
72 return;
73 for (int level = 0; level < n_refinement; ++level)
74 {
76 std::vector<bool> refine_mask(elements.size(), false);
77 for (int i = 0; i < elements.size(); i++)
78 if (elements[i].is_valid())
79 refine_mask[i] = true;
80
81 for (int i = 0; i < refine_mask.size(); i++)
82 if (refine_mask[i])
84 }
85 // refine_element() invalidates the id maps: rebuild them so that the
86 // mesh can be queried right away (e.g. to apply selections).
89 }
90
92 {
93 node_ids_.clear();
94 geometry_ids_.clear();
95 if (has_body_ids())
96 {
97 body_ids_.resize(n_faces());
98 for (int e = 0; e < body_ids_.size(); ++e)
99 body_ids_[e] = elements[valid_to_all_elem(e)].body_id;
100 }
101 if (has_boundary_ids())
102 {
103 boundary_ids_.resize(n_edges());
104 for (int b = 0; b < boundary_ids_.size(); ++b)
105 boundary_ids_[b] = edges[valid_to_all_edge(b)].boundary_id;
106 }
107 }
108
110 {
111 log_and_throw_error("NCMesh2D: the mesh was modified without calling prepare_mesh().");
112 }
113
115 {
116 const Eigen::MatrixXd &V = data.vertices;
117 const Eigen::MatrixXi &F = data.elements;
118 if (F.cols() != 3 || F.minCoeff() < 0)
119 return false;
120
121 n_elements = 0;
122 elements.clear();
123 vertices.clear();
124 edges.clear();
125 midpointMap.clear();
126 edgeMap.clear();
127 refineHistory.clear();
128 index_prepared = false;
129 adj_prepared = false;
130
131 vertices.reserve(V.rows());
132 for (int i = 0; i < V.rows(); i++)
133 {
134 vertices.emplace_back(V.row(i));
135 }
136 for (int i = 0; i < F.rows(); i++)
137 {
138 add_element(F.row(i), -1);
139 }
140
141 prepare_mesh();
142
143 return true;
144 }
145
146 void NCMesh2D::attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector<std::vector<int>> &nodes)
147 {
148 for (int f = 0; f < n_faces(); ++f)
149 if (nodes[f].size() != 3)
150 throw std::runtime_error("NCMesh doesn't support high order mesh!");
151 }
152 std::pair<RowVectorNd, int> NCMesh2D::edge_node(const Navigation::Index &index, const int n_new_nodes, const int i) const
153 {
154 const auto v1 = point(index.vertex);
155 const auto v2 = point(switch_vertex(index).vertex);
156
157 const double t = i / (n_new_nodes + 1.0);
158
159 return std::make_pair((1 - t) * v1 + t * v2, -1);
160 }
161 std::pair<RowVectorNd, int> NCMesh2D::face_node(const Navigation::Index &index, const int n_new_nodes, const int i, const int j) const
162 {
163 const auto v1 = point(index.vertex);
164 const auto v2 = point(switch_vertex(index).vertex);
165 const auto v3 = point(switch_vertex(switch_edge(index)).vertex);
166
167 const double b2 = i / (n_new_nodes + 2.0);
168 const double b3 = j / (n_new_nodes + 2.0);
169 const double b1 = 1 - b3 - b2;
170 assert(b3 < 1);
171 assert(b3 > 0);
172
173 return std::make_pair(b1 * v1 + b2 * v2 + b3 * v3, -1);
174 }
175
176 int NCMesh2D::find_vertex(Eigen::Vector2i v) const
177 {
178 std::sort(v.data(), v.data() + v.size());
179 auto search = midpointMap.find(v);
180 if (search != midpointMap.end())
181 return search->second;
182 else
183 return -1;
184 }
185
186 int NCMesh2D::get_vertex(Eigen::Vector2i v)
187 {
188 std::sort(v.data(), v.data() + v.size());
189 int id = find_vertex(v);
190 if (id < 0)
191 {
192 Eigen::VectorXd v_mid = (vertices[v[0]].pos + vertices[v[1]].pos) / 2.;
193 id = vertices.size();
194 vertices.emplace_back(v_mid);
195 midpointMap.emplace(v, id);
196 }
197 return id;
198 }
199
200 int NCMesh2D::find_edge(Eigen::Vector2i v) const
201 {
202 std::sort(v.data(), v.data() + v.size());
203 auto search = edgeMap.find(v);
204 if (search != edgeMap.end())
205 return search->second;
206 else
207 return -1;
208 }
209
210 int NCMesh2D::get_edge(Eigen::Vector2i v)
211 {
212 std::sort(v.data(), v.data() + v.size());
213 int id = find_edge(v);
214 if (id < 0)
215 {
216 edges.emplace_back(v);
217 id = edges.size() - 1;
218 edgeMap.emplace(v, id);
219 }
220 return id;
221 }
222
223 int NCMesh2D::add_element(Eigen::Vector3i v, int parent)
224 {
225 const int id = elements.size();
226 const int level = (parent < 0) ? 0 : elements[parent].level + 1;
227 elements.emplace_back(2, v, level, parent);
228
229 if (parent >= 0)
230 elements[id].body_id = elements[parent].body_id;
231
232 for (int i = 0; i < v.size(); i++)
233 vertices[v(i)].n_elem++;
234
235 // add edges if not exist
236 int edge01 = get_edge(Eigen::Vector2i(v[0], v[1]));
237 int edge12 = get_edge(Eigen::Vector2i(v[2], v[1]));
238 int edge20 = get_edge(Eigen::Vector2i(v[0], v[2]));
239
240 elements[id].edges << edge01, edge12, edge20;
241
242 edges[edge01].add_element(id);
243 edges[edge12].add_element(id);
244 edges[edge20].add_element(id);
245
246 n_elements++;
247 index_prepared = false;
248 adj_prepared = false;
249
250 return id;
251 }
252
253 void NCMesh2D::refine_element(int id_full)
254 {
255 auto &elem = elements[id_full];
256 if (elem.is_not_valid())
257 throw std::runtime_error("Cannot refine an invalid element!");
258
259 const auto v = elem.vertices;
260 elem.is_refined = true;
261 n_elements--;
262
263 // remove the old element from edge reference
264 for (int e = 0; e < 3; e++)
265 edges[elem.edges(e)].remove_element(id_full);
266
267 for (int i = 0; i < v.size(); i++)
268 vertices[v(i)].n_elem--;
269
270 if (elem.children(0) >= 0)
271 {
272 for (int c = 0; c < elem.children.size(); c++)
273 {
274 auto &child = elements[elem.children(c)];
275 child.is_ghost = false;
276 n_elements++;
277 for (int le = 0; le < child.edges.size(); le++)
278 edges[child.edges(le)].add_element(elem.children(c));
279 for (int i = 0; i < child.vertices.size(); i++)
280 vertices[child.vertices(i)].n_elem++;
281 }
282 }
283 else
284 {
285 // create mid-points if not exist
286 const int v01 = get_vertex(Eigen::Vector2i(v[0], v[1]));
287 const int v12 = get_vertex(Eigen::Vector2i(v[2], v[1]));
288 const int v20 = get_vertex(Eigen::Vector2i(v[0], v[2]));
289
290 // inherite line singularity flag from parent edge
291 for (int i = 0; i < v.size(); i++)
292 for (int j = 0; j < i; j++)
293 {
294 int mid_id = find_vertex(v[i], v[j]);
295 int edge_id = find_edge(v[i], v[j]);
296 int edge1 = get_edge(v[i], mid_id);
297 int edge2 = get_edge(v[j], mid_id);
298 edges[edge1].boundary_id = edges[edge_id].boundary_id;
299 edges[edge2].boundary_id = edges[edge_id].boundary_id;
300 }
301
302 // create and insert child elements
303 elements[id_full].children(0) = elements.size();
304 add_element(Eigen::Vector3i(v[0], v01, v20), id_full);
305 elements[id_full].children(1) = elements.size();
306 add_element(Eigen::Vector3i(v[1], v12, v01), id_full);
307 elements[id_full].children(2) = elements.size();
308 add_element(Eigen::Vector3i(v[2], v20, v12), id_full);
309 elements[id_full].children(3) = elements.size();
310 add_element(Eigen::Vector3i(v12, v20, v01), id_full);
311 }
312
313 refineHistory.push_back(id_full);
314
315 index_prepared = false;
316 adj_prepared = false;
317 }
318
319 void NCMesh2D::refine_elements(const std::vector<int> &ids)
320 {
321 std::vector<int> full_ids(ids.size());
322 for (int i = 0; i < ids.size(); i++)
323 full_ids[i] = valid_to_all_elem(ids[i]);
324
325 for (int i : full_ids)
327
328 prepare_mesh();
330 }
331
333 {
334 const int parent_id = elements[id_full].parent;
335 if (parent_id < 0)
336 log_and_throw_error("NCMesh2D: cannot coarsen element {}, it has no parent.", id_full);
337 auto &parent = elements[parent_id];
338
339 for (int i = 0; i < parent.children.size(); i++)
340 if (elements[parent.children(i)].is_not_valid())
341 throw std::runtime_error("Coarsen operation invalid!");
342
343 // remove elements
344 for (int i = 0; i < parent.children.size(); i++)
345 {
346 auto &elem = elements[parent.children(i)];
347 elem.is_ghost = true;
348 n_elements--;
349 for (int le = 0; le < elem.edges.size(); le++)
350 edges[elem.edges(le)].remove_element(parent.children(i));
351 for (int v = 0; v < elem.vertices.size(); v++)
352 vertices[elem.vertices(v)].n_elem--;
353 }
354
355 // add element
356 parent.is_refined = false;
357 n_elements++;
358 for (int le = 0; le < parent.edges.size(); le++)
359 edges[parent.edges(le)].add_element(parent_id);
360 for (int v = 0; v < parent.vertices.size(); v++)
361 vertices[parent.vertices(v)].n_elem++;
362
363 refineHistory.push_back(parent_id);
364
365 index_prepared = false;
366 adj_prepared = false;
367 }
368
369 int find(const Eigen::VectorXi &vec, int x)
370 {
371 for (int i = 0; i < vec.size(); i++)
372 {
373 if (x == vec[i])
374 return i;
375 }
376 return -1;
377 }
378
380 {
381 for (auto &edge : edges)
382 {
383 edge.leader = -1;
384 edge.followers.clear();
385 edge.weights.setConstant(-1);
386 }
387
388 Eigen::Vector2i v;
389 std::vector<follower_edge> followers;
390 for (int e_id = 0; e_id < elements.size(); e_id++)
391 {
392 const auto &element = elements[e_id];
393 if (element.is_not_valid())
394 continue;
395 for (int edge_local = 0; edge_local < 3; edge_local++)
396 {
397 v << element.vertices[edge_local], element.vertices[(edge_local + 1) % 3]; // order is important here!
398 int edge_global = element.edges[edge_local];
399 assert(edge_global >= 0);
400 traverse_edge(v, 0, 1, 0, followers);
401 for (auto &s : followers)
402 {
403 edges[s.id].leader = edge_global;
404 edges[edge_global].followers.push_back(s.id);
405 edges[s.id].weights << s.p1, s.p2;
406 }
407 followers.clear();
408 }
409 }
410 }
411
413 {
414 for (auto &edge : edges)
415 {
416 if (edge.n_elem() == 1)
417 edge.isboundary = true;
418 else
419 edge.isboundary = false;
420 }
421
422 for (auto &edge : edges)
423 {
424 if (edge.leader >= 0 && edge.n_elem() > 0 && edges[edge.leader].n_elem() > 0)
425 {
426 edge.isboundary = false;
427 edges[edge.leader].isboundary = false;
428 }
429 }
430
431 for (auto &vert : vertices)
432 vert.isboundary = false;
433
434 for (auto &edge : edges)
435 {
436 if (edge.isboundary && edge.n_elem())
437 {
438 for (int j = 0; j < 2; j++)
439 vertices[edge.vertices(j)].isboundary = true;
440 }
441 }
442 }
443
444 double line_weight(const Eigen::Matrix<double, 2, 2> &e, const InlineVector<double, 3> &v)
445 {
446 assert(v.size() == 2);
447 double w1 = (v(0) - e(0, 0)) / (e(1, 0) - e(0, 0));
448 double w2 = (v(1) - e(0, 1)) / (e(1, 1) - e(0, 1));
449 if (0 <= w1 && w1 <= 1)
450 return w1;
451 else
452 return w2;
453 }
454
456 {
457 for (auto &vert : vertices)
458 {
459 vert.edge = -1;
460 vert.weight = -1;
461 }
462
463 Eigen::VectorXi vertexEdgeAdjacency;
464 vertexEdgeAdjacency.setConstant(vertices.size(), 1, -1);
465
466 for (int small_edge = 0; small_edge < edges.size(); small_edge++)
467 {
468 if (edges[small_edge].n_elem() == 0)
469 continue;
470
471 int large_edge = edges[small_edge].leader;
472 if (large_edge < 0)
473 continue;
474
475 int large_elem = edges[large_edge].get_element();
476 for (int j = 0; j < 2; j++)
477 {
478 int v_id = edges[small_edge].vertices(j);
479 if (find(elements[large_elem].vertices, v_id) < 0) // or maybe 0 < weights(large_edge, v_id) < 1
480 vertexEdgeAdjacency[v_id] = large_edge;
481 }
482 }
483
484 for (auto &element : elements)
485 {
486 if (element.is_not_valid())
487 continue;
488 for (int v_local = 0; v_local < 3; v_local++)
489 {
490 int v_global = element.vertices[v_local];
491 if (vertexEdgeAdjacency[v_global] < 0)
492 continue;
493
494 auto &large_edge = edges[vertexEdgeAdjacency[v_global]];
495 auto &large_element = elements[large_edge.get_element()];
496 vertices[v_global].edge = vertexEdgeAdjacency[v_global];
497
498 int e_local = find(large_element.edges, vertices[v_global].edge);
499 Eigen::Matrix<double, 2, 2> edge;
500 edge.row(0) = vertices[large_element.vertices[e_local]].pos;
501 edge.row(1) = vertices[large_element.vertices[(e_local + 1) % 3]].pos;
502 vertices[v_global].weight = line_weight(edge, vertices[v_global].pos);
503 }
504 }
505 }
506
507 double NCMesh2D::element_weight_to_edge_weight(const int l, const Eigen::Vector2d &pos)
508 {
509 double w = -1;
510 switch (l)
511 {
512 case 0:
513 w = pos(0);
514 assert(fabs(pos(1)) < 1e-12);
515 break;
516 case 1:
517 w = pos(1);
518 assert(fabs(pos(0) + pos(1) - 1) < 1e-12);
519 break;
520 case 2:
521 w = 1 - pos(1);
522 assert(fabs(pos(0)) < 1e-12);
523 break;
524 default:
525 assert(false);
526 }
527 return w;
528 }
529
531 {
532 // Count the valid entities from the topology (not from the maps being rebuilt).
533 index_prepared = false;
534 all_to_valid_elemMap.assign(elements.size(), -1);
536
537 for (int i = 0, e = 0; i < elements.size(); i++)
538 {
539 if (elements[i].is_not_valid())
540 continue;
543 e++;
544 }
545
546 const int n_verts = n_vertices();
547
548 all_to_valid_vertexMap.assign(vertices.size(), -1);
549 valid_to_all_vertexMap.resize(n_verts);
550
551 for (int i = 0, j = 0; i < vertices.size(); i++)
552 {
553 if (vertices[i].n_elem == 0)
554 continue;
557 j++;
558 }
559
560 all_to_valid_edgeMap.assign(edges.size(), -1);
562
563 for (int i = 0, j = 0; i < edges.size(); i++)
564 {
565 if (edges[i].n_elem() == 0)
566 continue;
569 j++;
570 }
571 index_prepared = true;
572 }
573
574 void NCMesh2D::append(const Mesh &mesh)
575 {
576 assert(typeid(mesh) == typeid(NCMesh2D));
577 const NCMesh2D &other = dynamic_cast<const NCMesh2D &>(mesh);
578 const bool had_boundary_ids = has_boundary_ids(), other_has_boundary_ids = other.has_boundary_ids();
579 const bool had_body_ids = has_body_ids(), other_has_body_ids = other.has_body_ids();
580
581 Mesh::append(mesh);
582
583 // Merge the whole structure of the other mesh (refinement hierarchy,
584 // hanging nodes and per-entity labels included), offsetting its ids.
585 const int v_off = vertices.size(), e_off = edges.size(), el_off = elements.size();
586 const auto offset = [](Eigen::VectorXi ids, const int off) {
587 for (int i = 0; i < ids.size(); ++i)
588 if (ids[i] >= 0)
589 ids[i] += off;
590 return ids;
591 };
592
593 vertices.reserve(v_off + other.vertices.size());
594 for (const ncVert &v : other.vertices)
595 vertices.push_back(v); // edge/weight are rebuilt by prepare_mesh()
596
597 edges.reserve(e_off + other.edges.size());
598 for (const ncBoundary &e : other.edges)
599 {
600 ncBoundary copy = e;
601 copy.vertices = offset(e.vertices, v_off);
602 copy.elem_list.clear();
603 for (const int el : e.elem_list)
604 copy.elem_list.insert(el + el_off);
605 copy.global_ids.clear();
606 edges.push_back(std::move(copy)); // leader/followers are rebuilt by prepare_mesh()
607 }
608
609 elements.reserve(el_off + other.elements.size());
610 for (const ncElem &el : other.elements)
611 {
612 ncElem copy = el;
613 copy.geom_vertices = offset(el.geom_vertices, v_off);
614 copy.vertices = offset(el.vertices, v_off);
615 copy.edges = offset(el.edges, e_off);
616 copy.children = offset(el.children, el_off);
617 if (copy.parent >= 0)
618 copy.parent += el_off;
619 elements.push_back(std::move(copy));
620 }
621
622 for (const auto &[key, value] : other.midpointMap)
623 midpointMap[Eigen::Vector2i(key.array() + v_off)] = value + v_off;
624 for (const auto &[key, value] : other.edgeMap)
625 edgeMap[Eigen::Vector2i(key.array() + v_off)] = value + e_off;
626 for (const int id : other.refineHistory)
627 refineHistory.push_back(id + el_off);
628 n_elements += other.n_elements;
629
630 index_prepared = false;
631 adj_prepared = false;
632 prepare_mesh();
633
634 // The per-entity labels are authoritative; entities of a mesh that had
635 // no labels get the defaults, and the base-class arrays mirror them.
636 if (had_boundary_ids || other_has_boundary_ids)
637 {
638 boundary_ids_.resize(n_edges());
639 for (int e = 0; e < n_edges(); ++e)
640 {
642 const bool labeled = valid_to_all_edge(e) < e_off ? had_boundary_ids : other_has_boundary_ids;
643 if (!labeled)
645 boundary_ids_[e] = edge.boundary_id;
646 }
647 }
648 if (had_body_ids || other_has_body_ids)
649 {
650 body_ids_.resize(n_faces());
651 for (int f = 0; f < n_faces(); ++f)
652 {
653 ncElem &element = elements[valid_to_all_elem(f)];
654 const bool labeled = valid_to_all_elem(f) < el_off ? had_body_ids : other_has_body_ids;
655 if (!labeled)
656 element.body_id = 0;
657 body_ids_[f] = element.body_id;
658 }
659 }
660 }
661
662 std::unique_ptr<Mesh> NCMesh2D::copy() const
663 {
664 return std::make_unique<NCMesh2D>(*this);
665 }
666
667 void NCMesh2D::traverse_edge(Eigen::Vector2i v, double p1, double p2, int depth, std::vector<follower_edge> &list) const
668 {
669 int v_mid = find_vertex(v);
670 std::vector<follower_edge> list1, list2;
671 if (v_mid >= 0)
672 {
673 double p_mid = (p1 + p2) / 2;
674 traverse_edge(Eigen::Vector2i(v[0], v_mid), p1, p_mid, depth + 1, list1);
675 list.insert(
676 list.end(),
677 std::make_move_iterator(list1.begin()),
678 std::make_move_iterator(list1.end()));
679 traverse_edge(Eigen::Vector2i(v_mid, v[1]), p_mid, p2, depth + 1, list2);
680 list.insert(
681 list.end(),
682 std::make_move_iterator(list2.begin()),
683 std::make_move_iterator(list2.end()));
684 }
685 if (depth > 0)
686 {
687 int follower_id = find_edge(v);
688 if (follower_id >= 0 && edges[follower_id].n_elem() > 0)
689 list.emplace_back(follower_id, p1, p2);
690 }
691 }
692
694 {
695 min = vertices[0].pos;
696 max = vertices[0].pos;
697
698 for (const auto &v : vertices)
699 {
700 for (int d = 0; d < 2; d++)
701 {
702 if (v.pos[d] > max[d])
703 max[d] = v.pos[d];
704 if (v.pos[d] < min[d])
705 min[d] = v.pos[d];
706 }
707 }
708 }
709
711 {
712 const auto &elem = elements[valid_to_all_elem(f)];
713
715 idx2.face = f;
716 idx2.vertex = all_to_valid_vertex(elem.vertices(lv));
717 idx2.edge = all_to_valid_edge(elem.edges(lv));
718 idx2.face_corner = -1;
719
720 return idx2;
721 }
722
724 {
725 const auto &elem = elements[valid_to_all_elem(idx.face)];
726 const auto &edge = edges[valid_to_all_edge(idx.edge)];
727
729 idx2.face = idx.face;
730 idx2.edge = idx.edge;
731
732 int v1 = valid_to_all_vertex(idx.vertex);
733 int v2 = -1;
734 for (int i = 0; i < edge.vertices.size(); i++)
735 if (edge.vertices(i) != v1)
736 {
737 v2 = edge.vertices(i);
738 break;
739 }
740
741 idx2.vertex = all_to_valid_vertex(v2);
742 idx2.face_corner = -1;
743
744 return idx2;
745 }
746
748 {
749 const auto &elem = elements[valid_to_all_elem(idx.face)];
750
752 idx2.face = idx.face;
753 idx2.vertex = idx.vertex;
754 idx2.edge = -1;
755 idx2.face_corner = -1;
756 const int full_vertex_id = valid_to_all_vertex(idx.vertex);
757
758 for (int i = 0; i < elem.edges.size(); i++)
759 {
760 const auto &edge = edges[elem.edges(i)];
761 const int valid_edge_id = all_to_valid_edge(elem.edges(i));
762 if (valid_edge_id != idx.edge && find(edge.vertices, full_vertex_id) >= 0)
763 {
764 idx2.edge = valid_edge_id;
765 break;
766 }
767 }
768
769 return idx2;
770 }
771
773 {
775 idx2.edge = idx.edge;
776 idx2.vertex = idx.vertex;
777 idx2.face_corner = -1;
778
779 const auto &edge = edges[valid_to_all_edge(idx.edge)];
780 if (edge.n_elem() == 2)
781 idx2.face = all_to_valid_elem(edge.find_opposite_element(valid_to_all_elem(idx.face)));
782 else
783 idx2.face = -1;
784
785 return idx2;
786 }
787
789 {
790 polyfem::RowVectorNd min, max;
791 bounding_box(min, max);
792
793 auto extent = max - min;
794 double scale = std::max(extent(0), extent(1));
795
796 for (auto &v : vertices)
797 v.pos = (v.pos - min.transpose()) / scale;
798 }
799
800 double NCMesh2D::edge_length(const int gid) const
801 {
802 const int v1 = edge_vertex(gid, 0);
803 const int v2 = edge_vertex(gid, 1);
804
805 return (point(v1) - point(v2)).norm();
806 }
807
816
817 void NCMesh2D::set_point(const int global_index, const RowVectorNd &p)
818 {
819 vertices[valid_to_all_vertex(global_index)].pos = p;
820 }
821
823 {
824 const int v1 = edge_vertex(index, 0);
825 const int v2 = edge_vertex(index, 1);
826
827 return 0.5 * (point(v1) + point(v2));
828 }
829
830 // void NCMesh2D::triangulate_faces(Eigen::MatrixXi &tris, Eigen::MatrixXd &pts, std::vector<int> &ranges) const
831 // {
832 // ranges.clear();
833
834 // std::vector<Eigen::MatrixXi> local_tris(n_faces());
835 // std::vector<Eigen::MatrixXd> local_pts(n_faces());
836
837 // int total_tris = 0;
838 // int total_pts = 0;
839
840 // ranges.push_back(0);
841
842 // for (int f = 0; f < n_faces(); ++f)
843 // {
844 // const int n_vertices = n_face_vertices(f);
845
846 // Eigen::MatrixXd face_pts(n_vertices, 2);
847 // local_tris[f].resize(n_vertices - 2, 3);
848
849 // for (int i = 0; i < n_vertices; ++i)
850 // {
851 // const int vertex = face_vertex(f, i);
852 // auto pt = point(vertex);
853 // face_pts(i, 0) = pt[0];
854 // face_pts(i, 1) = pt[1];
855 // }
856
857 // for (int i = 1; i < n_vertices - 1; ++i)
858 // {
859 // local_tris[f].row(i - 1) << 0, i, i + 1;
860 // }
861
862 // local_pts[f] = face_pts;
863
864 // total_tris += local_tris[f].rows();
865 // total_pts += local_pts[f].rows();
866
867 // ranges.push_back(total_tris);
868
869 // assert(local_pts[f].rows() == face_pts.rows());
870 // }
871
872 // tris.resize(total_tris, 3);
873 // pts.resize(total_pts, 2);
874
875 // int tri_index = 0;
876 // int pts_index = 0;
877 // for (std::size_t i = 0; i < local_tris.size(); ++i)
878 // {
879 // tris.block(tri_index, 0, local_tris[i].rows(), local_tris[i].cols()) = local_tris[i].array() + pts_index;
880 // tri_index += local_tris[i].rows();
881
882 // pts.block(pts_index, 0, local_pts[i].rows(), local_pts[i].cols()) = local_pts[i];
883 // pts_index += local_pts[i].rows();
884 // }
885 // }
886
887 void NCMesh2D::set_body_ids(const std::vector<int> &body_ids)
888 {
889 assert(body_ids.size() == n_faces());
890 Mesh::set_body_ids(body_ids);
891 for (int i = 0; i < body_ids.size(); i++)
892 {
893 elements[valid_to_all_elem(i)].body_id = body_ids[i];
894 }
895 }
896
897 void NCMesh2D::set_boundary_ids(const std::vector<int> &boundary_ids)
898 {
899 assert(boundary_ids.size() == n_edges());
900 Mesh::set_boundary_ids(boundary_ids);
901 for (int i = 0; i < boundary_ids.size(); i++)
902 {
903 edges[valid_to_all_edge(i)].boundary_id = boundary_ids[i];
904 }
905 }
906
907 void NCMesh2D::compute_body_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &)> &marker)
908 {
909 body_ids_.resize(n_faces());
910 std::fill(body_ids_.begin(), body_ids_.end(), -1);
911
912 for (int e = 0; e < n_faces(); ++e)
913 {
914 const auto bary = face_barycenter(e);
915 body_ids_[e] = marker(e, element_vertices(e), bary);
916 elements[valid_to_all_elem(e)].body_id = body_ids_[e];
917 }
918 }
919
920 void NCMesh2D::compute_boundary_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &, bool)> &marker)
921 {
922 boundary_ids_.resize(n_edges());
923
924 for (int e = 0; e < n_edges(); ++e)
925 {
926 bool is_boundary = is_boundary_edge(e);
927 const auto p = edge_barycenter(e);
928
929 std::vector<int> vs = {edge_vertex(e, 0), edge_vertex(e, 1)};
930 std::sort(vs.begin(), vs.end());
931 boundary_ids_[e] = marker(e, vs, p, is_boundary);
932 edges[valid_to_all_edge(e)].boundary_id = boundary_ids_[e];
933 }
934 }
935
936 } // namespace mesh
937} // namespace polyfem
int V
Eigen::MatrixXd vec
Eigen::RowVectorXd point
int edge_id
int x
RowVectorNd face_barycenter(const int index) const override
face barycenter
Definition Mesh2D.cpp:64
Format-independent input used to construct a runtime Mesh.
Definition MeshData.hpp:12
Eigen::MatrixXd vertices
Definition MeshData.hpp:21
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 2D triangle mesh with hanging nodes, for adaptive h-refinement: refine_element() split...
Definition NCMesh2D.hpp:33
int all_to_valid_elem(const int id) const
Definition NCMesh2D.hpp:351
int find_edge(Eigen::Vector2i v) const
Definition NCMesh2D.cpp:200
bool is_boundary_element(const int element_global_id) const override
is cell boundary
Definition NCMesh2D.cpp:60
void traverse_edge(Eigen::Vector2i v, double p1, double p2, int depth, std::vector< follower_edge > &list) const
Definition NCMesh2D.cpp:667
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 NCMesh2D.cpp:907
Navigation::Index switch_face(Navigation::Index idx) const override
Definition NCMesh2D.cpp:772
std::vector< int > valid_to_all_edgeMap
Definition NCMesh2D.hpp:413
Navigation::Index switch_vertex(Navigation::Index idx) const override
Definition NCMesh2D.cpp:723
std::unique_ptr< Mesh > copy() const override
Create a copy of the mesh.
Definition NCMesh2D.cpp:662
void attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector< std::vector< int > > &nodes) override
attach high order nodes
Definition NCMesh2D.cpp:146
int face_edge(const int f_id, const int le_id) const
Definition NCMesh2D.hpp:221
bool build_topology(const MeshData &data) override
Construct only the dimension-specific topology.
Definition NCMesh2D.cpp:114
void refine_elements(const std::vector< int > &ids)
Refines the given elements (valid ids) and prepares the mesh.
Definition NCMesh2D.cpp:319
void prepare_mesh() override
method used to finalize the mesh.
Definition NCMesh2D.hpp:317
std::vector< ncVert > vertices
Definition NCMesh2D.hpp:405
std::vector< int > all_to_valid_elemMap
Definition NCMesh2D.hpp:411
static double element_weight_to_edge_weight(const int l, const Eigen::Vector2d &pos)
Definition NCMesh2D.cpp:507
void refine_element(int id_full)
Refines one element (full id).
Definition NCMesh2D.cpp:253
void set_boundary_ids(const std::vector< int > &boundary_ids) override
Set the boundary selection from a vector.
Definition NCMesh2D.cpp:897
int n_faces() const override
number of faces
Definition NCMesh2D.hpp:188
void set_point(const int global_index, const RowVectorNd &p) override
Set the point.
Definition NCMesh2D.cpp:817
int all_to_valid_vertex(const int id) const
Definition NCMesh2D.hpp:347
std::vector< ncElem > elements
Definition NCMesh2D.hpp:404
void set_body_ids(const std::vector< int > &body_ids) override
Set the volume sections.
Definition NCMesh2D.cpp:887
Navigation::Index switch_edge(Navigation::Index idx) const override
Definition NCMesh2D.cpp:747
int get_edge(Eigen::Vector2i v)
Definition NCMesh2D.cpp:210
int add_element(Eigen::Vector3i v, int parent=-1)
Definition NCMesh2D.cpp:223
void coarsen_element(int id_full)
Coarsens the parent of one element (full id).
Definition NCMesh2D.cpp:332
int get_vertex(Eigen::Vector2i v)
Definition NCMesh2D.cpp:186
Navigation::Index get_index_from_face(int f, int lv=0) const override
Definition NCMesh2D.cpp:710
double edge_length(const int gid) const override
edge length
Definition NCMesh2D.cpp:800
int valid_to_all_edge(const int id) const
Definition NCMesh2D.hpp:350
int n_vertices() const override
number of vertices
Definition NCMesh2D.hpp:201
bool is_boundary_edge(const int edge_global_id) const override
is edge boundary
Definition NCMesh2D.hpp:248
void refine(const int n_refinement, const double t) override
refine the mesh
Definition NCMesh2D.cpp:69
std::pair< RowVectorNd, int > face_node(const Navigation::Index &index, const int n_new_nodes, const int i, const int j) const override
Definition NCMesh2D.cpp:161
void normalize() override
normalize the mesh
Definition NCMesh2D.cpp:788
std::vector< int > refineHistory
Definition NCMesh2D.hpp:415
void stale_index_error() const
Throws: the id maps are stale (prepare_mesh() was not called after editing the mesh).
Definition NCMesh2D.cpp:109
int valid_to_all_elem(const int id) const
Definition NCMesh2D.hpp:352
std::vector< int > valid_to_all_elemMap
Definition NCMesh2D.hpp:411
int edge_vertex(const int e_id, const int lv_id) const override
id of the edge vertex
Definition NCMesh2D.hpp:218
std::vector< int > all_to_valid_edgeMap
Definition NCMesh2D.hpp:413
std::vector< int > all_to_valid_vertexMap
Definition NCMesh2D.hpp:412
std::unordered_map< Eigen::Vector2i, int, ArrayHasher2D > midpointMap
Definition NCMesh2D.hpp:408
std::vector< ncBoundary > edges
Definition NCMesh2D.hpp:406
void compute_elements_tag() override
compute element types, see ElementType
Definition NCMesh2D.cpp:808
RowVectorNd edge_barycenter(const int index) const override
edge barycenter
Definition NCMesh2D.cpp:822
int all_to_valid_edge(const int id) const
Definition NCMesh2D.hpp:349
std::unordered_map< Eigen::Vector2i, int, ArrayHasher2D > edgeMap
Definition NCMesh2D.hpp:409
int find_vertex(Eigen::Vector2i v) const
Definition NCMesh2D.cpp:176
void bounding_box(RowVectorNd &min, RowVectorNd &max) const override
computes the bbox of the mesh
Definition NCMesh2D.cpp:693
int n_face_vertices(const int f_id) const override
number of vertices of a face
Definition NCMesh2D.hpp:213
int valid_to_all_vertex(const int id) const
Definition NCMesh2D.hpp:348
bool index_prepared
Whether the valid <-> all id maps match the current topology.
Definition NCMesh2D.hpp:394
void build_element_vertex_adjacency()
Definition NCMesh2D.cpp:455
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 NCMesh2D.cpp:920
int n_edges() const override
number of edges
Definition NCMesh2D.hpp:191
std::vector< int > valid_to_all_vertexMap
Definition NCMesh2D.hpp:412
std::pair< RowVectorNd, int > edge_node(const Navigation::Index &index, const int n_new_nodes, const int i) const override
Definition NCMesh2D.cpp:152
void update_elements_tag() override
Update elements types.
Definition NCMesh2D.cpp:812
void append(const Mesh &mesh) override
appends a new mesh to the end of this
Definition NCMesh2D.cpp:574
void update_labels_after_refinement()
After a refinement: rebuild the body/boundary id arrays from the per-entity labels (inherited by the ...
Definition NCMesh2D.cpp:91
void remove_elements(const std::vector< bool > &keep) override
Remove all top-dimensional elements whose mask entry is false.
Definition NCMesh2D.cpp:13
int find(const Eigen::VectorXi &vec, int x)
Definition NCMesh2D.cpp:369
double line_weight(const Eigen::Matrix< double, 2, 2 > &e, const InlineVector< double, 3 > &v)
Definition NCMesh2D.cpp:444
Eigen::Matrix< T, Eigen::Dynamic, 1, Eigen::ColMajor, MaxSize, 1 > InlineVector
Small vector stored inline (no heap allocation), with the VectorXi / VectorXd API; used for the per-e...
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, 3 > geom_vertices
Definition NCMesh2D.hpp:165