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(this->goals().front().cc);
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 || f.cc + cost < g.cc) {
400 RRTNode *t = &this->steered().front();
401 RRTNode *f = this->nn(this->samples().back());
402 double cost = f->cc + this->cost_build(*f, *t);
403 for (auto n: this->nv(*t)) {
405 !std::get<0>(this->collide_two_nodes(*n, *t))
406 && n->cc + this->cost_build(*n, *t) < cost
409 cost = n->cc + this->cost_build(*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 && f->cc + this->cost_build(*f, *n) < n->cc
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))
453 this->join_tmp_steered(f);
454 f = &this->nodes().back();
457 n->c(this->cost_build(*f, *n));
469 this->nodes().clear();
470 this->samples().clear();
471 this->steered().clear();
472 this->store_node(RRTNode()); // root
479 std::vector<RRTNode *> RRTS::path()
481 std::vector<RRTNode *> path;
482 if (this->goals().size() == 0)
484 RRTNode *goal = &this->goals().back();
485 if (goal->p() == nullptr)
487 while (goal != nullptr) {
488 path.push_back(goal);
491 std::reverse(path.begin(), path.end());
497 if (this->icnt_ == 0)
498 this->tstart_ = std::chrono::high_resolution_clock::now();
500 if (this->icnt_ > this->log_path_iter_)
501 this->log_path_cost();
502 if (this->should_stop())
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());
513 *this->nn(this->samples().back()),
514 this->samples().back()
516 if (this->steered().size() == 0)
518 auto col = this->collide_steered_from(
519 *this->nn(this->samples().back())
521 if (std::get<0>(col)) {
522 auto rcnt = this->steered().size() - std::get<1>(col);
524 this->steered().pop_back();
527 if (!this->connect())
530 unsigned scnt = this->steered().size();
531 this->join_steered(&this->nodes().back());
532 RRTNode *just_added = &this->nodes().back();
534 // store all the steered1 nodes
535 this->steered1_.push_back(just_added);
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);
543 this->steered().pop_back();
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);
553 this->gf(this->goal_found(this->nodes().back()));
554 just_added = just_added->p();
559 void RRTS::set_sample_normal(
560 double mx, double dx,
561 double my, double dy,
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);
569 void RRTS::set_sample_uniform(
570 double xmin, double xmax,
571 double ymin, double ymax,
572 double hmin, double hmax
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);
579 void RRTS::set_sample_uniform_circle()
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);
585 void RRTS::set_sample(
586 double x1, double x2,
587 double y1, double y2,
591 switch (this->sample_dist_type()) {
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);
599 case 2: // uniform circle
600 this->set_sample_uniform_circle();
602 case 3: // uniform index of node in nodes
603 this->set_sample_uniform_circle();
606 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
610 Json::Value RRTS::json()
614 jvo["time"] = this->scnt();
617 jvo["iterations"] = this->icnt();
620 jvo["init"][0] = this->nodes().front().x();
621 jvo["init"][1] = this->nodes().front().y();
622 jvo["init"][2] = this->nodes().front().h();
625 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
628 if (this->path().size() > 0) {
629 jvo["cost"] = this->path().back()->cc;
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;
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;
645 jvo["goal"][0] = this->goals().back().x();
646 jvo["goal"][1] = this->goals().back().y();
647 jvo["goal"][2] = this->goals().back().h();
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))
660 if (n->t(RRTNodeType::connected))
664 jvo["cusps-in-path"] = cu;
665 jvo["connecteds-in-path"] = co;
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();
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);
689 jvo["nodes"] = (unsigned int) this->nodes().size();
692 unsigned int cnt = 0;
693 for (auto i: this->log_path_cost_)
694 jvo["log_path_cost"][cnt++] = i;
697 unsigned int cnt = 0;
698 for (auto i: this->log_opt_time_)
699 jvo["log_opt_time"][cnt++] = i;
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();
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();
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();
729 void RRTS::json(Json::Value jvi)
731 assert(jvi["init"] != Json::nullValue);
732 assert(jvi["goals"] != Json::nullValue);
733 assert(jvi["obst"] != Json::nullValue);
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());
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();
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();
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();
763 for (auto g: jvi["goals"]) {
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();
772 this->goals().front().set_t(RRTNodeType::cusp);
773 this->goals().back().set_t(RRTNodeType::cusp);
776 Obstacle tmp_obstacle;
777 for (auto o: jvi["obst"]) {
778 tmp_obstacle.poly().clear();
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);
785 this->obstacles().push_back(tmp_obstacle);
789 double edist_init_goal = sqrt(
791 this->nodes().front().x()
792 - this->goals().front().x(),
796 this->nodes().front().y()
797 - this->goals().front().y(),
802 this->nodes().front().x(), edist_init_goal,
803 this->nodes().front().y(), edist_init_goal,
810 : gen_(std::random_device{}())
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
819 double cc(RRTNode &t)
823 while (n != nullptr) {