File indexing completed on 2024-05-12 15:54:50

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2022 Silas Henrique <silash35@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #ifndef KOKO_KDTREE_H_
0008 #define KOKO_KDTREE_H_
0009 
0010 #include <QPointF>
0011 #include <QVariantMap>
0012 #include <cmath>
0013 #include <memory>
0014 
0015 /*
0016   Implementation of the k-dimensional tree algorithm in Qt/C++
0017 */
0018 
0019 class KdNode
0020 {
0021 private:
0022     std::unique_ptr<KdNode> left;
0023     std::unique_ptr<KdNode> right;
0024 
0025 public:
0026     QPointF point; // X, Y coordinates
0027     QVariantMap data;
0028 
0029     KdNode(QPointF p, QVariantMap d);
0030 
0031     void insert(KdNode *newNode, unsigned int axis);
0032     KdNode *findNearest(KdNode *pivot, unsigned int axis);
0033 };
0034 
0035 class KdTree
0036 {
0037 private:
0038     std::unique_ptr<KdNode> root;
0039 
0040 public:
0041     void clear();
0042     void insert(double x, double y, QVariantMap &data);
0043     bool isEmpty() const;
0044 
0045     KdNode *findNearest(double x, double y);
0046 };
0047 
0048 #endif /* KOKO_KDTREE_H_ */