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