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