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/>.
27 extern SDL_Window* gw;
28 extern SDL_GLContext gc;
32 for (auto n: this->nodes_)
35 for (auto n: this->dnodes_)
36 if (n != this->root_ && n != this->goal_)
38 for (auto s: this->samples_)
41 for (auto edges: this->rlog_)
52 this->nodes_.push_back(this->root_);
53 this->add_iy(this->root_);
56 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
60 this->nodes_.push_back(init);
64 RRTNode *RRTBase::root()
69 RRTNode *RRTBase::goal()
74 std::vector<RRTNode *> &RRTBase::nodes()
79 std::vector<RRTNode *> &RRTBase::dnodes()
84 std::vector<RRTNode *> &RRTBase::samples()
86 return this->samples_;
89 std::vector<CircleObstacle> *RRTBase::cos()
91 return this->cobstacles_;
94 std::vector<SegmentObstacle> *RRTBase::sos()
96 return this->sobstacles_;
99 std::vector<float> &RRTBase::clog()
104 std::vector<float> &RRTBase::nlog()
109 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
114 std::vector<float> &RRTBase::slog()
119 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
124 bool RRTBase::goal_found()
126 return this->goal_found_;
129 float RRTBase::elapsed()
131 std::chrono::duration<float> dt;
132 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
133 this->tend_ - this->tstart_);
137 bool RRTBase::logr(RRTNode *root)
139 std::vector<RRTEdge *> e; // Edges to log
140 std::vector<RRTNode *> s; // DFS stack
141 std::vector<RRTNode *> r; // reset visited_
144 while (s.size() > 0) {
149 for (auto ch: tmp->children()) {
151 e.push_back(new RRTEdge(tmp, ch));
157 this->rlog_.push_back(e);
161 float RRTBase::ocost(RRTNode *n)
164 for (auto o: *this->cobstacles_)
165 if (o.dist_to(n) < dist)
167 for (auto o: *this->sobstacles_)
168 if (o.dist_to(n) < dist)
170 return n->ocost(dist);
173 bool RRTBase::tlog(std::vector<RRTNode *> t)
176 this->slog_.push_back(this->elapsed());
177 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
178 this->nlog_.push_back(this->nodes_.size());
179 this->tlog_.push_back(t);
186 void RRTBase::tstart()
188 this->tstart_ = std::chrono::high_resolution_clock::now();
193 this->tend_ = std::chrono::high_resolution_clock::now();
196 bool RRTBase::link_obstacles(
197 std::vector<CircleObstacle> *cobstacles,
198 std::vector<SegmentObstacle> *sobstacles)
200 this->cobstacles_ = cobstacles;
201 this->sobstacles_ = sobstacles;
202 if (!this->cobstacles_ || !this->sobstacles_) {
208 bool RRTBase::add_iy(RRTNode *n)
215 this->iy_[i].push_back(n);
219 bool RRTBase::glplot()
221 glClear(GL_COLOR_BUFFER_BIT);
226 for (auto o: *this->sobstacles_) {
228 glVertex2f(GLVERTEX(o.init()));
229 glVertex2f(GLVERTEX(o.goal()));
236 glVertex2f(GLVERTEX(this->root_));
237 glVertex2f(GLVERTEX(this->goal_));
243 glVertex2f(GLVERTEX(this->samples_.back()));
246 std::vector<RRTNode *> s; // DFS stack
247 std::vector<RRTNode *> r; // reset visited_
250 s.push_back(this->root_);
251 while (s.size() > 0) {
256 for (auto ch: tmp->children()) {
258 glColor3f(0.5, 0.5, 0.5);
259 glVertex2f(GLVERTEX(tmp));
260 glVertex2f(GLVERTEX(ch));
265 std::vector<RRTNode *> cusps;
266 // Plot last trajectory
267 if (this->tlog().size() > 0) {
270 for (auto n: this->tlog().back()) {
273 glVertex2f(GLVERTEX(n));
274 glVertex2f(GLVERTEX(n->parent()));
275 if (sgn(n->s()) != sgn(n->parent()->s()))
284 for (auto n: cusps) {
286 glVertex2f(GLVERTEX(n));
289 SDL_GL_SwapWindow(gw);
295 bool RRTBase::goal_found(
297 float (*cost)(RRTNode *, RRTNode* ))
299 float xx = pow(node->x() - this->goal_->x(), 2);
300 float yy = pow(node->y() - this->goal_->y(), 2);
301 float dh = std::abs(node->h() - this->goal_->h());
302 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
303 dh < this->GOAL_FOUND_ANGLE) {
304 if (this->goal_found_) {
305 if (node->ccost() + (*cost)(node, this->goal_) <
306 this->goal_->ccost()) {
307 RRTNode *op; // old parent
308 float oc; // old cumulative cost
309 float od; // old direct cost
310 op = this->goal_->parent();
311 oc = this->goal_->ccost();
312 od = this->goal_->dcost();
313 node->add_child(this->goal_,
314 (*cost)(node, this->goal_));
315 if (this->collide(node, this->goal_)) {
316 node->children().pop_back();
317 this->goal_->parent(op);
318 this->goal_->ccost(oc);
319 this->goal_->dcost(od);
321 op->rem_child(this->goal_);
330 (*cost)(node, this->goal_));
331 if (this->collide(node, this->goal_)) {
332 node->children().pop_back();
333 this->goal_->remove_parent();
336 this->goal_found_ = true;
343 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
345 std::vector<RRTEdge *> edges;
347 volatile bool col = false;
349 while (tmp != init) {
350 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
351 std::vector<RRTEdge *> bcframe = bc.frame();
352 #pragma omp parallel for reduction(|: col)
353 for (i = 0; i < (*this->cobstacles_).size(); i++) {
354 if ((*this->cobstacles_)[i].collide(tmp)) {
357 // TODO collide with car frame
360 for (auto e: bcframe) {
365 for (auto e: edges) {
370 #pragma omp parallel for reduction(|: col)
371 for (i = 0; i < (*this->sobstacles_).size(); i++) {
372 for (auto &e: bcframe) {
373 if ((*this->sobstacles_)[i].collide(e)) {
379 for (auto e: bcframe) {
384 for (auto e: edges) {
389 if (!tmp->parent()) {
392 edges.push_back(new RRTEdge(tmp, tmp->parent()));
394 for (auto e: bcframe) {
400 for (auto &e: edges) {
401 #pragma omp parallel for reduction(|: col)
402 for (i = 0; i < (*this->cobstacles_).size(); i++) {
403 if ((*this->cobstacles_)[i].collide(e)) {
408 for (auto e: edges) {
413 #pragma omp parallel for reduction(|: col)
414 for (i = 0; i < (*this->sobstacles_).size(); i++) {
415 if ((*this->sobstacles_)[i].collide(e)) {
420 for (auto e: edges) {
426 for (auto e: edges) {
432 bool RRTBase::rebase(RRTNode *nr)
434 if (this->goal_ == nr || this->root_ == nr)
436 std::vector<RRTNode *> s; // DFS stack
439 unsigned int to_del = 0;
441 s.push_back(this->root_);
442 while (s.size() > 0) {
445 for (auto ch: tmp->children()) {
449 to_del = this->nodes_.size();
450 #pragma omp parallel for reduction(min: to_del)
451 for (i = 0; i < this->nodes_.size(); i++) {
452 if (this->nodes_[i] == tmp)
455 if (to_del < this->nodes_.size())
456 this->nodes_.erase(this->nodes_.begin() + to_del);
459 to_del = this->iy_[iy].size();
460 #pragma omp parallel for reduction(min: to_del)
461 for (i = 0; i < this->iy_[iy].size(); i++) {
462 if (this->iy_[iy][i] == tmp)
465 if (to_del < this->iy_[iy].size())
466 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
468 this->dnodes().push_back(tmp);
471 this->root_->remove_parent();
475 std::vector<RRTNode *> RRTBase::findt()
477 return this->findt(this->goal_);
480 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
482 std::vector<RRTNode *> nodes;
487 nodes.push_back(tmp);