use inline BLI_math functions for dot product and length calculation.
[blender.git] / source / blender / blenlib / intern / BLI_kdtree.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: none of this file.
22  *
23  * Contributor(s): Janne Karhu
24  *                 Brecht Van Lommel
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/blenlib/intern/BLI_kdtree.c
30  *  \ingroup bli
31  */
32
33
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_math.h"
38 #include "BLI_kdtree.h"
39
40 #ifndef SWAP
41 #define SWAP(type, a, b) { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
42 #endif
43
44 typedef struct KDTreeNode {
45         struct KDTreeNode *left, *right;
46         float co[3], nor[3];
47         int index;
48         short d;
49 } KDTreeNode;
50
51 struct KDTree {
52         KDTreeNode *nodes;
53         int totnode;
54         KDTreeNode *root;
55 };
56
57 KDTree *BLI_kdtree_new(int maxsize)
58 {
59         KDTree *tree;
60
61         tree= MEM_callocN(sizeof(KDTree), "KDTree");
62         tree->nodes= MEM_callocN(sizeof(KDTreeNode)*maxsize, "KDTreeNode");
63         tree->totnode= 0;
64
65         return tree;
66 }
67
68 void BLI_kdtree_free(KDTree *tree)
69 {
70         if(tree) {
71                 MEM_freeN(tree->nodes);
72                 MEM_freeN(tree);
73         }
74 }
75
76 void BLI_kdtree_insert(KDTree *tree, int index, float *co, float *nor)
77 {
78         KDTreeNode *node= &tree->nodes[tree->totnode++];
79
80         node->index= index;
81         copy_v3_v3(node->co, co);
82         if(nor) copy_v3_v3(node->nor, nor);
83 }
84
85 static KDTreeNode *kdtree_balance(KDTreeNode *nodes, int totnode, int axis)
86 {
87         KDTreeNode *node;
88         float co;
89         int left, right, median, i, j;
90
91         if(totnode <= 0)
92                 return NULL;
93         else if(totnode == 1)
94                 return nodes;
95         
96         /* quicksort style sorting around median */
97         left= 0;
98         right= totnode-1;
99         median= totnode/2;
100
101         while(right > left) {
102                 co= nodes[right].co[axis];
103                 i= left-1;
104                 j= right;
105
106                 while(1) {
107                         while(nodes[++i].co[axis] < co);
108                         while(nodes[--j].co[axis] > co && j>left);
109
110                         if(i >= j) break;
111                         SWAP(KDTreeNode, nodes[i], nodes[j]);
112                 }
113
114                 SWAP(KDTreeNode, nodes[i], nodes[right]);
115                 if(i >= median)
116                         right= i-1;
117                 if(i <= median)
118                         left= i+1;
119         }
120
121         /* set node and sort subnodes */
122         node= &nodes[median];
123         node->d= axis;
124         node->left= kdtree_balance(nodes, median, (axis+1)%3);
125         node->right= kdtree_balance(nodes+median+1, (totnode-(median+1)), (axis+1)%3);
126
127         return node;
128 }
129
130 void BLI_kdtree_balance(KDTree *tree)
131 {
132         tree->root= kdtree_balance(tree->nodes, tree->totnode, 0);
133 }
134
135 static float squared_distance(float *v2, float *v1, float *UNUSED(n1), float *n2)
136 {
137         float d[3], dist;
138
139         d[0]= v2[0]-v1[0];
140         d[1]= v2[1]-v1[1];
141         d[2]= v2[2]-v1[2];
142
143         dist = dot_v3v3(d, d);
144
145         //if(n1 && n2 && (dot_v3v3(n1, n2) < 0.0f))
146         if(n2 && (dot_v3v3(d, n2) < 0.0f))
147                 dist *= 10.0f;
148
149         return dist;
150 }
151
152 int     BLI_kdtree_find_nearest(KDTree *tree, float *co, float *nor, KDTreeNearest *nearest)
153 {
154         KDTreeNode *root, *node, *min_node;
155         KDTreeNode **stack, *defaultstack[100];
156         float min_dist, cur_dist;
157         int totstack, cur=0;
158
159         if(!tree->root)
160                 return -1;
161
162         stack= defaultstack;
163         totstack= 100;
164
165         root= tree->root;
166         min_node= root;
167         min_dist= squared_distance(root->co,co,root->nor,nor);
168
169         if(co[root->d] < root->co[root->d]) {
170                 if(root->right)
171                         stack[cur++]=root->right;
172                 if(root->left)
173                         stack[cur++]=root->left;
174         }
175         else {
176                 if(root->left)
177                         stack[cur++]=root->left;
178                 if(root->right)
179                         stack[cur++]=root->right;
180         }
181         
182         while(cur--){
183                 node=stack[cur];
184
185                 cur_dist = node->co[node->d] - co[node->d];
186
187                 if(cur_dist<0.0f){
188                         cur_dist= -cur_dist*cur_dist;
189
190                         if(-cur_dist<min_dist){
191                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
192                                 if(cur_dist<min_dist){
193                                         min_dist=cur_dist;
194                                         min_node=node;
195                                 }
196                                 if(node->left)
197                                         stack[cur++]=node->left;
198                         }
199                         if(node->right)
200                                 stack[cur++]=node->right;
201                 }
202                 else{
203                         cur_dist= cur_dist*cur_dist;
204
205                         if(cur_dist<min_dist){
206                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
207                                 if(cur_dist<min_dist){
208                                         min_dist=cur_dist;
209                                         min_node=node;
210                                 }
211                                 if(node->right)
212                                         stack[cur++]=node->right;
213                         }
214                         if(node->left)
215                                 stack[cur++]=node->left;
216                 }
217                 if(cur+3 > totstack){
218                         KDTreeNode **temp=MEM_callocN((totstack+100)*sizeof(KDTreeNode*), "psys_treestack");
219                         memcpy(temp,stack,totstack*sizeof(KDTreeNode*));
220                         if(stack != defaultstack)
221                                 MEM_freeN(stack);
222                         stack=temp;
223                         totstack+=100;
224                 }
225         }
226
227         if(nearest) {
228                 nearest->index= min_node->index;
229                 nearest->dist= sqrt(min_dist);
230                 copy_v3_v3(nearest->co, min_node->co);
231         }
232
233         if(stack != defaultstack)
234                 MEM_freeN(stack);
235
236         return min_node->index;
237 }
238
239 static void add_nearest(KDTreeNearest *ptn, int *found, int n, int index, float dist, float *co)
240 {
241         int i;
242
243         if(*found<n) (*found)++;
244
245         for(i=*found-1; i>0; i--) {
246                 if(dist >= ptn[i-1].dist)
247                         break;
248                 else
249                         ptn[i]= ptn[i-1];
250         }
251
252         ptn[i].index= index;
253         ptn[i].dist= dist;
254         copy_v3_v3(ptn[i].co, co);
255 }
256
257 /* finds the nearest n entries in tree to specified coordinates */
258 int     BLI_kdtree_find_n_nearest(KDTree *tree, int n, float *co, float *nor, KDTreeNearest *nearest)
259 {
260         KDTreeNode *root, *node= NULL;
261         KDTreeNode **stack, *defaultstack[100];
262         float cur_dist;
263         int i, totstack, cur=0, found=0;
264
265         if(!tree->root)
266                 return 0;
267
268         stack= defaultstack;
269         totstack= 100;
270
271         root= tree->root;
272
273         cur_dist= squared_distance(root->co,co,root->nor,nor);
274         add_nearest(nearest,&found,n,root->index,cur_dist,root->co);
275         
276         if(co[root->d] < root->co[root->d]) {
277                 if(root->right)
278                         stack[cur++]=root->right;
279                 if(root->left)
280                         stack[cur++]=root->left;
281         }
282         else {
283                 if(root->left)
284                         stack[cur++]=root->left;
285                 if(root->right)
286                         stack[cur++]=root->right;
287         }
288
289         while(cur--){
290                 node=stack[cur];
291
292                 cur_dist = node->co[node->d] - co[node->d];
293
294                 if(cur_dist<0.0f){
295                         cur_dist= -cur_dist*cur_dist;
296
297                         if(found<n || -cur_dist<nearest[found-1].dist){
298                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
299
300                                 if(found<n || cur_dist<nearest[found-1].dist)
301                                         add_nearest(nearest,&found,n,node->index,cur_dist,node->co);
302
303                                 if(node->left)
304                                         stack[cur++]=node->left;
305                         }
306                         if(node->right)
307                                 stack[cur++]=node->right;
308                 }
309                 else{
310                         cur_dist= cur_dist*cur_dist;
311
312                         if(found<n || cur_dist<nearest[found-1].dist){
313                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
314                                 if(found<n || cur_dist<nearest[found-1].dist)
315                                         add_nearest(nearest,&found,n,node->index,cur_dist,node->co);
316
317                                 if(node->right)
318                                         stack[cur++]=node->right;
319                         }
320                         if(node->left)
321                                 stack[cur++]=node->left;
322                 }
323                 if(cur+3 > totstack){
324                         KDTreeNode **temp=MEM_callocN((totstack+100)*sizeof(KDTreeNode*), "psys_treestack");
325                         memcpy(temp,stack,totstack*sizeof(KDTreeNode*));
326                         if(stack != defaultstack)
327                                 MEM_freeN(stack);
328                         stack=temp;
329                         totstack+=100;
330                 }
331         }
332
333         for(i=0; i<found; i++)
334                 nearest[i].dist= sqrt(nearest[i].dist);
335
336         if(stack != defaultstack)
337                 MEM_freeN(stack);
338
339         return found;
340 }
341
342 static int range_compare(const void * a, const void * b)
343 {
344         const KDTreeNearest *kda = a;
345         const KDTreeNearest *kdb = b;
346
347         if(kda->dist < kdb->dist)
348                 return -1;
349         else if(kda->dist > kdb->dist)
350                 return 1;
351         else
352                 return 0;
353 }
354 static void add_in_range(KDTreeNearest **ptn, int found, int *totfoundstack, int index, float dist, float *co)
355 {
356         KDTreeNearest *to;
357
358         if(found+1 > *totfoundstack) {
359                 KDTreeNearest *temp=MEM_callocN((*totfoundstack+50)*sizeof(KDTreeNode), "psys_treefoundstack");
360                 memcpy(temp, *ptn, *totfoundstack * sizeof(KDTreeNearest));
361                 if(*ptn)
362                         MEM_freeN(*ptn);
363                 *ptn = temp;
364                 *totfoundstack+=50;
365         }
366
367         to = (*ptn) + found;
368
369         to->index = index;
370         to->dist = sqrt(dist);
371         copy_v3_v3(to->co, co);
372 }
373 int BLI_kdtree_range_search(KDTree *tree, float range, float *co, float *nor, KDTreeNearest **nearest)
374 {
375         KDTreeNode *root, *node= NULL;
376         KDTreeNode **stack, *defaultstack[100];
377         KDTreeNearest *foundstack=NULL;
378         float range2 = range*range, dist2;
379         int totstack, cur=0, found=0, totfoundstack=0;
380
381         if(!tree || !tree->root)
382                 return 0;
383
384         stack= defaultstack;
385         totstack= 100;
386
387         root= tree->root;
388
389         if(co[root->d] + range < root->co[root->d]) {
390                 if(root->left)
391                         stack[cur++]=root->left;
392         }
393         else if(co[root->d] - range > root->co[root->d]) {
394                 if(root->right)
395                         stack[cur++]=root->right;
396         }
397         else {
398                 dist2 = squared_distance(root->co, co, root->nor, nor);
399                 if(dist2  <= range2)
400                         add_in_range(&foundstack, found++, &totfoundstack, root->index, dist2, root->co);
401
402                 if(root->left)
403                         stack[cur++]=root->left;
404                 if(root->right)
405                         stack[cur++]=root->right;
406         }
407
408         while(cur--) {
409                 node=stack[cur];
410
411                 if(co[node->d] + range < node->co[node->d]) {
412                         if(node->left)
413                                 stack[cur++]=node->left;
414                 }
415                 else if(co[node->d] - range > node->co[node->d]) {
416                         if(node->right)
417                                 stack[cur++]=node->right;
418                 }
419                 else {
420                         dist2 = squared_distance(node->co, co, node->nor, nor);
421                         if(dist2 <= range2)
422                                 add_in_range(&foundstack, found++, &totfoundstack, node->index, dist2, node->co);
423
424                         if(node->left)
425                                 stack[cur++]=node->left;
426                         if(node->right)
427                                 stack[cur++]=node->right;
428                 }
429
430                 if(cur+3 > totstack){
431                         KDTreeNode **temp=MEM_callocN((totstack+100)*sizeof(KDTreeNode*), "psys_treestack");
432                         memcpy(temp,stack,totstack*sizeof(KDTreeNode*));
433                         if(stack != defaultstack)
434                                 MEM_freeN(stack);
435                         stack=temp;
436                         totstack+=100;
437                 }
438         }
439
440         if(stack != defaultstack)
441                 MEM_freeN(stack);
442
443         if(found)
444                 qsort(foundstack, found, sizeof(KDTreeNearest), range_compare);
445
446         *nearest = foundstack;
447
448         return found;
449 }