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_);
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 std::queue<RRTNode *> &RRTBase::firsts()
144 return this->firsts_;
147 PolygonObstacle &RRTBase::frame()
152 std::vector<RRTNode *> &RRTBase::samples()
154 return this->samples_;
157 std::vector<CircleObstacle> *RRTBase::co()
159 return this->cobstacles_;
162 std::vector<SegmentObstacle> *RRTBase::so()
164 return this->sobstacles_;
167 std::vector<float> &RRTBase::clog()
172 std::vector<float> &RRTBase::nlog()
177 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
182 std::vector<float> &RRTBase::slog()
187 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
192 std::vector<RRTNode *> &RRTBase::slot_cusp()
194 return this->slot_cusp_;
197 bool RRTBase::goal_found()
199 return this->goal_found_;
202 float RRTBase::elapsed()
204 std::chrono::duration<float> dt;
205 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
206 this->tend_ - this->tstart_);
210 std::vector<RRTNode *> RRTBase::traj_cusp()
212 std::vector<RRTNode *> tmp_cusps;
213 for (auto n: this->tlog().back()) {
214 if (sgn(n->s()) == 0) {
215 tmp_cusps.push_back(n);
216 } else if (n->parent() &&
217 sgn(n->s()) != sgn(n->parent()->s())) {
218 tmp_cusps.push_back(n);
219 tmp_cusps.push_back(n->parent());
222 std::vector<RRTNode *> cusps;
223 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
224 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
225 cusps.push_back(tmp_cusps[i]);
231 void RRTBase::root(RRTNode *node)
236 void RRTBase::goal(RRTNode *node)
241 void RRTBase::goals(std::vector<RRTNode *> g)
244 std::reverse(this->goals_.begin(), this->goals_.end());
245 RRTNode *pn = this->goals_.front();
246 for (auto n: this->goals_) {
248 pn->add_child(n, this->cost(pn ,n));
254 bool RRTBase::logr(RRTNode *root)
256 std::vector<RRTEdge *> e; // Edges to log
257 std::vector<RRTNode *> s; // DFS stack
258 std::vector<RRTNode *> r; // reset visited_
261 while (s.size() > 0) {
266 for (auto ch: tmp->children()) {
268 e.push_back(new RRTEdge(tmp, ch));
274 this->rlog_.push_back(e);
278 float RRTBase::ocost(RRTNode *n)
281 for (auto o: *this->cobstacles_)
282 if (o.dist_to(n) < dist)
284 for (auto o: *this->sobstacles_)
285 if (o.dist_to(n) < dist)
287 return n->ocost(dist);
290 bool RRTBase::tlog(std::vector<RRTNode *> t)
293 this->slog_.push_back(this->elapsed());
294 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
295 this->nlog_.push_back(this->nodes_.size());
296 this->tlog_.push_back(t);
303 void RRTBase::tstart()
305 this->tstart_ = std::chrono::high_resolution_clock::now();
310 this->tend_ = std::chrono::high_resolution_clock::now();
313 bool RRTBase::link_obstacles(
314 std::vector<CircleObstacle> *cobstacles,
315 std::vector<SegmentObstacle> *sobstacles)
317 this->cobstacles_ = cobstacles;
318 this->sobstacles_ = sobstacles;
319 if (!this->cobstacles_ || !this->sobstacles_) {
325 bool RRTBase::add_iy(RRTNode *n)
332 this->iy_[i].push_back(n);
336 bool RRTBase::add_ixy(RRTNode *n)
338 int ix = IXI(n->x());
343 int iy = IYI(n->y());
348 this->ixy_[ix][iy].add_node(n);
352 bool RRTBase::goal_found(bool f)
354 this->goal_found_ = f;
358 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
360 for (unsigned int i = 0; i < sc.size() - 1; i++)
361 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
362 sc[0]->parent(this->goal());
363 this->slot_cusp_ = sc;
367 bool RRTBase::glplot()
370 glClear(GL_COLOR_BUFFER_BIT);
375 for (auto o: *this->sobstacles_) {
377 glVertex2f(GLVERTEX(o.init()));
378 glVertex2f(GLVERTEX(o.goal()));
385 glVertex2f(GLVERTEX(this->root_));
386 glVertex2f(GLVERTEX(this->goal_));
389 if (this->samples_.size() > 0) {
393 glVertex2f(GLVERTEX(this->samples_.back()));
397 std::vector<RRTNode *> s; // DFS stack
398 std::vector<RRTNode *> r; // reset visited_
401 s.push_back(this->root_);
402 while (s.size() > 0) {
407 for (auto ch: tmp->children()) {
409 glColor3f(0.5, 0.5, 0.5);
410 glVertex2f(GLVERTEX(tmp));
411 glVertex2f(GLVERTEX(ch));
416 // Plot nodes (from goal)
418 s.push_back(this->goal_);
419 while (s.size() > 0) {
424 for (auto ch: tmp->children()) {
426 glColor3f(0.5, 0.5, 0.5);
427 glVertex2f(GLVERTEX(tmp));
428 glVertex2f(GLVERTEX(ch));
433 std::vector<RRTNode *> cusps;
434 // Plot last trajectory
435 if (this->tlog().size() > 0) {
438 for (auto n: this->tlog().back()) {
441 glVertex2f(GLVERTEX(n));
442 glVertex2f(GLVERTEX(n->parent()));
443 if (sgn(n->s()) != sgn(n->parent()->s()))
452 for (auto n: cusps) {
454 glVertex2f(GLVERTEX(n));
457 SDL_GL_SwapWindow(gw);
464 bool RRTBase::goal_found(
466 float (*cost)(RRTNode *, RRTNode* ))
468 if (IS_NEAR(node, this->goal_)) {
469 if (this->goal_found_) {
470 if (node->ccost() + this->cost(node, this->goal_) <
471 this->goal_->ccost()) {
472 RRTNode *op; // old parent
473 float oc; // old cumulative cost
474 float od; // old direct cost
475 op = this->goal_->parent();
476 oc = this->goal_->ccost();
477 od = this->goal_->dcost();
478 node->add_child(this->goal_,
479 this->cost(node, this->goal_));
480 if (this->collide(node, this->goal_)) {
481 node->children().pop_back();
482 this->goal_->parent(op);
483 this->goal_->ccost(oc);
484 this->goal_->dcost(od);
486 op->rem_child(this->goal_);
495 this->cost(node, this->goal_));
496 if (this->collide(node, this->goal_)) {
497 node->children().pop_back();
498 this->goal_->remove_parent();
501 this->goal_found_ = true;
502 // Update ccost of goal's parents
503 if (this->goals().size() > 0) {
504 RRTNode *ch = this->goals().back();
505 RRTNode *pn = this->goals().back()->parent();
521 bool RRTBase::goal_found(
526 if (IS_NEAR(node, goal)) {
527 if (this->goal_found_) {
530 && node->ccost() + this->cost(node, goal)
533 RRTNode *op; // old parent
534 float oc; // old cumulative cost
535 float od; // old direct cost
539 node->add_child(goal,
540 this->cost(node, goal));
541 if (this->collide(node, goal)) {
542 node->children().pop_back();
556 this->cost(node, goal)
558 if (this->collide(node, goal)) {
559 node->children().pop_back();
560 goal->remove_parent();
563 this->goal_found_ = true;
564 // Update ccost of goal's children
565 goal->update_ccost();
566 // Update ccost of goals
567 for (auto g: this->goals()) {
578 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
580 std::vector<RRTEdge *> edges;
582 volatile bool col = false;
584 while (tmp != init) {
585 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
586 std::vector<RRTEdge *> bcframe = bc.frame();
587 #pragma omp parallel for reduction(|: col)
588 for (i = 0; i < (*this->cobstacles_).size(); i++) {
589 if ((*this->cobstacles_)[i].collide(tmp)) {
592 for (auto &e: bcframe) {
593 if ((*this->cobstacles_)[i].collide(e)) {
599 for (auto e: bcframe) {
604 for (auto e: edges) {
609 #pragma omp parallel for reduction(|: col)
610 for (i = 0; i < (*this->sobstacles_).size(); i++) {
611 for (auto &e: bcframe) {
612 if ((*this->sobstacles_)[i].collide(e)) {
618 for (auto e: bcframe) {
623 for (auto e: edges) {
628 if (!tmp->parent()) {
631 edges.push_back(new RRTEdge(tmp, tmp->parent()));
633 for (auto e: bcframe) {
639 for (auto &e: edges) {
640 #pragma omp parallel for reduction(|: col)
641 for (i = 0; i < (*this->cobstacles_).size(); i++) {
642 if ((*this->cobstacles_)[i].collide(e)) {
647 for (auto e: edges) {
652 #pragma omp parallel for reduction(|: col)
653 for (i = 0; i < (*this->sobstacles_).size(); i++) {
654 if ((*this->sobstacles_)[i].collide(e)) {
659 for (auto e: edges) {
665 for (auto e: edges) {
671 class RRTNodeDijkstra {
673 RRTNodeDijkstra(int i):
679 RRTNodeDijkstra(int i, float c):
685 RRTNodeDijkstra(int i, int p, float c):
704 class RRTNodeDijkstraComparator {
707 const RRTNodeDijkstra& n1,
708 const RRTNodeDijkstra& n2)
714 bool RRTBase::optp_dijkstra(
715 std::vector<RRTNode *> &cusps,
716 std::vector<int> &npi)
718 std::vector<RRTNodeDijkstra> dnodes;
719 for (unsigned int i = 0; i < cusps.size(); i++)
721 dnodes.push_back(RRTNodeDijkstra(
726 dnodes.push_back(RRTNodeDijkstra(
732 std::vector<RRTNodeDijkstra>,
733 RRTNodeDijkstraComparator> pq;
734 RRTNodeDijkstra tmp = dnodes[0];
736 float ch_cost = 9999;
737 std::vector<RRTNode *> steered;
738 while (!pq.empty()) {
741 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
742 ch_cost = dnodes[tmp.ni].c +
743 this->cost(cusps[tmp.ni], cusps[i]);
744 steered = this->steer(cusps[tmp.ni], cusps[i]);
745 for (unsigned int j = 0; j < steered.size() - 1; j++)
746 steered[j]->add_child(steered[j + 1], 1);
749 steered[steered.size() - 1])) {
750 for (auto n: steered)
754 if (ch_cost < dnodes[i].c) {
755 dnodes[i].c = ch_cost;
756 dnodes[i].pi = tmp.ni;
760 for (auto n: steered)
764 unsigned int tmpi = 0;
765 for (auto n: dnodes) {
766 if (n.v && n.ni > tmpi)
771 tmpi = dnodes[tmpi].pi;
774 std::reverse(npi.begin(), npi.end());
778 bool RRTBase::optp_rrp(
779 std::vector<RRTNode *> &cusps,
780 std::vector<int> &npi)
782 std::vector<RRTNode *> steered;
783 std::vector<int> candidates;
784 RRTNode *x_j = nullptr;
785 RRTNode *x_i = nullptr;
786 int j = cusps.size() - 1;
800 for (int i = 0; i < j; i++) {
801 steered = this->steer(cusps[i], x_j);
802 for (unsigned int k = 0; k < steered.size() - 1; k++)
803 steered[k]->add_child(steered[k + 1], 1);
806 steered[steered.size() - 1]))
807 candidates.push_back(i);
809 if (candidates.size() <= 0)
811 i_min = candidates[0];
814 for (auto c: candidates) {
816 dx = x_j->x() - x_i->x();
817 dy = x_j->y() - x_i->y();
818 ed = EDIST(x_i, x_j);
819 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
820 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
831 std::reverse(npi.begin(), npi.end());
835 bool RRTBase::optp_smart(
836 std::vector<RRTNode *> &cusps,
837 std::vector<int> &npi)
839 std::vector<RRTNode *> steered;
840 int li = cusps.size() - 1;
844 steered = this->steer(cusps[ai - 1], cusps[li]);
845 for (unsigned int j = 0; j < steered.size() - 1; j++)
846 steered[j]->add_child(steered[j + 1], 1);
847 if (this->collide(steered[0], steered[steered.size() - 1])) {
852 for (auto n: steered)
856 std::reverse(npi.begin(), npi.end());
860 bool RRTBase::opt_path()
862 if (this->tlog().size() == 0)
864 float oc = this->tlog().back().front()->ccost();
865 std::vector<RRTNode *> tmp_cusps;
866 for (auto n: this->tlog().back()) {
867 if (sgn(n->s()) == 0) {
868 tmp_cusps.push_back(n);
869 } else if (n->parent() &&
870 sgn(n->s()) != sgn(n->parent()->s())) {
871 tmp_cusps.push_back(n);
872 tmp_cusps.push_back(n->parent());
874 //tmp_cusps.push_back(n);
876 if (tmp_cusps.size() < 2)
878 std::vector<RRTNode *> cusps;
879 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
880 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
881 cusps.push_back(tmp_cusps[i]);
883 std::reverse(cusps.begin(), cusps.end());
884 std::vector<int> npi; // new path indexes
885 if (!this->optp_dijkstra(cusps, npi))
887 RRTNode *pn = cusps[npi[0]];
888 RRTNode *tmp = nullptr;
890 for (unsigned int i = 0; i < npi.size() - 1; i++) {
892 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
895 } else if (IS_NEAR(cusps[npi[i]], ns)) {
897 while (tmp && tmp != cusps[npi[i]]) {
903 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
905 cusps[npi[i + 1]]->parent()->rem_child(
909 this->cost(pn, cusps[npi[i + 1]]));
911 } else if (IS_NEAR(pn, ns)) {
914 this->nodes().push_back(ns);
917 pn->add_child(ns, this->cost(pn, ns));
922 this->root()->update_ccost();
923 if (this->tlog().back().front()->ccost() < oc)
928 bool RRTBase::rebase(RRTNode *nr)
930 if (!nr || this->goal_ == nr || this->root_ == nr)
932 std::vector<RRTNode *> s; // DFS stack
935 unsigned int to_del = 0;
937 s.push_back(this->root_);
938 while (s.size() > 0) {
941 for (auto ch: tmp->children()) {
945 to_del = this->nodes_.size();
946 #pragma omp parallel for reduction(min: to_del)
947 for (i = 0; i < this->nodes_.size(); i++) {
948 if (this->nodes_[i] == tmp)
951 if (to_del < this->nodes_.size())
952 this->nodes_.erase(this->nodes_.begin() + to_del);
954 to_del = this->iy_[iy].size();
955 #pragma omp parallel for reduction(min: to_del)
956 for (i = 0; i < this->iy_[iy].size(); i++) {
957 if (this->iy_[iy][i] == tmp)
960 if (to_del < this->iy_[iy].size())
961 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
962 this->dnodes().push_back(tmp);
965 this->root_->remove_parent();
969 std::vector<RRTNode *> RRTBase::findt()
971 return this->findt(this->goal_);
974 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
976 std::vector<RRTNode *> nodes;
977 if (!n || !n->parent())
987 RRTNode *RRTBase::sample()
989 if (this->useSamplingInfo_ && this->nodes().size() % 2 == 0) {
990 float x = static_cast<float>(rand());
991 x /= static_cast<float>(RAND_MAX / this->samplingInfo_.x);
992 x -= this->samplingInfo_.x / 2;
993 x += this->samplingInfo_.x0;
994 float y = static_cast<float>(rand());
995 y /= static_cast<float>(RAND_MAX / this->samplingInfo_.y);
996 y -= this->samplingInfo_.y / 2;
997 y += this->samplingInfo_.y0;
998 float h = static_cast<float>(rand());
999 h /= static_cast<float>(RAND_MAX / this->samplingInfo_.h);
1000 h -= this->samplingInfo_.h / 2;
1001 h += this->samplingInfo_.h0;
1002 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);