Repository navigation
Expand file tree
/
Copy pathCutter.cpp
More file actions
582 lines (477 loc) · 18.6 KB
/
Copy pathCutter.cpp
File metadata and controls
582 lines (477 loc) · 18.6 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include "Cutter.h"
namespace CEPS {
namespace ImplementationDetails {
// ==========================================================================
// Compute Mesh Cuts
// ==========================================================================
std::set<Edge> goodCuts(ManifoldSurfaceMesh& mesh,
const std::vector<Vertex>& cones,
const EdgeData<double>& edgeCost) {
return goodCuts(mesh, cones, edgeCost, {});
}
std::set<Edge> goodCuts(ManifoldSurfaceMesh& mesh,
const std::vector<Vertex>& cones,
const EdgeData<double>& edgeCost,
std::set<Edge> startingCut) {
// Add in homology generators to cut
std::vector<std::vector<Edge>> homologyGenerators =
computeHomologyGenerators(mesh, edgeCost);
for (const std::vector<Edge>& generator : homologyGenerators) {
for (Edge e : generator) startingCut.insert(e);
}
std::set<Vertex> unusedCones(std::begin(cones), std::end(cones));
for (Edge e : startingCut) {
unusedCones.erase(e.halfedge().vertex());
unusedCones.erase(e.halfedge().twin().vertex());
}
std::set<Edge> cutEdges = startingCut;
// initialize data structures
size_t nV = (size_t)mesh.nVertices();
DisjointSets ds(nV);
Halfedge nullHe;
VertexData<Halfedge> parent(mesh, nullHe);
std::priority_queue<VertexEntry, std::vector<VertexEntry>,
std::greater<VertexEntry>>
pq;
VertexData<size_t> vIdx = mesh.getVertexIndices();
// add starting cut to pq
for (Edge e : startingCut) {
ds.merge(vIdx[src(e)], vIdx[dst(e)]);
std::array<Vertex, 2> neigh{src(e), dst(e)};
for (Vertex v : neigh) {
for (Halfedge vHe : v.outgoingHalfedges()) {
pq.push(VertexEntry(edgeCost[vHe.edge()], vHe.twin().vertex(),
vHe));
}
ds.mark(vIdx[v]);
}
}
// add cone neighbors to pq
for (size_t i = 0; i < (size_t)cones.size(); i++) {
for (Halfedge he : cones[i].outgoingHalfedges()) {
pq.push(VertexEntry(edgeCost[he.edge()], he.twin().vertex(), he));
}
// mark set
ds.mark(vIdx[cones[i]]);
}
// construct approximate steiner tree
while (!pq.empty()) {
VertexEntry entry = pq.top();
pq.pop();
double weight = std::get<0>(entry);
Halfedge he = std::get<2>(entry);
Vertex v1 = std::get<1>(entry);
Vertex v2 = he.vertex();
if (ds.find(vIdx[v1]) != ds.find(vIdx[v2])) {
// if merging two marked sets, then mark edges that connect
// these two regions as cut edges
if (ds.isMarked(vIdx[v1]) && ds.isMarked(vIdx[v2])) {
// one side
Halfedge currHe = parent[v1];
while (currHe != nullHe) {
cutEdges.insert(currHe.edge());
currHe = parent[currHe.vertex()];
}
// bridge
cutEdges.insert(he.edge());
// other side
currHe = parent[v2];
while (currHe != nullHe) {
cutEdges.insert(currHe.edge());
currHe = parent[currHe.vertex()];
}
}
// record potential parent and merge sets
parent[v1] = he;
ds.merge(vIdx[v1], vIdx[v2]);
for (Halfedge vHe : v1.outgoingHalfedges()) {
pq.push(VertexEntry(edgeCost[vHe.edge()] + weight,
vHe.twin().vertex(), vHe));
}
}
}
return cutEdges;
}
std::set<Edge> bestCuts(ManifoldSurfaceMesh& mesh,
const std::vector<Vertex>& cones,
const EdgeData<double>& edgeCost) {
return bestCuts(mesh, cones, edgeCost, {});
}
std::set<Edge> bestCuts(ManifoldSurfaceMesh& mesh,
const std::vector<Vertex>& cones,
const EdgeData<double>& edgeCost,
std::set<Edge> startingCut) {
std::set<Vertex> unusedCones(std::begin(cones), std::end(cones));
for (Edge e : startingCut) {
unusedCones.erase(e.halfedge().vertex());
unusedCones.erase(e.halfedge().twin().vertex());
}
std::set<Edge> cutEdges = startingCut;
std::set<Vertex> cutVertices;
if (cutEdges.empty()) {
cutVertices.insert(cones[0]);
unusedCones.erase(cones[0]);
} else {
for (Edge e : cutEdges) {
cutVertices.insert(e.halfedge().vertex());
cutVertices.insert(e.halfedge().twin().vertex());
}
}
std::map<Vertex, VertexData<double>> pathDistances;
pathDistances[cones[0]] = distances(mesh, edgeCost, cones[0]);
while (!unusedCones.empty()) {
for (Vertex v : cutVertices) {
if (pathDistances.find(v) == pathDistances.end()) {
pathDistances[v] = distances(mesh, edgeCost, v);
}
}
Vertex cheapestDst;
Vertex cheapestDstSrc;
double cheapestDstCost = std::numeric_limits<double>::infinity();
for (Vertex dst : unusedCones) {
Vertex bestSrc;
double bestCost = std::numeric_limits<double>::infinity();
for (Vertex src : cutVertices) {
if (pathDistances[src][dst] < bestCost) {
bestCost = pathDistances[src][dst];
bestSrc = src;
}
}
if (bestCost < cheapestDstCost) {
cheapestDstCost = bestCost;
cheapestDst = dst;
cheapestDstSrc = bestSrc;
}
}
std::set<Edge> pathEdges =
shortestPath(mesh, edgeCost, cheapestDst, cheapestDstSrc);
for (Edge e : pathEdges) {
cutEdges.insert(e);
cutVertices.insert(e.halfedge().vertex());
cutVertices.insert(e.halfedge().twin().vertex());
}
unusedCones.erase(cheapestDst);
}
return cutEdges;
}
std::set<Edge> cutOutFront(ManifoldSurfaceMesh& mesh,
const std::set<Vertex>& frontVertices,
const std::vector<Vertex>& boundaryVertices) {
EdgeData<double> edgeCost(mesh, 0.01);
for (Vertex v : frontVertices) {
for (Edge e : v.adjacentEdges()) {
edgeCost[e] *= 100;
}
}
return goodCuts(mesh, boundaryVertices, edgeCost);
}
std::set<Edge> shortestPath(ManifoldSurfaceMesh& mesh,
const EdgeData<double>& weight, Vertex src,
Vertex dst) {
VertexData<double> dist(mesh, std::numeric_limits<double>::infinity());
VertexData<Halfedge> parent(mesh);
VertexData<char> visited(mesh, false);
dist[src] = 0;
std::priority_queue<std::pair<double, Vertex>> visitQueue;
for (Vertex v : mesh.vertices()) {
visitQueue.push(std::make_pair(-dist[v], v));
}
while (!visitQueue.empty()) {
Vertex curr = visitQueue.top().second;
visitQueue.pop();
if (visited[curr] || std::isinf(dist[curr]) || curr == dst) continue;
for (Halfedge he : curr.incomingHalfedges()) {
Vertex opp = he.vertex();
if (!visited[opp]) {
double cost = dist[curr] + weight[he.edge()];
if (cost < dist[opp]) {
dist[opp] = cost;
parent[opp] = he.twin();
visitQueue.push(std::make_pair(-cost, opp));
}
}
}
visited[curr] = true;
}
std::set<Edge> path;
Vertex curr = dst;
while (curr != src) {
path.insert(parent[curr].edge());
curr = parent[curr].vertex();
}
return path;
}
VertexData<double> distances(ManifoldSurfaceMesh& mesh,
const EdgeData<double>& weight, Vertex src) {
VertexData<double> dist(mesh, std::numeric_limits<double>::infinity());
VertexData<char> visited(mesh, false);
dist[src] = 0;
std::priority_queue<std::pair<double, Vertex>> visitQueue;
for (Vertex v : mesh.vertices()) {
visitQueue.push(std::make_pair(-dist[v], v));
}
while (!visitQueue.empty()) {
Vertex curr = visitQueue.top().second;
visitQueue.pop();
if (visited[curr] || std::isinf(dist[curr])) continue;
for (Halfedge he : curr.incomingHalfedges()) {
Vertex opp = he.vertex();
if (!visited[opp]) {
double cost = dist[curr] + weight[he.edge()];
if (cost < dist[opp]) {
dist[opp] = cost;
visitQueue.push(std::make_pair(-cost, opp));
}
}
}
visited[curr] = true;
}
return dist;
}
// Stolen from BFF
// Bff.cpp > BFFData::index Wedges();
std::tuple<CornerData<size_t>, size_t> indexCorners(ManifoldSurfaceMesh& mesh,
const std::set<Edge>& cut) {
VertexData<bool> interior(mesh, true);
EdgeData<bool> onCut(mesh, false);
for (Edge e : cut) {
interior[src(e)] = false;
interior[dst(e)] = false;
onCut[e] = true;
}
size_t iV = 0;
CornerData<size_t> cornerIndices(mesh);
// Index interior vertices
for (Vertex v : mesh.vertices()) {
if (interior[v]) {
for (Corner c : v.adjacentCorners()) cornerIndices[c] = iV;
iV++;
}
}
// Index boundary (cut) vertices
for (Edge e : cut) {
std::array<Halfedge, 2> hedges{e.halfedge(), e.halfedge().twin()};
for (Halfedge he : hedges) {
Halfedge curr = he;
do {
curr = curr.twin().next();
cornerIndices[curr.corner()] = iV;
} while (!onCut[curr.edge()]);
iV++;
}
}
return std::make_tuple(cornerIndices, iV);
}
// ==========================================================================
// Homology Generators via Tree-Coterie
// ==========================================================================
VertexData<Halfedge> buildPrimalSpanningTree(ManifoldSurfaceMesh& mesh) {
// Initialize each vertex as its own parent
VertexData<Halfedge> primalParent(mesh);
for (Vertex v : mesh.vertices()) primalParent[v] = v.halfedge();
VertexData<bool> inTree(mesh, false);
for (Vertex v : mesh.vertices()) {
if (!inTree[v]) {
inTree[v] = true;
Vertex root = v;
std::queue<Vertex> toVisit;
toVisit.push(root);
while (!toVisit.empty()) {
Vertex v = toVisit.front();
toVisit.pop();
for (Halfedge he : v.outgoingHalfedges()) {
Vertex w = he.twin().vertex();
if (primalParent[w].vertex() == w && w != root) {
primalParent[w] = he;
toVisit.push(w);
inTree[w] = true;
}
}
}
}
}
return primalParent;
}
FaceData<Halfedge>
buildDualSpanningTree(ManifoldSurfaceMesh& mesh,
const VertexData<Halfedge>& primalParent) {
// Initialize each face as its own parent
FaceData<Halfedge> dualParent(mesh);
for (Face f : mesh.faces()) dualParent[f] = f.halfedge();
FaceData<bool> inTree(mesh, false);
for (Face f : mesh.faces()) {
if (!inTree[f]) {
inTree[f] = true;
Face root = f;
std::queue<Face> toVisit;
toVisit.push(root);
while (!toVisit.empty()) {
Face f = toVisit.front();
toVisit.pop();
for (Halfedge he : f.adjacentHalfedges()) {
Face g = he.twin().face();
if (!inPrimalSpanningTree(he.edge(), primalParent) &&
dualParent[g].face() == g && g != root) {
dualParent[g] = he;
toVisit.push(g);
inTree[g] = true;
}
}
}
}
}
return dualParent;
}
VertexData<Halfedge> buildPrimalSpanningTree(ManifoldSurfaceMesh& mesh,
const EdgeData<double>& edgeCost) {
// Initialize each vertex as its own parent
VertexData<Halfedge> primalParent(mesh);
for (Vertex v : mesh.vertices()) primalParent[v] = v.halfedge();
// Distance estimates to build a shortest-paths tree
VertexData<double> dist(mesh, std::numeric_limits<double>::infinity());
VertexData<bool> inTree(mesh, false);
for (Vertex v : mesh.vertices()) {
if (!inTree[v]) {
inTree[v] = true;
Vertex root = v;
std::priority_queue<std::pair<double, Vertex>> toVisit;
toVisit.push({0, root});
dist[root] = 0;
while (!toVisit.empty()) {
Vertex v;
std::tie(std::ignore, v) = toVisit.top();
toVisit.pop();
for (Halfedge he : v.outgoingHalfedges()) {
Vertex w = he.twin().vertex();
dist[w] = fmin(dist[w], dist[v] + edgeCost[he.edge()]);
if (primalParent[w].vertex() == w && w != root) {
primalParent[w] = he;
// std::priority_queue is a max-heap, so we sort by
// negative cost to pop the closest vertex next
toVisit.push({-dist[w], w});
inTree[w] = true;
}
}
}
}
}
return primalParent;
}
FaceData<Halfedge>
buildDualSpanningTree(ManifoldSurfaceMesh& mesh,
const VertexData<Halfedge>& primalParent,
const EdgeData<double>& edgeCost) {
// Initialize each face as its own parent
FaceData<Halfedge> dualParent(mesh);
for (Face f : mesh.faces()) dualParent[f] = f.halfedge();
// The cost of an edge not in the primal spanning tree is the length
// of the shortest loop it makes with the primal spanning
EdgeData<double> edgeLoopCost(mesh, 0);
for (Edge e : mesh.edges()) {
if (!inPrimalSpanningTree(e, primalParent)) {
for (Edge e : primalTreeLoop(e, primalParent)) {
edgeLoopCost[e] += edgeCost[e];
}
}
}
FaceData<bool> inTree(mesh, false);
for (Face f : mesh.faces()) {
if (!inTree[f]) {
inTree[f] = true;
Face root = f;
std::priority_queue<std::pair<double, Face>> toVisit;
toVisit.push({0, root});
while (!toVisit.empty()) {
Face f;
std::tie(std::ignore, f) = toVisit.top();
toVisit.pop();
for (Halfedge he : f.adjacentHalfedges()) {
Face g = he.twin().face();
if (!inPrimalSpanningTree(he.edge(), primalParent) &&
dualParent[g].face() == g && g != root) {
dualParent[g] = he;
toVisit.push({edgeLoopCost[he.edge()], g});
inTree[g] = true;
}
}
}
}
}
return dualParent;
}
std::vector<std::vector<Edge>>
computeHomologyGenerators(ManifoldSurfaceMesh& mesh) {
VertexData<Halfedge> primalParent = buildPrimalSpanningTree(mesh);
FaceData<Halfedge> dualParent = buildDualSpanningTree(mesh, primalParent);
std::vector<std::vector<Edge>> generators;
for (Edge e : mesh.edges()) {
if (!inPrimalSpanningTree(e, primalParent) &&
!inDualSpanningTree(e, dualParent)) {
generators.push_back(primalTreeLoop(e, primalParent));
}
}
return generators;
}
std::vector<std::vector<Edge>>
computeHomologyGenerators(ManifoldSurfaceMesh& mesh,
const EdgeData<double>& edgeCost) {
VertexData<Halfedge> primalParent = buildPrimalSpanningTree(mesh, edgeCost);
FaceData<Halfedge> dualParent =
buildDualSpanningTree(mesh, primalParent, edgeCost);
std::vector<std::vector<Edge>> generators;
for (Edge e : mesh.edges()) {
if (!inPrimalSpanningTree(e, primalParent) &&
!inDualSpanningTree(e, dualParent)) {
generators.push_back(primalTreeLoop(e, primalParent));
}
}
return generators;
}
bool inPrimalSpanningTree(Edge e, const VertexData<Halfedge>& primalParent) {
Vertex v = e.halfedge().vertex();
Vertex w = e.halfedge().twin().vertex();
return primalParent[v].edge() == e || primalParent[w].edge() == e;
}
bool inDualSpanningTree(Edge e, const FaceData<Halfedge>& dualParent) {
Face f = e.halfedge().face();
Face g = e.halfedge().twin().face();
return dualParent[f].edge() == e || dualParent[g].edge() == e;
}
std::vector<Edge> primalTreeLoop(Edge e,
const VertexData<Halfedge>& primalParent) {
Halfedge he = e.halfedge();
// Walk up along primal tree to extract generator
Halfedge curr = primalParent[he.twin().vertex()];
std::vector<Edge> forwardPath{he.edge(), curr.edge()};
do {
curr = primalParent[curr.vertex()];
forwardPath.push_back(curr.edge());
} while (primalParent[curr.vertex()].vertex() != curr.vertex());
curr = primalParent[he.vertex()];
std::vector<Edge> backwardPath{curr.edge()};
do {
curr = primalParent[curr.vertex()];
backwardPath.push_back(curr.edge());
} while (primalParent[curr.vertex()].vertex() != curr.vertex());
// Identify and remove common edges at the ends of both paths
size_t nShared = 0;
size_t nF = forwardPath.size() - 1;
size_t nB = backwardPath.size() - 1;
while (nShared < nF && nShared < nB &&
forwardPath[nF - nShared] == backwardPath[nB - nShared]) {
nShared++;
}
// Remove last nShared elements from paths
// https://stackoverflow.com/questions/34452139/how-to-remove-several-elements-from-the-end-of-stdvector
forwardPath.resize(forwardPath.size() - nShared);
backwardPath.resize(backwardPath.size() - nShared);
std::vector<Edge> loop;
// First go backwards along backwardPath
loop.insert(std::end(loop), std::rbegin(backwardPath),
std::rend(backwardPath));
// Then go forwards along forwardPath
loop.insert(std::end(loop), std::begin(forwardPath), std::end(forwardPath));
return loop;
}
} // namespace ImplementationDetails
} // namespace CEPS