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 PolygonObstacle &RRTBase::frame()
132 std::vector<RRTNode *> &RRTBase::samples()
134 return this->samples_;
137 std::vector<CircleObstacle> *RRTBase::co()
139 return this->cobstacles_;
142 std::vector<SegmentObstacle> *RRTBase::so()
144 return this->sobstacles_;
147 std::vector<float> &RRTBase::clog()
152 std::vector<float> &RRTBase::nlog()
157 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
162 std::vector<float> &RRTBase::slog()
167 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
172 bool RRTBase::goal_found()
174 return this->goal_found_;
177 float RRTBase::elapsed()
179 std::chrono::duration<float> dt;
180 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
181 this->tend_ - this->tstart_);
186 void RRTBase::root(RRTNode *node)
191 void RRTBase::goal(RRTNode *node)
196 bool RRTBase::logr(RRTNode *root)
198 std::vector<RRTEdge *> e; // Edges to log
199 std::vector<RRTNode *> s; // DFS stack
200 std::vector<RRTNode *> r; // reset visited_
203 while (s.size() > 0) {
208 for (auto ch: tmp->children()) {
210 e.push_back(new RRTEdge(tmp, ch));
216 this->rlog_.push_back(e);
220 float RRTBase::ocost(RRTNode *n)
223 for (auto o: *this->cobstacles_)
224 if (o.dist_to(n) < dist)
226 for (auto o: *this->sobstacles_)
227 if (o.dist_to(n) < dist)
229 return n->ocost(dist);
232 bool RRTBase::tlog(std::vector<RRTNode *> t)
235 this->slog_.push_back(this->elapsed());
236 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
237 this->nlog_.push_back(this->nodes_.size());
238 this->tlog_.push_back(t);
245 void RRTBase::tstart()
247 this->tstart_ = std::chrono::high_resolution_clock::now();
252 this->tend_ = std::chrono::high_resolution_clock::now();
255 bool RRTBase::link_obstacles(
256 std::vector<CircleObstacle> *cobstacles,
257 std::vector<SegmentObstacle> *sobstacles)
259 this->cobstacles_ = cobstacles;
260 this->sobstacles_ = sobstacles;
261 if (!this->cobstacles_ || !this->sobstacles_) {
267 bool RRTBase::add_iy(RRTNode *n)
274 this->iy_[i].push_back(n);
278 bool RRTBase::add_ixy(RRTNode *n)
280 int ix = IXI(n->x());
285 int iy = IYI(n->y());
290 this->ixy_[ix][iy].add_node(n);
294 bool RRTBase::goal_found(bool f)
296 this->goal_found_ = f;
301 bool RRTBase::glplot()
303 glClear(GL_COLOR_BUFFER_BIT);
308 for (auto o: *this->sobstacles_) {
310 glVertex2f(GLVERTEX(o.init()));
311 glVertex2f(GLVERTEX(o.goal()));
318 glVertex2f(GLVERTEX(this->root_));
319 glVertex2f(GLVERTEX(this->goal_));
322 if (this->samples_.size() > 0) {
326 glVertex2f(GLVERTEX(this->samples_.back()));
330 std::vector<RRTNode *> s; // DFS stack
331 std::vector<RRTNode *> r; // reset visited_
334 s.push_back(this->root_);
335 while (s.size() > 0) {
340 for (auto ch: tmp->children()) {
342 glColor3f(0.5, 0.5, 0.5);
343 glVertex2f(GLVERTEX(tmp));
344 glVertex2f(GLVERTEX(ch));
349 // Plot nodes (from goal)
351 s.push_back(this->goal_);
352 while (s.size() > 0) {
357 for (auto ch: tmp->children()) {
359 glColor3f(0.5, 0.5, 0.5);
360 glVertex2f(GLVERTEX(tmp));
361 glVertex2f(GLVERTEX(ch));
366 std::vector<RRTNode *> cusps;
367 // Plot last trajectory
368 if (this->tlog().size() > 0) {
371 for (auto n: this->tlog().back()) {
374 glVertex2f(GLVERTEX(n));
375 glVertex2f(GLVERTEX(n->parent()));
376 if (sgn(n->s()) != sgn(n->parent()->s()))
385 for (auto n: cusps) {
387 glVertex2f(GLVERTEX(n));
390 SDL_GL_SwapWindow(gw);
396 bool RRTBase::goal_found(
398 float (*cost)(RRTNode *, RRTNode* ))
400 if (IS_NEAR(node, this->goal_)) {
401 if (this->goal_found_) {
402 if (node->ccost() + this->cost(node, this->goal_) <
403 this->goal_->ccost()) {
404 RRTNode *op; // old parent
405 float oc; // old cumulative cost
406 float od; // old direct cost
407 op = this->goal_->parent();
408 oc = this->goal_->ccost();
409 od = this->goal_->dcost();
410 node->add_child(this->goal_,
411 this->cost(node, this->goal_));
412 if (this->collide(node, this->goal_)) {
413 node->children().pop_back();
414 this->goal_->parent(op);
415 this->goal_->ccost(oc);
416 this->goal_->dcost(od);
418 op->rem_child(this->goal_);
427 this->cost(node, this->goal_));
428 if (this->collide(node, this->goal_)) {
429 node->children().pop_back();
430 this->goal_->remove_parent();
433 this->goal_found_ = true;
440 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
442 std::vector<RRTEdge *> edges;
444 volatile bool col = false;
446 while (tmp != init) {
447 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
448 std::vector<RRTEdge *> bcframe = bc.frame();
449 #pragma omp parallel for reduction(|: col)
450 for (i = 0; i < (*this->cobstacles_).size(); i++) {
451 if ((*this->cobstacles_)[i].collide(tmp)) {
454 for (auto &e: bcframe) {
455 if ((*this->cobstacles_)[i].collide(e)) {
461 for (auto e: bcframe) {
466 for (auto e: edges) {
471 #pragma omp parallel for reduction(|: col)
472 for (i = 0; i < (*this->sobstacles_).size(); i++) {
473 for (auto &e: bcframe) {
474 if ((*this->sobstacles_)[i].collide(e)) {
480 for (auto e: bcframe) {
485 for (auto e: edges) {
490 if (!tmp->parent()) {
493 edges.push_back(new RRTEdge(tmp, tmp->parent()));
495 for (auto e: bcframe) {
501 for (auto &e: edges) {
502 #pragma omp parallel for reduction(|: col)
503 for (i = 0; i < (*this->cobstacles_).size(); i++) {
504 if ((*this->cobstacles_)[i].collide(e)) {
509 for (auto e: edges) {
514 #pragma omp parallel for reduction(|: col)
515 for (i = 0; i < (*this->sobstacles_).size(); i++) {
516 if ((*this->sobstacles_)[i].collide(e)) {
521 for (auto e: edges) {
527 for (auto e: edges) {
533 class RRTNodeDijkstra {
535 RRTNodeDijkstra(int i):
541 RRTNodeDijkstra(int i, float c):
547 RRTNodeDijkstra(int i, int p, float c):
566 class RRTNodeDijkstraComparator {
569 const RRTNodeDijkstra& n1,
570 const RRTNodeDijkstra& n2)
576 bool RRTBase::optp_dijkstra(
577 std::vector<RRTNode *> &cusps,
578 std::vector<int> &npi)
580 std::vector<RRTNodeDijkstra> dnodes;
581 for (unsigned int i = 0; i < cusps.size(); i++)
583 dnodes.push_back(RRTNodeDijkstra(
588 dnodes.push_back(RRTNodeDijkstra(
594 std::vector<RRTNodeDijkstra>,
595 RRTNodeDijkstraComparator> pq;
596 RRTNodeDijkstra tmp = dnodes[0];
598 float ch_cost = 9999;
599 std::vector<RRTNode *> steered;
600 while (!pq.empty()) {
603 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
604 ch_cost = dnodes[tmp.ni].c +
605 this->cost(cusps[tmp.ni], cusps[i]);
606 steered = this->steer(cusps[tmp.ni], cusps[i]);
607 for (unsigned int j = 0; j < steered.size() - 1; j++)
608 steered[j]->add_child(steered[j + 1], 1);
611 steered[steered.size() - 1])) {
612 for (auto n: steered)
616 if (ch_cost < dnodes[i].c) {
617 dnodes[i].c = ch_cost;
618 dnodes[i].pi = tmp.ni;
622 for (auto n: steered)
626 unsigned int tmpi = 0;
627 for (auto n: dnodes) {
628 if (n.v && n.ni > tmpi)
633 tmpi = dnodes[tmpi].pi;
636 std::reverse(npi.begin(), npi.end());
640 bool RRTBase::optp_rrp(
641 std::vector<RRTNode *> &cusps,
642 std::vector<int> &npi)
644 std::vector<RRTNode *> steered;
645 std::vector<int> candidates;
646 RRTNode *x_j = nullptr;
647 RRTNode *x_i = nullptr;
648 int j = cusps.size() - 1;
662 for (int i = 0; i < j; i++) {
663 steered = this->steer(cusps[i], x_j);
664 for (unsigned int k = 0; k < steered.size() - 1; k++)
665 steered[k]->add_child(steered[k + 1], 1);
668 steered[steered.size() - 1]))
669 candidates.push_back(i);
671 if (candidates.size() <= 0)
673 i_min = candidates[0];
676 for (auto c: candidates) {
678 dx = x_j->x() - x_i->x();
679 dy = x_j->y() - x_i->y();
680 ed = EDIST(x_i, x_j);
681 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
682 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
693 std::reverse(npi.begin(), npi.end());
697 bool RRTBase::optp_smart(
698 std::vector<RRTNode *> &cusps,
699 std::vector<int> &npi)
701 std::vector<RRTNode *> steered;
702 int li = cusps.size() - 1;
706 steered = this->steer(cusps[ai - 1], cusps[li]);
707 for (unsigned int j = 0; j < steered.size() - 1; j++)
708 steered[j]->add_child(steered[j + 1], 1);
709 if (this->collide(steered[0], steered[steered.size() - 1])) {
714 for (auto n: steered)
718 std::reverse(npi.begin(), npi.end());
722 bool RRTBase::opt_path()
724 if (this->tlog().size() == 0)
726 float oc = this->tlog().back().front()->ccost();
727 std::vector<RRTNode *> tmp_cusps;
728 for (auto n: this->tlog().back()) {
729 if (sgn(n->s()) == 0) {
730 tmp_cusps.push_back(n);
731 } else if (n->parent() &&
732 sgn(n->s()) != sgn(n->parent()->s())) {
733 tmp_cusps.push_back(n);
734 tmp_cusps.push_back(n->parent());
736 //tmp_cusps.push_back(n);
738 if (tmp_cusps.size() < 2)
740 std::vector<RRTNode *> cusps;
741 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
742 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
743 cusps.push_back(tmp_cusps[i]);
745 std::reverse(cusps.begin(), cusps.end());
746 std::vector<int> npi; // new path indexes
747 if (!this->optp_dijkstra(cusps, npi))
749 RRTNode *pn = cusps[npi[0]];
750 RRTNode *tmp = nullptr;
752 for (unsigned int i = 0; i < npi.size() - 1; i++) {
754 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
757 } else if (IS_NEAR(cusps[npi[i]], ns)) {
759 while (tmp && tmp != cusps[npi[i]]) {
765 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
767 cusps[npi[i + 1]]->parent()->rem_child(
771 this->cost(pn, cusps[npi[i + 1]]));
773 } else if (IS_NEAR(pn, ns)) {
776 this->nodes().push_back(ns);
779 pn->add_child(ns, this->cost(pn, ns));
784 this->root()->update_ccost();
785 if (this->tlog().back().front()->ccost() < oc)
790 bool RRTBase::rebase(RRTNode *nr)
792 if (!nr || this->goal_ == nr || this->root_ == nr)
794 std::vector<RRTNode *> s; // DFS stack
797 unsigned int to_del = 0;
799 s.push_back(this->root_);
800 while (s.size() > 0) {
803 for (auto ch: tmp->children()) {
807 to_del = this->nodes_.size();
808 #pragma omp parallel for reduction(min: to_del)
809 for (i = 0; i < this->nodes_.size(); i++) {
810 if (this->nodes_[i] == tmp)
813 if (to_del < this->nodes_.size())
814 this->nodes_.erase(this->nodes_.begin() + to_del);
816 to_del = this->iy_[iy].size();
817 #pragma omp parallel for reduction(min: to_del)
818 for (i = 0; i < this->iy_[iy].size(); i++) {
819 if (this->iy_[iy][i] == tmp)
822 if (to_del < this->iy_[iy].size())
823 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
824 this->dnodes().push_back(tmp);
827 this->root_->remove_parent();
831 std::vector<RRTNode *> RRTBase::findt()
833 return this->findt(this->goal_);
836 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
838 std::vector<RRTNode *> nodes;
839 if (!n || !n->parent())
849 RRTNode *RRTBase::sample()
854 float RRTBase::cost(RRTNode *init, RRTNode *goal)
856 return co2(init, goal);
859 RRTNode *RRTBase::nn(RRTNode *rs)
861 return nn4(this->iy_, rs, nullptr);
862 //return nn3(this->iy_, rs, nullptr);
865 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
867 std::vector<RRTNode *> nvs;
868 unsigned int iy = IYI(node->y());
869 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
870 unsigned int i = 0; // vector index
871 unsigned int j = 0; // array index
872 unsigned int jmin = 0; // minimal j index
873 unsigned int jmax = 0; // maximal j index
875 jmin = (jmin > 0) ? jmin : 0;
876 jmax = iy + iy_dist + 1;
877 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
878 #pragma omp parallel for reduction(merge: nvs)
879 for (j = jmin; j < jmax; j++) {
880 #pragma omp parallel for reduction(merge: nvs)
881 for (i = 0; i < this->iy_[j].size(); i++) {
882 if (this->cost(this->iy_[j][i], node) < dist) {
883 nvs.push_back(this->iy_[j][i]);
890 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
892 return st3(init, goal);
895 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
897 return st3(init, goal, step);