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/>.
32 extern SDL_Window* gw;
33 extern SDL_GLContext gc;
37 for (auto n: this->nodes_)
40 for (auto n: this->dnodes_)
41 if (n != this->root_ && n != this->goal_)
43 for (auto s: this->samples_)
46 for (auto edges: this->rlog_)
57 this->nodes_.push_back(this->root_);
58 this->add_iy(this->root_);
61 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
65 this->nodes_.push_back(init);
70 RRTNode *RRTBase::root()
75 RRTNode *RRTBase::goal()
80 std::vector<RRTNode *> &RRTBase::nodes()
85 std::vector<RRTNode *> &RRTBase::dnodes()
90 std::vector<RRTNode *> &RRTBase::samples()
92 return this->samples_;
95 std::vector<CircleObstacle> *RRTBase::cos()
97 return this->cobstacles_;
100 std::vector<SegmentObstacle> *RRTBase::sos()
102 return this->sobstacles_;
105 std::vector<float> &RRTBase::clog()
110 std::vector<float> &RRTBase::nlog()
115 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
120 std::vector<float> &RRTBase::slog()
125 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
130 bool RRTBase::goal_found()
132 return this->goal_found_;
135 float RRTBase::elapsed()
137 std::chrono::duration<float> dt;
138 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
139 this->tend_ - this->tstart_);
144 void RRTBase::root(RRTNode *node)
149 void RRTBase::goal(RRTNode *node)
154 bool RRTBase::logr(RRTNode *root)
156 std::vector<RRTEdge *> e; // Edges to log
157 std::vector<RRTNode *> s; // DFS stack
158 std::vector<RRTNode *> r; // reset visited_
161 while (s.size() > 0) {
166 for (auto ch: tmp->children()) {
168 e.push_back(new RRTEdge(tmp, ch));
174 this->rlog_.push_back(e);
178 float RRTBase::ocost(RRTNode *n)
181 for (auto o: *this->cobstacles_)
182 if (o.dist_to(n) < dist)
184 for (auto o: *this->sobstacles_)
185 if (o.dist_to(n) < dist)
187 return n->ocost(dist);
190 bool RRTBase::tlog(std::vector<RRTNode *> t)
193 this->slog_.push_back(this->elapsed());
194 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
195 this->nlog_.push_back(this->nodes_.size());
196 this->tlog_.push_back(t);
203 void RRTBase::tstart()
205 this->tstart_ = std::chrono::high_resolution_clock::now();
210 this->tend_ = std::chrono::high_resolution_clock::now();
213 bool RRTBase::link_obstacles(
214 std::vector<CircleObstacle> *cobstacles,
215 std::vector<SegmentObstacle> *sobstacles)
217 this->cobstacles_ = cobstacles;
218 this->sobstacles_ = sobstacles;
219 if (!this->cobstacles_ || !this->sobstacles_) {
225 bool RRTBase::add_iy(RRTNode *n)
232 this->iy_[i].push_back(n);
236 bool RRTBase::goal_found(bool f)
238 this->goal_found_ = f;
243 bool RRTBase::glplot()
245 glClear(GL_COLOR_BUFFER_BIT);
250 for (auto o: *this->sobstacles_) {
252 glVertex2f(GLVERTEX(o.init()));
253 glVertex2f(GLVERTEX(o.goal()));
260 glVertex2f(GLVERTEX(this->root_));
261 glVertex2f(GLVERTEX(this->goal_));
264 if (this->samples_.size() > 0) {
268 glVertex2f(GLVERTEX(this->samples_.back()));
272 std::vector<RRTNode *> s; // DFS stack
273 std::vector<RRTNode *> r; // reset visited_
276 s.push_back(this->root_);
277 while (s.size() > 0) {
282 for (auto ch: tmp->children()) {
284 glColor3f(0.5, 0.5, 0.5);
285 glVertex2f(GLVERTEX(tmp));
286 glVertex2f(GLVERTEX(ch));
291 std::vector<RRTNode *> cusps;
292 // Plot last trajectory
293 if (this->tlog().size() > 0) {
296 for (auto n: this->tlog().back()) {
299 glVertex2f(GLVERTEX(n));
300 glVertex2f(GLVERTEX(n->parent()));
301 if (sgn(n->s()) != sgn(n->parent()->s()))
310 for (auto n: cusps) {
312 glVertex2f(GLVERTEX(n));
315 SDL_GL_SwapWindow(gw);
321 bool RRTBase::goal_found(
323 float (*cost)(RRTNode *, RRTNode* ))
325 float xx = pow(node->x() - this->goal_->x(), 2);
326 float yy = pow(node->y() - this->goal_->y(), 2);
327 float dh = std::abs(node->h() - this->goal_->h());
328 if (IS_NEAR(node, this->goal_)) {
329 if (this->goal_found_) {
330 if (node->ccost() + (*cost)(node, this->goal_) <
331 this->goal_->ccost()) {
332 RRTNode *op; // old parent
333 float oc; // old cumulative cost
334 float od; // old direct cost
335 op = this->goal_->parent();
336 oc = this->goal_->ccost();
337 od = this->goal_->dcost();
338 node->add_child(this->goal_,
339 (*cost)(node, this->goal_));
340 if (this->collide(node, this->goal_)) {
341 node->children().pop_back();
342 this->goal_->parent(op);
343 this->goal_->ccost(oc);
344 this->goal_->dcost(od);
346 op->rem_child(this->goal_);
355 (*cost)(node, this->goal_));
356 if (this->collide(node, this->goal_)) {
357 node->children().pop_back();
358 this->goal_->remove_parent();
361 this->goal_found_ = true;
368 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
370 std::vector<RRTEdge *> edges;
372 volatile bool col = false;
374 while (tmp != init) {
375 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
376 std::vector<RRTEdge *> bcframe = bc.frame();
377 #pragma omp parallel for reduction(|: col)
378 for (i = 0; i < (*this->cobstacles_).size(); i++) {
379 if ((*this->cobstacles_)[i].collide(tmp)) {
382 for (auto &e: bcframe) {
383 if ((*this->cobstacles_)[i].collide(e)) {
389 for (auto e: bcframe) {
394 for (auto e: edges) {
399 #pragma omp parallel for reduction(|: col)
400 for (i = 0; i < (*this->sobstacles_).size(); i++) {
401 for (auto &e: bcframe) {
402 if ((*this->sobstacles_)[i].collide(e)) {
408 for (auto e: bcframe) {
413 for (auto e: edges) {
418 if (!tmp->parent()) {
421 edges.push_back(new RRTEdge(tmp, tmp->parent()));
423 for (auto e: bcframe) {
429 for (auto &e: edges) {
430 #pragma omp parallel for reduction(|: col)
431 for (i = 0; i < (*this->cobstacles_).size(); i++) {
432 if ((*this->cobstacles_)[i].collide(e)) {
437 for (auto e: edges) {
442 #pragma omp parallel for reduction(|: col)
443 for (i = 0; i < (*this->sobstacles_).size(); i++) {
444 if ((*this->sobstacles_)[i].collide(e)) {
449 for (auto e: edges) {
455 for (auto e: edges) {
461 class RRTNodeDijkstra {
463 RRTNodeDijkstra(int i):
469 RRTNodeDijkstra(int i, float c):
475 RRTNodeDijkstra(int i, int p, float c):
494 class RRTNodeDijkstraComparator {
497 const RRTNodeDijkstra& n1,
498 const RRTNodeDijkstra& n2)
504 bool RRTBase::opt_path()
506 if (this->tlog().size() == 0)
508 float oc = this->tlog().back().front()->ccost();
509 std::vector<RRTNode *> tmp_cusps;
510 for (auto n: this->tlog().back()) {
511 if (sgn(n->s()) == 0) {
512 tmp_cusps.push_back(n);
513 } else if (n->parent() &&
514 sgn(n->s()) != sgn(n->parent()->s())) {
515 tmp_cusps.push_back(n);
516 tmp_cusps.push_back(n->parent());
519 if (tmp_cusps.size() < 2)
521 std::vector<RRTNode *> cusps;
522 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
523 if (tmp_cusps[i] != tmp_cusps[i + 1])
524 cusps.push_back(tmp_cusps[i]);
526 std::reverse(cusps.begin(), cusps.end());
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() && tmp.ni != cusps.size() - 1) {
551 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
552 ch_cost = dnodes[tmp.ni].c +
553 CO(cusps[tmp.ni], cusps[i]);
554 steered = ST(cusps[tmp.ni], cusps[i]);
555 for (unsigned int j = 0; j < steered.size() - 1; j++)
556 steered[j]->add_child(
561 if (i != tmp.ni + 1 && this->collide( // TODO
563 steered[steered.size() - 1]))
565 if (ch_cost < dnodes[i].c) {
566 dnodes[i].c = ch_cost;
567 dnodes[i].pi = tmp.ni;
573 if (tmp.ni != cusps.size() - 1)
575 std::vector<int> npi; // new path indexes
579 tmpi = dnodes[tmpi].pi;
582 std::reverse(npi.begin(), npi.end());
583 RRTNode *pn = cusps[npi[0]];
584 for (unsigned int i = 0; i < npi.size() - 1; i++) {
585 for (auto ns: ST(cusps[npi[i]], cusps[npi[i + 1]])) {
586 pn->add_child(ns, CO(pn, ns));
591 this->tlog().back().front(),
592 CO(pn, this->tlog().back().front()));
594 if (this->tlog().back().front()->ccost() < oc)
599 bool RRTBase::rebase(RRTNode *nr)
601 if (!nr || this->goal_ == nr || this->root_ == nr)
603 std::vector<RRTNode *> s; // DFS stack
606 unsigned int to_del = 0;
608 s.push_back(this->root_);
609 while (s.size() > 0) {
612 for (auto ch: tmp->children()) {
616 to_del = this->nodes_.size();
617 #pragma omp parallel for reduction(min: to_del)
618 for (i = 0; i < this->nodes_.size(); i++) {
619 if (this->nodes_[i] == tmp)
622 if (to_del < this->nodes_.size())
623 this->nodes_.erase(this->nodes_.begin() + to_del);
626 to_del = this->iy_[iy].size();
627 #pragma omp parallel for reduction(min: to_del)
628 for (i = 0; i < this->iy_[iy].size(); i++) {
629 if (this->iy_[iy][i] == tmp)
632 if (to_del < this->iy_[iy].size())
633 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
635 this->dnodes().push_back(tmp);
638 this->root_->remove_parent();
642 std::vector<RRTNode *> RRTBase::findt()
644 return this->findt(this->goal_);
647 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
649 std::vector<RRTNode *> nodes;
650 if (!n || !n->parent())
653 while (tmp != this->root()) {
654 nodes.push_back(tmp);