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_time_ += 0.1;
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_ > 999) 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();
422 pow(f->x() - t->x(), 2)
423 + pow(f->y() - t->y(), 2)
427 this->store_node(this->steered().front());
428 t = &this->nodes().back();
430 t->c(this->cost_build(*f, *t));
431 t->set_t(RRTNodeType::connected);
437 RRTNode *f = &this->nodes().back();
438 for (auto n: this->nv(*f)) {
440 !std::get<0>(this->collide_two_nodes(*f, *n))
441 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
444 n->c(this->cost_build(*f, *n));
456 this->nodes().clear();
457 this->samples().clear();
458 this->steered().clear();
459 this->store_node(RRTNode()); // root
466 std::vector<RRTNode *> RRTS::path()
468 std::vector<RRTNode *> path;
469 if (this->goals().size() == 0)
471 RRTNode *goal = &this->goals().back();
472 if (goal->p() == nullptr)
474 while (goal != nullptr) {
475 path.push_back(goal);
478 std::reverse(path.begin(), path.end());
484 if (this->icnt_ == 0)
485 this->tstart_ = std::chrono::high_resolution_clock::now();
487 if (this->scnt_ > this->log_path_time_)
488 this->log_path_cost();
489 if (this->should_stop())
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());
500 *this->nn(this->samples().back()),
501 this->samples().back()
503 if (this->steered().size() == 0)
505 auto col = this->collide_steered_from(
506 *this->nn(this->samples().back())
508 if (std::get<0>(col)) {
509 auto rcnt = this->steered().size() - std::get<1>(col);
511 this->steered().pop_back();
514 if (!this->connect())
517 unsigned scnt = this->steered().size();
518 this->join_steered(&this->nodes().back());
519 RRTNode *just_added = &this->nodes().back();
521 // store all the steered1 nodes
522 this->steered1_.push_back(just_added);
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);
530 this->steered().pop_back();
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);
540 this->gf(this->goal_found(this->nodes().back()));
541 just_added = just_added->p();
546 void RRTS::set_sample_normal(
547 double mx, double dx,
548 double my, double dy,
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);
556 void RRTS::set_sample_uniform(
557 double xmin, double xmax,
558 double ymin, double ymax,
559 double hmin, double hmax
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);
566 void RRTS::set_sample_uniform_circle()
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);
572 void RRTS::set_sample(
573 double x1, double x2,
574 double y1, double y2,
578 switch (this->sample_dist_type()) {
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);
586 case 2: // uniform circle
587 this->set_sample_uniform_circle();
589 case 3: // uniform index of node in nodes
590 this->set_sample_uniform_circle();
593 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
597 Json::Value RRTS::json()
601 jvo["time"] = this->scnt();
604 jvo["iterations"] = this->icnt();
607 jvo["init"][0] = this->nodes().front().x();
608 jvo["init"][1] = this->nodes().front().y();
609 jvo["init"][2] = this->nodes().front().h();
612 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
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;
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;
632 jvo["goal"][0] = this->goals().back().x();
633 jvo["goal"][1] = this->goals().back().y();
634 jvo["goal"][2] = this->goals().back().h();
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))
647 if (n->t(RRTNodeType::connected))
651 jvo["cusps-in-path"] = cu;
652 jvo["connecteds-in-path"] = co;
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();
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);
676 jvo["nodes"] = (unsigned int) this->nodes().size();
679 unsigned int cnt = 0;
680 for (auto i: this->log_path_cost_)
681 jvo["log_path_cost"][cnt++] = i;
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();
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();
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();
711 void RRTS::json(Json::Value jvi)
713 assert(jvi["init"] != Json::nullValue);
714 assert(jvi["goals"] != Json::nullValue);
715 assert(jvi["obst"] != Json::nullValue);
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());
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();
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();
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();
745 for (auto g: jvi["goals"]) {
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();
754 this->goals().front().set_t(RRTNodeType::cusp);
755 this->goals().back().set_t(RRTNodeType::cusp);
758 Obstacle tmp_obstacle;
759 for (auto o: jvi["obst"]) {
760 tmp_obstacle.poly().clear();
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);
767 this->obstacles().push_back(tmp_obstacle);
771 double edist_init_goal = sqrt(
773 this->nodes().front().x()
774 - this->goals().front().x(),
778 this->nodes().front().y()
779 - this->goals().front().y(),
784 this->nodes().front().x(), edist_init_goal,
785 this->nodes().front().y(), edist_init_goal,
792 : gen_(std::random_device{}())
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
801 double cc(RRTNode &t)
805 while (n != nullptr) {