4 #include "reeds_shepp.h"
6 #define ETA 1.0 // for steer, nv
8 __typeof__ (cV) _cV = (cV); \
9 pow(log(_cV) / _cV, 1.0 / 3.0); \
16 RRTNode::RRTNode(const BicycleCar &bc) : BicycleCar(bc)
25 std::tuple<bool, unsigned int, unsigned int>
26 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
28 for (auto &o: this->obstacles())
29 if (std::get<0>(::collide(poly, o.poly())))
30 return ::collide(poly, o.poly());
31 return std::make_tuple(false, 0, 0);
34 std::tuple<bool, unsigned int, unsigned int>
35 RRTS::collide_steered_from(RRTNode &f)
37 std::vector<std::tuple<double, double>> s;
38 s.push_back(std::make_tuple(f.x(), f.y()));
39 for (auto &n: this->steered()) {
40 s.push_back(std::make_tuple(n.lfx(), n.lfy()));
41 s.push_back(std::make_tuple(n.lrx(), n.lry()));
42 s.push_back(std::make_tuple(n.rrx(), n.rry()));
43 s.push_back(std::make_tuple(n.rfx(), n.rfy()));
45 auto col = this->collide(s);
46 auto strip_from = this->steered().size() - std::get<1>(col) / 4;
47 if (std::get<0>(col) && strip_from > 0) {
48 while (strip_from-- > 0) {
49 this->steered().pop_back();
51 return this->collide_steered_from(f);
56 std::tuple<bool, unsigned int, unsigned int>
57 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
59 std::vector<std::tuple<double, double>> p;
60 p.push_back(std::make_tuple(f.lfx(), f.lfy()));
61 p.push_back(std::make_tuple(f.lrx(), f.lry()));
62 p.push_back(std::make_tuple(f.rrx(), f.rry()));
63 p.push_back(std::make_tuple(f.rfx(), f.rfy()));
64 p.push_back(std::make_tuple(t.lfx(), t.lfy()));
65 p.push_back(std::make_tuple(t.lrx(), t.lry()));
66 p.push_back(std::make_tuple(t.rrx(), t.rry()));
67 p.push_back(std::make_tuple(t.rfx(), t.rfy()));
68 return this->collide(p);
71 double RRTS::cost_build(RRTNode &f, RRTNode &t)
74 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
78 double RRTS::cost_search(RRTNode &f, RRTNode &t)
81 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
87 double x = this->ndx_(this->gen_);
88 double y = this->ndy_(this->gen_);
89 double h = this->ndh_(this->gen_);
90 this->samples().push_back(RRTNode());
91 this->samples().back().x(x);
92 this->samples().back().y(y);
93 this->samples().back().h(h);
96 RRTNode *RRTS::nn(RRTNode &t)
98 RRTNode *nn = &this->nodes().front();
99 double cost = this->cost_search(*nn, t);
100 for (auto &f: this->nodes()) {
101 if (this->cost_search(f, t) < cost) {
103 cost = this->cost_search(f, t);
109 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
111 std::vector<RRTNode *> nv;
112 double cost = std::min(GAMMA(this->nodes().size()), ETA);
113 for (auto &f: this->nodes())
114 if (this->cost_search(f, t) < cost)
119 int cb_rs_steer(double q[3], void *user_data)
121 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
122 nodes->push_back(RRTNode());
123 nodes->back().x(q[0]);
124 nodes->back().y(q[1]);
125 nodes->back().h(q[2]);
129 void RRTS::steer(RRTNode &f, RRTNode &t)
131 this->steered().clear();
132 double q0[] = {f.x(), f.y(), f.h()};
133 double q1[] = {t.x(), t.y(), t.h()};
134 ReedsSheppStateSpace rsss(f.mtr());
135 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
138 void RRTS::join_steered(RRTNode *f)
140 while (this->steered().size() > 0) {
141 this->nodes().push_back(this->steered().front());
142 RRTNode *t = &this->nodes().back();
144 t->c(this->cost_build(*f, *t));
145 this->steered().erase(this->steered().begin());
150 bool RRTS::goal_found(RRTNode &f)
153 for (auto &g: this->goals()) {
154 double cost = this->cost_build(f, g);
156 pow(f.x() - g.x(), 2)
157 + pow(f.y() - g.y(), 2)
159 double adist = std::abs(f.h() - g.h());
160 if (edist < 0.05 && adist < M_PI / 32) {
162 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
174 RRTNode *t = &this->steered().front();
175 RRTNode *f = this->nn(this->samples().back());
176 double cost = this->cost_search(*f, *t);
177 for (auto n: this->nv(*t)) {
179 !std::get<0>(this->collide_two_nodes(*n, *t))
180 && this->cost_search(*n, *t) < cost
183 cost = this->cost_search(*n, *t);
186 this->nodes().push_back(this->steered().front());
187 t = &this->nodes().back();
189 t->c(this->cost_build(*f, *t));
195 RRTNode *f = &this->nodes().back();
196 for (auto n: this->nv(*f)) {
198 !std::get<0>(this->collide_two_nodes(*f, *n))
199 && cc(*f) + this->cost_search(*f, *n) < cc(*n)
202 n->c(this->cost_build(*f, *n));
208 std::vector<RRTNode *> RRTS::path()
210 std::vector<RRTNode *> path;
211 if (this->goals().size() == 0)
213 RRTNode *goal = &this->goals().front();
214 for (auto &n: this->goals()) {
217 && (n.c() < goal->c() || goal->p() == nullptr)
222 if (goal->p() == nullptr)
224 while (goal != nullptr) {
225 path.push_back(goal);
228 std::reverse(path.begin(), path.end());
235 if (++this->icnt_ > 999)
239 *this->nn(this->samples().back()),
240 this->samples().back()
242 if (std::get<0>(this->collide_steered_from(
243 *this->nn(this->samples().back())
246 if (!this->connect())
249 unsigned scnt = this->steered().size();
250 this->steered().erase(this->steered().begin());
251 this->join_steered(&this->nodes().back());
252 RRTNode *just_added = &this->nodes().back();
255 for (auto &g: this->goals()) {
256 this->steer(*just_added, g);
257 if (std::get<0>(this->collide_steered_from(
261 this->join_steered(just_added);
263 next = !this->goal_found(this->nodes().back());
264 just_added = just_added->p();
269 void RRTS::set_sample(
270 double mx, double dx,
271 double my, double dy,
275 this->ndx_ = std::normal_distribution<double>(mx, dx);
276 this->ndy_ = std::normal_distribution<double>(my, dy);
277 this->ndh_ = std::normal_distribution<double>(mh, dh);
281 : gen_(std::random_device{}())
283 this->goals().reserve(100);
284 this->nodes().reserve(4000000);
285 this->samples().reserve(1000);
286 this->steered().reserve(20000);
287 this->nodes().push_back(RRTNode()); // root
290 double cc(RRTNode &t)
294 while (n != nullptr) {