-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmainwindow.cpp
129 lines (119 loc) · 4.62 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
renderArea = ui->renderArea;
rrtstar = renderArea->rrtstar;
simulated = false;
}
/**
* @brief Start the simulator.
*/
void MainWindow::on_startButton_clicked()
{
if (simulated) {
ui->statusBox->setText(tr("Please reset!"));
renderArea->update();
return;
}
simulated = true;
// get step size and max iterations from GUI.
rrtstar->setMaxIterations(ui->maxIterations->text().toInt());
rrtstar->setStepSize(ui->stepSize->text().toInt());
assert(rrtstar->step_size > 0);
assert(rrtstar->max_iter > 0);
// RRTSTAR Algorithm
for(int i = 0; i < renderArea->rrtstar->max_iter; i++) {
Node *q = rrtstar->getRandomNode();
if (q) {
Node *qNearest = rrtstar->nearest(q->position);
if (rrtstar->distance(q->position, qNearest->position) > rrtstar->step_size) {
Vector3f newConfigPosOrient;
DubinsPath path;
if (BOT_FOLLOW_DUBIN)
newConfigPosOrient = rrtstar->newDubinConfig(q, qNearest, path);
else
newConfigPosOrient = rrtstar->newConfig(q, qNearest);
Vector2f newConfigPos(newConfigPosOrient.x(), newConfigPosOrient.y());
if (!rrtstar->obstacles->isSegmentInObstacle(newConfigPos, qNearest->position)) {
Node *qNew = new Node;
qNew->position = newConfigPos;
qNew->orientation = newConfigPosOrient.z();
qNew->path = path;
vector<Node *> Qnear;
rrtstar->near(qNew->position, rrtstar->step_size*RRTSTAR_NEIGHBOR_FACTOR, Qnear);
qDebug() << "Found Nearby " << Qnear.size() << "\n";
Node *qMin = qNearest;
double cmin = rrtstar->Cost(qNearest) + rrtstar->PathCost(qNearest, qNew);
for(int j = 0; j < Qnear.size(); j++){
Node *qNear = Qnear[j];
if(!rrtstar->obstacles->isSegmentInObstacle(qNear->position, qNew->position) &&
(rrtstar->Cost(qNear)+rrtstar->PathCost(qNear, qNew)) < cmin ){
qMin = qNear; cmin = rrtstar->Cost(qNear)+rrtstar->PathCost(qNear, qNew);
}
}
rrtstar->add(qMin, qNew);
for(int j = 0; j < Qnear.size(); j++){
Node *qNear = Qnear[j];
if(!rrtstar->obstacles->isSegmentInObstacle(qNew->position, qNear->position) &&
(rrtstar->Cost(qNew)+rrtstar->PathCost(qNew, qNear)) < rrtstar->Cost(qNear) ){
Node *qParent = qNear->parent;
// Remove edge between qParent and qNear
qParent->children.erase(std::remove(qParent->children.begin(), qParent->children.end(), qNear), qParent->children.end());
// Add edge between qNew and qNear
qNear->cost = rrtstar->Cost(qNew) + rrtstar->PathCost(qNew, qNear);
qNear->parent = qNew;
qNew->children.push_back(qNear);
}
}
}
}
}
if (rrtstar->reached()) {
ui->statusBox->setText(tr("Reached Destination!"));
break;
}
renderArea->update();
qApp->processEvents();
}
Node *q;
if (rrtstar->reached()) {
q = rrtstar->lastNode;
}
else
{
// if not reached yet, then shortestPath will start from the closest node to end point.
q = rrtstar->nearest(rrtstar->endPos);
ui->statusBox->setText(tr("Exceeded max iterations!"));
}
// generate shortest path to destination.
while (q != NULL) {
rrtstar->path.push_back(q);
q = q->parent;
}
renderArea->update();
}
/**
* @brief Delete all obstacles, nodes and paths from the simulator.
*/
void MainWindow::on_resetButton_clicked()
{
simulated = false;
ui->statusBox->setText(tr(""));
rrtstar->obstacles->obstacles.clear();
rrtstar->obstacles->obstacles.resize(0);
rrtstar->deleteNodes(rrtstar->root);
rrtstar->nodes.clear();
rrtstar->nodes.resize(0);
rrtstar->path.clear();
rrtstar->path.resize(0);
rrtstar->initialize();
renderArea->update();
}
MainWindow::~MainWindow()
{
delete ui;
}