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