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(RRTNode &f, RRTNode &t)
74 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
78 double RRTS::cost_build(RRTNode &f, RRTNode &t)
80 double q0[] = {f.x(), f.y(), f.h()};
81 double q1[] = {t.x(), t.y(), t.h()};
82 ReedsSheppStateSpace rsss(f.mtr());
83 return rsss.distance(q0, q1);
86 double RRTS::cost_search(RRTNode &f, RRTNode &t)
89 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
90 double heur = std::min(
91 std::abs(t.h() - f.h()),
92 2 * M_PI - std::abs(t.h() - f.h())
95 cost = std::max(cost, heur);
101 double x = this->ndx_(this->gen_);
102 double y = this->ndy_(this->gen_);
103 double h = this->ndh_(this->gen_);
104 this->samples().push_back(RRTNode());
105 this->samples().back().x(x);
106 this->samples().back().y(y);
107 this->samples().back().h(h);
110 RRTNode *RRTS::nn(RRTNode &t)
112 RRTNode *nn = &this->nodes().front();
113 double cost = this->cost(*nn, t);
114 for (auto &f: this->nodes()) {
115 if (this->cost(f, t) < cost) {
117 cost = this->cost(f, t);
123 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
125 std::vector<RRTNode *> nv;
126 double cost = std::min(GAMMA(this->nodes().size()), ETA);
127 for (auto &f: this->nodes())
128 if (this->cost(f, t) < cost)
133 int cb_rs_steer(double q[3], void *user_data)
135 std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
136 nodes->push_back(RRTNode());
137 nodes->back().x(q[0]);
138 nodes->back().y(q[1]);
139 nodes->back().h(q[2]);
143 void RRTS::steer(RRTNode &f, RRTNode &t)
145 this->steered().clear();
146 double q0[] = {f.x(), f.y(), f.h()};
147 double q1[] = {t.x(), t.y(), t.h()};
148 ReedsSheppStateSpace rsss(f.mtr());
149 rsss.sample(q0, q1, 0.5, cb_rs_steer, &this->steered());
152 void RRTS::join_steered(RRTNode *f)
154 while (this->steered().size() > 0) {
155 this->nodes().push_back(this->steered().front());
156 RRTNode *t = &this->nodes().back();
158 t->c(this->cost(*f, *t));
159 this->steered().erase(this->steered().begin());
164 bool RRTS::goal_found(RRTNode &f)
167 for (auto &g: this->goals()) {
168 double cost = this->cost(f, g);
170 pow(f.x() - g.x(), 2)
171 + pow(f.y() - g.y(), 2)
173 double adist = std::abs(f.h() - g.h());
174 if (edist < 0.05 && adist < M_PI / 32) {
176 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
189 RRTNode *t = &this->steered().front();
190 RRTNode *f = this->nn(this->samples().back());
191 double cost = this->cost(*f, *t);
192 for (auto n: this->nv(*t)) {
194 !std::get<0>(this->collide_two_nodes(*n, *t))
195 && this->cost(*n, *t) < cost
198 cost = this->cost(*n, *t);
201 this->nodes().push_back(this->steered().front());
202 t = &this->nodes().back();
204 t->c(this->cost(*f, *t));
211 RRTNode *f = &this->nodes().back();
212 for (auto n: this->nv(*f)) {
214 !std::get<0>(this->collide_two_nodes(*f, *n))
215 && cc(*f) + this->cost(*f, *n) < cc(*n)
222 std::vector<RRTNode *> RRTS::path()
224 std::vector<RRTNode *> path;
225 if (this->goals().size() == 0)
227 RRTNode *goal = &this->goals().front();
228 for (auto &n: this->goals()) {
231 && (n.c() < goal->c() || goal->p() == nullptr)
236 if (goal->p() == nullptr)
238 while (goal != nullptr) {
239 path.push_back(goal);
242 std::reverse(path.begin(), path.end());
252 *this->nn(this->samples().back()),
253 this->samples().back()
255 if (std::get<0>(this->collide_steered_from(
256 *this->nn(this->samples().back())
259 if (!this->connect())
262 unsigned scnt = this->steered().size();
263 this->steered().erase(this->steered().begin());
264 this->join_steered(&this->nodes().back());
265 RRTNode *just_added = &this->nodes().back();
268 for (auto &g: this->goals()) {
269 this->steer(*just_added, g);
270 if (std::get<0>(this->collide_steered_from(
274 this->join_steered(just_added);
276 next = !this->goal_found(this->nodes().back());
277 just_added = just_added->p();
279 if (this->icnt_ > 999)
284 void RRTS::set_sample(
285 double mx, double dx,
286 double my, double dy,
290 this->ndx_ = std::normal_distribution<double>(mx, dx);
291 this->ndy_ = std::normal_distribution<double>(my, dy);
292 this->ndh_ = std::normal_distribution<double>(mh, dh);
296 : gen_(std::random_device{}())
298 this->goals().reserve(100);
299 this->nodes().reserve(4000000);
300 this->samples().reserve(1000);
301 this->steered().reserve(20000);
302 this->nodes().push_back(RRTNode()); // root
305 double cc(RRTNode &t)
309 while (n != nullptr) {