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)
22 double RRTS::elapsed()
24 std::chrono::duration<double> dt;
25 dt = std::chrono::duration_cast<std::chrono::duration<double>>(
26 std::chrono::high_resolution_clock::now()
29 this->scnt_ = dt.count();
33 bool RRTS::should_stop()
35 // the following counters must be updated, do not comment
38 // current iteration stop conditions
39 if (this->should_finish()) return true;
40 if (this->should_break()) return true;
41 // but continue by default
45 bool RRTS::should_finish()
47 // decide finish conditions (maybe comment some lines)
48 //if (this->icnt_ > 999) return true;
49 if (this->scnt_ > 50) return true;
50 if (this->gf()) return true;
51 // but continue by default
55 bool RRTS::should_break()
57 // decide break conditions (maybe comment some lines)
58 //if (this->scnt_ - this->pcnt_ > 2) return true;
59 // but continue by default
63 bool RRTS::should_continue()
65 // decide the stop conditions (maybe comment some lines)
66 // it is exact opposite of `should_stop`
67 //if (this->icnt_ > 999) return false;
68 if (this->scnt_ > 10) return false;
69 if (this->gf()) return false;
70 // and reset pause counter if should continue
71 this->pcnt_ = this->scnt_;
75 void RRTS::store_node(RRTNode n)
77 this->nodes().push_back(n);
81 std::tuple<bool, unsigned int, unsigned int>
82 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
84 for (auto &o: this->obstacles())
85 if (std::get<0>(::collide(poly, o.poly())))
86 return ::collide(poly, o.poly());
87 return std::make_tuple(false, 0, 0);
90 std::tuple<bool, unsigned int, unsigned int>
91 RRTS::collide_steered_from(RRTNode &f)
93 std::vector<std::tuple<double, double>> s;
94 s.push_back(std::make_tuple(f.x(), f.y()));
95 for (auto &n: this->steered()) {
96 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
97 s.push_back(std::make_tuple(n.lrx(), n.lry()));
98 s.push_back(std::make_tuple(n.rrx(), n.rry()));
99 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
101 auto col = this->collide(s);
102 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
103 if (std::get<0>(col) && strip_from > 0) {
104 while (strip_from-- > 0) {
105 this->steered().pop_back();
107 return this->collide_steered_from(f);
112 std::tuple<bool, unsigned int, unsigned int>
113 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
115 std::vector<std::tuple<double, double>> p;
116 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
117 p.push_back(std::make_tuple(f.lrx(), f.lry()));
118 p.push_back(std::make_tuple(f.rrx(), f.rry()));
119 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
120 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
121 p.push_back(std::make_tuple(t.lrx(), t.lry()));
122 p.push_back(std::make_tuple(t.rrx(), t.rry()));
123 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
124 return this->collide(p);
127 double RRTS::cost_build(RRTNode &f, RRTNode &t)
130 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
134 double RRTS::cost_search(RRTNode &f, RRTNode &t)
137 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
146 switch (this->sample_dist_type()) {
148 x = this->udx_(this->gen_);
149 y = this->udy_(this->gen_);
150 h = this->udh_(this->gen_);
153 x = this->ndx_(this->gen_);
154 y = this->ndy_(this->gen_);
155 h = this->ndh_(this->gen_);
157 this->samples().push_back(RRTNode());
158 this->samples().back().x(x);
159 this->samples().back().y(y);
160 this->samples().back().h(h);
163 RRTNode *RRTS::nn(RRTNode &t)
165 RRTNode *nn = &this->nodes().front();
166 double cost = this->cost_search(*nn, t);
167 for (auto &f: this->nodes()) {
168 if (this->cost_search(f, t) < cost) {
170 cost = this->cost_search(f, t);
176 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
178 std::vector<RRTNode *> nv;
179 double cost = std::min(GAMMA(this->nodes().size()), ETA);
180 for (auto &f: this->nodes())
181 if (this->cost_search(f, t) < cost)
186 int cb_rs_steer(double q[4], void *user_data)
188 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
189 RRTNode *ln = nullptr;
190 if (nodes->size() > 0)
192 nodes->push_back(RRTNode());
193 nodes->back().x(q[0]);
194 nodes->back().y(q[1]);
195 nodes->back().h(q[2]);
196 nodes->back().sp(q[3]);
197 if (nodes->back().sp() == 0)
198 nodes->back().set_t(RRTNodeType::cusp);
199 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
200 ln->set_t(RRTNodeType::cusp);
204 void RRTS::steer(RRTNode &f, RRTNode &t)
206 this->steered().clear();
207 double q0[] = {f.x(), f.y(), f.h()};
208 double q1[] = {t.x(), t.y(), t.h()};
209 ReedsSheppStateSpace rsss(f.mtr());
210 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
213 void RRTS::join_steered(RRTNode *f)
215 while (this->steered().size() > 0) {
216 this->store_node(this->steered().front());
217 RRTNode *t = &this->nodes().back();
219 t->c(this->cost_build(*f, *t));
220 this->steered().erase(this->steered().begin());
225 bool RRTS::goal_found(RRTNode &f)
228 auto &g = this->goals().front();
229 bool in_zone = false;
230 double cost = this->cost_build(f, g);
231 // TODO find if ``f`` in ``g``'s zone.
234 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
245 RRTNode *t = &this->steered().front();
246 RRTNode *f = this->nn(this->samples().back());
247 double cost = this->cost_search(*f, *t);
248 for (auto n: this->nv(*t)) {
250 !std::get<0>(this->collide_two_nodes(*n, *t))
251 && this->cost_search(*n, *t) < cost
254 cost = this->cost_search(*n, *t);
257 this->store_node(this->steered().front());
258 t = &this->nodes().back();
260 t->c(this->cost_build(*f, *t));
261 t->set_t(RRTNodeType::connected);
267 RRTNode *f = &this->nodes().back();
268 for (auto n: this->nv(*f)) {
270 !std::get<0>(this->collide_two_nodes(*f, *n))
271 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
274 n->c(this->cost_build(*f, *n));
286 this->nodes().clear();
287 this->samples().clear();
288 this->steered().clear();
289 this->store_node(RRTNode()); // root
296 std::vector<RRTNode *> RRTS::path()
298 std::vector<RRTNode *> path;
299 if (this->goals().size() == 0)
301 RRTNode *goal = &this->goals().front();
302 if (goal->p() == nullptr)
304 while (goal != nullptr) {
305 path.push_back(goal);
308 std::reverse(path.begin(), path.end());
314 if (this->icnt_ == 0)
315 this->tstart_ = std::chrono::high_resolution_clock::now();
317 if (this->should_stop())
321 *this->nn(this->samples().back()),
322 this->samples().back()
324 if (std::get<0>(this->collide_steered_from(
325 *this->nn(this->samples().back())
328 if (!this->connect())
331 unsigned scnt = this->steered().size();
332 this->steered().erase(this->steered().begin());
333 this->join_steered(&this->nodes().back());
334 RRTNode *just_added = &this->nodes().back();
337 auto &g = this->goals().front();
338 this->steer(*just_added, g);
339 if (std::get<0>(this->collide_steered_from(
343 this->join_steered(just_added);
344 this->gf(this->goal_found(this->nodes().back()));
345 just_added = just_added->p();
350 void RRTS::set_sample_normal(
351 double mx, double dx,
352 double my, double dy,
356 this->ndx_ = std::normal_distribution<double>(mx, dx);
357 this->ndy_ = std::normal_distribution<double>(my, dy);
358 this->ndh_ = std::normal_distribution<double>(mh, dh);
360 void RRTS::set_sample_uniform(
361 double xmin, double xmax,
362 double ymin, double ymax,
363 double hmin, double hmax
366 this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
367 this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
368 this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
370 void RRTS::set_sample(
371 double x1, double x2,
372 double y1, double y2,
376 switch (this->sample_dist_type()) {
378 x1 += this->nodes().front().x();
379 x2 += this->nodes().front().x();
380 y1 += this->nodes().front().y();
381 y2 += this->nodes().front().y();
382 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
385 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
389 Json::Value RRTS::json()
393 jvo["time"] = this->scnt();
396 jvo["iterations"] = this->icnt();
399 jvo["init"][0] = this->nodes().front().x();
400 jvo["init"][1] = this->nodes().front().y();
401 jvo["init"][2] = this->nodes().front().h();
404 if (this->path().size() > 0) {
405 jvo["cost"] = cc(*this->path().back());
406 jvo["goal"][0] = this->path().back()->x();
407 jvo["goal"][1] = this->path().back()->y();
408 jvo["goal"][2] = this->path().back()->h();
414 unsigned int pcnt = 0;
415 for (auto n: this->path()) {
416 jvo["path"][pcnt][0] = n->x();
417 jvo["path"][pcnt][1] = n->y();
418 jvo["path"][pcnt][2] = n->h();
419 if (n->t(RRTNodeType::cusp))
421 if (n->t(RRTNodeType::connected))
425 jvo["cusps-in-path"] = cu;
426 jvo["connecteds-in-path"] = co;
429 unsigned int gcnt = 0;
430 for (auto g: this->goals()) {
431 jvo["goals"][gcnt][0] = g.x();
432 jvo["goals"][gcnt][1] = g.y();
433 jvo["goals"][gcnt][2] = g.h();
438 unsigned int ocnt = 0;
439 for (auto o: this->obstacles()) {
440 unsigned int ccnt = 0;
441 for (auto c: o.poly()) {
442 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
443 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
450 jvo["nodes"] = (unsigned int) this->nodes().size();
453 // unsigned int ncnt = 0;
454 // for (auto n: this->nodes()) {
455 // jvo["nodes_x"][ncnt] = n.x();
456 // jvo["nodes_y"][ncnt] = n.y();
457 // //jvo["nodes_h"][ncnt] = n.h();
464 void RRTS::json(Json::Value jvi)
466 assert(jvi["init"] != Json::nullValue);
467 assert(jvi["goal"] != Json::nullValue);
468 assert(jvi["goals"] != Json::nullValue);
469 assert(jvi["obst"] != Json::nullValue);
471 this->nodes().front().x(jvi["init"][0].asDouble());
472 this->nodes().front().y(jvi["init"][1].asDouble());
473 this->nodes().front().h(jvi["init"][2].asDouble());
476 tmp_node.x(jvi["goal"][0].asDouble());
477 tmp_node.y(jvi["goal"][1].asDouble());
478 tmp_node.h(jvi["goal"][2].asDouble());
479 this->goals().push_back(tmp_node);
480 for (auto g: jvi["goals"]) {
481 tmp_node.x(g[0].asDouble());
482 tmp_node.y(g[1].asDouble());
483 tmp_node.h(g[2].asDouble());
484 this->goals().push_back(tmp_node);
488 Obstacle tmp_obstacle;
489 for (auto o: jvi["obst"]) {
490 tmp_obstacle.poly().clear();
492 double tmp_x = c[0].asDouble();
493 double tmp_y = c[1].asDouble();
494 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
495 tmp_obstacle.poly().push_back(tmp_tuple);
497 this->obstacles().push_back(tmp_obstacle);
501 double edist_init_goal = sqrt(
503 this->nodes().front().x()
504 - this->goals().front().x(),
508 this->nodes().front().y()
509 - this->goals().front().y(),
514 this->nodes().front().x(), edist_init_goal,
515 this->nodes().front().y(), edist_init_goal,
522 : gen_(std::random_device{}())
524 this->goals().reserve(100);
525 this->nodes().reserve(4000000);
526 this->samples().reserve(1000);
527 this->steered().reserve(20000);
528 this->store_node(RRTNode()); // root
531 double cc(RRTNode &t)
535 while (n != nullptr) {