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/>.
40 extern SDL_Window* gw;
41 extern SDL_GLContext gc;
46 pthread_mutex_init(&this->m_, NULL);
51 pthread_mutex_lock(&this->m_);
52 bool ret = this->changed_;
53 pthread_mutex_unlock(&this->m_);
57 std::vector<RRTNode *> Cell::nodes()
59 pthread_mutex_lock(&this->m_);
60 std::vector<RRTNode *> ret(this->nodes_);
61 pthread_mutex_unlock(&this->m_);
65 void Cell::add_node(RRTNode *n)
67 pthread_mutex_lock(&this->m_);
68 this->nodes_.push_back(n);
69 this->changed_ = true;
70 pthread_mutex_unlock(&this->m_);
75 // Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
76 // please uncommend the following code:
78 for (auto n: this->nodes_)
81 for (auto n: this->dnodes_)
82 if (n != this->root_ && n != this->goal_)
84 for (auto s: this->samples_)
87 for (auto edges: this->rlog_)
97 gen_(std::random_device{}())
99 this->nodes_.reserve(NOFNODES);
100 this->nodes_.push_back(this->root_);
101 this->add_iy(this->root_);
102 this->add_ixy(this->root_);
105 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
108 gen_(std::random_device{}())
110 this->nodes_.reserve(NOFNODES);
111 this->nodes_.push_back(init);
117 RRTNode *RRTBase::root()
122 RRTNode *RRTBase::goal()
127 std::vector<RRTNode *> &RRTBase::nodes()
132 std::vector<RRTNode *> &RRTBase::dnodes()
134 return this->dnodes_;
137 PolygonObstacle &RRTBase::frame()
142 std::vector<RRTNode *> &RRTBase::samples()
144 return this->samples_;
147 std::vector<CircleObstacle> *RRTBase::co()
149 return this->cobstacles_;
152 std::vector<SegmentObstacle> *RRTBase::so()
154 return this->sobstacles_;
157 std::vector<float> &RRTBase::clog()
162 std::vector<float> &RRTBase::nlog()
167 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
172 std::vector<float> &RRTBase::slog()
177 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
182 std::vector<RRTNode *> &RRTBase::slot_cusp()
184 return this->slot_cusp_;
187 bool RRTBase::goal_found()
189 return this->goal_found_;
192 float RRTBase::elapsed()
194 std::chrono::duration<float> dt;
195 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
196 this->tend_ - this->tstart_);
200 std::vector<RRTNode *> RRTBase::traj_cusp()
202 std::vector<RRTNode *> tmp_cusps;
203 for (auto n: this->tlog().back()) {
204 if (sgn(n->s()) == 0) {
205 tmp_cusps.push_back(n);
206 } else if (n->parent() &&
207 sgn(n->s()) != sgn(n->parent()->s())) {
208 tmp_cusps.push_back(n);
209 tmp_cusps.push_back(n->parent());
212 std::vector<RRTNode *> cusps;
213 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
214 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
215 cusps.push_back(tmp_cusps[i]);
221 void RRTBase::root(RRTNode *node)
226 void RRTBase::goal(RRTNode *node)
231 bool RRTBase::logr(RRTNode *root)
233 std::vector<RRTEdge *> e; // Edges to log
234 std::vector<RRTNode *> s; // DFS stack
235 std::vector<RRTNode *> r; // reset visited_
238 while (s.size() > 0) {
243 for (auto ch: tmp->children()) {
245 e.push_back(new RRTEdge(tmp, ch));
251 this->rlog_.push_back(e);
255 float RRTBase::ocost(RRTNode *n)
258 for (auto o: *this->cobstacles_)
259 if (o.dist_to(n) < dist)
261 for (auto o: *this->sobstacles_)
262 if (o.dist_to(n) < dist)
264 return n->ocost(dist);
267 bool RRTBase::tlog(std::vector<RRTNode *> t)
270 this->slog_.push_back(this->elapsed());
271 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
272 this->nlog_.push_back(this->nodes_.size());
273 this->tlog_.push_back(t);
280 void RRTBase::tstart()
282 this->tstart_ = std::chrono::high_resolution_clock::now();
287 this->tend_ = std::chrono::high_resolution_clock::now();
290 bool RRTBase::link_obstacles(
291 std::vector<CircleObstacle> *cobstacles,
292 std::vector<SegmentObstacle> *sobstacles)
294 this->cobstacles_ = cobstacles;
295 this->sobstacles_ = sobstacles;
296 if (!this->cobstacles_ || !this->sobstacles_) {
302 bool RRTBase::add_iy(RRTNode *n)
309 this->iy_[i].push_back(n);
313 bool RRTBase::add_ixy(RRTNode *n)
315 int ix = IXI(n->x());
320 int iy = IYI(n->y());
325 this->ixy_[ix][iy].add_node(n);
329 bool RRTBase::goal_found(bool f)
331 this->goal_found_ = f;
335 void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
337 for (unsigned int i = 0; i < sc.size() - 1; i++)
338 sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
339 sc[0]->parent(this->goal());
340 this->slot_cusp_ = sc;
344 bool RRTBase::glplot()
347 glClear(GL_COLOR_BUFFER_BIT);
352 for (auto o: *this->sobstacles_) {
354 glVertex2f(GLVERTEX(o.init()));
355 glVertex2f(GLVERTEX(o.goal()));
362 glVertex2f(GLVERTEX(this->root_));
363 glVertex2f(GLVERTEX(this->goal_));
366 if (this->samples_.size() > 0) {
370 glVertex2f(GLVERTEX(this->samples_.back()));
374 std::vector<RRTNode *> s; // DFS stack
375 std::vector<RRTNode *> r; // reset visited_
378 s.push_back(this->root_);
379 while (s.size() > 0) {
384 for (auto ch: tmp->children()) {
386 glColor3f(0.5, 0.5, 0.5);
387 glVertex2f(GLVERTEX(tmp));
388 glVertex2f(GLVERTEX(ch));
393 // Plot nodes (from goal)
395 s.push_back(this->goal_);
396 while (s.size() > 0) {
401 for (auto ch: tmp->children()) {
403 glColor3f(0.5, 0.5, 0.5);
404 glVertex2f(GLVERTEX(tmp));
405 glVertex2f(GLVERTEX(ch));
410 std::vector<RRTNode *> cusps;
411 // Plot last trajectory
412 if (this->tlog().size() > 0) {
415 for (auto n: this->tlog().back()) {
418 glVertex2f(GLVERTEX(n));
419 glVertex2f(GLVERTEX(n->parent()));
420 if (sgn(n->s()) != sgn(n->parent()->s()))
429 for (auto n: cusps) {
431 glVertex2f(GLVERTEX(n));
434 SDL_GL_SwapWindow(gw);
441 bool RRTBase::goal_found(
443 float (*cost)(RRTNode *, RRTNode* ))
445 if (IS_NEAR(node, this->goal_)) {
446 if (this->goal_found_) {
447 if (node->ccost() + this->cost(node, this->goal_) <
448 this->goal_->ccost()) {
449 RRTNode *op; // old parent
450 float oc; // old cumulative cost
451 float od; // old direct cost
452 op = this->goal_->parent();
453 oc = this->goal_->ccost();
454 od = this->goal_->dcost();
455 node->add_child(this->goal_,
456 this->cost(node, this->goal_));
457 if (this->collide(node, this->goal_)) {
458 node->children().pop_back();
459 this->goal_->parent(op);
460 this->goal_->ccost(oc);
461 this->goal_->dcost(od);
463 op->rem_child(this->goal_);
472 this->cost(node, this->goal_));
473 if (this->collide(node, this->goal_)) {
474 node->children().pop_back();
475 this->goal_->remove_parent();
478 this->goal_found_ = true;
485 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
487 std::vector<RRTEdge *> edges;
489 volatile bool col = false;
491 while (tmp != init) {
492 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
493 std::vector<RRTEdge *> bcframe = bc.frame();
494 #pragma omp parallel for reduction(|: col)
495 for (i = 0; i < (*this->cobstacles_).size(); i++) {
496 if ((*this->cobstacles_)[i].collide(tmp)) {
499 for (auto &e: bcframe) {
500 if ((*this->cobstacles_)[i].collide(e)) {
506 for (auto e: bcframe) {
511 for (auto e: edges) {
516 #pragma omp parallel for reduction(|: col)
517 for (i = 0; i < (*this->sobstacles_).size(); i++) {
518 for (auto &e: bcframe) {
519 if ((*this->sobstacles_)[i].collide(e)) {
525 for (auto e: bcframe) {
530 for (auto e: edges) {
535 if (!tmp->parent()) {
538 edges.push_back(new RRTEdge(tmp, tmp->parent()));
540 for (auto e: bcframe) {
546 for (auto &e: edges) {
547 #pragma omp parallel for reduction(|: col)
548 for (i = 0; i < (*this->cobstacles_).size(); i++) {
549 if ((*this->cobstacles_)[i].collide(e)) {
554 for (auto e: edges) {
559 #pragma omp parallel for reduction(|: col)
560 for (i = 0; i < (*this->sobstacles_).size(); i++) {
561 if ((*this->sobstacles_)[i].collide(e)) {
566 for (auto e: edges) {
572 for (auto e: edges) {
578 class RRTNodeDijkstra {
580 RRTNodeDijkstra(int i):
586 RRTNodeDijkstra(int i, float c):
592 RRTNodeDijkstra(int i, int p, float c):
611 class RRTNodeDijkstraComparator {
614 const RRTNodeDijkstra& n1,
615 const RRTNodeDijkstra& n2)
621 bool RRTBase::optp_dijkstra(
622 std::vector<RRTNode *> &cusps,
623 std::vector<int> &npi)
625 std::vector<RRTNodeDijkstra> dnodes;
626 for (unsigned int i = 0; i < cusps.size(); i++)
628 dnodes.push_back(RRTNodeDijkstra(
633 dnodes.push_back(RRTNodeDijkstra(
639 std::vector<RRTNodeDijkstra>,
640 RRTNodeDijkstraComparator> pq;
641 RRTNodeDijkstra tmp = dnodes[0];
643 float ch_cost = 9999;
644 std::vector<RRTNode *> steered;
645 while (!pq.empty()) {
648 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
649 ch_cost = dnodes[tmp.ni].c +
650 this->cost(cusps[tmp.ni], cusps[i]);
651 steered = this->steer(cusps[tmp.ni], cusps[i]);
652 for (unsigned int j = 0; j < steered.size() - 1; j++)
653 steered[j]->add_child(steered[j + 1], 1);
656 steered[steered.size() - 1])) {
657 for (auto n: steered)
661 if (ch_cost < dnodes[i].c) {
662 dnodes[i].c = ch_cost;
663 dnodes[i].pi = tmp.ni;
667 for (auto n: steered)
671 unsigned int tmpi = 0;
672 for (auto n: dnodes) {
673 if (n.v && n.ni > tmpi)
678 tmpi = dnodes[tmpi].pi;
681 std::reverse(npi.begin(), npi.end());
685 bool RRTBase::optp_rrp(
686 std::vector<RRTNode *> &cusps,
687 std::vector<int> &npi)
689 std::vector<RRTNode *> steered;
690 std::vector<int> candidates;
691 RRTNode *x_j = nullptr;
692 RRTNode *x_i = nullptr;
693 int j = cusps.size() - 1;
707 for (int i = 0; i < j; i++) {
708 steered = this->steer(cusps[i], x_j);
709 for (unsigned int k = 0; k < steered.size() - 1; k++)
710 steered[k]->add_child(steered[k + 1], 1);
713 steered[steered.size() - 1]))
714 candidates.push_back(i);
716 if (candidates.size() <= 0)
718 i_min = candidates[0];
721 for (auto c: candidates) {
723 dx = x_j->x() - x_i->x();
724 dy = x_j->y() - x_i->y();
725 ed = EDIST(x_i, x_j);
726 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
727 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
738 std::reverse(npi.begin(), npi.end());
742 bool RRTBase::optp_smart(
743 std::vector<RRTNode *> &cusps,
744 std::vector<int> &npi)
746 std::vector<RRTNode *> steered;
747 int li = cusps.size() - 1;
751 steered = this->steer(cusps[ai - 1], cusps[li]);
752 for (unsigned int j = 0; j < steered.size() - 1; j++)
753 steered[j]->add_child(steered[j + 1], 1);
754 if (this->collide(steered[0], steered[steered.size() - 1])) {
759 for (auto n: steered)
763 std::reverse(npi.begin(), npi.end());
767 bool RRTBase::opt_path()
769 if (this->tlog().size() == 0)
771 float oc = this->tlog().back().front()->ccost();
772 std::vector<RRTNode *> tmp_cusps;
773 for (auto n: this->tlog().back()) {
774 if (sgn(n->s()) == 0) {
775 tmp_cusps.push_back(n);
776 } else if (n->parent() &&
777 sgn(n->s()) != sgn(n->parent()->s())) {
778 tmp_cusps.push_back(n);
779 tmp_cusps.push_back(n->parent());
781 //tmp_cusps.push_back(n);
783 if (tmp_cusps.size() < 2)
785 std::vector<RRTNode *> cusps;
786 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
787 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
788 cusps.push_back(tmp_cusps[i]);
790 std::reverse(cusps.begin(), cusps.end());
791 std::vector<int> npi; // new path indexes
792 if (!this->optp_dijkstra(cusps, npi))
794 RRTNode *pn = cusps[npi[0]];
795 RRTNode *tmp = nullptr;
797 for (unsigned int i = 0; i < npi.size() - 1; i++) {
799 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
802 } else if (IS_NEAR(cusps[npi[i]], ns)) {
804 while (tmp && tmp != cusps[npi[i]]) {
810 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
812 cusps[npi[i + 1]]->parent()->rem_child(
816 this->cost(pn, cusps[npi[i + 1]]));
818 } else if (IS_NEAR(pn, ns)) {
821 this->nodes().push_back(ns);
824 pn->add_child(ns, this->cost(pn, ns));
829 this->root()->update_ccost();
830 if (this->tlog().back().front()->ccost() < oc)
835 bool RRTBase::rebase(RRTNode *nr)
837 if (!nr || this->goal_ == nr || this->root_ == nr)
839 std::vector<RRTNode *> s; // DFS stack
842 unsigned int to_del = 0;
844 s.push_back(this->root_);
845 while (s.size() > 0) {
848 for (auto ch: tmp->children()) {
852 to_del = this->nodes_.size();
853 #pragma omp parallel for reduction(min: to_del)
854 for (i = 0; i < this->nodes_.size(); i++) {
855 if (this->nodes_[i] == tmp)
858 if (to_del < this->nodes_.size())
859 this->nodes_.erase(this->nodes_.begin() + to_del);
861 to_del = this->iy_[iy].size();
862 #pragma omp parallel for reduction(min: to_del)
863 for (i = 0; i < this->iy_[iy].size(); i++) {
864 if (this->iy_[iy][i] == tmp)
867 if (to_del < this->iy_[iy].size())
868 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
869 this->dnodes().push_back(tmp);
872 this->root_->remove_parent();
876 std::vector<RRTNode *> RRTBase::findt()
878 return this->findt(this->goal_);
881 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
883 std::vector<RRTNode *> nodes;
884 if (!n || !n->parent())
894 RRTNode *RRTBase::sample()
896 if (this->useSamplingInfo_ && this->nodes().size() % 2 == 0) {
897 float sar = static_cast<float>(rand());
898 sar /= static_cast<float>(RAND_MAX / this->samplingInfo_.r);
899 sar += this->samplingInfo_.mr;
900 float sah = static_cast<float>(rand());
901 sah /= static_cast<float>(RAND_MAX / this->samplingInfo_.h);
902 sah *= this->samplingInfo_.dh;
903 sah += this->samplingInfo_.sh;
904 float h = static_cast<float>(rand());
905 h /= static_cast<float>(RAND_MAX / this->samplingInfo_.mh);
906 h += this->samplingInfo_.mmh;
907 h *= this->samplingInfo_.dh;
908 h += this->samplingInfo_.sh;
910 this->samplingInfo_.x + sar * cos(sah),
911 this->samplingInfo_.y + sar * sin(sah),
919 float RRTBase::cost(RRTNode *init, RRTNode *goal)
921 return co2(init, goal);
924 RRTNode *RRTBase::nn(RRTNode *rs)
926 return nn4(this->iy_, rs, nullptr);
927 //return nn3(this->iy_, rs, nullptr);
930 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
932 std::vector<RRTNode *> nvs;
933 unsigned int iy = IYI(node->y());
934 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
935 unsigned int i = 0; // vector index
936 unsigned int j = 0; // array index
937 unsigned int jmin = 0; // minimal j index
938 unsigned int jmax = 0; // maximal j index
940 jmin = (jmin > 0) ? jmin : 0;
941 jmax = iy + iy_dist + 1;
942 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
943 #pragma omp parallel for reduction(merge: nvs)
944 for (j = jmin; j < jmax; j++) {
945 #pragma omp parallel for reduction(merge: nvs)
946 for (i = 0; i < this->iy_[j].size(); i++) {
947 if (this->cost(this->iy_[j][i], node) < dist) {
948 nvs.push_back(this->iy_[j][i]);
955 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
957 return st3(init, goal);
960 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
962 return st3(init, goal, step);