Merge branch 'blender-v2.93-release'
[blender.git] / source / blender / blenlib / BLI_kdopbvh.h
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
22 /** \file
23  * \ingroup bli
24  */
25
26 #include "BLI_sys_types.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 struct BVHTree;
33 struct DistProjectedAABBPrecalc;
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   };
46 } BVHTreeAxisRange;
47
48 typedef struct BVHTreeOverlap {
49   int indexA;
50   int indexB;
51 } BVHTreeOverlap;
52
53 typedef struct BVHTreeNearest {
54   /** The index of the nearest found
55    * (untouched if none is found within a dist radius from the given coordinates) */
56   int index;
57   /** Nearest coordinates
58    * (untouched it none is found within a dist radius from the given coordinates). */
59   float co[3];
60   /** Normal at nearest coordinates
61    * (untouched it none is found within a dist radius from the given coordinates). */
62   float no[3];
63   /** squared distance to search around */
64   float dist_sq;
65   int flags;
66 } BVHTreeNearest;
67
68 typedef struct BVHTreeRay {
69   /** ray origin */
70   float origin[3];
71   /** ray direction */
72   float direction[3];
73   /** radius around ray */
74   float radius;
75 #ifdef USE_KDOPBVH_WATERTIGHT
76   struct IsectRayPrecalc *isect_precalc;
77 #endif
78 } BVHTreeRay;
79
80 typedef struct BVHTreeRayHit {
81   /** Index of the tree node (untouched if no hit is found). */
82   int index;
83   /** Coordinates of the hit point. */
84   float co[3];
85   /** Normal on hit point. */
86   float no[3];
87   /** Distance to the hit point. */
88   float dist;
89 } BVHTreeRayHit;
90
91 enum {
92   /* Use a priority queue to process nodes in the optimal order (for slow callbacks) */
93   BVH_OVERLAP_USE_THREADING = (1 << 0),
94   BVH_OVERLAP_RETURN_PAIRS = (1 << 1),
95 };
96 enum {
97   /* Use a priority queue to process nodes in the optimal order (for slow callbacks) */
98   BVH_NEAREST_OPTIMAL_ORDER = (1 << 0),
99 };
100 enum {
101   /* calculate IsectRayPrecalc data */
102   BVH_RAYCAST_WATERTIGHT = (1 << 0),
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. */
137 typedef bool (*BVHTree_WalkLeafCallback)(const BVHTreeAxisRange *bounds,
138                                          int index,
139                                          void *userdata);
140 /* return true to search (min, max) else (max, min). */
141 typedef bool (*BVHTree_WalkOrderCallback)(const BVHTreeAxisRange *bounds,
142                                           char axis,
143                                           void *userdata);
144
145 BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis);
146 void BLI_bvhtree_free(BVHTree *tree);
147
148 /* construct: first insert points, then call balance */
149 void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints);
150 void BLI_bvhtree_balance(BVHTree *tree);
151
152 /* update: first update points/nodes, then call update_tree to refit the bounding volumes */
153 bool BLI_bvhtree_update_node(
154     BVHTree *tree, int index, const float co[3], const float co_moving[3], int numpoints);
155 void BLI_bvhtree_update_tree(BVHTree *tree);
156
157 int BLI_bvhtree_overlap_thread_num(const BVHTree *tree);
158
159 /* collision/overlap: check two trees if they overlap,
160  * alloc's *overlap with length of the int return value */
161 BVHTreeOverlap *BLI_bvhtree_overlap_ex(
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!) */
166     BVHTree_OverlapCallback callback,
167     void *userdata,
168     const uint max_interactions,
169     const int flag);
170 BVHTreeOverlap *BLI_bvhtree_overlap(const BVHTree *tree1,
171                                     const BVHTree *tree2,
172                                     unsigned int *r_overlap_tot,
173                                     BVHTree_OverlapCallback callback,
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);
179 int BLI_bvhtree_get_tree_type(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) */
186 int BLI_bvhtree_find_nearest_ex(BVHTree *tree,
187                                 const float co[3],
188                                 BVHTreeNearest *nearest,
189                                 BVHTree_NearestPointCallback callback,
190                                 void *userdata,
191                                 int flag);
192 int BLI_bvhtree_find_nearest(BVHTree *tree,
193                              const float co[3],
194                              BVHTreeNearest *nearest,
195                              BVHTree_NearestPointCallback callback,
196                              void *userdata);
197
198 int BLI_bvhtree_find_nearest_first(BVHTree *tree,
199                                    const float co[3],
200                                    const float dist_sq,
201                                    BVHTree_NearestPointCallback callback,
202                                    void *userdata);
203
204 int BLI_bvhtree_ray_cast_ex(BVHTree *tree,
205                             const float co[3],
206                             const float dir[3],
207                             float radius,
208                             BVHTreeRayHit *hit,
209                             BVHTree_RayCastCallback callback,
210                             void *userdata,
211                             int flag);
212 int BLI_bvhtree_ray_cast(BVHTree *tree,
213                          const float co[3],
214                          const float dir[3],
215                          float radius,
216                          BVHTreeRayHit *hit,
217                          BVHTree_RayCastCallback callback,
218                          void *userdata);
219
220 void BLI_bvhtree_ray_cast_all_ex(BVHTree *tree,
221                                  const float co[3],
222                                  const float dir[3],
223                                  float radius,
224                                  float hit_dist,
225                                  BVHTree_RayCastCallback callback,
226                                  void *userdata,
227                                  int flag);
228 void BLI_bvhtree_ray_cast_all(BVHTree *tree,
229                               const float co[3],
230                               const float dir[3],
231                               float radius,
232                               float hit_dist,
233                               BVHTree_RayCastCallback callback,
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 */
242 int BLI_bvhtree_range_query(
243     BVHTree *tree, const float co[3], float radius, BVHTree_RangeQuery callback, void *userdata);
244
245 int BLI_bvhtree_find_nearest_projected(BVHTree *tree,
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,
252                                        BVHTree_NearestProjectedCallback callback,
253                                        void *userdata);
254
255 void BLI_bvhtree_walk_dfs(BVHTree *tree,
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