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, bool connecting_goal)
61 if (!connecting_goal) {
62 assert(!(std::abs(p.x() - this->x()) < 1e-3
63 && std::abs(p.y() - this->y()) < 1e-3
64 && std::abs(p.h() - this->h()) < 1e-3));
67 this->p_is_cusp(this->would_be_cusp_if_parent(p));
72 RRTNode::p(RRTNode& p)
74 return this->p(p, false);
78 RRTNode::cusp_cnt() const
80 return this->_cusp_cnt;
84 RRTNode::cusp_cnt(RRTNode const& p)
86 this->_cusp_cnt = p.cusp_cnt();
87 if (this->_p_is_cusp) {
95 return this->_segment_type;
101 this->_segment_type = st;
105 RRTNode::p_is_cusp(void) const
107 return this->_p_is_cusp;
111 RRTNode::p_is_cusp(bool isit)
113 this->_p_is_cusp = isit;
117 RRTNode::would_be_cusp_if_parent(RRTNode const& p) const
120 assert(this->sp() != 0);
122 if (sgn(p.p()->sp()) != sgn(this->sp())) {
128 return true; // only root has no parent and it is cusp
131 if (this->sp() == 0) {
132 return false; // this is cusp, not the parent
133 } else if (sgn(p.sp()) != sgn(this->sp())) {
142 RRTNode::operator==(RRTNode const& n)
148 RRTS::recompute_cc_for_predecessors_and(RRTNode* g)
150 assert(this->_path.size() == 0);
151 while (g != nullptr) {
152 this->_path.push_back(g);
155 std::reverse(this->_path.begin(), this->_path.end());
156 for (unsigned int i = 1; i < this->_path.size(); i++) {
157 this->_path[i]->c(this->cost_build(
165 RRTS::recompute_path_cc()
167 this->recompute_cc_for_predecessors_and(&this->_goal);
171 RRTS::min_gamma_eta() const
173 double ns = this->_nodes.size();
174 double gamma = pow(log(ns) / ns, 1.0 / 3.0);
175 return std::min(gamma, this->eta());
179 RRTS::should_continue() const
181 return !this->should_finish();
185 RRTS::join_steered(RRTNode* f)
187 while (this->_steered.size() > 0) {
188 this->store(this->_steered.front());
189 RRTNode* t = &this->_nodes.back();
191 t->c(this->cost_build(*f, *t));
192 this->_steered.erase(this->_steered.begin());
200 RRTNode* f = this->_nn;
201 RRTNode* t = &this->_steered.front();
202 // Require the steer method to return first node equal to nn:
203 assert(std::abs(t->x() - f->x()) < 1e-3);
204 assert(std::abs(t->y() - f->x()) < 1e-3);
205 assert(std::abs(t->h() - f->x()) < 1e-3);
206 this->_steered.erase(this->_steered.begin());
207 t = &this->_steered.front();
209 double cost = f->cc() + this->cost_build(*f, *t);
210 for (auto n: this->nv_) {
211 double nc = n->cc() + this->cost_build(*n, *t);
217 // Check if it's possible to drive from *f to *t. If not, then fallback
218 // to *f = _nn. This could be also solved by additional steer from *f to
219 // *t instead of the following code.
220 this->set_bc_pose_to(*f);
221 if (!this->_bc.drivable(*t)) {
225 this->store(this->_steered.front());
226 t = &this->_nodes.back();
228 t->c(this->cost_build(*f, *t));
229 this->_steered.erase(this->_steered.begin());
236 RRTNode *f = &this->_nodes.back();
237 for (auto n: this->_nv) {
238 double fc = f->cc() + this->cost_build(*f, *n);
239 this->set_bc_pose_to(*f);
240 bool drivable = this->_bc.drivable(*n);
241 if (drivable && fc < n->cc()) {
243 n->c(this->cost_build(*f, *n));
249 RRTS::goal_drivable_from(RRTNode const& f)
251 this->set_bc_pose_to(f);
252 return this->_bc.drivable(this->_goal);
256 RRTS::store(RRTNode n)
258 this->_nodes.push_back(n);
262 RRTS::cost_build(RRTNode const& f, RRTNode const& t) const
268 RRTS::cost_search(RRTNode const& f, RRTNode const& t) const
270 return this->cost_build(f, t);
274 RRTS::find_nn(RRTNode const& t)
276 this->_nn = &this->_nodes.front();
277 this->_cost = this->cost_search(*this->_nn, t);
278 for (auto& f: this->_nodes) {
279 if (this->cost_search(f, t) < this->_cost) {
281 this->_cost = this->cost_search(f, t);
287 RRTS::find_nv(RRTNode const& t)
290 this->_cost = this->min_gamma_eta();
291 for (auto& f: this->_nodes) {
292 if (this->cost_search(f, t) < this->_cost) {
293 this->_nv.push_back(&f);
302 RRTNode *g = &this->_goal;
303 if (g->p() == nullptr) {
306 while (g != nullptr && this->_path.size() < 10000) {
309 * There shouldn't be this->_path.size() < 10000 condition.
310 * However, the RRTS::compute_path() called from
311 * RRTExt13::compute_path tends to re-allocate this->_path
312 * infinitely. There's probably node->p() = &node somewhere...
314 this->_path.push_back(g);
317 std::reverse(this->_path.begin(), this->_path.end());
320 RRTS::RRTS() : _goal(0.0, 0.0, 0.0, 0.0), _gen(std::random_device{}())
322 this->_nodes.reserve(4000000);
323 this->_steered.reserve(1000);
324 this->_path.reserve(10000);
325 this->_nv.reserve(1000);
326 this->store(RRTNode()); // root
330 RRTS::set_bc_pose_to(Pose const& p)
332 this->_bc.set_pose_to(p);
336 RRTS::set_bc_to_become(std::string what)
338 this->_bc.become(what);
342 RRTS::goal(void) const
348 RRTS::goal(double x, double y, double b, double e)
350 this->_goal = RRTGoal(x, y, b, e);
354 RRTS::icnt(void) const
360 RRTS::icnt(unsigned int i)
366 RRTS::icnt_max(void) const
368 return this->_icnt_max;
372 RRTS::icnt_max(unsigned int i)
386 return this->_ter.scnt();
390 RRTS::set_init_pose_to(Pose const& p)
392 this->_nodes.front().x(p.x());
393 this->_nodes.front().y(p.y());
394 this->_nodes.front().h(p.h());
400 std::vector<Pose> path;
401 for (auto n: this->_path) {
402 path.push_back(Pose(n->x(), n->y(), n->h()));
408 RRTS::path_cost() const
410 return this->_goal.cc();
414 RRTS::last_path_cost(void) const
416 if (this->_logged_paths.size() == 0) {
419 assert(this->_logged_paths.back().size() > 0);
420 return this->_logged_paths.back().back().cc();
436 RRTS::json(void) const
439 unsigned int i = 0, j = 0;
440 for (auto path: this->_logged_paths) {
443 jvo["paths"][j][i][0] = n.x();
444 jvo["paths"][j][i][1] = n.y();
445 jvo["paths"][j][i][2] = n.h();
446 jvo["paths"][j][i][3] = n.sp();
447 jvo["paths"][j][i][4] = n.st();
448 jvo["paths"][j][i][5] = n.p_is_cusp();
451 jvo["costs"][j] = path.back().cc();
455 for (auto n: this->_path) {
456 jvo["paths"][j][i][0] = n->x();
457 jvo["paths"][j][i][1] = n->y();
458 jvo["paths"][j][i][2] = n->h();
459 jvo["paths"][j][i][3] = n->sp();
460 jvo["paths"][j][i][4] = n->st();
461 jvo["paths"][j][i][5] = n->p_is_cusp();
462 jvo["path"][i][0] = n->x();
463 jvo["path"][i][1] = n->y();
464 jvo["path"][i][2] = n->h();
465 jvo["path"][i][3] = n->sp();
466 jvo["path"][i][4] = n->st();
467 jvo["path"][i][5] = n->p_is_cusp();
470 jvo["costs"][j] = this->_path.back()->cc();
472 jvo["goal_cc"] = this->_goal.cc(); // TODO remove, use the following
473 jvo["cost"] = this->path_cost();
474 jvo["time"] = this->scnt();
479 RRTS::json(Json::Value jvi)
481 assert(jvi["init"] != Json::nullValue);
482 assert(jvi["goal"] != Json::nullValue);
483 this->set_init_pose_to(Pose(
484 jvi["init"][0].asDouble(),
485 jvi["init"][1].asDouble(),
486 jvi["init"][2].asDouble()));
487 if (jvi["goal"].size() == 4) {
489 jvi["goal"][0].asDouble(),
490 jvi["goal"][1].asDouble(),
491 jvi["goal"][2].asDouble(),
492 jvi["goal"][3].asDouble());
495 jvi["goal"][0].asDouble(),
496 jvi["goal"][1].asDouble(),
497 jvi["goal"][2].asDouble(),
498 jvi["goal"][2].asDouble());
505 if (this->icnt() == 0) {
508 auto rs = this->sample();
509 #if 1 // anytime RRTs
511 double d1 = this->cost_search(this->_nodes.front(), rs);
512 double d2 = this->cost_search(rs, this->_goal);
513 if (this->last_path_cost() != 0.0 && d1 + d2 > this->last_path_cost()) {
514 auto& last_path = this->_logged_paths.back();
515 rs = last_path[rand() % last_path.size()];
520 this->steer(*this->_nn, rs);
521 if (this->collide_steered()) {
522 return this->should_continue();
525 this->find_nv(this->_steered.front());
527 if (!this->connect()) {
528 return this->should_continue();
533 unsigned int ss = this->_steered.size();
534 this->join_steered(&this->_nodes.back());
535 RRTNode* just_added = &this->_nodes.back();
537 while (ss > 0 && just_added != nullptr) {
538 this->steer(*just_added, this->_goal);
539 if (this->collide_steered()) {
541 just_added = just_added->p();
544 // The first of steered is the same as just_added.
545 this->_steered.erase(this->_steered.begin());
546 this->join_steered(just_added);
547 bool gn = this->_goal.edist(this->_nodes.back()) < this->eta();
548 bool gd = this->goal_drivable_from(this->_nodes.back());
550 double nc = this->cost_build(
553 double ncc = this->_nodes.back().cc() + nc;
554 if (this->_goal.p() == nullptr
555 || ncc < this->_goal.cc()) {
556 this->_goal.p(this->_nodes.back(), true);
562 just_added = just_added->p();
565 this->compute_path();
567 this->_time = this->scnt();
568 this->icnt(this->icnt() + 1);
569 return this->should_continue();
575 if (this->path_cost() != 0.0
576 && this->path_cost() < this->last_path_cost()) {
577 this->_logged_paths.push_back(std::vector<RRTNode>());
578 auto& last_path = this->_logged_paths.back();
579 last_path.reserve(this->_path.size());
580 RRTNode* p = nullptr;
581 for (auto n: this->_path) {
582 last_path.push_back(*n);
584 last_path.back().p(*p);
586 p = &last_path.back();
588 // Test that last path cost matches.
589 auto last_path_cost = last_path.back().cc();
590 for (unsigned int i = 1; i < last_path.size(); i++) {
591 last_path[i].c(this->cost_build(
595 assert(last_path_cost == last_path.back().cc());
597 this->_goal = RRTGoal(
603 this->_steered.clear();
604 this->_nodes.erase(this->_nodes.begin() + 1, this->_nodes.end());
607 this->_bc = BicycleCar();