]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blobdiff - base/rrtbase.cc
Use nn in RRTBase
[hubacji1/iamcar.git] / base / rrtbase.cc
index e10610b9247547d863ec45354c4570555ab354eb..41197b301a6732c0632f8ac67fccb8c47fe8dec5 100644 (file)
@@ -21,10 +21,14 @@ along with I am car. If not, see <http://www.gnu.org/licenses/>.
 #include <queue>
 #include "bcar.h"
 #include "rrtbase.h"
+
+#if USE_GL > 0
 // OpenGL
 #include <GL/gl.h>
 #include <GL/glu.h>
 #include <SDL2/SDL.h>
+#endif
+
 // RRT
 #include "sample.h"
 #include "cost.h"
@@ -32,11 +36,45 @@ along with I am car. If not, see <http://www.gnu.org/licenses/>.
 #include "nn.h"
 #include "nv.h"
 
+#if USE_GL > 0
 extern SDL_Window* gw;
 extern SDL_GLContext gc;
+#endif
+
+Cell::Cell()
+{
+        pthread_mutex_init(&this->m_, NULL);
+}
+
+bool Cell::changed()
+{
+        pthread_mutex_lock(&this->m_);
+        bool ret = this->changed_;
+        pthread_mutex_unlock(&this->m_);
+        return ret;
+}
+
+std::vector<RRTNode *> Cell::nodes()
+{
+        pthread_mutex_lock(&this->m_);
+        std::vector<RRTNode *> ret(this->nodes_);
+        pthread_mutex_unlock(&this->m_);
+        return ret;
+}
+
+void Cell::add_node(RRTNode *n)
+{
+        pthread_mutex_lock(&this->m_);
+        this->nodes_.push_back(n);
+        this->changed_ = true;
+        pthread_mutex_unlock(&this->m_);
+}
 
 RRTBase::~RRTBase()
 {
+// Fix heap-use-after-free error when T3 planner is used. If only T2 is used,
+// please uncommend the following code:
+//
         for (auto n: this->nodes_)
                 if (n != this->root_)
                         delete n;
@@ -53,20 +91,32 @@ RRTBase::~RRTBase()
         delete this->goal_;
 }
 
-RRTBase::RRTBase():
-        root_(new RRTNode()),
-        goal_(new RRTNode())
+RRTBase::RRTBase()
+        : root_(new RRTNode())
+        , goal_(new RRTNode())
+        , gen_(std::random_device{}())
+        , ndx_(HMAX - HMIN, (HMAX - HMIN) / 4)
+        , ndy_(VMAX - VMIN, (VMAX - VMIN) / 4)
+        , ndh_(0, M_PI * 2 / 4)
 {
+        this->nodes_.reserve(NOFNODES);
         this->nodes_.push_back(this->root_);
         this->add_iy(this->root_);
+        this->add_ixy(this->root_);
 }
 
-RRTBase::RRTBase(RRTNode *init, RRTNode *goal):
-        root_(init),
-        goal_(goal)
+RRTBase::RRTBase(RRTNode *init, RRTNode *goal)
+        : root_(init)
+        , goal_(goal)
+        , gen_(std::random_device{}())
+        , ndx_(HMIN + (HMAX - HMIN) / 2, (HMAX - HMIN) / 4)
+        , ndy_(VMIN + (VMAX - VMIN) / 2, (VMAX - VMIN) / 4)
+        , ndh_(0, M_PI * 2 / 4)
 {
+        this->nodes_.reserve(NOFNODES);
         this->nodes_.push_back(init);
         this->add_iy(init);
+        this->add_ixy(init);
 }
 
 // getter
@@ -80,6 +130,11 @@ RRTNode *RRTBase::goal()
         return this->goal_;
 }
 
+std::vector<RRTNode *> &RRTBase::goals()
+{
+        return this->goals_;
+}
+
 std::vector<RRTNode *> &RRTBase::nodes()
 {
         return this->nodes_;
@@ -90,6 +145,16 @@ std::vector<RRTNode *> &RRTBase::dnodes()
         return this->dnodes_;
 }
 
+std::queue<RRTNode *> &RRTBase::firsts()
+{
+        return this->firsts_;
+}
+
+PolygonObstacle &RRTBase::frame()
+{
+        return this->frame_;
+}
+
 std::vector<RRTNode *> &RRTBase::samples()
 {
         return this->samples_;
@@ -130,6 +195,11 @@ std::vector<std::vector<RRTNode *>> &RRTBase::tlog()
         return this->tlog_;
 }
 
