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/>.
25 for (auto n: this->nodes_)
28 for (auto n: this->dnodes_)
29 if (n != this->root_ && n != this->goal_)
31 for (auto s: this->samples_)
34 for (auto edges: this->rlog_)
45 this->nodes_.push_back(this->root_);
46 this->add_iy(this->root_);
49 RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
53 this->nodes_.push_back(init);
57 RRTNode *RRTBase::root()
62 RRTNode *RRTBase::goal()
67 std::vector<RRTNode *> &RRTBase::nodes()
72 std::vector<RRTNode *> &RRTBase::dnodes()
77 std::vector<RRTNode *> &RRTBase::samples()
79 return this->samples_;
82 std::vector<CircleObstacle> *RRTBase::cos()
84 return this->cobstacles_;
87 std::vector<SegmentObstacle> *RRTBase::sos()
89 return this->sobstacles_;
92 std::vector<float> &RRTBase::clog()
97 std::vector<float> &RRTBase::nlog()
102 std::vector<std::vector<RRTEdge *>> &RRTBase::rlog()
107 std::vector<float> &RRTBase::slog()
112 std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
117 bool RRTBase::goal_found()
119 return this->goal_found_;
122 float RRTBase::elapsed()
124 std::chrono::duration<float> dt;
125 dt = std::chrono::duration_cast<std::chrono::duration<float>>(
126 this->tend_ - this->tstart_);
130 bool RRTBase::logr(RRTNode *root)
132 std::vector<RRTEdge *> e; // Edges to log
133 std::vector<RRTNode *> s; // DFS stack
134 std::vector<RRTNode *> r; // reset visited_
137 while (s.size() > 0) {
142 for (auto ch: tmp->children()) {
144 e.push_back(new RRTEdge(tmp, ch));
150 this->rlog_.push_back(e);
154 bool RRTBase::tlog(std::vector<RRTNode *> t)
156 this->slog_.push_back(this->elapsed());
157 this->clog_.push_back(this->goal_->ccost());
158 this->nlog_.push_back(this->nodes_.size());
159 this->tlog_.push_back(t);
163 void RRTBase::tstart()
165 this->tstart_ = std::chrono::high_resolution_clock::now();
170 this->tend_ = std::chrono::high_resolution_clock::now();
173 bool RRTBase::link_obstacles(
174 std::vector<CircleObstacle> *cobstacles,
175 std::vector<SegmentObstacle> *sobstacles)
177 this->cobstacles_ = cobstacles;
178 this->sobstacles_ = sobstacles;
179 if (!this->cobstacles_ || !this->sobstacles_) {
185 bool RRTBase::add_iy(RRTNode *n)
187 int i = floor(n->y() / IYSTEP);
188 this->iy_[i].push_back(n);
192 bool RRTBase::goal_found(
194 float (*cost)(RRTNode *, RRTNode* ))
196 float xx = pow(node->x() - this->goal_->x(), 2);
197 float yy = pow(node->y() - this->goal_->y(), 2);
198 float dh = std::abs(node->h() - this->goal_->h());
199 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
200 dh < this->GOAL_FOUND_ANGLE) {
201 if (this->goal_found_) {
202 if (node->ccost() + (*cost)(node, this->goal_) <
203 this->goal_->ccost()) {
204 RRTNode *op; // old parent
205 float oc; // old cumulative cost
206 float od; // old direct cost
207 op = this->goal_->parent();
208 oc = this->goal_->ccost();
209 od = this->goal_->dcost();
210 node->add_child(this->goal_,
211 (*cost)(node, this->goal_));
212 if (this->collide(node, this->goal_)) {
213 node->children().pop_back();
214 this->goal_->parent(op);
215 this->goal_->ccost(oc);
216 this->goal_->dcost(od);
218 op->rem_child(this->goal_);
227 (*cost)(node, this->goal_));
228 if (this->collide(node, this->goal_)) {
229 node->children().pop_back();
230 this->goal_->remove_parent();
233 this->goal_found_ = true;
240 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
242 std::vector<RRTEdge *> edges;
244 volatile bool col = false;
246 while (tmp != init) {
247 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
248 std::vector<RRTEdge *> bcframe = bc.frame();
249 #pragma omp parallel for reduction(|: col)
250 for (i = 0; i < (*this->cobstacles_).size(); i++) {
251 if ((*this->cobstacles_)[i].collide(tmp)) {
254 // TODO collide with car frame
257 for (auto e: bcframe) {
262 for (auto e: edges) {
267 #pragma omp parallel for reduction(|: col)
268 for (i = 0; i < (*this->sobstacles_).size(); i++) {
269 for (auto &e: bcframe) {
270 if ((*this->sobstacles_)[i].collide(e)) {
276 for (auto e: bcframe) {
281 for (auto e: edges) {
286 if (!tmp->parent()) {
289 edges.push_back(new RRTEdge(tmp, tmp->parent()));
291 for (auto e: bcframe) {
297 for (auto &e: edges) {
298 #pragma omp parallel for reduction(|: col)
299 for (i = 0; i < (*this->cobstacles_).size(); i++) {
300 if ((*this->cobstacles_)[i].collide(e)) {
305 for (auto e: edges) {
310 #pragma omp parallel for reduction(|: col)
311 for (i = 0; i < (*this->sobstacles_).size(); i++) {
312 if ((*this->sobstacles_)[i].collide(e)) {
317 for (auto e: edges) {
323 for (auto e: edges) {
329 bool RRTBase::rebase(RRTNode *nr)
331 std::vector<RRTNode *> s; // DFS stack
334 unsigned int to_del = 0;
336 s.push_back(this->root_);
337 while (s.size() > 0) {
340 for (auto ch: tmp->children()) {
344 to_del = this->nodes_.size();
345 #pragma omp parallel for reduction(min: to_del)
346 for (i = 0; i < this->nodes_.size(); i++) {
347 if (this->nodes_[i] == tmp)
350 if (to_del < this->nodes_.size())
351 this->nodes_.erase(this->nodes_.begin() + to_del);
352 to_del = this->samples_.size();
353 #pragma omp parallel for reduction(min: to_del)
354 for (i = 0; i < this->samples_.size(); i++) {
355 if (this->samples_[i] == tmp)
358 if (to_del < this->samples_.size())
359 this->samples_.erase(this->nodes_.begin() + to_del);
360 iy = floor(tmp->y() / IYSTEP);
361 to_del = this->iy_[iy].size();
362 #pragma omp parallel for reduction(min: to_del)
363 for (i = 0; i < this->iy_[iy].size(); i++) {
364 if (this->iy_[iy][i] == tmp)
367 if (to_del < this->iy_[iy].size())
368 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
369 this->dnodes().push_back(tmp);
372 this->root_->remove_parent();
376 std::vector<RRTNode *> RRTBase::findt()
378 return this->findt(this->goal_);
381 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
383 std::vector<RRTNode *> nodes;
388 nodes.push_back(tmp);