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(20000);
91 this->nodes_.push_back(this->root_);
92 this->add_iy(this->root_);
95 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
99 this->nodes_.reserve(20000);
100 this->nodes_.push_back(init);
105 RRTNode *RRTBase::root()
110 RRTNode *RRTBase::goal()
115 std::vector<RRTNode *> &RRTBase::nodes()
120 std::vector<RRTNode *> &RRTBase::dnodes()
122 return this->dnodes_;
125 std::vector<RRTNode *> &RRTBase::samples()
127 return this->samples_;
130 std::vector<CircleObstacle> *RRTBase::co()
132 return this->cobstacles_;
135 std::vector<SegmentObstacle> *RRTBase::so()
137 return this->sobstacles_;
140 std::vector<float> &RRTBase::clog()
145 std::vector<float> &RRTBase::nlog()
150 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
155 std::vector<float> &RRTBase::slog()
160 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
165 bool RRTBase::goal_found()
167 return this->goal_found_;
170 float RRTBase::elapsed()
172 std::chrono::duration<float> dt;
173 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
174 this->tend_ - this->tstart_);
179 void RRTBase::root(RRTNode *node)
184 void RRTBase::goal(RRTNode *node)
189 bool RRTBase::logr(RRTNode *root)
191 std::vector<RRTEdge *> e; // Edges to log
192 std::vector<RRTNode *> s; // DFS stack
193 std::vector<RRTNode *> r; // reset visited_
196 while (s.size() > 0) {
201 for (auto ch: tmp->children()) {
203 e.push_back(new RRTEdge(tmp, ch));
209 this->rlog_.push_back(e);
213 float RRTBase::ocost(RRTNode *n)
216 for (auto o: *this->cobstacles_)
217 if (o.dist_to(n) < dist)
219 for (auto o: *this->sobstacles_)
220 if (o.dist_to(n) < dist)
222 return n->ocost(dist);
225 bool RRTBase::tlog(std::vector<RRTNode *> t)
228 this->slog_.push_back(this->elapsed());
229 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
230 this->nlog_.push_back(this->nodes_.size());
231 this->tlog_.push_back(t);
238 void RRTBase::tstart()
240 this->tstart_ = std::chrono::high_resolution_clock::now();
245 this->tend_ = std::chrono::high_resolution_clock::now();
248 bool RRTBase::link_obstacles(
249 std::vector<CircleObstacle> *cobstacles,
250 std::vector<SegmentObstacle> *sobstacles)
252 this->cobstacles_ = cobstacles;
253 this->sobstacles_ = sobstacles;
254 if (!this->cobstacles_ || !this->sobstacles_) {
260 bool RRTBase::add_iy(RRTNode *n)
267 this->iy_[i].push_back(n);
271 bool RRTBase::add_ixy(RRTNode *n)
273 int ix = IXI(n->x());
278 int iy = IYI(n->y());
283 this->ixy_[ix][iy].add_node(n);
287 bool RRTBase::goal_found(bool f)
289 this->goal_found_ = f;
294 bool RRTBase::glplot()
296 glClear(GL_COLOR_BUFFER_BIT);
301 for (auto o: *this->sobstacles_) {
303 glVertex2f(GLVERTEX(o.init()));
304 glVertex2f(GLVERTEX(o.goal()));
311 glVertex2f(GLVERTEX(this->root_));
312 glVertex2f(GLVERTEX(this->goal_));
315 if (this->samples_.size() > 0) {
319 glVertex2f(GLVERTEX(this->samples_.back()));
323 std::vector<RRTNode *> s; // DFS stack
324 std::vector<RRTNode *> r; // reset visited_
327 s.push_back(this->root_);
328 while (s.size() > 0) {
333 for (auto ch: tmp->children()) {
335 glColor3f(0.5, 0.5, 0.5);
336 glVertex2f(GLVERTEX(tmp));
337 glVertex2f(GLVERTEX(ch));
342 // Plot nodes (from goal)
344 s.push_back(this->goal_);
345 while (s.size() > 0) {
350 for (auto ch: tmp->children()) {
352 glColor3f(0.5, 0.5, 0.5);
353 glVertex2f(GLVERTEX(tmp));
354 glVertex2f(GLVERTEX(ch));
359 std::vector<RRTNode *> cusps;
360 // Plot last trajectory
361 if (this->tlog().size() > 0) {
364 for (auto n: this->tlog().back()) {
367 glVertex2f(GLVERTEX(n));
368 glVertex2f(GLVERTEX(n->parent()));
369 if (sgn(n->s()) != sgn(n->parent()->s()))
378 for (auto n: cusps) {
380 glVertex2f(GLVERTEX(n));
383 SDL_GL_SwapWindow(gw);
389 bool RRTBase::goal_found(
391 float (*cost)(RRTNode *, RRTNode* ))
393 if (IS_NEAR(node, this->goal_)) {
394 if (this->goal_found_) {
395 if (node->ccost() + this->cost(node, this->goal_) <
396 this->goal_->ccost()) {
397 RRTNode *op; // old parent
398 float oc; // old cumulative cost
399 float od; // old direct cost
400 op = this->goal_->parent();
401 oc = this->goal_->ccost();
402 od = this->goal_->dcost();
403 node->add_child(this->goal_,
404 this->cost(node, this->goal_));
405 if (this->collide(node, this->goal_)) {
406 node->children().pop_back();
407 this->goal_->parent(op);
408 this->goal_->ccost(oc);
409 this->goal_->dcost(od);
411 op->rem_child(this->goal_);
420 this->cost(node, this->goal_));
421 if (this->collide(node, this->goal_)) {
422 node->children().pop_back();
423 this->goal_->remove_parent();
426 this->goal_found_ = true;
433 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
435 std::vector<RRTEdge *> edges;
437 volatile bool col = false;
439 while (tmp != init) {
440 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
441 std::vector<RRTEdge *> bcframe = bc.frame();
442 #pragma omp parallel for reduction(|: col)
443 for (i = 0; i < (*this->cobstacles_).size(); i++) {
444 if ((*this->cobstacles_)[i].collide(tmp)) {
447 for (auto &e: bcframe) {
448 if ((*this->cobstacles_)[i].collide(e)) {
454 for (auto e: bcframe) {
459 for (auto e: edges) {
464 #pragma omp parallel for reduction(|: col)
465 for (i = 0; i < (*this->sobstacles_).size(); i++) {
466 for (auto &e: bcframe) {
467 if ((*this->sobstacles_)[i].collide(e)) {
473 for (auto e: bcframe) {
478 for (auto e: edges) {
483 if (!tmp->parent()) {
486 edges.push_back(new RRTEdge(tmp, tmp->parent()));
488 for (auto e: bcframe) {
494 for (auto &e: edges) {
495 #pragma omp parallel for reduction(|: col)
496 for (i = 0; i < (*this->cobstacles_).size(); i++) {
497 if ((*this->cobstacles_)[i].collide(e)) {
502 for (auto e: edges) {
507 #pragma omp parallel for reduction(|: col)
508 for (i = 0; i < (*this->sobstacles_).size(); i++) {
509 if ((*this->sobstacles_)[i].collide(e)) {
514 for (auto e: edges) {
520 for (auto e: edges) {
526 class RRTNodeDijkstra {
528 RRTNodeDijkstra(int i):
534 RRTNodeDijkstra(int i, float c):
540 RRTNodeDijkstra(int i, int p, float c):
559 class RRTNodeDijkstraComparator {
562 const RRTNodeDijkstra& n1,
563 const RRTNodeDijkstra& n2)
569 bool RRTBase::optp_dijkstra(
570 std::vector<RRTNode *> &cusps,
571 std::vector<int> &npi)
573 std::vector<RRTNodeDijkstra> dnodes;
574 for (unsigned int i = 0; i < cusps.size(); i++)
576 dnodes.push_back(RRTNodeDijkstra(
581 dnodes.push_back(RRTNodeDijkstra(
587 std::vector<RRTNodeDijkstra>,
588 RRTNodeDijkstraComparator> pq;
589 RRTNodeDijkstra tmp = dnodes[0];
591 float ch_cost = 9999;
592 std::vector<RRTNode *> steered;
593 while (!pq.empty()) {
596 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
597 ch_cost = dnodes[tmp.ni].c +
598 this->cost(cusps[tmp.ni], cusps[i]);
599 steered = this->steer(cusps[tmp.ni], cusps[i]);
600 for (unsigned int j = 0; j < steered.size() - 1; j++)
601 steered[j]->add_child(steered[j + 1], 1);
604 steered[steered.size() - 1])) {
605 for (auto n: steered)
609 if (ch_cost < dnodes[i].c) {
610 dnodes[i].c = ch_cost;
611 dnodes[i].pi = tmp.ni;
615 for (auto n: steered)
619 unsigned int tmpi = 0;
620 for (auto n: dnodes) {
621 if (n.v && n.ni > tmpi)
626 tmpi = dnodes[tmpi].pi;
629 std::reverse(npi.begin(), npi.end());
633 bool RRTBase::optp_rrp(
634 std::vector<RRTNode *> &cusps,
635 std::vector<int> &npi)
637 std::vector<RRTNode *> steered;
638 std::vector<int> candidates;
639 RRTNode *x_j = nullptr;
640 RRTNode *x_i = nullptr;
641 int j = cusps.size() - 1;
655 for (int i = 0; i < j; i++) {
656 steered = this->steer(cusps[i], x_j);
657 for (unsigned int k = 0; k < steered.size() - 1; k++)
658 steered[k]->add_child(steered[k + 1], 1);
661 steered[steered.size() - 1]))
662 candidates.push_back(i);
664 if (candidates.size() <= 0)
666 i_min = candidates[0];
669 for (auto c: candidates) {
671 dx = x_j->x() - x_i->x();
672 dy = x_j->y() - x_i->y();
673 ed = EDIST(x_i, x_j);
674 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
675 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
686 std::reverse(npi.begin(), npi.end());
690 bool RRTBase::optp_smart(
691 std::vector<RRTNode *> &cusps,
692 std::vector<int> &npi)
694 std::vector<RRTNode *> steered;
695 int li = cusps.size() - 1;
699 steered = this->steer(cusps[ai - 1], cusps[li]);
700 for (unsigned int j = 0; j < steered.size() - 1; j++)
701 steered[j]->add_child(steered[j + 1], 1);
702 if (this->collide(steered[0], steered[steered.size() - 1])) {
707 for (auto n: steered)
711 std::reverse(npi.begin(), npi.end());
715 bool RRTBase::opt_path()
717 if (this->tlog().size() == 0)
719 float oc = this->tlog().back().front()->ccost();
720 std::vector<RRTNode *> tmp_cusps;
721 for (auto n: this->tlog().back()) {
722 if (sgn(n->s()) == 0) {
723 tmp_cusps.push_back(n);
724 } else if (n->parent() &&
725 sgn(n->s()) != sgn(n->parent()->s())) {
726 tmp_cusps.push_back(n);
727 tmp_cusps.push_back(n->parent());
729 //tmp_cusps.push_back(n);
731 if (tmp_cusps.size() < 2)
733 std::vector<RRTNode *> cusps;
734 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
735 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
736 cusps.push_back(tmp_cusps[i]);
738 std::reverse(cusps.begin(), cusps.end());
739 std::vector<int> npi; // new path indexes
740 if (!this->optp_dijkstra(cusps, npi))
742 RRTNode *pn = cusps[npi[0]];
743 RRTNode *tmp = nullptr;
745 for (unsigned int i = 0; i < npi.size() - 1; i++) {
747 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
750 } else if (IS_NEAR(cusps[npi[i]], ns)) {
752 while (tmp && tmp != cusps[npi[i]]) {
758 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
760 cusps[npi[i + 1]]->parent()->rem_child(
764 this->cost(pn, cusps[npi[i + 1]]));
766 } else if (IS_NEAR(pn, ns)) {
769 this->nodes().push_back(ns);
771 pn->add_child(ns, this->cost(pn, ns));
776 this->root()->update_ccost();
777 if (this->tlog().back().front()->ccost() < oc)
782 bool RRTBase::rebase(RRTNode *nr)
784 if (!nr || this->goal_ == nr || this->root_ == nr)
786 std::vector<RRTNode *> s; // DFS stack
789 unsigned int to_del = 0;
791 s.push_back(this->root_);
792 while (s.size() > 0) {
795 for (auto ch: tmp->children()) {
799 to_del = this->nodes_.size();
800 #pragma omp parallel for reduction(min: to_del)
801 for (i = 0; i < this->nodes_.size(); i++) {
802 if (this->nodes_[i] == tmp)
805 if (to_del < this->nodes_.size())
806 this->nodes_.erase(this->nodes_.begin() + to_del);
808 to_del = this->iy_[iy].size();
809 #pragma omp parallel for reduction(min: to_del)
810 for (i = 0; i < this->iy_[iy].size(); i++) {
811 if (this->iy_[iy][i] == tmp)
814 if (to_del < this->iy_[iy].size())
815 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
816 this->dnodes().push_back(tmp);
819 this->root_->remove_parent();
823 std::vector<RRTNode *> RRTBase::findt()
825 return this->findt(this->goal_);
828 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
830 std::vector<RRTNode *> nodes;
831 if (!n || !n->parent())
841 RRTNode *RRTBase::sample()
846 float RRTBase::cost(RRTNode *init, RRTNode *goal)
848 return co2(init, goal);
851 RRTNode *RRTBase::nn(RRTNode *rs)
853 return nn4(this->iy_, rs, nullptr);
854 //return nn3(this->iy_, rs, nullptr);
857 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
859 std::vector<RRTNode *> nvs;
860 unsigned int iy = IYI(node->y());
861 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
862 unsigned int i = 0; // vector index
863 unsigned int j = 0; // array index
864 unsigned int jmin = 0; // minimal j index
865 unsigned int jmax = 0; // maximal j index
867 jmin = (jmin > 0) ? jmin : 0;
868 jmax = iy + iy_dist + 1;
869 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
870 #pragma omp parallel for reduction(merge: nvs)
871 for (j = jmin; j < jmax; j++) {
872 #pragma omp parallel for reduction(merge: nvs)
873 for (i = 0; i < this->iy_[j].size(); i++) {
874 if (this->cost(this->iy_[j][i], node) < dist) {
875 nvs.push_back(this->iy_[j][i]);
882 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
884 return st3(init, goal);
887 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
889 return st3(init, goal, step);