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