11#include <unordered_map>
16 using namespace utils;
22 assert(keep.size() ==
n_faces());
24 std::unordered_map<std::pair<int, int>, int,
utils::HashPair> old_edge_ids;
26 for (
int e = 0; e <
n_edges(); ++e)
35 auto filter_nodes = [&keep](
auto &nodes) {
36 if (nodes.size() != keep.size())
38 std::decay_t<
decltype(nodes)> filtered;
39 for (
int i = 0; i < keep.size(); ++i)
41 filtered.push_back(nodes[i]);
42 nodes = std::move(filtered);
51 for (
int f = 0; f < keep.size(); ++f)
52 to_delete[f] = !keep[f];
57 if (!old_edge_ids.empty())
60 for (
int e = 0; e <
n_edges(); ++e)
63 const auto it = old_edge_ids.find(key);
71 if (!old_edge_nodes.empty())
74 for (
int e = 0; e <
n_edges(); ++e)
77 const auto it = old_edge_nodes.find(key);
78 if (it != old_edge_nodes.end())
84 for (
int e = 0; e <
n_edges(); ++e)
92 if (n_refinement <= 0)
99 bool all_simplicial =
true;
105 for (
int i = 0; i < n_refinement; ++i)
140 for (
int lv = 0; lv < 2; ++lv)
153 const Eigen::MatrixXd &
V = data.
vertices;
154 const Eigen::MatrixXi &
F = data.
elements;
161 for (
int i = 0; i <
V.rows(); ++i)
168 for (
int i = 0; i <
F.rows(); ++i)
170 std::vector<int> vertices;
171 vertices.reserve(
F.cols());
172 for (
int j = 0; j <
F.cols() &&
F(i, j) >= 0; ++j)
173 vertices.push_back(
F(i, j));
174 if (vertices.size() < 3)
197 assert(nodes.size() ==
n_faces());
199 for (
int f = 0; f <
n_faces(); ++f)
203 const auto &nodes_ids = nodes[f];
215 if (nodes_ids.size() == 3)
221 else if (nodes_ids.size() == 6)
225 for (
int le = 0; le < 3; ++le)
230 if (n.nodes.size() <= 0)
236 if ((n.v1 == nodes_ids[0] && n.v2 == nodes_ids[1]) || (n.v2 == nodes_ids[0] && n.v1 == nodes_ids[1]))
238 else if ((n.v1 == nodes_ids[1] && n.v2 == nodes_ids[2]) || (n.v2 == nodes_ids[1] && n.v1 == nodes_ids[2]))
243 n.nodes.resize(1, 2);
244 n.nodes <<
V(nodes_ids[node_index], 0),
V(nodes_ids[node_index], 1);
245 n.nodes_ids.push_back(nodes_ids[node_index]);
251 else if (nodes_ids.size() == 10)
255 for (
int le = 0; le < 3; ++le)
260 if (n.nodes.size() <= 0)
267 if (n.v1 == nodes_ids[0] && n.v2 == nodes_ids[1])
272 else if (n.v2 == nodes_ids[0] && n.v1 == nodes_ids[1])
277 else if (n.v1 == nodes_ids[1] && n.v2 == nodes_ids[2])
282 else if (n.v2 == nodes_ids[1] && n.v1 == nodes_ids[2])
287 else if (n.v1 == nodes_ids[2] && n.v2 == nodes_ids[0])
294 assert(n.v2 == nodes_ids[2] && n.v1 == nodes_ids[0]);
299 n.nodes.resize(2, 2);
300 n.nodes.row(0) <<
V(nodes_ids[node_index1], 0),
V(nodes_ids[node_index1], 1);
301 n.nodes.row(1) <<
V(nodes_ids[node_index2], 0),
V(nodes_ids[node_index2], 1);
303 n.nodes_ids.push_back(nodes_ids[node_index1]);
304 n.nodes_ids.push_back(nodes_ids[node_index2]);
314 n.nodes.resize(1, 2);
315 n.nodes <<
V(nodes_ids[9], 0),
V(nodes_ids[9], 1);
316 n.nodes_ids.push_back(nodes_ids[9]);
322 log_and_throw_error(
"Unsupported higher-order triangle with {} nodes (supported: 3, 6, 10).", nodes_ids.size());
337 const double t = i / (n_new_nodes + 1.0);
339 return std::make_pair((1 - t) * v1 + t * v2, -1);
344 return std::make_pair(n.nodes.row(i - 1), n.nodes_ids[i - 1]);
347 assert(n.v2 == index.
vertex);
348 return std::make_pair(n.nodes.row(n.nodes.rows() - i), n.nodes_ids[n.nodes_ids.size() - i]);
362 const double b2 = i / (n_new_nodes + 2.0);
363 const double b3 = j / (n_new_nodes + 2.0);
364 const double b1 = 1 - b3 - b2;
368 return std::make_pair(b1 * v1 + b2 * v2 + b3 * v3, -1);
374 return std::make_pair(n.nodes.row(0), n.nodes_ids[0]);
386 const double b1 = i / (n_new_nodes + 1.0);
387 const double b2 = j / (n_new_nodes + 1.0);
389 return std::make_pair(v1 * (1 - b1) * (1 - b2) + v2 * b1 * (1 - b2) + v3 * b1 * b2 + v4 * (1 - b1) * b2, -1);
403 min(0) = min_corner[0];
404 min(1) = min_corner[1];
406 max(0) = max_corner[0];
407 max(1) = max_corner[1];
416 double scaling = std::max(extent[0], std::max(extent[1], extent[2]));
423 Eigen::RowVector2d shift;
424 shift << origin[0], origin[1];
427 if (n.nodes.size() > 0)
428 n.nodes = (n.nodes.rowwise() - shift) / scaling;
432 if (n.nodes.size() > 0)
433 n.nodes = (n.nodes.rowwise() - shift) / scaling;
435 Eigen::Vector2d normalization_translation;
436 normalization_translation << -origin[0] / scaling, -origin[1] / scaling;
438 Eigen::Matrix2d::Identity() / scaling,
439 normalization_translation);
441 logger().debug(
"-- bbox before normalization:");
442 logger().debug(
" min : {} {}", min_corner[0], min_corner[1]);
443 logger().debug(
" max : {} {}", max_corner[0], max_corner[1]);
444 logger().debug(
" extent: {} {}", max_corner[0] - min_corner[0], max_corner[1] - min_corner[1]);
446 logger().debug(
"-- bbox after normalization:");
447 logger().debug(
" min : {} {}", min_corner[0], min_corner[1]);
448 logger().debug(
" max : {} {}", max_corner[0], max_corner[1]);
449 logger().debug(
" extent: {} {}", max_corner[0] - min_corner[0], max_corner[1] - min_corner[1]);
451 Eigen::MatrixXd p0, p1, p;
454 logger().debug(
"-- edge length after normalization:");
455 logger().debug(
" min: {}", p.rowwise().norm().minCoeff());
456 logger().debug(
" max: {}", p.rowwise().norm().maxCoeff());
457 logger().debug(
" avg: {}", p.rowwise().norm().mean());
594 for (
int e = 0; e <
n_edges(); ++e)
599 std::sort(vs.begin(), vs.end());
606 assert(
typeid(mesh) ==
typeid(
CMesh2D));
620 std::vector<int> indices;
621 for (
int i = 0; i < mesh2d.
n_faces(); ++i)
638 return std::make_unique<CMesh2D>(*
this);
Conforming 2D mesh of arbitrary polygons, stored in a PolygonMesh.
virtual RowVectorNd edge_barycenter(const int index) const override
edge barycenter
Navigation::Index switch_edge(Navigation::Index idx) const override
void normalize() override
normalize the mesh
void compute_elements_tag() override
compute element types, see ElementType
int n_faces() const override
number of faces
int n_face_vertices(const int f_id) const override
number of vertices of a face
int n_vertices() const override
number of vertices
int n_edges() const override
number of edges
bool is_boundary_element(const int element_global_id) const override
is cell boundary
double edge_length(const int gid) const override
edge length
std::pair< RowVectorNd, int > edge_node(const Navigation::Index &index, const int n_new_nodes, const int i) const override
std::unique_ptr< Mesh > copy() const override
Create a copy of the mesh.
Navigation::Index switch_vertex(Navigation::Index idx) const override
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
void set_point(const int global_index, const RowVectorNd &p) override
Set the point.
bool build_topology(const MeshData &data) override
Construct only the dimension-specific topology.
bool is_boundary_edge(const int edge_global_id) const override
is edge boundary
std::pair< RowVectorNd, int > face_node(const Navigation::Index &index, const int n_new_nodes, const int i, const int j) const override
void append(const Mesh &mesh) override
appends a new mesh to the end of this
int edge_vertex(const int e_id, const int lv_id) const override
id of the edge vertex
Navigation::Index get_index_from_face(int f, int lv=0) const override
virtual void bounding_box(RowVectorNd &min, RowVectorNd &max) const override
computes the bbox of the mesh
void remove_elements(const std::vector< bool > &keep) override
Remove all top-dimensional elements whose mask entry is false.
void attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector< std::vector< int > > &nodes) override
attach high order nodes
void refine(const int n_refinement, const double t) override
refine the mesh
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
virtual RowVectorNd point(const int global_index) const override
point coordinates
virtual void update_elements_tag() override
Update elements types.
RowVectorNd face_barycenter(const int index) const override
face barycenter
Navigation::Index next_around_face(Navigation::Index idx) const
void get_edges(Eigen::MatrixXd &p0, Eigen::MatrixXd &p1) const override
Get all the edges.
Class to store the high-order edge nodes.
Format-independent input used to construct a runtime Mesh.
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
Eigen::MatrixXi orders_
list of geometry orders, one per cell
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.
bool has_boundary_ids() const
checks if surface selections are available
bool is_cube(const int el_id) const
checks if element is cube compatible
Eigen::MatrixXi in_ordered_faces_
Order of the input faces, TODO: change to std::vector of Eigen::Vector.
bool is_simplex(const int el_id) const
checks if element is simplex
std::vector< int > boundary_ids_
list of surface labels
std::vector< CellNodes > cell_nodes_
high-order nodes associates to cells
std::vector< int > element_vertices(const int el_id) const
list of vids of an element
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)
void transform_higher_order_data(const MatrixNd &A, const VectorNd &b)
std::vector< FaceNodes > face_nodes_
high-order nodes associates to faces
std::vector< EdgeNodes > edge_nodes_
high-order nodes associates to edges
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
Eigen::VectorXi in_ordered_vertices_
Order of the input vertices.
Minimal polygonal surface mesh used as the storage of CMesh2D.
const Point & point(const int v) const
void clear()
Removes all vertices, facets and edges.
int create_polygon(const std::vector< int > &vertices)
Appends a polygon (not glued to its neighbors) and returns its index.
void bounding_box(Point &min, Point &max) const
Bounding box of the vertices (min/max are initialized to the largest and lowest doubles,...
int facet_vertex(const int f, const int lv) const
int n_facet_vertices(const int f) const
int edge_vertex(const int e, const int lv) const
int create_vertices(int n)
Appends n vertices at the origin and returns the index of the first one.
void delete_facets(const std::vector< bool > &to_delete)
Removes the facets f with to_delete[f], keeping all vertices, and renumbers the remaining facets (and...
void prepare_mesh(PolygonMesh &M)
SplitFunction catmul_clark_split_func()
SplitFunction polar_split_func(double t)
Helper function.
void refine_triangle_mesh(const PolygonMesh &M_in, PolygonMesh &M_out)
Refine a triangle mesh.
void refine_polygonal_mesh(const PolygonMesh &M_in, PolygonMesh &M_out, Polygons::SplitFunction split_func)
Refine a polygonal mesh.
void compute_element_tags(const PolygonMesh &M, std::vector< ElementType > &element_tags)
Compute the type of each facet in a surface mesh.
void orient_normals_2d(PolygonMesh &M)
Orient facets of a 2D mesh so that each connected component has positive volume.
void generate_edges(PolygonMesh &M)
Creates the edges of M (sorted by vertex pair) if it has none yet.
spdlog::logger & logger()
Retrieves the current logger.
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)