Blender V2.61 - r43446
Classes | Defines | Typedefs | Functions | Variables

BLI_kdopbvh.c File Reference

#include <assert.h>
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BLI_kdopbvh.h"
#include "BLI_math.h"

Go to the source code of this file.

Classes

class  BVHNode
struct  BVHTree
struct  BVHOverlapData
struct  BVHNearestData
struct  BVHRayCastData
struct  BVHBuildHelper
struct  NodeDistance
struct  RangeQueryData

Defines

#define MAX_TREETYPE   32
#define DEFAULT_FIND_NEAREST_HEAP_SIZE   1024
#define PUSH_HEAP_BODY(HEAP_TYPE, PRIORITY, heap, heap_size)
#define POP_HEAP_BODY(HEAP_TYPE, PRIORITY, heap, heap_size)
#define NodeDistance_priority(a, b)   ( (a).dist < (b).dist )

Typedefs

typedef struct BVHNode BVHNode
typedef struct BVHOverlapData BVHOverlapData
typedef struct BVHNearestData BVHNearestData
typedef struct BVHRayCastData BVHRayCastData
typedef struct BVHBuildHelper BVHBuildHelper
typedef struct NodeDistance NodeDistance
typedef struct RangeQueryData RangeQueryData

Functions

static void bvh_insertionsort (BVHNode **a, int lo, int hi, int axis)
static int bvh_partition (BVHNode **a, int lo, int hi, BVHNode *x, int axis)
static BVHNodebvh_medianof3 (BVHNode **a, int lo, int mid, int hi, int axis)
static int partition_nth_element (BVHNode **a, int _begin, int _end, int n, int axis)
static void build_skip_links (BVHTree *tree, BVHNode *node, BVHNode *left, BVHNode *right)
static void create_kdop_hull (BVHTree *tree, BVHNode *node, const float *co, int numpoints, int moving)
static void refit_kdop_hull (BVHTree *tree, BVHNode *node, int start, int end)
static char get_largest_axis (float *bv)
static void node_join (BVHTree *tree, BVHNode *node)
static void build_implicit_tree_helper (BVHTree *tree, BVHBuildHelper *data)
static int implicit_leafs_index (BVHBuildHelper *data, int depth, int child_index)
static int implicit_needed_branches (int tree_type, int leafs)
static void split_leafs (BVHNode **leafs_array, int *nth, int partitions, int split_axis)
static void non_recursive_bvh_div_nodes (BVHTree *tree, BVHNode *branches_array, BVHNode **leafs_array, int num_leafs)
BVHTreeBLI_bvhtree_new (int maxsize, float epsilon, char tree_type, char axis)
void BLI_bvhtree_free (BVHTree *tree)
void BLI_bvhtree_balance (BVHTree *tree)
int BLI_bvhtree_insert (BVHTree *tree, int index, const float *co, int numpoints)
int BLI_bvhtree_update_node (BVHTree *tree, int index, const float *co, const float *co_moving, int numpoints)
void BLI_bvhtree_update_tree (BVHTree *tree)
float BLI_bvhtree_getepsilon (BVHTree *tree)
static int tree_overlap (BVHNode *node1, BVHNode *node2, int start_axis, int stop_axis)
static void traverse (BVHOverlapData *data, BVHNode *node1, BVHNode *node2)
BVHTreeOverlapBLI_bvhtree_overlap (BVHTree *tree1, BVHTree *tree2, unsigned int *result)
static float calc_nearest_point (const float proj[3], BVHNode *node, float *nearest)
static void dfs_find_nearest_dfs (BVHNearestData *data, BVHNode *node)
static void dfs_find_nearest_begin (BVHNearestData *data, BVHNode *node)
int BLI_bvhtree_find_nearest (BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
static float ray_nearest_hit (BVHRayCastData *data, float *bv)
static float fast_ray_nearest_hit (const BVHRayCastData *data, const BVHNode *node)
static void dfs_raycast (BVHRayCastData *data, BVHNode *node)
int BLI_bvhtree_ray_cast (BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
float BLI_bvhtree_bb_raycast (float *bv, const float light_start[3], const float light_end[3], float pos[3])
static void dfs_range_query (RangeQueryData *data, BVHNode *node)
int BLI_bvhtree_range_query (BVHTree *tree, const float co[3], float radius, BVHTree_RangeQuery callback, void *userdata)

Variables

static float KDOP_AXES [13][3]

Detailed Description

Definition in file BLI_kdopbvh.c.


Define Documentation

#define DEFAULT_FIND_NEAREST_HEAP_SIZE   1024

Definition at line 51 of file BLI_kdopbvh.c.

#define MAX_TREETYPE   32

Definition at line 50 of file BLI_kdopbvh.c.

Referenced by BLI_bvhtree_new(), and non_recursive_bvh_div_nodes().

#define NodeDistance_priority (   a,
 
)    ( (a).dist < (b).dist )

Definition at line 1243 of file BLI_kdopbvh.c.

#define POP_HEAP_BODY (   HEAP_TYPE,
  PRIORITY,
  heap,
  heap_size 
)
Value:
{                                                   \
    HEAP_TYPE element = heap[heap_size-1];          \
    int parent = 0;                                 \
    while(parent < (heap_size-1)/2 )                \
    {                                               \
        int child2 = (parent+1)*2;                  \
        if(PRIORITY(heap[child2-1], heap[child2]))  \
            --child2;                               \
                                                    \
        if(PRIORITY(element, heap[child2]))         \
            break;                                  \
                                                    \
        heap[parent] = heap[child2];                \
        parent = child2;                            \
    }                                               \
    heap[parent] = element;                         \
}

