]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blobdiff - rrts/src/rrts.cc
Use local variable for recompute cc method
[hubacji1/iamcar2.git] / rrts / src / rrts.cc
index 85bd8ed043210fd2558e387556911d17d7e4b2b7..5a142174de1babc904991b1b15d0b680cd29e60f 100644 (file)
@@ -55,10 +55,10 @@ RRTNode::p() const
 }
 
 void
-RRTNode::p(RRTNode& p, bool connecting_goal)
+RRTNode::p(RRTNode& p, bool can_be_too_close)
 {
        assert(this != &p);
-       if (!connecting_goal) {
+       if (!can_be_too_close) {
                assert(!(std::abs(p.x() - this->x()) < 1e-3
                        && std::abs(p.y() - this->y()) < 1e-3
                        && std::abs(p.h() - this->h()) < 1e-3));
@@ -116,8 +116,10 @@ RRTNode::p_is_cusp(bool isit)
 bool
 RRTNode::would_be_cusp_if_parent(RRTNode const& p) const
 {
-       if (p.sp() == 0) {
-               assert(this->sp() != 0);
+       if (std::abs(p.sp()) < 1e-3) {
+               // It should not be possible to have two zero speed nodes next
+               // to each other. In practice, this sometimes happens.
+               //assert(std::abs(this->sp()) > 1e-3);
                if (p.p()) {
                        if (sgn(p.p()->sp()) != sgn(this->sp())) {
                                return true;
@@ -128,7 +130,7 @@ RRTNode::would_be_cusp_if_parent(RRTNode const& p) const
                        return true; // only root has no parent and it is cusp
                }
        } else {
-               if (this->sp() == 0) {
+               if (std::abs(this->sp()) < 1e-3) {
                        return false; // this is cusp, not the parent
                } else if (sgn(p.sp()) != sgn(this->sp())) {
                        return true;
@@ -147,18 +149,16 @@ RRTNode::operator==(RRTNode const& n)
 void
 RRTS::recompute_cc_for_predecessors_and(RRTNode* g)
 {
-       assert(this->_path.size() == 0);
+       std::vector<RRTNode*> path;
+       path.reserve(this->_path.size());
        while (g != nullptr) {
-               this->_path.push_back(g);
+               path.push_back(g);
                g = g->p();
        }
-       std::reverse(this->_path.begin(), this->_path.end());
-       for (unsigned int i = 1; i < this->_path.size(); i++) {
-               this->_path[i]->c(this->cost_build(
-                       *this->_path[i - 1],
-                       *this->_path[i]));
+       std::reverse(path.begin(), path.end());
+       for (unsigned int i = 1; i < path.size(); i++) {
+               path[i]->c(this->cost_build(*path[i - 1], *path[i]));
        }
-       this->_path.clear();
 }
 
 void
@@ -200,11 +200,14 @@ RRTS::connect()
        RRTNode* f = this->_nn;
        RRTNode* t = &this->_steered.front();
        // Require the steer method to return first node equal to nn:
-       assert(std::abs(t->x() - f->x()) < 1e-3);
-       assert(std::abs(t->y() - f->x()) < 1e-3);
-       assert(std::abs(t->h() - f->x()) < 1e-3);
+       assert(std::abs(t->x() - f->x()) < 1e-3
+               && std::abs(t->y() - f->y()) < 1e-3
+               && std::abs(t->h() - f->h()) < 1e-3);
        this->_steered.erase(this->_steered.begin());
        t = &this->_steered.front();
+       assert(!(std::abs(t->x() - f->x()) < 1e-3
+               && std::abs(t->y() - f->y()) < 1e-3
+               && std::abs(t->h() - f->h()) < 1e-3));
 #if USE_RRTS
        double cost = f->cc() + this->cost_build(*f, *t);
        for (auto n: this->nv_) {
@@ -445,9 +448,20 @@ RRTS::json(void) const
                        jvo["paths"][j][i][2] = n.h();
                        jvo["paths"][j][i][3] = n.sp();
                        jvo["paths"][j][i][4] = n.st();
-                       jvo["paths"][j][i][5] = n.p_is_cusp();
+                       jvo["paths"][j][i][5] = false;
+                       if (n.p_is_cusp()) {
+                               assert(i > 0);
+                               jvo["paths"][j][i - 1][5] = true;
+                       }
                        i++;
                }
+               // Initial point is part of the first segment.
+               jvo["paths"][j][0][3] = jvo["paths"][j][1][3];
+               jvo["paths"][j][0][4] = jvo["paths"][j][1][4];
+               // Goal point is part of the last segment.
+               jvo["paths"][j][i - 1][3] = jvo["paths"][j][i - 2][3];
+               jvo["paths"][j][i - 1][4] = jvo["paths"][j][i - 2][4];
+               // --
                jvo["costs"][j] = path.back().cc();
                j++;
        }
@@ -464,11 +478,21 @@ RRTS::json(void) const
                jvo["path"][i][2] = n->h();
                jvo["path"][i][3] = n->sp();
                jvo["path"][i][4] = n->st();
-               jvo["path"][i][5] = n->p_is_cusp();
+               jvo["path"][i][5] = false;
+               if (n->p_is_cusp()) {
+                       assert(i > 0);
+                       jvo["path"][i - 1][5] = true;
+               }
                i++;
        }
+       // Initial point is part of the first segment.
+       jvo["path"][0][3] = jvo["path"][1][3];
+       jvo["path"][0][4] = jvo["path"][1][4];
+       // Goal point is part of the last segment.
+       jvo["path"][i - 1][3] = jvo["path"][i - 2][3];
+       jvo["path"][i - 1][4] = jvo["path"][i - 2][4];
+       // --
        jvo["costs"][j] = this->_path.back()->cc();
-       j++;
        jvo["goal_cc"] = this->_goal.cc(); // TODO remove, use the following
        jvo["cost"] = this->path_cost();
        jvo["time"] = this->scnt();