PolyFEM
Loading...
Searching...
No Matches
PolygonMesh.cpp
Go to the documentation of this file.
1#include "PolygonMesh.hpp"
2
3#include <algorithm>
4#include <cassert>
5#include <limits>
6#include <stack>
7
8namespace polyfem::mesh
9{
11 {
12 points_.clear();
13 facet_ptr_.assign(1, 0);
14 corner_vertex_.clear();
16 edges_.clear();
17 corner_edge.clear();
18 boundary_edge.clear();
19 boundary_vertex.clear();
20 }
21
23 {
24 const int first = n_vertices();
25 points_.resize(first + n);
26 boundary_vertex.resize(points_.size(), false);
27 return first;
28 }
29
31 {
32 const int v = create_vertices(1);
33 points_[v] = p;
34 return v;
35 }
36
37 void PolygonMesh::bounding_box(Point &min, Point &max) const
38 {
39 for (int c = 0; c < 3; ++c)
40 {
41 min[c] = std::numeric_limits<double>::max();
42 max[c] = std::numeric_limits<double>::lowest();
43 }
44 for (const Point &p : points_)
45 {
46 for (int c = 0; c < 3; ++c)
47 {
48 min[c] = std::min(min[c], p[c]);
49 max[c] = std::max(max[c], p[c]);
50 }
51 }
52 }
53
54 int PolygonMesh::create_polygon(const std::vector<int> &vertices)
55 {
56 const int f = n_facets();
57 for (const int v : vertices)
58 {
59 assert(v >= 0 && v < n_vertices());
60 corner_vertex_.push_back(v);
61 corner_adjacent_facet_.push_back(-1);
62 }
63 corner_edge.resize(corner_vertex_.size(), 0);
64 facet_ptr_.push_back(corner_vertex_.size());
65 return f;
66 }
67
68 void PolygonMesh::flip_facet(const int f)
69 {
70 const int d = n_facet_vertices(f);
71 const int c0 = corners_begin(f);
72 const std::vector<int> vertices(corner_vertex_.begin() + c0, corner_vertex_.begin() + c0 + d);
73 const std::vector<int> adjacent(corner_adjacent_facet_.begin() + c0, corner_adjacent_facet_.begin() + c0 + d);
74 for (int i = 0; i < d; ++i)
75 {
76 const int i_v = d - 1 - i;
77 // The edge starting at the new corner i is the reversed edge that
78 // started at the old corner before i_v.
79 const int i_f = (i_v == 0) ? d - 1 : i_v - 1;
80 corner_vertex_[c0 + i] = vertices[i_v];
81 corner_adjacent_facet_[c0 + i] = adjacent[i_f];
82 }
83 }
84
86 {
87 facet_ptr_.assign(1, 0);
88 corner_vertex_.clear();
90 corner_edge.clear();
91 }
92
93 void PolygonMesh::delete_facets(const std::vector<bool> &to_delete)
94 {
95 assert(to_delete.size() == n_facets());
96
97 std::vector<int> facets_old2new(n_facets(), -1);
98 std::vector<int> new_facet_ptr = {0};
99 int new_nb_corners = 0;
100 for (int f = 0; f < n_facets(); ++f)
101 {
102 if (to_delete[f])
103 continue;
104 facets_old2new[f] = new_facet_ptr.size() - 1;
105 for (int c = corners_begin(f); c < corners_end(f); ++c)
106 {
107 corner_vertex_[new_nb_corners] = corner_vertex_[c];
109 corner_edge[new_nb_corners] = corner_edge[c];
110 ++new_nb_corners;
111 }
112 new_facet_ptr.push_back(new_nb_corners);
113 }
114 corner_vertex_.resize(new_nb_corners);
115 corner_adjacent_facet_.resize(new_nb_corners);
116 corner_edge.resize(new_nb_corners);
117 facet_ptr_ = std::move(new_facet_ptr);
118
119 for (int &f : corner_adjacent_facet_)
120 {
121 if (f != -1)
122 f = facets_old2new[f];
123 }
124 }
125
127 {
128 // Chain the corners around each vertex.
129 std::vector<int> next_corner_around_vertex(n_corners(), -1);
130 std::vector<int> v2c(n_vertices(), -1);
131 std::vector<int> c2f(n_corners(), -1);
132 for (int f = 0; f < n_facets(); ++f)
133 {
134 for (int c = corners_begin(f); c < corners_end(f); ++c)
135 {
136 const int v = corner_vertex_[c];
137 next_corner_around_vertex[c] = v2c[v];
138 v2c[v] = c;
139 c2f[c] = f;
140 }
141 }
142
143 // Glue each free corner c1 (edge v1 -> v2) to an earlier corner c2 at v1
144 // whose facet has the opposite edge v2 -> v1.
145 for (int f1 = 0; f1 < n_facets(); ++f1)
146 {
147 for (int c1 = corners_begin(f1); c1 < corners_end(f1); ++c1)
148 {
149 if (corner_adjacent_facet_[c1] != -1)
150 continue;
151 const int v2 = corner_vertex_[next_corner_around_facet(f1, c1)];
152 // The chain only links each corner to earlier ones, so c2 != c1.
153 for (int c2 = next_corner_around_vertex[c1]; c2 != -1; c2 = next_corner_around_vertex[c2])
154 {
155 const int f2 = c2f[c2];
156 const int c3 = prev_corner_around_facet(f2, c2);
157 if (corner_vertex_[c3] == v2)
158 {
159 corner_adjacent_facet_[c1] = f2;
160 corner_adjacent_facet_[c3] = f1;
161 break;
162 }
163 }
164 }
165 }
166 }
167
168 int PolygonMesh::connected_components(std::vector<int> &component) const
169 {
170 int nb_components = 0;
171 component.assign(n_facets(), -1);
172 for (int f = 0; f < n_facets(); ++f)
173 {
174 if (component[f] != -1)
175 continue;
176 std::stack<int> S;
177 S.push(f);
178 component[f] = nb_components;
179 do
180 {
181 const int cur_f = S.top();
182 S.pop();
183 for (int c = corners_begin(cur_f); c < corners_end(cur_f); ++c)
184 {
185 const int adj_f = corner_adjacent_facet_[c];
186 if (adj_f != -1 && component[adj_f] == -1)
187 {
188 S.push(adj_f);
189 component[adj_f] = nb_components;
190 }
191 }
192 } while (!S.empty());
193 nb_components++;
194 }
195 return nb_components;
196 }
197
198 int PolygonMesh::create_edge(const int v0, const int v1)
199 {
200 edges_.push_back({{v0, v1}});
201 boundary_edge.push_back(false);
202 return edges_.size() - 1;
203 }
204
206 {
207 edges_.clear();
208 boundary_edge.clear();
209 }
210} // namespace polyfem::mesh
std::vector< int > corner_vertex_
int create_edge(int v0, int v1)
void flip_facet(int f)
Reverses the orientation of facet f (its vertex list is reversed).
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,...
std::vector< Point > points_
std::vector< int > facet_ptr_
int next_corner_around_facet(const int f, const int c) const
void connect_facets()
Glues every corner without neighbor to the facet sharing its edge in opposite orientation (already gl...
int corners_end(const int f) const
int n_facet_vertices(const int f) const
std::vector< bool > boundary_edge
Whether each edge is on the boundary.
std::vector< std::array< int, 2 > > edges_
std::vector< bool > boundary_vertex
Whether each vertex is on the boundary.
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...
int prev_corner_around_facet(const int f, const int c) const
int create_vertex(const Point &p)
Appends a vertex and returns its index.
int corners_begin(const int f) const
int connected_components(std::vector< int > &component) const
Assigns to each facet the index of its connected component (through the corner adjacency) and returns...
std::vector< int > corner_edge
Index of the edge starting at each corner.
void clear_facets()
Removes all facets, keeping the vertices and the edges.
std::vector< int > corner_adjacent_facet_