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);
139 std::tuple<bool, unsigned int, unsigned int>
140 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
142 auto fbc = BicycleCar();
146 auto tbc = BicycleCar();
150 std::vector<std::tuple<double, double>> p;
151 p.push_back(std::make_tuple(fbc.lfx(), fbc.lfy()));
152 p.push_back(std::make_tuple(fbc.lrx(), fbc.lry()));
153 p.push_back(std::make_tuple(fbc.rrx(), fbc.rry()));
154 p.push_back(std::make_tuple(fbc.rfx(), fbc.rfy()));
155 p.push_back(std::make_tuple(tbc.lfx(), tbc.lfy()));
156 p.push_back(std::make_tuple(tbc.lrx(), tbc.lry()));
157 p.push_back(std::make_tuple(tbc.rrx(), tbc.rry()));
158 p.push_back(std::make_tuple(tbc.rfx(), tbc.rfy()));
159 return this->collide(p);
162 double RRTS::cost_build(RRTNode &f, RRTNode &t)
165 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
169 double RRTS::cost_search(RRTNode &f, RRTNode &t)
172 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
181 switch (this->sample_dist_type()) {
183 x = this->udx_(this->gen_);
184 y = this->udy_(this->gen_);
185 h = this->udh_(this->gen_);
187 case 2: // uniform circle
189 // see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
192 this->nodes().front().x()
193 - this->goals().front().x(),
197 this->nodes().front().y()
198 - this->goals().front().y(),
203 this->goals().front().y() - this->nodes().front().y(),
204 this->goals().front().x() - this->nodes().front().x()
206 double cx = this->goals().front().x() - R/2 * cos(a);
207 double cy = this->goals().front().y() - R/2 * sin(a);
208 double r = R * sqrt(this->udx_(this->gen_));
209 double theta = this->udy_(this->gen_) * 2 * M_PI;
210 x = cx + r * cos(theta);
211 y = cy + r * sin(theta);
212 h = this->udh_(this->gen_);
217 this->steered1_.size() == 0
218 && this->steered2_.size() == 0
220 x = this->nodes().front().x();
221 y = this->nodes().front().y();
222 h = this->nodes().front().h();
224 this->udi1_ = std::uniform_int_distribution<unsigned int>(
226 this->steered1_.size() - 1
228 this->udi2_ = std::uniform_int_distribution<unsigned int>(
230 this->steered2_.size() - 1
232 auto ind1 = this->udi1_(this->gen_);
233 auto ind2 = this->udi2_(this->gen_);
235 this->steered2_.size() == 0
237 auto n1 = this->steered1_[ind1];
242 this->steered1_.size() == 0
244 auto n2 = this->steered2_[ind2];
249 auto n1 = this->steered1_[ind1];
250 auto n2 = this->steered2_[ind2];
251 auto which = this->udx_(this->gen_);
266 x = this->ndx_(this->gen_);
267 y = this->ndy_(this->gen_);
268 h = this->ndh_(this->gen_);
270 this->samples().push_back(RRTNode());
271 this->samples().back().x(x);
272 this->samples().back().y(y);
273 this->samples().back().h(h);
276 RRTNode *RRTS::nn(RRTNode &t)
278 RRTNode *nn = &this->nodes().front();
279 double cost = this->cost_search(*nn, t);
280 for (auto &f: this->nodes()) {
281 if (this->cost_search(f, t) < cost) {
283 cost = this->cost_search(f, t);
289 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
291 std::vector<RRTNode *> nv;
292 double cost = std::min(GAMMA(this->nodes().size()), ETA);
293 for (auto &f: this->nodes())
294 if (this->cost_search(f, t) < cost)
299 int cb_rs_steer(double q[4], void *user_data)
301 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
302 nodes->push_back(RRTNode());
303 nodes->back().x(q[0]);
304 nodes->back().y(q[1]);
305 nodes->back().h(q[2]);
306 nodes->back().sp(q[3]);
307 if (nodes->back().sp() == 0) {
308 nodes->back().set_t(RRTNodeType::cusp);
309 } else if (nodes->size() >= 2) {
310 RRTNode* lln = nodes->back().p();
311 RRTNode* ln = &nodes->back();
312 if (lln != nullptr && ln != nullptr && sgn(lln->sp()) != sgn(ln->sp()))
313 ln->set_t(RRTNodeType::cusp);
318 void RRTS::steer(RRTNode &f, RRTNode &t)
320 this->steered().clear();
321 double q0[] = {f.x(), f.y(), f.h()};
322 double q1[] = {t.x(), t.y(), t.h()};
323 ReedsSheppStateSpace rsss(this->bc.mtr());
324 rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->steered());
327 void RRTS::steer1(RRTNode &f, RRTNode &t)
329 return this->steer(f, t);
332 void RRTS::steer2(RRTNode &f, RRTNode &t)
334 return this->steer(f, t);
337 void RRTS::join_steered(RRTNode *f)
339 while (this->steered().size() > 0) {
340 this->store_node(this->steered().front());
341 RRTNode *t = &this->nodes().back();
343 t->c(this->cost_build(*f, *t));
344 this->steered().erase(this->steered().begin());
349 bool RRTS::goal_found(RRTNode &f)
351 auto &g = this->goals().front();
352 double cost = this->cost_build(f, g);
354 pow(f.x() - g.x(), 2)
355 + pow(f.y() - g.y(), 2)
357 double adist = std::abs(f.h() - g.h());
358 if (edist < 0.05 && adist < M_PI / 32) {
359 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
371 RRTNode *t = &this->steered().front();
372 RRTNode *f = this->nn(this->samples().back());
373 double cost = this->cost_search(*f, *t);
374 for (auto n: this->nv(*t)) {
376 !std::get<0>(this->collide_two_nodes(*n, *t))
377 && this->cost_search(*n, *t) < cost
380 cost = this->cost_search(*n, *t);
383 this->store_node(this->steered().front());
384 t = &this->nodes().back();
386 t->c(this->cost_build(*f, *t));
387 t->set_t(RRTNodeType::connected);
393 RRTNode *f = &this->nodes().back();
394 for (auto n: this->nv(*f)) {
396 !std::get<0>(this->collide_two_nodes(*f, *n))
397 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
400 n->c(this->cost_build(*f, *n));
412 this->nodes().clear();
413 this->samples().clear();
414 this->steered().clear();
415 this->store_node(RRTNode()); // root
422 std::vector<RRTNode *> RRTS::path()
424 std::vector<RRTNode *> path;
425 if (this->goals().size() == 0)
427 RRTNode *goal = &this->goals().back();
428 if (goal->p() == nullptr)
430 while (goal != nullptr) {
431 path.push_back(goal);
434 std::reverse(path.begin(), path.end());
440 if (this->icnt_ == 0)
441 this->tstart_ = std::chrono::high_resolution_clock::now();
443 if (this->scnt_ > this->log_path_time_)
444 this->log_path_cost();
445 if (this->should_stop())
447 if (this->samples().size() == 0) {
448 this->samples().push_back(RRTNode());
449 this->samples().back().x(this->goals().front().x());
450 this->samples().back().y(this->goals().front().y());
451 this->samples().back().h(this->goals().front().h());
456 *this->nn(this->samples().back()),
457 this->samples().back()
459 if (this->steered().size() == 0)
461 auto col = this->collide_steered_from(
462 *this->nn(this->samples().back())
464 if (std::get<0>(col)) {
465 auto rcnt = this->steered().size() - std::get<1>(col);
467 this->steered().pop_back();
470 if (!this->connect())
473 unsigned scnt = this->steered().size();
474 this->join_steered(&this->nodes().back());
475 RRTNode *just_added = &this->nodes().back();
477 // store all the steered1 nodes
478 this->steered1_.push_back(just_added);
480 auto &g = this->goals().front();
481 this->steer2(*just_added, g);
482 auto col = this->collide_steered_from(*just_added);
483 if (std::get<0>(col)) {
484 auto rcnt = this->steered().size() - std::get<1>(col);
486 this->steered().pop_back();
489 this->join_steered(just_added);
490 // store all the steered2 nodes
491 RRTNode* jap = &this->nodes().back();
492 while (jap != just_added) {
493 this->steered2_.push_back(jap);
496 this->gf(this->goal_found(this->nodes().back()));
497 just_added = just_added->p();
502 void RRTS::set_sample_normal(
503 double mx, double dx,
504 double my, double dy,
508 this->ndx_ = std::normal_distribution<double>(mx, dx);
509 this->ndy_ = std::normal_distribution<double>(my, dy);
510 this->ndh_ = std::normal_distribution<double>(mh, dh);
512 void RRTS::set_sample_uniform(
513 double xmin, double xmax,
514 double ymin, double ymax,
515 double hmin, double hmax
518 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
519 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
520 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
522 void RRTS::set_sample_uniform_circle()
524 this->udx_ = std::uniform_real_distribution<double>(0, 1);
525 this->udy_ = std::uniform_real_distribution<double>(0, 1);
526 this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
528 void RRTS::set_sample(
529 double x1, double x2,
530 double y1, double y2,
534 switch (this->sample_dist_type()) {
536 x1 += this->nodes().front().x();
537 x2 += this->nodes().front().x();
538 y1 += this->nodes().front().y();
539 y2 += this->nodes().front().y();
540 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
542 case 2: // uniform circle
543 this->set_sample_uniform_circle();
545 case 3: // uniform index of node in nodes
546 this->set_sample_uniform_circle();
549 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
553 Json::Value RRTS::json()
557 jvo["time"] = this->scnt();
560 jvo["iterations"] = this->icnt();
563 jvo["init"][0] = this->nodes().front().x();
564 jvo["init"][1] = this->nodes().front().y();
565 jvo["init"][2] = this->nodes().front().h();
568 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
571 if (this->path().size() > 0) {
572 jvo["cost"] = cc(*this->path().back());
573 jvo["entry"][0] = this->goals().front().x();
574 jvo["entry"][1] = this->goals().front().y();
575 jvo["entry"][2] = this->goals().front().h();
576 jvo["goal"][0] = this->goals().back().x();
577 jvo["goal"][1] = this->goals().back().y();
578 jvo["goal"][2] = this->goals().back().h();
584 unsigned int pcnt = 0;
585 for (auto n: this->path()) {
586 jvo["path"][pcnt][0] = n->x();
587 jvo["path"][pcnt][1] = n->y();
588 jvo["path"][pcnt][2] = n->h();
589 if (n->t(RRTNodeType::cusp))
591 if (n->t(RRTNodeType::connected))
595 jvo["cusps-in-path"] = cu;
596 jvo["connecteds-in-path"] = co;
599 unsigned int gcnt = 0;
600 for (auto g: this->goals()) {
601 jvo["goals"][gcnt][0] = g.x();
602 jvo["goals"][gcnt][1] = g.y();
603 jvo["goals"][gcnt][2] = g.h();
608 unsigned int ocnt = 0;
609 for (auto o: this->obstacles()) {
610 unsigned int ccnt = 0;
611 for (auto c: o.poly()) {
612 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
613 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
620 jvo["nodes"] = (unsigned int) this->nodes().size();
623 unsigned int cnt = 0;
624 for (auto i: this->log_path_cost_)
625 jvo["log_path_cost"][cnt++] = i;
628 // unsigned int ncnt = 0;
629 // for (auto n: this->nodes()) {
630 // jvo["nodes_x"][ncnt] = n.x();
631 // jvo["nodes_y"][ncnt] = n.y();
632 // //jvo["nodes_h"][ncnt] = n.h();
637 // unsigned int ncnt = 0;
638 // for (auto n: this->steered1_) {
639 // jvo["steered1_x"][ncnt] = n->x();
640 // jvo["steered1_y"][ncnt] = n->y();
641 // //jvo["nodes_h"][ncnt] = n.h();
645 // for (auto n: this->steered2_) {
646 // jvo["steered2_x"][ncnt] = n->x();
647 // jvo["steered2_y"][ncnt] = n->y();
648 // //jvo["nodes_h"][ncnt] = n.h();
655 void RRTS::json(Json::Value jvi)
657 assert(jvi["init"] != Json::nullValue);
658 assert(jvi["goals"] != Json::nullValue);
659 assert(jvi["obst"] != Json::nullValue);
661 this->nodes().front().x(jvi["init"][0].asDouble());
662 this->nodes().front().y(jvi["init"][1].asDouble());
663 this->nodes().front().h(jvi["init"][2].asDouble());
665 RRTNode* gp = nullptr;
666 if (jvi["entry"] != Json::nullValue) {
667 this->entry_set = true;
668 this->entry.x = jvi["entry"][0].asDouble();
669 this->entry.y = jvi["entry"][1].asDouble();
670 this->entry.b = jvi["entry"][2].asDouble();
671 this->entry.e = jvi["entry"][3].asDouble();
673 tmp_node.x(this->entry.x);
674 tmp_node.y(this->entry.y);
675 tmp_node.h((this->entry.b + this->entry.e) / 2.0);
676 this->goals().push_back(tmp_node);
677 this->goals().back().p(gp);
678 gp = &this->goals().back();
680 for (auto g: jvi["goals"]) {
682 tmp_node.x(g[0].asDouble());
683 tmp_node.y(g[1].asDouble());
684 tmp_node.h(g[2].asDouble());
685 this->goals().push_back(tmp_node);
686 this->goals().back().p(gp);
687 gp = &this->goals().back();
689 this->goals().front().set_t(RRTNodeType::cusp);
690 this->goals().back().set_t(RRTNodeType::cusp);
693 Obstacle tmp_obstacle;
694 for (auto o: jvi["obst"]) {
695 tmp_obstacle.poly().clear();
697 double tmp_x = c[0].asDouble();
698 double tmp_y = c[1].asDouble();
699 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
700 tmp_obstacle.poly().push_back(tmp_tuple);
702 this->obstacles().push_back(tmp_obstacle);
706 double edist_init_goal = sqrt(
708 this->nodes().front().x()
709 - this->goals().front().x(),
713 this->nodes().front().y()
714 - this->goals().front().y(),
719 this->nodes().front().x(), edist_init_goal,
720 this->nodes().front().y(), edist_init_goal,
727 : gen_(std::random_device{}())
729 this->goals().reserve(100);
730 this->nodes().reserve(4000000);
731 this->samples().reserve(1000);
732 this->steered().reserve(20000);
733 this->store_node(RRTNode()); // root
736 double cc(RRTNode &t)
740 while (n != nullptr) {