-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
701 lines (566 loc) · 19.4 KB
/
widget.cpp
File metadata and controls
701 lines (566 loc) · 19.4 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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include "widget.h"
#include "./ui_widget.h"
#include "solveTSP.h"
#include "optimizeCP.h"
#include "optimizeCP_global.h"
#include <iostream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <QMenuBar>
#include <QFileDialog>
#include <QTextStream>
double optPara = 10000;
QPointF *draggedControlPoint = nullptr;
QPointF *centerPoint = nullptr;
QPointF *pairControlPoint = nullptr;
bool isDragging = false;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
setStyleSheet("background-color: white;");
setupMenu();
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMenuBar(menuBar);
setLayout(layout);
}
Widget::~Widget()
{
delete ui;
}
bool intersected_clicked = false;
bool boundingBox_clicked = false;
bool largeAngle_clicked = false;
bool test_clicked = false;
bool Optimize_clicked = false;
bool optCpGlobal_clicked = false;
bool showControlPoints = true;
void Line::sortByTSP() {
qDebug() << "Sorting TSP";
int n = point.size();
if (n <= 2) return;
std::vector<int> path = /*ans.path*/SolveTSP(point);
//assert(path.size() == n);
std::vector<QPointF> res;
for (int i = 0; i < n; i++) {
res.push_back(point[path[i]]);
}
assert(point.size() == n);
//qDebug() << "point size: " << point.size() << "\n";
point = res;
controlPoint.clear(); cost = 0;
}
/*
Algorithm 1: only consider current part
Step1: Give default control points
Step2: for each part, c1 move on the line defined by sp and c1
c2 moves free
optimize by considering
k1 * length + k2 * max_curvature, only in this part
Algorithm 2: consider the whole curve
Step1: Give default control points
Step2: All control points move freely
optimize by considering
k1 * total length + k2 * max_curvature of whole curve
curvature in endpoint are approximated by
curvature of lastc1 lastc2 sp c1
curvature of lastc2 sp c1 c2
Algorithm 3:
Step1: Give default control points
Step2:
We note that, even if both c1 and c2 in part i freely change
it will only affect part i-1, will not affect part 1,2,...,i-2
Iterate each part
When interating part i
- both c1 and c2 can move freely
- and c1 will affect the c2 in i-1 -th part
The valuating function:
considering the curve consisting of part 1 to i
length: total length of i curves
max curvature:
max(max(curvature of lastsp lastc1 lastc2 sp),
max(curvature of lastc1 lastc2 sp c1)
max(curvature of lastc2 sp c1 c2),
max(curvature of sp c1 c2 ep)))
*/
void Line::generateControlPoint() {
// i = 0, 1, 2, ..., n - 1
int i = controlPoint.size() / 2;
if (i > point.size() - 1) return;
QPointF sp = point[i], ep = point[i + 1];
QPointF c1, c2, dir;
vector<float> sumLength(point.size());
if (i == 0) {
c1 = QPointF((sp.x() + ep.x()) / 2, sp.y());
c2 = (c1 + ep) / 2;
controlPoint.push_back(c1);
controlPoint.push_back(c2);
return;
}
dir = sp - controlPoint.back();
dir = dir * len(ep - sp) / len(dir) / 3;
c1 = sp + dir;
c2 = (c1 + ep) / 2;
if (Optimize_clicked) {
auto opt = OptimizeControlPoints(1, optPara, point[i-1], controlPoint[controlPoint.size() - 2], controlPoint[controlPoint.size() - 1],
sp, c1, c2, ep);
//qDebug() << "before:" << c1 << c2;
c1 = std::get<0>(opt); c2 = std::get<1>(opt);
//qDebug() << "after:" << c1 << c2;
//qDebug() << "optPara:" << optPara;
//this->maxCurvature = max(this->maxCurvature, std::get<2>(opt));
auto lc2 = controlPoint.back();
lc2 = sp * 2 - c1;
controlPoint.pop_back();
controlPoint.push_back(lc2);
} else {
// auto opt = OptimizeControlPoints(0, optPara, point[i-1], controlPoint[controlPoint.size() - 2], controlPoint[controlPoint.size() - 1],
// sp, c1, c2, ep);
//this->maxCurvature = max(this->maxCurvature, std::get<2>(opt));
}
controlPoint.push_back(c1);
controlPoint.push_back(c2);
}
void Line::generateCurve() {
int n = point.size();
int m = controlPoint.size() / 2;
if (Optimize_clicked) controlPoint.clear(), cost= 0;
while (controlPoint.size() < (n - 1) * 2) {
qDebug() << controlPoint.size() << (n - 1) * 2;
generateControlPoint();
}
QPainterPath path(point[0]);
this->maxCurvature = 0;
for (int i = 0; i < n - 1; i++) {
path.cubicTo(controlPoint[i * 2], controlPoint[i * 2 + 1], point[i + 1]);
// 0 01 1 23 2 34
if (i != 0) {
auto opt = OptimizeControlPoints(0, optPara, point[i-1], controlPoint[(i-1)*2], controlPoint[(i-1)*2+1],
point[i], controlPoint[i*2], controlPoint[i*2+1], point[i + 1]);
maxCurvature = max(maxCurvature, std::get<2>(opt));
}
}
curve = path;
//Optimize_clicked = false;
//optCpGlobal_clicked = false;
}
QPointF getIntersection(QPointF posA, QPointF posB, QPointF posC, QPointF posD)//返回AB与CD交点,无交点返回(0,0)
{
QLineF line1(posA, posB);
QLineF line2(posC, posD);
QPointF interPos(0,0);
QLineF::IntersectType type = line1.intersects(line2, &interPos);
if (type != QLineF::BoundedIntersection)
interPos = QPointF(1e9, 1e9);
return interPos;
}
float angle(QPointF A, QPointF O, QPointF B) {
auto OA = A - O, OB = B - O;
auto cosAOB = QPointF::dotProduct(OA, OB) / (len(OA) * len(OB));
return acos(cosAOB);
}
float crossProduct(QPointF A, QPointF B) {
return A.x() * B.y() - B.x() * A.y();
}
void Widget::drawBezierCurve(/*Line &line*/ int line_id) {
auto &line = lines[line_id];
if (line.point.empty()) return;
//24 05 06 add invisable points
// if (largeAngle_clicked) {
// const int segnum = 40;
// std::vector<QPoint> pt, tmp, inters;
// pt = line.point;
// tmp = pt;
// inters.clear();
// //24 05 07 switch to piecewise linearity
// QPainterPath path = generatePath(line.point);
// QPointF last = path.pointAtPercent(0);
// pt.push_back(last.toPoint());
// for (int i = 1; i <= segnum; i++) {
// auto s1 = last, e1 = path.pointAtPercent(1.f * i / segnum);
// bool changed = false;
// for (int id = 0; id < line_id; id++) {
// auto pre_path = lines[id].path;
// for (int j = 1; j <= segnum; j++) {
// auto s2 = pre_path.pointAtPercent(1.f*(j-1)/segnum), e2 = pre_path.pointAtPercent(1.f*j/segnum);
// auto inter = getIntersection(s1, e1, s2, e2);
// inters.push_back(inter.toPoint());
// if (inter.x() > 1e6 || std::min(angle(s1, inter, s2), angle(s1, inter, e2)) > 3.14159 / 6)
// continue;
// changed = true;
// auto u = (s2 - inter);
// auto v1 = QPoint(u.y(), -u.x()), v2 = -v1;
// if (crossProduct(v1, s2 - inter) * crossProduct(s1 - inter, s2 - inter) < 0) {
// std::swap(v1, v2);
// }
// //s1, e1 -> ns1, ne1
// auto ns1 = inter + v1 * (length(s1 - e1) / length(v1));
// auto ne1 = inter + v2 * (length(s1 - e1) / length(v2));
// last = ne1;
// tmp.push_back(last.toPoint());
// pt.push_back(ns1.toPoint()); pt.push_back(ne1.toPoint());
// break;
// }
// if (changed) break;
// }
// if (!changed) {
// last = e1;
// tmp.push_back(last.toPoint());
// }
// }
// }
// if (Optimize_clicked) {
//}
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(line.color, 2));
if (Optimize_clicked || line.controlPoint.size() < (line.point.size() - 1) * 2) {
line.generateCurve();
}
painter.drawPath(line.curve);
if (test_clicked) {
// 绘制曲线上的分段点
painter.setBrush(Qt::blue);
}
// 绘制曲线上的可见点
for (int i = 0; i < line.point.size(); ++i) {
painter.setBrush(Qt::gray);
if (i == 0) {
painter.drawEllipse(line.point[i], 6, 6);
auto str = std::to_string(line_id);
painter.setBrush(Qt::red);
painter.drawText(line.point[i] + QPoint(10, 0), QString(str.c_str()));
}
else painter.drawEllipse(line.point[i], 4, 4);
}
//绘制Control Point
if (showControlPoints) {
for (auto pt:line.controlPoint) {
painter.setBrush(Qt::green);
painter.drawEllipse(pt, 4, 4);
}
painter.setBrush(Qt::red);
if(centerPoint) painter.drawEllipse(*centerPoint, 4, 4);
// 设置画笔为黑色虚线
QPen pen(Qt::black, 1);
QVector<qreal> dashes;
qreal space = 10; // 空白长度
qreal dashLength = 4; // 虚线长度
dashes << dashLength << space;
pen.setDashPattern(dashes);
painter.setPen(pen);
// 绘制两个点之间的黑色虚线
auto p = line.point;
auto cp = line.controlPoint;
auto n = p.size();
for (int i = 0; i < n - 1; i++) {
// 0 01 1 23 2 45 n-2 2n-4 2n-3 n-1
painter.drawLine(p[i], cp[i * 2]);
painter.drawLine(cp[i * 2], cp[i * 2 + 1]);
painter.drawLine(cp[i * 2 + 1], p[i + 1]);
}
}
}
void Widget::paintEvent(QPaintEvent *event)
{
qDebug() << "paintEvent ing..";
double maxCurvature = 0, totalLength = 0;
if (1 || Optimize_clicked || lines.back().controlPoint.size() < (lines.back().point.size() - 1) * 2) {
for (int i = 0; i < lines.size(); i++) {
drawBezierCurve(i);
maxCurvature = max(maxCurvature, lines[i].maxCurvature);
totalLength += lines[i].curve.length();
}
}
QPainter painter(this);
// if (intersected_clicked) {
// for (auto line1 : lines) {
// for (auto line2 : lines) {
// if (line1.color == line2.color) continue;
// QPainterPath path1 = line1.curve;
// QPainterPath path2 = line2.curve;
// QPainterPath path_intersected = path1.intersected(path2);
// painter.setRenderHint(QPainter::Antialiasing, true);
// painter.setPen(QPen(QColor(255, 0, 0), 2));
// painter.drawPath(path_intersected);
// }
// }
// }
// if (boundingBox_clicked) {
// for (auto &line : lines) {
// QRectF boundingBox = line.curve.boundingRect();
// QPainterPath path;
// path.addRect(boundingBox);
// painter.setRenderHint(QPainter::Antialiasing, true);
// painter.setPen(QPen(QColor(0, 0, 255), 2));
// painter.drawPath(path);
// }
// }
qDebug() << maxCurvature;
ui->labelLineNumber->setText("Line number:" + QString(QString::number((int)lines.size())));
ui->labelTotalLength->setText("Length: " + QString(QString::number(totalLength)));
ui->labelMaxCurvature->setText("MaxCurvarure: " + QString(QString::number(maxCurvature)));
ui->labelTotalCost->setText("Cost: " + QString(QString::number(totalLength + optPara * maxCurvature)));
//qDebug() << "line number:" << lines.size() << "\n";
Optimize_clicked = 0;
}
void Widget::on_btnNewLine_clicked()
{
qDebug() << "New Line Clicked\n";
if (lines.empty() || !lines.back().point.empty()) lines.push_back(Line());
qDebug() << "Now line number:" << lines.size() << "\n";
update();
}
void Widget::on_btnClear_clicked()
{
qDebug() << "Clear Clicked\n";
lines.clear();
qDebug() << "Now line number:" << lines.size() << "\n";
update();
}
void Widget::on_btnRandom_clicked()
{
//lines.clear();
int pointNumber = 5;
Line l;
for (int i = 0; i < pointNumber; i++) {
l.point.push_back(generateRandomPoint());
}
//while (lines.back().point.empty()) lines.pop_back();
lines.push_back(l);
update();
}
QPoint Widget::generateRandomPoint()
{
int width = this->width() - 100;
int height = this->height() - 100;
// 生成随机的x和y坐标
int x = QRandomGenerator::global()->bounded(width) + 50;
int y = QRandomGenerator::global()->bounded(height) + 50;
qDebug() << x << " " << y << Qt::endl;
return QPoint(x, y);
}
struct DP {
double dis = 1e9;
std::vector<int> path;
bool operator<(const DP &t) const {
return dis < t.dis;
}
};
void Widget::on_bthSortCurrent_clicked()
{
if (!lines.empty()) {
qDebug() << "Length before sort: " << lines.back().curve.length() << "\n";
lines.back().sortByTSP();
qDebug() << "Length after sort: " << lines.back().curve.length() << "\n";
}
update();
}
void Widget::on_bthSortAll_clicked()
{
int cnt = 0;
for (auto &line : lines) {
line.sortByTSP();
qDebug() << "OK " << ++cnt << "\n";
}
update();
}
void Widget::on_bthIntersected_clicked()
{
intersected_clicked = !intersected_clicked;
qDebug() << "intersected_clicked\n";
update();
}
void Widget::on_labelLineNumber_linkActivated(const QString &link)
{
ui->labelLineNumber->setText("Line number:" + QString(QString::number((int)lines.size())));
}
void Widget::on_btnBoundingBox_clicked()
{
boundingBox_clicked = !boundingBox_clicked;
qDebug() << "boundingBox_clicked\n";
update();
}
void Widget::on_bthLargeAngle_clicked()
{
largeAngle_clicked = !largeAngle_clicked;
qDebug() << "large angle is" << (largeAngle_clicked ? "on\n" : "off\n");
update();
}
void Widget::on_bthTest_clicked()
{
test_clicked = !test_clicked;
qDebug() << "test_clicked is" << (test_clicked ? "on\n" : "off\n");
update();
}
void Widget::on_btnOptimize_clicked()
{
Optimize_clicked = !Optimize_clicked;
qDebug() << "Optimize_clicked is" << (Optimize_clicked ? "on\n" : "off\n");
update();
}
void Widget::on_bthCP_clicked()
{
if (showControlPoints) {
showControlPoints = false;
ui->bthCP->setText("Show Control Points");
} else {
showControlPoints = true;
ui->bthCP->setText("Hide Control Points");
}
update();
}
void Widget::on_btnOptCpGlobal_clicked()
{
optCpGlobal_clicked = !optCpGlobal_clicked;
qDebug() << "optCpGlobal_clicked is" << (optCpGlobal_clicked ? "on\n" : "off\n");
update();
}
void Widget::on_lineEdit_textChanged(const QString &arg1)
{
optPara = arg1.toDouble();
}
void Widget::on_btnUndo_clicked()
{
if (!lines.empty()) {
auto &l = lines.back();
if (!l.point.empty()) {
l.point.pop_back();
while (l.controlPoint.size() > (l.point.size() - 1) * 2)
l.controlPoint.pop_back();
l.generateCurve();
}
}
update();
}
void Widget::mousePressEvent(QMouseEvent *event)
{
QPointF clickedPoint = event->pos();
isDragging = false;
// 检查是否点击在控制点上
for (auto &line : lines) {
auto &cp = line.controlPoint;
for (int i = 0; i < cp.size(); i++) {
auto pt = &cp[i];
if (QLineF(clickedPoint, *pt).length() < 5) {
draggedControlPoint = pt;
if (i == 0) {
pairControlPoint = nullptr;
centerPoint = &line.point[0];
} else if (i == cp.size() - 1) {
pairControlPoint = nullptr;
centerPoint = &line.point.back();
} else if (i % 2 == 0) {
pairControlPoint = pt - 1;
centerPoint = &line.point[(i + 1) / 2];
// 12:1 34:2 56:3 ..
} else {
pairControlPoint = pt + 1;
centerPoint = &line.point[(i + 1) / 2];
}
isDragging = true;
break;
}
}
}
// 如果没有点击在控制点上,添加新点
if (!isDragging) {
if (lines.empty()) {
lines.push_back(Line());
}
lines.back().point.push_back(clickedPoint);
update();
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (isDragging && draggedControlPoint) {
*draggedControlPoint = event->pos();
if (pairControlPoint) {
auto old_len = len(*pairControlPoint - *centerPoint);
auto dir = *centerPoint - *draggedControlPoint;
dir = dir * old_len / len(dir);
*pairControlPoint = *centerPoint + dir;
}
for (auto &line : lines) {
line.generateCurve();
}
update();
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
draggedControlPoint = centerPoint = pairControlPoint = nullptr;
isDragging = false;
update();
}
void Widget::setupMenu()
{
menuBar = new QMenuBar(this);
QMenu *fileMenu = new QMenu("File", this);
importAction = new QAction("Load", this);
connect(importAction, &QAction::triggered, this, &Widget::importFromFile);
fileMenu->addAction(importAction);
exportAction = new QAction("Save", this);
connect(exportAction, &QAction::triggered, this, &Widget::exportToFile);
fileMenu->addAction(exportAction);
menuBar->addMenu(fileMenu);
}
void Widget::exportToFile()
{
QString fileName = QFileDialog::getSaveFileName(this, "保存文件", "", "Text Files (*.txt)");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
for (int i = 0; i < lines.size(); ++i) {
out << i << "\n";
for (const auto &pt : lines[i].controlPoint) {
out << pt.x() << " " << pt.y() << "\n";
}
}
file.close();
}
}
}
void Widget::importFromFile()
{
QString fileName = QFileDialog::getOpenFileName(this, "打开文件", "", "Text Files (*.txt)");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
lines.clear();
Line line;
int lineIndex = -1;
while (!in.atEnd()) {
QString lineText = in.readLine();
if (lineText.trimmed().isEmpty()) {
continue;
}
bool ok;
if (lineText.toInt(&ok)) {
if (lineIndex != -1) {
lines.push_back(line);
line.controlPoint.clear();
line.point.clear();
}
lineIndex = lineText.toInt();
} else {
QStringList coords = lineText.split(' ');
if (coords.size() == 2) {
QPointF pt(coords[0].toFloat(), coords[1].toFloat());
line.point.push_back(pt);
}
}
}
if (lineIndex != -1) {
lines.push_back(line);
}
file.close();
update();
}
}
}