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_)
73 for (auto n: this->dnodes_)
74 if (n != this->root_ && n != this->goal_)
76 for (auto s: this->samples_)
79 for (auto edges: this->rlog_)
89 gen_(std::random_device{}())
91 this->nodes_.reserve(NOFNODES);
92 this->nodes_.push_back(this->root_);
93 this->add_iy(this->root_);
94 this->add_ixy(this->root_);
97 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
100 gen_(std::random_device{}())
102 this->nodes_.reserve(NOFNODES);
103 this->nodes_.push_back(init);
109 RRTNode *RRTBase::root()
114 RRTNode *RRTBase::goal()
119 std::vector<RRTNode *> &RRTBase::nodes()
124 std::vector<RRTNode *> &RRTBase::dnodes()
126 return this->dnodes_;
129 PolygonObstacle &RRTBase::frame()
134 std::vector<RRTNode *> &RRTBase::samples()
136 return this->samples_;
139 std::vector<CircleObstacle> *RRTBase::co()
141 return this->cobstacles_;
144 std::vector<SegmentObstacle> *RRTBase::so()
146 return this->sobstacles_;
149 std::vector<float> &RRTBase::clog()
154 std::vector<float> &RRTBase::nlog()
159 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
164 std::vector<float> &RRTBase::slog()
169 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
174 std::vector<RRTNode *> &RRTBase::slot_cusp()
176 return this->slot_cusp_;
179 bool RRTBase::goal_found()
181 return this->goal_found_;
184 float RRTBase::elapsed()
186 std::chrono::duration<float> dt;
187 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
188 this->tend_ - this->tstart_);
193 void RRTBase::root(RRTNode *node)
198 void RRTBase::goal(RRTNode *node)
203 bool RRTBase::logr(RRTNode *root)
205 std::vector<RRTEdge *> e; // Edges to log
206 std::vector<RRTNode *> s; // DFS stack
207 std::vector<RRTNode *> r; // reset visited_
210 while (s.size() > 0) {
215 for (auto ch: tmp->children()) {
217 e.push_back(new RRTEdge(tmp, ch));
223 this->rlog_.push_back(e);
227 float RRTBase::ocost(RRTNode *n)
230 for (auto o: *this->cobstacles_)
231 if (o.dist_to(n) < dist)
233 for (auto o: *this->sobstacles_)
234 if (o.dist_to(n) < dist)
236 return n->ocost(dist);
239 bool RRTBase::tlog(std::vector<RRTNode *> t)
242 this->slog_.push_back(this->elapsed());
243 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
244 this->nlog_.push_back(this->nodes_.size());
245 this->tlog_.push_back(t);
252 void RRTBase::tstart()
254 this->tstart_ = std::chrono::high_resolution_clock::now();
259 this->tend_ = std::chrono::high_resolution_clock::now();
262 bool RRTBase::link_obstacles(
263 std::vector<CircleObstacle> *cobstacles,
264 std::vector<SegmentObstacle> *sobstacles)
266 this->cobstacles_ = cobstacles;
267 this->sobstacles_ = sobstacles;
268 if (!this->cobstacles_ || !this->sobstacles_) {
274 bool RRTBase::add_iy(RRTNode *n)
281 this->iy_[i].push_back(n);
285 bool RRTBase::add_ixy(RRTNode *n)
287 int ix = IXI(n->x());
292 int iy = IYI(n->y());
297 this->ixy_[ix][iy].add_node(n);
301 bool RRTBase::goal_found(bool f)
303 this->goal_found_ = f;
307 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
309 for (unsigned int i = 0; i < sc.size() - 1; i++)
310 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
311 sc[0]->parent(this->goal());
312 this->slot_cusp_ = sc;
316 bool RRTBase::glplot()
318 glClear(GL_COLOR_BUFFER_BIT);
323 for (auto o: *this->sobstacles_) {
325 glVertex2f(GLVERTEX(o.init()));
326 glVertex2f(GLVERTEX(o.goal()));
333 glVertex2f(GLVERTEX(this->root_));
334 glVertex2f(GLVERTEX(this->goal_));
337 if (this->samples_.size() > 0) {
341 glVertex2f(GLVERTEX(this->samples_.back()));
345 std::vector<RRTNode *> s; // DFS stack
346 std::vector<RRTNode *> r; // reset visited_
349 s.push_back(this->root_);
350 while (s.size() > 0) {
355 for (auto ch: tmp->children()) {
357 glColor3f(0.5, 0.5, 0.5);
358 glVertex2f(GLVERTEX(tmp));
359 glVertex2f(GLVERTEX(ch));
364 // Plot nodes (from goal)
366 s.push_back(this->goal_);
367 while (s.size() > 0) {
372 for (auto ch: tmp->children()) {
374 glColor3f(0.5, 0.5, 0.5);
375 glVertex2f(GLVERTEX(tmp));
376 glVertex2f(GLVERTEX(ch));
381 std::vector<RRTNode *> cusps;
382 // Plot last trajectory
383 if (this->tlog().size() > 0) {
386 for (auto n: this->tlog().back()) {
389 glVertex2f(GLVERTEX(n));
390 glVertex2f(GLVERTEX(n->parent()));
391 if (sgn(n->s()) != sgn(n->parent()->s()))
400 for (auto n: cusps) {
402 glVertex2f(GLVERTEX(n));
405 SDL_GL_SwapWindow(gw);
411 bool RRTBase::goal_found(
413 float (*cost)(RRTNode *, RRTNode* ))
415 if (IS_NEAR(node, this->goal_)) {
416 if (this->goal_found_) {
417 if (node->ccost() + this->cost(node, this->goal_) <
418 this->goal_->ccost()) {
419 RRTNode *op; // old parent
420 float oc; // old cumulative cost
421 float od; // old direct cost
422 op = this->goal_->parent();
423 oc = this->goal_->ccost();
424 od = this->goal_->dcost();
425 node->add_child(this->goal_,
426 this->cost(node, this->goal_));
427 if (this->collide(node, this->goal_)) {
428 node->children().pop_back();
429 this->goal_->parent(op);
430 this->goal_->ccost(oc);
431 this->goal_->dcost(od);
433 op->rem_child(this->goal_);
442 this->cost(node, this->goal_));
443 if (this->collide(node, this->goal_)) {
444 node->children().pop_back();
445 this->goal_->remove_parent();
448 this->goal_found_ = true;
455 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
457 std::vector<RRTEdge *> edges;
459 volatile bool col = false;
461 while (tmp != init) {
462 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
463 std::vector<RRTEdge *> bcframe = bc.frame();
464 #pragma omp parallel for reduction(|: col)
465 for (i = 0; i < (*this->cobstacles_).size(); i++) {
466 if ((*this->cobstacles_)[i].collide(tmp)) {
469 for (auto &e: bcframe) {
470 if ((*this->cobstacles_)[i].collide(e)) {
476 for (auto e: bcframe) {
481 for (auto e: edges) {
486 #pragma omp parallel for reduction(|: col)
487 for (i = 0; i < (*this->sobstacles_).size(); i++) {
488 for (auto &e: bcframe) {
489 if ((*this->sobstacles_)[i].collide(e)) {
495 for (auto e: bcframe) {
500 for (auto e: edges) {
505 if (!tmp->parent()) {
508 edges.push_back(new RRTEdge(tmp, tmp->parent()));
510 for (auto e: bcframe) {
516 for (auto &e: edges) {
517 #pragma omp parallel for reduction(|: col)
518 for (i = 0; i < (*this->cobstacles_).size(); i++) {
519 if ((*this->cobstacles_)[i].collide(e)) {
524 for (auto e: edges) {
529 #pragma omp parallel for reduction(|: col)
530 for (i = 0; i < (*this->sobstacles_).size(); i++) {
531 if ((*this->sobstacles_)[i].collide(e)) {
536 for (auto e: edges) {
542 for (auto e: edges) {
548 class RRTNodeDijkstra {
550 RRTNodeDijkstra(int i):
556 RRTNodeDijkstra(int i, float c):
562 RRTNodeDijkstra(int i, int p, float c):
581 class RRTNodeDijkstraComparator {
584 const RRTNodeDijkstra& n1,
585 const RRTNodeDijkstra& n2)
591 bool RRTBase::optp_dijkstra(
592 std::vector<RRTNode *> &cusps,
593 std::vector<int> &npi)
595 std::vector<RRTNodeDijkstra> dnodes;
596 for (unsigned int i = 0; i < cusps.size(); i++)
598 dnodes.push_back(RRTNodeDijkstra(
603 dnodes.push_back(RRTNodeDijkstra(
609 std::vector<RRTNodeDijkstra>,
610 RRTNodeDijkstraComparator> pq;
611 RRTNodeDijkstra tmp = dnodes[0];
613 float ch_cost = 9999;
614 std::vector<RRTNode *> steered;
615 while (!pq.empty()) {
618 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
619 ch_cost = dnodes[tmp.ni].c +
620 this->cost(cusps[tmp.ni], cusps[i]);
621 steered = this->steer(cusps[tmp.ni], cusps[i]);
622 for (unsigned int j = 0; j < steered.size() - 1; j++)
623 steered[j]->add_child(steered[j + 1], 1);
626 steered[steered.size() - 1])) {
627 for (auto n: steered)
631 if (ch_cost < dnodes[i].c) {
632 dnodes[i].c = ch_cost;
633 dnodes[i].pi = tmp.ni;
637 for (auto n: steered)
641 unsigned int tmpi = 0;
642 for (auto n: dnodes) {
643 if (n.v && n.ni > tmpi)
648 tmpi = dnodes[tmpi].pi;
651 std::reverse(npi.begin(), npi.end());
655 bool RRTBase::optp_rrp(
656 std::vector<RRTNode *> &cusps,
657 std::vector<int> &npi)
659 std::vector<RRTNode *> steered;
660 std::vector<int> candidates;
661 RRTNode *x_j = nullptr;
662 RRTNode *x_i = nullptr;
663 int j = cusps.size() - 1;
677 for (int i = 0; i < j; i++) {
678 steered = this->steer(cusps[i], x_j);
679 for (unsigned int k = 0; k < steered.size() - 1; k++)
680 steered[k]->add_child(steered[k + 1], 1);
683 steered[steered.size() - 1]))
684 candidates.push_back(i);
686 if (candidates.size() <= 0)
688 i_min = candidates[0];
691 for (auto c: candidates) {
693 dx = x_j->x() - x_i->x();
694 dy = x_j->y() - x_i->y();
695 ed = EDIST(x_i, x_j);
696 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
697 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
708 std::reverse(npi.begin(), npi.end());
712 bool RRTBase::optp_smart(
713 std::vector<RRTNode *> &cusps,
714 std::vector<int> &npi)
716 std::vector<RRTNode *> steered;
717 int li = cusps.size() - 1;
721 steered = this->steer(cusps[ai - 1], cusps[li]);
722 for (unsigned int j = 0; j < steered.size() - 1; j++)
723 steered[j]->add_child(steered[j + 1], 1);
724 if (this->collide(steered[0], steered[steered.size() - 1])) {
729 for (auto n: steered)
733 std::reverse(npi.begin(), npi.end());
737 bool RRTBase::opt_path()
739 if (this->tlog().size() == 0)
741 float oc = this->tlog().back().front()->ccost();
742 std::vector<RRTNode *> tmp_cusps;
743 for (auto n: this->tlog().back()) {
744 if (sgn(n->s()) == 0) {
745 tmp_cusps.push_back(n);
746 } else if (n->parent() &&
747 sgn(n->s()) != sgn(n->parent()->s())) {
748 tmp_cusps.push_back(n);
749 tmp_cusps.push_back(n->parent());
751 //tmp_cusps.push_back(n);
753 if (tmp_cusps.size() < 2)
755 std::vector<RRTNode *> cusps;
756 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
757 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
758 cusps.push_back(tmp_cusps[i]);
760 std::reverse(cusps.begin(), cusps.end());
761 std::vector<int> npi; // new path indexes
762 if (!this->optp_dijkstra(cusps, npi))
764 RRTNode *pn = cusps[npi[0]];
765 RRTNode *tmp = nullptr;
767 for (unsigned int i = 0; i < npi.size() - 1; i++) {
769 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
772 } else if (IS_NEAR(cusps[npi[i]], ns)) {
774 while (tmp && tmp != cusps[npi[i]]) {
780 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
782 cusps[npi[i + 1]]->parent()->rem_child(
786 this->cost(pn, cusps[npi[i + 1]]));
788 } else if (IS_NEAR(pn, ns)) {
791 this->nodes().push_back(ns);
794 pn->add_child(ns, this->cost(pn, ns));
799 this->root()->update_ccost();
800 if (this->tlog().back().front()->ccost() < oc)
805 bool RRTBase::rebase(RRTNode *nr)
807 if (!nr || this->goal_ == nr || this->root_ == nr)
809 std::vector<RRTNode *> s; // DFS stack
812 unsigned int to_del = 0;
814 s.push_back(this->root_);
815 while (s.size() > 0) {
818 for (auto ch: tmp->children()) {
822 to_del = this->nodes_.size();
823 #pragma omp parallel for reduction(min: to_del)
824 for (i = 0; i < this->nodes_.size(); i++) {
825 if (this->nodes_[i] == tmp)
828 if (to_del < this->nodes_.size())
829 this->nodes_.erase(this->nodes_.begin() + to_del);
831 to_del = this->iy_[iy].size();
832 #pragma omp parallel for reduction(min: to_del)
833 for (i = 0; i < this->iy_[iy].size(); i++) {
834 if (this->iy_[iy][i] == tmp)
837 if (to_del < this->iy_[iy].size())
838 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
839 this->dnodes().push_back(tmp);
842 this->root_->remove_parent();
846 std::vector<RRTNode *> RRTBase::findt()
848 return this->findt(this->goal_);
851 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
853 std::vector<RRTNode *> nodes;
854 if (!n || !n->parent())
864 RRTNode *RRTBase::sample()
869 float RRTBase::cost(RRTNode *init, RRTNode *goal)
871 return co2(init, goal);
874 RRTNode *RRTBase::nn(RRTNode *rs)
876 return nn4(this->iy_, rs, nullptr);
877 //return nn3(this->iy_, rs, nullptr);
880 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
882 std::vector<RRTNode *> nvs;
883 unsigned int iy = IYI(node->y());
884 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
885 unsigned int i = 0; // vector index
886 unsigned int j = 0; // array index
887 unsigned int jmin = 0; // minimal j index
888 unsigned int jmax = 0; // maximal j index
890 jmin = (jmin > 0) ? jmin : 0;
891 jmax = iy + iy_dist + 1;
892 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
893 #pragma omp parallel for reduction(merge: nvs)
894 for (j = jmin; j < jmax; j++) {
895 #pragma omp parallel for reduction(merge: nvs)
896 for (i = 0; i < this->iy_[j].size(); i++) {
897 if (this->cost(this->iy_[j][i], node) < dist) {
898 nvs.push_back(this->iy_[j][i]);
905 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
907 return st3(init, goal);
910 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
912 return st3(init, goal, step);