aboutsummaryrefslogtreecommitdiff
path: root/KDTree.h
blob: d2488e72c382f2ecae15f13135abf89592b45f6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
    A simple C++ wrapper for John Tsiombikas's kd-tree library.
    
    KDTree<PointT, LabelT, Dims> kdtree;
    PointT: 
    - must contain a constructor to recieve points via a double buffer [double*]
    - must have a .data() method that produces a double buffer

    LabelT:
    -  modifiable

    Dims:
    - Number of dimensions in PointT

    Search methods return ResultSet similar to an iterator with more contraints
*/

#pragma once

#include <memory>

namespace ldrs {
    
    #include <kdtree.h>
    
    template <typename _PointT, typename _LabelT, int _Dims>
    class KDTree {

        public:
        
        struct Pair {
            _PointT point;
            _LabelT label;

            typedef std::shared_ptr<Pair> Ptr;
            typedef std::shared_ptr<Pair const> ConstPtr;
        };

        private:

        struct kdtree * _tree;
        size_t _size;

        static void _delete_node_callback(void * node) {
            delete reinterpret_cast<_LabelT*>(node);
        }

        static Pair toPair(struct kdres const * res) {
            double pos[_Dims];
            void * data = kd_res_item(const_cast<struct kdres *>(res), pos);

            Pair pl {
                _PointT(reinterpret_cast<double *>(pos)),
                *reinterpret_cast<_LabelT *>(data)
            };

            return pl;
        }

        public:

        typedef std::shared_ptr<KDTree> Ptr;
        typedef std::shared_ptr<KDTree const> ConstPtr;

        class ResultSet {
            struct kdres * _res;
        public:
            ResultSet(struct kdres * res)
                : _res(res)
            {}

            ~ResultSet() {
                if(_res)
                    kd_res_free(_res);
            }

            inline bool next() { return kd_res_next(_res) != 0; }

            inline bool end() const { return kd_res_end(_res) != 0; }

            inline void rewind() { kd_res_rewind(_res); }
            
            inline int size() const { return kd_res_size(_res); }

            ResultSet(const ResultSet &) = delete;
            ResultSet& operator=(const ResultSet &) = delete;
            ResultSet(ResultSet && rs) {
                _res = rs._res;
                rs._res = nullptr;
            }

            ResultSet& operator=(ResultSet && rs) {
                _res = rs._res;
                rs._res = nullptr;
                return *this;
            }

            ResultSet& operator++() {
                next();
                return *this;
            }

            _PointT point() const {
                double pos[_Dims];
                kd_res_item(_res, pos);
                return _PointT(pos);
            }

            _LabelT& label() const {
                return *reinterpret_cast<_LabelT*>(kd_res_item_data(_res));
            }

            Pair pair() const {
                return KDTree::toPair(_res);
            }
        };

        KDTree()
            : _tree(kd_create(_Dims))
            , _size(0)
        {
            kd_data_destructor(_tree, &_delete_node_callback);
        }

        ~KDTree() {
            kd_free(_tree);
        }

        size_t size() const {
            return _size;
        }

        void clear() {
            _size = 0;
            kd_clear(_tree);
        }

        int insert(_PointT const & pt, _LabelT const & label) {
            _size++;

            return kd_insert(
                _tree,
                pt.data(), 
                reinterpret_cast<void *>(new _LabelT(label))
            );
        }

        ResultSet nearestRange(_PointT const & pt, double range) const {
            kdres * res = kd_nearest_range(_tree, pt.data(), range);
            return ResultSet(res);
        }

        ResultSet nearestPoint(_PointT const & pt) const {
            kdres * res = kd_nearest(_tree, pt.data());
            return ResultSet(res);
        }

        Pair nearestExistentPoint(_PointT const & pt) const {
            kdres * res = kd_nearest(_tree, pt.data());

            Pair pl = KDTree::toPair(res);
            
            kd_res_free(res);

            return pl;
        }

    };
};