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/>.
35 extern SDL_Window* gw;
36 extern SDL_GLContext gc;
40 pthread_mutex_init(&this->m_, NULL);
45 pthread_mutex_lock(&this->m_);
46 bool ret = this->changed_;
47 pthread_mutex_unlock(&this->m_);
51 std::vector<RRTNode *> Cell::nodes()
53 pthread_mutex_lock(&this->m_);
54 std::vector<RRTNode *> ret(this->nodes_);
55 pthread_mutex_unlock(&this->m_);
59 void Cell::add_node(RRTNode *n)
61 pthread_mutex_lock(&this->m_);
62 this->nodes_.push_back(n);
63 this->changed_ = true;
64 pthread_mutex_unlock(&this->m_);
69 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
70 // please uncommend the following code:
72 for (auto n: this->nodes_)
75 for (auto n: this->dnodes_)
76 if (n != this->root_ && n != this->goal_)
78 for (auto s: this->samples_)
81 for (auto edges: this->rlog_)
91 gen_(std::random_device{}())
93 this->nodes_.reserve(NOFNODES);
94 this->nodes_.push_back(this->root_);
95 this->add_iy(this->root_);
96 this->add_ixy(this->root_);
99 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
102 gen_(std::random_device{}())
104 this->nodes_.reserve(NOFNODES);
105 this->nodes_.push_back(init);
111 RRTNode *RRTBase::root()
116 RRTNode *RRTBase::goal()
121 std::vector<RRTNode *> &RRTBase::nodes()
126 std::vector<RRTNode *> &RRTBase::dnodes()
128 return this->dnodes_;
131 PolygonObstacle &RRTBase::frame()
136 std::vector<RRTNode *> &RRTBase::samples()
138 return this->samples_;
141 std::vector<CircleObstacle> *RRTBase::co()
143 return this->cobstacles_;
146 std::vector<SegmentObstacle> *RRTBase::so()
148 return this->sobstacles_;
151 std::vector<float> &RRTBase::clog()
156 std::vector<float> &RRTBase::nlog()
161 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
166 std::vector<float> &RRTBase::slog()
171 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
176 std::vector<RRTNode *> &RRTBase::slot_cusp()
178 return this->slot_cusp_;
181 bool RRTBase::goal_found()
183 return this->goal_found_;
186 float RRTBase::elapsed()
188 std::chrono::duration<float> dt;
189 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
190 this->tend_ - this->tstart_);
195 void RRTBase::root(RRTNode *node)
200 void RRTBase::goal(RRTNode *node)
205 bool RRTBase::logr(RRTNode *root)
207 std::vector<RRTEdge *> e; // Edges to log
208 std::vector<RRTNode *> s; // DFS stack
209 std::vector<RRTNode *> r; // reset visited_
212 while (s.size() > 0) {
217 for (auto ch: tmp->children()) {
219 e.push_back(new RRTEdge(tmp, ch));
225 this->rlog_.push_back(e);
229 float RRTBase::ocost(RRTNode *n)
232 for (auto o: *this->cobstacles_)
233 if (o.dist_to(n) < dist)
235 for (auto o: *this->sobstacles_)
236 if (o.dist_to(n) < dist)
238 return n->ocost(dist);
241 bool RRTBase::tlog(std::vector<RRTNode *> t)
244 this->slog_.push_back(this->elapsed());
245 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
246 this->nlog_.push_back(this->nodes_.size());
247 this->tlog_.push_back(t);
254 void RRTBase::tstart()
256 this->tstart_ = std::chrono::high_resolution_clock::now();
261 this->tend_ = std::chrono::high_resolution_clock::now();
264 bool RRTBase::link_obstacles(
265 std::vector<CircleObstacle> *cobstacles,
266 std::vector<SegmentObstacle> *sobstacles)
268 this->cobstacles_ = cobstacles;
269 this->sobstacles_ = sobstacles;
270 if (!this->cobstacles_ || !this->sobstacles_) {
276 bool RRTBase::add_iy(RRTNode *n)
283 this->iy_[i].push_back(n);
287 bool RRTBase::add_ixy(RRTNode *n)
289 int ix = IXI(n->x());
294 int iy = IYI(n->y());
299 this->ixy_[ix][iy].add_node(n);
303 bool RRTBase::goal_found(bool f)
305 this->goal_found_ = f;
309 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
311 for (unsigned int i = 0; i < sc.size() - 1; i++)
312 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
313 sc[0]->parent(this->goal());
314 this->slot_cusp_ = sc;
318 bool RRTBase::glplot()
320 glClear(GL_COLOR_BUFFER_BIT);
325 for (auto o: *this->sobstacles_) {
327 glVertex2f(GLVERTEX(o.init()));
328 glVertex2f(GLVERTEX(o.goal()));
335 glVertex2f(GLVERTEX(this->root_));
336 glVertex2f(GLVERTEX(this->goal_));
339 if (this->samples_.size() > 0) {
343 glVertex2f(GLVERTEX(this->samples_.back()));
347 std::vector<RRTNode *> s; // DFS stack
348 std::vector<RRTNode *> r; // reset visited_
351 s.push_back(this->root_);
352 while (s.size() > 0) {
357 for (auto ch: tmp->children()) {
359 glColor3f(0.5, 0.5, 0.5);
360 glVertex2f(GLVERTEX(tmp));
361 glVertex2f(GLVERTEX(ch));
366 // Plot nodes (from goal)
368 s.push_back(this->goal_);
369 while (s.size() > 0) {
374 for (auto ch: tmp->children()) {
376 glColor3f(0.5, 0.5, 0.5);
377 glVertex2f(GLVERTEX(tmp));
378 glVertex2f(GLVERTEX(ch));
383 std::vector<RRTNode *> cusps;
384 // Plot last trajectory
385 if (this->tlog().size() > 0) {
388 for (auto n: this->tlog().back()) {
391 glVertex2f(GLVERTEX(n));
392 glVertex2f(GLVERTEX(n->parent()));
393 if (sgn(n->s()) != sgn(n->parent()->s()))
402 for (auto n: cusps) {
404 glVertex2f(GLVERTEX(n));
407 SDL_GL_SwapWindow(gw);
413 bool RRTBase::goal_found(
415 float (*cost)(RRTNode *, RRTNode* ))
417 if (IS_NEAR(node, this->goal_)) {
418 if (this->goal_found_) {
419 if (node->ccost() + this->cost(node, this->goal_) <
420 this->goal_->ccost()) {
421 RRTNode *op; // old parent
422 float oc; // old cumulative cost
423 float od; // old direct cost
424 op = this->goal_->parent();
425 oc = this->goal_->ccost();
426 od = this->goal_->dcost();
427 node->add_child(this->goal_,
428 this->cost(node, this->goal_));
429 if (this->collide(node, this->goal_)) {
430 node->children().pop_back();
431 this->goal_->parent(op);
432 this->goal_->ccost(oc);
433 this->goal_->dcost(od);
435 op->rem_child(this->goal_);
444 this->cost(node, this->goal_));
445 if (this->collide(node, this->goal_)) {
446 node->children().pop_back();
447 this->goal_->remove_parent();
450 this->goal_found_ = true;
457 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
459 std::vector<RRTEdge *> edges;
461 volatile bool col = false;
463 while (tmp != init) {
464 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
465 std::vector<RRTEdge *> bcframe = bc.frame();
466 #pragma omp parallel for reduction(|: col)
467 for (i = 0; i < (*this->cobstacles_).size(); i++) {
468 if ((*this->cobstacles_)[i].collide(tmp)) {
471 for (auto &e: bcframe) {
472 if ((*this->cobstacles_)[i].collide(e)) {
478 for (auto e: bcframe) {
483 for (auto e: edges) {
488 #pragma omp parallel for reduction(|: col)
489 for (i = 0; i < (*this->sobstacles_).size(); i++) {
490 for (auto &e: bcframe) {
491 if ((*this->sobstacles_)[i].collide(e)) {
497 for (auto e: bcframe) {
502 for (auto e: edges) {
507 if (!tmp->parent()) {
510 edges.push_back(new RRTEdge(tmp, tmp->parent()));
512 for (auto e: bcframe) {
518 for (auto &e: edges) {
519 #pragma omp parallel for reduction(|: col)
520 for (i = 0; i < (*this->cobstacles_).size(); i++) {
521 if ((*this->cobstacles_)[i].collide(e)) {
526 for (auto e: edges) {
531 #pragma omp parallel for reduction(|: col)
532 for (i = 0; i < (*this->sobstacles_).size(); i++) {
533 if ((*this->sobstacles_)[i].collide(e)) {
538 for (auto e: edges) {
544 for (auto e: edges) {
550 class RRTNodeDijkstra {
552 RRTNodeDijkstra(int i):
558 RRTNodeDijkstra(int i, float c):
564 RRTNodeDijkstra(int i, int p, float c):
583 class RRTNodeDijkstraComparator {
586 const RRTNodeDijkstra& n1,
587 const RRTNodeDijkstra& n2)
593 bool RRTBase::optp_dijkstra(
594 std::vector<RRTNode *> &cusps,
595 std::vector<int> &npi)
597 std::vector<RRTNodeDijkstra> dnodes;
598 for (unsigned int i = 0; i < cusps.size(); i++)
600 dnodes.push_back(RRTNodeDijkstra(
605 dnodes.push_back(RRTNodeDijkstra(
611 std::vector<RRTNodeDijkstra>,
612 RRTNodeDijkstraComparator> pq;
613 RRTNodeDijkstra tmp = dnodes[0];
615 float ch_cost = 9999;
616 std::vector<RRTNode *> steered;
617 while (!pq.empty()) {
620 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
621 ch_cost = dnodes[tmp.ni].c +
622 this->cost(cusps[tmp.ni], cusps[i]);
623 steered = this->steer(cusps[tmp.ni], cusps[i]);
624 for (unsigned int j = 0; j < steered.size() - 1; j++)
625 steered[j]->add_child(steered[j + 1], 1);
628 steered[steered.size() - 1])) {
629 for (auto n: steered)
633 if (ch_cost < dnodes[i].c) {
634 dnodes[i].c = ch_cost;
635 dnodes[i].pi = tmp.ni;
639 for (auto n: steered)
643 unsigned int tmpi = 0;
644 for (auto n: dnodes) {
645 if (n.v && n.ni > tmpi)
650 tmpi = dnodes[tmpi].pi;
653 std::reverse(npi.begin(), npi.end());
657 bool RRTBase::optp_rrp(
658 std::vector<RRTNode *> &cusps,
659 std::vector<int> &npi)
661 std::vector<RRTNode *> steered;
662 std::vector<int> candidates;
663 RRTNode *x_j = nullptr;
664 RRTNode *x_i = nullptr;
665 int j = cusps.size() - 1;
679 for (int i = 0; i < j; i++) {
680 steered = this->steer(cusps[i], x_j);
681 for (unsigned int k = 0; k < steered.size() - 1; k++)
682 steered[k]->add_child(steered[k + 1], 1);
685 steered[steered.size() - 1]))
686 candidates.push_back(i);
688 if (candidates.size() <= 0)
690 i_min = candidates[0];
693 for (auto c: candidates) {
695 dx = x_j->x() - x_i->x();
696 dy = x_j->y() - x_i->y();
697 ed = EDIST(x_i, x_j);
698 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
699 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
710 std::reverse(npi.begin(), npi.end());
714 bool RRTBase::optp_smart(
715 std::vector<RRTNode *> &cusps,
716 std::vector<int> &npi)
718 std::vector<RRTNode *> steered;
719 int li = cusps.size() - 1;
723 steered = this->steer(cusps[ai - 1], cusps[li]);
724 for (unsigned int j = 0; j < steered.size() - 1; j++)
725 steered[j]->add_child(steered[j + 1], 1);
726 if (this->collide(steered[0], steered[steered.size() - 1])) {
731 for (auto n: steered)
735 std::reverse(npi.begin(), npi.end());
739 bool RRTBase::opt_path()
741 if (this->tlog().size() == 0)
743 float oc = this->tlog().back().front()->ccost();
744 std::vector<RRTNode *> tmp_cusps;
745 for (auto n: this->tlog().back()) {
746 if (sgn(n->s()) == 0) {
747 tmp_cusps.push_back(n);
748 } else if (n->parent() &&
749 sgn(n->s()) != sgn(n->parent()->s())) {
750 tmp_cusps.push_back(n);
751 tmp_cusps.push_back(n->parent());
753 //tmp_cusps.push_back(n);
755 if (tmp_cusps.size() < 2)
757 std::vector<RRTNode *> cusps;
758 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
759 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
760 cusps.push_back(tmp_cusps[i]);
762 std::reverse(cusps.begin(), cusps.end());
763 std::vector<int> npi; // new path indexes
764 if (!this->optp_dijkstra(cusps, npi))
766 RRTNode *pn = cusps[npi[0]];
767 RRTNode *tmp = nullptr;
769 for (unsigned int i = 0; i < npi.size() - 1; i++) {
771 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
774 } else if (IS_NEAR(cusps[npi[i]], ns)) {
776 while (tmp && tmp != cusps[npi[i]]) {
782 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
784 cusps[npi[i + 1]]->parent()->rem_child(
788 this->cost(pn, cusps[npi[i + 1]]));
790 } else if (IS_NEAR(pn, ns)) {
793 this->nodes().push_back(ns);
796 pn->add_child(ns, this->cost(pn, ns));
801 this->root()->update_ccost();
802 if (this->tlog().back().front()->ccost() < oc)
807 bool RRTBase::rebase(RRTNode *nr)
809 if (!nr || this->goal_ == nr || this->root_ == nr)
811 std::vector<RRTNode *> s; // DFS stack
814 unsigned int to_del = 0;
816 s.push_back(this->root_);
817 while (s.size() > 0) {
820 for (auto ch: tmp->children()) {
824 to_del = this->nodes_.size();
825 #pragma omp parallel for reduction(min: to_del)
826 for (i = 0; i < this->nodes_.size(); i++) {
827 if (this->nodes_[i] == tmp)
830 if (to_del < this->nodes_.size())
831 this->nodes_.erase(this->nodes_.begin() + to_del);
833 to_del = this->iy_[iy].size();
834 #pragma omp parallel for reduction(min: to_del)
835 for (i = 0; i < this->iy_[iy].size(); i++) {
836 if (this->iy_[iy][i] == tmp)
839 if (to_del < this->iy_[iy].size())
840 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
841 this->dnodes().push_back(tmp);
844 this->root_->remove_parent();
848 std::vector<RRTNode *> RRTBase::findt()
850 return this->findt(this->goal_);
853 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
855 std::vector<RRTNode *> nodes;
856 if (!n || !n->parent())
866 RRTNode *RRTBase::sample()
868 std::normal_distribution<float> xdist(
872 float x = this->samplingInfo_.x
874 + std::abs(xdist(this->gen_));
875 std::normal_distribution<float> ydist(
879 float y = this->samplingInfo_.y + ydist(this->gen_);
880 std::normal_distribution<float> hdist(
882 this->samplingInfo_.h / 3
884 float h = std::abs(hdist(this->gen_));
885 return new RRTNode(x, y, M_PI / 2 - h);
888 float RRTBase::cost(RRTNode *init, RRTNode *goal)
890 return co2(init, goal);
893 RRTNode *RRTBase::nn(RRTNode *rs)
895 return nn4(this->iy_, rs, nullptr);
896 //return nn3(this->iy_, rs, nullptr);
899 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
901 std::vector<RRTNode *> nvs;
902 unsigned int iy = IYI(node->y());
903 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
904 unsigned int i = 0; // vector index
905 unsigned int j = 0; // array index
906 unsigned int jmin = 0; // minimal j index
907 unsigned int jmax = 0; // maximal j index
909 jmin = (jmin > 0) ? jmin : 0;
910 jmax = iy + iy_dist + 1;
911 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
912 #pragma omp parallel for reduction(merge: nvs)
913 for (j = jmin; j < jmax; j++) {
914 #pragma omp parallel for reduction(merge: nvs)
915 for (i = 0; i < this->iy_[j].size(); i++) {
916 if (this->cost(this->iy_[j][i], node) < dist) {
917 nvs.push_back(this->iy_[j][i]);
924 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
926 return st3(init, goal);
929 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
931 return st3(init, goal, step);