-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgo.cpp
executable file
·303 lines (250 loc) · 9.4 KB
/
algo.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
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
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#include "dcel.h"
ofstream time_file;
ofstream cfile("dcel_cords.txt");
ofstream efile("dcel_edges.txt");
DCEL p, mp;
vector<DCEL> convex_polygons;
map<pair<double, double>, int> vertex_map;
int vertex_index = 0;
/*!
Function will initialize dcell with the given files for vertices and edges.
Here the function assumes that the a given edge is the prev for the next edge (and vice versa)
*/
void initialize_dcel(DCEL &d, string points_file, string lines_file)
{
fstream vertices_file(points_file, ios_base::in);
double x, y;
while (vertices_file >> x)
{
vertices_file >> y;
Vertex v = Vertex(x, y);
d.addVertex(v);
}
fstream edges_file(lines_file, ios_base::in);
int org, dest;
edges_file >> org;
edges_file >> dest;
d.addEdge(d.findVertexByIndex(org - 1), d.findVertexByIndex(dest - 1), NULL, NULL, NULL);
while (edges_file >> org)
{
edges_file >> dest;
d.addEdge(d.findVertexByIndex(org - 1), d.findVertexByIndex(dest - 1), &(*(-- --d.edges.end())), NULL, NULL);
}
(*(-- --d.edges.end())).next = &(*(d.edges.begin()));
(*(d.edges.begin())).prev = &(*(-- --d.edges.end()));
(*(-- --d.edges.end())).twin->prev = (*(d.edges.begin())).twin;
(*(d.edges.begin())).twin->next = (*(-- --d.edges.end())).twin;
}
/*!
THE function that performs the MP1 algorithm for partitioning recursively
Here the function assumes that the given vertices are in anti-clockwise order
*/
void mp1(DCEL &p, Edge *p_start)
{
DCEL l;
// To find the default starting edge when NULL is passed
if (!p_start)
for (auto e : (--p.vertices.end())->inc_edges)
if (e->left_face != &*p.faces.begin())
p_start = e;
Edge *p_cursor = p_start;
Edge *l_first_edge = l.addEdge(p_cursor->org, p_cursor->dest, NULL, NULL, NULL);
Edge *l_last_edge = l_first_edge;
p_cursor = p_cursor->next;
Vertex next_vertex = *p_cursor->dest;
bool flag = true;
while (l.n < p.n && p_cursor != NULL && p_cursor != NULL &&
isReflexAngle(*l.findVertexByIndex(l.n - 2), *l.findVertexByIndex(l.n - 1), next_vertex) &&
isReflexAngle(*l.findVertexByIndex(l.n - 1), next_vertex, *l.findVertexByIndex(0)) &&
isReflexAngle(next_vertex, *l.findVertexByIndex(0), *l.findVertexByIndex(1)))
{
l_last_edge = l.addEdge(p_cursor->org, p_cursor->dest, l_last_edge, NULL, NULL);
if (p_cursor->next == NULL)
{
flag = false;
break;
}
p_cursor = p_cursor->next;
next_vertex = *p_cursor->dest;
}
if (flag)
p_cursor = p_cursor->prev;
l.addEdge(&*l.findVertexByIndex(l.n - 1), &*l.findVertexByIndex(0), l_last_edge, l_first_edge, NULL);
if (l.n < p.n && l.n > 2)
{
vector<Vertex> interiors;
for (Vertex v : p.vertices)
if (l.findVertex(v) == NULL && l.isInteriorPoint(v) == true)
interiors.push_back(v);
while (!interiors.empty())
{
l_last_edge = l_last_edge->prev;
l.removeVertex(*l.findVertexByIndex(l.n - 1));
l_last_edge = l.addEdge(l.findVertexByIndex(l.n - 1), l.findVertexByIndex(0), l_last_edge, l_first_edge, NULL);
for (int i = interiors.size() - 1; i >= 0; i--)
if (!l.isInteriorPoint(interiors[i]))
interiors.erase(interiors.begin() + i);
}
}
if (l.n > 2)
{
Edge *new_start = p.remove(l);
convex_polygons.push_back(l);
for (auto v : l.vertices)
{
if (vertex_map.find(v.pairup()) == vertex_map.end())
{
vertex_map[v.pairup()] = ++vertex_index;
cfile << v.x << " " << v.y << endl;
}
}
for (auto it = l.edges.begin(); it != l.edges.end(); it++)
efile << vertex_map[(*(it->org)).pairup()] << " " << vertex_map[(*(it++->dest)).pairup()] << endl;
if (p.n > 3)
{
if (new_start->left_face != &*p.faces.begin())
mp1(p, new_start);
else
mp1(p, new_start->twin);
}
}
else
{
if (p.n > 3)
mp1(p, p_start->next);
}
}
/*!
THE function that performs the Merging algorithm on the decomposed DCEL p
and renders a new DCEL mp
*/
void merging()
{
initialize_dcel(mp, "dcel_cords.txt", "dcel_edges.txt");
mp.save();
cout << "Number of diagonals = " << p.added_diagonals.size() << endl;
vector<pair<Vertex, Vertex>> inessential_diagonals;
for (auto dig : p.added_diagonals)
{
Vertex org = dig.first;
Vertex *mp_org = mp.findVertex(org);
Vertex dest = dig.second;
Vertex *mp_dest = mp.findVertex(dest);
if(!mp_org || !mp_dest)
continue;
double p_min = DBL_MAX;
Vertex vp_min;
Vertex vn_min;
Vertex default_v;
double n_min = -DBL_MAX;
for (auto e : mp_org->inc_edges)
{
if (*(e->dest) == dest)
continue;
double slope_diff = (dest.y - org.y) / (dest.x - org.x) - (e->dest->y - org.y) / (e->dest->x - org.x);
if (slope_diff > 0)
{
p_min = min(p_min, slope_diff);
vp_min = Vertex(e->dest->x, e->dest->y);
}
else
{
n_min = max(n_min, slope_diff);
vn_min = Vertex(e->dest->x, e->dest->y);
}
}
if (vp_min == default_v || vn_min == default_v || ((getAngleOfFirst(org, dest, vp_min) + getAngleOfFirst(org, dest, vn_min)) > 180))
continue;
p_min = DBL_MAX;
vp_min = default_v;
vn_min = default_v;
default_v = default_v;
n_min = -DBL_MAX;
for (auto e : mp_dest->inc_edges)
{
if (*(e->dest) == org)
continue;
double slope_diff = (org.y - dest.y) / (org.x - dest.x) - (e->dest->y - dest.y) / (e->dest->x - dest.x);
if (slope_diff > 0)
{
p_min = min(p_min, slope_diff);
vp_min = Vertex(e->dest->x, e->dest->y);
}
else
{
n_min = max(n_min, slope_diff);
vn_min = Vertex(e->dest->x, e->dest->y);
}
}
if (vp_min == default_v || vn_min == default_v || ((getAngleOfFirst(dest, org, vp_min) + getAngleOfFirst(dest, org, vn_min)) > 180))
continue;
inessential_diagonals.push_back(dig);
}
ofstream mcfile("merged_dcel_cords.txt");
ofstream mefile("merged_dcel_edges.txt");
vertex_map.clear();
vertex_index = 0;
for (auto v : mp.vertices)
{
if (vertex_map.find(v.pairup()) == vertex_map.end())
{
vertex_map[v.pairup()] = ++vertex_index;
mcfile << v.x << " " << v.y << endl;
}
}
for (auto it = mp.edges.begin(); it != mp.edges.end(); it++)
{
bool flag = true;
for (auto dig : inessential_diagonals)
if ((*it->org == dig.first && *it->dest == dig.second) ||
(*it->org == dig.second && *it->dest == dig.first))
{
flag = false;
break;
}
if (flag)
mefile << vertex_map[(*(it->org)).pairup()] << " " << vertex_map[(*(it++->dest)).pairup()] << endl;
}
cout << "inessential size = " << inessential_diagonals.size() << endl;
mcfile.close();
mefile.close();
}
int main()
{
time_file.open("time_file.txt", ios::app);
initialize_dcel(p, "cords.txt", "edges.txt");
time_file<<p.n<<" ";
auto begin_time = high_resolution_clock::now();
auto initial_time = high_resolution_clock::now();
if (p.n > 3)
mp1(p, NULL);
auto decompose_time = high_resolution_clock::now();
for (auto v : p.vertices)
{
if (vertex_map.find(v.pairup()) == vertex_map.end())
{
vertex_map[v.pairup()] = ++vertex_index;
cfile << v.x << " " << v.y << endl;
}
}
for (auto it = p.edges.begin(); it != p.edges.end(); it++)
efile << vertex_map[(*(it->org)).pairup()] << " " << vertex_map[(*(it++->dest)).pairup()] << endl;
convex_polygons.push_back(p);
merging();
auto merge_time = high_resolution_clock::now();
auto end_time = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end_time - begin_time);
cout << "Total time : " << (double)(duration.count() / 1000.0) << " microseconds\n";
time_file<<(double)(duration.count() / 1000.0)<<" ";
duration = duration_cast<microseconds>(decompose_time - initial_time);
cout << "For decomposition : " << (double)(duration.count() / 1000.0) << " microseconds\n";
time_file<<(double)(duration.count() / 1000.0)<<" ";
duration = duration_cast<microseconds>(merge_time - decompose_time);
cout << "For merging : " << (double)(duration.count() / 1000.0) << " microseconds\n";
time_file<<(double)(duration.count() / 1000.0)<<"\n";
cfile.close();
efile.close();
}