4 #define ETA 1.0 // for steer, nv
6 __typeof__ (cV) _cV = (cV); \
7 pow(log(_cV) / _cV, 1.0 / 3.0); \
19 bool RRTS::collide(std::vector<std::tuple<double, double>> &poly)
24 double RRTS::cost(RRTNode &f, RRTNode &t)
27 cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
33 double x = this->ndx_(this->gen_);
34 double y = this->ndy_(this->gen_);
35 double h = this->ndh_(this->gen_);
36 this->samples().push_back(RRTNode());
37 this->samples().back().x(x);
38 this->samples().back().y(y);
39 this->samples().back().h(h);
42 RRTNode *RRTS::nn(RRTNode &t)
44 RRTNode *nn = &this->nodes().front();
45 double cost = this->cost(*nn, t);
46 for (auto &f: this->nodes()) {
47 if (this->cost(f, t) < cost) {
49 cost = this->cost(f, t);
55 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
57 std::vector<RRTNode *> nv;
58 double cost = std::min(GAMMA(this->nodes().size()), ETA);
59 for (auto &f: this->nodes())
60 if (this->cost(f, t) < cost)
65 void RRTS::steer(RRTNode &f, RRTNode &t)
67 double angl = atan2(t.y() - f.y(), t.x() - f.x());
68 this->steered().clear();
69 this->steered().push_back(RRTNode());
70 this->steered().back().x(f.x() + ETA * cos(angl));
71 this->steered().back().y(f.y() + ETA * sin(angl));
72 this->steered().back().h(angl);
79 RRTNode *t = &this->steered().front();
80 RRTNode *f = this->nn(this->samples().back());
81 double cost = this->cost(*f, *t);
82 for (auto n: this->nv(*t)) {
83 if (this->cost(*n, *t) < cost) {
85 cost = this->cost(*n, *t);
88 this->nodes().push_back(this->steered().front());
89 this->steered().erase(this->steered().begin());
90 t = &this->nodes().back();
92 t->c(this->cost(*f, *t));
99 RRTNode *f = &this->nodes().back();
100 for (auto n: this->nv(*f)) {
101 if (cc(*f) + this->cost(*f, *n) < cc(*n))
107 std::vector<RRTNode *> RRTS::path()
109 std::vector<RRTNode *> path;
110 if (this->goals().size() == 0)
112 RRTNode *goal = &this->goals().front();
113 for (auto &n: this->goals()) {
116 && (n.c() < goal->c() || goal->p() == nullptr)
121 if (goal->p() == nullptr)
123 while (goal != nullptr) {
124 path.push_back(goal);
127 std::reverse(path.begin(), path.end());
137 *this->nn(this->samples().back()),
138 this->samples().back()
142 for (auto &n: this->goals()) {
143 double cost = this->cost(this->nodes().back(), n);
148 || cc(this->nodes().back()) + cost < cc(n)
150 n.p(&this->nodes().back());
155 if (this->icnt_ > 999)
160 void RRTS::set_sample(
161 double mx, double dx,
162 double my, double dy,
166 this->ndx_ = std::normal_distribution<double>(mx, dx);
167 this->ndy_ = std::normal_distribution<double>(my, dy);
168 this->ndh_ = std::normal_distribution<double>(mh, dh);
172 : gen_(std::random_device{}())
174 this->goals().reserve(1);
175 this->nodes().reserve(20000);
176 this->samples().reserve(1000);
177 this->steered().reserve(20);
178 this->nodes().push_back(RRTNode()); // root
181 double cc(RRTNode &t)
185 while (n != nullptr) {