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 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
41 // please uncommend the following code:
43 // for (auto n: this->nodes_)
44 // if (n != this->root_)
46 // for (auto n: this->dnodes_)
47 // if (n != this->root_ && n != this->goal_)
49 // for (auto s: this->samples_)
50 // if (s != this->goal_)
52 // for (auto edges: this->rlog_)
53 // for (auto e: edges)
55 // delete this->root_;
56 // delete this->goal_;
63 this->nodes_.push_back(this->root_);
64 this->add_iy(this->root_);
67 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
71 this->nodes_.push_back(init);
76 RRTNode *RRTBase::root()
81 RRTNode *RRTBase::goal()
86 std::vector<RRTNode *> &RRTBase::nodes()
91 std::vector<RRTNode *> &RRTBase::dnodes()
96 std::vector<RRTNode *> &RRTBase::samples()
98 return this->samples_;
101 std::vector<CircleObstacle> *RRTBase::co()
103 return this->cobstacles_;
106 std::vector<SegmentObstacle> *RRTBase::so()
108 return this->sobstacles_;
111 std::vector<float> &RRTBase::clog()
116 std::vector<float> &RRTBase::nlog()
121 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
126 std::vector<float> &RRTBase::slog()
131 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
136 bool RRTBase::goal_found()
138 return this->goal_found_;
141 float RRTBase::elapsed()
143 std::chrono::duration<float> dt;
144 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
145 this->tend_ - this->tstart_);
150 void RRTBase::root(RRTNode *node)
155 void RRTBase::goal(RRTNode *node)
160 bool RRTBase::logr(RRTNode *root)
162 std::vector<RRTEdge *> e; // Edges to log
163 std::vector<RRTNode *> s; // DFS stack
164 std::vector<RRTNode *> r; // reset visited_
167 while (s.size() > 0) {
172 for (auto ch: tmp->children()) {
174 e.push_back(new RRTEdge(tmp, ch));
180 this->rlog_.push_back(e);
184 float RRTBase::ocost(RRTNode *n)
187 for (auto o: *this->cobstacles_)
188 if (o.dist_to(n) < dist)
190 for (auto o: *this->sobstacles_)
191 if (o.dist_to(n) < dist)
193 return n->ocost(dist);
196 bool RRTBase::tlog(std::vector<RRTNode *> t)
199 this->slog_.push_back(this->elapsed());
200 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
201 this->nlog_.push_back(this->nodes_.size());
202 this->tlog_.push_back(t);
209 void RRTBase::tstart()
211 this->tstart_ = std::chrono::high_resolution_clock::now();
216 this->tend_ = std::chrono::high_resolution_clock::now();
219 bool RRTBase::link_obstacles(
220 std::vector<CircleObstacle> *cobstacles,
221 std::vector<SegmentObstacle> *sobstacles)
223 this->cobstacles_ = cobstacles;
224 this->sobstacles_ = sobstacles;
225 if (!this->cobstacles_ || !this->sobstacles_) {
231 bool RRTBase::add_iy(RRTNode *n)
238 this->iy_[i].push_back(n);
242 bool RRTBase::goal_found(bool f)
244 this->goal_found_ = f;
249 bool RRTBase::glplot()
251 glClear(GL_COLOR_BUFFER_BIT);
256 for (auto o: *this->sobstacles_) {
258 glVertex2f(GLVERTEX(o.init()));
259 glVertex2f(GLVERTEX(o.goal()));
266 glVertex2f(GLVERTEX(this->root_));
267 glVertex2f(GLVERTEX(this->goal_));
270 if (this->samples_.size() > 0) {
274 glVertex2f(GLVERTEX(this->samples_.back()));
278 std::vector<RRTNode *> s; // DFS stack
279 std::vector<RRTNode *> r; // reset visited_
282 s.push_back(this->root_);
283 while (s.size() > 0) {
288 for (auto ch: tmp->children()) {
290 glColor3f(0.5, 0.5, 0.5);
291 glVertex2f(GLVERTEX(tmp));
292 glVertex2f(GLVERTEX(ch));
297 // Plot nodes (from goal)
299 s.push_back(this->goal_);
300 while (s.size() > 0) {
305 for (auto ch: tmp->children()) {
307 glColor3f(0.5, 0.5, 0.5);
308 glVertex2f(GLVERTEX(tmp));
309 glVertex2f(GLVERTEX(ch));
314 std::vector<RRTNode *> cusps;
315 // Plot last trajectory
316 if (this->tlog().size() > 0) {
319 for (auto n: this->tlog().back()) {
322 glVertex2f(GLVERTEX(n));
323 glVertex2f(GLVERTEX(n->parent()));
324 if (sgn(n->s()) != sgn(n->parent()->s()))
333 for (auto n: cusps) {
335 glVertex2f(GLVERTEX(n));
338 SDL_GL_SwapWindow(gw);
344 bool RRTBase::goal_found(
346 float (*cost)(RRTNode *, RRTNode* ))
348 float xx = pow(node->x() - this->goal_->x(), 2);
349 float yy = pow(node->y() - this->goal_->y(), 2);
350 float dh = std::abs(node->h() - this->goal_->h());
351 if (IS_NEAR(node, this->goal_)) {
352 if (this->goal_found_) {
353 if (node->ccost() + (*cost)(node, this->goal_) <
354 this->goal_->ccost()) {
355 RRTNode *op; // old parent
356 float oc; // old cumulative cost
357 float od; // old direct cost
358 op = this->goal_->parent();
359 oc = this->goal_->ccost();
360 od = this->goal_->dcost();
361 node->add_child(this->goal_,
362 (*cost)(node, this->goal_));
363 if (this->collide(node, this->goal_)) {
364 node->children().pop_back();
365 this->goal_->parent(op);
366 this->goal_->ccost(oc);
367 this->goal_->dcost(od);
369 op->rem_child(this->goal_);
378 (*cost)(node, this->goal_));
379 if (this->collide(node, this->goal_)) {
380 node->children().pop_back();
381 this->goal_->remove_parent();
384 this->goal_found_ = true;
391 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
393 std::vector<RRTEdge *> edges;
395 volatile bool col = false;
397 while (tmp != init) {
398 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
399 std::vector<RRTEdge *> bcframe = bc.frame();
400 #pragma omp parallel for reduction(|: col)
401 for (i = 0; i < (*this->cobstacles_).size(); i++) {
402 if ((*this->cobstacles_)[i].collide(tmp)) {
405 for (auto &e: bcframe) {
406 if ((*this->cobstacles_)[i].collide(e)) {
412 for (auto e: bcframe) {
417 for (auto e: edges) {
422 #pragma omp parallel for reduction(|: col)
423 for (i = 0; i < (*this->sobstacles_).size(); i++) {
424 for (auto &e: bcframe) {
425 if ((*this->sobstacles_)[i].collide(e)) {
431 for (auto e: bcframe) {
436 for (auto e: edges) {
441 if (!tmp->parent()) {
444 edges.push_back(new RRTEdge(tmp, tmp->parent()));
446 for (auto e: bcframe) {
452 for (auto &e: edges) {
453 #pragma omp parallel for reduction(|: col)
454 for (i = 0; i < (*this->cobstacles_).size(); i++) {
455 if ((*this->cobstacles_)[i].collide(e)) {
460 for (auto e: edges) {
465 #pragma omp parallel for reduction(|: col)
466 for (i = 0; i < (*this->sobstacles_).size(); i++) {
467 if ((*this->sobstacles_)[i].collide(e)) {
472 for (auto e: edges) {
478 for (auto e: edges) {
484 class RRTNodeDijkstra {
486 RRTNodeDijkstra(int i):
492 RRTNodeDijkstra(int i, float c):
498 RRTNodeDijkstra(int i, int p, float c):
517 class RRTNodeDijkstraComparator {
520 const RRTNodeDijkstra& n1,
521 const RRTNodeDijkstra& n2)
527 bool RRTBase::optp_dijkstra(
528 std::vector<RRTNode *> &cusps,
529 std::vector<int> &npi)
531 std::vector<RRTNodeDijkstra> dnodes;
532 for (unsigned int i = 0; i < cusps.size(); i++)
534 dnodes.push_back(RRTNodeDijkstra(
539 dnodes.push_back(RRTNodeDijkstra(
545 std::vector<RRTNodeDijkstra>,
546 RRTNodeDijkstraComparator> pq;
547 RRTNodeDijkstra tmp = dnodes[0];
549 float ch_cost = 9999;
550 std::vector<RRTNode *> steered;
551 while (!pq.empty()) {
554 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
555 ch_cost = dnodes[tmp.ni].c +
556 this->cost(cusps[tmp.ni], cusps[i]);
557 steered = this->steer(cusps[tmp.ni], cusps[i]);
558 for (unsigned int j = 0; j < steered.size() - 1; j++)
559 steered[j]->add_child(steered[j + 1], 1);
562 steered[steered.size() - 1])) {
563 for (auto n: steered)
567 if (ch_cost < dnodes[i].c) {
568 dnodes[i].c = ch_cost;
569 dnodes[i].pi = tmp.ni;
573 for (auto n: steered)
577 unsigned int tmpi = 0;
578 for (auto n: dnodes) {
579 if (n.v && n.ni > tmpi)
584 tmpi = dnodes[tmpi].pi;
587 std::reverse(npi.begin(), npi.end());
591 bool RRTBase::optp_rrp(
592 std::vector<RRTNode *> &cusps,
593 std::vector<int> &npi)
595 std::vector<RRTNode *> steered;
596 std::vector<int> candidates;
597 RRTNode *x_j = nullptr;
598 RRTNode *x_i = nullptr;
599 int j = cusps.size() - 1;
613 for (int i = 0; i < j; i++) {
614 steered = this->steer(cusps[i], x_j);
615 for (unsigned int k = 0; k < steered.size() - 1; k++)
616 steered[k]->add_child(steered[k + 1], 1);
619 steered[steered.size() - 1]))
620 candidates.push_back(i);
622 if (candidates.size() <= 0)
624 i_min = candidates[0];
627 for (auto c: candidates) {
629 dx = x_j->x() - x_i->x();
630 dy = x_j->y() - x_i->y();
631 ed = EDIST(x_i, x_j);
632 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
633 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
644 std::reverse(npi.begin(), npi.end());
648 bool RRTBase::optp_smart(
649 std::vector<RRTNode *> &cusps,
650 std::vector<int> &npi)
652 std::vector<RRTNode *> steered;
653 int li = cusps.size() - 1;
657 steered = this->steer(cusps[ai - 1], cusps[li]);
658 for (unsigned int j = 0; j < steered.size() - 1; j++)
659 steered[j]->add_child(steered[j + 1], 1);
660 if (this->collide(steered[0], steered[steered.size() - 1])) {
665 for (auto n: steered)
669 std::reverse(npi.begin(), npi.end());
673 bool RRTBase::opt_path()
675 if (this->tlog().size() == 0)
677 float oc = this->tlog().back().front()->ccost();
678 std::vector<RRTNode *> tmp_cusps;
679 for (auto n: this->tlog().back()) {
680 if (sgn(n->s()) == 0) {
681 tmp_cusps.push_back(n);
682 } else if (n->parent() &&
683 sgn(n->s()) != sgn(n->parent()->s())) {
684 tmp_cusps.push_back(n);
685 tmp_cusps.push_back(n->parent());
687 //tmp_cusps.push_back(n);
689 if (tmp_cusps.size() < 2)
691 std::vector<RRTNode *> cusps;
692 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
693 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
694 cusps.push_back(tmp_cusps[i]);
696 std::reverse(cusps.begin(), cusps.end());
697 std::vector<int> npi; // new path indexes
698 if (!this->optp_dijkstra(cusps, npi))
700 RRTNode *pn = cusps[npi[0]];
701 RRTNode *tmp = nullptr;
703 for (unsigned int i = 0; i < npi.size() - 1; i++) {
705 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
708 } else if (IS_NEAR(cusps[npi[i]], ns)) {
710 while (tmp && tmp != cusps[npi[i]]) {
716 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
718 cusps[npi[i + 1]]->parent()->rem_child(
722 this->cost(pn, cusps[npi[i + 1]]));
724 } else if (IS_NEAR(pn, ns)) {
727 this->nodes().push_back(ns);
729 pn->add_child(ns, this->cost(pn, ns));
734 this->root()->update_ccost();
735 if (this->tlog().back().front()->ccost() < oc)
740 bool RRTBase::rebase(RRTNode *nr)
742 if (!nr || this->goal_ == nr || this->root_ == nr)
744 std::vector<RRTNode *> s; // DFS stack
747 unsigned int to_del = 0;
749 s.push_back(this->root_);
750 while (s.size() > 0) {
753 for (auto ch: tmp->children()) {
757 to_del = this->nodes_.size();
758 #pragma omp parallel for reduction(min: to_del)
759 for (i = 0; i < this->nodes_.size(); i++) {
760 if (this->nodes_[i] == tmp)
763 if (to_del < this->nodes_.size())
764 this->nodes_.erase(this->nodes_.begin() + to_del);
766 to_del = this->iy_[iy].size();
767 #pragma omp parallel for reduction(min: to_del)
768 for (i = 0; i < this->iy_[iy].size(); i++) {
769 if (this->iy_[iy][i] == tmp)
772 if (to_del < this->iy_[iy].size())
773 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
774 this->dnodes().push_back(tmp);
777 this->root_->remove_parent();
781 std::vector<RRTNode *> RRTBase::findt()
783 return this->findt(this->goal_);
786 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
788 std::vector<RRTNode *> nodes;
789 if (!n || !n->parent())
799 RRTNode *RRTBase::sample()
804 float RRTBase::cost(RRTNode *init, RRTNode *goal)
806 return co2(init, goal);
809 RRTNode *RRTBase::nn(RRTNode *rs)
811 return nn4(this->iy_, rs, nullptr);
812 //return nn3(this->iy_, rs, nullptr);
815 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
817 std::vector<RRTNode *> nvs;
818 unsigned int iy = IYI(node->y());
819 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
820 unsigned int i = 0; // vector index
821 unsigned int j = 0; // array index
822 unsigned int jmin = 0; // minimal j index
823 unsigned int jmax = 0; // maximal j index
825 jmin = (jmin > 0) ? jmin : 0;
826 jmax = iy + iy_dist + 1;
827 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
828 #pragma omp parallel for reduction(merge: nvs)
829 for (j = jmin; j < jmax; j++) {
830 #pragma omp parallel for reduction(merge: nvs)
831 for (i = 0; i < this->iy_[j].size(); i++) {
832 if (this->cost(this->iy_[j][i], node) < dist) {
833 nvs.push_back(this->iy_[j][i]);
840 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
842 return st3(init, goal);
845 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
847 return st3(init, goal, step);