-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
200 lines (165 loc) · 5.15 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dijkstra.h"
#include "a_star.h"
#include "lpa_star.h"
#include "rrt.h"
#include <iostream>
#include <random>
#include <QPainter>
#include <QVector>
#include <QPoint>
#include <QLine>
#include <QDebug>
#define A_STAR 1
//#define LPA_STAR 1
//#define RRT_ 1
constexpr int n = 21;
std::vector<std::vector<int>> grid(n, std::vector<int>(n, 0));
std::vector<std::vector<int>> costGrid(n, std::vector<int>(n, 100000));
Node start(10, 10, 0, 0, 0, 0);
Node goal(2, 20, 0, 0, 0, 0);
std::vector<Node> path_vector;
Dijkstra new_dijkstra;
AStar new_a_star;
PaintWidget* graphWidget;
LPAStar new_lpa_star;
RRT new_rrt;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Dijkstra new_dijkstra;
graphWidget = new PaintWidget(nullptr);
graphWidget->setGeometry(0,0,840,900);
graphWidget->setStyleSheet("background-color: rgb(0, 0, 0);");
// get a random grid graph
MakeGrid(grid);
// std::random_device rd; // obtain a random number from hardware
// std::mt19937 eng(rd()); // seed the generator
// std::uniform_int_distribution<int> distr(0, n - 1); // define the range
// Node start(distr(eng), distr(eng), 0, 0, 0, 0);
// Node goal(distr(eng), distr(eng), 0, 0, 0, 0);
start.id_ = start.x_ * n + start.y_;
start.pid_ = start.x_ * n + start.y_;
goal.id_ = goal.x_ * n + goal.y_;
start.h_cost_ = abs(start.x_ - goal.x_) + abs(start.y_ - goal.y_);
// Make sure start and goal are not obstacles and their ids are correctly
// assigned.
grid[start.x_][start.y_] = 0;
grid[goal.x_][goal.y_] = 0;
//PrintGrid(grid);
// Store points after algorithm has run
std::vector<std::vector<int>> main_grid = grid;
// Variables for RRT and RRTStar
constexpr double threshold = 2;
constexpr int max_iter_x_factor = 20;
//grid = main_grid;
#ifdef DIJKSTRA
new_dijkstra.doDijkstra(grid, start, goal);
#elif defined (A_STAR)
new_a_star.a_star(grid, start, goal);
#elif defined (LPA_STAR)
path_vector.clear();
new_lpa_star.lpa_star(grid, start, goal, n, true);
#elif defined (D_STAR_LITE)
#elif defined (RRT_)
path_vector = new_rrt.rrt(grid, start, goal, max_iter_x_factor, threshold);
#elif defined (PRM)
#endif
// use QPainter draw graph on widget
// path_vector = new_dijkstra.closed_list_;
// PrintPath(path_vector, start, goal, grid);
update();
}
void PaintWidget::paintEvent(QPaintEvent *event)
{
#ifdef DIJKSTRA
path_vector = new_dijkstra.closed_list_;
#elif defined (A_STAR)
path_vector = new_a_star.closed_list_;
#elif defined (LPA_STAR)
path_vector = new_lpa_star.path_vector_;
#elif defined (D_STAR_LITE)
#elif defined (RRT_)
#elif defined (PRM)
#endif
PrintPath(path_vector, start, goal, grid);
Q_UNUSED(event);
QPainter painter(this);
QPen pen;
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
// draw the widget border
pen.setStyle(Qt::SolidLine);
pen.setWidth(1);
pen.setBrush(Qt::red);
painter.setPen(pen);
painter.drawRect(0,0,839,839);
pen.setStyle(Qt::SolidLine);
pen.setWidth(2);
pen.setBrush(Qt::black);
painter.setPen(pen);
#ifdef DIJKSTRA
painter.drawText(10,860,"The Dijkstra Algorithm");
#elif defined (A_STAR)
painter.drawText(10,860,"The A-star Algorithm");
#elif defined (LPA_STAR)
painter.drawText(10,860,"The LPA-star Algorithm");
#elif defined (D_STAR_LITE)
painter.drawText(10,860,"The D_STAR_LITE Algorithm");
#elif defined (RRT_)
painter.drawText(10,860,"The RRT Algorithm");
#elif defined (PRM)
painter.drawText(10,860,"The PRM Algorithm");
#endif
// draw the graph
int n = grid.size();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
switch(grid[i][j])
{
case 1:
painter.fillRect(40*j+1,40*i+1,38,38,QBrush(Qt::red));
break;
case 2:
painter.fillRect(40*j+1,40*i+1,38,38,QBrush(Qt::blue));
break;
case 3:
painter.fillRect(40*j+1,40*i+1,38,38,QBrush(Qt::green));
break;
case 4:
painter.fillRect(40*j+1,40*i+1,38,38,QBrush(Qt::yellow));
break;
case 5:
painter.fillRect(40*j+1,40*i+1,38,38,QBrush(Qt::darkYellow));
grid[i][j] = 2;
break;
case 6:
painter.fillRect(40*j+1,40*i+1,38,38,QBrush(Qt::darkGray));
break;
}
}
}
if(path_vector.size()>=2)
{
// draw the initial node and goal node
pen.setWidth(20);
pen.setBrush(Qt::black);
painter.setPen(pen);
painter.drawPoint(40*path_vector.back().y_+20,40*path_vector.back().x_+20);
pen.setBrush(Qt::magenta);
painter.setPen(pen);
painter.drawPoint(40*path_vector.front().y_+20,40*path_vector.front().x_+20);
}
}
PaintWidget::PaintWidget(QWidget *parent) : QWidget(parent){}
PaintWidget::~PaintWidget(){}
MainWindow::~MainWindow()
{
delete ui;
delete graphWidget;
}