]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - src/rrts.cc
Use goal zone in connect
[hubacji1/rrts.git] / src / rrts.cc
1 #include <algorithm>
2 #include "rrts.h"
3
4 #include "reeds_shepp.h"
5
6 template <typename T> int sgn(T val) {
7         return (T(0) < val) - (val < T(0));
8 }
9
10 RRTNode::RRTNode()
11 {
12 }
13
14 RRTNode::RRTNode(const BicycleCar &bc)
15 {
16     this->x(bc.x());
17     this->y(bc.y());
18     this->h(bc.h());
19     this->sp(bc.sp());
20     this->st(bc.st());
21 }
22
23 bool RRTNode::operator==(const RRTNode& n)
24 {
25         if (this == &n)
26                 return true;
27         return false;
28 }
29
30 Obstacle::Obstacle()
31 {
32 }
33
34 double RRTS::elapsed()
35 {
36         std::chrono::duration<double> dt;
37         dt = std::chrono::duration_cast<std::chrono::duration<double>>(
38                 std::chrono::high_resolution_clock::now()
39                 - this->tstart_
40         );
41         this->scnt_ = dt.count();
42         return this->scnt_;
43 }
44
45 void RRTS::log_path_cost()
46 {
47         this->log_path_cost_.push_back(cc(this->goals().front()));
48         this->log_path_iter_ += 20;
49 }
50
51 bool RRTS::should_stop()
52 {
53         // the following counters must be updated, do not comment
54         this->icnt_++;
55         this->elapsed();
56         // current iteration stop conditions
57         if (this->should_finish()) return true;
58         if (this->should_break()) return true;
59         // but continue by default
60         return false;
61 }
62
63 bool RRTS::should_finish()
64 {
65         // decide finish conditions (maybe comment some lines)
66         if (this->icnt_ > 1000) return true;
67         //if (this->scnt_ > 2) return true;
68         if (this->finishit) return true;
69         //if (this->gf()) return true;
70         // but continue by default
71         return false;
72 }
73
74 bool RRTS::should_break()
75 {
76         // decide break conditions (maybe comment some lines)
77         //if (this->scnt_ - this->pcnt_ > 2) return true;
78         // but continue by default
79         return false;
80 }
81
82 bool RRTS::should_continue()
83 {
84         // decide the stop conditions (maybe comment some lines)
85         // it is exact opposite of `should_stop`
86         //if (this->icnt_ > 999) return false;
87         if (this->scnt_ > 10) return false;
88         if (this->gf()) return false;
89         // and reset pause counter if should continue
90         this->pcnt_ = this->scnt_;
91         return true;
92 }
93
94 void RRTS::store_node(RRTNode n)
95 {
96         this->nodes().push_back(n);
97 }
98
99 // RRT procedures
100 std::tuple<bool, unsigned int, unsigned int>
101 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
102 {
103         for (auto &o: this->obstacles())
104                 if (std::get<0>(::collide(poly, o.poly())))
105                         return ::collide(poly, o.poly());
106         return std::make_tuple(false, 0, 0);
107 }
108
109 std::tuple<bool, unsigned int, unsigned int>
110 RRTS::collide_steered_from(RRTNode &f)
111 {
112         auto fbc = BicycleCar();
113         fbc.x(f.x());
114         fbc.y(f.y());
115         fbc.h(f.h());
116         std::vector<std::tuple<double, double>> s;
117         s.push_back(std::make_tuple(fbc.x(), fbc.y()));
118         for (auto &n: this->steered()) {
119                 auto nbc = BicycleCar();
120                 nbc.x(n.x());
121                 nbc.y(n.y());
122                 nbc.h(n.h());
123                 s.push_back(std::make_tuple(nbc.lfx(), nbc.lfy()));
124                 s.push_back(std::make_tuple(nbc.lrx(), nbc.lry()));
125                 s.push_back(std::make_tuple(nbc.rrx(), nbc.rry()));
126                 s.push_back(std::make_tuple(nbc.rfx(), nbc.rfy()));
127         }
128         auto col = this->collide(s);
129         auto strip_from = this->steered().size() - std::get<1>(col) / 4;
130         if (std::get<0>(col) && strip_from > 0) {
131                 while (strip_from-- > 0) {
132                         this->steered().pop_back();
133                 }
134                 return this->collide_steered_from(f);
135         }
136         return col;
137 }
138 std::tuple<bool, unsigned int, unsigned int>
139 RRTS::collide_tmp_steered_from(RRTNode &f)
140 {
141         return std::make_tuple(false, 0, 0);
142 }
143
144 std::tuple<bool, unsigned int, unsigned int>
145 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
146 {
147         auto fbc = BicycleCar();
148         fbc.x(f.x());
149         fbc.y(f.y());
150         fbc.h(f.h());
151         auto tbc = BicycleCar();
152         tbc.x(f.x());
153         tbc.y(f.y());
154         tbc.h(f.h());
155         std::vector<std::tuple<double, double>> p;
156         p.push_back(std::make_tuple(fbc.lfx(), fbc.lfy()));
157         p.push_back(std::make_tuple(fbc.lrx(), fbc.lry()));
158         p.push_back(std::make_tuple(fbc.rrx(), fbc.rry()));
159         p.push_back(std::make_tuple(fbc.rfx(), fbc.rfy()));
160         p.push_back(std::make_tuple(tbc.lfx(), tbc.lfy()));
161         p.push_back(std::make_tuple(tbc.lrx(), tbc.lry()));
162         p.push_back(std::make_tuple(tbc.rrx(), tbc.rry()));
163         p.push_back(std::make_tuple(tbc.rfx(), tbc.rfy()));
164         return this->collide(p);
165 }
166
167 double RRTS::cost_build(RRTNode &f, RRTNode &t)
168 {
169         double cost = 0;
170         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
171         return cost;
172 }
173
174 double RRTS::cost_search(RRTNode &f, RRTNode &t)
175 {
176         double cost = 0;
177         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
178         return cost;
179 }
180
181 void RRTS::sample()
182 {
183         double x = 0;
184         double y = 0;
185         double h = 0;
186         switch (this->sample_dist_type()) {
187         case 1: // uniform
188                 x = this->udx_(this->gen_);
189                 y = this->udy_(this->gen_);
190                 h = this->udh_(this->gen_);
191                 break;
192         case 2: // uniform circle
193         {
194                 // see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
195                 double R = sqrt(
196                         pow(
197                                 this->nodes().front().x()
198                                 - this->goals().front().x(),
199                                 2
200                         )
201                         + pow(
202                                 this->nodes().front().y()
203                                 - this->goals().front().y(),
204                                 2
205                         )
206                 );
207                 double a = atan2(
208                         this->goals().front().y() - this->nodes().front().y(),
209                         this->goals().front().x() - this->nodes().front().x()
210                 );
211                 double cx = this->goals().front().x() - R/2 * cos(a);
212                 double cy = this->goals().front().y() - R/2 * sin(a);
213                 double r = R * sqrt(this->udx_(this->gen_));
214                 double theta = this->udy_(this->gen_) * 2 * M_PI;
215                 x = cx + r * cos(theta);
216                 y = cy + r * sin(theta);
217                 h = this->udh_(this->gen_);
218         }
219                 break;
220         case 3: {
221                 if (
222                         this->steered1_.size() == 0
223                         && this->steered2_.size() == 0
224                 ) {
225                         x = this->nodes().front().x();
226                         y = this->nodes().front().y();
227                         h = this->nodes().front().h();
228                         this->use_nn = &this->nodes().front();
229                 } else {
230                         this->udi1_ = std::uniform_int_distribution<unsigned int>(
231                                 0,
232                                 this->steered1_.size() - 1
233                         );
234                         this->udi2_ = std::uniform_int_distribution<unsigned int>(
235                                 0,
236                                 this->steered2_.size() - 1
237                         );
238                         auto ind1 = this->udi1_(this->gen_);
239                         auto ind2 = this->udi2_(this->gen_);
240                         if (
241                                 this->steered2_.size() == 0
242                         ) {
243                                 auto n1 = this->steered1_[ind1];
244                                 x = n1->x();
245                                 y = n1->y();
246                                 h = n1->h();
247                                 this->use_nn = this->steered1_[ind1];
248                         } else if (
249                                 this->steered1_.size() == 0
250                         ) {
251                                 auto n2 = this->steered2_[ind2];
252                                 x = n2->x();
253                                 y = n2->y();
254                                 h = n2->h();
255                                 this->use_nn = this->steered2_[ind2];
256                         } else {
257                                 auto n1 = this->steered1_[ind1];
258                                 auto n2 = this->steered2_[ind2];
259                                 auto which = this->udx_(this->gen_);
260                                 if (which > 0.5) {
261                                         x = n1->x();
262                                         y = n1->y();
263                                         h = n1->h();
264                                         this->use_nn = this->steered1_[ind1];
265                                 } else {
266                                         x = n2->x();
267                                         y = n2->y();
268                                         h = n2->h();
269                                         this->use_nn = this->steered2_[ind2];
270                                 }
271                         }
272                 }
273                 break;
274                 }
275         default: // normal
276                 x = this->ndx_(this->gen_);
277                 y = this->ndy_(this->gen_);
278                 h = this->ndh_(this->gen_);
279         }
280         this->samples().push_back(RRTNode());
281         this->samples().back().x(x);
282         this->samples().back().y(y);
283         this->samples().back().h(h);
284 }
285
286 RRTNode *RRTS::nn(RRTNode &t)
287 {
288         RRTNode *nn = &this->nodes().front();
289         double cost = this->cost_search(*nn, t);
290         for (auto &f: this->nodes()) {
291                 if (this->cost_search(f, t) < cost) {
292                         nn = &f;
293                         cost = this->cost_search(f, t);
294                 }
295         }
296         return nn;
297 }
298
299 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
300 {
301         std::vector<RRTNode *> nv;
302         double cost = std::min(GAMMA(this->nodes().size()), ETA);
303         for (auto &f: this->nodes())
304                 if (this->cost_search(f, t) < cost)
305                         nv.push_back(&f);
306         return nv;
307 }
308
309 int cb_rs_steer(double q[4], void *user_data)
310 {
311         std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
312         nodes->push_back(RRTNode());
313         nodes->back().x(q[0]);
314         nodes->back().y(q[1]);
315         nodes->back().h(q[2]);
316         nodes->back().sp(q[3]);
317         if (nodes->back().sp() == 0) {
318                 nodes->back().set_t(RRTNodeType::cusp);
319         } else if (nodes->size() >= 2) {
320                 RRTNode* lln = nodes->back().p();
321                 RRTNode* ln = &nodes->back();
322                 if (lln != nullptr && ln != nullptr && sgn(lln->sp()) != sgn(ln->sp()))
323                         ln->set_t(RRTNodeType::cusp);
324         }
325         return 0;
326 }
327
328 void RRTS::steer(RRTNode &f, RRTNode &t)
329 {
330         this->steered().clear();
331         double q0[] = {f.x(), f.y(), f.h()};
332         double q1[] = {t.x(), t.y(), t.h()};
333         ReedsSheppStateSpace rsss(this->bc.mtr());
334         rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->steered());
335 }
336 void RRTS::tmp_steer(RRTNode &f, RRTNode &t)
337 {
338     this->tmp_steered_.clear();
339     double q0[] = {f.x(), f.y(), f.h()};
340     double q1[] = {t.x(), t.y(), t.h()};
341     ReedsSheppStateSpace rsss(this->bc.mtr());
342     rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->tmp_steered_);
343 }
344
345 void RRTS::steer1(RRTNode &f, RRTNode &t)
346 {
347     return this->steer(f, t);
348 }
349
350 void RRTS::steer2(RRTNode &f, RRTNode &t)
351 {
352     return this->steer(f, t);
353 }
354
355 void RRTS::join_steered(RRTNode *f)
356 {
357         while (this->steered().size() > 0) {
358                 this->store_node(this->steered().front());
359                 RRTNode *t = &this->nodes().back();
360                 t->p(f);
361                 t->c(this->cost_build(*f, *t));
362                 this->steered().erase(this->steered().begin());
363                 f = t;
364         }
365 }
366 void RRTS::join_tmp_steered(RRTNode *f)
367 {
368         while (this->tmp_steered_.size() > 0) {
369                 this->store_node(this->tmp_steered_.front());
370                 RRTNode *t = &this->nodes().back();
371                 t->p(f);
372                 t->c(this->cost_build(*f, *t));
373                 this->tmp_steered_.erase(this->tmp_steered_.begin());
374                 f = t;
375         }
376 }
377
378 bool RRTS::goal_found(RRTNode &f)
379 {
380         auto &g = this->goals().front();
381         double cost = this->cost_build(f, g);
382         double edist = sqrt(
383                 pow(f.x() - g.x(), 2)
384                 + pow(f.y() - g.y(), 2)
385         );
386         double adist = std::abs(f.h() - g.h());
387         if (edist < 0.05 && adist < M_PI / 32) {
388                 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
389                         g.p(&f);
390                         g.c(cost);
391                 }
392                 return true;
393         }
394         return false;
395 }
396
397 // RRT* procedures
398 bool RRTS::connect()
399 {
400         RRTNode *t = &this->steered().front();
401         RRTNode *f = this->nn(this->samples().back());
402         double cost = this->cost_search(*f, *t);
403         for (auto n: this->nv(*t)) {
404                 if (
405                         !std::get<0>(this->collide_two_nodes(*n, *t))
406                         && this->cost_search(*n, *t) < cost
407                 ) {
408                         f = n;
409                         cost = this->cost_search(*n, *t);
410                 }
411         }
412         // steer from f->t and then continue with the steered.
413         this->tmp_steer(*f, *t);
414         if (this->tmp_steered_.size() > 0) {
415                 auto col = this->collide_tmp_steered_from(*f);
416                 if (std::get<0>(col))
417                         return false;
418                 this->join_tmp_steered(f);
419                 f = &this->nodes().back();
420         }
421         auto fbc = BicycleCar();
422         fbc.x(f->x());
423         fbc.y(f->y());
424         fbc.h(f->h());
425         auto tbc = BicycleCar();
426         tbc.x(t->x());
427         tbc.y(t->y());
428         tbc.h(t->h());
429         if (!tbc.drivable(fbc))
430             return false;
431         // cont.
432         this->store_node(this->steered().front());
433         t = &this->nodes().back();
434         t->p(f);
435         t->c(this->cost_build(*f, *t));
436         t->set_t(RRTNodeType::connected);
437         return true;
438 }
439
440 void RRTS::rewire()
441 {
442         RRTNode *f = &this->nodes().back();
443         for (auto n: this->nv(*f)) {
444                 if (
445                         !std::get<0>(this->collide_two_nodes(*f, *n))
446                         && cc(*f) + this->cost_search(*f, *n) < cc(*n)
447                 ) {
448                         n->p(f);
449                         n->c(this->cost_build(*f, *n));
450                 }
451         }
452 }
453
454 // API
455 void RRTS::init()
456 {
457 }
458
459 void RRTS::deinit()
460 {
461         this->nodes().clear();
462         this->samples().clear();
463         this->steered().clear();
464         this->store_node(RRTNode()); // root
465         this->icnt_ = 0;
466         this->scnt_ = 0;
467         this->pcnt_ = 0;
468         this->gf_ = false;
469 }
470
471 std::vector<RRTNode *> RRTS::path()
472 {
473         std::vector<RRTNode *> path;
474         if (this->goals().size() == 0)
475                 return path;
476         RRTNode *goal = &this->goals().back();
477         if (goal->p() == nullptr)
478                 return path;
479         while (goal != nullptr) {
480                 path.push_back(goal);
481                 goal = goal->p();
482         }
483         std::reverse(path.begin(), path.end());
484         return path;
485 }
486
487 bool RRTS::next()
488 {
489         if (this->icnt_ == 0)
490                 this->tstart_ = std::chrono::high_resolution_clock::now();
491         bool next = true;
492         if (this->icnt_ > this->log_path_iter_)
493             this->log_path_cost();
494         if (this->should_stop())
495                 return false;
496         if (this->samples().size() == 0) {
497                 this->samples().push_back(RRTNode());
498                 this->samples().back().x(this->goals().front().x());
499                 this->samples().back().y(this->goals().front().y());
500                 this->samples().back().h(this->goals().front().h());
501         } else {
502                 this->sample();
503         }
504         this->steer1(
505                 *this->nn(this->samples().back()),
506                 this->samples().back()
507         );
508         if (this->steered().size() == 0)
509                 return next;
510         auto col = this->collide_steered_from(
511                 *this->nn(this->samples().back())
512         );
513         if (std::get<0>(col)) {
514                 auto rcnt = this->steered().size() - std::get<1>(col);
515                 while (rcnt-- > 0) {
516                         this->steered().pop_back();
517                 }
518         }
519         if (!this->connect())
520                 return next;
521         this->rewire();
522         unsigned scnt = this->steered().size();
523         this->join_steered(&this->nodes().back());
524         RRTNode *just_added = &this->nodes().back();
525         while (scnt > 0) {
526                 // store all the steered1 nodes
527                 this->steered1_.push_back(just_added);
528                 scnt--;
529                 auto &g = this->goals().front();
530                 this->steer2(*just_added, g);
531                 auto col = this->collide_steered_from(*just_added);
532                 if (std::get<0>(col)) {
533                         auto rcnt = this->steered().size() - std::get<1>(col);
534                         while (rcnt-- > 0) {
535                                 this->steered().pop_back();
536                         }
537                 }
538                 this->join_steered(just_added);
539                 // store all the steered2 nodes
540                 RRTNode* jap = &this->nodes().back();
541                 while (jap != just_added) {
542                         this->steered2_.push_back(jap);
543                         jap = jap->p();
544                 }
545                 this->gf(this->goal_found(this->nodes().back()));
546                 just_added = just_added->p();
547         }
548         return next;
549 }
550
551 void RRTS::set_sample_normal(
552         double mx, double dx,
553         double my, double dy,
554         double mh, double dh
555 )
556 {
557         this->ndx_ = std::normal_distribution<double>(mx, dx);
558         this->ndy_ = std::normal_distribution<double>(my, dy);
559         this->ndh_ = std::normal_distribution<double>(mh, dh);
560 }
561 void RRTS::set_sample_uniform(
562         double xmin, double xmax,
563         double ymin, double ymax,
564         double hmin, double hmax
565 )
566 {
567         this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
568         this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
569         this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
570 }
571 void RRTS::set_sample_uniform_circle()
572 {
573         this->udx_ = std::uniform_real_distribution<double>(0, 1);
574         this->udy_ = std::uniform_real_distribution<double>(0, 1);
575         this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
576 }
577 void RRTS::set_sample(
578         double x1, double x2,
579         double y1, double y2,
580         double h1, double h2
581 )
582 {
583         switch (this->sample_dist_type()) {
584         case 1: // uniform
585                 x1 += this->nodes().front().x();
586                 x2 += this->nodes().front().x();
587                 y1 += this->nodes().front().y();
588                 y2 += this->nodes().front().y();
589                 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
590                 break;
591         case 2: // uniform circle
592                 this->set_sample_uniform_circle();
593                 break;
594         case 3: // uniform index of node in nodes
595                 this->set_sample_uniform_circle();
596                 break;
597         default: // normal
598                 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
599         }
600 }
601
602 Json::Value RRTS::json()
603 {
604         Json::Value jvo;
605         {
606                 jvo["time"] = this->scnt();
607         }
608         {
609                 jvo["iterations"] = this->icnt();
610         }
611         {
612                 jvo["init"][0] = this->nodes().front().x();
613                 jvo["init"][1] = this->nodes().front().y();
614                 jvo["init"][2] = this->nodes().front().h();
615         }
616         {
617                 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
618         }
619         {
620                 if (this->path().size() > 0) {
621                         jvo["cost"] = cc(*this->path().back());
622                         jvo["entry"][0] = this->goals().front().x();
623                         jvo["entry"][1] = this->goals().front().y();
624                         jvo["entry"][2] = this->goals().front().h();
625                         if (this->entry_set) {
626                             jvo["entry"][2] = this->entry.b;
627                             jvo["entry"][3] = this->entry.e;
628                         }
629                         if (this->entries_set) {
630                                 jvo["entries"][0][0] = this->entry1.x;
631                                 jvo["entries"][0][1] = this->entry1.y;
632                                 jvo["entries"][0][2] = this->entry1.h;
633                                 jvo["entries"][1][0] = this->entry2.x;
634                                 jvo["entries"][1][1] = this->entry2.y;
635                                 jvo["entries"][1][2] = this->entry2.h;
636                         }
637                         jvo["goal"][0] = this->goals().back().x();
638                         jvo["goal"][1] = this->goals().back().y();
639                         jvo["goal"][2] = this->goals().back().h();
640                 }
641         }
642         {
643                 unsigned int cu = 0;
644                 unsigned int co = 0;
645                 unsigned int pcnt = 0;
646                 for (auto n: this->path()) {
647                         jvo["path"][pcnt][0] = n->x();
648                         jvo["path"][pcnt][1] = n->y();
649                         jvo["path"][pcnt][2] = n->h();
650                         if (n->t(RRTNodeType::cusp))
651                                 cu++;
652                         if (n->t(RRTNodeType::connected))
653                                 co++;
654                         pcnt++;
655                 }
656                 jvo["cusps-in-path"] = cu;
657                 jvo["connecteds-in-path"] = co;
658         }
659         {
660                 unsigned int gcnt = 0;
661                 for (auto g: this->goals()) {
662                         jvo["goals"][gcnt][0] = g.x();
663                         jvo["goals"][gcnt][1] = g.y();
664                         jvo["goals"][gcnt][2] = g.h();
665                         gcnt++;
666                 }
667         }
668         {
669                 unsigned int ocnt = 0;
670                 for (auto o: this->obstacles()) {
671                         unsigned int ccnt = 0;
672                         for (auto c: o.poly()) {
673                                 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
674                                 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
675                                 ccnt++;
676                         }
677                         ocnt++;
678                 }
679         }
680         {
681                 jvo["nodes"] = (unsigned int) this->nodes().size();
682         }
683         {
684                 unsigned int cnt = 0;
685                 for (auto i: this->log_path_cost_)
686                         jvo["log_path_cost"][cnt++] = i;
687         }
688         //{
689         //        unsigned int ncnt = 0;
690         //        for (auto n: this->nodes()) {
691         //                jvo["nodes_x"][ncnt] = n.x();
692         //                jvo["nodes_y"][ncnt] = n.y();
693         //                //jvo["nodes_h"][ncnt] = n.h();
694         //                ncnt++;
695         //        }
696         //}
697         //{
698         //        unsigned int ncnt = 0;
699         //        for (auto n: this->steered1_) {
700         //                jvo["steered1_x"][ncnt] = n->x();
701         //                jvo["steered1_y"][ncnt] = n->y();
702         //                //jvo["nodes_h"][ncnt] = n.h();
703         //                ncnt++;
704         //        }
705         //        ncnt = 0;
706         //        for (auto n: this->steered2_) {
707         //                jvo["steered2_x"][ncnt] = n->x();
708         //                jvo["steered2_y"][ncnt] = n->y();
709         //                //jvo["nodes_h"][ncnt] = n.h();
710         //                ncnt++;
711         //        }
712         //}
713         return jvo;
714 }
715
716 void RRTS::json(Json::Value jvi)
717 {
718         assert(jvi["init"] != Json::nullValue);
719         assert(jvi["goals"] != Json::nullValue);
720         assert(jvi["obst"] != Json::nullValue);
721
722         this->nodes().front().x(jvi["init"][0].asDouble());
723         this->nodes().front().y(jvi["init"][1].asDouble());
724         this->nodes().front().h(jvi["init"][2].asDouble());
725         {
726                 RRTNode* gp = nullptr;
727                 if (jvi["entry"] != Json::nullValue) {
728                         this->entry_set = true;
729                         this->entry.x = jvi["entry"][0].asDouble();
730                         this->entry.y = jvi["entry"][1].asDouble();
731                         this->entry.b = jvi["entry"][2].asDouble();
732                         this->entry.e = jvi["entry"][3].asDouble();
733                         RRTNode tmp_node;
734                         tmp_node.x(this->entry.x);
735                         tmp_node.y(this->entry.y);
736                         tmp_node.h((this->entry.b + this->entry.e) / 2.0);
737                         this->goals().push_back(tmp_node);
738                         this->goals().back().p(gp);
739                         gp = &this->goals().back();
740                 }
741                 if (jvi["entries"] != Json::nullValue) {
742                         this->entries_set = true;
743                         this->entry1.x = jvi["entries"][0][0].asDouble();
744                         this->entry1.y = jvi["entries"][0][1].asDouble();
745                         this->entry1.h = jvi["entries"][0][2].asDouble();
746                         this->entry2.x = jvi["entries"][1][0].asDouble();
747                         this->entry2.y = jvi["entries"][1][1].asDouble();
748                         this->entry2.h = jvi["entries"][1][2].asDouble();
749                 }
750                 for (auto g: jvi["goals"]) {
751                         RRTNode tmp_node;
752                         tmp_node.x(g[0].asDouble());
753                         tmp_node.y(g[1].asDouble());
754                         tmp_node.h(g[2].asDouble());
755                         this->goals().push_back(tmp_node);
756                         this->goals().back().p(gp);
757                         gp = &this->goals().back();
758                 }
759                 this->goals().front().set_t(RRTNodeType::cusp);
760                 this->goals().back().set_t(RRTNodeType::cusp);
761         }
762         {
763                 Obstacle tmp_obstacle;
764                 for (auto o: jvi["obst"]) {
765                         tmp_obstacle.poly().clear();
766                         for (auto c: o) {
767                                 double tmp_x = c[0].asDouble();
768                                 double tmp_y = c[1].asDouble();
769                                 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
770                                 tmp_obstacle.poly().push_back(tmp_tuple);
771                         }
772                         this->obstacles().push_back(tmp_obstacle);
773                 }
774         }
775         {
776                 double edist_init_goal = sqrt(
777                         pow(
778                                 this->nodes().front().x()
779                                 - this->goals().front().x(),
780                                 2
781                         )
782                         + pow(
783                                 this->nodes().front().y()
784                                 - this->goals().front().y(),
785                                 2
786                         )
787                 );
788                 this->set_sample(
789                         this->nodes().front().x(), edist_init_goal,
790                         this->nodes().front().y(), edist_init_goal,
791                         0, 2 * M_PI
792                 );
793         }
794 }
795
796 RRTS::RRTS()
797         : gen_(std::random_device{}())
798 {
799         this->goals().reserve(100);
800         this->nodes().reserve(4000000);
801         this->samples().reserve(1000);
802         this->steered().reserve(20000);
803         this->store_node(RRTNode()); // root
804 }
805
806 double cc(RRTNode &t)
807 {
808         RRTNode *n = &t;
809         double cost = 0;
810         while (n != nullptr) {
811                 cost += n->c();
812                 n = n->p();
813         }
814         return cost;
815 }