7 CKDNode::CKDNode(Mat &key, byte value, pair_mat_t &boundingBox, byte splitVal,
int splitDim, std::shared_ptr<CKDNode> left, std::shared_ptr<CKDNode> right)
9 , m_boundingBox(boundingBox)
10 , m_splitVal(splitVal)
11 , m_splitDim(splitDim)
21 fwrite(&_isLeaf,
sizeof(byte), 1, pFile);
23 fwrite(
m_key.data,
sizeof(byte),
m_key.cols, pFile);
24 fwrite(&
m_value,
sizeof(byte), 1, pFile);
36 void CKDNode::findNearestNeighbors(
const Mat &key,
size_t maxNeighbors, pair_mat_t &searchBox,
float &searchRadius, std::vector<std::shared_ptr<const CKDNode>> &nearestNeighbors)
const 39 float distance = mathop::Euclidian<byte, float>(key,
m_key) + 0.5f;
40 if (distance < searchRadius) {
41 if (nearestNeighbors.size() < maxNeighbors) nearestNeighbors.push_back(shared_from_this());
42 else nearestNeighbors.back() = shared_from_this();
45 float distTo_i = mathop::Euclidian<byte, float>(key, nearestNeighbors.back()->m_key);
46 for (
size_t i = nearestNeighbors.size() - 1; i > 0; i--) {
47 float distTo_p = mathop::Euclidian<byte, float>(key, nearestNeighbors[i - 1]->m_key);
48 if (distTo_p <= distTo_i)
break;
49 std::swap(nearestNeighbors[i - 1], nearestNeighbors[i]);
52 if (nearestNeighbors.size() == maxNeighbors) {
53 searchRadius = (maxNeighbors == 1) ? distance : mathop::Euclidian<byte, float>(key, nearestNeighbors.back()->m_key) + 0.5f;
54 searchBox.first = key - searchRadius;
55 searchBox.second = key + searchRadius;
60 if (mathop::ifOverlap<byte>(
m_pLeft->m_boundingBox, searchBox))
61 m_pLeft->findNearestNeighbors(key, maxNeighbors, searchBox, searchRadius, nearestNeighbors);
63 if (mathop::ifOverlap<byte>(
m_pRight->m_boundingBox, searchBox))
64 m_pRight->findNearestNeighbors(key, maxNeighbors, searchBox, searchRadius, nearestNeighbors);
std::shared_ptr< CKDNode > m_pRight
std::shared_ptr< CKDNode > m_pLeft
CKDNode(Mat &key, byte value)
Leaf node constructor.
bool isLeaf(void) const
Checks whether the node is either leaf or brach node.
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 ...
void save(FILE *pFile) const
Saves the state of the node to the file.