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