PolyFEM
Loading...
Searching...
No Matches
Mesh.cpp
Go to the documentation of this file.
1
7
10
11#include <Eigen/Geometry>
12
13#include <algorithm>
14#include <numeric>
15
16#include <igl/boundary_facets.h>
17#include <igl/oriented_facets.h>
18#include <igl/edges.h>
19
20#include <unordered_set>
21#include <set>
22#include <type_traits>
23
25namespace polyfem::mesh
26{
27 using namespace polyfem::utils;
28
29 std::vector<MeshWithID> Mesh::split() const
30 {
31 std::set<int> ids;
32 for (int e = 0; e < n_elements(); ++e)
33 ids.insert(get_geometry_id(e));
34
35 std::vector<MeshWithID> result;
36 result.reserve(ids.size());
37 for (const int id : ids)
38 {
39 auto child = copy();
40 std::vector<bool> keep(n_elements(), false);
41 for (int e = 0; e < n_elements(); ++e)
42 keep[e] = get_geometry_id(e) == id;
43 child->remove_elements(keep);
44 result.push_back({id, std::move(child)});
45 }
46 return result;
47 }
48
49 void Mesh::filter_element_data(const std::vector<bool> &keep)
50 {
51 const int kept = std::count(keep.begin(), keep.end(), true);
52
53 auto filter_vector = [&keep, kept](auto &values) {
54 if (values.empty())
55 return;
56 assert(values.size() == keep.size());
57 using Value = typename std::decay_t<decltype(values)>::value_type;
58 std::vector<Value> filtered;
59 filtered.reserve(kept);
60 for (int i = 0; i < keep.size(); ++i)
61 if (keep[i])
62 filtered.push_back(values[i]);
63 values = std::move(filtered);
64 };
65
66 filter_vector(elements_tag_);
67 filter_vector(body_ids_);
68 filter_vector(geometry_ids_);
69 filter_vector(cell_weights_);
70 filter_vector(higher_order_connectivity_);
71
72 if (orders_.size() > 0)
73 {
74 assert(orders_.rows() == keep.size());
75 Eigen::MatrixXi filtered(kept, orders_.cols());
76 for (int i = 0, j = 0; i < keep.size(); ++i)
77 if (keep[i])
78 filtered.row(j++) = orders_.row(i);
79 orders_ = std::move(filtered);
80 }
81 }
82
83 std::unique_ptr<Mesh> Mesh::create(MeshData data, const bool non_conforming)
84 {
85 data.validate();
86 const int dim = data.dimension();
87 assert(dim == 2 || dim == 3);
88
89 std::unique_ptr<Mesh> mesh;
90 if (dim == 2 && non_conforming)
91 mesh = std::make_unique<NCMesh2D>();
92 else if (dim == 2 && !non_conforming)
93 mesh = std::make_unique<CMesh2D>();
94 else if (dim == 3 && non_conforming)
95 mesh = std::make_unique<NCMesh3D>();
96 else if (dim == 3 && !non_conforming)
97 mesh = std::make_unique<CMesh3D>();
98 if (!mesh || !mesh->build_from_data(data))
99 log_and_throw_error("Unable to construct runtime mesh from MeshData.");
100 return mesh;
101 }
102
104 {
105 if (n_vertices() == 0 || n_elements() == 0)
106 log_and_throw_error("Cannot convert an empty runtime mesh to MeshData.");
107
108 Eigen::MatrixXd vertices(n_vertices(), dimension());
109 for (int i = 0; i < n_vertices(); ++i)
110 vertices.row(i) = point(i);
111
112 int element_width = 0;
113 for (int e = 0; e < n_elements(); ++e)
114 element_width = std::max(element_width, n_cell_vertices(e));
115 Eigen::MatrixXi elements = Eigen::MatrixXi::Constant(n_elements(), element_width, -1);
116 for (int e = 0; e < n_elements(); ++e)
117 {
118 for (int j = 0; j < n_cell_vertices(e); ++j)
119 elements(e, j) = element_vertex(e, j);
120
121 // Runtime navigation may reorder refined simplex corners. MeshData is a
122 // decoder-facing representation, so restore the canonical positive
123 // orientation before it is serialized or used to construct another mesh.
124 if (is_simplex(e))
125 {
126 if (dimension() == 2)
127 {
128 const Eigen::Vector2d a = vertices.row(elements(e, 1)) - vertices.row(elements(e, 0));
129 const Eigen::Vector2d b = vertices.row(elements(e, 2)) - vertices.row(elements(e, 0));
130 if (a.x() * b.y() - a.y() * b.x() < 0)
131 std::swap(elements(e, 1), elements(e, 2));
132 }
133 else
134 {
135 Eigen::Matrix3d jacobian;
136 jacobian.col(0) = vertices.row(elements(e, 1)) - vertices.row(elements(e, 0));
137 jacobian.col(1) = vertices.row(elements(e, 2)) - vertices.row(elements(e, 0));
138 jacobian.col(2) = vertices.row(elements(e, 3)) - vertices.row(elements(e, 0));
139 if (jacobian.determinant() < 0)
140 std::swap(elements(e, 1), elements(e, 2));
141 }
142 }
143 }
144
145 MeshData data(std::move(vertices), std::move(elements));
146 if (has_body_ids())
147 data.body_ids = body_ids_;
148 if (has_geometry_ids())
150 if (has_node_ids())
151 data.node_ids = node_ids_;
152 if (has_boundary_ids())
153 {
156 for (int i = 0; i < n_boundary_elements(); ++i)
157 for (int j = 0; j < (is_volume() ? n_face_vertices(i) : 2); ++j)
158 data.boundary_elements[i].push_back(boundary_element_vertex(i, j));
159 }
160
161 if (!higher_order_connectivity_.empty())
162 {
163 if (higher_order_connectivity_.size() != size_t(n_elements())
164 || higher_order_nodes_.rows() < n_vertices())
165 log_and_throw_error("Runtime mesh has inconsistent higher-order data.");
167 data.higher_order_nodes.topRows(n_vertices()) = data.vertices;
169 }
170 if (std::any_of(cell_weights_.begin(), cell_weights_.end(), [](const auto &weights) { return !weights.empty(); }))
171 {
172 if (cell_weights_.size() != size_t(n_elements()))
173 log_and_throw_error("Runtime mesh has inconsistent rational weights.");
175 }
176
178 {
179 const auto *mesh3d = dynamic_cast<const Mesh3D *>(this);
180 if (mesh3d == nullptr)
181 log_and_throw_error("Polyhedral topology is only supported for 3D meshes.");
182 data.faces.resize(n_faces());
183 for (int f = 0; f < n_faces(); ++f)
184 for (int j = 0; j < n_face_vertices(f); ++j)
185 data.faces[f].push_back(face_vertex(f, j));
186 data.cell_faces.resize(n_elements());
187 data.cell_face_orientations.resize(n_elements());
188 data.cell_is_hex.resize(n_elements());
189 data.cell_kernel_points.resize(n_elements(), 3);
190 for (int c = 0; c < n_elements(); ++c)
191 {
192 for (int j = 0; j < mesh3d->n_cell_faces(c); ++j)
193 {
194 data.cell_faces[c].push_back(mesh3d->cell_face(c, j));
195 data.cell_face_orientations[c].push_back(mesh3d->cell_face_orientation(c, j));
196 }
197 data.cell_is_hex[c] = is_cube(c);
198 data.cell_kernel_points.row(c) = mesh3d->kernel(c);
199 }
200 }
201
202 data.validate();
203 return data;
204 }
205
207 {
208 if (!build_topology(data))
209 return false;
211
212 std::vector<int> tmp(data.elements.data(), data.elements.data() + data.elements.size());
213 std::sort(tmp.begin(), tmp.end());
214 tmp.erase(std::unique(tmp.begin(), tmp.end()), tmp.end());
215 if (!tmp.empty() && tmp.front() == -1)
216 tmp.erase(tmp.begin());
217
218 in_ordered_vertices_ = Eigen::Map<Eigen::VectorXi, Eigen::Unaligned>(tmp.data(), tmp.size());
219
220 in_ordered_edges_.resize(n_edges(), 2);
221 for (int e = 0; e < n_edges(); ++e)
222 in_ordered_edges_.row(e) << edge_vertex(e, 0), edge_vertex(e, 1);
223 if (dimension() == 2)
224 in_ordered_faces_.resize(0, 0);
225 else
226 {
227 int width = 0;
228 for (int f = 0; f < n_faces(); ++f)
229 width = std::max(width, n_face_vertices(f));
230 in_ordered_faces_.setConstant(n_faces(), width, -1);
231 for (int f = 0; f < n_faces(); ++f)
232 for (int lv = 0; lv < n_face_vertices(f); ++lv)
233 in_ordered_faces_(f, lv) = face_vertex(f, lv);
234 }
235
236 if (!data.higher_order_connectivity.empty())
237 {
241 }
242 if (!data.higher_order_weights.empty())
243 {
245 set_is_rational(std::any_of(
246 data.higher_order_weights.begin(), data.higher_order_weights.end(),
247 [](const auto &weights) { return !weights.empty(); }));
248 }
249 if (!data.body_ids.empty())
251 if (!data.geometry_ids.empty())
253
254 // Mesh vertex of each input vertex. Conforming meshes keep the input
255 // vertices; non-conforming ones number only the referenced ones
256 // (in_ordered_vertices_ lists them in increasing order).
257 std::vector<int> input_to_vertex(data.vertices.rows());
258 if (is_conforming())
259 std::iota(input_to_vertex.begin(), input_to_vertex.end(), 0);
260 else
261 {
262 std::fill(input_to_vertex.begin(), input_to_vertex.end(), -1);
263 for (int v = 0; v < in_ordered_vertices_.size(); ++v)
264 input_to_vertex[in_ordered_vertices_[v]] = v;
265 }
266
267 if (!data.node_ids.empty())
268 {
269 if (is_conforming())
270 node_ids_ = data.node_ids;
271 else
272 {
273 node_ids_.resize(n_vertices());
274 for (int v = 0; v < n_vertices(); ++v)
276 }
277 }
278
279 if (!data.boundary_ids.empty())
280 {
281 std::unordered_map<std::vector<int>, int, HashVector> labels;
282 for (int i = 0; i < data.boundary_elements.size(); ++i)
283 {
284 std::vector<int> side = data.boundary_elements[i];
285 bool referenced = true;
286 for (int &v : side)
287 {
288 v = input_to_vertex[v];
289 referenced &= v >= 0;
290 }
291 if (!referenced)
292 continue;
293 std::sort(side.begin(), side.end());
294 const auto [it, inserted] = labels.emplace(std::move(side), data.boundary_ids[i]);
295 if (!inserted && it->second != data.boundary_ids[i])
296 logger().warn("Mesh side has multiple labels; using {}.", it->second);
297 }
298 compute_boundary_ids([&](const size_t primitive, const std::vector<int> &vertices, const RowVectorNd &, const bool) {
299 std::vector<int> side = vertices;
300 std::sort(side.begin(), side.end());
301 const auto it = labels.find(side);
302 return it == labels.end() ? get_default_boundary_id(primitive) : it->second;
303 });
304 }
305
306 return true;
307 }
308
310
311 void Mesh::edge_barycenters(Eigen::MatrixXd &barycenters) const
312 {
313 barycenters.resize(n_edges(), dimension());
314 for (int e = 0; e < n_edges(); ++e)
315 {
316 barycenters.row(e) = edge_barycenter(e);
317 }
318 }
319
320 void Mesh::face_barycenters(Eigen::MatrixXd &barycenters) const
321 {
322 barycenters.resize(n_faces(), dimension());
323 for (int f = 0; f < n_faces(); ++f)
324 {
325 barycenters.row(f) = face_barycenter(f);
326 }
327 }
328
329 void Mesh::cell_barycenters(Eigen::MatrixXd &barycenters) const
330 {
331 barycenters.resize(n_cells(), dimension());
332 for (int c = 0; c < n_cells(); ++c)
333 {
334 barycenters.row(c) = cell_barycenter(c);
335 }
336 }
337
339
340 // Queries on the tags
341 bool Mesh::is_spline_compatible(const int el_id) const
342 {
343 if (is_volume())
344 {
347 // || elements_tag_[el_id] == ElementType::SIMPLE_SINGULAR_INTERIOR_CUBE
348 // || elements_tag_[el_id] == ElementType::SIMPLE_SINGULAR_BOUNDARY_CUBE;
349 }
350 else
351 {
354 // || elements_tag_[el_id] == ElementType::INTERFACE_CUBE
355 // || elements_tag_[el_id] == ElementType::SIMPLE_SINGULAR_INTERIOR_CUBE;
356 }
357 }
358
359 // -----------------------------------------------------------------------------
360
371
372 // -----------------------------------------------------------------------------
373
374 bool Mesh::is_polytope(const int el_id) const
375 {
378 }
379
380 void Mesh::compute_node_ids(const std::function<int(const size_t, const RowVectorNd &, bool)> &marker)
381 {
382 node_ids_.resize(n_vertices());
383
384 for (int n = 0; n < n_vertices(); ++n)
385 {
386 bool is_boundary = is_boundary_vertex(n);
387 const auto p = point(n);
388 node_ids_[n] = marker(n, p, is_boundary);
389 }
390 }
391
392 bool Mesh::is_simplex(const int el_id) const
393 {
394 return elements_tag_[el_id] == ElementType::SIMPLEX;
395 }
396
397 bool Mesh::is_prism(const int el_id) const
398 {
399 return elements_tag_[el_id] == ElementType::PRISM;
400 }
401
402 bool Mesh::is_pyramid(const int el_id) const
403 {
404 return elements_tag_[el_id] == ElementType::PYRAMID;
405 }
406
407 std::vector<std::pair<int, int>> Mesh::edges() const
408 {
409 std::vector<std::pair<int, int>> res;
410 res.reserve(n_edges());
411
412 for (int e_id = 0; e_id < n_edges(); ++e_id)
413 {
414 const int e0 = edge_vertex(e_id, 0);
415 const int e1 = edge_vertex(e_id, 1);
416
417 res.emplace_back(std::min(e0, e1), std::max(e0, e1));
418 }
419
420 return res;
421 }
422
423 std::vector<std::vector<int>> Mesh::faces() const
424 {
425 std::vector<std::vector<int>> res(n_faces());
426
427 for (int f_id = 0; f_id < n_faces(); ++f_id)
428 {
429 auto &tmp = res[f_id];
430 for (int lv_id = 0; lv_id < n_face_vertices(f_id); ++lv_id)
431 tmp.push_back(face_vertex(f_id, lv_id));
432
433 std::sort(tmp.begin(), tmp.end());
434 }
435
436 return res;
437 }
438
439 std::unordered_map<std::pair<int, int>, size_t, HashPair> Mesh::edges_to_ids() const
440 {
441 std::unordered_map<std::pair<int, int>, size_t, HashPair> res;
442 res.reserve(n_edges());
443
444 for (int e_id = 0; e_id < n_edges(); ++e_id)
445 {
446 const int e0 = edge_vertex(e_id, 0);
447 const int e1 = edge_vertex(e_id, 1);
448
449 res[std::pair<int, int>(std::min(e0, e1), std::max(e0, e1))] = e_id;
450 }
451
452 return res;
453 }
454
455 std::unordered_map<std::vector<int>, size_t, HashVector> Mesh::faces_to_ids() const
456 {
457 std::unordered_map<std::vector<int>, size_t, HashVector> res;
458 res.reserve(n_faces());
459
460 for (int f_id = 0; f_id < n_faces(); ++f_id)
461 {
462 std::vector<int> f;
463 f.reserve(n_face_vertices(f_id));
464 for (int lv_id = 0; lv_id < n_face_vertices(f_id); ++lv_id)
465 f.push_back(face_vertex(f_id, lv_id));
466 std::sort(f.begin(), f.end());
467
468 res[f] = f_id;
469 }
470
471 return res;
472 }
473
474 void Mesh::append(const Mesh &mesh)
475 {
476 const int n_vertices = this->n_vertices();
477 const int other_vertices = mesh.n_vertices();
480 if (!higher_order_connectivity_.empty() || !mesh.higher_order_connectivity_.empty())
481 {
482 auto complete_higher_order = [](const Mesh &source) {
483 std::pair<Eigen::MatrixXd, std::vector<std::vector<int>>> result;
484 if (!source.higher_order_connectivity_.empty())
485 {
486 result.first = source.higher_order_nodes_;
487 result.second = source.higher_order_connectivity_;
488 }
489 else
490 {
491 result.first.resize(source.n_vertices(), source.dimension());
492 result.second.resize(source.n_elements());
493 for (int v = 0; v < source.n_vertices(); ++v)
494 result.first.row(v) = source.point(v);
495 for (int e = 0; e < source.n_elements(); ++e)
496 for (int j = 0; j < source.n_cell_vertices(e); ++j)
497 result.second[e].push_back(source.element_vertex(e, j));
498 }
499 if (result.first.rows() < source.n_vertices()
500 || result.second.size() != size_t(source.n_elements()))
501 log_and_throw_error("Cannot append inconsistent higher-order mesh data.");
502 for (int v = 0; v < source.n_vertices(); ++v)
503 result.first.row(v) = source.point(v);
504 return result;
505 };
506
507 auto left = complete_higher_order(*this);
508 auto right = complete_higher_order(mesh);
509 const int left_extra = left.first.rows() - n_vertices;
510 const int right_extra = right.first.rows() - other_vertices;
511 Eigen::MatrixXd nodes(n_vertices + other_vertices + left_extra + right_extra, dimension());
512 nodes.topRows(n_vertices) = left.first.topRows(n_vertices);
513 nodes.middleRows(n_vertices, other_vertices) = right.first.topRows(other_vertices);
514 if (left_extra)
515 nodes.middleRows(n_vertices + other_vertices, left_extra) = left.first.bottomRows(left_extra);
516 if (right_extra)
517 nodes.bottomRows(right_extra) = right.first.bottomRows(right_extra);
518
519 for (auto &element : left.second)
520 for (int &node : element)
521 if (node >= n_vertices)
522 node += other_vertices;
523 for (auto &element : right.second)
524 for (int &node : element)
525 node = node < other_vertices
526 ? node + n_vertices
527 : node + n_vertices + left_extra;
528 higher_order_nodes_ = std::move(nodes);
529 higher_order_connectivity_ = std::move(left.second);
531 higher_order_connectivity_.end(), right.second.begin(), right.second.end());
532 }
533
534 elements_tag_.insert(elements_tag_.end(), mesh.elements_tag_.begin(), mesh.elements_tag_.end());
535
536 // --------------------------------------------------------------------
537
538 // Per-vertex node ids; the side without ids gets the default -1.
539 if (!node_ids_.empty() || !mesh.node_ids_.empty())
540 {
541 node_ids_.resize(n_vertices, -1);
542 if (mesh.node_ids_.empty())
543 node_ids_.resize(n_vertices + mesh.n_vertices(), -1);
544 else
545 node_ids_.insert(node_ids_.end(), mesh.node_ids_.begin(), mesh.node_ids_.end());
546 }
547
548 assert(node_ids_.empty() || node_ids_.size() == n_vertices + mesh.n_vertices());
549
550 // --------------------------------------------------------------------
551
552 // Initialize boundary_ids_ if it is not initialized yet.
553 if (!has_boundary_ids() && mesh.has_boundary_ids())
554 {
556 for (int i = 0; i < boundary_ids_.size(); ++i)
558 }
559
560 if (mesh.has_boundary_ids())
561 {
562 boundary_ids_.insert(boundary_ids_.end(), mesh.boundary_ids_.begin(), mesh.boundary_ids_.end());
563 }
564 else if (has_boundary_ids()) // && !mesh.has_boundary_ids()
565 {
567 for (int i = 0; i < mesh.n_boundary_elements(); ++i)
568 boundary_ids_[n_boundary_elements() + i] = mesh.get_boundary_id(i); // results in default if mesh.boundary_ids_ is empty
569 }
570
571 // --------------------------------------------------------------------
572
573 // Initialize body_ids_ if it is not initialized yet.
574 if (!has_body_ids() && mesh.has_body_ids())
575 body_ids_ = std::vector<int>(n_elements(), 0); // 0 is the default body_id
576
577 if (mesh.has_body_ids())
578 body_ids_.insert(body_ids_.end(), mesh.body_ids_.begin(), mesh.body_ids_.end());
579 else if (has_body_ids()) // && !mesh.has_body_ids()
580 body_ids_.resize(n_elements() + mesh.n_elements(), 0); // 0 is the default body_id
581
582 // --------------------------------------------------------------------
583 // Initialize geometry_ids_ if it is not initialized yet.
584 if (!has_geometry_ids() && mesh.has_geometry_ids())
585 geometry_ids_ = std::vector<int>(n_elements(), 0);
586
587 if (mesh.has_geometry_ids())
588 geometry_ids_.insert(geometry_ids_.end(), mesh.geometry_ids_.begin(), mesh.geometry_ids_.end());
589 else if (has_geometry_ids())
590 geometry_ids_.resize(n_elements() + mesh.n_elements(), 0);
591
592 // --------------------------------------------------------------------
593
594 if (orders_.size() == 0)
595 orders_.setOnes(n_elements(), 1);
596 Eigen::MatrixXi mesh_orders = mesh.orders_;
597 if (mesh_orders.size() == 0)
598 mesh_orders.setOnes(mesh.n_elements(), 1);
599 assert(orders_.cols() == mesh_orders.cols());
600 orders_.conservativeResize(orders_.rows() + mesh_orders.rows(), orders_.cols());
601 orders_.bottomRows(mesh_orders.rows()) = mesh_orders;
602
604
605 // --------------------------------------------------------------------
606 // The curved-geometry nodes and the rational weights are indexed by
607 // edge/face/cell/element id, and the appended entities keep their ids
608 // shifted by this mesh's counts: pad both sides with empty entries
609 // (= linear) so the ids stay aligned when only one mesh has them.
610 const auto append_padded = [](auto &entries, const int n_entries, const auto &other, const int n_other, const auto &shift) {
611 if (entries.empty() && other.empty())
612 return;
613 entries.resize(n_entries);
614 for (auto n : other)
615 {
616 shift(n);
617 entries.push_back(std::move(n));
618 }
619 entries.resize(n_entries + n_other);
620 };
621 const int n_cells_this = is_volume() ? n_cells() : 0;
622 const int n_cells_other = mesh.is_volume() ? mesh.n_cells() : 0;
623 append_padded(edge_nodes_, n_edges(), mesh.edge_nodes_, mesh.n_edges(), [&](EdgeNodes &n) {
624 n.v1 += n_vertices;
625 n.v2 += n_vertices;
626 });
627 append_padded(face_nodes_, n_faces(), mesh.face_nodes_, mesh.n_faces(), [&](FaceNodes &n) {
628 n.v1 += n_vertices;
629 n.v2 += n_vertices;
630 n.v3 += n_vertices;
631 });
632 append_padded(cell_nodes_, n_cells_this, mesh.cell_nodes_, n_cells_other, [&](CellNodes &n) {
633 n.v1 += n_vertices;
634 n.v2 += n_vertices;
635 n.v3 += n_vertices;
636 n.v4 += n_vertices;
637 });
638 append_padded(cell_weights_, n_elements(), mesh.cell_weights_, mesh.n_elements(), [](std::vector<double> &) {});
639 // --------------------------------------------------------------------
640
641 assert(in_ordered_vertices_.cols() == mesh.in_ordered_vertices_.cols());
642 in_ordered_vertices_.conservativeResize(in_ordered_vertices_.rows() + mesh.in_ordered_vertices_.rows(), in_ordered_vertices_.cols());
643 in_ordered_vertices_.bottomRows(mesh.in_ordered_vertices_.rows()) = mesh.in_ordered_vertices_.array() + n_vertices;
644
645 if (in_ordered_edges_.size() == 0 || mesh.in_ordered_edges_.size() == 0)
646 in_ordered_edges_.resize(0, 0);
647 else
648 {
649 assert(in_ordered_edges_.cols() == mesh.in_ordered_edges_.cols());
651 }
652
653 if (in_ordered_faces_.size() == 0 || mesh.in_ordered_faces_.size() == 0)
654 in_ordered_faces_.resize(0, 0);
655 else
656 {
657 Eigen::MatrixXi incoming_faces = mesh.in_ordered_faces_;
658 for (int i = 0; i < incoming_faces.size(); ++i)
659 if (incoming_faces(i) >= 0)
660 incoming_faces(i) += n_vertices;
661
662 const int old_rows = in_ordered_faces_.rows();
663 const int width = std::max(in_ordered_faces_.cols(), incoming_faces.cols());
664 Eigen::MatrixXi combined = Eigen::MatrixXi::Constant(
665 old_rows + incoming_faces.rows(), width, -1);
666 combined.topLeftCorner(old_rows, in_ordered_faces_.cols()) = in_ordered_faces_;
667 combined.bottomLeftCorner(incoming_faces.rows(), incoming_faces.cols()) = incoming_faces;
668 in_ordered_faces_ = std::move(combined);
669 }
670 }
671
672 namespace
673 {
674 template <typename T>
675 void transform_high_order_nodes(std::vector<T> &nodes, const MatrixNd &A, const VectorNd &b)
676 {
677 for (T &n : nodes)
678 {
679 if (n.nodes.size())
680 {
681 n.nodes = (n.nodes * A.transpose()).rowwise() + b.transpose();
682 }
683 }
684 }
685 } // namespace
686
688 {
689 for (int i = 0; i < n_vertices(); ++i)
690 {
691 VectorNd p = point(i).transpose();
692 p = A * p + b;
693 set_point(i, p.transpose());
694 }
695
696 transform_high_order_nodes(edge_nodes_, A, b);
697 transform_high_order_nodes(face_nodes_, A, b);
698 transform_high_order_nodes(cell_nodes_, A, b);
700 }
701
703 {
704 boundary_ids_.clear();
705 body_ids_.clear();
706 node_ids_.clear();
707 geometry_ids_.clear();
708 orders_.resize(0, 0);
709 edge_nodes_.clear();
710 face_nodes_.clear();
711 cell_nodes_.clear();
713 }
714
716 {
717 higher_order_nodes_.resize(0, 0);
719 cell_weights_.clear();
720 is_rational_ = false;
721 }
722
724 {
725 if (higher_order_nodes_.rows() > n_vertices())
726 {
727 auto nodes = higher_order_nodes_.bottomRows(higher_order_nodes_.rows() - n_vertices());
728 nodes = (nodes * A.transpose()).rowwise() + b.transpose();
729 }
730 }
731} // namespace polyfem::mesh
std::vector< Eigen::Triplet< double > > entries
Eigen::RowVectorXd point
StiffnessMatrix source
std::vector< std::pair< int, double > > weights
3D mesh interface: the elements are the cells (tetrahedra, hexahedra, prisms, pyramids or general pol...
Definition Mesh3D.hpp:22
Class to store the high-order cells nodes.
Definition Mesh.hpp:99
Class to store the high-order edge nodes.
Definition Mesh.hpp:81
Class to store the high-order face nodes.
Definition Mesh.hpp:90
Format-independent input used to construct a runtime Mesh.
Definition MeshData.hpp:12
Eigen::MatrixXd vertices
Definition MeshData.hpp:21
bool has_polyhedral_topology() const
Definition MeshData.hpp:19
Eigen::MatrixXd higher_order_nodes
Definition MeshData.hpp:33
std::vector< std::vector< int > > higher_order_connectivity
Definition MeshData.hpp:34
std::vector< int > node_ids
Definition MeshData.hpp:29
std::vector< int > geometry_ids
Definition MeshData.hpp:28
std::vector< std::vector< int > > boundary_elements
Definition MeshData.hpp:30
std::vector< std::vector< int > > faces
Optional topology for arbitrary polyhedral cells.
Definition MeshData.hpp:38
std::vector< int > body_ids
Definition MeshData.hpp:27
std::vector< int > boundary_ids
Definition MeshData.hpp:31
Eigen::MatrixXi elements
Definition MeshData.hpp:22
void validate() const
Definition MeshData.cpp:7
Eigen::MatrixXd cell_kernel_points
Definition MeshData.hpp:42
std::vector< std::vector< int > > cell_faces
Definition MeshData.hpp:39
std::vector< std::vector< int > > cell_face_orientations
Definition MeshData.hpp:40
std::vector< std::vector< double > > higher_order_weights
Definition MeshData.hpp:35
std::vector< bool > cell_is_hex
Definition MeshData.hpp:41
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
Definition Mesh.hpp:181
virtual int n_vertices() const =0
number of vertices
void set_cell_weights(const std::vector< std::vector< double > > &in_cell_weights)
Set the cell weights for rational polynomial meshes.
Definition Mesh.hpp:590
bool is_polytope(const int el_id) const
checks if element is polygon compatible
Definition Mesh.cpp:374
Eigen::MatrixXi orders_
list of geometry orders, one per cell
Definition Mesh.hpp:728
std::unordered_map< std::pair< int, int >, size_t, polyfem::utils::HashPair > edges_to_ids() const
map from edge (pair of v id) to the id of the edge
Definition Mesh.cpp:439
std::vector< ElementType > elements_tag_
list of element types
Definition Mesh.hpp:718
void clear_data_lost_by_refinement()
Drop the labels and the higher-order geometry before a conforming refinement.
Definition Mesh.cpp:702
virtual RowVectorNd edge_barycenter(const int e) const =0
edge barycenter
int element_vertex(const int el_id, const int lv_id) const
id of the vertex of a element
Definition Mesh.hpp:240
bool is_rational_
stores if the mesh is rational
Definition Mesh.hpp:730
bool has_boundary_ids() const
checks if surface selections are available
Definition Mesh.hpp:557
virtual void compute_boundary_ids(const std::function< int(const size_t, const std::vector< int > &, const RowVectorNd &, bool)> &marker)=0
computes boundary selections based on a function
void cell_barycenters(Eigen::MatrixXd &barycenters) const
all cells barycenters
Definition Mesh.cpp:329
std::vector< std::vector< int > > higher_order_connectivity_
Definition Mesh.hpp:747
virtual bool is_conforming() const =0
if the mesh is conforming
virtual RowVectorNd face_barycenter(const int f) const =0
face barycenter
int get_geometry_id(const int element) const
Get the geometry ID of an element. The default geometry is 0.
Definition Mesh.hpp:543
virtual void set_point(const int global_index, const RowVectorNd &p)=0
Set the point.
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 void set_body_ids(const std::vector< int > &body_ids)
Set the volume sections.
Definition Mesh.hpp:477
Eigen::MatrixXi in_ordered_faces_
Order of the input faces, TODO: change to std::vector of Eigen::Vector.
Definition Mesh.hpp:754
void compute_node_ids(const std::function< int(const size_t, const RowVectorNd &, bool)> &marker)
computes boundary selections based on a function
Definition Mesh.cpp:380
virtual int get_boundary_id(const int primitive) const
Get the boundary selection of an element (face in 3d, edge in 2d)
Definition Mesh.hpp:499
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
void face_barycenters(Eigen::MatrixXd &barycenters) const
all face barycenters
Definition Mesh.cpp:320
virtual bool has_body_ids() const
checks if volumes selections are available
Definition Mesh.hpp:561
bool is_spline_compatible(const int el_id) const
checks if element is spline compatible
Definition Mesh.cpp:341
std::vector< int > geometry_ids_
list of geometry labels, one per top-dimensional element
Definition Mesh.hpp:726
void set_is_rational(const bool in_is_rational)
Set the is rational object.
Definition Mesh.hpp:309
std::vector< int > boundary_ids_
list of surface labels
Definition Mesh.hpp:722
bool is_prism(const int el_id) const
checks if element is a prism
Definition Mesh.cpp:397
void clear_higher_order_data()
Definition Mesh.cpp:715
virtual void apply_affine_transformation(const MatrixNd &A, const VectorNd &b)
Apply an affine transformation to the vertex positions (and to every other stored position,...
Definition Mesh.cpp:687
std::vector< int > node_ids_
list of node labels, one per vertex
Definition Mesh.hpp:720
std::unordered_map< std::vector< int >, size_t, polyfem::utils::HashVector > faces_to_ids() const
map from face (tuple of v id) to the id of the face
Definition Mesh.cpp:455
std::vector< CellNodes > cell_nodes_
high-order nodes associates to cells
Definition Mesh.hpp:737
MeshData to_mesh_data() const
Capture the complete runtime mesh in the canonical, format-independent representation.
Definition Mesh.cpp:103
virtual bool build_topology(const MeshData &data)=0
Construct only the dimension-specific topology.
std::vector< std::vector< double > > cell_weights_
weights associates to cells for rational polynomail meshes
Definition Mesh.hpp:739
virtual bool is_volume() const =0
checks if mesh is volume
std::vector< std::pair< int, int > > edges() const
list of sorted edges.
Definition Mesh.cpp:407
virtual int edge_vertex(const int e_id, const int lv_id) const =0
id of the edge vertex
virtual std::unique_ptr< Mesh > copy() const =0
Create a copy of the mesh.
void set_geometry_ids(const std::vector< int > &geometry_ids)
Set the geometry selection, one ID per element.
Definition Mesh.hpp:536
std::vector< MeshWithID > split() const
Split the mesh according to its per-element geometry IDs.
Definition Mesh.cpp:29
bool has_poly() const
checks if the mesh has polytopes
Definition Mesh.hpp:599
Eigen::MatrixXd higher_order_nodes_
Canonical connectivity is retained because the runtime edge/face caches do not preserve the original ...
Definition Mesh.hpp:746
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
int dimension() const
utily for dimension
Definition Mesh.hpp:171
virtual int n_cells() const =0
number of cells
void transform_higher_order_data(const MatrixNd &A, const VectorNd &b)
Definition Mesh.cpp:723
static std::unique_ptr< Mesh > create(MeshData data, bool non_conforming=false)
Construct a runtime mesh from format-independent decoded data.
Definition Mesh.cpp:83
std::vector< FaceNodes > face_nodes_
high-order nodes associates to faces
Definition Mesh.hpp:735
virtual int n_faces() const =0
number of faces
bool build_from_data(const MeshData &data)
Import all topology and metadata from MeshData.
Definition Mesh.cpp:206
std::vector< EdgeNodes > edge_nodes_
high-order nodes associates to edges
Definition Mesh.hpp:733
std::vector< std::vector< int > > faces() const
list of sorted faces.
Definition Mesh.cpp:423
virtual void attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector< std::vector< int > > &nodes)=0
attach high order nodes
int n_boundary_elements() const
utitlity to return the number of boundary elements, faces or edges in 3d and 2d
Definition Mesh.hpp:186
void filter_element_data(const std::vector< bool > &keep)
Definition Mesh.cpp:49
Eigen::MatrixXi in_ordered_edges_
Order of the input edges.
Definition Mesh.hpp:752
virtual void append(const Mesh &mesh)
appends a new mesh to the end of this
Definition Mesh.cpp:474
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
void edge_barycenters(Eigen::MatrixXd &barycenters) const
all edges barycenters
Definition Mesh.cpp:311
virtual int n_face_vertices(const int f_id) const =0
number of vertices of a face
bool has_explicit_polyhedral_topology_
Whether the source MeshData supplied explicit face/cell incidence.
Definition Mesh.hpp:743
Eigen::VectorXi in_ordered_vertices_
Order of the input vertices.
Definition Mesh.hpp:750
bool has_geometry_ids() const
Definition Mesh.hpp:549
virtual int n_cell_vertices(const int c_id) const =0
number of vertices of a cell
virtual int face_vertex(const int f_id, const int lv_id) const =0
id of the face vertex
int boundary_element_vertex(const int primitive_id, const int lv_id) const
Definition Mesh.hpp:258
@ REGULAR_INTERIOR_CUBE
Triangle/tet element.
@ REGULAR_BOUNDARY_CUBE
Quad/Hex incident to more than 1 singular vertices (should not happen in 2D)
@ MULTI_SINGULAR_BOUNDARY_CUBE
Quad incident to exactly 1 singular vertex (in 2D); hex incident to exactly 1 singular interior edge,...
@ PRISM
Boundary polytope.
@ MULTI_SINGULAR_INTERIOR_CUBE
Quad/hex incident to exactly 1 singular vertex (in 2D) or edge (in 3D)
@ SIMPLE_SINGULAR_INTERIOR_CUBE
Regular quad/hex inside a 3^n patch.
@ INTERFACE_CUBE
Boundary hex that is not regular nor SimpleSingularBoundaryCube.
@ INTERIOR_POLYTOPE
Quad/hex that is at the interface with a polytope (if a cube has both external boundary and and inter...
@ SIMPLE_SINGULAR_BOUNDARY_CUBE
Boundary quad/hex, where all boundary vertices/edges are incident to at most 2 quads/hexes.
@ BOUNDARY_POLYTOPE
Interior polytope.
void append_rows(DstMat &dst, const SrcMat &src)
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > VectorNd
Definition Types.hpp:11
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 3, 3 > MatrixNd
Definition Types.hpp:14
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73