4 #include "reeds_shepp.h"
6 template <typename T> int sgn(T val) {
7 return (T(0) < val) - (val < T(0));
10 double edist(RRTNode const& n1, RRTNode const& n2)
12 auto dx = n2.x() - n1.x();
13 auto dy = n2.y() - n1.y();
14 return sqrt(dx*dx + dy*dy);
16 double edist(RRTNode const* n1, RRTNode const* n2)
18 return edist(*n1, *n2);
25 RRTNode::RRTNode(const BicycleCar &bc)
34 bool RRTNode::operator==(const RRTNode& n)
45 double RRTS::elapsed()
47 std::chrono::duration<double> dt;
48 dt = std::chrono::duration_cast<std::chrono::duration<double>>(
49 std::chrono::high_resolution_clock::now()
52 this->scnt_ = dt.count();
56 void RRTS::log_path_cost()
58 this->log_path_cost_.push_back(cc(this->goals().front()));
59 this->log_path_time_ += 0.1;
62 bool RRTS::should_stop()
64 // the following counters must be updated, do not comment
67 // current iteration stop conditions
68 if (this->should_finish()) return true;
69 if (this->should_break()) return true;
70 // but continue by default
74 bool RRTS::should_finish()
76 // decide finish conditions (maybe comment some lines)
77 //if (this->icnt_ > 999) return true;
78 if (this->scnt_ > 2) return true;
79 if (this->finishit) return true;
80 //if (this->gf()) return true;
81 // but continue by default
85 bool RRTS::should_break()
87 // decide break conditions (maybe comment some lines)
88 //if (this->scnt_ - this->pcnt_ > 2) return true;
89 // but continue by default
93 bool RRTS::should_continue()
95 // decide the stop conditions (maybe comment some lines)
96 // it is exact opposite of `should_stop`
97 //if (this->icnt_ > 999) return false;
98 if (this->scnt_ > 10) return false;
99 if (this->gf()) return false;
100 // and reset pause counter if should continue
101 this->pcnt_ = this->scnt_;
105 void RRTS::store_node(RRTNode n)
107 this->nodes().push_back(n);
111 std::tuple<bool, unsigned int, unsigned int>
112 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
114 for (auto &o: this->obstacles())
115 if (std::get<0>(::collide(poly, o.poly())))
116 return ::collide(poly, o.poly());
117 return std::make_tuple(false, 0, 0);
120 std::tuple<bool, unsigned int, unsigned int>
121 RRTS::collide_steered_from(RRTNode &f)
123 auto fbc = BicycleCar();
127 std::vector<std::tuple<double, double>> s;
128 s.push_back(std::make_tuple(fbc.x(), fbc.y()));
129 for (auto &n: this->steered()) {
130 auto nbc = BicycleCar();
134 s.push_back(std::make_tuple(nbc.lfx(), nbc.lfy()));
135 s.push_back(std::make_tuple(nbc.lrx(), nbc.lry()));
136 s.push_back(std::make_tuple(nbc.rrx(), nbc.rry()));
137 s.push_back(std::make_tuple(nbc.rfx(), nbc.rfy()));
139 auto col = this->collide(s);
140 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
141 if (std::get<0>(col) && strip_from > 0) {
142 while (strip_from-- > 0) {
143 this->steered().pop_back();
145 return this->collide_steered_from(f);
150 std::tuple<bool, unsigned int, unsigned int>
151 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
153 auto fbc = BicycleCar();
157 auto tbc = BicycleCar();
161 std::vector<std::tuple<double, double>> p;
162 p.push_back(std::make_tuple(fbc.lfx(), fbc.lfy()));
163 p.push_back(std::make_tuple(fbc.lrx(), fbc.lry()));
164 p.push_back(std::make_tuple(fbc.rrx(), fbc.rry()));
165 p.push_back(std::make_tuple(fbc.rfx(), fbc.rfy()));
166 p.push_back(std::make_tuple(tbc.lfx(), tbc.lfy()));
167 p.push_back(std::make_tuple(tbc.lrx(), tbc.lry()));
168 p.push_back(std::make_tuple(tbc.rrx(), tbc.rry()));
169 p.push_back(std::make_tuple(tbc.rfx(), tbc.rfy()));
170 return this->collide(p);
173 double RRTS::cost_build(RRTNode &f, RRTNode &t)
176 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
180 double RRTS::cost_search(RRTNode &f, RRTNode &t)
183 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
192 switch (this->sample_dist_type()) {
194 x = this->udx_(this->gen_);
195 y = this->udy_(this->gen_);
196 h = this->udh_(this->gen_);
198 case 2: // uniform circle
200 // see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
203 this->nodes().front().x()
204 - this->goals().front().x(),
208 this->nodes().front().y()
209 - this->goals().front().y(),
214 this->goals().front().y() - this->nodes().front().y(),
215 this->goals().front().x() - this->nodes().front().x()
217 double cx = this->goals().front().x() - R/2 * cos(a);
218 double cy = this->goals().front().y() - R/2 * sin(a);
219 double r = R * sqrt(this->udx_(this->gen_));
220 double theta = this->udy_(this->gen_) * 2 * M_PI;
221 x = cx + r * cos(theta);
222 y = cy + r * sin(theta);
223 h = this->udh_(this->gen_);
228 this->steered1_.size() == 0
229 && this->steered2_.size() == 0
231 x = this->nodes().front().x();
232 y = this->nodes().front().y();
233 h = this->nodes().front().h();
235 this->udi1_ = std::uniform_int_distribution<unsigned int>(
237 this->steered1_.size() - 1
239 this->udi2_ = std::uniform_int_distribution<unsigned int>(
241 this->steered2_.size() - 1
243 auto ind1 = this->udi1_(this->gen_);
244 auto ind2 = this->udi2_(this->gen_);
246 this->steered2_.size() == 0
248 auto n1 = this->steered1_[ind1];
253 this->steered1_.size() == 0
255 auto n2 = this->steered2_[ind2];
260 auto n1 = this->steered1_[ind1];
261 auto n2 = this->steered2_[ind2];
262 auto which = this->udx_(this->gen_);
277 x = this->ndx_(this->gen_);
278 y = this->ndy_(this->gen_);
279 h = this->ndh_(this->gen_);
281 this->samples().push_back(RRTNode());
282 this->samples().back().x(x);
283 this->samples().back().y(y);
284 this->samples().back().h(h);
287 RRTNode *RRTS::nn(RRTNode &t)
289 RRTNode *nn = &this->nodes().front();
290 double cost = this->cost_search(*nn, t);
291 for (auto &f: this->nodes()) {
292 if (this->cost_search(f, t) < cost) {
294 cost = this->cost_search(f, t);
300 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
302 std::vector<RRTNode *> nv;
303 double cost = std::min(GAMMA(this->nodes().size()), ETA);
304 for (auto &f: this->nodes())
305 if (this->cost_search(f, t) < cost)
310 int cb_rs_steer(double q[4], void *user_data)
312 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
313 nodes->push_back(RRTNode());
314 nodes->back().x(q[0]);
315 nodes->back().y(q[1]);
316 nodes->back().h(q[2]);
317 nodes->back().sp(q[3]);
318 if (nodes->back().sp() == 0) {
319 nodes->back().set_t(RRTNodeType::cusp);
320 } else if (nodes->size() >= 2) {
321 RRTNode* lln = nodes->back().p();
322 RRTNode* ln = &nodes->back();
323 if (lln != nullptr && ln != nullptr && sgn(lln->sp()) != sgn(ln->sp()))
324 ln->set_t(RRTNodeType::cusp);
329 void RRTS::steer(RRTNode &f, RRTNode &t)
331 this->steered().clear();
332 double q0[] = {f.x(), f.y(), f.h()};
333 double q1[] = {t.x(), t.y(), t.h()};
334 ReedsSheppStateSpace rsss(this->bc.mtr());
335 rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->steered());
337 void RRTS::tmp_steer(RRTNode &f, RRTNode &t)
339 this->tmp_steered_.clear();
340 double q0[] = {f.x(), f.y(), f.h()};
341 double q1[] = {t.x(), t.y(), t.h()};
342 ReedsSheppStateSpace rsss(this->bc.mtr());
343 rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->tmp_steered_);
346 void RRTS::steer1(RRTNode &f, RRTNode &t)
348 return this->steer(f, t);
351 void RRTS::steer2(RRTNode &f, RRTNode &t)
353 return this->steer(f, t);
356 void RRTS::join_steered(RRTNode *f)
358 while (this->steered().size() > 0) {
359 this->store_node(this->steered().front());
360 RRTNode *t = &this->nodes().back();
362 t->c(this->cost_build(*f, *t));
363 this->steered().erase(this->steered().begin());
367 void RRTS::join_tmp_steered(RRTNode *f)
369 while (this->tmp_steered_.size() > 0) {
370 this->store_node(this->tmp_steered_.front());
371 RRTNode *t = &this->nodes().back();
373 t->c(this->cost_build(*f, *t));
374 this->tmp_steered_.erase(this->tmp_steered_.begin());
379 bool RRTS::goal_found(RRTNode &f)
381 auto &g = this->goals().front();
382 double cost = this->cost_build(f, g);
384 pow(f.x() - g.x(), 2)
385 + pow(f.y() - g.y(), 2)
387 double adist = std::abs(f.h() - g.h());
388 if (edist < 0.05 && adist < M_PI / 32) {
389 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
401 RRTNode *t = &this->steered().front();
402 RRTNode *f = this->nn(this->samples().back());
403 double cost = this->cost_search(*f, *t);
404 for (auto n: this->nv(*t)) {
406 !std::get<0>(this->collide_two_nodes(*n, *t))
407 && this->cost_search(*n, *t) < cost
410 cost = this->cost_search(*n, *t);
413 // steer from f->t and then continue with the steered.
414 this->tmp_steer(*f, *t);
415 if (this->tmp_steered_.size() > 0) {
416 auto col = this->collide_tmp_steered_from(*f);
417 if (std::get<0>(col))
419 this->join_tmp_steered(f);
420 f = &this->nodes().back();
422 if (edist(f, t) > 0.2)
425 this->store_node(this->steered().front());
426 t = &this->nodes().back();
428 t->c(this->cost_build(*f, *t));
429 t->set_t(RRTNodeType::connected);
435 RRTNode *f = &this->nodes().back();
436 for (auto n: this->nv(*f)) {
438 !std::get<0>(this->collide_two_nodes(*f, *n))
439 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
442 n->c(this->cost_build(*f, *n));
454 this->nodes().clear();
455 this->samples().clear();
456 this->steered().clear();
457 this->store_node(RRTNode()); // root
464 std::vector<RRTNode *> RRTS::path()
466 std::vector<RRTNode *> path;
467 if (this->goals().size() == 0)
469 RRTNode *goal = &this->goals().back();
470 if (goal->p() == nullptr)
472 while (goal != nullptr) {
473 path.push_back(goal);
476 std::reverse(path.begin(), path.end());
482 if (this->icnt_ == 0)
483 this->tstart_ = std::chrono::high_resolution_clock::now();
485 if (this->scnt_ > this->log_path_time_)
486 this->log_path_cost();
487 if (this->should_stop())
489 if (this->samples().size() == 0) {
490 this->samples().push_back(RRTNode());
491 this->samples().back().x(this->goals().front().x());
492 this->samples().back().y(this->goals().front().y());
493 this->samples().back().h(this->goals().front().h());
498 *this->nn(this->samples().back()),
499 this->samples().back()
501 if (this->steered().size() == 0)
503 auto col = this->collide_steered_from(
504 *this->nn(this->samples().back())
506 if (std::get<0>(col)) {
507 auto rcnt = this->steered().size() - std::get<1>(col);
509 this->steered().pop_back();
512 if (!this->connect())
515 unsigned scnt = this->steered().size();
516 this->join_steered(&this->nodes().back());
517 RRTNode *just_added = &this->nodes().back();
519 // store all the steered1 nodes
520 this->steered1_.push_back(just_added);
522 auto &g = this->goals().front();
523 this->steer2(*just_added, g);
524 auto col = this->collide_steered_from(*just_added);
525 if (std::get<0>(col)) {
526 auto rcnt = this->steered().size() - std::get<1>(col);
528 this->steered().pop_back();
531 this->join_steered(just_added);
532 // store all the steered2 nodes
533 RRTNode* jap = &this->nodes().back();
534 while (jap != just_added) {
535 this->steered2_.push_back(jap);
538 this->gf(this->goal_found(this->nodes().back()));
539 just_added = just_added->p();
544 void RRTS::set_sample_normal(
545 double mx, double dx,
546 double my, double dy,
550 this->ndx_ = std::normal_distribution<double>(mx, dx);
551 this->ndy_ = std::normal_distribution<double>(my, dy);
552 this->ndh_ = std::normal_distribution<double>(mh, dh);
554 void RRTS::set_sample_uniform(
555 double xmin, double xmax,
556 double ymin, double ymax,
557 double hmin, double hmax
560 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
561 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
562 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
564 void RRTS::set_sample_uniform_circle()
566 this->udx_ = std::uniform_real_distribution<double>(0, 1);
567 this->udy_ = std::uniform_real_distribution<double>(0, 1);
568 this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
570 void RRTS::set_sample(
571 double x1, double x2,
572 double y1, double y2,
576 switch (this->sample_dist_type()) {
578 x1 += this->nodes().front().x();
579 x2 += this->nodes().front().x();
580 y1 += this->nodes().front().y();
581 y2 += this->nodes().front().y();
582 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
584 case 2: // uniform circle
585 this->set_sample_uniform_circle();
587 case 3: // uniform index of node in nodes
588 this->set_sample_uniform_circle();
591 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
595 Json::Value RRTS::json()
599 jvo["time"] = this->scnt();
602 jvo["iterations"] = this->icnt();
605 jvo["init"][0] = this->nodes().front().x();
606 jvo["init"][1] = this->nodes().front().y();
607 jvo["init"][2] = this->nodes().front().h();
610 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
613 if (this->path().size() > 0) {
614 jvo["cost"] = cc(*this->path().back());
615 jvo["entry"][0] = this->goals().front().x();
616 jvo["entry"][1] = this->goals().front().y();
617 jvo["entry"][2] = this->goals().front().h();
618 jvo["goal"][0] = this->goals().back().x();
619 jvo["goal"][1] = this->goals().back().y();
620 jvo["goal"][2] = this->goals().back().h();
626 unsigned int pcnt = 0;
627 for (auto n: this->path()) {
628 jvo["path"][pcnt][0] = n->x();
629 jvo["path"][pcnt][1] = n->y();
630 jvo["path"][pcnt][2] = n->h();
631 if (n->t(RRTNodeType::cusp))
633 if (n->t(RRTNodeType::connected))
637 jvo["cusps-in-path"] = cu;
638 jvo["connecteds-in-path"] = co;
641 unsigned int gcnt = 0;
642 for (auto g: this->goals()) {
643 jvo["goals"][gcnt][0] = g.x();
644 jvo["goals"][gcnt][1] = g.y();
645 jvo["goals"][gcnt][2] = g.h();
650 unsigned int ocnt = 0;
651 for (auto o: this->obstacles()) {
652 unsigned int ccnt = 0;
653 for (auto c: o.poly()) {
654 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
655 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
662 jvo["nodes"] = (unsigned int) this->nodes().size();
665 unsigned int cnt = 0;
666 for (auto i: this->log_path_cost_)
667 jvo["log_path_cost"][cnt++] = i;
670 // unsigned int ncnt = 0;
671 // for (auto n: this->nodes()) {
672 // jvo["nodes_x"][ncnt] = n.x();
673 // jvo["nodes_y"][ncnt] = n.y();
674 // //jvo["nodes_h"][ncnt] = n.h();
679 // unsigned int ncnt = 0;
680 // for (auto n: this->steered1_) {
681 // jvo["steered1_x"][ncnt] = n->x();
682 // jvo["steered1_y"][ncnt] = n->y();
683 // //jvo["nodes_h"][ncnt] = n.h();
687 // for (auto n: this->steered2_) {
688 // jvo["steered2_x"][ncnt] = n->x();
689 // jvo["steered2_y"][ncnt] = n->y();
690 // //jvo["nodes_h"][ncnt] = n.h();
697 void RRTS::json(Json::Value jvi)
699 assert(jvi["init"] != Json::nullValue);
700 assert(jvi["goals"] != Json::nullValue);
701 assert(jvi["obst"] != Json::nullValue);
703 this->nodes().front().x(jvi["init"][0].asDouble());
704 this->nodes().front().y(jvi["init"][1].asDouble());
705 this->nodes().front().h(jvi["init"][2].asDouble());
707 RRTNode* gp = nullptr;
708 if (jvi["entry"] != Json::nullValue) {
709 this->entry_set = true;
710 this->entry.x = jvi["entry"][0].asDouble();
711 this->entry.y = jvi["entry"][1].asDouble();
712 this->entry.b = jvi["entry"][2].asDouble();
713 this->entry.e = jvi["entry"][3].asDouble();
715 tmp_node.x(this->entry.x);
716 tmp_node.y(this->entry.y);
717 tmp_node.h((this->entry.b + this->entry.e) / 2.0);
718 this->goals().push_back(tmp_node);
719 this->goals().back().p(gp);
720 gp = &this->goals().back();
722 for (auto g: jvi["goals"]) {
724 tmp_node.x(g[0].asDouble());
725 tmp_node.y(g[1].asDouble());
726 tmp_node.h(g[2].asDouble());
727 this->goals().push_back(tmp_node);
728 this->goals().back().p(gp);
729 gp = &this->goals().back();
731 this->goals().front().set_t(RRTNodeType::cusp);
732 this->goals().back().set_t(RRTNodeType::cusp);
735 Obstacle tmp_obstacle;
736 for (auto o: jvi["obst"]) {
737 tmp_obstacle.poly().clear();
739 double tmp_x = c[0].asDouble();
740 double tmp_y = c[1].asDouble();
741 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
742 tmp_obstacle.poly().push_back(tmp_tuple);
744 this->obstacles().push_back(tmp_obstacle);
748 double edist_init_goal = sqrt(
750 this->nodes().front().x()
751 - this->goals().front().x(),
755 this->nodes().front().y()
756 - this->goals().front().y(),
761 this->nodes().front().x(), edist_init_goal,
762 this->nodes().front().y(), edist_init_goal,
769 : gen_(std::random_device{}())
771 this->goals().reserve(100);
772 this->nodes().reserve(4000000);
773 this->samples().reserve(1000);
774 this->steered().reserve(20000);
775 this->store_node(RRTNode()); // root
778 double cc(RRTNode &t)
782 while (n != nullptr) {