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