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 float RRTBase::ocost(RRTNode *n)
157 for (auto o: *this->cobstacles_)
158 if (o.dist_to(n) < dist)
160 for (auto o: *this->sobstacles_)
161 if (o.dist_to(n) < dist)
163 return n->ocost(dist);
166 bool RRTBase::tlog(std::vector<RRTNode *> t)
169 this->slog_.push_back(this->elapsed());
170 this->clog_.push_back(t.front()->ccost() - t.back()->ccost());
171 this->nlog_.push_back(this->nodes_.size());
172 this->tlog_.push_back(t);
179 void RRTBase::tstart()
181 this->tstart_ = std::chrono::high_resolution_clock::now();
186 this->tend_ = std::chrono::high_resolution_clock::now();
189 bool RRTBase::link_obstacles(
190 std::vector<CircleObstacle> *cobstacles,
191 std::vector<SegmentObstacle> *sobstacles)
193 this->cobstacles_ = cobstacles;
194 this->sobstacles_ = sobstacles;
195 if (!this->cobstacles_ || !this->sobstacles_) {
201 bool RRTBase::add_iy(RRTNode *n)
208 this->iy_[i].push_back(n);
212 bool RRTBase::goal_found(
214 float (*cost)(RRTNode *, RRTNode* ))
216 float xx = pow(node->x() - this->goal_->x(), 2);
217 float yy = pow(node->y() - this->goal_->y(), 2);
218 float dh = std::abs(node->h() - this->goal_->h());
219 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
220 dh < this->GOAL_FOUND_ANGLE) {
221 if (this->goal_found_) {
222 if (node->ccost() + (*cost)(node, this->goal_) <
223 this->goal_->ccost()) {
224 RRTNode *op; // old parent
225 float oc; // old cumulative cost
226 float od; // old direct cost
227 op = this->goal_->parent();
228 oc = this->goal_->ccost();
229 od = this->goal_->dcost();
230 node->add_child(this->goal_,
231 (*cost)(node, this->goal_));
232 if (this->collide(node, this->goal_)) {
233 node->children().pop_back();
234 this->goal_->parent(op);
235 this->goal_->ccost(oc);
236 this->goal_->dcost(od);
238 op->rem_child(this->goal_);
247 (*cost)(node, this->goal_));
248 if (this->collide(node, this->goal_)) {
249 node->children().pop_back();
250 this->goal_->remove_parent();
253 this->goal_found_ = true;
260 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
262 std::vector<RRTEdge *> edges;
264 volatile bool col = false;
266 while (tmp != init) {
267 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
268 std::vector<RRTEdge *> bcframe = bc.frame();
269 #pragma omp parallel for reduction(|: col)
270 for (i = 0; i < (*this->cobstacles_).size(); i++) {
271 if ((*this->cobstacles_)[i].collide(tmp)) {
274 // TODO collide with car frame
277 for (auto e: bcframe) {
282 for (auto e: edges) {
287 #pragma omp parallel for reduction(|: col)
288 for (i = 0; i < (*this->sobstacles_).size(); i++) {
289 for (auto &e: bcframe) {
290 if ((*this->sobstacles_)[i].collide(e)) {
296 for (auto e: bcframe) {
301 for (auto e: edges) {
306 if (!tmp->parent()) {
309 edges.push_back(new RRTEdge(tmp, tmp->parent()));
311 for (auto e: bcframe) {
317 for (auto &e: edges) {
318 #pragma omp parallel for reduction(|: col)
319 for (i = 0; i < (*this->cobstacles_).size(); i++) {
320 if ((*this->cobstacles_)[i].collide(e)) {
325 for (auto e: edges) {
330 #pragma omp parallel for reduction(|: col)
331 for (i = 0; i < (*this->sobstacles_).size(); i++) {
332 if ((*this->sobstacles_)[i].collide(e)) {
337 for (auto e: edges) {
343 for (auto e: edges) {
349 bool RRTBase::rebase(RRTNode *nr)
351 if (this->goal_ == nr || this->root_ == nr)
353 std::vector<RRTNode *> s; // DFS stack
356 unsigned int to_del = 0;
358 s.push_back(this->root_);
359 while (s.size() > 0) {
362 for (auto ch: tmp->children()) {
366 to_del = this->nodes_.size();
367 #pragma omp parallel for reduction(min: to_del)
368 for (i = 0; i < this->nodes_.size(); i++) {
369 if (this->nodes_[i] == tmp)
372 if (to_del < this->nodes_.size())
373 this->nodes_.erase(this->nodes_.begin() + to_del);
376 to_del = this->iy_[iy].size();
377 #pragma omp parallel for reduction(min: to_del)
378 for (i = 0; i < this->iy_[iy].size(); i++) {
379 if (this->iy_[iy][i] == tmp)
382 if (to_del < this->iy_[iy].size())
383 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
385 this->dnodes().push_back(tmp);
388 this->root_->remove_parent();
392 std::vector<RRTNode *> RRTBase::findt()
394 return this->findt(this->goal_);
397 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
399 std::vector<RRTNode *> nodes;
404 nodes.push_back(tmp);