2 * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
4 * SPDX-License-Identifier: GPL-3.0-only
12 #define USE_RRTS 0 // TODO improve, this solution isn't clear.
20 this->_tstart = std::chrono::high_resolution_clock::now();
26 auto t = std::chrono::high_resolution_clock::now() - this->_tstart;
27 auto d = std::chrono::duration_cast<std::chrono::seconds>(t);
40 assert(this->_p != nullptr);
42 this->_cc = this->_p->cc() + c;
58 RRTNode::p(RRTNode& p)
66 RRTNode::cusp_cnt() const
68 return this->_cusp_cnt;
72 RRTNode::cusp_cnt(RRTNode const& p)
74 this->_cusp_cnt = p.cusp_cnt();
75 if (this->sp() != p.sp() || this->sp() == 0.0) {
83 return this->_segment_type;
89 this->_segment_type = st;
93 RRTNode::operator==(RRTNode const& n)
99 RRTS::recompute_cc_for_predecessors_and(RRTNode* g)
101 assert(this->_path.size() == 0);
102 while (g != nullptr) {
103 this->_path.push_back(g);
106 std::reverse(this->_path.begin(), this->_path.end());
107 for (unsigned int i = 1; i < this->_path.size(); i++) {
108 this->_path[i]->c(this->cost_build(
116 RRTS::recompute_path_cc()
118 this->recompute_cc_for_predecessors_and(&this->_goal);
122 RRTS::min_gamma_eta() const
124 double ns = this->_nodes.size();
125 double gamma = pow(log(ns) / ns, 1.0 / 3.0);
126 return std::min(gamma, this->eta());
130 RRTS::should_continue() const
132 return !this->should_finish();
136 RRTS::join_steered(RRTNode* f)
138 while (this->_steered.size() > 0) {
139 this->store(this->_steered.front());
140 RRTNode* t = &this->_nodes.back();
142 t->c(this->cost_build(*f, *t));
144 this->_steered.erase(this->_steered.begin());
152 RRTNode* f = this->_nn;
153 RRTNode* t = &this->_steered.front();
154 // Require the steer method to return first node equal to nn:
155 assert(std::abs(t->x() - f->x()) < 1e-3);
156 assert(std::abs(t->y() - f->x()) < 1e-3);
157 assert(std::abs(t->h() - f->x()) < 1e-3);
158 // When f and t has different directions, the node (f == t) is cusp:
160 this->_steered.erase(this->_steered.begin());
161 t = &this->_steered.front();
163 double cost = f->cc() + this->cost_build(*f, *t);
164 for (auto n: this->nv_) {
165 double nc = n->cc() + this->cost_build(*n, *t);
171 // Check if it's possible to drive from *f to *t. If not, then fallback
172 // to *f = _nn. This could be also solved by additional steer from *f to
173 // *t instead of the following code.
174 this->set_bc_pose_to(*f);
175 if (!this->_bc.drivable(*t)) {
179 this->store(this->_steered.front());
180 t = &this->_nodes.back();
182 t->c(this->cost_build(*f, *t));
184 this->_steered.erase(this->_steered.begin());
191 RRTNode *f = &this->_nodes.back();
192 for (auto n: this->_nv) {
193 double fc = f->cc() + this->cost_build(*f, *n);
194 this->set_bc_pose_to(*f);
195 bool drivable = this->_bc.drivable(*n);
196 if (drivable && fc < n->cc()) {
198 n->c(this->cost_build(*f, *n));
204 RRTS::goal_drivable_from(RRTNode const& f)
206 this->set_bc_pose_to(f);
207 return this->_bc.drivable(this->_goal);
211 RRTS::store(RRTNode n)
213 this->_nodes.push_back(n);
217 RRTS::cost_build(RRTNode const& f, RRTNode const& t) const
223 RRTS::cost_search(RRTNode const& f, RRTNode const& t) const
225 return this->cost_build(f, t);
229 RRTS::find_nn(RRTNode const& t)
231 this->_nn = &this->_nodes.front();
232 this->_cost = this->cost_search(*this->_nn, t);
233 for (auto& f: this->_nodes) {
234 if (this->cost_search(f, t) < this->_cost) {
236 this->_cost = this->cost_search(f, t);
242 RRTS::find_nv(RRTNode const& t)
245 this->_cost = this->min_gamma_eta();
246 for (auto& f: this->_nodes) {
247 if (this->cost_search(f, t) < this->_cost) {
248 this->_nv.push_back(&f);
257 RRTNode *g = &this->_goal;
258 if (g->p() == nullptr) {
261 while (g != nullptr && this->_path.size() < 10000) {
264 * There shouldn't be this->_path.size() < 10000 condition.
265 * However, the RRTS::compute_path() called from
266 * RRTExt13::compute_path tends to re-allocate this->_path
267 * infinitely. There's probably node->p() = &node somewhere...
269 this->_path.push_back(g);
272 std::reverse(this->_path.begin(), this->_path.end());
275 RRTS::RRTS() : _goal(0.0, 0.0, 0.0, 0.0), _gen(std::random_device{}())
277 this->_nodes.reserve(4000000);
278 this->_steered.reserve(1000);
279 this->_path.reserve(10000);
280 this->_nv.reserve(1000);
281 this->store(RRTNode()); // root
285 RRTS::set_bc_pose_to(Pose const& p)
287 this->_bc.set_pose_to(p);
291 RRTS::set_bc_to_become(std::string what)
293 this->_bc.become(what);
297 RRTS::goal(void) const
303 RRTS::goal(double x, double y, double b, double e)
305 this->_goal = RRTGoal(x, y, b, e);
309 RRTS::icnt(void) const
315 RRTS::icnt(unsigned int i)
321 RRTS::icnt_max(void) const
323 return this->_icnt_max;
327 RRTS::icnt_max(unsigned int i)
341 return this->_ter.scnt();
345 RRTS::set_init_pose_to(Pose const& p)
347 this->_nodes.front().x(p.x());
348 this->_nodes.front().y(p.y());
349 this->_nodes.front().h(p.h());
355 std::vector<Pose> path;
356 for (auto n: this->_path) {
357 path.push_back(Pose(n->x(), n->y(), n->h()));
363 RRTS::path_cost() const
365 return this->_goal.cc();
369 RRTS::last_path_cost(void) const
371 if (this->_logged_paths.size() == 0) {
374 assert(this->_logged_paths.back().size() > 0);
375 return this->_logged_paths.back().back().cc();
391 RRTS::json(void) const
394 unsigned int i = 0, j = 0;
395 for (auto path: this->_logged_paths) {
398 jvo["paths"][j][i][0] = n.x();
399 jvo["paths"][j][i][1] = n.y();
400 jvo["paths"][j][i][2] = n.h();
401 jvo["paths"][j][i][3] = n.sp();
402 jvo["paths"][j][i][4] = n.st();
405 jvo["costs"][j] = path.back().cc();
409 for (auto n: this->_path) {
410 jvo["paths"][j][i][0] = n->x();
411 jvo["paths"][j][i][1] = n->y();
412 jvo["paths"][j][i][2] = n->h();
413 jvo["paths"][j][i][3] = n->sp();
414 jvo["paths"][j][i][4] = n->st();
415 jvo["path"][i][0] = n->x();
416 jvo["path"][i][1] = n->y();
417 jvo["path"][i][2] = n->h();
418 jvo["path"][i][3] = n->sp();
419 jvo["path"][i][4] = n->st();
422 jvo["costs"][j] = this->_path.back()->cc();
424 jvo["goal_cc"] = this->_goal.cc(); // TODO remove, use the following
425 jvo["cost"] = this->path_cost();
426 jvo["time"] = this->scnt();
431 RRTS::json(Json::Value jvi)
433 assert(jvi["init"] != Json::nullValue);
434 assert(jvi["goal"] != Json::nullValue);
435 this->set_init_pose_to(Pose(
436 jvi["init"][0].asDouble(),
437 jvi["init"][1].asDouble(),
438 jvi["init"][2].asDouble()));
439 if (jvi["goal"].size() == 4) {
441 jvi["goal"][0].asDouble(),
442 jvi["goal"][1].asDouble(),
443 jvi["goal"][2].asDouble(),
444 jvi["goal"][3].asDouble());
447 jvi["goal"][0].asDouble(),
448 jvi["goal"][1].asDouble(),
449 jvi["goal"][2].asDouble(),
450 jvi["goal"][2].asDouble());
457 if (this->icnt() == 0) {
460 auto rs = this->sample();
461 #if 1 // anytime RRTs
463 double d1 = this->cost_search(this->_nodes.front(), rs);
464 double d2 = this->cost_search(rs, this->_goal);
465 if (this->last_path_cost() != 0.0 && d1 + d2 > this->last_path_cost()) {
466 auto& last_path = this->_logged_paths.back();
467 rs = last_path[rand() % last_path.size()];
472 this->steer(*this->_nn, rs);
473 if (this->collide_steered()) {
474 return this->should_continue();
477 this->find_nv(this->_steered.front());
479 if (!this->connect()) {
480 return this->should_continue();
485 unsigned int ss = this->_steered.size();
486 this->join_steered(&this->_nodes.back());
487 RRTNode* just_added = &this->_nodes.back();
489 while (ss > 0 && just_added->p() != nullptr) {
490 this->steer(*just_added, this->_goal);
491 if (this->collide_steered()) {
493 just_added = just_added->p();
496 this->join_steered(just_added);
497 bool gn = this->_goal.edist(this->_nodes.back()) < this->eta();
498 bool gd = this->goal_drivable_from(this->_nodes.back());
500 double nc = this->cost_build(
503 double ncc = this->_nodes.back().cc() + nc;
504 if (this->_goal.p() == nullptr
505 || ncc < this->_goal.cc()) {
506 this->_goal.p(this->_nodes.back());
512 just_added = just_added->p();
515 this->compute_path();
517 this->_time = this->scnt();
518 this->icnt(this->icnt() + 1);
519 return this->should_continue();
525 if (this->path_cost() != 0.0
526 && this->path_cost() < this->last_path_cost()) {
527 this->_logged_paths.push_back(std::vector<RRTNode>());
528 auto& last_path = this->_logged_paths.back();
529 last_path.reserve(this->_path.size());
530 RRTNode* p = nullptr;
531 for (auto n: this->_path) {
532 last_path.push_back(*n);
534 last_path.back().p(*p);
536 p = &last_path.back();
538 // Test that last path cost matches.
539 auto last_path_cost = last_path.back().cc();
540 for (unsigned int i = 1; i < last_path.size(); i++) {
541 last_path[i].c(this->cost_build(
545 assert(last_path_cost == last_path.back().cc());
547 this->_goal = RRTGoal(
553 this->_steered.clear();
554 this->_nodes.erase(this->_nodes.begin() + 1, this->_nodes.end());
557 this->_bc = BicycleCar();