]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/rrtplanner.cc
Reformat IS_NEAR, add IS_NEAR for goal
[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 (this->firsts().size() > 0) {
404                 rs = this->firsts().front();
405                 this->firsts().pop();
406         } else {
407                 rs = this->sample();
408         }
409         this->samples().push_back(rs);
410         RRTNode *nn = this->nn(rs);
411         if (!nn)
412                 return false;
413         RRTNode *pn = nn;
414         std::vector<RRTNode *> nvs;
415         std::vector<RRTNode *> newly_added;
416         bool en_add = true;
417         int cusps = 0;
418         for (auto ns: this->steer(nn, rs)) {
419                 ns->rs(rs);
420                 if (!en_add) {
421                         delete ns;
422                 } else if (IS_NEAR(pn, ns)) {
423                         delete ns;
424                 } else {
425                         if (sgn(ns->s()) == 0 || sgn(pn->s()) != sgn(ns->s()))
426                                 cusps++;
427                         if (cusps > 4)
428                                 en_add = false;
429                         nvs = this->nv(
430                                         ns,
431                                         MIN(
432                                                 GAMMA_RRTSTAR(
433                                                         this->nodes().size()),
434                                                 0.2)); // TODO const
435                         this->nodes().push_back(ns);
436                         this->add_iy(ns);
437                         // connect
438                         if (!this->connect(pn, ns, nvs)) {
439                                 this->iy_[IYI(ns->y())].pop_back();
440                                 this->nodes().pop_back();
441                                 delete ns;
442                                 en_add = false;
443                         } else {
444                                 this->add_ixy(ns);
445                                 // rewire
446                                 this->rewire(nvs, ns);
447                                 pn = ns;
448                                 newly_added.push_back(pn);
449                                 if (this->goal_found(pn, CO)) {
450                                         this->goal_cost();
451                                         this->tlog(this->findt());
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                 ns->rs(rs);
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         // steer to goal
501         for (auto na: newly_added) {
502                 pn = na;
503                 en_add = true;
504                 cusps = 0;
505                 for (auto ns: this->steer(na, this->goal())) {
506                         ns->rs(rs);
507                         if (!en_add) {
508                                 delete ns;
509                         } else if (IS_NEAR(pn, ns)) {
510                                 delete ns;
511                         } else {
512                                 if (sgn(pn->s()) != sgn(ns->s()))
513                                         cusps++;
514                                 if (cusps > 4)
515                                         en_add = false;
516                                 this->nodes().push_back(ns);
517                                 this->add_iy(ns);
518                                 pn->add_child(ns, this->cost(pn, ns));
519                                 if (this->collide(pn, ns)) {
520                                         pn->children().pop_back();
521                                         ns->remove_parent();
522                                         this->iy_[IYI(ns->y())].pop_back();
523                                         this->nodes().pop_back();
524                                         delete ns;
525                                         en_add = false;
526                                 } else {
527                                         this->add_ixy(ns);
528                                         this->ocost(ns);
529                                         pn = ns;
530                                         if (this->goal_found(pn, CO)) {
531                                                 this->goal_cost();
532                                                 this->tlog(this->findt());
533                                                 en_add = false;
534                                         }
535                                 }
536                         }
537                 }
538                 en_add = true;
539                 cusps = 0;
540                 for (auto ns: this->steer(pn, this->goal(), 0.01)) {
541                         ns->rs(rs);
542                         if (!en_add) {
543                                 delete ns;
544                         } else if (IS_NEAR(pn, ns)) {
545                                 delete ns;
546                         } else {
547                                 if (sgn(pn->s()) != sgn(ns->s()))
548                                         cusps++;
549                                 if (cusps > 4)
550                                         en_add = false;
551                                 this->nodes().push_back(ns);
552                                 this->add_iy(ns);
553                                 pn->add_child(ns, this->cost(pn, ns));
554                                 if (this->collide(pn, ns)) {
555                                         pn->children().pop_back();
556                                         ns->remove_parent();
557                                         this->iy_[IYI(ns->y())].pop_back();
558                                         this->nodes().pop_back();
559                                         delete ns;
560                                         en_add = false;
561                                 } else {
562                                         this->add_ixy(ns);
563                                         this->ocost(ns);
564                                         pn = ns;
565                                         if (this->goal_found(pn, CO)) {
566                                                 this->goal_cost();
567                                                 this->tlog(this->findt());
568                                                 en_add = false;
569                                         }
570                                 }
571                         }
572                 }
573         }
574         // steer to goals
575         for (auto na: newly_added) {
576         for (auto go: this->goals()) {
577                 pn = na;
578                 en_add = true;
579                 cusps = 0;
580                 for (auto ns: this->steer(na, go)) {
581                         ns->rs(rs);
582                         if (!en_add) {
583                                 delete ns;
584                         } else if (IS_NEAR(pn, ns)) {
585                                 delete ns;
586                         } else {
587                                 if (sgn(pn->s()) != sgn(ns->s()))
588                                         cusps++;
589                                 if (cusps > 4)
590                                         en_add = false;
591                                 this->nodes().push_back(ns);
592                                 this->add_iy(ns);
593                                 pn->add_child(ns, this->cost(pn, ns));
594                                 if (this->collide(pn, ns)) {
595                                         pn->children().pop_back();
596                                         ns->remove_parent();
597                                         this->iy_[IYI(ns->y())].pop_back();
598                                         this->nodes().pop_back();
599                                         delete ns;
600                                         en_add = false;
601                                 } else {
602                                         this->add_ixy(ns);
603                                         this->ocost(ns);
604                                         pn = ns;
605                                         if (this->goal_found(pn, go)) {
606                                                 this->tlog(this->findt());
607                                                 en_add = false;
608                                         }
609                                 }
610                         }
611                 }
612         }}
613         return this->goal_found();
614 }
615
616 float T2::goal_cost()
617 {
618         std::vector<RRTNode *> nvs;
619         nvs = this->nv(this->goal(), 0.2);
620         for (auto nv: nvs) {
621                 if (std::abs(this->goal()->h() - nv->h()) >=
622                                 this->GOAL_FOUND_ANGLE)
623                         continue;
624                 if (nv->ccost() + this->cost(nv, this->goal()) >=
625                                 this->goal()->ccost())
626                         continue;
627                 RRTNode *op; // old parent
628                 float oc; // old cumulative cost
629                 float od; // old direct cost
630                 op = this->goal()->parent();
631                 oc = this->goal()->ccost();
632                 od = this->goal()->dcost();
633                 nv->add_child(this->goal(),
634                                 this->cost(nv, this->goal()));
635                 if (this->collide(nv, this->goal())) {
636                         nv->children().pop_back();
637                         this->goal()->parent(op);
638                         this->goal()->ccost(oc);
639                         this->goal()->dcost(od);
640                 } else {
641                         op->rem_child(this->goal());
642                 }
643         }
644         return this->goal()->ccost();
645 }
646
647 T3::~T3()
648 {
649         for (auto n: this->p_root_.nodes())
650                 if (n != this->p_root_.root() && n != this->p_root_.goal())
651                         delete n;
652         for (auto n: this->p_root_.dnodes())
653                 if (n != this->p_root_.root() && n != this->p_root_.goal())
654                         delete n;
655         for (auto s: this->p_root_.samples())
656                 if (s != this->p_root_.goal())
657                         delete s;
658         for (auto edges: this->p_root_.rlog())
659                 for (auto e: edges)
660                         delete e;
661
662         for (auto n: this->p_goal_.nodes())
663                 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
664                         delete n;
665         for (auto n: this->p_goal_.dnodes())
666                 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
667                         delete n;
668         for (auto s: this->p_goal_.samples())
669                 if (s != this->p_goal_.goal())
670                         delete s;
671         for (auto edges: this->p_goal_.rlog())
672                 for (auto e: edges)
673                         delete e;
674
675         for (auto n: this->nodes())
676                 if (n != this->root())
677                         delete n;
678         for (auto n: this->dnodes())
679                 if (n != this->root() && n != this->goal())
680                         delete n;
681         for (auto s: this->samples())
682                 if (s != this->goal())
683                         delete s;
684         for (auto edges: this->rlog())
685                 for (auto e: edges)
686                         delete e;
687
688         delete this->root();
689         delete this->goal();
690 }
691
692 T3::T3()
693 {
694         srand(static_cast<unsigned>(time(0)));
695 }
696
697 T3::T3(RRTNode *init, RRTNode *goal):
698         RRTBase(init, goal),
699         p_root_(init, goal),
700         p_goal_(goal, init)
701 {
702         srand(static_cast<unsigned>(time(0)));
703 }
704
705 bool T3::next()
706 {
707         RRTNode *ron = nullptr;
708         RRTNode *gon = nullptr;
709         bool ret = false;
710         ret = this->p_root_.next();
711         ret |= this->p_goal_.next();
712         if (this->overlaptrees(&ron, &gon)) {
713                 if (this->connecttrees(ron, gon))
714                         this->goal_found(true);
715                 this->tlog(this->findt());
716                 ret |= true;
717         }
718         return ret;
719 }
720
721 bool T3::link_obstacles(
722         std::vector<CircleObstacle> *cobstacles,
723         std::vector<SegmentObstacle> *sobstacles)
724 {
725         bool ret = false;
726         ret = RRTBase::link_obstacles(cobstacles, sobstacles);
727         ret &= this->p_root_.link_obstacles(cobstacles, sobstacles);
728         ret &= this->p_goal_.link_obstacles(cobstacles, sobstacles);
729         return ret;
730 }
731
732 bool T3::connecttrees(RRTNode *rn, RRTNode *gn)
733 {
734         while (gn != this->goal()) {
735                 this->p_root_.nodes().push_back(new RRTNode(
736                                 gn->x(),
737                                 gn->y(),
738                                 gn->h()));
739                 rn->add_child(
740                                 this->p_root_.nodes().back(),
741                                 this->p_root_.cost(
742                                                 rn,
743                                                 this->p_root_.nodes().back()));
744                 rn = this->p_root_.nodes().back();
745                 gn = gn->parent();
746         }
747         rn->add_child(this->goal(), this->p_root_.cost(rn, this->goal()));
748         return true;
749 }
750
751 bool T3::overlaptrees(RRTNode **ron, RRTNode **gon)
752 {
753         for (auto rn: this->p_root_.nodes()) {
754                 if (rn->parent() == nullptr)
755                         continue;
756                 for (auto gn: this->p_goal_.nodes()) {
757                         if (gn->parent() == nullptr)
758                                 continue;
759                         if (IS_NEAR(rn, gn)) {
760                                 *ron = rn;
761                                 *gon = gn;
762                                 return true;
763                         }
764                 }
765         }
766         return false;
767 }
768
769 Klemm2015::Klemm2015(RRTNode *init, RRTNode *goal):
770         Karaman2011(init, goal),
771         orig_root_(init),
772         orig_goal_(goal)
773 {
774         srand(static_cast<unsigned>(time(0)));
775         this->root()->tree('R');
776         this->goal()->tree('G');
777         this->add_iy(this->goal());
778 }
779
780 bool Klemm2015::next()
781 {
782         RRTNode *xn = nullptr;
783         RRTNode *rs;
784         int ret = 0;
785 #if GOALFIRST > 0
786         if (this->samples().size() == 0)
787                 rs = this->goal();
788         else
789                 rs = this->sample();
790 #else
791         rs = this->sample();
792 #endif
793         this->samples().push_back(rs);
794         //std::cerr << "next" << std::endl;
795         if (this->extendstar1(rs, &xn) != 2) {
796         //        if (xn) {
797         //                std::cerr << "- xn: " << xn->x() << ", " << xn->y();
798         //                std::cerr << std::endl;
799         //        } else {
800         //                std::cerr << "- xn: nullptr" << std::endl;
801         //        }
802                 this->swap();
803                 ret = this->connectstar(xn);
804         } else {
805                 this->swap();
806         }
807         if (ret == 1) {
808                 this->tlog(this->findt());
809                 return true;
810         }
811         return this->goal_found();
812 }
813
814 int Klemm2015::extendstar1(RRTNode *rs, RRTNode **xn)
815 {
816         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
817         char tree = this->root()->tree();
818         //std::cerr << "extend*1" << std::endl;
819         //std::cerr << "- tree is " << tree << std::endl;
820         //std::cerr << "  - rs: " << rs->x() << ", " << rs->y() << std::endl;
821         //if (xn && *xn) {
822         //        std::cerr << "  - xn: " << (*xn)->x() << ", " << (*xn)->y();
823         //        std::cerr << std::endl;
824         //}
825         //for (int i = 0; i < IYSIZE; i++) {
826         //        if (this->iy_[i].size() > 0) {
827         //                RRTNode *tmpn = this->iy_[i].back();
828         //                float tmpd = EDIST(tmpn, this->goal());
829
830         //                std::cerr << i << ": " << tmpn->x();
831         //                std::cerr << ", " << tmpn->y();
832         //                std::cerr << ", " << tmpn->tree();
833         //                std::cerr << " (" << tmpd << ")";
834
835         //                if (tmpn == this->root())
836         //                        std::cerr << " root";
837         //                if (tmpn == this->goal())
838         //                        std::cerr << " goal";
839         //                std::cerr << std::endl;
840         //        }
841         //}
842         RRTNode *nn = this->nn(rs);
843         //std::cerr << "  - nn: " << nn->x() << ", " << nn->y() << std::endl;
844         std::vector<RRTNode *> nvs;
845         std::vector<RRTNode *> steered = this->steer(nn, rs);
846         RRTNode *ns = steered[1];
847         ns->tree(tree);
848         nvs = this->nv(
849                         ns,
850                         MIN(
851                                 GAMMA_RRTSTAR(
852                                         this->nodes().size()),
853                                 0.2)); // TODO const
854         this->nodes().push_back(ns);
855         this->add_iy(ns);
856         // connect
857         if (!this->connect(nn, ns, nvs)) {
858                 this->iy_[IYI(ns->y())].pop_back();
859                 ret = 2;
860         } else {
861                 // rewire
862                 this->rewire(nvs, ns);
863         }
864         for (auto n: steered) {
865                 if (n != steered[1])
866                         delete n;
867         }
868         *xn = ns;
869         //std::cerr << "  - xn: " << (*xn)->x() << ", " << (*xn)->y();
870         //std::cerr << std::endl;
871         return ret;
872 }
873
874 int Klemm2015::extendstarC(RRTNode *rs)
875 {
876         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
877         char tree = this->root()->tree();
878         //std::cerr << "extend*C" << std::endl;
879         //std::cerr << "- tree is " << tree << std::endl;
880         //std::cerr << "  - rs: " << rs->x() << ", " << rs->y() << std::endl;
881         RRTNode *nn = this->nn(rs);
882         RRTNode *pn = nn;
883         std::vector<RRTNode *> nvs;
884         bool en_add = true;
885         for (auto ns: this->steer(nn, rs)) {
886                 if (!en_add) {
887                         delete ns;
888                 } else {
889                         nvs = this->nv(
890                                         ns,
891                                         MIN(
892                                                 GAMMA_RRTSTAR(
893                                                         this->nodes().size()),
894                                                 0.2)); // TODO const
895                         this->nodes().push_back(ns);
896                         this->add_iy(ns);
897                         // connect
898                         if (!this->connect(pn, ns, nvs)) {
899                                 this->iy_[IYI(ns->y())].pop_back();
900                                 en_add = false;
901                                 ret = 2;
902                         } else {
903                                 // rewire
904                                 this->rewire(nvs, ns);
905                                 pn = ns;
906 if (IS_NEAR(pn, rs)) { // GOAL FOUND !
907         RRTNode *tmp;
908         if (this->orig_root_ == this->root()) { // rs is in G tree
909                 // add all rs parents to pn
910                 tmp = rs->parent();
911         } else { // rs is in R tree
912                 tmp = pn->parent();
913                 pn = rs;
914                 this->swap();
915         }
916         while (tmp != this->goal()) {
917                 this->nodes().push_back(new RRTNode(
918                                 tmp->x(),
919                                 tmp->y(),
920                                 tmp->h()));
921                 this->nodes().back()->s(tmp->s());
922                 this->nodes().back()->tree('R');
923                 pn->add_child(
924                                 this->nodes().back(),
925                                 this->cost(pn, this->nodes().back()));
926                 pn = this->nodes().back();
927                 tmp = tmp->parent();
928         }
929         pn->add_child(tmp, this->cost(pn, tmp)); // add goal()
930         en_add = false;
931         ret = 1;
932 }
933                         }
934                 }
935         }
936         return ret;
937 }
938
939 int Klemm2015::connectstar(RRTNode *x)
940 {
941         int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
942         //std::cerr << "connect* (start)" << std::endl;
943         ret = this->extendstarC(x);
944         //std::cerr << "connect* (end)" << std::endl;
945         return ret;
946 }
947
948 void Klemm2015::swap()
949 {
950         RRTNode *tmp;
951         tmp = this->root();
952         this->root(this->goal());
953         this->goal(tmp);
954 }