-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathDiscreteGradient.cpp
253 lines (202 loc) · 6.53 KB
/
DiscreteGradient.cpp
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
#include <DiscreteGradient.h>
using namespace std;
using namespace ttk;
using namespace dcg;
int DiscreteGradient::getDimensionality() const {
return dimensionality_;
}
int DiscreteGradient::getNumberOfDimensions() const {
return dimensionality_ + 1;
}
void DiscreteGradient::initMemory(const AbstractTriangulation &triangulation) {
Timer tm{};
const int numberOfDimensions = this->getNumberOfDimensions();
// init number of cells by dimension
std::vector<SimplexId> numberOfCells(numberOfDimensions);
for(int i = 0; i < numberOfDimensions; ++i) {
numberOfCells[i] = this->getNumberOfCells(i, triangulation);
}
dmtMax2PL_.clear();
dmt1Saddle2PL_.clear();
dmt2Saddle2PL_.clear();
// clear & init gradient memory
for(int i = 0; i < dimensionality_; ++i) {
(*gradient_)[2 * i].clear();
(*gradient_)[2 * i].resize(numberOfCells[i], -1);
(*gradient_)[2 * i + 1].clear();
(*gradient_)[2 * i + 1].resize(numberOfCells[i + 1], -1);
}
std::vector<std::vector<std::string>> rows{
{"#Vertices", std::to_string(numberOfCells[0])},
{"#Edges", std::to_string(numberOfCells[1])},
};
if(dimensionality_ >= 2) {
rows.emplace_back(
std::vector<std::string>{"#Triangles", std::to_string(numberOfCells[2])});
}
if(dimensionality_ == 3) {
rows.emplace_back(
std::vector<std::string>{"#Tetras", std::to_string(numberOfCells[3])});
}
this->printMsg(rows);
this->printMsg("Initialized discrete gradient memory", 1.0,
tm.getElapsedTime(), this->threadNumber_);
}
std::pair<size_t, SimplexId>
DiscreteGradient::numUnpairedFaces(const CellExt &c,
const lowerStarType &ls) const {
// c.dim_ cannot be <= 1
if(c.dim_ == 2) {
return numUnpairedFacesTriangle(c, ls);
} else if(c.dim_ == 3) {
return numUnpairedFacesTetra(c, ls);
}
return {0, -1};
}
std::pair<size_t, SimplexId>
DiscreteGradient::numUnpairedFacesTriangle(const CellExt &c,
const lowerStarType &ls) const {
// number of unpaired faces
std::pair<size_t, SimplexId> res{0, -1};
// loop over edge faces of triangle
// (2 edges per triangle in lower star)
for(size_t i = 0; i < 2; ++i) {
if(!ls[1][c.faces_[i]].paired_) {
res.first++;
res.second = c.faces_[i];
}
}
return res;
}
std::pair<size_t, SimplexId>
DiscreteGradient::numUnpairedFacesTetra(const CellExt &c,
const lowerStarType &ls) const {
// number of unpaired faces
std::pair<size_t, SimplexId> res{0, -1};
// loop over triangle faces of tetra
for(const auto f : c.faces_) {
if(!ls[2][f].paired_) {
res.first++;
res.second = f;
}
}
return res;
}
CriticalType
DiscreteGradient::criticalTypeFromCellDimension(const int dim) const {
if(dim == 0) {
return CriticalType::Local_minimum;
} else if(dim == 1) {
return CriticalType::Saddle1;
} else if(dim == 2 && dimensionality_ == 3) {
return CriticalType::Saddle2;
} else if(dim == dimensionality_) {
return CriticalType::Local_maximum;
} else {
return CriticalType::Regular;
}
}
bool DiscreteGradient::isMinimum(const Cell &cell) const {
if(cell.dim_ == 0) {
return ((*gradient_)[0][cell.id_] == -1);
}
return false;
}
bool DiscreteGradient::isSaddle1(const Cell &cell) const {
if(cell.dim_ == 1) {
return ((*gradient_)[1][cell.id_] == -1
and (*gradient_)[2][cell.id_] == -1);
}
return false;
}
bool DiscreteGradient::isSaddle2(const Cell &cell) const {
if(dimensionality_ == 3 and cell.dim_ == 2) {
return ((*gradient_)[3][cell.id_] == -1
and (*gradient_)[4][cell.id_] == -1);
}
return false;
}
bool DiscreteGradient::isMaximum(const Cell &cell) const {
if(dimensionality_ == 1 and cell.dim_ == 1) {
return ((*gradient_)[1][cell.id_] == -1);
}
if(dimensionality_ == 2 and cell.dim_ == 2) {
return ((*gradient_)[3][cell.id_] == -1);
}
if(dimensionality_ == 3 and cell.dim_ == 3) {
return ((*gradient_)[5][cell.id_] == -1);
}
return false;
}
bool DiscreteGradient::isCellCritical(const int cellDim,
const SimplexId cellId) const {
if(cellDim > this->dimensionality_) {
return false;
}
if(cellDim == 0) {
return ((*gradient_)[0][cellId] == -1);
}
if(cellDim == 1) {
return ((*gradient_)[1][cellId] == -1
&& (dimensionality_ == 1 || (*gradient_)[2][cellId] == -1));
}
if(cellDim == 2) {
return ((*gradient_)[3][cellId] == -1
&& (dimensionality_ == 2 || (*gradient_)[4][cellId] == -1));
}
if(cellDim == 3) {
return ((*gradient_)[5][cellId] == -1);
}
return false;
}
bool DiscreteGradient::isCellCritical(const Cell &cell) const {
return isCellCritical(cell.dim_, cell.id_);
}
int DiscreteGradient::getCriticalPointMap(
const vector<pair<SimplexId, char>> &criticalPoints, vector<char> &isPL) {
isPL.resize(numberOfVertices_);
std::fill(isPL.begin(), isPL.end(), 0);
for(pair<SimplexId, char> criticalPoint : criticalPoints) {
const SimplexId criticalPointId = criticalPoint.first;
const char criticalPointType = criticalPoint.second;
isPL[criticalPointId] = criticalPointType;
}
return 0;
}
int DiscreteGradient::setManifoldSize(
const size_t nCritPoints,
const std::vector<size_t> &nCriticalPointsByDim,
const SimplexId *const ascendingManifold,
const SimplexId *const descendingManifold,
std::vector<SimplexId> &manifoldSize) const {
// in the manifoldSize vector, critical points are sorted first by
// dim then by index
// ascendingManifold (resp. descendingManifold) region indices are
// numbered from 0 to #maxima == nCriticalPointsByDim.back()
// (resp. #minina == nCriticalPointsByDim[0])
if(nCritPoints == 0
|| (nCriticalPointsByDim[0] == 0 && nCriticalPointsByDim.back() == 0)) {
// no critical points || no extrema
return 0;
}
manifoldSize.resize(nCritPoints, 0);
// descending manifold cells size
if(nCriticalPointsByDim[0] > 0) {
for(SimplexId i = 0; i < numberOfVertices_; ++i) {
if(descendingManifold[i] != -1) {
manifoldSize[descendingManifold[i]]++;
}
}
}
if(nCriticalPointsByDim.back() > 0) {
// index of first maximum in critical points array
const auto nFirstMaximum{nCritPoints - nCriticalPointsByDim.back()};
// ascending manifold cells size
for(SimplexId i = 0; i < numberOfVertices_; ++i) {
if(ascendingManifold[i] != -1) {
manifoldSize[ascendingManifold[i] + nFirstMaximum]++;
}
}
}
return 0;
}