Repository navigation
Expand file tree
/
Copy pathsimplicial-complex-operators.cpp
More file actions
368 lines (309 loc) · 11 KB
/
Copy pathsimplicial-complex-operators.cpp
File metadata and controls
368 lines (309 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Implement member functions for SimplicialComplexOperators class.
#include "simplicial-complex-operators.h"
using namespace geometrycentral;
using namespace geometrycentral::surface;
/*
* Assign a unique index to each vertex, edge, and face of a mesh.
* All elements are 0-indexed.
*
* Input: None. Access geometry via the member variable <geometry>, and pointer to the mesh via <mesh>.
* Returns: None.
*/
void SimplicialComplexOperators::assignElementIndices() {
// Needed to access geometry->vertexIndices, etc. as cached quantities.
// Not needed if you're just using v->getIndex(), etc.
geometry->requireVertexIndices();
geometry->requireEdgeIndices();
geometry->requireFaceIndices();
// You can set the index field of a vertex via geometry->vertexIndices[v], where v is a Vertex object (or an
// integer). Similarly you can do edges and faces via geometry->edgeIndices, geometry->faceIndices, like so:
size_t idx = 0;
for (Vertex v : mesh->vertices()) {
idx = geometry->vertexIndices[v];
}
for (Edge e : mesh->edges()) {
idx = geometry->edgeIndices[e];
}
for (Face f : mesh->faces()) {
idx = geometry->faceIndices[f];
}
// You can more easily get the indices of mesh elements using the function getIndex(), albeit less efficiently and
// technically less safe (although you don't need to worry about it), like so:
//
// v.getIndex()
//
// where v can be a Vertex, Edge, Face, Halfedge, etc. For example:
for (Vertex v : mesh->vertices()) {
idx = v.getIndex(); // == geometry->vertexIndices[v])
}
// Geometry Central already sets the indices for us, though, so this function is just here for demonstration.
// You don't have to do anything :)
}
/*
* Construct the unsigned vertex-edge adjacency matrix A0.
*
* Input:
* Returns: The sparse vertex-edge adjacency matrix which gets stored in the global variable A0.
*/
SparseMatrix<size_t> SimplicialComplexOperators::buildVertexEdgeAdjacencyMatrix() const {
// TODO
// Note: You can build an Eigen sparse matrix from triplets, then return it as a Geometry Central SparseMatrix.
// See <https://eigen.tuxfamily.org/dox/group__TutorialSparse.html> for documentation.
size_t nVertices = mesh->nVertices();
size_t nEdges = mesh->nEdges();
Eigen::SparseMatrix<size_t> M(nEdges, nVertices);
std::vector<Eigen::Triplet<size_t>> T;
for (Edge e : mesh->edges())
{
size_t eIndex = e.getIndex();
T.emplace_back(eIndex, e.firstVertex().getIndex(), 1);
T.emplace_back(eIndex, e.secondVertex().getIndex(), 1);
}
M.setFromTriplets(T.begin(), T.end());
M.makeCompressed();
return M; // placeholder
}
/*
* Construct the unsigned face-edge adjacency matrix A1.
*
* Input:
* Returns: The sparse face-edge adjacency matrix which gets stored in the global variable A1.
*/
SparseMatrix<size_t> SimplicialComplexOperators::buildFaceEdgeAdjacencyMatrix() const {
size_t nFaces = mesh->nFaces();
size_t nEdges = mesh->nEdges();
Eigen::SparseMatrix<size_t> M(nFaces, nEdges);
std::vector<Eigen::Triplet<size_t>> T;
for (Face f : mesh->faces())
{
size_t fIndex = f.getIndex();
for (Edge e : f.adjacentEdges())
{
T.emplace_back(fIndex, e.getIndex(), 1);
}
}
M.setFromTriplets(T.begin(), T.end());
M.makeCompressed();
return M; // placeholder
}
/*
* Construct a vector encoding the vertices in the selected subset of simplices.
*
* Input: Selected subset of simplices.
* Returns: Vector of length |V|, where |V| = # of vertices in the mesh.
*/
Vector<size_t> SimplicialComplexOperators::buildVertexVector(const MeshSubset& subset) const {
size_t nVertices = mesh->nVertices();
Vector<size_t> vertexVector(nVertices);
vertexVector.setZero();
for (size_t iVertex : subset.vertices)
{
vertexVector(iVertex, 0) = 1;
}
return vertexVector;
}
/*
* Construct a vector encoding the edges in the selected subset of simplices.
*
* Input: Selected subset of simplices.
* Returns: Vector of length |E|, where |E| = # of edges in mesh.
*/
Vector<size_t> SimplicialComplexOperators::buildEdgeVector(const MeshSubset& subset) const {
size_t nEdges = mesh->nEdges();
Vector<size_t> edgeVector(nEdges);
edgeVector.setZero();
for (size_t iEdge : subset.edges)
{
edgeVector(iEdge, 0) = 1;
}
return edgeVector;
}
/*
* Construct a vector encoding the faces in the selected subset of simplices.
*
* Input: Selected subset of simplices.
* Returns: Vector of length |F|, where |F| = # of faces in mesh.
*/
Vector<size_t> SimplicialComplexOperators::buildFaceVector(const MeshSubset& subset) const {
size_t nFaces = mesh->nFaces();
Vector<size_t> faceVector(nFaces);
faceVector.setZero();
for (size_t iFace : subset.faces)
{
faceVector(iFace, 0) = 1;
}
return faceVector;
}
/*
* Compute the simplicial star St(S) of the selected subset of simplices.
*
* Input: A MeshSubset object containing the indices of the currently active vertices, edges, and faces, respectively.
* Returns: The star of the given subset.
*/
MeshSubset SimplicialComplexOperators::star(const MeshSubset& subset) const {
//1.Deep copy a subset as a star
MeshSubset star = subset.deepCopy();
//2.Use vertex-edge matrix to find all the edges
Vector<size_t> edgeVector = this->A0 * this->buildVertexVector(star);
size_t edgeLength = edgeVector.size();
for (size_t i = 0; i < edgeLength; i++)
{
if (edgeVector[i] != 0)
{
star.addEdge(i);
}
}
//3.Use edge-face matrix to find all the faces
Vector<size_t> faceVector = this->A1 * this->buildEdgeVector(star);
size_t faceLength = faceVector.size();
for (size_t i = 0; i < faceLength; i++)
{
if (faceVector[i] != 0)
{
star.addFace(i);
}
}
//4.Return the star
return star;
}
/*
* Compute the closure Cl(S) of the selected subset of simplices.
*
* Input: A MeshSubset object containing the indices of the currently active vertices, edges, and faces, respectively.
* Returns: The closure of the given subset.
*/
MeshSubset SimplicialComplexOperators::closure(const MeshSubset& subset) const {
//1. Make a deepcopy of the input simplices
MeshSubset closure = subset.deepCopy();
//2. Use transpose of edge-face matrix, to have all the edges
Vector<size_t> edgeVector = this->A1.transpose() * this->buildFaceVector(closure);
size_t edgeLength = edgeVector.size();
for (size_t i = 0; i < edgeLength; i++)
{
if (edgeVector[i] != 0)
{
closure.addEdge(i);
}
}
//3. Use the transpose of vertex-edge matrix, to have all the vertices
Vector<size_t> vertexVector = this->A0.transpose() * this->buildEdgeVector(closure);
size_t vertexLength = vertexVector.size();
for (size_t i = 0; i < vertexLength; i++)
{
if (vertexVector[i] != 0)
{
closure.addVertex(i);
}
}
//4. Return the closure
return closure;
}
/*
* Compute the link Lk(S) of the selected subset of simplices.
*
* Input: A MeshSubset object containing the indices of the currently active vertices, edges, and faces, respectively.
* Returns: The link of the given subset.
*/
MeshSubset SimplicialComplexOperators::link(const MeshSubset& subset) const {
MeshSubset closureStar = SimplicialComplexOperators::closure(SimplicialComplexOperators::star(subset));
MeshSubset starClosure = SimplicialComplexOperators::star(SimplicialComplexOperators::closure(subset));
closureStar.deleteSubset(starClosure);
return closureStar;
}
/*
* Return true if the selected subset is a simplicial complex, false otherwise.
*
* Input: A MeshSubset object containing the indices of the currently active vertices, edges, and faces, respectively.
* Returns: True if given subset is a simplicial complex, false otherwise.
*/
bool SimplicialComplexOperators::isComplex(const MeshSubset& subset) const {
MeshSubset closure = SimplicialComplexOperators::closure(subset);
bool flag = subset.equals(closure);
return flag;
}
/*
* Check if the given subset S is a pure simplicial complex. If so, return the degree of the complex. Otherwise, return
* -1.
*
* Input: A MeshSubset object containing the indices of the currently active vertices, edges, and faces, respectively.
* Returns: int representing the degree of the given complex (-1 if not pure)
*/
int SimplicialComplexOperators::isPureComplex(const MeshSubset& subset) const {
//1. Check if it is a complex
if (!SimplicialComplexOperators::isComplex(subset))
{
return -1;
}
//2. Prepare the ingredients for computing
Vector<size_t> vertexVector = this->buildVertexVector(subset);
Vector<size_t> edgeVector = this->buildEdgeVector(subset);
Vector<size_t> faceVector = this->buildFaceVector(subset);
size_t nVertices = mesh->nVertices();
//3. See if it is a 2-complex
if (subset.faces.size() > 0)
{
Vector<size_t> faceVertices = this->A0.transpose().operator*(this->A1.transpose().operator*(faceVector));
for (size_t i = 0; i < nVertices; i++)
{
if (faceVertices(i, 0) == 0 && vertexVector(i, 0) != 0)
{
return -1;
}
}
return 2;
}
//4. See if it is a 1-complex
else if (subset.edges.size() > 0)
{
Vector<size_t> edgeVertices = this->A0.transpose().operator*(edgeVector);
for (size_t i = 0; i < nVertices; i++)
{
if (edgeVertices(i, 0) == 0 && vertexVector(i, 0) != 0)
{
return -1;
}
}
return 1;
}
//5. See if it is a 0-complex
else if (subset.vertices.size() > 0)
{
return 0;
}
else
{
return 0;
}
}
/*
* Compute the set of simplices contained in the boundary bd(S) of the selected subset S of simplices.
*
* Input: A MeshSubset object containing the indices of the currently active vertices, edges, and faces, respectively.
* Returns: The boundary of the given subset.
*/
MeshSubset SimplicialComplexOperators::boundary(const MeshSubset& subset) const {
//1. Made a brandnew empty meshsubset
MeshSubset boundary;
//2.1. If it has faces
if (subset.faces.size() > 0) {
Vector<size_t> faceEdges = this->A1.transpose().operator*(this->buildFaceVector(subset));
size_t nEdges = mesh->nEdges();
for (size_t i = 0; i < nEdges; i++) {
if (faceEdges(i, 0) == 1) {
boundary.addEdge(i);
}
}
}
//2.2. If it has edges
else if (subset.edges.size() > 0) {
Vector<size_t> edgeVertices = this->A0.transpose().operator*(this->buildEdgeVector(subset));
size_t nVertices = mesh->nVertices();
for (size_t i = 0; i < nVertices; i++) {
if (edgeVertices(i, 0) == 1) {
boundary.addVertex(i);
}
}
}
//3. Return
return this->closure(boundary);
}