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