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