2 * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
4 * SPDX-License-Identifier: GPL-3.0-only
16 this->tstart_ = std::chrono::high_resolution_clock::now();
22 using namespace std::chrono;
23 auto t = high_resolution_clock::now() - this->tstart_;
24 auto d = duration_cast<duration<double>>(t);
37 assert(this->p_ != nullptr);
39 this->cc_ = this->p_->cc() + c;
55 RRTNode::p(RRTNode& p)
63 RRTNode::operator==(RRTNode const& n)
69 RRTS::recompute_cc(RRTNode* g)
72 while (g != nullptr) {
73 this->path_.push_back(g);
76 std::reverse(this->path_.begin(), this->path_.end());
77 for (unsigned int i = 1; i < this->path_.size(); i++) {
78 this->path_[i]->c(this->cost_build(*this->path_[i - 1],
84 RRTS::recompute_path_cc()
86 this->recompute_cc(&this->goal_);
90 RRTS::min_gamma_eta() const
92 double ns = this->nodes_.size();
93 double gamma = pow(log(ns) / ns, 1.0 / 3.0);
94 return std::min(gamma, this->eta_);
98 RRTS::should_continue() const
100 return !this->should_finish();
104 RRTS::join_steered(RRTNode* f)
106 while (this->steered_.size() > 0) {
107 this->store(this->steered_.front());
108 RRTNode* t = &this->nodes_.back();
110 t->c(this->cost_build(*f, *t));
111 this->steered_.erase(this->steered_.begin());
125 RRTNode* f = this->nn_;
126 RRTNode* t = &this->steered_.front();
127 double cost = f->cc() + this->cost_build(*f, *t);
128 for (auto n: this->nv_) {
129 double nc = n->cc() + this->cost_build(*n, *t);
135 // Check if it's possible to drive from *f to *t. If not, then fallback
136 // to *f = nn_. This could be also solved by additional steer from *f to
137 // *t instead of the following code.
138 this->bc_.set_pose(*f);
139 if (!this->bc_.drivable(*t)) {
142 this->store(this->steered_.front());
143 t = &this->nodes_.back();
145 t->c(this->cost_build(*f, *t));
146 this->steered_.erase(this->steered_.begin());
153 RRTNode *f = &this->nodes_.back();
154 for (auto n: this->nv_) {
155 double fc = f->cc() + this->cost_build(*f, *n);
156 this->bc_.set_pose(*f);
157 bool drivable = this->bc_.drivable(*n);
158 if (drivable && fc < n->cc()) {
160 n->c(this->cost_build(*f, *n));
166 RRTS::goal_drivable_from(RRTNode const& f)
168 this->bc_.set_pose(f);
169 return this->bc_.drivable(this->goal_);
173 RRTS::store(RRTNode n)
175 this->nodes_.push_back(n);
179 RRTS::cost_build(RRTNode const& f, RRTNode const& t) const
185 RRTS::cost_search(RRTNode const& f, RRTNode const& t) const
187 return this->cost_build(f, t);
191 RRTS::find_nn(RRTNode const& t)
193 this->nn_ = &this->nodes_.front();
194 this->cost_ = this->cost_search(*this->nn_, t);
195 for (auto& f: this->nodes_) {
196 if (this->cost_search(f, t) < this->cost_) {
198 this->cost_ = this->cost_search(f, t);
204 RRTS::find_nv(RRTNode const& t)
207 this->cost_ = this->min_gamma_eta();
208 for (auto& f: this->nodes_) {
209 if (this->cost_search(f, t) < this->cost_) {
210 this->nv_.push_back(&f);
219 RRTNode *g = &this->goal_;
220 if (g->p() == nullptr) {
223 while (g != nullptr && this->path_.size() < 10000) {
226 * There shouldn't be this->path_.size() < 10000 condition.
227 * However, the RRTS::compute_path() called from
228 * RRTExt13::compute_path tends to re-allocate this->path_
229 * infinitely. There's probably node->p() = &node somewhere...
231 this->path_.push_back(g);
234 std::reverse(this->path_.begin(), this->path_.end());
237 RRTS::RRTS() : gen_(std::random_device{}()), goal_(0.0, 0.0, 0.0, 0.0)
239 this->nodes_.reserve(4000000);
240 this->steered_.reserve(1000);
241 this->path_.reserve(10000);
242 this->nv_.reserve(1000);
243 this->store(RRTNode()); // root
253 RRTS::icnt(unsigned int i)
261 return this->ter_.scnt();
269 for (auto n: this->path_) {
270 jvo["path"][i][0] = n->x();
271 jvo["path"][i][1] = n->y();
272 jvo["path"][i][2] = n->h();
275 jvo["goal_cc"] = this->goal_.cc();
276 jvo["time"] = this->time_;
281 RRTS::json(Json::Value jvi)
283 assert(jvi["init"] != Json::nullValue);
284 assert(jvi["goal"] != Json::nullValue);
285 assert(jvi["obst"] != Json::nullValue);
286 this->nodes_.front().x(jvi["init"][0].asDouble());
287 this->nodes_.front().y(jvi["init"][1].asDouble());
288 this->nodes_.front().h(jvi["init"][2].asDouble());
289 if (jvi["goal"].size() == 4) {
290 this->goal_ = RRTGoal(jvi["goal"][0].asDouble(),
291 jvi["goal"][1].asDouble(),
292 jvi["goal"][2].asDouble(),
293 jvi["goal"][3].asDouble());
295 this->goal_ = RRTGoal(jvi["goal"][0].asDouble(),
296 jvi["goal"][1].asDouble(),
297 jvi["goal"][2].asDouble(),
298 jvi["goal"][2].asDouble());
305 if (this->icnt_ == 0) {
309 auto rs = this->sample();
310 #if 1 // anytime RRTs
312 double d1 = this->cost_search(this->nodes_.front(), rs);
313 double d2 = this->cost_search(rs, this->goal_);
314 if (this->last_goal_cc_ != 0.0 && d1 + d2 > this->last_goal_cc_) {
315 rs = this->last_path_[rand() % this->last_path_.size()];
320 this->steer(this->nn(), rs);
321 if (this->collide_steered()) {
322 return this->should_continue();
324 this->find_nv(this->steered_.front());
325 if (!this->connect()) {
326 return this->should_continue();
329 unsigned int ss = this->steered_.size();
330 this->join_steered(&this->nodes_.back());
331 RRTNode* just_added = &this->nodes_.back();
333 while (ss > 0 && just_added->p() != nullptr) {
334 this->steer(*just_added, this->goal_);
335 if (this->collide_steered()) {
337 just_added = just_added->p();
340 this->join_steered(just_added);
341 bool gn = this->goal_.edist(this->nodes_.back()) < this->eta_;
342 bool gd = this->goal_drivable_from(this->nodes_.back());
344 double nc = this->cost_build(this->nodes_.back(),
346 double ncc = this->nodes_.back().cc() + nc;
347 if (this->goal_.p() == nullptr
348 || ncc < this->goal_.cc()) {
349 this->goal_.p(this->nodes_.back());
355 just_added = just_added->p();
358 this->compute_path();
360 this->time_ = this->ter_.scnt();
361 return this->should_continue();
367 if (this->goal_.cc() != 0.0 && this->goal_.cc() < this->last_goal_cc_) {
368 this->last_goal_cc_ = this->goal_.cc();
369 this->last_path_.clear();
370 for (auto n: this->path_) {
371 this->last_path_.push_back(*n);
374 this->goal_ = RRTGoal(this->goal_.x(), this->goal_.y(), this->goal_.b(),
377 this->steered_.clear();
378 this->nodes_.erase(this->nodes_.begin() + 1, this->nodes_.end());