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 bool RRTS::should_stop()
47 // the following counters must be updated, do not comment
50 // current iteration stop conditions
51 if (this->should_finish()) return true;
52 if (this->should_break()) return true;
53 // but continue by default
57 bool RRTS::should_finish()
59 // decide finish conditions (maybe comment some lines)
60 //if (this->icnt_ > 999) return true;
61 if (this->scnt_ > 10) return true;
62 if (this->gf()) return true;
63 // but continue by default
67 bool RRTS::should_break()
69 // decide break conditions (maybe comment some lines)
70 //if (this->scnt_ - this->pcnt_ > 2) return true;
71 // but continue by default
75 bool RRTS::should_continue()
77 // decide the stop conditions (maybe comment some lines)
78 // it is exact opposite of `should_stop`
79 //if (this->icnt_ > 999) return false;
80 if (this->scnt_ > 10) return false;
81 if (this->gf()) return false;
82 // and reset pause counter if should continue
83 this->pcnt_ = this->scnt_;
87 void RRTS::store_node(RRTNode n)
89 this->nodes().push_back(n);
93 std::tuple<bool, unsigned int, unsigned int>
94 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
96 for (auto &o: this->obstacles())
97 if (std::get<0>(::collide(poly, o.poly())))
98 return ::collide(poly, o.poly());
99 return std::make_tuple(false, 0, 0);
102 std::tuple<bool, unsigned int, unsigned int>
103 RRTS::collide_steered_from(RRTNode &f)
105 std::vector<std::tuple<double, double>> s;
106 s.push_back(std::make_tuple(f.x(), f.y()));
107 for (auto &n: this->steered()) {
108 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
109 s.push_back(std::make_tuple(n.lrx(), n.lry()));
110 s.push_back(std::make_tuple(n.rrx(), n.rry()));
111 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
113 auto col = this->collide(s);
114 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
115 if (std::get<0>(col) && strip_from > 0) {
116 while (strip_from-- > 0) {
117 this->steered().pop_back();
119 return this->collide_steered_from(f);
124 std::tuple<bool, unsigned int, unsigned int>
125 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
127 std::vector<std::tuple<double, double>> p;
128 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
129 p.push_back(std::make_tuple(f.lrx(), f.lry()));
130 p.push_back(std::make_tuple(f.rrx(), f.rry()));
131 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
132 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
133 p.push_back(std::make_tuple(t.lrx(), t.lry()));
134 p.push_back(std::make_tuple(t.rrx(), t.rry()));
135 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
136 return this->collide(p);
139 double RRTS::cost_build(RRTNode &f, RRTNode &t)
142 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
146 double RRTS::cost_search(RRTNode &f, RRTNode &t)
149 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
158 switch (this->sample_dist_type()) {
160 x = this->udx_(this->gen_);
161 y = this->udy_(this->gen_);
162 h = this->udh_(this->gen_);
165 x = this->ndx_(this->gen_);
166 y = this->ndy_(this->gen_);
167 h = this->ndh_(this->gen_);
169 this->samples().push_back(RRTNode());
170 this->samples().back().x(x);
171 this->samples().back().y(y);
172 this->samples().back().h(h);
175 RRTNode *RRTS::nn(RRTNode &t)
177 RRTNode *nn = &this->nodes().front();
178 double cost = this->cost_search(*nn, t);
179 for (auto &f: this->nodes()) {
180 if (this->cost_search(f, t) < cost) {
182 cost = this->cost_search(f, t);
188 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
190 std::vector<RRTNode *> nv;
191 double cost = std::min(GAMMA(this->nodes().size()), ETA);
192 for (auto &f: this->nodes())
193 if (this->cost_search(f, t) < cost)
198 int cb_rs_steer(double q[4], void *user_data)
200 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
201 RRTNode *ln = nullptr;
202 if (nodes->size() > 0)
204 nodes->push_back(RRTNode());
205 nodes->back().x(q[0]);
206 nodes->back().y(q[1]);
207 nodes->back().h(q[2]);
208 nodes->back().sp(q[3]);
209 if (nodes->back().sp() == 0)
210 nodes->back().set_t(RRTNodeType::cusp);
211 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
212 ln->set_t(RRTNodeType::cusp);
216 void RRTS::steer(RRTNode &f, RRTNode &t)
218 this->steered().clear();
219 double q0[] = {f.x(), f.y(), f.h()};
220 double q1[] = {t.x(), t.y(), t.h()};
221 ReedsSheppStateSpace rsss(f.mtr());
222 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
225 void RRTS::join_steered(RRTNode *f)
227 while (this->steered().size() > 0) {
228 this->store_node(this->steered().front());
229 RRTNode *t = &this->nodes().back();
231 t->c(this->cost_build(*f, *t));
232 this->steered().erase(this->steered().begin());
237 bool RRTS::goal_found(RRTNode &f)
239 auto &g = this->goals().front();
240 double cost = this->cost_build(f, g);
242 pow(f.x() - g.x(), 2)
243 + pow(f.y() - g.y(), 2)
245 double adist = std::abs(f.h() - g.h());
246 if (edist < 0.05 && adist < M_PI / 32) {
247 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
259 RRTNode *t = &this->steered().front();
260 RRTNode *f = this->nn(this->samples().back());
261 double cost = this->cost_search(*f, *t);
262 for (auto n: this->nv(*t)) {
264 !std::get<0>(this->collide_two_nodes(*n, *t))
265 && this->cost_search(*n, *t) < cost
268 cost = this->cost_search(*n, *t);
271 this->store_node(this->steered().front());
272 t = &this->nodes().back();
274 t->c(this->cost_build(*f, *t));
275 t->set_t(RRTNodeType::connected);
281 RRTNode *f = &this->nodes().back();
282 for (auto n: this->nv(*f)) {
284 !std::get<0>(this->collide_two_nodes(*f, *n))
285 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
288 n->c(this->cost_build(*f, *n));
300 this->nodes().clear();
301 this->samples().clear();
302 this->steered().clear();
303 this->store_node(RRTNode()); // root
310 std::vector<RRTNode *> RRTS::path()
312 std::vector<RRTNode *> path;
313 if (this->goals().size() == 0)
315 RRTNode *goal = &this->goals().front();
316 if (goal->p() == nullptr)
318 while (goal != nullptr) {
319 path.push_back(goal);
322 std::reverse(path.begin(), path.end());
328 if (this->icnt_ == 0)
329 this->tstart_ = std::chrono::high_resolution_clock::now();
331 if (this->should_stop())
335 *this->nn(this->samples().back()),
336 this->samples().back()
338 if (std::get<0>(this->collide_steered_from(
339 *this->nn(this->samples().back())
342 if (!this->connect())
345 unsigned scnt = this->steered().size();
346 this->join_steered(&this->nodes().back());
347 RRTNode *just_added = &this->nodes().back();
350 auto &g = this->goals().front();
351 this->steer(*just_added, g);
352 if (std::get<0>(this->collide_steered_from(
356 this->join_steered(just_added);
357 this->gf(this->goal_found(this->nodes().back()));
358 just_added = just_added->p();
363 void RRTS::set_sample_normal(
364 double mx, double dx,
365 double my, double dy,
369 this->ndx_ = std::normal_distribution<double>(mx, dx);
370 this->ndy_ = std::normal_distribution<double>(my, dy);
371 this->ndh_ = std::normal_distribution<double>(mh, dh);
373 void RRTS::set_sample_uniform(
374 double xmin, double xmax,
375 double ymin, double ymax,
376 double hmin, double hmax
379 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
380 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
381 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
383 void RRTS::set_sample(
384 double x1, double x2,
385 double y1, double y2,
389 switch (this->sample_dist_type()) {
391 x1 += this->nodes().front().x();
392 x2 += this->nodes().front().x();
393 y1 += this->nodes().front().y();
394 y2 += this->nodes().front().y();
395 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
398 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
402 Json::Value RRTS::json()
406 jvo["time"] = this->scnt();
409 jvo["iterations"] = this->icnt();
412 jvo["init"][0] = this->nodes().front().x();
413 jvo["init"][1] = this->nodes().front().y();
414 jvo["init"][2] = this->nodes().front().h();
417 if (this->path().size() > 0) {
418 jvo["cost"] = cc(*this->path().back());
419 jvo["goal"][0] = this->path().back()->x();
420 jvo["goal"][1] = this->path().back()->y();
421 jvo["goal"][2] = this->path().back()->h();
427 unsigned int pcnt = 0;
428 for (auto n: this->path()) {
429 jvo["path"][pcnt][0] = n->x();
430 jvo["path"][pcnt][1] = n->y();
431 jvo["path"][pcnt][2] = n->h();
432 if (n->t(RRTNodeType::cusp))
434 if (n->t(RRTNodeType::connected))
438 jvo["cusps-in-path"] = cu;
439 jvo["connecteds-in-path"] = co;
442 unsigned int gcnt = 0;
443 for (auto g: this->goals()) {
444 jvo["goals"][gcnt][0] = g.x();
445 jvo["goals"][gcnt][1] = g.y();
446 jvo["goals"][gcnt][2] = g.h();
451 unsigned int ocnt = 0;
452 for (auto o: this->obstacles()) {
453 unsigned int ccnt = 0;
454 for (auto c: o.poly()) {
455 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
456 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
463 jvo["nodes"] = (unsigned int) this->nodes().size();
466 // unsigned int ncnt = 0;
467 // for (auto n: this->nodes()) {
468 // jvo["nodes_x"][ncnt] = n.x();
469 // jvo["nodes_y"][ncnt] = n.y();
470 // //jvo["nodes_h"][ncnt] = n.h();
477 void RRTS::json(Json::Value jvi)
479 assert(jvi["init"] != Json::nullValue);
480 assert(jvi["goal"] != Json::nullValue);
481 assert(jvi["goals"] != Json::nullValue);
482 assert(jvi["obst"] != Json::nullValue);
484 this->nodes().front().x(jvi["init"][0].asDouble());
485 this->nodes().front().y(jvi["init"][1].asDouble());
486 this->nodes().front().h(jvi["init"][2].asDouble());
489 tmp_node.x(jvi["goal"][0].asDouble());
490 tmp_node.y(jvi["goal"][1].asDouble());
491 tmp_node.h(jvi["goal"][2].asDouble());
492 this->goals().push_back(tmp_node);
493 for (auto g: jvi["goals"]) {
494 tmp_node.x(g[0].asDouble());
495 tmp_node.y(g[1].asDouble());
496 tmp_node.h(g[2].asDouble());
497 this->goals().push_back(tmp_node);
501 Obstacle tmp_obstacle;
502 for (auto o: jvi["obst"]) {
503 tmp_obstacle.poly().clear();
505 double tmp_x = c[0].asDouble();
506 double tmp_y = c[1].asDouble();
507 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
508 tmp_obstacle.poly().push_back(tmp_tuple);
510 this->obstacles().push_back(tmp_obstacle);
514 double edist_init_goal = sqrt(
516 this->nodes().front().x()
517 - this->goals().front().x(),
521 this->nodes().front().y()
522 - this->goals().front().y(),
527 this->nodes().front().x(), edist_init_goal,
528 this->nodes().front().y(), edist_init_goal,
535 : gen_(std::random_device{}())
537 this->goals().reserve(100);
538 this->nodes().reserve(4000000);
539 this->samples().reserve(1000);
540 this->steered().reserve(20000);
541 this->store_node(RRTNode()); // root
544 double cc(RRTNode &t)
548 while (n != nullptr) {