+std::vector<RRTNode *> &RRTBase::slot_cusp()
+{
+        return this->slot_cusp_;
+}
+
 bool RRTBase::goal_found()
 {
         return this->goal_found_;
@@ -143,6 +213,26 @@ float RRTBase::elapsed()
         return dt.count();
 }
 
+std::vector<RRTNode *> RRTBase::traj_cusp()
+{
+        std::vector<RRTNode *> tmp_cusps;
+        for (auto n: this->tlog().back()) {
+                if (sgn(n->s()) == 0) {
+                        tmp_cusps.push_back(n);
+                } else if (n->parent() &&
+                                sgn(n->s()) != sgn(n->parent()->s())) {
+                        tmp_cusps.push_back(n);
+                        tmp_cusps.push_back(n->parent());
+                }
+        }
+        std::vector<RRTNode *> cusps;
+        for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
+                if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
+                        cusps.push_back(tmp_cusps[i]);
+        }
+        return cusps;
+}
+
 // setter
 void RRTBase::root(RRTNode *node)
 {
@@ -154,6 +244,19 @@ void RRTBase::goal(RRTNode *node)
         this->goal_ = node;
 }
 
+void RRTBase::goals(std::vector<RRTNode *> g)
+{
+        this->goals_ = g;
+        std::reverse(this->goals_.begin(), this->goals_.end());
+        RRTNode *pn = this->goals_.front();
+        for (auto n: this->goals_) {
+                if (n != pn) {
+                        pn->add_child(n, this->cost(pn ,n));
+                        pn = n;
+                }
+        }
+}
+
 bool RRTBase::logr(RRTNode *root)
 {
         std::vector<RRTEdge *> e; // Edges to log
@@ -236,15 +339,40 @@ bool RRTBase::add_iy(RRTNode *n)
         return true;
 }
 
+bool RRTBase::add_ixy(RRTNode *n)
+{
+        int ix = IXI(n->x());
+        if (ix < 0)
+                ix = 0;
+        if (ix >= IXSIZE)
+                ix = IXSIZE - 1;
+        int iy = IYI(n->y());
+        if (iy < 0)
+                iy = 0;
+        if (iy >= IYSIZE)
+                iy = IYSIZE - 1;
+        this->ixy_[ix][iy].add_node(n);
+        return true;
+}
+
 bool RRTBase::goal_found(bool f)
 {
         this->goal_found_ = f;
         return f;
 }
 
+void RRTBase::slot_cusp(std::vector<RRTNode *> sc)
+{
+        for (unsigned int i = 0; i < sc.size() - 1; i++)
+                sc[i]->add_child(sc[i + 1], this->cost(sc[i], sc[i + 1]));
+        sc[0]->parent(this->goal());
+        this->slot_cusp_ = sc;
+}
+
 // other
 bool RRTBase::glplot()
 {
+#if USE_GL > 0
         glClear(GL_COLOR_BUFFER_BIT);
         glLineWidth(1);
         glPointSize(1);
@@ -271,12 +399,52 @@ bool RRTBase::glplot()
                 glVertex2f(GLVERTEX(this->samples_.back()));
                 glEnd();
         }
-        // Plot nodes
+        // Plot nodes (position, orientation
         std::vector<RRTNode *> s; // DFS stack
         std::vector<RRTNode *> r; // reset visited_
         RRTNode *tmp;
         glBegin(GL_LINES);
         s.push_back(this->root_);
+        while (s.size() > 0) {
+                tmp = s.back();
+                s.pop_back();
+                if (!tmp->visit()) {
+                        r.push_back(tmp);
+                        for (auto ch: tmp->children()) {
+                                s.push_back(ch);
+                                glColor3f(0.5, 0.5, 0.5);
+                                BicycleCar bc(tmp->x(), tmp->y(), tmp->h());
+                                glVertex2f(
+                                        bc.lfx() * GLPLWSCALE,
+                                        bc.lfy() * GLPLHSCALE
+                                );
+                                glVertex2f(
+                                        bc.lrx() * GLPLWSCALE,
+                                        bc.lry() * GLPLHSCALE
+                                );
+                                glVertex2f(
+                                        bc.lrx() * GLPLWSCALE,
+                                        bc.lry() * GLPLHSCALE
+                                );
+                                glVertex2f(
+                                        bc.rrx() * GLPLWSCALE,
+                                        bc.rry() * GLPLHSCALE
+                                );
+                                glVertex2f(
+                                        bc.rrx() * GLPLWSCALE,
+                                        bc.rry() * GLPLHSCALE
+                                );
+                                glVertex2f(
+                                        bc.rfx() * GLPLWSCALE,
+                                        bc.rfy() * GLPLHSCALE
+                                );
+                        }
+                }
+        }
+        glEnd();
+        // Plot nodes
+        glBegin(GL_LINES);
+        s.push_back(this->root_);
         while (s.size() > 0) {
                 tmp = s.back();
                 s.pop_back();
@@ -335,6 +503,7 @@ bool RRTBase::glplot()
         SDL_GL_SwapWindow(gw);
         for (auto n: r)
                 n->visit(false);
+#endif
         return true;
 }
 
