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)
241 bool RRTBase::logr(RRTNode *root)
243 std::vector<RRTEdge *> e; // Edges to log
244 std::vector<RRTNode *> s; // DFS stack
245 std::vector<RRTNode *> r; // reset visited_
248 while (s.size() > 0) {
253 for (auto ch: tmp->children()) {
255 e.push_back(new RRTEdge(tmp, ch));
261 this->rlog_.push_back(e);
265 float RRTBase::ocost(RRTNode *n)
268 for (auto o: *this->cobstacles_)
269 if (o.dist_to(n) < dist)
271 for (auto o: *this->sobstacles_)
272 if (o.dist_to(n) < dist)
274 return n->ocost(dist);
277 bool RRTBase::tlog(std::vector<RRTNode *> t)
280 this->slog_.push_back(this->elapsed());
281 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
282 this->nlog_.push_back(this->nodes_.size());
283 this->tlog_.push_back(t);
290 void RRTBase::tstart()
292 this->tstart_ = std::chrono::high_resolution_clock::now();
297 this->tend_ = std::chrono::high_resolution_clock::now();
300 bool RRTBase::link_obstacles(
301 std::vector<CircleObstacle> *cobstacles,
302 std::vector<SegmentObstacle> *sobstacles)
304 this->cobstacles_ = cobstacles;
305 this->sobstacles_ = sobstacles;
306 if (!this->cobstacles_ || !this->sobstacles_) {
312 bool RRTBase::add_iy(RRTNode *n)
319 this->iy_[i].push_back(n);
323 bool RRTBase::add_ixy(RRTNode *n)
325 int ix = IXI(n->x());
330 int iy = IYI(n->y());
335 this->ixy_[ix][iy].add_node(n);
339 bool RRTBase::goal_found(bool f)
341 this->goal_found_ = f;
345 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
347 for (unsigned int i = 0; i < sc.size() - 1; i++)
348 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
349 sc[0]->parent(this->goal());
350 this->slot_cusp_ = sc;
354 bool RRTBase::glplot()
357 glClear(GL_COLOR_BUFFER_BIT);
362 for (auto o: *this->sobstacles_) {
364 glVertex2f(GLVERTEX(o.init()));
365 glVertex2f(GLVERTEX(o.goal()));
372 glVertex2f(GLVERTEX(this->root_));
373 glVertex2f(GLVERTEX(this->goal_));
376 if (this->samples_.size() > 0) {
380 glVertex2f(GLVERTEX(this->samples_.back()));
384 std::vector<RRTNode *> s; // DFS stack
385 std::vector<RRTNode *> r; // reset visited_
388 s.push_back(this->root_);
389 while (s.size() > 0) {
394 for (auto ch: tmp->children()) {
396 glColor3f(0.5, 0.5, 0.5);
397 glVertex2f(GLVERTEX(tmp));
398 glVertex2f(GLVERTEX(ch));
403 // Plot nodes (from goal)
405 s.push_back(this->goal_);
406 while (s.size() > 0) {
411 for (auto ch: tmp->children()) {
413 glColor3f(0.5, 0.5, 0.5);
414 glVertex2f(GLVERTEX(tmp));
415 glVertex2f(GLVERTEX(ch));
420 std::vector<RRTNode *> cusps;
421 // Plot last trajectory
422 if (this->tlog().size() > 0) {
425 for (auto n: this->tlog().back()) {
428 glVertex2f(GLVERTEX(n));
429 glVertex2f(GLVERTEX(n->parent()));
430 if (sgn(n->s()) != sgn(n->parent()->s()))
439 for (auto n: cusps) {
441 glVertex2f(GLVERTEX(n));
444 SDL_GL_SwapWindow(gw);
451 bool RRTBase::goal_found(
453 float (*cost)(RRTNode *, RRTNode* ))
455 if (IS_NEAR(node, this->goal_)) {
456 if (this->goal_found_) {
457 if (node->ccost() + this->cost(node, this->goal_) <
458 this->goal_->ccost()) {
459 RRTNode *op; // old parent
460 float oc; // old cumulative cost
461 float od; // old direct cost
462 op = this->goal_->parent();
463 oc = this->goal_->ccost();
464 od = this->goal_->dcost();
465 node->add_child(this->goal_,
466 this->cost(node, this->goal_));
467 if (this->collide(node, this->goal_)) {
468 node->children().pop_back();
469 this->goal_->parent(op);
470 this->goal_->ccost(oc);
471 this->goal_->dcost(od);
473 op->rem_child(this->goal_);
482 this->cost(node, this->goal_));
483 if (this->collide(node, this->goal_)) {
484 node->children().pop_back();
485 this->goal_->remove_parent();
488 this->goal_found_ = true;
495 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
497 std::vector<RRTEdge *> edges;
499 volatile bool col = false;
501 while (tmp != init) {
502 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
503 std::vector<RRTEdge *> bcframe = bc.frame();
504 #pragma omp parallel for reduction(|: col)
505 for (i = 0; i < (*this->cobstacles_).size(); i++) {
506 if ((*this->cobstacles_)[i].collide(tmp)) {
509 for (auto &e: bcframe) {
510 if ((*this->cobstacles_)[i].collide(e)) {
516 for (auto e: bcframe) {
521 for (auto e: edges) {
526 #pragma omp parallel for reduction(|: col)
527 for (i = 0; i < (*this->sobstacles_).size(); i++) {
528 for (auto &e: bcframe) {
529 if ((*this->sobstacles_)[i].collide(e)) {
535 for (auto e: bcframe) {
540 for (auto e: edges) {
545 if (!tmp->parent()) {
548 edges.push_back(new RRTEdge(tmp, tmp->parent()));
550 for (auto e: bcframe) {
556 for (auto &e: edges) {
557 #pragma omp parallel for reduction(|: col)
558 for (i = 0; i < (*this->cobstacles_).size(); i++) {
559 if ((*this->cobstacles_)[i].collide(e)) {
564 for (auto e: edges) {
569 #pragma omp parallel for reduction(|: col)
570 for (i = 0; i < (*this->sobstacles_).size(); i++) {
571 if ((*this->sobstacles_)[i].collide(e)) {
576 for (auto e: edges) {
582 for (auto e: edges) {
588 class RRTNodeDijkstra {
590 RRTNodeDijkstra(int i):
596 RRTNodeDijkstra(int i, float c):
602 RRTNodeDijkstra(int i, int p, float c):
621 class RRTNodeDijkstraComparator {
624 const RRTNodeDijkstra& n1,
625 const RRTNodeDijkstra& n2)
631 bool RRTBase::optp_dijkstra(
632 std::vector<RRTNode *> &cusps,
633 std::vector<int> &npi)
635 std::vector<RRTNodeDijkstra> dnodes;
636 for (unsigned int i = 0; i < cusps.size(); i++)
638 dnodes.push_back(RRTNodeDijkstra(
643 dnodes.push_back(RRTNodeDijkstra(
649 std::vector<RRTNodeDijkstra>,
650 RRTNodeDijkstraComparator> pq;
651 RRTNodeDijkstra tmp = dnodes[0];
653 float ch_cost = 9999;
654 std::vector<RRTNode *> steered;
655 while (!pq.empty()) {
658 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
659 ch_cost = dnodes[tmp.ni].c +
660 this->cost(cusps[tmp.ni], cusps[i]);
661 steered = this->steer(cusps[tmp.ni], cusps[i]);
662 for (unsigned int j = 0; j < steered.size() - 1; j++)
663 steered[j]->add_child(steered[j + 1], 1);
666 steered[steered.size() - 1])) {
667 for (auto n: steered)
671 if (ch_cost < dnodes[i].c) {
672 dnodes[i].c = ch_cost;
673 dnodes[i].pi = tmp.ni;
677 for (auto n: steered)
681 unsigned int tmpi = 0;
682 for (auto n: dnodes) {
683 if (n.v && n.ni > tmpi)
688 tmpi = dnodes[tmpi].pi;
691 std::reverse(npi.begin(), npi.end());
695 bool RRTBase::optp_rrp(
696 std::vector<RRTNode *> &cusps,
697 std::vector<int> &npi)
699 std::vector<RRTNode *> steered;
700 std::vector<int> candidates;
701 RRTNode *x_j = nullptr;
702 RRTNode *x_i = nullptr;
703 int j = cusps.size() - 1;
717 for (int i = 0; i < j; i++) {
718 steered = this->steer(cusps[i], x_j);
719 for (unsigned int k = 0; k < steered.size() - 1; k++)
720 steered[k]->add_child(steered[k + 1], 1);
723 steered[steered.size() - 1]))
724 candidates.push_back(i);
726 if (candidates.size() <= 0)
728 i_min = candidates[0];
731 for (auto c: candidates) {
733 dx = x_j->x() - x_i->x();
734 dy = x_j->y() - x_i->y();
735 ed = EDIST(x_i, x_j);
736 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
737 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
748 std::reverse(npi.begin(), npi.end());
752 bool RRTBase::optp_smart(
753 std::vector<RRTNode *> &cusps,
754 std::vector<int> &npi)
756 std::vector<RRTNode *> steered;
757 int li = cusps.size() - 1;
761 steered = this->steer(cusps[ai - 1], cusps[li]);
762 for (unsigned int j = 0; j < steered.size() - 1; j++)
763 steered[j]->add_child(steered[j + 1], 1);
764 if (this->collide(steered[0], steered[steered.size() - 1])) {
769 for (auto n: steered)
773 std::reverse(npi.begin(), npi.end());
777 bool RRTBase::opt_path()
779 if (this->tlog().size() == 0)
781 float oc = this->tlog().back().front()->ccost();
782 std::vector<RRTNode *> tmp_cusps;
783 for (auto n: this->tlog().back()) {
784 if (sgn(n->s()) == 0) {
785 tmp_cusps.push_back(n);
786 } else if (n->parent() &&
787 sgn(n->s()) != sgn(n->parent()->s())) {
788 tmp_cusps.push_back(n);
789 tmp_cusps.push_back(n->parent());
791 //tmp_cusps.push_back(n);
793 if (tmp_cusps.size() < 2)
795 std::vector<RRTNode *> cusps;
796 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
797 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
798 cusps.push_back(tmp_cusps[i]);
800 std::reverse(cusps.begin(), cusps.end());
801 std::vector<int> npi; // new path indexes
802 if (!this->optp_dijkstra(cusps, npi))
804 RRTNode *pn = cusps[npi[0]];
805 RRTNode *tmp = nullptr;
807 for (unsigned int i = 0; i < npi.size() - 1; i++) {
809 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
812 } else if (IS_NEAR(cusps[npi[i]], ns)) {
814 while (tmp && tmp != cusps[npi[i]]) {
820 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
822 cusps[npi[i + 1]]->parent()->rem_child(
826 this->cost(pn, cusps[npi[i + 1]]));
828 } else if (IS_NEAR(pn, ns)) {
831 this->nodes().push_back(ns);
834 pn->add_child(ns, this->cost(pn, ns));
839 this->root()->update_ccost();
840 if (this->tlog().back().front()->ccost() < oc)
845 bool RRTBase::rebase(RRTNode *nr)
847 if (!nr || this->goal_ == nr || this->root_ == nr)
849 std::vector<RRTNode *> s; // DFS stack
852 unsigned int to_del = 0;
854 s.push_back(this->root_);
855 while (s.size() > 0) {
858 for (auto ch: tmp->children()) {
862 to_del = this->nodes_.size();
863 #pragma omp parallel for reduction(min: to_del)
864 for (i = 0; i < this->nodes_.size(); i++) {
865 if (this->nodes_[i] == tmp)
868 if (to_del < this->nodes_.size())
869 this->nodes_.erase(this->nodes_.begin() + to_del);
871 to_del = this->iy_[iy].size();
872 #pragma omp parallel for reduction(min: to_del)
873 for (i = 0; i < this->iy_[iy].size(); i++) {
874 if (this->iy_[iy][i] == tmp)
877 if (to_del < this->iy_[iy].size())
878 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
879 this->dnodes().push_back(tmp);
882 this->root_->remove_parent();
886 std::vector<RRTNode *> RRTBase::findt()
888 return this->findt(this->goal_);
891 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
893 std::vector<RRTNode *> nodes;
894 if (!n || !n->parent())
904 RRTNode *RRTBase::sample()
906 if (this->useSamplingInfo_ && this->nodes().size() % 2 == 0) {
907 float x = static_cast<float>(rand());
908 x /= static_cast<float>(RAND_MAX / this->samplingInfo_.x);
909 x -= this->samplingInfo_.x / 2;
910 x += this->samplingInfo_.x0;
911 float y = static_cast<float>(rand());
912 y /= static_cast<float>(RAND_MAX / this->samplingInfo_.y);
913 y -= this->samplingInfo_.y / 2;
914 y += this->samplingInfo_.y0;
915 float h = static_cast<float>(rand());
916 h /= static_cast<float>(RAND_MAX / this->samplingInfo_.h);
917 h -= this->samplingInfo_.h / 2;
918 h += this->samplingInfo_.h0;
919 return new RRTNode(x, y, h);
925 float RRTBase::cost(RRTNode *init, RRTNode *goal)
927 return co2(init, goal);
930 RRTNode *RRTBase::nn(RRTNode *rs)
932 return nn4(this->iy_, rs, nullptr);
933 //return nn3(this->iy_, rs, nullptr);
936 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
938 std::vector<RRTNode *> nvs;
939 unsigned int iy = IYI(node->y());
940 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
941 unsigned int i = 0; // vector index
942 unsigned int j = 0; // array index
943 unsigned int jmin = 0; // minimal j index
944 unsigned int jmax = 0; // maximal j index
946 jmin = (jmin > 0) ? jmin : 0;
947 jmax = iy + iy_dist + 1;
948 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
949 #pragma omp parallel for reduction(merge: nvs)
950 for (j = jmin; j < jmax; j++) {
951 #pragma omp parallel for reduction(merge: nvs)
952 for (i = 0; i < this->iy_[j].size(); i++) {
953 if (this->cost(this->iy_[j][i], node) < dist) {
954 nvs.push_back(this->iy_[j][i]);
961 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
963 return st3(init, goal);
966 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
968 return st3(init, goal, step);