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)
239 std::reverse(this->goals_.begin(), this->goals_.end());
240 RRTNode *pn = this->goals_.front();
241 for (auto n: this->goals_) {
243 pn->add_child(n, this->cost(pn ,n));
249 bool RRTBase::logr(RRTNode *root)
251 std::vector<RRTEdge *> e; // Edges to log
252 std::vector<RRTNode *> s; // DFS stack
253 std::vector<RRTNode *> r; // reset visited_
256 while (s.size() > 0) {
261 for (auto ch: tmp->children()) {
263 e.push_back(new RRTEdge(tmp, ch));
269 this->rlog_.push_back(e);
273 float RRTBase::ocost(RRTNode *n)
276 for (auto o: *this->cobstacles_)
277 if (o.dist_to(n) < dist)
279 for (auto o: *this->sobstacles_)
280 if (o.dist_to(n) < dist)
282 return n->ocost(dist);
285 bool RRTBase::tlog(std::vector<RRTNode *> t)
288 this->slog_.push_back(this->elapsed());
289 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
290 this->nlog_.push_back(this->nodes_.size());
291 this->tlog_.push_back(t);
298 void RRTBase::tstart()
300 this->tstart_ = std::chrono::high_resolution_clock::now();
305 this->tend_ = std::chrono::high_resolution_clock::now();
308 bool RRTBase::link_obstacles(
309 std::vector<CircleObstacle> *cobstacles,
310 std::vector<SegmentObstacle> *sobstacles)
312 this->cobstacles_ = cobstacles;
313 this->sobstacles_ = sobstacles;
314 if (!this->cobstacles_ || !this->sobstacles_) {
320 bool RRTBase::add_iy(RRTNode *n)
327 this->iy_[i].push_back(n);
331 bool RRTBase::add_ixy(RRTNode *n)
333 int ix = IXI(n->x());
338 int iy = IYI(n->y());
343 this->ixy_[ix][iy].add_node(n);
347 bool RRTBase::goal_found(bool f)
349 this->goal_found_ = f;
353 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
355 for (unsigned int i = 0; i < sc.size() - 1; i++)
356 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
357 sc[0]->parent(this->goal());
358 this->slot_cusp_ = sc;
362 bool RRTBase::glplot()
365 glClear(GL_COLOR_BUFFER_BIT);
370 for (auto o: *this->sobstacles_) {
372 glVertex2f(GLVERTEX(o.init()));
373 glVertex2f(GLVERTEX(o.goal()));
380 glVertex2f(GLVERTEX(this->root_));
381 glVertex2f(GLVERTEX(this->goal_));
384 if (this->samples_.size() > 0) {
388 glVertex2f(GLVERTEX(this->samples_.back()));
392 std::vector<RRTNode *> s; // DFS stack
393 std::vector<RRTNode *> r; // reset visited_
396 s.push_back(this->root_);
397 while (s.size() > 0) {
402 for (auto ch: tmp->children()) {
404 glColor3f(0.5, 0.5, 0.5);
405 glVertex2f(GLVERTEX(tmp));
406 glVertex2f(GLVERTEX(ch));
411 // Plot nodes (from goal)
413 s.push_back(this->goal_);
414 while (s.size() > 0) {
419 for (auto ch: tmp->children()) {
421 glColor3f(0.5, 0.5, 0.5);
422 glVertex2f(GLVERTEX(tmp));
423 glVertex2f(GLVERTEX(ch));
428 std::vector<RRTNode *> cusps;
429 // Plot last trajectory
430 if (this->tlog().size() > 0) {
433 for (auto n: this->tlog().back()) {
436 glVertex2f(GLVERTEX(n));
437 glVertex2f(GLVERTEX(n->parent()));
438 if (sgn(n->s()) != sgn(n->parent()->s()))
447 for (auto n: cusps) {
449 glVertex2f(GLVERTEX(n));
452 SDL_GL_SwapWindow(gw);
459 bool RRTBase::goal_found(
461 float (*cost)(RRTNode *, RRTNode* ))
463 if (IS_NEAR(node, this->goal_)) {
464 if (this->goal_found_) {
465 if (node->ccost() + this->cost(node, this->goal_) <
466 this->goal_->ccost()) {
467 RRTNode *op; // old parent
468 float oc; // old cumulative cost
469 float od; // old direct cost
470 op = this->goal_->parent();
471 oc = this->goal_->ccost();
472 od = this->goal_->dcost();
473 node->add_child(this->goal_,
474 this->cost(node, this->goal_));
475 if (this->collide(node, this->goal_)) {
476 node->children().pop_back();
477 this->goal_->parent(op);
478 this->goal_->ccost(oc);
479 this->goal_->dcost(od);
481 op->rem_child(this->goal_);
490 this->cost(node, this->goal_));
491 if (this->collide(node, this->goal_)) {
492 node->children().pop_back();
493 this->goal_->remove_parent();
496 this->goal_found_ = true;
503 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
505 std::vector<RRTEdge *> edges;
507 volatile bool col = false;
509 while (tmp != init) {
510 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
511 std::vector<RRTEdge *> bcframe = bc.frame();
512 #pragma omp parallel for reduction(|: col)
513 for (i = 0; i < (*this->cobstacles_).size(); i++) {
514 if ((*this->cobstacles_)[i].collide(tmp)) {
517 for (auto &e: bcframe) {
518 if ((*this->cobstacles_)[i].collide(e)) {
524 for (auto e: bcframe) {
529 for (auto e: edges) {
534 #pragma omp parallel for reduction(|: col)
535 for (i = 0; i < (*this->sobstacles_).size(); i++) {
536 for (auto &e: bcframe) {
537 if ((*this->sobstacles_)[i].collide(e)) {
543 for (auto e: bcframe) {
548 for (auto e: edges) {
553 if (!tmp->parent()) {
556 edges.push_back(new RRTEdge(tmp, tmp->parent()));
558 for (auto e: bcframe) {
564 for (auto &e: edges) {
565 #pragma omp parallel for reduction(|: col)
566 for (i = 0; i < (*this->cobstacles_).size(); i++) {
567 if ((*this->cobstacles_)[i].collide(e)) {
572 for (auto e: edges) {
577 #pragma omp parallel for reduction(|: col)
578 for (i = 0; i < (*this->sobstacles_).size(); i++) {
579 if ((*this->sobstacles_)[i].collide(e)) {
584 for (auto e: edges) {
590 for (auto e: edges) {
596 class RRTNodeDijkstra {
598 RRTNodeDijkstra(int i):
604 RRTNodeDijkstra(int i, float c):
610 RRTNodeDijkstra(int i, int p, float c):
629 class RRTNodeDijkstraComparator {
632 const RRTNodeDijkstra& n1,
633 const RRTNodeDijkstra& n2)
639 bool RRTBase::optp_dijkstra(
640 std::vector<RRTNode *> &cusps,
641 std::vector<int> &npi)
643 std::vector<RRTNodeDijkstra> dnodes;
644 for (unsigned int i = 0; i < cusps.size(); i++)
646 dnodes.push_back(RRTNodeDijkstra(
651 dnodes.push_back(RRTNodeDijkstra(
657 std::vector<RRTNodeDijkstra>,
658 RRTNodeDijkstraComparator> pq;
659 RRTNodeDijkstra tmp = dnodes[0];
661 float ch_cost = 9999;
662 std::vector<RRTNode *> steered;
663 while (!pq.empty()) {
666 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
667 ch_cost = dnodes[tmp.ni].c +
668 this->cost(cusps[tmp.ni], cusps[i]);
669 steered = this->steer(cusps[tmp.ni], cusps[i]);
670 for (unsigned int j = 0; j < steered.size() - 1; j++)
671 steered[j]->add_child(steered[j + 1], 1);
674 steered[steered.size() - 1])) {
675 for (auto n: steered)
679 if (ch_cost < dnodes[i].c) {
680 dnodes[i].c = ch_cost;
681 dnodes[i].pi = tmp.ni;
685 for (auto n: steered)
689 unsigned int tmpi = 0;
690 for (auto n: dnodes) {
691 if (n.v && n.ni > tmpi)
696 tmpi = dnodes[tmpi].pi;
699 std::reverse(npi.begin(), npi.end());
703 bool RRTBase::optp_rrp(
704 std::vector<RRTNode *> &cusps,
705 std::vector<int> &npi)
707 std::vector<RRTNode *> steered;
708 std::vector<int> candidates;
709 RRTNode *x_j = nullptr;
710 RRTNode *x_i = nullptr;
711 int j = cusps.size() - 1;
725 for (int i = 0; i < j; i++) {
726 steered = this->steer(cusps[i], x_j);
727 for (unsigned int k = 0; k < steered.size() - 1; k++)
728 steered[k]->add_child(steered[k + 1], 1);
731 steered[steered.size() - 1]))
732 candidates.push_back(i);
734 if (candidates.size() <= 0)
736 i_min = candidates[0];
739 for (auto c: candidates) {
741 dx = x_j->x() - x_i->x();
742 dy = x_j->y() - x_i->y();
743 ed = EDIST(x_i, x_j);
744 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
745 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
756 std::reverse(npi.begin(), npi.end());
760 bool RRTBase::optp_smart(
761 std::vector<RRTNode *> &cusps,
762 std::vector<int> &npi)
764 std::vector<RRTNode *> steered;
765 int li = cusps.size() - 1;
769 steered = this->steer(cusps[ai - 1], cusps[li]);
770 for (unsigned int j = 0; j < steered.size() - 1; j++)
771 steered[j]->add_child(steered[j + 1], 1);
772 if (this->collide(steered[0], steered[steered.size() - 1])) {
777 for (auto n: steered)
781 std::reverse(npi.begin(), npi.end());
785 bool RRTBase::opt_path()
787 if (this->tlog().size() == 0)
789 float oc = this->tlog().back().front()->ccost();
790 std::vector<RRTNode *> tmp_cusps;
791 for (auto n: this->tlog().back()) {
792 if (sgn(n->s()) == 0) {
793 tmp_cusps.push_back(n);
794 } else if (n->parent() &&
795 sgn(n->s()) != sgn(n->parent()->s())) {
796 tmp_cusps.push_back(n);
797 tmp_cusps.push_back(n->parent());
799 //tmp_cusps.push_back(n);
801 if (tmp_cusps.size() < 2)
803 std::vector<RRTNode *> cusps;
804 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
805 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
806 cusps.push_back(tmp_cusps[i]);
808 std::reverse(cusps.begin(), cusps.end());
809 std::vector<int> npi; // new path indexes
810 if (!this->optp_dijkstra(cusps, npi))
812 RRTNode *pn = cusps[npi[0]];
813 RRTNode *tmp = nullptr;
815 for (unsigned int i = 0; i < npi.size() - 1; i++) {
817 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
820 } else if (IS_NEAR(cusps[npi[i]], ns)) {
822 while (tmp && tmp != cusps[npi[i]]) {
828 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
830 cusps[npi[i + 1]]->parent()->rem_child(
834 this->cost(pn, cusps[npi[i + 1]]));
836 } else if (IS_NEAR(pn, ns)) {
839 this->nodes().push_back(ns);
842 pn->add_child(ns, this->cost(pn, ns));
847 this->root()->update_ccost();
848 if (this->tlog().back().front()->ccost() < oc)
853 bool RRTBase::rebase(RRTNode *nr)
855 if (!nr || this->goal_ == nr || this->root_ == nr)
857 std::vector<RRTNode *> s; // DFS stack
860 unsigned int to_del = 0;
862 s.push_back(this->root_);
863 while (s.size() > 0) {
866 for (auto ch: tmp->children()) {
870 to_del = this->nodes_.size();
871 #pragma omp parallel for reduction(min: to_del)
872 for (i = 0; i < this->nodes_.size(); i++) {
873 if (this->nodes_[i] == tmp)
876 if (to_del < this->nodes_.size())
877 this->nodes_.erase(this->nodes_.begin() + to_del);
879 to_del = this->iy_[iy].size();
880 #pragma omp parallel for reduction(min: to_del)
881 for (i = 0; i < this->iy_[iy].size(); i++) {
882 if (this->iy_[iy][i] == tmp)
885 if (to_del < this->iy_[iy].size())
886 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
887 this->dnodes().push_back(tmp);
890 this->root_->remove_parent();
894 std::vector<RRTNode *> RRTBase::findt()
896 return this->findt(this->goal_);
899 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
901 std::vector<RRTNode *> nodes;
902 if (!n || !n->parent())
912 RRTNode *RRTBase::sample()
914 if (this->useSamplingInfo_ && this->nodes().size() % 2 == 0) {
915 float x = static_cast<float>(rand());
916 x /= static_cast<float>(RAND_MAX / this->samplingInfo_.x);
917 x -= this->samplingInfo_.x / 2;
918 x += this->samplingInfo_.x0;
919 float y = static_cast<float>(rand());
920 y /= static_cast<float>(RAND_MAX / this->samplingInfo_.y);
921 y -= this->samplingInfo_.y / 2;
922 y += this->samplingInfo_.y0;
923 float h = static_cast<float>(rand());
924 h /= static_cast<float>(RAND_MAX / this->samplingInfo_.h);
925 h -= this->samplingInfo_.h / 2;
926 h += this->samplingInfo_.h0;
927 return new RRTNode(x, y, h);
933 float RRTBase::cost(RRTNode *init, RRTNode *goal)
935 return co2(init, goal);
938 RRTNode *RRTBase::nn(RRTNode *rs)
940 return nn4(this->iy_, rs, nullptr);
941 //return nn3(this->iy_, rs, nullptr);
944 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
946 std::vector<RRTNode *> nvs;
947 unsigned int iy = IYI(node->y());
948 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
949 unsigned int i = 0; // vector index
950 unsigned int j = 0; // array index
951 unsigned int jmin = 0; // minimal j index
952 unsigned int jmax = 0; // maximal j index
954 jmin = (jmin > 0) ? jmin : 0;
955 jmax = iy + iy_dist + 1;
956 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
957 #pragma omp parallel for reduction(merge: nvs)
958 for (j = jmin; j < jmax; j++) {
959 #pragma omp parallel for reduction(merge: nvs)
960 for (i = 0; i < this->iy_[j].size(); i++) {
961 if (this->cost(this->iy_[j][i], node) < dist) {
962 nvs.push_back(this->iy_[j][i]);
969 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
971 return st3(init, goal);
974 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
976 return st3(init, goal, step);