]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/rrtplanner.cc
Update T2 with adding to `ixy_` data structure
[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                                         en_add = false;
453                                 }
454                         }
455                 }
456         }
457         en_add = true;
458         cusps = 0;
459         for (auto ns: this->steer(pn, rs, 0.01)) {
460                 if (!en_add) {
461                         delete ns;
462                 } else if (IS_NEAR(pn, ns)) {
463                         delete ns;
464                 } else {
465                         if (sgn(ns->s()) == 0 || sgn(pn->s()) != sgn(ns->s()))
466                                 cusps++;
467                         if (cusps > 4)
468                                 en_add = false;
469                         nvs = this->nv(
470                                         ns,
471                                         MIN(
472                                                 GAMMA_RRTSTAR(
473                                                         this->nodes().size()),
474                                                 0.2)); // TODO const
475                         this->nodes().push_back(ns);
476                         this->add_iy(ns);
477                         // connect
478                         if (!this->connect(pn, ns, nvs)) {
479                                 this->iy_[IYI(ns->y())].pop_back();
480                                 this->nodes().pop_back();
481                                 delete ns;
482                                 en_add = false;
483                         } else {
484                                 this->add_ixy(ns);
485                                 // rewire
486                                 this->rewire(nvs, ns);
487                                 pn = ns;
488                                 newly_added.push_back(pn);
489                                 if (this->goal_found(pn, CO)) {
490                                         this->goal_cost();
491                                         en_add = false;
492                                 }
493                         }
494                 }
495         }
496         if (this->samples().size() <= 1)
497                 return this->goal_found();
498         for (auto na: newly_added) {
499                 pn = na;
500                 en_add = true;
501                 cusps = 0;
502                 for (auto ns: this->steer(na, this->goal())) {
503                         if (!en_add) {
504                                 delete ns;
505                         } else if (IS_NEAR(pn, ns)) {
506                                 delete ns;
507                         } else {
508                                 if (sgn(pn->s()) != sgn(ns->s()))
509                                         cusps++;
510                                 if (cusps > 4)
511                                         en_add = false;
512                                 this->nodes().push_back(ns);
513                                 this->add_iy(ns);
514                                 pn->add_child(ns, this->cost(pn, ns));
515                                 if (this->collide(pn, ns)) {
516                                         pn->children().pop_back();
517                                         ns->remove_parent();
518                                         this->iy_[IYI(ns->y())].pop_back();
519                                         this->nodes().pop_back();
520                                         delete ns;
521                                         en_add = false;
522                                 } else {
523                                         this->add_ixy(ns);
524                                         this->ocost(ns);
525                                         pn = ns;
526                                         if (this->goal_found(pn, CO)) {
527                                                 this->goal_cost();
528                                                 en_add = false;
529                                         }
530                                 }
531                         }
532                 }
533                 en_add = true;
534                 cusps = 0;
535                 for (auto ns: this->steer(pn, this->goal(), 0.01)) {
536                         if (!en_add) {
537                                 delete ns;
538                         } else if (IS_NEAR(pn, ns)) {
539                                 delete ns;
540                         } else {
541                                 if (sgn(pn->s()) != sgn(ns->s()))
542                                         cusps++;
543                                 if (cusps > 4)
544                                         en_add = false;
545                                 this->nodes().push_back(ns);
546                                 this->add_iy(ns);
547                                 pn->add_child(ns, this->cost(pn, ns));
548                                 if (this->collide(pn, ns)) {
549                                         pn->children().pop_back();
550                                         ns->remove_parent();
551                                         this->iy_[IYI(ns->y())].pop_back();
552                                         this->nodes().pop_back();
553                                         delete ns;
554                                         en_add = false;
555                                 } else {
556                                         this->add_ixy(ns);
557                                         this->ocost(ns);
558                                         pn = ns;
559                                         if (this->goal_found(pn, CO)) {
560                                                 this->goal_cost();
561                                                 en_add = false;
562                                         }
563                                 }
564                         }
565                 }
566         }
567         return this->goal_found();
568 }
569
570 float T2::goal_cost()
571 {
572         std::vector<RRTNode *> nvs;
573         nvs = this->nv(this->goal(), 0.2);
574         for (auto nv: nvs) {
575                 if (std::abs(this->goal()->h() - nv->h()) >=
576                                 this->GOAL_FOUND_ANGLE)
577                         continue;
578                 if (nv->ccost() + this->cost(nv, this->goal()) >=
579                                 this->goal()->ccost())
580                         continue;
581                 RRTNode *op; // old parent
582                 float oc; // old cumulative cost
583                 float od; // old direct cost
584                 op = this->goal()->parent();
585                 oc = this->goal()->ccost();
586                 od = this->goal()->dcost();
587                 nv->add_child(this->goal(),
588                                 this->cost(nv, this->goal()));
589                 if (this->collide(nv, this->goal())) {
590                         nv->children().pop_back();
591                         this->goal()->parent(op);
592                         this->goal()->ccost(oc);
593                         this->goal()->dcost(od);
594                 } else {
595                         op->rem_child(this->goal());
596                 }
597         }
598         return this->goal()->ccost();
599 }
600
601 T3::~T3()
602 {
603         for (auto n: this->p_root_.nodes())
604                 if (n != this->p_root_.root() && n != this->p_root_.goal())
605                         delete n;
606         for (auto n: this->p_root_.dnodes())
607                 if (n != this->p_root_.root() && n != this->p_root_.goal())
608                         delete n;
609         for (auto s: this->p_root_.samples())
610                 if (s != this->p_root_.goal())
611                         delete s;
612         for (auto edges: this->p_root_.rlog())
613                 for (auto e: edges)
614                         delete e;
615
616         for (auto n: this->p_goal_.nodes())
617                 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
618                         delete n;
619         for (auto n: this->p_goal_.dnodes())
620                 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
621                         delete n;
622         for (auto s: this->p_goal_.samples())
623                 if (s != this->p_goal_.goal())
624                         delete s;
625         for (auto edges: this->p_goal_.rlog())
626                 for (auto e: edges)
627                         delete e;
628
629         for (auto n: this->nodes())
630                 if (n != this->root())
631                         delete n;
632         for (auto n: this->dnodes())
633                 if (n != this->root() && n != this->goal())
634                         delete n;
635         for (auto s: this->samples())
636                 if (s != this->goal())
637                         delete s;
638         for (auto edges: this->rlog())
639                 for (auto e: edges)
640                         delete e;
641
642         delete this->root();
643         delete this->goal();
644 }
645
646 T3::T3()
647 {
648         srand(static_cast<unsigned>(time(0)));
649 }
650
651 T3::T3(RRTNode *init, RRTNode *goal):
652         RRTBase(init, goal),
653         p_root_(init, goal),
654         p_goal_(goal, init)
655 {
656         srand(static_cast<unsigned>(time(0)));
657 }
658
659 bool T3::next()
660 {
661         RRTNode *ron = nullptr;
662         RRTNode *gon = nullptr;
663         bool ret = false;
664         ret = this->p_root_.next();
665         ret |= this->p_goal_.next();
666         if (this->overlaptrees(&ron, &gon)) {
667                 if (this->connecttrees(ron, gon))
668                         this->goal_found(true);
669                 this->tlog(this->findt());
670                 ret |= true;
671         }
672         return ret;
673 }
674
675 bool T3::link_obstacles(
676         std::vector<CircleObstacle> *cobstacles,
677         std::vector<SegmentObstacle> *sobstacles)
678 {
679         bool ret = false;
680         ret = RRTBase::link_obstacles(cobstacles, sobstacles);
681         ret &= this->p_root_.link_obstacles(cobstacles, sobstacles);
682         ret &= this->p_goal_.link_obstacles(cobstacles, sobstacles);
683         return ret;
684 }
685
686 bool T3::connecttrees(RRTNode *rn, RRTNode *gn)
687 {
688         while (gn != this->goal()) {
689                 this->p_root_.nodes().push_back(new RRTNode(
690                                 gn->x(),
691                                 gn->y(),
692                                 gn->h()));
693                 rn->add_child(
694                                 this->p_root_.nodes().back(),
695                                 this->p_root_.cost(
696                                                 rn,
697                                                 this->p_root_.nodes().back()));
698                 rn = this->p_root_.nodes().back();
699                 gn = gn->parent();
700         }
701         rn->add_child(this->goal(), this->p_root_.cost(rn, this->goal()));
702         return true;
703 }
704
705 bool T3::overlaptrees(RRTNode **ron, RRTNode **gon)
706 {
707         for (auto rn: this->p_root_.nodes()) {
708                 if (rn->parent() == nullptr)
709                         continue;
710                 for (auto gn: this->p_goal_.nodes()) {
711                         if (gn->parent() == nullptr)
712                                 continue;
713                         if (IS_NEAR(rn, gn)) {
714                                 *ron = rn;
715                                 *gon = gn;
716                                 return true;
717                         }
718                 }
719         }
720         return false;
721 }
722
723 Klemm2015::Klemm2015(RRTNode *init, RRTNode *goal):
724         Karaman2011(init, goal),
725         orig_root_(init),
726         orig_goal_(goal)
727 {
728         srand(static_cast<unsigned>(time(0)));
729         this->root()->tree('R');
730         this->goal()->tree('G');
731         this->add_iy(this->goal());
732 }
733
734 bool Klemm2015::next()
735 {
736         RRTNode *xn = nullptr;
737         RRTNode *rs;
738         int ret = 0;
739 #if GOALFIRST > 0
740         if (this->samples().size() == 0)
741                 rs = this->goal();
742         else
743                 rs = this->sample();
744 #else
745         rs = this->sample();
746 #endif
747         this->samples().push_back(rs);
748         //std::cerr << "next" << std::endl;
749         if (this->extendstar1(rs, &xn) != 2) {
750         //        if (xn) {
751         //                std::cerr << "- xn: " << xn->x() << ", " << xn->y();
752         //                std::cerr << std::endl;
753         //        } else {
754         //                std::cerr << "- xn: nullptr" << std::endl;
755         //        }
756                 this->swap();
757                 ret = this->connectstar(xn);
758         } else {
759                 this->swap();
760         }
761         if (ret == 1) {
762                 this->tlog(this->findt());
763                 return true;
764         }
765         return this->goal_found();
766 }
767
768 int Klemm2015::extendstar1(RRTNode *rs, RRTNode **xn)
769 {
770         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
771         char tree = this->root()->tree();
772         //std::cerr << "extend*1" << std::endl;
773         //std::cerr << "- tree is " << tree << std::endl;
774         //std::cerr << "  - rs: " << rs->x() << ", " << rs->y() << std::endl;
775         //if (xn && *xn) {
776         //        std::cerr << "  - xn: " << (*xn)->x() << ", " << (*xn)->y();
777         //        std::cerr << std::endl;
778         //}
779         //for (int i = 0; i < IYSIZE; i++) {
780         //        if (this->iy_[i].size() > 0) {
781         //                RRTNode *tmpn = this->iy_[i].back();
782         //                float tmpd = EDIST(tmpn, this->goal());
783
784         //                std::cerr << i << ": " << tmpn->x();
785         //                std::cerr << ", " << tmpn->y();
786         //                std::cerr << ", " << tmpn->tree();
787         //                std::cerr << " (" << tmpd << ")";
788
789         //                if (tmpn == this->root())
790         //                        std::cerr << " root";
791         //                if (tmpn == this->goal())
792         //                        std::cerr << " goal";
793         //                std::cerr << std::endl;
794         //        }
795         //}
796         RRTNode *nn = this->nn(rs);
797         //std::cerr << "  - nn: " << nn->x() << ", " << nn->y() << std::endl;
798         std::vector<RRTNode *> nvs;
799         std::vector<RRTNode *> steered = this->steer(nn, rs);
800         RRTNode *ns = steered[1];
801         ns->tree(tree);
802         nvs = this->nv(
803                         ns,
804                         MIN(
805                                 GAMMA_RRTSTAR(
806                                         this->nodes().size()),
807                                 0.2)); // TODO const
808         this->nodes().push_back(ns);
809         this->add_iy(ns);
810         // connect
811         if (!this->connect(nn, ns, nvs)) {
812                 this->iy_[IYI(ns->y())].pop_back();
813                 ret = 2;
814         } else {
815                 // rewire
816                 this->rewire(nvs, ns);
817         }
818         for (auto n: steered) {
819                 if (n != steered[1])
820                         delete n;
821         }
822         *xn = ns;
823         //std::cerr << "  - xn: " << (*xn)->x() << ", " << (*xn)->y();
824         //std::cerr << std::endl;
825         return ret;
826 }
827
828 int Klemm2015::extendstarC(RRTNode *rs)
829 {
830         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
831         char tree = this->root()->tree();
832         //std::cerr << "extend*C" << std::endl;
833         //std::cerr << "- tree is " << tree << std::endl;
834         //std::cerr << "  - rs: " << rs->x() << ", " << rs->y() << std::endl;
835         RRTNode *nn = this->nn(rs);
836         RRTNode *pn = nn;
837         std::vector<RRTNode *> nvs;
838         bool en_add = true;
839         for (auto ns: this->steer(nn, rs)) {
840                 if (!en_add) {
841                         delete ns;
842                 } else {
843                         nvs = this->nv(
844                                         ns,
845                                         MIN(
846                                                 GAMMA_RRTSTAR(
847                                                         this->nodes().size()),
848                                                 0.2)); // TODO const
849                         this->nodes().push_back(ns);
850                         this->add_iy(ns);
851                         // connect
852                         if (!this->connect(pn, ns, nvs)) {
853                                 this->iy_[IYI(ns->y())].pop_back();
854                                 en_add = false;
855                                 ret = 2;
856                         } else {
857                                 // rewire
858                                 this->rewire(nvs, ns);
859                                 pn = ns;
860 if (IS_NEAR(pn, rs)) { // GOAL FOUND !
861         RRTNode *tmp;
862         if (this->orig_root_ == this->root()) { // rs is in G tree
863                 // add all rs parents to pn
864                 tmp = rs->parent();
865         } else { // rs is in R tree
866                 tmp = pn->parent();
867                 pn = rs;
868                 this->swap();
869         }
870         while (tmp != this->goal()) {
871                 this->nodes().push_back(new RRTNode(
872                                 tmp->x(),
873                                 tmp->y(),
874                                 tmp->h()));
875                 this->nodes().back()->s(tmp->s());
876                 this->nodes().back()->tree('R');
877                 pn->add_child(
878                                 this->nodes().back(),
879                                 this->cost(pn, this->nodes().back()));
880                 pn = this->nodes().back();
881                 tmp = tmp->parent();
882         }
883         pn->add_child(tmp, this->cost(pn, tmp)); // add goal()
884         en_add = false;
885         ret = 1;
886 }
887                         }
888                 }
889         }
890         return ret;
891 }
892
893 int Klemm2015::connectstar(RRTNode *x)
894 {
895         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
896         //std::cerr << "connect* (start)" << std::endl;
897         ret = this->extendstarC(x);
898         //std::cerr << "connect* (end)" << std::endl;
899         return ret;
900 }
901
902 void Klemm2015::swap()
903 {
904         RRTNode *tmp;
905         tmp = this->root();
906         this->root(this->goal());
907         this->goal(tmp);
908 }