-
Notifications
You must be signed in to change notification settings - Fork 5
/
graph.h
357 lines (310 loc) · 9.95 KB
/
graph.h
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
/*******************************************************
** Generalized Voronoi Diagram Project **
** Copyright (c) 2015 John Martin Edwards **
** Scientific Computing and Imaging Institute **
** 72 S Central Campus Drive, Room 3750 **
** Salt Lake City, UT 84112 **
** **
** For information about this project contact **
** John Edwards at **
** edwardsjohnmartin@gmail.com **
** or visit **
** sci.utah.edu/~jedwards/research/gvd/index.html **
*******************************************************/
#ifndef __JME_GRAPH_H__
#define __JME_GRAPH_H__
#include <vector>
#include <limits>
#include <iterator>
#include "./opencl/vec.h"
#include "./opencl/edge.h"
#include "./edge_cpp.h"
// Only topological information. No spatial information.
class TopoGraph {
public:
typedef std::pair<int, int> Pair;
typedef std::set<int> Set;
public:
TopoGraph() {}
void AddEdge(int ai, int bi) {
Ensure(ai);
Ensure(bi);
_links[ai].insert(bi);
_links[bi].insert(ai);
}
// a is pushing b
double3 Dir(int ai, int bi) const {
if (ai == bi) return double3();
std::pair<int, int> ab(ai, bi);
return _link2dir.find(ab)->second;
// const int ri = WhichRing(ai, bi);
// Set aring = Ring(ai, ri);
// aring.insert(Ring(ai, ri-1).begin(), Ring(ai, ri-1).end());
// const Set& bring = Ring(bi, 1);
// // // Find the direction between ai and bi and also between every
// // // ni where ni is a neighbor of ai.
// // std::pair<int, int> ab(ai, bi);
// // double3 dir = _link2dir.find(ab)->second;
// double3 dir;
// // const std::set<int>& a_nbrs = _links[ai];
// // const std::set<int>& b_nbrs = _links[bi];
// typedef std::list<int> List;
// typedef List::const_iterator iter;
// // Neighbors of both a and b
// List ab_nbrs;
// // std::set_intersection(a_nbrs.begin(), a_nbrs.end(),
// // b_nbrs.begin(), b_nbrs.end(),
// // std::inserter(ab_nbrs, ab_nbrs.end()));
// std::set_intersection(aring.begin(), aring.end(),
// bring.begin(), bring.end(),
// std::inserter(ab_nbrs, ab_nbrs.end()));
// for (iter it = ab_nbrs.begin(); it != ab_nbrs.end(); ++it) {
// const int ni = *it;
// std::pair<int, int> nb(ni, bi);
// dir += _link2dir.find(nb)->second;
// }
// return dir.unit();
}
void SetDir(int ai, int bi, const double3& dir) {
Pair ab(ai, bi);
_link2dir[ab] = dir;
}
void SetArea(int ai, int bi, double area) {
Pair ab(ai, bi);
_link2area[ab] = area;
}
double GetArea(int ai, int bi) const {
Pair ab(ai, bi);
return _link2area.find(ab)->second;
}
const std::set<int>& operator[](int i) const { return _links[i]; }
const std::set<int>& at(int i) const { return _links[i]; }
const std::set<int>& Ring(int i, int ring) const {
if (_rings.empty())
ComputeRings();
while (_rings[i].size() <= ring) {
_rings[i].push_back(Set());
}
return _rings[i][ring];
}
int WhichRing(int center, int i) const {
for (int ri = 0; ri < 10000; ++ri) {
const Set& ring = Ring(center, ri);
if (ring.find(i) != ring.end())
return ri;
}
throw logic_error("Not found in ring");
}
private:
void Ensure(int i) {
if (_links.size() <= i) {
_links.resize(i+1);
}
}
void ComputeRings() const {
_rings.resize(_links.size());
for (int i = 0; i < _links.size(); ++i) {
_rings[i].push_back(Set());
_rings[i].push_back(Set());
_rings[i][0].insert(i);
const std::set<int>& nbrs = _links[i];
_rings[i][1] = nbrs;
}
// ring index
int ri = 2;
bool stop = false;
while (!stop) {
stop = true;
for (int i = 0; i < _links.size(); ++i) {
const Set& prev_ring = _rings[i][ri-1];
Set used = prev_ring;
used.insert(_rings[i][ri-2].begin(), _rings[i][ri-2].end());
Set ring;
for (Set::const_iterator it = prev_ring.begin();
it != prev_ring.end(); ++it) {
const int j = *it;
const Set& j_ring = _rings[j][1];
std::set_difference(j_ring.begin(), j_ring.end(),
used.begin(), used.end(),
std::inserter(ring, ring.end()));
}
_rings[i].push_back(Set());
_rings[i][ri] = ring;
if (!ring.empty())
stop = false;
}
++ri;
}
}
private:
std::vector<std::set<int> > _links;
// Maps edges of the graph to directions
std::map<std::pair<int, int>, double3> _link2dir;
std::map<std::pair<int, int>, double> _link2area;
// _rings[0][1] refers to the zeroth object's one-ring neighbors
// _rings[3][2] refers to the third object's two-ring neighbors
mutable std::vector<std::vector<std::set<int> > > _rings;
};
struct CostStruct {
CostStruct(int vi_, double cost_)
: vi(vi_), cost(cost_) {}
bool operator<(const CostStruct& rhs) const {
if (cost == rhs.cost)
return vi < rhs.vi;
return cost < rhs.cost;
}
int vi;
double cost;
};
template <int D>
class Graph {
public:
Graph() {}
int Add(const doublen& p) {
_vertices.push_back(p);
_links.push_back(std::vector<int>());
return _vertices.size()-1;
}
void AddEdge(int ai, int bi) {
_links[ai].push_back(bi);
_links[bi].push_back(ai);
}
template <typename Visitor>
void VisitEdges(Visitor v) const {
for (int vi = 0; vi < _vertices.size(); ++vi) {
for (int i = 0; i < _links[vi].size(); ++i) {
const int n_vi = _links[vi][i];
v(vi, _vertices[vi], n_vi, _vertices[n_vi]);
}
}
}
// Gives the path from the end to the front
template <typename Out_iter>
void Dijkstra(const int start, const int end, Out_iter path) {
std::vector<int> parents(_vertices.size(), -1);
std::set<CostStruct> heap;
std::vector<double> cost(_vertices.size(),
std::numeric_limits<double>::max());
std::vector<bool> visited(_vertices.size(), false);
heap.insert(CostStruct(start, 0));
cost[start] = 0;
bool done = false;
while (!heap.empty() && !done) {
const int vi = heap.begin()->vi;
heap.erase(heap.begin());
if (vi == end) {
done = true;
} else if (!visited[vi]) {
visited[vi] = true;
for (int i = 0; i < _links[vi].size(); ++i) {
const int n_vi = _links[vi][i];
if (!visited[n_vi]) {
const double d = cost[vi] + length(_vertices[vi]-_vertices[n_vi]);
if (d < cost[n_vi]) {
cost[n_vi] = d;
heap.insert(CostStruct(n_vi, d));
parents[n_vi] = vi;
}
}
}
}
}
if (!done) {
// throw std::logic_error("Shortest cost path failed");
std::cerr << "Shortest cost path failed" << std::endl;
return;
}
int vi = end;
while (vi != start) {
*path++ = vi;
vi = parents[vi];
}
*path++ = start;
}
const std::vector<doublen>& GetVertices() const { return _vertices; }
std::vector<doublen>& GetVertices() { return _vertices; }
const doublen& operator[](int i) const { return _vertices[i]; }
doublen& operator[](int i) { return _vertices[i]; }
void Clear() { _vertices.clear(); _links.clear(); }
private:
std::vector<doublen> _vertices;
std::vector<std::vector<int> > _links;
};
template <int D>
class GraphConstructor {
public:
typedef typename std::map<Edge, int>::const_iterator e2i_iter;
typedef typename std::map<doublen, int>::const_iterator c2i_iter;
typedef typename std::map<doublen, int>::const_iterator p2i_iter;
public:
GraphConstructor() {}
//------------------------------------------------------------
// For use with 2D and 3D bisectors
//------------------------------------------------------------
void AddEdge(const doublen& a, const doublen& b, const Edge& a_seg) {
int a_idx, b_idx;
e2i_iter e2i = _edge2idx.find(a_seg);
if (e2i != _edge2idx.end()) {
a_idx = e2i->second;
} else {
a_idx = _graph.Add(a);
_edge2idx[a_seg] = a_idx;
}
c2i_iter c2i = _centroid2idx.find(b);
if (c2i != _centroid2idx.end()) {
b_idx = c2i->second;
} else {
b_idx = _graph.Add(b);
_centroid2idx[b] = b_idx;
}
_graph.AddEdge(a_idx, b_idx);
}
//------------------------------------------------------------
// For use with 3D bisectors
//------------------------------------------------------------
void AddEdge(const doublen& a, const doublen& b) {
int a_idx, b_idx;
{
p2i_iter p2i = _point2idx.find(a);
if (p2i != _point2idx.end()) {
a_idx = p2i->second;
} else {
a_idx = _graph.Add(a);
_point2idx[a] = a_idx;
}
} {
p2i_iter p2i = _point2idx.find(b);
if (p2i != _point2idx.end()) {
b_idx = p2i->second;
} else {
b_idx = _graph.Add(b);
_point2idx[b] = b_idx;
}
}
_graph.AddEdge(a_idx, b_idx);
}
//------------------------------------------------------------
// For use with 3D bisectors
//------------------------------------------------------------
void AddTriangle(const doublen& a, const doublen& b, const doublen& c) {
AddEdge(a, b);
AddEdge(b, c);
AddEdge(a, c);
}
void ReplacePoint(const doublen& oldp, const doublen& newp) {
const int i = _point2idx[oldp];
_graph[i] = newp;
_point2idx[newp] = i;
}
const Graph<D>& GetGraph() const { return _graph; }
private:
Graph<D> _graph;
std::map<Edge, int> _edge2idx;
std::map<doublen, int> _centroid2idx;
std::map<doublen, int> _point2idx;
};
typedef Graph<2> Graph2;
typedef Graph<3> Graph3;
typedef GraphConstructor<2> GraphConstructor2;
typedef GraphConstructor<3> GraphConstructor3;
#endif