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{}())
99 this->nodes_.reserve(NOFNODES);
100 this->nodes_.push_back(this->root_);
101 this->add_iy(this->root_);
102 this->add_ixy(this->root_);
103 float hcenter = (this->HMAX - this->HMIN) / 2 + this->HMIN;
104 float hrange = (this->HMAX - this->HMIN) / 2;
105 float vcenter = (this->VMAX - this->VMIN) / 2 + this->VMIN;
106 float vrange = (this->VMAX - this->VMIN) / 2;
107 this->ndx_ = std::normal_distribution<float>(hcenter, hrange);
108 this->ndy_ = std::normal_distribution<float>(vcenter, vrange);
109 this->ndh_ = std::normal_distribution<float>(0, 2 * M_PI);
112 RRTBase::RRTBase(RRTNode *init, RRTNode *goal)
115 , gen_(std::random_device{}())
117 this->nodes_.reserve(NOFNODES);
118 this->nodes_.push_back(init);
121 float hcenter = (this->HMAX - this->HMIN) / 2 + this->HMIN;
122 float hrange = (this->HMAX - this->HMIN) / 2;
123 float vcenter = (this->VMAX - this->VMIN) / 2 + this->VMIN;
124 float vrange = (this->VMAX - this->VMIN) / 2;
125 this->ndx_ = std::normal_distribution<float>(hcenter, hrange);
126 this->ndy_ = std::normal_distribution<float>(vcenter, vrange);
127 this->ndh_ = std::normal_distribution<float>(0, 2 * M_PI);
131 RRTNode *RRTBase::root()
136 RRTNode *RRTBase::goal()
141 std::vector<RRTNode *> &RRTBase::goals()
146 std::vector<RRTNode *> &RRTBase::nodes()
151 std::vector<RRTNode *> &RRTBase::dnodes()
153 return this->dnodes_;
156 std::queue<RRTNode *> &RRTBase::firsts()
158 return this->firsts_;
161 PolygonObstacle &RRTBase::frame()
166 std::vector<RRTNode *> &RRTBase::samples()
168 return this->samples_;
171 std::vector<CircleObstacle> *RRTBase::co()
173 return this->cobstacles_;
176 std::vector<SegmentObstacle> *RRTBase::so()
178 return this->sobstacles_;
181 std::vector<float> &RRTBase::clog()
186 std::vector<float> &RRTBase::nlog()
191 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
196 std::vector<float> &RRTBase::slog()
201 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
206 std::vector<RRTNode *> &RRTBase::slot_cusp()
208 return this->slot_cusp_;
211 bool RRTBase::goal_found()
213 return this->goal_found_;
216 float RRTBase::elapsed()
218 std::chrono::duration<float> dt;
219 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
220 this->tend_ - this->tstart_);
224 std::vector<RRTNode *> RRTBase::traj_cusp()
226 std::vector<RRTNode *> tmp_cusps;
227 for (auto n: this->tlog().back()) {
228 if (sgn(n->s()) == 0) {
229 tmp_cusps.push_back(n);
230 } else if (n->parent() &&
231 sgn(n->s()) != sgn(n->parent()->s())) {
232 tmp_cusps.push_back(n);
233 tmp_cusps.push_back(n->parent());
236 std::vector<RRTNode *> cusps;
237 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
238 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
239 cusps.push_back(tmp_cusps[i]);
245 void RRTBase::root(RRTNode *node)
250 void RRTBase::goal(RRTNode *node)
255 void RRTBase::goals(std::vector<RRTNode *> g)
258 std::reverse(this->goals_.begin(), this->goals_.end());
259 RRTNode *pn = this->goals_.front();
260 for (auto n: this->goals_) {
262 pn->add_child(n, this->cost(pn ,n));
268 bool RRTBase::logr(RRTNode *root)
270 std::vector<RRTEdge *> e; // Edges to log
271 std::vector<RRTNode *> s; // DFS stack
272 std::vector<RRTNode *> r; // reset visited_
275 while (s.size() > 0) {
280 for (auto ch: tmp->children()) {
282 e.push_back(new RRTEdge(tmp, ch));
288 this->rlog_.push_back(e);
292 float RRTBase::ocost(RRTNode *n)
295 for (auto o: *this->cobstacles_)
296 if (o.dist_to(n) < dist)
298 for (auto o: *this->sobstacles_)
299 if (o.dist_to(n) < dist)
301 return n->ocost(dist);
304 bool RRTBase::tlog(std::vector<RRTNode *> t)
307 this->slog_.push_back(this->elapsed());
308 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
309 this->nlog_.push_back(this->nodes_.size());
310 this->tlog_.push_back(t);
317 void RRTBase::tstart()
319 this->tstart_ = std::chrono::high_resolution_clock::now();
324 this->tend_ = std::chrono::high_resolution_clock::now();
327 bool RRTBase::link_obstacles(
328 std::vector<CircleObstacle> *cobstacles,
329 std::vector<SegmentObstacle> *sobstacles)
331 this->cobstacles_ = cobstacles;
332 this->sobstacles_ = sobstacles;
333 if (!this->cobstacles_ || !this->sobstacles_) {
339 bool RRTBase::add_iy(RRTNode *n)
346 this->iy_[i].push_back(n);
350 bool RRTBase::add_ixy(RRTNode *n)
352 int ix = this->XI(n);
357 int iy = this->YI(n);
362 this->ixy_[ix][iy].add_node(n);
366 bool RRTBase::goal_found(bool f)
368 this->goal_found_ = f;
372 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
374 for (unsigned int i = 0; i < sc.size() - 1; i++)
375 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
376 sc[0]->parent(this->goal());
377 this->slot_cusp_ = sc;
381 bool RRTBase::glplot()
384 glClear(GL_COLOR_BUFFER_BIT);
389 for (auto o: *this->sobstacles_) {
391 glVertex2f(GLVERTEX(o.init()));
392 glVertex2f(GLVERTEX(o.goal()));
399 glVertex2f(GLVERTEX(this->root_));
400 glVertex2f(GLVERTEX(this->goal_));
403 if (this->samples_.size() > 0) {
407 glVertex2f(GLVERTEX(this->samples_.back()));
410 // Plot nodes (position, orientation
411 std::vector<RRTNode *> s; // DFS stack
412 std::vector<RRTNode *> r; // reset visited_
415 s.push_back(this->root_);
416 while (s.size() > 0) {
421 for (auto ch: tmp->children()) {
423 glColor3f(0.5, 0.5, 0.5);
424 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
426 bc.lfx() * GLPLWSCALE,
427 bc.lfy() * GLPLHSCALE
430 bc.lrx() * GLPLWSCALE,
431 bc.lry() * GLPLHSCALE
434 bc.lrx() * GLPLWSCALE,
435 bc.lry() * GLPLHSCALE
438 bc.rrx() * GLPLWSCALE,
439 bc.rry() * GLPLHSCALE
442 bc.rrx() * GLPLWSCALE,
443 bc.rry() * GLPLHSCALE
446 bc.rfx() * GLPLWSCALE,
447 bc.rfy() * GLPLHSCALE
455 s.push_back(this->root_);
456 while (s.size() > 0) {
461 for (auto ch: tmp->children()) {
463 glColor3f(0.5, 0.5, 0.5);
464 glVertex2f(GLVERTEX(tmp));
465 glVertex2f(GLVERTEX(ch));
470 // Plot nodes (from goal)
472 s.push_back(this->goal_);
473 while (s.size() > 0) {
478 for (auto ch: tmp->children()) {
480 glColor3f(0.5, 0.5, 0.5);
481 glVertex2f(GLVERTEX(tmp));
482 glVertex2f(GLVERTEX(ch));
487 std::vector<RRTNode *> cusps;
488 // Plot last trajectory
489 if (this->tlog().size() > 0) {
492 for (auto n: this->tlog().back()) {
495 glVertex2f(GLVERTEX(n));
496 glVertex2f(GLVERTEX(n->parent()));
497 if (sgn(n->s()) != sgn(n->parent()->s()))
506 for (auto n: cusps) {
508 glVertex2f(GLVERTEX(n));
511 SDL_GL_SwapWindow(gw);
518 bool RRTBase::goal_found(
520 float (*cost)(RRTNode *, RRTNode* ))
522 if (GOAL_IS_NEAR(node, this->goal_)) {
523 if (this->goal_found_) {
524 if (node->ccost() + this->cost(node, this->goal_) <
525 this->goal_->ccost()) {
526 RRTNode *op; // old parent
527 float oc; // old cumulative cost
528 float od; // old direct cost
529 op = this->goal_->parent();
530 oc = this->goal_->ccost();
531 od = this->goal_->dcost();
532 node->add_child(this->goal_,
533 this->cost(node, this->goal_));
534 if (this->collide(node, this->goal_)) {
535 node->children().pop_back();
536 this->goal_->parent(op);
537 this->goal_->ccost(oc);
538 this->goal_->dcost(od);
540 op->rem_child(this->goal_);
549 this->cost(node, this->goal_));
550 if (this->collide(node, this->goal_)) {
551 node->children().pop_back();
552 this->goal_->remove_parent();
555 this->goal_found_ = true;
556 // Update ccost of goal's parents
557 if (this->goals().size() > 0) {
558 RRTNode *ch = this->goals().back();
559 RRTNode *pn = this->goals().back()->parent();
575 bool RRTBase::goal_found(
580 if (GOAL_IS_NEAR(node, goal)) {
581 if (this->goal_found_) {
584 && node->ccost() + this->cost(node, goal)
587 RRTNode *op; // old parent
588 float oc; // old cumulative cost
589 float od; // old direct cost
593 node->add_child(goal,
594 this->cost(node, goal));
595 if (this->collide(node, goal)) {
596 node->children().pop_back();
610 this->cost(node, goal)
612 if (this->collide(node, goal)) {
613 node->children().pop_back();
614 goal->remove_parent();
617 this->goal_found_ = true;
618 // Update ccost of goal's children
619 goal->update_ccost();
620 // Update ccost of goals
621 for (auto g: this->goals()) {
632 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
634 std::vector<RRTEdge *> edges;
636 volatile bool col = false;
638 while (tmp != init) {
639 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
640 std::vector<RRTEdge *> bcframe = bc.frame();
641 #pragma omp parallel for reduction(|: col)
642 for (i = 0; i < (*this->cobstacles_).size(); i++) {
643 if ((*this->cobstacles_)[i].collide(tmp)) {
646 for (auto &e: bcframe) {
647 if ((*this->cobstacles_)[i].collide(e)) {
653 for (auto e: bcframe) {
658 for (auto e: edges) {
663 #pragma omp parallel for reduction(|: col)
664 for (i = 0; i < (*this->sobstacles_).size(); i++) {
665 for (auto &e: bcframe) {
666 if ((*this->sobstacles_)[i].collide(e)) {
672 for (auto e: bcframe) {
677 for (auto e: edges) {
682 if (!tmp->parent()) {
685 edges.push_back(new RRTEdge(tmp, tmp->parent()));
687 for (auto e: bcframe) {
693 for (auto &e: edges) {
694 #pragma omp parallel for reduction(|: col)
695 for (i = 0; i < (*this->cobstacles_).size(); i++) {
696 if ((*this->cobstacles_)[i].collide(e)) {
701 for (auto e: edges) {
706 #pragma omp parallel for reduction(|: col)
707 for (i = 0; i < (*this->sobstacles_).size(); i++) {
708 if ((*this->sobstacles_)[i].collide(e)) {
713 for (auto e: edges) {
719 for (auto e: edges) {
725 class RRTNodeDijkstra {
727 RRTNodeDijkstra(int i):
733 RRTNodeDijkstra(int i, float c):
739 RRTNodeDijkstra(int i, int p, float c):
758 class RRTNodeDijkstraComparator {
761 const RRTNodeDijkstra& n1,
762 const RRTNodeDijkstra& n2)
768 bool RRTBase::optp_dijkstra(
769 std::vector<RRTNode *> &cusps,
770 std::vector<int> &npi)
772 std::vector<RRTNodeDijkstra> dnodes;
773 for (unsigned int i = 0; i < cusps.size(); i++)
775 dnodes.push_back(RRTNodeDijkstra(
780 dnodes.push_back(RRTNodeDijkstra(
786 std::vector<RRTNodeDijkstra>,
787 RRTNodeDijkstraComparator> pq;
788 RRTNodeDijkstra tmp = dnodes[0];
790 float ch_cost = 9999;
791 std::vector<RRTNode *> steered;
792 while (!pq.empty()) {
795 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
796 ch_cost = dnodes[tmp.ni].c +
797 this->cost(cusps[tmp.ni], cusps[i]);
798 steered = this->steer(cusps[tmp.ni], cusps[i]);
799 if (steered.size() <= 0)
801 for (unsigned int j = 0; j < steered.size() - 1; j++)
802 steered[j]->add_child(steered[j + 1], 1);
805 steered[steered.size() - 1])) {
806 for (auto n: steered)
810 if (ch_cost < dnodes[i].c) {
811 dnodes[i].c = ch_cost;
812 dnodes[i].pi = tmp.ni;
816 for (auto n: steered)
820 unsigned int tmpi = 0;
821 for (auto n: dnodes) {
822 if (n.v && n.ni > tmpi)
827 tmpi = dnodes[tmpi].pi;
830 std::reverse(npi.begin(), npi.end());
834 bool RRTBase::optp_rrp(
835 std::vector<RRTNode *> &cusps,
836 std::vector<int> &npi)
838 std::vector<RRTNode *> steered;
839 std::vector<int> candidates;
840 RRTNode *x_j = nullptr;
841 RRTNode *x_i = nullptr;
842 int j = cusps.size() - 1;
856 for (int i = 0; i < j; i++) {
857 steered = this->steer(cusps[i], x_j);
858 for (unsigned int k = 0; k < steered.size() - 1; k++)
859 steered[k]->add_child(steered[k + 1], 1);
862 steered[steered.size() - 1]))
863 candidates.push_back(i);
865 if (candidates.size() <= 0)
867 i_min = candidates[0];
870 for (auto c: candidates) {
872 dx = x_j->x() - x_i->x();
873 dy = x_j->y() - x_i->y();
874 ed = EDIST(x_i, x_j);
875 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
876 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
887 std::reverse(npi.begin(), npi.end());
891 bool RRTBase::optp_smart(
892 std::vector<RRTNode *> &cusps,
893 std::vector<int> &npi)
895 std::vector<RRTNode *> steered;
896 int li = cusps.size() - 1;
900 steered = this->steer(cusps[ai - 1], cusps[li]);
901 for (unsigned int j = 0; j < steered.size() - 1; j++)
902 steered[j]->add_child(steered[j + 1], 1);
903 if (this->collide(steered[0], steered[steered.size() - 1])) {
908 for (auto n: steered)
912 std::reverse(npi.begin(), npi.end());
916 bool RRTBase::opt_path()
918 if (this->tlog().size() == 0)
920 float oc = this->tlog().back().front()->ccost();
921 std::vector<RRTNode *> tmp_cusps;
922 for (auto n: this->tlog().back()) {
923 if (sgn(n->s()) == 0) {
924 tmp_cusps.push_back(n);
925 } else if (n->parent() &&
926 sgn(n->s()) != sgn(n->parent()->s())) {
927 tmp_cusps.push_back(n);
928 tmp_cusps.push_back(n->parent());
930 //tmp_cusps.push_back(n);
932 if (tmp_cusps.size() < 2)
934 std::vector<RRTNode *> cusps;
935 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
938 tmp_cusps[(i + 1) % tmp_cusps.size()]
940 cusps.push_back(tmp_cusps[i]);
942 std::reverse(cusps.begin(), cusps.end());
943 std::vector<int> npi; // new path indexes
944 if (!this->optp_dijkstra(cusps, npi))
946 RRTNode *pn = cusps[npi[0]];
947 RRTNode *tmp = nullptr;
949 for (unsigned int i = 0; i < npi.size() - 1; i++) {
951 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
954 } else if (IS_NEAR(cusps[npi[i]], ns)) {
956 while (tmp && tmp != cusps[npi[i]]) {
962 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
964 cusps[npi[i + 1]]->parent()->rem_child(
968 this->cost(pn, cusps[npi[i + 1]]));
970 } else if (IS_NEAR(pn, ns)) {
973 this->nodes().push_back(ns);
976 pn->add_child(ns, this->cost(pn, ns));
981 this->root()->update_ccost();
982 if (this->tlog().back().front()->ccost() < oc)
987 bool RRTBase::rebase(RRTNode *nr)
989 if (!nr || this->goal_ == nr || this->root_ == nr)
991 std::vector<RRTNode *> s; // DFS stack
994 unsigned int to_del = 0;
996 s.push_back(this->root_);
997 while (s.size() > 0) {
1000 for (auto ch: tmp->children()) {
1004 to_del = this->nodes_.size();
1005 #pragma omp parallel for reduction(min: to_del)
1006 for (i = 0; i < this->nodes_.size(); i++) {
1007 if (this->nodes_[i] == tmp)
1010 if (to_del < this->nodes_.size())
1011 this->nodes_.erase(this->nodes_.begin() + to_del);
1013 to_del = this->iy_[iy].size();
1014 #pragma omp parallel for reduction(min: to_del)
1015 for (i = 0; i < this->iy_[iy].size(); i++) {
1016 if (this->iy_[iy][i] == tmp)
1019 if (to_del < this->iy_[iy].size())
1020 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
1021 this->dnodes().push_back(tmp);
1024 this->root_->remove_parent();
1028 std::vector<RRTNode *> RRTBase::findt()
1030 return this->findt(this->goal_);
1033 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
1035 std::vector<RRTNode *> nodes;
1036 if (!n || !n->parent())
1045 int RRTBase::XI(RRTNode *n)
1047 float step = (this->HMAX - this->HMIN) / IXSIZE;
1048 float index = (int) (floor(n->x() - this->HMIN) / step);
1052 int RRTBase::YI(RRTNode *n)
1054 float step = (this->VMAX - this->VMIN) / IYSIZE;
1055 float index = (int) (floor(n->y() - this->VMIN) / step);
1060 void RRTBase::setSamplingInfo(SamplingInfo si)
1062 this->ndx_ = std::normal_distribution<float>(si.x0, si.x);
1063 this->ndy_ = std::normal_distribution<float>(si.y0, si.y);
1064 this->ndh_ = std::normal_distribution<float>(si.h0, si.h);
1067 RRTNode *RRTBase::sample()
1069 float x = this->ndx_(this->gen_);
1070 float y = this->ndy_(this->gen_);
1071 float h = this->ndh_(this->gen_);
1072 return new RRTNode(x, y, h);
1075 float RRTBase::cost(RRTNode *init, RRTNode *goal)
1077 return co2(init, goal);
1080 RRTNode *RRTBase::nn(RRTNode *rs)
1082 int iy = this->YI(rs);
1086 unsigned int i = 0; // vector step
1087 unsigned int j = 0; // array step
1089 while (nn.mc > j * IYSTEP) {
1090 iyj = (int) (iy + j);
1093 #pragma omp parallel for reduction(minn: nn)
1094 for (i = 0; i < this->iy_[iyj].size(); i++) {
1095 if (EDIST(this->iy_[iyj][i], rs) < nn.mc) {
1096 nn.mc = EDIST(this->iy_[iyj][i], rs);
1097 nn.nn = this->iy_[iyj][i];
1101 iyj = (int) (iy - j);
1104 #pragma omp parallel for reduction(minn: nn)
1105 for (i = 0; i < this->iy_[iyj].size(); i++) {
1106 if (EDIST(this->iy_[iyj][i], rs) < nn.mc) {
1107 nn.mc = EDIST(this->iy_[iyj][i], rs);
1108 nn.nn = this->iy_[iyj][i];
1117 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
1119 std::vector<RRTNode *> nvs;
1120 unsigned int iy = this->YI(node);
1121 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
1122 unsigned int i = 0; // vector index
1123 unsigned int j = 0; // array index
1124 unsigned int jmin = 0; // minimal j index
1125 unsigned int jmax = 0; // maximal j index
1126 jmin = iy - iy_dist;
1127 jmin = (jmin > 0) ? jmin : 0;
1128 jmax = iy + iy_dist + 1;
1129 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
1130 #pragma omp parallel for reduction(merge: nvs)
1131 for (j = jmin; j < jmax; j++) {
1132 #pragma omp parallel for reduction(merge: nvs)
1133 for (i = 0; i < this->iy_[j].size(); i++) {
1134 if (this->cost(this->iy_[j][i], node) < dist) {
1135 nvs.push_back(this->iy_[j][i]);
1142 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
1144 return st3(init, goal);
1147 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
1149 return st3(init, goal, step);