Blender  V2.93
BLI_kdopbvh.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 #include "BLI_sys_types.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct BVHTree;
34 
35 typedef struct BVHTree BVHTree;
36 #define USE_KDOPBVH_WATERTIGHT
37 
38 typedef struct BVHTreeAxisRange {
39  union {
40  struct {
41  float min, max;
42  };
43  /* alternate access */
44  float range[2];
45  };
47 
48 typedef struct BVHTreeOverlap {
49  int indexA;
50  int indexB;
52 
53 typedef struct BVHTreeNearest {
56  int index;
59  float co[3];
62  float no[3];
64  float dist_sq;
65  int flags;
67 
68 typedef struct BVHTreeRay {
70  float origin[3];
72  float direction[3];
74  float radius;
75 #ifdef USE_KDOPBVH_WATERTIGHT
77 #endif
79 
80 typedef struct BVHTreeRayHit {
82  int index;
84  float co[3];
86  float no[3];
88  float dist;
90 
91 enum {
92  /* Use a priority queue to process nodes in the optimal order (for slow callbacks) */
95 };
96 enum {
97  /* Use a priority queue to process nodes in the optimal order (for slow callbacks) */
99 };
100 enum {
101  /* calculate IsectRayPrecalc data */
103 };
104 #define BVH_RAYCAST_DEFAULT (BVH_RAYCAST_WATERTIGHT)
105 #define BVH_RAYCAST_DIST_MAX (FLT_MAX / 2.0f)
106 
107 /* callback must update nearest in case it finds a nearest result */
108 typedef void (*BVHTree_NearestPointCallback)(void *userdata,
109  int index,
110  const float co[3],
111  BVHTreeNearest *nearest);
112 
113 /* callback must update hit in case it finds a nearest successful hit */
114 typedef void (*BVHTree_RayCastCallback)(void *userdata,
115  int index,
116  const BVHTreeRay *ray,
117  BVHTreeRayHit *hit);
118 
119 /* callback to check if 2 nodes overlap (use thread if intersection results need to be stored) */
120 typedef bool (*BVHTree_OverlapCallback)(void *userdata, int index_a, int index_b, int thread);
121 
122 /* callback to range search query */
123 typedef void (*BVHTree_RangeQuery)(void *userdata, int index, const float co[3], float dist_sq);
124 
125 /* callback to find nearest projected */
126 typedef void (*BVHTree_NearestProjectedCallback)(void *userdata,
127  int index,
128  const struct DistProjectedAABBPrecalc *precalc,
129  const float (*clip_plane)[4],
130  const int clip_plane_len,
131  BVHTreeNearest *nearest);
132 
133 /* callbacks to BLI_bvhtree_walk_dfs */
134 /* return true to traverse into this nodes children, else skip. */
135 typedef bool (*BVHTree_WalkParentCallback)(const BVHTreeAxisRange *bounds, void *userdata);
136 /* return true to keep walking, else early-exit the search. */
138  int index,
139  void *userdata);
140 /* return true to search (min, max) else (max, min). */
142  char axis,
143  void *userdata);
144 
145 BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis);
147 
148 /* construct: first insert points, then call balance */
149 void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints);
151 
152 /* update: first update points/nodes, then call update_tree to refit the bounding volumes */
154  BVHTree *tree, int index, const float co[3], const float co_moving[3], int numpoints);
156 
158 
159 /* collision/overlap: check two trees if they overlap,
160  * alloc's *overlap with length of the int return value */
162  const BVHTree *tree1,
163  const BVHTree *tree2,
164  uint *r_overlap_tot,
165  /* optional callback to test the overlap before adding (must be thread-safe!) */
167  void *userdata,
168  const uint max_interactions,
169  const int flag);
171  const BVHTree *tree2,
172  unsigned int *r_overlap_tot,
174  void *userdata);
175 
176 int *BLI_bvhtree_intersect_plane(BVHTree *tree, float plane[4], uint *r_intersect_tot);
177 
178 int BLI_bvhtree_get_len(const BVHTree *tree);
180 float BLI_bvhtree_get_epsilon(const BVHTree *tree);
181 void BLI_bvhtree_get_bounding_box(BVHTree *tree, float r_bb_min[3], float r_bb_max[3]);
182 
183 /* find nearest node to the given coordinates
184  * (if nearest is given it will only search nodes where
185  * square distance is smaller than nearest->dist) */
187  const float co[3],
188  BVHTreeNearest *nearest,
190  void *userdata,
191  int flag);
193  const float co[3],
194  BVHTreeNearest *nearest,
196  void *userdata);
197 
199  const float co[3],
200  const float dist_sq,
202  void *userdata);
203 
205  const float co[3],
206  const float dir[3],
207  float radius,
208  BVHTreeRayHit *hit,
210  void *userdata,
211  int flag);
213  const float co[3],
214  const float dir[3],
215  float radius,
216  BVHTreeRayHit *hit,
218  void *userdata);
219 
221  const float co[3],
222  const float dir[3],
223  float radius,
224  float hit_dist,
226  void *userdata,
227  int flag);
229  const float co[3],
230  const float dir[3],
231  float radius,
232  float hit_dist,
234  void *userdata);
235 
236 float BLI_bvhtree_bb_raycast(const float bv[6],
237  const float light_start[3],
238  const float light_end[3],
239  float pos[3]);
240 
241 /* range query */
243  BVHTree *tree, const float co[3], float radius, BVHTree_RangeQuery callback, void *userdata);
244 
246  float projmat[4][4],
247  float winsize[2],
248  float mval[2],
249  float clip_planes[6][4],
250  int clip_plane_len,
251  BVHTreeNearest *nearest,
253  void *userdata);
254 
256  BVHTree_WalkParentCallback walk_parent_cb,
257  BVHTree_WalkLeafCallback walk_leaf_cb,
258  BVHTree_WalkOrderCallback walk_order_cb,
259  void *userdata);
260 
261 /* expose for bvh callbacks to use */
262 extern const float bvhtree_kdop_axes[13][3];
263 
264 #ifdef __cplusplus
265 }
266 #endif
typedef float(TangentPoint)[2]
struct BVHTreeNearest BVHTreeNearest
int BLI_bvhtree_range_query(BVHTree *tree, const float co[3], float radius, BVHTree_RangeQuery callback, void *userdata)
Definition: BLI_kdopbvh.c:2131
@ BVH_NEAREST_OPTIMAL_ORDER
Definition: BLI_kdopbvh.h:98
struct BVHTreeOverlap BVHTreeOverlap
int BLI_bvhtree_get_tree_type(const BVHTree *tree)
Definition: BLI_kdopbvh.c:1069
@ BVH_OVERLAP_USE_THREADING
Definition: BLI_kdopbvh.h:93
@ BVH_OVERLAP_RETURN_PAIRS
Definition: BLI_kdopbvh.h:94
void BLI_bvhtree_ray_cast_all_ex(BVHTree *tree, const float co[3], const float dir[3], float radius, float hit_dist, BVHTree_RayCastCallback callback, void *userdata, int flag)
Definition: BLI_kdopbvh.c:2032
int BLI_bvhtree_find_nearest_first(BVHTree *tree, const float co[3], const float dist_sq, BVHTree_NearestPointCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1724
BVHTreeOverlap * BLI_bvhtree_overlap_ex(const BVHTree *tree1, const BVHTree *tree2, uint *r_overlap_tot, BVHTree_OverlapCallback callback, void *userdata, const uint max_interactions, const int flag)
Definition: BLI_kdopbvh.c:1301
int BLI_bvhtree_find_nearest_ex(BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata, int flag)
Definition: BLI_kdopbvh.c:1605
const float bvhtree_kdop_axes[13][3]
Definition: BLI_kdopbvh.c:186
int * BLI_bvhtree_intersect_plane(BVHTree *tree, float plane[4], uint *r_intersect_tot)
Definition: BLI_kdopbvh.c:1458
BVHTreeOverlap * BLI_bvhtree_overlap(const BVHTree *tree1, const BVHTree *tree2, unsigned int *r_overlap_tot, BVHTree_OverlapCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1401
void BLI_bvhtree_balance(BVHTree *tree)
Definition: BLI_kdopbvh.c:956
BVHTree * BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
Definition: BLI_kdopbvh.c:873
void BLI_bvhtree_update_tree(BVHTree *tree)
Definition: BLI_kdopbvh.c:1044
int BLI_bvhtree_ray_cast_ex(BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata, int flag)
Definition: BLI_kdopbvh.c:1939
int BLI_bvhtree_find_nearest_projected(BVHTree *tree, float projmat[4][4], float winsize[2], float mval[2], float clip_planes[6][4], int clip_plane_len, BVHTreeNearest *nearest, BVHTree_NearestProjectedCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:2284
float BLI_bvhtree_bb_raycast(const float bv[6], const float light_start[3], const float light_end[3], float pos[3])
Definition: BLI_kdopbvh.c:1996
bool(* BVHTree_WalkLeafCallback)(const BVHTreeAxisRange *bounds, int index, void *userdata)
Definition: BLI_kdopbvh.h:137
void BLI_bvhtree_free(BVHTree *tree)
Definition: BLI_kdopbvh.c:945
bool(* BVHTree_WalkOrderCallback)(const BVHTreeAxisRange *bounds, char axis, void *userdata)
Definition: BLI_kdopbvh.h:141
void(* BVHTree_RayCastCallback)(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
Definition: BLI_kdopbvh.h:114
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints)
Definition: BLI_kdopbvh.c:998
@ BVH_RAYCAST_WATERTIGHT
Definition: BLI_kdopbvh.h:102
bool BLI_bvhtree_update_node(BVHTree *tree, int index, const float co[3], const float co_moving[3], int numpoints)
Definition: BLI_kdopbvh.c:1017
void BLI_bvhtree_walk_dfs(BVHTree *tree, BVHTree_WalkParentCallback walk_parent_cb, BVHTree_WalkLeafCallback walk_leaf_cb, BVHTree_WalkOrderCallback walk_order_cb, void *userdata)
Definition: BLI_kdopbvh.c:2410
int BLI_bvhtree_get_len(const BVHTree *tree)
Definition: BLI_kdopbvh.c:1061
bool(* BVHTree_WalkParentCallback)(const BVHTreeAxisRange *bounds, void *userdata)
Definition: BLI_kdopbvh.h:135
void(* BVHTree_NearestProjectedCallback)(void *userdata, int index, const struct DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, BVHTreeNearest *nearest)
Definition: BLI_kdopbvh.h:126
int BLI_bvhtree_overlap_thread_num(const BVHTree *tree)
Definition: BLI_kdopbvh.c:1272
void(* BVHTree_NearestPointCallback)(void *userdata, int index, const float co[3], BVHTreeNearest *nearest)
Definition: BLI_kdopbvh.h:108
bool(* BVHTree_OverlapCallback)(void *userdata, int index_a, int index_b, int thread)
Definition: BLI_kdopbvh.h:120
struct BVHTreeAxisRange BVHTreeAxisRange
struct BVHTreeRayHit BVHTreeRayHit
float BLI_bvhtree_get_epsilon(const BVHTree *tree)
Definition: BLI_kdopbvh.c:1074
struct BVHTreeRay BVHTreeRay
void BLI_bvhtree_get_bounding_box(BVHTree *tree, float r_bb_min[3], float r_bb_max[3])
Definition: BLI_kdopbvh.c:1082
void BLI_bvhtree_ray_cast_all(BVHTree *tree, const float co[3], const float dir[3], float radius, float hit_dist, BVHTree_RayCastCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:2066
int BLI_bvhtree_ray_cast(BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1984
void(* BVHTree_RangeQuery)(void *userdata, int index, const float co[3], float dist_sq)
Definition: BLI_kdopbvh.h:123
int BLI_bvhtree_find_nearest(BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1654
unsigned int uint
Definition: BLI_sys_types.h:83
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition: btDbvt.cpp:299
DEGForeachIDComponentCallback callback
void * tree
uint pos
static double epsilon
float range[2]
Definition: BLI_kdopbvh.h:44
float co[3]
Definition: BLI_kdopbvh.h:59
float no[3]
Definition: BLI_kdopbvh.h:62
float co[3]
Definition: BLI_kdopbvh.h:84
float no[3]
Definition: BLI_kdopbvh.h:86
float origin[3]
Definition: BLI_kdopbvh.h:70
struct IsectRayPrecalc * isect_precalc
Definition: BLI_kdopbvh.h:76
float direction[3]
Definition: BLI_kdopbvh.h:72
float radius
Definition: BLI_kdopbvh.h:74