]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/rrtbase.cc
Add goal found setter
[hubacji1/iamcar.git] / base / rrtbase.cc
1 /*
2 This file is part of I am car.
3
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <algorithm>
19 #include <cmath>
20 #include <omp.h>
21 #include <queue>
22 #include "bcar.h"
23 #include "rrtbase.h"
24 // OpenGL
25 #include <GL/gl.h>
26 #include <GL/glu.h>
27 #include <SDL2/SDL.h>
28 // RRT
29 #include "cost.h"
30 #include "steer.h"
31
32 extern SDL_Window* gw;
33 extern SDL_GLContext gc;
34
35 RRTBase::~RRTBase()
36 {
37         for (auto n: this->nodes_)
38                 if (n != this->root_)
39                         delete n;
40         for (auto n: this->dnodes_)
41                 if (n != this->root_ && n != this->goal_)
42                         delete n;
43         for (auto s: this->samples_)
44                 if (s != this->goal_)
45                         delete s;
46         for (auto edges: this->rlog_)
47                 for (auto e: edges)
48                         delete e;
49         delete this->root_;
50         delete this->goal_;
51 }
52
53 RRTBase::RRTBase():
54         root_(new RRTNode()),
55         goal_(new RRTNode())
56 {
57         this->nodes_.push_back(this->root_);
58         this->add_iy(this->root_);
59 }
60
61 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
62         root_(init),
63         goal_(goal)
64 {
65         this->nodes_.push_back(init);
66         this->add_iy(init);
67 }
68
69 RRTNode *RRTBase::root()
70 {
71         return this->root_;
72 }
73
74 RRTNode *RRTBase::goal()
75 {
76         return this->goal_;
77 }
78
79 std::vector<RRTNode *> &RRTBase::nodes()
80 {
81         return this->nodes_;
82 }
83
84 std::vector<RRTNode *> &RRTBase::dnodes()
85 {
86         return this->dnodes_;
87 }
88
89 std::vector<RRTNode *> &RRTBase::samples()
90 {
91         return this->samples_;
92 }
93
94 std::vector<CircleObstacle> *RRTBase::cos()
95 {
96         return this->cobstacles_;
97 }
98
99 std::vector<SegmentObstacle> *RRTBase::sos()
100 {
101         return this->sobstacles_;
102 }
103
104 std::vector<float> &RRTBase::clog()
105 {
106         return this->clog_;
107 }
108
109 std::vector<float> &RRTBase::nlog()
110 {
111         return this->nlog_;
112 }
113
114 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
115 {
116         return this->rlog_;
117 }
118
119 std::vector<float> &RRTBase::slog()
120 {
121         return this->slog_;
122 }
123
124 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
125 {
126         return this->tlog_;
127 }
128
129 bool RRTBase::goal_found()
130 {
131         return this->goal_found_;
132 }
133
134 float RRTBase::elapsed()
135 {
136         std::chrono::duration<float> dt;
137         dt = std::chrono::duration_cast<std::chrono::duration<float>>(
138                         this->tend_ - this->tstart_);
139         return dt.count();
140 }
141
142 void RRTBase::root(RRTNode *node)
143 {
144         this->root_ = node;
145 }
146
147 void RRTBase::goal(RRTNode *node)
148 {
149         this->goal_ = node;
150 }
151
152 bool RRTBase::logr(RRTNode *root)
153 {
154         std::vector<RRTEdge *> e; // Edges to log
155         std::vector<RRTNode *> s; // DFS stack
156         std::vector<RRTNode *> r; // reset visited_
157         RRTNode *tmp;
158         s.push_back(root);
159         while (s.size() > 0) {
160                 tmp = s.back();
161                 s.pop_back();
162                 if (!tmp->visit()) {
163                         r.push_back(tmp);
164                         for (auto ch: tmp->children()) {
165                                 s.push_back(ch);
166                                 e.push_back(new RRTEdge(tmp, ch));
167                         }
168                 }
169         }
170         for (auto n: r)
171                 n->visit(false);
172         this->rlog_.push_back(e);
173         return true;
174 }
175
176 float RRTBase::ocost(RRTNode *n)
177 {
178         float dist = 9999;
179         for (auto o: *this->cobstacles_)
180                 if (o.dist_to(n) < dist)
181                         dist = o.dist_to(n);
182         for (auto o: *this->sobstacles_)
183                 if (o.dist_to(n) < dist)
184                         dist = o.dist_to(n);
185         return n->ocost(dist);
186 }
187
188 bool RRTBase::tlog(std::vector<RRTNode *> t)
189 {
190         if (t.size() > 0) {
191                 this->slog_.push_back(this->elapsed());
192                 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
193                 this->nlog_.push_back(this->nodes_.size());
194                 this->tlog_.push_back(t);
195                 return true;
196         } else {
197                 return false;
198         }
199 }
200
201 void RRTBase::tstart()
202 {
203         this->tstart_ = std::chrono::high_resolution_clock::now();
204 }
205
206 void RRTBase::tend()
207 {
208         this->tend_ = std::chrono::high_resolution_clock::now();
209 }
210
211 bool RRTBase::link_obstacles(
212                 std::vector<CircleObstacle> *cobstacles,
213                 std::vector<SegmentObstacle> *sobstacles)
214 {
215         this->cobstacles_ = cobstacles;
216         this->sobstacles_ = sobstacles;
217         if (!this->cobstacles_ || !this->sobstacles_) {
218                 return false;
219         }
220         return true;
221 }
222
223 bool RRTBase::add_iy(RRTNode *n)
224 {
225         int i = IYI(n->y());
226         if (i < 0)
227                 i = 0;
228         if (i >= IYSIZE)
229                 i = IYSIZE - 1;
230         this->iy_[i].push_back(n);
231         return true;
232 }
233
234 bool RRTBase::goal_found(bool f)
235 {
236         this->goal_found_ = f;
237         return f;
238 }
239
240 bool RRTBase::glplot()
241 {
242         glClear(GL_COLOR_BUFFER_BIT);
243         glLineWidth(1);
244         glPointSize(1);
245         // Plot obstacles
246         glBegin(GL_LINES);
247         for (auto o: *this->sobstacles_) {
248                 glColor3f(0, 0, 0);
249                 glVertex2f(GLVERTEX(o.init()));
250                 glVertex2f(GLVERTEX(o.goal()));
251         }
252         glEnd();
253         // Plot root, goal
254         glPointSize(8);
255         glBegin(GL_POINTS);
256         glColor3f(1, 0, 0);
257         glVertex2f(GLVERTEX(this->root_));
258         glVertex2f(GLVERTEX(this->goal_));
259         glEnd();
260         // Plot last sample
261         if (this->samples_.size() > 0) {
262                 glPointSize(8);
263                 glBegin(GL_POINTS);
264                 glColor3f(0, 1, 0);
265                 glVertex2f(GLVERTEX(this->samples_.back()));
266                 glEnd();
267         }
268         // Plot nodes
269         std::vector<RRTNode *> s; // DFS stack
270         std::vector<RRTNode *> r; // reset visited_
271         RRTNode *tmp;
272         glBegin(GL_LINES);
273         s.push_back(this->root_);
274         while (s.size() > 0) {
275                 tmp = s.back();
276                 s.pop_back();
277                 if (!tmp->visit()) {
278                         r.push_back(tmp);
279                         for (auto ch: tmp->children()) {
280                                 s.push_back(ch);
281                                 glColor3f(0.5, 0.5, 0.5);
282                                 glVertex2f(GLVERTEX(tmp));
283                                 glVertex2f(GLVERTEX(ch));
284                         }
285                 }
286         }
287         glEnd();
288         std::vector<RRTNode *> cusps;
289         // Plot last trajectory
290         if (this->tlog().size() > 0) {
291                 glLineWidth(2);
292                 glBegin(GL_LINES);
293                 for (auto n: this->tlog().back()) {
294                         if (n->parent()) {
295                                 glColor3f(0, 0, 1);
296                                 glVertex2f(GLVERTEX(n));
297                                 glVertex2f(GLVERTEX(n->parent()));
298                                 if (sgn(n->s()) != sgn(n->parent()->s()))
299                                         cusps.push_back(n);
300                         }
301                 }
302                 glEnd();
303         }
304         // Plot cusps
305         glPointSize(8);
306         glBegin(GL_POINTS);
307         for (auto n: cusps) {
308                 glColor3f(0, 0, 1);
309                 glVertex2f(GLVERTEX(n));
310         }
311         glEnd();
312         SDL_GL_SwapWindow(gw);
313         for (auto n: r)
314                 n->visit(false);
315         return true;
316 }
317
318 bool RRTBase::goal_found(
319                 RRTNode *node,
320                 float (*cost)(RRTNode *, RRTNode* ))
321 {
322         float xx = pow(node->x() - this->goal_->x(), 2);
323         float yy = pow(node->y() - this->goal_->y(), 2);
324         float dh = std::abs(node->h() - this->goal_->h());
325         if (IS_NEAR(node, this->goal_)) {
326                 if (this->goal_found_) {
327                         if (node->ccost() + (*cost)(node, this->goal_) <
328                                         this->goal_->ccost()) {
329                                 RRTNode *op; // old parent
330                                 float oc; // old cumulative cost
331                                 float od; // old direct cost
332                                 op = this->goal_->parent();
333                                 oc = this->goal_->ccost();
334                                 od = this->goal_->dcost();
335                                 node->add_child(this->goal_,
336                                                 (*cost)(node, this->goal_));
337                                 if (this->collide(node, this->goal_)) {
338                                         node->children().pop_back();
339                                         this->goal_->parent(op);
340                                         this->goal_->ccost(oc);
341                                         this->goal_->dcost(od);
342                                 } else {
343                                         op->rem_child(this->goal_);
344                                         return true;
345                                 }
346                         } else {
347                                 return false;
348                         }
349                 } else {
350                         node->add_child(
351                                         this->goal_,
352                                         (*cost)(node, this->goal_));
353                         if (this->collide(node, this->goal_)) {
354                                 node->children().pop_back();
355                                 this->goal_->remove_parent();
356                                 return false;
357                         }
358                         this->goal_found_ = true;
359                         return true;
360                 }
361         }
362         return false;
363 }
364
365 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
366 {
367         std::vector<RRTEdge *> edges;
368         RRTNode *tmp = goal;
369         volatile bool col = false;
370         unsigned int i;
371         while (tmp != init) {
372                 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
373                 std::vector<RRTEdge *> bcframe = bc.frame();
374                 #pragma omp parallel for reduction(|: col)
375                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
376                         if ((*this->cobstacles_)[i].collide(tmp)) {
377                                 col = true;
378                         }
379                         for (auto &e: bcframe) {
380                                 if ((*this->cobstacles_)[i].collide(e)) {
381                                         col = true;
382                                 }
383                         }
384                 }
385                 if (col) {
386                         for (auto e: bcframe) {
387                                 delete e->init();
388                                 delete e->goal();
389                                 delete e;
390                         }
391                         for (auto e: edges) {
392                                 delete e;
393                         }
394                         return true;
395                 }
396                 #pragma omp parallel for reduction(|: col)
397                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
398                         for (auto &e: bcframe) {
399                                 if ((*this->sobstacles_)[i].collide(e)) {
400                                         col = true;
401                                 }
402                         }
403                 }
404                 if (col) {
405                         for (auto e: bcframe) {
406                                 delete e->init();
407                                 delete e->goal();
408                                 delete e;
409                         }
410                         for (auto e: edges) {
411                                 delete e;
412                         }
413                         return true;
414                 }
415                 if (!tmp->parent()) {
416                         break;
417                 }
418                 edges.push_back(new RRTEdge(tmp, tmp->parent()));
419                 tmp = tmp->parent();
420                 for (auto e: bcframe) {
421                         delete e->init();
422                         delete e->goal();
423                         delete e;
424                 }
425         }
426         for (auto &e: edges) {
427                 #pragma omp parallel for reduction(|: col)
428                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
429                         if ((*this->cobstacles_)[i].collide(e)) {
430                                 col = true;
431                         }
432                 }
433                 if (col) {
434                         for (auto e: edges) {
435                                 delete e;
436                         }
437                         return true;
438                 }
439                 #pragma omp parallel for reduction(|: col)
440                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
441                         if ((*this->sobstacles_)[i].collide(e)) {
442                                 col = true;
443                         }
444                 }
445                 if (col) {
446                         for (auto e: edges) {
447                                 delete e;
448                         }
449                         return true;
450                 }
451         }
452         for (auto e: edges) {
453                 delete e;
454         }
455         return false;
456 }
457
458 class RRTNodeDijkstra {
459         public:
460                 RRTNodeDijkstra(int i):
461                         ni(i),
462                         pi(0),
463                         c(9999),
464                         v(false)
465                 {};
466                 RRTNodeDijkstra(int i, float c):
467                         ni(i),
468                         pi(0),
469                         c(c),
470                         v(false)
471                 {};
472                 RRTNodeDijkstra(int i, int p, float c):
473                         ni(i),
474                         pi(p),
475                         c(c),
476                         v(false)
477                 {};
478                 unsigned int ni;
479                 unsigned int pi;
480                 float c;
481                 bool v;
482                 bool vi()
483                 {
484                         if (this->v)
485                                 return true;
486                         this->v = true;
487                         return false;
488                 };
489 };
490
491 class RRTNodeDijkstraComparator {
492         public:
493                 int operator() (
494                                 const RRTNodeDijkstra& n1,
495                                 const RRTNodeDijkstra& n2)
496                 {
497                         return n1.c > n2.c;
498                 }
499 };
500
501 bool RRTBase::opt_path()
502 {
503         if (this->tlog().size() == 0)
504                 return false;
505         float oc = this->tlog().back().front()->ccost();
506         std::vector<RRTNode *> tmp_cusps;
507         for (auto n: this->tlog().back()) {
508                 if (sgn(n->s()) == 0) {
509                         tmp_cusps.push_back(n);
510                 } else if (n->parent() &&
511                                 sgn(n->s()) != sgn(n->parent()->s())) {
512                         tmp_cusps.push_back(n);
513                         tmp_cusps.push_back(n->parent());
514                 }
515         }
516         if (tmp_cusps.size() < 2)
517                 return false;
518         std::vector<RRTNode *> cusps;
519         for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
520                 if (tmp_cusps[i] != tmp_cusps[i + 1])
521                         cusps.push_back(tmp_cusps[i]);
522         }
523         std::reverse(cusps.begin(), cusps.end());
524         // Begin of Dijkstra
525         std::vector<RRTNodeDijkstra> dnodes;
526         for (unsigned int i = 0; i < cusps.size(); i++)
527                 if (i > 0)
528                         dnodes.push_back(RRTNodeDijkstra(
529                                                 i,
530                                                 i - 1,
531                                                 cusps[i]->ccost()));
532                 else
533                         dnodes.push_back(RRTNodeDijkstra(
534                                                 i,
535                                                 cusps[i]->ccost()));
536         dnodes[0].vi();
537         std::priority_queue<
538                 RRTNodeDijkstra,
539                 std::vector<RRTNodeDijkstra>,
540                 RRTNodeDijkstraComparator> pq;
541         RRTNodeDijkstra tmp = dnodes[0];
542         pq.push(tmp);
543         float ch_cost = 9999;
544         std::vector<RRTNode *> steered;
545         while (!pq.empty() && tmp.ni != cusps.size() - 1) {
546                 tmp = pq.top();
547                 pq.pop();
548                 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
549                         ch_cost = dnodes[tmp.ni].c +
550                                 CO(cusps[tmp.ni], cusps[i]);
551                         steered = ST(cusps[tmp.ni], cusps[i]);
552                         for (unsigned int j = 0; j < steered.size() - 1; j++)
553                                 steered[j]->add_child(
554                                                 steered[j + 1],
555                                                 CO(
556                                                         steered[j],
557                                                         steered[j + 1]));
558                         if (i != tmp.ni + 1 && this->collide( // TODO
559                                                 steered[0],
560                                                 steered[steered.size() - 1]))
561                                 continue;
562                         if (ch_cost < dnodes[i].c) {
563                                 dnodes[i].c = ch_cost;
564                                 dnodes[i].pi = tmp.ni;
565                                 if (!dnodes[i].vi())
566                                         pq.push(dnodes[i]);
567                         }
568                 }
569         }
570         if (tmp.ni != cusps.size() - 1)
571                 return false;
572         std::vector<int> npi; // new path indexes
573         int tmpi = tmp.ni;
574         while (tmpi > 0) {
575                 npi.push_back(tmpi);
576                 tmpi = dnodes[tmpi].pi;
577         }
578         npi.push_back(tmpi);
579         std::reverse(npi.begin(), npi.end());
580         RRTNode *pn = cusps[npi[0]];
581         for (unsigned int i = 0; i < npi.size() - 1; i++) {
582                 for (auto ns: ST(cusps[npi[i]], cusps[npi[i + 1]])) {
583                         pn->add_child(ns, CO(pn, ns));
584                         pn = ns;
585                 }
586         }
587         pn->add_child(
588                         this->tlog().back().front(),
589                         CO(pn, this->tlog().back().front()));
590         // End of Dijkstra
591         if (this->tlog().back().front()->ccost() < oc)
592                 return true;
593         return false;
594 }
595
596 bool RRTBase::rebase(RRTNode *nr)
597 {
598         if (!nr || this->goal_ == nr || this->root_ == nr)
599                 return false;
600         std::vector<RRTNode *> s; // DFS stack
601         RRTNode *tmp;
602         unsigned int i = 0;
603         unsigned int to_del = 0;
604         int iy = 0;
605         s.push_back(this->root_);
606         while (s.size() > 0) {
607                 tmp = s.back();
608                 s.pop_back();
609                 for (auto ch: tmp->children()) {
610                         if (ch != nr)
611                                 s.push_back(ch);
612                 }
613                 to_del = this->nodes_.size();
614                 #pragma omp parallel for reduction(min: to_del)
615                 for (i = 0; i < this->nodes_.size(); i++) {
616                         if (this->nodes_[i] == tmp)
617                                 to_del = i;
618                 }
619                 if (to_del < this->nodes_.size())
620                         this->nodes_.erase(this->nodes_.begin() + to_del);
621 #if NNVERSION > 1
622                 iy = IYI(tmp->y());
623                 to_del = this->iy_[iy].size();
624                 #pragma omp parallel  for reduction(min: to_del)
625                 for (i = 0; i < this->iy_[iy].size(); i++) {
626                         if (this->iy_[iy][i] == tmp)
627                                 to_del = i;
628                 }
629                 if (to_del < this->iy_[iy].size())
630                         this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
631 #endif
632                 this->dnodes().push_back(tmp);
633         }
634         this->root_ = nr;
635         this->root_->remove_parent();
636         return true;
637 }
638
639 std::vector<RRTNode *> RRTBase::findt()
640 {
641         return this->findt(this->goal_);
642 }
643
644 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
645 {
646         std::vector<RRTNode *> nodes;
647         if (!n || !n->parent())
648                 return nodes;
649         RRTNode *tmp = n;
650         while (tmp) {
651                 nodes.push_back(tmp);
652                 tmp = tmp->parent();
653         }
654         return nodes;
655 }