PolyFEM
Loading...
Searching...
No Matches
MeshNodes.cpp
Go to the documentation of this file.
1
7
10
11namespace polyfem::mesh
12{
13 namespace
14 {
15
16 std::vector<bool> interface_edges(const Mesh2D &mesh)
17 {
18 std::vector<bool> is_interface(mesh.n_edges(), false);
19 for (int f = 0; f < mesh.n_faces(); ++f)
20 {
21 if (mesh.is_cube(f))
22 {
23 continue;
24 } // Skip quads
25
26 auto index = mesh.get_index_from_face(f, 0);
27 for (int lv = 0; lv < mesh.n_face_vertices(f); ++lv)
28 {
29 auto index2 = mesh.switch_face(index);
30 if (index2.face >= 0)
31 {
32 is_interface[index.edge] = true;
33 }
34 index = mesh.next_around_face(index);
35 }
36 }
37 return is_interface;
38 }
39
40 std::vector<bool> interface_faces(const Mesh3D &mesh)
41 {
42 std::vector<bool> is_interface(mesh.n_faces(), false);
43 for (int c = 0; c < mesh.n_cells(); ++c)
44 {
45 if (mesh.is_cube(c))
46 {
47 continue;
48 } // Skip hexes
49
50 for (int lf = 0; lf < mesh.n_cell_faces(c); ++lf)
51 {
52 auto index = mesh.get_index_from_element(c, lf, 0);
53 auto index2 = mesh.switch_element(index);
54 if (index2.element >= 0)
55 {
56 is_interface[index.face] = true;
57 }
58 }
59 }
60 return is_interface;
61 }
62
63 } // anonymous namespace
64
65 // -----------------------------------------------------------------------------
66
67 MeshNodes::MeshNodes(const Mesh &mesh, const bool has_poly, const bool connect_nodes, const int max_nodes_per_edge, const int max_nodes_per_face, const int max_nodes_per_cell)
68 : mesh_(mesh), connect_nodes_(connect_nodes), edge_offset_(mesh.n_vertices()), face_offset_(edge_offset_ + mesh.n_edges() * max_nodes_per_edge), cell_offset_(face_offset_ + mesh.n_faces() * max_nodes_per_face)
69
70 ,
71 max_nodes_per_edge_(max_nodes_per_edge), max_nodes_per_face_(max_nodes_per_face), max_nodes_per_cell_(max_nodes_per_cell)
72 {
73 // Initialization
74 int n_nodes = cell_offset_ + mesh.n_cells() * max_nodes_per_cell;
75 primitive_to_node_.assign(n_nodes, -1); // #v + #e + #f + #c
76 nodes_.resize(n_nodes, mesh.dimension());
77 is_boundary_.assign(n_nodes, false);
78
79 if (has_poly)
80 is_interface_.assign(n_nodes, false);
81
82 // Vertex nodes
83 for (int v = 0; v < mesh.n_vertices(); ++v)
84 {
85 // nodes_.row(v) = mesh.point(v);
87 }
88 // if (!vertices_only) {
89 // Eigen::MatrixXd bary;
90 // Edge nodes
91 // mesh.edge_barycenters(bary);
92
93 for (int e = 0; e < mesh.n_edges(); ++e)
94 {
95 // nodes_.row(edge_offset_ + e) = bary.row(e);
96 const bool is_boundary = mesh.is_boundary_edge(e);
97 for (int tmp = 0; tmp < max_nodes_per_edge; ++tmp)
98 is_boundary_[edge_offset_ + max_nodes_per_edge * e + tmp] = is_boundary;
99 }
100 // Face nodes
101 // mesh.face_barycenters(bary);
102 for (int f = 0; f < mesh.n_faces(); ++f)
103 {
104 // nodes_.row(face_offset_ + f) = bary.row(f);
105 for (int tmp = 0; tmp < max_nodes_per_face; ++tmp)
106 {
107 if (mesh.is_volume())
108 {
109 is_boundary_[face_offset_ + max_nodes_per_face * f + tmp] = mesh.is_boundary_face(f);
110 }
111 else
112 {
113 is_boundary_[face_offset_ + max_nodes_per_face * f + tmp] = false;
114 }
115 }
116 }
117 // Cell nodes
118 // mesh.cell_barycenters(bary);
119 for (int c = 0; c < mesh.n_cells(); ++c)
120 {
121 for (int tmp = 0; tmp < max_nodes_per_cell; ++tmp)
122 is_boundary_[cell_offset_ + max_nodes_per_cell * c + tmp] = false;
123 }
124 // }
125
126 // Vertices only, no need to compute interface marker
127 // if (vertices_only) { return; }
128
129 // Compute edges/faces that are at interface with a polytope
130 if (has_poly)
131 {
132 if (mesh.is_volume())
133 {
134 const Mesh3D *mesh3d = dynamic_cast<const Mesh3D *>(&mesh);
135 assert(mesh3d);
136 auto is_interface_face = interface_faces(*mesh3d);
137 for (int f = 0; f < mesh.n_faces(); ++f)
138 {
139 for (int tmp = 0; tmp < max_nodes_per_face; ++tmp)
140 is_interface_[face_offset_ + max_nodes_per_face * f + tmp] = is_interface_face[f];
141 }
142 }
143 else
144 {
145 const Mesh2D *mesh2d = dynamic_cast<const Mesh2D *>(&mesh);
146 assert(mesh2d);
147 auto is_interface_edge = interface_edges(*mesh2d);
148 for (int e = 0; e < mesh.n_edges(); ++e)
149 {
150 for (int tmp = 0; tmp < max_nodes_per_edge; ++tmp)
151 is_interface_[edge_offset_ + max_nodes_per_edge * e + tmp] = is_interface_edge[e];
152 }
153 }
154 }
155 }
156
158
160 {
161 // A primitive without node slots (e.g. cells when no space has
162 // interior nodes) has no valid id here.
163 if (primitive_id < 0 || primitive_id >= int(primitive_to_node_.size()))
164 log_and_throw_error("Node slot {} is out of range ({} slots).", primitive_id, primitive_to_node_.size());
165 if (primitive_to_node_[primitive_id] < 0 || !connect_nodes_)
166 {
167 primitive_to_node_[primitive_id] = n_nodes();
168
169 in_ordered_vertices_.push_back(primitive_id);
170 node_to_primitive_.push_back(primitive_id);
171 node_to_primitive_gid_.push_back(primitive_id);
172 assert(in_ordered_vertices_.size() == n_nodes());
173
174 // Each edge/face/cell owns max_nodes_per_* consecutive primitive slots.
175 if (primitive_id < edge_offset_)
176 nodes_.row(primitive_id) = mesh_.point(primitive_id);
177 else if (primitive_id < face_offset_)
178 nodes_.row(primitive_id) = mesh_.edge_barycenter((primitive_id - edge_offset_) / max_nodes_per_edge_);
179 else if (primitive_id < cell_offset_)
180 nodes_.row(primitive_id) = mesh_.face_barycenter((primitive_id - face_offset_) / max_nodes_per_face_);
181 else
182 nodes_.row(primitive_id) = mesh_.cell_barycenter((primitive_id - cell_offset_) / max_nodes_per_cell_);
183 }
184 return primitive_to_node_[primitive_id];
185 }
186
187 std::vector<int> MeshNodes::node_ids_from_edge(const Navigation::Index &index, const int n_new_nodes)
188 {
189 std::vector<int> res;
190 if (n_new_nodes <= 0)
191 return res;
192
193 const Mesh2D *mesh2d = dynamic_cast<const Mesh2D *>(&mesh_);
194 int start;
195
196 if (connect_nodes_)
197 start = edge_offset_ + index.edge * max_nodes_per_edge_;
198 else
199 {
200 if (mesh2d->is_boundary_edge(index.edge) || mesh2d->switch_face(index).face > index.face)
201 start = edge_offset_ + index.edge * max_nodes_per_edge_;
202 else
204 }
205
206 // const auto v1 = mesh2d->point(index.vertex);
207 // const auto v2 = mesh2d->point(mesh2d->switch_vertex(index).vertex);
208 assert(start < primitive_to_node_.size());
209 const int start_node_id = primitive_to_node_[start];
210#ifndef NDEBUG
211 if (!connect_nodes_)
212 {
213 assert(start_node_id < 0);
214 }
215#endif
216 if (start_node_id < 0 || !connect_nodes_)
217 {
218 for (int i = 1; i <= n_new_nodes; ++i)
219 {
220 // const double t = i/(n_new_nodes + 1.0);
221
222 const int primitive_id = start + i - 1;
223 assert(primitive_id < primitive_to_node_.size());
224
225 const auto [node, node_id] = mesh2d->edge_node(index, n_new_nodes, i);
226
227 primitive_to_node_[primitive_id] = n_nodes();
228
229 in_ordered_vertices_.push_back(node_id);
230 node_to_primitive_.push_back(primitive_id);
231 node_to_primitive_gid_.push_back(index.edge);
232
233 nodes_.row(primitive_id) = node;
234
235 res.push_back(primitive_to_node_[primitive_id]);
236 assert(in_ordered_vertices_.size() == n_nodes());
237 }
238 }
239 else
240 {
241 const auto [v, _] = mesh2d->edge_node(index, n_new_nodes, 1);
242
243 if ((node_position(start_node_id) - v).norm() < 1e-10)
244 {
245 for (int i = 0; i < n_new_nodes; ++i)
246 {
247 const int primitive_id = start + i;
248 res.push_back(primitive_to_node_[primitive_id]);
249 }
250 }
251 else
252 {
253 for (int i = n_new_nodes - 1; i >= 0; --i)
254 {
255 const int primitive_id = start + i;
256 res.push_back(primitive_to_node_[primitive_id]);
257 }
258 }
259 }
260
261 assert(res.size() == size_t(n_new_nodes));
262 return res;
263 }
264
265 std::vector<int> MeshNodes::node_ids_from_edge(const Navigation3D::Index &index, const int n_new_nodes)
266 {
267 std::vector<int> res;
268 if (n_new_nodes <= 0)
269 return res;
270
271 const int start = edge_offset_ + index.edge * max_nodes_per_edge_;
272
273 const Mesh3D *mesh3d = dynamic_cast<const Mesh3D *>(&mesh_);
274
275 const int start_node_id = primitive_to_node_[start];
276 if (start_node_id < 0)
277 {
278 for (int i = 1; i <= n_new_nodes; ++i)
279 {
280 const auto [node, node_id] = mesh3d->edge_node(index, n_new_nodes, i);
281
282 const int primitive_id = start + i - 1;
283 primitive_to_node_[primitive_id] = n_nodes();
284
285 in_ordered_vertices_.push_back(node_id);
286 node_to_primitive_.push_back(primitive_id);
287 node_to_primitive_gid_.push_back(index.edge);
288
289 nodes_.row(primitive_id) = node;
290
291 res.push_back(primitive_to_node_[primitive_id]);
292 assert(in_ordered_vertices_.size() == n_nodes());
293 }
294 }
295 else
296 {
297 const auto [v, _] = mesh3d->edge_node(index, n_new_nodes, 1);
298 if ((node_position(start_node_id) - v).norm() < 1e-10)
299 {
300 for (int i = 0; i < n_new_nodes; ++i)
301 {
302 const int primitive_id = start + i;
303 res.push_back(primitive_to_node_[primitive_id]);
304 }
305 }
306 else
307 {
308 for (int i = n_new_nodes - 1; i >= 0; --i)
309 {
310 const int primitive_id = start + i;
311 res.push_back(primitive_to_node_[primitive_id]);
312 }
313 }
314 }
315
316 assert(res.size() == size_t(n_new_nodes));
317 return res;
318 }
319
320 std::vector<int> MeshNodes::node_ids_from_face(const Navigation::Index &index, const int n_new_nodes)
321 {
322 std::vector<int> res;
323 if (n_new_nodes <= 0)
324 return res;
325
326 const int start = face_offset_ + index.face * max_nodes_per_face_;
327 const int start_node_id = primitive_to_node_[start];
328
329 const Mesh2D *mesh2d = dynamic_cast<const Mesh2D *>(&mesh_);
330
331 if (start_node_id < 0 || !connect_nodes_)
332 {
333 int loc_index = 0;
334 for (int i = 1; i <= n_new_nodes; ++i)
335 {
336 const int end = mesh2d->is_simplex(index.face) ? (n_new_nodes - i + 1) : n_new_nodes;
337 for (int j = 1; j <= end; ++j)
338 {
339 const auto [node, node_id] = mesh2d->face_node(index, n_new_nodes, i, j);
340
341 const int primitive_id = start + loc_index;
342 primitive_to_node_[primitive_id] = n_nodes();
343
344 in_ordered_vertices_.push_back(node_id);
345 node_to_primitive_.push_back(primitive_id);
346 node_to_primitive_gid_.push_back(index.face);
347
348 nodes_.row(primitive_id) = node;
349
350 res.push_back(primitive_to_node_[primitive_id]);
351
352 assert(in_ordered_vertices_.size() == n_nodes());
353
354 ++loc_index;
355 }
356 }
357 }
358 else
359 {
360 assert(false);
361 }
362
363#ifndef NDEBUG
364 if (mesh2d->is_simplex(index.face))
365 assert(res.size() == size_t(n_new_nodes * (n_new_nodes + 1) / 2));
366 else
367 assert(res.size() == size_t(n_new_nodes * n_new_nodes));
368#endif
369 return res;
370 }
371
372 std::vector<int> MeshNodes::node_ids_from_face(const Navigation3D::Index &index1, const int n_new_nodes, const int n_new_nodesq)
373 {
374 Navigation3D::Index index = index1;
375
376 std::vector<int> res;
377 if (n_new_nodes <= 0)
378 return res;
379
380 const Mesh3D *mesh3d = dynamic_cast<const Mesh3D *>(&mesh_);
381 assert(mesh3d->is_prism(index.element));
382
383 int start;
384 const bool is_tri = mesh3d->n_face_vertices(index.face) == 3;
385
386 if (connect_nodes_)
387 start = face_offset_ + index.face * max_nodes_per_face_;
388 else
389 {
390 if (mesh3d->is_boundary_face(index.face) || mesh3d->switch_element(index).element > index.element)
391 start = face_offset_ + index.face * max_nodes_per_face_;
392 else
394 }
395
396 const int start_node_id = primitive_to_node_[start];
397
398#ifndef NDEBUG
399 if (!connect_nodes_)
400 {
401 assert(start_node_id < 0);
402 }
403#endif
404
405 if (start_node_id < 0 || !connect_nodes_)
406 {
407 int loc_index = 0;
408
409 if (is_tri)
410 {
411 for (int i = 1; i <= n_new_nodes; ++i)
412 {
413 const int end = n_new_nodes - i + 1;
414 for (int j = 1; j <= end; ++j)
415 {
416 const int primitive_id = start + loc_index;
417 const auto [node, node_id] = mesh3d->face_node(index, n_new_nodes, -1, i, j);
418
419 primitive_to_node_[primitive_id] = n_nodes();
420
421 in_ordered_vertices_.push_back(node_id);
422 node_to_primitive_.push_back(primitive_id);
423 node_to_primitive_gid_.push_back(index.face);
424
425 nodes_.row(primitive_id) = node;
426
427 res.push_back(primitive_to_node_[primitive_id]);
428 assert(in_ordered_vertices_.size() == n_nodes());
429
430 ++loc_index;
431 }
432 }
433 }
434 else
435 {
436 const bool is_on_tri_face = mesh3d->n_face_vertices(mesh3d->switch_face(index).face) == 3; // on quad face, next to tri face
437 if (is_on_tri_face)
438 index = mesh3d->next_around_face(index);
439 assert(mesh3d->n_face_vertices(mesh3d->switch_face(index).face) == 4); // on quad face, next to quad face
440
441 for (int i = 1; i <= n_new_nodes; ++i)
442 {
443 for (int j = 1; j <= n_new_nodesq; ++j)
444 {
445 const int primitive_id = start + loc_index;
446 const auto [node, node_id] = mesh3d->face_node(index, n_new_nodes, n_new_nodesq, j, i);
447
448 primitive_to_node_[primitive_id] = n_nodes();
449
450 in_ordered_vertices_.push_back(node_id);
451 node_to_primitive_.push_back(primitive_id);
452 node_to_primitive_gid_.push_back(index.face);
453
454 nodes_.row(primitive_id) = node;
455
456 res.push_back(primitive_to_node_[primitive_id]);
457 assert(in_ordered_vertices_.size() == n_nodes());
458
459 ++loc_index;
460 }
461 }
462 }
463 }
464 else // find existing nodes, todo
465 {
466 if (is_tri)
467 {
468 const int total_nodes = n_new_nodes * (n_new_nodes + 1) / 2;
469 for (int i = 1; i <= n_new_nodes; ++i)
470 {
471 const int end = n_new_nodes - i + 1;
472 for (int j = 1; j <= end; ++j)
473 {
474 const auto [p, _] = mesh3d->face_node(index, n_new_nodes, n_new_nodesq, i, j);
475
476 bool found = false;
477 for (int k = start; k < start + total_nodes; ++k)
478 {
479 const double dist = (nodes_.row(k) - p).norm();
480 if (dist < 1e-10)
481 {
482 res.push_back(primitive_to_node_[k]);
483 found = true;
484 break;
485 }
486 }
487
488 assert(found);
489 }
490 }
491 }
492 else
493 {
494 const bool is_on_tri_face = mesh3d->n_face_vertices(mesh3d->switch_face(index).face) == 3; // on quad face, next to tri face
495 if (is_on_tri_face)
496 index = mesh3d->next_around_face(index);
497 assert(mesh3d->n_face_vertices(mesh3d->switch_face(index).face) == 4); // on quad face, next to quad face
498
499 const int total_nodes = n_new_nodes * n_new_nodesq;
500 for (int i = 1; i <= n_new_nodes; ++i)
501 {
502 const int end = n_new_nodesq;
503 for (int j = 1; j <= end; ++j)
504 {
505 const auto [p, _] = mesh3d->face_node(index, n_new_nodes, n_new_nodesq, j, i);
506
507 bool found = false;
508 for (int k = start; k < start + total_nodes; ++k)
509 {
510 const double dist = (nodes_.row(k) - p).norm();
511 if (dist < 1e-10)
512 {
513 res.push_back(primitive_to_node_[k]);
514 found = true;
515 break;
516 }
517 }
518
519 assert(found);
520 }
521 }
522 }
523 }
524
525#ifndef NDEBUG
526 if (is_tri)
527 assert(res.size() == size_t(n_new_nodes * (n_new_nodes + 1) / 2));
528 else
529 assert(res.size() == size_t(n_new_nodes * n_new_nodesq));
530#endif
531 return res;
532 }
533
534 std::vector<int> MeshNodes::node_ids_from_face(const Navigation3D::Index &index, const int n_new_nodes)
535 {
536 std::vector<int> res;
537 if (n_new_nodes <= 0)
538 return res;
539
540 const Mesh3D *mesh3d = dynamic_cast<const Mesh3D *>(&mesh_);
541 assert(!mesh3d->is_prism(index.element));
542
543 int start;
544 const bool is_tri = mesh3d->is_simplex(index.element) || (mesh3d->is_prism(index.element) && mesh3d->n_face_vertices(index.face) == 3) || (mesh3d->is_pyramid(index.element) && mesh3d->n_face_vertices(index.face) == 3);
545
546 if (connect_nodes_)
547 start = face_offset_ + index.face * max_nodes_per_face_;
548 else
549 {
550 if (mesh3d->is_boundary_face(index.face) || mesh3d->switch_element(index).element > index.element)
551 start = face_offset_ + index.face * max_nodes_per_face_;
552 else
554 }
555
556 const int start_node_id = primitive_to_node_[start];
557
558#ifndef NDEBUG
559 if (!connect_nodes_)
560 {
561 assert(start_node_id < 0);
562 }
563#endif
564
565 if (start_node_id < 0 || !connect_nodes_)
566 {
567 int loc_index = 0;
568 for (int i = 1; i <= n_new_nodes; ++i)
569 {
570 const int end = is_tri ? (n_new_nodes - i + 1) : n_new_nodes;
571 for (int j = 1; j <= end; ++j)
572 {
573 const int primitive_id = start + loc_index;
574 const auto [node, node_id] = mesh3d->face_node(index, n_new_nodes, i, j);
575
576 primitive_to_node_[primitive_id] = n_nodes();
577
578 in_ordered_vertices_.push_back(node_id);
579 node_to_primitive_.push_back(primitive_id);
580 node_to_primitive_gid_.push_back(index.face);
581
582 nodes_.row(primitive_id) = node;
583
584 res.push_back(primitive_to_node_[primitive_id]);
585 assert(in_ordered_vertices_.size() == n_nodes());
586
587 ++loc_index;
588 }
589 }
590 }
591 else
592 {
593 if (n_new_nodes == 1)
594 {
595 res.push_back(start_node_id);
596 }
597 else
598 {
599 const int total_nodes = is_tri ? (n_new_nodes * (n_new_nodes + 1) / 2) : (n_new_nodes * n_new_nodes);
600 for (int i = 1; i <= n_new_nodes; ++i)
601 {
602 const int end = is_tri ? (n_new_nodes - i + 1) : n_new_nodes;
603 for (int j = 1; j <= end; ++j)
604 {
605 const auto [p, _] = mesh3d->face_node(index, n_new_nodes, i, j);
606
607 bool found = false;
608 for (int k = start; k < start + total_nodes; ++k)
609 {
610 const double dist = (nodes_.row(k) - p).norm();
611 if (dist < 1e-10)
612 {
613 res.push_back(primitive_to_node_[k]);
614 found = true;
615 break;
616 }
617 }
618
619 assert(found);
620 }
621 }
622 }
623 }
624
625#ifndef NDEBUG
626 if (is_tri)
627 assert(res.size() == size_t(n_new_nodes * (n_new_nodes + 1) / 2));
628 else
629 assert(res.size() == size_t(n_new_nodes * n_new_nodes));
630#endif
631 return res;
632 }
633
634 std::vector<int> MeshNodes::node_ids_from_cell(const Navigation3D::Index &index, const int n_new_nodes)
635 {
636 std::vector<int> res;
637 const int start = cell_offset_ + index.element * max_nodes_per_cell_;
638 const Mesh3D *mesh3d = dynamic_cast<const Mesh3D *>(&mesh_);
639
640 if (mesh3d->is_simplex(index.element))
641 {
642 int loc_index = 0;
643 for (int i = 1; i <= n_new_nodes; ++i)
644 {
645 const int endj = (n_new_nodes - i + 1);
646 for (int j = 1; j <= endj; ++j)
647 {
648 const int endk = (n_new_nodes - i - j + 2);
649 for (int k = 1; k <= endk; ++k)
650 {
651 const int primitive_id = start + loc_index;
652
653 auto [node, node_id] = mesh3d->cell_node(index, n_new_nodes, i, j, k);
654
655 primitive_to_node_[primitive_id] = n_nodes();
656
657 in_ordered_vertices_.push_back(node_id);
658 node_to_primitive_.push_back(primitive_id);
659 node_to_primitive_gid_.push_back(index.element);
660
661 nodes_.row(primitive_id) = node;
662 res.push_back(primitive_to_node_[primitive_id]);
663 assert(in_ordered_vertices_.size() == n_nodes());
664
665 ++loc_index;
666 }
667 }
668 }
669 }
670 else if (mesh3d->is_cube(index.element))
671 {
672 int loc_index = 0;
673 for (int i = 1; i <= n_new_nodes; ++i)
674 {
675 for (int j = 1; j <= n_new_nodes; ++j)
676 {
677 for (int k = 1; k <= n_new_nodes; ++k)
678 {
679 const int primitive_id = start + loc_index;
680
681 const auto [node, node_id] = mesh3d->cell_node(index, n_new_nodes, i, j, k);
682 primitive_to_node_[primitive_id] = n_nodes();
683
684 in_ordered_vertices_.push_back(node_id);
685 node_to_primitive_.push_back(primitive_id);
686 node_to_primitive_gid_.push_back(index.element);
687
688 nodes_.row(primitive_id) = node;
689 res.push_back(primitive_to_node_[primitive_id]);
690 assert(in_ordered_vertices_.size() == n_nodes());
691
692 ++loc_index;
693 }
694 }
695 }
696 }
697 else if (mesh3d->is_prism(index.element))
698 {
699 assert(n_new_nodes == 1);
700 int loc_index = 0;
701
702 const int primitive_id = start + loc_index;
703
704 const auto [node, node_id] = mesh3d->cell_node(index, n_new_nodes, 0, 0, 0);
705 primitive_to_node_[primitive_id] = n_nodes();
706
707 in_ordered_vertices_.push_back(node_id);
708 node_to_primitive_.push_back(primitive_id);
709 node_to_primitive_gid_.push_back(index.element);
710
711 nodes_.row(primitive_id) = node;
712 res.push_back(primitive_to_node_[primitive_id]);
713 assert(in_ordered_vertices_.size() == n_nodes());
714 }
715 else if (mesh3d->is_pyramid(index.element))
716 {
717 // n_new_nodes = p-1, mirrors create_point_set interior loop in pyramid_bases.py
718 int loc_index = 0;
719 for (int k = 1; k < n_new_nodes; ++k)
720 {
721 const int grid = (k == 1) ? 1 : k; // 1 node for k=1, k×k for k>1
722 for (int i = 1; i <= grid; ++i)
723 {
724 for (int j = 1; j <= grid; ++j)
725 {
726 const int primitive_id = start + loc_index;
727 const auto [node, node_id] = mesh3d->cell_node(index, n_new_nodes, i, j, k);
728
729 primitive_to_node_[primitive_id] = n_nodes();
730 in_ordered_vertices_.push_back(node_id);
731 node_to_primitive_.push_back(primitive_id);
732 node_to_primitive_gid_.push_back(index.element);
733 nodes_.row(primitive_id) = node;
734
735 res.push_back(primitive_to_node_[primitive_id]);
736 assert(in_ordered_vertices_.size() == n_nodes());
737 ++loc_index;
738 }
739 }
740 }
741 }
742
743#ifndef NDEBUG
744 if (res.size() == 1 && connect_nodes_)
745 {
746 const int idx = node_id_from_cell(index.element);
747 assert(idx == res.front());
748 }
749#endif
750
751#ifndef NDEBUG
752 if (mesh3d->is_simplex(index.element))
753 {
754 int n_cell_nodes = 0;
755 for (int pp = 0; pp <= n_new_nodes; ++pp)
756 n_cell_nodes += (pp * (pp + 1) / 2);
757 assert(res.size() == size_t(n_cell_nodes));
758 }
759 else if (mesh3d->is_cube(index.element))
760 assert(res.size() == size_t(n_new_nodes * n_new_nodes * n_new_nodes));
761#endif
762
763 return res;
764 }
765
767 {
768 return node_id_from_primitive(v);
769 }
770
775
780
785
787
788 int MeshNodes::vertex_from_node_id(int node_id) const
789 {
790 const int res = node_to_primitive_[node_id];
791 assert(res >= 0 && res < edge_offset_);
792 return res;
793 }
794
795 int MeshNodes::edge_from_node_id(int node_id) const
796 {
797 const int res = node_to_primitive_[node_id];
798 assert(res >= edge_offset_ && res < face_offset_);
799 return (res - edge_offset_) / max_nodes_per_edge_;
800 }
801
802 int MeshNodes::face_from_node_id(int node_id) const
803 {
804 const int res = node_to_primitive_[node_id];
805 assert(res >= face_offset_ && res < cell_offset_);
806 return (res - face_offset_) / max_nodes_per_face_;
807 }
808
809 int MeshNodes::cell_from_node_id(int node_id) const
810 {
811 const int res = node_to_primitive_[node_id];
812 assert(res >= cell_offset_ && res < primitive_to_node_.size());
813 return (res - cell_offset_) / max_nodes_per_cell_;
814 }
815
817
819 {
820 node_tags_.clear();
821 if (!mesh_.has_node_ids())
822 return;
823
824 // The mesh labels vertices; a node on a vertex carries its label.
825 node_tags_.assign(n_nodes(), -1);
826 for (int n = 0; n < n_nodes(); ++n)
827 {
828 const int primitive = node_to_primitive_[n];
829 if (is_vertex_node(primitive))
830 node_tags_[n] = mesh_.get_node_id(primitive);
831 }
832 }
833
835
836 std::vector<int> MeshNodes::boundary_nodes() const
837 {
838 std::vector<int> res;
839 res.reserve(n_nodes());
840 for (size_t prim_id = 0; prim_id < primitive_to_node_.size(); ++prim_id)
841 {
842 int node_id = primitive_to_node_[prim_id];
843 if (node_id >= 0 && is_boundary_[prim_id])
844 {
845 res.push_back(node_id);
846 }
847 }
848 return res;
849 }
850
851 int MeshNodes::count_nonnegative_nodes(int start_i, int end_i) const
852 {
853 int count = 0;
854 for (int i = start_i; i < end_i; i++)
855 count += primitive_to_node_[i] >= 0;
856 return count;
857 }
858
859} // namespace polyfem::mesh
2D mesh interface: the elements are the faces (polygons) and the boundary elements are the edges.
Definition Mesh2D.hpp:18
virtual std::pair< RowVectorNd, int > edge_node(const Navigation::Index &index, const int n_new_nodes, const int i) const =0
virtual std::pair< RowVectorNd, int > face_node(const Navigation::Index &index, const int n_new_nodes, const int i, const int j) const =0
virtual Navigation::Index switch_face(Navigation::Index idx) const =0
3D mesh interface: the elements are the cells (tetrahedra, hexahedra, prisms, pyramids or general pol...
Definition Mesh3D.hpp:22
virtual Navigation3D::Index switch_element(Navigation3D::Index idx) const =0
std::pair< RowVectorNd, int > cell_node(const Navigation3D::Index &index, const int n_new_nodes, const int i, const int j, const int k) const
Definition Mesh3D.cpp:339
std::pair< RowVectorNd, int > face_node(const Navigation3D::Index &index, const int n_new_nodes, const int i, const int j) const
Definition Mesh3D.cpp:233
std::pair< RowVectorNd, int > edge_node(const Navigation3D::Index &index, const int n_new_nodes, const int i) const
Definition Mesh3D.cpp:79
virtual Navigation3D::Index next_around_face(Navigation3D::Index idx) const =0
virtual Navigation3D::Index switch_face(Navigation3D::Index idx) const =0
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
virtual int n_vertices() const =0
number of vertices
virtual RowVectorNd edge_barycenter(const int e) const =0
edge barycenter
virtual RowVectorNd face_barycenter(const int f) const =0
face barycenter
bool is_cube(const int el_id) const
checks if element is cube compatible
Definition Mesh.cpp:361
bool has_node_ids() const
checks if points selections are available
Definition Mesh.hpp:553
virtual RowVectorNd point(const int global_index) const =0
point coordinates
virtual bool is_boundary_face(const int face_global_id) const =0
is face boundary
virtual bool is_boundary_vertex(const int vertex_global_id) const =0
is vertex boundary
bool is_simplex(const int el_id) const
checks if element is simplex
Definition Mesh.cpp:392
bool is_prism(const int el_id) const
checks if element is a prism
Definition Mesh.cpp:397
virtual bool is_volume() const =0
checks if mesh is volume
virtual bool is_boundary_edge(const int edge_global_id) const =0
is edge boundary
int dimension() const
utily for dimension
Definition Mesh.hpp:171
virtual int n_cells() const =0
number of cells
virtual int n_faces() const =0
number of faces
bool is_pyramid(const int el_id) const
checks if element is a pyramid
Definition Mesh.cpp:402
virtual RowVectorNd cell_barycenter(const int c) const =0
cell barycenter
virtual int n_edges() const =0
number of edges
virtual int n_face_vertices(const int f_id) const =0
number of vertices of a face
virtual int get_node_id(const int node_id) const
Get the boundary selection of a node.
Definition Mesh.hpp:509
bool is_boundary(int node_id) const
Definition MeshNodes.hpp:84
int node_id_from_primitive(int primitive_id)
std::vector< int > primitive_to_node_
void finalize()
Called once all the nodes are created (at the end of the basis construction): computes the node tags ...
std::vector< int > node_ids_from_face(const Navigation::Index &index, const int n_new_nodes)
std::vector< int > node_ids_from_cell(const Navigation3D::Index &index, const int n_new_nodes)
int cell_from_node_id(int node_id) const
RowVectorNd node_position(int node_id) const
Definition MeshNodes.hpp:69
MeshNodes(const Mesh &mesh, const bool has_poly, const bool connect_nodes, const int max_nodes_per_edge, const int max_nodes_per_face, const int max_nodes_per_cell=0)
Definition MeshNodes.cpp:67
std::vector< int > node_to_primitive_gid_
Eigen::MatrixXd nodes_
std::vector< int > in_ordered_vertices_
int face_from_node_id(int node_id) const
int vertex_from_node_id(int node_id) const
bool is_vertex_node(int i) const
Definition MeshNodes.hpp:47
int count_nonnegative_nodes(int start_i, int end_i) const
std::vector< int > boundary_nodes() const
std::vector< int > node_tags_
int edge_from_node_id(int node_id) const
std::vector< int > node_to_primitive_
std::vector< int > node_ids_from_edge(const Navigation::Index &index, const int n_new_nodes)
std::vector< bool > is_boundary_
std::vector< bool > is_interface_
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73