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