Nodes: Support for socket shapes other than circle
[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 #ifndef __BLI_KDOPBVH_H__
21 #define __BLI_KDOPBVH_H__
22
23 /** \file
24  * \ingroup bli
25  */
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 struct BVHTree;
32 struct DistProjectedAABBPrecalc;
33
34 typedef struct BVHTree BVHTree;
35 #define USE_KDOPBVH_WATERTIGHT
36
37 typedef struct BVHTreeAxisRange {
38   union {
39     struct {
40       float min, max;
41     };
42     /* alternate access */
43     float range[2];
44   };
45 } BVHTreeAxisRange;
46
47 typedef struct BVHTreeOverlap {
48   int indexA;
49   int indexB;
50 } BVHTreeOverlap;
51
52 typedef struct BVHTreeNearest {
53   /** The index of the nearest found
54    * (untouched if none is found within a dist radius from the given coordinates) */
55   int index;
56   /** Nearest coordinates
57    * (untouched it none is found within a dist radius from the given coordinates). */
58   float co[3];
59   /** Normal at nearest coordinates
60    * (untouched it none is found within a dist radius from the given coordinates). */
61   float no[3];
62   /** squared distance to search around */
63   float dist_sq;
64   int flags;
65 } BVHTreeNearest;
66
67 typedef struct BVHTreeRay {
68   /** ray origin */
69   float origin[3];
70   /** ray direction */
71   float direction[3];
72   /** radius around ray */
73   float radius;
74 #ifdef USE_KDOPBVH_WATERTIGHT
75   struct IsectRayPrecalc *isect_precalc;
76 #endif
77 } BVHTreeRay;
78
79 typedef struct BVHTreeRayHit {
80   /** Index of the tree node (untouched if no hit is found). */
81   int index;
82   /** Coordinates of the hit point. */
83   float co[3];
84   /** Normal on hit point. */
85   float no[3];
86   /** Distance to the hit point. */
87   float dist;
88 } BVHTreeRayHit;
89
90 enum {
91   /* Use a priority queue to process nodes in the optimal order (for slow callbacks) */
92   BVH_NEAREST_OPTIMAL_ORDER = (1 << 0),
93 };
94 enum {
95   /* calculate IsectRayPrecalc data */
96   BVH_RAYCAST_WATERTIGHT = (1 << 0),
97 };
98 #define BVH_RAYCAST_DEFAULT (BVH_RAYCAST_WATERTIGHT)
99 #define BVH_RAYCAST_DIST_MAX (FLT_MAX / 2.0f)
100
101 /* callback must update nearest in case it finds a nearest result */
102 typedef void (*BVHTree_NearestPointCallback)(void *userdata,
103                                              int index,
104                                              const float co[3],
105                                              BVHTreeNearest *nearest);
106
107 /* callback must update hit in case it finds a nearest successful hit */
108 typedef void (*BVHTree_RayCastCallback)(void *userdata,
109                                         int index,
110                                         const BVHTreeRay *ray,
111                                         BVHTreeRayHit *hit);
112
113 /* callback to check if 2 nodes overlap (use thread if intersection results need to be stored) */
114 typedef bool (*BVHTree_OverlapCallback)(void *userdata, int index_a, int index_b, int thread);
115
116 /* callback to range search query */
117 typedef void (*BVHTree_RangeQuery)(void *userdata, int index, const float co[3], float dist_sq);
118
119 /* callback to find nearest projected */
120 typedef void (*BVHTree_NearestProjectedCallback)(void *userdata,
121                                                  int index,
122                                                  const struct DistProjectedAABBPrecalc *precalc,
123                                                  const float (*clip_plane)[4],
124                                                  const int clip_plane_len,
125                                                  BVHTreeNearest *nearest);
126
127 /* callbacks to BLI_bvhtree_walk_dfs */
128 /* return true to traverse into this nodes children, else skip. */
129 typedef bool (*BVHTree_WalkParentCallback)(const BVHTreeAxisRange *bounds, void *userdata);
130 /* return true to keep walking, else early-exit the search. */
131 typedef bool (*BVHTree_WalkLeafCallback)(const BVHTreeAxisRange *bounds,
132                                          int index,
133                                          void *userdata);
134 /* return true to search (min, max) else (max, min). */
135 typedef bool (*BVHTree_WalkOrderCallback)(const BVHTreeAxisRange *bounds,
136                                           char axis,
137                                           void *userdata);
138
139 BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis);
140 void BLI_bvhtree_free(BVHTree *tree);
141
142 /* construct: first insert points, then call balance */
143 void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints);
144 void BLI_bvhtree_balance(BVHTree *tree);
145
146 /* update: first update points/nodes, then call update_tree to refit the bounding volumes */
147 bool BLI_bvhtree_update_node(
148     BVHTree *tree, int index, const float co[3], const float co_moving[3], int numpoints);
149 void BLI_bvhtree_update_tree(BVHTree *tree);
150
151 int BLI_bvhtree_overlap_thread_num(const BVHTree *tree);
152
153 /* collision/overlap: check two trees if they overlap,
154  * alloc's *overlap with length of the int return value */
155 BVHTreeOverlap *BLI_bvhtree_overlap(const BVHTree *tree1,
156                                     const BVHTree *tree2,
157                                     unsigned int *r_overlap_tot,
158                                     BVHTree_OverlapCallback callback,
159                                     void *userdata);
160
161 int BLI_bvhtree_get_len(const BVHTree *tree);
162 int BLI_bvhtree_get_tree_type(const BVHTree *tree);
163 float BLI_bvhtree_get_epsilon(const BVHTree *tree);
164
165 /* find nearest node to the given coordinates
166  * (if nearest is given it will only search nodes where
167  * square distance is smaller than nearest->dist) */
168 int BLI_bvhtree_find_nearest_ex(BVHTree *tree,
169                                 const float co[3],
170                                 BVHTreeNearest *nearest,
171                                 BVHTree_NearestPointCallback callback,
172                                 void *userdata,
173                                 int flag);
174 int BLI_bvhtree_find_nearest(BVHTree *tree,
175                              const float co[3],
176                              BVHTreeNearest *nearest,
177                              BVHTree_NearestPointCallback callback,
178                              void *userdata);
179
180 int BLI_bvhtree_ray_cast_ex(BVHTree *tree,
181                             const float co[3],
182                             const float dir[3],
183                             float radius,
184                             BVHTreeRayHit *hit,
185                             BVHTree_RayCastCallback callback,
186                             void *userdata,
187                             int flag);
188 int BLI_bvhtree_ray_cast(BVHTree *tree,
189                          const float co[3],
190                          const float dir[3],
191                          float radius,
192                          BVHTreeRayHit *hit,
193                          BVHTree_RayCastCallback callback,
194                          void *userdata);
195
196 void BLI_bvhtree_ray_cast_all_ex(BVHTree *tree,
197                                  const float co[3],
198                                  const float dir[3],
199                                  float radius,
200                                  float hit_dist,
201                                  BVHTree_RayCastCallback callback,
202                                  void *userdata,
203                                  int flag);
204 void BLI_bvhtree_ray_cast_all(BVHTree *tree,
205                               const float co[3],
206                               const float dir[3],
207                               float radius,
208                               float hit_dist,
209                               BVHTree_RayCastCallback callback,
210                               void *userdata);
211
212 float BLI_bvhtree_bb_raycast(const float bv[6],
213                              const float light_start[3],
214                              const float light_end[3],
215                              float pos[3]);
216
217 /* range query */
218 int BLI_bvhtree_range_query(
219     BVHTree *tree, const float co[3], float radius, BVHTree_RangeQuery callback, void *userdata);
220
221 int BLI_bvhtree_find_nearest_projected(BVHTree *tree,
222                                        float projmat[4][4],
223                                        float winsize[2],
224                                        float mval[2],
225                                        float clip_planes[6][4],
226                                        int clip_num,
227                                        BVHTreeNearest *nearest,
228                                        BVHTree_NearestProjectedCallback callback,
229                                        void *userdata);
230
231 void BLI_bvhtree_walk_dfs(BVHTree *tree,
232                           BVHTree_WalkParentCallback walk_parent_cb,
233                           BVHTree_WalkLeafCallback walk_leaf_cb,
234                           BVHTree_WalkOrderCallback walk_order_cb,
235                           void *userdata);
236
237 /* expose for bvh callbacks to use */
238 extern const float bvhtree_kdop_axes[13][3];
239
240 #ifdef __cplusplus
241 }
242 #endif
243
244 #endif /* __BLI_KDOPBVH_H__ */