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