-
Notifications
You must be signed in to change notification settings - Fork 12
/
3dKDtree.cpp
executable file
·261 lines (221 loc) · 5.63 KB
/
3dKDtree.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "3dKDtree.h"
kdTree::kdTree(const int nodes)
{
storedKDNodes = 0;
maxNumOfNodes = nodes;
kdNodes = new kdNode[maxNumOfNodes + 1];
if(!kdNodes)
{
std::cout<<"初始化kd树时内存溢出!"<<std::endl;
exit(-1);
}
boundrayMin[0] = boundrayMin[1] = boundrayMin[2] = 1e8f;
boundrayMax[0] = boundrayMax[1] = boundrayMax[2] = -1e8f;
}
kdTree::~kdTree()
{
delete [] kdNodes;
}
void kdTree::treeBalance()
{
if(storedKDNodes > 1)
{
kdNode **pa1 = new kdNode*[storedKDNodes + 1]; //组织好树后的指针
kdNode **pa2 = new kdNode*[storedKDNodes + 1]; //原始元素的指针
for(int i =0; i <= storedKDNodes; i++)
pa2[i] = &kdNodes[i];
balancePartition(pa1, pa2, 1, 1, storedKDNodes);
delete []pa2;
//重新排列树
//__w64 int d, j = 1; // According to the warning given when 'int ' is used
int d, j = 1; //j位置元素已经转移走
int foo = 1; //fooNodes存储的元素的最初位置
kdNode fooNodes = kdNodes[j];
for(int i = 1; i <= storedKDNodes; i++)
{
d = pa1[j] - kdNodes;
pa1[j] = NULL;
if(d != foo)
kdNodes[j] = kdNodes[d];
else
{
kdNodes[j] = fooNodes;
if(i < storedKDNodes)
{
for(; foo <= storedKDNodes; foo++)
if(NULL != pa1[foo])
break;
fooNodes = kdNodes[foo];
j = foo;
}
continue;
}
j = d;
}
delete []pa1;
}
halfStoredKDNodes = storedKDNodes/2 - 1;
}
void kdTree::locateNodes(nNearestNodes * const nNN,const int index)const
{
const kdNode *p = &kdNodes[index];
double dist1;
if(index < halfStoredKDNodes)
{
dist1 = nNN->pos[p->plane] - p->pos[p->plane];
if(0.0 < dist1)
{
locateNodes(nNN, 2 * index + 1);
if(nNN->dist2[0] > dist1 * dist1)
locateNodes(nNN, 2 * index);
}
else
{
locateNodes(nNN, 2 * index);
if(nNN->dist2[0] > dist1 * dist1)
locateNodes(nNN, 2 * index + 1);
}//if
}//if
// 计算距离
dist1 = p->pos[0] - nNN->pos[0];
double dist2 = dist1 * dist1;
dist1 = p->pos[1] - nNN->pos[1];
dist2 += dist1 * dist1;
dist1 = p->pos[2] - nNN->pos[2];
dist2 += dist1 * dist1;
if(nNN->dist2[0] > dist2)
{
if(nNN->found < nNN->max)
{
nNN->found++;
nNN->dist2[nNN->found] = dist2;
nNN->index[nNN->found] = p;
}
else
{
int j, parent;
if(0 == nNN->got_Heap)//建立大顶堆
{
double dst2;
const kdNode *nd;
int halfFound = nNN->found >> 1;
for(int k = halfFound; k >= 1; k--)
{
parent = k;
nd = nNN->index[k];
dst2 = nNN->dist2[k];
while(parent <= halfFound)
{
j = parent + parent;
if(j < nNN->found && nNN->dist2[j] < nNN->dist2[j + 1])
j ++;
if(dst2 >= nNN->dist2[j])
break;
nNN->dist2[parent] = nNN->dist2[j];
nNN->index[parent] = nNN->index[j];
parent = j;
}//while
nNN->dist2[parent] = dst2;
nNN->index[parent] = nd;
}//for
nNN->got_Heap = 1;
}//if
//插入
parent = 1;
//if()
j = 2;
while(j <= nNN->found)
{
if(j < nNN->found && nNN->dist2[j] < nNN->dist2[j + 1])
j++;
if(dist2 > nNN->dist2[j])
break;
nNN->dist2[parent] = nNN->dist2[j];
nNN->index[parent] = nNN->index[j];
parent = j;
j += j;
}//while
if((parent != 1)||(dist2 < nNN->dist2[parent]))
{
nNN->index[parent] = p;
nNN->dist2[parent] = dist2;
}
nNN->dist2[0] = nNN->dist2[1];//??????
}//else
}//if
}
#define swap(kdN,a,b){ kdNode* tmp = kdN[a]; kdN[a] = kdN[b]; kdN[b] = tmp;}
void kdTree::medianPartition(kdNode** pOrig,const int start,const int end,const int median,const int axis)
{
int left = start;
int right = end;
while(right > left)
{
const TYPE v = pOrig[right]->pos[axis];
int i = left - 1;
int j = right;
for(;;)
{
while(pOrig[++i]->pos[axis] < v);
while(pOrig[--j]->pos[axis] > v && j > left);
if(i >= j)
break;
swap(pOrig, i, j);
}
swap(pOrig, i, right);
if(i >= median)
right = i - 1;
if(i <= median)
left = i + 1;
}
}
void kdTree::balancePartition(kdNode** pBalanced,kdNode** pOriginal,const int index,const int start,const int end)
{
//计算median,这是怎么计算的呢???
int median = 1;
while((4 * median) <= (end - start + 1))
median += median; //median*=2;
if((3 * median) <= (end - start +1))
{
median += median;
median += start - 1;
}
else
median = end - median + 1;
// 寻找分割数据的轴
int axis = 2;
if((boundrayMax[0] - boundrayMin[0]) > (boundrayMax[1] - boundrayMin[1])&&
(boundrayMax[0] - boundrayMin[0]) > (boundrayMax[2] - boundrayMin[2]))
axis = 0;
else if((boundrayMax[1] - boundrayMin[1]) > (boundrayMax[2] - boundrayMin[2]))
axis = 1;
// 按median分割节点
medianPartition(pOriginal, start, end, median, axis);
pBalanced[index] = pOriginal[median];
pBalanced[index]->plane = axis;
// 迭代平衡左右子树
if(median > start)
{
if(start < median - 1)
{
const double tmp = boundrayMax[axis];
boundrayMax[axis] = pBalanced[index]->pos[axis];
balancePartition(pBalanced, pOriginal, 2 * index, start, median - 1);
boundrayMax[axis] = tmp;
}
else
pBalanced[2 * index] = pOriginal[start];
}
if(median < end)
{
if(median + 1 < end)
{
const double tmp = boundrayMin[axis];
boundrayMin[axis] = pBalanced[index]->pos[axis];
balancePartition(pBalanced, pOriginal, 2 * index + 1, median + 1, end);
boundrayMin[axis] = tmp;
}
else
pBalanced[2 * index + 1] = pOriginal[end];
}
}