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 for (auto n: this->nodes_)
43 for (auto n: this->dnodes_)
44 if (n != this->root_ && n != this->goal_)
46 for (auto s: this->samples_)
49 for (auto edges: this->rlog_)
60 this->nodes_.push_back(this->root_);
61 this->add_iy(this->root_);
64 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
68 this->nodes_.push_back(init);
73 RRTNode *RRTBase::root()
78 RRTNode *RRTBase::goal()
83 std::vector<RRTNode *> &RRTBase::nodes()
88 std::vector<RRTNode *> &RRTBase::dnodes()
93 std::vector<RRTNode *> &RRTBase::samples()
95 return this->samples_;
98 std::vector<CircleObstacle> *RRTBase::co()
100 return this->cobstacles_;
103 std::vector<SegmentObstacle> *RRTBase::so()
105 return this->sobstacles_;
108 std::vector<float> &RRTBase::clog()
113 std::vector<float> &RRTBase::nlog()
118 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
123 std::vector<float> &RRTBase::slog()
128 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
133 bool RRTBase::goal_found()
135 return this->goal_found_;
138 float RRTBase::elapsed()
140 std::chrono::duration<float> dt;
141 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
142 this->tend_ - this->tstart_);
147 void RRTBase::root(RRTNode *node)
152 void RRTBase::goal(RRTNode *node)
157 bool RRTBase::logr(RRTNode *root)
159 std::vector<RRTEdge *> e; // Edges to log
160 std::vector<RRTNode *> s; // DFS stack
161 std::vector<RRTNode *> r; // reset visited_
164 while (s.size() > 0) {
169 for (auto ch: tmp->children()) {
171 e.push_back(new RRTEdge(tmp, ch));
177 this->rlog_.push_back(e);
181 float RRTBase::ocost(RRTNode *n)
184 for (auto o: *this->cobstacles_)
185 if (o.dist_to(n) < dist)
187 for (auto o: *this->sobstacles_)
188 if (o.dist_to(n) < dist)
190 return n->ocost(dist);
193 bool RRTBase::tlog(std::vector<RRTNode *> t)
196 this->slog_.push_back(this->elapsed());
197 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
198 this->nlog_.push_back(this->nodes_.size());
199 this->tlog_.push_back(t);
206 void RRTBase::tstart()
208 this->tstart_ = std::chrono::high_resolution_clock::now();
213 this->tend_ = std::chrono::high_resolution_clock::now();
216 bool RRTBase::link_obstacles(
217 std::vector<CircleObstacle> *cobstacles,
218 std::vector<SegmentObstacle> *sobstacles)
220 this->cobstacles_ = cobstacles;
221 this->sobstacles_ = sobstacles;
222 if (!this->cobstacles_ || !this->sobstacles_) {
228 bool RRTBase::add_iy(RRTNode *n)
235 this->iy_[i].push_back(n);
239 bool RRTBase::goal_found(bool f)
241 this->goal_found_ = f;
246 bool RRTBase::glplot()
248 glClear(GL_COLOR_BUFFER_BIT);
253 for (auto o: *this->sobstacles_) {
255 glVertex2f(GLVERTEX(o.init()));
256 glVertex2f(GLVERTEX(o.goal()));
263 glVertex2f(GLVERTEX(this->root_));
264 glVertex2f(GLVERTEX(this->goal_));
267 if (this->samples_.size() > 0) {
271 glVertex2f(GLVERTEX(this->samples_.back()));
275 std::vector<RRTNode *> s; // DFS stack
276 std::vector<RRTNode *> r; // reset visited_
279 s.push_back(this->root_);
280 while (s.size() > 0) {
285 for (auto ch: tmp->children()) {
287 glColor3f(0.5, 0.5, 0.5);
288 glVertex2f(GLVERTEX(tmp));
289 glVertex2f(GLVERTEX(ch));
294 // Plot nodes (from goal)
296 s.push_back(this->goal_);
297 while (s.size() > 0) {
302 for (auto ch: tmp->children()) {
304 glColor3f(0.5, 0.5, 0.5);
305 glVertex2f(GLVERTEX(tmp));
306 glVertex2f(GLVERTEX(ch));
311 std::vector<RRTNode *> cusps;
312 // Plot last trajectory
313 if (this->tlog().size() > 0) {
316 for (auto n: this->tlog().back()) {
319 glVertex2f(GLVERTEX(n));
320 glVertex2f(GLVERTEX(n->parent()));
321 if (sgn(n->s()) != sgn(n->parent()->s()))
330 for (auto n: cusps) {
332 glVertex2f(GLVERTEX(n));
335 SDL_GL_SwapWindow(gw);
341 bool RRTBase::goal_found(
343 float (*cost)(RRTNode *, RRTNode* ))
345 float xx = pow(node->x() - this->goal_->x(), 2);
346 float yy = pow(node->y() - this->goal_->y(), 2);
347 float dh = std::abs(node->h() - this->goal_->h());
348 if (IS_NEAR(node, this->goal_)) {
349 if (this->goal_found_) {
350 if (node->ccost() + (*cost)(node, this->goal_) <
351 this->goal_->ccost()) {
352 RRTNode *op; // old parent
353 float oc; // old cumulative cost
354 float od; // old direct cost
355 op = this->goal_->parent();
356 oc = this->goal_->ccost();
357 od = this->goal_->dcost();
358 node->add_child(this->goal_,
359 (*cost)(node, this->goal_));
360 if (this->collide(node, this->goal_)) {
361 node->children().pop_back();
362 this->goal_->parent(op);
363 this->goal_->ccost(oc);
364 this->goal_->dcost(od);
366 op->rem_child(this->goal_);
375 (*cost)(node, this->goal_));
376 if (this->collide(node, this->goal_)) {
377 node->children().pop_back();
378 this->goal_->remove_parent();
381 this->goal_found_ = true;
388 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
390 std::vector<RRTEdge *> edges;
392 volatile bool col = false;
394 while (tmp != init) {
395 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
396 std::vector<RRTEdge *> bcframe = bc.frame();
397 #pragma omp parallel for reduction(|: col)
398 for (i = 0; i < (*this->cobstacles_).size(); i++) {
399 if ((*this->cobstacles_)[i].collide(tmp)) {
402 for (auto &e: bcframe) {
403 if ((*this->cobstacles_)[i].collide(e)) {
409 for (auto e: bcframe) {
414 for (auto e: edges) {
419 #pragma omp parallel for reduction(|: col)
420 for (i = 0; i < (*this->sobstacles_).size(); i++) {
421 for (auto &e: bcframe) {
422 if ((*this->sobstacles_)[i].collide(e)) {
428 for (auto e: bcframe) {
433 for (auto e: edges) {
438 if (!tmp->parent()) {
441 edges.push_back(new RRTEdge(tmp, tmp->parent()));
443 for (auto e: bcframe) {
449 for (auto &e: edges) {
450 #pragma omp parallel for reduction(|: col)
451 for (i = 0; i < (*this->cobstacles_).size(); i++) {
452 if ((*this->cobstacles_)[i].collide(e)) {
457 for (auto e: edges) {
462 #pragma omp parallel for reduction(|: col)
463 for (i = 0; i < (*this->sobstacles_).size(); i++) {
464 if ((*this->sobstacles_)[i].collide(e)) {
469 for (auto e: edges) {
475 for (auto e: edges) {
481 class RRTNodeDijkstra {
483 RRTNodeDijkstra(int i):
489 RRTNodeDijkstra(int i, float c):
495 RRTNodeDijkstra(int i, int p, float c):
514 class RRTNodeDijkstraComparator {
517 const RRTNodeDijkstra& n1,
518 const RRTNodeDijkstra& n2)
524 bool RRTBase::optp_dijkstra(
525 std::vector<RRTNode *> &cusps,
526 std::vector<int> &npi)
528 std::vector<RRTNodeDijkstra> dnodes;
529 for (unsigned int i = 0; i < cusps.size(); i++)
531 dnodes.push_back(RRTNodeDijkstra(
536 dnodes.push_back(RRTNodeDijkstra(
542 std::vector<RRTNodeDijkstra>,
543 RRTNodeDijkstraComparator> pq;
544 RRTNodeDijkstra tmp = dnodes[0];
546 float ch_cost = 9999;
547 std::vector<RRTNode *> steered;
548 while (!pq.empty()) {
551 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
552 ch_cost = dnodes[tmp.ni].c +
553 this->cost(cusps[tmp.ni], cusps[i]);
554 steered = this->steer(cusps[tmp.ni], cusps[i]);
555 for (unsigned int j = 0; j < steered.size() - 1; j++)
556 steered[j]->add_child(steered[j + 1], 1);
559 steered[steered.size() - 1])) {
560 for (auto n: steered)
564 if (ch_cost < dnodes[i].c) {
565 dnodes[i].c = ch_cost;
566 dnodes[i].pi = tmp.ni;
570 for (auto n: steered)
574 unsigned int tmpi = 0;
575 for (auto n: dnodes) {
576 if (n.v && n.ni > tmpi)
581 tmpi = dnodes[tmpi].pi;
584 std::reverse(npi.begin(), npi.end());
588 bool RRTBase::optp_rrp(
589 std::vector<RRTNode *> &cusps,
590 std::vector<int> &npi)
592 std::vector<RRTNode *> steered;
593 std::vector<int> candidates;
594 RRTNode *x_j = nullptr;
595 RRTNode *x_i = nullptr;
596 int j = cusps.size() - 1;
610 for (int i = 0; i < j; i++) {
611 steered = this->steer(cusps[i], x_j);
612 for (unsigned int k = 0; k < steered.size() - 1; k++)
613 steered[k]->add_child(steered[k + 1], 1);
616 steered[steered.size() - 1]))
617 candidates.push_back(i);
619 if (candidates.size() <= 0)
621 i_min = candidates[0];
624 for (auto c: candidates) {
626 dx = x_j->x() - x_i->x();
627 dy = x_j->y() - x_i->y();
628 ed = EDIST(x_i, x_j);
629 th_i = (cos(x_i->h()) * dx + sin(x_i->h()) * dy) / ed;
630 th_j = (cos(x_j->h()) * dx + sin(x_j->h()) * dy) / ed;
641 std::reverse(npi.begin(), npi.end());
645 bool RRTBase::optp_smart(
646 std::vector<RRTNode *> &cusps,
647 std::vector<int> &npi)
649 std::vector<RRTNode *> steered;
650 int li = cusps.size() - 1;
654 steered = this->steer(cusps[ai - 1], cusps[li]);
655 for (unsigned int j = 0; j < steered.size() - 1; j++)
656 steered[j]->add_child(steered[j + 1], 1);
657 if (this->collide(steered[0], steered[steered.size() - 1])) {
662 for (auto n: steered)
666 std::reverse(npi.begin(), npi.end());
670 bool RRTBase::opt_path()
672 if (this->tlog().size() == 0)
674 float oc = this->tlog().back().front()->ccost();
675 std::vector<RRTNode *> tmp_cusps;
676 for (auto n: this->tlog().back()) {
677 if (sgn(n->s()) == 0) {
678 tmp_cusps.push_back(n);
679 } else if (n->parent() &&
680 sgn(n->s()) != sgn(n->parent()->s())) {
681 tmp_cusps.push_back(n);
682 tmp_cusps.push_back(n->parent());
684 //tmp_cusps.push_back(n);
686 if (tmp_cusps.size() < 2)
688 std::vector<RRTNode *> cusps;
689 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
690 if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
691 cusps.push_back(tmp_cusps[i]);
693 std::reverse(cusps.begin(), cusps.end());
694 std::vector<int> npi; // new path indexes
695 if (!this->optp_dijkstra(cusps, npi))
697 RRTNode *pn = cusps[npi[0]];
698 RRTNode *tmp = nullptr;
700 for (unsigned int i = 0; i < npi.size() - 1; i++) {
702 for (auto ns: this->steer(cusps[npi[i]], cusps[npi[i + 1]])) {
705 } else if (IS_NEAR(cusps[npi[i]], ns)) {
707 while (tmp && tmp != cusps[npi[i]]) {
713 } else if (IS_NEAR(ns, cusps[npi[i + 1]])) {
715 cusps[npi[i + 1]]->parent()->rem_child(
719 this->cost(pn, cusps[npi[i + 1]]));
721 } else if (IS_NEAR(pn, ns)) {
724 this->nodes().push_back(ns);
726 pn->add_child(ns, this->cost(pn, ns));
731 this->root()->update_ccost();
732 if (this->tlog().back().front()->ccost() < oc)
737 bool RRTBase::rebase(RRTNode *nr)
739 if (!nr || this->goal_ == nr || this->root_ == nr)
741 std::vector<RRTNode *> s; // DFS stack
744 unsigned int to_del = 0;
746 s.push_back(this->root_);
747 while (s.size() > 0) {
750 for (auto ch: tmp->children()) {
754 to_del = this->nodes_.size();
755 #pragma omp parallel for reduction(min: to_del)
756 for (i = 0; i < this->nodes_.size(); i++) {
757 if (this->nodes_[i] == tmp)
760 if (to_del < this->nodes_.size())
761 this->nodes_.erase(this->nodes_.begin() + to_del);
763 to_del = this->iy_[iy].size();
764 #pragma omp parallel for reduction(min: to_del)
765 for (i = 0; i < this->iy_[iy].size(); i++) {
766 if (this->iy_[iy][i] == tmp)
769 if (to_del < this->iy_[iy].size())
770 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
771 this->dnodes().push_back(tmp);
774 this->root_->remove_parent();
778 std::vector<RRTNode *> RRTBase::findt()
780 return this->findt(this->goal_);
783 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
785 std::vector<RRTNode *> nodes;
786 if (!n || !n->parent())
796 RRTNode *RRTBase::sample()
801 float RRTBase::cost(RRTNode *init, RRTNode *goal)
803 return co2(init, goal);
806 RRTNode *RRTBase::nn(RRTNode *rs)
808 return nn4(this->iy_, rs, nullptr);
809 //return nn3(this->iy_, rs, nullptr);
812 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
814 std::vector<RRTNode *> nvs;
815 unsigned int iy = IYI(node->y());
816 unsigned int iy_dist = floor(dist / IYSTEP) + 1;
817 unsigned int i = 0; // vector index
818 unsigned int j = 0; // array index
819 unsigned int jmin = 0; // minimal j index
820 unsigned int jmax = 0; // maximal j index
822 jmin = (jmin > 0) ? jmin : 0;
823 jmax = iy + iy_dist + 1;
824 jmax = (jmax < IYSIZE) ? jmax : IYSIZE;
825 #pragma omp parallel for reduction(merge: nvs)
826 for (j = jmin; j < jmax; j++) {
827 #pragma omp parallel for reduction(merge: nvs)
828 for (i = 0; i < this->iy_[j].size(); i++) {
829 if (this->cost(this->iy_[j][i], node) < dist) {
830 nvs.push_back(this->iy_[j][i]);
837 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
839 return st3(init, goal);
842 std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
844 return st3(init, goal, step);