File indexing completed on 2025-03-09 03:55:01

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam
0004  *
0005  * Date        : 2019-06-08
0006  * Description : Node of KD-Tree for vector space partitioning
0007  *
0008  * SPDX-FileCopyrightText: 2020 by Nghia Duong <minhnghiaduong997 at gmail dot com>
0009  *
0010  * SPDX-License-Identifier: GPL-2.0-or-later
0011  *
0012  * ============================================================ */
0013 
0014 #ifndef DIGIKAM_KD_NODE_H
0015 #define DIGIKAM_KD_NODE_H
0016 
0017 // C++ includes
0018 
0019 #include <vector>
0020 
0021 // Qt includes
0022 
0023 #include <QVector>
0024 #include <QMap>
0025 
0026 // Local include
0027 
0028 #include "digikam_opencv.h"
0029 
0030 namespace Digikam
0031 {
0032 
0033 class KDNode
0034 {
0035 public:
0036 
0037     explicit KDNode(const cv::Mat& nodePos,
0038                     const int      identity,
0039                     int            splitAxis,
0040                     int            dimension);
0041     ~KDNode();
0042 
0043 public:
0044 
0045     static float sqrDistance(const float* const pos1, const float* const pos2, int dimension);
0046     static float cosDistance(const float* const pos1, const float* const pos2, int dimension);
0047 
0048 public:
0049 
0050     /**
0051      * Insert a new node to the sub-tree
0052      */
0053     KDNode* insert(const cv::Mat& nodePos, const int identity);
0054 
0055     /**
0056      * Return position vector of a node
0057      */
0058     cv::Mat getPosition() const;
0059 
0060     /**
0061      * Return a list of closest neighbors, limited by maxNbNeighbors and sqRange
0062      */
0063     double getClosestNeighbors(QMap<double, QVector<int> >& neighborList,
0064                                const cv::Mat&               position,
0065                                float                        sqRange,
0066                                float                        cosThreshold,
0067                                int                          maxNbNeighbors) const;
0068     /**
0069      * Return identity of the node
0070      */
0071     int getIdentity();
0072 
0073     /**
0074      * Set database entry ID of the node
0075      */
0076     void setNodeId(int id);
0077 
0078 private:
0079 
0080     void updateRange(const cv::Mat&);
0081 
0082     KDNode* findParent(const cv::Mat& nodePos);
0083 
0084 private:
0085 
0086     // Disable
0087     KDNode(const KDNode&)            = delete;
0088     KDNode& operator=(const KDNode&) = delete;
0089 
0090 private:
0091 
0092     class Private;
0093     Private* d;
0094 };
0095 
0096 } // namespace Digikam
0097 
0098 #endif // DIGIKAM_KD_NODE_H