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 (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
308 dh < this->GOAL_FOUND_ANGLE) {
309 if (this->goal_found_) {
310 if (node->ccost() + (*cost)(node, this->goal_) <
311 this->goal_->ccost()) {
312 RRTNode *op; // old parent
313 float oc; // old cumulative cost
314 float od; // old direct cost
315 op = this->goal_->parent();
316 oc = this->goal_->ccost();
317 od = this->goal_->dcost();
318 node->add_child(this->goal_,
319 (*cost)(node, this->goal_));
320 if (this->collide(node, this->goal_)) {
321 node->children().pop_back();
322 this->goal_->parent(op);
323 this->goal_->ccost(oc);
324 this->goal_->dcost(od);
326 op->rem_child(this->goal_);
335 (*cost)(node, this->goal_));
336 if (this->collide(node, this->goal_)) {
337 node->children().pop_back();
338 this->goal_->remove_parent();
341 this->goal_found_ = true;
348 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
350 std::vector<RRTEdge *> edges;
352 volatile bool col = false;
354 while (tmp != init) {
355 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
356 std::vector<RRTEdge *> bcframe = bc.frame();
357 #pragma omp parallel for reduction(|: col)
358 for (i = 0; i < (*this->cobstacles_).size(); i++) {
359 if ((*this->cobstacles_)[i].collide(tmp)) {
362 // TODO collide with car frame
365 for (auto e: bcframe) {
370 for (auto e: edges) {
375 #pragma omp parallel for reduction(|: col)
376 for (i = 0; i < (*this->sobstacles_).size(); i++) {
377 for (auto &e: bcframe) {
378 if ((*this->sobstacles_)[i].collide(e)) {
384 for (auto e: bcframe) {
389 for (auto e: edges) {
394 if (!tmp->parent()) {
397 edges.push_back(new RRTEdge(tmp, tmp->parent()));
399 for (auto e: bcframe) {
405 for (auto &e: edges) {
406 #pragma omp parallel for reduction(|: col)
407 for (i = 0; i < (*this->cobstacles_).size(); i++) {
408 if ((*this->cobstacles_)[i].collide(e)) {
413 for (auto e: edges) {
418 #pragma omp parallel for reduction(|: col)
419 for (i = 0; i < (*this->sobstacles_).size(); i++) {
420 if ((*this->sobstacles_)[i].collide(e)) {
425 for (auto e: edges) {
431 for (auto e: edges) {
437 class RRTNodeDijkstra {
439 RRTNodeDijkstra(int i):
445 RRTNodeDijkstra(int i, float c):
451 RRTNodeDijkstra(int i, int p, float c):
470 class RRTNodeDijkstraComparator {
473 const RRTNodeDijkstra& n1,
474 const RRTNodeDijkstra& n2)
480 bool RRTBase::opt_path()
482 if (this->tlog().size() == 0)
484 float oc = this->tlog().back().front()->ccost();
485 std::vector<RRTNode *> tmp_cusps;
486 for (auto n: this->tlog().back()) {
487 if (sgn(n->s()) == 0) {
488 tmp_cusps.push_back(n);
489 } else if (n->parent() &&
490 sgn(n->s()) != sgn(n->parent()->s())) {
491 tmp_cusps.push_back(n);
492 tmp_cusps.push_back(n->parent());
495 if (tmp_cusps.size() < 2)
497 std::vector<RRTNode *> cusps;
498 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
499 if (tmp_cusps[i] != tmp_cusps[i + 1])
500 cusps.push_back(tmp_cusps[i]);
502 std::reverse(cusps.begin(), cusps.end());
504 std::vector<RRTNodeDijkstra> dnodes;
505 for (unsigned int i = 0; i < cusps.size(); i++)
507 dnodes.push_back(RRTNodeDijkstra(
512 dnodes.push_back(RRTNodeDijkstra(
518 std::vector<RRTNodeDijkstra>,
519 RRTNodeDijkstraComparator> pq;
520 RRTNodeDijkstra tmp = dnodes[0];
522 float ch_cost = 9999;
523 std::vector<RRTNode *> steered;
524 while (!pq.empty() && tmp.ni != cusps.size() - 1) {
527 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
528 ch_cost = dnodes[tmp.ni].c +
529 CO(cusps[tmp.ni], cusps[i]);
530 steered = ST(cusps[tmp.ni], cusps[i]);
531 for (unsigned int j = 0; j < steered.size() - 1; j++)
532 steered[j]->add_child(
537 if (i != tmp.ni + 1 && this->collide( // TODO
539 steered[steered.size() - 1]))
541 if (ch_cost < dnodes[i].c) {
542 dnodes[i].c = ch_cost;
543 dnodes[i].pi = tmp.ni;
549 if (tmp.ni != cusps.size() - 1)
551 std::vector<int> npi; // new path indexes
555 tmpi = dnodes[tmpi].pi;
558 std::reverse(npi.begin(), npi.end());
559 RRTNode *pn = cusps[npi[0]];
560 for (unsigned int i = 0; i < npi.size() - 1; i++) {
561 for (auto ns: ST(cusps[npi[i]], cusps[npi[i + 1]])) {
562 pn->add_child(ns, CO(pn, ns));
567 this->tlog().back().front(),
568 CO(pn, this->tlog().back().front()));
570 if (this->tlog().back().front()->ccost() < oc)
575 bool RRTBase::rebase(RRTNode *nr)
577 if (!nr || this->goal_ == nr || this->root_ == nr)
579 std::vector<RRTNode *> s; // DFS stack
582 unsigned int to_del = 0;
584 s.push_back(this->root_);
585 while (s.size() > 0) {
588 for (auto ch: tmp->children()) {
592 to_del = this->nodes_.size();
593 #pragma omp parallel for reduction(min: to_del)
594 for (i = 0; i < this->nodes_.size(); i++) {
595 if (this->nodes_[i] == tmp)
598 if (to_del < this->nodes_.size())
599 this->nodes_.erase(this->nodes_.begin() + to_del);
602 to_del = this->iy_[iy].size();
603 #pragma omp parallel for reduction(min: to_del)
604 for (i = 0; i < this->iy_[iy].size(); i++) {
605 if (this->iy_[iy][i] == tmp)
608 if (to_del < this->iy_[iy].size())
609 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
611 this->dnodes().push_back(tmp);
614 this->root_->remove_parent();
618 std::vector<RRTNode *> RRTBase::findt()
620 return this->findt(this->goal_);
623 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
625 std::vector<RRTNode *> nodes;
626 if (!n || !n->parent())
630 nodes.push_back(tmp);