PolyFEM
Loading...
Searching...
No Matches
NCMesh2D.hpp
Go to the documentation of this file.
1#pragma once
2
4
6#include <set>
7
8namespace polyfem
9{
10 namespace mesh
11 {
32 class NCMesh2D : public Mesh2D
33 {
34 public:
36 {
37 int id;
38 double p1, p2;
39
40 follower_edge(int id_, double p1_, double p2_)
41 {
42 id = id_;
43 p1 = p1_;
44 p2 = p2_;
45 }
46 };
47
49 {
50 int id;
51 Eigen::Vector2d p1, p2, p3;
52
53 follower_face(int id_, Eigen::Vector2d p1_, Eigen::Vector2d p2_, Eigen::Vector2d p3_)
54 {
55 id = id_;
56 p1 = p1_;
57 p2 = p2_;
58 p3 = p3_;
59 }
60 };
61
62 struct ncVert
63 {
64 ncVert(const Eigen::VectorXd &pos_) : pos(pos_) {};
65
67 bool isboundary = false;
68
69 int edge = -1; // only used if the vertex is on the interior of an edge
70 int face = -1; // only 3d, only used if the vertex is on the interior of an face
71
72 double weight = -1.; // only 2d, the local position of this vertex on the edge
73
74 int n_elem = 0; // number of valid elements that share this vertex
75 };
76
78 {
79 ncBoundary(const Eigen::VectorXi &vert) : vertices(vert)
80 {
81 weights.setConstant(-1);
82 };
83
84 int n_elem() const
85 {
86 return elem_list.size();
87 }
88
89 void add_element(const int e)
90 {
92 };
93
94 void remove_element(const int e)
95 {
96 int num = elem_list.erase(e);
97 assert(num == 1);
98 }
99
100 int get_element() const
101 {
102 assert(n_elem() > 0);
103 auto it = elem_list.cbegin();
104 return *it;
105 }
106
107 int find_opposite_element(int e) const
108 {
109 assert(n_elem() == 2);
110 bool exist = false;
111 int oppo = -1;
112 for (int elem : elem_list)
113 if (elem == e)
114 exist = true;
115 else
116 oppo = elem;
117
118 assert(oppo >= 0);
119 assert(exist);
120 return oppo;
121 }
122
123 SortedIds elem_list; // elements that contain this edge/face
125
126 bool isboundary = false; // valid only after calling mark_boundary()
127 int boundary_id = -1;
128
129 int leader = -1; // if this edge/face lies on a larger edge/face
130 std::vector<int> followers; // followers of this edge/face
131
132 int leader_face = -1; // if this edge lies in the interior of a face
133
134 std::vector<int> global_ids; // only used for building basis
135
136 // the following only used if it's an edge
137 Eigen::Vector2d weights; // position of this edge on its leader edge
138 };
139
140 struct ncElem
141 {
142 ncElem(const int dim_, const Eigen::VectorXi &vertices_, const int level_, const int parent_) : dim(dim_), level(level_), parent(parent_), geom_vertices(vertices_)
143 {
145
146 assert(geom_vertices.size() == dim + 1);
147 edges.setConstant(3 * (dim - 1), 1, -1);
148 faces.setConstant(4 * (dim - 2), 1, -1);
149 children.setConstant(std::round(pow(2, dim)), 1, -1);
150 };
151
152 bool is_valid() const
153 {
154 return (!is_ghost) && (!is_refined);
155 };
156
157 bool is_not_valid() const
158 {
159 return is_ghost || is_refined;
160 };
161
162 int dim;
163 int level; // level of refinement
166
167 InlineVector<int, 3> vertices; // geom_vertices with a different order that used in polyfem
168
172
173 int body_id = 0;
174
175 bool is_refined = false;
176 bool is_ghost = false;
177 };
178
179 NCMesh2D() = default;
180 virtual ~NCMesh2D() = default;
181 NCMesh2D(NCMesh2D &&) = default;
183 NCMesh2D(const NCMesh2D &) = default;
184 NCMesh2D &operator=(const NCMesh2D &) = default;
185
186 bool is_conforming() const override { return false; }
187
188 int n_faces() const override { return n_elements; }
189 // The valid counts are the sizes of the valid -> all id maps while they
190 // are up to date (O(1)); otherwise they are counted (O(n)).
191 int n_edges() const override
192 {
193 if (index_prepared)
194 return valid_to_all_edgeMap.size();
195 int n = 0;
196 for (const auto &edge : edges)
197 if (edge.n_elem())
198 n++;
199 return n;
200 }
201 int n_vertices() const override
202 {
203 if (index_prepared)
204 return valid_to_all_vertexMap.size();
205 int n_verts = 0;
206 for (const auto &vert : vertices)
207 if (vert.n_elem)
208 n_verts++;
209
210 return n_verts;
211 }
212
213 inline int n_face_vertices(const int f_id) const override { return 3; }
214
215 inline int face_ref_level(const int f_id) const { return elements[valid_to_all_elem(f_id)].level; }
216
217 int face_vertex(const int f_id, const int lv_id) const override { return all_to_valid_vertex(elements[valid_to_all_elem(f_id)].vertices(lv_id)); }
218 int edge_vertex(const int e_id, const int lv_id) const override { return all_to_valid_vertex(edges[valid_to_all_edge(e_id)].vertices(lv_id)); }
219 int cell_vertex(const int f_id, const int lv_id) const override { return all_to_valid_vertex(elements[valid_to_all_elem(f_id)].vertices(lv_id)); }
220
221 int face_edge(const int f_id, const int le_id) const { return all_to_valid_edge(elements[valid_to_all_elem(f_id)].edges(le_id)); }
222 int leader_edge_of_vertex(const int v_id) const
223 {
224 if (!adj_prepared)
226 return (vertices[valid_to_all_vertex(v_id)].edge < 0) ? -1 : all_to_valid_edge(vertices[valid_to_all_vertex(v_id)].edge);
227 }
228 int leader_edge_of_edge(const int e_id) const
229 {
230 if (!adj_prepared)
232 return (edges[valid_to_all_edge(e_id)].leader < 0) ? -1 : all_to_valid_edge(edges[valid_to_all_edge(e_id)].leader);
233 }
234
235 // number of follower edges of a leader edge
236 int n_follower_edges(const int e_id) const
237 {
238 if (!adj_prepared)
240 return edges[valid_to_all_edge(e_id)].followers.size();
241 }
242 // number of elements have this edge
243 int n_face_neighbors(const int e_id) const { return edges[valid_to_all_edge(e_id)].n_elem(); }
244 // return the only element that has this edge
245 int face_neighbor(const int e_id) const { return all_to_valid_elem(edges[valid_to_all_edge(e_id)].get_element()); }
246
247 bool is_boundary_vertex(const int vertex_global_id) const override { return vertices[valid_to_all_vertex(vertex_global_id)].isboundary; }
248 bool is_boundary_edge(const int edge_global_id) const override { return edges[valid_to_all_edge(edge_global_id)].isboundary; }
249 bool is_boundary_element(const int element_global_id) const override;
250
251 void refine(const int n_refinement, const double t) override;
252
253 bool build_topology(const MeshData &data) override;
254
255 void attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector<std::vector<int>> &nodes) override;
256 std::pair<RowVectorNd, int> edge_node(const Navigation::Index &index, const int n_new_nodes, const int i) const override;
257 std::pair<RowVectorNd, int> face_node(const Navigation::Index &index, const int n_new_nodes, const int i, const int j) const override;
258
259 void normalize() override;
260
261 double edge_length(const int gid) const override;
262
263 void compute_elements_tag() override;
264 void update_elements_tag() override;
265
266 void set_point(const int global_index, const RowVectorNd &p) override;
267
268 RowVectorNd point(const int global_index) const override
269 {
270 return vertices[valid_to_all_vertex(global_index)].pos.transpose();
271 }
272 RowVectorNd edge_barycenter(const int index) const override;
273
274 void bounding_box(RowVectorNd &min, RowVectorNd &max) const override;
275
276 void compute_boundary_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &, bool)> &marker) override;
277
278 void compute_body_ids(const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &)> &marker) override;
279
280 void set_boundary_ids(const std::vector<int> &boundary_ids) override;
281 void set_body_ids(const std::vector<int> &body_ids) override;
282
283 int get_boundary_id(const int primitive) const override
284 {
285 // Without boundary ids, use the Mesh defaults (boundary: max int, interior: -1).
286 return has_boundary_ids() ? edges[valid_to_all_edge(primitive)].boundary_id : get_default_boundary_id(primitive);
287 };
288 int get_body_id(const int primitive) const override { return elements[valid_to_all_elem(primitive)].body_id; };
289
290 // Navigation wrapper
291 Navigation::Index get_index_from_face(int f, int lv = 0) const override;
292
293 // Navigation in a surface mesh
297
298 // void triangulate_faces(Eigen::MatrixXi &tris, Eigen::MatrixXd &pts, std::vector<int> &ranges) const override;
299
302 void refine_element(int id_full);
304 void refine_elements(const std::vector<int> &ids);
305
308 void coarsen_element(int id_full);
309
310 // mark the true boundary vertices
311 void mark_boundary();
312
313 // map the barycentric coordinate in element to the weight on edge
314 static double element_weight_to_edge_weight(const int l, const Eigen::Vector2d &pos);
315
316 // call necessary functions before building bases
326
327 void build_index_mapping();
328
329 void append(const Mesh &mesh) override;
330
331 std::unique_ptr<Mesh> copy() const override;
332
333 private:
335 {
336 long operator()(const Eigen::Vector2i &a) const
337 {
338 return (long)((long)984120265 * a[0] + (long)125965121 * a[1]);
339 }
340 };
341
342 protected:
343 void remove_elements(const std::vector<bool> &keep) override;
344
345 // Maps between the valid ids and the full ids (see the class comment).
346 // They are up to date only after prepare_mesh().
347 inline int all_to_valid_vertex(const int id) const { return map_id(all_to_valid_vertexMap, id); }
348 inline int valid_to_all_vertex(const int id) const { return map_id(valid_to_all_vertexMap, id); }
349 inline int all_to_valid_edge(const int id) const { return map_id(all_to_valid_edgeMap, id); }
350 inline int valid_to_all_edge(const int id) const { return map_id(valid_to_all_edgeMap, id); }
351 inline int all_to_valid_elem(const int id) const { return map_id(all_to_valid_elemMap, id); }
352 inline int valid_to_all_elem(const int id) const { return map_id(valid_to_all_elemMap, id); }
353 int map_id(const std::vector<int> &map, const int id) const
354 {
355 if (!index_prepared)
357 assert(id >= 0 && id < int(map.size()));
358 return map[id];
359 }
360
361 // find the mid-point of edge v[0]v[1], return -1 if not exists
362 int find_vertex(Eigen::Vector2i v) const;
363 int find_vertex(const int v1, const int v2) const { return find_vertex(Eigen::Vector2i(v1, v2)); };
364
365 // find the mid-point of edge v[0]v[1], create one if not exists
366 int get_vertex(Eigen::Vector2i v);
367
368 // find the edge v[0]v[1], return -1 if not exists
369 int find_edge(Eigen::Vector2i v) const;
370 int find_edge(const int v1, const int v2) const { return find_edge(Eigen::Vector2i(v1, v2)); };
371
372 // find the edge v[0]v[1], create one if not exists
373 int get_edge(Eigen::Vector2i v);
374 int get_edge(const int v1, const int v2) { return get_edge(Eigen::Vector2i(v1, v2)); };
375
376 // list all follower edges of a potential leader edge, returns nothing if it's a follower or conforming edge
377 void traverse_edge(Eigen::Vector2i v, double p1, double p2, int depth, std::vector<follower_edge> &list) const;
378
379 // call traverse_edge() for every interface, and store everything needed
381
382 // assign ncElement2D.leader_edges and ncVertex2D.weight
384
385 // edges are created if not exist
386 // return the id of this new element
387 int add_element(Eigen::Vector3i v, int parent = -1);
388
389 int n_elements = 0;
390
394 bool index_prepared = false;
395 bool adj_prepared = false;
396
398 [[noreturn]] void stale_index_error() const;
403
404 std::vector<ncElem> elements;
405 std::vector<ncVert> vertices;
406 std::vector<ncBoundary> edges;
407
408 std::unordered_map<Eigen::Vector2i, int, ArrayHasher2D> midpointMap;
409 std::unordered_map<Eigen::Vector2i, int, ArrayHasher2D> edgeMap;
410
414
415 std::vector<int> refineHistory;
416
417 // elementAdj(i, j) = 1 iff element i touches element j
418 Eigen::SparseMatrix<bool, Eigen::RowMajor> elementAdj;
419 };
420 } // namespace mesh
421} // namespace polyfem
int V
2D mesh interface: the elements are the faces (polygons) and the boundary elements are the edges.
Definition Mesh2D.hpp:18
Format-independent input used to construct a runtime Mesh.
Definition MeshData.hpp:12
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
bool has_boundary_ids() const
checks if surface selections are available
Definition Mesh.hpp:557
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
Non-conforming 2D triangle mesh with hanging nodes, for adaptive h-refinement: refine_element() split...
Definition NCMesh2D.hpp:33
int all_to_valid_elem(const int id) const
Definition NCMesh2D.hpp:351
int find_edge(Eigen::Vector2i v) const
Definition NCMesh2D.cpp:200
bool is_boundary_element(const int element_global_id) const override
is cell boundary
Definition NCMesh2D.cpp:60
void traverse_edge(Eigen::Vector2i v, double p1, double p2, int depth, std::vector< follower_edge > &list) const
Definition NCMesh2D.cpp:667
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
Definition NCMesh2D.cpp:907
Navigation::Index switch_face(Navigation::Index idx) const override
Definition NCMesh2D.cpp:772
std::vector< int > valid_to_all_edgeMap
Definition NCMesh2D.hpp:413
int n_face_neighbors(const int e_id) const
Definition NCMesh2D.hpp:243
NCMesh2D(NCMesh2D &&)=default
RowVectorNd point(const int global_index) const override
point coordinates
Definition NCMesh2D.hpp:268
Navigation::Index switch_vertex(Navigation::Index idx) const override
Definition NCMesh2D.cpp:723
int leader_edge_of_vertex(const int v_id) const
Definition NCMesh2D.hpp:222
std::unique_ptr< Mesh > copy() const override
Create a copy of the mesh.
Definition NCMesh2D.cpp:662
void attach_higher_order_nodes(const Eigen::MatrixXd &V, const std::vector< std::vector< int > > &nodes) override
attach high order nodes
Definition NCMesh2D.cpp:146
int face_edge(const int f_id, const int le_id) const
Definition NCMesh2D.hpp:221
bool build_topology(const MeshData &data) override
Construct only the dimension-specific topology.
Definition NCMesh2D.cpp:114
void refine_elements(const std::vector< int > &ids)
Refines the given elements (valid ids) and prepares the mesh.
Definition NCMesh2D.cpp:319
void prepare_mesh() override
method used to finalize the mesh.
Definition NCMesh2D.hpp:317
std::vector< ncVert > vertices
Definition NCMesh2D.hpp:405
std::vector< int > all_to_valid_elemMap
Definition NCMesh2D.hpp:411
bool is_conforming() const override
if the mesh is conforming
Definition NCMesh2D.hpp:186
static double element_weight_to_edge_weight(const int l, const Eigen::Vector2d &pos)
Definition NCMesh2D.cpp:507
int n_follower_edges(const int e_id) const
Definition NCMesh2D.hpp:236
void refine_element(int id_full)
Refines one element (full id).
Definition NCMesh2D.cpp:253
void set_boundary_ids(const std::vector< int > &boundary_ids) override
Set the boundary selection from a vector.
Definition NCMesh2D.cpp:897
int n_faces() const override
number of faces
Definition NCMesh2D.hpp:188
void set_point(const int global_index, const RowVectorNd &p) override
Set the point.
Definition NCMesh2D.cpp:817
int all_to_valid_vertex(const int id) const
Definition NCMesh2D.hpp:347
std::vector< ncElem > elements
Definition NCMesh2D.hpp:404
int find_vertex(const int v1, const int v2) const
Definition NCMesh2D.hpp:363
void set_body_ids(const std::vector< int > &body_ids) override
Set the volume sections.
Definition NCMesh2D.cpp:887
Navigation::Index switch_edge(Navigation::Index idx) const override
Definition NCMesh2D.cpp:747
int get_edge(Eigen::Vector2i v)
Definition NCMesh2D.cpp:210
int add_element(Eigen::Vector3i v, int parent=-1)
Definition NCMesh2D.cpp:223
void coarsen_element(int id_full)
Coarsens the parent of one element (full id).
Definition NCMesh2D.cpp:332
int get_vertex(Eigen::Vector2i v)
Definition NCMesh2D.cpp:186
Navigation::Index get_index_from_face(int f, int lv=0) const override
Definition NCMesh2D.cpp:710
double edge_length(const int gid) const override
edge length
Definition NCMesh2D.cpp:800
int get_boundary_id(const int primitive) const override
Get the boundary selection of an element (face in 3d, edge in 2d)
Definition NCMesh2D.hpp:283
int valid_to_all_edge(const int id) const
Definition NCMesh2D.hpp:350
int n_vertices() const override
number of vertices
Definition NCMesh2D.hpp:201
bool is_boundary_edge(const int edge_global_id) const override
is edge boundary
Definition NCMesh2D.hpp:248
int get_body_id(const int primitive) const override
Get the volume selection of an element (cell in 3d, face in 2d)
Definition NCMesh2D.hpp:288
void refine(const int n_refinement, const double t) override
refine the mesh
Definition NCMesh2D.cpp:69
std::pair< RowVectorNd, int > face_node(const Navigation::Index &index, const int n_new_nodes, const int i, const int j) const override
Definition NCMesh2D.cpp:161
void normalize() override
normalize the mesh
Definition NCMesh2D.cpp:788
std::vector< int > refineHistory
Definition NCMesh2D.hpp:415
void stale_index_error() const
Throws: the id maps are stale (prepare_mesh() was not called after editing the mesh).
Definition NCMesh2D.cpp:109
int valid_to_all_elem(const int id) const
Definition NCMesh2D.hpp:352
int map_id(const std::vector< int > &map, const int id) const
Definition NCMesh2D.hpp:353
std::vector< int > valid_to_all_elemMap
Definition NCMesh2D.hpp:411
int edge_vertex(const int e_id, const int lv_id) const override
id of the edge vertex
Definition NCMesh2D.hpp:218
int leader_edge_of_edge(const int e_id) const
Definition NCMesh2D.hpp:228
std::vector< int > all_to_valid_edgeMap
Definition NCMesh2D.hpp:413
std::vector< int > all_to_valid_vertexMap
Definition NCMesh2D.hpp:412
std::unordered_map< Eigen::Vector2i, int, ArrayHasher2D > midpointMap
Definition NCMesh2D.hpp:408
std::vector< ncBoundary > edges
Definition NCMesh2D.hpp:406
int find_edge(const int v1, const int v2) const
Definition NCMesh2D.hpp:370
void compute_elements_tag() override
compute element types, see ElementType
Definition NCMesh2D.cpp:808
int face_vertex(const int f_id, const int lv_id) const override
id of the face vertex
Definition NCMesh2D.hpp:217
RowVectorNd edge_barycenter(const int index) const override
edge barycenter
Definition NCMesh2D.cpp:822
int all_to_valid_edge(const int id) const
Definition NCMesh2D.hpp:349
std::unordered_map< Eigen::Vector2i, int, ArrayHasher2D > edgeMap
Definition NCMesh2D.hpp:409
int find_vertex(Eigen::Vector2i v) const
Definition NCMesh2D.cpp:176
void bounding_box(RowVectorNd &min, RowVectorNd &max) const override
computes the bbox of the mesh
Definition NCMesh2D.cpp:693
int cell_vertex(const int f_id, const int lv_id) const override
id of the vertex of a cell
Definition NCMesh2D.hpp:219
int face_ref_level(const int f_id) const
Definition NCMesh2D.hpp:215
int n_face_vertices(const int f_id) const override
number of vertices of a face
Definition NCMesh2D.hpp:213
int valid_to_all_vertex(const int id) const
Definition NCMesh2D.hpp:348
virtual ~NCMesh2D()=default
bool index_prepared
Whether the valid <-> all id maps match the current topology.
Definition NCMesh2D.hpp:394
void build_element_vertex_adjacency()
Definition NCMesh2D.cpp:455
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
Definition NCMesh2D.cpp:920
int n_edges() const override
number of edges
Definition NCMesh2D.hpp:191
NCMesh2D & operator=(const NCMesh2D &)=default
int get_edge(const int v1, const int v2)
Definition NCMesh2D.hpp:374
NCMesh2D & operator=(NCMesh2D &&)=default
int face_neighbor(const int e_id) const
Definition NCMesh2D.hpp:245
bool is_boundary_vertex(const int vertex_global_id) const override
is vertex boundary
Definition NCMesh2D.hpp:247
Eigen::SparseMatrix< bool, Eigen::RowMajor > elementAdj
Definition NCMesh2D.hpp:418
NCMesh2D(const NCMesh2D &)=default
std::vector< int > valid_to_all_vertexMap
Definition NCMesh2D.hpp:412
std::pair< RowVectorNd, int > edge_node(const Navigation::Index &index, const int n_new_nodes, const int i) const override
Definition NCMesh2D.cpp:152
void update_elements_tag() override
Update elements types.
Definition NCMesh2D.cpp:812
void append(const Mesh &mesh) override
appends a new mesh to the end of this
Definition NCMesh2D.cpp:574
void update_labels_after_refinement()
After a refinement: rebuild the body/boundary id arrays from the per-entity labels (inherited by the ...
Definition NCMesh2D.cpp:91
void remove_elements(const std::vector< bool > &keep) override
Remove all top-dimensional elements whose mask entry is false.
Definition NCMesh2D.cpp:13
Set of ids stored as a sorted vector: iterates in increasing order like std::set<int>,...
void insert(const int id)
const_iterator cbegin() const
size_t erase(const int id)
Removes id; returns the number of removed ids (0 or 1).
Eigen::Matrix< T, Eigen::Dynamic, 1, Eigen::ColMajor, MaxSize, 1 > InlineVector
Small vector stored inline (no heap allocation), with the VectorXi / VectorXd API; used for the per-e...
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13
long operator()(const Eigen::Vector2i &a) const
Definition NCMesh2D.hpp:336
follower_edge(int id_, double p1_, double p2_)
Definition NCMesh2D.hpp:40
follower_face(int id_, Eigen::Vector2d p1_, Eigen::Vector2d p2_, Eigen::Vector2d p3_)
Definition NCMesh2D.hpp:53
ncBoundary(const Eigen::VectorXi &vert)
Definition NCMesh2D.hpp:79
InlineVector< int, 2 > vertices
Definition NCMesh2D.hpp:124
InlineVector< int, 1 > faces
Definition NCMesh2D.hpp:170
InlineVector< int, 3 > vertices
Definition NCMesh2D.hpp:167
InlineVector< int, 3 > geom_vertices
Definition NCMesh2D.hpp:165
InlineVector< int, 3 > edges
Definition NCMesh2D.hpp:169
ncElem(const int dim_, const Eigen::VectorXi &vertices_, const int level_, const int parent_)
Definition NCMesh2D.hpp:142
InlineVector< int, 4 > children
Definition NCMesh2D.hpp:171
ncVert(const Eigen::VectorXd &pos_)
Definition NCMesh2D.hpp:64
InlineVector< double, 3 > pos
Definition NCMesh2D.hpp:66