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