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/>.
35 extern SDL_Window* gw;
36 extern SDL_GLContext gc;
43 pthread_mutex_lock(&this->m_);
44 bool ret = this->changed_;
45 pthread_mutex_unlock(&this->m_);
49 std::vector<RRTNode *> Cell::nodes()
51 pthread_mutex_lock(&this->m_);
52 std::vector<RRTNode *> ret(this->nodes_);
53 pthread_mutex_unlock(&this->m_);
57 void Cell::add_node(RRTNode *n)
59 pthread_mutex_lock(&this->m_);
60 this->nodes_.push_back(n);
61 this->changed_ = true;
62 pthread_mutex_unlock(&this->m_);
67 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
68 // please uncommend the following code:
70 // for (auto n: this->nodes_)
71 // if (n != this->root_)
73 // for (auto n: this->dnodes_)
74 // if (n != this->root_ && n != this->goal_)
76 // for (auto s: this->samples_)
77 // if (s != this->goal_)
79 // for (auto edges: this->rlog_)
80 // for (auto e: edges)
82 // delete this->root_;
83 // delete this->goal_;
90 this->nodes_.reserve(NOFNODES);
91 this->nodes_.push_back(this->root_);
92 this->add_iy(this->root_);
93 this->add_ixy(this->root_);
96 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
100 this->nodes_.reserve(NOFNODES);
101 this->nodes_.push_back(init);
107 RRTNode *RRTBase::root()
112 RRTNode *RRTBase::goal()
117 std::vector<RRTNode *> &RRTBase::nodes()
122 std::vector<RRTNode *> &RRTBase::dnodes()
124 return this->dnodes_;
127 std::vector<RRTNode *> &RRTBase::samples()
129 return this->samples_;
132 std::vector<CircleObstacle> *RRTBase::co()
134 return this->cobstacles_;
137 std::vector<SegmentObstacle> *RRTBase::so()
139 return this->sobstacles_;
142 std::vector<float> &RRTBase::clog()
147 std::vector<float> &RRTBase::nlog()
152 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
157 std::vector<float> &RRTBase::slog()
162 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
167 bool RRTBase::goal_found()
169 return this->goal_found_;
172 float RRTBase::elapsed()
174 std::chrono::duration<float> dt;
175 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
176 this->tend_ - this->tstart_);
181 void RRTBase::root(RRTNode *node)
186 void RRTBase::goal(RRTNode *node)
191 bool RRTBase::logr(RRTNode *root)
193 std::vector<RRTEdge *> e; // Edges to log
194 std::vector<RRTNode *> s; // DFS stack
195 std::vector<RRTNode *> r; // reset visited_
198 while (s.size() > 0) {
203 for (auto ch: tmp->children()) {
205 e.push_back(new RRTEdge(tmp, ch));
211 this->rlog_.push_back(e);
215 float RRTBase::ocost(RRTNode *n)
218 for (auto o: *this->cobstacles_)
219 if (o.dist_to(n) < dist)
221 for (auto o: *this->sobstacles_)
222 if (o.dist_to(n) < dist)
224 return n->ocost(dist);
227 bool RRTBase::tlog(std::vector<RRTNode *> t)
230 this->slog_.push_back(this->elapsed());
231 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
232 this->nlog_.push_back(this->nodes_.size());
233 this->tlog_.push_back(t);
240 void RRTBase::tstart()
242 this->tstart_ = std::chrono::high_resolution_clock::now();
247 this->tend_ = std::chrono::high_resolution_clock::now();
250 bool RRTBase::link_obstacles(
251 std::vector<CircleObstacle> *cobstacles,
252 std::vector<SegmentObstacle> *sobstacles)
254 this->cobstacles_ = cobstacles;
255 this->sobstacles_ = sobstacles;
256 if (!this->cobstacles_ || !this->sobstacles_) {
262 bool RRTBase::add_iy(RRTNode *n)
269 this->iy_[i].push_back(n);
273 bool RRTBase::add_ixy(RRTNode *n)
275 int ix = IXI(n->x());
280 int iy = IYI(n->y());
285 this->ixy_[ix][iy].add_node(n);
289 bool RRTBase::goal_found(bool f)
291 this->goal_found_ = f;
296 bool RRTBase::glplot()
298 glClear(GL_COLOR_BUFFER_BIT);
303 for (auto o: *this->sobstacles_) {
305 glVertex2f(GLVERTEX(o.init()));
306 glVertex2f(GLVERTEX(o.goal()));
313 glVertex2f(GLVERTEX(this->root_));
314 glVertex2f(GLVERTEX(this->goal_));
317 if (this->samples_.size() > 0) {
321 glVertex2f(GLVERTEX(this->samples_.back()));
325 std::vector<RRTNode *> s; // DFS stack
326 std::vector<RRTNode *> r; // reset visited_
329 s.push_back(this->root_);
330 while (s.size() > 0) {
335 for (auto ch: tmp->children()) {
337 glColor3f(0.5, 0.5, 0.5);
338 glVertex2f(GLVERTEX(tmp));
339 glVertex2f(GLVERTEX(ch));
344 // Plot nodes (from goal)
346 s.push_back(this->goal_);
347 while (s.size() > 0) {
352 for (auto ch: tmp->children()) {
354 glColor3f(0.5, 0.5, 0.5);
355 glVertex2f(GLVERTEX(tmp));
356 glVertex2f(GLVERTEX(ch));
361 std::vector<RRTNode *> cusps;
362 // Plot last trajectory
363 if (this->tlog().size() > 0) {
366 for (auto n: this->tlog().back()) {
369 glVertex2f(GLVERTEX(n));
370 glVertex2f(GLVERTEX(n->parent()));
371 if (sgn(n->s()) != sgn(n->parent()->s()))
380 for (auto n: cusps) {
382 glVertex2f(GLVERTEX(n));
385 SDL_GL_SwapWindow(gw);
391 bool RRTBase::goal_found(
393 float (*cost)(RRTNode *, RRTNode* ))
395 if (IS_NEAR(node, this->goal_)) {
396 if (this->goal_found_) {
397 if (node->ccost() + this->cost(node, this->goal_) <
398 this->goal_->ccost()) {
399 RRTNode *op; // old parent
400 float oc; // old cumulative cost
401 float od; // old direct cost
402 op = this->goal_->parent();
403 oc = this->goal_->ccost();
404 od = this->goal_->dcost();
405 node->add_child(this->goal_,
406 this->cost(node, this->goal_));
407 if (this->collide(node, this->goal_)) {
408 node->children().pop_back();
409 this->goal_->parent(op);
410 this->goal_->ccost(oc);
411 this->goal_->dcost(od);
413 op->rem_child(this->goal_);
422 this->cost(node, this->goal_));
423 if (this->collide(node, this->goal_)) {
424 node->children().pop_back();
425 this->goal_->remove_parent();
428 this->goal_found_ = true;
435 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
437 std::vector<RRTEdge *> edges;
439 volatile bool col = false;
441 while (tmp != init) {
442 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
443 std::vector<RRTEdge *> bcframe = bc.frame();
444 #pragma omp parallel for reduction(|: col)
445 for (i = 0; i < (*this->cobstacles_).size(); i++) {
446 if ((*this->cobstacles_)[i].collide(tmp)) {
449 for (auto &e: bcframe) {
450 if ((*this->cobstacles_)[i].collide(e)) {
456 for (auto e: bcframe) {
461 for (auto e: edges) {
466 #pragma omp parallel for reduction(|: col)
467 for (i = 0; i < (*this->sobstacles_).size(); i++) {
468 for (auto &e: bcframe) {
469 if ((*this->sobstacles_)[i].collide(e)) {
475 for (auto e: bcframe) {
480 for (auto e: edges) {
485 if (!tmp->parent()) {
488 edges.push_back(new RRTEdge(tmp, tmp->parent()));
490 for (auto e: bcframe) {
496 for (auto &e: edges) {
497 #pragma omp parallel for reduction(|: col)
498 for (i = 0; i < (*this->cobstacles_).size(); i++) {
499 if ((*this->cobstacles_)[i].collide(e)) {
504 for (auto e: edges) {
509 #pragma omp parallel for reduction(|: col)
510 for (i = 0; i < (*this->sobstacles_).size(); i++) {
511 if ((*this->sobstacles_)[i].collide(e)) {
516 for (auto e: edges) {
522 for (auto e: edges) {
528 class RRTNodeDijkstra {
530 RRTNodeDijkstra(int i):
536 RRTNodeDijkstra(int i, float c):
542 RRTNodeDijkstra(int i, int p, float c):
561 class RRTNodeDijkstraComparator {
564 const RRTNodeDijkstra& n1,
565 const RRTNodeDijkstra& n2)
571 bool RRTBase::optp_dijkstra(
572 std::vector<RRTNode *> &cusps,
573 std::vector<int> &npi)
575 std::vector<RRTNodeDijkstra> dnodes;
576 for (unsigned int i = 0; i < cusps.size(); i++)
578 dnodes.push_back(RRTNodeDijkstra(
583 dnodes.push_back(RRTNodeDijkstra(
589 std::vector<RRTNodeDijkstra>,
590 RRTNodeDijkstraComparator> pq;
591 RRTNodeDijkstra tmp = dnodes[0];
593 float ch_cost = 9999;
594 std::vector<RRTNode *> steered;
595 while (!pq.empty()) {
598 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
599 ch_cost = dnodes[tmp.ni].c +
600 this->cost(cusps[tmp.ni], cusps[i]);
601 steered = this->steer(cusps[tmp.ni], cusps[i]);
602 for (unsigned int j = 0; j < steered.size() - 1; j++)
603 steered[j]->add_child(steered[j + 1], 1);
606 steered[steered.size() - 1])) {
607 for (auto n: steered)
611 if (ch_cost < dnodes[i].c) {
612 dnodes[i].c = ch_cost;
613 dnodes[i].pi = tmp.ni;
617 for (auto n: steered)
621 unsigned int tmpi = 0;
622 for (auto n: dnodes) {
623 if (n.v && n.ni > tmpi)
628 tmpi = dnodes[tmpi].pi;
631 std::reverse(npi.begin(), npi.end());
635 bool RRTBase::optp_rrp(
636 std::vector<RRTNode *> &cusps,
637 std::vector<int> &npi)
639 std::vector<RRTNode *> steered;
640 std::vector<int> candidates;
641 RRTNode *x_j = nullptr;
642 RRTNode *x_i = nullptr;
643 int j = cusps.size() - 1;
657 for (int i = 0; i < j; i++) {
658 steered = this->steer(cusps[i], x_j);
659 for (unsigned int k = 0; k < steered.size() - 1; k++)
660 steered[k]->add_child(steered[k + 1], 1);
663 steered[steered.size() - 1]))
664 candidates.push_back(i);
666 if (candidates.size() <= 0)
668 i_min = candidates[0];
671 for (auto c: candidates) {
673 dx = x_j->x() - x_i->x();
674 dy = x_j->y() - x_i->y();
675 ed = EDIST(x_i, x_j);
676 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
677 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
688 std::reverse(npi.begin(), npi.end());
692 bool RRTBase::optp_smart(
693 std::vector<RRTNode *> &cusps,
694 std::vector<int> &npi)
696 std::vector<RRTNode *> steered;
697 int li = cusps.size() - 1;
701 steered = this->steer(cusps[ai - 1], cusps[li]);
702 for (unsigned int j = 0; j < steered.size() - 1; j++)
703 steered[j]->add_child(steered[j + 1], 1);
704 if (this->collide(steered[0], steered[steered.size() - 1])) {
709 for (auto n: steered)
713 std::reverse(npi.begin(), npi.end());
717 bool RRTBase::opt_path()
719 if (this->tlog().size() == 0)
721 float oc = this->tlog().back().front()->ccost();
722 std::vector<RRTNode *> tmp_cusps;
723 for (auto n: this->tlog().back()) {
724 if (sgn(n->s()) == 0) {
725 tmp_cusps.push_back(n);
726 } else if (n->parent() &&
727 sgn(n->s()) != sgn(n->parent()->s())) {
728 tmp_cusps.push_back(n);
729 tmp_cusps.push_back(n->parent());
731 //tmp_cusps.push_back(n);
733 if (tmp_cusps.size() < 2)
735 std::vector<RRTNode *> cusps;
736 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
737 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
738 cusps.push_back(tmp_cusps[i]);
740 std::reverse(cusps.begin(), cusps.end());
741 std::vector<int> npi; // new path indexes
742 if (!this->optp_dijkstra(cusps, npi))
744 RRTNode *pn = cusps[npi[0]];
745 RRTNode *tmp = nullptr;
747 for (unsigned int i = 0; i < npi.size() - 1; i++) {
749 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
752 } else if (IS_NEAR(cusps[npi[i]], ns)) {
754 while (tmp && tmp != cusps[npi[i]]) {
760 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
762 cusps[npi[i + 1]]->parent()->rem_child(
766 this->cost(pn, cusps[npi[i + 1]]));
768 } else if (IS_NEAR(pn, ns)) {
771 this->nodes().push_back(ns);
774 pn->add_child(ns, this->cost(pn, ns));
779 this->root()->update_ccost();
780 if (this->tlog().back().front()->ccost() < oc)
785 bool RRTBase::rebase(RRTNode *nr)
787 if (!nr || this->goal_ == nr || this->root_ == nr)
789 std::vector<RRTNode *> s; // DFS stack
792 unsigned int to_del = 0;
794 s.push_back(this->root_);
795 while (s.size() > 0) {
798 for (auto ch: tmp->children()) {
802 to_del = this->nodes_.size();
803 #pragma omp parallel for reduction(min: to_del)
804 for (i = 0; i < this->nodes_.size(); i++) {
805 if (this->nodes_[i] == tmp)
808 if (to_del < this->nodes_.size())
809 this->nodes_.erase(this->nodes_.begin() + to_del);
811 to_del = this->iy_[iy].size();
812 #pragma omp parallel for reduction(min: to_del)
813 for (i = 0; i < this->iy_[iy].size(); i++) {
814 if (this->iy_[iy][i] == tmp)
817 if (to_del < this->iy_[iy].size())
818 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
819 this->dnodes().push_back(tmp);
822 this->root_->remove_parent();
826 std::vector<RRTNode *> RRTBase::findt()
828 return this->findt(this->goal_);
831 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
833 std::vector<RRTNode *> nodes;
834 if (!n || !n->parent())
844 RRTNode *RRTBase::sample()
849 float RRTBase::cost(RRTNode *init, RRTNode *goal)
851 return co2(init, goal);
854 RRTNode *RRTBase::nn(RRTNode *rs)
856 return nn4(this->iy_, rs, nullptr);
857 //return nn3(this->iy_, rs, nullptr);
860 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
862 std::vector<RRTNode *> nvs;
863 unsigned int iy = IYI(node->y());
864 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
865 unsigned int i = 0; // vector index
866 unsigned int j = 0; // array index
867 unsigned int jmin = 0; // minimal j index
868 unsigned int jmax = 0; // maximal j index
870 jmin = (jmin > 0) ? jmin : 0;
871 jmax = iy + iy_dist + 1;
872 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
873 #pragma omp parallel for reduction(merge: nvs)
874 for (j = jmin; j < jmax; j++) {
875 #pragma omp parallel for reduction(merge: nvs)
876 for (i = 0; i < this->iy_[j].size(); i++) {
877 if (this->cost(this->iy_[j][i], node) < dist) {
878 nvs.push_back(this->iy_[j][i]);
885 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
887 return st3(init, goal);
890 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
892 return st3(init, goal, step);