Revision: 15102
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15102
Author:   genscher
Date:     2008-06-03 20:53:03 +0200 (Tue, 03 Jun 2008)

Log Message:
-----------
last commit didn't include new files and included old file

Added Paths:
-----------
    trunk/blender/source/blender/blenlib/BLI_kdopbvh.h
    trunk/blender/source/blender/blenlib/intern/BLI_kdopbvh.c

Removed Paths:
-------------
    trunk/blender/source/blender/blenkernel/intern/kdop.c

Deleted: trunk/blender/source/blender/blenkernel/intern/kdop.c
===================================================================

Added: trunk/blender/source/blender/blenlib/BLI_kdopbvh.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_kdopbvh.h                          
(rev 0)
+++ trunk/blender/source/blender/blenlib/BLI_kdopbvh.h  2008-06-03 18:53:03 UTC 
(rev 15102)
@@ -0,0 +1,60 @@
+/**
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2006 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Daniel Genrich, Andre Pinto
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+
+#ifndef BLI_KDOPBVH_H
+#define BLI_KDOPBVH_H
+
+#include <float.h>
+
+struct BVHTree;
+typedef struct BVHTree BVHTree;
+
+typedef struct BVHTreeOverlap {
+       int indexA;
+       int indexB;
+} BVHTreeOverlap;
+
+BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char 
axis);
+void BLI_bvhtree_free(BVHTree *tree);
+
+/* construct: first insert points, then call balance */
+int BLI_bvhtree_insert(BVHTree *tree, int index, float *co, int numpoints);
+void BLI_bvhtree_balance(BVHTree *tree);
+
+/* update: first update points/nodes, then call update_tree to refit the 
bounding volumes */
+int BLI_bvhtree_update_node(BVHTree *tree, int index, float *co, float 
*co_moving, int numpoints);
+void BLI_bvhtree_update_tree(BVHTree *tree);
+
+/* collision/overlap: check two trees if they overlap, alloc's *overlap with 
length of the int return value */
+BVHTreeOverlap *BLI_bvhtree_overlap(BVHTree *tree1, BVHTree *tree2, int 
*result);
+
+float BLI_bvhtree_getepsilon(BVHTree *tree);
+
+#endif // BLI_KDOPBVH_H
+

Added: trunk/blender/source/blender/blenlib/intern/BLI_kdopbvh.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_kdopbvh.c                   
        (rev 0)
