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)
63 this->p_is_cusp(this->would_be_cusp_if_parent(p));
68 RRTNode::cusp_cnt() const
70 return this->_cusp_cnt;
74 RRTNode::cusp_cnt(RRTNode const& p)
76 this->_cusp_cnt = p.cusp_cnt();
77 if (this->_p_is_cusp) {
85 return this->_segment_type;
91 this->_segment_type = st;
95 RRTNode::p_is_cusp(void) const
97 return this->_p_is_cusp;
101 RRTNode::p_is_cusp(bool isit)
103 this->_p_is_cusp = isit;
107 RRTNode::would_be_cusp_if_parent(RRTNode const& p) const
110 assert(this->sp() != 0);
112 if (sgn(p.p()->sp()) != sgn(this->sp())) {
118 return true; // only root has no parent and it is cusp
121 if (this->sp() == 0) {
122 return false; // this is cusp, not the parent
123 } else if (sgn(p.sp()) != sgn(this->sp())) {
132 RRTNode::operator==(RRTNode const& n)
138 RRTS::recompute_cc_for_predecessors_and(RRTNode* g)
140 assert(this->_path.size() == 0);
141 while (g != nullptr) {
142 this->_path.push_back(g);
145 std::reverse(this->_path.begin(), this->_path.end());
146 for (unsigned int i = 1; i < this->_path.size(); i++) {
147 this->_path[i]->c(this->cost_build(
155 RRTS::recompute_path_cc()
157 this->recompute_cc_for_predecessors_and(&this->_goal);
161 RRTS::min_gamma_eta() const
163 double ns = this->_nodes.size();
164 double gamma = pow(log(ns) / ns, 1.0 / 3.0);
165 return std::min(gamma, this->eta());
169 RRTS::should_continue() const
171 return !this->should_finish();
175 RRTS::join_steered(RRTNode* f)
177 while (this->_steered.size() > 0) {
178 this->store(this->_steered.front());
179 RRTNode* t = &this->_nodes.back();
181 t->c(this->cost_build(*f, *t));
182 this->_steered.erase(this->_steered.begin());
190 RRTNode* f = this->_nn;
191 RRTNode* t = &this->_steered.front();
192 // Require the steer method to return first node equal to nn:
193 assert(std::abs(t->x() - f->x()) < 1e-3);
194 assert(std::abs(t->y() - f->x()) < 1e-3);
195 assert(std::abs(t->h() - f->x()) < 1e-3);
196 this->_steered.erase(this->_steered.begin());
197 t = &this->_steered.front();
199 double cost = f->cc() + this->cost_build(*f, *t);
200 for (auto n: this->nv_) {
201 double nc = n->cc() + this->cost_build(*n, *t);
207 // Check if it's possible to drive from *f to *t. If not, then fallback
208 // to *f = _nn. This could be also solved by additional steer from *f to
209 // *t instead of the following code.
210 this->set_bc_pose_to(*f);
211 if (!this->_bc.drivable(*t)) {
215 this->store(this->_steered.front());
216 t = &this->_nodes.back();
218 t->c(this->cost_build(*f, *t));
219 this->_steered.erase(this->_steered.begin());
226 RRTNode *f = &this->_nodes.back();
227 for (auto n: this->_nv) {
228 double fc = f->cc() + this->cost_build(*f, *n);
229 this->set_bc_pose_to(*f);
230 bool drivable = this->_bc.drivable(*n);
231 if (drivable && fc < n->cc()) {
233 n->c(this->cost_build(*f, *n));
239 RRTS::goal_drivable_from(RRTNode const& f)
241 this->set_bc_pose_to(f);
242 return this->_bc.drivable(this->_goal);
246 RRTS::store(RRTNode n)
248 this->_nodes.push_back(n);
252 RRTS::cost_build(RRTNode const& f, RRTNode const& t) const
258 RRTS::cost_search(RRTNode const& f, RRTNode const& t) const
260 return this->cost_build(f, t);
264 RRTS::find_nn(RRTNode const& t)
266 this->_nn = &this->_nodes.front();
267 this->_cost = this->cost_search(*this->_nn, t);
268 for (auto& f: this->_nodes) {
269 if (this->cost_search(f, t) < this->_cost) {
271 this->_cost = this->cost_search(f, t);
277 RRTS::find_nv(RRTNode const& t)
280 this->_cost = this->min_gamma_eta();
281 for (auto& f: this->_nodes) {
282 if (this->cost_search(f, t) < this->_cost) {
283 this->_nv.push_back(&f);
292 RRTNode *g = &this->_goal;
293 if (g->p() == nullptr) {
296 while (g != nullptr && this->_path.size() < 10000) {
299 * There shouldn't be this->_path.size() < 10000 condition.
300 * However, the RRTS::compute_path() called from
301 * RRTExt13::compute_path tends to re-allocate this->_path
302 * infinitely. There's probably node->p() = &node somewhere...
304 this->_path.push_back(g);
307 std::reverse(this->_path.begin(), this->_path.end());
310 RRTS::RRTS() : _goal(0.0, 0.0, 0.0, 0.0), _gen(std::random_device{}())
312 this->_nodes.reserve(4000000);
313 this->_steered.reserve(1000);
314 this->_path.reserve(10000);
315 this->_nv.reserve(1000);
316 this->store(RRTNode()); // root
320 RRTS::set_bc_pose_to(Pose const& p)
322 this->_bc.set_pose_to(p);
326 RRTS::set_bc_to_become(std::string what)
328 this->_bc.become(what);
332 RRTS::goal(void) const
338 RRTS::goal(double x, double y, double b, double e)
340 this->_goal = RRTGoal(x, y, b, e);
344 RRTS::icnt(void) const
350 RRTS::icnt(unsigned int i)
356 RRTS::icnt_max(void) const
358 return this->_icnt_max;
362 RRTS::icnt_max(unsigned int i)
376 return this->_ter.scnt();
380 RRTS::set_init_pose_to(Pose const& p)
382 this->_nodes.front().x(p.x());
383 this->_nodes.front().y(p.y());
384 this->_nodes.front().h(p.h());
390 std::vector<Pose> path;
391 for (auto n: this->_path) {
392 path.push_back(Pose(n->x(), n->y(), n->h()));
398 RRTS::path_cost() const
400 return this->_goal.cc();
404 RRTS::last_path_cost(void) const
406 if (this->_logged_paths.size() == 0) {
409 assert(this->_logged_paths.back().size() > 0);
410 return this->_logged_paths.back().back().cc();
426 RRTS::json(void) const
429 unsigned int i = 0, j = 0;
430 for (auto path: this->_logged_paths) {
433 jvo["paths"][j][i][0] = n.x();
434 jvo["paths"][j][i][1] = n.y();
435 jvo["paths"][j][i][2] = n.h();
436 jvo["paths"][j][i][3] = n.sp();
437 jvo["paths"][j][i][4] = n.st();
438 jvo["paths"][j][i][5] = n.p_is_cusp();
441 jvo["costs"][j] = path.back().cc();
445 for (auto n: this->_path) {
446 jvo["paths"][j][i][0] = n->x();
447 jvo["paths"][j][i][1] = n->y();
448 jvo["paths"][j][i][2] = n->h();
449 jvo["paths"][j][i][3] = n->sp();
450 jvo["paths"][j][i][4] = n->st();
451 jvo["paths"][j][i][5] = n->p_is_cusp();
452 jvo["path"][i][0] = n->x();
453 jvo["path"][i][1] = n->y();
454 jvo["path"][i][2] = n->h();
455 jvo["path"][i][3] = n->sp();
456 jvo["path"][i][4] = n->st();
457 jvo["path"][i][5] = n->p_is_cusp();
460 jvo["costs"][j] = this->_path.back()->cc();
462 jvo["goal_cc"] = this->_goal.cc(); // TODO remove, use the following
463 jvo["cost"] = this->path_cost();
464 jvo["time"] = this->scnt();
469 RRTS::json(Json::Value jvi)
471 assert(jvi["init"] != Json::nullValue);
472 assert(jvi["goal"] != Json::nullValue);
473 this->set_init_pose_to(Pose(
474 jvi["init"][0].asDouble(),
475 jvi["init"][1].asDouble(),
476 jvi["init"][2].asDouble()));
477 if (jvi["goal"].size() == 4) {
479 jvi["goal"][0].asDouble(),
480 jvi["goal"][1].asDouble(),
481 jvi["goal"][2].asDouble(),
482 jvi["goal"][3].asDouble());
485 jvi["goal"][0].asDouble(),
486 jvi["goal"][1].asDouble(),
487 jvi["goal"][2].asDouble(),
488 jvi["goal"][2].asDouble());
495 if (this->icnt() == 0) {
498 auto rs = this->sample();
499 #if 1 // anytime RRTs
501 double d1 = this->cost_search(this->_nodes.front(), rs);
502 double d2 = this->cost_search(rs, this->_goal);
503 if (this->last_path_cost() != 0.0 && d1 + d2 > this->last_path_cost()) {
504 auto& last_path = this->_logged_paths.back();
505 rs = last_path[rand() % last_path.size()];
510 this->steer(*this->_nn, rs);
511 if (this->collide_steered()) {
512 return this->should_continue();
515 this->find_nv(this->_steered.front());
517 if (!this->connect()) {
518 return this->should_continue();
523 unsigned int ss = this->_steered.size();
524 this->join_steered(&this->_nodes.back());
525 RRTNode* just_added = &this->_nodes.back();
527 while (ss > 0 && just_added->p() != nullptr) {
528 this->steer(*just_added, this->_goal);
529 if (this->collide_steered()) {
531 just_added = just_added->p();
534 this->join_steered(just_added);
535 bool gn = this->_goal.edist(this->_nodes.back()) < this->eta();
536 bool gd = this->goal_drivable_from(this->_nodes.back());
538 double nc = this->cost_build(
541 double ncc = this->_nodes.back().cc() + nc;
542 if (this->_goal.p() == nullptr
543 || ncc < this->_goal.cc()) {
544 this->_goal.p(this->_nodes.back());
550 just_added = just_added->p();
553 this->compute_path();
555 this->_time = this->scnt();
556 this->icnt(this->icnt() + 1);
557 return this->should_continue();
563 if (this->path_cost() != 0.0
564 && this->path_cost() < this->last_path_cost()) {
565 this->_logged_paths.push_back(std::vector<RRTNode>());
566 auto& last_path = this->_logged_paths.back();
567 last_path.reserve(this->_path.size());
568 RRTNode* p = nullptr;
569 for (auto n: this->_path) {
570 last_path.push_back(*n);
572 last_path.back().p(*p);
574 p = &last_path.back();
576 // Test that last path cost matches.
577 auto last_path_cost = last_path.back().cc();
578 for (unsigned int i = 1; i < last_path.size(); i++) {
579 last_path[i].c(this->cost_build(
583 assert(last_path_cost == last_path.back().cc());
585 this->_goal = RRTGoal(
591 this->_steered.clear();
592 this->_nodes.erase(this->_nodes.begin() + 1, this->_nodes.end());
595 this->_bc = BicycleCar();