2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2006 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Daniel Genrich, Andre Pinto
25 * ***** END GPL LICENSE BLOCK *****
29 #ifndef __BLI_KDOPBVH_H__
30 #define __BLI_KDOPBVH_H__
32 /** \file BLI_kdopbvh.h
34 * \author Daniel Genrich
43 typedef struct BVHTree BVHTree;
44 #define USE_KDOPBVH_WATERTIGHT
46 typedef struct BVHTreeAxisRange {
51 /* alternate access */
56 typedef struct BVHTreeOverlap {
61 typedef struct BVHTreeNearest {
62 int index; /* the index of the nearest found (untouched if none is found within a dist radius from the given coordinates) */
63 float co[3]; /* nearest coordinates (untouched it none is found within a dist radius from the given coordinates) */
64 float no[3]; /* normal at nearest coordinates (untouched it none is found within a dist radius from the given coordinates) */
65 float dist_sq; /* squared distance to search around */
69 typedef struct BVHTreeRay {
70 float origin[3]; /* ray origin */
71 float direction[3]; /* ray direction */
72 float radius; /* radius around ray */
73 #ifdef USE_KDOPBVH_WATERTIGHT
74 struct IsectRayPrecalc *isect_precalc;
78 typedef struct BVHTreeRayHit {
79 int index; /* index of the tree node (untouched if no hit is found) */
80 float co[3]; /* coordinates of the hit point */
81 float no[3]; /* normal on hit point */
82 float dist; /* distance to the hit point */
86 /* calculate IsectRayPrecalc data */
87 BVH_RAYCAST_WATERTIGHT = (1 << 0),
89 #define BVH_RAYCAST_DEFAULT (BVH_RAYCAST_WATERTIGHT)
90 #define BVH_RAYCAST_DIST_MAX (FLT_MAX / 2.0f)
92 /* callback must update nearest in case it finds a nearest result */
93 typedef void (*BVHTree_NearestPointCallback)(void *userdata, int index, const float co[3], BVHTreeNearest *nearest);
95 /* callback must update hit in case it finds a nearest successful hit */
96 typedef void (*BVHTree_RayCastCallback)(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit);
98 /* callback to check if 2 nodes overlap (use thread if intersection results need to be stored) */
99 typedef bool (*BVHTree_OverlapCallback)(void *userdata, int index_a, int index_b, int thread);
101 /* callback to range search query */
102 typedef void (*BVHTree_RangeQuery)(void *userdata, int index, const float co[3], float dist_sq);
105 /* callbacks to BLI_bvhtree_walk_dfs */
106 /* return true to traverse into this nodes children, else skip. */
107 typedef bool (*BVHTree_WalkParentCallback)(const BVHTreeAxisRange *bounds, void *userdata);
108 /* return true to keep walking, else early-exit the search. */
109 typedef bool (*BVHTree_WalkLeafCallback)(const BVHTreeAxisRange *bounds, int index, void *userdata);
110 /* return true to search (min, max) else (max, min). */
111 typedef bool (*BVHTree_WalkOrderCallback)(const BVHTreeAxisRange *bounds, char axis, void *userdata);
114 BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis);
115 void BLI_bvhtree_free(BVHTree *tree);
117 /* construct: first insert points, then call balance */
118 void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints);
119 void BLI_bvhtree_balance(BVHTree *tree);
121 /* update: first update points/nodes, then call update_tree to refit the bounding volumes */
122 bool BLI_bvhtree_update_node(BVHTree *tree, int index, const float co[3], const float co_moving[3], int numpoints);
123 void BLI_bvhtree_update_tree(BVHTree *tree);
125 int BLI_bvhtree_overlap_thread_num(const BVHTree *tree);
127 /* collision/overlap: check two trees if they overlap, alloc's *overlap with length of the int return value */
128 BVHTreeOverlap *BLI_bvhtree_overlap(
129 const BVHTree *tree1, const BVHTree *tree2, unsigned int *r_overlap_tot,
130 BVHTree_OverlapCallback callback, void *userdata);
132 int BLI_bvhtree_get_len(const BVHTree *tree);
134 float BLI_bvhtree_get_epsilon(const BVHTree *tree);
136 /* find nearest node to the given coordinates
137 * (if nearest is given it will only search nodes where square distance is smaller than nearest->dist) */
138 int BLI_bvhtree_find_nearest(
139 BVHTree *tree, const float co[3], BVHTreeNearest *nearest,
140 BVHTree_NearestPointCallback callback, void *userdata);
142 int BLI_bvhtree_ray_cast_ex(
143 BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit,
144 BVHTree_RayCastCallback callback, void *userdata,
146 int BLI_bvhtree_ray_cast(
147 BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit,
148 BVHTree_RayCastCallback callback, void *userdata);
150 void BLI_bvhtree_ray_cast_all_ex(
151 BVHTree *tree, const float co[3], const float dir[3], float radius, float hit_dist,
152 BVHTree_RayCastCallback callback, void *userdata,
154 void BLI_bvhtree_ray_cast_all(
155 BVHTree *tree, const float co[3], const float dir[3], float radius, float hit_dist,
156 BVHTree_RayCastCallback callback, void *userdata);
158 float BLI_bvhtree_bb_raycast(const float bv[6], const float light_start[3], const float light_end[3], float pos[3]);
161 int BLI_bvhtree_range_query(
162 BVHTree *tree, const float co[3], float radius,
163 BVHTree_RangeQuery callback, void *userdata);
165 void BLI_bvhtree_walk_dfs(
167 BVHTree_WalkParentCallback walk_parent_cb,
168 BVHTree_WalkLeafCallback walk_leaf_cb,
169 BVHTree_WalkOrderCallback walk_order_cb,
173 /* expose for bvh callbacks to use */
174 extern const float bvhtree_kdop_axes[13][3];
180 #endif /* __BLI_KDOPBVH_H__ */