-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bai1.cpp
426 lines (365 loc) · 10.4 KB
/
Bai1.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
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
// chinh do chinh xac cua double o day la 1e-2
#define eps 1e-2
unordered_map<string, Edge> edges;
unordered_map<string, Junction> junctions;
unordered_map<string, Connection> connections;
void input1(string file_xml)
{
// load the XML file
ifstream file(file_xml);
// variable to parse the XML file
string line;
Xml_line xml_line;
while (getline(file, line))
{
if (line.empty())
continue;
xml_line.setLine(line);
if (xml_line.tag == "/net")
break;
if (xml_line.tag == "edge")
{
Edge edge;
for (auto p : xml_line.attbs)
{
edge.attbs[p.first] = p.second;
}
while (getline(file, line))
{
if (line.empty())
continue;
xml_line.setLine(line);
if (xml_line.tag == "/edge")
break;
Lane l;
for (auto p : xml_line.attbs)
{
l.attbs[p.first] = p.second;
}
edge.lanes.push_back(l);
}
edges[edge.attbs["id"]] = edge;
}
else if (xml_line.tag == "junction")
{
Junction junction;
for (auto p : xml_line.attbs)
{
junction.attbs[p.first] = p.second;
}
// while (getline(file, line) && line != "</junction>") {
// xml_line.setLine(line);
// if (xml_line.tag == "incEdge") {
// junction.inc_edges.push_back(xml_line.attbs["id"]);
// }
// else if (xml_line.tag == "outEdge") {
// junction.out_edges.push_back(xml_line.attbs["id"]);
// }
// }
junctions[junction.attbs["id"]] = junction;
}
else if (xml_line.tag == "connection")
{
Connection connection;
for (auto p : xml_line.attbs)
{
connection.attbs[p.first] = p.second;
}
connections[connection.attbs["from"] + " " + connection.attbs["to"]] = connection;
}
}
}
void printEdge(string nameOfEdge)
{
auto it = edges.find(nameOfEdge);
if (it == edges.end())
{
cout << "Khong tim thay canh co id: " << nameOfEdge;
return;
}
Edge edge = it->second;
Lane laned;
int counts = 0;
for (Lane l : edge.lanes)
{
if (l.attbs.find("disallow") != l.attbs.end() && l.attbs["disallow"] == "pedestrian")
{
laned = l;
counts++;
}
}
if (counts == 1)
{
cout << nameOfEdge << " " << edge.attbs["from"] << " " << edge.attbs["to"] << " " << laned.attbs["shape"] << endl;
}
else if (counts == 0)
{
cout << "Khong tim thay disallow trong canh co id: " << nameOfEdge;
}
else
{
cout << "Co nhieu hon 1 disallow trong canh co id: " << nameOfEdge;
}
}
void printEdge2Juncs(string edge_id1, string edge_id2)
{
auto it = connections.find(edge_id1 + " " + edge_id2);
if (it == connections.end())
{
cout << "Khong tim thay connection giua 2 canh co id: " << edge_id1 << " " << edge_id2;
return;
}
Connection connection = it->second;
string via = connection.attbs["via"];
int pos = via.find('_', via.find('_') + 1); // tim vi tri cua dau '_' thu 2 trong via
via = via.substr(0, pos); // cat via tu vi tri 0 den truoc dau '_' thu 2
Edge edge = edges[via];
// kiem tra xem edge co disallow pedestrian khong
Lane laned;
int counts = 0;
for (Lane l : edge.lanes)
{
if (l.attbs.find("disallow") != l.attbs.end() && l.attbs["disallow"] == "pedestrian")
{
laned = l;
counts++;
}
}
if (counts == 0)
{
cout << "Khong phai junction crossing" << endl;
return;
}
cout << edge.attbs["id"] << " " << laned.attbs["shape"] << endl;
}
set<string> startEdges()
{
set<string> res;
for (auto junc : junctions)
{
Junction junction = junc.second;
if (junction.attbs["type"] == "dead_end")
{
for (auto e : edges)
{
Edge edge = e.second;
if (edge.attbs["from"] == junction.attbs["id"])
{
res.insert(edge.attbs["id"]);
}
}
}
}
return res;
}
void printStart()
{
set<string> res = startEdges();
for (string edge_id : res)
{
printEdge(edge_id);
}
}
set<string> endEdges()
{
set<string> res;
for (auto junc : junctions)
{
Junction junction = junc.second;
if (junction.attbs["type"] == "dead_end")
{
for (auto e : edges)
{
Edge edge = e.second;
if (edge.attbs["to"] == junction.attbs["id"])
{
res.insert(edge.attbs["id"]);
}
}
}
}
return res;
}
void printEnd()
{
set<string> res = endEdges();
for (string edge_id : res)
{
printEdge(edge_id);
}
}
void append(string &s, double x, double y)
{
string s1 = to_string(x);
string s2 = to_string(y);
s1 = s1.substr(0, s1.find('.') + 3); // lay 2 chu so thap phan
s2 = s2.substr(0, s2.find('.') + 3); // lay 2 chu so thap phan
s += s1 + "," + s2 + "_";
}
vector<string> decompose(double len, string path)
{
vector<string> res;
vector<pair<double, double>> points;
istringstream iss(path);
string token1, token2;
while (getline(iss, token1, ',') && getline(iss, token2, ' '))
{
points.push_back(make_pair(stod(token1), stod(token2)));
}
// khoi tao chuoi dau tien
int id = 0;
string s = to_string(id++) + "_";
append(s, points[0].first, points[0].second);
double preDis = 0;
for (int i = 0; i < points.size() - 1; i++)
{
// lay toa do 2 diem lien tiep
double x1 = points[i].first;
double y1 = points[i].second;
double x2 = points[i + 1].first;
double y2 = points[i + 1].second;
if (preDis < -eps || preDis > eps)
append(s, x1, y1); // them diem dau tien vao chuoi, tranh duplicate
double dis = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); // tinh khoang cach giua 2 diem
double x = (x2 - x1) * len / dis; // tinh x cua vector chi phuong
double y = (y2 - y1) * len / dis; // tinh y cua vector chi phuong
// dis + preDis < len thi tiep tuc
if (preDis + dis < len)
{
preDis += dis;
continue;
}
// dis + preDis >= len thi tinh toa do diem tiep theo
x1 = x1 + x * (len - preDis) / len;
y1 = y1 + y * (len - preDis) / len;
// them diem tiep theo vao chuoi
append(s, x1, y1);
s.pop_back();
res.push_back(s); // them chuoi vao vector
// tao chuoi moi
s = to_string(id++) + "_";
append(s, x1, y1);
dis -= len - preDis; // tinh khoang cach con lai
// tinh toa do cac diem tiep theo
while (dis > len)
{
x1 = x1 + x;
y1 = y1 + y;
// them diem tiep theo vao chuoi
append(s, x1, y1);
s.pop_back();
res.push_back(s); // them chuoi vao vector
// tao chuoi moi
s = to_string(id++) + "_";
append(s, x1, y1);
dis -= len; // tinh khoang cach con lai
}
preDis = dis;
}
// them diem cuoi cung vao chuoi
if (preDis < -eps || preDis > eps)
{
append(s, points[points.size() - 1].first, points[points.size() - 1].second);
s.pop_back();
res.push_back(s);
}
return res;
}
string splits(double len, string id)
{
string res;
auto it1 = edges.find(id);
if (it1 != edges.end())
{
Edge edge = it1->second;
Lane laned;
int counts = 0;
for (Lane l : edge.lanes)
{
if (l.attbs.find("disallow") != l.attbs.end() && l.attbs["disallow"] == "pedestrian")
{
laned = l;
counts++;
break;
}
}
if (counts == 0)
{
cout << "Khong tim thay lane co disallow = pedestrian" << endl;
return "";
}
string path = laned.attbs["shape"];
vector<string> dpath = decompose(len, path);
for (string s : dpath)
{
res += edge.attbs["id"] + ' ' + s + '\n';
}
return res;
}
// auto it2 = junctions.find(id);
// if (it2 != junctions.end()) {
// Junction junc = it2->second;
// string path = junc.attbs["shape"];
// vector<string> dpath = decompose(len, path);
// for (string s : dpath) {
// res += junc.attbs["id"] + ' ' + s + '\n';
// }
// return res;
// }
return "Khong tim thay Edge co id: " + id;
}
void allParts(double len, string file_name)
{
set<string> t = startEdges();
set<string> e = endEdges();
set<string> s;
ofstream file(file_name, ios::out);
while (!t.empty())
{
string id = *t.begin();
t.erase(t.begin());
if (e.find(id) == e.end())
{
string u = splits(len, id);
// ghi ra file AllParts.txt
file << u;
s.insert(id);
// tim nhung canh noi voi canh vua duyet
set<string> n;
for (auto e : edges)
{
Edge edge = e.second;
if (edge.attbs["from"] == id)
{
n.insert(edge.attbs["id"]);
}
}
// loai bo nhung canh da duyet
for (string id : s)
{
n.erase(id);
}
// them nhung canh con lai vao tap t
for (string id : n)
{
t.insert(id);
}
}
}
file.close();
}
void Bai_1() {
input1(file_xml);
//# a
// printEdge("E1");
//# b
// printEdge2Juncs("E1", "E2");
//# c
// printStart();
//# e
// cout << splits(20, "MyEdge_for_testing");
//# f
// printEnd();
//# g
allParts(1, file_parts);
}