+++ trunk/blender/source/blender/blenlib/intern/BLI_kdopbvh.c   2008-06-03 
18:53:03 UTC (rev 15102)
@@ -0,0 +1,811 @@
+/**
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2006 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Daniel Genrich, Andre Pinto
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "math.h"
+#include <stdio.h>
+#include <stdlib.h> 
+#include <string.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BKE_utildefines.h"
+
+#include "BLI_kdopbvh.h"
+#include "BLI_arithb.h"
+
+#ifdef _OPENMP
+#include <omp.h>
+#endif
+
+typedef struct BVHNode
+{
+       struct BVHNode **children; // max 8 children
+       struct BVHNode *parent; // needed for bottom - top update
+       float *bv; // Bounding volume of all nodes, max 13 axis
+       int index; /* face, edge, vertex index */
+       char totnode; // how many nodes are used, used for speedup
+       char traversed;  // how many nodes already traversed until this level?
+       char main_axis;
+} BVHNode;
+
+struct BVHTree
+{
+       BVHNode **nodes;
+       BVHNode *nodearray; /* pre-alloc branch nodes */
+       BVHNode **nodechild;    // pre-alloc childs for nodes
+       float   *nodebv;                // pre-alloc bounding-volumes for nodes
+       float   epsilon; /* epslion is used for inflation of the k-dop     */
+       int     totleaf; // leafs
+       int     totbranch;
+       char    tree_type; // type of tree (4 => quadtree)
+       char    axis; // kdop type (6 => OBB, 7 => AABB, ...)
+       char    start_axis, stop_axis; // KDOP_AXES array indices according to 
axis
+};
+
+typedef struct BVHOverlapData 
+{  
+       BVHTree *tree1, *tree2; 
+       BVHTreeOverlap *overlap; 
+       int i, max_overlap; /* i is number of overlaps */
+} BVHOverlapData;
+////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////////
+// Bounding Volume Hierarchy Definition
+// 
+// Notes: From OBB until 26-DOP --> all bounding volumes possible, just choose 
type below
+// Notes: You have to choose the type at compile time ITM
+// Notes: You can choose the tree type --> binary, quad, octree, choose below
+////////////////////////////////////////////////////////////////////////
+
+static float KDOP_AXES[13][3] =
+{ {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},
+{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},
+{0, 1.0, -1.0}
+};
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////
+// Introsort 
+// with permission deriven from the following Java code:
+// http://ralphunden.net/content/tutorials/a-guide-to-introsort/
+// and he derived it from the SUN STL 
+//////////////////////////////////////////////////////////////////////////////////////////////////////
+static int size_threshold = 16;
+/*
+* Common methods for all algorithms
+*/
+static int floor_lg(int a)
+{
+       return (int)(floor(log(a)/log(2)));
+}
+
+/*
+* Insertion sort algorithm
+*/
+static void bvh_insertionsort(BVHNode **a, int lo, int hi, int axis)
+{
+       int i,j;
+       BVHNode *t;
+       for (i=lo; i < hi; i++)
+       {
+               j=i;
+               t = a[i];
+               while((j!=lo) && (t->bv[axis] < (a[j-1])->bv[axis]))
+               {
+                       a[j] = a[j-1];
+                       j--;
+               }
+               a[j] = t;
+       }
+}
+
+static int bvh_partition(BVHNode **a, int lo, int hi, BVHNode * x, int axis)
+{
+       int i=lo, j=hi;
+       while (1)
+       {
+               while ((a[i])->bv[axis] < x->bv[axis]) i++;
+               j--;
+               while (x->bv[axis] < (a[j])->bv[axis]) j--;
+               if(!(i < j))
+                       return i;
+               SWAP( BVHNode* , a[i], a[j]);
+               i++;
+       }
+}
+
+/*
+* Heapsort algorithm
+*/
+static void bvh_downheap(BVHNode **a, int i, int n, int lo, int axis)
+{
+       BVHNode * d = a[lo+i-1];
+       int child;
+       while (i<=n/2)
+       {
+               child = 2*i;
+               if ((child < n) && ((a[lo+child-1])->bv[axis] < 
(a[lo+child])->bv[axis]))
+               {
+                       child++;
+               }
+               if (!(d->bv[axis] < (a[lo+child-1])->bv[axis])) break;
+               a[lo+i-1] = a[lo+child-1];
+               i = child;
+       }
+       a[lo+i-1] = d;
+}
+
+static void bvh_heapsort(BVHNode **a, int lo, int hi, int axis)
+{
+       int n = hi-lo, i;
+       for (i=n/2; i>=1; i=i-1)
+       {
+               bvh_downheap(a, i,n,lo, axis);
+       }
+       for (i=n; i>1; i=i-1)
+       {
+               SWAP(BVHNode*, a[lo],a[lo+i-1]);
+               bvh_downheap(a, 1,i-1,lo, axis);
+       }
+}
+
+static BVHNode *bvh_medianof3(BVHNode **a, int lo, int mid, int hi, int axis) 
// returns Sortable
+{
+       if ((a[mid])->bv[axis] < (a[lo])->bv[axis])
+       {
+               if ((a[hi])->bv[axis] < (a[mid])->bv[axis])
+                       return a[mid];
+               else
+               {
+                       if ((a[hi])->bv[axis] < (a[lo])->bv[axis])
+                               return a[hi];
+                       else
+                               return a[lo];
+               }
+       }
+       else
+       {
+               if ((a[hi])->bv[axis] < (a[mid])->bv[axis])
+               {
+                       if ((a[hi])->bv[axis] < (a[lo])->bv[axis])
+                               return a[lo];
+                       else
+                               return a[hi];
+               }
+               else
+                       return a[mid];
+       }
+}
+/*
+* Quicksort algorithm modified for Introsort
+*/
+static void bvh_introsort_loop (BVHNode **a, int lo, int hi, int depth_limit, 
int axis)
+{
+       int p;
+
+       while (hi-lo > size_threshold)
+       {
+               if (depth_limit == 0)
+               {
+                       bvh_heapsort(a, lo, hi, axis);
+                       return;
+               }
+               depth_limit=depth_limit-1;
+               p=bvh_partition(a, lo, hi, bvh_medianof3(a, lo, 
lo+((hi-lo)/2)+1, hi-1, axis), axis);
+               bvh_introsort_loop(a, p, hi, depth_limit, axis);
+               hi=p;
+       }
+}
+
+static void sort(BVHNode **a0, int begin, int end, int axis)
+{
+       if (begin < end)
+       {
+               BVHNode **a=a0;
+               bvh_introsort_loop(a, begin, end, 2*floor_lg(end-begin), axis);
+               bvh_insertionsort(a, begin, end, axis);
+       }
+}
+void sort_along_axis(BVHTree *tree, int start, int end, int axis)
+{
+       sort(tree->nodes, start, end, axis);
+}
+
+//after a call to this function you can expect one of:
+//      every node to left of a[n] are smaller or equal to it
+//      every node to the right of a[n] are greater or equal to it
+int partition_nth_element(BVHNode **a, int _begin, int _end, int n, int axis){ 
       
+       int begin = _begin, end = _end, cut;        
+       int i;         
+       while(end-begin > 3)        
+       {                            
+               cut = bvh_partition(a, begin, end, bvh_medianof3(a, begin, 
(begin+end)/2, end-1, axis), axis );                 
+               if(cut <= n)                        
+                       begin = cut;                
+               else                        
+                       end = cut;        
+       }        
+       bvh_insertionsort(a, begin, end, axis);
+
+       return n;
+}
+
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void BLI_bvhtree_free(BVHTree *tree)
+{      
+       if(tree)
+       {
+               MEM_freeN(tree->nodes);
+               MEM_freeN(tree->nodearray);
+               MEM_freeN(tree->nodebv);
+               MEM_freeN(tree->nodechild);
+               MEM_freeN(tree);
+       }
+}
+
+BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
+{
+       BVHTree *tree;
+       int numbranches=0, i;
+       
+       // only support up to octree
+       if(tree_type > 8)
+               return NULL;
+
+       tree = (BVHTree *)MEM_callocN(sizeof(BVHTree), "BVHTree");
+       
+       if(tree)
+       {
+               tree->epsilon = epsilon;
+               tree->tree_type = tree_type; 
+               tree->axis = axis;
+               
+               if(axis == 26)
+               {
+                       tree->start_axis = 0;
+                       tree->stop_axis = 13;
+               }
+               else if(axis == 18)
+               {
+                       tree->start_axis = 7;
+                       tree->stop_axis = 13;
+               }

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to