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 float xx = pow(node->x() - this->goal_->x(), 2);
351 float yy = pow(node->y() - this->goal_->y(), 2);
352 float dh = std::abs(node->h() - this->goal_->h());
353 if (IS_NEAR(node, this->goal_)) {
354 if (this->goal_found_) {
355 if (node->ccost() + (*cost)(node, this->goal_) <
356 this->goal_->ccost()) {
357 RRTNode *op; // old parent
358 float oc; // old cumulative cost
359 float od; // old direct cost
360 op = this->goal_->parent();
361 oc = this->goal_->ccost();
362 od = this->goal_->dcost();
363 node->add_child(this->goal_,
364 (*cost)(node, this->goal_));
365 if (this->collide(node, this->goal_)) {
366 node->children().pop_back();
367 this->goal_->parent(op);
368 this->goal_->ccost(oc);
369 this->goal_->dcost(od);
371 op->rem_child(this->goal_);
380 (*cost)(node, this->goal_));
381 if (this->collide(node, this->goal_)) {
382 node->children().pop_back();
383 this->goal_->remove_parent();
386 this->goal_found_ = true;
393 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
395 std::vector<RRTEdge *> edges;
397 volatile bool col = false;
399 while (tmp != init) {
400 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
401 std::vector<RRTEdge *> bcframe = bc.frame();
402 #pragma omp parallel for reduction(|: col)
403 for (i = 0; i < (*this->cobstacles_).size(); i++) {
404 if ((*this->cobstacles_)[i].collide(tmp)) {
407 for (auto &e: bcframe) {
408 if ((*this->cobstacles_)[i].collide(e)) {
414 for (auto e: bcframe) {
419 for (auto e: edges) {
424 #pragma omp parallel for reduction(|: col)
425 for (i = 0; i < (*this->sobstacles_).size(); i++) {
426 for (auto &e: bcframe) {
427 if ((*this->sobstacles_)[i].collide(e)) {
433 for (auto e: bcframe) {
438 for (auto e: edges) {
443 if (!tmp->parent()) {
446 edges.push_back(new RRTEdge(tmp, tmp->parent()));
448 for (auto e: bcframe) {
454 for (auto &e: edges) {
455 #pragma omp parallel for reduction(|: col)
456 for (i = 0; i < (*this->cobstacles_).size(); i++) {
457 if ((*this->cobstacles_)[i].collide(e)) {
462 for (auto e: edges) {
467 #pragma omp parallel for reduction(|: col)
468 for (i = 0; i < (*this->sobstacles_).size(); i++) {
469 if ((*this->sobstacles_)[i].collide(e)) {
474 for (auto e: edges) {
480 for (auto e: edges) {
486 class RRTNodeDijkstra {
488 RRTNodeDijkstra(int i):
494 RRTNodeDijkstra(int i, float c):
500 RRTNodeDijkstra(int i, int p, float c):
519 class RRTNodeDijkstraComparator {
522 const RRTNodeDijkstra& n1,
523 const RRTNodeDijkstra& n2)
529 bool RRTBase::optp_dijkstra(
530 std::vector<RRTNode *> &cusps,
531 std::vector<int> &npi)
533 std::vector<RRTNodeDijkstra> dnodes;
534 for (unsigned int i = 0; i < cusps.size(); i++)
536 dnodes.push_back(RRTNodeDijkstra(
541 dnodes.push_back(RRTNodeDijkstra(
547 std::vector<RRTNodeDijkstra>,
548 RRTNodeDijkstraComparator> pq;
549 RRTNodeDijkstra tmp = dnodes[0];
551 float ch_cost = 9999;
552 std::vector<RRTNode *> steered;
553 while (!pq.empty()) {
556 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
557 ch_cost = dnodes[tmp.ni].c +
558 this->cost(cusps[tmp.ni], cusps[i]);
559 steered = this->steer(cusps[tmp.ni], cusps[i]);
560 for (unsigned int j = 0; j < steered.size() - 1; j++)
561 steered[j]->add_child(steered[j + 1], 1);
564 steered[steered.size() - 1])) {
565 for (auto n: steered)
569 if (ch_cost < dnodes[i].c) {
570 dnodes[i].c = ch_cost;
571 dnodes[i].pi = tmp.ni;
575 for (auto n: steered)
579 unsigned int tmpi = 0;
580 for (auto n: dnodes) {
581 if (n.v && n.ni > tmpi)
586 tmpi = dnodes[tmpi].pi;
589 std::reverse(npi.begin(), npi.end());
593 bool RRTBase::optp_rrp(
594 std::vector<RRTNode *> &cusps,
595 std::vector<int> &npi)
597 std::vector<RRTNode *> steered;
598 std::vector<int> candidates;
599 RRTNode *x_j = nullptr;
600 RRTNode *x_i = nullptr;
601 int j = cusps.size() - 1;
615 for (int i = 0; i < j; i++) {
616 steered = this->steer(cusps[i], x_j);
617 for (unsigned int k = 0; k < steered.size() - 1; k++)
618 steered[k]->add_child(steered[k + 1], 1);
621 steered[steered.size() - 1]))
622 candidates.push_back(i);
624 if (candidates.size() <= 0)
626 i_min = candidates[0];
629 for (auto c: candidates) {
631 dx = x_j->x() - x_i->x();
632 dy = x_j->y() - x_i->y();
633 ed = EDIST(x_i, x_j);
634 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
635 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
646 std::reverse(npi.begin(), npi.end());
650 bool RRTBase::optp_smart(
651 std::vector<RRTNode *> &cusps,
652 std::vector<int> &npi)
654 std::vector<RRTNode *> steered;
655 int li = cusps.size() - 1;
659 steered = this->steer(cusps[ai - 1], cusps[li]);
660 for (unsigned int j = 0; j < steered.size() - 1; j++)
661 steered[j]->add_child(steered[j + 1], 1);
662 if (this->collide(steered[0], steered[steered.size() - 1])) {
667 for (auto n: steered)
671 std::reverse(npi.begin(), npi.end());
675 bool RRTBase::opt_path()
677 if (this->tlog().size() == 0)
679 float oc = this->tlog().back().front()->ccost();
680 std::vector<RRTNode *> tmp_cusps;
681 for (auto n: this->tlog().back()) {
682 if (sgn(n->s()) == 0) {
683 tmp_cusps.push_back(n);
684 } else if (n->parent() &&
685 sgn(n->s()) != sgn(n->parent()->s())) {
686 tmp_cusps.push_back(n);
687 tmp_cusps.push_back(n->parent());
689 //tmp_cusps.push_back(n);
691 if (tmp_cusps.size() < 2)
693 std::vector<RRTNode *> cusps;
694 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
695 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
696 cusps.push_back(tmp_cusps[i]);
698 std::reverse(cusps.begin(), cusps.end());
699 std::vector<int> npi; // new path indexes
700 if (!this->optp_dijkstra(cusps, npi))
702 RRTNode *pn = cusps[npi[0]];
703 RRTNode *tmp = nullptr;
705 for (unsigned int i = 0; i < npi.size() - 1; i++) {
707 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
710 } else if (IS_NEAR(cusps[npi[i]], ns)) {
712 while (tmp && tmp != cusps[npi[i]]) {
718 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
720 cusps[npi[i + 1]]->parent()->rem_child(
724 this->cost(pn, cusps[npi[i + 1]]));
726 } else if (IS_NEAR(pn, ns)) {
729 this->nodes().push_back(ns);
731 pn->add_child(ns, this->cost(pn, ns));
736 this->root()->update_ccost();
737 if (this->tlog().back().front()->ccost() < oc)
742 bool RRTBase::rebase(RRTNode *nr)
744 if (!nr || this->goal_ == nr || this->root_ == nr)
746 std::vector<RRTNode *> s; // DFS stack
749 unsigned int to_del = 0;
751 s.push_back(this->root_);
752 while (s.size() > 0) {
755 for (auto ch: tmp->children()) {
759 to_del = this->nodes_.size();
760 #pragma omp parallel for reduction(min: to_del)
761 for (i = 0; i < this->nodes_.size(); i++) {
762 if (this->nodes_[i] == tmp)
765 if (to_del < this->nodes_.size())
766 this->nodes_.erase(this->nodes_.begin() + to_del);
768 to_del = this->iy_[iy].size();
769 #pragma omp parallel for reduction(min: to_del)
770 for (i = 0; i < this->iy_[iy].size(); i++) {
771 if (this->iy_[iy][i] == tmp)
774 if (to_del < this->iy_[iy].size())
775 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
776 this->dnodes().push_back(tmp);
779 this->root_->remove_parent();
783 std::vector<RRTNode *> RRTBase::findt()
785 return this->findt(this->goal_);
788 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
790 std::vector<RRTNode *> nodes;
791 if (!n || !n->parent())
801 RRTNode *RRTBase::sample()
806 float RRTBase::cost(RRTNode *init, RRTNode *goal)
808 return co2(init, goal);
811 RRTNode *RRTBase::nn(RRTNode *rs)
813 return nn4(this->iy_, rs, nullptr);
814 //return nn3(this->iy_, rs, nullptr);
817 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
819 std::vector<RRTNode *> nvs;
820 unsigned int iy = IYI(node->y());
821 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
822 unsigned int i = 0; // vector index
823 unsigned int j = 0; // array index
824 unsigned int jmin = 0; // minimal j index
825 unsigned int jmax = 0; // maximal j index
827 jmin = (jmin > 0) ? jmin : 0;
828 jmax = iy + iy_dist + 1;
829 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
830 #pragma omp parallel for reduction(merge: nvs)
831 for (j = jmin; j < jmax; j++) {
832 #pragma omp parallel for reduction(merge: nvs)
833 for (i = 0; i < this->iy_[j].size(); i++) {
834 if (this->cost(this->iy_[j][i], node) < dist) {
835 nvs.push_back(this->iy_[j][i]);
842 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
844 return st3(init, goal);
847 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
849 return st3(init, goal, step);