3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2006 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): Daniel Genrich, Andre Pinto
26 * ***** END GPL LICENSE BLOCK *****
35 #include "MEM_guardedalloc.h"
37 #include "BKE_utildefines.h"
39 #include "BLI_kdopbvh.h"
40 #include "BLI_arithb.h"
48 #define MAX_TREETYPE 32
49 #define DEFAULT_FIND_NEAREST_HEAP_SIZE 1024
51 typedef struct BVHNode
53 struct BVHNode **children;
54 struct BVHNode *parent; // some user defined traversed need that
55 struct BVHNode *skip[2];
56 float *bv; // Bounding volume of all nodes, max 13 axis
57 int index; // face, edge, vertex index
58 char totnode; // how many nodes are used, used for speedup
59 char main_axis; // Axis used to split this node
65 BVHNode *nodearray; /* pre-alloc branch nodes */
66 BVHNode **nodechild; // pre-alloc childs for nodes
67 float *nodebv; // pre-alloc bounding-volumes for nodes
68 float epsilon; /* epslion is used for inflation of the k-dop */
71 char tree_type; // type of tree (4 => quadtree)
72 char axis; // kdop type (6 => OBB, 7 => AABB, ...)
73 char start_axis, stop_axis; // KDOP_AXES array indices according to axis
76 typedef struct BVHOverlapData
78 BVHTree *tree1, *tree2;
79 BVHTreeOverlap *overlap;
80 int i, max_overlap; /* i is number of overlaps */
81 int start_axis, stop_axis;
84 typedef struct BVHNearestData
88 BVHTree_NearestPointCallback callback;
90 float proj[13]; //coordinates projection over axis
91 BVHTreeNearest nearest;
95 typedef struct BVHRayCastData
99 BVHTree_RayCastCallback callback;
104 float ray_dot_axis[13];
110 ////////////////////////////////////////m
113 ////////////////////////////////////////////////////////////////////////
114 // Bounding Volume Hierarchy Definition
116 // Notes: From OBB until 26-DOP --> all bounding volumes possible, just choose type below
117 // Notes: You have to choose the type at compile time ITM
118 // Notes: You can choose the tree type --> binary, quad, octree, choose below
119 ////////////////////////////////////////////////////////////////////////
121 static float KDOP_AXES[13][3] =
122 { {1.0, 0, 0}, {0, 1.0, 0}, {0, 0, 1.0}, {1.0, 1.0, 1.0}, {1.0, -1.0, 1.0}, {1.0, 1.0, -1.0},
123 {1.0, -1.0, -1.0}, {1.0, 1.0, 0}, {1.0, 0, 1.0}, {0, 1.0, 1.0}, {1.0, -1.0, 0}, {1.0, 0, -1.0},
128 * Generic push and pop heap
130 #define PUSH_HEAP_BODY(HEAP_TYPE,PRIORITY,heap,heap_size) \
132 HEAP_TYPE element = heap[heap_size-1]; \
133 int child = heap_size-1; \
136 int parent = (child-1) / 2; \
137 if(PRIORITY(element, heap[parent])) \
139 heap[child] = heap[parent]; \
144 heap[child] = element; \
147 #define POP_HEAP_BODY(HEAP_TYPE, PRIORITY,heap,heap_size) \
149 HEAP_TYPE element = heap[heap_size-1]; \
151 while(parent < (heap_size-1)/2 ) \
153 int child2 = (parent+1)*2; \
154 if(PRIORITY(heap[child2-1], heap[child2])) \
157 if(PRIORITY(element, heap[child2])) \
160 heap[parent] = heap[child2]; \
163 heap[parent] = element; \
166 int ADJUST_MEMORY(void *local_memblock, void **memblock, int new_size, int *max_size, int size_per_item)
168 int new_max_size = *max_size * 2;
169 void *new_memblock = NULL;
171 if(new_size <= *max_size)
174 if(*memblock == local_memblock)
176 new_memblock = malloc( size_per_item * new_max_size );
177 memcpy( new_memblock, *memblock, size_per_item * *max_size );
180 new_memblock = realloc(*memblock, size_per_item * new_max_size );
184 *memblock = new_memblock;
185 *max_size = new_max_size;
193 //////////////////////////////////////////////////////////////////////////////////////////////////////
195 // with permission deriven from the following Java code:
196 // http://ralphunden.net/content/tutorials/a-guide-to-introsort/
197 // and he derived it from the SUN STL
198 //////////////////////////////////////////////////////////////////////////////////////////////////////
199 static int size_threshold = 16;
201 * Common methods for all algorithms
203 static int floor_lg(int a)
205 return (int)(floor(log(a)/log(2)));
209 * Insertion sort algorithm
211 static void bvh_insertionsort(BVHNode **a, int lo, int hi, int axis)
215 for (i=lo; i < hi; i++)
219 while((j!=lo) && (t->bv[axis] < (a[j-1])->bv[axis]))
228 static int bvh_partition(BVHNode **a, int lo, int hi, BVHNode * x, int axis)
233 while ((a[i])->bv[axis] < x->bv[axis]) i++;
235 while (x->bv[axis] < (a[j])->bv[axis]) j--;
238 SWAP( BVHNode* , a[i], a[j]);
246 static void bvh_downheap(BVHNode **a, int i, int n, int lo, int axis)
248 BVHNode * d = a[lo+i-1];
253 if ((child < n) && ((a[lo+child-1])->bv[axis] < (a[lo+child])->bv[axis]))
257 if (!(d->bv[axis] < (a[lo+child-1])->bv[axis])) break;
258 a[lo+i-1] = a[lo+child-1];
264 static void bvh_heapsort(BVHNode **a, int lo, int hi, int axis)
267 for (i=n/2; i>=1; i=i-1)
269 bvh_downheap(a, i,n,lo, axis);
271 for (i=n; i>1; i=i-1)
273 SWAP(BVHNode*, a[lo],a[lo+i-1]);
274 bvh_downheap(a, 1,i-1,lo, axis);
278 static BVHNode *bvh_medianof3(BVHNode **a, int lo, int mid, int hi, int axis) // returns Sortable
280 if ((a[mid])->bv[axis] < (a[lo])->bv[axis])
282 if ((a[hi])->bv[axis] < (a[mid])->bv[axis])
286 if ((a[hi])->bv[axis] < (a[lo])->bv[axis])
294 if ((a[hi])->bv[axis] < (a[mid])->bv[axis])
296 if ((a[hi])->bv[axis] < (a[lo])->bv[axis])
306 * Quicksort algorithm modified for Introsort
308 static void bvh_introsort_loop (BVHNode **a, int lo, int hi, int depth_limit, int axis)
312 while (hi-lo > size_threshold)
314 if (depth_limit == 0)
316 bvh_heapsort(a, lo, hi, axis);
319 depth_limit=depth_limit-1;
320 p=bvh_partition(a, lo, hi, bvh_medianof3(a, lo, lo+((hi-lo)/2)+1, hi-1, axis), axis);
321 bvh_introsort_loop(a, p, hi, depth_limit, axis);
326 static void sort(BVHNode **a0, int begin, int end, int axis)
331 bvh_introsort_loop(a, begin, end, 2*floor_lg(end-begin), axis);
332 bvh_insertionsort(a, begin, end, axis);
335 void sort_along_axis(BVHTree *tree, int start, int end, int axis)
337 sort(tree->nodes, start, end, axis);
340 //after a call to this function you can expect one of:
341 // every node to left of a[n] are smaller or equal to it
342 // every node to the right of a[n] are greater or equal to it
343 int partition_nth_element(BVHNode **a, int _begin, int _end, int n, int axis){
344 int begin = _begin, end = _end, cut;
347 cut = bvh_partition(a, begin, end, bvh_medianof3(a, begin, (begin+end)/2, end-1, axis), axis );
353 bvh_insertionsort(a, begin, end, axis);
358 //////////////////////////////////////////////////////////////////////////////////////////////////////
359 static void build_skip_links(BVHTree *tree, BVHNode *node, BVHNode *left, BVHNode *right)
363 node->skip[0] = left;
364 node->skip[1] = right;
366 for (i = 0; i < node->totnode; i++)
368 if(i+1 < node->totnode)
369 build_skip_links(tree, node->children[i], left, node->children[i+1] );
371 build_skip_links(tree, node->children[i], left, right );
373 left = node->children[i];
378 * BVHTree bounding volumes functions
380 static void create_kdop_hull(BVHTree *tree, BVHNode *node, float *co, int numpoints, int moving)
383 float *bv = node->bv;
386 // don't init boudings for the moving case
389 for (i = tree->start_axis; i < tree->stop_axis; i++)
392 bv[2*i + 1] = -FLT_MAX;
396 for(k = 0; k < numpoints; k++)
399 for (i = tree->start_axis; i < tree->stop_axis; i++)
401 newminmax = INPR(&co[k * 3], KDOP_AXES[i]);
402 if (newminmax < bv[2 * i])
403 bv[2 * i] = newminmax;
404 if (newminmax > bv[(2 * i) + 1])
405 bv[(2 * i) + 1] = newminmax;
410 // depends on the fact that the BVH's for each face is already build
411 static void refit_kdop_hull(BVHTree *tree, BVHNode *node, int start, int end)
415 float *bv = node->bv;
418 for (i = tree->start_axis; i < tree->stop_axis; i++)
421 bv[2*i + 1] = -FLT_MAX;
424 for (j = start; j < end; j++)
427 for (i = tree->start_axis; i < tree->stop_axis; i++)
429 newmin = tree->nodes[j]->bv[(2 * i)];
430 if ((newmin < bv[(2 * i)]))
431 bv[(2 * i)] = newmin;
433 newmax = tree->nodes[j]->bv[(2 * i) + 1];
434 if ((newmax > bv[(2 * i) + 1]))
435 bv[(2 * i) + 1] = newmax;
441 // only supports x,y,z axis in the moment
442 // but we should use a plain and simple function here for speed sake
443 static char get_largest_axis(float *bv)
445 float middle_point[3];
447 middle_point[0] = (bv[1]) - (bv[0]); // x axis
448 middle_point[1] = (bv[3]) - (bv[2]); // y axis
449 middle_point[2] = (bv[5]) - (bv[4]); // z axis
450 if (middle_point[0] > middle_point[1])
452 if (middle_point[0] > middle_point[2])
453 return 1; // max x axis
455 return 5; // max z axis
459 if (middle_point[1] > middle_point[2])
460 return 3; // max y axis
462 return 5; // max z axis
466 // bottom-up update of bvh node BV
467 // join the children on the parent BV
468 static void node_join(BVHTree *tree, BVHNode *node)
472 for (i = tree->start_axis; i < tree->stop_axis; i++)
474 node->bv[2*i] = FLT_MAX;
475 node->bv[2*i + 1] = -FLT_MAX;
478 for (i = 0; i < tree->tree_type; i++)
480 if (node->children[i])
482 for (j = tree->start_axis; j < tree->stop_axis; j++)
485 if (node->children[i]->bv[(2 * j)] < node->bv[(2 * j)])
486 node->bv[(2 * j)] = node->children[i]->bv[(2 * j)];
489 if (node->children[i]->bv[(2 * j) + 1] > node->bv[(2 * j) + 1])
490 node->bv[(2 * j) + 1] = node->children[i]->bv[(2 * j) + 1];
499 * Debug and information functions
502 static void bvhtree_print_tree(BVHTree *tree, BVHNode *node, int depth)
505 for(i=0; i<depth; i++) printf(" ");
506 printf(" - %d (%ld): ", node->index, node - tree->nodearray);
507 for(i=2*tree->start_axis; i<2*tree->stop_axis; i++)
508 printf("%.3f ", node->bv[i]);
511 for(i=0; i<tree->tree_type; i++)
512 if(node->children[i])
513 bvhtree_print_tree(tree, node->children[i], depth+1);
516 static void bvhtree_info(BVHTree *tree)
518 printf("BVHTree info\n");
519 printf("tree_type = %d, axis = %d, epsilon = %f\n", tree->tree_type, tree->axis, tree->epsilon);
520 printf("nodes = %d, branches = %d, leafs = %d\n", tree->totbranch + tree->totleaf, tree->totbranch, tree->totleaf);
521 printf("Memory per node = %ldbytes\n", sizeof(BVHNode) + sizeof(BVHNode*)*tree->tree_type + sizeof(float)*tree->axis);
522 printf("BV memory = %dbytes\n", MEM_allocN_len(tree->nodebv));
524 printf("Total memory = %ldbytes\n", sizeof(BVHTree)
525 + MEM_allocN_len(tree->nodes)
526 + MEM_allocN_len(tree->nodearray)
527 + MEM_allocN_len(tree->nodechild)
528 + MEM_allocN_len(tree->nodebv)
531 // bvhtree_print_tree(tree, tree->nodes[tree->totleaf], 0);
538 static void verify_tree(BVHTree *tree)
542 // check the pointer list
543 for(i = 0; i < tree->totleaf; i++)
545 if(tree->nodes[i]->parent == NULL)
546 printf("Leaf has no parent: %d\n", i);
549 for(j = 0; j < tree->tree_type; j++)
551 if(tree->nodes[i]->parent->children[j] == tree->nodes[i])
556 printf("Parent child relationship doesn't match: %d\n", i);
562 // check the leaf list
563 for(i = 0; i < tree->totleaf; i++)
565 if(tree->nodearray[i].parent == NULL)
566 printf("Leaf has no parent: %d\n", i);
569 for(j = 0; j < tree->tree_type; j++)
571 if(tree->nodearray[i].parent->children[j] == &tree->nodearray[i])
576 printf("Parent child relationship doesn't match: %d\n", i);
582 printf("branches: %d, leafs: %d, total: %d\n", tree->totbranch, tree->totleaf, tree->totbranch + tree->totleaf);
586 //Helper data and structures to build a min-leaf generalized implicit tree
587 //This code can be easily reduced (basicly this is only method to calculate pow(k, n) in O(1).. and stuff like that)
588 typedef struct BVHBuildHelper
593 int leafs_per_child [32]; //Min number of leafs that are archievable from a node at depth N
594 int branches_on_level[32]; //Number of nodes at depth N (tree_type^N)
596 int remain_leafs; //Number of leafs that are placed on the level that is not 100% filled
600 static void build_implicit_tree_helper(BVHTree *tree, BVHBuildHelper *data)
606 data->totleafs = tree->totleaf;
607 data->tree_type= tree->tree_type;
609 //Calculate the smallest tree_type^n such that tree_type^n >= num_leafs
611 data->leafs_per_child[0] = 1;
612 data->leafs_per_child[0] < data->totleafs;
613 data->leafs_per_child[0] *= data->tree_type
616 data->branches_on_level[0] = 1;
618 //We could stop the loop first (but I am lazy to find out when)
619 for(depth = 1; depth < 32; depth++)
621 data->branches_on_level[depth] = data->branches_on_level[depth-1] * data->tree_type;
622 data->leafs_per_child [depth] = data->leafs_per_child [depth-1] / data->tree_type;
625 remain = data->totleafs - data->leafs_per_child[1];
626 nnodes = (remain + data->tree_type - 2) / (data->tree_type - 1);
627 data->remain_leafs = remain + nnodes;
630 // return the min index of all the leafs archivable with the given branch
631 static int implicit_leafs_index(BVHBuildHelper *data, int depth, int child_index)
633 int min_leaf_index = child_index * data->leafs_per_child[depth-1];
634 if(min_leaf_index <= data->remain_leafs)
635 return min_leaf_index;
636 else if(data->leafs_per_child[depth])
637 return data->totleafs - (data->branches_on_level[depth-1] - child_index) * data->leafs_per_child[depth];
639 return data->remain_leafs;
643 * Generalized implicit tree build
645 * An implicit tree is a tree where its structure is implied, thus there is no need to store child pointers or indexs.
646 * Its possible to find the position of the child or the parent with simple maths (multiplication and adittion). This type
647 * of tree is for example used on heaps.. where node N has its childs at indexs N*2 and N*2+1.
649 * Altought in this case the tree type is general.. and not know until runtime.
650 * tree_type stands for the maximum number of childs that a tree node can have.
651 * All tree types >= 2 are supported.
653 * Advantages of the used trees include:
654 * - No need to store child/parent relations (they are implicit);
655 * - Any node child always has an index greater than the parent;
656 * - Brother nodes are sequencial in memory;
659 * Some math relations derived for general implicit trees:
661 * K = tree_type, ( 2 <= K )
663 * N child of node A = A * K + (2 - K) + N, (0 <= N < K)
667 * (looping elements, knowing if its a leaf or not.. etc...)
670 // This functions returns the number of branches needed to have the requested number of leafs.
671 static int implicit_needed_branches(int tree_type, int leafs)
673 return MAX2(1, (leafs + tree_type - 3) / (tree_type-1) );
677 * This function handles the problem of "sorting" the leafs (along the split_axis).
679 * It arranges the elements in the given partitions such that:
680 * - any element in partition N is less or equal to any element in partition N+1.
681 * - if all elements are diferent all partition will get the same subset of elements
682 * as if the array was sorted.
684 * partition P is described as the elements in the range ( nth[P] , nth[P+1] ]
686 * TODO: This can be optimized a bit by doing a specialized nth_element instead of K nth_elements
688 static void split_leafs(BVHNode **leafs_array, int *nth, int partitions, int split_axis)
691 for(i=0; i < partitions-1; i++)
693 if(nth[i] >= nth[partitions])
696 partition_nth_element(leafs_array, nth[i], nth[partitions], nth[i+1], split_axis);
701 * This functions builds an optimal implicit tree from the given leafs.
702 * Where optimal stands for:
703 * - The resulting tree will have the smallest number of branches;
704 * - At most only one branch will have NULL childs;
705 * - All leafs will be stored at level N or N+1.
707 * This function creates an implicit tree on branches_array, the leafs are given on the leafs_array.
709 * The tree is built per depth levels. First branchs at depth 1.. then branches at depth 2.. etc..
710 * The reason is that we can build level N+1 from level N witouth any data dependencies.. thus it allows
711 * to use multithread building.
713 * To archieve this is necessary to find how much leafs are accessible from a certain branch, BVHBuildHelper
714 * implicit_needed_branches and implicit_leafs_index are auxiliar functions to solve that "optimal-split".
716 static void non_recursive_bvh_div_nodes(BVHTree *tree, BVHNode *branches_array, BVHNode **leafs_array, int num_leafs)
720 const int tree_type = tree->tree_type;
721 const int tree_offset = 2 - tree->tree_type; //this value is 0 (on binary trees) and negative on the others
722 const int num_branches= implicit_needed_branches(tree_type, num_leafs);
727 // set parent from root node to NULL
728 BVHNode *tmp = branches_array+0;
731 //Most of bvhtree code relies on 1-leaf trees having at least one branch
732 //We handle that special case here
735 BVHNode *root = branches_array+0;
736 refit_kdop_hull(tree, root, 0, num_leafs);
737 root->main_axis = get_largest_axis(root->bv) / 2;
739 root->children[0] = leafs_array[0];
740 root->children[0]->parent = root;
744 branches_array--; //Implicit trees use 1-based indexs
746 build_implicit_tree_helper(tree, &data);
748 //Loop tree levels (log N) loops
749 for(i=1, depth = 1; i <= num_branches; i = i*tree_type + tree_offset, depth++)
751 const int first_of_next_level = i*tree_type + tree_offset;
752 const int end_j = MIN2(first_of_next_level, num_branches + 1); //index of last branch on this level
755 //Loop all branches on this level
756 #pragma omp parallel for private(j) schedule(static)
757 for(j = i; j < end_j; j++)
760 const int parent_level_index= j-i;
761 BVHNode* parent = branches_array + j;
762 int nth_positions[ MAX_TREETYPE + 1];
765 int parent_leafs_begin = implicit_leafs_index(&data, depth, parent_level_index);
766 int parent_leafs_end = implicit_leafs_index(&data, depth, parent_level_index+1);
768 //This calculates the bounding box of this branch
769 //and chooses the largest axis as the axis to divide leafs
770 refit_kdop_hull(tree, parent, parent_leafs_begin, parent_leafs_end);
771 split_axis = get_largest_axis(parent->bv);
773 //Save split axis (this can be used on raytracing to speedup the query time)
774 parent->main_axis = split_axis / 2;
776 //Split the childs along the split_axis, note: its not needed to sort the whole leafs array
777 //Only to assure that the elements are partioned on a way that each child takes the elements
778 //it would take in case the whole array was sorted.
779 //Split_leafs takes care of that "sort" problem.
780 nth_positions[ 0] = parent_leafs_begin;
781 nth_positions[tree_type] = parent_leafs_end;
782 for(k = 1; k < tree_type; k++)
784 int child_index = j * tree_type + tree_offset + k;
785 int child_level_index = child_index - first_of_next_level; //child level index
786 nth_positions[k] = implicit_leafs_index(&data, depth+1, child_level_index);
789 split_leafs(leafs_array, nth_positions, tree_type, split_axis);
792 //Setup children and totnode counters
793 //Not really needed but currently most of BVH code relies on having an explicit children structure
794 for(k = 0; k < tree_type; k++)
796 int child_index = j * tree_type + tree_offset + k;
797 int child_level_index = child_index - first_of_next_level; //child level index
799 int child_leafs_begin = implicit_leafs_index(&data, depth+1, child_level_index);
800 int child_leafs_end = implicit_leafs_index(&data, depth+1, child_level_index+1);
802 if(child_leafs_end - child_leafs_begin > 1)
804 parent->children[k] = branches_array + child_index;
805 parent->children[k]->parent = parent;
807 else if(child_leafs_end - child_leafs_begin == 1)
809 parent->children[k] = leafs_array[ child_leafs_begin ];
810 parent->children[k]->parent = parent;
815 parent->totnode = k+1;
825 BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
830 // theres not support for trees below binary-trees :P
834 if(tree_type > MAX_TREETYPE)
837 tree = (BVHTree *)MEM_callocN(sizeof(BVHTree), "BVHTree");
839 //tree epsilon must be >= FLT_EPSILON
840 //so that tangent rays can still hit a bounding volume..
841 //this bug would show up when casting a ray aligned with a kdop-axis and with an edge of 2 faces
842 epsilon = MAX2(FLT_EPSILON, epsilon);
846 tree->epsilon = epsilon;
847 tree->tree_type = tree_type;
852 tree->start_axis = 0;
853 tree->stop_axis = 13;
857 tree->start_axis = 7;
858 tree->stop_axis = 13;
862 tree->start_axis = 0;
865 else if(axis == 8) // AABB
867 tree->start_axis = 0;
870 else if(axis == 6) // OBB
872 tree->start_axis = 0;
883 numnodes = maxsize + implicit_needed_branches(tree_type, maxsize) + tree_type;
885 tree->nodes = (BVHNode **)MEM_callocN(sizeof(BVHNode *)*numnodes, "BVHNodes");
893 tree->nodebv = (float*)MEM_callocN(sizeof(float)* axis * numnodes, "BVHNodeBV");
896 MEM_freeN(tree->nodes);
900 tree->nodechild = (BVHNode**)MEM_callocN(sizeof(BVHNode*) * tree_type * numnodes, "BVHNodeBV");
903 MEM_freeN(tree->nodebv);
904 MEM_freeN(tree->nodes);
908 tree->nodearray = (BVHNode *)MEM_callocN(sizeof(BVHNode)* numnodes, "BVHNodeArray");
912 MEM_freeN(tree->nodechild);
913 MEM_freeN(tree->nodebv);
914 MEM_freeN(tree->nodes);
919 //link the dynamic bv and child links
920 for(i=0; i< numnodes; i++)
922 tree->nodearray[i].bv = tree->nodebv + i * axis;
923 tree->nodearray[i].children = tree->nodechild + i * tree_type;
931 void BLI_bvhtree_free(BVHTree *tree)
935 MEM_freeN(tree->nodes);
936 MEM_freeN(tree->nodearray);
937 MEM_freeN(tree->nodebv);
938 MEM_freeN(tree->nodechild);
943 void BLI_bvhtree_balance(BVHTree *tree)
947 BVHNode* branches_array = tree->nodearray + tree->totleaf;
948 BVHNode** leafs_array = tree->nodes;
950 //This function should only be called once (some big bug goes here if its being called more than once per tree)
951 assert(tree->totbranch == 0);
953 //Build the implicit tree
954 non_recursive_bvh_div_nodes(tree, branches_array, leafs_array, tree->totleaf);
956 //current code expects the branches to be linked to the nodes array
957 //we perform that linkage here
958 tree->totbranch = implicit_needed_branches(tree->tree_type, tree->totleaf);
959 for(i = 0; i < tree->totbranch; i++)
960 tree->nodes[tree->totleaf + i] = branches_array + i;
962 build_skip_links(tree, tree->nodes[tree->totleaf], NULL, NULL);
963 //bvhtree_info(tree);
966 int BLI_bvhtree_insert(BVHTree *tree, int index, float *co, int numpoints)
969 BVHNode *node = NULL;
971 // insert should only possible as long as tree->totbranch is 0
972 if(tree->totbranch > 0)
975 if(tree->totleaf+1 >= MEM_allocN_len(tree->nodes)/sizeof(*(tree->nodes)))
978 // TODO check if have enough nodes in array
980 node = tree->nodes[tree->totleaf] = &(tree->nodearray[tree->totleaf]);
983 create_kdop_hull(tree, node, co, numpoints, 0);
986 // inflate the bv with some epsilon
987 for (i = tree->start_axis; i < tree->stop_axis; i++)
989 node->bv[(2 * i)] -= tree->epsilon; // minimum
990 node->bv[(2 * i) + 1] += tree->epsilon; // maximum
997 // call before BLI_bvhtree_update_tree()
998 int BLI_bvhtree_update_node(BVHTree *tree, int index, float *co, float *co_moving, int numpoints)
1001 BVHNode *node= NULL;
1003 // check if index exists
1004 if(index > tree->totleaf)
1007 node = tree->nodearray + index;
1009 create_kdop_hull(tree, node, co, numpoints, 0);
1012 create_kdop_hull(tree, node, co_moving, numpoints, 1);
1014 // inflate the bv with some epsilon
1015 for (i = tree->start_axis; i < tree->stop_axis; i++)
1017 node->bv[(2 * i)] -= tree->epsilon; // minimum
1018 node->bv[(2 * i) + 1] += tree->epsilon; // maximum
1024 // call BLI_bvhtree_update_node() first for every node/point/triangle
1025 void BLI_bvhtree_update_tree(BVHTree *tree)
1027 //Update bottom=>top
1028 //TRICKY: the way we build the tree all the childs have an index greater than the parent
1029 //This allows us todo a bottom up update by starting on the biger numbered branch
1031 BVHNode** root = tree->nodes + tree->totleaf;
1032 BVHNode** index = tree->nodes + tree->totleaf + tree->totbranch-1;
1034 for (; index >= root; index--)
1035 node_join(tree, *index);
1038 float BLI_bvhtree_getepsilon(BVHTree *tree)
1040 return tree->epsilon;
1045 * BLI_bvhtree_overlap
1047 // overlap - is it possbile for 2 bv's to collide ?
1048 static int tree_overlap(BVHNode *node1, BVHNode *node2, int start_axis, int stop_axis)
1050 float *bv1 = node1->bv;
1051 float *bv2 = node2->bv;
1053 float *bv1_end = bv1 + (stop_axis<<1);
1055 bv1 += start_axis<<1;
1056 bv2 += start_axis<<1;
1058 // test all axis if min + max overlap
1059 for (; bv1 != bv1_end; bv1+=2, bv2+=2)
1061 if ((*(bv1) > *(bv2 + 1)) || (*(bv2) > *(bv1 + 1)))
1068 static void traverse(BVHOverlapData *data, BVHNode *node1, BVHNode *node2)
1072 if(tree_overlap(node1, node2, data->start_axis, data->stop_axis))
1074 // check if node1 is a leaf
1077 // check if node2 is a leaf
1086 if(data->i >= data->max_overlap)
1088 // try to make alloc'ed memory bigger
1089 data->overlap = realloc(data->overlap, sizeof(BVHTreeOverlap)*data->max_overlap*2);
1093 printf("Out of Memory in traverse\n");
1096 data->max_overlap *= 2;
1099 // both leafs, insert overlap!
1100 data->overlap[data->i].indexA = node1->index;
1101 data->overlap[data->i].indexB = node2->index;
1107 for(j = 0; j < data->tree2->tree_type; j++)
1109 if(node2->children[j])
1110 traverse(data, node1, node2->children[j]);
1117 for(j = 0; j < data->tree2->tree_type; j++)
1119 if(node1->children[j])
1120 traverse(data, node1->children[j], node2);
1127 BVHTreeOverlap *BLI_bvhtree_overlap(BVHTree *tree1, BVHTree *tree2, int *result)
1130 BVHTreeOverlap *overlap = NULL, *to = NULL;
1131 BVHOverlapData **data;
1133 // check for compatibility of both trees (can't compare 14-DOP with 18-DOP)
1134 if((tree1->axis != tree2->axis) && (tree1->axis == 14 || tree2->axis == 14) && (tree1->axis == 18 || tree2->axis == 18))
1137 // fast check root nodes for collision before doing big splitting + traversal
1138 if(!tree_overlap(tree1->nodes[tree1->totleaf], tree2->nodes[tree2->totleaf], MIN2(tree1->start_axis, tree2->start_axis), MIN2(tree1->stop_axis, tree2->stop_axis)))
1141 data = MEM_callocN(sizeof(BVHOverlapData *)* tree1->tree_type, "BVHOverlapData_star");
1143 for(j = 0; j < tree1->tree_type; j++)
1145 data[j] = (BVHOverlapData *)MEM_callocN(sizeof(BVHOverlapData), "BVHOverlapData");
1147 // init BVHOverlapData
1148 data[j]->overlap = (BVHTreeOverlap *)malloc(sizeof(BVHTreeOverlap)*MAX2(tree1->totleaf, tree2->totleaf));
1149 data[j]->tree1 = tree1;
1150 data[j]->tree2 = tree2;
1151 data[j]->max_overlap = MAX2(tree1->totleaf, tree2->totleaf);
1153 data[j]->start_axis = MIN2(tree1->start_axis, tree2->start_axis);
1154 data[j]->stop_axis = MIN2(tree1->stop_axis, tree2->stop_axis );
1157 #pragma omp parallel for private(j) schedule(static)
1158 for(j = 0; j < MIN2(tree1->tree_type, tree1->nodes[tree1->totleaf]->totnode); j++)
1160 traverse(data[j], tree1->nodes[tree1->totleaf]->children[j], tree2->nodes[tree2->totleaf]);
1163 for(j = 0; j < tree1->tree_type; j++)
1164 total += data[j]->i;
1166 to = overlap = (BVHTreeOverlap *)MEM_callocN(sizeof(BVHTreeOverlap)*total, "BVHTreeOverlap");
1168 for(j = 0; j < tree1->tree_type; j++)
1170 memcpy(to, data[j]->overlap, data[j]->i*sizeof(BVHTreeOverlap));
1174 for(j = 0; j < tree1->tree_type; j++)
1176 free(data[j]->overlap);
1187 * Nearest neighbour - BLI_bvhtree_find_nearest
1189 static float squared_dist(const float *a, const float *b)
1193 return INPR(tmp, tmp);
1196 //Determines the nearest point of the given node BV. Returns the squared distance to that point.
1197 static float calc_nearest_point(BVHNearestData *data, BVHNode *node, float *nearest)
1200 const float *bv = node->bv;
1202 //nearest on AABB hull
1203 for(i=0; i != 3; i++, bv += 2)
1205 if(bv[0] > data->proj[i])
1207 else if(bv[1] < data->proj[i])
1210 nearest[i] = data->proj[i];
1214 //nearest on a general hull
1215 VECCOPY(nearest, data->co);
1216 for(i = data->tree->start_axis; i != data->tree->stop_axis; i++, bv+=2)
1218 float proj = INPR( nearest, KDOP_AXES[i]);
1219 float dl = bv[0] - proj;
1220 float du = bv[1] - proj;
1224 VECADDFAC(nearest, nearest, KDOP_AXES[i], dl);
1228 VECADDFAC(nearest, nearest, KDOP_AXES[i], du);
1232 return squared_dist(data->co, nearest);
1236 typedef struct NodeDistance
1243 #define NodeDistance_priority(a,b) ( (a).dist < (b).dist )
1245 // TODO: use a priority queue to reduce the number of nodes looked on
1246 static void dfs_find_nearest_dfs(BVHNearestData *data, BVHNode *node)
1248 if(node->totnode == 0)
1251 data->callback(data->userdata , node->index, data->co, &data->nearest);
1254 data->nearest.index = node->index;
1255 data->nearest.dist = calc_nearest_point(data, node, data->nearest.co);
1260 //Better heuristic to pick the closest node to dive on
1264 if(data->proj[ (int)node->main_axis ] <= node->children[0]->bv[(int)node->main_axis*2+1])
1267 for(i=0; i != node->totnode; i++)
1269 if( calc_nearest_point(data, node->children[i], nearest) >= data->nearest.dist) continue;
1270 dfs_find_nearest_dfs(data, node->children[i]);
1275 for(i=node->totnode-1; i >= 0 ; i--)
1277 if( calc_nearest_point(data, node->children[i], nearest) >= data->nearest.dist) continue;
1278 dfs_find_nearest_dfs(data, node->children[i]);
1284 static void dfs_find_nearest_begin(BVHNearestData *data, BVHNode *node)
1286 float nearest[3], sdist;
1287 sdist = calc_nearest_point(data, node, nearest);
1288 if(sdist >= data->nearest.dist) return;
1289 dfs_find_nearest_dfs(data, node);
1294 static void NodeDistance_push_heap(NodeDistance *heap, int heap_size)
1295 PUSH_HEAP_BODY(NodeDistance, NodeDistance_priority, heap, heap_size)
1297 static void NodeDistance_pop_heap(NodeDistance *heap, int heap_size)
1298 POP_HEAP_BODY(NodeDistance, NodeDistance_priority, heap, heap_size)
1300 //NN function that uses an heap.. this functions leads to an optimal number of min-distance
1301 //but for normal tri-faces and BV 6-dop.. a simple dfs with local heuristics (as implemented
1302 //in source/blender/blenkernel/intern/shrinkwrap.c) works faster.
1304 //It may make sense to use this function if the callback queries are very slow.. or if its impossible
1305 //to get a nice heuristic
1307 //this function uses "malloc/free" instead of the MEM_* because it intends to be openmp safe
1308 static void bfs_find_nearest(BVHNearestData *data, BVHNode *node)
1311 NodeDistance default_heap[DEFAULT_FIND_NEAREST_HEAP_SIZE];
1312 NodeDistance *heap=default_heap, current;
1313 int heap_size = 0, max_heap_size = sizeof(default_heap)/sizeof(default_heap[0]);
1316 int callbacks = 0, push_heaps = 0;
1318 if(node->totnode == 0)
1320 dfs_find_nearest_dfs(data, node);
1324 current.node = node;
1325 current.dist = calc_nearest_point(data, node, nearest);
1327 while(current.dist < data->nearest.dist)
1329 // printf("%f : %f\n", current.dist, data->nearest.dist);
1330 for(i=0; i< current.node->totnode; i++)
1332 BVHNode *child = current.node->children[i];
1333 if(child->totnode == 0)
1336 dfs_find_nearest_dfs(data, child);
1341 if(heap_size >= max_heap_size
1342 && ADJUST_MEMORY(default_heap, (void**)&heap, heap_size+1, &max_heap_size, sizeof(heap[0])) == FALSE)
1344 printf("WARNING: bvh_find_nearest got out of memory\n");
1346 if(heap != default_heap)
1352 heap[heap_size].node = current.node->children[i];
1353 heap[heap_size].dist = calc_nearest_point(data, current.node->children[i], nearest);
1355 if(heap[heap_size].dist >= data->nearest.dist) continue;
1358 NodeDistance_push_heap(heap, heap_size);
1359 // PUSH_HEAP_BODY(NodeDistance, NodeDistance_priority, heap, heap_size);
1364 if(heap_size == 0) break;
1367 NodeDistance_pop_heap(heap, heap_size);
1368 // POP_HEAP_BODY(NodeDistance, NodeDistance_priority, heap, heap_size);
1372 // printf("hsize=%d, callbacks=%d, pushs=%d\n", heap_size, callbacks, push_heaps);
1374 if(heap != default_heap)
1379 int BLI_bvhtree_find_nearest(BVHTree *tree, const float *co, BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
1383 BVHNearestData data;
1384 BVHNode* root = tree->nodes[tree->totleaf];
1386 //init data to search
1390 data.callback = callback;
1391 data.userdata = userdata;
1393 for(i = data.tree->start_axis; i != data.tree->stop_axis; i++)
1395 data.proj[i] = INPR(data.co, KDOP_AXES[i]);
1400 memcpy( &data.nearest , nearest, sizeof(*nearest) );
1404 data.nearest.index = -1;
1405 data.nearest.dist = FLT_MAX;
1410 dfs_find_nearest_begin(&data, root);
1415 memcpy(nearest, &data.nearest, sizeof(*nearest));
1418 return data.nearest.index;
1423 * Raycast - BLI_bvhtree_ray_cast
1425 * raycast is done by performing a DFS on the BVHTree and saving the closest hit
1429 //Determines the distance that the ray must travel to hit the bounding volume of the given node
1430 static float ray_nearest_hit(BVHRayCastData *data, float *bv)
1434 float low = 0, upper = data->hit.dist;
1436 for(i=0; i != 3; i++, bv += 2)
1438 if(data->ray_dot_axis[i] == 0.0f)
1441 if(data->ray.origin[i] < bv[0] - data->ray.radius
1442 || data->ray.origin[i] > bv[1] + data->ray.radius)
1447 float ll = (bv[0] - data->ray.radius - data->ray.origin[i]) / data->ray_dot_axis[i];
1448 float lu = (bv[1] + data->ray.radius - data->ray.origin[i]) / data->ray_dot_axis[i];
1450 if(data->ray_dot_axis[i] > 0.0f)
1452 if(ll > low) low = ll;
1453 if(lu < upper) upper = lu;
1457 if(lu > low) low = lu;
1458 if(ll < upper) upper = ll;
1461 if(low > upper) return FLT_MAX;
1467 //Determines the distance that the ray must travel to hit the bounding volume of the given node
1468 //Based on Tactical Optimization of Ray/Box Intersection, by Graham Fyffe
1469 //[http://tog.acm.org/resources/RTNews/html/rtnv21n1.html#art9]
1471 //TODO this doens't has data->ray.radius in consideration
1472 static float fast_ray_nearest_hit(const BVHRayCastData *data, const BVHNode *node)
1474 const float *bv = node->bv;
1477 float t1x = (bv[data->index[0]] - data->ray.origin[0]) * data->idot_axis[0];
1478 float t2x = (bv[data->index[1]] - data->ray.origin[0]) * data->idot_axis[0];
1479 float t1y = (bv[data->index[2]] - data->ray.origin[1]) * data->idot_axis[1];
1480 float t2y = (bv[data->index[3]] - data->ray.origin[1]) * data->idot_axis[1];
1481 float t1z = (bv[data->index[4]] - data->ray.origin[2]) * data->idot_axis[2];
1482 float t2z = (bv[data->index[5]] - data->ray.origin[2]) * data->idot_axis[2];
1484 if(t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return FLT_MAX;
1485 if(t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return FLT_MAX;
1486 if(t1x > data->hit.dist || t1y > data->hit.dist || t1z > data->hit.dist) return FLT_MAX;
1489 if (t1y > dist) dist = t1y;
1490 if (t1z > dist) dist = t1z;
1494 static void dfs_raycast(BVHRayCastData *data, BVHNode *node)
1498 //ray-bv is really fast.. and simple tests revealed its worth to test it
1499 //before calling the ray-primitive functions
1500 float dist = fast_ray_nearest_hit(data, node);
1501 if(dist >= data->hit.dist) return;
1503 if(node->totnode == 0)
1506 data->callback(data->userdata, node->index, &data->ray, &data->hit);
1509 data->hit.index = node->index;
1510 data->hit.dist = dist;
1511 VECADDFAC(data->hit.co, data->ray.origin, data->ray.direction, dist);
1516 //pick loop direction to dive into the tree (based on ray direction and split axis)
1517 if(data->ray_dot_axis[ (int)node->main_axis ] > 0.0f)
1519 for(i=0; i != node->totnode; i++)
1521 dfs_raycast(data, node->children[i]);
1526 for(i=node->totnode-1; i >= 0; i--)
1528 dfs_raycast(data, node->children[i]);
1534 static void iterative_raycast(BVHRayCastData *data, BVHNode *node)
1538 float dist = fast_ray_nearest_hit(data, node);
1539 if(dist >= data->hit.dist)
1541 node = node->skip[1];
1545 if(node->totnode == 0)
1548 data->callback(data->userdata, node->index, &data->ray, &data->hit);
1551 data->hit.index = node->index;
1552 data->hit.dist = dist;
1553 VECADDFAC(data->hit.co, data->ray.origin, data->ray.direction, dist);
1556 node = node->skip[1];
1560 node = node->children[0];
1565 int BLI_bvhtree_ray_cast(BVHTree *tree, const float *co, const float *dir, float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
1568 BVHRayCastData data;
1569 BVHNode * root = tree->nodes[tree->totleaf];
1573 data.callback = callback;
1574 data.userdata = userdata;
1576 VECCOPY(data.ray.origin, co);
1577 VECCOPY(data.ray.direction, dir);
1578 data.ray.radius = radius;
1580 Normalize(data.ray.direction);
1584 data.ray_dot_axis[i] = INPR( data.ray.direction, KDOP_AXES[i]);
1585 data.idot_axis[i] = 1.0f / data.ray_dot_axis[i];
1587 if(fabs(data.ray_dot_axis[i]) < FLT_EPSILON)
1589 data.ray_dot_axis[i] = 0.0;
1591 data.index[2*i] = data.idot_axis[i] < 0.0 ? 1 : 0;
1592 data.index[2*i+1] = 1 - data.index[2*i];
1593 data.index[2*i] += 2*i;
1594 data.index[2*i+1] += 2*i;
1599 memcpy( &data.hit, hit, sizeof(*hit) );
1602 data.hit.index = -1;
1603 data.hit.dist = FLT_MAX;
1608 dfs_raycast(&data, root);
1609 // iterative_raycast(&data, root);
1614 memcpy( hit, &data.hit, sizeof(*hit) );
1616 return data.hit.index;
1619 float BLI_bvhtree_bb_raycast(float *bv, float *light_start, float *light_end, float *pos)
1621 BVHRayCastData data;
1624 data.hit.dist = FLT_MAX;
1626 // get light direction
1627 data.ray.direction[0] = light_end[0] - light_start[0];
1628 data.ray.direction[1] = light_end[1] - light_start[1];
1629 data.ray.direction[2] = light_end[2] - light_start[2];
1631 data.ray.radius = 0.0;
1633 data.ray.origin[0] = light_start[0];
1634 data.ray.origin[1] = light_start[1];
1635 data.ray.origin[2] = light_start[2];
1637 Normalize(data.ray.direction);
1638 VECCOPY(data.ray_dot_axis, data.ray.direction);
1640 dist = ray_nearest_hit(&data, bv);
1644 VECADDFAC(pos, light_start, data.ray.direction, dist);