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