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 if (this->log_path_cost_.size() == 0) {
48 this->log_path_cost_.push_back(this->goals().front().cc);
50 auto lc = this->log_path_cost_.back();
51 auto gc = this->goals().front().cc;
52 auto goal_is_better = this->goals().front().cc > 0 && lc < gc;
54 this->log_path_cost_.back() > 0
56 this->goals().front().cc == 0
58 this->goals().front().cc > 0
63 this->log_path_cost_.push_back(
64 this->log_path_cost_.back()
67 this->log_path_cost_.push_back(
68 this->goals().front().cc
72 this->log_path_iter_ += 1;
75 bool RRTS::should_stop()
77 // the following counters must be updated, do not comment
80 // current iteration stop conditions
81 if (this->should_finish()) return true;
82 if (this->should_break()) return true;
83 // but continue by default
87 bool RRTS::should_finish()
89 // decide finish conditions (maybe comment some lines)
90 if (this->icnt_ > 1000) return true;
91 //if (this->scnt_ > 2) return true;
92 if (this->finishit) return true;
93 //if (this->gf()) return true;
94 // but continue by default
98 bool RRTS::should_break()
100 // decide break conditions (maybe comment some lines)
101 //if (this->scnt_ - this->pcnt_ > 2) return true;
102 // but continue by default
106 bool RRTS::should_continue()
108 // decide the stop conditions (maybe comment some lines)
109 // it is exact opposite of `should_stop`
110 //if (this->icnt_ > 999) return false;
111 if (this->scnt_ > 10) return false;
112 if (this->gf()) return false;
113 // and reset pause counter if should continue
114 this->pcnt_ = this->scnt_;
118 void RRTS::store_node(RRTNode n)
120 this->nodes().push_back(n);
124 std::tuple<bool, unsigned int, unsigned int>
125 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
127 for (auto &o: this->obstacles())
128 if (std::get<0>(::collide(poly, o.poly())))
129 return ::collide(poly, o.poly());
130 return std::make_tuple(false, 0, 0);
133 std::tuple<bool, unsigned int, unsigned int>
134 RRTS::collide_steered_from(RRTNode &f)
136 auto fbc = BicycleCar();
140 std::vector<std::tuple<double, double>> s;
141 s.push_back(std::make_tuple(fbc.x(), fbc.y()));
142 for (auto &n: this->steered()) {
143 auto nbc = BicycleCar();
147 s.push_back(std::make_tuple(nbc.lfx(), nbc.lfy()));
148 s.push_back(std::make_tuple(nbc.lrx(), nbc.lry()));
149 s.push_back(std::make_tuple(nbc.rrx(), nbc.rry()));
150 s.push_back(std::make_tuple(nbc.rfx(), nbc.rfy()));
152 auto col = this->collide(s);
153 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
154 if (std::get<0>(col) && strip_from > 0) {
155 while (strip_from-- > 0) {
156 this->steered().pop_back();
158 return this->collide_steered_from(f);
162 std::tuple<bool, unsigned int, unsigned int>
163 RRTS::collide_tmp_steered_from(RRTNode &f)
165 return std::make_tuple(false, 0, 0);
168 std::tuple<bool, unsigned int, unsigned int>
169 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
171 auto fbc = BicycleCar();
175 auto tbc = BicycleCar();
179 std::vector<std::tuple<double, double>> p;
180 p.push_back(std::make_tuple(fbc.lfx(), fbc.lfy()));
181 p.push_back(std::make_tuple(fbc.lrx(), fbc.lry()));
182 p.push_back(std::make_tuple(fbc.rrx(), fbc.rry()));
183 p.push_back(std::make_tuple(fbc.rfx(), fbc.rfy()));
184 p.push_back(std::make_tuple(tbc.lfx(), tbc.lfy()));
185 p.push_back(std::make_tuple(tbc.lrx(), tbc.lry()));
186 p.push_back(std::make_tuple(tbc.rrx(), tbc.rry()));
187 p.push_back(std::make_tuple(tbc.rfx(), tbc.rfy()));
188 return this->collide(p);
191 double RRTS::cost_build(RRTNode &f, RRTNode &t)
194 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
198 double RRTS::cost_search(RRTNode &f, RRTNode &t)
201 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
210 switch (this->sample_dist_type()) {
212 x = this->udx_(this->gen_);
213 y = this->udy_(this->gen_);
214 h = this->udh_(this->gen_);
216 case 2: // uniform circle
218 // see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
221 this->nodes().front().x()
222 - this->goals().front().x(),
226 this->nodes().front().y()
227 - this->goals().front().y(),
232 this->goals().front().y() - this->nodes().front().y(),
233 this->goals().front().x() - this->nodes().front().x()
235 double cx = this->goals().front().x() - R/2 * cos(a);
236 double cy = this->goals().front().y() - R/2 * sin(a);
237 double r = R * sqrt(this->udx_(this->gen_));
238 double theta = this->udy_(this->gen_) * 2 * M_PI;
239 x = cx + r * cos(theta);
240 y = cy + r * sin(theta);
241 h = this->udh_(this->gen_);
246 this->steered1_.size() == 0
247 && this->steered2_.size() == 0
249 x = this->nodes().front().x();
250 y = this->nodes().front().y();
251 h = this->nodes().front().h();
252 this->use_nn = &this->nodes().front();
254 this->udi1_ = std::uniform_int_distribution<unsigned int>(
256 this->steered1_.size() - 1
258 this->udi2_ = std::uniform_int_distribution<unsigned int>(
260 this->steered2_.size() - 1
262 auto ind1 = this->udi1_(this->gen_);
263 auto ind2 = this->udi2_(this->gen_);
265 this->steered2_.size() == 0
267 auto n1 = this->steered1_[ind1];
271 this->use_nn = this->steered1_[ind1];
273 this->steered1_.size() == 0
275 auto n2 = this->steered2_[ind2];
279 this->use_nn = this->steered2_[ind2];
281 auto n1 = this->steered1_[ind1];
282 auto n2 = this->steered2_[ind2];
283 auto which = this->udx_(this->gen_);
288 this->use_nn = this->steered1_[ind1];
293 this->use_nn = this->steered2_[ind2];
300 x = this->ndx_(this->gen_);
301 y = this->ndy_(this->gen_);
302 h = this->ndh_(this->gen_);
304 this->samples().push_back(RRTNode());
305 this->samples().back().x(x);
306 this->samples().back().y(y);
307 this->samples().back().h(h);
310 RRTNode *RRTS::nn(RRTNode &t)
312 RRTNode *nn = &this->nodes().front();
313 double cost = this->cost_search(*nn, t);
314 for (auto &f: this->nodes()) {
315 if (this->cost_search(f, t) < cost) {
317 cost = this->cost_search(f, t);
323 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
325 std::vector<RRTNode *> nv;
326 double cost = std::min(GAMMA(this->nodes().size()), ETA);
327 for (auto &f: this->nodes())
328 if (this->cost_search(f, t) < cost)
333 int cb_rs_steer(double q[4], void *user_data)
335 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
336 nodes->push_back(RRTNode());
337 nodes->back().x(q[0]);
338 nodes->back().y(q[1]);
339 nodes->back().h(q[2]);
340 nodes->back().sp(q[3]);
341 if (nodes->back().sp() == 0) {
342 nodes->back().set_t(RRTNodeType::cusp);
343 } else if (nodes->size() >= 2) {
344 RRTNode* lln = nodes->back().p();
345 RRTNode* ln = &nodes->back();
346 if (lln != nullptr && ln != nullptr && sgn(lln->sp()) != sgn(ln->sp()))
347 ln->set_t(RRTNodeType::cusp);
352 void RRTS::steer(RRTNode &f, RRTNode &t)
354 this->steered().clear();
355 double q0[] = {f.x(), f.y(), f.h()};
356 double q1[] = {t.x(), t.y(), t.h()};
357 ReedsSheppStateSpace rsss(this->bc.mtr());
358 rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->steered());
360 void RRTS::tmp_steer(RRTNode &f, RRTNode &t)
362 this->tmp_steered_.clear();
363 double q0[] = {f.x(), f.y(), f.h()};
364 double q1[] = {t.x(), t.y(), t.h()};
365 ReedsSheppStateSpace rsss(this->bc.mtr());
366 rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->tmp_steered_);
369 void RRTS::steer1(RRTNode &f, RRTNode &t)
371 return this->steer(f, t);
374 void RRTS::steer2(RRTNode &f, RRTNode &t)
376 return this->steer(f, t);
379 void RRTS::join_steered(RRTNode *f)
381 while (this->steered().size() > 0) {
382 this->store_node(this->steered().front());
383 RRTNode *t = &this->nodes().back();
385 t->c(this->cost_build(*f, *t));
386 this->steered().erase(this->steered().begin());
390 void RRTS::join_tmp_steered(RRTNode *f)
392 while (this->tmp_steered_.size() > 0) {
393 this->store_node(this->tmp_steered_.front());
394 RRTNode *t = &this->nodes().back();
396 t->c(this->cost_build(*f, *t));
397 this->tmp_steered_.erase(this->tmp_steered_.begin());
402 bool RRTS::goal_found(RRTNode &f)
404 auto &g = this->goals().front();
405 double cost = this->cost_build(f, g);
407 pow(f.x() - g.x(), 2)
408 + pow(f.y() - g.y(), 2)
410 double adist = std::abs(f.h() - g.h());
411 if (edist < 0.05 && adist < M_PI / 32) {
412 if (g.p() == nullptr || f.cc + cost < g.cc) {
424 RRTNode *t = &this->steered().front();
425 RRTNode *f = this->nn(this->samples().back());
426 double cost = f->cc + this->cost_build(*f, *t);
427 for (auto n: this->nv(*t)) {
429 !std::get<0>(this->collide_two_nodes(*n, *t))
430 && n->cc + this->cost_build(*n, *t) < cost
433 cost = n->cc + this->cost_build(*n, *t);
436 // steer from f->t and then continue with the steered.
437 this->tmp_steer(*f, *t);
438 if (this->tmp_steered_.size() > 0) {
439 auto col = this->collide_tmp_steered_from(*f);
440 if (std::get<0>(col))
442 this->join_tmp_steered(f);
443 f = &this->nodes().back();
445 auto fbc = BicycleCar();
449 auto tbc = BicycleCar();
453 if (!tbc.drivable(fbc))
456 this->store_node(this->steered().front());
457 t = &this->nodes().back();
459 t->c(this->cost_build(*f, *t));
460 t->set_t(RRTNodeType::connected);
466 RRTNode *f = &this->nodes().back();
467 for (auto n: this->nv(*f)) {
469 !std::get<0>(this->collide_two_nodes(*f, *n))
470 && f->cc + this->cost_build(*f, *n) < n->cc
472 this->tmp_steer(*f, *n);
473 if (this->tmp_steered_.size() > 0) {
474 auto col = this->collide_tmp_steered_from(*f);
475 if (std::get<0>(col))
477 this->join_tmp_steered(f);
478 f = &this->nodes().back();
481 n->c(this->cost_build(*f, *n));
493 this->nodes().clear();
494 this->samples().clear();
495 this->steered().clear();
496 this->store_node(RRTNode()); // root
503 void RRTS::compute_path()
505 if (this->goals().size() == 0)
507 RRTNode *goal = &this->goals().front();
508 if (goal->p() == nullptr)
511 while (goal != nullptr) {
512 this->path_.push_back(goal);
515 std::reverse(this->path_.begin(), this->path_.end());
520 if (this->icnt_ == 0)
521 this->tstart_ = std::chrono::high_resolution_clock::now();
523 if (this->should_stop()) {
524 this->log_path_cost();
527 if (this->samples().size() == 0) {
528 this->samples().push_back(RRTNode());
529 this->samples().back().x(this->goals().front().x());
530 this->samples().back().y(this->goals().front().y());
531 this->samples().back().h(this->goals().front().h());
536 *this->nn(this->samples().back()),
537 this->samples().back()
539 if (this->steered().size() == 0) {
540 this->log_path_cost();
543 auto col = this->collide_steered_from(
544 *this->nn(this->samples().back())
546 if (std::get<0>(col)) {
547 auto rcnt = this->steered().size() - std::get<1>(col);
549 this->steered().pop_back();
552 if (!this->connect()) {
553 this->log_path_cost();
557 unsigned scnt = this->steered().size();
558 this->join_steered(&this->nodes().back());
559 RRTNode *just_added = &this->nodes().back();
561 // store all the steered1 nodes
562 this->steered1_.push_back(just_added);
564 auto &g = this->goals().front();
565 this->steer2(*just_added, g);
566 auto col = this->collide_steered_from(*just_added);
567 if (std::get<0>(col)) {
568 auto rcnt = this->steered().size() - std::get<1>(col);
570 this->steered().pop_back();
573 this->join_steered(just_added);
574 // store all the steered2 nodes
575 RRTNode* jap = &this->nodes().back();
576 while (jap != just_added) {
577 this->steered2_.push_back(jap);
580 auto gf = this->goal_found(this->nodes().back());
582 just_added = just_added->p();
587 this->path().size() == 0
588 || this->goals().front().cc < this->path().back()->cc
591 this->compute_path();
593 this->log_path_cost();
597 void RRTS::set_sample_normal(
598 double mx, double dx,
599 double my, double dy,
603 this->ndx_ = std::normal_distribution<double>(mx, dx);
604 this->ndy_ = std::normal_distribution<double>(my, dy);
605 this->ndh_ = std::normal_distribution<double>(mh, dh);
607 void RRTS::set_sample_uniform(
608 double xmin, double xmax,
609 double ymin, double ymax,
610 double hmin, double hmax
613 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
614 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
615 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
617 void RRTS::set_sample_uniform_circle()
619 this->udx_ = std::uniform_real_distribution<double>(0, 1);
620 this->udy_ = std::uniform_real_distribution<double>(0, 1);
621 this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
623 void RRTS::set_sample(
624 double x1, double x2,
625 double y1, double y2,
629 switch (this->sample_dist_type()) {
631 x1 += this->nodes().front().x();
632 x2 += this->nodes().front().x();
633 y1 += this->nodes().front().y();
634 y2 += this->nodes().front().y();
635 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
637 case 2: // uniform circle
638 this->set_sample_uniform_circle();
640 case 3: // uniform index of node in nodes
641 this->set_sample_uniform_circle();
644 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
648 Json::Value RRTS::json()
652 jvo["time"] = this->scnt();
655 jvo["iterations"] = this->icnt();
658 jvo["init"][0] = this->nodes().front().x();
659 jvo["init"][1] = this->nodes().front().y();
660 jvo["init"][2] = this->nodes().front().h();
663 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
666 if (this->path().size() > 0) {
667 jvo["cost"] = this->path().back()->cc;
668 jvo["entry"][0] = this->goals().front().x();
669 jvo["entry"][1] = this->goals().front().y();
670 jvo["entry"][2] = this->goals().front().h();
671 if (this->entry_set) {
672 jvo["entry"][2] = this->entry.b;
673 jvo["entry"][3] = this->entry.e;
675 if (this->entries_set) {
676 jvo["entries"][0][0] = this->entry1.x;
677 jvo["entries"][0][1] = this->entry1.y;
678 jvo["entries"][0][2] = this->entry1.h;
679 jvo["entries"][1][0] = this->entry2.x;
680 jvo["entries"][1][1] = this->entry2.y;
681 jvo["entries"][1][2] = this->entry2.h;
683 jvo["goal"][0] = this->goals().back().x();
684 jvo["goal"][1] = this->goals().back().y();
685 jvo["goal"][2] = this->goals().back().h();
691 unsigned int pcnt = 0;
692 for (auto n: this->path()) {
693 jvo["path"][pcnt][0] = n->x();
694 jvo["path"][pcnt][1] = n->y();
695 jvo["path"][pcnt][2] = n->h();
696 if (n->t(RRTNodeType::cusp))
698 if (n->t(RRTNodeType::connected))
702 jvo["cusps-in-path"] = cu;
703 jvo["connecteds-in-path"] = co;
706 unsigned int gcnt = 0;
707 for (auto g: this->goals()) {
708 jvo["goals"][gcnt][0] = g.x();
709 jvo["goals"][gcnt][1] = g.y();
710 jvo["goals"][gcnt][2] = g.h();
715 unsigned int ocnt = 0;
716 for (auto o: this->obstacles()) {
717 unsigned int ccnt = 0;
718 for (auto c: o.poly()) {
719 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
720 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
727 jvo["nodes"] = (unsigned int) this->nodes().size();
730 unsigned int cnt = 0;
731 for (auto i: this->log_path_cost_)
732 jvo["log_path_cost"][cnt++] = i;
735 unsigned int cnt = 0;
736 for (auto i: this->log_opt_time_)
737 jvo["log_opt_time"][cnt++] = i;
740 // unsigned int ncnt = 0;
741 // for (auto n: this->nodes()) {
742 // jvo["nodes_x"][ncnt] = n.x();
743 // jvo["nodes_y"][ncnt] = n.y();
744 // //jvo["nodes_h"][ncnt] = n.h();
749 // unsigned int ncnt = 0;
750 // for (auto n: this->steered1_) {
751 // jvo["steered1_x"][ncnt] = n->x();
752 // jvo["steered1_y"][ncnt] = n->y();
753 // //jvo["nodes_h"][ncnt] = n.h();
757 // for (auto n: this->steered2_) {
758 // jvo["steered2_x"][ncnt] = n->x();
759 // jvo["steered2_y"][ncnt] = n->y();
760 // //jvo["nodes_h"][ncnt] = n.h();
767 void RRTS::json(Json::Value jvi)
769 assert(jvi["init"] != Json::nullValue);
770 assert(jvi["goals"] != Json::nullValue);
771 assert(jvi["obst"] != Json::nullValue);
773 this->nodes().front().x(jvi["init"][0].asDouble());
774 this->nodes().front().y(jvi["init"][1].asDouble());
775 this->nodes().front().h(jvi["init"][2].asDouble());
777 RRTNode* gp = nullptr;
778 if (jvi["entry"] != Json::nullValue) {
779 this->entry_set = true;
780 this->entry.x = jvi["entry"][0].asDouble();
781 this->entry.y = jvi["entry"][1].asDouble();
782 this->entry.b = jvi["entry"][2].asDouble();
783 this->entry.e = jvi["entry"][3].asDouble();
785 tmp_node.x(this->entry.x);
786 tmp_node.y(this->entry.y);
787 tmp_node.h((this->entry.b + this->entry.e) / 2.0);
788 this->goals().push_back(tmp_node);
789 this->goals().back().p(gp);
790 gp = &this->goals().back();
792 if (jvi["entries"] != Json::nullValue) {
793 this->entries_set = true;
794 this->entry1.x = jvi["entries"][0][0].asDouble();
795 this->entry1.y = jvi["entries"][0][1].asDouble();
796 this->entry1.h = jvi["entries"][0][2].asDouble();
797 this->entry2.x = jvi["entries"][1][0].asDouble();
798 this->entry2.y = jvi["entries"][1][1].asDouble();
799 this->entry2.h = jvi["entries"][1][2].asDouble();
801 for (auto g: jvi["goals"]) {
803 tmp_node.x(g[0].asDouble());
804 tmp_node.y(g[1].asDouble());
805 tmp_node.h(g[2].asDouble());
806 this->goals().push_back(tmp_node);
807 this->goals().back().p(gp);
808 gp = &this->goals().back();
810 this->goals().front().set_t(RRTNodeType::cusp);
811 this->goals().back().set_t(RRTNodeType::cusp);
814 Obstacle tmp_obstacle;
815 for (auto o: jvi["obst"]) {
816 tmp_obstacle.poly().clear();
818 double tmp_x = c[0].asDouble();
819 double tmp_y = c[1].asDouble();
820 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
821 tmp_obstacle.poly().push_back(tmp_tuple);
823 this->obstacles().push_back(tmp_obstacle);
827 double edist_init_goal = sqrt(
829 this->nodes().front().x()
830 - this->goals().front().x(),
834 this->nodes().front().y()
835 - this->goals().front().y(),
840 this->nodes().front().x(), edist_init_goal,
841 this->nodes().front().y(), edist_init_goal,
848 : gen_(std::random_device{}())
850 this->goals().reserve(100);
851 this->nodes().reserve(4000000);
852 this->samples().reserve(1000);
853 this->steered().reserve(20000);
854 this->store_node(RRTNode()); // root
857 double cc(RRTNode &t)
861 while (n != nullptr) {