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 // decide the stop conditions (maybe comment some lines)
39 if (this->icnt_ > 999) return true;
40 if (this->scnt_ > 10) return true;
41 if (this->gf()) return true;
42 //if (this->scnt_ - this->pcnt_ > 1) return true;
43 // but continue by default
47 bool RRTS::should_continue()
49 // decide the stop conditions (maybe comment some lines)
50 // it is exact opposite of `should_stop`
51 //if (this->icnt_ > 999) return false;
52 if (this->scnt_ > 10) return false;
53 if (this->gf()) return false;
54 // and reset pause counter if should continue
55 this->pcnt_ = this->scnt_;
59 void RRTS::store_node(RRTNode n)
61 this->nodes().push_back(n);
65 std::tuple<bool, unsigned int, unsigned int>
66 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
68 for (auto &o: this->obstacles())
69 if (std::get<0>(::collide(poly, o.poly())))
70 return ::collide(poly, o.poly());
71 return std::make_tuple(false, 0, 0);
74 std::tuple<bool, unsigned int, unsigned int>
75 RRTS::collide_steered_from(RRTNode &f)
77 std::vector<std::tuple<double, double>> s;
78 s.push_back(std::make_tuple(f.x(), f.y()));
79 for (auto &n: this->steered()) {
80 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
81 s.push_back(std::make_tuple(n.lrx(), n.lry()));
82 s.push_back(std::make_tuple(n.rrx(), n.rry()));
83 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
85 auto col = this->collide(s);
86 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
87 if (std::get<0>(col) && strip_from > 0) {
88 while (strip_from-- > 0) {
89 this->steered().pop_back();
91 return this->collide_steered_from(f);
96 std::tuple<bool, unsigned int, unsigned int>
97 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
99 std::vector<std::tuple<double, double>> p;
100 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
101 p.push_back(std::make_tuple(f.lrx(), f.lry()));
102 p.push_back(std::make_tuple(f.rrx(), f.rry()));
103 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
104 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
105 p.push_back(std::make_tuple(t.lrx(), t.lry()));
106 p.push_back(std::make_tuple(t.rrx(), t.rry()));
107 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
108 return this->collide(p);
111 double RRTS::cost_build(RRTNode &f, RRTNode &t)
114 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
118 double RRTS::cost_search(RRTNode &f, RRTNode &t)
121 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
127 double x = this->ndx_(this->gen_);
128 double y = this->ndy_(this->gen_);
129 double h = this->ndh_(this->gen_);
130 this->samples().push_back(RRTNode());
131 this->samples().back().x(x);
132 this->samples().back().y(y);
133 this->samples().back().h(h);
136 RRTNode *RRTS::nn(RRTNode &t)
138 RRTNode *nn = &this->nodes().front();
139 double cost = this->cost_search(*nn, t);
140 for (auto &f: this->nodes()) {
141 if (this->cost_search(f, t) < cost) {
143 cost = this->cost_search(f, t);
149 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
151 std::vector<RRTNode *> nv;
152 double cost = std::min(GAMMA(this->nodes().size()), ETA);
153 for (auto &f: this->nodes())
154 if (this->cost_search(f, t) < cost)
159 int cb_rs_steer(double q[4], void *user_data)
161 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
162 RRTNode *ln = nullptr;
163 if (nodes->size() > 0)
165 nodes->push_back(RRTNode());
166 nodes->back().x(q[0]);
167 nodes->back().y(q[1]);
168 nodes->back().h(q[2]);
169 nodes->back().sp(q[3]);
170 if (nodes->back().sp() == 0)
171 nodes->back().set_t(RRTNodeType::cusp);
172 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
173 ln->set_t(RRTNodeType::cusp);
177 void RRTS::steer(RRTNode &f, RRTNode &t)
179 this->steered().clear();
180 double q0[] = {f.x(), f.y(), f.h()};
181 double q1[] = {t.x(), t.y(), t.h()};
182 ReedsSheppStateSpace rsss(f.mtr());
183 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
186 void RRTS::join_steered(RRTNode *f)
188 while (this->steered().size() > 0) {
189 this->store_node(this->steered().front());
190 RRTNode *t = &this->nodes().back();
192 t->c(this->cost_build(*f, *t));
193 this->steered().erase(this->steered().begin());
198 bool RRTS::goal_found(RRTNode &f)
201 for (auto &g: this->goals()) {
202 double cost = this->cost_build(f, g);
204 pow(f.x() - g.x(), 2)
205 + pow(f.y() - g.y(), 2)
207 double adist = std::abs(f.h() - g.h());
208 if (edist < 0.05 && adist < M_PI / 32) {
210 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
222 RRTNode *t = &this->steered().front();
223 RRTNode *f = this->nn(this->samples().back());
224 double cost = this->cost_search(*f, *t);
225 for (auto n: this->nv(*t)) {
227 !std::get<0>(this->collide_two_nodes(*n, *t))
228 && this->cost_search(*n, *t) < cost
231 cost = this->cost_search(*n, *t);
234 this->store_node(this->steered().front());
235 t = &this->nodes().back();
237 t->c(this->cost_build(*f, *t));
238 t->set_t(RRTNodeType::connected);
244 RRTNode *f = &this->nodes().back();
245 for (auto n: this->nv(*f)) {
247 !std::get<0>(this->collide_two_nodes(*f, *n))
248 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
251 n->c(this->cost_build(*f, *n));
263 this->nodes().clear();
264 this->samples().clear();
265 this->steered().clear();
266 this->store_node(RRTNode()); // root
273 std::vector<RRTNode *> RRTS::path()
275 std::vector<RRTNode *> path;
276 if (this->goals().size() == 0)
278 RRTNode *goal = &this->goals().front();
279 for (auto &n: this->goals()) {
282 && (n.c() < goal->c() || goal->p() == nullptr)
287 if (goal->p() == nullptr)
289 while (goal != nullptr) {
290 path.push_back(goal);
293 std::reverse(path.begin(), path.end());
299 if (this->icnt_ == 0)
300 this->tstart_ = std::chrono::high_resolution_clock::now();
302 if (this->should_stop())
306 *this->nn(this->samples().back()),
307 this->samples().back()
309 if (std::get<0>(this->collide_steered_from(
310 *this->nn(this->samples().back())
313 if (!this->connect())
316 unsigned scnt = this->steered().size();
317 this->steered().erase(this->steered().begin());
318 this->join_steered(&this->nodes().back());
319 RRTNode *just_added = &this->nodes().back();
322 for (auto &g: this->goals()) {
323 this->steer(*just_added, g);
324 if (std::get<0>(this->collide_steered_from(
328 this->join_steered(just_added);
330 this->gf(this->goal_found(this->nodes().back()));
331 just_added = just_added->p();
336 void RRTS::set_sample(
337 double mx, double dx,
338 double my, double dy,
342 this->ndx_ = std::normal_distribution<double>(mx, dx);
343 this->ndy_ = std::normal_distribution<double>(my, dy);
344 this->ndh_ = std::normal_distribution<double>(mh, dh);
347 Json::Value RRTS::json()
351 jvo["time"] = this->scnt();
354 jvo["iterations"] = this->icnt();
357 jvo["init"][0] = this->nodes().front().x();
358 jvo["init"][1] = this->nodes().front().y();
359 jvo["init"][2] = this->nodes().front().h();
362 if (this->path().size() > 0) {
363 jvo["cost"] = cc(*this->path().back());
364 jvo["goal"][0] = this->path().back()->x();
365 jvo["goal"][1] = this->path().back()->y();
366 jvo["goal"][2] = this->path().back()->h();
372 unsigned int pcnt = 0;
373 for (auto n: this->path()) {
374 jvo["path"][pcnt][0] = n->x();
375 jvo["path"][pcnt][1] = n->y();
376 jvo["path"][pcnt][2] = n->h();
377 if (n->t(RRTNodeType::cusp))
379 if (n->t(RRTNodeType::connected))
383 jvo["cusps-in-path"] = cu;
384 jvo["connecteds-in-path"] = co;
387 unsigned int gcnt = 0;
388 for (auto g: this->goals()) {
389 jvo["goals"][gcnt][0] = g.x();
390 jvo["goals"][gcnt][1] = g.y();
391 jvo["goals"][gcnt][2] = g.h();
396 unsigned int ocnt = 0;
397 for (auto o: this->obstacles()) {
398 unsigned int ccnt = 0;
399 for (auto c: o.poly()) {
400 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
401 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
408 jvo["nodes"] = (unsigned int) this->nodes().size();
411 // unsigned int ncnt = 0;
412 // for (auto n: this->nodes()) {
413 // jvo["nodes_x"][ncnt] = n.x();
414 // jvo["nodes_y"][ncnt] = n.y();
415 // //jvo["nodes_h"][ncnt] = n.h();
423 : gen_(std::random_device{}())
425 this->goals().reserve(100);
426 this->nodes().reserve(4000000);
427 this->samples().reserve(1000);
428 this->steered().reserve(20000);
429 this->store_node(RRTNode()); // root
432 double cc(RRTNode &t)
436 while (n != nullptr) {