]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/commitdiff
Add kd tree skeleton
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Tue, 28 Jan 2020 11:58:32 +0000 (12:58 +0100)
committerJiri Vlasak <hubacji1@fel.cvut.cz>
Wed, 29 Jan 2020 10:58:25 +0000 (11:58 +0100)
CMakeLists.txt
README.md
api/rrtext.h
src/rrtext7.cc [new file with mode: 0644]

index fe6d4b30a60ab3392791b740d24902627a4a50b3..dd0d21dc72b52405252ac53024efc4a8e91dd3ad 100644 (file)
@@ -15,6 +15,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/api)
 
 add_library(rrts SHARED
         src/rrts.cc
+        src/rrtext7.cc
         src/rrtext6.cc
         src/rrtext5.cc
         src/rrtext4.cc
index 2d419c3e64dd0532825442f372378fc148529882..9c5df8d9268b624daf019d0069a2562c11b3f24c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -34,6 +34,7 @@ and upgrades to RRT, *extensions* are declared in `rrtext.h` and implemented in
 
 ## Implemented extensions
 There is a list of implemented extensions and what they include:
+- `rrtext7.cc`: [K-d tree][] for node storage.
 - `rrtext6.cc`: Reeds and Shepp for both -- building and search costs,
 - `rrtext5.cc`: different cost for building (Reeds and Shepp) and searching
   (Euclidean distance),
@@ -44,6 +45,7 @@ There is a list of implemented extensions and what they include:
   (Matej's heuristics).
 
 [cute c2]: https://github.com/RandyGaul/cute_headers/blob/master/cute_c2.h
+[K-d tree]: https://en.wikipedia.org/wiki/K-d_tree
 
 ## Compound extensions
 There is a list of classes with reference to extensions used:
index ee9581096737f8e7eea8ab14f10077db4a3d5327..9d34ae251f872c48670aa0cc612562bc783d44d2 100644 (file)
 #define GRID_MAX_XI ((unsigned int) floor(GRID_WIDTH / GRID)) // min is 0
 #define GRID_MAX_YI ((unsigned int) floor(GRID_HEIGHT / GRID)) // min is 0
 
+// ext7
+#define MAX_DIM 2
+
+/*! \brief Use k-d tree data structure to store RRT nodes.
+
+This approach speeds up the search process for the nearest neighbor and
+the near vertices procedures. This extension implements 2D K-d tree.
+
+\see https://en.wikipedia.org/wiki/K-d_tree
+*/
+class RRTExt7 : public virtual RRTS {
+        private:
+                class KdNode {
+                        private:
+                                RRTNode *node_ = nullptr;
+                                KdNode *left_ = nullptr;
+                                KdNode *right_ = nullptr;
+                        public:
+                                // getter, setter
+                                RRTNode *node() const { return this->node_; }
+                                KdNode *&left() { return this->left_; }
+                                KdNode *&right() { return this->right_; }
+                                KdNode(RRTNode *n);
+                };
+                KdNode *kdroot_ = nullptr;
+                void store_node(RRTNode *n, KdNode *&r, int l);
+        public:
+                void init();
+                void deinit();
+                void store_node(RRTNode n);
+                RRTNode *nn(RRTNode &t);
+                std::vector<RRTNode *> nv(RRTNode &t);
+};
+
 /*! \brief Reeds and Shepp cost for building and search.
 */
 class RRTExt6 : public virtual RRTS {
diff --git a/src/rrtext7.cc b/src/rrtext7.cc
new file mode 100644 (file)
index 0000000..3481034
--- /dev/null
@@ -0,0 +1,50 @@
+#include "rrtext.h"
+
+RRTExt7::KdNode::KdNode(RRTNode *n)
+        : node_(n)
+        , left_(nullptr)
+        , right_(nullptr)
+{
+}
+
+void RRTExt7::store_node(RRTNode *n, KdNode *&r, int l)
+{
+        if (r == nullptr)
+                r = new KdNode(n);
+        else if (l % 2 == 0 && n->x() < r->node()->x())
+                store_node(n, r->left(), l + 1);
+        else if (l % 2 == 0)
+                store_node(n, r->right(), l + 1);
+        else if (l % 2 == 1 && n->y() < r->node()->y())
+                store_node(n, r->left(), l + 1);
+        else
+                store_node(n, r->right(), l + 1);
+}
+
+// API
+void RRTExt7::init()
+{
+}
+
+void RRTExt7::deinit()
+{
+}
+
+void RRTExt7::store_node(RRTNode n)
+{
+        RRTS::store_node(n);
+        RRTNode *sn = &this->nodes().back();
+        this->store_node(sn, this->kdroot_, 0);
+}
+
+RRTNode *RRTExt7::nn(RRTNode &t)
+{
+        RRTNode *nn = this->kdroot_->node();
+        return nn;
+}
+
+std::vector<RRTNode *> RRTExt7::nv(RRTNode &t)
+{
+        std::vector<RRTNode *> nv;
+        return nv;
+}