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_.reserve(20000);
64 this->nodes_.push_back(this->root_);
65 this->add_iy(this->root_);
68 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
72 this->nodes_.reserve(20000);
73 this->nodes_.push_back(init);
78 RRTNode *RRTBase::root()
83 RRTNode *RRTBase::goal()
88 std::vector<RRTNode *> &RRTBase::nodes()
93 std::vector<RRTNode *> &RRTBase::dnodes()
98 std::vector<RRTNode *> &RRTBase::samples()
100 return this->samples_;
103 std::vector<CircleObstacle> *RRTBase::co()
105 return this->cobstacles_;
108 std::vector<SegmentObstacle> *RRTBase::so()
110 return this->sobstacles_;
113 std::vector<float> &RRTBase::clog()
118 std::vector<float> &RRTBase::nlog()
123 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
128 std::vector<float> &RRTBase::slog()
133 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
138 bool RRTBase::goal_found()
140 return this->goal_found_;
143 float RRTBase::elapsed()
145 std::chrono::duration<float> dt;
146 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
147 this->tend_ - this->tstart_);
152 void RRTBase::root(RRTNode *node)
157 void RRTBase::goal(RRTNode *node)
162 bool RRTBase::logr(RRTNode *root)
164 std::vector<RRTEdge *> e; // Edges to log
165 std::vector<RRTNode *> s; // DFS stack
166 std::vector<RRTNode *> r; // reset visited_
169 while (s.size() > 0) {
174 for (auto ch: tmp->children()) {
176 e.push_back(new RRTEdge(tmp, ch));
182 this->rlog_.push_back(e);
186 float RRTBase::ocost(RRTNode *n)
189 for (auto o: *this->cobstacles_)
190 if (o.dist_to(n) < dist)
192 for (auto o: *this->sobstacles_)
193 if (o.dist_to(n) < dist)
195 return n->ocost(dist);
198 bool RRTBase::tlog(std::vector<RRTNode *> t)
201 this->slog_.push_back(this->elapsed());
202 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
203 this->nlog_.push_back(this->nodes_.size());
204 this->tlog_.push_back(t);
211 void RRTBase::tstart()
213 this->tstart_ = std::chrono::high_resolution_clock::now();
218 this->tend_ = std::chrono::high_resolution_clock::now();
221 bool RRTBase::link_obstacles(
222 std::vector<CircleObstacle> *cobstacles,
223 std::vector<SegmentObstacle> *sobstacles)
225 this->cobstacles_ = cobstacles;
226 this->sobstacles_ = sobstacles;
227 if (!this->cobstacles_ || !this->sobstacles_) {
233 bool RRTBase::add_iy(RRTNode *n)
240 this->iy_[i].push_back(n);
244 bool RRTBase::goal_found(bool f)
246 this->goal_found_ = f;
251 bool RRTBase::glplot()
253 glClear(GL_COLOR_BUFFER_BIT);
258 for (auto o: *this->sobstacles_) {
260 glVertex2f(GLVERTEX(o.init()));
261 glVertex2f(GLVERTEX(o.goal()));
268 glVertex2f(GLVERTEX(this->root_));
269 glVertex2f(GLVERTEX(this->goal_));
272 if (this->samples_.size() > 0) {
276 glVertex2f(GLVERTEX(this->samples_.back()));
280 std::vector<RRTNode *> s; // DFS stack
281 std::vector<RRTNode *> r; // reset visited_
284 s.push_back(this->root_);
285 while (s.size() > 0) {
290 for (auto ch: tmp->children()) {
292 glColor3f(0.5, 0.5, 0.5);
293 glVertex2f(GLVERTEX(tmp));
294 glVertex2f(GLVERTEX(ch));
299 // Plot nodes (from goal)
301 s.push_back(this->goal_);
302 while (s.size() > 0) {
307 for (auto ch: tmp->children()) {
309 glColor3f(0.5, 0.5, 0.5);
310 glVertex2f(GLVERTEX(tmp));
311 glVertex2f(GLVERTEX(ch));
316 std::vector<RRTNode *> cusps;
317 // Plot last trajectory
318 if (this->tlog().size() > 0) {
321 for (auto n: this->tlog().back()) {
324 glVertex2f(GLVERTEX(n));
325 glVertex2f(GLVERTEX(n->parent()));
326 if (sgn(n->s()) != sgn(n->parent()->s()))
335 for (auto n: cusps) {
337 glVertex2f(GLVERTEX(n));
340 SDL_GL_SwapWindow(gw);
346 bool RRTBase::goal_found(
348 float (*cost)(RRTNode *, RRTNode* ))
350 if (IS_NEAR(node, this->goal_)) {
351 if (this->goal_found_) {
352 if (node->ccost() + this->cost(node, this->goal_) <
353 this->goal_->ccost()) {
354 RRTNode *op; // old parent
355 float oc; // old cumulative cost
356 float od; // old direct cost
357 op = this->goal_->parent();
358 oc = this->goal_->ccost();
359 od = this->goal_->dcost();
360 node->add_child(this->goal_,
361 this->cost(node, this->goal_));
362 if (this->collide(node, this->goal_)) {
363 node->children().pop_back();
364 this->goal_->parent(op);
365 this->goal_->ccost(oc);
366 this->goal_->dcost(od);
368 op->rem_child(this->goal_);
377 this->cost(node, this->goal_));
378 if (this->collide(node, this->goal_)) {
379 node->children().pop_back();
380 this->goal_->remove_parent();
383 this->goal_found_ = true;
390 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
392 std::vector<RRTEdge *> edges;
394 volatile bool col = false;
396 while (tmp != init) {
397 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
398 std::vector<RRTEdge *> bcframe = bc.frame();
399 #pragma omp parallel for reduction(|: col)
400 for (i = 0; i < (*this->cobstacles_).size(); i++) {
401 if ((*this->cobstacles_)[i].collide(tmp)) {
404 for (auto &e: bcframe) {
405 if ((*this->cobstacles_)[i].collide(e)) {
411 for (auto e: bcframe) {
416 for (auto e: edges) {
421 #pragma omp parallel for reduction(|: col)
422 for (i = 0; i < (*this->sobstacles_).size(); i++) {
423 for (auto &e: bcframe) {
424 if ((*this->sobstacles_)[i].collide(e)) {
430 for (auto e: bcframe) {
435 for (auto e: edges) {
440 if (!tmp->parent()) {
443 edges.push_back(new RRTEdge(tmp, tmp->parent()));
445 for (auto e: bcframe) {
451 for (auto &e: edges) {
452 #pragma omp parallel for reduction(|: col)
453 for (i = 0; i < (*this->cobstacles_).size(); i++) {
454 if ((*this->cobstacles_)[i].collide(e)) {
459 for (auto e: edges) {
464 #pragma omp parallel for reduction(|: col)
465 for (i = 0; i < (*this->sobstacles_).size(); i++) {
466 if ((*this->sobstacles_)[i].collide(e)) {
471 for (auto e: edges) {
477 for (auto e: edges) {
483 class RRTNodeDijkstra {
485 RRTNodeDijkstra(int i):
491 RRTNodeDijkstra(int i, float c):
497 RRTNodeDijkstra(int i, int p, float c):
516 class RRTNodeDijkstraComparator {
519 const RRTNodeDijkstra& n1,
520 const RRTNodeDijkstra& n2)
526 bool RRTBase::optp_dijkstra(
527 std::vector<RRTNode *> &cusps,
528 std::vector<int> &npi)
530 std::vector<RRTNodeDijkstra> dnodes;
531 for (unsigned int i = 0; i < cusps.size(); i++)
533 dnodes.push_back(RRTNodeDijkstra(
538 dnodes.push_back(RRTNodeDijkstra(
544 std::vector<RRTNodeDijkstra>,
545 RRTNodeDijkstraComparator> pq;
546 RRTNodeDijkstra tmp = dnodes[0];
548 float ch_cost = 9999;
549 std::vector<RRTNode *> steered;
550 while (!pq.empty()) {
553 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
554 ch_cost = dnodes[tmp.ni].c +
555 this->cost(cusps[tmp.ni], cusps[i]);
556 steered = this->steer(cusps[tmp.ni], cusps[i]);
557 for (unsigned int j = 0; j < steered.size() - 1; j++)
558 steered[j]->add_child(steered[j + 1], 1);
561 steered[steered.size() - 1])) {
562 for (auto n: steered)
566 if (ch_cost < dnodes[i].c) {
567 dnodes[i].c = ch_cost;
568 dnodes[i].pi = tmp.ni;
572 for (auto n: steered)
576 unsigned int tmpi = 0;
577 for (auto n: dnodes) {
578 if (n.v && n.ni > tmpi)
583 tmpi = dnodes[tmpi].pi;
586 std::reverse(npi.begin(), npi.end());
590 bool RRTBase::optp_rrp(
591 std::vector<RRTNode *> &cusps,
592 std::vector<int> &npi)
594 std::vector<RRTNode *> steered;
595 std::vector<int> candidates;
596 RRTNode *x_j = nullptr;
597 RRTNode *x_i = nullptr;
598 int j = cusps.size() - 1;
612 for (int i = 0; i < j; i++) {
613 steered = this->steer(cusps[i], x_j);
614 for (unsigned int k = 0; k < steered.size() - 1; k++)
615 steered[k]->add_child(steered[k + 1], 1);
618 steered[steered.size() - 1]))
619 candidates.push_back(i);
621 if (candidates.size() <= 0)
623 i_min = candidates[0];
626 for (auto c: candidates) {
628 dx = x_j->x() - x_i->x();
629 dy = x_j->y() - x_i->y();
630 ed = EDIST(x_i, x_j);
631 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
632 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
643 std::reverse(npi.begin(), npi.end());
647 bool RRTBase::optp_smart(
648 std::vector<RRTNode *> &cusps,
649 std::vector<int> &npi)
651 std::vector<RRTNode *> steered;
652 int li = cusps.size() - 1;
656 steered = this->steer(cusps[ai - 1], cusps[li]);
657 for (unsigned int j = 0; j < steered.size() - 1; j++)
658 steered[j]->add_child(steered[j + 1], 1);
659 if (this->collide(steered[0], steered[steered.size() - 1])) {
664 for (auto n: steered)
668 std::reverse(npi.begin(), npi.end());
672 bool RRTBase::opt_path()
674 if (this->tlog().size() == 0)
676 float oc = this->tlog().back().front()->ccost();
677 std::vector<RRTNode *> tmp_cusps;
678 for (auto n: this->tlog().back()) {
679 if (sgn(n->s()) == 0) {
680 tmp_cusps.push_back(n);
681 } else if (n->parent() &&
682 sgn(n->s()) != sgn(n->parent()->s())) {
683 tmp_cusps.push_back(n);
684 tmp_cusps.push_back(n->parent());
686 //tmp_cusps.push_back(n);
688 if (tmp_cusps.size() < 2)
690 std::vector<RRTNode *> cusps;
691 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
692 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
693 cusps.push_back(tmp_cusps[i]);
695 std::reverse(cusps.begin(), cusps.end());
696 std::vector<int> npi; // new path indexes
697 if (!this->optp_dijkstra(cusps, npi))
699 RRTNode *pn = cusps[npi[0]];
700 RRTNode *tmp = nullptr;
702 for (unsigned int i = 0; i < npi.size() - 1; i++) {
704 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
707 } else if (IS_NEAR(cusps[npi[i]], ns)) {
709 while (tmp && tmp != cusps[npi[i]]) {
715 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
717 cusps[npi[i + 1]]->parent()->rem_child(
721 this->cost(pn, cusps[npi[i + 1]]));
723 } else if (IS_NEAR(pn, ns)) {
726 this->nodes().push_back(ns);
728 pn->add_child(ns, this->cost(pn, ns));
733 this->root()->update_ccost();
734 if (this->tlog().back().front()->ccost() < oc)
739 bool RRTBase::rebase(RRTNode *nr)
741 if (!nr || this->goal_ == nr || this->root_ == nr)
743 std::vector<RRTNode *> s; // DFS stack
746 unsigned int to_del = 0;
748 s.push_back(this->root_);
749 while (s.size() > 0) {
752 for (auto ch: tmp->children()) {
756 to_del = this->nodes_.size();
757 #pragma omp parallel for reduction(min: to_del)
758 for (i = 0; i < this->nodes_.size(); i++) {
759 if (this->nodes_[i] == tmp)
762 if (to_del < this->nodes_.size())
763 this->nodes_.erase(this->nodes_.begin() + to_del);
765 to_del = this->iy_[iy].size();
766 #pragma omp parallel for reduction(min: to_del)
767 for (i = 0; i < this->iy_[iy].size(); i++) {
768 if (this->iy_[iy][i] == tmp)
771 if (to_del < this->iy_[iy].size())
772 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
773 this->dnodes().push_back(tmp);
776 this->root_->remove_parent();
780 std::vector<RRTNode *> RRTBase::findt()
782 return this->findt(this->goal_);
785 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
787 std::vector<RRTNode *> nodes;
788 if (!n || !n->parent())
798 RRTNode *RRTBase::sample()
803 float RRTBase::cost(RRTNode *init, RRTNode *goal)
805 return co2(init, goal);
808 RRTNode *RRTBase::nn(RRTNode *rs)
810 return nn4(this->iy_, rs, nullptr);
811 //return nn3(this->iy_, rs, nullptr);
814 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
816 std::vector<RRTNode *> nvs;
817 unsigned int iy = IYI(node->y());
818 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
819 unsigned int i = 0; // vector index
820 unsigned int j = 0; // array index
821 unsigned int jmin = 0; // minimal j index
822 unsigned int jmax = 0; // maximal j index
824 jmin = (jmin > 0) ? jmin : 0;
825 jmax = iy + iy_dist + 1;
826 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
827 #pragma omp parallel for reduction(merge: nvs)
828 for (j = jmin; j < jmax; j++) {
829 #pragma omp parallel for reduction(merge: nvs)
830 for (i = 0; i < this->iy_[j].size(); i++) {
831 if (this->cost(this->iy_[j][i], node) < dist) {
832 nvs.push_back(this->iy_[j][i]);
839 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
841 return st3(init, goal);
844 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
846 return st3(init, goal, step);