0828ea54280256c1e266e608386037f1acada4fc
[blender.git] / source / blender / blenkernel / BKE_pbvh.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 #ifndef __BKE_PBVH_H__
22 #define __BKE_PBVH_H__
23
24 /** \file BKE_pbvh.h
25  *  \ingroup bke
26  *  \brief A BVH for high poly meshes.
27  */
28
29 #include "BLI_bitmap.h"
30 #include "BLI_ghash.h"
31 #include "BLI_utildefines.h"
32
33 struct CCGElem;
34 struct CCGKey;
35 struct CustomData;
36 struct DMFlagMat;
37 struct DMGridAdjacency;
38 struct GHash;
39 struct MFace;
40 struct MVert;
41 struct PBVH;
42 struct PBVHNode;
43 struct BMesh;
44 struct BMLog;
45
46 typedef struct PBVH PBVH;
47 typedef struct PBVHNode PBVHNode;
48
49 typedef struct {
50         float (*co)[3];
51 } PBVHProxyNode;
52
53 /* Callbacks */
54
55 /* returns 1 if the search should continue from this node, 0 otherwise */
56 typedef bool (*BKE_pbvh_SearchCallback)(PBVHNode *node, void *data);
57
58 typedef void (*BKE_pbvh_HitCallback)(PBVHNode *node, void *data);
59 typedef void (*BKE_pbvh_HitOccludedCallback)(PBVHNode *node, void *data, float *tmin);
60
61 /* Building */
62
63 PBVH *BKE_pbvh_new(void);
64 void BKE_pbvh_build_mesh(PBVH *bvh, struct MFace *faces, struct MVert *verts,
65                          int totface, int totvert, struct CustomData *vdata);
66 void BKE_pbvh_build_grids(PBVH *bvh, struct CCGElem **grid_elems,
67                           struct DMGridAdjacency *gridadj, int totgrid,
68                           struct CCGKey *key, void **gridfaces, struct DMFlagMat *flagmats,
69                           unsigned int **grid_hidden);
70 void BKE_pbvh_build_bmesh(PBVH *bvh, struct BMesh *bm, int smooth_shading,
71                           struct BMLog *log);
72
73 void BKE_pbvh_free(PBVH *bvh);
74 void BKE_pbvh_free_layer_disp(PBVH *bvh);
75
76 /* Hierarchical Search in the BVH, two methods:
77  * - for each hit calling a callback
78  * - gather nodes in an array (easy to multithread) */
79
80 void BKE_pbvh_search_callback(PBVH *bvh,
81                               BKE_pbvh_SearchCallback scb, void *search_data,
82                               BKE_pbvh_HitCallback hcb, void *hit_data);
83
84 void BKE_pbvh_search_gather(PBVH *bvh,
85                             BKE_pbvh_SearchCallback scb, void *search_data,
86                             PBVHNode ***array, int *tot);
87
88 /* Raycast
89  * the hit callback is called for all leaf nodes intersecting the ray;
90  * it's up to the callback to find the primitive within the leaves that is
91  * hit first */
92
93 void BKE_pbvh_raycast(PBVH *bvh, BKE_pbvh_HitOccludedCallback cb, void *data,
94                       const float ray_start[3], const float ray_normal[3],
95                       int original);
96
97 int BKE_pbvh_node_raycast(PBVH *bvh, PBVHNode *node, float (*origco)[3], int use_origco,
98                           const float ray_start[3], const float ray_normal[3],
99                           float *dist);
100
101 /* for orthographic cameras, project the far away ray segment points to the root node so
102  * we can have better precision. */
103 void BKE_pbvh_raycast_project_ray_root(PBVH *bvh, bool original, float ray_start[3],
104                                        float ray_end[3], float ray_normal[3]);
105
106 /* Drawing */
107
108 void BKE_pbvh_node_draw(PBVHNode *node, void *data);
109 void BKE_pbvh_draw(PBVH *bvh, float (*planes)[4], float (*face_nors)[3],
110                    int (*setMaterial)(int, void *attribs), int wireframe);
111
112 /* PBVH Access */
113 typedef enum {
114         PBVH_FACES,
115         PBVH_GRIDS,
116         PBVH_BMESH
117 } PBVHType;
118
119 PBVHType BKE_pbvh_type(const PBVH *bvh);
120
121 /* Get the PBVH root's bounding box */
122 void BKE_pbvh_bounding_box(const PBVH *bvh, float min[3], float max[3]);
123
124 /* multires hidden data, only valid for type == PBVH_GRIDS */
125 unsigned int **BKE_pbvh_grid_hidden(const PBVH *bvh);
126
127 /* multires level, only valid for type == PBVH_GRIDS */
128 void BKE_pbvh_get_grid_key(const PBVH *pbvh, struct CCGKey *key);
129
130 /* Only valid for type == PBVH_BMESH */
131 struct BMesh *BKE_pbvh_get_bmesh(PBVH *pbvh);
132 void BKE_pbvh_bmesh_detail_size_set(PBVH *pbvh, float detail_size);
133
134 typedef enum {
135         PBVH_Subdivide = 1,
136         PBVH_Collapse = 2,
137 } PBVHTopologyUpdateMode;
138 bool BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
139                                    const float center[3], float radius);
140
141 /* Node Access */
142
143 typedef enum {
144         PBVH_Leaf = 1,
145
146         PBVH_UpdateNormals = 2,
147         PBVH_UpdateBB = 4,
148         PBVH_UpdateOriginalBB = 8,
149         PBVH_UpdateDrawBuffers = 16,
150         PBVH_UpdateRedraw = 32,
151
152         PBVH_RebuildDrawBuffers = 64,
153         PBVH_FullyHidden = 128,
154
155         PBVH_UpdateTopology = 256,
156 } PBVHNodeFlags;
157
158 void BKE_pbvh_node_mark_update(PBVHNode *node);
159 void BKE_pbvh_node_mark_rebuild_draw(PBVHNode *node);
160 void BKE_pbvh_node_mark_redraw(PBVHNode *node);
161 void BKE_pbvh_node_mark_topology_update(PBVHNode *node);
162 void BKE_pbvh_node_fully_hidden_set(PBVHNode *node, int fully_hidden);
163
164 void BKE_pbvh_node_get_grids(PBVH *bvh, PBVHNode *node,
165                              int **grid_indices, int *totgrid, int *maxgrid, int *gridsize,
166                              struct CCGElem ***grid_elems, struct DMGridAdjacency **gridadj);
167 void BKE_pbvh_node_num_verts(PBVH *bvh, PBVHNode *node,
168                              int *uniquevert, int *totvert);
169 void BKE_pbvh_node_get_verts(PBVH *bvh, PBVHNode *node,
170                              int **vert_indices, struct MVert **verts);
171
172 void BKE_pbvh_node_get_BB(PBVHNode *node, float bb_min[3], float bb_max[3]);
173 void BKE_pbvh_node_get_original_BB(PBVHNode *node, float bb_min[3], float bb_max[3]);
174
175 float BKE_pbvh_node_get_tmin(PBVHNode *node);
176
177 /* test if AABB is at least partially inside the planes' volume */
178 bool BKE_pbvh_node_planes_contain_AABB(PBVHNode *node, void *data);
179 /* test if AABB is at least partially outside the planes' volume */
180 bool BKE_pbvh_node_planes_exclude_AABB(PBVHNode *node, void *data);
181
182 struct GSet *BKE_pbvh_bmesh_node_unique_verts(PBVHNode *node);
183 struct GSet *BKE_pbvh_bmesh_node_other_verts(PBVHNode *node);
184 void BKE_pbvh_bmesh_node_save_orig(PBVHNode *node);
185 void BKE_pbvh_bmesh_after_stroke(PBVH *bvh);
186
187 /* Update Normals/Bounding Box/Draw Buffers/Redraw and clear flags */
188
189 void BKE_pbvh_update(PBVH *bvh, int flags, float (*face_nors)[3]);
190 void BKE_pbvh_redraw_BB(PBVH *bvh, float bb_min[3], float bb_max[3]);
191 void BKE_pbvh_get_grid_updates(PBVH *bvh, int clear, void ***gridfaces, int *totface);
192 void BKE_pbvh_grids_update(PBVH *bvh, struct CCGElem **grid_elems,
193                            struct DMGridAdjacency *gridadj, void **gridfaces,
194                            struct DMFlagMat *flagmats, unsigned int **grid_hidden);
195
196 /* Layer displacement */
197
198 /* Get the node's displacement layer, creating it if necessary */
199 float *BKE_pbvh_node_layer_disp_get(PBVH *pbvh, PBVHNode *node);
200
201 /* If the node has a displacement layer, free it and set to null */
202 void BKE_pbvh_node_layer_disp_free(PBVHNode *node);
203
204 /* vertex deformer */
205 float (*BKE_pbvh_get_vertCos(struct PBVH *pbvh))[3];
206 void BKE_pbvh_apply_vertCos(struct PBVH *pbvh, float (*vertCos)[3]);
207 bool BKE_pbvh_isDeformed(struct PBVH *pbvh);
208
209 /* Vertex Iterator */
210
211 /* this iterator has quite a lot of code, but it's designed to:
212  * - allow the compiler to eliminate dead code and variables
213  * - spend most of the time in the relatively simple inner loop */
214
215 /* note: PBVH_ITER_ALL does not skip hidden vertices,
216  * PBVH_ITER_UNIQUE does */
217 #define PBVH_ITER_ALL       0
218 #define PBVH_ITER_UNIQUE    1
219
220 typedef struct PBVHVertexIter {
221         /* iteration */
222         int g;
223         int width;
224         int height;
225         int gx;
226         int gy;
227         int i;
228
229         /* grid */
230         struct CCGElem **grids;
231         struct CCGElem *grid;
232         struct CCGKey *key;
233         BLI_bitmap **grid_hidden, *gh;
234         int *grid_indices;
235         int totgrid;
236         int gridsize;
237
238         /* mesh */
239         struct MVert *mverts;
240         int totvert;
241         int *vert_indices;
242         float *vmask;
243
244         /* bmesh */
245         struct GSetIterator bm_unique_verts;
246         struct GSetIterator bm_other_verts;
247         struct CustomData *bm_vdata;
248         int cd_vert_mask_offset;
249
250         /* result: these are all computed in the macro, but we assume
251          * that compiler optimization's will skip the ones we don't use */
252         struct MVert *mvert;
253         struct BMVert *bm_vert;
254         float *co;
255         short *no;
256         float *fno;
257         float *mask;
258 } PBVHVertexIter;
259
260 void pbvh_vertex_iter_init(PBVH *bvh, PBVHNode *node,
261                            PBVHVertexIter *vi, int mode);
262
263 #define BKE_pbvh_vertex_iter_begin(bvh, node, vi, mode) \
264         pbvh_vertex_iter_init(bvh, node, &vi, mode); \
265          \
266         for (vi.i = 0, vi.g = 0; vi.g < vi.totgrid; vi.g++) { \
267                 if (vi.grids) { \
268                         vi.width = vi.gridsize; \
269                         vi.height = vi.gridsize; \
270                         vi.grid = vi.grids[vi.grid_indices[vi.g]]; \
271                         if (mode == PBVH_ITER_UNIQUE) \
272                                 vi.gh = vi.grid_hidden[vi.grid_indices[vi.g]];   \
273                 } \
274                 else { \
275                         vi.width = vi.totvert; \
276                         vi.height = 1; \
277                 } \
278                  \
279                 for (vi.gy = 0; vi.gy < vi.height; vi.gy++) { \
280                         for (vi.gx = 0; vi.gx < vi.width; vi.gx++, vi.i++) { \
281                                 if (vi.grid) { \
282                                         vi.co = CCG_elem_co(vi.key, vi.grid); \
283                                         vi.fno = CCG_elem_no(vi.key, vi.grid); \
284                                         vi.mask = vi.key->has_mask ? CCG_elem_mask(vi.key, vi.grid) : NULL; \
285                                         vi.grid = CCG_elem_next(vi.key, vi.grid); \
286                                         if (vi.gh) { \
287                                                 if (BLI_BITMAP_GET(vi.gh, vi.gy * vi.gridsize + vi.gx)) \
288                                                         continue; \
289                                         } \
290                                 } \
291                                 else if (vi.mverts) { \
292                                         vi.mvert = &vi.mverts[vi.vert_indices[vi.gx]]; \
293                                         if (mode == PBVH_ITER_UNIQUE && vi.mvert->flag & ME_HIDE) \
294                                                 continue; \
295                                         vi.co = vi.mvert->co; \
296                                         vi.no = vi.mvert->no; \
297                                         if (vi.vmask) \
298                                                 vi.mask = &vi.vmask[vi.vert_indices[vi.gx]]; \
299                                 } \
300                                 else { \
301                                         if (!BLI_gsetIterator_done(&vi.bm_unique_verts)) {\
302                                                 vi.bm_vert = BLI_gsetIterator_getKey(&vi.bm_unique_verts); \
303                                                 BLI_gsetIterator_step(&vi.bm_unique_verts); \
304                                         } \
305                                         else { \
306                                                 vi.bm_vert = BLI_gsetIterator_getKey(&vi.bm_other_verts); \
307                                                 BLI_gsetIterator_step(&vi.bm_other_verts); \
308                                         } \
309                                         if (mode == PBVH_ITER_UNIQUE && \
310                                                 BM_elem_flag_test(vi.bm_vert, BM_ELEM_HIDDEN)) \
311                                                 continue; \
312                                         vi.co = vi.bm_vert->co; \
313                                         vi.fno = vi.bm_vert->no; \
314                                         vi.mask = BM_ELEM_CD_GET_VOID_P(vi.bm_vert, vi.cd_vert_mask_offset); \
315                                 }
316
317 #define BKE_pbvh_vertex_iter_end \
318                         } \
319                 } \
320         }
321
322 void BKE_pbvh_node_get_proxies(PBVHNode *node, PBVHProxyNode **proxies, int *proxy_count);
323 void BKE_pbvh_node_free_proxies(PBVHNode *node);
324 PBVHProxyNode *BKE_pbvh_node_add_proxy(PBVH *bvh, PBVHNode *node);
325 void BKE_pbvh_gather_proxies(PBVH *pbvh, PBVHNode ***nodes,  int *totnode);
326
327 //void BKE_pbvh_node_BB_reset(PBVHNode *node);
328 //void BKE_pbvh_node_BB_expand(PBVHNode *node, float co[3]);
329
330 void pbvh_show_diffuse_color_set(PBVH *bvh, bool show_diffuse_color);
331
332 #endif /* __BKE_PBVH_H__ */