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_)
95 : root_(new RRTNode())
96 , goal_(new RRTNode())
97 , gen_(std::random_device{}())
98 , ndx_(HMAX - HMIN, (HMAX - HMIN) / 4)
99 , ndy_(VMAX - VMIN, (VMAX - VMIN) / 4)
100 , ndh_(0, M_PI * 2 / 4)
102 this->nodes_.reserve(NOFNODES);
103 this->nodes_.push_back(this->root_);
104 this->add_iy(this->root_);
105 this->add_ixy(this->root_);
108 RRTBase::RRTBase(RRTNode *init, RRTNode *goal)
111 , gen_(std::random_device{}())
112 , ndx_(HMIN + (HMAX - HMIN) / 2, (HMAX - HMIN) / 4)
113 , ndy_(VMIN + (VMAX - VMIN) / 2, (VMAX - VMIN) / 4)
114 , ndh_(0, M_PI * 2 / 4)
116 this->nodes_.reserve(NOFNODES);
117 this->nodes_.push_back(init);
123 RRTNode *RRTBase::root()
128 RRTNode *RRTBase::goal()
133 std::vector<RRTNode *> &RRTBase::goals()
138 std::vector<RRTNode *> &RRTBase::nodes()
143 std::vector<RRTNode *> &RRTBase::dnodes()
145 return this->dnodes_;
148 std::queue<RRTNode *> &RRTBase::firsts()
150 return this->firsts_;
153 PolygonObstacle &RRTBase::frame()
158 std::vector<RRTNode *> &RRTBase::samples()
160 return this->samples_;
163 std::vector<CircleObstacle> *RRTBase::co()
165 return this->cobstacles_;
168 std::vector<SegmentObstacle> *RRTBase::so()
170 return this->sobstacles_;
173 std::vector<float> &RRTBase::clog()
178 std::vector<float> &RRTBase::nlog()
183 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
188 std::vector<float> &RRTBase::slog()
193 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
198 std::vector<RRTNode *> &RRTBase::slot_cusp()
200 return this->slot_cusp_;
203 bool RRTBase::goal_found()
205 return this->goal_found_;
208 float RRTBase::elapsed()
210 std::chrono::duration<float> dt;
211 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
212 this->tend_ - this->tstart_);
216 std::vector<RRTNode *> RRTBase::traj_cusp()
218 std::vector<RRTNode *> tmp_cusps;
219 for (auto n: this->tlog().back()) {
220 if (sgn(n->s()) == 0) {
221 tmp_cusps.push_back(n);
222 } else if (n->parent() &&
223 sgn(n->s()) != sgn(n->parent()->s())) {
224 tmp_cusps.push_back(n);
225 tmp_cusps.push_back(n->parent());
228 std::vector<RRTNode *> cusps;
229 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
230 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
231 cusps.push_back(tmp_cusps[i]);
237 void RRTBase::root(RRTNode *node)
242 void RRTBase::goal(RRTNode *node)
247 void RRTBase::goals(std::vector<RRTNode *> g)
250 std::reverse(this->goals_.begin(), this->goals_.end());
251 RRTNode *pn = this->goals_.front();
252 for (auto n: this->goals_) {
254 pn->add_child(n, this->cost(pn ,n));
260 bool RRTBase::logr(RRTNode *root)
262 std::vector<RRTEdge *> e; // Edges to log
263 std::vector<RRTNode *> s; // DFS stack
264 std::vector<RRTNode *> r; // reset visited_
267 while (s.size() > 0) {
272 for (auto ch: tmp->children()) {
274 e.push_back(new RRTEdge(tmp, ch));
280 this->rlog_.push_back(e);
284 float RRTBase::ocost(RRTNode *n)
287 for (auto o: *this->cobstacles_)
288 if (o.dist_to(n) < dist)
290 for (auto o: *this->sobstacles_)
291 if (o.dist_to(n) < dist)
293 return n->ocost(dist);
296 bool RRTBase::tlog(std::vector<RRTNode *> t)
299 this->slog_.push_back(this->elapsed());
300 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
301 this->nlog_.push_back(this->nodes_.size());
302 this->tlog_.push_back(t);
309 void RRTBase::tstart()
311 this->tstart_ = std::chrono::high_resolution_clock::now();
316 this->tend_ = std::chrono::high_resolution_clock::now();
319 bool RRTBase::link_obstacles(
320 std::vector<CircleObstacle> *cobstacles,
321 std::vector<SegmentObstacle> *sobstacles)
323 this->cobstacles_ = cobstacles;
324 this->sobstacles_ = sobstacles;
325 if (!this->cobstacles_ || !this->sobstacles_) {
331 bool RRTBase::add_iy(RRTNode *n)
338 this->iy_[i].push_back(n);
342 bool RRTBase::add_ixy(RRTNode *n)
344 int ix = IXI(n->x());
349 int iy = IYI(n->y());
354 this->ixy_[ix][iy].add_node(n);
358 bool RRTBase::goal_found(bool f)
360 this->goal_found_ = f;
364 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
366 for (unsigned int i = 0; i < sc.size() - 1; i++)
367 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
368 sc[0]->parent(this->goal());
369 this->slot_cusp_ = sc;
373 bool RRTBase::glplot()
376 glClear(GL_COLOR_BUFFER_BIT);
381 for (auto o: *this->sobstacles_) {
383 glVertex2f(GLVERTEX(o.init()));
384 glVertex2f(GLVERTEX(o.goal()));
391 glVertex2f(GLVERTEX(this->root_));
392 glVertex2f(GLVERTEX(this->goal_));
395 if (this->samples_.size() > 0) {
399 glVertex2f(GLVERTEX(this->samples_.back()));
403 std::vector<RRTNode *> s; // DFS stack
404 std::vector<RRTNode *> r; // reset visited_
407 s.push_back(this->root_);
408 while (s.size() > 0) {
413 for (auto ch: tmp->children()) {
415 glColor3f(0.5, 0.5, 0.5);
416 glVertex2f(GLVERTEX(tmp));
417 glVertex2f(GLVERTEX(ch));
422 // Plot nodes (from goal)
424 s.push_back(this->goal_);
425 while (s.size() > 0) {
430 for (auto ch: tmp->children()) {
432 glColor3f(0.5, 0.5, 0.5);
433 glVertex2f(GLVERTEX(tmp));
434 glVertex2f(GLVERTEX(ch));
439 std::vector<RRTNode *> cusps;
440 // Plot last trajectory
441 if (this->tlog().size() > 0) {
444 for (auto n: this->tlog().back()) {
447 glVertex2f(GLVERTEX(n));
448 glVertex2f(GLVERTEX(n->parent()));
449 if (sgn(n->s()) != sgn(n->parent()->s()))
458 for (auto n: cusps) {
460 glVertex2f(GLVERTEX(n));
463 SDL_GL_SwapWindow(gw);
470 bool RRTBase::goal_found(
472 float (*cost)(RRTNode *, RRTNode* ))
474 if (GOAL_IS_NEAR(node, this->goal_)) {
475 if (this->goal_found_) {
476 if (node->ccost() + this->cost(node, this->goal_) <
477 this->goal_->ccost()) {
478 RRTNode *op; // old parent
479 float oc; // old cumulative cost
480 float od; // old direct cost
481 op = this->goal_->parent();
482 oc = this->goal_->ccost();
483 od = this->goal_->dcost();
484 node->add_child(this->goal_,
485 this->cost(node, this->goal_));
486 if (this->collide(node, this->goal_)) {
487 node->children().pop_back();
488 this->goal_->parent(op);
489 this->goal_->ccost(oc);
490 this->goal_->dcost(od);
492 op->rem_child(this->goal_);
501 this->cost(node, this->goal_));
502 if (this->collide(node, this->goal_)) {
503 node->children().pop_back();
504 this->goal_->remove_parent();
507 this->goal_found_ = true;
508 // Update ccost of goal's parents
509 if (this->goals().size() > 0) {
510 RRTNode *ch = this->goals().back();
511 RRTNode *pn = this->goals().back()->parent();
527 bool RRTBase::goal_found(
532 if (GOAL_IS_NEAR(node, goal)) {
533 if (this->goal_found_) {
536 && node->ccost() + this->cost(node, goal)
539 RRTNode *op; // old parent
540 float oc; // old cumulative cost
541 float od; // old direct cost
545 node->add_child(goal,
546 this->cost(node, goal));
547 if (this->collide(node, goal)) {
548 node->children().pop_back();
562 this->cost(node, goal)
564 if (this->collide(node, goal)) {
565 node->children().pop_back();
566 goal->remove_parent();
569 this->goal_found_ = true;
570 // Update ccost of goal's children
571 goal->update_ccost();
572 // Update ccost of goals
573 for (auto g: this->goals()) {
584 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
586 std::vector<RRTEdge *> edges;
588 volatile bool col = false;
590 while (tmp != init) {
591 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
592 std::vector<RRTEdge *> bcframe = bc.frame();
593 #pragma omp parallel for reduction(|: col)
594 for (i = 0; i < (*this->cobstacles_).size(); i++) {
595 if ((*this->cobstacles_)[i].collide(tmp)) {
598 for (auto &e: bcframe) {
599 if ((*this->cobstacles_)[i].collide(e)) {
605 for (auto e: bcframe) {
610 for (auto e: edges) {
615 #pragma omp parallel for reduction(|: col)
616 for (i = 0; i < (*this->sobstacles_).size(); i++) {
617 for (auto &e: bcframe) {
618 if ((*this->sobstacles_)[i].collide(e)) {
624 for (auto e: bcframe) {
629 for (auto e: edges) {
634 if (!tmp->parent()) {
637 edges.push_back(new RRTEdge(tmp, tmp->parent()));
639 for (auto e: bcframe) {
645 for (auto &e: edges) {
646 #pragma omp parallel for reduction(|: col)
647 for (i = 0; i < (*this->cobstacles_).size(); i++) {
648 if ((*this->cobstacles_)[i].collide(e)) {
653 for (auto e: edges) {
658 #pragma omp parallel for reduction(|: col)
659 for (i = 0; i < (*this->sobstacles_).size(); i++) {
660 if ((*this->sobstacles_)[i].collide(e)) {
665 for (auto e: edges) {
671 for (auto e: edges) {
677 class RRTNodeDijkstra {
679 RRTNodeDijkstra(int i):
685 RRTNodeDijkstra(int i, float c):
691 RRTNodeDijkstra(int i, int p, float c):
710 class RRTNodeDijkstraComparator {
713 const RRTNodeDijkstra& n1,
714 const RRTNodeDijkstra& n2)
720 bool RRTBase::optp_dijkstra(
721 std::vector<RRTNode *> &cusps,
722 std::vector<int> &npi)
724 std::vector<RRTNodeDijkstra> dnodes;
725 for (unsigned int i = 0; i < cusps.size(); i++)
727 dnodes.push_back(RRTNodeDijkstra(
732 dnodes.push_back(RRTNodeDijkstra(
738 std::vector<RRTNodeDijkstra>,
739 RRTNodeDijkstraComparator> pq;
740 RRTNodeDijkstra tmp = dnodes[0];
742 float ch_cost = 9999;
743 std::vector<RRTNode *> steered;
744 while (!pq.empty()) {
747 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
748 ch_cost = dnodes[tmp.ni].c +
749 this->cost(cusps[tmp.ni], cusps[i]);
750 steered = this->steer(cusps[tmp.ni], cusps[i]);
751 for (unsigned int j = 0; j < steered.size() - 1; j++)
752 steered[j]->add_child(steered[j + 1], 1);
755 steered[steered.size() - 1])) {
756 for (auto n: steered)
760 if (ch_cost < dnodes[i].c) {
761 dnodes[i].c = ch_cost;
762 dnodes[i].pi = tmp.ni;
766 for (auto n: steered)
770 unsigned int tmpi = 0;
771 for (auto n: dnodes) {
772 if (n.v && n.ni > tmpi)
777 tmpi = dnodes[tmpi].pi;
780 std::reverse(npi.begin(), npi.end());
784 bool RRTBase::optp_rrp(
785 std::vector<RRTNode *> &cusps,
786 std::vector<int> &npi)
788 std::vector<RRTNode *> steered;
789 std::vector<int> candidates;
790 RRTNode *x_j = nullptr;
791 RRTNode *x_i = nullptr;
792 int j = cusps.size() - 1;
806 for (int i = 0; i < j; i++) {
807 steered = this->steer(cusps[i], x_j);
808 for (unsigned int k = 0; k < steered.size() - 1; k++)
809 steered[k]->add_child(steered[k + 1], 1);
812 steered[steered.size() - 1]))
813 candidates.push_back(i);
815 if (candidates.size() <= 0)
817 i_min = candidates[0];
820 for (auto c: candidates) {
822 dx = x_j->x() - x_i->x();
823 dy = x_j->y() - x_i->y();
824 ed = EDIST(x_i, x_j);
825 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
826 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
837 std::reverse(npi.begin(), npi.end());
841 bool RRTBase::optp_smart(
842 std::vector<RRTNode *> &cusps,
843 std::vector<int> &npi)
845 std::vector<RRTNode *> steered;
846 int li = cusps.size() - 1;
850 steered = this->steer(cusps[ai - 1], cusps[li]);
851 for (unsigned int j = 0; j < steered.size() - 1; j++)
852 steered[j]->add_child(steered[j + 1], 1);
853 if (this->collide(steered[0], steered[steered.size() - 1])) {
858 for (auto n: steered)
862 std::reverse(npi.begin(), npi.end());
866 bool RRTBase::opt_path()
868 if (this->tlog().size() == 0)
870 float oc = this->tlog().back().front()->ccost();
871 std::vector<RRTNode *> tmp_cusps;
872 for (auto n: this->tlog().back()) {
873 if (sgn(n->s()) == 0) {
874 tmp_cusps.push_back(n);
875 } else if (n->parent() &&
876 sgn(n->s()) != sgn(n->parent()->s())) {
877 tmp_cusps.push_back(n);
878 tmp_cusps.push_back(n->parent());
880 //tmp_cusps.push_back(n);
882 if (tmp_cusps.size() < 2)
884 std::vector<RRTNode *> cusps;
885 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
886 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
887 cusps.push_back(tmp_cusps[i]);
889 std::reverse(cusps.begin(), cusps.end());
890 std::vector<int> npi; // new path indexes
891 if (!this->optp_dijkstra(cusps, npi))
893 RRTNode *pn = cusps[npi[0]];
894 RRTNode *tmp = nullptr;
896 for (unsigned int i = 0; i < npi.size() - 1; i++) {
898 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
901 } else if (IS_NEAR(cusps[npi[i]], ns)) {
903 while (tmp && tmp != cusps[npi[i]]) {
909 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
911 cusps[npi[i + 1]]->parent()->rem_child(
915 this->cost(pn, cusps[npi[i + 1]]));
917 } else if (IS_NEAR(pn, ns)) {
920 this->nodes().push_back(ns);
923 pn->add_child(ns, this->cost(pn, ns));
928 this->root()->update_ccost();
929 if (this->tlog().back().front()->ccost() < oc)
934 bool RRTBase::rebase(RRTNode *nr)
936 if (!nr || this->goal_ == nr || this->root_ == nr)
938 std::vector<RRTNode *> s; // DFS stack
941 unsigned int to_del = 0;
943 s.push_back(this->root_);
944 while (s.size() > 0) {
947 for (auto ch: tmp->children()) {
951 to_del = this->nodes_.size();
952 #pragma omp parallel for reduction(min: to_del)
953 for (i = 0; i < this->nodes_.size(); i++) {
954 if (this->nodes_[i] == tmp)
957 if (to_del < this->nodes_.size())
958 this->nodes_.erase(this->nodes_.begin() + to_del);
960 to_del = this->iy_[iy].size();
961 #pragma omp parallel for reduction(min: to_del)
962 for (i = 0; i < this->iy_[iy].size(); i++) {
963 if (this->iy_[iy][i] == tmp)
966 if (to_del < this->iy_[iy].size())
967 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
968 this->dnodes().push_back(tmp);
971 this->root_->remove_parent();
975 std::vector<RRTNode *> RRTBase::findt()
977 return this->findt(this->goal_);
980 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
982 std::vector<RRTNode *> nodes;
983 if (!n || !n->parent())
993 void RRTBase::setSamplingInfo(SamplingInfo si)
995 this->ndx_ = std::normal_distribution<float>(si.x0, si.x);
996 this->ndy_ = std::normal_distribution<float>(si.y0, si.y);
997 this->ndh_ = std::normal_distribution<float>(si.h0, si.h);
1000 RRTNode *RRTBase::sample()
1002 float x = this->ndx_(this->gen_);
1003 float y = this->ndy_(this->gen_);
1004 float h = this->ndh_(this->gen_);
1005 return new RRTNode(x, y, h);
1008 float RRTBase::cost(RRTNode *init, RRTNode *goal)
1010 return co2(init, goal);
1013 RRTNode *RRTBase::nn(RRTNode *rs)
1015 return nn4(this->iy_, rs, nullptr);
1016 //return nn3(this->iy_, rs, nullptr);
1019 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
1021 std::vector<RRTNode *> nvs;
1022 unsigned int iy = IYI(node->y());
1023 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
1024 unsigned int i = 0; // vector index
1025 unsigned int j = 0; // array index
1026 unsigned int jmin = 0; // minimal j index
1027 unsigned int jmax = 0; // maximal j index
1028 jmin = iy - iy_dist;
1029 jmin = (jmin > 0) ? jmin : 0;
1030 jmax = iy + iy_dist + 1;
1031 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
1032 #pragma omp parallel for reduction(merge: nvs)
1033 for (j = jmin; j < jmax; j++) {
1034 #pragma omp parallel for reduction(merge: nvs)
1035 for (i = 0; i < this->iy_[j].size(); i++) {
1036 if (this->cost(this->iy_[j][i], node) < dist) {
1037 nvs.push_back(this->iy_[j][i]);
1044 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
1046 return st3(init, goal);
1049 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
1051 return st3(init, goal, step);