]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/rrtplanner.cc
Add getter for final pose used in fipr
[hubacji1/iamcar.git] / decision_control / rrtplanner.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 <iostream>
19 #include <cstdlib>
20 #include <ctime>
21 #include "compile.h"
22 #include "nn.h"
23 #include "nv.h"
24 #include "sample.h"
25 #include "steer.h"
26 #include "rrtplanner.h"
27 #include "cost.h"
28
29 #define CATI(a, b) a ## b
30 #define CAT(a, b) CATI(a, b)
31 #define KUWATA2008_CCOST CAT(c, CO)
32 #define KUWATA2008_DCOST CO
33
34 LaValle1998::LaValle1998(RRTNode *init, RRTNode *goal):
35         RRTBase(init, goal)
36 {
37         srand(static_cast<unsigned>(time(0)));
38 }
39
40 bool LaValle1998::next()
41 {
42         RRTNode *rs;
43 #if GOALFIRST > 0
44         if (this->samples().size() == 0)
45                 rs = this->goal();
46         else
47                 rs = this->sample();
48 #else
49         rs = this->sample();
50 #endif
51         this->samples().push_back(rs);
52         RRTNode *nn = this->nn(rs);
53         RRTNode *pn = nn;
54         bool en_add = true;
55         for (auto ns: this->steer(nn, rs)) {
56                 if (!en_add) {
57                         delete ns;
58                 } else {
59                         this->nodes().push_back(ns);
60                         this->add_iy(ns);
61                         pn->add_child(ns, this->cost(pn, ns));
62                         if (this->collide(pn, ns)) {
63                                 pn->children().pop_back();
64                                 ns->remove_parent();
65                                 this->iy_[IYI(ns->y())].pop_back();
66                                 en_add = false;
67                         } else {
68                                 this->ocost(ns);
69                                 pn = ns;
70                                 if (this->goal_found(pn, CO)) {
71                                         this->tlog(this->findt());
72                                         en_add = false;
73                                 }
74                         }
75                 }
76         }
77         return this->goal_found();
78 }
79
80 Kuwata2008::Kuwata2008(RRTNode *init, RRTNode *goal):
81         RRTBase(init, goal)
82 {
83         srand(static_cast<unsigned>(time(0)));
84 }
85
86 bool Kuwata2008::next()
87 {
88         RRTNode *rs;
89         if (this->samples().size() == 0) {
90                 rs = this->goal();
91         } else {
92                 rs = this->sample();
93         }
94         this->samples().push_back(rs);
95         float heur = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
96         if (this->goal_found()) {
97                 if (heur < 0.7)
98                         {}//this->cost = &KUWATA2008_CCOST;
99                 else
100                         {}//this->cost = &KUWATA2008_DCOST;
101         } else {
102                 if (heur < 0.3)
103                         {}//this->cost = &KUWATA2008_CCOST;
104                 else
105                         {}//this->cost = &KUWATA2008_DCOST;
106         }
107         RRTNode *nn = this->nn(rs);
108         RRTNode *pn = nn;
109         std::vector<RRTNode *> newly_added;
110         bool en_add = true;
111         for (auto ns: this->steer(nn, rs)) {
112                 if (!en_add) {
113                         delete ns;
114                 } else {
115                         this->nodes().push_back(ns);
116                         this->add_iy(ns);
117                         pn->add_child(ns, KUWATA2008_DCOST(pn, ns));
118                         if (this->collide(pn, ns)) {
119                                 pn->children().pop_back();
120                                 ns->remove_parent();
121                                 this->iy_[IYI(ns->y())].pop_back();
122                                 en_add = false;
123                         } else {
124                                 this->ocost(ns);
125                                 pn = ns;
126                                 newly_added.push_back(pn);
127                                 if (this->goal_found(pn, &KUWATA2008_DCOST)) {
128                                         this->tlog(this->findt());
129                                         en_add = false;
130                                 }
131                         }
132                 }
133         }
134         if (this->samples().size() <= 1)
135                 return this->goal_found();
136         for (auto na: newly_added) {
137                 pn = na;
138                 en_add = true;
139                 for (auto ns: this->steer(na, this->goal())) {
140                         if (!en_add) {
141                                 delete ns;
142                         } else {
143                                 this->nodes().push_back(ns);
144                                 this->add_iy(ns);
145                                 pn->add_child(ns, KUWATA2008_DCOST(pn, ns));
146                                 if (this->collide(pn, ns)) {
147                                         pn->children().pop_back();
148                                         ns->remove_parent();
149                                         this->iy_[IYI(ns->y())].pop_back();
150                                         en_add = false;
151                                 } else {
152                                         this->ocost(ns);
153                                         pn = ns;
154                                         if (this->goal_found(pn,
155                                                         &KUWATA2008_DCOST)) {
156                                                 this->tlog(this->findt());
157                                                 en_add = false;
158                                         }
159                                 }
160                         }
161                 }
162         }
163         return this->goal_found();
164 }
165
166 Karaman2011::Karaman2011()
167 {
168         srand(static_cast<unsigned>(time(0)));
169 }
170
171 Karaman2011::Karaman2011(RRTNode *init, RRTNode *goal):
172         RRTBase(init, goal)
173 {
174         srand(static_cast<unsigned>(time(0)));
175 }
176
177 bool Karaman2011::next()
178 {
179         RRTNode *rs;
180 #if GOALFIRST > 0
181         if (this->samples().size() == 0)
182                 rs = this->goal();
183         else
184                 rs = this->sample();
185 #else
186         rs = this->sample();
187 #endif
188         this->samples().push_back(rs);
189         RRTNode *nn = this->nn(rs);
190         RRTNode *pn = nn;
191         std::vector<RRTNode *> nvs;
192         bool en_add = true;
193         bool skip = false;
194         for (auto ns: this->steer(nn, rs)) {
195                 if (!en_add) {
196                         delete ns;
197                 } else if (IS_NEAR(nn, ns)) {
198                         delete ns;
199                 } else {
200                         if (IS_NEAR(ns, rs))
201                                 en_add = false;
202                         nvs = this->nv(ns,
203                                         MIN(GAMMA_RRTSTAR(
204                                                         this->nodes().size()),
205                                                         0.2));
206                         skip = false;
207                         for (auto nv: nvs) {
208                                 if (IS_NEAR(nv, ns))
209                                         skip = true;
210                         }
211                         if (skip)
212                                 continue;
213                         this->nodes().push_back(ns);
214                         this->add_iy(ns);
215                         // connect
216                         if (!this->connect(pn, ns, nvs)) {
217                                 this->iy_[IYI(ns->y())].pop_back();
218                                 en_add = false;
219                         } else {
220                                 // rewire
221                                 this->rewire(nvs, ns);
222                                 pn = ns;
223                                 if (this->goal_found(pn, CO)) {
224                                         this->tlog(this->findt());
225                                         en_add = false;
226                                 }
227                         }
228                 }
229         }
230         return this->goal_found();
231 }
232
233 bool Karaman2011::connect(
234                 RRTNode *pn,
235                 RRTNode *ns,
236                 std::vector<RRTNode *> nvs)
237 {
238         RRTNode *op; // old parent
239         float od; // old direct cost
240         float oc; // old cumulative cost
241         bool connected = false;
242         pn->add_child(ns, this->cost(pn, ns));
243         if (this->collide(pn, ns)) {
244                 pn->children().pop_back();
245                 ns->remove_parent();
246         } else {
247                 this->ocost(ns);
248                 connected = true;
249         }
250         for (auto nv: nvs) {
251                 if (!connected || (nv->ccost() + this->cost(nv, ns) <
252                                 ns->ccost())) {
253                         op = ns->parent();
254                         od = ns->dcost();
255                         oc = ns->ccost();
256                         nv->add_child(ns, this->cost(nv, ns));
257                         if (this->collide(nv, ns)) {
258                                 nv->children().pop_back();
259                                 if (op)
260                                         ns->parent(op);
261                                 else
262                                         ns->remove_parent();
263                                 ns->dcost(od);
264                                 ns->ccost(oc);
265                         } else if (connected) {
266                                 op->children().pop_back();
267                         } else {
268                                 this->ocost(ns);
269                                 connected = true;
270                         }
271                 }
272         }
273         return connected;
274 }
275
276 bool Karaman2011::rewire(std::vector<RRTNode *> nvs, RRTNode *ns)
277 {
278         RRTNode *op; // old parent
279         float od; // old direct cost
280         float oc; // old cumulative cost
281         for (auto nv: nvs) {
282                 if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
283                         op = nv->parent();
284                         od = nv->dcost();
285                         oc = nv->ccost();
286                         ns->add_child(nv, this->cost(ns, nv));
287                         if (this->collide(ns, nv)) {
288                                 ns->children().pop_back();
289                                 nv->parent(op);
290                                 nv->dcost(od);
291                                 nv->ccost(oc);
292                         } else {
293                                 op->rem_child(nv);
294                         }
295                 }
296         }
297         return true;
298 }
299
300 T1::T1(RRTNode *init, RRTNode *goal):
301         RRTBase(init, goal)
302 {
303         srand(static_cast<unsigned>(time(0)));
304 }
305
306 bool T1::next()
307 {
308         RRTNode *rs;
309         if (this->samples().size() == 0)
310                 rs = this->goal();
311         else
312                 rs = this->sample();
313         this->samples().push_back(rs);
314         RRTNode *nn = this->nn(rs);
315         RRTNode *pn = nn;
316         std::vector<RRTNode *> nvs;
317         bool connected;
318         RRTNode *op; // old parent
319         float od; // old direct cost
320         float oc; // old cumulative cost
321         std::vector<RRTNode *> steered = this->steer(nn, rs);
322         // RRT* for first node
323         RRTNode *ns = steered[0];
324         {
325                 nvs = this->nv(ns, MIN(
326                                 GAMMA_RRTSTAR(this->nodes().size()),
327                                 0.2)); // TODO const
328                 this->nodes().push_back(ns);
329                 this->add_iy(ns);
330                 connected = false;
331                 pn->add_child(ns, this->cost(pn, ns));
332                 if (this->collide(pn, ns)) {
333                         pn->children().pop_back();
334                 } else {
335                         connected = true;
336                 }
337                 // connect
338                 for (auto nv: nvs) {
339                         if (!connected || (nv->ccost() + this->cost(nv, ns) <
340                                         ns->ccost())) {
341                                 op = ns->parent();
342                                 od = ns->dcost();
343                                 oc = ns->ccost();
344                                 nv->add_child(ns, this->cost(nv, ns));
345                                 if (this->collide(nv, ns)) {
346                                         nv->children().pop_back();
347                                         ns->parent(op);
348                                         ns->dcost(od);
349                                         ns->ccost(oc);
350                                 } else if (connected) {
351                                         op->children().pop_back();
352                                 } else {
353                                         connected = true;
354                                 }
355                         }
356                 }
357                 if (!connected)
358                         return false;
359                 // rewire
360                 for (auto nv: nvs) {
361                         if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
362                                 op = nv->parent();
363                                 od = nv->dcost();
364                                 oc = nv->ccost();
365                                 ns->add_child(nv, this->cost(ns, nv));
366                                 if (this->collide(ns, nv)) {
367                                         ns->children().pop_back();
368                                         nv->parent(op);
369                                         nv->dcost(od);
370                                         nv->ccost(oc);
371                                 } else {
372                                         op->rem_child(nv);
373                                 }
374                         }
375                 }
376                 pn = ns;
377                 if (this->goal_found(pn, CO)) {
378                         this->tlog(this->findt());
379                 }
380         }
381         unsigned int i = 0;
382         for (i = 1; i < steered.size(); i++) {
383                 ns = steered[i];
384                 this->nodes().push_back(ns);
385                 this->add_iy(ns);
386                 pn->add_child(ns, this->cost(pn, ns));
387                 if (this->collide(pn, ns)) {
388                         pn->children().pop_back();
389                         break;
390                 }
391                 pn = ns;
392                 if (this->goal_found(pn, CO)) {
393                         this->tlog(this->findt());
394                         break;
395                 }
396         }
397         return this->goal_found();
398 }
399
400 bool T2::next()
401 {
402         RRTNode *rs;
403 #if GOALFIRST > 0
404         if (this->samples().size() == 0)
405                 rs = this->goal();
406         else
407                 rs = this->sample();
408 #else
409         rs = this->sample();
410 #endif
411         this->samples().push_back(rs);
412         RRTNode *nn = this->nn(rs);
413         if (!nn)
414                 return false;
415         RRTNode *pn = nn;
416         std::vector<RRTNode *> nvs;
417         std::vector<RRTNode *> newly_added;
418         bool en_add = true;
419         int cusps = 0;
420         for (auto ns: this->steer(nn, rs)) {
421                 if (!en_add) {
422                         delete ns;
423                 } else if (IS_NEAR(pn, ns)) {
424                         delete ns;
425                 } else {
426                         if (sgn(ns->s()) == 0 || sgn(pn->s()) != sgn(ns->s()))
427                                 cusps++;
428                         if (cusps > 4)
429                                 en_add = false;
430                         nvs = this->nv(
431                                         ns,
432                                         MIN(
433                                                 GAMMA_RRTSTAR(
434                                                         this->nodes().size()),
435                                                 0.2)); // TODO const
436                         this->nodes().push_back(ns);
437                         this->add_iy(ns);
438                         // connect
439                         if (!this->connect(pn, ns, nvs)) {
440                                 this->iy_[IYI(ns->y())].pop_back();
441                                 this->nodes().pop_back();
442                                 delete ns;
443                                 en_add = false;
444                         } else {
445                                 this->add_ixy(ns);
446                                 // rewire
447                                 this->rewire(nvs, ns);
448                                 pn = ns;
449                                 newly_added.push_back(pn);
450                                 if (this->goal_found(pn, CO)) {
451                                         this->goal_cost();
452                                         this->tlog(this->findt());
453                                         en_add = false;
454                                 }
455                         }
456                 }
457         }
458         en_add = true;
459         cusps = 0;
460         for (auto ns: this->steer(pn, rs, 0.01)) {
461                 if (!en_add) {
462                         delete ns;
463                 } else if (IS_NEAR(pn, ns)) {
464                         delete ns;
465                 } else {
466                         if (sgn(ns->s()) == 0 || sgn(pn->s()) != sgn(ns->s()))
467                                 cusps++;
468                         if (cusps > 4)
469                                 en_add = false;
470                         nvs = this->nv(
471                                         ns,
472                                         MIN(
473                                                 GAMMA_RRTSTAR(
474                                                         this->nodes().size()),
475                                                 0.2)); // TODO const
476                         this->nodes().push_back(ns);
477                         this->add_iy(ns);
478                         // connect
479                         if (!this->connect(pn, ns, nvs)) {
480                                 this->iy_[IYI(ns->y())].pop_back();
481                                 this->nodes().pop_back();
482                                 delete ns;
483                                 en_add = false;
484                         } else {
485                                 this->add_ixy(ns);
486                                 // rewire
487                                 this->rewire(nvs, ns);
488                                 pn = ns;
489                                 newly_added.push_back(pn);
490                                 if (this->goal_found(pn, CO)) {
491                                         this->goal_cost();
492                                         this->tlog(this->findt());
493                                         en_add = false;
494                                 }
495                         }
496                 }
497         }
498         if (this->samples().size() <= 1)
499                 return this->goal_found();
500         for (auto na: newly_added) {
501                 pn = na;
502                 en_add = true;
503                 cusps = 0;
504                 for (auto ns: this->steer(na, this->goal())) {
505                         if (!en_add) {
506                                 delete ns;
507                         } else if (IS_NEAR(pn, ns)) {
508                                 delete ns;
509                         } else {
510                                 if (sgn(pn->s()) != sgn(ns->s()))
511                                         cusps++;
512                                 if (cusps > 4)
513                                         en_add = false;
514                                 this->nodes().push_back(ns);
515                                 this->add_iy(ns);
516                                 pn->add_child(ns, this->cost(pn, ns));
517                                 if (this->collide(pn, ns)) {
518                                         pn->children().pop_back();
519                                         ns->remove_parent();
520                                         this->iy_[IYI(ns->y())].pop_back();
521                                         this->nodes().pop_back();
522                                         delete ns;
523                                         en_add = false;
524                                 } else {
525                                         this->add_ixy(ns);
526                                         this->ocost(ns);
527                                         pn = ns;
528                                         if (this->goal_found(pn, CO)) {
529                                                 this->goal_cost();
530                                                 this->tlog(this->findt());
531                                                 en_add = false;
532                                         }
533                                 }
534                         }
535                 }
536                 en_add = true;
537                 cusps = 0;
538                 for (auto ns: this->steer(pn, this->goal(), 0.01)) {
539                         if (!en_add) {
540                                 delete ns;
541                         } else if (IS_NEAR(pn, ns)) {
542                                 delete ns;
543                         } else {
544                                 if (sgn(pn->s()) != sgn(ns->s()))
545                                         cusps++;
546                                 if (cusps > 4)
547                                         en_add = false;
548                                 this->nodes().push_back(ns);
549                                 this->add_iy(ns);
550                                 pn->add_child(ns, this->cost(pn, ns));
551                                 if (this->collide(pn, ns)) {
552                                         pn->children().pop_back();
553                                         ns->remove_parent();
554                                         this->iy_[IYI(ns->y())].pop_back();
555                                         this->nodes().pop_back();
556                                         delete ns;
557                                         en_add = false;
558                                 } else {
559                                         this->add_ixy(ns);
560                                         this->ocost(ns);
561                                         pn = ns;
562                                         if (this->goal_found(pn, CO)) {
563                                                 this->goal_cost();
564                                                 this->tlog(this->findt());
565                                                 en_add = false;
566                                         }
567                                 }
568                         }
569                 }
570         }
571         return this->goal_found();
572 }
573
574 float T2::goal_cost()
575 {
576         std::vector<RRTNode *> nvs;
577         nvs = this->nv(this->goal(), 0.2);
578         for (auto nv: nvs) {
579                 if (std::abs(this->goal()->h() - nv->h()) >=
580                                 this->GOAL_FOUND_ANGLE)
581                         continue;
582                 if (nv->ccost() + this->cost(nv, this->goal()) >=
583                                 this->goal()->ccost())
584                         continue;
585                 RRTNode *op; // old parent
586                 float oc; // old cumulative cost
587                 float od; // old direct cost
588                 op = this->goal()->parent();
589                 oc = this->goal()->ccost();
590                 od = this->goal()->dcost();
591                 nv->add_child(this->goal(),
592                                 this->cost(nv, this->goal()));
593                 if (this->collide(nv, this->goal())) {
594                         nv->children().pop_back();
595                         this->goal()->parent(op);
596                         this->goal()->ccost(oc);
597                         this->goal()->dcost(od);
598                 } else {
599                         op->rem_child(this->goal());
600                 }
601         }
602         return this->goal()->ccost();
603 }
604
605 T3::~T3()
606 {
607         for (auto n: this->p_root_.nodes())
608                 if (n != this->p_root_.root() && n != this->p_root_.goal())
609                         delete n;
610         for (auto n: this->p_root_.dnodes())
611                 if (n != this->p_root_.root() && n != this->p_root_.goal())
612                         delete n;
613         for (auto s: this->p_root_.samples())
614                 if (s != this->p_root_.goal())
615                         delete s;
616         for (auto edges: this->p_root_.rlog())
617                 for (auto e: edges)
618                         delete e;
619
620         for (auto n: this->p_goal_.nodes())
621                 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
622                         delete n;
623         for (auto n: this->p_goal_.dnodes())
624                 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
625                         delete n;
626         for (auto s: this->p_goal_.samples())
627                 if (s != this->p_goal_.goal())
628                         delete s;
629         for (auto edges: this->p_goal_.rlog())
630                 for (auto e: edges)
631                         delete e;
632
633         for (auto n: this->nodes())
634                 if (n != this->root())
635                         delete n;
636         for (auto n: this->dnodes())
637                 if (n != this->root() && n != this->goal())
638                         delete n;
639         for (auto s: this->samples())
640                 if (s != this->goal())
641                         delete s;
642         for (auto edges: this->rlog())
643                 for (auto e: edges)
644                         delete e;
645
646         delete this->root();
647         delete this->goal();
648 }
649
650 T3::T3()
651 {
652         srand(static_cast<unsigned>(time(0)));
653 }
654
655 T3::T3(RRTNode *init, RRTNode *goal):
656         RRTBase(init, goal),
657         p_root_(init, goal),
658         p_goal_(goal, init)
659 {
660         srand(static_cast<unsigned>(time(0)));
661 }
662
663 bool T3::next()
664 {
665         RRTNode *ron = nullptr;
666         RRTNode *gon = nullptr;
667         bool ret = false;
668         ret = this->p_root_.next();
669         ret |= this->p_goal_.next();
670         if (this->overlaptrees(&ron, &gon)) {
671                 if (this->connecttrees(ron, gon))
672                         this->goal_found(true);
673                 this->tlog(this->findt());
674                 ret |= true;
675         }
676         return ret;
677 }
678
679 bool T3::link_obstacles(
680         std::vector<CircleObstacle> *cobstacles,
681         std::vector<SegmentObstacle> *sobstacles)
682 {
683         bool ret = false;
684         ret = RRTBase::link_obstacles(cobstacles, sobstacles);
685         ret &= this->p_root_.link_obstacles(cobstacles, sobstacles);
686         ret &= this->p_goal_.link_obstacles(cobstacles, sobstacles);
687         return ret;
688 }
689
690 bool T3::connecttrees(RRTNode *rn, RRTNode *gn)
691 {
692         while (gn != this->goal()) {
693                 this->p_root_.nodes().push_back(new RRTNode(
694                                 gn->x(),
695                                 gn->y(),
696                                 gn->h()));
697                 rn->add_child(
698                                 this->p_root_.nodes().back(),
699                                 this->p_root_.cost(
700                                                 rn,
701                                                 this->p_root_.nodes().back()));
702                 rn = this->p_root_.nodes().back();
703                 gn = gn->parent();
704         }
705         rn->add_child(this->goal(), this->p_root_.cost(rn, this->goal()));
706         return true;
707 }
708
709 bool T3::overlaptrees(RRTNode **ron, RRTNode **gon)
710 {
711         for (auto rn: this->p_root_.nodes()) {
712                 if (rn->parent() == nullptr)
713                         continue;
714                 for (auto gn: this->p_goal_.nodes()) {
715                         if (gn->parent() == nullptr)
716                                 continue;
717                         if (IS_NEAR(rn, gn)) {
718                                 *ron = rn;
719                                 *gon = gn;
720                                 return true;
721                         }
722                 }
723         }
724         return false;
725 }
726
727 Klemm2015::Klemm2015(RRTNode *init, RRTNode *goal):
728         Karaman2011(init, goal),
729         orig_root_(init),
730         orig_goal_(goal)
731 {
732         srand(static_cast<unsigned>(time(0)));
733         this->root()->tree('R');
734         this->goal()->tree('G');
735         this->add_iy(this->goal());
736 }
737
738 bool Klemm2015::next()
739 {
740         RRTNode *xn = nullptr;
741         RRTNode *rs;
742         int ret = 0;
743 #if GOALFIRST > 0
744         if (this->samples().size() == 0)
745                 rs = this->goal();
746         else
747                 rs = this->sample();
748 #else
749         rs = this->sample();
750 #endif
751         this->samples().push_back(rs);
752         //std::cerr << "next" << std::endl;
753         if (this->extendstar1(rs, &xn) != 2) {
754         //        if (xn) {
755         //                std::cerr << "- xn: " << xn->x() << ", " << xn->y();
756         //                std::cerr << std::endl;
757         //        } else {
758         //                std::cerr << "- xn: nullptr" << std::endl;
759         //        }
760                 this->swap();
761                 ret = this->connectstar(xn);
762         } else {
763                 this->swap();
764         }
765         if (ret == 1) {
766                 this->tlog(this->findt());
767                 return true;
768         }
769         return this->goal_found();
770 }
771
772 int Klemm2015::extendstar1(RRTNode *rs, RRTNode **xn)
773 {
774         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
775         char tree = this->root()->tree();
776         //std::cerr << "extend*1" << std::endl;
777         //std::cerr << "- tree is " << tree << std::endl;
778         //std::cerr << "  - rs: " << rs->x() << ", " << rs->y() << std::endl;
779         //if (xn && *xn) {
780         //        std::cerr << "  - xn: " << (*xn)->x() << ", " << (*xn)->y();
781         //        std::cerr << std::endl;
782         //}
783         //for (int i = 0; i < IYSIZE; i++) {
784         //        if (this->iy_[i].size() > 0) {
785         //                RRTNode *tmpn = this->iy_[i].back();
786         //                float tmpd = EDIST(tmpn, this->goal());
787
788         //                std::cerr << i << ": " << tmpn->x();
789         //                std::cerr << ", " << tmpn->y();
790         //                std::cerr << ", " << tmpn->tree();
791         //                std::cerr << " (" << tmpd << ")";
792
793         //                if (tmpn == this->root())
794         //                        std::cerr << " root";
795         //                if (tmpn == this->goal())
796         //                        std::cerr << " goal";
797         //                std::cerr << std::endl;
798         //        }
799         //}
800         RRTNode *nn = this->nn(rs);
801         //std::cerr << "  - nn: " << nn->x() << ", " << nn->y() << std::endl;
802         std::vector<RRTNode *> nvs;
803         std::vector<RRTNode *> steered = this->steer(nn, rs);
804         RRTNode *ns = steered[1];
805         ns->tree(tree);
806         nvs = this->nv(
807                         ns,
808                         MIN(
809                                 GAMMA_RRTSTAR(
810                                         this->nodes().size()),
811                                 0.2)); // TODO const
812         this->nodes().push_back(ns);
813         this->add_iy(ns);
814         // connect
815         if (!this->connect(nn, ns, nvs)) {
816                 this->iy_[IYI(ns->y())].pop_back();
817                 ret = 2;
818         } else {
819                 // rewire
820                 this->rewire(nvs, ns);
821         }
822         for (auto n: steered) {
823                 if (n != steered[1])
824                         delete n;
825         }
826         *xn = ns;
827         //std::cerr << "  - xn: " << (*xn)->x() << ", " << (*xn)->y();
828         //std::cerr << std::endl;
829         return ret;
830 }
831
832 int Klemm2015::extendstarC(RRTNode *rs)
833 {
834         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
835         char tree = this->root()->tree();
836         //std::cerr << "extend*C" << std::endl;
837         //std::cerr << "- tree is " << tree << std::endl;
838         //std::cerr << "  - rs: " << rs->x() << ", " << rs->y() << std::endl;
839         RRTNode *nn = this->nn(rs);
840         RRTNode *pn = nn;
841         std::vector<RRTNode *> nvs;
842         bool en_add = true;
843         for (auto ns: this->steer(nn, rs)) {
844                 if (!en_add) {
845                         delete ns;
846                 } else {
847                         nvs = this->nv(
848                                         ns,
849                                         MIN(
850                                                 GAMMA_RRTSTAR(
851                                                         this->nodes().size()),
852                                                 0.2)); // TODO const
853                         this->nodes().push_back(ns);
854                         this->add_iy(ns);
855                         // connect
856                         if (!this->connect(pn, ns, nvs)) {
857                                 this->iy_[IYI(ns->y())].pop_back();
858                                 en_add = false;
859                                 ret = 2;
860                         } else {
861                                 // rewire
862                                 this->rewire(nvs, ns);
863                                 pn = ns;
864 if (IS_NEAR(pn, rs)) { // GOAL FOUND !
865         RRTNode *tmp;
866         if (this->orig_root_ == this->root()) { // rs is in G tree
867                 // add all rs parents to pn
868                 tmp = rs->parent();
869         } else { // rs is in R tree
870                 tmp = pn->parent();
871                 pn = rs;
872                 this->swap();
873         }
874         while (tmp != this->goal()) {
875                 this->nodes().push_back(new RRTNode(
876                                 tmp->x(),
877                                 tmp->y(),
878                                 tmp->h()));
879                 this->nodes().back()->s(tmp->s());
880                 this->nodes().back()->tree('R');
881                 pn->add_child(
882                                 this->nodes().back(),
883                                 this->cost(pn, this->nodes().back()));
884                 pn = this->nodes().back();
885                 tmp = tmp->parent();
886         }
887         pn->add_child(tmp, this->cost(pn, tmp)); // add goal()
888         en_add = false;
889         ret = 1;
890 }
891                         }
892                 }
893         }
894         return ret;
895 }
896
897 int Klemm2015::connectstar(RRTNode *x)
898 {
899         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
900         //std::cerr << "connect* (start)" << std::endl;
901         ret = this->extendstarC(x);
902         //std::cerr << "connect* (end)" << std::endl;
903         return ret;
904 }
905
906 void Klemm2015::swap()
907 {
908         RRTNode *tmp;
909         tmp = this->root();
910         this->root(this->goal());
911         this->goal(tmp);
912 }