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);
224 for (auto o: *this->sobstacles_) {
226 glVertex2f(GLVERTEX(o.init()));
227 glVertex2f(GLVERTEX(o.goal()));
231 std::vector<RRTNode *> s; // DFS stack
232 std::vector<RRTNode *> r; // reset visited_
235 s.push_back(this->root_);
236 while (s.size() > 0) {
241 for (auto ch: tmp->children()) {
243 glColor3f(0.5, 0.5, 0.5);
244 glVertex2f(GLVERTEX(tmp));
245 glVertex2f(GLVERTEX(ch));
250 SDL_GL_SwapWindow(gw);
256 bool RRTBase::goal_found(
258 float (*cost)(RRTNode *, RRTNode* ))
260 float xx = pow(node->x() - this->goal_->x(), 2);
261 float yy = pow(node->y() - this->goal_->y(), 2);
262 float dh = std::abs(node->h() - this->goal_->h());
263 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
264 dh < this->GOAL_FOUND_ANGLE) {
265 if (this->goal_found_) {
266 if (node->ccost() + (*cost)(node, this->goal_) <
267 this->goal_->ccost()) {
268 RRTNode *op; // old parent
269 float oc; // old cumulative cost
270 float od; // old direct cost
271 op = this->goal_->parent();
272 oc = this->goal_->ccost();
273 od = this->goal_->dcost();
274 node->add_child(this->goal_,
275 (*cost)(node, this->goal_));
276 if (this->collide(node, this->goal_)) {
277 node->children().pop_back();
278 this->goal_->parent(op);
279 this->goal_->ccost(oc);
280 this->goal_->dcost(od);
282 op->rem_child(this->goal_);
291 (*cost)(node, this->goal_));
292 if (this->collide(node, this->goal_)) {
293 node->children().pop_back();
294 this->goal_->remove_parent();
297 this->goal_found_ = true;
304 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
306 std::vector<RRTEdge *> edges;
308 volatile bool col = false;
310 while (tmp != init) {
311 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
312 std::vector<RRTEdge *> bcframe = bc.frame();
313 #pragma omp parallel for reduction(|: col)
314 for (i = 0; i < (*this->cobstacles_).size(); i++) {
315 if ((*this->cobstacles_)[i].collide(tmp)) {
318 // TODO collide with car frame
321 for (auto e: bcframe) {
326 for (auto e: edges) {
331 #pragma omp parallel for reduction(|: col)
332 for (i = 0; i < (*this->sobstacles_).size(); i++) {
333 for (auto &e: bcframe) {
334 if ((*this->sobstacles_)[i].collide(e)) {
340 for (auto e: bcframe) {
345 for (auto e: edges) {
350 if (!tmp->parent()) {
353 edges.push_back(new RRTEdge(tmp, tmp->parent()));
355 for (auto e: bcframe) {
361 for (auto &e: edges) {
362 #pragma omp parallel for reduction(|: col)
363 for (i = 0; i < (*this->cobstacles_).size(); i++) {
364 if ((*this->cobstacles_)[i].collide(e)) {
369 for (auto e: edges) {
374 #pragma omp parallel for reduction(|: col)
375 for (i = 0; i < (*this->sobstacles_).size(); i++) {
376 if ((*this->sobstacles_)[i].collide(e)) {
381 for (auto e: edges) {
387 for (auto e: edges) {
393 bool RRTBase::rebase(RRTNode *nr)
395 if (this->goal_ == nr || this->root_ == nr)
397 std::vector<RRTNode *> s; // DFS stack
400 unsigned int to_del = 0;
402 s.push_back(this->root_);
403 while (s.size() > 0) {
406 for (auto ch: tmp->children()) {
410 to_del = this->nodes_.size();
411 #pragma omp parallel for reduction(min: to_del)
412 for (i = 0; i < this->nodes_.size(); i++) {
413 if (this->nodes_[i] == tmp)
416 if (to_del < this->nodes_.size())
417 this->nodes_.erase(this->nodes_.begin() + to_del);
420 to_del = this->iy_[iy].size();
421 #pragma omp parallel for reduction(min: to_del)
422 for (i = 0; i < this->iy_[iy].size(); i++) {
423 if (this->iy_[iy][i] == tmp)
426 if (to_del < this->iy_[iy].size())
427 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
429 this->dnodes().push_back(tmp);
432 this->root_->remove_parent();
436 std::vector<RRTNode *> RRTBase::findt()
438 return this->findt(this->goal_);
441 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
443 std::vector<RRTNode *> nodes;
448 nodes.push_back(tmp);