-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrrt.cpp
236 lines (217 loc) · 7.55 KB
/
rrt.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
/**
* @file rrt.cpp
* @author ShieldQiQi
* @brief Contains the RRT class
*/
#include "rrt.h"
#include <cmath>
#include <random>
// constants
constexpr double half_grid_unit = 0.5;
constexpr double tol_l_limit = 0.000001;
Node RRT::FindNearestPoint(Node& new_node) const {
Node nearest_node(-1, -1, -1, -1, -1, -1);
std::vector<Node>::const_iterator it_v;
std::vector<Node>::const_iterator it_v_store;
// use just distance not total cost
auto dist = static_cast<double>(n * n);
for (it_v = point_list_.begin(); it_v != point_list_.end(); ++it_v) {
auto new_dist = static_cast<double>(
std::sqrt(static_cast<double>(it_v->x_ - new_node.x_) *
static_cast<double>(it_v->x_ - new_node.x_) +
static_cast<double>(it_v->y_ - new_node.y_) *
static_cast<double>(it_v->y_ - new_node.y_)));
if (new_dist > threshold_) {
continue;
}
if (CheckObstacle(*it_v, new_node)) {
continue;
}
if (it_v->id_ == new_node.id_) {
continue;
}
if (it_v->pid_ == new_node.id_) {
continue;
}
if (new_dist >= dist) {
continue;
}
dist = new_dist;
it_v_store = it_v;
}
if (dist != n * n) {
nearest_node = *it_v_store;
new_node.pid_ = nearest_node.id_;
new_node.cost_ = nearest_node.cost_ + dist;
}
return nearest_node;
}
bool RRT::CheckObstacle(const Node& n_1, const Node& n_2) const {
if (n_2.y_ - n_1.y_ == 0) {
double c = n_2.y_;
for (const auto& obs_node : obstacle_list_) {
if (!(((n_1.x_ >= obs_node.x_) && (obs_node.x_ >= n_2.x_)) ||
((n_1.x_ <= obs_node.x_) && (obs_node.x_ <= n_2.x_)))) {
continue;
}
if (static_cast<double>(obs_node.y_) == c) {
return true;
}
}
} else {
double slope = static_cast<double>(n_2.x_ - n_1.x_) /
static_cast<double>(n_2.y_ - n_1.y_);
double c =
static_cast<double>(n_2.x_) - slope * static_cast<double>(n_2.y_);
for (const auto& obs_node : obstacle_list_) {
if (!(((n_1.y_ >= obs_node.y_) && (obs_node.y_ >= n_2.y_)) ||
((n_1.y_ <= obs_node.y_) && (obs_node.y_ <= n_2.y_)))) {
continue;
}
if (!(((n_1.x_ >= obs_node.x_) && (obs_node.x_ >= n_2.x_)) ||
((n_1.x_ <= obs_node.x_) && (obs_node.x_ <= n_2.x_)))) {
continue;
}
std::vector<double> arr(4);
// Using properties of a point and a line here.
// If the obtacle lies on one side of a line, substituting its edge points
// (all obstacles are grid sqaures in this example) into the equation of
// the line passing through the coordinated of the two nodes under
// consideration will lead to all four resulting values having the same
// sign. Hence if their sum of the value/abs(value) is 4 the obstacle is
// not in the way. If a single point is touched ie the substitution leads
// ot a value under 10^-7, it is set to 0. Hence the obstacle has
// 1 point on side 1, 3 points on side 2, the sum is 2 (-1+3)
// 2 point on side 1, 2 points on side 2, the sum is 0 (-2+2)
// 0 point on side 1, 3 points on side 2, (1 point on the line, ie,
// grazes the obstacle) the sum is 3 (0+3)
// Hence the condition < 3
arr[0] = static_cast<double>(obs_node.x_) + half_grid_unit -
slope * (static_cast<double>(obs_node.y_) + half_grid_unit) - c;
arr[1] = static_cast<double>(obs_node.x_) + half_grid_unit -
slope * (static_cast<double>(obs_node.y_) - half_grid_unit) - c;
arr[2] = static_cast<double>(obs_node.x_) - half_grid_unit -
slope * (static_cast<double>(obs_node.y_) + half_grid_unit) - c;
arr[3] = static_cast<double>(obs_node.x_) - half_grid_unit -
slope * (static_cast<double>(obs_node.y_) - half_grid_unit) - c;
double count = 0;
for (auto& a : arr) {
if (std::fabs(a) <= tol_l_limit) {
a = 0;
} else {
count += a / std::fabs(a);
}
}
if (std::abs(count) < 3) {
return true;
}
}
}
return false;
}
Node RRT::GenerateRandomNode(const int n) {
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
int x = distr(eng);
int y = distr(eng);
Node new_node(x, y, 0, 0, n * x + y, 0);
return new_node;
}
std::vector<Node> RRT::rrt(std::vector<std::vector<int>>& grid,
const Node& start_in, const Node& goal_in,
const int max_iter_x_factor,
const double threshold_in) {
start_ = start_in;
goal_ = goal_in;
n = grid.size();
threshold_ = threshold_in;
int max_iter = max_iter_x_factor * n * n;
CreateObstacleList(grid);
point_list_.push_back(start_);
Node new_node = start_;
grid[start_.x_][start_.y_] = 2;
if (CheckGoalVisible(new_node)) {
return this->point_list_;
}
int iter = 0;
while (iter <= max_iter) {
iter++;
new_node = GenerateRandomNode(n);
if (grid[new_node.x_][new_node.y_] != 0) {
continue;
}
Node nearest_node = FindNearestPoint(new_node);
if (nearest_node.id_ == -1) {
continue;
}
grid[new_node.x_][new_node.y_] = 2;
point_list_.push_back(new_node);
if (CheckGoalVisible(new_node)) {
return this->point_list_;
}
}
Node no_path_node(-1, -1, -1, -1, -1, -1);
point_list_.clear();
point_list_.push_back(no_path_node);
return point_list_;
}
bool RRT::CheckGoalVisible(const Node& new_node) {
if (!CheckObstacle(new_node, goal_)) {
auto new_dist = static_cast<double>(
std::sqrt(static_cast<double>(goal_.x_ - new_node.x_) *
static_cast<double>(goal_.x_ - new_node.x_) +
static_cast<double>(goal_.y_ - new_node.y_) *
static_cast<double>(goal_.y_ - new_node.y_)));
if (new_dist <= threshold_) {
goal_.pid_ = new_node.id_;
goal_.cost_ = new_dist + new_node.cost_;
point_list_.push_back(goal_);
return true;
}
}
return false;
}
void RRT::CreateObstacleList(std::vector<std::vector<int>>& grid) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
Node obs(i, j, 0, 0, i * n + j, 0);
obstacle_list_.push_back(obs);
}
}
}
}
#ifdef BUILD_INDIVIDUAL
/**
* @brief Script main function. Generates start and end nodes as well as grid,
* then creates the algorithm object and calls the main algorithm function.
* @return 0
*/
int main() {
int n = 11;
std::vector<std::vector<int>> grid(n, std::vector<int>(n));
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);
RRT new_rrt;
double threshold = 2;
int max_iter_x_factor = 20;
std::vector<Node> path_vector =
new_rrt.rrt(grid, start, goal, max_iter_x_factor, threshold);
PrintPath(path_vector, start, goal, grid);
return 0;
}
#endif // BUILD_INDIVIDUAL