4 #include "reeds_shepp.h"
6 template <typename T> int sgn(T val) {
7 return (T(0) < val) - (val < T(0));
14 RRTNode::RRTNode(const BicycleCar &bc)
23 bool RRTNode::operator==(const RRTNode& n)
34 double RRTS::elapsed()
36 std::chrono::duration<double> dt;
37 dt = std::chrono::duration_cast<std::chrono::duration<double>>(
38 std::chrono::high_resolution_clock::now()
41 this->scnt_ = dt.count();
45 void RRTS::log_path_cost()
47 this->log_path_cost_.push_back(cc(this->goals().front()));
48 this->log_path_iter_ += 20;
51 bool RRTS::should_stop()
53 // the following counters must be updated, do not comment
56 // current iteration stop conditions
57 if (this->should_finish()) return true;
58 if (this->should_break()) return true;
59 // but continue by default
63 bool RRTS::should_finish()
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
74 bool RRTS::should_break()
76 // decide break conditions (maybe comment some lines)
77 //if (this->scnt_ - this->pcnt_ > 2) return true;
78 // but continue by default
82 bool RRTS::should_continue()
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_;
94 void RRTS::store_node(RRTNode n)
96 this->nodes().push_back(n);
100 std::tuple<bool, unsigned int, unsigned int>
101 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
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);
109 std::tuple<bool, unsigned int, unsigned int>
110 RRTS::collide_steered_from(RRTNode &f)
112 auto fbc = BicycleCar();
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();
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()));
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();
134 return this->collide_steered_from(f);
138 std::tuple<bool, unsigned int, unsigned int>
139 RRTS::collide_tmp_steered_from(RRTNode &f)
141 return std::make_tuple(false, 0, 0);
144 std::tuple<bool, unsigned int, unsigned int>
145 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
147 auto fbc = BicycleCar();
151 auto tbc = BicycleCar();
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);
167 double RRTS::cost_build(RRTNode &f, RRTNode &t)
170 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
174 double RRTS::cost_search(RRTNode &f, RRTNode &t)
177 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
186 switch (this->sample_dist_type()) {
188 x = this->udx_(this->gen_);
189 y = this->udy_(this->gen_);
190 h = this->udh_(this->gen_);
192 case 2: // uniform circle
194 // see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
197 this->nodes().front().x()
198 - this->goals().front().x(),
202 this->nodes().front().y()
203 - this->goals().front().y(),
208 this->goals().front().y() - this->nodes().front().y(),
209 this->goals().front().x() - this->nodes().front().x()
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_);
222 this->steered1_.size() == 0
223 && this->steered2_.size() == 0
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();
230 this->udi1_ = std::uniform_int_distribution<unsigned int>(
232 this->steered1_.size() - 1
234 this->udi2_ = std::uniform_int_distribution<unsigned int>(
236 this->steered2_.size() - 1
238 auto ind1 = this->udi1_(this->gen_);
239 auto ind2 = this->udi2_(this->gen_);
241 this->steered2_.size() == 0
243 auto n1 = this->steered1_[ind1];
247 this->use_nn = this->steered1_[ind1];
249 this->steered1_.size() == 0
251 auto n2 = this->steered2_[ind2];
255 this->use_nn = this->steered2_[ind2];
257 auto n1 = this->steered1_[ind1];
258 auto n2 = this->steered2_[ind2];
259 auto which = this->udx_(this->gen_);
264 this->use_nn = this->steered1_[ind1];
269 this->use_nn = this->steered2_[ind2];
276 x = this->ndx_(this->gen_);
277 y = this->ndy_(this->gen_);
278 h = this->ndh_(this->gen_);
280 this->samples().push_back(RRTNode());
281 this->samples().back().x(x);
282 this->samples().back().y(y);
283 this->samples().back().h(h);
286 RRTNode *RRTS::nn(RRTNode &t)
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) {
293 cost = this->cost_search(f, t);
299 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
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)
309 int cb_rs_steer(double q[4], void *user_data)
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);
328 void RRTS::steer(RRTNode &f, RRTNode &t)
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());
336 void RRTS::tmp_steer(RRTNode &f, RRTNode &t)
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_);
345 void RRTS::steer1(RRTNode &f, RRTNode &t)
347 return this->steer(f, t);
350 void RRTS::steer2(RRTNode &f, RRTNode &t)
352 return this->steer(f, t);
355 void RRTS::join_steered(RRTNode *f)
357 while (this->steered().size() > 0) {
358 this->store_node(this->steered().front());
359 RRTNode *t = &this->nodes().back();
361 t->c(this->cost_build(*f, *t));
362 this->steered().erase(this->steered().begin());
366 void RRTS::join_tmp_steered(RRTNode *f)
368 while (this->tmp_steered_.size() > 0) {
369 this->store_node(this->tmp_steered_.front());
370 RRTNode *t = &this->nodes().back();
372 t->c(this->cost_build(*f, *t));
373 this->tmp_steered_.erase(this->tmp_steered_.begin());
378 bool RRTS::goal_found(RRTNode &f)
380 auto &g = this->goals().front();
381 double cost = this->cost_build(f, g);
383 pow(f.x() - g.x(), 2)
384 + pow(f.y() - g.y(), 2)
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)) {
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)) {
405 !std::get<0>(this->collide_two_nodes(*n, *t))
406 && this->cost_search(*n, *t) < cost
409 cost = this->cost_search(*n, *t);
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))
418 this->join_tmp_steered(f);
419 f = &this->nodes().back();
421 auto fbc = BicycleCar();
425 auto tbc = BicycleCar();
429 if (!tbc.drivable(fbc))
432 this->store_node(this->steered().front());
433 t = &this->nodes().back();
435 t->c(this->cost_build(*f, *t));
436 t->set_t(RRTNodeType::connected);
442 RRTNode *f = &this->nodes().back();
443 for (auto n: this->nv(*f)) {
445 !std::get<0>(this->collide_two_nodes(*f, *n))
446 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
449 n->c(this->cost_build(*f, *n));
461 this->nodes().clear();
462 this->samples().clear();
463 this->steered().clear();
464 this->store_node(RRTNode()); // root
471 std::vector<RRTNode *> RRTS::path()
473 std::vector<RRTNode *> path;
474 if (this->goals().size() == 0)
476 RRTNode *goal = &this->goals().back();
477 if (goal->p() == nullptr)
479 while (goal != nullptr) {
480 path.push_back(goal);
483 std::reverse(path.begin(), path.end());
489 if (this->icnt_ == 0)
490 this->tstart_ = std::chrono::high_resolution_clock::now();
492 if (this->icnt_ > this->log_path_iter_)
493 this->log_path_cost();
494 if (this->should_stop())
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());
505 *this->nn(this->samples().back()),
506 this->samples().back()
508 if (this->steered().size() == 0)
510 auto col = this->collide_steered_from(
511 *this->nn(this->samples().back())
513 if (std::get<0>(col)) {
514 auto rcnt = this->steered().size() - std::get<1>(col);
516 this->steered().pop_back();
519 if (!this->connect())
522 unsigned scnt = this->steered().size();
523 this->join_steered(&this->nodes().back());
524 RRTNode *just_added = &this->nodes().back();
526 // store all the steered1 nodes
527 this->steered1_.push_back(just_added);
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);
535 this->steered().pop_back();
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);
545 this->gf(this->goal_found(this->nodes().back()));
546 just_added = just_added->p();
551 void RRTS::set_sample_normal(
552 double mx, double dx,
553 double my, double dy,
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);
561 void RRTS::set_sample_uniform(
562 double xmin, double xmax,
563 double ymin, double ymax,
564 double hmin, double hmax
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);
571 void RRTS::set_sample_uniform_circle()
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);
577 void RRTS::set_sample(
578 double x1, double x2,
579 double y1, double y2,
583 switch (this->sample_dist_type()) {
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);
591 case 2: // uniform circle
592 this->set_sample_uniform_circle();
594 case 3: // uniform index of node in nodes
595 this->set_sample_uniform_circle();
598 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
602 Json::Value RRTS::json()
606 jvo["time"] = this->scnt();
609 jvo["iterations"] = this->icnt();
612 jvo["init"][0] = this->nodes().front().x();
613 jvo["init"][1] = this->nodes().front().y();
614 jvo["init"][2] = this->nodes().front().h();
617 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
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;
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;
637 jvo["goal"][0] = this->goals().back().x();
638 jvo["goal"][1] = this->goals().back().y();
639 jvo["goal"][2] = this->goals().back().h();
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))
652 if (n->t(RRTNodeType::connected))
656 jvo["cusps-in-path"] = cu;
657 jvo["connecteds-in-path"] = co;
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();
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);
681 jvo["nodes"] = (unsigned int) this->nodes().size();
684 unsigned int cnt = 0;
685 for (auto i: this->log_path_cost_)
686 jvo["log_path_cost"][cnt++] = i;
689 unsigned int cnt = 0;
690 for (auto i: this->log_opt_time_)
691 jvo["log_opt_time"][cnt++] = i;
694 // unsigned int ncnt = 0;
695 // for (auto n: this->nodes()) {
696 // jvo["nodes_x"][ncnt] = n.x();
697 // jvo["nodes_y"][ncnt] = n.y();
698 // //jvo["nodes_h"][ncnt] = n.h();
703 // unsigned int ncnt = 0;
704 // for (auto n: this->steered1_) {
705 // jvo["steered1_x"][ncnt] = n->x();
706 // jvo["steered1_y"][ncnt] = n->y();
707 // //jvo["nodes_h"][ncnt] = n.h();
711 // for (auto n: this->steered2_) {
712 // jvo["steered2_x"][ncnt] = n->x();
713 // jvo["steered2_y"][ncnt] = n->y();
714 // //jvo["nodes_h"][ncnt] = n.h();
721 void RRTS::json(Json::Value jvi)
723 assert(jvi["init"] != Json::nullValue);
724 assert(jvi["goals"] != Json::nullValue);
725 assert(jvi["obst"] != Json::nullValue);
727 this->nodes().front().x(jvi["init"][0].asDouble());
728 this->nodes().front().y(jvi["init"][1].asDouble());
729 this->nodes().front().h(jvi["init"][2].asDouble());
731 RRTNode* gp = nullptr;
732 if (jvi["entry"] != Json::nullValue) {
733 this->entry_set = true;
734 this->entry.x = jvi["entry"][0].asDouble();
735 this->entry.y = jvi["entry"][1].asDouble();
736 this->entry.b = jvi["entry"][2].asDouble();
737 this->entry.e = jvi["entry"][3].asDouble();
739 tmp_node.x(this->entry.x);
740 tmp_node.y(this->entry.y);
741 tmp_node.h((this->entry.b + this->entry.e) / 2.0);
742 this->goals().push_back(tmp_node);
743 this->goals().back().p(gp);
744 gp = &this->goals().back();
746 if (jvi["entries"] != Json::nullValue) {
747 this->entries_set = true;
748 this->entry1.x = jvi["entries"][0][0].asDouble();
749 this->entry1.y = jvi["entries"][0][1].asDouble();
750 this->entry1.h = jvi["entries"][0][2].asDouble();
751 this->entry2.x = jvi["entries"][1][0].asDouble();
752 this->entry2.y = jvi["entries"][1][1].asDouble();
753 this->entry2.h = jvi["entries"][1][2].asDouble();
755 for (auto g: jvi["goals"]) {
757 tmp_node.x(g[0].asDouble());
758 tmp_node.y(g[1].asDouble());
759 tmp_node.h(g[2].asDouble());
760 this->goals().push_back(tmp_node);
761 this->goals().back().p(gp);
762 gp = &this->goals().back();
764 this->goals().front().set_t(RRTNodeType::cusp);
765 this->goals().back().set_t(RRTNodeType::cusp);
768 Obstacle tmp_obstacle;
769 for (auto o: jvi["obst"]) {
770 tmp_obstacle.poly().clear();
772 double tmp_x = c[0].asDouble();
773 double tmp_y = c[1].asDouble();
774 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
775 tmp_obstacle.poly().push_back(tmp_tuple);
777 this->obstacles().push_back(tmp_obstacle);
781 double edist_init_goal = sqrt(
783 this->nodes().front().x()
784 - this->goals().front().x(),
788 this->nodes().front().y()
789 - this->goals().front().y(),
794 this->nodes().front().x(), edist_init_goal,
795 this->nodes().front().y(), edist_init_goal,
802 : gen_(std::random_device{}())
804 this->goals().reserve(100);
805 this->nodes().reserve(4000000);
806 this->samples().reserve(1000);
807 this->steered().reserve(20000);
808 this->store_node(RRTNode()); // root
811 double cc(RRTNode &t)
815 while (n != nullptr) {