Definition at line 149 of file BLI_kdopbvh.c.

#define PUSH_HEAP_BODY (   HEAP_TYPE,
  PRIORITY,
  heap,
  heap_size 
)
Value:
{                                                   \
    HEAP_TYPE element = heap[heap_size-1];          \
    int child = heap_size-1;                        \
    while(child != 0)                               \
    {                                               \
        int parent = (child-1) / 2;                 \
        if(PRIORITY(element, heap[parent]))         \
        {                                           \
            heap[child] = heap[parent];             \
            child = parent;                         \
        }                                           \
        else break;                                 \
    }                                               \
    heap[child] = element;                          \
}

Definition at line 132 of file BLI_kdopbvh.c.


Typedef Documentation

typedef struct BVHNode BVHNode
typedef struct NodeDistance NodeDistance

Function Documentation

void BLI_bvhtree_balance ( BVHTree tree)
float BLI_bvhtree_bb_raycast ( float *  bv,
const float  light_start[3],
const float  light_end[3],
float  pos[3] 
)
int BLI_bvhtree_find_nearest ( BVHTree tree,
const float  co[3],
BVHTreeNearest nearest,
BVHTree_NearestPointCallback  callback,
void *  userdata 
)
void BLI_bvhtree_free ( BVHTree tree)
float BLI_bvhtree_getepsilon ( BVHTree tree)

Definition at line 1048 of file BLI_kdopbvh.c.

References BVHTree::epsilon.

Referenced by cloth_collision(), cloth_collision_response_static(), and deformVerts().

int BLI_bvhtree_insert ( BVHTree tree,
int  index,
const float *  co,
int  numpoints 
)
BVHTree* BLI_bvhtree_new ( int  maxsize,
float  epsilon,
char  tree_type,
char  axis 
)
BVHTreeOverlap* BLI_bvhtree_overlap ( BVHTree tree1,
BVHTree tree2,
unsigned int *  result 
)
int BLI_bvhtree_range_query ( BVHTree tree,
const float  co[3],
float  radius,
BVHTree_RangeQuery  callback,
void *  userdata 
)
int BLI_bvhtree_ray_cast ( BVHTree tree,
const float  co[3],
const float  dir[3],
float  radius,
BVHTreeRayHit hit,
BVHTree_RayCastCallback  callback,
void *  userdata 
)
int BLI_bvhtree_update_node ( BVHTree tree,
int  index,
const float *  co,
const float *  co_moving,
int  numpoints 
)
void BLI_bvhtree_update_tree ( BVHTree tree)
static void build_implicit_tree_helper ( BVHTree tree,
BVHBuildHelper data 
) [static]
static void build_skip_links ( BVHTree tree,
BVHNode node,
BVHNode left,
BVHNode right 
) [static]

Definition at line 369 of file BLI_kdopbvh.c.

References BVHNode::children, i, left(), BVHNode::skip, and BVHNode::totnode.

Referenced by BLI_bvhtree_balance().

static void bvh_insertionsort ( BVHNode **  a,
int  lo,
int  hi,
int  axis 
) [static]

Definition at line 214 of file BLI_kdopbvh.c.

References BVHNode::bv, and i.

Referenced by partition_nth_element().

static BVHNode* bvh_medianof3 ( BVHNode **  a,
int  lo,
int  mid,
int  hi,
int  axis 
) [static]

Definition at line 283 of file BLI_kdopbvh.c.

Referenced by partition_nth_element().

