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