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 // but continue by default
46 void RRTS::store_node(RRTNode n)
48 this->nodes().push_back(n);
52 std::tuple<bool, unsigned int, unsigned int>
53 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
55 for (auto &o: this->obstacles())
56 if (std::get<0>(::collide(poly, o.poly())))
57 return ::collide(poly, o.poly());
58 return std::make_tuple(false, 0, 0);
61 std::tuple<bool, unsigned int, unsigned int>
62 RRTS::collide_steered_from(RRTNode &f)
64 std::vector<std::tuple<double, double>> s;
65 s.push_back(std::make_tuple(f.x(), f.y()));
66 for (auto &n: this->steered()) {
67 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
68 s.push_back(std::make_tuple(n.lrx(), n.lry()));
69 s.push_back(std::make_tuple(n.rrx(), n.rry()));
70 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
72 auto col = this->collide(s);
73 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
74 if (std::get<0>(col) && strip_from > 0) {
75 while (strip_from-- > 0) {
76 this->steered().pop_back();
78 return this->collide_steered_from(f);
83 std::tuple<bool, unsigned int, unsigned int>
84 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
86 std::vector<std::tuple<double, double>> p;
87 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
88 p.push_back(std::make_tuple(f.lrx(), f.lry()));
89 p.push_back(std::make_tuple(f.rrx(), f.rry()));
90 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
91 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
92 p.push_back(std::make_tuple(t.lrx(), t.lry()));
93 p.push_back(std::make_tuple(t.rrx(), t.rry()));
94 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
95 return this->collide(p);
98 double RRTS::cost_build(RRTNode &f, RRTNode &t)
101 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
105 double RRTS::cost_search(RRTNode &f, RRTNode &t)
108 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
114 double x = this->ndx_(this->gen_);
115 double y = this->ndy_(this->gen_);
116 double h = this->ndh_(this->gen_);
117 this->samples().push_back(RRTNode());
118 this->samples().back().x(x);
119 this->samples().back().y(y);
120 this->samples().back().h(h);
123 RRTNode *RRTS::nn(RRTNode &t)
125 RRTNode *nn = &this->nodes().front();
126 double cost = this->cost_search(*nn, t);
127 for (auto &f: this->nodes()) {
128 if (this->cost_search(f, t) < cost) {
130 cost = this->cost_search(f, t);
136 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
138 std::vector<RRTNode *> nv;
139 double cost = std::min(GAMMA(this->nodes().size()), ETA);
140 for (auto &f: this->nodes())
141 if (this->cost_search(f, t) < cost)
146 int cb_rs_steer(double q[4], void *user_data)
148 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
149 RRTNode *ln = nullptr;
150 if (nodes->size() > 0)
152 nodes->push_back(RRTNode());
153 nodes->back().x(q[0]);
154 nodes->back().y(q[1]);
155 nodes->back().h(q[2]);
156 nodes->back().sp(q[3]);
157 if (nodes->back().sp() == 0)
158 nodes->back().set_t(RRTNodeType::cusp);
159 else if (ln != nullptr && sgn(ln->sp()) != sgn(nodes->back().sp()))
160 ln->set_t(RRTNodeType::cusp);
164 void RRTS::steer(RRTNode &f, RRTNode &t)
166 this->steered().clear();
167 double q0[] = {f.x(), f.y(), f.h()};
168 double q1[] = {t.x(), t.y(), t.h()};
169 ReedsSheppStateSpace rsss(f.mtr());
170 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
173 void RRTS::join_steered(RRTNode *f)
175 while (this->steered().size() > 0) {
176 this->store_node(this->steered().front());
177 RRTNode *t = &this->nodes().back();
179 t->c(this->cost_build(*f, *t));
180 this->steered().erase(this->steered().begin());
185 bool RRTS::goal_found(RRTNode &f)
188 for (auto &g: this->goals()) {
189 double cost = this->cost_build(f, g);
191 pow(f.x() - g.x(), 2)
192 + pow(f.y() - g.y(), 2)
194 double adist = std::abs(f.h() - g.h());
195 if (edist < 0.05 && adist < M_PI / 32) {
197 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
209 RRTNode *t = &this->steered().front();
210 RRTNode *f = this->nn(this->samples().back());
211 double cost = this->cost_search(*f, *t);
212 for (auto n: this->nv(*t)) {
214 !std::get<0>(this->collide_two_nodes(*n, *t))
215 && this->cost_search(*n, *t) < cost
218 cost = this->cost_search(*n, *t);
221 this->store_node(this->steered().front());
222 t = &this->nodes().back();
224 t->c(this->cost_build(*f, *t));
225 t->set_t(RRTNodeType::connected);
231 RRTNode *f = &this->nodes().back();
232 for (auto n: this->nv(*f)) {
234 !std::get<0>(this->collide_two_nodes(*f, *n))
235 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
238 n->c(this->cost_build(*f, *n));
252 std::vector<RRTNode *> RRTS::path()
254 std::vector<RRTNode *> path;
255 if (this->goals().size() == 0)
257 RRTNode *goal = &this->goals().front();
258 for (auto &n: this->goals()) {
261 && (n.c() < goal->c() || goal->p() == nullptr)
266 if (goal->p() == nullptr)
268 while (goal != nullptr) {
269 path.push_back(goal);
272 std::reverse(path.begin(), path.end());
278 if (this->icnt_ == 0)
279 this->tstart_ = std::chrono::high_resolution_clock::now();
281 if (this->should_stop())
285 *this->nn(this->samples().back()),
286 this->samples().back()
288 if (std::get<0>(this->collide_steered_from(
289 *this->nn(this->samples().back())
292 if (!this->connect())
295 unsigned scnt = this->steered().size();
296 this->steered().erase(this->steered().begin());
297 this->join_steered(&this->nodes().back());
298 RRTNode *just_added = &this->nodes().back();
301 for (auto &g: this->goals()) {
302 this->steer(*just_added, g);
303 if (std::get<0>(this->collide_steered_from(
307 this->join_steered(just_added);
309 this->gf(this->goal_found(this->nodes().back()));
310 just_added = just_added->p();
315 void RRTS::set_sample(
316 double mx, double dx,
317 double my, double dy,
321 this->ndx_ = std::normal_distribution<double>(mx, dx);
322 this->ndy_ = std::normal_distribution<double>(my, dy);
323 this->ndh_ = std::normal_distribution<double>(mh, dh);
326 Json::Value RRTS::json()
333 : gen_(std::random_device{}())
335 this->goals().reserve(100);
336 this->nodes().reserve(4000000);
337 this->samples().reserve(1000);
338 this->steered().reserve(20000);
339 this->store_node(RRTNode()); // root
342 double cc(RRTNode &t)
346 while (n != nullptr) {