MauiKit Image Tools

modules/geolocation/kdtree.hpp
1#pragma once
2
3/*
4 * file: KDTree.hpp
5 * author: J. Frederico Carvalho
6 *
7 * This is an adaptation of the KD-tree implementation in rosetta code
8 * https://rosettacode.org/wiki/K-d_tree
9 * It is a reimplementation of the C code using C++.
10 * It also includes a few more queries than the original
11 *
12 */
13
14#include <algorithm>
15#include <functional>
16#include <memory>
17#include <vector>
18
19using point_t = std::vector< double >;
20using indexArr = std::vector< size_t >;
21using pointIndex = typename std::pair< std::vector< double >, size_t >;
22
23class KDNode {
24 public:
25 using KDNodePtr = std::shared_ptr< KDNode >;
26 size_t index;
27 point_t x;
28 KDNodePtr left;
29 KDNodePtr right;
30
31 // initializer
32 KDNode();
33 KDNode(const point_t &, const size_t &, const KDNodePtr &,
34 const KDNodePtr &);
35 KDNode(const pointIndex &, const KDNodePtr &, const KDNodePtr &);
36 ~KDNode();
37
38 // getter
39 double coord(const size_t &);
40
41 // conversions
42 explicit operator bool();
43 explicit operator point_t();
44 explicit operator size_t();
45 explicit operator pointIndex();
46};
47
48using KDNodePtr = std::shared_ptr< KDNode >;
49
50KDNodePtr NewKDNodePtr();
51
52// square euclidean distance
53inline double dist2(const point_t &, const point_t &);
54inline double dist2(const KDNodePtr &, const KDNodePtr &);
55
56// euclidean distance
57inline double dist(const point_t &, const point_t &);
58inline double dist(const KDNodePtr &, const KDNodePtr &);
59
60// Need for sorting
61class comparer {
62 public:
63 size_t idx;
64 explicit comparer(size_t idx_);
65 inline bool compare_idx(
66 const std::pair< std::vector< double >, size_t > &, //
67 const std::pair< std::vector< double >, size_t > & //
68 );
69};
70
71using pointIndexArr = typename std::vector< pointIndex >;
72
73inline void sort_on_idx(const pointIndexArr::iterator &, //
74 const pointIndexArr::iterator &, //
75 size_t idx);
76
77using pointVec = std::vector< point_t >;
78
79class KDTree {
80 KDNodePtr root;
81 KDNodePtr leaf;
82
83 KDNodePtr make_tree(const pointIndexArr::iterator &begin, //
84 const pointIndexArr::iterator &end, //
85 const size_t &length, //
86 const size_t &level //
87 );
88
89 public:
90 KDTree() = default;
91 explicit KDTree(pointVec point_array);
92
93 private:
94 KDNodePtr nearest_( //
95 const KDNodePtr &branch, //
96 const point_t &pt, //
97 const size_t &level, //
98 const KDNodePtr &best, //
99 const double &best_dist //
100 );
101
102 bool m_empty {true};
103 // default caller
104 KDNodePtr nearest_(const point_t &pt);
105
106 public:
107 point_t nearest_point(const point_t &pt);
108 size_t nearest_index(const point_t &pt);
109 pointIndex nearest_pointIndex(const point_t &pt);
110 bool empty();
111
112 private:
113 pointIndexArr neighborhood_( //
114 const KDNodePtr &branch, //
115 const point_t &pt, //
116 const double &rad, //
117 const size_t &level //
118 );
119
120 public:
121 pointIndexArr neighborhood( //
122 const point_t &pt, //
123 const double &rad);
124
125 pointVec neighborhood_points( //
126 const point_t &pt, //
127 const double &rad);
128
129 indexArr neighborhood_indices( //
130 const point_t &pt, //
131 const double &rad);
132};
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:32:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.