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_)
73 for (auto n: this->dnodes_)
74 if (n != this->root_ && n != this->goal_)
76 for (auto s: this->samples_)
79 for (auto edges: this->rlog_)
90 this->nodes_.reserve(NOFNODES);
91 this->nodes_.push_back(this->root_);
92 this->add_iy(this->root_);
93 this->add_ixy(this->root_);
96 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
100 this->nodes_.reserve(NOFNODES);
101 this->nodes_.push_back(init);
107 RRTNode *RRTBase::root()
112 RRTNode *RRTBase::goal()
117 std::vector<RRTNode *> &RRTBase::nodes()
122 std::vector<RRTNode *> &RRTBase::dnodes()
124 return this->dnodes_;
127 PolygonObstacle &RRTBase::frame()
132 std::vector<RRTNode *> &RRTBase::samples()
134 return this->samples_;
137 std::vector<CircleObstacle> *RRTBase::co()
139 return this->cobstacles_;
142 std::vector<SegmentObstacle> *RRTBase::so()
144 return this->sobstacles_;
147 std::vector<float> &RRTBase::clog()
152 std::vector<float> &RRTBase::nlog()
157 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
162 std::vector<float> &RRTBase::slog()
167 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
172 std::vector<RRTNode *> &RRTBase::slot_cusp()
174 return this->slot_cusp_;
177 bool RRTBase::goal_found()
179 return this->goal_found_;
182 float RRTBase::elapsed()
184 std::chrono::duration<float> dt;
185 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
186 this->tend_ - this->tstart_);
191 void RRTBase::root(RRTNode *node)
196 void RRTBase::goal(RRTNode *node)
201 bool RRTBase::logr(RRTNode *root)
203 std::vector<RRTEdge *> e; // Edges to log
204 std::vector<RRTNode *> s; // DFS stack
205 std::vector<RRTNode *> r; // reset visited_
208 while (s.size() > 0) {
213 for (auto ch: tmp->children()) {
215 e.push_back(new RRTEdge(tmp, ch));
221 this->rlog_.push_back(e);
225 float RRTBase::ocost(RRTNode *n)
228 for (auto o: *this->cobstacles_)
229 if (o.dist_to(n) < dist)
231 for (auto o: *this->sobstacles_)
232 if (o.dist_to(n) < dist)
234 return n->ocost(dist);
237 bool RRTBase::tlog(std::vector<RRTNode *> t)
240 this->slog_.push_back(this->elapsed());
241 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
242 this->nlog_.push_back(this->nodes_.size());
243 this->tlog_.push_back(t);
250 void RRTBase::tstart()
252 this->tstart_ = std::chrono::high_resolution_clock::now();
257 this->tend_ = std::chrono::high_resolution_clock::now();
260 bool RRTBase::link_obstacles(
261 std::vector<CircleObstacle> *cobstacles,
262 std::vector<SegmentObstacle> *sobstacles)
264 this->cobstacles_ = cobstacles;
265 this->sobstacles_ = sobstacles;
266 if (!this->cobstacles_ || !this->sobstacles_) {
272 bool RRTBase::add_iy(RRTNode *n)
279 this->iy_[i].push_back(n);
283 bool RRTBase::add_ixy(RRTNode *n)
285 int ix = IXI(n->x());
290 int iy = IYI(n->y());
295 this->ixy_[ix][iy].add_node(n);
299 bool RRTBase::goal_found(bool f)
301 this->goal_found_ = f;
305 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
307 for (unsigned int i = 0; i < sc.size() - 1; i++)
308 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
309 sc[0]->parent(this->goal());
310 this->slot_cusp_ = sc;
314 bool RRTBase::glplot()
316 glClear(GL_COLOR_BUFFER_BIT);
321 for (auto o: *this->sobstacles_) {
323 glVertex2f(GLVERTEX(o.init()));
324 glVertex2f(GLVERTEX(o.goal()));
331 glVertex2f(GLVERTEX(this->root_));
332 glVertex2f(GLVERTEX(this->goal_));
335 if (this->samples_.size() > 0) {
339 glVertex2f(GLVERTEX(this->samples_.back()));
343 std::vector<RRTNode *> s; // DFS stack
344 std::vector<RRTNode *> r; // reset visited_
347 s.push_back(this->root_);
348 while (s.size() > 0) {
353 for (auto ch: tmp->children()) {
355 glColor3f(0.5, 0.5, 0.5);
356 glVertex2f(GLVERTEX(tmp));
357 glVertex2f(GLVERTEX(ch));
362 // Plot nodes (from goal)
364 s.push_back(this->goal_);
365 while (s.size() > 0) {
370 for (auto ch: tmp->children()) {
372 glColor3f(0.5, 0.5, 0.5);
373 glVertex2f(GLVERTEX(tmp));
374 glVertex2f(GLVERTEX(ch));
379 std::vector<RRTNode *> cusps;
380 // Plot last trajectory
381 if (this->tlog().size() > 0) {
384 for (auto n: this->tlog().back()) {
387 glVertex2f(GLVERTEX(n));
388 glVertex2f(GLVERTEX(n->parent()));
389 if (sgn(n->s()) != sgn(n->parent()->s()))
398 for (auto n: cusps) {
400 glVertex2f(GLVERTEX(n));
403 SDL_GL_SwapWindow(gw);
409 bool RRTBase::goal_found(
411 float (*cost)(RRTNode *, RRTNode* ))
413 if (IS_NEAR(node, this->goal_)) {
414 if (this->goal_found_) {
415 if (node->ccost() + this->cost(node, this->goal_) <
416 this->goal_->ccost()) {
417 RRTNode *op; // old parent
418 float oc; // old cumulative cost
419 float od; // old direct cost
420 op = this->goal_->parent();
421 oc = this->goal_->ccost();
422 od = this->goal_->dcost();
423 node->add_child(this->goal_,
424 this->cost(node, this->goal_));
425 if (this->collide(node, this->goal_)) {
426 node->children().pop_back();
427 this->goal_->parent(op);
428 this->goal_->ccost(oc);
429 this->goal_->dcost(od);
431 op->rem_child(this->goal_);
440 this->cost(node, this->goal_));
441 if (this->collide(node, this->goal_)) {
442 node->children().pop_back();
443 this->goal_->remove_parent();
446 this->goal_found_ = true;
453 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
455 std::vector<RRTEdge *> edges;
457 volatile bool col = false;
459 while (tmp != init) {
460 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
461 std::vector<RRTEdge *> bcframe = bc.frame();
462 #pragma omp parallel for reduction(|: col)
463 for (i = 0; i < (*this->cobstacles_).size(); i++) {
464 if ((*this->cobstacles_)[i].collide(tmp)) {
467 for (auto &e: bcframe) {
468 if ((*this->cobstacles_)[i].collide(e)) {
474 for (auto e: bcframe) {
479 for (auto e: edges) {
484 #pragma omp parallel for reduction(|: col)
485 for (i = 0; i < (*this->sobstacles_).size(); i++) {
486 for (auto &e: bcframe) {
487 if ((*this->sobstacles_)[i].collide(e)) {
493 for (auto e: bcframe) {
498 for (auto e: edges) {
503 if (!tmp->parent()) {
506 edges.push_back(new RRTEdge(tmp, tmp->parent()));
508 for (auto e: bcframe) {
514 for (auto &e: edges) {
515 #pragma omp parallel for reduction(|: col)
516 for (i = 0; i < (*this->cobstacles_).size(); i++) {
517 if ((*this->cobstacles_)[i].collide(e)) {
522 for (auto e: edges) {
527 #pragma omp parallel for reduction(|: col)
528 for (i = 0; i < (*this->sobstacles_).size(); i++) {
529 if ((*this->sobstacles_)[i].collide(e)) {
534 for (auto e: edges) {
540 for (auto e: edges) {
546 class RRTNodeDijkstra {
548 RRTNodeDijkstra(int i):
554 RRTNodeDijkstra(int i, float c):
560 RRTNodeDijkstra(int i, int p, float c):
579 class RRTNodeDijkstraComparator {
582 const RRTNodeDijkstra& n1,
583 const RRTNodeDijkstra& n2)
589 bool RRTBase::optp_dijkstra(
590 std::vector<RRTNode *> &cusps,
591 std::vector<int> &npi)
593 std::vector<RRTNodeDijkstra> dnodes;
594 for (unsigned int i = 0; i < cusps.size(); i++)
596 dnodes.push_back(RRTNodeDijkstra(
601 dnodes.push_back(RRTNodeDijkstra(
607 std::vector<RRTNodeDijkstra>,
608 RRTNodeDijkstraComparator> pq;
609 RRTNodeDijkstra tmp = dnodes[0];
611 float ch_cost = 9999;
612 std::vector<RRTNode *> steered;
613 while (!pq.empty()) {
616 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
617 ch_cost = dnodes[tmp.ni].c +
618 this->cost(cusps[tmp.ni], cusps[i]);
619 steered = this->steer(cusps[tmp.ni], cusps[i]);
620 for (unsigned int j = 0; j < steered.size() - 1; j++)
621 steered[j]->add_child(steered[j + 1], 1);
624 steered[steered.size() - 1])) {
625 for (auto n: steered)
629 if (ch_cost < dnodes[i].c) {
630 dnodes[i].c = ch_cost;
631 dnodes[i].pi = tmp.ni;
635 for (auto n: steered)
639 unsigned int tmpi = 0;
640 for (auto n: dnodes) {
641 if (n.v && n.ni > tmpi)
646 tmpi = dnodes[tmpi].pi;
649 std::reverse(npi.begin(), npi.end());
653 bool RRTBase::optp_rrp(
654 std::vector<RRTNode *> &cusps,
655 std::vector<int> &npi)
657 std::vector<RRTNode *> steered;
658 std::vector<int> candidates;
659 RRTNode *x_j = nullptr;
660 RRTNode *x_i = nullptr;
661 int j = cusps.size() - 1;
675 for (int i = 0; i < j; i++) {
676 steered = this->steer(cusps[i], x_j);
677 for (unsigned int k = 0; k < steered.size() - 1; k++)
678 steered[k]->add_child(steered[k + 1], 1);
681 steered[steered.size() - 1]))
682 candidates.push_back(i);
684 if (candidates.size() <= 0)
686 i_min = candidates[0];
689 for (auto c: candidates) {
691 dx = x_j->x() - x_i->x();
692 dy = x_j->y() - x_i->y();
693 ed = EDIST(x_i, x_j);
694 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
695 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
706 std::reverse(npi.begin(), npi.end());
710 bool RRTBase::optp_smart(
711 std::vector<RRTNode *> &cusps,
712 std::vector<int> &npi)
714 std::vector<RRTNode *> steered;
715 int li = cusps.size() - 1;
719 steered = this->steer(cusps[ai - 1], cusps[li]);
720 for (unsigned int j = 0; j < steered.size() - 1; j++)
721 steered[j]->add_child(steered[j + 1], 1);
722 if (this->collide(steered[0], steered[steered.size() - 1])) {
727 for (auto n: steered)
731 std::reverse(npi.begin(), npi.end());
735 bool RRTBase::opt_path()
737 if (this->tlog().size() == 0)
739 float oc = this->tlog().back().front()->ccost();
740 std::vector<RRTNode *> tmp_cusps;
741 for (auto n: this->tlog().back()) {
742 if (sgn(n->s()) == 0) {
743 tmp_cusps.push_back(n);
744 } else if (n->parent() &&
745 sgn(n->s()) != sgn(n->parent()->s())) {
746 tmp_cusps.push_back(n);
747 tmp_cusps.push_back(n->parent());
749 //tmp_cusps.push_back(n);
751 if (tmp_cusps.size() < 2)
753 std::vector<RRTNode *> cusps;
754 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
755 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
756 cusps.push_back(tmp_cusps[i]);
758 std::reverse(cusps.begin(), cusps.end());
759 std::vector<int> npi; // new path indexes
760 if (!this->optp_dijkstra(cusps, npi))
762 RRTNode *pn = cusps[npi[0]];
763 RRTNode *tmp = nullptr;
765 for (unsigned int i = 0; i < npi.size() - 1; i++) {
767 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
770 } else if (IS_NEAR(cusps[npi[i]], ns)) {
772 while (tmp && tmp != cusps[npi[i]]) {
778 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
780 cusps[npi[i + 1]]->parent()->rem_child(
784 this->cost(pn, cusps[npi[i + 1]]));
786 } else if (IS_NEAR(pn, ns)) {
789 this->nodes().push_back(ns);
792 pn->add_child(ns, this->cost(pn, ns));
797 this->root()->update_ccost();
798 if (this->tlog().back().front()->ccost() < oc)
803 bool RRTBase::rebase(RRTNode *nr)
805 if (!nr || this->goal_ == nr || this->root_ == nr)
807 std::vector<RRTNode *> s; // DFS stack
810 unsigned int to_del = 0;
812 s.push_back(this->root_);
813 while (s.size() > 0) {
816 for (auto ch: tmp->children()) {
820 to_del = this->nodes_.size();
821 #pragma omp parallel for reduction(min: to_del)
822 for (i = 0; i < this->nodes_.size(); i++) {
823 if (this->nodes_[i] == tmp)
826 if (to_del < this->nodes_.size())
827 this->nodes_.erase(this->nodes_.begin() + to_del);
829 to_del = this->iy_[iy].size();
830 #pragma omp parallel for reduction(min: to_del)
831 for (i = 0; i < this->iy_[iy].size(); i++) {
832 if (this->iy_[iy][i] == tmp)
835 if (to_del < this->iy_[iy].size())
836 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
837 this->dnodes().push_back(tmp);
840 this->root_->remove_parent();
844 std::vector<RRTNode *> RRTBase::findt()
846 return this->findt(this->goal_);
849 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
851 std::vector<RRTNode *> nodes;
852 if (!n || !n->parent())
862 RRTNode *RRTBase::sample()
867 float RRTBase::cost(RRTNode *init, RRTNode *goal)
869 return co2(init, goal);
872 RRTNode *RRTBase::nn(RRTNode *rs)
874 return nn4(this->iy_, rs, nullptr);
875 //return nn3(this->iy_, rs, nullptr);
878 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
880 std::vector<RRTNode *> nvs;
881 unsigned int iy = IYI(node->y());
882 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
883 unsigned int i = 0; // vector index
884 unsigned int j = 0; // array index
885 unsigned int jmin = 0; // minimal j index
886 unsigned int jmax = 0; // maximal j index
888 jmin = (jmin > 0) ? jmin : 0;
889 jmax = iy + iy_dist + 1;
890 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
891 #pragma omp parallel for reduction(merge: nvs)
892 for (j = jmin; j < jmax; j++) {
893 #pragma omp parallel for reduction(merge: nvs)
894 for (i = 0; i < this->iy_[j].size(); i++) {
895 if (this->cost(this->iy_[j][i], node) < dist) {
896 nvs.push_back(this->iy_[j][i]);
903 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
905 return st3(init, goal);
908 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
910 return st3(init, goal, step);