-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
228 lines (188 loc) · 6.84 KB
/
Copy pathgraph.cpp
File metadata and controls
228 lines (188 loc) · 6.84 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
#include <cmath>
#include <array>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include "graph.hpp"
#include "util.hpp"
namespace graph
{
struct {
bool operator()(const Vec2f* v1, const Vec2f* v2)
{
if (v1->x < v2->x) {
return true;
} else if (v1->x > v2->x) {
return false;
} else if (v1->y < v2->y) {
return true;
} else {
return false;
}
}
} vecLess;
std::ostream& operator << (std::ostream& os, const Edge& e)
{
return os << "Edge [(" << e.p1.x << ", " << e.p1.y << "), ("
<< e.p2.x << ", " << e.p2.y << ")]";
}
struct EdgeHash
{
std::size_t operator()(const Edge &e) const
{
std::size_t seed = 0;
// Sort the points so that if there are two edges with the same
// points but different order, then hash to the same value.
std::array<const Vec2f*, 2> pts = { &e.p1, &e.p2 };
std::sort(pts.begin(), pts.end(), vecLess);
util::hash_combine(seed, pts[0]->x);
util::hash_combine(seed, pts[0]->y);
util::hash_combine(seed, pts[1]->x);
util::hash_combine(seed, pts[1]->y);
return seed;
}
};
std::ostream& operator << (std::ostream& os, const Triangle& t)
{
return os << "Triangle ["
<< "(" << t.p1.x << ", " << t.p1.y << "), "
<< "(" << t.p2.x << ", " << t.p2.y << "), "
<< "(" << t.p3.x << ", " << t.p3.y << ")] ";
}
struct TriangleHash
{
std::size_t operator()(const Triangle &t) const
{
std::size_t seed = 0;
// Sort the points so that if there are two tris with the same
// points but different order, then hash to the same value.
std::array<const Vec2f*, 3> pts = { &t.p1, &t.p2, &t.p3 };
std::sort(pts.begin(), pts.end(), vecLess);
util::hash_combine(seed, pts[0]->x);
util::hash_combine(seed, pts[0]->y);
util::hash_combine(seed, pts[1]->x);
util::hash_combine(seed, pts[1]->y);
util::hash_combine(seed, pts[2]->x);
util::hash_combine(seed, pts[2]->y);
return seed;
}
};
/*
* findSuperTriangle
*
* Finds the super-triangle which encloses a given set of points in the 2D
* plane. The points of the triangle will be in clockwise order.
*/
static Triangle findSuperTriangle(std::vector<Vec2f> &vertices)
{
float minX = vertices[0].x;
float minY = vertices[0].y;
float maxX = minX;
float maxY = minY;
for (auto&& vert : vertices) {
float x = vert.x;
float y = vert.y;
if (x < minX) { minX = x; }
if (y < minY) { minY = y; }
if (x > maxX) { maxX = x; }
if (y > minY) { maxY = y; }
}
float dx = maxX - minX;
float dy = maxY - minY;
float maxD = std::max(dx, dy);
float midX = (minX + maxX) / 2.0f;
float midY = (minY + maxY) / 2.0f;
return Triangle(Vec2f(midX - 20 * maxD, midY - maxD),
Vec2f(midX, midY + 20 * maxD),
Vec2f(midX + 20 * maxD, midY - maxD));
}
/*
* generateDelaunay
*
* See function declaration for more detail.
*/
std::vector<Triangle>
generateDelaunay(std::vector<Vec2f> &vertices)
{
std::vector<Triangle> tris;
// Find the super-triangle and add it to the list of tris.
Triangle superTri = findSuperTriangle(vertices);
tris.push_back(superTri);
// Insert vertices into the mesh one at a time.
for (auto&& v : vertices) {
std::unordered_set<Triangle, TriangleHash> badTris;
// Find all the tris that are no longer valid once the current
// vertex is inserted.
for (auto &&tri : tris) {
if (tri.circumcircleContains(v)) {
badTris.insert(tri);
}
}
std::vector<Edge> poly;
std::unordered_set<Edge, EdgeHash> edges;
std::unordered_set<Edge, EdgeHash> duplicateEdges;
// Find all unique edges in the bad tris and add them to the poly.
for (auto &&tri : badTris) {
for (auto &&edge : tri.GetEdges()) {
if (edges.find(edge) != edges.end()) {
duplicateEdges.insert(edge);
} else {
edges.insert(edge);
}
}
}
for (auto &&edge : edges) {
if (duplicateEdges.find(edge) == duplicateEdges.end()) {
poly.push_back(edge);
}
}
// Remove the bad triangles from the list of tris.
tris.erase(std::remove_if(
tris.begin(), tris.end(), [badTris](Triangle t) {
return badTris.find(t) != badTris.end();
}), tris.end());
// Create new triangles from the current vertex and the edges of
// the polygon, and add them to the list of tris.
for (auto &&edge : poly) {
tris.push_back(Triangle(v, edge.p1, edge.p2));
}
}
// Remove any triangles that contain a vertex from the super-triangle.
tris.erase(std::remove_if(tris.begin(), tris.end(),
[superTri](const Triangle &tri) {
return superTri.hasCommonPoints(tri);
}), tris.end());
return tris;
}
/*
* generateUrquhart
*
* See function declaration for more detail.
*/
std::vector<Edge>
generateUrquhart(std::vector<Vec2f> &vertices)
{
std::vector<Triangle> delaunayTris = generateDelaunay(vertices);
// Generate the set of edges of the delaunay triangles.
std::unordered_set<Edge, EdgeHash> edges;
for (auto &&tri : delaunayTris) {
auto triEdges = tri.GetEdges();
edges.insert(triEdges.begin(), triEdges.end());
}
// Remove the longest edge from each triangle from the set to give the
// Urquhart graph.
for (auto &&tri : delaunayTris) {
auto triEdges = tri.GetEdges();
auto &longestEdge =
*std::max_element(triEdges.begin(), triEdges.end(),
[](const Edge &e1, const Edge &e2) {
return e1.Length() < e2.Length();
});
edges.erase(longestEdge);
}
std::vector<Edge> result;
result.assign(edges.begin(), edges.end());
return result;
}
}