]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - src/rrts.cc
Steer to goals from just added nodes, too
[hubacji1/rrts.git] / src / rrts.cc
1 #include <algorithm>
2 #include "rrts.h"
3
4 #include "reeds_shepp.h"
5
6 #define ETA 1.0 // for steer, nv
7 #define GAMMA(cV) ({ \
8         __typeof__ (cV) _cV = (cV); \
9         pow(log(_cV) / _cV, 1.0 / 3.0); \
10 })
11
12 RRTNode::RRTNode()
13 {
14 }
15
16 RRTNode::RRTNode(const BicycleCar &bc) : BicycleCar(bc)
17 {
18 }
19
20 Obstacle::Obstacle()
21 {
22 }
23
24 // RRT procedures
25 bool RRTS::collide(std::vector<std::tuple<double, double>> &poly)
26 {
27         for (auto &o: this->obstacles())
28                 if (std::get<0>(::collide(poly, o.poly())))
29                         return true;
30         return false;
31 }
32
33 bool RRTS::collide_steered_from(RRTNode &f)
34 {
35         std::vector<std::tuple<double, double>> s;
36         s.push_back(std::make_tuple(f.x(), f.y()));
37         for (auto &n: this->steered()) {
38                 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
39                 s.push_back(std::make_tuple(n.lrx(), n.lry()));
40                 s.push_back(std::make_tuple(n.rrx(), n.rry()));
41                 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
42         }
43         return this->collide(s);
44 }
45
46 bool RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
47 {
48         std::vector<std::tuple<double, double>> p;
49         p.push_back(std::make_tuple(f.lfx(), f.lfy()));
50         p.push_back(std::make_tuple(f.lrx(), f.lry()));
51         p.push_back(std::make_tuple(f.rrx(), f.rry()));
52         p.push_back(std::make_tuple(f.rfx(), f.rfy()));
53         p.push_back(std::make_tuple(t.lfx(), t.lfy()));
54         p.push_back(std::make_tuple(t.lrx(), t.lry()));
55         p.push_back(std::make_tuple(t.rrx(), t.rry()));
56         p.push_back(std::make_tuple(t.rfx(), t.rfy()));
57         return this->collide(p);
58 }
59
60 double RRTS::cost(RRTNode &f, RRTNode &t)
61 {
62         double cost = 0;
63         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
64         return cost;
65 }
66
67 double RRTS::cost_build(RRTNode &f, RRTNode &t)
68 {
69         double q0[] = {f.x(), f.y(), f.h()};
70         double q1[] = {t.x(), t.y(), t.h()};
71         ReedsSheppStateSpace rsss(f.mtr());
72         return rsss.distance(q0, q1);
73 }
74
75 double RRTS::cost_search(RRTNode &f, RRTNode &t)
76 {
77         double cost = 0;
78         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
79         double heur = std::min(
80                 std::abs(t.h() - f.h()),
81                 2 * M_PI - std::abs(t.h() - f.h())
82         );
83         heur *= f.mtr();
84         cost = std::max(cost, heur);
85         return cost;
86 }
87
88 void RRTS::sample()
89 {
90         double x = this->ndx_(this->gen_);
91         double y = this->ndy_(this->gen_);
92         double h = this->ndh_(this->gen_);
93         this->samples().push_back(RRTNode());
94         this->samples().back().x(x);
95         this->samples().back().y(y);
96         this->samples().back().h(h);
97 }
98
99 RRTNode *RRTS::nn(RRTNode &t)
100 {
101         RRTNode *nn = &this->nodes().front();
102         double cost = this->cost(*nn, t);
103         for (auto &f: this->nodes()) {
104                 if (this->cost(f, t) < cost) {
105                         nn = &f;
106                         cost = this->cost(f, t);
107                 }
108         }
109         return nn;
110 }
111
112 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
113 {
114         std::vector<RRTNode *> nv;
115         double cost = std::min(GAMMA(this->nodes().size()), ETA);
116         for (auto &f: this->nodes())
117                 if (this->cost(f, t) < cost)
118                         nv.push_back(&f);
119         return nv;
120 }
121
122 int cb_rs_steer(double q[3], void *user_data)
123 {
124         std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
125         nodes->push_back(RRTNode());
126         nodes->back().x(q[0]);
127         nodes->back().y(q[1]);
128         nodes->back().h(q[2]);
129         return 0;
130 }
131
132 void RRTS::steer(RRTNode &f, RRTNode &t)
133 {
134         this->steered().clear();
135         double q0[] = {f.x(), f.y(), f.h()};
136         double q1[] = {t.x(), t.y(), t.h()};
137         ReedsSheppStateSpace rsss(f.mtr());
138         rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
139 }
140
141 // RRT* procedures
142 bool RRTS::connect()
143 {
144         bool conn = false;
145         RRTNode *t = &this->steered().front();
146         RRTNode *f = this->nn(this->samples().back());
147         double cost = this->cost(*f, *t);
148         for (auto n: this->nv(*t)) {
149                 if (
150                         !this->collide_two_nodes(*n, *t)
151                         && this->cost(*n, *t) < cost
152                 ) {
153                         f = n;
154                         cost = this->cost(*n, *t);
155                 }
156         }
157         this->nodes().push_back(this->steered().front());
158         t = &this->nodes().back();
159         t->p(f);
160         t->c(this->cost(*f, *t));
161         conn = true;
162         return conn;
163 }
164
165 void RRTS::rewire()
166 {
167         RRTNode *f = &this->nodes().back();
168         for (auto n: this->nv(*f)) {
169                 if (
170                         !this->collide_two_nodes(*f, *n)
171                         && cc(*f) + this->cost(*f, *n) < cc(*n)
172                 )
173                         n->p(f);
174         }
175 }
176
177 // API
178 std::vector<RRTNode *> RRTS::path()
179 {
180         std::vector<RRTNode *> path;
181         if (this->goals().size() == 0)
182                 return path;
183         RRTNode *goal = &this->goals().front();
184         for (auto &n: this->goals()) {
185                 if (
186                         n.p() != nullptr
187                         && (n.c() < goal->c() || goal->p() == nullptr)
188                 ) {
189                         goal = &n;
190                 }
191         }
192         if (goal->p() == nullptr)
193                 return path;
194         while (goal != nullptr) {
195                 path.push_back(goal);
196                 goal = goal->p();
197         }
198         std::reverse(path.begin(), path.end());
199         return path;
200 }
201
202 bool RRTS::next()
203 {
204         bool next = true;
205         this->icnt_++;
206         this->sample();
207         this->steer(
208                 *this->nn(this->samples().back()),
209                 this->samples().back()
210         );
211         if (this->collide_steered_from(*this->nn(this->samples().back())))
212                 return next;
213         if (!this->connect())
214                 return next;
215         this->rewire();
216         unsigned scnt = this->steered().size();
217         this->steered().erase(this->steered().begin());
218         while (this->steered().size() > 0) {
219                 RRTNode *f = &this->nodes().back();
220                 this->nodes().push_back(this->steered().front());
221                 RRTNode *t = &this->nodes().back();
222                 t->p(f);
223                 t->c(this->cost(*f, *t));
224                 this->steered().erase(this->steered().begin());
225         }
226         RRTNode *just_added = &this->nodes().back();
227         while (scnt > 0) {
228                 scnt--;
229                 for (auto &g: this->goals()) {
230                         this->steer(*just_added, g);
231                         if (this->collide_steered_from(*just_added))
232                                 continue;
233                         RRTNode *f = just_added;
234                         while (this->steered().size() > 0) {
235                                 RRTNode s = this->steered().front();
236                                 this->nodes().push_back(s);
237                                 RRTNode *t = &this->nodes().back();
238                                 t->p(f);
239                                 t->c(this->cost(*f, *t));
240                                 this->steered().erase(this->steered().begin());
241                                 f = t;
242                         }
243                 }
244                 just_added = just_added->p();
245         }
246         for (auto &g: this->goals()) {
247                 double cost = this->cost(this->nodes().back(), g);
248                 double edist = sqrt(
249                         pow(this->nodes().back().x() - g.x(), 2)
250                         + pow(this->nodes().back().y() - g.y(), 2)
251                 );
252                 double adist = std::abs(this->nodes().back().h() - g.h());
253                 if (edist < 0.05 && adist < M_PI / 32) {
254                         next = false;
255                         if (
256                                 g.p() == nullptr
257                                 || cc(this->nodes().back()) + cost < cc(g)
258                         ) {
259                                 g.p(&this->nodes().back());
260                                 g.c(cost);
261                         }
262                 }
263         }
264         if (this->icnt_ > 999)
265                 next = false;
266         return next;
267 }
268
269 void RRTS::set_sample(
270         double mx, double dx,
271         double my, double dy,
272         double mh, double dh
273 )
274 {
275         this->ndx_ = std::normal_distribution<double>(mx, dx);
276         this->ndy_ = std::normal_distribution<double>(my, dy);
277         this->ndh_ = std::normal_distribution<double>(mh, dh);
278 }
279
280 RRTS::RRTS()
281         : gen_(std::random_device{}())
282 {
283         this->goals().reserve(1);
284         this->nodes().reserve(40000);
285         this->samples().reserve(1000);
286         this->steered().reserve(200);
287         this->nodes().push_back(RRTNode()); // root
288 }
289
290 double cc(RRTNode &t)
291 {
292         RRTNode *n = &t;
293         double cost = 0;
294         while (n != nullptr) {
295                 cost += n->c();
296                 n = n->p();
297         }
298         return cost;
299 }