2nd try to merge sim_physics with trunk rev 19825
[blender.git] / source / blender / blenlib / intern / BLI_kdtree.c
1 /**
2  * $Id: BLI_kdtree.c 19805 2009-04-20 00:19:16Z genscher $
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: none of this file.
24  *
25  * Contributor(s): Janne Karhu
26  *                 Brecht Van Lommel
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  */
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_arithb.h"
38 #include "BLI_kdtree.h"
39
40 #define SWAP(type, a, b) { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
41
42 typedef struct KDTreeNode {
43         struct KDTreeNode *left, *right;
44         float co[3], nor[3];
45         int index;
46         short d;
47 } KDTreeNode;
48
49 struct KDTree {
50         KDTreeNode *nodes;
51         int totnode;
52         KDTreeNode *root;
53 };
54
55 KDTree *BLI_kdtree_new(int maxsize)
56 {
57         KDTree *tree;
58
59         tree= MEM_callocN(sizeof(KDTree), "KDTree");
60         tree->nodes= MEM_callocN(sizeof(KDTreeNode)*maxsize, "KDTreeNode");
61         tree->totnode= 0;
62
63         return tree;
64 }
65
66 void BLI_kdtree_free(KDTree *tree)
67 {
68         if(tree) {
69                 MEM_freeN(tree->nodes);
70                 MEM_freeN(tree);
71         }
72 }
73
74 void BLI_kdtree_insert(KDTree *tree, int index, float *co, float *nor)
75 {
76         KDTreeNode *node= &tree->nodes[tree->totnode++];
77
78         node->index= index;
79         VecCopyf(node->co, co);
80         if(nor) VecCopyf(node->nor, nor);
81 }
82
83 static KDTreeNode *kdtree_balance(KDTreeNode *nodes, int totnode, int axis)
84 {
85         KDTreeNode *node;
86         float co;
87         int left, right, median, i, j;
88
89         if(totnode <= 0)
90                 return NULL;
91         else if(totnode == 1)
92                 return nodes;
93         
94         /* quicksort style sorting around median */
95         left= 0;
96         right= totnode-1;
97         median= totnode/2;
98
99         while(right > left) {
100                 co= nodes[right].co[axis];
101                 i= left-1;
102                 j= right;
103
104                 while(1) {
105                         while(nodes[++i].co[axis] < co);
106                         while(nodes[--j].co[axis] > co && j>left);
107
108                         if(i >= j) break;
109                         SWAP(KDTreeNode, nodes[i], nodes[j]);
110                 }
111
112                 SWAP(KDTreeNode, nodes[i], nodes[right]);
113                 if(i >= median)
114                         right= i-1;
115                 if(i <= median)
116                         left= i+1;
117         }
118
119         /* set node and sort subnodes */
120         node= &nodes[median];
121         node->d= axis;
122         node->left= kdtree_balance(nodes, median, (axis+1)%3);
123         node->right= kdtree_balance(nodes+median+1, (totnode-(median+1)), (axis+1)%3);
124
125         return node;
126 }
127
128 void BLI_kdtree_balance(KDTree *tree)
129 {
130         tree->root= kdtree_balance(tree->nodes, tree->totnode, 0);
131 }
132
133 static float squared_distance(float *v1, float *v2, float *n1, float *n2)
134 {
135         float d[3], dist;
136
137         d[0]= v2[0]-v1[0];
138         d[1]= v2[1]-v1[1];
139         d[2]= v2[2]-v1[2];
140
141         dist= d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
142
143         if(n1 && n2 && n1[0]*n2[0] + n1[1]*n2[1] + n1[2]*n2[2] < 0.0f)
144                 dist *= 10.0f;
145
146         return dist;
147 }
148
149 int     BLI_kdtree_find_nearest(KDTree *tree, float *co, float *nor, KDTreeNearest *nearest)
150 {
151         KDTreeNode *root, *node, *min_node;
152         KDTreeNode **stack, *defaultstack[100];
153         float min_dist, cur_dist;
154         int totstack, cur=0;
155
156         if(!tree->root)
157                 return -1;
158
159         stack= defaultstack;
160         totstack= 100;
161
162         root= tree->root;
163         min_node= root;
164         min_dist= squared_distance(root->co,co,root->nor,nor);
165
166         if(co[root->d] < root->co[root->d]) {
167                 if(root->right)
168                         stack[cur++]=root->right;
169                 if(root->left)
170                         stack[cur++]=root->left;
171         }
172         else {
173                 if(root->left)
174                         stack[cur++]=root->left;
175                 if(root->right)
176                         stack[cur++]=root->right;
177         }
178         
179         while(cur--){
180                 node=stack[cur];
181
182                 cur_dist = node->co[node->d] - co[node->d];
183
184                 if(cur_dist<0.0){
185                         cur_dist= -cur_dist*cur_dist;
186
187                         if(-cur_dist<min_dist){
188                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
189                                 if(cur_dist<min_dist){
190                                         min_dist=cur_dist;
191                                         min_node=node;
192                                 }
193                                 if(node->left)
194                                         stack[cur++]=node->left;
195                         }
196                         if(node->right)
197                                 stack[cur++]=node->right;
198                 }
199                 else{
200                         cur_dist= cur_dist*cur_dist;
201
202                         if(cur_dist<min_dist){
203                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
204                                 if(cur_dist<min_dist){
205                                         min_dist=cur_dist;
206                                         min_node=node;
207                                 }
208                                 if(node->right)
209                                         stack[cur++]=node->right;
210                         }
211                         if(node->left)
212                                 stack[cur++]=node->left;
213                 }
214                 if(cur+3 > totstack){
215                         KDTreeNode **temp=MEM_callocN((totstack+100)*sizeof(KDTreeNode*), "psys_treestack");
216                         memcpy(temp,stack,totstack*sizeof(KDTreeNode*));
217                         if(stack != defaultstack)
218                                 MEM_freeN(stack);
219                         stack=temp;
220                         totstack+=100;
221                 }
222         }
223
224         if(nearest) {
225                 nearest->index= min_node->index;
226                 nearest->dist= sqrt(min_dist);
227                 VecCopyf(nearest->co, min_node->co);
228         }
229
230         if(stack != defaultstack)
231                 MEM_freeN(stack);
232
233         return min_node->index;
234 }
235
236 static void add_nearest(KDTreeNearest *ptn, int *found, int n, int index, float dist, float *co)
237 {
238         int i;
239
240         if(*found<n) (*found)++;
241
242         for(i=*found-1; i>0; i--) {
243                 if(dist >= ptn[i-1].dist)
244                         break;
245                 else
246                         ptn[i]= ptn[i-1];
247         }
248
249         ptn[i].index= index;
250         ptn[i].dist= dist;
251         VecCopyf(ptn[i].co, co);
252 }
253
254 /* finds the nearest n entries in tree to specified coordinates */
255 int     BLI_kdtree_find_n_nearest(KDTree *tree, int n, float *co, float *nor, KDTreeNearest *nearest)
256 {
257         KDTreeNode *root, *node=0;
258         KDTreeNode **stack, *defaultstack[100];
259         float cur_dist;
260         int i, totstack, cur=0, found=0;
261
262         if(!tree->root)
263                 return 0;
264
265         stack= defaultstack;
266         totstack= 100;
267
268         root= tree->root;
269
270         cur_dist= squared_distance(root->co,co,root->nor,nor);
271         add_nearest(nearest,&found,n,root->index,cur_dist,root->co);
272         
273         if(co[root->d] < root->co[root->d]) {
274                 if(root->right)
275                         stack[cur++]=root->right;
276                 if(root->left)
277                         stack[cur++]=root->left;
278         }
279         else {
280                 if(root->left)
281                         stack[cur++]=root->left;
282                 if(root->right)
283                         stack[cur++]=root->right;
284         }
285
286         while(cur--){
287                 node=stack[cur];
288
289                 cur_dist = node->co[node->d] - co[node->d];
290
291                 if(cur_dist<0.0){
292                         cur_dist= -cur_dist*cur_dist;
293
294                         if(found<n || -cur_dist<nearest[found-1].dist){
295                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
296
297                                 if(found<n || cur_dist<nearest[found-1].dist)
298                                         add_nearest(nearest,&found,n,node->index,cur_dist,node->co);
299
300                                 if(node->left)
301                                         stack[cur++]=node->left;
302                         }
303                         if(node->right)
304                                 stack[cur++]=node->right;
305                 }
306                 else{
307                         cur_dist= cur_dist*cur_dist;
308
309                         if(found<n || cur_dist<nearest[found-1].dist){
310                                 cur_dist=squared_distance(node->co,co,node->nor,nor);
311                                 if(found<n || cur_dist<nearest[found-1].dist)
312                                         add_nearest(nearest,&found,n,node->index,cur_dist,node->co);
313
314                                 if(node->right)
315                                         stack[cur++]=node->right;
316                         }
317                         if(node->left)
318                                 stack[cur++]=node->left;
319                 }
320                 if(cur+3 > totstack){
321                         KDTreeNode **temp=MEM_callocN((totstack+100)*sizeof(KDTreeNode*), "psys_treestack");
322                         memcpy(temp,stack,totstack*sizeof(KDTreeNode*));
323                         if(stack != defaultstack)
324                                 MEM_freeN(stack);
325                         stack=temp;
326                         totstack+=100;
327                 }
328         }
329
330         for(i=0; i<found; i++)
331                 nearest[i].dist= sqrt(nearest[i].dist);
332
333         if(stack != defaultstack)
334                 MEM_freeN(stack);
335
336         return found;
337 }
338