2 This file is part of I am car.
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
32 extern SDL_Window* gw;
33 extern SDL_GLContext gc;
37 for (auto n: this->nodes_)
40 for (auto n: this->dnodes_)
41 if (n != this->root_ && n != this->goal_)
43 for (auto s: this->samples_)
46 for (auto edges: this->rlog_)
57 this->nodes_.push_back(this->root_);
58 this->add_iy(this->root_);
61 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
65 this->nodes_.push_back(init);
69 RRTNode *RRTBase::root()
74 RRTNode *RRTBase::goal()
79 std::vector<RRTNode *> &RRTBase::nodes()
84 std::vector<RRTNode *> &RRTBase::dnodes()
89 std::vector<RRTNode *> &RRTBase::samples()
91 return this->samples_;
94 std::vector<CircleObstacle> *RRTBase::cos()
96 return this->cobstacles_;
99 std::vector<SegmentObstacle> *RRTBase::sos()
101 return this->sobstacles_;
104 std::vector<float> &RRTBase::clog()
109 std::vector<float> &RRTBase::nlog()
114 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
119 std::vector<float> &RRTBase::slog()
124 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
129 bool RRTBase::goal_found()
131 return this->goal_found_;
134 float RRTBase::elapsed()
136 std::chrono::duration<float> dt;
137 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
138 this->tend_ - this->tstart_);
142 void RRTBase::root(RRTNode *node)
147 void RRTBase::goal(RRTNode *node)
152 bool RRTBase::logr(RRTNode *root)
154 std::vector<RRTEdge *> e; // Edges to log
155 std::vector<RRTNode *> s; // DFS stack
156 std::vector<RRTNode *> r; // reset visited_
159 while (s.size() > 0) {
164 for (auto ch: tmp->children()) {
166 e.push_back(new RRTEdge(tmp, ch));
172 this->rlog_.push_back(e);
176 float RRTBase::ocost(RRTNode *n)
179 for (auto o: *this->cobstacles_)
180 if (o.dist_to(n) < dist)
182 for (auto o: *this->sobstacles_)
183 if (o.dist_to(n) < dist)
185 return n->ocost(dist);
188 bool RRTBase::tlog(std::vector<RRTNode *> t)
191 this->slog_.push_back(this->elapsed());
192 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
193 this->nlog_.push_back(this->nodes_.size());
194 this->tlog_.push_back(t);
201 void RRTBase::tstart()
203 this->tstart_ = std::chrono::high_resolution_clock::now();
208 this->tend_ = std::chrono::high_resolution_clock::now();
211 bool RRTBase::link_obstacles(
212 std::vector<CircleObstacle> *cobstacles,
213 std::vector<SegmentObstacle> *sobstacles)
215 this->cobstacles_ = cobstacles;
216 this->sobstacles_ = sobstacles;
217 if (!this->cobstacles_ || !this->sobstacles_) {
223 bool RRTBase::add_iy(RRTNode *n)
230 this->iy_[i].push_back(n);
234 bool RRTBase::goal_found(bool f)
236 this->goal_found_ = f;
240 bool RRTBase::glplot()
242 glClear(GL_COLOR_BUFFER_BIT);
247 for (auto o: *this->sobstacles_) {
249 glVertex2f(GLVERTEX(o.init()));
250 glVertex2f(GLVERTEX(o.goal()));
257 glVertex2f(GLVERTEX(this->root_));
258 glVertex2f(GLVERTEX(this->goal_));
261 if (this->samples_.size() > 0) {
265 glVertex2f(GLVERTEX(this->samples_.back()));
269 std::vector<RRTNode *> s; // DFS stack
270 std::vector<RRTNode *> r; // reset visited_
273 s.push_back(this->root_);
274 while (s.size() > 0) {
279 for (auto ch: tmp->children()) {
281 glColor3f(0.5, 0.5, 0.5);
282 glVertex2f(GLVERTEX(tmp));
283 glVertex2f(GLVERTEX(ch));
288 std::vector<RRTNode *> cusps;
289 // Plot last trajectory
290 if (this->tlog().size() > 0) {
293 for (auto n: this->tlog().back()) {
296 glVertex2f(GLVERTEX(n));
297 glVertex2f(GLVERTEX(n->parent()));
298 if (sgn(n->s()) != sgn(n->parent()->s()))
307 for (auto n: cusps) {
309 glVertex2f(GLVERTEX(n));
312 SDL_GL_SwapWindow(gw);
318 bool RRTBase::goal_found(
320 float (*cost)(RRTNode *, RRTNode* ))
322 float xx = pow(node->x() - this->goal_->x(), 2);
323 float yy = pow(node->y() - this->goal_->y(), 2);
324 float dh = std::abs(node->h() - this->goal_->h());
325 if (IS_NEAR(node, this->goal_)) {
326 if (this->goal_found_) {
327 if (node->ccost() + (*cost)(node, this->goal_) <
328 this->goal_->ccost()) {
329 RRTNode *op; // old parent
330 float oc; // old cumulative cost
331 float od; // old direct cost
332 op = this->goal_->parent();
333 oc = this->goal_->ccost();
334 od = this->goal_->dcost();
335 node->add_child(this->goal_,
336 (*cost)(node, this->goal_));
337 if (this->collide(node, this->goal_)) {
338 node->children().pop_back();
339 this->goal_->parent(op);
340 this->goal_->ccost(oc);
341 this->goal_->dcost(od);
343 op->rem_child(this->goal_);
352 (*cost)(node, this->goal_));
353 if (this->collide(node, this->goal_)) {
354 node->children().pop_back();
355 this->goal_->remove_parent();
358 this->goal_found_ = true;
365 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
367 std::vector<RRTEdge *> edges;
369 volatile bool col = false;
371 while (tmp != init) {
372 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
373 std::vector<RRTEdge *> bcframe = bc.frame();
374 #pragma omp parallel for reduction(|: col)
375 for (i = 0; i < (*this->cobstacles_).size(); i++) {
376 if ((*this->cobstacles_)[i].collide(tmp)) {
379 for (auto &e: bcframe) {
380 if ((*this->cobstacles_)[i].collide(e)) {
386 for (auto e: bcframe) {
391 for (auto e: edges) {
396 #pragma omp parallel for reduction(|: col)
397 for (i = 0; i < (*this->sobstacles_).size(); i++) {
398 for (auto &e: bcframe) {
399 if ((*this->sobstacles_)[i].collide(e)) {
405 for (auto e: bcframe) {
410 for (auto e: edges) {
415 if (!tmp->parent()) {
418 edges.push_back(new RRTEdge(tmp, tmp->parent()));
420 for (auto e: bcframe) {
426 for (auto &e: edges) {
427 #pragma omp parallel for reduction(|: col)
428 for (i = 0; i < (*this->cobstacles_).size(); i++) {
429 if ((*this->cobstacles_)[i].collide(e)) {
434 for (auto e: edges) {
439 #pragma omp parallel for reduction(|: col)
440 for (i = 0; i < (*this->sobstacles_).size(); i++) {
441 if ((*this->sobstacles_)[i].collide(e)) {
446 for (auto e: edges) {
452 for (auto e: edges) {
458 class RRTNodeDijkstra {
460 RRTNodeDijkstra(int i):
466 RRTNodeDijkstra(int i, float c):
472 RRTNodeDijkstra(int i, int p, float c):
491 class RRTNodeDijkstraComparator {
494 const RRTNodeDijkstra& n1,
495 const RRTNodeDijkstra& n2)
501 bool RRTBase::opt_path()
503 if (this->tlog().size() == 0)
505 float oc = this->tlog().back().front()->ccost();
506 std::vector<RRTNode *> tmp_cusps;
507 for (auto n: this->tlog().back()) {
508 if (sgn(n->s()) == 0) {
509 tmp_cusps.push_back(n);
510 } else if (n->parent() &&
511 sgn(n->s()) != sgn(n->parent()->s())) {
512 tmp_cusps.push_back(n);
513 tmp_cusps.push_back(n->parent());
516 if (tmp_cusps.size() < 2)
518 std::vector<RRTNode *> cusps;
519 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
520 if (tmp_cusps[i] != tmp_cusps[i + 1])
521 cusps.push_back(tmp_cusps[i]);
523 std::reverse(cusps.begin(), cusps.end());
525 std::vector<RRTNodeDijkstra> dnodes;
526 for (unsigned int i = 0; i < cusps.size(); i++)
528 dnodes.push_back(RRTNodeDijkstra(
533 dnodes.push_back(RRTNodeDijkstra(
539 std::vector<RRTNodeDijkstra>,
540 RRTNodeDijkstraComparator> pq;
541 RRTNodeDijkstra tmp = dnodes[0];
543 float ch_cost = 9999;
544 std::vector<RRTNode *> steered;
545 while (!pq.empty() && tmp.ni != cusps.size() - 1) {
548 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
549 ch_cost = dnodes[tmp.ni].c +
550 CO(cusps[tmp.ni], cusps[i]);
551 steered = ST(cusps[tmp.ni], cusps[i]);
552 for (unsigned int j = 0; j < steered.size() - 1; j++)
553 steered[j]->add_child(
558 if (i != tmp.ni + 1 && this->collide( // TODO
560 steered[steered.size() - 1]))
562 if (ch_cost < dnodes[i].c) {
563 dnodes[i].c = ch_cost;
564 dnodes[i].pi = tmp.ni;
570 if (tmp.ni != cusps.size() - 1)
572 std::vector<int> npi; // new path indexes
576 tmpi = dnodes[tmpi].pi;
579 std::reverse(npi.begin(), npi.end());
580 RRTNode *pn = cusps[npi[0]];
581 for (unsigned int i = 0; i < npi.size() - 1; i++) {
582 for (auto ns: ST(cusps[npi[i]], cusps[npi[i + 1]])) {
583 pn->add_child(ns, CO(pn, ns));
588 this->tlog().back().front(),
589 CO(pn, this->tlog().back().front()));
591 if (this->tlog().back().front()->ccost() < oc)
596 bool RRTBase::rebase(RRTNode *nr)
598 if (!nr || this->goal_ == nr || this->root_ == nr)
600 std::vector<RRTNode *> s; // DFS stack
603 unsigned int to_del = 0;
605 s.push_back(this->root_);
606 while (s.size() > 0) {
609 for (auto ch: tmp->children()) {
613 to_del = this->nodes_.size();
614 #pragma omp parallel for reduction(min: to_del)
615 for (i = 0; i < this->nodes_.size(); i++) {
616 if (this->nodes_[i] == tmp)
619 if (to_del < this->nodes_.size())
620 this->nodes_.erase(this->nodes_.begin() + to_del);
623 to_del = this->iy_[iy].size();
624 #pragma omp parallel for reduction(min: to_del)
625 for (i = 0; i < this->iy_[iy].size(); i++) {
626 if (this->iy_[iy][i] == tmp)
629 if (to_del < this->iy_[iy].size())
630 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
632 this->dnodes().push_back(tmp);
635 this->root_->remove_parent();
639 std::vector<RRTNode *> RRTBase::findt()
641 return this->findt(this->goal_);
644 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
646 std::vector<RRTNode *> nodes;
647 if (!n || !n->parent())
650 while (tmp != this->root()) {
651 nodes.push_back(tmp);