]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/rrtbase.cc
Add `Cell` class
[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::goal_found(bool f)
272 {
273         this->goal_found_ = f;
274         return f;
275 }
276
277 // other
278 bool RRTBase::glplot()
279 {
280         glClear(GL_COLOR_BUFFER_BIT);
281         glLineWidth(1);
282         glPointSize(1);
283         // Plot obstacles
284         glBegin(GL_LINES);
285         for (auto o: *this->sobstacles_) {
286                 glColor3f(0, 0, 0);
287                 glVertex2f(GLVERTEX(o.init()));
288                 glVertex2f(GLVERTEX(o.goal()));
289         }
290         glEnd();
291         // Plot root, goal
292         glPointSize(8);
293         glBegin(GL_POINTS);
294         glColor3f(1, 0, 0);
295         glVertex2f(GLVERTEX(this->root_));
296         glVertex2f(GLVERTEX(this->goal_));
297         glEnd();
298         // Plot last sample
299         if (this->samples_.size() > 0) {
300                 glPointSize(8);
301                 glBegin(GL_POINTS);
302                 glColor3f(0, 1, 0);
303                 glVertex2f(GLVERTEX(this->samples_.back()));
304                 glEnd();
305         }
306         // Plot nodes
307         std::vector<RRTNode *> s; // DFS stack
308         std::vector<RRTNode *> r; // reset visited_
309         RRTNode *tmp;
310         glBegin(GL_LINES);
311         s.push_back(this->root_);
312         while (s.size() > 0) {
313                 tmp = s.back();
314                 s.pop_back();
315                 if (!tmp->visit()) {
316                         r.push_back(tmp);
317                         for (auto ch: tmp->children()) {
318                                 s.push_back(ch);
319                                 glColor3f(0.5, 0.5, 0.5);
320                                 glVertex2f(GLVERTEX(tmp));
321                                 glVertex2f(GLVERTEX(ch));
322                         }
323                 }
324         }
325         glEnd();
326         // Plot nodes (from goal)
327         glBegin(GL_LINES);
328         s.push_back(this->goal_);
329         while (s.size() > 0) {
330                 tmp = s.back();
331                 s.pop_back();
332                 if (!tmp->visit()) {
333                         r.push_back(tmp);
334                         for (auto ch: tmp->children()) {
335                                 s.push_back(ch);
336                                 glColor3f(0.5, 0.5, 0.5);
337                                 glVertex2f(GLVERTEX(tmp));
338                                 glVertex2f(GLVERTEX(ch));
339                         }
340                 }
341         }
342         glEnd();
343         std::vector<RRTNode *> cusps;
344         // Plot last trajectory
345         if (this->tlog().size() > 0) {
346                 glLineWidth(2);
347                 glBegin(GL_LINES);
348                 for (auto n: this->tlog().back()) {
349                         if (n->parent()) {
350                                 glColor3f(0, 0, 1);
351                                 glVertex2f(GLVERTEX(n));
352                                 glVertex2f(GLVERTEX(n->parent()));
353                                 if (sgn(n->s()) != sgn(n->parent()->s()))
354                                         cusps.push_back(n);
355                         }
356                 }
357                 glEnd();
358         }
359         // Plot cusps
360         glPointSize(8);
361         glBegin(GL_POINTS);
362         for (auto n: cusps) {
363                 glColor3f(0, 0, 1);
364                 glVertex2f(GLVERTEX(n));
365         }
366         glEnd();
367         SDL_GL_SwapWindow(gw);
368         for (auto n: r)
369                 n->visit(false);
370         return true;
371 }
372
373 bool RRTBase::goal_found(
374                 RRTNode *node,
375                 float (*cost)(RRTNode *, RRTNode* ))
376 {
377         if (IS_NEAR(node, this->goal_)) {
378                 if (this->goal_found_) {
379                         if (node->ccost() + this->cost(node, this->goal_) <
380                                         this->goal_->ccost()) {
381                                 RRTNode *op; // old parent
382                                 float oc; // old cumulative cost
383                                 float od; // old direct cost
384                                 op = this->goal_->parent();
385                                 oc = this->goal_->ccost();
386                                 od = this->goal_->dcost();
387                                 node->add_child(this->goal_,
388                                                 this->cost(node, this->goal_));
389                                 if (this->collide(node, this->goal_)) {
390                                         node->children().pop_back();
391                                         this->goal_->parent(op);
392                                         this->goal_->ccost(oc);
393                                         this->goal_->dcost(od);
394                                 } else {
395                                         op->rem_child(this->goal_);
396                                         return true;
397                                 }
398                         } else {
399                                 return false;
400                         }
401                 } else {
402                         node->add_child(
403                                         this->goal_,
404                                         this->cost(node, this->goal_));
405                         if (this->collide(node, this->goal_)) {
406                                 node->children().pop_back();
407                                 this->goal_->remove_parent();
408                                 return false;
409                         }
410                         this->goal_found_ = true;
411                         return true;
412                 }
413         }
414         return false;
415 }
416
417 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
418 {
419         std::vector<RRTEdge *> edges;
420         RRTNode *tmp = goal;
421         volatile bool col = false;
422         unsigned int i;
423         while (tmp != init) {
424                 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
425                 std::vector<RRTEdge *> bcframe = bc.frame();
426                 #pragma omp parallel for reduction(|: col)
427                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
428                         if ((*this->cobstacles_)[i].collide(tmp)) {
429                                 col = true;
430                         }
431                         for (auto &e: bcframe) {
432                                 if ((*this->cobstacles_)[i].collide(e)) {
433                                         col = true;
434                                 }
435                         }
436                 }
437                 if (col) {
438                         for (auto e: bcframe) {
439                                 delete e->init();
440                                 delete e->goal();
441                                 delete e;
442                         }
443                         for (auto e: edges) {
444                                 delete e;
445                         }
446                         return true;
447                 }
448                 #pragma omp parallel for reduction(|: col)
449                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
450                         for (auto &e: bcframe) {
451                                 if ((*this->sobstacles_)[i].collide(e)) {
452                                         col = true;
453                                 }
454                         }
455                 }
456                 if (col) {
457                         for (auto e: bcframe) {
458                                 delete e->init();
459                                 delete e->goal();
460                                 delete e;
461                         }
462                         for (auto e: edges) {
463                                 delete e;
464                         }
465                         return true;
466                 }
467                 if (!tmp->parent()) {
468                         break;
469                 }
470                 edges.push_back(new RRTEdge(tmp, tmp->parent()));
471                 tmp = tmp->parent();
472                 for (auto e: bcframe) {
473                         delete e->init();
474                         delete e->goal();
475                         delete e;
476                 }
477         }
478         for (auto &e: edges) {
479                 #pragma omp parallel for reduction(|: col)
480                 for (i = 0; i < (*this->cobstacles_).size(); i++) {
481                         if ((*this->cobstacles_)[i].collide(e)) {
482                                 col = true;
483                         }
484                 }
485                 if (col) {
486                         for (auto e: edges) {
487                                 delete e;
488                         }
489                         return true;
490                 }
491                 #pragma omp parallel for reduction(|: col)
492                 for (i = 0; i < (*this->sobstacles_).size(); i++) {
493                         if ((*this->sobstacles_)[i].collide(e)) {
494                                 col = true;
495                         }
496                 }
497                 if (col) {
498                         for (auto e: edges) {
499                                 delete e;
500                         }
501                         return true;
502                 }
503         }
504         for (auto e: edges) {
505                 delete e;
506         }
507         return false;
508 }
509
510 class RRTNodeDijkstra {
511         public:
512                 RRTNodeDijkstra(int i):
513                         ni(i),
514                         pi(0),
515                         c(9999),
516                         v(false)
517                 {};
518                 RRTNodeDijkstra(int i, float c):
519                         ni(i),
520                         pi(0),
521                         c(c),
522                         v(false)
523                 {};
524                 RRTNodeDijkstra(int i, int p, float c):
525                         ni(i),
526                         pi(p),
527                         c(c),
528                         v(false)
529                 {};
530                 unsigned int ni;
531                 unsigned int pi;
532                 float c;
533                 bool v;
534                 bool vi()
535                 {
536                         if (this->v)
537                                 return true;
538                         this->v = true;
539                         return false;
540                 };
541 };
542
543 class RRTNodeDijkstraComparator {
544         public:
545                 int operator() (
546                                 const RRTNodeDijkstra& n1,
547                                 const RRTNodeDijkstra& n2)
548                 {
549                         return n1.c > n2.c;
550                 }
551 };
552
553 bool RRTBase::optp_dijkstra(
554                 std::vector<RRTNode *> &cusps,
555                 std::vector<int> &npi)
556 {
557         std::vector<RRTNodeDijkstra> dnodes;
558         for (unsigned int i = 0; i < cusps.size(); i++)
559                 if (i > 0)
560                         dnodes.push_back(RRTNodeDijkstra(
561                                                 i,
562                                                 i - 1,
563                                                 cusps[i]->ccost()));
564                 else
565                         dnodes.push_back(RRTNodeDijkstra(
566                                                 i,
567                                                 cusps[i]->ccost()));
568         dnodes[0].vi();
569         std::priority_queue<
570                 RRTNodeDijkstra,
571                 std::vector<RRTNodeDijkstra>,
572                 RRTNodeDijkstraComparator> pq;
573         RRTNodeDijkstra tmp = dnodes[0];
574         pq.push(tmp);
575         float ch_cost = 9999;
576         std::vector<RRTNode *> steered;
577         while (!pq.empty()) {
578                 tmp = pq.top();
579                 pq.pop();
580                 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
581                         ch_cost = dnodes[tmp.ni].c +
582                                 this->cost(cusps[tmp.ni], cusps[i]);
583                         steered = this->steer(cusps[tmp.ni], cusps[i]);
584                         for (unsigned int j = 0; j < steered.size() - 1; j++)
585                                 steered[j]->add_child(steered[j + 1], 1);
586                         if (this->collide(
587                                         steered[0],
588                                         steered[steered.size() - 1])) {
589                                 for (auto n: steered)
590                                         delete n;
591                                 continue;
592                         }
593                         if (ch_cost < dnodes[i].c) {
594                                 dnodes[i].c = ch_cost;
595                                 dnodes[i].pi = tmp.ni;
596                                 if (!dnodes[i].vi())
597                                         pq.push(dnodes[i]);
598                         }
599                         for (auto n: steered)
600                                 delete n;
601                 }
602         }
603         unsigned int tmpi = 0;
604         for (auto n: dnodes) {
605                 if (n.v && n.ni > tmpi)
606                         tmpi = n.ni;
607         }
608         while (tmpi > 0) {
609                 npi.push_back(tmpi);
610                 tmpi = dnodes[tmpi].pi;
611         }
612         npi.push_back(tmpi);
613         std::reverse(npi.begin(), npi.end());
614         return true;
615 }
616
617 bool RRTBase::optp_rrp(
618                 std::vector<RRTNode *> &cusps,
619                 std::vector<int> &npi)
620 {
621         std::vector<RRTNode *> steered;
622         std::vector<int> candidates;
623         RRTNode *x_j = nullptr;
624         RRTNode *x_i = nullptr;
625         int j = cusps.size() - 1;
626         int i_min = 0;
627         float c_min = 0;
628         float cost = 0;
629         float dx = 0;
630         float dy = 0;
631         float ed = 0;
632         float th_i = 0;
633         float th_j = 0;
634         while (j > 0) {
635                 npi.push_back(j);
636                 steered.clear();
637                 candidates.clear();
638                 x_j = cusps[j];
639                 for (int i = 0; i < j; i++) {
640                         steered = this->steer(cusps[i], x_j);
641                         for (unsigned int k = 0; k < steered.size() - 1; k++)
642                                 steered[k]->add_child(steered[k + 1], 1);
643                         if (!this->collide(
644                                         steered[0],
645                                         steered[steered.size() - 1]))
646                                 candidates.push_back(i);
647                 }
648                 if (candidates.size() <= 0)
649                         return false;
650                 i_min = candidates[0];
651                 x_i = cusps[i_min];
652                 c_min = 9999;
653                 for (auto c: candidates) {
654                         x_i = cusps[c];
655                         dx = x_j->x() - x_i->x();
656                         dy = x_j->y() - x_i->y();
657                         ed = EDIST(x_i, x_j);
658                         th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
659                         th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
660                         cost = th_i + th_j;
661                         if (cost < c_min) {
662                                 i_min = c;
663                                 c_min = cost;
664                         }
665                 }
666                 j = i_min;
667
668         }
669         npi.push_back(j);
670         std::reverse(npi.begin(), npi.end());
671         return true;
672 }
673
674 bool RRTBase::optp_smart(
675                 std::vector<RRTNode *> &cusps,
676                 std::vector<int> &npi)
677 {
678         std::vector<RRTNode *> steered;
679         int li = cusps.size() - 1;
680         int ai = li - 1;
681         npi.push_back(li);
682         while (ai > 1) {
683                 steered = this->steer(cusps[ai - 1], cusps[li]);
684                 for (unsigned int j = 0; j < steered.size() - 1; j++)
685                         steered[j]->add_child(steered[j + 1], 1);
686                 if (this->collide(steered[0], steered[steered.size() - 1])) {
687                         npi.push_back(ai);
688                         li = ai;
689                 }
690                 ai--;
691                 for (auto n: steered)
692                         delete n;
693         }
694         npi.push_back(0);
695         std::reverse(npi.begin(), npi.end());
696         return true;
697 }
698
699 bool RRTBase::opt_path()
700 {
701         if (this->tlog().size() == 0)
702                 return false;
703         float oc = this->tlog().back().front()->ccost();
704         std::vector<RRTNode *> tmp_cusps;
705         for (auto n: this->tlog().back()) {
706                 if (sgn(n->s()) == 0) {
707                         tmp_cusps.push_back(n);
708                 } else if (n->parent() &&
709                                 sgn(n->s()) != sgn(n->parent()->s())) {
710                         tmp_cusps.push_back(n);
711                         tmp_cusps.push_back(n->parent());
712                 }
713                 //tmp_cusps.push_back(n);
714         }
715         if (tmp_cusps.size() < 2)
716                 return false;
717         std::vector<RRTNode *> cusps;
718         for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
719                 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
720                         cusps.push_back(tmp_cusps[i]);
721         }
722         std::reverse(cusps.begin(), cusps.end());
723         std::vector<int> npi; // new path indexes
724         if (!this->optp_dijkstra(cusps, npi))
725                 return false;
726         RRTNode *pn = cusps[npi[0]];
727         RRTNode *tmp = nullptr;
728         bool en_add = true;
729         for (unsigned int i = 0; i < npi.size() - 1; i++) {
730                 pn = cusps[npi[i]];
731                 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
732                         if (!en_add) {
733                                 delete ns;
734                         } else if (IS_NEAR(cusps[npi[i]], ns)) {
735                                 tmp = ns;
736                                 while (tmp && tmp != cusps[npi[i]]) {
737                                         pn = tmp->parent();
738                                         delete tmp;
739                                         tmp = pn;
740                                 }
741                                 pn = cusps[npi[i]];
742                         } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
743                                 delete ns;
744                                 cusps[npi[i + 1]]->parent()->rem_child(
745                                                 cusps[npi[i + 1]]);
746                                 pn->add_child(
747                                         cusps[npi[i + 1]],
748                                         this->cost(pn, cusps[npi[i + 1]]));
749                                 en_add = false;
750                         } else if (IS_NEAR(pn, ns)) {
751                                 delete ns;
752                         } else {
753                                 this->nodes().push_back(ns);
754                                 this->add_iy(ns);
755                                 pn->add_child(ns, this->cost(pn, ns));
756                                 pn = ns;
757                         }
758                 }
759         }
760         this->root()->update_ccost();
761         if (this->tlog().back().front()->ccost() < oc)
762                 return true;
763         return false;
764 }
765
766 bool RRTBase::rebase(RRTNode *nr)
767 {
768         if (!nr || this->goal_ == nr || this->root_ == nr)
769                 return false;
770         std::vector<RRTNode *> s; // DFS stack
771         RRTNode *tmp;
772         unsigned int i = 0;
773         unsigned int to_del = 0;
774         int iy = 0;
775         s.push_back(this->root_);
776         while (s.size() > 0) {
777                 tmp = s.back();
778                 s.pop_back();
779                 for (auto ch: tmp->children()) {
780                         if (ch != nr)
781                                 s.push_back(ch);
782                 }
783                 to_del = this->nodes_.size();
784                 #pragma omp parallel for reduction(min: to_del)
785                 for (i = 0; i < this->nodes_.size(); i++) {
786                         if (this->nodes_[i] == tmp)
787                                 to_del = i;
788                 }
789                 if (to_del < this->nodes_.size())
790                         this->nodes_.erase(this->nodes_.begin() + to_del);
791                 iy = IYI(tmp->y());
792                 to_del = this->iy_[iy].size();
793                 #pragma omp parallel  for reduction(min: to_del)
794                 for (i = 0; i < this->iy_[iy].size(); i++) {
795                         if (this->iy_[iy][i] == tmp)
796                                 to_del = i;
797                 }
798                 if (to_del < this->iy_[iy].size())
799                         this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
800                 this->dnodes().push_back(tmp);
801         }
802         this->root_ = nr;
803         this->root_->remove_parent();
804         return true;
805 }
806
807 std::vector<RRTNode *> RRTBase::findt()
808 {
809         return this->findt(this->goal_);
810 }
811
812 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
813 {
814         std::vector<RRTNode *> nodes;
815         if (!n || !n->parent())
816                 return nodes;
817         while (n) {
818                 nodes.push_back(n);
819                 n = n->parent();
820         }
821         return nodes;
822 }
823
824 // RRT Framework
825 RRTNode *RRTBase::sample()
826 {
827         return sa1();
828 }
829
830 float RRTBase::cost(RRTNode *init, RRTNode *goal)
831 {
832         return co2(init, goal);
833 }
834
835 RRTNode *RRTBase::nn(RRTNode *rs)
836 {
837         return nn4(this->iy_, rs, nullptr);
838         //return nn3(this->iy_, rs, nullptr);
839 }
840
841 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
842 {
843         std::vector<RRTNode *> nvs;
844         unsigned int iy = IYI(node->y());
845         unsigned int iy_dist = floor(dist / IYSTEP) + 1;
846         unsigned int i = 0; // vector index
847         unsigned int j = 0; // array index
848         unsigned int jmin = 0; // minimal j index
849         unsigned int jmax = 0; // maximal j index
850         jmin = iy - iy_dist;
851         jmin = (jmin > 0) ? jmin : 0;
852         jmax = iy + iy_dist + 1;
853         jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
854         #pragma omp parallel for reduction(merge: nvs)
855         for (j = jmin; j < jmax; j++) {
856                 #pragma omp parallel for reduction(merge: nvs)
857                 for (i = 0; i < this->iy_[j].size(); i++) {
858                         if (this->cost(this->iy_[j][i], node) < dist) {
859                                 nvs.push_back(this->iy_[j][i]);
860                         }
861                 }
862         }
863         return nvs;
864 }
865
866 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
867 {
868         return st3(init, goal);
869 }
870
871 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
872 {
873         return st3(init, goal, step);
874 }