BKE_pbvh.h 11 KB

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