11#include <Eigen/Geometry>
16#include <igl/boundary_facets.h>
17#include <igl/oriented_facets.h>
20#include <unordered_set>
35 std::vector<MeshWithID> result;
36 result.reserve(ids.size());
37 for (
const int id : ids)
43 child->remove_elements(keep);
44 result.push_back({id, std::move(child)});
51 const int kept = std::count(keep.begin(), keep.end(),
true);
53 auto filter_vector = [&keep, kept](
auto &values) {
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)
62 filtered.push_back(values[i]);
63 values = std::move(filtered);
74 assert(
orders_.rows() == keep.size());
75 Eigen::MatrixXi filtered(kept,
orders_.cols());
76 for (
int i = 0, j = 0; i < keep.size(); ++i)
78 filtered.row(j++) =
orders_.row(i);
87 assert(dim == 2 || dim == 3);
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))
110 vertices.row(i) =
point(i);
112 int element_width = 0;
115 Eigen::MatrixXi elements = Eigen::MatrixXi::Constant(
n_elements(), element_width, -1);
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));
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));
145 MeshData data(std::move(vertices), std::move(elements));
179 const auto *mesh3d =
dynamic_cast<const Mesh3D *
>(
this);
180 if (mesh3d ==
nullptr)
183 for (
int f = 0; f <
n_faces(); ++f)
192 for (
int j = 0; j < mesh3d->n_cell_faces(c); ++j)
194 data.
cell_faces[c].push_back(mesh3d->cell_face(c, j));
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());
221 for (
int e = 0; e <
n_edges(); ++e)
228 for (
int f = 0; f <
n_faces(); ++f)
231 for (
int f = 0; f <
n_faces(); ++f)
247 [](
const auto &
weights) { return !weights.empty(); }));
257 std::vector<int> input_to_vertex(data.
vertices.rows());
259 std::iota(input_to_vertex.begin(), input_to_vertex.end(), 0);
262 std::fill(input_to_vertex.begin(), input_to_vertex.end(), -1);
281 std::unordered_map<std::vector<int>, int,
HashVector> labels;
285 bool referenced =
true;
288 v = input_to_vertex[v];
289 referenced &= v >= 0;
293 std::sort(side.begin(), side.end());
294 const auto [it, inserted] = labels.emplace(std::move(side), data.
boundary_ids[i]);
296 logger().warn(
"Mesh side has multiple labels; using {}.", it->second);
299 std::vector<int> side = vertices;
300 std::sort(side.begin(), side.end());
301 const auto it = labels.find(side);
314 for (
int e = 0; e <
n_edges(); ++e)
323 for (
int f = 0; f <
n_faces(); ++f)
332 for (
int c = 0; c <
n_cells(); ++c)
387 const auto p =
point(n);
388 node_ids_[n] = marker(n, p, is_boundary);
409 std::vector<std::pair<int, int>> res;
412 for (
int e_id = 0; e_id <
n_edges(); ++e_id)
417 res.emplace_back(std::min(e0, e1), std::max(e0, e1));
425 std::vector<std::vector<int>> res(
n_faces());
427 for (
int f_id = 0; f_id <
n_faces(); ++f_id)
429 auto &tmp = res[f_id];
433 std::sort(tmp.begin(), tmp.end());
441 std::unordered_map<std::pair<int, int>, size_t,
HashPair> res;
444 for (
int e_id = 0; e_id <
n_edges(); ++e_id)
449 res[std::pair<int, int>(std::min(e0, e1), std::max(e0, e1))] = e_id;
457 std::unordered_map<std::vector<int>, size_t,
HashVector> res;
460 for (
int f_id = 0; f_id <
n_faces(); ++f_id)
466 std::sort(f.begin(), f.end());
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())
486 result.first =
source.higher_order_nodes_;
487 result.second =
source.higher_order_connectivity_;
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));
499 if (result.first.rows() <
source.n_vertices()
500 || result.second.size() != size_t(
source.n_elements()))
502 for (
int v = 0; v <
source.n_vertices(); ++v)
503 result.first.row(v) =
source.point(v);
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());
513 nodes.middleRows(
n_vertices, other_vertices) = right.first.topRows(other_vertices);
515 nodes.middleRows(
n_vertices + other_vertices, left_extra) = left.first.bottomRows(left_extra);
517 nodes.bottomRows(right_extra) = right.first.bottomRows(right_extra);
519 for (
auto &element : left.second)
520 for (
int &node : element)
522 node += other_vertices;
523 for (
auto &element : right.second)
524 for (
int &node : element)
525 node = node < other_vertices
596 Eigen::MatrixXi mesh_orders = mesh.
orders_;
597 if (mesh_orders.size() == 0)
599 assert(
orders_.cols() == mesh_orders.cols());
601 orders_.bottomRows(mesh_orders.rows()) = mesh_orders;
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())
617 entries.push_back(std::move(n));
619 entries.resize(n_entries + n_other);
658 for (
int i = 0; i < incoming_faces.size(); ++i)
659 if (incoming_faces(i) >= 0)
664 Eigen::MatrixXi combined = Eigen::MatrixXi::Constant(
665 old_rows + incoming_faces.rows(), width, -1);
667 combined.bottomLeftCorner(incoming_faces.rows(), incoming_faces.cols()) = incoming_faces;
674 template <
typename T>
675 void transform_high_order_nodes(std::vector<T> &nodes,
const MatrixNd &A,
const VectorNd &b)
681 n.nodes = (n.nodes * A.transpose()).rowwise() + b.transpose();
728 nodes = (nodes * A.transpose()).rowwise() + b.transpose();
std::vector< Eigen::Triplet< double > > entries
3D mesh interface: the elements are the cells (tetrahedra, hexahedra, prisms, pyramids or general pol...
Class to store the high-order cells nodes.
Class to store the high-order edge nodes.
Class to store the high-order face nodes.
Format-independent input used to construct a runtime Mesh.
bool has_polyhedral_topology() const
Eigen::MatrixXd higher_order_nodes
std::vector< std::vector< int > > higher_order_connectivity
std::vector< int > node_ids
std::vector< int > geometry_ids
std::vector< std::vector< int > > boundary_elements
std::vector< std::vector< int > > faces
Optional topology for arbitrary polyhedral cells.
std::vector< int > body_ids
std::vector< int > boundary_ids
Eigen::MatrixXd cell_kernel_points
std::vector< std::vector< int > > cell_faces
std::vector< std::vector< int > > cell_face_orientations
std::vector< std::vector< double > > higher_order_weights
std::vector< bool > cell_is_hex
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
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.
bool is_polytope(const int el_id) const
checks if element is polygon compatible
Eigen::MatrixXi orders_
list of geometry orders, one per cell
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
std::vector< ElementType > elements_tag_
list of element types
void clear_data_lost_by_refinement()
Drop the labels and the higher-order geometry before a conforming refinement.
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
bool is_rational_
stores if the mesh is rational
bool has_boundary_ids() const
checks if surface selections are available
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
std::vector< std::vector< int > > higher_order_connectivity_
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.
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
bool has_node_ids() const
checks if points selections are available
virtual void set_body_ids(const std::vector< int > &body_ids)
Set the volume sections.
Eigen::MatrixXi in_ordered_faces_
Order of the input faces, TODO: change to std::vector of Eigen::Vector.
void compute_node_ids(const std::function< int(const size_t, const RowVectorNd &, bool)> &marker)
computes boundary selections based on a function
virtual int get_boundary_id(const int primitive) const
Get the boundary selection of an element (face in 3d, edge in 2d)
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
void face_barycenters(Eigen::MatrixXd &barycenters) const
all face barycenters
virtual bool has_body_ids() const
checks if volumes selections are available
bool is_spline_compatible(const int el_id) const
checks if element is spline compatible
std::vector< int > geometry_ids_
list of geometry labels, one per top-dimensional element
void set_is_rational(const bool in_is_rational)
Set the is rational object.
std::vector< int > boundary_ids_
list of surface labels
bool is_prism(const int el_id) const
checks if element is a prism
void clear_higher_order_data()
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,...
std::vector< int > node_ids_
list of node labels, one per vertex
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
std::vector< CellNodes > cell_nodes_
high-order nodes associates to cells
MeshData to_mesh_data() const
Capture the complete runtime mesh in the canonical, format-independent representation.
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
virtual bool is_volume() const =0
checks if mesh is volume
std::vector< std::pair< int, int > > edges() const
list of sorted edges.
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.
std::vector< MeshWithID > split() const
Split the mesh according to its per-element geometry IDs.
bool has_poly() const
checks if the mesh has polytopes
Eigen::MatrixXd higher_order_nodes_
Canonical connectivity is retained because the runtime edge/face caches do not preserve the original ...
std::vector< int > body_ids_
list of volume labels
virtual int get_default_boundary_id(const int primitive) const
Get the default boundary selection of an element (face in 3d, edge in 2d)
int dimension() const
utily for dimension
virtual int n_cells() const =0
number of cells
void transform_higher_order_data(const MatrixNd &A, const VectorNd &b)
static std::unique_ptr< Mesh > create(MeshData data, bool non_conforming=false)
Construct a runtime mesh from format-independent decoded data.
std::vector< FaceNodes > face_nodes_
high-order nodes associates to faces
virtual int n_faces() const =0
number of faces
bool build_from_data(const MeshData &data)
Import all topology and metadata from MeshData.
std::vector< EdgeNodes > edge_nodes_
high-order nodes associates to edges
std::vector< std::vector< int > > faces() const
list of sorted faces.
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
void filter_element_data(const std::vector< bool > &keep)
Eigen::MatrixXi in_ordered_edges_
Order of the input edges.
virtual void append(const Mesh &mesh)
appends a new mesh to the end of this
bool is_pyramid(const int el_id) const
checks if element is a pyramid
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
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.
Eigen::VectorXi in_ordered_vertices_
Order of the input vertices.
bool has_geometry_ids() const
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
@ 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.
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > VectorNd
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 3, 3 > MatrixNd
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)