]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - src/rrts.cc
Merge branch 'feature/search-grid'
[hubacji1/rrts.git] / src / rrts.cc
1 #include <algorithm>
2 #include "rrts.h"
3
4 #include "reeds_shepp.h"
5
6 template <typename T> int sgn(T val) {
7         return (T(0) < val) - (val < T(0));
8 }
9
10 RRTNode::RRTNode()
11 {
12 }
13
14 RRTNode::RRTNode(const BicycleCar &bc) : BicycleCar(bc)
15 {
16 }
17
18 Obstacle::Obstacle()
19 {
20 }
21
22 double RRTS::elapsed()
23 {
24         std::chrono::duration<double> dt;
25         dt = std::chrono::duration_cast<std::chrono::duration<double>>(
26                 std::chrono::high_resolution_clock::now()
27                 - this->tstart_
28         );
29         this->scnt_ = dt.count();
30         return this->scnt_;
31 }
32
33 bool RRTS::should_stop()
34 {
35         // the following counters must be updated, do not comment
36         this->icnt_++;
37         this->elapsed();
38         // decide the stop conditions (maybe comment some lines)
39         if (this->icnt_ > 999) return true;
40         if (this->scnt_ > 10) return true;
41         if (this->gf()) return true;
42         // but continue by default
43         return false;
44 }
45
46 void RRTS::store_node(RRTNode n)
47 {
48         this->nodes().push_back(n);
49 }
50
51 // RRT procedures
52 std::tuple<bool, unsigned int, unsigned int>
53 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
54 {
55         for (auto &o: this->obstacles())
56                 if (std::get<0>(::collide(poly, o.poly())))
57                         return ::collide(poly, o.poly());
58         return std::make_tuple(false, 0, 0);
59 }
60
61 std::tuple<bool, unsigned int, unsigned int>
62 RRTS::collide_steered_from(RRTNode &f)
63 {
64         std::vector<std::tuple<double, double>> s;
65         s.push_back(std::make_tuple(f.x(), f.y()));
66         for (auto &n: this->steered()) {
67                 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
68                 s.push_back(std::make_tuple(n.lrx(), n.lry()));
69                 s.push_back(std::make_tuple(n.rrx(), n.rry()));
70                 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
71         }
72         auto col = this->collide(s);
73         auto strip_from = this->steered().size() - std::get<1>(col) / 4;
74         if (std::get<0>(col) && strip_from > 0) {
75                 while (strip_from-- > 0) {
76                         this->steered().pop_back();
77                 }
78                 return this->collide_steered_from(f);
79         }
80         return col;
81 }
82
83 std::tuple<bool, unsigned int, unsigned int>
84 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
85 {
86         std::vector<std::tuple<double, double>> p;
87         p.push_back(std::make_tuple(f.lfx(), f.lfy()));
88         p.push_back(std::make_tuple(f.lrx(), f.lry()));
89         p.push_back(std::make_tuple(f.rrx(), f.rry()));
90         p.push_back(std::make_tuple(f.rfx(), f.rfy()));
91         p.push_back(std::make_tuple(t.lfx(), t.lfy()));
92         p.push_back(std::make_tuple(t.lrx(), t.lry()));
93         p.push_back(std::make_tuple(t.rrx(), t.rry()));
94         p.push_back(std::make_tuple(t.rfx(), t.rfy()));
95         return this->collide(p);
96 }
97
98 double RRTS::cost_build(RRTNode &f, RRTNode &t)
99 {
100         double cost = 0;
101         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
102         return cost;
103 }
104
105 double RRTS::cost_search(RRTNode &f, RRTNode &t)
106 {
107         double cost = 0;
108         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
109         return cost;
110 }
111
112 void RRTS::sample()
113 {
114         double x = this->ndx_(this->gen_);
115         double y = this->ndy_(this->gen_);
116         double h = this->ndh_(this->gen_);
117         this->samples().push_back(RRTNode());
118         this->samples().back().x(x);
119         this->samples().back().y(y);
120         this->samples().back().h(h);
121 }
122
123 RRTNode *RRTS::nn(RRTNode &t)
124 {
125         RRTNode *nn = &this->nodes().front();
126         double cost = this->cost_search(*nn, t);
127         for (auto &f: this->nodes()) {
128                 if (this->cost_search(f, t) < cost) {
129                         nn = &f;
130                         cost = this->cost_search(f, t);
131                 }
132         }
133         return nn;
134 }
135
136 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
137 {
138         std::vector<RRTNode *> nv;
139         double cost = std::min(GAMMA(this->nodes().size()), ETA);
140         for (auto &f: this->nodes())
141                 if (this->cost_search(f, t) < cost)
142                         nv.push_back(&f);
143         return nv;
144 }
145
146 int cb_rs_steer(double q[4], void *user_data)
147 {
148         std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
149         RRTNode *ln = nullptr;
150         if (nodes->size() > 0)
151                 ln = &nodes->back();
152         nodes->push_back(RRTNode());
153         nodes->back().x(q[0]);
154         nodes->back().y(q[1]);
155         nodes->back().h(q[2]);
156         nodes->back().sp(q[3]);
157         if (nodes->back().sp() == 0)
158                 nodes->back().set_t(RRTNodeType::cusp);
159         else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
160                 ln->set_t(RRTNodeType::cusp);
161         return 0;
162 }
163
164 void RRTS::steer(RRTNode &f, RRTNode &t)
165 {
166         this->steered().clear();
167         double q0[] = {f.x(), f.y(), f.h()};
168         double q1[] = {t.x(), t.y(), t.h()};
169         ReedsSheppStateSpace rsss(f.mtr());
170         rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
171 }
172
173 void RRTS::join_steered(RRTNode *f)
174 {
175         while (this->steered().size() > 0) {
176                 this->store_node(this->steered().front());
177                 RRTNode *t = &this->nodes().back();
178                 t->p(f);
179                 t->c(this->cost_build(*f, *t));
180                 this->steered().erase(this->steered().begin());
181                 f = t;
182         }
183 }
184
185 bool RRTS::goal_found(RRTNode &f)
186 {
187         bool found = false;
188         for (auto &g: this->goals()) {
189                 double cost = this->cost_build(f, g);
190                 double edist = sqrt(
191                         pow(f.x() - g.x(), 2)
192                         + pow(f.y() - g.y(), 2)
193                 );
194                 double adist = std::abs(f.h() - g.h());
195                 if (edist < 0.05 && adist < M_PI / 32) {
196                         found = true;
197                         if (g.p() == nullptr || cc(f) + cost < cc(g)) {
198                                 g.p(&f);
199                                 g.c(cost);
200                         }
201                 }
202         }
203         return found;
204 }
205
206 // RRT* procedures
207 bool RRTS::connect()
208 {
209         RRTNode *t = &this->steered().front();
210         RRTNode *f = this->nn(this->samples().back());
211         double cost = this->cost_search(*f, *t);
212         for (auto n: this->nv(*t)) {
213                 if (
214                         !std::get<0>(this->collide_two_nodes(*n, *t))
215                         && this->cost_search(*n, *t) < cost
216                 ) {
217                         f = n;
218                         cost = this->cost_search(*n, *t);
219                 }
220         }
221         this->store_node(this->steered().front());
222         t = &this->nodes().back();
223         t->p(f);
224         t->c(this->cost_build(*f, *t));
225         t->set_t(RRTNodeType::connected);
226         return true;
227 }
228
229 void RRTS::rewire()
230 {
231         RRTNode *f = &this->nodes().back();
232         for (auto n: this->nv(*f)) {
233                 if (
234                         !std::get<0>(this->collide_two_nodes(*f, *n))
235                         && cc(*f) + this->cost_search(*f, *n) < cc(*n)
236                 ) {
237                         n->p(f);
238                         n->c(this->cost_build(*f, *n));
239                 }
240         }
241 }
242
243 // API
244 void RRTS::init()
245 {
246 }
247
248 void RRTS::deinit()
249 {
250 }
251
252 std::vector<RRTNode *> RRTS::path()
253 {
254         std::vector<RRTNode *> path;
255         if (this->goals().size() == 0)
256                 return path;
257         RRTNode *goal = &this->goals().front();
258         for (auto &n: this->goals()) {
259                 if (
260                         n.p() != nullptr
261                         && (n.c() < goal->c() || goal->p() == nullptr)
262                 ) {
263                         goal = &n;
264                 }
265         }
266         if (goal->p() == nullptr)
267                 return path;
268         while (goal != nullptr) {
269                 path.push_back(goal);
270                 goal = goal->p();
271         }
272         std::reverse(path.begin(), path.end());
273         return path;
274 }
275
276 bool RRTS::next()
277 {
278         if (this->icnt_ == 0)
279                 this->tstart_ = std::chrono::high_resolution_clock::now();
280         bool next = true;
281         if (this->should_stop())
282                 return false;
283         this->sample();
284         this->steer(
285                 *this->nn(this->samples().back()),
286                 this->samples().back()
287         );
288         if (std::get<0>(this->collide_steered_from(
289                 *this->nn(this->samples().back())
290         )))
291                 return next;
292         if (!this->connect())
293                 return next;
294         this->rewire();
295         unsigned scnt = this->steered().size();
296         this->steered().erase(this->steered().begin());
297         this->join_steered(&this->nodes().back());
298         RRTNode *just_added = &this->nodes().back();
299         while (scnt > 0) {
300                 scnt--;
301                 for (auto &g: this->goals()) {
302                         this->steer(*just_added, g);
303                         if (std::get<0>(this->collide_steered_from(
304                                 *just_added
305                         )))
306                                 continue;
307                         this->join_steered(just_added);
308                 }
309                 this->gf(this->goal_found(this->nodes().back()));
310                 just_added = just_added->p();
311         }
312         return next;
313 }
314
315 void RRTS::set_sample(
316         double mx, double dx,
317         double my, double dy,
318         double mh, double dh
319 )
320 {
321         this->ndx_ = std::normal_distribution<double>(mx, dx);
322         this->ndy_ = std::normal_distribution<double>(my, dy);
323         this->ndh_ = std::normal_distribution<double>(mh, dh);
324 }
325
326 RRTS::RRTS()
327         : gen_(std::random_device{}())
328 {
329         this->goals().reserve(100);
330         this->nodes().reserve(4000000);
331         this->samples().reserve(1000);
332         this->steered().reserve(20000);
333         this->store_node(RRTNode()); // root
334 }
335
336 double cc(RRTNode &t)
337 {
338         RRTNode *n = &t;
339         double cost = 0;
340         while (n != nullptr) {
341                 cost += n->c();
342                 n = n->p();
343         }
344         return cost;
345 }