]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/rrtbase.cc
Merge branch 'feature/polygon-drivable'
[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 "sample.h"
30 #include "cost.h"
31 #include "steer.h"
32 #include "nn.h"
33 #include "nv.h"
34
35 extern SDL_Window* gw;
36 extern SDL_GLContext gc;
37
38 Cell::Cell()
39 {}
40
41 bool Cell::changed()
42 {
43         pthread_mutex_lock(&this->m_);
44         bool ret = this->changed_;
45         pthread_mutex_unlock(&this->m_);
46         return ret;
47 }
48
49 std::vector<RRTNode *> Cell::nodes()
50 {
51         pthread_mutex_lock(&this->m_);
52         std::vector<RRTNode *> ret(this->nodes_);
53         pthread_mutex_unlock(&this->m_);
54         return ret;
55 }
56
57 void Cell::add_node(RRTNode *n)
58 {
59         pthread_mutex_lock(&this->m_);
60         this->nodes_.push_back(n);
61         this->changed_ = true;
62         pthread_mutex_unlock(&this->m_);
63 }
64
65 RRTBase::~RRTBase()
66 {
67 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
68 // please uncommend the following code:
69 //
70 //        for (auto n: this->nodes_)
71 //                if (n != this->root_)
72 //                        delete n;
73 //        for (auto n: this->dnodes_)
74 //                if (n != this->root_ && n != this->goal_)
75 //                        delete n;
76 //        for (auto s: this->samples_)
77 //                if (s != this->goal_)
78 //                        delete s;
79 //        for (auto edges: this->rlog_)
80 //                for (auto e: edges)
81 //                        delete e;
82 //        delete this->root_;
83 //        delete this->goal_;
84 }
85
86 RRTBase::RRTBase():
87         root_(new RRTNode()),
88         goal_(new RRTNode())
89 {
90         this->nodes_.reserve(NOFNODES);
91         this->nodes_.push_back(this->root_);
92         this->add_iy(this->root_);
93         this->add_ixy(this->root_);
94 }
95
96 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
97         root_(init),
98         goal_(goal)
99 {
100         this->nodes_.reserve(NOFNODES);
101         this->nodes_.push_back(init);
102         this->add_iy(init);
103         this->add_ixy(init);
104 }
105
106 // getter
107 RRTNode *RRTBase::root()
108 {
109         return this->root_;
110 }
111
112 RRTNode *RRTBase::goal()
113 {
114         return this->goal_;
115 }
116
117 std::vector<RRTNode *> &RRTBase::nodes()
118 {
119         return this->nodes_;
120 }
121
122 std::vector<RRTNode *> &RRTBase::dnodes()
123 {
124         return this->dnodes_;
125 }
126
127 PolygonObstacle &RRTBase::frame()
128 {
129         return this->frame_;
130 }
131
132 std::vector<RRTNode *> &RRTBase::samples()
133 {
134         return this->samples_;
135 }
136
137 std::vector<CircleObstacle> *RRTBase::co()
138 {
139         return this->cobstacles_;
140 }
141
142 std::vector<SegmentObstacle> *RRTBase::so()
143 {
144         return this->sobstacles_;
145 }
146
147 std::vector<float> &RRTBase::clog()
148 {
149         return this->clog_;
150 }
151
152 std::vector<float> &RRTBase::nlog()
153 {
154         return this->nlog_;
155 }
156
157 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
158 {
159         return this->rlog_;
160 }
161
162 std::vector<float> &RRTBase::slog()
163 {
164         return this->slog_;
165 }
166
167 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
168 {
169         return this->tlog_;
170 }
171
172 bool RRTBase::goal_found()
173 {
174         return this->goal_found_;
175 }
176
177 float RRTBase::elapsed()
178 {
179         std::chrono::duration<float> dt;
180         dt = std::chrono::duration_cast<std::chrono::duration<float>>(
181                         this->tend_ - this->tstart_);
182         return dt.count();
183 }
184
185 // setter
186 void RRTBase::root(RRTNode *node)
187 {
188         this->root_ = node;
189 }
190
191 void RRTBase::goal(RRTNode *node)
192 {
193         this->goal_ = node;
194 }
195
196 bool RRTBase::logr(RRTNode *root)
197 {
198         std::vector<RRTEdge *> e; // Edges to log
199         std::vector<RRTNode *> s; // DFS stack
200         std::vector<RRTNode *> r; // reset visited_
201         RRTNode *tmp;
202         s.push_back(root);
203         while (s.size() > 0) {
204                 tmp = s.back();
205                 s.pop_back();
206                 if (!tmp->visit()) {
207                         r.push_back(tmp);
208                         for (auto ch: tmp->children()) {
209                                 s.push_back(ch);
210                                 e.push_back(new RRTEdge(tmp, ch));
211                         }
212                 }
213         }
214         for (auto n: r)
215                 n->visit(false);
216         this->rlog_.push_back(e);
217         return true;
218 }
219
220 float RRTBase::ocost(RRTNode *n)
221 {
222         float dist = 9999;
223         for (auto o: *this->cobstacles_)
224                 if (o.dist_to(n) < dist)
225                         dist = o.dist_to(n);
226         for (auto o: *this->sobstacles_)
227                 if (o.dist_to(n) < dist)
228                         dist = o.dist_to(n);
229         return n->ocost(dist);
230 }
231
232 bool RRTBase::tlog(std::vector<RRTNode *> t)
233 {
234         if (t.size() > 0) {
235                 this->slog_.push_back(this->elapsed());
236                 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
237                 this->nlog_.push_back(this->nodes_.size());
238                 this->tlog_.push_back(t);
239                 return true;
240         } else {
241                 return false;
242         }
243 }
244
245 void RRTBase::tstart()
246 {
247         this->tstart_ = std::chrono::high_resolution_clock::now();
248 }
249
250 void RRTBase::tend()
251 {
252         this->tend_ = std::chrono::high_resolution_clock::now();
253 }
254
255 bool RRTBase::link_obstacles(
256                 std::vector<CircleObstacle> *cobstacles,
257                 std::vector<SegmentObstacle> *sobstacles)
258 {
259         this->cobstacles_ = cobstacles;
260         this->sobstacles_ = sobstacles;
261         if (!this->cobstacles_ || !this->sobstacles_) {
262                 return false;
263         }
264         return true;
265 }
266
267 bool RRTBase::add_iy(RRTNode *n)
268 {
269         int i = IYI(n->y());
270         if (i < 0)
271                 i = 0;
272         if (i >= IYSIZE)
273                 i = IYSIZE - 1;
274         this->iy_[i].push_back(n);
275         return true;
276 }
277
278 bool RRTBase::add_ixy(RRTNode *n)
279 {
280         int ix = IXI(n->x());
281         if (ix < 0)
282                 ix = 0;
283         if (ix >= IXSIZE)
284                 ix = IXSIZE - 1;
285         int iy = IYI(n->y());
286         if (iy < 0)
287                 iy = 0;
288         if (iy >= IYSIZE)
289                 iy = IYSIZE - 1;
290         this->ixy_[ix][iy].add_node(n);
291         return true;
292 }
293
294 bool RRTBase::goal_found(bool f)
295 {
296         this->goal_found_ = f;
297         return f;
298 }
299
300 // other
301 bool RRTBase::glplot()
302 {
303         glClear(GL_COLOR_BUFFER_BIT);
304         glLineWidth(1);
305         glPointSize(1);
306         // Plot obstacles
307         glBegin(GL_LINES);
308         for (auto o: *this->sobstacles_) {
309                 glColor3f(0, 0, 0);
310                 glVertex2f(GLVERTEX(o.init()));
311                 glVertex2f(GLVERTEX(o.goal()));
312         }
313         glEnd();
314         // Plot root, goal
315         glPointSize(8);
316         glBegin(GL_POINTS);
317         glColor3f(1, 0, 0);
318         glVertex2f(GLVERTEX(this->root_));
319         glVertex2f(GLVERTEX(this->goal_));
320         glEnd();
321         // Plot last sample
322         if (this->samples_.size() > 0) {
323                 glPointSize(8);
324                 glBegin(GL_POINTS);
325                 glColor3f(0, 1, 0);
326                 glVertex2f(GLVERTEX(this->samples_.back()));
327                 glEnd();
328         }
329         // Plot nodes
330         std::vector<RRTNode *> s; // DFS stack
331         std::vector<RRTNode *> r; // reset visited_
332         RRTNode *tmp;
333         glBegin(GL_LINES);
334         s.push_back(this->root_);
335         while (s.size() > 0) {
336                 tmp = s.back();
337                 s.pop_back();
338                 if (!tmp->visit()) {
339                         r.push_back(tmp);
340                         for (auto ch: tmp->children()) {
341                                 s.push_back(ch);
342                                 glColor3f(0.5, 0.5, 0.5);
343                                 glVertex2f(GLVERTEX(tmp));
344                                 glVertex2f(GLVERTEX(ch));
345                         }
346                 }
347         }
348         glEnd();
349         // Plot nodes (from goal)
350         glBegin(GL_LINES);
351         s.push_back(this->goal_);
352         while (s.size() > 0) {
353                 tmp = s.back();
354                 s.pop_back();
355                 if (!tmp->visit()) {
356                         r.push_back(tmp);
357                         for (auto ch: tmp->children()) {
358                                 s.push_back(ch);
359                                 glColor3f(0.5, 0.5, 0.5);
360                                 glVertex2f(GLVERTEX(tmp));
361                                 glVertex2f(GLVERTEX(ch));
362                         }
363                 }
364         }
365         glEnd();
366         std::vector<RRTNode *> cusps;
367         // Plot last trajectory
368         if (this->tlog().size() > 0) {
369                 glLineWidth(2);
370                 glBegin(GL_LINES);
371                 for (auto n: this->tlog().back()) {
372                         if (n->parent()) {
373                                 glColor3f(0, 0, 1);
374                                 glVertex2f(GLVERTEX(n));
375                                 glVertex2f(GLVERTEX(n->parent()));
376                                 if (sgn(n->s()) != sgn(n->parent()->s()))
377                                         cusps.push_back(n);
378                         }
379                 }
380                 glEnd();
381         }
382         // Plot cusps
383         glPointSize(8);
384         glBegin(GL_POINTS);
385         for (auto n: cusps) {
386                 glColor3f(0, 0, 1);
387                 glVertex2f(GLVERTEX(n));
388         }
389         glEnd();
390         SDL_GL_SwapWindow(gw);
391         for (auto n: r)
392                 n->visit(false);
393         return true;
394 }
395
396 bool RRTBase::goal_found(
397                 RRTNode *node,
398                 float (*cost)(RRTNode *, RRTNode* ))
399 {
400         if (IS_NEAR(node, this->goal_)) {
401                 if (this->goal_found_) {
402                         if (node->ccost() + this->cost(node, this->goal_) <
403                                         this->goal_->ccost()) {
404                                 RRTNode *op; // old parent
405                                 float oc; // old cumulative cost
406                                 float od; // old direct cost
407                                 op = this->goal_->parent();
408                                 oc = this->goal_->ccost();
409                                 od = this->goal_->dcost();
410                                 node->add_child(this->goal_,
411                                                 this->cost(node, this->goal_));
412                                 if (this->collide(node, this->goal_)) {
413                                         node->children().pop_back();
414                                         this->goal_->parent(op);
415                                         this->goal_->ccost(oc);
416                                         this->goal_->dcost(od);
417                                 } else {
418                                         op->rem_child(this->goal_);
419                                         return true;
420                                 }
421                         } else {
422                                 return false;
423                         }
424                 } else {
425                         node->add_child(
426                                         this->goal_,
427                                         this->cost(node, this->goal_));
428                         if (this->collide(node, this->goal_)) {
429                                 node->children().pop_back();
430                                 this->goal_->remove_parent();
431                                 return false;
432                         }
433                         this->goal_found_ = true;
434                         return true;
435                 }
436         }
437         return false;
438 }
439
440 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
441 {
442         std::vector<RRTEdge *> edges;
443         RRTNode *tmp = goal;
444         volatile bool col = false;
445         unsigned int i;
446         while (tmp != init) {
447                 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
448                 std::vector<RRTEdge *> bcframe = bc.frame();
449                 #pragma omp parallel for reduction(|: col)
450                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
451                         if ((*this->cobstacles_)[i].collide(tmp)) {
452                                 col = true;
453                         }
454                         for (auto &e: bcframe) {
455                                 if ((*this->cobstacles_)[i].collide(e)) {
456                                         col = true;
457                                 }
458                         }
459                 }
460                 if (col) {
461                         for (auto e: bcframe) {
462                                 delete e->init();
463                                 delete e->goal();
464                                 delete e;
465                         }
466                         for (auto e: edges) {
467                                 delete e;
468                         }
469                         return true;
470                 }
471                 #pragma omp parallel for reduction(|: col)
472                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
473                         for (auto &e: bcframe) {
474                                 if ((*this->sobstacles_)[i].collide(e)) {
475                                         col = true;
476                                 }
477                         }
478                 }
479                 if (col) {
480                         for (auto e: bcframe) {
481                                 delete e->init();
482                                 delete e->goal();
483                                 delete e;
484                         }
485                         for (auto e: edges) {
486                                 delete e;
487                         }
488                         return true;
489                 }
490                 if (!tmp->parent()) {
491                         break;
492                 }
493                 edges.push_back(new RRTEdge(tmp, tmp->parent()));
494                 tmp = tmp->parent();
495                 for (auto e: bcframe) {
496                         delete e->init();
497                         delete e->goal();
498                         delete e;
499                 }
500         }
501         for (auto &e: edges) {
502                 #pragma omp parallel for reduction(|: col)
503                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
504                         if ((*this->cobstacles_)[i].collide(e)) {
505                                 col = true;
506                         }
507                 }
508                 if (col) {
509                         for (auto e: edges) {
510                                 delete e;
511                         }
512                         return true;
513                 }
514                 #pragma omp parallel for reduction(|: col)
515                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
516                         if ((*this->sobstacles_)[i].collide(e)) {
517                                 col = true;
518                         }
519                 }
520                 if (col) {
521                         for (auto e: edges) {
522                                 delete e;
523                         }
524                         return true;
525                 }
526         }
527         for (auto e: edges) {
528                 delete e;
529         }
530         return false;
531 }
532
533 class RRTNodeDijkstra {
534         public:
535                 RRTNodeDijkstra(int i):
536                         ni(i),
537                         pi(0),
538                         c(9999),
539                         v(false)
540                 {};
541                 RRTNodeDijkstra(int i, float c):
542                         ni(i),
543                         pi(0),
544                         c(c),
545                         v(false)
546                 {};
547                 RRTNodeDijkstra(int i, int p, float c):
548                         ni(i),
549                         pi(p),
550                         c(c),
551                         v(false)
552                 {};
553                 unsigned int ni;
554                 unsigned int pi;
555                 float c;
556                 bool v;
557                 bool vi()
558                 {
559                         if (this->v)
560                                 return true;
561                         this->v = true;
562                         return false;
563                 };
564 };
565
566 class RRTNodeDijkstraComparator {
567         public:
568                 int operator() (
569                                 const RRTNodeDijkstra& n1,
570                                 const RRTNodeDijkstra& n2)
571                 {
572                         return n1.c > n2.c;
573                 }
574 };
575
576 bool RRTBase::optp_dijkstra(
577                 std::vector<RRTNode *> &cusps,
578                 std::vector<int> &npi)
579 {
580         std::vector<RRTNodeDijkstra> dnodes;
581         for (unsigned int i = 0; i < cusps.size(); i++)
582                 if (i > 0)
583                         dnodes.push_back(RRTNodeDijkstra(
584                                                 i,
585                                                 i - 1,
586                                                 cusps[i]->ccost()));
587                 else
588                         dnodes.push_back(RRTNodeDijkstra(
589                                                 i,
590                                                 cusps[i]->ccost()));
591         dnodes[0].vi();
592         std::priority_queue<
593                 RRTNodeDijkstra,
594                 std::vector<RRTNodeDijkstra>,
595                 RRTNodeDijkstraComparator> pq;
596         RRTNodeDijkstra tmp = dnodes[0];
597         pq.push(tmp);
598         float ch_cost = 9999;
599         std::vector<RRTNode *> steered;
600         while (!pq.empty()) {
601                 tmp = pq.top();
602                 pq.pop();
603                 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
604                         ch_cost = dnodes[tmp.ni].c +
605                                 this->cost(cusps[tmp.ni], cusps[i]);
606                         steered = this->steer(cusps[tmp.ni], cusps[i]);
607                         for (unsigned int j = 0; j < steered.size() - 1; j++)
608                                 steered[j]->add_child(steered[j + 1], 1);
609                         if (this->collide(
610                                         steered[0],
611                                         steered[steered.size() - 1])) {
612                                 for (auto n: steered)
613                                         delete n;
614                                 continue;
615                         }
616                         if (ch_cost < dnodes[i].c) {
617                                 dnodes[i].c = ch_cost;
618                                 dnodes[i].pi = tmp.ni;
619                                 if (!dnodes[i].vi())
620                                         pq.push(dnodes[i]);
621                         }
622                         for (auto n: steered)
623                                 delete n;
624                 }
625         }
626         unsigned int tmpi = 0;
627         for (auto n: dnodes) {
628                 if (n.v && n.ni > tmpi)
629                         tmpi = n.ni;
630         }
631         while (tmpi > 0) {
632                 npi.push_back(tmpi);
633                 tmpi = dnodes[tmpi].pi;
634         }
635         npi.push_back(tmpi);
636         std::reverse(npi.begin(), npi.end());
637         return true;
638 }
639
640 bool RRTBase::optp_rrp(
641                 std::vector<RRTNode *> &cusps,
642                 std::vector<int> &npi)
643 {
644         std::vector<RRTNode *> steered;
645         std::vector<int> candidates;
646         RRTNode *x_j = nullptr;
647         RRTNode *x_i = nullptr;
648         int j = cusps.size() - 1;
649         int i_min = 0;
650         float c_min = 0;
651         float cost = 0;
652         float dx = 0;
653         float dy = 0;
654         float ed = 0;
655         float th_i = 0;
656         float th_j = 0;
657         while (j > 0) {
658                 npi.push_back(j);
659                 steered.clear();
660                 candidates.clear();
661                 x_j = cusps[j];
662                 for (int i = 0; i < j; i++) {
663                         steered = this->steer(cusps[i], x_j);
664                         for (unsigned int k = 0; k < steered.size() - 1; k++)
665                                 steered[k]->add_child(steered[k + 1], 1);
666                         if (!this->collide(
667                                         steered[0],
668                                         steered[steered.size() - 1]))
669                                 candidates.push_back(i);
670                 }
671                 if (candidates.size() <= 0)
672                         return false;
673                 i_min = candidates[0];
674                 x_i = cusps[i_min];
675                 c_min = 9999;
676                 for (auto c: candidates) {
677                         x_i = cusps[c];
678                         dx = x_j->x() - x_i->x();
679                         dy = x_j->y() - x_i->y();
680                         ed = EDIST(x_i, x_j);
681                         th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
682                         th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
683                         cost = th_i + th_j;
684                         if (cost < c_min) {
685                                 i_min = c;
686                                 c_min = cost;
687                         }
688                 }
689                 j = i_min;
690
691         }
692         npi.push_back(j);
693         std::reverse(npi.begin(), npi.end());
694         return true;
695 }
696
697 bool RRTBase::optp_smart(
698                 std::vector<RRTNode *> &cusps,
699                 std::vector<int> &npi)
700 {
701         std::vector<RRTNode *> steered;
702         int li = cusps.size() - 1;
703         int ai = li - 1;
704         npi.push_back(li);
705         while (ai > 1) {
706                 steered = this->steer(cusps[ai - 1], cusps[li]);
707                 for (unsigned int j = 0; j < steered.size() - 1; j++)
708                         steered[j]->add_child(steered[j + 1], 1);
709                 if (this->collide(steered[0], steered[steered.size() - 1])) {
710                         npi.push_back(ai);
711                         li = ai;
712                 }
713                 ai--;
714                 for (auto n: steered)
715                         delete n;
716         }
717         npi.push_back(0);
718         std::reverse(npi.begin(), npi.end());
719         return true;
720 }
721
722 bool RRTBase::opt_path()
723 {
724         if (this->tlog().size() == 0)
725                 return false;
726         float oc = this->tlog().back().front()->ccost();
727         std::vector<RRTNode *> tmp_cusps;
728         for (auto n: this->tlog().back()) {
729                 if (sgn(n->s()) == 0) {
730                         tmp_cusps.push_back(n);
731                 } else if (n->parent() &&
732                                 sgn(n->s()) != sgn(n->parent()->s())) {
733                         tmp_cusps.push_back(n);
734                         tmp_cusps.push_back(n->parent());
735                 }
736                 //tmp_cusps.push_back(n);
737         }
738         if (tmp_cusps.size() < 2)
739                 return false;
740         std::vector<RRTNode *> cusps;
741         for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
742                 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
743                         cusps.push_back(tmp_cusps[i]);
744         }
745         std::reverse(cusps.begin(), cusps.end());
746         std::vector<int> npi; // new path indexes
747         if (!this->optp_dijkstra(cusps, npi))
748                 return false;
749         RRTNode *pn = cusps[npi[0]];
750         RRTNode *tmp = nullptr;
751         bool en_add = true;
752         for (unsigned int i = 0; i < npi.size() - 1; i++) {
753                 pn = cusps[npi[i]];
754                 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
755                         if (!en_add) {
756                                 delete ns;
757                         } else if (IS_NEAR(cusps[npi[i]], ns)) {
758                                 tmp = ns;
759                                 while (tmp && tmp != cusps[npi[i]]) {
760                                         pn = tmp->parent();
761                                         delete tmp;
762                                         tmp = pn;
763                                 }
764                                 pn = cusps[npi[i]];
765                         } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
766                                 delete ns;
767                                 cusps[npi[i + 1]]->parent()->rem_child(
768                                                 cusps[npi[i + 1]]);
769                                 pn->add_child(
770                                         cusps[npi[i + 1]],
771                                         this->cost(pn, cusps[npi[i + 1]]));
772                                 en_add = false;
773                         } else if (IS_NEAR(pn, ns)) {
774                                 delete ns;
775                         } else {
776                                 this->nodes().push_back(ns);
777                                 this->add_iy(ns);
778                                 this->add_ixy(ns);
779                                 pn->add_child(ns, this->cost(pn, ns));
780                                 pn = ns;
781                         }
782                 }
783         }
784         this->root()->update_ccost();
785         if (this->tlog().back().front()->ccost() < oc)
786                 return true;
787         return false;
788 }
789
790 bool RRTBase::rebase(RRTNode *nr)
791 {
792         if (!nr || this->goal_ == nr || this->root_ == nr)
793                 return false;
794         std::vector<RRTNode *> s; // DFS stack
795         RRTNode *tmp;
796         unsigned int i = 0;
797         unsigned int to_del = 0;
798         int iy = 0;
799         s.push_back(this->root_);
800         while (s.size() > 0) {
801                 tmp = s.back();
802                 s.pop_back();
803                 for (auto ch: tmp->children()) {
804                         if (ch != nr)
805                                 s.push_back(ch);
806                 }
807                 to_del = this->nodes_.size();
808                 #pragma omp parallel for reduction(min: to_del)
809                 for (i = 0; i < this->nodes_.size(); i++) {
810                         if (this->nodes_[i] == tmp)
811                                 to_del = i;
812                 }
813                 if (to_del < this->nodes_.size())
814                         this->nodes_.erase(this->nodes_.begin() + to_del);
815                 iy = IYI(tmp->y());
816                 to_del = this->iy_[iy].size();
817                 #pragma omp parallel  for reduction(min: to_del)
818                 for (i = 0; i < this->iy_[iy].size(); i++) {
819                         if (this->iy_[iy][i] == tmp)
820                                 to_del = i;
821                 }
822                 if (to_del < this->iy_[iy].size())
823                         this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
824                 this->dnodes().push_back(tmp);
825         }
826         this->root_ = nr;
827         this->root_->remove_parent();
828         return true;
829 }
830
831 std::vector<RRTNode *> RRTBase::findt()
832 {
833         return this->findt(this->goal_);
834 }
835
836 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
837 {
838         std::vector<RRTNode *> nodes;
839         if (!n || !n->parent())
840                 return nodes;
841         while (n) {
842                 nodes.push_back(n);
843                 n = n->parent();
844         }
845         return nodes;
846 }
847
848 // RRT Framework
849 RRTNode *RRTBase::sample()
850 {
851         return sa1();
852 }
853
854 float RRTBase::cost(RRTNode *init, RRTNode *goal)
855 {
856         return co2(init, goal);
857 }
858
859 RRTNode *RRTBase::nn(RRTNode *rs)
860 {
861         return nn4(this->iy_, rs, nullptr);
862         //return nn3(this->iy_, rs, nullptr);
863 }
864
865 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
866 {
867         std::vector<RRTNode *> nvs;
868         unsigned int iy = IYI(node->y());
869         unsigned int iy_dist = floor(dist / IYSTEP) + 1;
870         unsigned int i = 0; // vector index
871         unsigned int j = 0; // array index
872         unsigned int jmin = 0; // minimal j index
873         unsigned int jmax = 0; // maximal j index
874         jmin = iy - iy_dist;
875         jmin = (jmin > 0) ? jmin : 0;
876         jmax = iy + iy_dist + 1;
877         jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
878         #pragma omp parallel for reduction(merge: nvs)
879         for (j = jmin; j < jmax; j++) {
880                 #pragma omp parallel for reduction(merge: nvs)
881                 for (i = 0; i < this->iy_[j].size(); i++) {
882                         if (this->cost(this->iy_[j][i], node) < dist) {
883                                 nvs.push_back(this->iy_[j][i]);
884                         }
885                 }
886         }
887         return nvs;
888 }
889
890 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
891 {
892         return st3(init, goal);
893 }
894
895 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
896 {
897         return st3(init, goal, step);
898 }