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