]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - src/rrts.cc
Log path based on iterations
[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         if (sqrt(
422                 pow(f->x() - t->x(), 2)
423                 + pow(f->y() - t->y(), 2)
424         ) > 0.2)
425                 return false;
426         // cont.
427         this->store_node(this->steered().front());
428         t = &this->nodes().back();
429         t->p(f);
430         t->c(this->cost_build(*f, *t));
431         t->set_t(RRTNodeType::connected);
432         return true;
433 }
434
435 void RRTS::rewire()
436 {
437         RRTNode *f = &this->nodes().back();
438         for (auto n: this->nv(*f)) {
439                 if (
440                         !std::get<0>(this->collide_two_nodes(*f, *n))
441                         && cc(*f) + this->cost_search(*f, *n) < cc(*n)
442                 ) {
443                         n->p(f);
444                         n->c(this->cost_build(*f, *n));
445                 }
446         }
447 }
448
449 // API
450 void RRTS::init()
451 {
452 }
453
454 void RRTS::deinit()
455 {
456         this->nodes().clear();
457         this->samples().clear();
458         this->steered().clear();
459         this->store_node(RRTNode()); // root
460         this->icnt_ = 0;
461         this->scnt_ = 0;
462         this->pcnt_ = 0;
463         this->gf_ = false;
464 }
465
466 std::vector<RRTNode *> RRTS::path()
467 {
468         std::vector<RRTNode *> path;
469         if (this->goals().size() == 0)
470                 return path;
471         RRTNode *goal = &this->goals().back();
472         if (goal->p() == nullptr)
473                 return path;
474         while (goal != nullptr) {
475                 path.push_back(goal);
476                 goal = goal->p();
477         }
478         std::reverse(path.begin(), path.end());
479         return path;
480 }
481
482 bool RRTS::next()
483 {
484         if (this->icnt_ == 0)
485                 this->tstart_ = std::chrono::high_resolution_clock::now();
486         bool next = true;
487         if (this->icnt_ > this->log_path_iter_)
488             this->log_path_cost();
489         if (this->should_stop())
490                 return false;
491         if (this->samples().size() == 0) {
492                 this->samples().push_back(RRTNode());
493                 this->samples().back().x(this->goals().front().x());
494                 this->samples().back().y(this->goals().front().y());
495                 this->samples().back().h(this->goals().front().h());
496         } else {
497                 this->sample();
498         }
499         this->steer1(
500                 *this->nn(this->samples().back()),
501                 this->samples().back()
502         );
503         if (this->steered().size() == 0)
504                 return next;
505         auto col = this->collide_steered_from(
506                 *this->nn(this->samples().back())
507         );
508         if (std::get<0>(col)) {
509                 auto rcnt = this->steered().size() - std::get<1>(col);
510                 while (rcnt-- > 0) {
511                         this->steered().pop_back();
512                 }
513         }
514         if (!this->connect())
515                 return next;
516         this->rewire();
517         unsigned scnt = this->steered().size();
518         this->join_steered(&this->nodes().back());
519         RRTNode *just_added = &this->nodes().back();
520         while (scnt > 0) {
521                 // store all the steered1 nodes
522                 this->steered1_.push_back(just_added);
523                 scnt--;
524                 auto &g = this->goals().front();
525                 this->steer2(*just_added, g);
526                 auto col = this->collide_steered_from(*just_added);
527                 if (std::get<0>(col)) {
528                         auto rcnt = this->steered().size() - std::get<1>(col);
529                         while (rcnt-- > 0) {
530                                 this->steered().pop_back();
531                         }
532                 }
533                 this->join_steered(just_added);
534                 // store all the steered2 nodes
535                 RRTNode* jap = &this->nodes().back();
536                 while (jap != just_added) {
537                         this->steered2_.push_back(jap);
538                         jap = jap->p();
539                 }
540                 this->gf(this->goal_found(this->nodes().back()));
541                 just_added = just_added->p();
542         }
543         return next;
544 }
545
546 void RRTS::set_sample_normal(
547         double mx, double dx,
548         double my, double dy,
549         double mh, double dh
550 )
551 {
552         this->ndx_ = std::normal_distribution<double>(mx, dx);
553         this->ndy_ = std::normal_distribution<double>(my, dy);
554         this->ndh_ = std::normal_distribution<double>(mh, dh);
555 }
556 void RRTS::set_sample_uniform(
557         double xmin, double xmax,
558         double ymin, double ymax,
559         double hmin, double hmax
560 )
561 {
562         this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
563         this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
564         this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
565 }
566 void RRTS::set_sample_uniform_circle()
567 {
568         this->udx_ = std::uniform_real_distribution<double>(0, 1);
569         this->udy_ = std::uniform_real_distribution<double>(0, 1);
570         this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
571 }
572 void RRTS::set_sample(
573         double x1, double x2,
574         double y1, double y2,
575         double h1, double h2
576 )
577 {
578         switch (this->sample_dist_type()) {
579         case 1: // uniform
580                 x1 += this->nodes().front().x();
581                 x2 += this->nodes().front().x();
582                 y1 += this->nodes().front().y();
583                 y2 += this->nodes().front().y();
584                 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
585                 break;
586         case 2: // uniform circle
587                 this->set_sample_uniform_circle();
588                 break;
589         case 3: // uniform index of node in nodes
590                 this->set_sample_uniform_circle();
591                 break;
592         default: // normal
593                 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
594         }
595 }
596
597 Json::Value RRTS::json()
598 {
599         Json::Value jvo;
600         {
601                 jvo["time"] = this->scnt();
602         }
603         {
604                 jvo["iterations"] = this->icnt();
605         }
606         {
607                 jvo["init"][0] = this->nodes().front().x();
608                 jvo["init"][1] = this->nodes().front().y();
609                 jvo["init"][2] = this->nodes().front().h();
610         }
611         {
612                 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
613         }
614         {
615                 if (this->path().size() > 0) {
616                         jvo["cost"] = cc(*this->path().back());
617                         jvo["entry"][0] = this->goals().front().x();
618                         jvo["entry"][1] = this->goals().front().y();
619                         jvo["entry"][2] = this->goals().front().h();
620                         if (this->entry_set) {
621                             jvo["entry"][2] = this->entry.b;
622                             jvo["entry"][3] = this->entry.e;
623                         }
624                         if (this->entries_set) {
625                                 jvo["entries"][0][0] = this->entry1.x;
626                                 jvo["entries"][0][1] = this->entry1.y;
627                                 jvo["entries"][0][2] = this->entry1.h;
628                                 jvo["entries"][1][0] = this->entry2.x;
629                                 jvo["entries"][1][1] = this->entry2.y;
630                                 jvo["entries"][1][2] = this->entry2.h;
631                         }
632                         jvo["goal"][0] = this->goals().back().x();
633                         jvo["goal"][1] = this->goals().back().y();
634                         jvo["goal"][2] = this->goals().back().h();
635                 }
636         }
637         {
638                 unsigned int cu = 0;
639                 unsigned int co = 0;
640                 unsigned int pcnt = 0;
641                 for (auto n: this->path()) {
642                         jvo["path"][pcnt][0] = n->x();
643                         jvo["path"][pcnt][1] = n->y();
644                         jvo["path"][pcnt][2] = n->h();
645                         if (n->t(RRTNodeType::cusp))
646                                 cu++;
647                         if (n->t(RRTNodeType::connected))
648                                 co++;
649                         pcnt++;
650                 }
651                 jvo["cusps-in-path"] = cu;
652                 jvo["connecteds-in-path"] = co;
653         }
654         {
655                 unsigned int gcnt = 0;
656                 for (auto g: this->goals()) {
657                         jvo["goals"][gcnt][0] = g.x();
658                         jvo["goals"][gcnt][1] = g.y();
659                         jvo["goals"][gcnt][2] = g.h();
660                         gcnt++;
661                 }
662         }
663         {
664                 unsigned int ocnt = 0;
665                 for (auto o: this->obstacles()) {
666                         unsigned int ccnt = 0;
667                         for (auto c: o.poly()) {
668                                 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
669                                 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
670                                 ccnt++;
671                         }
672                         ocnt++;
673                 }
674         }
675         {
676                 jvo["nodes"] = (unsigned int) this->nodes().size();
677         }
678         {
679                 unsigned int cnt = 0;
680                 for (auto i: this->log_path_cost_)
681                         jvo["log_path_cost"][cnt++] = i;
682         }
683         //{
684         //        unsigned int ncnt = 0;
685         //        for (auto n: this->nodes()) {
686         //                jvo["nodes_x"][ncnt] = n.x();
687         //                jvo["nodes_y"][ncnt] = n.y();
688         //                //jvo["nodes_h"][ncnt] = n.h();
689         //                ncnt++;
690         //        }
691         //}
692         //{
693         //        unsigned int ncnt = 0;
694         //        for (auto n: this->steered1_) {
695         //                jvo["steered1_x"][ncnt] = n->x();
696         //                jvo["steered1_y"][ncnt] = n->y();
697         //                //jvo["nodes_h"][ncnt] = n.h();
698         //                ncnt++;
699         //        }
700         //        ncnt = 0;
701         //        for (auto n: this->steered2_) {
702         //                jvo["steered2_x"][ncnt] = n->x();
703         //                jvo["steered2_y"][ncnt] = n->y();
704         //                //jvo["nodes_h"][ncnt] = n.h();
705         //                ncnt++;
706         //        }
707         //}
708         return jvo;
709 }
710
711 void RRTS::json(Json::Value jvi)
712 {
713         assert(jvi["init"] != Json::nullValue);
714         assert(jvi["goals"] != Json::nullValue);
715         assert(jvi["obst"] != Json::nullValue);
716
717         this->nodes().front().x(jvi["init"][0].asDouble());
718         this->nodes().front().y(jvi["init"][1].asDouble());
719         this->nodes().front().h(jvi["init"][2].asDouble());
720         {
721                 RRTNode* gp = nullptr;
722                 if (jvi["entry"] != Json::nullValue) {
723                         this->entry_set = true;
724                         this->entry.x = jvi["entry"][0].asDouble();
725                         this->entry.y = jvi["entry"][1].asDouble();
726                         this->entry.b = jvi["entry"][2].asDouble();
727                         this->entry.e = jvi["entry"][3].asDouble();
728                         RRTNode tmp_node;
729                         tmp_node.x(this->entry.x);
730                         tmp_node.y(this->entry.y);
731                         tmp_node.h((this->entry.b + this->entry.e) / 2.0);
732                         this->goals().push_back(tmp_node);
733                         this->goals().back().p(gp);
734                         gp = &this->goals().back();
735                 }
736                 if (jvi["entries"] != Json::nullValue) {
737                         this->entries_set = true;
738                         this->entry1.x = jvi["entries"][0][0].asDouble();
739                         this->entry1.y = jvi["entries"][0][1].asDouble();
740                         this->entry1.h = jvi["entries"][0][2].asDouble();
741                         this->entry2.x = jvi["entries"][1][0].asDouble();
742                         this->entry2.y = jvi["entries"][1][1].asDouble();
743                         this->entry2.h = jvi["entries"][1][2].asDouble();
744                 }
745                 for (auto g: jvi["goals"]) {
746                         RRTNode tmp_node;
747                         tmp_node.x(g[0].asDouble());
748                         tmp_node.y(g[1].asDouble());
749                         tmp_node.h(g[2].asDouble());
750                         this->goals().push_back(tmp_node);
751                         this->goals().back().p(gp);
752                         gp = &this->goals().back();
753                 }
754                 this->goals().front().set_t(RRTNodeType::cusp);
755                 this->goals().back().set_t(RRTNodeType::cusp);
756         }
757         {
758                 Obstacle tmp_obstacle;
759                 for (auto o: jvi["obst"]) {
760                         tmp_obstacle.poly().clear();
761                         for (auto c: o) {
762                                 double tmp_x = c[0].asDouble();
763                                 double tmp_y = c[1].asDouble();
764                                 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
765                                 tmp_obstacle.poly().push_back(tmp_tuple);
766                         }
767                         this->obstacles().push_back(tmp_obstacle);
768                 }
769         }
770         {
771                 double edist_init_goal = sqrt(
772                         pow(
773                                 this->nodes().front().x()
774                                 - this->goals().front().x(),
775                                 2
776                         )
777                         + pow(
778                                 this->nodes().front().y()
779                                 - this->goals().front().y(),
780                                 2
781                         )
782                 );
783                 this->set_sample(
784                         this->nodes().front().x(), edist_init_goal,
785                         this->nodes().front().y(), edist_init_goal,
786                         0, 2 * M_PI
787                 );
788         }
789 }
790
791 RRTS::RRTS()
792         : gen_(std::random_device{}())
793 {
794         this->goals().reserve(100);
795         this->nodes().reserve(4000000);
796         this->samples().reserve(1000);
797         this->steered().reserve(20000);
798         this->store_node(RRTNode()); // root
799 }
800
801 double cc(RRTNode &t)
802 {
803         RRTNode *n = &t;
804         double cost = 0;
805         while (n != nullptr) {
806                 cost += n->c();
807                 n = n->p();
808         }
809         return cost;
810 }