-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibpoint.cpp
570 lines (509 loc) · 13.4 KB
/
libpoint.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
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
#include "mysql.h"
#include "point.h"
#include "base.h"
#include "libpoint.h"
#include <QFileInfo>
#include <QDir>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QMessageBox>
#include <QFile>
#ifndef INFI
//#define INFI 6555
#define MATSIZE 10000
#endif
/**************libpoint 相关操作方法*************/
/*****************************************/
void LibPoint::Test(){
qDebug() << "test finished!";
}
LibPoint::LibPoint()
{
Head = _POS();
End = Head;
}
int LibPoint::get_position(QVector<QPoint> V,QPoint val){
for (int i = 0;i < V.size();i++){
if (V.at(i) == val){
return i+1;
}
}
return 0;
}
QVector<QPoint> LibPoint::get_min_path(QPoint A,QPoint B){
QVector<QPoint> AP = getALLQPoints();
GSize = AP.size() + 1;
for(int i = 0;i < MATSIZE;i++){
Length[i] = INFI;
FLAG[i] = 1;
for (int j = 0;j < MATSIZE;j++){
GMat[i][j] = INFI;
}
}
qDebug() << AP.size();
init_GMat(AP);
int index1 = get_position(AP,A);
qDebug() << "A:position" << index1;
int index2 = get_position(AP,B);
qDebug() << "B:position" << index2;
Length[index2] = 0;
Path[index2] = index2;
FLAG[index2] = 0;
Dijsk(index2);
QVector<QPoint> retval = QVector<QPoint>();
if (Path[index1] == INFI){
qDebug() << "不可达";
return retval;
}
int index = Path[index1];
retval.append(AP.at(index1 - 1));
qDebug() << "路径:" << index;
int i = 0;
while (index != index2) {
index = Path[index];
qDebug() << "路径:" << index;
if (index == INFI){
qDebug() << "此路不通";
return retval;
}
retval.append(AP.at(index - 1));
i++;
if (i > 10000){
qDebug() << "Error:too deep!";
break;
}
}
return retval;
}
void LibPoint::Dijsk(int p){
FLAG[p] = 0;
qDebug() << "以" << p << "为基点";
QVector<int> val = QVector<int> ();
for (int i = 0 ; i < MATSIZE;i++){
if (GMat[p][i] < INFI ){ //联通
if (Length[p] + GMat[p][i] < Length[i]){ //新路劲更短
Length[i] = Length[p] + GMat[p][i];
Path[i] = p;
Dijsk(i);
}
val.append(i);
}
}
for (auto i :val){
if (FLAG[i]){ //遍历该点为基准点
Dijsk(i);
}
}
}
void LibPoint:: init_GMat(QVector<QPoint> P){
QVector<Point> V = getAllLines();
for (auto val : V){
int index1 = get_position(P,val.toQPoint());
int index2 = get_position(P,val.next_to_point());
GMat[index1][index2] = get_dis_int( val.x.toInt(),val.y.toInt(),val.next_x.toInt(),val.next_y.toInt());
}
}
void LibPoint::init_from_sql(){
MySQL sql;
QVector<Point> val = sql.getAllPoints();
for(auto tmp : val){
qDebug() << "info:append point:" << tmp.to_string();
SPos spos = _SPos();
spos->x = tmp.x.toInt();
spos->y = tmp.y.toInt();
append(tmp.x.toInt(),tmp.y.toInt());
}
qDebug() << "info:append point finished!";
QVector<Point> val2 = sql.getAllLines();
qDebug() << "info:clear!";
for(auto tmp : val2){
qDebug() << "info:append edge:" << tmp.to_string();
append_edge(tmp.x.toInt(),tmp.y.toInt(),tmp.next_x.toInt(),tmp.next_y.toInt());
}
}
POS LibPoint::append(SPos P){
POS res = find(P->x,P->y);
if (res != NULL){
return res;
}
End->next = _POS(P);
End = End->next;
End->point = P;
return End;
}
POS LibPoint::append(int x,int y){
POS res = find(x,y);
if (res != NULL){
return res;
}
End->next = _POS(x,y);
End = End->next;
return End;
}
POS LibPoint::find(int x,int y){
POS val = Head->next;
while (val) {
if (val->point->x >= x - RANGE && val->point->x <= x + RANGE &&
val->point->y >= y - RANGE && val->point->y <= y + RANGE){
return val;
}
val = val->next;
}
return NULL;
}
POS LibPoint::valueAt(int x,int y){
POS val = Head->next;
while (val) {
if (val->point->x == x && val->point->y == y){
return val;
}
val = val->next;
}
return NULL;
}// 有value表明是精准匹配 xy,否则是模糊查找点
/**
* @brief LibPoint::append_edge
* 根据四个坐标插入相邻的点,如果第一个点不存在,则创建第一个点
* 查找是否已经存在,存在结束,否则插入
* @param x 点 x
* @param y 点 y
* @param nx 相邻点 x
* @param ny 相邻点 y
*/
void LibPoint::append_edge(int x,int y,int nx,int ny){
POS res = find(x,y);
if (res == NULL){
qDebug() << "info:hava not found x:" << x <<",y:"<<y<<".apend it";
res = append(x,y);
}
POS_appendEdge(res,nx,ny);
res = find(nx,ny);
if (res == NULL){
qDebug() << "info:hava not found x:" << x <<",y:"<<y<<".apend it";
res = append(x,y);
}
POS_appendEdge(res,x,y);
}
void LibPoint::remove(int x,int y){
POS pos = find(x,y);
if (pos == NULL){return;}
removeValue(pos);
pos = Head->next;
while (pos) {
POS_removeEdge(pos,pos->point->x,pos->point->y);
pos = pos->next;
}
}
void LibPoint::removeValue(int x,int y){
POS pos = Head;
if (pos == NULL){qDebug() << "Error:head null";}
while (pos->next) {
if (pos->next == NULL){
qDebug() << "Error:next null;";
}
if (pos->next->point->x == x && pos->next->point->y == y){
POS tmp = pos->next;
pos->next = pos->next->next;
free_pos(tmp);
return;
}
pos = pos->next;
}
}
void LibPoint::removeValue(POS pos){
POS pos_ = Head;
while (pos_->next) {
if (pos_->next == pos){
pos_->next = pos->next;
free(pos);
}
pos_ = pos_->next;
}
}
/**
* @brief nearDisFind
* 用坐标模糊查询 dis
* @param x
* @param y
* @param nx
* @param ny
* @return
*/
int LibPoint::nearDisFind(int x,int y,int nx,int ny){
POS pos = find(x,y);
return POS_nearDisFind(pos,nx,ny);
}
void LibPoint::showAll(){
qDebug() << "info:showAll!";
POS pos = Head->next;
while (pos) {
qDebug() << POS_toString(pos);
pos = pos->next;
}
}
QVector<QPoint> LibPoint::getALLQPoints(){
QVector<QPoint> retval = QVector<QPoint>();
POS val = Head->next;
while (val) {
retval.append(QPoint(val->point->x,val->point->y));
val = val->next;
}
return retval;
}
QVector<Point> LibPoint::getAllLines(){
QVector<Point> retval = QVector<Point>();
POS val = Head->next;
while (val) {
Edge edge = val->edge;
while (edge) {
retval.append(Point(val->point->x,val->point->y,edge->end->x,edge->end->y));
edge = edge->next;
}
val = val->next;
}
return retval;
}
/**************POS 相关操作方法*************/
/*****************************************/
/** 构造方法
* @brief LibPoint::_POS
* @return
*/
POS LibPoint::_POS(){
POS retval = (POS)malloc(sizeof (SPOS));
retval->edge = NULL;
retval->next = NULL;
retval->point = _SPos();
return retval;
}
POS LibPoint::_POS(int x,int y){
POS retval = (POS)malloc(sizeof (SPOS));
retval->edge = NULL;
retval->next = NULL;
retval->point = _SPos(x,y);
return retval;
}
POS LibPoint::_POS(SPos P){
POS retval = (POS)malloc(sizeof (SPOS));
retval->edge = NULL;
retval->next = NULL;
retval->point = P;
return retval;
}
void LibPoint::free_pos(POS p){
free_spos(p->point);
free_edge(p->edge);
}
/**
* @brief LibPoint::inser_near
* 在已知pos 处插入邻接点 x,y
* @param pos
* @param next_x
* @param next_y
*/
void LibPoint::POS_appendEdge(POS pos,int next_x, int next_y){
qDebug() << "func:POS_appendEdge!";
if (pos == NULL){ //第一个点不存在,省去查询dis
qDebug() << "Error: nullptr:inser to POS but pos is NULL";
return;
}else if (POS_nearDisFind(pos,next_x,next_y) > 0){ //dis不为0,则已存在,结束;
qDebug() << "info:dis exist!";
return;
}
qDebug() << "new Edge!";
Edge edge = _Edge();
edge->end = _SPos(next_x,next_y);
edge->dis = get_dis_int(pos->point->x,pos->point->y,next_x,next_y);
edge->next = pos->edge;
pos->edge = edge;
}
void LibPoint::POS_removeEdge(POS pos,int nx,int ny){
if (pos == NULL){ //第一个点不存在,省去查询dis
qDebug() << "Error: nullptr:inser to POS but pos is NULL";
return;
}else if (POS_nearDisFind(pos,nx,ny) == 0){ //dis为0,则不存在,结束;
return;
}
Edge edge = pos->edge;
Edge tmp;
if (edge->end->x <= nx + RANGE && edge->end->x >= nx - RANGE &&
edge->end->y <= ny + RANGE && edge->end->y >= ny - RANGE){
tmp = edge;
pos->edge = pos->edge->next;
free(tmp);
return;
}
while (edge->next) {
if (edge->end->x <= nx + RANGE && edge->end->x >= nx - RANGE &&
edge->end->y <= ny + RANGE && edge->end->y >= ny - RANGE){
tmp = edge->next;
edge->next = edge->next->next;
free(tmp);
return;
}
edge = edge->next;
}
}
void LibPoint::POS_removeEdgeValueAt(POS pos,int nx,int ny){
if (pos == NULL){ //第一个点不存在,省去查询dis
qDebug() << "Error: nullptr:inser to POS but pos is NULL";
return;
}else if (POS_nearDisValueAt(pos,nx,ny) == 0){ //dis为0,则不存在,结束;
return;
}
Edge edge = pos->edge;
Edge tmp;
if (edge->end->x == nx && edge->end->y == ny){
tmp = edge;
pos->edge = pos->edge->next;
free(tmp);
}
while (edge->next) {
if (edge->end->x == nx && edge->end->y == ny){
tmp = edge->next;
edge->next = edge->next->next;
free(tmp);
}
edge = edge->next;
}
}
QVector<Edge> LibPoint::POS_getEdges(POS pos){
QVector<Edge> retval = QVector<Edge>();
if (pos == NULL){
qDebug() << "Error: nullptr:option to POS but pos is NULL";
return retval;
}
Edge edge = pos->edge;
while (edge) {
retval.append(edge);
edge = edge->next;
}
return retval;
}
/**
* @brief nearDisValueAt
* 精确查找 邻点坐标 的dis
* @param pos
* @param nx
* @param ny
* @return
*/
int LibPoint::POS_nearDisValueAt(POS pos,int nx,int ny){
if (pos == NULL){return -1;}
Edge edge = pos->edge;
while (edge) {
if (edge->end->x == nx && edge->end->y == ny){
return edge->dis;
}
edge = edge->next;
}
return 0;
}
/**
* @brief nearDisFind
* 模糊查找 邻点坐标 dis
* @param pos
* @param nx
* @param ny
* @return
*/
int LibPoint::POS_nearDisFind(POS pos ,int nx,int ny){
if (pos == NULL){return -1;}
Edge edge = pos->edge;
while (edge) {
if (edge->end->x >= nx - RANGE && edge->end->y >= ny - RANGE &&
edge->end->x <= nx + RANGE && edge->end->y <= ny + RANGE){
return edge->dis;
}
edge = edge->next;
}
return 0;
}
QString LibPoint::POS_toString(POS pos){
if (pos == NULL){return "NULL";}
return "{point:" + SPos_toString(pos->point) +
",Edge:" + Edge_toString(pos->edge) +
"}";
}
/********* SPos 操作方法 ****************/
/***************************************/
/** 构造方法
* @brief LibPoint::_SPos
* @return
*/
SPos LibPoint::_SPos(){
SPos retval = (SPos)malloc(sizeof (SSPos));
retval->x = 0;
retval->y = 0;
retval->build = 0;
return retval;
}
SPos LibPoint::_SPos(int x,int y){
SPos retval = (SPos)malloc(sizeof (SSPos));
retval->x = x;
retval->y = y;
QString name ;
QString about;
retval->build = 0;
return retval;
}
void LibPoint::free_spos(SPos p){
// free(p->name);
// free(p->about);
free(p);
}
QString LibPoint::SPos_toString(SPos spos){
if (spos == NULL){return "NULL";}
return "{x:" +
QString::number(spos->x) +
",y:" +
QString::number(spos->y) +
",build:" +
QString::number(1) +
"}";
}
/*****************************************/
/********* Edge 操作方法****************/
/***************************************/
Edge LibPoint:: _Edge(){
Edge retval = (Edge)malloc(sizeof (SEdge));
retval->dis = 0;
retval->end = NULL;
retval->next = NULL;
return retval;
}
Edge LibPoint:: _Edge(int nx,int ny){
Edge retval = (Edge)malloc(sizeof (SEdge));
retval->dis = 0;
retval->end = _SPos(nx,ny);
retval->next = NULL;
return retval;
}
QString LibPoint::Edge_toString(Edge edge){
if (edge == NULL){return "NULL";}
int n = 1;
QString retval = "{";
Edge tmp = edge;
while (tmp) {
retval.append("{No." + QString::number(n) +
",dis:" +
QString::number(tmp->dis) +
",end:" +
SPos_toString(tmp->end) +
"}");
tmp = tmp->next;
n++;
}
retval.append("}");
return retval;
}
void LibPoint::free_edge(Edge edge){
if (edge->next == NULL){
free(edge);
return;
}
free_edge(edge->next);
free(edge);
}
/*****************************************/