Repository navigation
Expand file tree
/
Copy pathkdtree.cpp
More file actions
178 lines (146 loc) · 3.6 KB
/
Copy pathkdtree.cpp
File metadata and controls
178 lines (146 loc) · 3.6 KB
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
170
171
172
173
174
175
176
177
178
#include <kdtree.h>
bool KDTree::inside(Vector2D &minv,Vector2D &maxv,state &s)
{
return(s.pos.x>minv.x && s.pos.y>minv.y &&
s.pos.x<maxv.x && s.pos.y<maxv.y);
}
float KDTree::box_distance(Vector2D &minv,Vector2D &maxv,Vector2D &p)
{
float dx,dy;
dx = p.x - boundTo(p.x,minv.x,maxv.x);
dy = p.y - boundTo(p.y,minv.y,maxv.y);
return(sqrt(dx*dx + dy*dy));
}
void KDTree::split(node *t,int split_dim)
{
node *a,*b;
state *p,*n;
float split_val;
// make new nodes
a = anode.alloc();
b = anode.alloc();
if(!a || !b) return;
a->child[0] = b->child[0] = NULL;
a->child[1] = b->child[1] = NULL;
a->states = b->states = NULL;
a->num_states = b->num_states = 0;
// determine split value
a->minv = b->minv = t->minv;
a->maxv = b->maxv = t->maxv;
if(split_dim == 0){
split_val = (t->minv.x + t->maxv.x) / 2;
a->maxv.x = b->minv.x = split_val;
}else{
split_val = (t->minv.y + t->maxv.y) / 2;
a->maxv.y = b->minv.y = split_val;
}
// separate children based on split
n = t->states;
while((p = n)){
n = n->next;
if(((split_dim == 0)?p->pos.x : p->pos.y) < split_val){
p->next = a->states;
a->states = p;
a->num_states++;
}else{
p->next = b->states;
b->states = p;
b->num_states++;
}
}
// insert into tree
t->states = NULL;
t->child[0] = a;
t->child[1] = b;
}
bool KDTree::setdim(Vector2D &minv,Vector2D &maxv,int nleaf_size,int nmax_depth)
{
clear();
if(!root) root = anode.alloc();
if(!root) return(false);
mzero(*root);
root->minv = minv;
root->maxv = maxv;
leaf_size = nleaf_size;
max_depth = nmax_depth;
return(true);
}
bool KDTree::add(state *s)
{
node *p;
int c,level;
level = 0;
p = root;
if(!p || !inside(p->minv,p->maxv,*s)) return(false);
// go down tree to see where new state should go
while(p->child[0]){ // implies p->child[1] also
c = !inside(p->child[0]->minv,p->child[0]->maxv,*s);
p = p->child[c];
level++;
}
// add it to leaf; and split leaf if too many children
s->next = p->states;
p->states = s;
p->num_states++;
// split leaf if not too deep and too many children for one node
if(level<max_depth && p->num_states>leaf_size){
split(p,level % 2);
}
return(true);
}
void KDTree::clear(node *t)
{
if(!t) return;
if(t->child[0]) clear(t->child[0]);
if(t->child[1]) clear(t->child[1]);
t->child[0] = t->child[1] = NULL;
t->states = NULL;
t->num_states = 0;
anode.free(t);
}
void KDTree::clear()
{
if(!root) return;
clear(root->child[0]);
clear(root->child[1]);
root->child[0] = root->child[1] = NULL;
root->states = NULL;
root->num_states = 0;
}
#include <QDebug>
state *KDTree::nearest(node *t,state *best,float &best_dist,Vector2D &x)
{
float d,dc[2];
state *p;
int c;
// look at states at current node
p = t->states;
while(p){
d = p->pos.dist(x);
if(d < best_dist){
best = p;
best_dist = d;
}
tests++;
p = p->next;
}
// recurse on children (nearest first to maximize pruning)
if(t->child[0]){ // implies t->child[1]
dc[0] = box_distance(t->child[0]->minv,t->child[0]->maxv,x);
dc[1] = box_distance(t->child[1]->minv,t->child[1]->maxv,x);
c = dc[1] < dc[0]; // c indicates nearest lower boundTo distance child
if(dc[ c] < best_dist) best = nearest(t->child[ c],best,best_dist,x);
if(dc[!c] < best_dist) best = nearest(t->child[!c],best,best_dist,x);
}
return(best);
}
state *KDTree::nearest(float &dist,Vector2D &x)
{
state *best;
best = NULL;
dist = 4000;
tests = 0;
if(root != NULL)
best = nearest(root,best,dist,x);
return(best);
}