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