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 // Plot nodes (from goal)
293 s.push_back(this->goal_);
294 while (s.size() > 0) {
299 for (auto ch: tmp->children()) {
301 glColor3f(0.5, 0.5, 0.5);
302 glVertex2f(GLVERTEX(tmp));
303 glVertex2f(GLVERTEX(ch));
308 std::vector<RRTNode *> cusps;
309 // Plot last trajectory
310 if (this->tlog().size() > 0) {
313 for (auto n: this->tlog().back()) {
316 glVertex2f(GLVERTEX(n));
317 glVertex2f(GLVERTEX(n->parent()));
318 if (sgn(n->s()) != sgn(n->parent()->s()))
327 for (auto n: cusps) {
329 glVertex2f(GLVERTEX(n));
332 SDL_GL_SwapWindow(gw);
338 bool RRTBase::goal_found(
340 float (*cost)(RRTNode *, RRTNode* ))
342 float xx = pow(node->x() - this->goal_->x(), 2);
343 float yy = pow(node->y() - this->goal_->y(), 2);
344 float dh = std::abs(node->h() - this->goal_->h());
345 if (IS_NEAR(node, this->goal_)) {
346 if (this->goal_found_) {
347 if (node->ccost() + (*cost)(node, this->goal_) <
348 this->goal_->ccost()) {
349 RRTNode *op; // old parent
350 float oc; // old cumulative cost
351 float od; // old direct cost
352 op = this->goal_->parent();
353 oc = this->goal_->ccost();
354 od = this->goal_->dcost();
355 node->add_child(this->goal_,
356 (*cost)(node, this->goal_));
357 if (this->collide(node, this->goal_)) {
358 node->children().pop_back();
359 this->goal_->parent(op);
360 this->goal_->ccost(oc);
361 this->goal_->dcost(od);
363 op->rem_child(this->goal_);
372 (*cost)(node, this->goal_));
373 if (this->collide(node, this->goal_)) {
374 node->children().pop_back();
375 this->goal_->remove_parent();
378 this->goal_found_ = true;
385 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
387 std::vector<RRTEdge *> edges;
389 volatile bool col = false;
391 while (tmp != init) {
392 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
393 std::vector<RRTEdge *> bcframe = bc.frame();
394 #pragma omp parallel for reduction(|: col)
395 for (i = 0; i < (*this->cobstacles_).size(); i++) {
396 if ((*this->cobstacles_)[i].collide(tmp)) {
399 for (auto &e: bcframe) {
400 if ((*this->cobstacles_)[i].collide(e)) {
406 for (auto e: bcframe) {
411 for (auto e: edges) {
416 #pragma omp parallel for reduction(|: col)
417 for (i = 0; i < (*this->sobstacles_).size(); i++) {
418 for (auto &e: bcframe) {
419 if ((*this->sobstacles_)[i].collide(e)) {
425 for (auto e: bcframe) {
430 for (auto e: edges) {
435 if (!tmp->parent()) {
438 edges.push_back(new RRTEdge(tmp, tmp->parent()));
440 for (auto e: bcframe) {
446 for (auto &e: edges) {
447 #pragma omp parallel for reduction(|: col)
448 for (i = 0; i < (*this->cobstacles_).size(); i++) {
449 if ((*this->cobstacles_)[i].collide(e)) {
454 for (auto e: edges) {
459 #pragma omp parallel for reduction(|: col)
460 for (i = 0; i < (*this->sobstacles_).size(); i++) {
461 if ((*this->sobstacles_)[i].collide(e)) {
466 for (auto e: edges) {
472 for (auto e: edges) {
478 class RRTNodeDijkstra {
480 RRTNodeDijkstra(int i):
486 RRTNodeDijkstra(int i, float c):
492 RRTNodeDijkstra(int i, int p, float c):
511 class RRTNodeDijkstraComparator {
514 const RRTNodeDijkstra& n1,
515 const RRTNodeDijkstra& n2)
521 bool RRTBase::opt_path()
523 if (this->tlog().size() == 0)
525 float oc = this->tlog().back().front()->ccost();
526 std::vector<RRTNode *> tmp_cusps;
527 for (auto n: this->tlog().back()) {
528 if (sgn(n->s()) == 0) {
529 tmp_cusps.push_back(n);
530 } else if (n->parent() &&
531 sgn(n->s()) != sgn(n->parent()->s())) {
532 tmp_cusps.push_back(n);
533 tmp_cusps.push_back(n->parent());
536 if (tmp_cusps.size() < 2)
538 std::vector<RRTNode *> cusps;
539 for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
540 if (tmp_cusps[i] != tmp_cusps[i + 1])
541 cusps.push_back(tmp_cusps[i]);
543 std::reverse(cusps.begin(), cusps.end());
545 std::vector<RRTNodeDijkstra> dnodes;
546 for (unsigned int i = 0; i < cusps.size(); i++)
548 dnodes.push_back(RRTNodeDijkstra(
553 dnodes.push_back(RRTNodeDijkstra(
559 std::vector<RRTNodeDijkstra>,
560 RRTNodeDijkstraComparator> pq;
561 RRTNodeDijkstra tmp = dnodes[0];
563 float ch_cost = 9999;
564 std::vector<RRTNode *> steered;
565 while (!pq.empty() && tmp.ni != cusps.size() - 1) {
568 for (unsigned int i = tmp.ni + 1; i < cusps.size(); i++) {
569 ch_cost = dnodes[tmp.ni].c +
570 CO(cusps[tmp.ni], cusps[i]);
571 steered = ST(cusps[tmp.ni], cusps[i]);
572 for (unsigned int j = 0; j < steered.size() - 1; j++)
573 steered[j]->add_child(
578 if (i != tmp.ni + 1 && this->collide( // TODO
580 steered[steered.size() - 1]))
582 if (ch_cost < dnodes[i].c) {
583 dnodes[i].c = ch_cost;
584 dnodes[i].pi = tmp.ni;
590 if (tmp.ni != cusps.size() - 1)
592 std::vector<int> npi; // new path indexes
596 tmpi = dnodes[tmpi].pi;
599 std::reverse(npi.begin(), npi.end());
600 RRTNode *pn = cusps[npi[0]];
601 for (unsigned int i = 0; i < npi.size() - 1; i++) {
602 for (auto ns: ST(cusps[npi[i]], cusps[npi[i + 1]])) {
603 pn->add_child(ns, CO(pn, ns));
608 this->tlog().back().front(),
609 CO(pn, this->tlog().back().front()));
611 if (this->tlog().back().front()->ccost() < oc)
616 bool RRTBase::rebase(RRTNode *nr)
618 if (!nr || this->goal_ == nr || this->root_ == nr)
620 std::vector<RRTNode *> s; // DFS stack
623 unsigned int to_del = 0;
625 s.push_back(this->root_);
626 while (s.size() > 0) {
629 for (auto ch: tmp->children()) {
633 to_del = this->nodes_.size();
634 #pragma omp parallel for reduction(min: to_del)
635 for (i = 0; i < this->nodes_.size(); i++) {
636 if (this->nodes_[i] == tmp)
639 if (to_del < this->nodes_.size())
640 this->nodes_.erase(this->nodes_.begin() + to_del);
643 to_del = this->iy_[iy].size();
644 #pragma omp parallel for reduction(min: to_del)
645 for (i = 0; i < this->iy_[iy].size(); i++) {
646 if (this->iy_[iy][i] == tmp)
649 if (to_del < this->iy_[iy].size())
650 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
652 this->dnodes().push_back(tmp);
655 this->root_->remove_parent();
659 std::vector<RRTNode *> RRTBase::findt()
661 return this->findt(this->goal_);
664 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
666 std::vector<RRTNode *> nodes;
667 if (!n || !n->parent())
670 while (tmp != this->root()) {
671 nodes.push_back(tmp);