Direct Graphical Models  v.1.7.0
KDNode.h
1 // k-D node class for k-D trees
2 // Written by Sergey Kosov in 2017 for Project X
3 #pragma once
4 
5 #include "types.h"
6 
7 namespace DirectGraphicalModels
8 {
9  // ================================ k-D Node Class ================================
17  class CKDNode : public std::enable_shared_from_this<CKDNode>
18  {
19  public:
25  DllExport CKDNode(Mat &key, byte value)
26  : CKDNode(key, value, lvalue_cast(std::make_pair(Mat(), Mat())), 0, 0, nullptr, nullptr) {}
37  DllExport CKDNode(pair_mat_t &boundingBox, byte splitVal, int splitDim, std::shared_ptr<CKDNode> left, std::shared_ptr<CKDNode> right)
38  : CKDNode(EmptyMat, 0, boundingBox, splitVal, splitDim, left, right) {}
39  // Copy constructor
40  DllExport CKDNode(const CKDNode &) = delete;
41  // Destructor
42  DllExport ~CKDNode(void) {};
43  // Assignment operator
44  DllExport bool operator=(const CKDNode) = delete;
45 
50  DllExport void save(FILE *pFile) const;
56  DllExport bool isLeaf(void) const { return (!m_pLeft && !m_pRight); }
65  DllExport void findNearestNeighbors(const Mat &key, size_t maxNeighbors, pair_mat_t &searchBox, float &searchRadius, std::vector<std::shared_ptr<const CKDNode>> &nearestNeighbors) const;
70  DllExport Mat getKey(void) const { return m_key; }
75  DllExport byte getValue(void) const { return m_value; }
80  DllExport pair_mat_t getBoundingBox(void) const { return isLeaf() ? std::make_pair(m_key, m_key) : m_boundingBox; }
86  DllExport byte getSplitVal(void) const { return m_splitVal; }
92  DllExport int getSplitDim(void) const { return m_splitDim; }
97  DllExport std::shared_ptr<CKDNode> Left(void) const { return m_pLeft; }
102  DllExport std::shared_ptr<CKDNode> Right(void) const { return m_pRight; }
103 
104 
105  private:
106  DllExport CKDNode(Mat &key, byte value, pair_mat_t &boundingBox, byte splitVal, int splitDim, std::shared_ptr<CKDNode> left, std::shared_ptr<CKDNode> right);
107 
108 
109  private:
110  Mat m_key;
111  byte m_value;
112  pair_mat_t m_boundingBox;
113  byte m_splitVal;
115  std::shared_ptr<CKDNode> m_pLeft;
116  std::shared_ptr<CKDNode> m_pRight;
117  };
118 
119 }
k-D Node class for the k-D Tree data structure
Definition: KDNode.h:17
pair_mat_t getBoundingBox(void) const
Returns the spatial bounding box, containing all the keys for the current branch. ...
Definition: KDNode.h:80
std::shared_ptr< CKDNode > m_pRight
Definition: KDNode.h:116
Mat getKey(void) const
Returns the key of the leaf-node (k-d point)
Definition: KDNode.h:70
std::shared_ptr< CKDNode > m_pLeft
Definition: KDNode.h:115
CKDNode(Mat &key, byte value)
Leaf node constructor.
Definition: KDNode.h:25
std::shared_ptr< CKDNode > Right(void) const
Returns the pointer to the right child.
Definition: KDNode.h:102
int getSplitDim(void) const
Returns the split dimension of the branch-node.
Definition: KDNode.h:92
STL namespace.
byte getValue(void) const
Returns the value of the leaf-node.
Definition: KDNode.h:75
CKDNode(pair_mat_t &boundingBox, byte splitVal, int splitDim, std::shared_ptr< CKDNode > left, std::shared_ptr< CKDNode > right)
Branch node constructor.
Definition: KDNode.h:37
bool operator=(const CKDNode)=delete
bool isLeaf(void) const
Checks whether the node is either leaf or brach node.
Definition: KDNode.h:56
std::shared_ptr< CKDNode > Left(void) const
Returns the pointer to the left child.
Definition: KDNode.h:97
void findNearestNeighbors(const Mat &key, size_t maxNeighbors, pair_mat_t &searchBox, float &searchRadius, std::vector< std::shared_ptr< const CKDNode >> &nearestNeighbors) const
Auxiliary recursive method for finding the k-nearest (in terms of the Euclidian distance between the ...
Definition: KDNode.cpp:36
byte getSplitVal(void) const
Returns the split value of the brach-node.
Definition: KDNode.h:86
void save(FILE *pFile) const
Saves the state of the node to the file.
Definition: KDNode.cpp:18