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->gf()) return true;
69 // but continue by default
73 bool RRTS::should_break()
75 // decide break conditions (maybe comment some lines)
76 //if (this->scnt_ - this->pcnt_ > 2) return true;
77 // but continue by default
81 bool RRTS::should_continue()
83 // decide the stop conditions (maybe comment some lines)
84 // it is exact opposite of `should_stop`
85 //if (this->icnt_ > 999) return false;
86 if (this->scnt_ > 10) return false;
87 if (this->gf()) return false;
88 // and reset pause counter if should continue
89 this->pcnt_ = this->scnt_;
93 void RRTS::store_node(RRTNode n)
95 this->nodes().push_back(n);
99 std::tuple<bool, unsigned int, unsigned int>
100 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
102 for (auto &o: this->obstacles())
103 if (std::get<0>(::collide(poly, o.poly())))
104 return ::collide(poly, o.poly());
105 return std::make_tuple(false, 0, 0);
108 std::tuple<bool, unsigned int, unsigned int>
109 RRTS::collide_steered_from(RRTNode &f)
111 auto fbc = BicycleCar();
115 std::vector<std::tuple<double, double>> s;
116 s.push_back(std::make_tuple(fbc.x(), fbc.y()));
117 for (auto &n: this->steered()) {
118 auto nbc = BicycleCar();
122 s.push_back(std::make_tuple(nbc.lfx(), nbc.lfy()));
123 s.push_back(std::make_tuple(nbc.lrx(), nbc.lry()));
124 s.push_back(std::make_tuple(nbc.rrx(), nbc.rry()));
125 s.push_back(std::make_tuple(nbc.rfx(), nbc.rfy()));
127 auto col = this->collide(s);
128 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
129 if (std::get<0>(col) && strip_from > 0) {
130 while (strip_from-- > 0) {
131 this->steered().pop_back();
133 return this->collide_steered_from(f);
138 std::tuple<bool, unsigned int, unsigned int>
139 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
141 auto fbc = BicycleCar();
145 auto tbc = BicycleCar();
149 std::vector<std::tuple<double, double>> p;
150 p.push_back(std::make_tuple(fbc.lfx(), fbc.lfy()));
151 p.push_back(std::make_tuple(fbc.lrx(), fbc.lry()));
152 p.push_back(std::make_tuple(fbc.rrx(), fbc.rry()));
153 p.push_back(std::make_tuple(fbc.rfx(), fbc.rfy()));
154 p.push_back(std::make_tuple(tbc.lfx(), tbc.lfy()));
155 p.push_back(std::make_tuple(tbc.lrx(), tbc.lry()));
156 p.push_back(std::make_tuple(tbc.rrx(), tbc.rry()));
157 p.push_back(std::make_tuple(tbc.rfx(), tbc.rfy()));
158 return this->collide(p);
161 double RRTS::cost_build(RRTNode &f, RRTNode &t)
164 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
168 double RRTS::cost_search(RRTNode &f, RRTNode &t)
171 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
180 switch (this->sample_dist_type()) {
182 x = this->udx_(this->gen_);
183 y = this->udy_(this->gen_);
184 h = this->udh_(this->gen_);
186 case 2: // uniform circle
188 // see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
191 this->nodes().front().x()
192 - this->goals().front().x(),
196 this->nodes().front().y()
197 - this->goals().front().y(),
202 this->goals().front().y() - this->nodes().front().y(),
203 this->goals().front().x() - this->nodes().front().x()
205 double cx = this->goals().front().x() + R/2 * cos(a);
206 double cy = this->goals().front().y() + R/2 * sin(a);
207 double r = R * sqrt(this->udx_(this->gen_));
208 double theta = this->udy_(this->gen_) * 2 * M_PI;
209 x = cx + r * cos(theta);
210 y = cy + r * sin(theta);
211 h = this->udh_(this->gen_);
215 this->udi_ = std::uniform_int_distribution<unsigned int>(
217 this->nodes().size() - 1
219 auto ind = this->udi_(this->gen_);
220 auto n = this->nodes()[ind];
227 x = this->ndx_(this->gen_);
228 y = this->ndy_(this->gen_);
229 h = this->ndh_(this->gen_);
231 this->samples().push_back(RRTNode());
232 this->samples().back().x(x);
233 this->samples().back().y(y);
234 this->samples().back().h(h);
237 RRTNode *RRTS::nn(RRTNode &t)
239 RRTNode *nn = &this->nodes().front();
240 double cost = this->cost_search(*nn, t);
241 for (auto &f: this->nodes()) {
242 if (this->cost_search(f, t) < cost) {
244 cost = this->cost_search(f, t);
250 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
252 std::vector<RRTNode *> nv;
253 double cost = std::min(GAMMA(this->nodes().size()), ETA);
254 for (auto &f: this->nodes())
255 if (this->cost_search(f, t) < cost)
260 int cb_rs_steer(double q[4], void *user_data)
262 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
263 RRTNode *ln = nullptr;
264 if (nodes->size() > 0)
266 nodes->push_back(RRTNode());
267 nodes->back().x(q[0]);
268 nodes->back().y(q[1]);
269 nodes->back().h(q[2]);
270 nodes->back().sp(q[3]);
271 if (nodes->back().sp() == 0)
272 nodes->back().set_t(RRTNodeType::cusp);
273 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
274 ln->set_t(RRTNodeType::cusp);
278 void RRTS::steer(RRTNode &f, RRTNode &t)
280 this->steered().clear();
281 double q0[] = {f.x(), f.y(), f.h()};
282 double q1[] = {t.x(), t.y(), t.h()};
283 ReedsSheppStateSpace rsss(this->bc.mtr());
284 rsss.sample(q0, q1, 0.05, cb_rs_steer, &this->steered());
287 void RRTS::steer1(RRTNode &f, RRTNode &t)
289 return this->steer(f, t);
292 void RRTS::steer2(RRTNode &f, RRTNode &t)
294 return this->steer(f, t);
297 void RRTS::join_steered(RRTNode *f)
299 while (this->steered().size() > 0) {
300 this->store_node(this->steered().front());
301 RRTNode *t = &this->nodes().back();
303 t->c(this->cost_build(*f, *t));
304 this->steered().erase(this->steered().begin());
309 bool RRTS::goal_found(RRTNode &f)
311 auto &g = this->goals().front();
312 double cost = this->cost_build(f, g);
314 pow(f.x() - g.x(), 2)
315 + pow(f.y() - g.y(), 2)
317 double adist = std::abs(f.h() - g.h());
318 if (edist < 0.05 && adist < M_PI / 32) {
319 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
331 RRTNode *t = &this->steered().front();
332 RRTNode *f = this->nn(this->samples().back());
333 double cost = this->cost_search(*f, *t);
334 for (auto n: this->nv(*t)) {
336 !std::get<0>(this->collide_two_nodes(*n, *t))
337 && this->cost_search(*n, *t) < cost
340 cost = this->cost_search(*n, *t);
343 this->store_node(this->steered().front());
344 t = &this->nodes().back();
346 t->c(this->cost_build(*f, *t));
347 t->set_t(RRTNodeType::connected);
353 RRTNode *f = &this->nodes().back();
354 for (auto n: this->nv(*f)) {
356 !std::get<0>(this->collide_two_nodes(*f, *n))
357 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
360 n->c(this->cost_build(*f, *n));
372 this->nodes().clear();
373 this->samples().clear();
374 this->steered().clear();
375 this->store_node(RRTNode()); // root
382 std::vector<RRTNode *> RRTS::path()
384 std::vector<RRTNode *> path;
385 if (this->goals().size() == 0)
387 RRTNode *goal = &this->goals().back();
388 if (goal->p() == nullptr)
390 while (goal != nullptr) {
391 path.push_back(goal);
394 std::reverse(path.begin(), path.end());
400 if (this->icnt_ == 0)
401 this->tstart_ = std::chrono::high_resolution_clock::now();
403 if (this->scnt_ > this->log_path_time_)
404 this->log_path_cost();
405 if (this->should_stop())
407 if (this->samples().size() == 0) {
408 this->samples().push_back(RRTNode());
409 this->samples().back().x(this->goals().front().x());
410 this->samples().back().y(this->goals().front().y());
411 this->samples().back().h(this->goals().front().h());
416 *this->nn(this->samples().back()),
417 this->samples().back()
419 if (std::get<0>(this->collide_steered_from(
420 *this->nn(this->samples().back())
423 if (!this->connect())
426 unsigned scnt = this->steered().size();
427 this->join_steered(&this->nodes().back());
428 RRTNode *just_added = &this->nodes().back();
431 auto &g = this->goals().front();
432 this->steer2(*just_added, g);
433 if (std::get<0>(this->collide_steered_from(
437 this->join_steered(just_added);
438 this->gf(this->goal_found(this->nodes().back()));
439 just_added = just_added->p();
444 void RRTS::set_sample_normal(
445 double mx, double dx,
446 double my, double dy,
450 this->ndx_ = std::normal_distribution<double>(mx, dx);
451 this->ndy_ = std::normal_distribution<double>(my, dy);
452 this->ndh_ = std::normal_distribution<double>(mh, dh);
454 void RRTS::set_sample_uniform(
455 double xmin, double xmax,
456 double ymin, double ymax,
457 double hmin, double hmax
460 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
461 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
462 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
464 void RRTS::set_sample_uniform_circle()
466 this->udx_ = std::uniform_real_distribution<double>(0, 1);
467 this->udy_ = std::uniform_real_distribution<double>(0, 1);
468 this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
470 void RRTS::set_sample(
471 double x1, double x2,
472 double y1, double y2,
476 switch (this->sample_dist_type()) {
478 x1 += this->nodes().front().x();
479 x2 += this->nodes().front().x();
480 y1 += this->nodes().front().y();
481 y2 += this->nodes().front().y();
482 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
484 case 2: // uniform circle
485 this->set_sample_uniform_circle();
487 case 3: // uniform index of node in nodes
488 this->set_sample_uniform_circle();
491 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
495 Json::Value RRTS::json()
499 jvo["time"] = this->scnt();
502 jvo["iterations"] = this->icnt();
505 jvo["init"][0] = this->nodes().front().x();
506 jvo["init"][1] = this->nodes().front().y();
507 jvo["init"][2] = this->nodes().front().h();
510 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
513 if (this->path().size() > 0) {
514 jvo["cost"] = cc(*this->path().back());
515 jvo["entry"][0] = this->goals().front().x();
516 jvo["entry"][1] = this->goals().front().y();
517 jvo["entry"][2] = this->goals().front().h();
518 jvo["goal"][0] = this->goals().back().x();
519 jvo["goal"][1] = this->goals().back().y();
520 jvo["goal"][2] = this->goals().back().h();
526 unsigned int pcnt = 0;
527 for (auto n: this->path()) {
528 jvo["path"][pcnt][0] = n->x();
529 jvo["path"][pcnt][1] = n->y();
530 jvo["path"][pcnt][2] = n->h();
531 if (n->t(RRTNodeType::cusp))
533 if (n->t(RRTNodeType::connected))
537 jvo["cusps-in-path"] = cu;
538 jvo["connecteds-in-path"] = co;
541 unsigned int gcnt = 0;
542 for (auto g: this->goals()) {
543 jvo["goals"][gcnt][0] = g.x();
544 jvo["goals"][gcnt][1] = g.y();
545 jvo["goals"][gcnt][2] = g.h();
550 unsigned int ocnt = 0;
551 for (auto o: this->obstacles()) {
552 unsigned int ccnt = 0;
553 for (auto c: o.poly()) {
554 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
555 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
562 jvo["nodes"] = (unsigned int) this->nodes().size();
565 unsigned int cnt = 0;
566 for (auto i: this->log_path_cost_)
567 jvo["log_path_cost"][cnt++] = i;
570 // unsigned int ncnt = 0;
571 // for (auto n: this->nodes()) {
572 // jvo["nodes_x"][ncnt] = n.x();
573 // jvo["nodes_y"][ncnt] = n.y();
574 // //jvo["nodes_h"][ncnt] = n.h();
581 void RRTS::json(Json::Value jvi)
583 assert(jvi["init"] != Json::nullValue);
584 assert(jvi["goals"] != Json::nullValue);
585 assert(jvi["obst"] != Json::nullValue);
587 this->nodes().front().x(jvi["init"][0].asDouble());
588 this->nodes().front().y(jvi["init"][1].asDouble());
589 this->nodes().front().h(jvi["init"][2].asDouble());
591 if (jvi["entry"] != Json::nullValue) {
592 this->entry_set = true;
593 this->entry.x = jvi["entry"][0].asDouble();
594 this->entry.y = jvi["entry"][1].asDouble();
595 this->entry.b = jvi["entry"][2].asDouble();
596 this->entry.e = jvi["entry"][3].asDouble();
606 RRTNode* gp = nullptr;
607 if (jvi["entry"] != Json::nullValue) {
608 this->entry_set = true;
609 this->entry.x = jvi["entry"][0].asDouble();
610 this->entry.y = jvi["entry"][1].asDouble();
611 this->entry.b = jvi["entry"][2].asDouble();
612 this->entry.e = jvi["entry"][3].asDouble();
613 tmp_node.x(this->entry.x);
614 tmp_node.y(this->entry.y);
615 tmp_node.h((this->entry.b + this->entry.e) / 2.0);
616 this->goals().push_back(tmp_node);
617 this->goals().back().p(gp);
618 gp = &this->goals().back();
620 for (auto g: jvi["goals"]) {
621 tmp_node.x(g[0].asDouble());
622 tmp_node.y(g[1].asDouble());
623 tmp_node.h(g[2].asDouble());
624 this->goals().push_back(tmp_node);
625 this->goals().back().p(gp);
626 gp = &this->goals().back();
628 this->goals().front().set_t(RRTNodeType::cusp);
629 this->goals().back().set_t(RRTNodeType::cusp);
632 Obstacle tmp_obstacle;
633 for (auto o: jvi["obst"]) {
634 tmp_obstacle.poly().clear();
636 double tmp_x = c[0].asDouble();
637 double tmp_y = c[1].asDouble();
638 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
639 tmp_obstacle.poly().push_back(tmp_tuple);
641 this->obstacles().push_back(tmp_obstacle);
645 double edist_init_goal = sqrt(
647 this->nodes().front().x()
648 - this->goals().front().x(),
652 this->nodes().front().y()
653 - this->goals().front().y(),
658 this->nodes().front().x(), edist_init_goal,
659 this->nodes().front().y(), edist_init_goal,
666 : gen_(std::random_device{}())
668 this->goals().reserve(100);
669 this->nodes().reserve(4000000);
670 this->samples().reserve(1000);
671 this->steered().reserve(20000);
672 this->store_node(RRTNode()); // root
675 double cc(RRTNode &t)
679 while (n != nullptr) {