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 // Plot last trajectory
266 if (this->tlog().size() > 0) {
269 for (auto n: this->tlog().back()) {
272 glVertex2f(GLVERTEX(n));
273 glVertex2f(GLVERTEX(n->parent()));
278 SDL_GL_SwapWindow(gw);
284 bool RRTBase::goal_found(
286 float (*cost)(RRTNode *, RRTNode* ))
288 float xx = pow(node->x() - this->goal_->x(), 2);
289 float yy = pow(node->y() - this->goal_->y(), 2);
290 float dh = std::abs(node->h() - this->goal_->h());
291 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
292 dh < this->GOAL_FOUND_ANGLE) {
293 if (this->goal_found_) {
294 if (node->ccost() + (*cost)(node, this->goal_) <
295 this->goal_->ccost()) {
296 RRTNode *op; // old parent
297 float oc; // old cumulative cost
298 float od; // old direct cost
299 op = this->goal_->parent();
300 oc = this->goal_->ccost();
301 od = this->goal_->dcost();
302 node->add_child(this->goal_,
303 (*cost)(node, this->goal_));
304 if (this->collide(node, this->goal_)) {
305 node->children().pop_back();
306 this->goal_->parent(op);
307 this->goal_->ccost(oc);
308 this->goal_->dcost(od);
310 op->rem_child(this->goal_);
319 (*cost)(node, this->goal_));
320 if (this->collide(node, this->goal_)) {
321 node->children().pop_back();
322 this->goal_->remove_parent();
325 this->goal_found_ = true;
332 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
334 std::vector<RRTEdge *> edges;
336 volatile bool col = false;
338 while (tmp != init) {
339 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
340 std::vector<RRTEdge *> bcframe = bc.frame();
341 #pragma omp parallel for reduction(|: col)
342 for (i = 0; i < (*this->cobstacles_).size(); i++) {
343 if ((*this->cobstacles_)[i].collide(tmp)) {
346 // TODO collide with car frame
349 for (auto e: bcframe) {
354 for (auto e: edges) {
359 #pragma omp parallel for reduction(|: col)
360 for (i = 0; i < (*this->sobstacles_).size(); i++) {
361 for (auto &e: bcframe) {
362 if ((*this->sobstacles_)[i].collide(e)) {
368 for (auto e: bcframe) {
373 for (auto e: edges) {
378 if (!tmp->parent()) {
381 edges.push_back(new RRTEdge(tmp, tmp->parent()));
383 for (auto e: bcframe) {
389 for (auto &e: edges) {
390 #pragma omp parallel for reduction(|: col)
391 for (i = 0; i < (*this->cobstacles_).size(); i++) {
392 if ((*this->cobstacles_)[i].collide(e)) {
397 for (auto e: edges) {
402 #pragma omp parallel for reduction(|: col)
403 for (i = 0; i < (*this->sobstacles_).size(); i++) {
404 if ((*this->sobstacles_)[i].collide(e)) {
409 for (auto e: edges) {
415 for (auto e: edges) {
421 bool RRTBase::rebase(RRTNode *nr)
423 if (this->goal_ == nr || this->root_ == nr)
425 std::vector<RRTNode *> s; // DFS stack
428 unsigned int to_del = 0;
430 s.push_back(this->root_);
431 while (s.size() > 0) {
434 for (auto ch: tmp->children()) {
438 to_del = this->nodes_.size();
439 #pragma omp parallel for reduction(min: to_del)
440 for (i = 0; i < this->nodes_.size(); i++) {
441 if (this->nodes_[i] == tmp)
444 if (to_del < this->nodes_.size())
445 this->nodes_.erase(this->nodes_.begin() + to_del);
448 to_del = this->iy_[iy].size();
449 #pragma omp parallel for reduction(min: to_del)
450 for (i = 0; i < this->iy_[iy].size(); i++) {
451 if (this->iy_[iy][i] == tmp)
454 if (to_del < this->iy_[iy].size())
455 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
457 this->dnodes().push_back(tmp);
460 this->root_->remove_parent();
464 std::vector<RRTNode *> RRTBase::findt()
466 return this->findt(this->goal_);
469 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
471 std::vector<RRTNode *> nodes;
476 nodes.push_back(tmp);