]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blobdiff - rrts/src/rrts.cc
Rename set parent of rrt node method argument
[hubacji1/iamcar2.git] / rrts / src / rrts.cc
index 8c200281786ea074f328c53bbac6352bf2645a37..bc87c7f1ada2bab0417d07d7fb028f97d86f3060 100644 (file)
@@ -55,11 +55,23 @@ RRTNode::p() const
 }
 
 void
-RRTNode::p(RRTNode& p)
+RRTNode::p(RRTNode& p, bool can_be_too_close)
 {
-       if (this != &p) {
-               this->_p = &p;
+       assert(this != &p);
+       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));
        }
+       this->_p = &p;
+       this->p_is_cusp(this->would_be_cusp_if_parent(p));
+       this->cusp_cnt(p);
+}
+
+void
+RRTNode::p(RRTNode& p)
+{
+       return this->p(p, false);
 }
 
 unsigned int
@@ -72,7 +84,7 @@ void
 RRTNode::cusp_cnt(RRTNode const& p)
 {
        this->_cusp_cnt = p.cusp_cnt();
-       if (this->sp() != p.sp() || this->sp() == 0.0) {
+       if (this->_p_is_cusp) {
                this->_cusp_cnt++;
        }
 }
@@ -89,6 +101,45 @@ RRTNode::st(int st)
        this->_segment_type = st;
 }
 
+bool
+RRTNode::p_is_cusp(void) const
+{
+       return this->_p_is_cusp;
+}
+
+void
+RRTNode::p_is_cusp(bool isit)
+{
+       this->_p_is_cusp = isit;
+}
+
+bool
+RRTNode::would_be_cusp_if_parent(RRTNode const& p) const
+{
+       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;
+                       } else {
+                               return false;
+                       }
+               } else {
+                       return true; // only root has no parent and it is cusp
+               }
+       } else {
+               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;
+               } else {
+                       return false;
+               }
+       }
+}
+
 bool
 RRTNode::operator==(RRTNode const& n)
 {
@@ -140,7 +191,6 @@ RRTS::join_steered(RRTNode* f)
                RRTNode* t = &this->_nodes.back();
                t->p(*f);
                t->c(this->cost_build(*f, *t));
-               t->cusp_cnt(*f);
                this->_steered.erase(this->_steered.begin());
                f = t;
        }
@@ -152,13 +202,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);
-       // When f and t has different directions, the node (f == t) is cusp:
-       // TODO
+       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_) {
@@ -180,7 +231,6 @@ RRTS::connect()
        t = &this->_nodes.back();
        t->p(*f);
        t->c(this->cost_build(*f, *t));
-       t->cusp_cnt(*f);
        this->_steered.erase(this->_steered.begin());
        return true;
 }
@@ -400,8 +450,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] = 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++;
        }
@@ -412,15 +474,27 @@ 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["path"][i][0] = n->x();
                jvo["path"][i][1] = n->y();
                jvo["path"][i][2] = n->h();
                jvo["path"][i][3] = n->sp();
                jvo["path"][i][4] = n->st();
+               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();
@@ -486,13 +560,15 @@ RRTS::next()
        this->join_steered(&this->_nodes.back());
        RRTNode* just_added = &this->_nodes.back();
        bool gf = false;
-       while (ss > 0 && just_added->p() != nullptr) {
+       while (ss > 0 && just_added != nullptr) {
                this->steer(*just_added, this->_goal);
                if (this->collide_steered()) {
                        ss--;
                        just_added = just_added->p();
                        continue;
                }
+               // The first of steered is the same as just_added.
+               this->_steered.erase(this->_steered.begin());
                this->join_steered(just_added);
                bool gn = this->_goal.edist(this->_nodes.back()) < this->eta();
                bool gd = this->goal_drivable_from(this->_nodes.back());
@@ -503,7 +579,7 @@ RRTS::next()
                        double ncc = this->_nodes.back().cc() + nc;
                        if (this->_goal.p() == nullptr
                                        || ncc < this->_goal.cc()) {
-                               this->_goal.p(this->_nodes.back());
+                               this->_goal.p(this->_nodes.back(), true);
                                this->_goal.c(nc);
                                gf = true;
                        }