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/>.
40 extern SDL_Window* gw;
41 extern SDL_GLContext gc;
46 pthread_mutex_init(&this->m_, NULL);
51 pthread_mutex_lock(&this->m_);
52 bool ret = this->changed_;
53 pthread_mutex_unlock(&this->m_);
57 std::vector<RRTNode *> Cell::nodes()
59 pthread_mutex_lock(&this->m_);
60 std::vector<RRTNode *> ret(this->nodes_);
61 pthread_mutex_unlock(&this->m_);
65 void Cell::add_node(RRTNode *n)
67 pthread_mutex_lock(&this->m_);
68 this->nodes_.push_back(n);
69 this->changed_ = true;
70 pthread_mutex_unlock(&this->m_);
75 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
76 // please uncommend the following code:
78 for (auto n: this->nodes_)
81 for (auto n: this->dnodes_)
82 if (n != this->root_ && n != this->goal_)
84 for (auto s: this->samples_)
87 for (auto edges: this->rlog_)
97 gen_(std::random_device{}())
99 this->nodes_.reserve(NOFNODES);
100 this->nodes_.push_back(this->root_);
101 this->add_iy(this->root_);
102 this->add_ixy(this->root_);
105 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
108 gen_(std::random_device{}())
110 this->nodes_.reserve(NOFNODES);
111 this->nodes_.push_back(init);
117 RRTNode *RRTBase::root()
122 RRTNode *RRTBase::goal()
127 std::vector<RRTNode *> &RRTBase::goals()
132 std::vector<RRTNode *> &RRTBase::nodes()
137 std::vector<RRTNode *> &RRTBase::dnodes()
139 return this->dnodes_;
142 PolygonObstacle &RRTBase::frame()
147 std::vector<RRTNode *> &RRTBase::samples()
149 return this->samples_;
152 std::vector<CircleObstacle> *RRTBase::co()
154 return this->cobstacles_;
157 std::vector<SegmentObstacle> *RRTBase::so()
159 return this->sobstacles_;
162 std::vector<float> &RRTBase::clog()
167 std::vector<float> &RRTBase::nlog()
172 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
177 std::vector<float> &RRTBase::slog()
182 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
187 std::vector<RRTNode *> &RRTBase::slot_cusp()
189 return this->slot_cusp_;
192 bool RRTBase::goal_found()
194 return this->goal_found_;
197 float RRTBase::elapsed()
199 std::chrono::duration<float> dt;
200 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
201 this->tend_ - this->tstart_);
205 std::vector<RRTNode *> RRTBase::traj_cusp()
207 std::vector<RRTNode *> tmp_cusps;
208 for (auto n: this->tlog().back()) {
209 if (sgn(n->s()) == 0) {
210 tmp_cusps.push_back(n);
211 } else if (n->parent() &&
212 sgn(n->s()) != sgn(n->parent()->s())) {
213 tmp_cusps.push_back(n);
214 tmp_cusps.push_back(n->parent());
217 std::vector<RRTNode *> cusps;
218 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
219 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
220 cusps.push_back(tmp_cusps[i]);
226 void RRTBase::root(RRTNode *node)
231 void RRTBase::goal(RRTNode *node)
236 void RRTBase::goals(std::vector<RRTNode *> g)
239 std::reverse(this->goals_.begin(), this->goals_.end());
240 RRTNode *pn = this->goals_.front();
241 for (auto n: this->goals_) {
243 pn->add_child(n, this->cost(pn ,n));
249 bool RRTBase::logr(RRTNode *root)
251 std::vector<RRTEdge *> e; // Edges to log
252 std::vector<RRTNode *> s; // DFS stack
253 std::vector<RRTNode *> r; // reset visited_
256 while (s.size() > 0) {
261 for (auto ch: tmp->children()) {
263 e.push_back(new RRTEdge(tmp, ch));
269 this->rlog_.push_back(e);
273 float RRTBase::ocost(RRTNode *n)
276 for (auto o: *this->cobstacles_)
277 if (o.dist_to(n) < dist)
279 for (auto o: *this->sobstacles_)
280 if (o.dist_to(n) < dist)
282 return n->ocost(dist);
285 bool RRTBase::tlog(std::vector<RRTNode *> t)
288 this->slog_.push_back(this->elapsed());
289 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
290 this->nlog_.push_back(this->nodes_.size());
291 this->tlog_.push_back(t);
298 void RRTBase::tstart()
300 this->tstart_ = std::chrono::high_resolution_clock::now();
305 this->tend_ = std::chrono::high_resolution_clock::now();
308 bool RRTBase::link_obstacles(
309 std::vector<CircleObstacle> *cobstacles,
310 std::vector<SegmentObstacle> *sobstacles)
312 this->cobstacles_ = cobstacles;
313 this->sobstacles_ = sobstacles;
314 if (!this->cobstacles_ || !this->sobstacles_) {
320 bool RRTBase::add_iy(RRTNode *n)
327 this->iy_[i].push_back(n);
331 bool RRTBase::add_ixy(RRTNode *n)
333 int ix = IXI(n->x());
338 int iy = IYI(n->y());
343 this->ixy_[ix][iy].add_node(n);
347 bool RRTBase::goal_found(bool f)
349 this->goal_found_ = f;
353 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
355 for (unsigned int i = 0; i < sc.size() - 1; i++)
356 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
357 sc[0]->parent(this->goal());
358 this->slot_cusp_ = sc;
362 bool RRTBase::glplot()
365 glClear(GL_COLOR_BUFFER_BIT);
370 for (auto o: *this->sobstacles_) {
372 glVertex2f(GLVERTEX(o.init()));
373 glVertex2f(GLVERTEX(o.goal()));
380 glVertex2f(GLVERTEX(this->root_));
381 glVertex2f(GLVERTEX(this->goal_));
384 if (this->samples_.size() > 0) {
388 glVertex2f(GLVERTEX(this->samples_.back()));
392 std::vector<RRTNode *> s; // DFS stack
393 std::vector<RRTNode *> r; // reset visited_
396 s.push_back(this->root_);
397 while (s.size() > 0) {
402 for (auto ch: tmp->children()) {
404 glColor3f(0.5, 0.5, 0.5);
405 glVertex2f(GLVERTEX(tmp));
406 glVertex2f(GLVERTEX(ch));
411 // Plot nodes (from goal)
413 s.push_back(this->goal_);
414 while (s.size() > 0) {
419 for (auto ch: tmp->children()) {
421 glColor3f(0.5, 0.5, 0.5);
422 glVertex2f(GLVERTEX(tmp));
423 glVertex2f(GLVERTEX(ch));
428 std::vector<RRTNode *> cusps;
429 // Plot last trajectory
430 if (this->tlog().size() > 0) {
433 for (auto n: this->tlog().back()) {
436 glVertex2f(GLVERTEX(n));
437 glVertex2f(GLVERTEX(n->parent()));
438 if (sgn(n->s()) != sgn(n->parent()->s()))
447 for (auto n: cusps) {
449 glVertex2f(GLVERTEX(n));
452 SDL_GL_SwapWindow(gw);
459 bool RRTBase::goal_found(
461 float (*cost)(RRTNode *, RRTNode* ))
463 if (IS_NEAR(node, this->goal_)) {
464 if (this->goal_found_) {
465 if (node->ccost() + this->cost(node, this->goal_) <
466 this->goal_->ccost()) {
467 RRTNode *op; // old parent
468 float oc; // old cumulative cost
469 float od; // old direct cost
470 op = this->goal_->parent();
471 oc = this->goal_->ccost();
472 od = this->goal_->dcost();
473 node->add_child(this->goal_,
474 this->cost(node, this->goal_));
475 if (this->collide(node, this->goal_)) {
476 node->children().pop_back();
477 this->goal_->parent(op);
478 this->goal_->ccost(oc);
479 this->goal_->dcost(od);
481 op->rem_child(this->goal_);
490 this->cost(node, this->goal_));
491 if (this->collide(node, this->goal_)) {
492 node->children().pop_back();
493 this->goal_->remove_parent();
496 this->goal_found_ = true;
497 // Update ccost of goal's parents
498 if (this->goals().size() > 0) {
499 RRTNode *ch = this->goals().back();
500 RRTNode *pn = this->goals().back()->parent();
516 bool RRTBase::goal_found(
521 if (IS_NEAR(node, goal)) {
522 if (this->goal_found_) {
525 && node->ccost() + this->cost(node, goal)
528 RRTNode *op; // old parent
529 float oc; // old cumulative cost
530 float od; // old direct cost
534 node->add_child(goal,
535 this->cost(node, goal));
536 if (this->collide(node, goal)) {
537 node->children().pop_back();
551 this->cost(node, goal)
553 if (this->collide(node, goal)) {
554 node->children().pop_back();
555 goal->remove_parent();
558 this->goal_found_ = true;
559 // Update ccost of goal's children
560 goal->update_ccost();
561 // Update ccost of goals
562 for (auto g: this->goals()) {
573 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
575 std::vector<RRTEdge *> edges;
577 volatile bool col = false;
579 while (tmp != init) {
580 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
581 std::vector<RRTEdge *> bcframe = bc.frame();
582 #pragma omp parallel for reduction(|: col)
583 for (i = 0; i < (*this->cobstacles_).size(); i++) {
584 if ((*this->cobstacles_)[i].collide(tmp)) {
587 for (auto &e: bcframe) {
588 if ((*this->cobstacles_)[i].collide(e)) {
594 for (auto e: bcframe) {
599 for (auto e: edges) {
604 #pragma omp parallel for reduction(|: col)
605 for (i = 0; i < (*this->sobstacles_).size(); i++) {
606 for (auto &e: bcframe) {
607 if ((*this->sobstacles_)[i].collide(e)) {
613 for (auto e: bcframe) {
618 for (auto e: edges) {
623 if (!tmp->parent()) {
626 edges.push_back(new RRTEdge(tmp, tmp->parent()));
628 for (auto e: bcframe) {
634 for (auto &e: edges) {
635 #pragma omp parallel for reduction(|: col)
636 for (i = 0; i < (*this->cobstacles_).size(); i++) {
637 if ((*this->cobstacles_)[i].collide(e)) {
642 for (auto e: edges) {
647 #pragma omp parallel for reduction(|: col)
648 for (i = 0; i < (*this->sobstacles_).size(); i++) {
649 if ((*this->sobstacles_)[i].collide(e)) {
654 for (auto e: edges) {
660 for (auto e: edges) {
666 class RRTNodeDijkstra {
668 RRTNodeDijkstra(int i):
674 RRTNodeDijkstra(int i, float c):
680 RRTNodeDijkstra(int i, int p, float c):
699 class RRTNodeDijkstraComparator {
702 const RRTNodeDijkstra& n1,
703 const RRTNodeDijkstra& n2)
709 bool RRTBase::optp_dijkstra(
710 std::vector<RRTNode *> &cusps,
711 std::vector<int> &npi)
713 std::vector<RRTNodeDijkstra> dnodes;
714 for (unsigned int i = 0; i < cusps.size(); i++)
716 dnodes.push_back(RRTNodeDijkstra(
721 dnodes.push_back(RRTNodeDijkstra(
727 std::vector<RRTNodeDijkstra>,
728 RRTNodeDijkstraComparator> pq;
729 RRTNodeDijkstra tmp = dnodes[0];
731 float ch_cost = 9999;
732 std::vector<RRTNode *> steered;
733 while (!pq.empty()) {
736 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
737 ch_cost = dnodes[tmp.ni].c +
738 this->cost(cusps[tmp.ni], cusps[i]);
739 steered = this->steer(cusps[tmp.ni], cusps[i]);
740 for (unsigned int j = 0; j < steered.size() - 1; j++)
741 steered[j]->add_child(steered[j + 1], 1);
744 steered[steered.size() - 1])) {
745 for (auto n: steered)
749 if (ch_cost < dnodes[i].c) {
750 dnodes[i].c = ch_cost;
751 dnodes[i].pi = tmp.ni;
755 for (auto n: steered)
759 unsigned int tmpi = 0;
760 for (auto n: dnodes) {
761 if (n.v && n.ni > tmpi)
766 tmpi = dnodes[tmpi].pi;
769 std::reverse(npi.begin(), npi.end());
773 bool RRTBase::optp_rrp(
774 std::vector<RRTNode *> &cusps,
775 std::vector<int> &npi)
777 std::vector<RRTNode *> steered;
778 std::vector<int> candidates;
779 RRTNode *x_j = nullptr;
780 RRTNode *x_i = nullptr;
781 int j = cusps.size() - 1;
795 for (int i = 0; i < j; i++) {
796 steered = this->steer(cusps[i], x_j);
797 for (unsigned int k = 0; k < steered.size() - 1; k++)
798 steered[k]->add_child(steered[k + 1], 1);
801 steered[steered.size() - 1]))
802 candidates.push_back(i);
804 if (candidates.size() <= 0)
806 i_min = candidates[0];
809 for (auto c: candidates) {
811 dx = x_j->x() - x_i->x();
812 dy = x_j->y() - x_i->y();
813 ed = EDIST(x_i, x_j);
814 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
815 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
826 std::reverse(npi.begin(), npi.end());
830 bool RRTBase::optp_smart(
831 std::vector<RRTNode *> &cusps,
832 std::vector<int> &npi)
834 std::vector<RRTNode *> steered;
835 int li = cusps.size() - 1;
839 steered = this->steer(cusps[ai - 1], cusps[li]);
840 for (unsigned int j = 0; j < steered.size() - 1; j++)
841 steered[j]->add_child(steered[j + 1], 1);
842 if (this->collide(steered[0], steered[steered.size() - 1])) {
847 for (auto n: steered)
851 std::reverse(npi.begin(), npi.end());
855 bool RRTBase::opt_path()
857 if (this->tlog().size() == 0)
859 float oc = this->tlog().back().front()->ccost();
860 std::vector<RRTNode *> tmp_cusps;
861 for (auto n: this->tlog().back()) {
862 if (sgn(n->s()) == 0) {
863 tmp_cusps.push_back(n);
864 } else if (n->parent() &&
865 sgn(n->s()) != sgn(n->parent()->s())) {
866 tmp_cusps.push_back(n);
867 tmp_cusps.push_back(n->parent());
869 //tmp_cusps.push_back(n);
871 if (tmp_cusps.size() < 2)
873 std::vector<RRTNode *> cusps;
874 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
875 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
876 cusps.push_back(tmp_cusps[i]);
878 std::reverse(cusps.begin(), cusps.end());
879 std::vector<int> npi; // new path indexes
880 if (!this->optp_dijkstra(cusps, npi))
882 RRTNode *pn = cusps[npi[0]];
883 RRTNode *tmp = nullptr;
885 for (unsigned int i = 0; i < npi.size() - 1; i++) {
887 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
890 } else if (IS_NEAR(cusps[npi[i]], ns)) {
892 while (tmp && tmp != cusps[npi[i]]) {
898 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
900 cusps[npi[i + 1]]->parent()->rem_child(
904 this->cost(pn, cusps[npi[i + 1]]));
906 } else if (IS_NEAR(pn, ns)) {
909 this->nodes().push_back(ns);
912 pn->add_child(ns, this->cost(pn, ns));
917 this->root()->update_ccost();
918 if (this->tlog().back().front()->ccost() < oc)
923 bool RRTBase::rebase(RRTNode *nr)
925 if (!nr || this->goal_ == nr || this->root_ == nr)
927 std::vector<RRTNode *> s; // DFS stack
930 unsigned int to_del = 0;
932 s.push_back(this->root_);
933 while (s.size() > 0) {
936 for (auto ch: tmp->children()) {
940 to_del = this->nodes_.size();
941 #pragma omp parallel for reduction(min: to_del)
942 for (i = 0; i < this->nodes_.size(); i++) {
943 if (this->nodes_[i] == tmp)
946 if (to_del < this->nodes_.size())
947 this->nodes_.erase(this->nodes_.begin() + to_del);
949 to_del = this->iy_[iy].size();
950 #pragma omp parallel for reduction(min: to_del)
951 for (i = 0; i < this->iy_[iy].size(); i++) {
952 if (this->iy_[iy][i] == tmp)
955 if (to_del < this->iy_[iy].size())
956 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
957 this->dnodes().push_back(tmp);
960 this->root_->remove_parent();
964 std::vector<RRTNode *> RRTBase::findt()
966 return this->findt(this->goal_);
969 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
971 std::vector<RRTNode *> nodes;
972 if (!n || !n->parent())
982 RRTNode *RRTBase::sample()
984 if (this->useSamplingInfo_ && this->nodes().size() % 2 == 0) {
985 float x = static_cast<float>(rand());
986 x /= static_cast<float>(RAND_MAX / this->samplingInfo_.x);
987 x -= this->samplingInfo_.x / 2;
988 x += this->samplingInfo_.x0;
989 float y = static_cast<float>(rand());
990 y /= static_cast<float>(RAND_MAX / this->samplingInfo_.y);
991 y -= this->samplingInfo_.y / 2;
992 y += this->samplingInfo_.y0;
993 float h = static_cast<float>(rand());
994 h /= static_cast<float>(RAND_MAX / this->samplingInfo_.h);
995 h -= this->samplingInfo_.h / 2;
996 h += this->samplingInfo_.h0;
997 return new RRTNode(x, y, h);
1003 float RRTBase::cost(RRTNode *init, RRTNode *goal)
1005 return co2(init, goal);
1008 RRTNode *RRTBase::nn(RRTNode *rs)
1010 return nn4(this->iy_, rs, nullptr);
1011 //return nn3(this->iy_, rs, nullptr);
1014 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
1016 std::vector<RRTNode *> nvs;
1017 unsigned int iy = IYI(node->y());
1018 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
1019 unsigned int i = 0; // vector index
1020 unsigned int j = 0; // array index
1021 unsigned int jmin = 0; // minimal j index
1022 unsigned int jmax = 0; // maximal j index
1023 jmin = iy - iy_dist;
1024 jmin = (jmin > 0) ? jmin : 0;
1025 jmax = iy + iy_dist + 1;
1026 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
1027 #pragma omp parallel for reduction(merge: nvs)
1028 for (j = jmin; j < jmax; j++) {
1029 #pragma omp parallel for reduction(merge: nvs)
1030 for (i = 0; i < this->iy_[j].size(); i++) {
1031 if (this->cost(this->iy_[j][i], node) < dist) {
1032 nvs.push_back(this->iy_[j][i]);
1039 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
1041 return st3(init, goal);
1044 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
1046 return st3(init, goal, step);