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 std::vector<RRTNode *> s; // DFS stack
222 std::vector<RRTNode *> r; // reset visited_
224 glClear(GL_COLOR_BUFFER_BIT);
226 s.push_back(this->root_);
227 while (s.size() > 0) {
232 for (auto ch: tmp->children()) {
234 glColor3f(0.5, 0.5, 0.5);
235 glVertex2f(tmp->x() * GLPLWSCALE,
236 tmp->y() * GLPLHSCALE);
237 glVertex2f(ch->x() * GLPLWSCALE,
238 ch->y() *GLPLHSCALE);
243 SDL_GL_SwapWindow(gw);
249 bool RRTBase::goal_found(
251 float (*cost)(RRTNode *, RRTNode* ))
253 float xx = pow(node->x() - this->goal_->x(), 2);
254 float yy = pow(node->y() - this->goal_->y(), 2);
255 float dh = std::abs(node->h() - this->goal_->h());
256 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
257 dh < this->GOAL_FOUND_ANGLE) {
258 if (this->goal_found_) {
259 if (node->ccost() + (*cost)(node, this->goal_) <
260 this->goal_->ccost()) {
261 RRTNode *op; // old parent
262 float oc; // old cumulative cost
263 float od; // old direct cost
264 op = this->goal_->parent();
265 oc = this->goal_->ccost();
266 od = this->goal_->dcost();
267 node->add_child(this->goal_,
268 (*cost)(node, this->goal_));
269 if (this->collide(node, this->goal_)) {
270 node->children().pop_back();
271 this->goal_->parent(op);
272 this->goal_->ccost(oc);
273 this->goal_->dcost(od);
275 op->rem_child(this->goal_);
284 (*cost)(node, this->goal_));
285 if (this->collide(node, this->goal_)) {
286 node->children().pop_back();
287 this->goal_->remove_parent();
290 this->goal_found_ = true;
297 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
299 std::vector<RRTEdge *> edges;
301 volatile bool col = false;
303 while (tmp != init) {
304 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
305 std::vector<RRTEdge *> bcframe = bc.frame();
306 #pragma omp parallel for reduction(|: col)
307 for (i = 0; i < (*this->cobstacles_).size(); i++) {
308 if ((*this->cobstacles_)[i].collide(tmp)) {
311 // TODO collide with car frame
314 for (auto e: bcframe) {
319 for (auto e: edges) {
324 #pragma omp parallel for reduction(|: col)
325 for (i = 0; i < (*this->sobstacles_).size(); i++) {
326 for (auto &e: bcframe) {
327 if ((*this->sobstacles_)[i].collide(e)) {
333 for (auto e: bcframe) {
338 for (auto e: edges) {
343 if (!tmp->parent()) {
346 edges.push_back(new RRTEdge(tmp, tmp->parent()));
348 for (auto e: bcframe) {
354 for (auto &e: edges) {
355 #pragma omp parallel for reduction(|: col)
356 for (i = 0; i < (*this->cobstacles_).size(); i++) {
357 if ((*this->cobstacles_)[i].collide(e)) {
362 for (auto e: edges) {
367 #pragma omp parallel for reduction(|: col)
368 for (i = 0; i < (*this->sobstacles_).size(); i++) {
369 if ((*this->sobstacles_)[i].collide(e)) {
374 for (auto e: edges) {
380 for (auto e: edges) {
386 bool RRTBase::rebase(RRTNode *nr)
388 if (this->goal_ == nr || this->root_ == nr)
390 std::vector<RRTNode *> s; // DFS stack
393 unsigned int to_del = 0;
395 s.push_back(this->root_);
396 while (s.size() > 0) {
399 for (auto ch: tmp->children()) {
403 to_del = this->nodes_.size();
404 #pragma omp parallel for reduction(min: to_del)
405 for (i = 0; i < this->nodes_.size(); i++) {
406 if (this->nodes_[i] == tmp)
409 if (to_del < this->nodes_.size())
410 this->nodes_.erase(this->nodes_.begin() + to_del);
413 to_del = this->iy_[iy].size();
414 #pragma omp parallel for reduction(min: to_del)
415 for (i = 0; i < this->iy_[iy].size(); i++) {
416 if (this->iy_[iy][i] == tmp)
419 if (to_del < this->iy_[iy].size())
420 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
422 this->dnodes().push_back(tmp);
425 this->root_->remove_parent();
429 std::vector<RRTNode *> RRTBase::findt()
431 return this->findt(this->goal_);
434 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
436 std::vector<RRTNode *> nodes;
441 nodes.push_back(tmp);