10#include <geogram/basic/file_system.h>
11#include <geogram/mesh/mesh_io.h>
12#include <geogram/mesh/mesh_geometry.h>
13#include <geogram/mesh/mesh_repair.h>
17#include <unordered_map>
23 using namespace utils;
29 assert(keep.size() ==
n_faces());
31 std::unordered_map<std::pair<int, int>, int,
utils::HashPair> old_edge_ids;
33 for (
int e = 0; e <
n_edges(); ++e)
42 auto filter_nodes = [&keep](
auto &nodes) {
43 if (nodes.size() != keep.size())
45 std::decay_t<
decltype(nodes)> filtered;
46 for (
int i = 0; i < keep.size(); ++i)
48 filtered.push_back(nodes[i]);
49 nodes = std::move(filtered);
55 GEO::vector<GEO::index_t> to_delete(
mesh_.facets.nb(), 0);
56 for (
int f = 0; f < keep.size(); ++f)
57 to_delete[f] = keep[f] ? 0 : 1;
58 mesh_.facets.delete_elements(to_delete);
64 c2e_ = std::make_unique<GEO::Attribute<GEO::index_t>>(
mesh_.facet_corners.attributes(),
"edge_id");
66 boundary_edges_ = std::make_unique<GEO::Attribute<bool>>(
mesh_.edges.attributes(),
"boundary_edge");
68 if (!old_edge_ids.empty())
71 for (
int e = 0; e <
n_edges(); ++e)
74 const auto it = old_edge_ids.find(key);
82 if (!old_edge_nodes.empty())
85 for (
int e = 0; e <
n_edges(); ++e)
88 const auto it = old_edge_nodes.find(key);
89 if (it != old_edge_nodes.end())
95 for (
int e = 0; e <
n_edges(); ++e)
103 if (n_refinement <= 0)
110 bool all_simplicial =
true;
116 for (
int i = 0; i < n_refinement; ++i)
125 mesh_.clear(
false,
false);
142 c2e_ = std::make_unique<GEO::Attribute<GEO::index_t>>(
mesh_.facet_corners.attributes(),
"edge_id");
144 boundary_edges_ = std::make_unique<GEO::Attribute<bool>>(
mesh_.edges.attributes(),
"boundary_edge");
158 for (
int e = 0; e < (int)
mesh_.edges.nb(); ++e)
160 for (
int lv = 0; lv < 2; ++lv)
227 mesh_.clear(
false,
false);
232 c2e_ = std::make_unique<GEO::Attribute<GEO::index_t>>(
mesh_.facet_corners.attributes(),
"edge_id");
234 boundary_edges_ = std::make_unique<GEO::Attribute<bool>>(
mesh_.edges.attributes(),
"boundary_edge");
242 if (!mesh_save(
mesh_, path))
258 mesh_.clear(
false,
false);
264 c2e_ = std::make_unique<GEO::Attribute<GEO::index_t>>(
mesh_.facet_corners.attributes(),
"edge_id");
266 boundary_edges_ = std::make_unique<GEO::Attribute<bool>>(
mesh_.edges.attributes(),
"boundary_edge");
283 assert(nodes.size() ==
n_faces());
285 for (
int f = 0; f <
n_faces(); ++f)
289 const auto &nodes_ids = nodes[f];
291 if (nodes_ids.size() == 3)
297 else if (nodes_ids.size() == 6)
301 for (
int le = 0; le < 3; ++le)
306 if (n.nodes.size() <= 0)
312 if ((n.v1 == nodes_ids[0] && n.v2 == nodes_ids[1]) || (n.v2 == nodes_ids[0] && n.v1 == nodes_ids[1]))
314 else if ((n.v1 == nodes_ids[1] && n.v2 == nodes_ids[2]) || (n.v2 == nodes_ids[1] && n.v1 == nodes_ids[2]))
319 n.nodes.resize(1, 2);
320 n.nodes <<
V(nodes_ids[node_index], 0),
V(nodes_ids[node_index], 1);
321 n.nodes_ids.push_back(nodes_ids[node_index]);
327 else if (nodes_ids.size() == 10)
331 for (
int le = 0; le < 3; ++le)
336 if (n.nodes.size() <= 0)
343 if (n.v1 == nodes_ids[0] && n.v2 == nodes_ids[1])
348 else if (n.v2 == nodes_ids[0] && n.v1 == nodes_ids[1])
353 else if (n.v1 == nodes_ids[1] && n.v2 == nodes_ids[2])
358 else if (n.v2 == nodes_ids[1] && n.v1 == nodes_ids[2])
363 else if (n.v1 == nodes_ids[2] && n.v2 == nodes_ids[0])
370 assert(n.v2 == nodes_ids[2] && n.v1 == nodes_ids[0]);
375 n.nodes.resize(2, 2);
376 n.nodes.row(0) <<
V(nodes_ids[node_index1], 0),
V(nodes_ids[node_index1], 1);
377 n.nodes.row(1) <<
V(nodes_ids[node_index2], 0),
V(nodes_ids[node_index2], 1);
379 n.nodes_ids.push_back(nodes_ids[node_index1]);
380 n.nodes_ids.push_back(nodes_ids[node_index2]);
387 n.v1 =
mesh_.facets.vertex(f, 0);
388 n.v2 =
mesh_.facets.vertex(f, 1);
389 n.v3 =
mesh_.facets.vertex(f, 2);
390 n.nodes.resize(1, 2);
391 n.nodes <<
V(nodes_ids[9], 0),
V(nodes_ids[9], 1);
392 n.nodes_ids.push_back(nodes_ids[9]);
396 else if (nodes_ids.size() == 15)
420 const double t = i / (n_new_nodes + 1.0);
422 return std::make_pair((1 - t) * v1 + t * v2, -1);
427 return std::make_pair(n.nodes.row(i - 1), n.nodes_ids[i - 1]);
430 assert(n.v2 == index.
vertex);
431 return std::make_pair(n.nodes.row(n.nodes.rows() - i), n.nodes_ids[n.nodes_ids.size() - i]);
445 const double b2 = i / (n_new_nodes + 2.0);
446 const double b3 = j / (n_new_nodes + 2.0);
447 const double b1 = 1 - b3 - b2;
451 return std::make_pair(b1 * v1 + b2 * v2 + b3 * v3, -1);
457 return std::make_pair(n.nodes.row(0), n.nodes_ids[0]);
469 const double b1 = i / (n_new_nodes + 1.0);
470 const double b2 = j / (n_new_nodes + 1.0);
472 return std::make_pair(v1 * (1 - b1) * (1 - b2) + v2 * b1 * (1 - b2) + v3 * b1 * b2 + v4 * (1 - b1) * b2, -1);
481 GEO::vec3 min_corner, max_corner;
482 GEO::get_bbox(
mesh_, &min_corner[0], &max_corner[0]);
486 min(0) = min_corner.x;
487 min(1) = min_corner.y;
489 max(0) = max_corner.x;
490 max(1) = max_corner.y;
496 GEO::vec3 min_corner, max_corner;
497 GEO::get_bbox(
mesh_, &min_corner[0], &max_corner[0]);
498 GEO::vec3 extent = max_corner - min_corner;
499 double scaling = std::max(extent[0], std::max(extent[1], extent[2]));
501 const GEO::vec3 origin = min_corner;
502 for (GEO::index_t v = 0; v <
mesh_.vertices.nb(); ++v)
504 mesh_.vertices.point(v) = (
mesh_.vertices.point(v) - origin) / scaling;
506 Eigen::RowVector2d shift;
507 shift << origin[0], origin[1];
510 if (n.nodes.size() > 0)
511 n.nodes = (n.nodes.rowwise() - shift) / scaling;
515 if (n.nodes.size() > 0)
516 n.nodes = (n.nodes.rowwise() - shift) / scaling;
519 logger().debug(
"-- bbox before normalization:");
520 logger().debug(
" min : {} {}", min_corner[0], min_corner[1]);
521 logger().debug(
" max : {} {}", max_corner[0], max_corner[1]);
522 logger().debug(
" extent: {} {}", max_corner[0] - min_corner[0], max_corner[1] - min_corner[1]);
523 GEO::get_bbox(
mesh_, &min_corner[0], &max_corner[0]);
524 logger().debug(
"-- bbox after normalization:");
525 logger().debug(
" min : {} {}", min_corner[0], min_corner[1]);
526 logger().debug(
" max : {} {}", max_corner[0], max_corner[1]);
527 logger().debug(
" extent: {} {}", max_corner[0] - min_corner[0], max_corner[1] - min_corner[1]);
529 Eigen::MatrixXd p0, p1, p;
532 logger().debug(
"-- edge length after normalization:");
533 logger().debug(
" min: {}", p.rowwise().norm().minCoeff());
534 logger().debug(
" max: {}", p.rowwise().norm().maxCoeff());
535 logger().debug(
" avg: {}", p.rowwise().norm().mean());
540 const int v0 =
mesh_.edges.vertex(gid, 0);
541 const int v1 =
mesh_.edges.vertex(gid, 1);
548 mesh_.vertices.point(global_index).x = p(0);
549 mesh_.vertices.point(global_index).y = p(1);
554 const double *ptr =
mesh_.vertices.point_ptr(global_index);
650 const int v0 =
mesh_.edges.vertex(index, 0);
651 const int v1 =
mesh_.edges.vertex(index, 1);
672 for (
int e = 0; e <
n_edges(); ++e)
677 std::sort(vs.begin(), vs.end());
684 assert(
typeid(mesh) ==
typeid(
CMesh2D));
693 for (
int i = n_v; i < (int)
mesh_.vertices.nb(); ++i)
695 GEO::vec3 &p =
mesh_.vertices.point(i);
699 std::vector<GEO::index_t> indices;
700 for (
int i = 0; i < mesh2d.
n_faces(); ++i)
703 for (
int j = 0; j < mesh2d.
mesh_.facets.nb_vertices(i); ++j)
704 indices.push_back(mesh2d.
mesh_.facets.vertex(i, j) + n_v);
706 mesh_.facets.create_polygon(indices.size(), &indices[0]);
716 c2e_ = std::make_unique<GEO::Attribute<GEO::index_t>>(
mesh_.facet_corners.attributes(),
"edge_id");
718 boundary_edges_ = std::make_unique<GEO::Attribute<bool>>(
mesh_.edges.attributes(),
"boundary_edge");
723 std::unique_ptr<CMesh2D> copy_mesh = std::make_unique<CMesh2D>();
724 copy_mesh->load(this->
mesh_);
732 copy_mesh->orders_ = this->
orders_;
virtual RowVectorNd edge_barycenter(const int index) const override
edge barycenter
Navigation::Index switch_edge(Navigation::Index idx) const override
bool load(const std::string &path) override
loads a mesh from the path
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::unique_ptr< GEO::Attribute< bool > > boundary_edges_
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 is_boundary_edge(const int edge_global_id) const override
is edge boundary
std::unique_ptr< GEO::Attribute< GEO::index_t > > c2e_
std::unique_ptr< GEO::Attribute< bool > > boundary_vertices_
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
bool build_from_matrices(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F) override
build a mesh from matrices
void refine(const int n_refinement, const double t) override
refine the mesh
bool save(const std::string &path) const override
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.
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
bool is_rational_
stores if the mesh is rational
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 > geometry_ids_
list of geometry labels, one per top-dimensional element
std::vector< int > boundary_ids_
list of surface labels
std::vector< int > node_ids_
list of node labels
std::vector< CellNodes > cell_nodes_
high-order nodes associates to cells
std::vector< std::vector< double > > cell_weights_
weights associates to cells for rational polynomail meshes
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)
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.
void prepare_mesh(GEO::Mesh &M)
SplitFunction catmul_clark_split_func()
SplitFunction polar_split_func(double t)
Helper function.
void refine_triangle_mesh(const GEO::Mesh &M_in, GEO::Mesh &M_out)
Refine a triangle mesh.
void orient_normals_2d(GEO::Mesh &M)
Orient facets of a 2D mesh so that each connected component has positive volume.
void to_geogram_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, GEO::Mesh &M)
Converts a triangle mesh to a Geogram mesh.
void generate_edges(GEO::Mesh &M)
assing edges to M
void refine_polygonal_mesh(const GEO::Mesh &M_in, GEO::Mesh &M_out, Polygons::SplitFunction split_func)
Refine a polygonal mesh.
void compute_element_tags(const GEO::Mesh &M, std::vector< ElementType > &element_tags)
Compute the type of each facet in a surface mesh.
spdlog::logger & logger()
Retrieves the current logger.
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd