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