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