static int bvh_partition ( BVHNode **  a,
int  lo,
int  hi,
BVHNode x,
int  axis 
) [static]

Definition at line 231 of file BLI_kdopbvh.c.

References BVHNode::bv, i, and SWAP.

Referenced by partition_nth_element().

static float calc_nearest_point ( const float  proj[3],
BVHNode node,
float *  nearest 
) [static]
static void create_kdop_hull ( BVHTree tree,
BVHNode node,
const float *  co,
int  numpoints,
int  moving 
) [static]

Definition at line 390 of file BLI_kdopbvh.c.

References BVHNode::bv, dot_v3v3(), FLT_MAX, i, KDOP_AXES, and BVHTree::start_axis.

Referenced by BLI_bvhtree_insert(), and BLI_bvhtree_update_node().

static void dfs_find_nearest_begin ( BVHNearestData data,
BVHNode node 
) [static]
static void dfs_find_nearest_dfs ( BVHNearestData data,
BVHNode node 
) [static]
static void dfs_range_query ( RangeQueryData data,
BVHNode node 
) [static]
static void dfs_raycast ( BVHRayCastData data,
BVHNode node 
) [static]
static float fast_ray_nearest_hit ( const BVHRayCastData data,
const BVHNode node 
) [static]
static char get_largest_axis ( float *  bv) [static]

Definition at line 453 of file BLI_kdopbvh.c.

Referenced by non_recursive_bvh_div_nodes().

static int implicit_leafs_index ( BVHBuildHelper data,
int  depth,
int  child_index 
) [static]
static int implicit_needed_branches ( int  tree_type,
int  leafs 
) [static]

Generalized implicit tree build

An implicit tree is a tree where its structure is implied, thus there is no need to store child pointers or indexs. Its possible to find the position of the child or the parent with simple maths (multiplication and adittion). This type of tree is for example used on heaps.. where node N has its childs at indexs N*2 and N*2+1.

Altought in this case the tree type is general.. and not know until runtime. tree_type stands for the maximum number of childs that a tree node can have. All tree types >= 2 are supported.

Advantages of the used trees include:

  • No need to store child/parent relations (they are implicit);
  • Any node child always has an index greater than the parent;
  • Brother nodes are sequencial in memory;

Some math relations derived for general implicit trees:

K = tree_type, ( 2 <= K ) ROOT = 1 N child of node A = A * K + (2 - K) + N, (0 <= N < K)

Util methods: TODO... (looping elements, knowing if its a leaf or not.. etc...)

Definition at line 681 of file BLI_kdopbvh.c.

References MAX2.

Referenced by BLI_bvhtree_balance(), BLI_bvhtree_new(), and non_recursive_bvh_div_nodes().

static void node_join ( BVHTree tree,
BVHNode node 
) [static]
static void non_recursive_bvh_div_nodes ( BVHTree tree,
BVHNode branches_array,
BVHNode **  leafs_array,
int  num_leafs 
) [static]
static int partition_nth_element ( BVHNode **  a,
int  _begin,
int  _end,
int  n,
int  axis 
) [static]

Definition at line 352 of file BLI_kdopbvh.c.

References bvh_insertionsort(), bvh_medianof3(), and bvh_partition().

Referenced by split_leafs().

static float ray_nearest_hit ( BVHRayCastData data,
float *  bv 
) [static]
static void refit_kdop_hull ( BVHTree tree,
BVHNode node,
int  start,
int  end 
) [static]

Definition at line 421 of file BLI_kdopbvh.c.

References BVHNode::bv, FLT_MAX, i, BVHTree::nodes, and BVHTree::start_axis.

Referenced by non_recursive_bvh_div_nodes().

static void split_leafs ( BVHNode **  leafs_array,
int *  nth,
int  partitions,
int  split_axis 
) [static]

Definition at line 698 of file BLI_kdopbvh.c.

References i, and partition_nth_element().

Referenced by non_recursive_bvh_div_nodes().

static void traverse ( BVHOverlapData data,
BVHNode node1,
BVHNode node2 
) [static]
static int tree_overlap ( BVHNode node1,
BVHNode node2,
int  start_axis,
int  stop_axis 
) [static]

Definition at line 1058 of file BLI_kdopbvh.c.

References BVHNode::bv.

Referenced by BLI_bvhtree_overlap(), and traverse().


Variable Documentation

float KDOP_AXES[13][3] [static]
Initial value:
{ {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}
}

Definition at line 123 of file BLI_kdopbvh.c.

Referenced by BLI_bvhtree_find_nearest(), BLI_bvhtree_ray_cast(), and create_kdop_hull().