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;
43 pthread_mutex_lock(&this->m_);
44 bool ret = this->changed_;
45 pthread_mutex_unlock(&this->m_);
49 std::vector<RRTNode *> Cell::nodes()
51 pthread_mutex_lock(&this->m_);
52 std::vector<RRTNode *> ret(this->nodes_);
53 pthread_mutex_unlock(&this->m_);
57 void Cell::add_node(RRTNode *n)
59 pthread_mutex_lock(&this->m_);
60 this->nodes_.push_back(n);
61 this->changed_ = true;
62 pthread_mutex_unlock(&this->m_);
67 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
68 // please uncommend the following code:
70 // for (auto n: this->nodes_)
71 // if (n != this->root_)
73 // for (auto n: this->dnodes_)
74 // if (n != this->root_ && n != this->goal_)
76 // for (auto s: this->samples_)
77 // if (s != this->goal_)
79 // for (auto edges: this->rlog_)
80 // for (auto e: edges)
82 // delete this->root_;
83 // delete this->goal_;
90 this->nodes_.reserve(20000);
91 this->nodes_.push_back(this->root_);
92 this->add_iy(this->root_);
95 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
99 this->nodes_.reserve(20000);
100 this->nodes_.push_back(init);
105 RRTNode *RRTBase::root()
110 RRTNode *RRTBase::goal()
115 std::vector<RRTNode *> &RRTBase::nodes()
120 std::vector<RRTNode *> &RRTBase::dnodes()
122 return this->dnodes_;
125 std::vector<RRTNode *> &RRTBase::samples()
127 return this->samples_;
130 std::vector<CircleObstacle> *RRTBase::co()
132 return this->cobstacles_;
135 std::vector<SegmentObstacle> *RRTBase::so()
137 return this->sobstacles_;
140 std::vector<float> &RRTBase::clog()
145 std::vector<float> &RRTBase::nlog()
150 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
155 std::vector<float> &RRTBase::slog()
160 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
165 bool RRTBase::goal_found()
167 return this->goal_found_;
170 float RRTBase::elapsed()
172 std::chrono::duration<float> dt;
173 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
174 this->tend_ - this->tstart_);
179 void RRTBase::root(RRTNode *node)
184 void RRTBase::goal(RRTNode *node)
189 bool RRTBase::logr(RRTNode *root)
191 std::vector<RRTEdge *> e; // Edges to log
192 std::vector<RRTNode *> s; // DFS stack
193 std::vector<RRTNode *> r; // reset visited_
196 while (s.size() > 0) {
201 for (auto ch: tmp->children()) {
203 e.push_back(new RRTEdge(tmp, ch));
209 this->rlog_.push_back(e);
213 float RRTBase::ocost(RRTNode *n)
216 for (auto o: *this->cobstacles_)
217 if (o.dist_to(n) < dist)
219 for (auto o: *this->sobstacles_)
220 if (o.dist_to(n) < dist)
222 return n->ocost(dist);
225 bool RRTBase::tlog(std::vector<RRTNode *> t)
228 this->slog_.push_back(this->elapsed());
229 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
230 this->nlog_.push_back(this->nodes_.size());
231 this->tlog_.push_back(t);
238 void RRTBase::tstart()
240 this->tstart_ = std::chrono::high_resolution_clock::now();
245 this->tend_ = std::chrono::high_resolution_clock::now();
248 bool RRTBase::link_obstacles(
249 std::vector<CircleObstacle> *cobstacles,
250 std::vector<SegmentObstacle> *sobstacles)
252 this->cobstacles_ = cobstacles;
253 this->sobstacles_ = sobstacles;
254 if (!this->cobstacles_ || !this->sobstacles_) {
260 bool RRTBase::add_iy(RRTNode *n)
267 this->iy_[i].push_back(n);
271 bool RRTBase::goal_found(bool f)
273 this->goal_found_ = f;
278 bool RRTBase::glplot()
280 glClear(GL_COLOR_BUFFER_BIT);
285 for (auto o: *this->sobstacles_) {
287 glVertex2f(GLVERTEX(o.init()));
288 glVertex2f(GLVERTEX(o.goal()));
295 glVertex2f(GLVERTEX(this->root_));
296 glVertex2f(GLVERTEX(this->goal_));
299 if (this->samples_.size() > 0) {
303 glVertex2f(GLVERTEX(this->samples_.back()));
307 std::vector<RRTNode *> s; // DFS stack
308 std::vector<RRTNode *> r; // reset visited_
311 s.push_back(this->root_);
312 while (s.size() > 0) {
317 for (auto ch: tmp->children()) {
319 glColor3f(0.5, 0.5, 0.5);
320 glVertex2f(GLVERTEX(tmp));
321 glVertex2f(GLVERTEX(ch));
326 // Plot nodes (from goal)
328 s.push_back(this->goal_);
329 while (s.size() > 0) {
334 for (auto ch: tmp->children()) {
336 glColor3f(0.5, 0.5, 0.5);
337 glVertex2f(GLVERTEX(tmp));
338 glVertex2f(GLVERTEX(ch));
343 std::vector<RRTNode *> cusps;
344 // Plot last trajectory
345 if (this->tlog().size() > 0) {
348 for (auto n: this->tlog().back()) {
351 glVertex2f(GLVERTEX(n));
352 glVertex2f(GLVERTEX(n->parent()));
353 if (sgn(n->s()) != sgn(n->parent()->s()))
362 for (auto n: cusps) {
364 glVertex2f(GLVERTEX(n));
367 SDL_GL_SwapWindow(gw);
373 bool RRTBase::goal_found(
375 float (*cost)(RRTNode *, RRTNode* ))
377 if (IS_NEAR(node, this->goal_)) {
378 if (this->goal_found_) {
379 if (node->ccost() + this->cost(node, this->goal_) <
380 this->goal_->ccost()) {
381 RRTNode *op; // old parent
382 float oc; // old cumulative cost
383 float od; // old direct cost
384 op = this->goal_->parent();
385 oc = this->goal_->ccost();
386 od = this->goal_->dcost();
387 node->add_child(this->goal_,
388 this->cost(node, this->goal_));
389 if (this->collide(node, this->goal_)) {
390 node->children().pop_back();
391 this->goal_->parent(op);
392 this->goal_->ccost(oc);
393 this->goal_->dcost(od);
395 op->rem_child(this->goal_);
404 this->cost(node, this->goal_));
405 if (this->collide(node, this->goal_)) {
406 node->children().pop_back();
407 this->goal_->remove_parent();
410 this->goal_found_ = true;
417 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
419 std::vector<RRTEdge *> edges;
421 volatile bool col = false;
423 while (tmp != init) {
424 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
425 std::vector<RRTEdge *> bcframe = bc.frame();
426 #pragma omp parallel for reduction(|: col)
427 for (i = 0; i < (*this->cobstacles_).size(); i++) {
428 if ((*this->cobstacles_)[i].collide(tmp)) {
431 for (auto &e: bcframe) {
432 if ((*this->cobstacles_)[i].collide(e)) {
438 for (auto e: bcframe) {
443 for (auto e: edges) {
448 #pragma omp parallel for reduction(|: col)
449 for (i = 0; i < (*this->sobstacles_).size(); i++) {
450 for (auto &e: bcframe) {
451 if ((*this->sobstacles_)[i].collide(e)) {
457 for (auto e: bcframe) {
462 for (auto e: edges) {
467 if (!tmp->parent()) {
470 edges.push_back(new RRTEdge(tmp, tmp->parent()));
472 for (auto e: bcframe) {
478 for (auto &e: edges) {
479 #pragma omp parallel for reduction(|: col)
480 for (i = 0; i < (*this->cobstacles_).size(); i++) {
481 if ((*this->cobstacles_)[i].collide(e)) {
486 for (auto e: edges) {
491 #pragma omp parallel for reduction(|: col)
492 for (i = 0; i < (*this->sobstacles_).size(); i++) {
493 if ((*this->sobstacles_)[i].collide(e)) {
498 for (auto e: edges) {
504 for (auto e: edges) {
510 class RRTNodeDijkstra {
512 RRTNodeDijkstra(int i):
518 RRTNodeDijkstra(int i, float c):
524 RRTNodeDijkstra(int i, int p, float c):
543 class RRTNodeDijkstraComparator {
546 const RRTNodeDijkstra& n1,
547 const RRTNodeDijkstra& n2)
553 bool RRTBase::optp_dijkstra(
554 std::vector<RRTNode *> &cusps,
555 std::vector<int> &npi)
557 std::vector<RRTNodeDijkstra> dnodes;
558 for (unsigned int i = 0; i < cusps.size(); i++)
560 dnodes.push_back(RRTNodeDijkstra(
565 dnodes.push_back(RRTNodeDijkstra(
571 std::vector<RRTNodeDijkstra>,
572 RRTNodeDijkstraComparator> pq;
573 RRTNodeDijkstra tmp = dnodes[0];
575 float ch_cost = 9999;
576 std::vector<RRTNode *> steered;
577 while (!pq.empty()) {
580 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
581 ch_cost = dnodes[tmp.ni].c +
582 this->cost(cusps[tmp.ni], cusps[i]);
583 steered = this->steer(cusps[tmp.ni], cusps[i]);
584 for (unsigned int j = 0; j < steered.size() - 1; j++)
585 steered[j]->add_child(steered[j + 1], 1);
588 steered[steered.size() - 1])) {
589 for (auto n: steered)
593 if (ch_cost < dnodes[i].c) {
594 dnodes[i].c = ch_cost;
595 dnodes[i].pi = tmp.ni;
599 for (auto n: steered)
603 unsigned int tmpi = 0;
604 for (auto n: dnodes) {
605 if (n.v && n.ni > tmpi)
610 tmpi = dnodes[tmpi].pi;
613 std::reverse(npi.begin(), npi.end());
617 bool RRTBase::optp_rrp(
618 std::vector<RRTNode *> &cusps,
619 std::vector<int> &npi)
621 std::vector<RRTNode *> steered;
622 std::vector<int> candidates;
623 RRTNode *x_j = nullptr;
624 RRTNode *x_i = nullptr;
625 int j = cusps.size() - 1;
639 for (int i = 0; i < j; i++) {
640 steered = this->steer(cusps[i], x_j);
641 for (unsigned int k = 0; k < steered.size() - 1; k++)
642 steered[k]->add_child(steered[k + 1], 1);
645 steered[steered.size() - 1]))
646 candidates.push_back(i);
648 if (candidates.size() <= 0)
650 i_min = candidates[0];
653 for (auto c: candidates) {
655 dx = x_j->x() - x_i->x();
656 dy = x_j->y() - x_i->y();
657 ed = EDIST(x_i, x_j);
658 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
659 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
670 std::reverse(npi.begin(), npi.end());
674 bool RRTBase::optp_smart(
675 std::vector<RRTNode *> &cusps,
676 std::vector<int> &npi)
678 std::vector<RRTNode *> steered;
679 int li = cusps.size() - 1;
683 steered = this->steer(cusps[ai - 1], cusps[li]);
684 for (unsigned int j = 0; j < steered.size() - 1; j++)
685 steered[j]->add_child(steered[j + 1], 1);
686 if (this->collide(steered[0], steered[steered.size() - 1])) {
691 for (auto n: steered)
695 std::reverse(npi.begin(), npi.end());
699 bool RRTBase::opt_path()
701 if (this->tlog().size() == 0)
703 float oc = this->tlog().back().front()->ccost();
704 std::vector<RRTNode *> tmp_cusps;
705 for (auto n: this->tlog().back()) {
706 if (sgn(n->s()) == 0) {
707 tmp_cusps.push_back(n);
708 } else if (n->parent() &&
709 sgn(n->s()) != sgn(n->parent()->s())) {
710 tmp_cusps.push_back(n);
711 tmp_cusps.push_back(n->parent());
713 //tmp_cusps.push_back(n);
715 if (tmp_cusps.size() < 2)
717 std::vector<RRTNode *> cusps;
718 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
719 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
720 cusps.push_back(tmp_cusps[i]);
722 std::reverse(cusps.begin(), cusps.end());
723 std::vector<int> npi; // new path indexes
724 if (!this->optp_dijkstra(cusps, npi))
726 RRTNode *pn = cusps[npi[0]];
727 RRTNode *tmp = nullptr;
729 for (unsigned int i = 0; i < npi.size() - 1; i++) {
731 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
734 } else if (IS_NEAR(cusps[npi[i]], ns)) {
736 while (tmp && tmp != cusps[npi[i]]) {
742 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
744 cusps[npi[i + 1]]->parent()->rem_child(
748 this->cost(pn, cusps[npi[i + 1]]));
750 } else if (IS_NEAR(pn, ns)) {
753 this->nodes().push_back(ns);
755 pn->add_child(ns, this->cost(pn, ns));
760 this->root()->update_ccost();
761 if (this->tlog().back().front()->ccost() < oc)
766 bool RRTBase::rebase(RRTNode *nr)
768 if (!nr || this->goal_ == nr || this->root_ == nr)
770 std::vector<RRTNode *> s; // DFS stack
773 unsigned int to_del = 0;
775 s.push_back(this->root_);
776 while (s.size() > 0) {
779 for (auto ch: tmp->children()) {
783 to_del = this->nodes_.size();
784 #pragma omp parallel for reduction(min: to_del)
785 for (i = 0; i < this->nodes_.size(); i++) {
786 if (this->nodes_[i] == tmp)
789 if (to_del < this->nodes_.size())
790 this->nodes_.erase(this->nodes_.begin() + to_del);
792 to_del = this->iy_[iy].size();
793 #pragma omp parallel for reduction(min: to_del)
794 for (i = 0; i < this->iy_[iy].size(); i++) {
795 if (this->iy_[iy][i] == tmp)
798 if (to_del < this->iy_[iy].size())
799 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
800 this->dnodes().push_back(tmp);
803 this->root_->remove_parent();
807 std::vector<RRTNode *> RRTBase::findt()
809 return this->findt(this->goal_);
812 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
814 std::vector<RRTNode *> nodes;
815 if (!n || !n->parent())
825 RRTNode *RRTBase::sample()
830 float RRTBase::cost(RRTNode *init, RRTNode *goal)
832 return co2(init, goal);
835 RRTNode *RRTBase::nn(RRTNode *rs)
837 return nn4(this->iy_, rs, nullptr);
838 //return nn3(this->iy_, rs, nullptr);
841 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
843 std::vector<RRTNode *> nvs;
844 unsigned int iy = IYI(node->y());
845 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
846 unsigned int i = 0; // vector index
847 unsigned int j = 0; // array index
848 unsigned int jmin = 0; // minimal j index
849 unsigned int jmax = 0; // maximal j index
851 jmin = (jmin > 0) ? jmin : 0;
852 jmax = iy + iy_dist + 1;
853 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
854 #pragma omp parallel for reduction(merge: nvs)
855 for (j = jmin; j < jmax; j++) {
856 #pragma omp parallel for reduction(merge: nvs)
857 for (i = 0; i < this->iy_[j].size(); i++) {
858 if (this->cost(this->iy_[j][i], node) < dist) {
859 nvs.push_back(this->iy_[j][i]);
866 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
868 return st3(init, goal);
871 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
873 return st3(init, goal, step);