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