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) : BicycleCar(bc)
18 bool RRTNode::operator==(const RRTNode& n)
29 double RRTS::elapsed()
31 std::chrono::duration<double> dt;
32 dt = std::chrono::duration_cast<std::chrono::duration<double>>(
33 std::chrono::high_resolution_clock::now()
36 this->scnt_ = dt.count();
40 bool RRTS::should_stop()
42 // the following counters must be updated, do not comment
45 // current iteration stop conditions
46 if (this->should_finish()) return true;
47 if (this->should_break()) return true;
48 // but continue by default
52 bool RRTS::should_finish()
54 // decide finish conditions (maybe comment some lines)
55 //if (this->icnt_ > 999) return true;
56 if (this->scnt_ > 10) return true;
57 if (this->gf()) return true;
58 // but continue by default
62 bool RRTS::should_break()
64 // decide break conditions (maybe comment some lines)
65 //if (this->scnt_ - this->pcnt_ > 2) return true;
66 // but continue by default
70 bool RRTS::should_continue()
72 // decide the stop conditions (maybe comment some lines)
73 // it is exact opposite of `should_stop`
74 //if (this->icnt_ > 999) return false;
75 if (this->scnt_ > 10) return false;
76 if (this->gf()) return false;
77 // and reset pause counter if should continue
78 this->pcnt_ = this->scnt_;
82 void RRTS::store_node(RRTNode n)
84 this->nodes().push_back(n);
88 std::tuple<bool, unsigned int, unsigned int>
89 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
91 for (auto &o: this->obstacles())
92 if (std::get<0>(::collide(poly, o.poly())))
93 return ::collide(poly, o.poly());
94 return std::make_tuple(false, 0, 0);
97 std::tuple<bool, unsigned int, unsigned int>
98 RRTS::collide_steered_from(RRTNode &f)
100 std::vector<std::tuple<double, double>> s;
101 s.push_back(std::make_tuple(f.x(), f.y()));
102 for (auto &n: this->steered()) {
103 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
104 s.push_back(std::make_tuple(n.lrx(), n.lry()));
105 s.push_back(std::make_tuple(n.rrx(), n.rry()));
106 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
108 auto col = this->collide(s);
109 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
110 if (std::get<0>(col) && strip_from > 0) {
111 while (strip_from-- > 0) {
112 this->steered().pop_back();
114 return this->collide_steered_from(f);
119 std::tuple<bool, unsigned int, unsigned int>
120 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
122 std::vector<std::tuple<double, double>> p;
123 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
124 p.push_back(std::make_tuple(f.lrx(), f.lry()));
125 p.push_back(std::make_tuple(f.rrx(), f.rry()));
126 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
127 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
128 p.push_back(std::make_tuple(t.lrx(), t.lry()));
129 p.push_back(std::make_tuple(t.rrx(), t.rry()));
130 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
131 return this->collide(p);
134 double RRTS::cost_build(RRTNode &f, RRTNode &t)
137 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
141 double RRTS::cost_search(RRTNode &f, RRTNode &t)
144 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
153 switch (this->sample_dist_type()) {
155 x = this->udx_(this->gen_);
156 y = this->udy_(this->gen_);
157 h = this->udh_(this->gen_);
160 x = this->ndx_(this->gen_);
161 y = this->ndy_(this->gen_);
162 h = this->ndh_(this->gen_);
164 this->samples().push_back(RRTNode());
165 this->samples().back().x(x);
166 this->samples().back().y(y);
167 this->samples().back().h(h);
170 RRTNode *RRTS::nn(RRTNode &t)
172 RRTNode *nn = &this->nodes().front();
173 double cost = this->cost_search(*nn, t);
174 for (auto &f: this->nodes()) {
175 if (this->cost_search(f, t) < cost) {
177 cost = this->cost_search(f, t);
183 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
185 std::vector<RRTNode *> nv;
186 double cost = std::min(GAMMA(this->nodes().size()), ETA);
187 for (auto &f: this->nodes())
188 if (this->cost_search(f, t) < cost)
193 int cb_rs_steer(double q[4], void *user_data)
195 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
196 RRTNode *ln = nullptr;
197 if (nodes->size() > 0)
199 nodes->push_back(RRTNode());
200 nodes->back().x(q[0]);
201 nodes->back().y(q[1]);
202 nodes->back().h(q[2]);
203 nodes->back().sp(q[3]);
204 if (nodes->back().sp() == 0)
205 nodes->back().set_t(RRTNodeType::cusp);
206 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
207 ln->set_t(RRTNodeType::cusp);
211 void RRTS::steer(RRTNode &f, RRTNode &t)
213 this->steered().clear();
214 double q0[] = {f.x(), f.y(), f.h()};
215 double q1[] = {t.x(), t.y(), t.h()};
216 ReedsSheppStateSpace rsss(f.mtr());
217 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
220 void RRTS::join_steered(RRTNode *f)
222 while (this->steered().size() > 0) {
223 this->store_node(this->steered().front());
224 RRTNode *t = &this->nodes().back();
226 t->c(this->cost_build(*f, *t));
227 this->steered().erase(this->steered().begin());
232 bool RRTS::goal_found(RRTNode &f)
234 auto &g = this->goals().front();
235 double cost = this->cost_build(f, g);
237 pow(f.x() - g.x(), 2)
238 + pow(f.y() - g.y(), 2)
240 double adist = std::abs(f.h() - g.h());
241 if (edist < 0.05 && adist < M_PI / 32) {
242 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
254 RRTNode *t = &this->steered().front();
255 RRTNode *f = this->nn(this->samples().back());
256 double cost = this->cost_search(*f, *t);
257 for (auto n: this->nv(*t)) {
259 !std::get<0>(this->collide_two_nodes(*n, *t))
260 && this->cost_search(*n, *t) < cost
263 cost = this->cost_search(*n, *t);
266 this->store_node(this->steered().front());
267 t = &this->nodes().back();
269 t->c(this->cost_build(*f, *t));
270 t->set_t(RRTNodeType::connected);
276 RRTNode *f = &this->nodes().back();
277 for (auto n: this->nv(*f)) {
279 !std::get<0>(this->collide_two_nodes(*f, *n))
280 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
283 n->c(this->cost_build(*f, *n));
295 this->nodes().clear();
296 this->samples().clear();
297 this->steered().clear();
298 this->store_node(RRTNode()); // root
305 std::vector<RRTNode *> RRTS::path()
307 std::vector<RRTNode *> path;
308 if (this->goals().size() == 0)
310 RRTNode *goal = &this->goals().front();
311 if (goal->p() == nullptr)
313 while (goal != nullptr) {
314 path.push_back(goal);
317 std::reverse(path.begin(), path.end());
323 if (this->icnt_ == 0)
324 this->tstart_ = std::chrono::high_resolution_clock::now();
326 if (this->should_stop())
330 *this->nn(this->samples().back()),
331 this->samples().back()
333 if (std::get<0>(this->collide_steered_from(
334 *this->nn(this->samples().back())
337 if (!this->connect())
340 unsigned scnt = this->steered().size();
341 this->join_steered(&this->nodes().back());
342 RRTNode *just_added = &this->nodes().back();
345 auto &g = this->goals().front();
346 this->steer(*just_added, g);
347 if (std::get<0>(this->collide_steered_from(
351 this->join_steered(just_added);
352 this->gf(this->goal_found(this->nodes().back()));
353 just_added = just_added->p();
358 void RRTS::set_sample_normal(
359 double mx, double dx,
360 double my, double dy,
364 this->ndx_ = std::normal_distribution<double>(mx, dx);
365 this->ndy_ = std::normal_distribution<double>(my, dy);
366 this->ndh_ = std::normal_distribution<double>(mh, dh);
368 void RRTS::set_sample_uniform(
369 double xmin, double xmax,
370 double ymin, double ymax,
371 double hmin, double hmax
374 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
375 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
376 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
378 void RRTS::set_sample(
379 double x1, double x2,
380 double y1, double y2,
384 switch (this->sample_dist_type()) {
386 x1 += this->nodes().front().x();
387 x2 += this->nodes().front().x();
388 y1 += this->nodes().front().y();
389 y2 += this->nodes().front().y();
390 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
393 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
397 Json::Value RRTS::json()
401 jvo["time"] = this->scnt();
404 jvo["iterations"] = this->icnt();
407 jvo["init"][0] = this->nodes().front().x();
408 jvo["init"][1] = this->nodes().front().y();
409 jvo["init"][2] = this->nodes().front().h();
412 if (this->path().size() > 0) {
413 jvo["cost"] = cc(*this->path().back());
414 jvo["goal"][0] = this->path().back()->x();
415 jvo["goal"][1] = this->path().back()->y();
416 jvo["goal"][2] = this->path().back()->h();
422 unsigned int pcnt = 0;
423 for (auto n: this->path()) {
424 jvo["path"][pcnt][0] = n->x();
425 jvo["path"][pcnt][1] = n->y();
426 jvo["path"][pcnt][2] = n->h();
427 if (n->t(RRTNodeType::cusp))
429 if (n->t(RRTNodeType::connected))
433 jvo["cusps-in-path"] = cu;
434 jvo["connecteds-in-path"] = co;
437 unsigned int gcnt = 0;
438 for (auto g: this->goals()) {
439 jvo["goals"][gcnt][0] = g.x();
440 jvo["goals"][gcnt][1] = g.y();
441 jvo["goals"][gcnt][2] = g.h();
446 unsigned int ocnt = 0;
447 for (auto o: this->obstacles()) {
448 unsigned int ccnt = 0;
449 for (auto c: o.poly()) {
450 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
451 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
458 jvo["nodes"] = (unsigned int) this->nodes().size();
461 // unsigned int ncnt = 0;
462 // for (auto n: this->nodes()) {
463 // jvo["nodes_x"][ncnt] = n.x();
464 // jvo["nodes_y"][ncnt] = n.y();
465 // //jvo["nodes_h"][ncnt] = n.h();
472 void RRTS::json(Json::Value jvi)
474 assert(jvi["init"] != Json::nullValue);
475 assert(jvi["goal"] != Json::nullValue);
476 assert(jvi["goals"] != Json::nullValue);
477 assert(jvi["obst"] != Json::nullValue);
479 this->nodes().front().x(jvi["init"][0].asDouble());
480 this->nodes().front().y(jvi["init"][1].asDouble());
481 this->nodes().front().h(jvi["init"][2].asDouble());
484 tmp_node.x(jvi["goal"][0].asDouble());
485 tmp_node.y(jvi["goal"][1].asDouble());
486 tmp_node.h(jvi["goal"][2].asDouble());
487 this->goals().push_back(tmp_node);
488 for (auto g: jvi["goals"]) {
489 tmp_node.x(g[0].asDouble());
490 tmp_node.y(g[1].asDouble());
491 tmp_node.h(g[2].asDouble());
492 this->goals().push_back(tmp_node);
496 Obstacle tmp_obstacle;
497 for (auto o: jvi["obst"]) {
498 tmp_obstacle.poly().clear();
500 double tmp_x = c[0].asDouble();
501 double tmp_y = c[1].asDouble();
502 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
503 tmp_obstacle.poly().push_back(tmp_tuple);
505 this->obstacles().push_back(tmp_obstacle);
509 double edist_init_goal = sqrt(
511 this->nodes().front().x()
512 - this->goals().front().x(),
516 this->nodes().front().y()
517 - this->goals().front().y(),
522 this->nodes().front().x(), edist_init_goal,
523 this->nodes().front().y(), edist_init_goal,
530 : gen_(std::random_device{}())
532 this->goals().reserve(100);
533 this->nodes().reserve(4000000);
534 this->samples().reserve(1000);
535 this->steered().reserve(20000);
536 this->store_node(RRTNode()); // root
539 double cc(RRTNode &t)
543 while (n != nullptr) {