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 x = this->ndx_(this->gen_);
216 y = this->ndy_(this->gen_);
217 h = this->ndh_(this->gen_);
219 this->samples().push_back(RRTNode());
220 this->samples().back().x(x);
221 this->samples().back().y(y);
222 this->samples().back().h(h);
225 RRTNode *RRTS::nn(RRTNode &t)
227 RRTNode *nn = &this->nodes().front();
228 double cost = this->cost_search(*nn, t);
229 for (auto &f: this->nodes()) {
230 if (this->cost_search(f, t) < cost) {
232 cost = this->cost_search(f, t);
238 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
240 std::vector<RRTNode *> nv;
241 double cost = std::min(GAMMA(this->nodes().size()), ETA);
242 for (auto &f: this->nodes())
243 if (this->cost_search(f, t) < cost)
248 int cb_rs_steer(double q[4], void *user_data)
250 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
251 RRTNode *ln = nullptr;
252 if (nodes->size() > 0)
254 nodes->push_back(RRTNode());
255 nodes->back().x(q[0]);
256 nodes->back().y(q[1]);
257 nodes->back().h(q[2]);
258 nodes->back().sp(q[3]);
259 if (nodes->back().sp() == 0)
260 nodes->back().set_t(RRTNodeType::cusp);
261 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
262 ln->set_t(RRTNodeType::cusp);
266 void RRTS::steer(RRTNode &f, RRTNode &t)
268 this->steered().clear();
269 double q0[] = {f.x(), f.y(), f.h()};
270 double q1[] = {t.x(), t.y(), t.h()};
271 ReedsSheppStateSpace rsss(this->bc.mtr());
272 rsss.sample(q0, q1, 0.05, cb_rs_steer, &this->steered());
275 void RRTS::steer1(RRTNode &f, RRTNode &t)
277 return this->steer(f, t);
280 void RRTS::steer2(RRTNode &f, RRTNode &t)
282 return this->steer(f, t);
285 void RRTS::join_steered(RRTNode *f)
287 while (this->steered().size() > 0) {
288 this->store_node(this->steered().front());
289 RRTNode *t = &this->nodes().back();
291 t->c(this->cost_build(*f, *t));
292 this->steered().erase(this->steered().begin());
297 bool RRTS::goal_found(RRTNode &f)
299 auto &g = this->goals().front();
300 double cost = this->cost_build(f, g);
302 pow(f.x() - g.x(), 2)
303 + pow(f.y() - g.y(), 2)
305 double adist = std::abs(f.h() - g.h());
306 if (edist < 0.05 && adist < M_PI / 32) {
307 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
319 RRTNode *t = &this->steered().front();
320 RRTNode *f = this->nn(this->samples().back());
321 double cost = this->cost_search(*f, *t);
322 for (auto n: this->nv(*t)) {
324 !std::get<0>(this->collide_two_nodes(*n, *t))
325 && this->cost_search(*n, *t) < cost
328 cost = this->cost_search(*n, *t);
331 this->store_node(this->steered().front());
332 t = &this->nodes().back();
334 t->c(this->cost_build(*f, *t));
335 t->set_t(RRTNodeType::connected);
341 RRTNode *f = &this->nodes().back();
342 for (auto n: this->nv(*f)) {
344 !std::get<0>(this->collide_two_nodes(*f, *n))
345 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
348 n->c(this->cost_build(*f, *n));
360 this->nodes().clear();
361 this->samples().clear();
362 this->steered().clear();
363 this->store_node(RRTNode()); // root
370 std::vector<RRTNode *> RRTS::path()
372 std::vector<RRTNode *> path;
373 if (this->goals().size() == 0)
375 RRTNode *goal = &this->goals().back();
376 if (goal->p() == nullptr)
378 while (goal != nullptr) {
379 path.push_back(goal);
382 std::reverse(path.begin(), path.end());
388 if (this->icnt_ == 0)
389 this->tstart_ = std::chrono::high_resolution_clock::now();
391 if (this->scnt_ > this->log_path_time_)
392 this->log_path_cost();
393 if (this->should_stop())
395 if (this->samples().size() == 0) {
396 this->samples().push_back(RRTNode());
397 this->samples().back().x(this->goals().front().x());
398 this->samples().back().y(this->goals().front().y());
399 this->samples().back().h(this->goals().front().h());
404 *this->nn(this->samples().back()),
405 this->samples().back()
407 if (std::get<0>(this->collide_steered_from(
408 *this->nn(this->samples().back())
411 if (!this->connect())
414 unsigned scnt = this->steered().size();
415 this->join_steered(&this->nodes().back());
416 RRTNode *just_added = &this->nodes().back();
419 auto &g = this->goals().front();
420 this->steer2(*just_added, g);
421 if (std::get<0>(this->collide_steered_from(
425 this->join_steered(just_added);
426 this->gf(this->goal_found(this->nodes().back()));
427 just_added = just_added->p();
432 void RRTS::set_sample_normal(
433 double mx, double dx,
434 double my, double dy,
438 this->ndx_ = std::normal_distribution<double>(mx, dx);
439 this->ndy_ = std::normal_distribution<double>(my, dy);
440 this->ndh_ = std::normal_distribution<double>(mh, dh);
442 void RRTS::set_sample_uniform(
443 double xmin, double xmax,
444 double ymin, double ymax,
445 double hmin, double hmax
448 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
449 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
450 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
452 void RRTS::set_sample_uniform_circle()
454 this->udx_ = std::uniform_real_distribution<double>(0, 1);
455 this->udy_ = std::uniform_real_distribution<double>(0, 1);
456 this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
458 void RRTS::set_sample(
459 double x1, double x2,
460 double y1, double y2,
464 switch (this->sample_dist_type()) {
466 x1 += this->nodes().front().x();
467 x2 += this->nodes().front().x();
468 y1 += this->nodes().front().y();
469 y2 += this->nodes().front().y();
470 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
472 case 2: // uniform circle
473 this->set_sample_uniform_circle();
476 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
480 Json::Value RRTS::json()
484 jvo["time"] = this->scnt();
487 jvo["iterations"] = this->icnt();
490 jvo["init"][0] = this->nodes().front().x();
491 jvo["init"][1] = this->nodes().front().y();
492 jvo["init"][2] = this->nodes().front().h();
495 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
498 if (this->path().size() > 0) {
499 jvo["cost"] = cc(*this->path().back());
500 jvo["entry"][0] = this->goals().front().x();
501 jvo["entry"][1] = this->goals().front().y();
502 jvo["entry"][2] = this->goals().front().h();
503 jvo["goal"][0] = this->goals().back().x();
504 jvo["goal"][1] = this->goals().back().y();
505 jvo["goal"][2] = this->goals().back().h();
511 unsigned int pcnt = 0;
512 for (auto n: this->path()) {
513 jvo["path"][pcnt][0] = n->x();
514 jvo["path"][pcnt][1] = n->y();
515 jvo["path"][pcnt][2] = n->h();
516 if (n->t(RRTNodeType::cusp))
518 if (n->t(RRTNodeType::connected))
522 jvo["cusps-in-path"] = cu;
523 jvo["connecteds-in-path"] = co;
526 unsigned int gcnt = 0;
527 for (auto g: this->goals()) {
528 jvo["goals"][gcnt][0] = g.x();
529 jvo["goals"][gcnt][1] = g.y();
530 jvo["goals"][gcnt][2] = g.h();
535 unsigned int ocnt = 0;
536 for (auto o: this->obstacles()) {
537 unsigned int ccnt = 0;
538 for (auto c: o.poly()) {
539 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
540 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
547 jvo["nodes"] = (unsigned int) this->nodes().size();
550 unsigned int cnt = 0;
551 for (auto i: this->log_path_cost_)
552 jvo["log_path_cost"][cnt++] = i;
555 // unsigned int ncnt = 0;
556 // for (auto n: this->nodes()) {
557 // jvo["nodes_x"][ncnt] = n.x();
558 // jvo["nodes_y"][ncnt] = n.y();
559 // //jvo["nodes_h"][ncnt] = n.h();
566 void RRTS::json(Json::Value jvi)
568 assert(jvi["init"] != Json::nullValue);
569 assert(jvi["goals"] != Json::nullValue);
570 assert(jvi["obst"] != Json::nullValue);
572 this->nodes().front().x(jvi["init"][0].asDouble());
573 this->nodes().front().y(jvi["init"][1].asDouble());
574 this->nodes().front().h(jvi["init"][2].asDouble());
576 if (jvi["entry"] != Json::nullValue) {
577 this->entry_set = true;
578 this->entry.x = jvi["entry"][0].asDouble();
579 this->entry.y = jvi["entry"][1].asDouble();
580 this->entry.b = jvi["entry"][2].asDouble();
581 this->entry.e = jvi["entry"][3].asDouble();
591 RRTNode* gp = nullptr;
592 if (jvi["entry"] != Json::nullValue) {
593 this->entry_set = true;
594 this->entry.x = jvi["entry"][0].asDouble();
595 this->entry.y = jvi["entry"][1].asDouble();
596 this->entry.b = jvi["entry"][2].asDouble();
597 this->entry.e = jvi["entry"][3].asDouble();
598 tmp_node.x(this->entry.x);
599 tmp_node.y(this->entry.y);
600 tmp_node.h((this->entry.b + this->entry.e) / 2.0);
601 this->goals().push_back(tmp_node);
602 this->goals().back().p(gp);
603 gp = &this->goals().back();
605 for (auto g: jvi["goals"]) {
606 tmp_node.x(g[0].asDouble());
607 tmp_node.y(g[1].asDouble());
608 tmp_node.h(g[2].asDouble());
609 this->goals().push_back(tmp_node);
610 this->goals().back().p(gp);
611 gp = &this->goals().back();
613 this->goals().front().set_t(RRTNodeType::cusp);
614 this->goals().back().set_t(RRTNodeType::cusp);
617 Obstacle tmp_obstacle;
618 for (auto o: jvi["obst"]) {
619 tmp_obstacle.poly().clear();
621 double tmp_x = c[0].asDouble();
622 double tmp_y = c[1].asDouble();
623 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
624 tmp_obstacle.poly().push_back(tmp_tuple);
626 this->obstacles().push_back(tmp_obstacle);
630 double edist_init_goal = sqrt(
632 this->nodes().front().x()
633 - this->goals().front().x(),
637 this->nodes().front().y()
638 - this->goals().front().y(),
643 this->nodes().front().x(), edist_init_goal,
644 this->nodes().front().y(), edist_init_goal,
651 : gen_(std::random_device{}())
653 this->goals().reserve(100);
654 this->nodes().reserve(4000000);
655 this->samples().reserve(1000);
656 this->steered().reserve(20000);
657 this->store_node(RRTNode()); // root
660 double cc(RRTNode &t)
664 while (n != nullptr) {