@@ -342,12 +511,9 @@ bool RRTBase::goal_found(
                 RRTNode *node,
                 float (*cost)(RRTNode *, RRTNode* ))
 {
-        float xx = pow(node->x() - this->goal_->x(), 2);
-        float yy = pow(node->y() - this->goal_->y(), 2);
-        float dh = std::abs(node->h() - this->goal_->h());
-        if (IS_NEAR(node, this->goal_)) {
+        if (GOAL_IS_NEAR(node, this->goal_)) {
                 if (this->goal_found_) {
-                        if (node->ccost() + (*cost)(node, this->goal_) <
+                        if (node->ccost() + this->cost(node, this->goal_) <
                                         this->goal_->ccost()) {
                                 RRTNode *op; // old parent
                                 float oc; // old cumulative cost
@@ -356,7 +522,7 @@ bool RRTBase::goal_found(
                                 oc = this->goal_->ccost();
                                 od = this->goal_->dcost();
                                 node->add_child(this->goal_,
-                                                (*cost)(node, this->goal_));
+                                                this->cost(node, this->goal_));
                                 if (this->collide(node, this->goal_)) {
                                         node->children().pop_back();
                                         this->goal_->parent(op);
@@ -372,13 +538,83 @@ bool RRTBase::goal_found(
                 } else {
                         node->add_child(
                                         this->goal_,
-                                        (*cost)(node, this->goal_));
+                                        this->cost(node, this->goal_));
                         if (this->collide(node, this->goal_)) {
                                 node->children().pop_back();
                                 this->goal_->remove_parent();
                                 return false;
                         }
                         this->goal_found_ = true;
+                        // Update ccost of goal's parents
+                        if (this->goals().size() > 0) {
+                                RRTNode *ch = this->goals().back();
+                                RRTNode *pn = this->goals().back()->parent();
+                                while (pn) {
+                                        pn->ccost(
+                                                ch->ccost()
+                                                - this->cost(pn, ch)
+                                        );
+                                        ch = pn;
+                                        pn = pn->parent();
+                                }
+                        }
+                        return true;
+                }
+        }
+        return false;
+}
+
+bool RRTBase::goal_found(
+        RRTNode *node,
+        RRTNode *goal
+)
+{
+        if (GOAL_IS_NEAR(node, goal)) {
+                if (this->goal_found_) {
+                        if (
+                                goal->ccost() != -1
+                                && node->ccost() + this->cost(node, goal)
+                                < goal->ccost()
+                        ) {
+                                RRTNode *op; // old parent
+                                float oc; // old cumulative cost
+                                float od; // old direct cost
+                                op = goal->parent();
+                                oc = goal->ccost();
+                                od = goal->dcost();
+                                node->add_child(goal,
+                                                this->cost(node, goal));
+                                if (this->collide(node, goal)) {
+                                        node->children().pop_back();
+                                        goal->parent(op);
+                                        goal->ccost(oc);
+                                        goal->dcost(od);
+                                } else {
+                                        op->rem_child(goal);
+                                        return true;
+                                }
+                        } else {
+                                return false;
+                        }
+                } else {
+                        node->add_child(
+                                goal,
+                                this->cost(node, goal)
+                        );
+                        if (this->collide(node, goal)) {
+                                node->children().pop_back();
+                                goal->remove_parent();
+                                return false;
+                        }
+                        this->goal_found_ = true;
+                        // Update ccost of goal's children
+                        goal->update_ccost();
+                        // Update ccost of goals
+                        for (auto g: this->goals()) {
+                                if (g == goal)
+                                        break;
+                                g->ccost(-1);
+                        }
                         return true;
                 }
         }
@@ -552,12 +788,17 @@ bool RRTBase::optp_dijkstra(
                         ch_cost = dnodes[tmp.ni].c +
                                 this->cost(cusps[tmp.ni], cusps[i]);
                         steered = this->steer(cusps[tmp.ni], cusps[i]);
+                        if (steered.size() <= 0)
+                                break;
                         for (unsigned int j = 0; j < steered.size() - 1; j++)
                                 steered[j]->add_child(steered[j + 1], 1);
                         if (this->collide(
                                         steered[0],
-                                        steered[steered.size() - 1]))
+                                        steered[steered.size() - 1])) {
+                                for (auto n: steered)
+                                        delete n;
                                 continue;
+                        }
                         if (ch_cost < dnodes[i].c) {
                                 dnodes[i].c = ch_cost;
                                 dnodes[i].pi = tmp.ni;
@@ -684,7 +925,10 @@ bool RRTBase::opt_path()
                 return false;
         std::vector<RRTNode *> cusps;
         for (unsigned int i = 0; i < tmp_cusps.size(); i++) {
-                if (tmp_cusps[i] != tmp_cusps[(i + 1) % tmp_cusps.size()])
+                if (!IS_NEAR(
+                        tmp_cusps[i],
+                        tmp_cusps[(i + 1) % tmp_cusps.size()]
+                ))
                         cusps.push_back(tmp_cusps[i]);
         }
         std::reverse(cusps.begin(), cusps.end());
@@ -715,7 +959,12 @@ bool RRTBase::opt_path()
                                         cusps[npi[i + 1]],
                                         this->cost(pn, cusps[npi[i + 1]]));
                                 en_add = false;
+                        } else if (IS_NEAR(pn, ns)) {
+                                delete ns;
                         } else {
+                                this->nodes().push_back(ns);
+                                this->add_iy(ns);
+                                this->add_ixy(ns);
                                 pn->add_child(ns, this->cost(pn, ns));
                                 pn = ns;
                         }
@@ -786,9 +1035,19 @@ std::vector<RRTNode *> RRTBase::findt(RRTNode *n)
 }
 
 // RRT Framework
+void RRTBase::setSamplingInfo(SamplingInfo si)
+{
+        this->ndx_ = std::normal_distribution<float>(si.x0, si.x);
+        this->ndy_ = std::normal_distribution<float>(si.y0, si.y);
+        this->ndh_ = std::normal_distribution<float>(si.h0, si.h);
+}
+
 RRTNode *RRTBase::sample()
 {
-        return sa1();
+        float x = this->ndx_(this->gen_);
+        float y = this->ndy_(this->gen_);
+        float h = this->ndh_(this->gen_);
+        return new RRTNode(x, y, h);
 }
 
 float RRTBase::cost(RRTNode *init, RRTNode *goal)
@@ -798,8 +1057,39 @@ float RRTBase::cost(RRTNode *init, RRTNode *goal)
 
 RRTNode *RRTBase::nn(RRTNode *rs)
 {
-        return nn4(this->iy_, rs, nullptr);
-        //return nn3(this->iy_, rs, nullptr);
+        int iy = IYI(rs->y());
+        struct mcnn nn;
+        nn.nn = nullptr;
+        nn.mc = 9999;
+        unsigned int i = 0; // vector step
+        unsigned int j = 0; // array step
+        int iyj = 0;
+        while (nn.mc > j * IYSTEP) {
+                iyj = (int) (iy + j);
+                if (iyj >= IYSIZE)
+                        iyj = IYSIZE - 1;
+                #pragma omp parallel for reduction(minn: nn)
+                for (i = 0; i < this->iy_[iyj].size(); i++) {
+                        if (EDIST(this->iy_[iyj][i], rs) < nn.mc) {
+                                nn.mc = EDIST(this->iy_[iyj][i], rs);
+                                nn.nn = this->iy_[iyj][i];
+                        }
+                }
+                if (j > 0) {
+                        iyj = (int) (iy - j);
+                        if (iyj < 0)
+                                iyj = 0;
+                        #pragma omp parallel for reduction(minn: nn)
+                        for (i = 0; i < this->iy_[iyj].size(); i++) {
+                                if (EDIST(this->iy_[iyj][i], rs) < nn.mc) {
+                                        nn.mc = EDIST(this->iy_[iyj][i], rs);
+                                        nn.nn = this->iy_[iyj][i];
+                                }
+                        }
+                }
+                j++;
+        }
+        return nn.nn;
 }
 
 std::vector<RRTNode *> RRTBase::nv(RRTNode *node, float dist)
@@ -831,3 +1121,8 @@ std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal)
 {
         return st3(init, goal);
 }
+
+std::vector<RRTNode *> RRTBase::steer(RRTNode *init, RRTNode *goal, float step)
+{
+        return st3(init, goal, step);
+}