Blender  V2.93
pbvh.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLI_utildefines.h"
24 
25 #include "BLI_bitmap.h"
26 #include "BLI_ghash.h"
27 #include "BLI_math.h"
28 #include "BLI_rand.h"
29 #include "BLI_task.h"
30 
31 #include "DNA_mesh_types.h"
32 #include "DNA_meshdata_types.h"
33 
34 #include "BKE_ccg.h"
35 #include "BKE_mesh.h" /* for BKE_mesh_calc_normals */
36 #include "BKE_paint.h"
37 #include "BKE_pbvh.h"
38 #include "BKE_subdiv_ccg.h"
39 
40 #include "PIL_time.h"
41 
42 #include "GPU_buffers.h"
43 
44 #include "bmesh.h"
45 
46 #include "atomic_ops.h"
47 
48 #include "pbvh_intern.h"
49 
50 #include <limits.h>
51 
52 #define LEAF_LIMIT 10000
53 
54 //#define PERFCNTRS
55 
56 #define STACK_FIXED_DEPTH 100
57 
58 typedef struct PBVHStack {
60  bool revisiting;
62 
63 typedef struct PBVHIter {
66  void *search_data;
67 
69  int stacksize;
70 
74 
75 void BB_reset(BB *bb)
76 {
77  bb->bmin[0] = bb->bmin[1] = bb->bmin[2] = FLT_MAX;
78  bb->bmax[0] = bb->bmax[1] = bb->bmax[2] = -FLT_MAX;
79 }
80 
81 /* Expand the bounding box to include a new coordinate */
82 void BB_expand(BB *bb, const float co[3])
83 {
84  for (int i = 0; i < 3; i++) {
85  bb->bmin[i] = min_ff(bb->bmin[i], co[i]);
86  bb->bmax[i] = max_ff(bb->bmax[i], co[i]);
87  }
88 }
89 
90 /* Expand the bounding box to include another bounding box */
91 void BB_expand_with_bb(BB *bb, BB *bb2)
92 {
93  for (int i = 0; i < 3; i++) {
94  bb->bmin[i] = min_ff(bb->bmin[i], bb2->bmin[i]);
95  bb->bmax[i] = max_ff(bb->bmax[i], bb2->bmax[i]);
96  }
97 }
98 
99 /* Return 0, 1, or 2 to indicate the widest axis of the bounding box */
100 int BB_widest_axis(const BB *bb)
101 {
102  float dim[3];
103 
104  for (int i = 0; i < 3; i++) {
105  dim[i] = bb->bmax[i] - bb->bmin[i];
106  }
107 
108  if (dim[0] > dim[1]) {
109  if (dim[0] > dim[2]) {
110  return 0;
111  }
112 
113  return 2;
114  }
115 
116  if (dim[1] > dim[2]) {
117  return 1;
118  }
119 
120  return 2;
121 }
122 
124 {
125  for (int i = 0; i < 3; i++) {
126  bbc->bcentroid[i] = (bbc->bmin[i] + bbc->bmax[i]) * 0.5f;
127  }
128 }
129 
130 /* Not recursive */
131 static void update_node_vb(PBVH *pbvh, PBVHNode *node)
132 {
133  BB vb;
134 
135  BB_reset(&vb);
136 
137  if (node->flag & PBVH_Leaf) {
138  PBVHVertexIter vd;
139 
141  BB_expand(&vb, vd.co);
142  }
144  }
145  else {
146  BB_expand_with_bb(&vb, &pbvh->nodes[node->children_offset].vb);
147  BB_expand_with_bb(&vb, &pbvh->nodes[node->children_offset + 1].vb);
148  }
149 
150  node->vb = vb;
151 }
152 
153 // void BKE_pbvh_node_BB_reset(PBVHNode *node)
154 //{
155 // BB_reset(&node->vb);
156 //}
157 //
158 // void BKE_pbvh_node_BB_expand(PBVHNode *node, float co[3])
159 //{
160 // BB_expand(&node->vb, co);
161 //}
162 
163 static bool face_materials_match(const MPoly *f1, const MPoly *f2)
164 {
165  return ((f1->flag & ME_SMOOTH) == (f2->flag & ME_SMOOTH) && (f1->mat_nr == f2->mat_nr));
166 }
167 
168 static bool grid_materials_match(const DMFlagMat *f1, const DMFlagMat *f2)
169 {
170  return ((f1->flag & ME_SMOOTH) == (f2->flag & ME_SMOOTH) && (f1->mat_nr == f2->mat_nr));
171 }
172 
173 /* Adapted from BLI_kdopbvh.c */
174 /* Returns the index of the first element on the right of the partition */
175 static int partition_indices(int *prim_indices, int lo, int hi, int axis, float mid, BBC *prim_bbc)
176 {
177  int i = lo, j = hi;
178  for (;;) {
179  for (; prim_bbc[prim_indices[i]].bcentroid[axis] < mid; i++) {
180  /* pass */
181  }
182  for (; mid < prim_bbc[prim_indices[j]].bcentroid[axis]; j--) {
183  /* pass */
184  }
185 
186  if (!(i < j)) {
187  return i;
188  }
189 
190  SWAP(int, prim_indices[i], prim_indices[j]);
191  i++;
192  }
193 }
194 
195 /* Returns the index of the first element on the right of the partition */
196 static int partition_indices_material(PBVH *pbvh, int lo, int hi)
197 {
198  const MPoly *mpoly = pbvh->mpoly;
199  const MLoopTri *looptri = pbvh->looptri;
200  const DMFlagMat *flagmats = pbvh->grid_flag_mats;
201  const int *indices = pbvh->prim_indices;
202  const void *first;
203  int i = lo, j = hi;
204 
205  if (pbvh->looptri) {
206  first = &mpoly[looptri[pbvh->prim_indices[lo]].poly];
207  }
208  else {
209  first = &flagmats[pbvh->prim_indices[lo]];
210  }
211 
212  for (;;) {
213  if (pbvh->looptri) {
214  for (; face_materials_match(first, &mpoly[looptri[indices[i]].poly]); i++) {
215  /* pass */
216  }
217  for (; !face_materials_match(first, &mpoly[looptri[indices[j]].poly]); j--) {
218  /* pass */
219  }
220  }
221  else {
222  for (; grid_materials_match(first, &flagmats[indices[i]]); i++) {
223  /* pass */
224  }
225  for (; !grid_materials_match(first, &flagmats[indices[j]]); j--) {
226  /* pass */
227  }
228  }
229 
230  if (!(i < j)) {
231  return i;
232  }
233 
234  SWAP(int, pbvh->prim_indices[i], pbvh->prim_indices[j]);
235  i++;
236  }
237 }
238 
239 void pbvh_grow_nodes(PBVH *pbvh, int totnode)
240 {
241  if (UNLIKELY(totnode > pbvh->node_mem_count)) {
242  pbvh->node_mem_count = pbvh->node_mem_count + (pbvh->node_mem_count / 3);
243  if (pbvh->node_mem_count < totnode) {
244  pbvh->node_mem_count = totnode;
245  }
246  pbvh->nodes = MEM_recallocN(pbvh->nodes, sizeof(PBVHNode) * pbvh->node_mem_count);
247  }
248 
249  pbvh->totnode = totnode;
250 }
251 
252 /* Add a vertex to the map, with a positive value for unique vertices and
253  * a negative value for additional vertices */
254 static int map_insert_vert(
255  PBVH *pbvh, GHash *map, unsigned int *face_verts, unsigned int *uniq_verts, int vertex)
256 {
257  void *key, **value_p;
258 
259  key = POINTER_FROM_INT(vertex);
260  if (!BLI_ghash_ensure_p(map, key, &value_p)) {
261  int value_i;
262  if (BLI_BITMAP_TEST(pbvh->vert_bitmap, vertex) == 0) {
263  BLI_BITMAP_ENABLE(pbvh->vert_bitmap, vertex);
264  value_i = *uniq_verts;
265  (*uniq_verts)++;
266  }
267  else {
268  value_i = ~(*face_verts);
269  (*face_verts)++;
270  }
271  *value_p = POINTER_FROM_INT(value_i);
272  return value_i;
273  }
274 
275  return POINTER_AS_INT(*value_p);
276 }
277 
278 /* Find vertices used by the faces in this node and update the draw buffers */
280 {
281  bool has_visible = false;
282 
283  node->uniq_verts = node->face_verts = 0;
284  const int totface = node->totprim;
285 
286  /* reserve size is rough guess */
287  GHash *map = BLI_ghash_int_new_ex("build_mesh_leaf_node gh", 2 * totface);
288 
289  int(*face_vert_indices)[3] = MEM_mallocN(sizeof(int[3]) * totface, "bvh node face vert indices");
290 
291  node->face_vert_indices = (const int(*)[3])face_vert_indices;
292 
293  if (pbvh->respect_hide == false) {
294  has_visible = true;
295  }
296 
297  for (int i = 0; i < totface; i++) {
298  const MLoopTri *lt = &pbvh->looptri[node->prim_indices[i]];
299  for (int j = 0; j < 3; j++) {
300  face_vert_indices[i][j] = map_insert_vert(
301  pbvh, map, &node->face_verts, &node->uniq_verts, pbvh->mloop[lt->tri[j]].v);
302  }
303 
304  if (has_visible == false) {
305  if (!paint_is_face_hidden(lt, pbvh->verts, pbvh->mloop)) {
306  has_visible = true;
307  }
308  }
309  }
310 
311  int *vert_indices = MEM_callocN(sizeof(int) * (node->uniq_verts + node->face_verts),
312  "bvh node vert indices");
313  node->vert_indices = vert_indices;
314 
315  /* Build the vertex list, unique verts first */
316  GHashIterator gh_iter;
317  GHASH_ITER (gh_iter, map) {
318  void *value = BLI_ghashIterator_getValue(&gh_iter);
319  int ndx = POINTER_AS_INT(value);
320 
321  if (ndx < 0) {
322  ndx = -ndx + node->uniq_verts - 1;
323  }
324 
325  vert_indices[ndx] = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
326  }
327 
328  for (int i = 0; i < totface; i++) {
329  const int sides = 3;
330 
331  for (int j = 0; j < sides; j++) {
332  if (face_vert_indices[i][j] < 0) {
333  face_vert_indices[i][j] = -face_vert_indices[i][j] + node->uniq_verts - 1;
334  }
335  }
336  }
337 
339 
340  BKE_pbvh_node_fully_hidden_set(node, !has_visible);
341 
342  BLI_ghash_free(map, NULL, NULL);
343 }
344 
345 static void update_vb(PBVH *pbvh, PBVHNode *node, BBC *prim_bbc, int offset, int count)
346 {
347  BB_reset(&node->vb);
348  for (int i = offset + count - 1; i >= offset; i--) {
349  BB_expand_with_bb(&node->vb, (BB *)(&prim_bbc[pbvh->prim_indices[i]]));
350  }
351  node->orig_vb = node->vb;
352 }
353 
354 /* Returns the number of visible quads in the nodes' grids. */
356  const int *grid_indices,
357  int totgrid,
358  int gridsize)
359 {
360  const int gridarea = (gridsize - 1) * (gridsize - 1);
361  int totquad = 0;
362 
363  /* grid hidden layer is present, so have to check each grid for
364  * visibility */
365 
366  for (int i = 0; i < totgrid; i++) {
367  const BLI_bitmap *gh = grid_hidden[grid_indices[i]];
368 
369  if (gh) {
370  /* grid hidden are present, have to check each element */
371  for (int y = 0; y < gridsize - 1; y++) {
372  for (int x = 0; x < gridsize - 1; x++) {
373  if (!paint_is_grid_face_hidden(gh, gridsize, x, y)) {
374  totquad++;
375  }
376  }
377  }
378  }
379  else {
380  totquad += gridarea;
381  }
382  }
383 
384  return totquad;
385 }
386 
388 {
389  const int gridsize = pbvh->gridkey.grid_size;
390  for (int i = 0; i < pbvh->totgrid; i++) {
391  BLI_bitmap *gh = pbvh->grid_hidden[i];
392  const int face_index = BKE_subdiv_ccg_grid_to_face_index(pbvh->subdiv_ccg, i);
393  if (!gh && pbvh->face_sets[face_index] < 0) {
394  gh = pbvh->grid_hidden[i] = BLI_BITMAP_NEW(pbvh->gridkey.grid_area,
395  "partialvis_update_grids");
396  }
397  if (gh) {
398  for (int y = 0; y < gridsize; y++) {
399  for (int x = 0; x < gridsize; x++) {
400  BLI_BITMAP_SET(gh, y * gridsize + x, pbvh->face_sets[face_index] < 0);
401  }
402  }
403  }
404  }
405 }
406 
408 {
409  int totquads = BKE_pbvh_count_grid_quads(
410  pbvh->grid_hidden, node->prim_indices, node->totprim, pbvh->gridkey.grid_size);
411  BKE_pbvh_node_fully_hidden_set(node, (totquads == 0));
413 }
414 
415 static void build_leaf(PBVH *pbvh, int node_index, BBC *prim_bbc, int offset, int count)
416 {
417  pbvh->nodes[node_index].flag |= PBVH_Leaf;
418 
419  pbvh->nodes[node_index].prim_indices = pbvh->prim_indices + offset;
420  pbvh->nodes[node_index].totprim = count;
421 
422  /* Still need vb for searches */
423  update_vb(pbvh, &pbvh->nodes[node_index], prim_bbc, offset, count);
424 
425  if (pbvh->looptri) {
426  build_mesh_leaf_node(pbvh, pbvh->nodes + node_index);
427  }
428  else {
429  build_grid_leaf_node(pbvh, pbvh->nodes + node_index);
430  }
431 }
432 
433 /* Return zero if all primitives in the node can be drawn with the
434  * same material (including flat/smooth shading), non-zero otherwise */
435 static bool leaf_needs_material_split(PBVH *pbvh, int offset, int count)
436 {
437  if (count <= 1) {
438  return false;
439  }
440 
441  if (pbvh->looptri) {
442  const MLoopTri *first = &pbvh->looptri[pbvh->prim_indices[offset]];
443  const MPoly *mp = &pbvh->mpoly[first->poly];
444 
445  for (int i = offset + count - 1; i > offset; i--) {
446  int prim = pbvh->prim_indices[i];
447  const MPoly *mp_other = &pbvh->mpoly[pbvh->looptri[prim].poly];
448  if (!face_materials_match(mp, mp_other)) {
449  return true;
450  }
451  }
452  }
453  else {
454  const DMFlagMat *first = &pbvh->grid_flag_mats[pbvh->prim_indices[offset]];
455 
456  for (int i = offset + count - 1; i > offset; i--) {
457  int prim = pbvh->prim_indices[i];
458  if (!grid_materials_match(first, &pbvh->grid_flag_mats[prim])) {
459  return true;
460  }
461  }
462  }
463 
464  return false;
465 }
466 
467 /* Recursively build a node in the tree
468  *
469  * vb is the voxel box around all of the primitives contained in
470  * this node.
471  *
472  * cb is the bounding box around all the centroids of the primitives
473  * contained in this node
474  *
475  * offset and start indicate a range in the array of primitive indices
476  */
477 
478 static void build_sub(PBVH *pbvh, int node_index, BB *cb, BBC *prim_bbc, int offset, int count)
479 {
480  int end;
481  BB cb_backing;
482 
483  /* Decide whether this is a leaf or not */
484  const bool below_leaf_limit = count <= pbvh->leaf_limit;
485  if (below_leaf_limit) {
486  if (!leaf_needs_material_split(pbvh, offset, count)) {
487  build_leaf(pbvh, node_index, prim_bbc, offset, count);
488  return;
489  }
490  }
491 
492  /* Add two child nodes */
493  pbvh->nodes[node_index].children_offset = pbvh->totnode;
494  pbvh_grow_nodes(pbvh, pbvh->totnode + 2);
495 
496  /* Update parent node bounding box */
497  update_vb(pbvh, &pbvh->nodes[node_index], prim_bbc, offset, count);
498 
499  if (!below_leaf_limit) {
500  /* Find axis with widest range of primitive centroids */
501  if (!cb) {
502  cb = &cb_backing;
503  BB_reset(cb);
504  for (int i = offset + count - 1; i >= offset; i--) {
505  BB_expand(cb, prim_bbc[pbvh->prim_indices[i]].bcentroid);
506  }
507  }
508  const int axis = BB_widest_axis(cb);
509 
510  /* Partition primitives along that axis */
511  end = partition_indices(pbvh->prim_indices,
512  offset,
513  offset + count - 1,
514  axis,
515  (cb->bmax[axis] + cb->bmin[axis]) * 0.5f,
516  prim_bbc);
517  }
518  else {
519  /* Partition primitives by material */
520  end = partition_indices_material(pbvh, offset, offset + count - 1);
521  }
522 
523  /* Build children */
524  build_sub(pbvh, pbvh->nodes[node_index].children_offset, NULL, prim_bbc, offset, end - offset);
525  build_sub(pbvh,
526  pbvh->nodes[node_index].children_offset + 1,
527  NULL,
528  prim_bbc,
529  end,
530  offset + count - end);
531 }
532 
533 static void pbvh_build(PBVH *pbvh, BB *cb, BBC *prim_bbc, int totprim)
534 {
535  if (totprim != pbvh->totprim) {
536  pbvh->totprim = totprim;
537  if (pbvh->nodes) {
538  MEM_freeN(pbvh->nodes);
539  }
540  if (pbvh->prim_indices) {
541  MEM_freeN(pbvh->prim_indices);
542  }
543  pbvh->prim_indices = MEM_mallocN(sizeof(int) * totprim, "bvh prim indices");
544  for (int i = 0; i < totprim; i++) {
545  pbvh->prim_indices[i] = i;
546  }
547  pbvh->totnode = 0;
548  if (pbvh->node_mem_count < 100) {
549  pbvh->node_mem_count = 100;
550  pbvh->nodes = MEM_callocN(sizeof(PBVHNode) * pbvh->node_mem_count, "bvh initial nodes");
551  }
552  }
553 
554  pbvh->totnode = 1;
555  build_sub(pbvh, 0, cb, prim_bbc, 0, totprim);
556 }
557 
565  const Mesh *mesh,
566  const MPoly *mpoly,
567  const MLoop *mloop,
568  MVert *verts,
569  int totvert,
570  struct CustomData *vdata,
571  struct CustomData *ldata,
572  struct CustomData *pdata,
573  const MLoopTri *looptri,
574  int looptri_num)
575 {
576  BBC *prim_bbc = NULL;
577  BB cb;
578 
579  pbvh->mesh = mesh;
580  pbvh->type = PBVH_FACES;
581  pbvh->mpoly = mpoly;
582  pbvh->mloop = mloop;
583  pbvh->looptri = looptri;
584  pbvh->verts = verts;
585  pbvh->vert_bitmap = BLI_BITMAP_NEW(totvert, "bvh->vert_bitmap");
586  pbvh->totvert = totvert;
587  pbvh->leaf_limit = LEAF_LIMIT;
588  pbvh->vdata = vdata;
589  pbvh->ldata = ldata;
590  pbvh->pdata = pdata;
591 
594 
595  BB_reset(&cb);
596 
597  /* For each face, store the AABB and the AABB centroid */
598  prim_bbc = MEM_mallocN(sizeof(BBC) * looptri_num, "prim_bbc");
599 
600  for (int i = 0; i < looptri_num; i++) {
601  const MLoopTri *lt = &looptri[i];
602  const int sides = 3;
603  BBC *bbc = prim_bbc + i;
604 
605  BB_reset((BB *)bbc);
606 
607  for (int j = 0; j < sides; j++) {
608  BB_expand((BB *)bbc, verts[pbvh->mloop[lt->tri[j]].v].co);
609  }
610 
611  BBC_update_centroid(bbc);
612 
613  BB_expand(&cb, bbc->bcentroid);
614  }
615 
616  if (looptri_num) {
617  pbvh_build(pbvh, &cb, prim_bbc, looptri_num);
618  }
619 
620  MEM_freeN(prim_bbc);
621  MEM_freeN(pbvh->vert_bitmap);
622 }
623 
624 /* Do a full rebuild with on Grids data structure */
626  CCGElem **grids,
627  int totgrid,
628  CCGKey *key,
629  void **gridfaces,
630  DMFlagMat *flagmats,
631  BLI_bitmap **grid_hidden)
632 {
633  const int gridsize = key->grid_size;
634 
635  pbvh->type = PBVH_GRIDS;
636  pbvh->grids = grids;
637  pbvh->gridfaces = gridfaces;
638  pbvh->grid_flag_mats = flagmats;
639  pbvh->totgrid = totgrid;
640  pbvh->gridkey = *key;
641  pbvh->grid_hidden = grid_hidden;
642  pbvh->leaf_limit = max_ii(LEAF_LIMIT / (gridsize * gridsize), 1);
643 
644  BB cb;
645  BB_reset(&cb);
646 
647  /* For each grid, store the AABB and the AABB centroid */
648  BBC *prim_bbc = MEM_mallocN(sizeof(BBC) * totgrid, "prim_bbc");
649 
650  for (int i = 0; i < totgrid; i++) {
651  CCGElem *grid = grids[i];
652  BBC *bbc = prim_bbc + i;
653 
654  BB_reset((BB *)bbc);
655 
656  for (int j = 0; j < gridsize * gridsize; j++) {
657  BB_expand((BB *)bbc, CCG_elem_offset_co(key, grid, j));
658  }
659 
660  BBC_update_centroid(bbc);
661 
662  BB_expand(&cb, bbc->bcentroid);
663  }
664 
665  if (totgrid) {
666  pbvh_build(pbvh, &cb, prim_bbc, totgrid);
667  }
668 
669  MEM_freeN(prim_bbc);
670 }
671 
673 {
674  PBVH *pbvh = MEM_callocN(sizeof(PBVH), "pbvh");
675  pbvh->respect_hide = true;
676  return pbvh;
677 }
678 
679 void BKE_pbvh_free(PBVH *pbvh)
680 {
681  for (int i = 0; i < pbvh->totnode; i++) {
682  PBVHNode *node = &pbvh->nodes[i];
683 
684  if (node->flag & PBVH_Leaf) {
685  if (node->draw_buffers) {
686  GPU_pbvh_buffers_free(node->draw_buffers);
687  }
688  if (node->vert_indices) {
689  MEM_freeN((void *)node->vert_indices);
690  }
691  if (node->face_vert_indices) {
692  MEM_freeN((void *)node->face_vert_indices);
693  }
694  if (node->bm_faces) {
695  BLI_gset_free(node->bm_faces, NULL);
696  }
697  if (node->bm_unique_verts) {
698  BLI_gset_free(node->bm_unique_verts, NULL);
699  }
700  if (node->bm_other_verts) {
701  BLI_gset_free(node->bm_other_verts, NULL);
702  }
703  }
704  }
705 
706  if (pbvh->deformed) {
707  if (pbvh->verts) {
708  /* if pbvh was deformed, new memory was allocated for verts/faces -- free it */
709 
710  MEM_freeN((void *)pbvh->verts);
711  }
712  }
713 
714  if (pbvh->looptri) {
715  MEM_freeN((void *)pbvh->looptri);
716  }
717 
718  if (pbvh->nodes) {
719  MEM_freeN(pbvh->nodes);
720  }
721 
722  if (pbvh->prim_indices) {
723  MEM_freeN(pbvh->prim_indices);
724  }
725 
726  MEM_freeN(pbvh);
727 }
728 
729 static void pbvh_iter_begin(PBVHIter *iter,
730  PBVH *pbvh,
732  void *search_data)
733 {
734  iter->pbvh = pbvh;
735  iter->scb = scb;
736  iter->search_data = search_data;
737 
738  iter->stack = iter->stackfixed;
740 
741  iter->stack[0].node = pbvh->nodes;
742  iter->stack[0].revisiting = false;
743  iter->stacksize = 1;
744 }
745 
746 static void pbvh_iter_end(PBVHIter *iter)
747 {
748  if (iter->stackspace > STACK_FIXED_DEPTH) {
749  MEM_freeN(iter->stack);
750  }
751 }
752 
753 static void pbvh_stack_push(PBVHIter *iter, PBVHNode *node, bool revisiting)
754 {
755  if (UNLIKELY(iter->stacksize == iter->stackspace)) {
756  iter->stackspace *= 2;
757  if (iter->stackspace != (STACK_FIXED_DEPTH * 2)) {
758  iter->stack = MEM_reallocN(iter->stack, sizeof(PBVHStack) * iter->stackspace);
759  }
760  else {
761  iter->stack = MEM_mallocN(sizeof(PBVHStack) * iter->stackspace, "PBVHStack");
762  memcpy(iter->stack, iter->stackfixed, sizeof(PBVHStack) * iter->stacksize);
763  }
764  }
765 
766  iter->stack[iter->stacksize].node = node;
767  iter->stack[iter->stacksize].revisiting = revisiting;
768  iter->stacksize++;
769 }
770 
772 {
773  /* purpose here is to traverse tree, visiting child nodes before their
774  * parents, this order is necessary for e.g. computing bounding boxes */
775 
776  while (iter->stacksize) {
777  /* pop node */
778  iter->stacksize--;
779  PBVHNode *node = iter->stack[iter->stacksize].node;
780 
781  /* on a mesh with no faces this can happen
782  * can remove this check if we know meshes have at least 1 face */
783  if (node == NULL) {
784  return NULL;
785  }
786 
787  bool revisiting = iter->stack[iter->stacksize].revisiting;
788 
789  /* revisiting node already checked */
790  if (revisiting) {
791  return node;
792  }
793 
794  if (iter->scb && !iter->scb(node, iter->search_data)) {
795  continue; /* don't traverse, outside of search zone */
796  }
797 
798  if (node->flag & PBVH_Leaf) {
799  /* immediately hit leaf node */
800  return node;
801  }
802 
803  /* come back later when children are done */
804  pbvh_stack_push(iter, node, true);
805 
806  /* push two child nodes on the stack */
807  pbvh_stack_push(iter, iter->pbvh->nodes + node->children_offset + 1, false);
808  pbvh_stack_push(iter, iter->pbvh->nodes + node->children_offset, false);
809  }
810 
811  return NULL;
812 }
813 
815 {
816  while (iter->stacksize) {
817  /* pop node */
818  iter->stacksize--;
819  PBVHNode *node = iter->stack[iter->stacksize].node;
820 
821  /* on a mesh with no faces this can happen
822  * can remove this check if we know meshes have at least 1 face */
823  if (node == NULL) {
824  return NULL;
825  }
826 
827  if (iter->scb && !iter->scb(node, iter->search_data)) {
828  continue; /* don't traverse, outside of search zone */
829  }
830 
831  if (node->flag & PBVH_Leaf) {
832  /* immediately hit leaf node */
833  return node;
834  }
835 
836  pbvh_stack_push(iter, iter->pbvh->nodes + node->children_offset + 1, false);
837  pbvh_stack_push(iter, iter->pbvh->nodes + node->children_offset, false);
838  }
839 
840  return NULL;
841 }
842 
844  PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, PBVHNode ***r_array, int *r_tot)
845 {
846  PBVHIter iter;
847  PBVHNode **array = NULL, *node;
848  int tot = 0, space = 0;
849 
850  pbvh_iter_begin(&iter, pbvh, scb, search_data);
851 
852  while ((node = pbvh_iter_next(&iter))) {
853  if (node->flag & PBVH_Leaf) {
854  if (UNLIKELY(tot == space)) {
855  /* resize array if needed */
856  space = (tot == 0) ? 32 : space * 2;
857  array = MEM_recallocN_id(array, sizeof(PBVHNode *) * space, __func__);
858  }
859 
860  array[tot] = node;
861  tot++;
862  }
863  }
864 
865  pbvh_iter_end(&iter);
866 
867  if (tot == 0 && array) {
868  MEM_freeN(array);
869  array = NULL;
870  }
871 
872  *r_array = array;
873  *r_tot = tot;
874 }
875 
878  void *search_data,
880  void *hit_data)
881 {
882  PBVHIter iter;
883  PBVHNode *node;
884 
885  pbvh_iter_begin(&iter, pbvh, scb, search_data);
886 
887  while ((node = pbvh_iter_next(&iter))) {
888  if (node->flag & PBVH_Leaf) {
889  hcb(node, hit_data);
890  }
891  }
892 
893  pbvh_iter_end(&iter);
894 }
895 
896 typedef struct node_tree {
898 
899  struct node_tree *left;
900  struct node_tree *right;
902 
903 static void node_tree_insert(node_tree *tree, node_tree *new_node)
904 {
905  if (new_node->data->tmin < tree->data->tmin) {
906  if (tree->left) {
907  node_tree_insert(tree->left, new_node);
908  }
909  else {
910  tree->left = new_node;
911  }
912  }
913  else {
914  if (tree->right) {
915  node_tree_insert(tree->right, new_node);
916  }
917  else {
918  tree->right = new_node;
919  }
920  }
921 }
922 
925  void *hit_data,
926  float *tmin)
927 {
928  if (tree->left) {
929  traverse_tree(tree->left, hcb, hit_data, tmin);
930  }
931 
932  hcb(tree->data, hit_data, tmin);
933 
934  if (tree->right) {
935  traverse_tree(tree->right, hcb, hit_data, tmin);
936  }
937 }
938 
939 static void free_tree(node_tree *tree)
940 {
941  if (tree->left) {
942  free_tree(tree->left);
943  tree->left = NULL;
944  }
945 
946  if (tree->right) {
947  free_tree(tree->right);
948  tree->right = NULL;
949  }
950 
951  free(tree);
952 }
953 
955 {
956  return node->tmin;
957 }
958 
961  void *search_data,
963  void *hit_data)
964 {
965  PBVHIter iter;
966  PBVHNode *node;
967  node_tree *tree = NULL;
968 
969  pbvh_iter_begin(&iter, pbvh, scb, search_data);
970 
971  while ((node = pbvh_iter_next_occluded(&iter))) {
972  if (node->flag & PBVH_Leaf) {
973  node_tree *new_node = malloc(sizeof(node_tree));
974 
975  new_node->data = node;
976 
977  new_node->left = NULL;
978  new_node->right = NULL;
979 
980  if (tree) {
981  node_tree_insert(tree, new_node);
982  }
983  else {
984  tree = new_node;
985  }
986  }
987  }
988 
989  pbvh_iter_end(&iter);
990 
991  if (tree) {
992  float tmin = FLT_MAX;
993  traverse_tree(tree, hcb, hit_data, &tmin);
994  free_tree(tree);
995  }
996 }
997 
998 static bool update_search_cb(PBVHNode *node, void *data_v)
999 {
1000  int flag = POINTER_AS_INT(data_v);
1001 
1002  if (node->flag & PBVH_Leaf) {
1003  return (node->flag & flag) != 0;
1004  }
1005 
1006  return true;
1007 }
1008 
1009 typedef struct PBVHUpdateData {
1012  int totnode;
1013 
1014  float (*vnors)[3];
1015  int flag;
1018 
1019 static void pbvh_update_normals_accum_task_cb(void *__restrict userdata,
1020  const int n,
1021  const TaskParallelTLS *__restrict UNUSED(tls))
1022 {
1023  PBVHUpdateData *data = userdata;
1024 
1025  PBVH *pbvh = data->pbvh;
1026  PBVHNode *node = data->nodes[n];
1027  float(*vnors)[3] = data->vnors;
1028 
1029  if ((node->flag & PBVH_UpdateNormals)) {
1030  unsigned int mpoly_prev = UINT_MAX;
1031  float fn[3];
1032 
1033  const int *faces = node->prim_indices;
1034  const int totface = node->totprim;
1035 
1036  for (int i = 0; i < totface; i++) {
1037  const MLoopTri *lt = &pbvh->looptri[faces[i]];
1038  const unsigned int vtri[3] = {
1039  pbvh->mloop[lt->tri[0]].v,
1040  pbvh->mloop[lt->tri[1]].v,
1041  pbvh->mloop[lt->tri[2]].v,
1042  };
1043  const int sides = 3;
1044 
1045  /* Face normal and mask */
1046  if (lt->poly != mpoly_prev) {
1047  const MPoly *mp = &pbvh->mpoly[lt->poly];
1048  BKE_mesh_calc_poly_normal(mp, &pbvh->mloop[mp->loopstart], pbvh->verts, fn);
1049  mpoly_prev = lt->poly;
1050  }
1051 
1052  for (int j = sides; j--;) {
1053  const int v = vtri[j];
1054 
1055  if (pbvh->verts[v].flag & ME_VERT_PBVH_UPDATE) {
1056  /* Note: This avoids `lock, add_v3_v3, unlock`
1057  * and is five to ten times quicker than a spin-lock.
1058  * Not exact equivalent though, since atomicity is only ensured for one component
1059  * of the vector at a time, but here it shall not make any sensible difference. */
1060  for (int k = 3; k--;) {
1061  atomic_add_and_fetch_fl(&vnors[v][k], fn[k]);
1062  }
1063  }
1064  }
1065  }
1066  }
1067 }
1068 
1069 static void pbvh_update_normals_store_task_cb(void *__restrict userdata,
1070  const int n,
1071  const TaskParallelTLS *__restrict UNUSED(tls))
1072 {
1073  PBVHUpdateData *data = userdata;
1074  PBVH *pbvh = data->pbvh;
1075  PBVHNode *node = data->nodes[n];
1076  float(*vnors)[3] = data->vnors;
1077 
1078  if (node->flag & PBVH_UpdateNormals) {
1079  const int *verts = node->vert_indices;
1080  const int totvert = node->uniq_verts;
1081 
1082  for (int i = 0; i < totvert; i++) {
1083  const int v = verts[i];
1084  MVert *mvert = &pbvh->verts[v];
1085 
1086  /* No atomics necessary because we are iterating over uniq_verts only,
1087  * so we know only this thread will handle this vertex. */
1088  if (mvert->flag & ME_VERT_PBVH_UPDATE) {
1089  normalize_v3(vnors[v]);
1090  normal_float_to_short_v3(mvert->no, vnors[v]);
1091  mvert->flag &= ~ME_VERT_PBVH_UPDATE;
1092  }
1093  }
1094 
1095  node->flag &= ~PBVH_UpdateNormals;
1096  }
1097 }
1098 
1099 static void pbvh_faces_update_normals(PBVH *pbvh, PBVHNode **nodes, int totnode)
1100 {
1101  /* could be per node to save some memory, but also means
1102  * we have to store for each vertex which node it is in */
1103  float(*vnors)[3] = MEM_callocN(sizeof(*vnors) * pbvh->totvert, __func__);
1104 
1105  /* subtle assumptions:
1106  * - We know that for all edited vertices, the nodes with faces
1107  * adjacent to these vertices have been marked with PBVH_UpdateNormals.
1108  * This is true because if the vertex is inside the brush radius, the
1109  * bounding box of its adjacent faces will be as well.
1110  * - However this is only true for the vertices that have actually been
1111  * edited, not for all vertices in the nodes marked for update, so we
1112  * can only update vertices marked with ME_VERT_PBVH_UPDATE.
1113  */
1114 
1115  PBVHUpdateData data = {
1116  .pbvh = pbvh,
1117  .nodes = nodes,
1118  .vnors = vnors,
1119  };
1120 
1121  TaskParallelSettings settings;
1122  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
1123 
1126 
1127  MEM_freeN(vnors);
1128 }
1129 
1130 static void pbvh_update_mask_redraw_task_cb(void *__restrict userdata,
1131  const int n,
1132  const TaskParallelTLS *__restrict UNUSED(tls))
1133 {
1134 
1135  PBVHUpdateData *data = userdata;
1136  PBVH *pbvh = data->pbvh;
1137  PBVHNode *node = data->nodes[n];
1138  if (node->flag & PBVH_UpdateMask) {
1139 
1140  bool has_unmasked = false;
1141  bool has_masked = true;
1142  if (node->flag & PBVH_Leaf) {
1143  PBVHVertexIter vd;
1144 
1146  if (vd.mask && *vd.mask < 1.0f) {
1147  has_unmasked = true;
1148  }
1149  if (vd.mask && *vd.mask > 0.0f) {
1150  has_masked = false;
1151  }
1152  }
1154  }
1155  else {
1156  has_unmasked = true;
1157  has_masked = true;
1158  }
1159  BKE_pbvh_node_fully_masked_set(node, !has_unmasked);
1161 
1162  node->flag &= ~PBVH_UpdateMask;
1163  }
1164 }
1165 
1166 static void pbvh_update_mask_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
1167 {
1168  PBVHUpdateData data = {
1169  .pbvh = pbvh,
1170  .nodes = nodes,
1171  .flag = flag,
1172  };
1173 
1174  TaskParallelSettings settings;
1175  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
1177 }
1178 
1179 static void pbvh_update_visibility_redraw_task_cb(void *__restrict userdata,
1180  const int n,
1181  const TaskParallelTLS *__restrict UNUSED(tls))
1182 {
1183 
1184  PBVHUpdateData *data = userdata;
1185  PBVH *pbvh = data->pbvh;
1186  PBVHNode *node = data->nodes[n];
1187  if (node->flag & PBVH_UpdateVisibility) {
1188  node->flag &= ~PBVH_UpdateVisibility;
1190  if (node->flag & PBVH_Leaf) {
1191  PBVHVertexIter vd;
1193  if (vd.visible) {
1195  return;
1196  }
1197  }
1199  }
1200  }
1201 }
1202 
1203 static void pbvh_update_visibility_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
1204 {
1205  PBVHUpdateData data = {
1206  .pbvh = pbvh,
1207  .nodes = nodes,
1208  .flag = flag,
1209  };
1210 
1211  TaskParallelSettings settings;
1212  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
1214 }
1215 
1216 static void pbvh_update_BB_redraw_task_cb(void *__restrict userdata,
1217  const int n,
1218  const TaskParallelTLS *__restrict UNUSED(tls))
1219 {
1220  PBVHUpdateData *data = userdata;
1221  PBVH *pbvh = data->pbvh;
1222  PBVHNode *node = data->nodes[n];
1223  const int flag = data->flag;
1224 
1225  if ((flag & PBVH_UpdateBB) && (node->flag & PBVH_UpdateBB)) {
1226  /* don't clear flag yet, leave it for flushing later */
1227  /* Note that bvh usage is read-only here, so no need to thread-protect it. */
1228  update_node_vb(pbvh, node);
1229  }
1230 
1231  if ((flag & PBVH_UpdateOriginalBB) && (node->flag & PBVH_UpdateOriginalBB)) {
1232  node->orig_vb = node->vb;
1233  }
1234 
1235  if ((flag & PBVH_UpdateRedraw) && (node->flag & PBVH_UpdateRedraw)) {
1236  node->flag &= ~PBVH_UpdateRedraw;
1237  }
1238 }
1239 
1240 void pbvh_update_BB_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
1241 {
1242  /* update BB, redraw flag */
1243  PBVHUpdateData data = {
1244  .pbvh = pbvh,
1245  .nodes = nodes,
1246  .flag = flag,
1247  };
1248 
1249  TaskParallelSettings settings;
1250  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
1252 }
1253 
1255 {
1258  return update_flags;
1259 }
1260 
1261 static void pbvh_update_draw_buffer_cb(void *__restrict userdata,
1262  const int n,
1263  const TaskParallelTLS *__restrict UNUSED(tls))
1264 {
1265  /* Create and update draw buffers. The functions called here must not
1266  * do any OpenGL calls. Flags are not cleared immediately, that happens
1267  * after GPU_pbvh_buffer_flush() which does the final OpenGL calls. */
1268  PBVHUpdateData *data = userdata;
1269  PBVH *pbvh = data->pbvh;
1270  PBVHNode *node = data->nodes[n];
1271 
1272  if (node->flag & PBVH_RebuildDrawBuffers) {
1273  switch (pbvh->type) {
1274  case PBVH_GRIDS:
1275  node->draw_buffers = GPU_pbvh_grid_buffers_build(node->totprim, pbvh->grid_hidden);
1276  break;
1277  case PBVH_FACES:
1278  node->draw_buffers = GPU_pbvh_mesh_buffers_build(
1279  pbvh->mpoly,
1280  pbvh->mloop,
1281  pbvh->looptri,
1282  pbvh->verts,
1283  node->prim_indices,
1285  node->totprim,
1286  pbvh->mesh);
1287  break;
1288  case PBVH_BMESH:
1289  node->draw_buffers = GPU_pbvh_bmesh_buffers_build(pbvh->flags &
1291  break;
1292  }
1293  }
1294 
1295  if (node->flag & PBVH_UpdateDrawBuffers) {
1296  const int update_flags = pbvh_get_buffers_update_flags(pbvh);
1297  switch (pbvh->type) {
1298  case PBVH_GRIDS:
1299  GPU_pbvh_grid_buffers_update(node->draw_buffers,
1300  pbvh->subdiv_ccg,
1301  pbvh->grids,
1302  pbvh->grid_flag_mats,
1303  node->prim_indices,
1304  node->totprim,
1305  pbvh->face_sets,
1306  pbvh->face_sets_color_seed,
1308  &pbvh->gridkey,
1309  update_flags);
1310  break;
1311  case PBVH_FACES:
1312  GPU_pbvh_mesh_buffers_update(node->draw_buffers,
1313  pbvh->verts,
1317  pbvh->face_sets_color_seed,
1320  update_flags);
1321  break;
1322  case PBVH_BMESH:
1323  GPU_pbvh_bmesh_buffers_update(node->draw_buffers,
1324  pbvh->bm,
1325  node->bm_faces,
1326  node->bm_unique_verts,
1327  node->bm_other_verts,
1328  update_flags);
1329  break;
1330  }
1331  }
1332 }
1333 
1334 static void pbvh_update_draw_buffers(PBVH *pbvh, PBVHNode **nodes, int totnode, int update_flag)
1335 {
1336  if ((update_flag & PBVH_RebuildDrawBuffers) || ELEM(pbvh->type, PBVH_GRIDS, PBVH_BMESH)) {
1337  /* Free buffers uses OpenGL, so not in parallel. */
1338  for (int n = 0; n < totnode; n++) {
1339  PBVHNode *node = nodes[n];
1340  if (node->flag & PBVH_RebuildDrawBuffers) {
1341  GPU_pbvh_buffers_free(node->draw_buffers);
1342  node->draw_buffers = NULL;
1343  }
1344  else if ((node->flag & PBVH_UpdateDrawBuffers) && node->draw_buffers) {
1345  if (pbvh->type == PBVH_GRIDS) {
1347  node->draw_buffers, pbvh->grid_flag_mats, node->prim_indices);
1348  }
1349  else if (pbvh->type == PBVH_BMESH) {
1351  }
1352  }
1353  }
1354  }
1355 
1356  /* Parallel creation and update of draw buffers. */
1357  PBVHUpdateData data = {
1358  .pbvh = pbvh,
1359  .nodes = nodes,
1360  };
1361 
1362  TaskParallelSettings settings;
1363  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
1364  BLI_task_parallel_range(0, totnode, &data, pbvh_update_draw_buffer_cb, &settings);
1365 
1366  for (int i = 0; i < totnode; i++) {
1367  PBVHNode *node = nodes[i];
1368 
1369  if (node->flag & PBVH_UpdateDrawBuffers) {
1370  /* Flush buffers uses OpenGL, so not in parallel. */
1371  GPU_pbvh_buffers_update_flush(node->draw_buffers);
1372  }
1373 
1375  }
1376 }
1377 
1378 static int pbvh_flush_bb(PBVH *pbvh, PBVHNode *node, int flag)
1379 {
1380  int update = 0;
1381 
1382  /* difficult to multithread well, we just do single threaded recursive */
1383  if (node->flag & PBVH_Leaf) {
1384  if (flag & PBVH_UpdateBB) {
1385  update |= (node->flag & PBVH_UpdateBB);
1386  node->flag &= ~PBVH_UpdateBB;
1387  }
1388 
1389  if (flag & PBVH_UpdateOriginalBB) {
1390  update |= (node->flag & PBVH_UpdateOriginalBB);
1391  node->flag &= ~PBVH_UpdateOriginalBB;
1392  }
1393 
1394  return update;
1395  }
1396 
1397  update |= pbvh_flush_bb(pbvh, pbvh->nodes + node->children_offset, flag);
1398  update |= pbvh_flush_bb(pbvh, pbvh->nodes + node->children_offset + 1, flag);
1399 
1400  if (update & PBVH_UpdateBB) {
1401  update_node_vb(pbvh, node);
1402  }
1403  if (update & PBVH_UpdateOriginalBB) {
1404  node->orig_vb = node->vb;
1405  }
1406 
1407  return update;
1408 }
1409 
1410 void BKE_pbvh_update_bounds(PBVH *pbvh, int flag)
1411 {
1412  if (!pbvh->nodes) {
1413  return;
1414  }
1415 
1416  PBVHNode **nodes;
1417  int totnode;
1418 
1419  BKE_pbvh_search_gather(pbvh, update_search_cb, POINTER_FROM_INT(flag), &nodes, &totnode);
1420 
1422  pbvh_update_BB_redraw(pbvh, nodes, totnode, flag);
1423  }
1424 
1425  if (flag & (PBVH_UpdateBB | PBVH_UpdateOriginalBB)) {
1426  pbvh_flush_bb(pbvh, pbvh->nodes, flag);
1427  }
1428 
1429  MEM_SAFE_FREE(nodes);
1430 }
1431 
1432 void BKE_pbvh_update_vertex_data(PBVH *pbvh, int flag)
1433 {
1434  if (!pbvh->nodes) {
1435  return;
1436  }
1437 
1438  PBVHNode **nodes;
1439  int totnode;
1440 
1441  BKE_pbvh_search_gather(pbvh, update_search_cb, POINTER_FROM_INT(flag), &nodes, &totnode);
1442 
1443  if (flag & (PBVH_UpdateMask)) {
1444  pbvh_update_mask_redraw(pbvh, nodes, totnode, flag);
1445  }
1446 
1447  if (flag & (PBVH_UpdateColor)) {
1448  /* Do nothing */
1449  }
1450 
1451  if (flag & (PBVH_UpdateVisibility)) {
1452  pbvh_update_visibility_redraw(pbvh, nodes, totnode, flag);
1453  }
1454 
1455  if (nodes) {
1456  MEM_freeN(nodes);
1457  }
1458 }
1459 
1461 {
1462  MVert *mvert;
1463  const int *vert_indices;
1464  int totvert, i;
1465  BKE_pbvh_node_num_verts(pbvh, node, NULL, &totvert);
1466  BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
1467 
1468  for (i = 0; i < totvert; i++) {
1469  MVert *v = &mvert[vert_indices[i]];
1470  if (!(v->flag & ME_HIDE)) {
1472  return;
1473  }
1474  }
1475 
1477 }
1478 
1480 {
1481  CCGElem **grids;
1482  BLI_bitmap **grid_hidden;
1483  int *grid_indices, totgrid, i;
1484 
1485  BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid, NULL, NULL, &grids);
1486  grid_hidden = BKE_pbvh_grid_hidden(pbvh);
1487  CCGKey key = *BKE_pbvh_get_grid_key(pbvh);
1488 
1489  for (i = 0; i < totgrid; i++) {
1490  int g = grid_indices[i], x, y;
1491  BLI_bitmap *gh = grid_hidden[g];
1492 
1493  if (!gh) {
1495  return;
1496  }
1497 
1498  for (y = 0; y < key.grid_size; y++) {
1499  for (x = 0; x < key.grid_size; x++) {
1500  if (!BLI_BITMAP_TEST(gh, y * key.grid_size + x)) {
1502  return;
1503  }
1504  }
1505  }
1506  }
1508 }
1509 
1511 {
1512  GSet *unique, *other;
1513 
1516 
1517  GSetIterator gs_iter;
1518 
1519  GSET_ITER (gs_iter, unique) {
1520  BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
1523  return;
1524  }
1525  }
1526 
1527  GSET_ITER (gs_iter, other) {
1528  BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
1531  return;
1532  }
1533  }
1534 
1536 }
1537 
1538 static void pbvh_update_visibility_task_cb(void *__restrict userdata,
1539  const int n,
1540  const TaskParallelTLS *__restrict UNUSED(tls))
1541 {
1542 
1543  PBVHUpdateData *data = userdata;
1544  PBVH *pbvh = data->pbvh;
1545  PBVHNode *node = data->nodes[n];
1546  if (node->flag & PBVH_UpdateVisibility) {
1547  switch (BKE_pbvh_type(pbvh)) {
1548  case PBVH_FACES:
1550  break;
1551  case PBVH_GRIDS:
1553  break;
1554  case PBVH_BMESH:
1556  break;
1557  }
1558  node->flag &= ~PBVH_UpdateVisibility;
1559  }
1560 }
1561 
1562 static void pbvh_update_visibility(PBVH *pbvh, PBVHNode **nodes, int totnode)
1563 {
1564  PBVHUpdateData data = {
1565  .pbvh = pbvh,
1566  .nodes = nodes,
1567  };
1568 
1569  TaskParallelSettings settings;
1570  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
1572 }
1573 
1575 {
1576  if (!pbvh->nodes) {
1577  return;
1578  }
1579 
1580  PBVHNode **nodes;
1581  int totnode;
1582 
1584  pbvh, update_search_cb, POINTER_FROM_INT(PBVH_UpdateVisibility), &nodes, &totnode);
1585  pbvh_update_visibility(pbvh, nodes, totnode);
1586 
1587  if (nodes) {
1588  MEM_freeN(nodes);
1589  }
1590 }
1591 
1592 void BKE_pbvh_redraw_BB(PBVH *pbvh, float bb_min[3], float bb_max[3])
1593 {
1594  PBVHIter iter;
1595  PBVHNode *node;
1596  BB bb;
1597 
1598  BB_reset(&bb);
1599 
1600  pbvh_iter_begin(&iter, pbvh, NULL, NULL);
1601 
1602  while ((node = pbvh_iter_next(&iter))) {
1603  if (node->flag & PBVH_UpdateRedraw) {
1604  BB_expand_with_bb(&bb, &node->vb);
1605  }
1606  }
1607 
1608  pbvh_iter_end(&iter);
1609 
1610  copy_v3_v3(bb_min, bb.bmin);
1611  copy_v3_v3(bb_max, bb.bmax);
1612 }
1613 
1614 void BKE_pbvh_get_grid_updates(PBVH *pbvh, bool clear, void ***r_gridfaces, int *r_totface)
1615 {
1616  GSet *face_set = BLI_gset_ptr_new(__func__);
1617  PBVHNode *node;
1618  PBVHIter iter;
1619 
1620  pbvh_iter_begin(&iter, pbvh, NULL, NULL);
1621 
1622  while ((node = pbvh_iter_next(&iter))) {
1623  if (node->flag & PBVH_UpdateNormals) {
1624  for (uint i = 0; i < node->totprim; i++) {
1625  void *face = pbvh->gridfaces[node->prim_indices[i]];
1626  BLI_gset_add(face_set, face);
1627  }
1628 
1629  if (clear) {
1630  node->flag &= ~PBVH_UpdateNormals;
1631  }
1632  }
1633  }
1634 
1635  pbvh_iter_end(&iter);
1636 
1637  const int tot = BLI_gset_len(face_set);
1638  if (tot == 0) {
1639  *r_totface = 0;
1640  *r_gridfaces = NULL;
1641  BLI_gset_free(face_set, NULL);
1642  return;
1643  }
1644 
1645  void **faces = MEM_mallocN(sizeof(*faces) * tot, "PBVH Grid Faces");
1646 
1647  GSetIterator gs_iter;
1648  int i;
1649  GSET_ITER_INDEX (gs_iter, face_set, i) {
1650  faces[i] = BLI_gsetIterator_getKey(&gs_iter);
1651  }
1652 
1653  BLI_gset_free(face_set, NULL);
1654 
1655  *r_totface = tot;
1656  *r_gridfaces = faces;
1657 }
1658 
1659 /***************************** PBVH Access ***********************************/
1660 
1662 {
1663  return pbvh->type;
1664 }
1665 
1666 bool BKE_pbvh_has_faces(const PBVH *pbvh)
1667 {
1668  if (pbvh->type == PBVH_BMESH) {
1669  return (pbvh->bm->totface != 0);
1670  }
1671 
1672  return (pbvh->totprim != 0);
1673 }
1674 
1675 void BKE_pbvh_bounding_box(const PBVH *pbvh, float min[3], float max[3])
1676 {
1677  if (pbvh->totnode) {
1678  const BB *bb = &pbvh->nodes[0].vb;
1679  copy_v3_v3(min, bb->bmin);
1680  copy_v3_v3(max, bb->bmax);
1681  }
1682  else {
1683  zero_v3(min);
1684  zero_v3(max);
1685  }
1686 }
1687 
1689 {
1690  BLI_assert(pbvh->type == PBVH_GRIDS);
1691  return pbvh->grid_hidden;
1692 }
1693 
1694 const CCGKey *BKE_pbvh_get_grid_key(const PBVH *pbvh)
1695 {
1696  BLI_assert(pbvh->type == PBVH_GRIDS);
1697  return &pbvh->gridkey;
1698 }
1699 
1700 struct CCGElem **BKE_pbvh_get_grids(const PBVH *pbvh)
1701 {
1702  BLI_assert(pbvh->type == PBVH_GRIDS);
1703  return pbvh->grids;
1704 }
1705 
1707 {
1708  BLI_assert(pbvh->type == PBVH_GRIDS);
1709  return pbvh->grid_hidden;
1710 }
1711 
1713 {
1714  BLI_assert(pbvh->type == PBVH_GRIDS);
1715  return pbvh->totgrid * pbvh->gridkey.grid_area;
1716 }
1717 
1719 {
1720  BLI_assert(pbvh->type == PBVH_GRIDS);
1721  return pbvh->totgrid * (pbvh->gridkey.grid_size - 1) * (pbvh->gridkey.grid_size - 1);
1722 }
1723 
1725 {
1726  BLI_assert(pbvh->type == PBVH_BMESH);
1727  return pbvh->bm;
1728 }
1729 
1730 /***************************** Node Access ***********************************/
1731 
1733 {
1736 }
1737 
1739 {
1741 }
1742 
1744 {
1746 }
1747 
1749 {
1752 }
1753 
1755 {
1757 }
1758 
1760 {
1762 }
1763 
1765 {
1766  node->flag |= PBVH_UpdateNormals;
1767 }
1768 
1770 {
1771  BLI_assert(node->flag & PBVH_Leaf);
1772 
1773  if (fully_hidden) {
1774  node->flag |= PBVH_FullyHidden;
1775  }
1776  else {
1777  node->flag &= ~PBVH_FullyHidden;
1778  }
1779 }
1780 
1782 {
1783  return (node->flag & PBVH_Leaf) && (node->flag & PBVH_FullyHidden);
1784 }
1785 
1787 {
1788  BLI_assert(node->flag & PBVH_Leaf);
1789 
1790  if (fully_masked) {
1791  node->flag |= PBVH_FullyMasked;
1792  }
1793  else {
1794  node->flag &= ~PBVH_FullyMasked;
1795  }
1796 }
1797 
1799 {
1800  return (node->flag & PBVH_Leaf) && (node->flag & PBVH_FullyMasked);
1801 }
1802 
1804 {
1805  BLI_assert(node->flag & PBVH_Leaf);
1806 
1807  if (fully_masked) {
1808  node->flag |= PBVH_FullyUnmasked;
1809  }
1810  else {
1811  node->flag &= ~PBVH_FullyUnmasked;
1812  }
1813 }
1814 
1816 {
1817  return (node->flag & PBVH_Leaf) && (node->flag & PBVH_FullyUnmasked);
1818 }
1819 
1821  PBVHNode *node,
1822  const int **r_vert_indices,
1823  MVert **r_verts)
1824 {
1825  if (r_vert_indices) {
1826  *r_vert_indices = node->vert_indices;
1827  }
1828 
1829  if (r_verts) {
1830  *r_verts = pbvh->verts;
1831  }
1832 }
1833 
1834 void BKE_pbvh_node_num_verts(PBVH *pbvh, PBVHNode *node, int *r_uniquevert, int *r_totvert)
1835 {
1836  int tot;
1837 
1838  switch (pbvh->type) {
1839  case PBVH_GRIDS:
1840  tot = node->totprim * pbvh->gridkey.grid_area;
1841  if (r_totvert) {
1842  *r_totvert = tot;
1843  }
1844  if (r_uniquevert) {
1845  *r_uniquevert = tot;
1846  }
1847  break;
1848  case PBVH_FACES:
1849  if (r_totvert) {
1850  *r_totvert = node->uniq_verts + node->face_verts;
1851  }
1852  if (r_uniquevert) {
1853  *r_uniquevert = node->uniq_verts;
1854  }
1855  break;
1856  case PBVH_BMESH:
1857  tot = BLI_gset_len(node->bm_unique_verts);
1858  if (r_totvert) {
1859  *r_totvert = tot + BLI_gset_len(node->bm_other_verts);
1860  }
1861  if (r_uniquevert) {
1862  *r_uniquevert = tot;
1863  }
1864  break;
1865  }
1866 }
1867 
1869  PBVHNode *node,
1870  int **r_grid_indices,
1871  int *r_totgrid,
1872  int *r_maxgrid,
1873  int *r_gridsize,
1874  CCGElem ***r_griddata)
1875 {
1876  switch (pbvh->type) {
1877  case PBVH_GRIDS:
1878  if (r_grid_indices) {
1879  *r_grid_indices = node->prim_indices;
1880  }
1881  if (r_totgrid) {
1882  *r_totgrid = node->totprim;
1883  }
1884  if (r_maxgrid) {
1885  *r_maxgrid = pbvh->totgrid;
1886  }
1887  if (r_gridsize) {
1888  *r_gridsize = pbvh->gridkey.grid_size;
1889  }
1890  if (r_griddata) {
1891  *r_griddata = pbvh->grids;
1892  }
1893  break;
1894  case PBVH_FACES:
1895  case PBVH_BMESH:
1896  if (r_grid_indices) {
1897  *r_grid_indices = NULL;
1898  }
1899  if (r_totgrid) {
1900  *r_totgrid = 0;
1901  }
1902  if (r_maxgrid) {
1903  *r_maxgrid = 0;
1904  }
1905  if (r_gridsize) {
1906  *r_gridsize = 0;
1907  }
1908  if (r_griddata) {
1909  *r_griddata = NULL;
1910  }
1911  break;
1912  }
1913 }
1914 
1915 void BKE_pbvh_node_get_BB(PBVHNode *node, float bb_min[3], float bb_max[3])
1916 {
1917  copy_v3_v3(bb_min, node->vb.bmin);
1918  copy_v3_v3(bb_max, node->vb.bmax);
1919 }
1920 
1921 void BKE_pbvh_node_get_original_BB(PBVHNode *node, float bb_min[3], float bb_max[3])
1922 {
1923  copy_v3_v3(bb_min, node->orig_vb.bmin);
1924  copy_v3_v3(bb_max, node->orig_vb.bmax);
1925 }
1926 
1927 void BKE_pbvh_node_get_proxies(PBVHNode *node, PBVHProxyNode **proxies, int *proxy_count)
1928 {
1929  if (node->proxy_count > 0) {
1930  if (proxies) {
1931  *proxies = node->proxies;
1932  }
1933  if (proxy_count) {
1934  *proxy_count = node->proxy_count;
1935  }
1936  }
1937  else {
1938  if (proxies) {
1939  *proxies = NULL;
1940  }
1941  if (proxy_count) {
1942  *proxy_count = 0;
1943  }
1944  }
1945 }
1946 
1948  int (**r_orco_tris)[3],
1949  int *r_orco_tris_num,
1950  float (**r_orco_coords)[3])
1951 {
1952  *r_orco_tris = node->bm_ortri;
1953  *r_orco_tris_num = node->bm_tot_ortri;
1954  *r_orco_coords = node->bm_orco;
1955 }
1956 
1963 {
1964  BLI_assert(pbvh->type == PBVH_FACES);
1965  const int *verts = node->vert_indices;
1966  const int totvert = node->uniq_verts + node->face_verts;
1967 
1968  for (int i = 0; i < totvert; i++) {
1969  const int v = verts[i];
1970  const MVert *mvert = &pbvh->verts[v];
1971 
1972  if (mvert->flag & ME_VERT_PBVH_UPDATE) {
1973  return true;
1974  }
1975  }
1976 
1977  return false;
1978 }
1979 
1980 /********************************* Raycast ***********************************/
1981 
1982 typedef struct {
1983  struct IsectRayAABB_Precalc ray;
1984  bool original;
1985 } RaycastData;
1986 
1987 static bool ray_aabb_intersect(PBVHNode *node, void *data_v)
1988 {
1989  RaycastData *rcd = data_v;
1990  const float *bb_min, *bb_max;
1991 
1992  if (rcd->original) {
1993  /* BKE_pbvh_node_get_original_BB */
1994  bb_min = node->orig_vb.bmin;
1995  bb_max = node->orig_vb.bmax;
1996  }
1997  else {
1998  /* BKE_pbvh_node_get_BB */
1999  bb_min = node->vb.bmin;
2000  bb_max = node->vb.bmax;
2001  }
2002 
2003  return isect_ray_aabb_v3(&rcd->ray, bb_min, bb_max, &node->tmin);
2004 }
2005 
2008  void *data,
2009  const float ray_start[3],
2010  const float ray_normal[3],
2011  bool original)
2012 {
2013  RaycastData rcd;
2014 
2015  isect_ray_aabb_v3_precalc(&rcd.ray, ray_start, ray_normal);
2016  rcd.original = original;
2017 
2019 }
2020 
2021 bool ray_face_intersection_quad(const float ray_start[3],
2022  struct IsectRayPrecalc *isect_precalc,
2023  const float t0[3],
2024  const float t1[3],
2025  const float t2[3],
2026  const float t3[3],
2027  float *depth)
2028 {
2029  float depth_test;
2030 
2031  if ((isect_ray_tri_watertight_v3(ray_start, isect_precalc, t0, t1, t2, &depth_test, NULL) &&
2032  (depth_test < *depth)) ||
2033  (isect_ray_tri_watertight_v3(ray_start, isect_precalc, t0, t2, t3, &depth_test, NULL) &&
2034  (depth_test < *depth))) {
2035  *depth = depth_test;
2036  return true;
2037  }
2038 
2039  return false;
2040 }
2041 
2042 bool ray_face_intersection_tri(const float ray_start[3],
2043  struct IsectRayPrecalc *isect_precalc,
2044  const float t0[3],
2045  const float t1[3],
2046  const float t2[3],
2047  float *depth)
2048 {
2049  float depth_test;
2050  if ((isect_ray_tri_watertight_v3(ray_start, isect_precalc, t0, t1, t2, &depth_test, NULL) &&
2051  (depth_test < *depth))) {
2052  *depth = depth_test;
2053  return true;
2054  }
2055 
2056  return false;
2057 }
2058 
2059 /* Take advantage of the fact we know this wont be an intersection.
2060  * Just handle ray-tri edges. */
2061 static float dist_squared_ray_to_tri_v3_fast(const float ray_origin[3],
2062  const float ray_direction[3],
2063  const float v0[3],
2064  const float v1[3],
2065  const float v2[3],
2066  float r_point[3],
2067  float *r_depth)
2068 {
2069  const float *tri[3] = {v0, v1, v2};
2070  float dist_sq_best = FLT_MAX;
2071  for (int i = 0, j = 2; i < 3; j = i++) {
2072  float point_test[3], depth_test = FLT_MAX;
2073  const float dist_sq_test = dist_squared_ray_to_seg_v3(
2074  ray_origin, ray_direction, tri[i], tri[j], point_test, &depth_test);
2075  if (dist_sq_test < dist_sq_best || i == 0) {
2076  copy_v3_v3(r_point, point_test);
2077  *r_depth = depth_test;
2078  dist_sq_best = dist_sq_test;
2079  }
2080  }
2081  return dist_sq_best;
2082 }
2083 
2084 bool ray_face_nearest_quad(const float ray_start[3],
2085  const float ray_normal[3],
2086  const float t0[3],
2087  const float t1[3],
2088  const float t2[3],
2089  const float t3[3],
2090  float *depth,
2091  float *dist_sq)
2092 {
2093  float dist_sq_test;
2094  float co[3], depth_test;
2095 
2096  if (((dist_sq_test = dist_squared_ray_to_tri_v3_fast(
2097  ray_start, ray_normal, t0, t1, t2, co, &depth_test)) < *dist_sq)) {
2098  *dist_sq = dist_sq_test;
2099  *depth = depth_test;
2100  if (((dist_sq_test = dist_squared_ray_to_tri_v3_fast(
2101  ray_start, ray_normal, t0, t2, t3, co, &depth_test)) < *dist_sq)) {
2102  *dist_sq = dist_sq_test;
2103  *depth = depth_test;
2104  }
2105  return true;
2106  }
2107 
2108  return false;
2109 }
2110 
2111 bool ray_face_nearest_tri(const float ray_start[3],
2112  const float ray_normal[3],
2113  const float t0[3],
2114  const float t1[3],
2115  const float t2[3],
2116  float *depth,
2117  float *dist_sq)
2118 {
2119  float dist_sq_test;
2120  float co[3], depth_test;
2121 
2122  if (((dist_sq_test = dist_squared_ray_to_tri_v3_fast(
2123  ray_start, ray_normal, t0, t1, t2, co, &depth_test)) < *dist_sq)) {
2124  *dist_sq = dist_sq_test;
2125  *depth = depth_test;
2126  return true;
2127  }
2128 
2129  return false;
2130 }
2131 
2132 static bool pbvh_faces_node_raycast(PBVH *pbvh,
2133  const PBVHNode *node,
2134  float (*origco)[3],
2135  const float ray_start[3],
2136  const float ray_normal[3],
2137  struct IsectRayPrecalc *isect_precalc,
2138  float *depth,
2139  int *r_active_vertex_index,
2140  int *r_active_face_index,
2141  float *r_face_normal)
2142 {
2143  const MVert *vert = pbvh->verts;
2144  const MLoop *mloop = pbvh->mloop;
2145  const int *faces = node->prim_indices;
2146  int totface = node->totprim;
2147  bool hit = false;
2148  float nearest_vertex_co[3] = {0.0f};
2149 
2150  for (int i = 0; i < totface; i++) {
2151  const MLoopTri *lt = &pbvh->looptri[faces[i]];
2152  const int *face_verts = node->face_vert_indices[i];
2153 
2154  if (pbvh->respect_hide && paint_is_face_hidden(lt, vert, mloop)) {
2155  continue;
2156  }
2157 
2158  const float *co[3];
2159  if (origco) {
2160  /* intersect with backuped original coordinates */
2161  co[0] = origco[face_verts[0]];
2162  co[1] = origco[face_verts[1]];
2163  co[2] = origco[face_verts[2]];
2164  }
2165  else {
2166  /* intersect with current coordinates */
2167  co[0] = vert[mloop[lt->tri[0]].v].co;
2168  co[1] = vert[mloop[lt->tri[1]].v].co;
2169  co[2] = vert[mloop[lt->tri[2]].v].co;
2170  }
2171 
2172  if (ray_face_intersection_tri(ray_start, isect_precalc, co[0], co[1], co[2], depth)) {
2173  hit = true;
2174 
2175  if (r_face_normal) {
2176  normal_tri_v3(r_face_normal, co[0], co[1], co[2]);
2177  }
2178 
2179  if (r_active_vertex_index) {
2180  float location[3] = {0.0f};
2181  madd_v3_v3v3fl(location, ray_start, ray_normal, *depth);
2182  for (int j = 0; j < 3; j++) {
2183  /* Always assign nearest_vertex_co in the first iteration to avoid comparison against
2184  * uninitialized values. This stores the closest vertex in the current intersecting
2185  * triangle. */
2186  if (j == 0 ||
2187  len_squared_v3v3(location, co[j]) < len_squared_v3v3(location, nearest_vertex_co)) {
2188  copy_v3_v3(nearest_vertex_co, co[j]);
2189  *r_active_vertex_index = mloop[lt->tri[j]].v;
2190  *r_active_face_index = lt->poly;
2191  }
2192  }
2193  }
2194  }
2195  }
2196 
2197  return hit;
2198 }
2199 
2200 static bool pbvh_grids_node_raycast(PBVH *pbvh,
2201  PBVHNode *node,
2202  float (*origco)[3],
2203  const float ray_start[3],
2204  const float ray_normal[3],
2205  struct IsectRayPrecalc *isect_precalc,
2206  float *depth,
2207  int *r_active_vertex_index,
2208  int *r_active_grid_index,
2209  float *r_face_normal)
2210 {
2211  const int totgrid = node->totprim;
2212  const int gridsize = pbvh->gridkey.grid_size;
2213  bool hit = false;
2214  float nearest_vertex_co[3] = {0.0};
2215  const CCGKey *gridkey = &pbvh->gridkey;
2216 
2217  for (int i = 0; i < totgrid; i++) {
2218  const int grid_index = node->prim_indices[i];
2219  CCGElem *grid = pbvh->grids[grid_index];
2220  BLI_bitmap *gh;
2221 
2222  if (!grid) {
2223  continue;
2224  }
2225 
2226  gh = pbvh->grid_hidden[grid_index];
2227 
2228  for (int y = 0; y < gridsize - 1; y++) {
2229  for (int x = 0; x < gridsize - 1; x++) {
2230  /* check if grid face is hidden */
2231  if (gh) {
2232  if (paint_is_grid_face_hidden(gh, gridsize, x, y)) {
2233  continue;
2234  }
2235  }
2236 
2237  const float *co[4];
2238  if (origco) {
2239  co[0] = origco[y * gridsize + x];
2240  co[1] = origco[y * gridsize + x + 1];
2241  co[2] = origco[(y + 1) * gridsize + x + 1];
2242  co[3] = origco[(y + 1) * gridsize + x];
2243  }
2244  else {
2245  co[0] = CCG_grid_elem_co(gridkey, grid, x, y);
2246  co[1] = CCG_grid_elem_co(gridkey, grid, x + 1, y);
2247  co[2] = CCG_grid_elem_co(gridkey, grid, x + 1, y + 1);
2248  co[3] = CCG_grid_elem_co(gridkey, grid, x, y + 1);
2249  }
2250 
2252  ray_start, isect_precalc, co[0], co[1], co[2], co[3], depth)) {
2253  hit = true;
2254 
2255  if (r_face_normal) {
2256  normal_quad_v3(r_face_normal, co[0], co[1], co[2], co[3]);
2257  }
2258 
2259  if (r_active_vertex_index) {
2260  float location[3] = {0.0};
2261  madd_v3_v3v3fl(location, ray_start, ray_normal, *depth);
2262 
2263  const int x_it[4] = {0, 1, 1, 0};
2264  const int y_it[4] = {0, 0, 1, 1};
2265 
2266  for (int j = 0; j < 4; j++) {
2267  /* Always assign nearest_vertex_co in the first iteration to avoid comparison against
2268  * uninitialized values. This stores the closest vertex in the current intersecting
2269  * quad. */
2270  if (j == 0 || len_squared_v3v3(location, co[j]) <
2271  len_squared_v3v3(location, nearest_vertex_co)) {
2272  copy_v3_v3(nearest_vertex_co, co[j]);
2273 
2274  *r_active_vertex_index = gridkey->grid_area * grid_index +
2275  (y + y_it[j]) * gridkey->grid_size + (x + x_it[j]);
2276  }
2277  }
2278  }
2279  if (r_active_grid_index) {
2280  *r_active_grid_index = grid_index;
2281  }
2282  }
2283  }
2284  }
2285 
2286  if (origco) {
2287  origco += gridsize * gridsize;
2288  }
2289  }
2290 
2291  return hit;
2292 }
2293 
2295  PBVHNode *node,
2296  float (*origco)[3],
2297  bool use_origco,
2298  const float ray_start[3],
2299  const float ray_normal[3],
2300  struct IsectRayPrecalc *isect_precalc,
2301  float *depth,
2302  int *active_vertex_index,
2303  int *active_face_grid_index,
2304  float *face_normal)
2305 {
2306  bool hit = false;
2307 
2308  if (node->flag & PBVH_FullyHidden) {
2309  return false;
2310  }
2311 
2312  switch (pbvh->type) {
2313  case PBVH_FACES:
2314  hit |= pbvh_faces_node_raycast(pbvh,
2315  node,
2316  origco,
2317  ray_start,
2318  ray_normal,
2319  isect_precalc,
2320  depth,
2321  active_vertex_index,
2322  active_face_grid_index,
2323  face_normal);
2324  break;
2325  case PBVH_GRIDS:
2326  hit |= pbvh_grids_node_raycast(pbvh,
2327  node,
2328  origco,
2329  ray_start,
2330  ray_normal,
2331  isect_precalc,
2332  depth,
2333  active_vertex_index,
2334  active_face_grid_index,
2335  face_normal);
2336  break;
2337  case PBVH_BMESH:
2340  ray_start,
2341  ray_normal,
2342  isect_precalc,
2343  depth,
2344  use_origco,
2345  active_vertex_index,
2346  face_normal);
2347  break;
2348  }
2349 
2350  return hit;
2351 }
2352 
2354  PBVH *pbvh, bool original, float ray_start[3], float ray_end[3], float ray_normal[3])
2355 {
2356  if (pbvh->nodes) {
2357  float rootmin_start, rootmin_end;
2358  float bb_min_root[3], bb_max_root[3], bb_center[3], bb_diff[3];
2359  struct IsectRayAABB_Precalc ray;
2360  float ray_normal_inv[3];
2361  float offset = 1.0f + 1e-3f;
2362  const float offset_vec[3] = {1e-3f, 1e-3f, 1e-3f};
2363 
2364  if (original) {
2365  BKE_pbvh_node_get_original_BB(pbvh->nodes, bb_min_root, bb_max_root);
2366  }
2367  else {
2368  BKE_pbvh_node_get_BB(pbvh->nodes, bb_min_root, bb_max_root);
2369  }
2370 
2371  /* Slightly offset min and max in case we have a zero width node
2372  * (due to a plane mesh for instance), or faces very close to the bounding box boundary. */
2373  mid_v3_v3v3(bb_center, bb_max_root, bb_min_root);
2374  /* diff should be same for both min/max since it's calculated from center */
2375  sub_v3_v3v3(bb_diff, bb_max_root, bb_center);
2376  /* handles case of zero width bb */
2377  add_v3_v3(bb_diff, offset_vec);
2378  madd_v3_v3v3fl(bb_max_root, bb_center, bb_diff, offset);
2379  madd_v3_v3v3fl(bb_min_root, bb_center, bb_diff, -offset);
2380 
2381  /* first project start ray */
2382  isect_ray_aabb_v3_precalc(&ray, ray_start, ray_normal);
2383  if (!isect_ray_aabb_v3(&ray, bb_min_root, bb_max_root, &rootmin_start)) {
2384  return;
2385  }
2386 
2387  /* then the end ray */
2388  mul_v3_v3fl(ray_normal_inv, ray_normal, -1.0);
2389  isect_ray_aabb_v3_precalc(&ray, ray_end, ray_normal_inv);
2390  /* unlikely to fail exiting if entering succeeded, still keep this here */
2391  if (!isect_ray_aabb_v3(&ray, bb_min_root, bb_max_root, &rootmin_end)) {
2392  return;
2393  }
2394 
2395  madd_v3_v3v3fl(ray_start, ray_start, ray_normal, rootmin_start);
2396  madd_v3_v3v3fl(ray_end, ray_end, ray_normal_inv, rootmin_end);
2397  }
2398 }
2399 
2400 /* -------------------------------------------------------------------- */
2401 
2402 typedef struct {
2403  struct DistRayAABB_Precalc dist_ray_to_aabb_precalc;
2404  bool original;
2406 
2407 static bool nearest_to_ray_aabb_dist_sq(PBVHNode *node, void *data_v)
2408 {
2409  FindNearestRayData *rcd = data_v;
2410  const float *bb_min, *bb_max;
2411 
2412  if (rcd->original) {
2413  /* BKE_pbvh_node_get_original_BB */
2414  bb_min = node->orig_vb.bmin;
2415  bb_max = node->orig_vb.bmax;
2416  }
2417  else {
2418  /* BKE_pbvh_node_get_BB */
2419  bb_min = node->vb.bmin;
2420  bb_max = node->vb.bmax;
2421  }
2422 
2423  float co_dummy[3], depth;
2425  &rcd->dist_ray_to_aabb_precalc, bb_min, bb_max, co_dummy, &depth);
2426  /* Ideally we would skip distances outside the range. */
2427  return depth > 0.0f;
2428 }
2429 
2432  void *data,
2433  const float ray_start[3],
2434  const float ray_normal[3],
2435  bool original)
2436 {
2437  FindNearestRayData ncd;
2438 
2440  ncd.original = original;
2441 
2443 }
2444 
2446  const PBVHNode *node,
2447  float (*origco)[3],
2448  const float ray_start[3],
2449  const float ray_normal[3],
2450  float *depth,
2451  float *dist_sq)
2452 {
2453  const MVert *vert = pbvh->verts;
2454  const MLoop *mloop = pbvh->mloop;
2455  const int *faces = node->prim_indices;
2456  int i, totface = node->totprim;
2457  bool hit = false;
2458 
2459  for (i = 0; i < totface; i++) {
2460  const MLoopTri *lt = &pbvh->looptri[faces[i]];
2461  const int *face_verts = node->face_vert_indices[i];
2462 
2463  if (pbvh->respect_hide && paint_is_face_hidden(lt, vert, mloop)) {
2464  continue;
2465  }
2466 
2467  if (origco) {
2468  /* intersect with backuped original coordinates */
2469  hit |= ray_face_nearest_tri(ray_start,
2470  ray_normal,
2471  origco[face_verts[0]],
2472  origco[face_verts[1]],
2473  origco[face_verts[2]],
2474  depth,
2475  dist_sq);
2476  }
2477  else {
2478  /* intersect with current coordinates */
2479  hit |= ray_face_nearest_tri(ray_start,
2480  ray_normal,
2481  vert[mloop[lt->tri[0]].v].co,
2482  vert[mloop[lt->tri[1]].v].co,
2483  vert[mloop[lt->tri[2]].v].co,
2484  depth,
2485  dist_sq);
2486  }
2487  }
2488 
2489  return hit;
2490 }
2491 
2493  PBVHNode *node,
2494  float (*origco)[3],
2495  const float ray_start[3],
2496  const float ray_normal[3],
2497  float *depth,
2498  float *dist_sq)
2499 {
2500  const int totgrid = node->totprim;
2501  const int gridsize = pbvh->gridkey.grid_size;
2502  bool hit = false;
2503 
2504  for (int i = 0; i < totgrid; i++) {
2505  CCGElem *grid = pbvh->grids[node->prim_indices[i]];
2506  BLI_bitmap *gh;
2507 
2508  if (!grid) {
2509  continue;
2510  }
2511 
2512  gh = pbvh->grid_hidden[node->prim_indices[i]];
2513 
2514  for (int y = 0; y < gridsize - 1; y++) {
2515  for (int x = 0; x < gridsize - 1; x++) {
2516  /* check if grid face is hidden */
2517  if (gh) {
2518  if (paint_is_grid_face_hidden(gh, gridsize, x, y)) {
2519  continue;
2520  }
2521  }
2522 
2523  if (origco) {
2524  hit |= ray_face_nearest_quad(ray_start,
2525  ray_normal,
2526  origco[y * gridsize + x],
2527  origco[y * gridsize + x + 1],
2528  origco[(y + 1) * gridsize + x + 1],
2529  origco[(y + 1) * gridsize + x],
2530  depth,
2531  dist_sq);
2532  }
2533  else {
2534  hit |= ray_face_nearest_quad(ray_start,
2535  ray_normal,
2536  CCG_grid_elem_co(&pbvh->gridkey, grid, x, y),
2537  CCG_grid_elem_co(&pbvh->gridkey, grid, x + 1, y),
2538  CCG_grid_elem_co(&pbvh->gridkey, grid, x + 1, y + 1),
2539  CCG_grid_elem_co(&pbvh->gridkey, grid, x, y + 1),
2540  depth,
2541  dist_sq);
2542  }
2543  }
2544  }
2545 
2546  if (origco) {
2547  origco += gridsize * gridsize;
2548  }
2549  }
2550 
2551  return hit;
2552 }
2553 
2555  PBVHNode *node,
2556  float (*origco)[3],
2557  bool use_origco,
2558  const float ray_start[3],
2559  const float ray_normal[3],
2560  float *depth,
2561  float *dist_sq)
2562 {
2563  bool hit = false;
2564 
2565  if (node->flag & PBVH_FullyHidden) {
2566  return false;
2567  }
2568 
2569  switch (pbvh->type) {
2570  case PBVH_FACES:
2572  pbvh, node, origco, ray_start, ray_normal, depth, dist_sq);
2573  break;
2574  case PBVH_GRIDS:
2576  pbvh, node, origco, ray_start, ray_normal, depth, dist_sq);
2577  break;
2578  case PBVH_BMESH:
2580  node, ray_start, ray_normal, depth, dist_sq, use_origco);
2581  break;
2582  }
2583 
2584  return hit;
2585 }
2586 
2587 typedef enum {
2591 } PlaneAABBIsect;
2592 
2593 /* Adapted from:
2594  * http://www.gamedev.net/community/forums/topic.asp?topic_id=512123
2595  * Returns true if the AABB is at least partially within the frustum
2596  * (ok, not a real frustum), false otherwise.
2597  */
2598 static PlaneAABBIsect test_frustum_aabb(const float bb_min[3],
2599  const float bb_max[3],
2600  PBVHFrustumPlanes *frustum)
2601 {
2603  float(*planes)[4] = frustum->planes;
2604 
2605  for (int i = 0; i < frustum->num_planes; i++) {
2606  float vmin[3], vmax[3];
2607 
2608  for (int axis = 0; axis < 3; axis++) {
2609  if (planes[i][axis] < 0) {
2610  vmin[axis] = bb_min[axis];
2611  vmax[axis] = bb_max[axis];
2612  }
2613  else {
2614  vmin[axis] = bb_max[axis];
2615  vmax[axis] = bb_min[axis];
2616  }
2617  }
2618 
2619  if (dot_v3v3(planes[i], vmin) + planes[i][3] < 0) {
2620  return ISECT_OUTSIDE;
2621  }
2622  if (dot_v3v3(planes[i], vmax) + planes[i][3] <= 0) {
2623  ret = ISECT_INTERSECT;
2624  }
2625  }
2626 
2627  return ret;
2628 }
2629 
2631 {
2632  const float *bb_min, *bb_max;
2633  /* BKE_pbvh_node_get_BB */
2634  bb_min = node->vb.bmin;
2635  bb_max = node->vb.bmax;
2636 
2637  return test_frustum_aabb(bb_min, bb_max, data) != ISECT_OUTSIDE;
2638 }
2639 
2641 {
2642  const float *bb_min, *bb_max;
2643  /* BKE_pbvh_node_get_BB */
2644  bb_min = node->vb.bmin;
2645  bb_max = node->vb.bmax;
2646 
2647  return test_frustum_aabb(bb_min, bb_max, data) != ISECT_INSIDE;
2648 }
2649 
2650 void BKE_pbvh_update_normals(PBVH *pbvh, struct SubdivCCG *subdiv_ccg)
2651 {
2652  /* Update normals */
2653  PBVHNode **nodes;
2654  int totnode;
2655 
2657  pbvh, update_search_cb, POINTER_FROM_INT(PBVH_UpdateNormals), &nodes, &totnode);
2658 
2659  if (totnode > 0) {
2660  if (pbvh->type == PBVH_BMESH) {
2661  pbvh_bmesh_normals_update(nodes, totnode);
2662  }
2663  else if (pbvh->type == PBVH_FACES) {
2664  pbvh_faces_update_normals(pbvh, nodes, totnode);
2665  }
2666  else if (pbvh->type == PBVH_GRIDS) {
2667  struct CCGFace **faces;
2668  int num_faces;
2669  BKE_pbvh_get_grid_updates(pbvh, true, (void ***)&faces, &num_faces);
2670  if (num_faces > 0) {
2671  BKE_subdiv_ccg_update_normals(subdiv_ccg, faces, num_faces);
2672  MEM_freeN(faces);
2673  }
2674  }
2675  }
2676 
2677  MEM_SAFE_FREE(nodes);
2678 }
2679 
2680 void BKE_pbvh_face_sets_color_set(PBVH *pbvh, int seed, int color_default)
2681 {
2682  pbvh->face_sets_color_seed = seed;
2683  pbvh->face_sets_color_default = color_default;
2684 }
2685 
2690 typedef struct PBVHDrawSearchData {
2694 
2695 static bool pbvh_draw_search_cb(PBVHNode *node, void *data_v)
2696 {
2697  PBVHDrawSearchData *data = data_v;
2698  if (data->frustum && !BKE_pbvh_node_frustum_contain_AABB(node, data->frustum)) {
2699  return false;
2700  }
2701 
2702  data->accum_update_flag |= node->flag;
2703  return true;
2704 }
2705 
2707  bool update_only_visible,
2708  PBVHFrustumPlanes *update_frustum,
2709  PBVHFrustumPlanes *draw_frustum,
2710  void (*draw_fn)(void *user_data, GPU_PBVH_Buffers *buffers),
2711  void *user_data)
2712 {
2713  PBVHNode **nodes;
2714  int totnode;
2715  int update_flag = 0;
2716 
2717  /* Search for nodes that need updates. */
2718  if (update_only_visible) {
2719  /* Get visible nodes with draw updates. */
2720  PBVHDrawSearchData data = {.frustum = update_frustum, .accum_update_flag = 0};
2721  BKE_pbvh_search_gather(pbvh, pbvh_draw_search_cb, &data, &nodes, &totnode);
2722  update_flag = data.accum_update_flag;
2723  }
2724  else {
2725  /* Get all nodes with draw updates, also those outside the view. */
2726  const int search_flag = PBVH_RebuildDrawBuffers | PBVH_UpdateDrawBuffers;
2728  pbvh, update_search_cb, POINTER_FROM_INT(search_flag), &nodes, &totnode);
2730  }
2731 
2732  /* Update draw buffers. */
2733  if (totnode != 0 && (update_flag & (PBVH_RebuildDrawBuffers | PBVH_UpdateDrawBuffers))) {
2734  pbvh_update_draw_buffers(pbvh, nodes, totnode, update_flag);
2735  }
2736  MEM_SAFE_FREE(nodes);
2737 
2738  /* Draw visible nodes. */
2739  PBVHDrawSearchData draw_data = {.frustum = draw_frustum, .accum_update_flag = 0};
2740  BKE_pbvh_search_gather(pbvh, pbvh_draw_search_cb, &draw_data, &nodes, &totnode);
2741 
2742  for (int i = 0; i < totnode; i++) {
2743  PBVHNode *node = nodes[i];
2744  if (!(node->flag & PBVH_FullyHidden)) {
2745  draw_fn(user_data, node->draw_buffers);
2746  }
2747  }
2748 
2749  MEM_SAFE_FREE(nodes);
2750 }
2751 
2753  PBVH *pbvh,
2754  void (*draw_fn)(void *user_data, const float bmin[3], const float bmax[3], PBVHNodeFlags flag),
2755  void *user_data)
2756 {
2757  for (int a = 0; a < pbvh->totnode; a++) {
2758  PBVHNode *node = &pbvh->nodes[a];
2759 
2760  draw_fn(user_data, node->vb.bmin, node->vb.bmax, node->flag);
2761  }
2762 }
2763 
2765  PBVH *pbvh, CCGElem **grids, void **gridfaces, DMFlagMat *flagmats, BLI_bitmap **grid_hidden)
2766 {
2767  pbvh->grids = grids;
2768  pbvh->gridfaces = gridfaces;
2769 
2770  if (flagmats != pbvh->grid_flag_mats || pbvh->grid_hidden != grid_hidden) {
2771  pbvh->grid_flag_mats = flagmats;
2772  pbvh->grid_hidden = grid_hidden;
2773 
2774  for (int a = 0; a < pbvh->totnode; a++) {
2776  }
2777  }
2778 }
2779 
2781 {
2782  float(*vertCos)[3] = NULL;
2783 
2784  if (pbvh->verts) {
2785  MVert *mvert = pbvh->verts;
2786 
2787  vertCos = MEM_callocN(3 * pbvh->totvert * sizeof(float), "BKE_pbvh_get_vertCoords");
2788  float *co = (float *)vertCos;
2789 
2790  for (int a = 0; a < pbvh->totvert; a++, mvert++, co += 3) {
2791  copy_v3_v3(co, mvert->co);
2792  }
2793  }
2794 
2795  return vertCos;
2796 }
2797 
2798 void BKE_pbvh_vert_coords_apply(PBVH *pbvh, const float (*vertCos)[3], const int totvert)
2799 {
2800  if (totvert != pbvh->totvert) {
2801  BLI_assert(!"PBVH: Given deforming vcos number does not natch PBVH vertex number!");
2802  return;
2803  }
2804 
2805  if (!pbvh->deformed) {
2806  if (pbvh->verts) {
2807  /* if pbvh is not already deformed, verts/faces points to the */
2808  /* original data and applying new coords to this arrays would lead to */
2809  /* unneeded deformation -- duplicate verts/faces to avoid this */
2810 
2811  pbvh->verts = MEM_dupallocN(pbvh->verts);
2812  /* No need to dupalloc pbvh->looptri, this one is 'totally owned' by pbvh,
2813  * it's never some mesh data. */
2814 
2815  pbvh->deformed = true;
2816  }
2817  }
2818 
2819  if (pbvh->verts) {
2820  MVert *mvert = pbvh->verts;
2821  /* copy new verts coords */
2822  for (int a = 0; a < pbvh->totvert; a++, mvert++) {
2823  /* no need for float comparison here (memory is exactly equal or not) */
2824  if (memcmp(mvert->co, vertCos[a], sizeof(float[3])) != 0) {
2825  copy_v3_v3(mvert->co, vertCos[a]);
2826  mvert->flag |= ME_VERT_PBVH_UPDATE;
2827  }
2828  }
2829 
2830  /* coordinates are new -- normals should also be updated */
2832  pbvh->verts, pbvh->totvert, pbvh->mloop, pbvh->looptri, pbvh->totprim, NULL);
2833 
2834  for (int a = 0; a < pbvh->totnode; a++) {
2836  }
2837 
2839  }
2840 }
2841 
2843 {
2844  return pbvh->deformed;
2845 }
2846 /* Proxies */
2847 
2849 {
2850  int index, totverts;
2851 
2852  index = node->proxy_count;
2853 
2854  node->proxy_count++;
2855 
2856  if (node->proxies) {
2857  node->proxies = MEM_reallocN(node->proxies, node->proxy_count * sizeof(PBVHProxyNode));
2858  }
2859  else {
2860  node->proxies = MEM_mallocN(sizeof(PBVHProxyNode), "PBVHNodeProxy");
2861  }
2862 
2863  BKE_pbvh_node_num_verts(pbvh, node, &totverts, NULL);
2864  node->proxies[index].co = MEM_callocN(sizeof(float[3]) * totverts, "PBVHNodeProxy.co");
2865 
2866  return node->proxies + index;
2867 }
2868 
2870 {
2871  for (int p = 0; p < node->proxy_count; p++) {
2872  MEM_freeN(node->proxies[p].co);
2873  node->proxies[p].co = NULL;
2874  }
2875 
2876  MEM_freeN(node->proxies);
2877  node->proxies = NULL;
2878 
2879  node->proxy_count = 0;
2880 }
2881 
2882 void BKE_pbvh_gather_proxies(PBVH *pbvh, PBVHNode ***r_array, int *r_tot)
2883 {
2884  PBVHNode **array = NULL;
2885  int tot = 0, space = 0;
2886 
2887  for (int n = 0; n < pbvh->totnode; n++) {
2888  PBVHNode *node = pbvh->nodes + n;
2889 
2890  if (node->proxy_count > 0) {
2891  if (tot == space) {
2892  /* resize array if needed */
2893  space = (tot == 0) ? 32 : space * 2;
2894  array = MEM_recallocN_id(array, sizeof(PBVHNode *) * space, __func__);
2895  }
2896 
2897  array[tot] = node;
2898  tot++;
2899  }
2900  }
2901 
2902  if (tot == 0 && array) {
2903  MEM_freeN(array);
2904  array = NULL;
2905  }
2906 
2907  *r_array = array;
2908  *r_tot = tot;
2909 }
2910 
2912 {
2913 
2914  if (!node->color_buffer.color) {
2915  node->color_buffer.color = MEM_callocN(sizeof(float[4]) * node->uniq_verts, "Color buffer");
2916  }
2917  return &node->color_buffer;
2918 }
2919 
2921 {
2922  PBVHNode **nodes;
2923  int totnode;
2924  BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
2925  for (int i = 0; i < totnode; i++) {
2926  MEM_SAFE_FREE(nodes[i]->color_buffer.color);
2927  }
2928  MEM_SAFE_FREE(nodes);
2929 }
2930 
2932 {
2933  struct CCGElem **grids;
2934  struct MVert *verts;
2935  const int *vert_indices;
2936  int *grid_indices;
2937  int totgrid, gridsize, uniq_verts, totvert;
2938 
2939  vi->grid = NULL;
2940  vi->no = NULL;
2941  vi->fno = NULL;
2942  vi->mvert = NULL;
2943 
2944  vi->respect_hide = pbvh->respect_hide;
2945  if (pbvh->respect_hide == false) {
2946  /* The same value for all vertices. */
2947  vi->visible = true;
2948  }
2949 
2950  BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid, NULL, &gridsize, &grids);
2951  BKE_pbvh_node_num_verts(pbvh, node, &uniq_verts, &totvert);
2952  BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &verts);
2953  vi->key = pbvh->gridkey;
2954 
2955  vi->grids = grids;
2956  vi->grid_indices = grid_indices;
2957  vi->totgrid = (grids) ? totgrid : 1;
2958  vi->gridsize = gridsize;
2959 
2960  if (mode == PBVH_ITER_ALL) {
2961  vi->totvert = totvert;
2962  }
2963  else {
2964  vi->totvert = uniq_verts;
2965  }
2966  vi->vert_indices = vert_indices;
2967  vi->mverts = verts;
2968 
2969  if (pbvh->type == PBVH_BMESH) {
2970  BLI_gsetIterator_init(&vi->bm_unique_verts, node->bm_unique_verts);
2971  BLI_gsetIterator_init(&vi->bm_other_verts, node->bm_other_verts);
2972  vi->bm_vdata = &pbvh->bm->vdata;
2974  }
2975 
2976  vi->gh = NULL;
2977  if (vi->grids && mode == PBVH_ITER_UNIQUE) {
2978  vi->grid_hidden = pbvh->grid_hidden;
2979  }
2980 
2981  vi->mask = NULL;
2982  if (pbvh->type == PBVH_FACES) {
2985  }
2986 }
2987 
2988 bool pbvh_has_mask(PBVH *pbvh)
2989 {
2990  switch (pbvh->type) {
2991  case PBVH_GRIDS:
2992  return (pbvh->gridkey.has_mask != 0);
2993  case PBVH_FACES:
2994  return (pbvh->vdata && CustomData_get_layer(pbvh->vdata, CD_PAINT_MASK));
2995  case PBVH_BMESH:
2996  return (pbvh->bm && (CustomData_get_offset(&pbvh->bm->vdata, CD_PAINT_MASK) != -1));
2997  }
2998 
2999  return false;
3000 }
3001 
3003 {
3004  switch (pbvh->type) {
3005  case PBVH_GRIDS:
3006  return (pbvh->pdata && CustomData_get_layer(pbvh->pdata, CD_SCULPT_FACE_SETS));
3007  case PBVH_FACES:
3008  return (pbvh->pdata && CustomData_get_layer(pbvh->pdata, CD_SCULPT_FACE_SETS));
3009  case PBVH_BMESH:
3010  return false;
3011  }
3012 
3013  return false;
3014 }
3015 
3016 void pbvh_show_mask_set(PBVH *pbvh, bool show_mask)
3017 {
3018  pbvh->show_mask = show_mask;
3019 }
3020 
3021 void pbvh_show_face_sets_set(PBVH *pbvh, bool show_face_sets)
3022 {
3023  pbvh->show_face_sets = show_face_sets;
3024 }
3025 
3027 {
3028  pbvh->num_planes = planes->num_planes;
3029  for (int i = 0; i < pbvh->num_planes; i++) {
3030  copy_v4_v4(pbvh->planes[i], planes->planes[i]);
3031  }
3032 }
3033 
3035 {
3036  planes->num_planes = pbvh->num_planes;
3037  for (int i = 0; i < planes->num_planes; i++) {
3038  copy_v4_v4(planes->planes[i], pbvh->planes[i]);
3039  }
3040 }
3041 
3043  bool use_threading,
3044  int totnode)
3045 {
3046  memset(settings, 0, sizeof(*settings));
3047  settings->use_threading = use_threading && totnode > 1;
3048 }
3049 
3051 {
3052  BLI_assert(pbvh->type == PBVH_FACES);
3053  return pbvh->verts;
3054 }
3055 
3056 void BKE_pbvh_subdiv_cgg_set(PBVH *pbvh, SubdivCCG *subdiv_ccg)
3057 {
3058  pbvh->subdiv_ccg = subdiv_ccg;
3059 }
3060 
3061 void BKE_pbvh_face_sets_set(PBVH *pbvh, int *face_sets)
3062 {
3063  pbvh->face_sets = face_sets;
3064 }
3065 
3066 void BKE_pbvh_respect_hide_set(PBVH *pbvh, bool respect_hide)
3067 {
3068  pbvh->respect_hide = respect_hide;
3069 }
typedef float(TangentPoint)[2]
BLI_INLINE float * CCG_grid_elem_co(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:130
struct CCGElem CCGElem
Definition: BKE_ccg.h:46
BLI_INLINE float * CCG_elem_offset_co(const CCGKey *key, CCGElem *elem, int offset)
Definition: BKE_ccg.h:145
void * CustomData_get_layer(const struct CustomData *data, int type)
int CustomData_get_offset(const struct CustomData *data, int type)
void BKE_mesh_calc_normals_looptri(struct MVert *mverts, int numVerts, const struct MLoop *mloop, const struct MLoopTri *looptri, int looptri_num, float(*r_tri_nors)[3])
void BKE_mesh_calc_poly_normal(const struct MPoly *mpoly, const struct MLoop *loopstart, const struct MVert *mvarray, float r_no[3])
bool paint_is_face_hidden(const struct MLoopTri *lt, const struct MVert *mvert, const struct MLoop *mloop)
bool paint_is_grid_face_hidden(const unsigned int *grid_hidden, int gridsize, int x, int y)
Definition: paint.c:1247
A BVH for high poly meshes.
#define BKE_pbvh_vertex_iter_begin(pbvh, node, vi, mode)
Definition: BKE_pbvh.h:384
#define PBVH_ITER_ALL
Definition: BKE_pbvh.h:334
void(* BKE_pbvh_HitCallback)(PBVHNode *node, void *data)
Definition: BKE_pbvh.h:96
#define BKE_pbvh_vertex_iter_end
Definition: BKE_pbvh.h:457
#define PBVH_ITER_UNIQUE
Definition: BKE_pbvh.h:335
struct GSet * BKE_pbvh_bmesh_node_other_verts(PBVHNode *node)
Definition: pbvh_bmesh.c:2126
PBVHType
Definition: BKE_pbvh.h:209
@ PBVH_GRIDS
Definition: BKE_pbvh.h:211
@ PBVH_BMESH
Definition: BKE_pbvh.h:212
@ PBVH_FACES
Definition: BKE_pbvh.h:210
void(* BKE_pbvh_HitOccludedCallback)(PBVHNode *node, void *data, float *tmin)
Definition: BKE_pbvh.h:97
struct GSet * BKE_pbvh_bmesh_node_unique_verts(PBVHNode *node)
Definition: pbvh_bmesh.c:2121
void(* BKE_pbvh_SearchNearestCallback)(PBVHNode *node, void *data, float *tmin)
Definition: BKE_pbvh.h:99
PBVHNodeFlags
Definition: BKE_pbvh.h:63
@ PBVH_FullyMasked
Definition: BKE_pbvh.h:76
@ PBVH_UpdateDrawBuffers
Definition: BKE_pbvh.h:69
@ PBVH_RebuildDrawBuffers
Definition: BKE_pbvh.h:74
@ PBVH_UpdateVisibility
Definition: BKE_pbvh.h:72
@ PBVH_UpdateMask
Definition: BKE_pbvh.h:71
@ PBVH_UpdateColor
Definition: BKE_pbvh.h:80
@ PBVH_UpdateNormals
Definition: BKE_pbvh.h:66
@ PBVH_FullyHidden
Definition: BKE_pbvh.h:75
@ PBVH_UpdateBB
Definition: BKE_pbvh.h:67
@ PBVH_Leaf
Definition: BKE_pbvh.h:64
@ PBVH_UpdateOriginalBB
Definition: BKE_pbvh.h:68
@ PBVH_UpdateRedraw
Definition: BKE_pbvh.h:70
@ PBVH_FullyUnmasked
Definition: BKE_pbvh.h:77
bool(* BKE_pbvh_SearchCallback)(PBVHNode *node, void *data)
Definition: BKE_pbvh.h:94
void BKE_subdiv_ccg_update_normals(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:879
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *subdiv_ccg, const int grid_index)
Definition: subdiv_ccg.c:1832
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:78
#define BLI_BITMAP_NEW(_tot, _alloc_string)
Definition: BLI_bitmap.h:50
#define BLI_BITMAP_SET(_bitmap, _index, _set)
Definition: BLI_bitmap.h:93
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:32
#define GSET_ITER_INDEX(gs_iter_, gset_, i_)
Definition: BLI_ghash.h:272
struct GSet GSet
Definition: BLI_ghash.h:189
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:146
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:150
unsigned int BLI_gset_len(GSet *gs) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1138
#define GHASH_ITER(gh_iter_, ghash_)
Definition: BLI_ghash.h:169
GSet * BLI_gset_ptr_new(const char *info)
GHash * BLI_ghash_int_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
#define GSET_ITER(gs_iter_, gset_)
Definition: BLI_ghash.h:268
BLI_INLINE void BLI_gsetIterator_init(GSetIterator *gsi, GSet *gs)
Definition: BLI_ghash.h:247
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1253
BLI_INLINE void * BLI_gsetIterator_getKey(GSetIterator *gsi)
Definition: BLI_ghash.h:255
bool BLI_ghash_ensure_p(GHash *gh, void *key, void ***r_val) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:851
bool BLI_gset_add(GSet *gs, void *key)
Definition: BLI_ghash.c:1160
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
MINLINE float max_ff(float a, float b)
MINLINE float min_ff(float a, float b)
MINLINE int max_ii(int a, int b)
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition: math_geom.c:68
bool isect_ray_aabb_v3(const struct IsectRayAABB_Precalc *data, const float bb_min[3], const float bb_max[3], float *tmin)
Definition: math_geom.c:3251
void dist_squared_ray_to_aabb_v3_precalc(struct DistRayAABB_Precalc *neasrest_precalc, const float ray_origin[3], const float ray_direction[3])
Definition: math_geom.c:696
bool isect_ray_tri_watertight_v3(const float ray_origin[3], const struct IsectRayPrecalc *isect_precalc, const float v0[3], const float v1[3], const float v2[3], float *r_dist, float r_uv[2])
Definition: math_geom.c:1906
void isect_ray_aabb_v3_precalc(struct IsectRayAABB_Precalc *data, const float ray_origin[3], const float ray_direction[3])
Definition: math_geom.c:3235
float dist_squared_ray_to_seg_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float r_point[3], float *r_depth)
Definition: math_geom.c:622
float dist_squared_ray_to_aabb_v3(const struct DistRayAABB_Precalc *data, const float bb_min[3], const float bb_max[3], float r_point[3], float *r_depth)
Definition: math_geom.c:713
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:51
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void normal_float_to_short_v3(short r[3], const float n[3])
MINLINE float normalize_v3(float r[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
Definition: math_vector.c:270
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
Random number functions.
unsigned int uint
Definition: BLI_sys_types.h:83
void BLI_task_parallel_range(const int start, const int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition: task_range.cc:110
#define SWAP(type, a, b)
#define POINTER_FROM_INT(i)
#define UNUSED(x)
#define POINTER_AS_INT(i)
#define UNLIKELY(x)
#define ELEM(...)
@ CD_PAINT_MASK
@ CD_PROP_COLOR
@ CD_MLOOPCOL
@ CD_SCULPT_FACE_SETS
@ ME_HIDE
@ ME_VERT_PBVH_UPDATE
@ ME_SMOOTH
@ GPU_PBVH_BUFFERS_SHOW_MASK
Definition: GPU_buffers.h:73
@ GPU_PBVH_BUFFERS_SHOW_VCOL
Definition: GPU_buffers.h:74
@ GPU_PBVH_BUFFERS_SHOW_SCULPT_FACE_SETS
Definition: GPU_buffers.h:75
void GPU_pbvh_buffers_free(GPU_PBVH_Buffers *buffers)
Definition: gpu_buffers.c:1110
void GPU_pbvh_bmesh_buffers_update(GPU_PBVH_Buffers *buffers, struct BMesh *bm, struct GSet *bm_faces, struct GSet *bm_unique_verts, struct GSet *bm_other_verts, const int update_flags)
Definition: gpu_buffers.c:884
void GPU_pbvh_bmesh_buffers_update_free(GPU_PBVH_Buffers *buffers)
Definition: gpu_buffers.c:866
GPU_PBVH_Buffers * GPU_pbvh_mesh_buffers_build(const struct MPoly *mpoly, const struct MLoop *mloop, const struct MLoopTri *looptri, const struct MVert *mvert, const int *face_indices, const int *sculpt_face_sets, const int face_indices_len, const struct Mesh *mesh)
void GPU_pbvh_buffers_update_flush(GPU_PBVH_Buffers *buffers)
Definition: gpu_buffers.c:1096
void GPU_pbvh_mesh_buffers_update(GPU_PBVH_Buffers *buffers, const struct MVert *mvert, const float *vmask, const struct MLoopCol *vcol, const int *sculpt_face_sets, const int face_sets_color_seed, const int face_sets_color_default, const struct MPropCol *vtcol, const int update_flags)
GPU_PBVH_Buffers * GPU_pbvh_bmesh_buffers_build(bool smooth_shading)
Definition: gpu_buffers.c:1052
void GPU_pbvh_grid_buffers_update_free(GPU_PBVH_Buffers *buffers, const struct DMFlagMat *grid_flag_mats, const int *grid_indices)
Definition: gpu_buffers.c:567
GPU_PBVH_Buffers * GPU_pbvh_grid_buffers_build(int totgrid, unsigned int **grid_hidden)
Definition: gpu_buffers.c:768
void GPU_pbvh_grid_buffers_update(GPU_PBVH_Buffers *buffers, struct SubdivCCG *subdiv_ccg, struct CCGElem **grids, const struct DMFlagMat *grid_flag_mats, int *grid_indices, int totgrid, const int *sculpt_face_sets, const int face_sets_color_seed, const int face_sets_color_default, const struct CCGKey *key, const int update_flags)
Definition: gpu_buffers.c:588
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
Read Guarded memory(de)allocation.
#define MEM_recallocN(vmemh, len)
#define MEM_SAFE_FREE(v)
#define MEM_reallocN(vmemh, len)
Platform independent time functions.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE float atomic_add_and_fetch_fl(float *p, const float x)
@ BM_VERT
Definition: bmesh_class.h:383
@ BM_ELEM_HIDDEN
Definition: bmesh_class.h:472
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:26
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2152
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
static unsigned long seed
Definition: btSoftBody.h:39
OperationNode * node
void * user_data
void * tree
static ushort indices[]
static float verts[][3]
#define UINT_MAX
Definition: hash_md5.c:58
int count
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_recallocN_id)(void *vmemh, size_t len, const char *str)
Definition: mallocn.c:44
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static char faces[256]
static void clear(Message *msg)
Definition: msgfmt.c:294
static unsigned a[3]
Definition: RandGen.cpp:92
static void update(bNodeTree *ntree)
static PlaneAABBIsect test_frustum_aabb(const float bb_min[3], const float bb_max[3], PBVHFrustumPlanes *frustum)
Definition: pbvh.c:2598
bool BKE_pbvh_node_fully_masked_get(PBVHNode *node)
Definition: pbvh.c:1798
static float dist_squared_ray_to_tri_v3_fast(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float r_point[3], float *r_depth)
Definition: pbvh.c:2061
static void pbvh_build(PBVH *pbvh, BB *cb, BBC *prim_bbc, int totprim)
Definition: pbvh.c:533
bool ray_face_nearest_tri(const float ray_start[3], const float ray_normal[3], const float t0[3], const float t1[3], const float t2[3], float *depth, float *dist_sq)
Definition: pbvh.c:2111
int BKE_pbvh_get_grid_num_faces(const PBVH *pbvh)
Definition: pbvh.c:1718
void BKE_pbvh_node_free_proxies(PBVHNode *node)
Definition: pbvh.c:2869
void BKE_pbvh_set_frustum_planes(PBVH *pbvh, PBVHFrustumPlanes *planes)
Definition: pbvh.c:3026
static void pbvh_iter_end(PBVHIter *iter)
Definition: pbvh.c:746
static void build_mesh_leaf_node(PBVH *pbvh, PBVHNode *node)
Definition: pbvh.c:279
void BKE_pbvh_node_mark_update(PBVHNode *node)
Definition: pbvh.c:1732
BLI_bitmap ** BKE_pbvh_get_grid_visibility(const PBVH *pbvh)
Definition: pbvh.c:1706
void BKE_pbvh_node_get_bm_orco_data(PBVHNode *node, int(**r_orco_tris)[3], int *r_orco_tris_num, float(**r_orco_coords)[3])
Definition: pbvh.c:1947
void BKE_pbvh_build_grids(PBVH *pbvh, CCGElem **grids, int totgrid, CCGKey *key, void **gridfaces, DMFlagMat *flagmats, BLI_bitmap **grid_hidden)
Definition: pbvh.c:625
static void node_tree_insert(node_tree *tree, node_tree *new_node)
Definition: pbvh.c:903
void BKE_pbvh_sync_face_sets_to_grids(PBVH *pbvh)
Definition: pbvh.c:387
static bool grid_materials_match(const DMFlagMat *f1, const DMFlagMat *f2)
Definition: pbvh.c:168
void BKE_pbvh_node_fully_masked_set(PBVHNode *node, int fully_masked)
Definition: pbvh.c:1786
static int partition_indices(int *prim_indices, int lo, int hi, int axis, float mid, BBC *prim_bbc)
Definition: pbvh.c:175
struct PBVHUpdateData PBVHUpdateData
void BKE_pbvh_node_get_original_BB(PBVHNode *node, float bb_min[3], float bb_max[3])
Definition: pbvh.c:1921
void BKE_pbvh_subdiv_cgg_set(PBVH *pbvh, SubdivCCG *subdiv_ccg)
Definition: pbvh.c:3056
void BKE_pbvh_node_mark_update_visibility(PBVHNode *node)
Definition: pbvh.c:1748
struct CCGElem ** BKE_pbvh_get_grids(const PBVH *pbvh)
Definition: pbvh.c:1700
void BKE_pbvh_node_mark_update_color(PBVHNode *node)
Definition: pbvh.c:1743
static void BKE_pbvh_search_callback_occluded(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, BKE_pbvh_HitOccludedCallback hcb, void *hit_data)
Definition: pbvh.c:959
void BKE_pbvh_vert_coords_apply(PBVH *pbvh, const float(*vertCos)[3], const int totvert)
Definition: pbvh.c:2798
bool pbvh_has_mask(PBVH *pbvh)
Definition: pbvh.c:2988
void BKE_pbvh_get_frustum_planes(PBVH *pbvh, PBVHFrustumPlanes *planes)
Definition: pbvh.c:3034
static PBVHNode * pbvh_iter_next(PBVHIter *iter)
Definition: pbvh.c:771
void BKE_pbvh_raycast_project_ray_root(PBVH *pbvh, bool original, float ray_start[3], float ray_end[3], float ray_normal[3])
Definition: pbvh.c:2353
void BKE_pbvh_gather_proxies(PBVH *pbvh, PBVHNode ***r_array, int *r_tot)
Definition: pbvh.c:2882
void BKE_pbvh_redraw_BB(PBVH *pbvh, float bb_min[3], float bb_max[3])
Definition: pbvh.c:1592
void BKE_pbvh_node_num_verts(PBVH *pbvh, PBVHNode *node, int *r_uniquevert, int *r_totvert)
Definition: pbvh.c:1834
void BB_expand_with_bb(BB *bb, BB *bb2)
Definition: pbvh.c:91
static void pbvh_update_draw_buffer_cb(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: pbvh.c:1261
struct node_tree node_tree
void BKE_pbvh_node_fully_unmasked_set(PBVHNode *node, int fully_masked)
Definition: pbvh.c:1803
void BKE_pbvh_update_bounds(PBVH *pbvh, int flag)
Definition: pbvh.c:1410
PlaneAABBIsect
Definition: pbvh.c:2587
@ ISECT_INTERSECT
Definition: pbvh.c:2590
@ ISECT_INSIDE
Definition: pbvh.c:2588
@ ISECT_OUTSIDE
Definition: pbvh.c:2589
void BKE_pbvh_draw_debug_cb(PBVH *pbvh, void(*draw_fn)(void *user_data, const float bmin[3], const float bmax[3], PBVHNodeFlags flag), void *user_data)
Definition: pbvh.c:2752
static void update_vb(PBVH *pbvh, PBVHNode *node, BBC *prim_bbc, int offset, int count)
Definition: pbvh.c:345
static void build_sub(PBVH *pbvh, int node_index, BB *cb, BBC *prim_bbc, int offset, int count)
Definition: pbvh.c:478
static void pbvh_faces_update_normals(PBVH *pbvh, PBVHNode **nodes, int totnode)
Definition: pbvh.c:1099
void BKE_pbvh_update_visibility(PBVH *pbvh)
Definition: pbvh.c:1574
static void pbvh_iter_begin(PBVHIter *iter, PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data)
Definition: pbvh.c:729
void BKE_pbvh_get_grid_updates(PBVH *pbvh, bool clear, void ***r_gridfaces, int *r_totface)
Definition: pbvh.c:1614
MVert * BKE_pbvh_get_verts(const PBVH *pbvh)
Definition: pbvh.c:3050
void pbvh_show_mask_set(PBVH *pbvh, bool show_mask)
Definition: pbvh.c:3016
static int pbvh_flush_bb(PBVH *pbvh, PBVHNode *node, int flag)
Definition: pbvh.c:1378
static bool ray_aabb_intersect(PBVHNode *node, void *data_v)
Definition: pbvh.c:1987
static int pbvh_get_buffers_update_flags(PBVH *UNUSED(pbvh))
Definition: pbvh.c:1254
static bool nearest_to_ray_aabb_dist_sq(PBVHNode *node, void *data_v)
Definition: pbvh.c:2407
void BKE_pbvh_free(PBVH *pbvh)
Definition: pbvh.c:679
int BB_widest_axis(const BB *bb)
Definition: pbvh.c:100
void BKE_pbvh_node_get_BB(PBVHNode *node, float bb_min[3], float bb_max[3])
Definition: pbvh.c:1915
static bool leaf_needs_material_split(PBVH *pbvh, int offset, int count)
Definition: pbvh.c:435
void BKE_pbvh_node_color_buffer_free(PBVH *pbvh)
Definition: pbvh.c:2920
static bool pbvh_grids_node_nearest_to_ray(PBVH *pbvh, PBVHNode *node, float(*origco)[3], const float ray_start[3], const float ray_normal[3], float *depth, float *dist_sq)
Definition: pbvh.c:2492
PBVHType BKE_pbvh_type(const PBVH *pbvh)
Definition: pbvh.c:1661
static void pbvh_update_visibility_redraw_task_cb(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: pbvh.c:1179
void pbvh_show_face_sets_set(PBVH *pbvh, bool show_face_sets)
Definition: pbvh.c:3021
bool ray_face_nearest_quad(const float ray_start[3], const float ray_normal[3], const float t0[3], const float t1[3], const float t2[3], const float t3[3], float *depth, float *dist_sq)
Definition: pbvh.c:2084
static bool pbvh_grids_node_raycast(PBVH *pbvh, PBVHNode *node, float(*origco)[3], const float ray_start[3], const float ray_normal[3], struct IsectRayPrecalc *isect_precalc, float *depth, int *r_active_vertex_index, int *r_active_grid_index, float *r_face_normal)
Definition: pbvh.c:2200
float BKE_pbvh_node_get_tmin(PBVHNode *node)
Definition: pbvh.c:954
static bool pbvh_draw_search_cb(PBVHNode *node, void *data_v)
Definition: pbvh.c:2695
void pbvh_grow_nodes(PBVH *pbvh, int totnode)
Definition: pbvh.c:239
void pbvh_update_BB_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
Definition: pbvh.c:1240
static void pbvh_update_visibility(PBVH *pbvh, PBVHNode **nodes, int totnode)
Definition: pbvh.c:1562
bool ray_face_intersection_tri(const float ray_start[3], struct IsectRayPrecalc *isect_precalc, const float t0[3], const float t1[3], const float t2[3], float *depth)
Definition: pbvh.c:2042
void BKE_pbvh_node_mark_rebuild_draw(PBVHNode *node)
Definition: pbvh.c:1754
static void pbvh_update_visibility_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
Definition: pbvh.c:1203
struct PBVHIter PBVHIter
bool BKE_pbvh_node_raycast(PBVH *pbvh, PBVHNode *node, float(*origco)[3], bool use_origco, const float ray_start[3], const float ray_normal[3], struct IsectRayPrecalc *isect_precalc, float *depth, int *active_vertex_index, int *active_face_grid_index, float *face_normal)
Definition: pbvh.c:2294
void BKE_pbvh_node_mark_normals_update(PBVHNode *node)
Definition: pbvh.c:1764
void BKE_pbvh_face_sets_color_set(PBVH *pbvh, int seed, int color_default)
Definition: pbvh.c:2680
void BKE_pbvh_raycast(PBVH *pbvh, BKE_pbvh_HitOccludedCallback cb, void *data, const float ray_start[3], const float ray_normal[3], bool original)
Definition: pbvh.c:2006
void BKE_pbvh_update_vertex_data(PBVH *pbvh, int flag)
Definition: pbvh.c:1432
void BKE_pbvh_build_mesh(PBVH *pbvh, const Mesh *mesh, const MPoly *mpoly, const MLoop *mloop, MVert *verts, int totvert, struct CustomData *vdata, struct CustomData *ldata, struct CustomData *pdata, const MLoopTri *looptri, int looptri_num)
Definition: pbvh.c:564
bool ray_face_intersection_quad(const float ray_start[3], struct IsectRayPrecalc *isect_precalc, const float t0[3], const float t1[3], const float t2[3], const float t3[3], float *depth)
Definition: pbvh.c:2021
void BKE_pbvh_draw_cb(PBVH *pbvh, bool update_only_visible, PBVHFrustumPlanes *update_frustum, PBVHFrustumPlanes *draw_frustum, void(*draw_fn)(void *user_data, GPU_PBVH_Buffers *buffers), void *user_data)
Definition: pbvh.c:2706
void BKE_pbvh_search_gather(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, PBVHNode ***r_array, int *r_tot)
Definition: pbvh.c:843
static void pbvh_update_visibility_task_cb(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: pbvh.c:1538
static void pbvh_bmesh_node_visibility_update(PBVHNode *node)
Definition: pbvh.c:1510
struct PBVHDrawSearchData PBVHDrawSearchData
static bool pbvh_faces_node_nearest_to_ray(PBVH *pbvh, const PBVHNode *node, float(*origco)[3], const float ray_start[3], const float ray_normal[3], float *depth, float *dist_sq)
Definition: pbvh.c:2445
static void pbvh_update_normals_accum_task_cb(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: pbvh.c:1019
bool BKE_pbvh_is_deformed(PBVH *pbvh)
Definition: pbvh.c:2842
int BKE_pbvh_get_grid_num_vertices(const PBVH *pbvh)
Definition: pbvh.c:1712
int BKE_pbvh_count_grid_quads(BLI_bitmap **grid_hidden, const int *grid_indices, int totgrid, int gridsize)
Definition: pbvh.c:355
void BKE_pbvh_update_normals(PBVH *pbvh, struct SubdivCCG *subdiv_ccg)
Definition: pbvh.c:2650
bool BKE_pbvh_node_find_nearest_to_ray(PBVH *pbvh, PBVHNode *node, float(*origco)[3], bool use_origco, const float ray_start[3], const float ray_normal[3], float *depth, float *dist_sq)
Definition: pbvh.c:2554
static void pbvh_update_mask_redraw_task_cb(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: pbvh.c:1130
static bool face_materials_match(const MPoly *f1, const MPoly *f2)
Definition: pbvh.c:163
PBVHProxyNode * BKE_pbvh_node_add_proxy(PBVH *pbvh, PBVHNode *node)
Definition: pbvh.c:2848
static void build_leaf(PBVH *pbvh, int node_index, BBC *prim_bbc, int offset, int count)
Definition: pbvh.c:415
bool BKE_pbvh_node_vert_update_check_any(PBVH *pbvh, PBVHNode *node)
Definition: pbvh.c:1962
static bool update_search_cb(PBVHNode *node, void *data_v)
Definition: pbvh.c:998
static void pbvh_stack_push(PBVHIter *iter, PBVHNode *node, bool revisiting)
Definition: pbvh.c:753
PBVH * BKE_pbvh_new(void)
Definition: pbvh.c:672
static bool pbvh_faces_node_raycast(PBVH *pbvh, const PBVHNode *node, float(*origco)[3], const float ray_start[3], const float ray_normal[3], struct IsectRayPrecalc *isect_precalc, float *depth, int *r_active_vertex_index, int *r_active_face_index, float *r_face_normal)
Definition: pbvh.c:2132
bool BKE_pbvh_has_faces(const PBVH *pbvh)
Definition: pbvh.c:1666
bool BKE_pbvh_node_fully_hidden_get(PBVHNode *node)
Definition: pbvh.c:1781
static void build_grid_leaf_node(PBVH *pbvh, PBVHNode *node)
Definition: pbvh.c:407
void BKE_pbvh_node_fully_hidden_set(PBVHNode *node, int fully_hidden)
Definition: pbvh.c:1769
void BKE_pbvh_grids_update(PBVH *pbvh, CCGElem **grids, void **gridfaces, DMFlagMat *flagmats, BLI_bitmap **grid_hidden)
Definition: pbvh.c:2764
bool BKE_pbvh_node_frustum_contain_AABB(PBVHNode *node, void *data)
Definition: pbvh.c:2630
static void pbvh_update_normals_store_task_cb(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: pbvh.c:1069
PBVHColorBufferNode * BKE_pbvh_node_color_buffer_get(PBVHNode *node)
Definition: pbvh.c:2911
static void pbvh_grids_node_visibility_update(PBVH *pbvh, PBVHNode *node)
Definition: pbvh.c:1479
const CCGKey * BKE_pbvh_get_grid_key(const PBVH *pbvh)
Definition: pbvh.c:1694
static void pbvh_faces_node_visibility_update(PBVH *pbvh, PBVHNode *node)
Definition: pbvh.c:1460
bool BKE_pbvh_node_frustum_exclude_AABB(PBVHNode *node, void *data)
Definition: pbvh.c:2640
void BKE_pbvh_respect_hide_set(PBVH *pbvh, bool respect_hide)
Definition: pbvh.c:3066
void BKE_pbvh_node_mark_update_mask(PBVHNode *node)
Definition: pbvh.c:1738
void BBC_update_centroid(BBC *bbc)
Definition: pbvh.c:123
void BKE_pbvh_node_get_verts(PBVH *pbvh, PBVHNode *node, const int **r_vert_indices, MVert **r_verts)
Definition: pbvh.c:1820
static int map_insert_vert(PBVH *pbvh, GHash *map, unsigned int *face_verts, unsigned int *uniq_verts, int vertex)
Definition: pbvh.c:254
bool BKE_pbvh_node_fully_unmasked_get(PBVHNode *node)
Definition: pbvh.c:1815
BMesh * BKE_pbvh_get_bmesh(PBVH *pbvh)
Definition: pbvh.c:1724
static void pbvh_update_BB_redraw_task_cb(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: pbvh.c:1216
BLI_bitmap ** BKE_pbvh_grid_hidden(const PBVH *pbvh)
Definition: pbvh.c:1688
static int partition_indices_material(PBVH *pbvh, int lo, int hi)
Definition: pbvh.c:196
static void update_node_vb(PBVH *pbvh, PBVHNode *node)
Definition: pbvh.c:131
void BKE_pbvh_bounding_box(const PBVH *pbvh, float min[3], float max[3])
Definition: pbvh.c:1675
void BKE_pbvh_search_callback(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, BKE_pbvh_HitCallback hcb, void *hit_data)
Definition: pbvh.c:876
void BB_reset(BB *bb)
Definition: pbvh.c:75
void BKE_pbvh_node_mark_redraw(PBVHNode *node)
Definition: pbvh.c:1759
#define STACK_FIXED_DEPTH
Definition: pbvh.c:56
static PBVHNode * pbvh_iter_next_occluded(PBVHIter *iter)
Definition: pbvh.c:814
static void traverse_tree(node_tree *tree, BKE_pbvh_HitOccludedCallback hcb, void *hit_data, float *tmin)
Definition: pbvh.c:923
void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int mode)
Definition: pbvh.c:2931
void BKE_pbvh_node_get_grids(PBVH *pbvh, PBVHNode *node, int **r_grid_indices, int *r_totgrid, int *r_maxgrid, int *r_gridsize, CCGElem ***r_griddata)
Definition: pbvh.c:1868
struct PBVHStack PBVHStack
static void pbvh_update_mask_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
Definition: pbvh.c:1166
void BKE_pbvh_face_sets_set(PBVH *pbvh, int *face_sets)
Definition: pbvh.c:3061
void BKE_pbvh_node_get_proxies(PBVHNode *node, PBVHProxyNode **proxies, int *proxy_count)
Definition: pbvh.c:1927
void BKE_pbvh_find_nearest_to_ray(PBVH *pbvh, BKE_pbvh_SearchNearestCallback cb, void *data, const float ray_start[3], const float ray_normal[3], bool original)
Definition: pbvh.c:2430
static void free_tree(node_tree *tree)
Definition: pbvh.c:939
float(* BKE_pbvh_vert_coords_alloc(PBVH *pbvh))[3]
Definition: pbvh.c:2780
#define LEAF_LIMIT
Definition: pbvh.c:52
void BB_expand(BB *bb, const float co[3])
Definition: pbvh.c:82
static void pbvh_update_draw_buffers(PBVH *pbvh, PBVHNode **nodes, int totnode, int update_flag)
Definition: pbvh.c:1334
void BKE_pbvh_parallel_range_settings(TaskParallelSettings *settings, bool use_threading, int totnode)
Definition: pbvh.c:3042
bool pbvh_has_face_sets(PBVH *pbvh)
Definition: pbvh.c:3002
bool pbvh_bmesh_node_nearest_to_ray(PBVHNode *node, const float ray_start[3], const float ray_normal[3], float *depth, float *dist_sq, bool use_original)
Definition: pbvh_bmesh.c:1618
bool pbvh_bmesh_node_raycast(PBVHNode *node, const float ray_start[3], const float ray_normal[3], struct IsectRayPrecalc *isect_precalc, float *depth, bool use_original, int *r_active_vertex_index, float *r_face_normal)
Definition: pbvh_bmesh.c:1510
void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode)
Definition: pbvh_bmesh.c:1659
@ PBVH_DYNTOPO_SMOOTH_SHADING
Definition: pbvh_intern.h:113
return ret
#define min(a, b)
Definition: sort.c:51
float bmax[3]
Definition: pbvh_intern.h:30
float bmin[3]
Definition: pbvh_intern.h:30
float bcentroid[3]
Definition: pbvh_intern.h:30
Definition: pbvh_intern.h:24
float bmax[3]
Definition: pbvh_intern.h:25
float bmin[3]
Definition: pbvh_intern.h:25
CustomData vdata
Definition: bmesh_class.h:337
int totface
Definition: bmesh_class.h:297
Definition: BKE_ccg.h:48
int has_mask
Definition: BKE_ccg.h:71
int grid_size
Definition: BKE_ccg.h:56
int grid_area
Definition: BKE_ccg.h:58
struct DistRayAABB_Precalc dist_ray_to_aabb_precalc
Definition: pbvh.c:2403
unsigned int poly
unsigned int tri[3]
unsigned int v
short mat_nr
float co[3]
short no[3]
int face_sets_color_seed
int face_sets_color_default
PBVHFrustumPlanes * frustum
Definition: pbvh.c:2691
int accum_update_flag
Definition: pbvh.c:2692
float(* planes)[4]
Definition: BKE_pbvh.h:84
Definition: pbvh.c:63
PBVHStack stackfixed[STACK_FIXED_DEPTH]
Definition: pbvh.c:71
void * search_data
Definition: pbvh.c:66
PBVH * pbvh
Definition: pbvh.c:64
BKE_pbvh_SearchCallback scb
Definition: pbvh.c:65
PBVHStack * stack
Definition: pbvh.c:68
int stacksize
Definition: pbvh.c:69
int stackspace
Definition: pbvh.c:72
unsigned int totprim
Definition: pbvh_intern.h:53
float tmin
Definition: pbvh_intern.h:92
int * prim_indices
Definition: pbvh_intern.h:52
int children_offset
Definition: pbvh_intern.h:45
PBVHNodeFlags flag
Definition: pbvh_intern.h:89
Definition: pbvh.c:58
PBVHNode * node
Definition: pbvh.c:59
bool revisiting
Definition: pbvh.c:60
PBVH * pbvh
Definition: pbvh.c:1010
int totnode
Definition: pbvh.c:1012
bool show_sculpt_face_sets
Definition: pbvh.c:1016
float(* vnors)[3]
Definition: pbvh.c:1014
PBVHNode ** nodes
Definition: pbvh.c:1011
struct MVert * mvert
Definition: BKE_pbvh.h:372
int * grid_indices
Definition: BKE_pbvh.h:353
short * no
Definition: BKE_pbvh.h:375
bool respect_hide
Definition: BKE_pbvh.h:346
struct CCGKey key
Definition: BKE_pbvh.h:349
float * co
Definition: BKE_pbvh.h:374
int cd_vert_mask_offset
Definition: BKE_pbvh.h:368
float * fno
Definition: BKE_pbvh.h:376
BLI_bitmap ** grid_hidden
Definition: BKE_pbvh.h:352
struct GSetIterator bm_other_verts
Definition: BKE_pbvh.h:366
struct GSetIterator bm_unique_verts
Definition: BKE_pbvh.h:365
BLI_bitmap * gh
Definition: BKE_pbvh.h:352
float * vmask
Definition: BKE_pbvh.h:362
struct CCGElem ** grids
Definition: BKE_pbvh.h:350
struct CCGElem * grid
Definition: BKE_pbvh.h:351
float * mask
Definition: BKE_pbvh.h:377
const int * vert_indices
Definition: BKE_pbvh.h:360
struct MPropCol * vcol
Definition: BKE_pbvh.h:361
struct CustomData * bm_vdata
Definition: BKE_pbvh.h:367
struct MVert * mverts
Definition: BKE_pbvh.h:358
PBVHType type
Definition: pbvh_intern.h:119
float planes[6][4]
Definition: pbvh_intern.h:174
int totvert
Definition: pbvh_intern.h:127
int * prim_indices
Definition: pbvh_intern.h:125
CustomData * ldata
Definition: pbvh_intern.h:138
const DMFlagMat * grid_flag_mats
Definition: pbvh_intern.h:149
CustomData * pdata
Definition: pbvh_intern.h:139
bool respect_hide
Definition: pbvh_intern.h:165
BLI_bitmap ** grid_hidden
Definition: pbvh_intern.h:151
bool deformed
Definition: pbvh_intern.h:162
bool show_mask
Definition: pbvh_intern.h:163
int face_sets_color_seed
Definition: pbvh_intern.h:141
const MPoly * mpoly
Definition: pbvh_intern.h:134
CCGElem ** grids
Definition: pbvh_intern.h:147
const MLoopTri * looptri
Definition: pbvh_intern.h:136
const struct Mesh * mesh
Definition: pbvh_intern.h:132
CCGKey gridkey
Definition: pbvh_intern.h:146
int totprim
Definition: pbvh_intern.h:126
BLI_bitmap * vert_bitmap
Definition: pbvh_intern.h:155
int num_planes
Definition: pbvh_intern.h:175
int node_mem_count
Definition: pbvh_intern.h:123
int totgrid
Definition: pbvh_intern.h:150
void ** gridfaces
Definition: pbvh_intern.h:148
int leaf_limit
Definition: pbvh_intern.h:129
MVert * verts
Definition: pbvh_intern.h:133
bool show_face_sets
Definition: pbvh_intern.h:164
int * face_sets
Definition: pbvh_intern.h:143
PBVHFlags flags
Definition: pbvh_intern.h:120
struct SubdivCCG * subdiv_ccg
Definition: pbvh_intern.h:178
BMesh * bm
Definition: pbvh_intern.h:168
int totnode
Definition: pbvh_intern.h:123
CustomData * vdata
Definition: pbvh_intern.h:137
const MLoop * mloop
Definition: pbvh_intern.h:135
PBVHNode * nodes
Definition: pbvh_intern.h:122
int face_sets_color_default
Definition: pbvh_intern.h:142
bool original
Definition: pbvh.c:1984
struct IsectRayAABB_Precalc ray
Definition: pbvh.c:1983
struct node_tree * left
Definition: pbvh.c:899
PBVHNode * data
Definition: pbvh.c:897
struct node_tree * right
Definition: pbvh.c:900
float max