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)
168 this->slog_.push_back(this->elapsed());
169 this->clog_.push_back(this->goal_->ccost());
170 this->nlog_.push_back(this->nodes_.size());
171 this->tlog_.push_back(t);
175 void RRTBase::tstart()
177 this->tstart_ = std::chrono::high_resolution_clock::now();
182 this->tend_ = std::chrono::high_resolution_clock::now();
185 bool RRTBase::link_obstacles(
186 std::vector<CircleObstacle> *cobstacles,
187 std::vector<SegmentObstacle> *sobstacles)
189 this->cobstacles_ = cobstacles;
190 this->sobstacles_ = sobstacles;
191 if (!this->cobstacles_ || !this->sobstacles_) {
197 bool RRTBase::add_iy(RRTNode *n)
204 this->iy_[i].push_back(n);
208 bool RRTBase::goal_found(
210 float (*cost)(RRTNode *, RRTNode* ))
212 float xx = pow(node->x() - this->goal_->x(), 2);
213 float yy = pow(node->y() - this->goal_->y(), 2);
214 float dh = std::abs(node->h() - this->goal_->h());
215 if (pow(xx + yy, 0.5) < this->GOAL_FOUND_DISTANCE &&
216 dh < this->GOAL_FOUND_ANGLE) {
217 if (this->goal_found_) {
218 if (node->ccost() + (*cost)(node, this->goal_) <
219 this->goal_->ccost()) {
220 RRTNode *op; // old parent
221 float oc; // old cumulative cost
222 float od; // old direct cost
223 op = this->goal_->parent();
224 oc = this->goal_->ccost();
225 od = this->goal_->dcost();
226 node->add_child(this->goal_,
227 (*cost)(node, this->goal_));
228 if (this->collide(node, this->goal_)) {
229 node->children().pop_back();
230 this->goal_->parent(op);
231 this->goal_->ccost(oc);
232 this->goal_->dcost(od);
234 op->rem_child(this->goal_);
243 (*cost)(node, this->goal_));
244 if (this->collide(node, this->goal_)) {
245 node->children().pop_back();
246 this->goal_->remove_parent();
249 this->goal_found_ = true;
256 bool RRTBase::collide(RRTNode *init, RRTNode *goal)
258 std::vector<RRTEdge *> edges;
260 volatile bool col = false;
262 while (tmp != init) {
263 BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
264 std::vector<RRTEdge *> bcframe = bc.frame();
265 #pragma omp parallel for reduction(|: col)
266 for (i = 0; i < (*this->cobstacles_).size(); i++) {
267 if ((*this->cobstacles_)[i].collide(tmp)) {
270 // TODO collide with car frame
273 for (auto e: bcframe) {
278 for (auto e: edges) {
283 #pragma omp parallel for reduction(|: col)
284 for (i = 0; i < (*this->sobstacles_).size(); i++) {
285 for (auto &e: bcframe) {
286 if ((*this->sobstacles_)[i].collide(e)) {
292 for (auto e: bcframe) {
297 for (auto e: edges) {
302 if (!tmp->parent()) {
305 edges.push_back(new RRTEdge(tmp, tmp->parent()));
307 for (auto e: bcframe) {
313 for (auto &e: edges) {
314 #pragma omp parallel for reduction(|: col)
315 for (i = 0; i < (*this->cobstacles_).size(); i++) {
316 if ((*this->cobstacles_)[i].collide(e)) {
321 for (auto e: edges) {
326 #pragma omp parallel for reduction(|: col)
327 for (i = 0; i < (*this->sobstacles_).size(); i++) {
328 if ((*this->sobstacles_)[i].collide(e)) {
333 for (auto e: edges) {
339 for (auto e: edges) {
345 bool RRTBase::rebase(RRTNode *nr)
347 if (this->goal_ == nr || this->root_ == nr)
349 std::vector<RRTNode *> s; // DFS stack
352 unsigned int to_del = 0;
354 s.push_back(this->root_);
355 while (s.size() > 0) {
358 for (auto ch: tmp->children()) {
362 to_del = this->nodes_.size();
363 #pragma omp parallel for reduction(min: to_del)
364 for (i = 0; i < this->nodes_.size(); i++) {
365 if (this->nodes_[i] == tmp)
368 if (to_del < this->nodes_.size())
369 this->nodes_.erase(this->nodes_.begin() + to_del);
372 to_del = this->iy_[iy].size();
373 #pragma omp parallel for reduction(min: to_del)
374 for (i = 0; i < this->iy_[iy].size(); i++) {
375 if (this->iy_[iy][i] == tmp)
378 if (to_del < this->iy_[iy].size())
379 this->iy_[iy].erase(this->iy_[iy].begin() + to_del);
381 this->dnodes().push_back(tmp);
384 this->root_->remove_parent();
388 std::vector<RRTNode *> RRTBase::findt()
390 return this->findt(this->goal_);
393 std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
395 std::vector<RRTNode *> nodes;
400 nodes.push_back(tmp);