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);
69 RRTNode *RRTBase::root()
74 RRTNode *RRTBase::goal()
79 std::vector<RRTNode *> &RRTBase::nodes()
84 std::vector<RRTNode *> &RRTBase::dnodes()
89 std::vector<RRTNode *> &RRTBase::samples()
91 return this->samples_;
94 std::vector<CircleObstacle> *RRTBase::cos()
96 return this->cobstacles_;
99 std::vector<SegmentObstacle> *RRTBase::sos()
101 return this->sobstacles_;
104 std::vector<float> &RRTBase::clog()
109 std::vector<float> &RRTBase::nlog()
114 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
119 std::vector<float> &RRTBase::slog()
124 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
129 bool RRTBase::goal_found()
131 return this->goal_found_;
134 float RRTBase::elapsed()
136 std::chrono::duration<float> dt;
137 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
138 this->tend_ - this->tstart_);
142 bool RRTBase::logr(RRTNode *root)
144 std::vector<RRTEdge *> e; // Edges to log
145 std::vector<RRTNode *> s; // DFS stack
146 std::vector<RRTNode *> r; // reset visited_
149 while (s.size() > 0) {
154 for (auto ch: tmp->children()) {
156 e.push_back(new RRTEdge(tmp, ch));
162 this->rlog_.push_back(e);
166 float RRTBase::ocost(RRTNode *n)
169 for (auto o: *this->cobstacles_)
170 if (o.dist_to(n) < dist)
172 for (auto o: *this->sobstacles_)
173 if (o.dist_to(n) < dist)
175 return n->ocost(dist);
178 bool RRTBase::tlog(std::vector<RRTNode *> t)
181 this->slog_.push_back(this->elapsed());
182 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
183 this->nlog_.push_back(this->nodes_.size());
184 this->tlog_.push_back(t);
191 void RRTBase::tstart()
193 this->tstart_ = std::chrono::high_resolution_clock::now();
198 this->tend_ = std::chrono::high_resolution_clock::now();
201 bool RRTBase::link_obstacles(
202 std::vector<CircleObstacle> *cobstacles,
203 std::vector<SegmentObstacle> *sobstacles)
205 this->cobstacles_ = cobstacles;
206 this->sobstacles_ = sobstacles;
207 if (!this->cobstacles_ || !this->sobstacles_) {
213 bool RRTBase::add_iy(RRTNode *n)
220 this->iy_[i].push_back(n);
224 bool RRTBase::glplot()
226 glClear(GL_COLOR_BUFFER_BIT);
231 for (auto o: *this->sobstacles_) {
233 glVertex2f(GLVERTEX(o.init()));
234 glVertex2f(GLVERTEX(o.goal()));
241 glVertex2f(GLVERTEX(this->root_));
242 glVertex2f(GLVERTEX(this->goal_));
248 glVertex2f(GLVERTEX(this->samples_.back()));
251 std::vector<RRTNode *> s; // DFS stack
252 std::vector<RRTNode *> r; // reset visited_
255 s.push_back(this->root_);
256 while (s.size() > 0) {
261 for (auto ch: tmp->children()) {
263 glColor3f(0.5, 0.5, 0.5);
264 glVertex2f(GLVERTEX(tmp));
265 glVertex2f(GLVERTEX(ch));
270 std::vector<RRTNode *> cusps;
271 // Plot last trajectory
272 if (this->tlog().size() > 0) {
275 for (auto n: this->tlog().back()) {
278 glVertex2f(GLVERTEX(n));
279 glVertex2f(GLVERTEX(n->parent()));
280 if (sgn(n->s()) != sgn(n->parent()->s()))
289 for (auto n: cusps) {
291 glVertex2f(GLVERTEX(n));
294 SDL_GL_SwapWindow(gw);
300 bool RRTBase::goal_found(
302 float (*cost)(RRTNode *, RRTNode* ))
304 float xx = pow(node->x() - this->goal_->x(), 2);
305 float yy = pow(node->y() - this->goal_->y(), 2);
306 float dh = std::abs(node->h() - this->goal_->h());
307 if (IS_NEAR(node, this->goal_)) {
308 if (this->goal_found_) {
309 if (node->ccost() + (*cost)(node, this->goal_) <
310 this->goal_->ccost()) {
311 RRTNode *op; // old parent
312 float oc; // old cumulative cost
313 float od; // old direct cost
314 op = this->goal_->parent();
315 oc = this->goal_->ccost();
316 od = this->goal_->dcost();
317 node->add_child(this->goal_,
318 (*cost)(node, this->goal_));
319 if (this->collide(node, this->goal_)) {
320 node->children().pop_back();
321 this->goal_->parent(op);
322 this->goal_->ccost(oc);
323 this->goal_->dcost(od);
325 op->rem_child(this->goal_);
334 (*cost)(node, this->goal_));
335 if (this->collide(node, this->goal_)) {
336 node->children().pop_back();
337 this->goal_->remove_parent();
340 this->goal_found_ = true;
347 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
349 std::vector<RRTEdge *> edges;
351 volatile bool col = false;
353 while (tmp != init) {
354 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
355 std::vector<RRTEdge *> bcframe = bc.frame();
356 #pragma omp parallel for reduction(|: col)
357 for (i = 0; i < (*this->cobstacles_).size(); i++) {
358 if ((*this->cobstacles_)[i].collide(tmp)) {
361 for (auto &e: bcframe) {
362 if ((*this->cobstacles_)[i].collide(e)) {
368 for (auto e: bcframe) {
373 for (auto e: edges) {
378 #pragma omp parallel for reduction(|: col)
379 for (i = 0; i < (*this->sobstacles_).size(); i++) {
380 for (auto &e: bcframe) {
381 if ((*this->sobstacles_)[i].collide(e)) {
387 for (auto e: bcframe) {
392 for (auto e: edges) {
397 if (!tmp->parent()) {
400 edges.push_back(new RRTEdge(tmp, tmp->parent()));
402 for (auto e: bcframe) {
408 for (auto &e: edges) {
409 #pragma omp parallel for reduction(|: col)
410 for (i = 0; i < (*this->cobstacles_).size(); i++) {
411 if ((*this->cobstacles_)[i].collide(e)) {
416 for (auto e: edges) {
421 #pragma omp parallel for reduction(|: col)
422 for (i = 0; i < (*this->sobstacles_).size(); i++) {
423 if ((*this->sobstacles_)[i].collide(e)) {
428 for (auto e: edges) {
434 for (auto e: edges) {
440 class RRTNodeDijkstra {
442 RRTNodeDijkstra(int i):
448 RRTNodeDijkstra(int i, float c):
454 RRTNodeDijkstra(int i, int p, float c):
473 class RRTNodeDijkstraComparator {
476 const RRTNodeDijkstra& n1,
477 const RRTNodeDijkstra& n2)
483 bool RRTBase::opt_path()
485 if (this->tlog().size() == 0)
487 float oc = this->tlog().back().front()->ccost();
488 std::vector<RRTNode *> tmp_cusps;
489 for (auto n: this->tlog().back()) {
490 if (sgn(n->s()) == 0) {
491 tmp_cusps.push_back(n);
492 } else if (n->parent() &&
493 sgn(n->s()) != sgn(n->parent()->s())) {
494 tmp_cusps.push_back(n);
495 tmp_cusps.push_back(n->parent());
498 if (tmp_cusps.size() < 2)
500 std::vector<RRTNode *> cusps;
501 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
502 if (tmp_cusps[i] != tmp_cusps[i + 1])
503 cusps.push_back(tmp_cusps[i]);
505 std::reverse(cusps.begin(), cusps.end());
507 std::vector<RRTNodeDijkstra> dnodes;
508 for (unsigned int i = 0; i < cusps.size(); i++)
510 dnodes.push_back(RRTNodeDijkstra(
515 dnodes.push_back(RRTNodeDijkstra(
521 std::vector<RRTNodeDijkstra>,
522 RRTNodeDijkstraComparator> pq;
523 RRTNodeDijkstra tmp = dnodes[0];
525 float ch_cost = 9999;
526 std::vector<RRTNode *> steered;
527 while (!pq.empty() && tmp.ni != cusps.size() - 1) {
530 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
531 ch_cost = dnodes[tmp.ni].c +
532 CO(cusps[tmp.ni], cusps[i]);
533 steered = ST(cusps[tmp.ni], cusps[i]);
534 for (unsigned int j = 0; j < steered.size() - 1; j++)
535 steered[j]->add_child(
540 if (i != tmp.ni + 1 && this->collide( // TODO
542 steered[steered.size() - 1]))
544 if (ch_cost < dnodes[i].c) {
545 dnodes[i].c = ch_cost;
546 dnodes[i].pi = tmp.ni;
552 if (tmp.ni != cusps.size() - 1)
554 std::vector<int> npi; // new path indexes
558 tmpi = dnodes[tmpi].pi;
561 std::reverse(npi.begin(), npi.end());
562 RRTNode *pn = cusps[npi[0]];
563 for (unsigned int i = 0; i < npi.size() - 1; i++) {
564 for (auto ns: ST(cusps[npi[i]], cusps[npi[i + 1]])) {
565 pn->add_child(ns, CO(pn, ns));
570 this->tlog().back().front(),
571 CO(pn, this->tlog().back().front()));
573 if (this->tlog().back().front()->ccost() < oc)
578 bool RRTBase::rebase(RRTNode *nr)
580 if (!nr || this->goal_ == nr || this->root_ == nr)
582 std::vector<RRTNode *> s; // DFS stack
585 unsigned int to_del = 0;
587 s.push_back(this->root_);
588 while (s.size() > 0) {
591 for (auto ch: tmp->children()) {
595 to_del = this->nodes_.size();
596 #pragma omp parallel for reduction(min: to_del)
597 for (i = 0; i < this->nodes_.size(); i++) {
598 if (this->nodes_[i] == tmp)
601 if (to_del < this->nodes_.size())
602 this->nodes_.erase(this->nodes_.begin() + to_del);
605 to_del = this->iy_[iy].size();
606 #pragma omp parallel for reduction(min: to_del)
607 for (i = 0; i < this->iy_[iy].size(); i++) {
608 if (this->iy_[iy][i] == tmp)
611 if (to_del < this->iy_[iy].size())
612 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
614 this->dnodes().push_back(tmp);
617 this->root_->remove_parent();
621 std::vector<RRTNode *> RRTBase::findt()
623 return this->findt(this->goal_);
626 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
628 std::vector<RRTNode *> nodes;
629 if (!n || !n->parent())
633 nodes.push_back(tmp);