forked from sourishg/rrt-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrt.cpp
150 lines (138 loc) · 3.06 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
#include "rrt.h"
RRT::RRT()
{
obstacles = new Obstacles;
startPos.x() = START_POS_X;
startPos.y() = START_POS_Y;
endPos.x() = END_POS_X;
endPos.y() = END_POS_Y;
root = new Node;
root->parent = NULL;
root->position = startPos;
lastNode = root;
nodes.push_back(root);
step_size = 3;
max_iter = 3000;
}
/**
* @brief Initialize root node of RRT.
*/
void RRT::initialize()
{
root = new Node;
root->parent = NULL;
root->position = startPos;
lastNode = root;
nodes.push_back(root);
}
/**
* @brief Modify the tree node for parallel version
*/
void RRT::modify_start_goal(float start_x, float start_y, float goal_x, float goal_y, Obstacles *obs)
{
startPos.x() = start_x;
startPos.y() = start_y;
endPos.x() = goal_x;
endPos.y() = goal_y;
obstacles = obs;
}
/**
* @brief Generate a random node in the field.
* @return
*/
Node* RRT::getRandomNode()
{
Node* ret;
Vector2f point(drand48() * WORLD_WIDTH, drand48() * WORLD_HEIGHT);
if ((point.x() >= startPos.x() || point.y() >= startPos.y()) && point.x() <= WORLD_WIDTH && point.y() <= WORLD_HEIGHT) {
ret = new Node;
ret->position = point;
return ret;
}
return NULL;
}
/**
* @brief Helper method to find distance between two positions.
* @param p
* @param q
* @return
*/
int RRT::distance(Vector2f &p, Vector2f &q)
{
Vector2f v = p - q;
return sqrt(powf(v.x(), 2) + powf(v.y(), 2));
}
/**
* @brief Get nearest node from a given configuration/position.
* @param point
* @return
*/
Node* RRT::nearest(Vector2f point)
{
float minDist = 1e9;
Node *closest = NULL;
for(int i = 0; i < (int)nodes.size(); i++) {
float dist = distance(point, nodes[i]->position);
if (dist < minDist) {
minDist = dist;
closest = nodes[i];
}
}
return closest;
}
/**
* @brief Find a configuration at a distance step_size from nearest node to random node.
* @param q
* @param qNearest
* @return
*/
Vector2f RRT::newConfig(Node *q, Node *qNearest)
{
Vector2f to = q->position;
Vector2f from = qNearest->position;
Vector2f intermediate = to - from;
intermediate = intermediate / intermediate.norm();
Vector2f ret = from + step_size * intermediate;
return ret;
}
/**
* @brief Add a node to the tree.
* @param qNearest
* @param qNew
*/
void RRT::add(Node *qNearest, Node *qNew)
{
qNew->parent = qNearest;
qNearest->children.push_back(qNew);
nodes.push_back(qNew);
lastNode = qNew;
}
/**
* @brief Check if the last node is close to the end position.
* @return
*/
bool RRT::reached()
{
if (distance(lastNode->position, endPos) < END_DIST_THRESHOLD)
return true;
return false;
}
void RRT::setStepSize(int step)
{
step_size = step;
}
void RRT::setMaxIterations(int iter)
{
max_iter = iter;
}
/**
* @brief Delete all nodes using DFS technique.
* @param root
*/
void RRT::deleteNodes(Node *root)
{
for(int i = 0; i < (int)root->children.size(); i++) {
deleteNodes(root->children[i]);
}
delete root;
}