]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/rrtbase.cc
fcec42d5059c87b393dc37906e702995251bdd08
[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 #if USE_GL > 0
321         glClear(GL_COLOR_BUFFER_BIT);
322         glLineWidth(1);
323         glPointSize(1);
324         // Plot obstacles
325         glBegin(GL_LINES);
326         for (auto o: *this->sobstacles_) {
327                 glColor3f(0, 0, 0);
328                 glVertex2f(GLVERTEX(o.init()));
329                 glVertex2f(GLVERTEX(o.goal()));
330         }
331         glEnd();
332         // Plot root, goal
333         glPointSize(8);
334         glBegin(GL_POINTS);
335         glColor3f(1, 0, 0);
336         glVertex2f(GLVERTEX(this->root_));
337         glVertex2f(GLVERTEX(this->goal_));
338         glEnd();
339         // Plot last sample
340         if (this->samples_.size() > 0) {
341                 glPointSize(8);
342                 glBegin(GL_POINTS);
343                 glColor3f(0, 1, 0);
344                 glVertex2f(GLVERTEX(this->samples_.back()));
345                 glEnd();
346         }
347         // Plot nodes
348         std::vector<RRTNode *> s; // DFS stack
349         std::vector<RRTNode *> r; // reset visited_
350         RRTNode *tmp;
351         glBegin(GL_LINES);
352         s.push_back(this->root_);
353         while (s.size() > 0) {
354                 tmp = s.back();
355                 s.pop_back();
356                 if (!tmp->visit()) {
357                         r.push_back(tmp);
358                         for (auto ch: tmp->children()) {
359                                 s.push_back(ch);
360                                 glColor3f(0.5, 0.5, 0.5);
361                                 glVertex2f(GLVERTEX(tmp));
362                                 glVertex2f(GLVERTEX(ch));
363                         }
364                 }
365         }
366         glEnd();
367         // Plot nodes (from goal)
368         glBegin(GL_LINES);
369         s.push_back(this->goal_);
370         while (s.size() > 0) {
371                 tmp = s.back();
372                 s.pop_back();
373                 if (!tmp->visit()) {
374                         r.push_back(tmp);
375                         for (auto ch: tmp->children()) {
376                                 s.push_back(ch);
377                                 glColor3f(0.5, 0.5, 0.5);
378                                 glVertex2f(GLVERTEX(tmp));
379                                 glVertex2f(GLVERTEX(ch));
380                         }
381                 }
382         }
383         glEnd();
384         std::vector<RRTNode *> cusps;
385         // Plot last trajectory
386         if (this->tlog().size() > 0) {
387                 glLineWidth(2);
388                 glBegin(GL_LINES);
389                 for (auto n: this->tlog().back()) {
390                         if (n->parent()) {
391                                 glColor3f(0, 0, 1);
392                                 glVertex2f(GLVERTEX(n));
393                                 glVertex2f(GLVERTEX(n->parent()));
394                                 if (sgn(n->s()) != sgn(n->parent()->s()))
395                                         cusps.push_back(n);
396                         }
397                 }
398                 glEnd();
399         }
400         // Plot cusps
401         glPointSize(8);
402         glBegin(GL_POINTS);
403         for (auto n: cusps) {
404                 glColor3f(0, 0, 1);
405                 glVertex2f(GLVERTEX(n));
406         }
407         glEnd();
408         SDL_GL_SwapWindow(gw);
409         for (auto n: r)
410                 n->visit(false);
411 #endif
412         return true;
413 }
414
415 bool RRTBase::goal_found(
416                 RRTNode *node,
417                 float (*cost)(RRTNode *, RRTNode* ))
418 {
419         if (IS_NEAR(node, this->goal_)) {
420                 if (this->goal_found_) {
421                         if (node->ccost() + this->cost(node, this->goal_) <
422                                         this->goal_->ccost()) {
423                                 RRTNode *op; // old parent
424                                 float oc; // old cumulative cost
425                                 float od; // old direct cost
426                                 op = this->goal_->parent();
427                                 oc = this->goal_->ccost();
428                                 od = this->goal_->dcost();
429                                 node->add_child(this->goal_,
430                                                 this->cost(node, this->goal_));
431                                 if (this->collide(node, this->goal_)) {
432                                         node->children().pop_back();
433                                         this->goal_->parent(op);
434                                         this->goal_->ccost(oc);
435                                         this->goal_->dcost(od);
436                                 } else {
437                                         op->rem_child(this->goal_);
438                                         return true;
439                                 }
440                         } else {
441                                 return false;
442                         }
443                 } else {
444                         node->add_child(
445                                         this->goal_,
446                                         this->cost(node, this->goal_));
447                         if (this->collide(node, this->goal_)) {
448                                 node->children().pop_back();
449                                 this->goal_->remove_parent();
450                                 return false;
451                         }
452                         this->goal_found_ = true;
453                         return true;
454                 }
455         }
456         return false;
457 }
458
459 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
460 {
461         std::vector<RRTEdge *> edges;
462         RRTNode *tmp = goal;
463         volatile bool col = false;
464         unsigned int i;
465         while (tmp != init) {
466                 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
467                 std::vector<RRTEdge *> bcframe = bc.frame();
468                 #pragma omp parallel for reduction(|: col)
469                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
470                         if ((*this->cobstacles_)[i].collide(tmp)) {
471                                 col = true;
472                         }
473                         for (auto &e: bcframe) {
474                                 if ((*this->cobstacles_)[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                 #pragma omp parallel for reduction(|: col)
491                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
492                         for (auto &e: bcframe) {
493                                 if ((*this->sobstacles_)[i].collide(e)) {
494                                         col = true;
495                                 }
496                         }
497                 }
498                 if (col) {
499                         for (auto e: bcframe) {
500                                 delete e->init();
501                                 delete e->goal();
502                                 delete e;
503                         }
504                         for (auto e: edges) {
505                                 delete e;
506                         }
507                         return true;
508                 }
509                 if (!tmp->parent()) {
510                         break;
511                 }
512                 edges.push_back(new RRTEdge(tmp, tmp->parent()));
513                 tmp = tmp->parent();
514                 for (auto e: bcframe) {
515                         delete e->init();
516                         delete e->goal();
517                         delete e;
518                 }
519         }
520         for (auto &e: edges) {
521                 #pragma omp parallel for reduction(|: col)
522                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
523                         if ((*this->cobstacles_)[i].collide(e)) {
524                                 col = true;
525                         }
526                 }
527                 if (col) {
528                         for (auto e: edges) {
529                                 delete e;
530                         }
531                         return true;
532                 }
533                 #pragma omp parallel for reduction(|: col)
534                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
535                         if ((*this->sobstacles_)[i].collide(e)) {
536                                 col = true;
537                         }
538                 }
539                 if (col) {
540                         for (auto e: edges) {
541                                 delete e;
542                         }
543                         return true;
544                 }
545         }
546         for (auto e: edges) {
547                 delete e;
548         }
549         return false;
550 }
551
552 class RRTNodeDijkstra {
553         public:
554                 RRTNodeDijkstra(int i):
555                         ni(i),
556                         pi(0),
557                         c(9999),
558                         v(false)
559                 {};
560                 RRTNodeDijkstra(int i, float c):
561                         ni(i),
562                         pi(0),
563                         c(c),
564                         v(false)
565                 {};
566                 RRTNodeDijkstra(int i, int p, float c):
567                         ni(i),
568                         pi(p),
569                         c(c),
570                         v(false)
571                 {};
572                 unsigned int ni;
573                 unsigned int pi;
574                 float c;
575                 bool v;
576                 bool vi()
577                 {
578                         if (this->v)
579                                 return true;
580                         this->v = true;
581                         return false;
582                 };
583 };
584
585 class RRTNodeDijkstraComparator {
586         public:
587                 int operator() (
588                                 const RRTNodeDijkstra& n1,
589                                 const RRTNodeDijkstra& n2)
590                 {
591                         return n1.c > n2.c;
592                 }
593 };
594
595 bool RRTBase::optp_dijkstra(
596                 std::vector<RRTNode *> &cusps,
597                 std::vector<int> &npi)
598 {
599         std::vector<RRTNodeDijkstra> dnodes;
600         for (unsigned int i = 0; i < cusps.size(); i++)
601                 if (i > 0)
602                         dnodes.push_back(RRTNodeDijkstra(
603                                                 i,
604                                                 i - 1,
605                                                 cusps[i]->ccost()));
606                 else
607                         dnodes.push_back(RRTNodeDijkstra(
608                                                 i,
609                                                 cusps[i]->ccost()));
610         dnodes[0].vi();
611         std::priority_queue<
612                 RRTNodeDijkstra,
613                 std::vector<RRTNodeDijkstra>,
614                 RRTNodeDijkstraComparator> pq;
615         RRTNodeDijkstra tmp = dnodes[0];
616         pq.push(tmp);
617         float ch_cost = 9999;
618         std::vector<RRTNode *> steered;
619         while (!pq.empty()) {
620                 tmp = pq.top();
621                 pq.pop();
622                 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
623                         ch_cost = dnodes[tmp.ni].c +
624                                 this->cost(cusps[tmp.ni], cusps[i]);
625                         steered = this->steer(cusps[tmp.ni], cusps[i]);
626                         for (unsigned int j = 0; j < steered.size() - 1; j++)
627                                 steered[j]->add_child(steered[j + 1], 1);
628                         if (this->collide(
629                                         steered[0],
630                                         steered[steered.size() - 1])) {
631                                 for (auto n: steered)
632                                         delete n;
633                                 continue;
634                         }
635                         if (ch_cost < dnodes[i].c) {
636                                 dnodes[i].c = ch_cost;
637                                 dnodes[i].pi = tmp.ni;
638                                 if (!dnodes[i].vi())
639                                         pq.push(dnodes[i]);
640                         }
641                         for (auto n: steered)
642                                 delete n;
643                 }
644         }
645         unsigned int tmpi = 0;
646         for (auto n: dnodes) {
647                 if (n.v && n.ni > tmpi)
648                         tmpi = n.ni;
649         }
650         while (tmpi > 0) {
651                 npi.push_back(tmpi);
652                 tmpi = dnodes[tmpi].pi;
653         }
654         npi.push_back(tmpi);
655         std::reverse(npi.begin(), npi.end());
656         return true;
657 }
658
659 bool RRTBase::optp_rrp(
660                 std::vector<RRTNode *> &cusps,
661                 std::vector<int> &npi)
662 {
663         std::vector<RRTNode *> steered;
664         std::vector<int> candidates;
665         RRTNode *x_j = nullptr;
666         RRTNode *x_i = nullptr;
667         int j = cusps.size() - 1;
668         int i_min = 0;
669         float c_min = 0;
670         float cost = 0;
671         float dx = 0;
672         float dy = 0;
673         float ed = 0;
674         float th_i = 0;
675         float th_j = 0;
676         while (j > 0) {
677                 npi.push_back(j);
678                 steered.clear();
679                 candidates.clear();
680                 x_j = cusps[j];
681                 for (int i = 0; i < j; i++) {
682                         steered = this->steer(cusps[i], x_j);
683                         for (unsigned int k = 0; k < steered.size() - 1; k++)
684                                 steered[k]->add_child(steered[k + 1], 1);
685                         if (!this->collide(
686                                         steered[0],
687                                         steered[steered.size() - 1]))
688                                 candidates.push_back(i);
689                 }
690                 if (candidates.size() <= 0)
691                         return false;
692                 i_min = candidates[0];
693                 x_i = cusps[i_min];
694                 c_min = 9999;
695                 for (auto c: candidates) {
696                         x_i = cusps[c];
697                         dx = x_j->x() - x_i->x();
698                         dy = x_j->y() - x_i->y();
699                         ed = EDIST(x_i, x_j);
700                         th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
701                         th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
702                         cost = th_i + th_j;
703                         if (cost < c_min) {
704                                 i_min = c;
705                                 c_min = cost;
706                         }
707                 }
708                 j = i_min;
709
710         }
711         npi.push_back(j);
712         std::reverse(npi.begin(), npi.end());
713         return true;
714 }
715
716 bool RRTBase::optp_smart(
717                 std::vector<RRTNode *> &cusps,
718                 std::vector<int> &npi)
719 {
720         std::vector<RRTNode *> steered;
721         int li = cusps.size() - 1;
722         int ai = li - 1;
723         npi.push_back(li);
724         while (ai > 1) {
725                 steered = this->steer(cusps[ai - 1], cusps[li]);
726                 for (unsigned int j = 0; j < steered.size() - 1; j++)
727                         steered[j]->add_child(steered[j + 1], 1);
728                 if (this->collide(steered[0], steered[steered.size() - 1])) {
729                         npi.push_back(ai);
730                         li = ai;
731                 }
732                 ai--;
733                 for (auto n: steered)
734                         delete n;
735         }
736         npi.push_back(0);
737         std::reverse(npi.begin(), npi.end());
738         return true;
739 }
740
741 bool RRTBase::opt_path()
742 {
743         if (this->tlog().size() == 0)
744                 return false;
745         float oc = this->tlog().back().front()->ccost();
746         std::vector<RRTNode *> tmp_cusps;
747         for (auto n: this->tlog().back()) {
748                 if (sgn(n->s()) == 0) {
749                         tmp_cusps.push_back(n);
750                 } else if (n->parent() &&
751                                 sgn(n->s()) != sgn(n->parent()->s())) {
752                         tmp_cusps.push_back(n);
753                         tmp_cusps.push_back(n->parent());
754                 }
755                 //tmp_cusps.push_back(n);
756         }
757         if (tmp_cusps.size() < 2)
758                 return false;
759         std::vector<RRTNode *> cusps;
760         for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
761                 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
762                         cusps.push_back(tmp_cusps[i]);
763         }
764         std::reverse(cusps.begin(), cusps.end());
765         std::vector<int> npi; // new path indexes
766         if (!this->optp_dijkstra(cusps, npi))
767                 return false;
768         RRTNode *pn = cusps[npi[0]];
769         RRTNode *tmp = nullptr;
770         bool en_add = true;
771         for (unsigned int i = 0; i < npi.size() - 1; i++) {
772                 pn = cusps[npi[i]];
773                 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
774                         if (!en_add) {
775                                 delete ns;
776                         } else if (IS_NEAR(cusps[npi[i]], ns)) {
777                                 tmp = ns;
778                                 while (tmp && tmp != cusps[npi[i]]) {
779                                         pn = tmp->parent();
780                                         delete tmp;
781                                         tmp = pn;
782                                 }
783                                 pn = cusps[npi[i]];
784                         } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
785                                 delete ns;
786                                 cusps[npi[i + 1]]->parent()->rem_child(
787                                                 cusps[npi[i + 1]]);
788                                 pn->add_child(
789                                         cusps[npi[i + 1]],
790                                         this->cost(pn, cusps[npi[i + 1]]));
791                                 en_add = false;
792                         } else if (IS_NEAR(pn, ns)) {
793                                 delete ns;
794                         } else {
795                                 this->nodes().push_back(ns);
796                                 this->add_iy(ns);
797                                 this->add_ixy(ns);
798                                 pn->add_child(ns, this->cost(pn, ns));
799                                 pn = ns;
800                         }
801                 }
802         }
803         this->root()->update_ccost();
804         if (this->tlog().back().front()->ccost() < oc)
805                 return true;
806         return false;
807 }
808
809 bool RRTBase::rebase(RRTNode *nr)
810 {
811         if (!nr || this->goal_ == nr || this->root_ == nr)
812                 return false;
813         std::vector<RRTNode *> s; // DFS stack
814         RRTNode *tmp;
815         unsigned int i = 0;
816         unsigned int to_del = 0;
817         int iy = 0;
818         s.push_back(this->root_);
819         while (s.size() > 0) {
820                 tmp = s.back();
821                 s.pop_back();
822                 for (auto ch: tmp->children()) {
823                         if (ch != nr)
824                                 s.push_back(ch);
825                 }
826                 to_del = this->nodes_.size();
827                 #pragma omp parallel for reduction(min: to_del)
828                 for (i = 0; i < this->nodes_.size(); i++) {
829                         if (this->nodes_[i] == tmp)
830                                 to_del = i;
831                 }
832                 if (to_del < this->nodes_.size())
833                         this->nodes_.erase(this->nodes_.begin() + to_del);
834                 iy = IYI(tmp->y());
835                 to_del = this->iy_[iy].size();
836                 #pragma omp parallel  for reduction(min: to_del)
837                 for (i = 0; i < this->iy_[iy].size(); i++) {
838                         if (this->iy_[iy][i] == tmp)
839                                 to_del = i;
840                 }
841                 if (to_del < this->iy_[iy].size())
842                         this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
843                 this->dnodes().push_back(tmp);
844         }
845         this->root_ = nr;
846         this->root_->remove_parent();
847         return true;
848 }
849
850 std::vector<RRTNode *> RRTBase::findt()
851 {
852         return this->findt(this->goal_);
853 }
854
855 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
856 {
857         std::vector<RRTNode *> nodes;
858         if (!n || !n->parent())
859                 return nodes;
860         while (n) {
861                 nodes.push_back(n);
862                 n = n->parent();
863         }
864         return nodes;
865 }
866
867 // RRT Framework
868 RRTNode *RRTBase::sample()
869 {
870         float x = this->samplingInfo_.x;
871         float y = this->samplingInfo_.y;
872         std::normal_distribution<float> xdist(
873                 0,
874                 BCAR_WIDTH / 3
875         );
876         x += this->samplingInfo_.dx
877                 * (BCAR_WIDTH / 2 + std::abs(xdist(this->gen_)))
878                 * cos(this->samplingInfo_.sh + M_PI / 2);
879         y += this->samplingInfo_.dx
880                 * (BCAR_WIDTH / 2 + std::abs(xdist(this->gen_)))
881                 * sin(this->samplingInfo_.sh + M_PI / 2);
882         std::normal_distribution<float> ydist(
883                 0,
884                 BCAR_LENGTH / 3
885         );
886         x += ydist(this->gen_) * cos(this->samplingInfo_.sh);
887         y += ydist(this->gen_) * sin(this->samplingInfo_.sh);
888         std::normal_distribution<float> hdist(
889                 0,
890                 this->samplingInfo_.h / 3
891         );
892         float h = std::abs(hdist(this->gen_));
893         return new RRTNode(
894                 x,
895                 y,
896                 this->samplingInfo_.sh + this->samplingInfo_.dh * h
897         );
898 }
899
900 float RRTBase::cost(RRTNode *init, RRTNode *goal)
901 {
902         return co2(init, goal);
903 }
904
905 RRTNode *RRTBase::nn(RRTNode *rs)
906 {
907         return nn4(this->iy_, rs, nullptr);
908         //return nn3(this->iy_, rs, nullptr);
909 }
910
911 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
912 {
913         std::vector<RRTNode *> nvs;
914         unsigned int iy = IYI(node->y());
915         unsigned int iy_dist = floor(dist / IYSTEP) + 1;
916         unsigned int i = 0; // vector index
917         unsigned int j = 0; // array index
918         unsigned int jmin = 0; // minimal j index
919         unsigned int jmax = 0; // maximal j index
920         jmin = iy - iy_dist;
921         jmin = (jmin > 0) ? jmin : 0;
922         jmax = iy + iy_dist + 1;
923         jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
924         #pragma omp parallel for reduction(merge: nvs)
925         for (j = jmin; j < jmax; j++) {
926                 #pragma omp parallel for reduction(merge: nvs)
927                 for (i = 0; i < this->iy_[j].size(); i++) {
928                         if (this->cost(this->iy_[j][i], node) < dist) {
929                                 nvs.push_back(this->iy_[j][i]);
930                         }
931                 }
932         }
933         return nvs;
934 }
935
936 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
937 {
938         return st3(init, goal);
939 }
940
941 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
942 {
943         return st3(init, goal, step);
944 }