Blender  V2.93
pbvh_bmesh.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_buffer.h"
24 #include "BLI_ghash.h"
25 #include "BLI_heap_simple.h"
26 #include "BLI_math.h"
27 #include "BLI_memarena.h"
28 #include "BLI_utildefines.h"
29 
30 #include "BKE_DerivedMesh.h"
31 #include "BKE_ccg.h"
32 #include "BKE_pbvh.h"
33 
34 #include "GPU_buffers.h"
35 
36 #include "bmesh.h"
37 #include "pbvh_intern.h"
38 
39 /* Avoid skinny faces */
40 #define USE_EDGEQUEUE_EVEN_SUBDIV
41 #ifdef USE_EDGEQUEUE_EVEN_SUBDIV
42 # include "BKE_global.h"
43 #endif
44 
45 /* Support for only operating on front-faces */
46 #define USE_EDGEQUEUE_FRONTFACE
47 
48 /* don't add edges into the queue multiple times */
49 #define USE_EDGEQUEUE_TAG
54 #if defined(USE_EDGEQUEUE_TAG) && 0
55 # if !defined(NDEBUG)
56 # define USE_EDGEQUEUE_TAG_VERIFY
57 # endif
58 #endif
59 
60 // #define USE_VERIFY
61 
62 #ifdef USE_VERIFY
63 static void pbvh_bmesh_verify(PBVH *pbvh);
64 #endif
65 
66 /* -------------------------------------------------------------------- */
79 #define BM_LOOPS_OF_VERT_ITER_BEGIN(l_iter_radial_, v_) \
80  { \
81  struct { \
82  BMVert *v; \
83  BMEdge *e_iter, *e_first; \
84  BMLoop *l_iter_radial; \
85  } _iter; \
86  _iter.v = v_; \
87  if (_iter.v->e) { \
88  _iter.e_iter = _iter.e_first = _iter.v->e; \
89  do { \
90  if (_iter.e_iter->l) { \
91  _iter.l_iter_radial = _iter.e_iter->l; \
92  do { \
93  if (_iter.l_iter_radial->v == _iter.v) { \
94  l_iter_radial_ = _iter.l_iter_radial;
95 
96 #define BM_LOOPS_OF_VERT_ITER_END \
97  } \
98  } \
99  while ((_iter.l_iter_radial = _iter.l_iter_radial->radial_next) != _iter.e_iter->l) \
100  ; \
101  } \
102  } \
103  while ((_iter.e_iter = BM_DISK_EDGE_NEXT(_iter.e_iter, _iter.v)) != _iter.e_first) \
104  ; \
105  } \
106  } \
107  ((void)0)
108 
109 #define BM_FACES_OF_VERT_ITER_BEGIN(f_iter_, v_) \
110  { \
111  BMLoop *l_iter_radial_; \
112  BM_LOOPS_OF_VERT_ITER_BEGIN (l_iter_radial_, v_) { \
113  f_iter_ = l_iter_radial_->f;
114 
115 #define BM_FACES_OF_VERT_ITER_END \
116  } \
117  BM_LOOPS_OF_VERT_ITER_END; \
118  } \
119  ((void)0)
120 
121 static void bm_edges_from_tri(BMesh *bm, BMVert *v_tri[3], BMEdge *e_tri[3])
122 {
123  e_tri[0] = BM_edge_create(bm, v_tri[0], v_tri[1], NULL, BM_CREATE_NO_DOUBLE);
124  e_tri[1] = BM_edge_create(bm, v_tri[1], v_tri[2], NULL, BM_CREATE_NO_DOUBLE);
125  e_tri[2] = BM_edge_create(bm, v_tri[2], v_tri[0], NULL, BM_CREATE_NO_DOUBLE);
126 }
127 
129 {
131 
132  BLI_assert(f->len == 3);
133 
134  r_index[0] = BM_elem_index_get(l->v);
135  l = l->next;
136  r_index[1] = BM_elem_index_get(l->v);
137  l = l->next;
138  r_index[2] = BM_elem_index_get(l->v);
139 }
140 
158 static BMFace *bm_face_exists_tri_from_loop_vert(BMLoop *l_radial_first, BMVert *v_opposite)
159 {
160  BLI_assert(
161  !ELEM(v_opposite, l_radial_first->v, l_radial_first->next->v, l_radial_first->prev->v));
162  if (l_radial_first->radial_next != l_radial_first) {
163  BMLoop *l_radial_iter = l_radial_first->radial_next;
164  do {
165  BLI_assert(l_radial_iter->f->len == 3);
166  if (l_radial_iter->prev->v == v_opposite) {
167  return l_radial_iter->f;
168  }
169  } while ((l_radial_iter = l_radial_iter->radial_next) != l_radial_first);
170  }
171  return NULL;
172 }
173 
178 static BMVert *bm_vert_hash_lookup_chain(GHash *deleted_verts, BMVert *v)
179 {
180  while (true) {
181  BMVert **v_next_p = (BMVert **)BLI_ghash_lookup_p(deleted_verts, v);
182  if (v_next_p == NULL) {
183  /* not remapped*/
184  return v;
185  }
186  if (*v_next_p == NULL) {
187  /* removed and not remapped */
188  return NULL;
189  }
190 
191  /* remapped */
192  v = *v_next_p;
193  }
194 }
195 
198 /****************************** Building ******************************/
199 
200 /* Update node data after splitting */
201 static void pbvh_bmesh_node_finalize(PBVH *pbvh,
202  const int node_index,
203  const int cd_vert_node_offset,
204  const int cd_face_node_offset)
205 {
206  GSetIterator gs_iter;
207  PBVHNode *n = &pbvh->nodes[node_index];
208  bool has_visible = false;
209 
210  /* Create vert hash sets */
211  n->bm_unique_verts = BLI_gset_ptr_new("bm_unique_verts");
212  n->bm_other_verts = BLI_gset_ptr_new("bm_other_verts");
213 
214  BB_reset(&n->vb);
215 
216  GSET_ITER (gs_iter, n->bm_faces) {
217  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
218 
219  /* Update ownership of faces */
220  BM_ELEM_CD_SET_INT(f, cd_face_node_offset, node_index);
221 
222  /* Update vertices */
223  BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
224  BMLoop *l_iter = l_first;
225 
226  do {
227  BMVert *v = l_iter->v;
228  if (!BLI_gset_haskey(n->bm_unique_verts, v)) {
229  if (BM_ELEM_CD_GET_INT(v, cd_vert_node_offset) != DYNTOPO_NODE_NONE) {
231  }
232  else {
234  BM_ELEM_CD_SET_INT(v, cd_vert_node_offset, node_index);
235  }
236  }
237  /* Update node bounding box */
238  BB_expand(&n->vb, v->co);
239  } while ((l_iter = l_iter->next) != l_first);
240 
242  has_visible = true;
243  }
244  }
245 
246  BLI_assert(n->vb.bmin[0] <= n->vb.bmax[0] && n->vb.bmin[1] <= n->vb.bmax[1] &&
247  n->vb.bmin[2] <= n->vb.bmax[2]);
248 
249  n->orig_vb = n->vb;
250 
251  /* Build GPU buffers for new node and update vertex normals */
253 
254  BKE_pbvh_node_fully_hidden_set(n, !has_visible);
255  n->flag |= PBVH_UpdateNormals;
256 }
257 
258 /* Recursively split the node if it exceeds the leaf_limit */
259 static void pbvh_bmesh_node_split(PBVH *pbvh, const BBC *bbc_array, int node_index)
260 {
261  const int cd_vert_node_offset = pbvh->cd_vert_node_offset;
262  const int cd_face_node_offset = pbvh->cd_face_node_offset;
263  PBVHNode *n = &pbvh->nodes[node_index];
264 
265  if (BLI_gset_len(n->bm_faces) <= pbvh->leaf_limit) {
266  /* Node limit not exceeded */
267  pbvh_bmesh_node_finalize(pbvh, node_index, cd_vert_node_offset, cd_face_node_offset);
268  return;
269  }
270 
271  /* Calculate bounding box around primitive centroids */
272  BB cb;
273  BB_reset(&cb);
274  GSetIterator gs_iter;
275  GSET_ITER (gs_iter, n->bm_faces) {
276  const BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
277  const BBC *bbc = &bbc_array[BM_elem_index_get(f)];
278 
279  BB_expand(&cb, bbc->bcentroid);
280  }
281 
282  /* Find widest axis and its midpoint */
283  const int axis = BB_widest_axis(&cb);
284  const float mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f;
285 
286  /* Add two new child nodes */
287  const int children = pbvh->totnode;
288  n->children_offset = children;
289  pbvh_grow_nodes(pbvh, pbvh->totnode + 2);
290 
291  /* Array reallocated, update current node pointer */
292  n = &pbvh->nodes[node_index];
293 
294  /* Initialize children */
295  PBVHNode *c1 = &pbvh->nodes[children], *c2 = &pbvh->nodes[children + 1];
296  c1->flag |= PBVH_Leaf;
297  c2->flag |= PBVH_Leaf;
298  c1->bm_faces = BLI_gset_ptr_new_ex("bm_faces", BLI_gset_len(n->bm_faces) / 2);
299  c2->bm_faces = BLI_gset_ptr_new_ex("bm_faces", BLI_gset_len(n->bm_faces) / 2);
300 
301  /* Partition the parent node's faces between the two children */
302  GSET_ITER (gs_iter, n->bm_faces) {
303  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
304  const BBC *bbc = &bbc_array[BM_elem_index_get(f)];
305 
306  if (bbc->bcentroid[axis] < mid) {
307  BLI_gset_insert(c1->bm_faces, f);
308  }
309  else {
310  BLI_gset_insert(c2->bm_faces, f);
311  }
312  }
313 
314  /* Enforce at least one primitive in each node */
315  GSet *empty = NULL, *other;
316  if (BLI_gset_len(c1->bm_faces) == 0) {
317  empty = c1->bm_faces;
318  other = c2->bm_faces;
319  }
320  else if (BLI_gset_len(c2->bm_faces) == 0) {
321  empty = c2->bm_faces;
322  other = c1->bm_faces;
323  }
324  if (empty) {
325  GSET_ITER (gs_iter, other) {
326  void *key = BLI_gsetIterator_getKey(&gs_iter);
327  BLI_gset_insert(empty, key);
328  BLI_gset_remove(other, key, NULL);
329  break;
330  }
331  }
332 
333  /* Clear this node */
334 
335  /* Mark this node's unique verts as unclaimed */
336  if (n->bm_unique_verts) {
337  GSET_ITER (gs_iter, n->bm_unique_verts) {
338  BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
339  BM_ELEM_CD_SET_INT(v, cd_vert_node_offset, DYNTOPO_NODE_NONE);
340  }
342  }
343 
344  /* Unclaim faces */
345  GSET_ITER (gs_iter, n->bm_faces) {
346  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
347  BM_ELEM_CD_SET_INT(f, cd_face_node_offset, DYNTOPO_NODE_NONE);
348  }
350 
351  if (n->bm_other_verts) {
353  }
354 
355  if (n->layer_disp) {
356  MEM_freeN(n->layer_disp);
357  }
358 
359  n->bm_faces = NULL;
360  n->bm_unique_verts = NULL;
361  n->bm_other_verts = NULL;
362  n->layer_disp = NULL;
363 
364  if (n->draw_buffers) {
366  n->draw_buffers = NULL;
367  }
368  n->flag &= ~PBVH_Leaf;
369 
370  /* Recurse */
371  pbvh_bmesh_node_split(pbvh, bbc_array, children);
372  pbvh_bmesh_node_split(pbvh, bbc_array, children + 1);
373 
374  /* Array maybe reallocated, update current node pointer */
375  n = &pbvh->nodes[node_index];
376 
377  /* Update bounding box */
378  BB_reset(&n->vb);
379  BB_expand_with_bb(&n->vb, &pbvh->nodes[n->children_offset].vb);
380  BB_expand_with_bb(&n->vb, &pbvh->nodes[n->children_offset + 1].vb);
381  n->orig_vb = n->vb;
382 }
383 
384 /* Recursively split the node if it exceeds the leaf_limit */
385 static bool pbvh_bmesh_node_limit_ensure(PBVH *pbvh, int node_index)
386 {
387  GSet *bm_faces = pbvh->nodes[node_index].bm_faces;
388  const int bm_faces_size = BLI_gset_len(bm_faces);
389  if (bm_faces_size <= pbvh->leaf_limit) {
390  /* Node limit not exceeded */
391  return false;
392  }
393 
394  /* For each BMFace, store the AABB and AABB centroid */
395  BBC *bbc_array = MEM_mallocN(sizeof(BBC) * bm_faces_size, "BBC");
396 
397  GSetIterator gs_iter;
398  int i;
399  GSET_ITER_INDEX (gs_iter, bm_faces, i) {
400  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
401  BBC *bbc = &bbc_array[i];
402 
403  BB_reset((BB *)bbc);
404  BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
405  BMLoop *l_iter = l_first;
406  do {
407  BB_expand((BB *)bbc, l_iter->v->co);
408  } while ((l_iter = l_iter->next) != l_first);
409  BBC_update_centroid(bbc);
410 
411  /* so we can do direct lookups on 'bbc_array' */
412  BM_elem_index_set(f, i); /* set_dirty! */
413  }
414  /* Likely this is already dirty. */
415  pbvh->bm->elem_index_dirty |= BM_FACE;
416 
417  pbvh_bmesh_node_split(pbvh, bbc_array, node_index);
418 
419  MEM_freeN(bbc_array);
420 
421  return true;
422 }
423 
424 /**********************************************************************/
425 
426 #if 0
427 static int pbvh_bmesh_node_offset_from_elem(PBVH *pbvh, BMElem *ele)
428 {
429  switch (ele->head.htype) {
430  case BM_VERT:
431  return pbvh->cd_vert_node_offset;
432  default:
433  BLI_assert(ele->head.htype == BM_FACE);
434  return pbvh->cd_face_node_offset;
435  }
436 }
437 
438 static int pbvh_bmesh_node_index_from_elem(PBVH *pbvh, void *key)
439 {
440  const int cd_node_offset = pbvh_bmesh_node_offset_from_elem(pbvh, key);
441  const int node_index = BM_ELEM_CD_GET_INT((BMElem *)key, cd_node_offset);
442 
443  BLI_assert(node_index != DYNTOPO_NODE_NONE);
444  BLI_assert(node_index < pbvh->totnode);
445  (void)pbvh;
446 
447  return node_index;
448 }
449 
450 static PBVHNode *pbvh_bmesh_node_from_elem(PBVH *pbvh, void *key)
451 {
452  return &pbvh->nodes[pbvh_bmesh_node_index_from_elem(pbvh, key)];
453 }
454 
455 /* typecheck */
456 # define pbvh_bmesh_node_index_from_elem(pbvh, key) \
457  (CHECK_TYPE_ANY(key, BMFace *, BMVert *), pbvh_bmesh_node_index_from_elem(pbvh, key))
458 # define pbvh_bmesh_node_from_elem(pbvh, key) \
459  (CHECK_TYPE_ANY(key, BMFace *, BMVert *), pbvh_bmesh_node_from_elem(pbvh, key))
460 #endif
461 
463 {
464  const int node_index = BM_ELEM_CD_GET_INT((const BMElem *)key, pbvh->cd_vert_node_offset);
465  BLI_assert(node_index != DYNTOPO_NODE_NONE);
466  BLI_assert(node_index < pbvh->totnode);
467  return node_index;
468 }
469 
471 {
472  const int node_index = BM_ELEM_CD_GET_INT((const BMElem *)key, pbvh->cd_face_node_offset);
473  BLI_assert(node_index != DYNTOPO_NODE_NONE);
474  BLI_assert(node_index < pbvh->totnode);
475  return node_index;
476 }
477 
479 {
480  return &pbvh->nodes[pbvh_bmesh_node_index_from_vert(pbvh, key)];
481 }
482 
484 {
485  return &pbvh->nodes[pbvh_bmesh_node_index_from_face(pbvh, key)];
486 }
487 
489  int node_index,
490  const float co[3],
491  const float no[3],
492  const int cd_vert_mask_offset)
493 {
494  PBVHNode *node = &pbvh->nodes[node_index];
495 
496  BLI_assert((pbvh->totnode == 1 || node_index) && node_index <= pbvh->totnode);
497 
498  /* avoid initializing customdata because its quite involved */
501 
502  /* This value is logged below */
503  copy_v3_v3(v->no, no);
504 
505  BLI_gset_insert(node->bm_unique_verts, v);
506  BM_ELEM_CD_SET_INT(v, pbvh->cd_vert_node_offset, node_index);
507 
509 
510  /* Log the new vertex */
511  BM_log_vert_added(pbvh->bm_log, v, cd_vert_mask_offset);
512 
513  return v;
514 }
515 
520  PBVH *pbvh, int node_index, BMVert *v_tri[3], BMEdge *e_tri[3], const BMFace *f_example)
521 {
522  PBVHNode *node = &pbvh->nodes[node_index];
523 
524  /* ensure we never add existing face */
525  BLI_assert(!BM_face_exists(v_tri, 3));
526 
527  BMFace *f = BM_face_create(pbvh->bm, v_tri, e_tri, 3, f_example, BM_CREATE_NOP);
528  f->head.hflag = f_example->head.hflag;
529 
530  BLI_gset_insert(node->bm_faces, f);
531  BM_ELEM_CD_SET_INT(f, pbvh->cd_face_node_offset, node_index);
532 
533  /* mark node for update */
535  node->flag &= ~PBVH_FullyHidden;
536 
537  /* Log the new face */
538  BM_log_face_added(pbvh->bm_log, f);
539 
540  return f;
541 }
542 
543 /* Return the number of faces in 'node' that use vertex 'v' */
544 #if 0
545 static int pbvh_bmesh_node_vert_use_count(PBVH *pbvh, PBVHNode *node, BMVert *v)
546 {
547  BMFace *f;
548  int count = 0;
549 
551  PBVHNode *f_node = pbvh_bmesh_node_from_face(pbvh, f);
552  if (f_node == node) {
553  count++;
554  }
555  }
557 
558  return count;
559 }
560 #endif
561 
562 #define pbvh_bmesh_node_vert_use_count_is_equal(pbvh, node, v, n) \
563  (pbvh_bmesh_node_vert_use_count_at_most(pbvh, node, v, (n) + 1) == n)
564 
566  PBVHNode *node,
567  BMVert *v,
568  const int count_max)
569 {
570  int count = 0;
571  BMFace *f;
572 
574  PBVHNode *f_node = pbvh_bmesh_node_from_face(pbvh, f);
575  if (f_node == node) {
576  count++;
577  if (count == count_max) {
578  return count;
579  }
580  }
581  }
583 
584  return count;
585 }
586 
587 /* Return a node that uses vertex 'v' other than its current owner */
589 {
590  PBVHNode *current_node = pbvh_bmesh_node_from_vert(pbvh, v);
591  BMFace *f;
592 
594  PBVHNode *f_node = pbvh_bmesh_node_from_face(pbvh, f);
595 
596  if (f_node != current_node) {
597  return f_node;
598  }
599  }
601 
602  return NULL;
603 }
604 
605 static void pbvh_bmesh_vert_ownership_transfer(PBVH *pbvh, PBVHNode *new_owner, BMVert *v)
606 {
607  PBVHNode *current_owner = pbvh_bmesh_node_from_vert(pbvh, v);
608  /* mark node for update */
609  current_owner->flag |= PBVH_UpdateDrawBuffers | PBVH_UpdateBB;
610 
611  BLI_assert(current_owner != new_owner);
612 
613  /* Remove current ownership */
614  BLI_gset_remove(current_owner->bm_unique_verts, v, NULL);
615 
616  /* Set new ownership */
617  BM_ELEM_CD_SET_INT(v, pbvh->cd_vert_node_offset, new_owner - pbvh->nodes);
618  BLI_gset_insert(new_owner->bm_unique_verts, v);
619  BLI_gset_remove(new_owner->bm_other_verts, v, NULL);
621 
622  /* mark node for update */
623  new_owner->flag |= PBVH_UpdateDrawBuffers | PBVH_UpdateBB;
624 }
625 
626 static void pbvh_bmesh_vert_remove(PBVH *pbvh, BMVert *v)
627 {
628  /* never match for first time */
629  int f_node_index_prev = DYNTOPO_NODE_NONE;
630 
631  PBVHNode *v_node = pbvh_bmesh_node_from_vert(pbvh, v);
634 
635  /* Have to check each neighboring face's node */
636  BMFace *f;
638  const int f_node_index = pbvh_bmesh_node_index_from_face(pbvh, f);
639 
640  /* faces often share the same node,
641  * quick check to avoid redundant #BLI_gset_remove calls */
642  if (f_node_index_prev != f_node_index) {
643  f_node_index_prev = f_node_index;
644 
645  PBVHNode *f_node = &pbvh->nodes[f_node_index];
647 
648  /* Remove current ownership */
650 
653  }
654  }
656 }
657 
658 static void pbvh_bmesh_face_remove(PBVH *pbvh, BMFace *f)
659 {
660  PBVHNode *f_node = pbvh_bmesh_node_from_face(pbvh, f);
661 
662  /* Check if any of this face's vertices need to be removed
663  * from the node */
664  BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
665  BMLoop *l_iter = l_first;
666  do {
667  BMVert *v = l_iter->v;
668  if (pbvh_bmesh_node_vert_use_count_is_equal(pbvh, f_node, v, 1)) {
669  if (BLI_gset_haskey(f_node->bm_unique_verts, v)) {
670  /* Find a different node that uses 'v' */
671  PBVHNode *new_node;
672 
673  new_node = pbvh_bmesh_vert_other_node_find(pbvh, v);
674  BLI_assert(new_node || BM_vert_face_count_is_equal(v, 1));
675 
676  if (new_node) {
677  pbvh_bmesh_vert_ownership_transfer(pbvh, new_node, v);
678  }
679  }
680  else {
681  /* Remove from other verts */
683  }
684  }
685  } while ((l_iter = l_iter->next) != l_first);
686 
687  /* Remove face from node and top level */
688  BLI_gset_remove(f_node->bm_faces, f, NULL);
690 
691  /* Log removed face */
692  BM_log_face_removed(pbvh->bm_log, f);
693 
694  /* mark node for update */
696 }
697 
699 {
700  /* fast-path for most common case where an edge has 2 faces,
701  * no need to iterate twice.
702  * This assumes that the buffer */
703  BMLoop **data = buf->data;
704  BLI_assert(buf->alloc_count >= 2);
705  if (LIKELY(BM_edge_loop_pair(e, &data[0], &data[1]))) {
706  buf->count = 2;
707  }
708  else {
711  }
712 }
713 
715 {
716  if (node->bm_orco) {
717  MEM_freeN(node->bm_orco);
718  }
719  if (node->bm_ortri) {
720  MEM_freeN(node->bm_ortri);
721  }
722  node->bm_orco = NULL;
723  node->bm_ortri = NULL;
724  node->bm_tot_ortri = 0;
725 }
726 
727 /****************************** EdgeQueue *****************************/
728 
729 struct EdgeQueue;
730 
731 typedef struct EdgeQueue {
733  const float *center;
734  float center_proj[3]; /* for when we use projected coords. */
737 #ifdef USE_EDGEQUEUE_EVEN_SUBDIV
738  float limit_len;
739 #endif
740 
741  bool (*edge_queue_tri_in_range)(const struct EdgeQueue *q, BMFace *f);
742 
743  const float *view_normal;
744 #ifdef USE_EDGEQUEUE_FRONTFACE
745  unsigned int use_view_normal : 1;
746 #endif
748 
749 typedef struct {
757 
758 /* only tag'd edges are in the queue */
759 #ifdef USE_EDGEQUEUE_TAG
760 # define EDGE_QUEUE_TEST(e) (BM_elem_flag_test((CHECK_TYPE_INLINE(e, BMEdge *), e), BM_ELEM_TAG))
761 # define EDGE_QUEUE_ENABLE(e) \
762  BM_elem_flag_enable((CHECK_TYPE_INLINE(e, BMEdge *), e), BM_ELEM_TAG)
763 # define EDGE_QUEUE_DISABLE(e) \
764  BM_elem_flag_disable((CHECK_TYPE_INLINE(e, BMEdge *), e), BM_ELEM_TAG)
765 #endif
766 
767 #ifdef USE_EDGEQUEUE_TAG_VERIFY
768 /* simply check no edges are tagged
769  * (it's a requirement that edges enter and leave a clean tag state) */
770 static void pbvh_bmesh_edge_tag_verify(PBVH *pbvh)
771 {
772  for (int n = 0; n < pbvh->totnode; n++) {
773  PBVHNode *node = &pbvh->nodes[n];
774  if (node->bm_faces) {
775  GSetIterator gs_iter;
776  GSET_ITER (gs_iter, node->bm_faces) {
777  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
778  BMEdge *e_tri[3];
779  BMLoop *l_iter;
780 
781  BLI_assert(f->len == 3);
782  l_iter = BM_FACE_FIRST_LOOP(f);
783  e_tri[0] = l_iter->e;
784  l_iter = l_iter->next;
785  e_tri[1] = l_iter->e;
786  l_iter = l_iter->next;
787  e_tri[2] = l_iter->e;
788 
789  BLI_assert((EDGE_QUEUE_TEST(e_tri[0]) == false) && (EDGE_QUEUE_TEST(e_tri[1]) == false) &&
790  (EDGE_QUEUE_TEST(e_tri[2]) == false));
791  }
792  }
793  }
794 }
795 #endif
796 
797 static bool edge_queue_tri_in_sphere(const EdgeQueue *q, BMFace *f)
798 {
799  BMVert *v_tri[3];
800  float c[3];
801 
802  /* Get closest point in triangle to sphere center */
803  BM_face_as_array_vert_tri(f, v_tri);
804 
805  closest_on_tri_to_point_v3(c, q->center, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co);
806 
807  /* Check if triangle intersects the sphere */
808  return len_squared_v3v3(q->center, c) <= q->radius_squared;
809 }
810 
811 static bool edge_queue_tri_in_circle(const EdgeQueue *q, BMFace *f)
812 {
813  BMVert *v_tri[3];
814  float c[3];
815  float tri_proj[3][3];
816 
817  /* Get closest point in triangle to sphere center */
818  BM_face_as_array_vert_tri(f, v_tri);
819 
820  project_plane_normalized_v3_v3v3(tri_proj[0], v_tri[0]->co, q->view_normal);
821  project_plane_normalized_v3_v3v3(tri_proj[1], v_tri[1]->co, q->view_normal);
822  project_plane_normalized_v3_v3v3(tri_proj[2], v_tri[2]->co, q->view_normal);
823 
824  closest_on_tri_to_point_v3(c, q->center_proj, tri_proj[0], tri_proj[1], tri_proj[2]);
825 
826  /* Check if triangle intersects the sphere */
827  return len_squared_v3v3(q->center_proj, c) <= q->radius_squared;
828 }
829 
830 /* Return true if the vertex mask is less than 1.0, false otherwise */
831 static bool check_mask(EdgeQueueContext *eq_ctx, BMVert *v)
832 {
833  return BM_ELEM_CD_GET_FLOAT(v, eq_ctx->cd_vert_mask_offset) < 1.0f;
834 }
835 
836 static void edge_queue_insert(EdgeQueueContext *eq_ctx, BMEdge *e, float priority)
837 {
838  /* Don't let topology update affect fully masked vertices. This used to
839  * have a 50% mask cutoff, with the reasoning that you can't do a 50%
840  * topology update. But this gives an ugly border in the mesh. The mask
841  * should already make the brush move the vertices only 50%, which means
842  * that topology updates will also happen less frequent, that should be
843  * enough. */
844  if (((eq_ctx->cd_vert_mask_offset == -1) ||
845  (check_mask(eq_ctx, e->v1) || check_mask(eq_ctx, e->v2))) &&
848  BMVert **pair = BLI_mempool_alloc(eq_ctx->pool);
849  pair[0] = e->v1;
850  pair[1] = e->v2;
851  BLI_heapsimple_insert(eq_ctx->q->heap, priority, pair);
852 #ifdef USE_EDGEQUEUE_TAG
853  BLI_assert(EDGE_QUEUE_TEST(e) == false);
855 #endif
856  }
857 }
858 
860 {
861 #ifdef USE_EDGEQUEUE_TAG
862  if (EDGE_QUEUE_TEST(e) == false)
863 #endif
864  {
865  const float len_sq = BM_edge_calc_length_squared(e);
866  if (len_sq > eq_ctx->q->limit_len_squared) {
867  edge_queue_insert(eq_ctx, e, -len_sq);
868  }
869  }
870 }
871 
872 #ifdef USE_EDGEQUEUE_EVEN_SUBDIV
874  EdgeQueueContext *eq_ctx, BMLoop *l_edge, BMLoop *l_end, const float len_sq, float limit_len)
875 {
876  BLI_assert(len_sq > square_f(limit_len));
877 
878 # ifdef USE_EDGEQUEUE_FRONTFACE
879  if (eq_ctx->q->use_view_normal) {
880  if (dot_v3v3(l_edge->f->no, eq_ctx->q->view_normal) < 0.0f) {
881  return;
882  }
883  }
884 # endif
885 
886 # ifdef USE_EDGEQUEUE_TAG
887  if (EDGE_QUEUE_TEST(l_edge->e) == false)
888 # endif
889  {
890  edge_queue_insert(eq_ctx, l_edge->e, -len_sq);
891  }
892 
893  /* temp support previous behavior! */
894  if (UNLIKELY(G.debug_value == 1234)) {
895  return;
896  }
897 
898  if ((l_edge->radial_next != l_edge)) {
899  /* How much longer we need to be to consider for subdividing
900  * (avoids subdividing faces which are only *slightly* skinny) */
901 # define EVEN_EDGELEN_THRESHOLD 1.2f
902  /* How much the limit increases per recursion
903  * (avoids performing subdivisions too far away). */
904 # define EVEN_GENERATION_SCALE 1.6f
905 
906  const float len_sq_cmp = len_sq * EVEN_EDGELEN_THRESHOLD;
907 
908  limit_len *= EVEN_GENERATION_SCALE;
909  const float limit_len_sq = square_f(limit_len);
910 
911  BMLoop *l_iter = l_edge;
912  do {
913  BMLoop *l_adjacent[2] = {l_iter->next, l_iter->prev};
914  for (int i = 0; i < ARRAY_SIZE(l_adjacent); i++) {
915  float len_sq_other = BM_edge_calc_length_squared(l_adjacent[i]->e);
916  if (len_sq_other > max_ff(len_sq_cmp, limit_len_sq)) {
917  // edge_queue_insert(eq_ctx, l_adjacent[i]->e, -len_sq_other);
919  eq_ctx, l_adjacent[i]->radial_next, l_adjacent[i], len_sq_other, limit_len);
920  }
921  }
922  } while ((l_iter = l_iter->radial_next) != l_end);
923 
924 # undef EVEN_EDGELEN_THRESHOLD
925 # undef EVEN_GENERATION_SCALE
926  }
927 }
928 #endif /* USE_EDGEQUEUE_EVEN_SUBDIV */
929 
931 {
932 #ifdef USE_EDGEQUEUE_TAG
933  if (EDGE_QUEUE_TEST(e) == false)
934 #endif
935  {
936  const float len_sq = BM_edge_calc_length_squared(e);
937  if (len_sq < eq_ctx->q->limit_len_squared) {
938  edge_queue_insert(eq_ctx, e, len_sq);
939  }
940  }
941 }
942 
944 {
945 #ifdef USE_EDGEQUEUE_FRONTFACE
946  if (eq_ctx->q->use_view_normal) {
947  if (dot_v3v3(f->no, eq_ctx->q->view_normal) < 0.0f) {
948  return;
949  }
950  }
951 #endif
952 
953  if (eq_ctx->q->edge_queue_tri_in_range(eq_ctx->q, f)) {
954  /* Check each edge of the face */
955  BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
956  BMLoop *l_iter = l_first;
957  do {
958 #ifdef USE_EDGEQUEUE_EVEN_SUBDIV
959  const float len_sq = BM_edge_calc_length_squared(l_iter->e);
960  if (len_sq > eq_ctx->q->limit_len_squared) {
962  eq_ctx, l_iter->radial_next, l_iter, len_sq, eq_ctx->q->limit_len);
963  }
964 #else
965  long_edge_queue_edge_add(eq_ctx, l_iter->e);
966 #endif
967  } while ((l_iter = l_iter->next) != l_first);
968  }
969 }
970 
972 {
973 #ifdef USE_EDGEQUEUE_FRONTFACE
974  if (eq_ctx->q->use_view_normal) {
975  if (dot_v3v3(f->no, eq_ctx->q->view_normal) < 0.0f) {
976  return;
977  }
978  }
979 #endif
980 
981  if (eq_ctx->q->edge_queue_tri_in_range(eq_ctx->q, f)) {
982  BMLoop *l_iter;
983  BMLoop *l_first;
984 
985  /* Check each edge of the face */
986  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
987  do {
988  short_edge_queue_edge_add(eq_ctx, l_iter->e);
989  } while ((l_iter = l_iter->next) != l_first);
990  }
991 }
992 
993 /* Create a priority queue containing vertex pairs connected by a long
994  * edge as defined by PBVH.bm_max_edge_len.
995  *
996  * Only nodes marked for topology update are checked, and in those
997  * nodes only edges used by a face intersecting the (center, radius)
998  * sphere are checked.
999  *
1000  * The highest priority (lowest number) is given to the longest edge.
1001  */
1003  PBVH *pbvh,
1004  const float center[3],
1005  const float view_normal[3],
1006  float radius,
1007  const bool use_frontface,
1008  const bool use_projected)
1009 {
1010  eq_ctx->q->heap = BLI_heapsimple_new();
1011  eq_ctx->q->center = center;
1012  eq_ctx->q->radius_squared = radius * radius;
1013  eq_ctx->q->limit_len_squared = pbvh->bm_max_edge_len * pbvh->bm_max_edge_len;
1014 #ifdef USE_EDGEQUEUE_EVEN_SUBDIV
1015  eq_ctx->q->limit_len = pbvh->bm_max_edge_len;
1016 #endif
1017 
1018  eq_ctx->q->view_normal = view_normal;
1019 
1020 #ifdef USE_EDGEQUEUE_FRONTFACE
1021  eq_ctx->q->use_view_normal = use_frontface;
1022 #else
1023  UNUSED_VARS(use_frontface);
1024 #endif
1025 
1026  if (use_projected) {
1028  project_plane_normalized_v3_v3v3(eq_ctx->q->center_proj, center, view_normal);
1029  }
1030  else {
1032  }
1033 
1034 #ifdef USE_EDGEQUEUE_TAG_VERIFY
1035  pbvh_bmesh_edge_tag_verify(pbvh);
1036 #endif
1037 
1038  for (int n = 0; n < pbvh->totnode; n++) {
1039  PBVHNode *node = &pbvh->nodes[n];
1040 
1041  /* Check leaf nodes marked for topology update */
1042  if ((node->flag & PBVH_Leaf) && (node->flag & PBVH_UpdateTopology) &&
1043  !(node->flag & PBVH_FullyHidden)) {
1044  GSetIterator gs_iter;
1045 
1046  /* Check each face */
1047  GSET_ITER (gs_iter, node->bm_faces) {
1048  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
1049 
1050  long_edge_queue_face_add(eq_ctx, f);
1051  }
1052  }
1053  }
1054 }
1055 
1056 /* Create a priority queue containing vertex pairs connected by a
1057  * short edge as defined by PBVH.bm_min_edge_len.
1058  *
1059  * Only nodes marked for topology update are checked, and in those
1060  * nodes only edges used by a face intersecting the (center, radius)
1061  * sphere are checked.
1062  *
1063  * The highest priority (lowest number) is given to the shortest edge.
1064  */
1066  PBVH *pbvh,
1067  const float center[3],
1068  const float view_normal[3],
1069  float radius,
1070  const bool use_frontface,
1071  const bool use_projected)
1072 {
1073  eq_ctx->q->heap = BLI_heapsimple_new();
1074  eq_ctx->q->center = center;
1075  eq_ctx->q->radius_squared = radius * radius;
1076  eq_ctx->q->limit_len_squared = pbvh->bm_min_edge_len * pbvh->bm_min_edge_len;
1077 #ifdef USE_EDGEQUEUE_EVEN_SUBDIV
1078  eq_ctx->q->limit_len = pbvh->bm_min_edge_len;
1079 #endif
1080 
1081  eq_ctx->q->view_normal = view_normal;
1082 
1083 #ifdef USE_EDGEQUEUE_FRONTFACE
1084  eq_ctx->q->use_view_normal = use_frontface;
1085 #else
1086  UNUSED_VARS(use_frontface);
1087 #endif
1088 
1089  if (use_projected) {
1091  project_plane_normalized_v3_v3v3(eq_ctx->q->center_proj, center, view_normal);
1092  }
1093  else {
1095  }
1096 
1097  for (int n = 0; n < pbvh->totnode; n++) {
1098  PBVHNode *node = &pbvh->nodes[n];
1099 
1100  /* Check leaf nodes marked for topology update */
1101  if ((node->flag & PBVH_Leaf) && (node->flag & PBVH_UpdateTopology) &&
1102  !(node->flag & PBVH_FullyHidden)) {
1103  GSetIterator gs_iter;
1104 
1105  /* Check each face */
1106  GSET_ITER (gs_iter, node->bm_faces) {
1107  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
1108 
1109  short_edge_queue_face_add(eq_ctx, f);
1110  }
1111  }
1112  }
1113 }
1114 
1115 /*************************** Topology update **************************/
1116 
1118  PBVH *pbvh,
1119  BMEdge *e,
1120  BLI_Buffer *edge_loops)
1121 {
1122  float co_mid[3], no_mid[3];
1123 
1124  /* Get all faces adjacent to the edge */
1125  pbvh_bmesh_edge_loops(edge_loops, e);
1126 
1127  /* Create a new vertex in current node at the edge's midpoint */
1128  mid_v3_v3v3(co_mid, e->v1->co, e->v2->co);
1129  mid_v3_v3v3(no_mid, e->v1->no, e->v2->no);
1130  normalize_v3(no_mid);
1131 
1132  int node_index = BM_ELEM_CD_GET_INT(e->v1, eq_ctx->cd_vert_node_offset);
1133  BMVert *v_new = pbvh_bmesh_vert_create(
1134  pbvh, node_index, co_mid, no_mid, eq_ctx->cd_vert_mask_offset);
1135 
1136  /* update paint mask */
1137  if (eq_ctx->cd_vert_mask_offset != -1) {
1138  float mask_v1 = BM_ELEM_CD_GET_FLOAT(e->v1, eq_ctx->cd_vert_mask_offset);
1139  float mask_v2 = BM_ELEM_CD_GET_FLOAT(e->v2, eq_ctx->cd_vert_mask_offset);
1140  float mask_v_new = 0.5f * (mask_v1 + mask_v2);
1141 
1142  BM_ELEM_CD_SET_FLOAT(v_new, eq_ctx->cd_vert_mask_offset, mask_v_new);
1143  }
1144 
1145  /* For each face, add two new triangles and delete the original */
1146  for (int i = 0; i < edge_loops->count; i++) {
1147  BMLoop *l_adj = BLI_buffer_at(edge_loops, BMLoop *, i);
1148  BMFace *f_adj = l_adj->f;
1149  BMFace *f_new;
1150  BMVert *v_opp, *v1, *v2;
1151  BMVert *v_tri[3];
1152  BMEdge *e_tri[3];
1153 
1154  BLI_assert(f_adj->len == 3);
1155  int ni = BM_ELEM_CD_GET_INT(f_adj, eq_ctx->cd_face_node_offset);
1156 
1157  /* Find the vertex not in the edge */
1158  v_opp = l_adj->prev->v;
1159 
1160  /* Get e->v1 and e->v2 in the order they appear in the
1161  * existing face so that the new faces' winding orders
1162  * match */
1163  v1 = l_adj->v;
1164  v2 = l_adj->next->v;
1165 
1166  if (ni != node_index && i == 0) {
1167  pbvh_bmesh_vert_ownership_transfer(pbvh, &pbvh->nodes[ni], v_new);
1168  }
1169 
1196  /* Create two new faces */
1197  v_tri[0] = v1;
1198  v_tri[1] = v_new;
1199  v_tri[2] = v_opp;
1200  bm_edges_from_tri(pbvh->bm, v_tri, e_tri);
1201  f_new = pbvh_bmesh_face_create(pbvh, ni, v_tri, e_tri, f_adj);
1202  long_edge_queue_face_add(eq_ctx, f_new);
1203 
1204  v_tri[0] = v_new;
1205  v_tri[1] = v2;
1206  /* v_tri[2] = v_opp; */ /* unchanged */
1207  e_tri[0] = BM_edge_create(pbvh->bm, v_tri[0], v_tri[1], NULL, BM_CREATE_NO_DOUBLE);
1208  e_tri[2] = e_tri[1]; /* switched */
1209  e_tri[1] = BM_edge_create(pbvh->bm, v_tri[1], v_tri[2], NULL, BM_CREATE_NO_DOUBLE);
1210  f_new = pbvh_bmesh_face_create(pbvh, ni, v_tri, e_tri, f_adj);
1211  long_edge_queue_face_add(eq_ctx, f_new);
1212 
1213  /* Delete original */
1214  pbvh_bmesh_face_remove(pbvh, f_adj);
1215  BM_face_kill(pbvh->bm, f_adj);
1216 
1217  /* Ensure new vertex is in the node */
1218  if (!BLI_gset_haskey(pbvh->nodes[ni].bm_unique_verts, v_new)) {
1219  BLI_gset_add(pbvh->nodes[ni].bm_other_verts, v_new);
1220  }
1221 
1222  if (BM_vert_edge_count_is_over(v_opp, 8)) {
1223  BMIter bm_iter;
1224  BMEdge *e2;
1225 
1226  BM_ITER_ELEM (e2, &bm_iter, v_opp, BM_EDGES_OF_VERT) {
1227  long_edge_queue_edge_add(eq_ctx, e2);
1228  }
1229  }
1230  }
1231 
1232  BM_edge_kill(pbvh->bm, e);
1233 }
1234 
1236  PBVH *pbvh,
1237  BLI_Buffer *edge_loops)
1238 {
1239  bool any_subdivided = false;
1240 
1241  while (!BLI_heapsimple_is_empty(eq_ctx->q->heap)) {
1242  BMVert **pair = BLI_heapsimple_pop_min(eq_ctx->q->heap);
1243  BMVert *v1 = pair[0], *v2 = pair[1];
1244  BMEdge *e;
1245 
1246  BLI_mempool_free(eq_ctx->pool, pair);
1247  pair = NULL;
1248 
1249  /* Check that the edge still exists */
1250  if (!(e = BM_edge_exists(v1, v2))) {
1251  continue;
1252  }
1253 #ifdef USE_EDGEQUEUE_TAG
1255 #endif
1256 
1257  /* At the moment edges never get shorter (subdiv will make new edges)
1258  * unlike collapse where edges can become longer. */
1259 #if 0
1260  if (len_squared_v3v3(v1->co, v2->co) <= eq_ctx->q->limit_len_squared) {
1261  continue;
1262  }
1263 #else
1264  BLI_assert(len_squared_v3v3(v1->co, v2->co) > eq_ctx->q->limit_len_squared);
1265 #endif
1266 
1267  /* Check that the edge's vertices are still in the PBVH. It's
1268  * possible that an edge collapse has deleted adjacent faces
1269  * and the node has been split, thus leaving wire edges and
1270  * associated vertices. */
1271  if ((BM_ELEM_CD_GET_INT(e->v1, eq_ctx->cd_vert_node_offset) == DYNTOPO_NODE_NONE) ||
1273  continue;
1274  }
1275 
1276  any_subdivided = true;
1277 
1278  pbvh_bmesh_split_edge(eq_ctx, pbvh, e, edge_loops);
1279  }
1280 
1281 #ifdef USE_EDGEQUEUE_TAG_VERIFY
1282  pbvh_bmesh_edge_tag_verify(pbvh);
1283 #endif
1284 
1285  return any_subdivided;
1286 }
1287 
1288 static void pbvh_bmesh_collapse_edge(PBVH *pbvh,
1289  BMEdge *e,
1290  BMVert *v1,
1291  BMVert *v2,
1292  GHash *deleted_verts,
1293  BLI_Buffer *deleted_faces,
1294  EdgeQueueContext *eq_ctx)
1295 {
1296  BMVert *v_del, *v_conn;
1297 
1298  /* one of the two vertices may be masked, select the correct one for deletion */
1301  v_del = v1;
1302  v_conn = v2;
1303  }
1304  else {
1305  v_del = v2;
1306  v_conn = v1;
1307  }
1308 
1309  /* Remove the merge vertex from the PBVH */
1310  pbvh_bmesh_vert_remove(pbvh, v_del);
1311 
1312  /* Remove all faces adjacent to the edge */
1313  BMLoop *l_adj;
1314  while ((l_adj = e->l)) {
1315  BMFace *f_adj = l_adj->f;
1316 
1317  pbvh_bmesh_face_remove(pbvh, f_adj);
1318  BM_face_kill(pbvh->bm, f_adj);
1319  }
1320 
1321  /* Kill the edge */
1323  BM_edge_kill(pbvh->bm, e);
1324 
1325  /* For all remaining faces of v_del, create a new face that is the
1326  * same except it uses v_conn instead of v_del */
1327  /* Note: this could be done with BM_vert_splice(), but that
1328  * requires handling other issues like duplicate edges, so doesn't
1329  * really buy anything. */
1330  BLI_buffer_clear(deleted_faces);
1331 
1332  BMLoop *l;
1333 
1334  BM_LOOPS_OF_VERT_ITER_BEGIN (l, v_del) {
1335  BMFace *existing_face;
1336 
1337  /* Get vertices, replace use of v_del with v_conn */
1338  // BM_iter_as_array(NULL, BM_VERTS_OF_FACE, f, (void **)v_tri, 3);
1339  BMFace *f = l->f;
1340 #if 0
1341  BMVert *v_tri[3];
1342  BM_face_as_array_vert_tri(f, v_tri);
1343  for (int i = 0; i < 3; i++) {
1344  if (v_tri[i] == v_del) {
1345  v_tri[i] = v_conn;
1346  }
1347  }
1348 #endif
1349 
1350  /* Check if a face using these vertices already exists. If so,
1351  * skip adding this face and mark the existing one for
1352  * deletion as well. Prevents extraneous "flaps" from being
1353  * created. */
1354 #if 0
1355  if (UNLIKELY(existing_face = BM_face_exists(v_tri, 3)))
1356 #else
1357  if (UNLIKELY(existing_face = bm_face_exists_tri_from_loop_vert(l->next, v_conn)))
1358 #endif
1359  {
1360  BLI_buffer_append(deleted_faces, BMFace *, existing_face);
1361  }
1362  else
1363  {
1364  BMVert *v_tri[3] = {v_conn, l->next->v, l->prev->v};
1365 
1366  BLI_assert(!BM_face_exists(v_tri, 3));
1367  BMEdge *e_tri[3];
1368  PBVHNode *n = pbvh_bmesh_node_from_face(pbvh, f);
1369  int ni = n - pbvh->nodes;
1370  bm_edges_from_tri(pbvh->bm, v_tri, e_tri);
1371  pbvh_bmesh_face_create(pbvh, ni, v_tri, e_tri, f);
1372 
1373  /* Ensure that v_conn is in the new face's node */
1374  if (!BLI_gset_haskey(n->bm_unique_verts, v_conn)) {
1375  BLI_gset_add(n->bm_other_verts, v_conn);
1376  }
1377  }
1378 
1379  BLI_buffer_append(deleted_faces, BMFace *, f);
1380  }
1382 
1383  /* Delete the tagged faces */
1384  for (int i = 0; i < deleted_faces->count; i++) {
1385  BMFace *f_del = BLI_buffer_at(deleted_faces, BMFace *, i);
1386 
1387  /* Get vertices and edges of face */
1388  BLI_assert(f_del->len == 3);
1389  BMLoop *l_iter = BM_FACE_FIRST_LOOP(f_del);
1390  BMVert *v_tri[3];
1391  BMEdge *e_tri[3];
1392  v_tri[0] = l_iter->v;
1393  e_tri[0] = l_iter->e;
1394  l_iter = l_iter->next;
1395  v_tri[1] = l_iter->v;
1396  e_tri[1] = l_iter->e;
1397  l_iter = l_iter->next;
1398  v_tri[2] = l_iter->v;
1399  e_tri[2] = l_iter->e;
1400 
1401  /* Remove the face */
1402  pbvh_bmesh_face_remove(pbvh, f_del);
1403  BM_face_kill(pbvh->bm, f_del);
1404 
1405  /* Check if any of the face's edges are now unused by any
1406  * face, if so delete them */
1407  for (int j = 0; j < 3; j++) {
1408  if (BM_edge_is_wire(e_tri[j])) {
1409  BM_edge_kill(pbvh->bm, e_tri[j]);
1410  }
1411  }
1412 
1413  /* Check if any of the face's vertices are now unused, if so
1414  * remove them from the PBVH */
1415  for (int j = 0; j < 3; j++) {
1416  if ((v_tri[j] != v_del) && (v_tri[j]->e == NULL)) {
1417  pbvh_bmesh_vert_remove(pbvh, v_tri[j]);
1418 
1419  BM_log_vert_removed(pbvh->bm_log, v_tri[j], eq_ctx->cd_vert_mask_offset);
1420 
1421  if (v_tri[j] == v_conn) {
1422  v_conn = NULL;
1423  }
1424  BLI_ghash_insert(deleted_verts, v_tri[j], NULL);
1425  BM_vert_kill(pbvh->bm, v_tri[j]);
1426  }
1427  }
1428  }
1429 
1430  /* Move v_conn to the midpoint of v_conn and v_del (if v_conn still exists, it
1431  * may have been deleted above) */
1432  if (v_conn != NULL) {
1433  BM_log_vert_before_modified(pbvh->bm_log, v_conn, eq_ctx->cd_vert_mask_offset);
1434  mid_v3_v3v3(v_conn->co, v_conn->co, v_del->co);
1435  add_v3_v3(v_conn->no, v_del->no);
1436  normalize_v3(v_conn->no);
1437 
1438  /* update boundboxes attached to the connected vertex
1439  * note that we can often get-away without this but causes T48779 */
1440  BM_LOOPS_OF_VERT_ITER_BEGIN (l, v_conn) {
1441  PBVHNode *f_node = pbvh_bmesh_node_from_face(pbvh, l->f);
1443  }
1445  }
1446 
1447  /* Delete v_del */
1448  BLI_assert(!BM_vert_face_check(v_del));
1449  BM_log_vert_removed(pbvh->bm_log, v_del, eq_ctx->cd_vert_mask_offset);
1450  /* v_conn == NULL is OK */
1451  BLI_ghash_insert(deleted_verts, v_del, v_conn);
1452  BM_vert_kill(pbvh->bm, v_del);
1453 }
1454 
1456  PBVH *pbvh,
1457  BLI_Buffer *deleted_faces)
1458 {
1459  const float min_len_squared = pbvh->bm_min_edge_len * pbvh->bm_min_edge_len;
1460  bool any_collapsed = false;
1461  /* deleted verts point to vertices they were merged into, or NULL when removed. */
1462  GHash *deleted_verts = BLI_ghash_ptr_new("deleted_verts");
1463 
1464  while (!BLI_heapsimple_is_empty(eq_ctx->q->heap)) {
1465  BMVert **pair = BLI_heapsimple_pop_min(eq_ctx->q->heap);
1466  BMVert *v1 = pair[0], *v2 = pair[1];
1467  BLI_mempool_free(eq_ctx->pool, pair);
1468  pair = NULL;
1469 
1470  /* Check the verts still exist */
1471  if (!(v1 = bm_vert_hash_lookup_chain(deleted_verts, v1)) ||
1472  !(v2 = bm_vert_hash_lookup_chain(deleted_verts, v2)) || (v1 == v2)) {
1473  continue;
1474  }
1475 
1476  /* Check that the edge still exists */
1477  BMEdge *e;
1478  if (!(e = BM_edge_exists(v1, v2))) {
1479  continue;
1480  }
1481 #ifdef USE_EDGEQUEUE_TAG
1483 #endif
1484 
1485  if (len_squared_v3v3(v1->co, v2->co) >= min_len_squared) {
1486  continue;
1487  }
1488 
1489  /* Check that the edge's vertices are still in the PBVH. It's
1490  * possible that an edge collapse has deleted adjacent faces
1491  * and the node has been split, thus leaving wire edges and
1492  * associated vertices. */
1493  if ((BM_ELEM_CD_GET_INT(e->v1, eq_ctx->cd_vert_node_offset) == DYNTOPO_NODE_NONE) ||
1495  continue;
1496  }
1497 
1498  any_collapsed = true;
1499 
1500  pbvh_bmesh_collapse_edge(pbvh, e, v1, v2, deleted_verts, deleted_faces, eq_ctx);
1501  }
1502 
1503  BLI_ghash_free(deleted_verts, NULL, NULL);
1504 
1505  return any_collapsed;
1506 }
1507 
1508 /************************* Called from pbvh.c *************************/
1509 
1511  const float ray_start[3],
1512  const float ray_normal[3],
1513  struct IsectRayPrecalc *isect_precalc,
1514  float *depth,
1515  bool use_original,
1516  int *r_active_vertex_index,
1517  float *r_face_normal)
1518 {
1519  bool hit = false;
1520  float nearest_vertex_co[3] = {0.0f};
1521 
1522  if (use_original && node->bm_tot_ortri) {
1523  for (int i = 0; i < node->bm_tot_ortri; i++) {
1524  const int *t = node->bm_ortri[i];
1525  hit |= ray_face_intersection_tri(ray_start,
1526  isect_precalc,
1527  node->bm_orco[t[0]],
1528  node->bm_orco[t[1]],
1529  node->bm_orco[t[2]],
1530  depth);
1531  }
1532  }
1533  else {
1534  GSetIterator gs_iter;
1535 
1536  GSET_ITER (gs_iter, node->bm_faces) {
1537  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
1538 
1539  BLI_assert(f->len == 3);
1540  if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
1541  BMVert *v_tri[3];
1542 
1543  BM_face_as_array_vert_tri(f, v_tri);
1544 
1546  ray_start, isect_precalc, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co, depth)) {
1547  hit = true;
1548 
1549  if (r_face_normal) {
1550  normal_tri_v3(r_face_normal, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co);
1551  }
1552 
1553  if (r_active_vertex_index) {
1554  float location[3] = {0.0f};
1555  madd_v3_v3v3fl(location, ray_start, ray_normal, *depth);
1556  for (int j = 0; j < 3; j++) {
1557  if (len_squared_v3v3(location, v_tri[j]->co) <
1558  len_squared_v3v3(location, nearest_vertex_co)) {
1559  copy_v3_v3(nearest_vertex_co, v_tri[j]->co);
1560  *r_active_vertex_index = BM_elem_index_get(v_tri[j]);
1561  }
1562  }
1563  }
1564  }
1565  }
1566  }
1567  }
1568 
1569  return hit;
1570 }
1571 
1573  const float ray_start[3],
1574  struct IsectRayPrecalc *isect_precalc,
1575  float *depth,
1576  float *r_edge_length)
1577 {
1578  if (node->flag & PBVH_FullyHidden) {
1579  return 0;
1580  }
1581 
1582  GSetIterator gs_iter;
1583  bool hit = false;
1584  BMFace *f_hit = NULL;
1585 
1586  GSET_ITER (gs_iter, node->bm_faces) {
1587  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
1588 
1589  BLI_assert(f->len == 3);
1590  if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
1591  BMVert *v_tri[3];
1592  bool hit_local;
1593  BM_face_as_array_vert_tri(f, v_tri);
1594  hit_local = ray_face_intersection_tri(
1595  ray_start, isect_precalc, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co, depth);
1596 
1597  if (hit_local) {
1598  f_hit = f;
1599  hit = true;
1600  }
1601  }
1602  }
1603 
1604  if (hit) {
1605  BMVert *v_tri[3];
1606  BM_face_as_array_vert_tri(f_hit, v_tri);
1607  float len1 = len_squared_v3v3(v_tri[0]->co, v_tri[1]->co);
1608  float len2 = len_squared_v3v3(v_tri[1]->co, v_tri[2]->co);
1609  float len3 = len_squared_v3v3(v_tri[2]->co, v_tri[0]->co);
1610 
1611  /* detail returned will be set to the maximum allowed size, so take max here */
1612  *r_edge_length = sqrtf(max_fff(len1, len2, len3));
1613  }
1614 
1615  return hit;
1616 }
1617 
1619  const float ray_start[3],
1620  const float ray_normal[3],
1621  float *depth,
1622  float *dist_sq,
1623  bool use_original)
1624 {
1625  bool hit = false;
1626 
1627  if (use_original && node->bm_tot_ortri) {
1628  for (int i = 0; i < node->bm_tot_ortri; i++) {
1629  const int *t = node->bm_ortri[i];
1630  hit |= ray_face_nearest_tri(ray_start,
1631  ray_normal,
1632  node->bm_orco[t[0]],
1633  node->bm_orco[t[1]],
1634  node->bm_orco[t[2]],
1635  depth,
1636  dist_sq);
1637  }
1638  }
1639  else {
1640  GSetIterator gs_iter;
1641 
1642  GSET_ITER (gs_iter, node->bm_faces) {
1643  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
1644 
1645  BLI_assert(f->len == 3);
1646  if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
1647  BMVert *v_tri[3];
1648 
1649  BM_face_as_array_vert_tri(f, v_tri);
1650  hit |= ray_face_nearest_tri(
1651  ray_start, ray_normal, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co, depth, dist_sq);
1652  }
1653  }
1654  }
1655 
1656  return hit;
1657 }
1658 
1659 void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode)
1660 {
1661  for (int n = 0; n < totnode; n++) {
1662  PBVHNode *node = nodes[n];
1663 
1664  if (node->flag & PBVH_UpdateNormals) {
1665  GSetIterator gs_iter;
1666 
1667  GSET_ITER (gs_iter, node->bm_faces) {
1669  }
1670  GSET_ITER (gs_iter, node->bm_unique_verts) {
1672  }
1673  /* This should be unneeded normally */
1674  GSET_ITER (gs_iter, node->bm_other_verts) {
1676  }
1677  node->flag &= ~PBVH_UpdateNormals;
1678  }
1679  }
1680 }
1681 
1683  int totface; /* number of faces */
1684  int start; /* start of faces in array */
1687 };
1688 
1695  PBVH *pbvh, BMFace **nodeinfo, BBC *bbc_array, struct FastNodeBuildInfo *node, MemArena *arena)
1696 {
1697  struct FastNodeBuildInfo *child1, *child2;
1698 
1699  if (node->totface <= pbvh->leaf_limit) {
1700  return;
1701  }
1702 
1703  /* Calculate bounding box around primitive centroids */
1704  BB cb;
1705  BB_reset(&cb);
1706  for (int i = 0; i < node->totface; i++) {
1707  BMFace *f = nodeinfo[i + node->start];
1708  BBC *bbc = &bbc_array[BM_elem_index_get(f)];
1709 
1710  BB_expand(&cb, bbc->bcentroid);
1711  }
1712 
1713  /* initialize the children */
1714 
1715  /* Find widest axis and its midpoint */
1716  const int axis = BB_widest_axis(&cb);
1717  const float mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f;
1718 
1719  int num_child1 = 0, num_child2 = 0;
1720 
1721  /* split vertices along the middle line */
1722  const int end = node->start + node->totface;
1723  for (int i = node->start; i < end - num_child2; i++) {
1724  BMFace *f = nodeinfo[i];
1725  BBC *bbc = &bbc_array[BM_elem_index_get(f)];
1726 
1727  if (bbc->bcentroid[axis] > mid) {
1728  int i_iter = end - num_child2 - 1;
1729  int candidate = -1;
1730  /* found a face that should be part of another node, look for a face to substitute with */
1731 
1732  for (; i_iter > i; i_iter--) {
1733  BMFace *f_iter = nodeinfo[i_iter];
1734  const BBC *bbc_iter = &bbc_array[BM_elem_index_get(f_iter)];
1735  if (bbc_iter->bcentroid[axis] <= mid) {
1736  candidate = i_iter;
1737  break;
1738  }
1739 
1740  num_child2++;
1741  }
1742 
1743  if (candidate != -1) {
1744  BMFace *tmp = nodeinfo[i];
1745  nodeinfo[i] = nodeinfo[candidate];
1746  nodeinfo[candidate] = tmp;
1747  /* increase both counts */
1748  num_child1++;
1749  num_child2++;
1750  }
1751  else {
1752  /* not finding candidate means second half of array part is full of
1753  * second node parts, just increase the number of child nodes for it */
1754  num_child2++;
1755  }
1756  }
1757  else {
1758  num_child1++;
1759  }
1760  }
1761 
1762  /* ensure at least one child in each node */
1763  if (num_child2 == 0) {
1764  num_child2++;
1765  num_child1--;
1766  }
1767  else if (num_child1 == 0) {
1768  num_child1++;
1769  num_child2--;
1770  }
1771 
1772  /* at this point, faces should have been split along the array range sequentially,
1773  * each sequential part belonging to one node only */
1774  BLI_assert((num_child1 + num_child2) == node->totface);
1775 
1776  node->child1 = child1 = BLI_memarena_alloc(arena, sizeof(struct FastNodeBuildInfo));
1777  node->child2 = child2 = BLI_memarena_alloc(arena, sizeof(struct FastNodeBuildInfo));
1778 
1779  child1->totface = num_child1;
1780  child1->start = node->start;
1781  child2->totface = num_child2;
1782  child2->start = node->start + num_child1;
1784 
1785  pbvh_bmesh_node_limit_ensure_fast(pbvh, nodeinfo, bbc_array, child1, arena);
1786  pbvh_bmesh_node_limit_ensure_fast(pbvh, nodeinfo, bbc_array, child2, arena);
1787 }
1788 
1790  PBVH *pbvh, BMFace **nodeinfo, BBC *bbc_array, struct FastNodeBuildInfo *node, int node_index)
1791 {
1792  PBVHNode *n = pbvh->nodes + node_index;
1793  /* two cases, node does not have children or does have children */
1794  if (node->child1) {
1795  int children_offset = pbvh->totnode;
1796 
1797  n->children_offset = children_offset;
1798  pbvh_grow_nodes(pbvh, pbvh->totnode + 2);
1800  pbvh, nodeinfo, bbc_array, node->child1, children_offset);
1802  pbvh, nodeinfo, bbc_array, node->child2, children_offset + 1);
1803 
1804  n = &pbvh->nodes[node_index];
1805 
1806  /* Update bounding box */
1807  BB_reset(&n->vb);
1808  BB_expand_with_bb(&n->vb, &pbvh->nodes[n->children_offset].vb);
1809  BB_expand_with_bb(&n->vb, &pbvh->nodes[n->children_offset + 1].vb);
1810  n->orig_vb = n->vb;
1811  }
1812  else {
1813  /* node does not have children so it's a leaf node, populate with faces and tag accordingly
1814  * this is an expensive part but it's not so easily thread-able due to vertex node indices */
1815  const int cd_vert_node_offset = pbvh->cd_vert_node_offset;
1816  const int cd_face_node_offset = pbvh->cd_face_node_offset;
1817 
1818  bool has_visible = false;
1819 
1820  n->flag = PBVH_Leaf;
1821  n->bm_faces = BLI_gset_ptr_new_ex("bm_faces", node->totface);
1822 
1823  /* Create vert hash sets */
1824  n->bm_unique_verts = BLI_gset_ptr_new("bm_unique_verts");
1825  n->bm_other_verts = BLI_gset_ptr_new("bm_other_verts");
1826 
1827  BB_reset(&n->vb);
1828 
1829  const int end = node->start + node->totface;
1830 
1831  for (int i = node->start; i < end; i++) {
1832  BMFace *f = nodeinfo[i];
1833  BBC *bbc = &bbc_array[BM_elem_index_get(f)];
1834 
1835  /* Update ownership of faces */
1836  BLI_gset_insert(n->bm_faces, f);
1837  BM_ELEM_CD_SET_INT(f, cd_face_node_offset, node_index);
1838 
1839  /* Update vertices */
1840  BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
1841  BMLoop *l_iter = l_first;
1842  do {
1843  BMVert *v = l_iter->v;
1844  if (!BLI_gset_haskey(n->bm_unique_verts, v)) {
1845  if (BM_ELEM_CD_GET_INT(v, cd_vert_node_offset) != DYNTOPO_NODE_NONE) {
1847  }
1848  else {
1850  BM_ELEM_CD_SET_INT(v, cd_vert_node_offset, node_index);
1851  }
1852  }
1853  /* Update node bounding box */
1854  } while ((l_iter = l_iter->next) != l_first);
1855 
1856  if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
1857  has_visible = true;
1858  }
1859 
1860  BB_expand_with_bb(&n->vb, (BB *)bbc);
1861  }
1862 
1863  BLI_assert(n->vb.bmin[0] <= n->vb.bmax[0] && n->vb.bmin[1] <= n->vb.bmax[1] &&
1864  n->vb.bmin[2] <= n->vb.bmax[2]);
1865 
1866  n->orig_vb = n->vb;
1867 
1868  /* Build GPU buffers for new node and update vertex normals */
1870 
1871  BKE_pbvh_node_fully_hidden_set(n, !has_visible);
1872  n->flag |= PBVH_UpdateNormals;
1873  }
1874 }
1875 
1876 /***************************** Public API *****************************/
1877 
1878 /* Build a PBVH from a BMesh */
1880  BMesh *bm,
1881  bool smooth_shading,
1882  BMLog *log,
1883  const int cd_vert_node_offset,
1884  const int cd_face_node_offset)
1885 {
1886  pbvh->cd_vert_node_offset = cd_vert_node_offset;
1887  pbvh->cd_face_node_offset = cd_face_node_offset;
1888  pbvh->bm = bm;
1889 
1890  BKE_pbvh_bmesh_detail_size_set(pbvh, 0.75);
1891 
1892  pbvh->type = PBVH_BMESH;
1893  pbvh->bm_log = log;
1894 
1895  /* TODO: choose leaf limit better */
1896  pbvh->leaf_limit = 100;
1897 
1898  if (smooth_shading) {
1900  }
1901 
1902  /* bounding box array of all faces, no need to recalculate every time */
1903  BBC *bbc_array = MEM_mallocN(sizeof(BBC) * bm->totface, "BBC");
1904  BMFace **nodeinfo = MEM_mallocN(sizeof(*nodeinfo) * bm->totface, "nodeinfo");
1905  MemArena *arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "fast PBVH node storage");
1906 
1907  BMIter iter;
1908  BMFace *f;
1909  int i;
1910  BM_ITER_MESH_INDEX (f, &iter, bm, BM_FACES_OF_MESH, i) {
1911  BBC *bbc = &bbc_array[i];
1912  BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
1913  BMLoop *l_iter = l_first;
1914 
1915  BB_reset((BB *)bbc);
1916  do {
1917  BB_expand((BB *)bbc, l_iter->v->co);
1918  } while ((l_iter = l_iter->next) != l_first);
1919  BBC_update_centroid(bbc);
1920 
1921  /* so we can do direct lookups on 'bbc_array' */
1922  BM_elem_index_set(f, i); /* set_dirty! */
1923  nodeinfo[i] = f;
1924  BM_ELEM_CD_SET_INT(f, cd_face_node_offset, DYNTOPO_NODE_NONE);
1925  }
1926  /* Likely this is already dirty. */
1928 
1929  BMVert *v;
1930  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1931  BM_ELEM_CD_SET_INT(v, cd_vert_node_offset, DYNTOPO_NODE_NONE);
1932  }
1933 
1934  /* setup root node */
1935  struct FastNodeBuildInfo rootnode = {0};
1936  rootnode.totface = bm->totface;
1937 
1938  /* start recursion, assign faces to nodes accordingly */
1939  pbvh_bmesh_node_limit_ensure_fast(pbvh, nodeinfo, bbc_array, &rootnode, arena);
1940 
1941  /* We now have all faces assigned to a node,
1942  * next we need to assign those to the gsets of the nodes. */
1943 
1944  /* Start with all faces in the root node */
1945  pbvh->nodes = MEM_callocN(sizeof(PBVHNode), "PBVHNode");
1946  pbvh->totnode = 1;
1947 
1948  /* take root node and visit and populate children recursively */
1949  pbvh_bmesh_create_nodes_fast_recursive(pbvh, nodeinfo, bbc_array, &rootnode, 0);
1950 
1951  BLI_memarena_free(arena);
1952  MEM_freeN(bbc_array);
1953  MEM_freeN(nodeinfo);
1954 }
1955 
1956 /* Collapse short edges, subdivide long edges */
1959  const float center[3],
1960  const float view_normal[3],
1961  float radius,
1962  const bool use_frontface,
1963  const bool use_projected)
1964 {
1965  /* 2 is enough for edge faces - manifold edge */
1966  BLI_buffer_declare_static(BMLoop *, edge_loops, BLI_BUFFER_NOP, 2);
1967  BLI_buffer_declare_static(BMFace *, deleted_faces, BLI_BUFFER_NOP, 32);
1968  const int cd_vert_mask_offset = CustomData_get_offset(&pbvh->bm->vdata, CD_PAINT_MASK);
1969  const int cd_vert_node_offset = pbvh->cd_vert_node_offset;
1970  const int cd_face_node_offset = pbvh->cd_face_node_offset;
1971 
1972  bool modified = false;
1973 
1974  if (view_normal) {
1975  BLI_assert(len_squared_v3(view_normal) != 0.0f);
1976  }
1977 
1978  if (mode & PBVH_Collapse) {
1979  EdgeQueue q;
1980  BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert *) * 2, 0, 128, BLI_MEMPOOL_NOP);
1981  EdgeQueueContext eq_ctx = {
1982  &q,
1983  queue_pool,
1984  pbvh->bm,
1985  cd_vert_mask_offset,
1986  cd_vert_node_offset,
1987  cd_face_node_offset,
1988  };
1989 
1991  &eq_ctx, pbvh, center, view_normal, radius, use_frontface, use_projected);
1992  modified |= pbvh_bmesh_collapse_short_edges(&eq_ctx, pbvh, &deleted_faces);
1994  BLI_mempool_destroy(queue_pool);
1995  }
1996 
1997  if (mode & PBVH_Subdivide) {
1998  EdgeQueue q;
1999  BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert *) * 2, 0, 128, BLI_MEMPOOL_NOP);
2000  EdgeQueueContext eq_ctx = {
2001  &q,
2002  queue_pool,
2003  pbvh->bm,
2004  cd_vert_mask_offset,
2005  cd_vert_node_offset,
2006  cd_face_node_offset,
2007  };
2008 
2010  &eq_ctx, pbvh, center, view_normal, radius, use_frontface, use_projected);
2011  modified |= pbvh_bmesh_subdivide_long_edges(&eq_ctx, pbvh, &edge_loops);
2013  BLI_mempool_destroy(queue_pool);
2014  }
2015 
2016  /* Unmark nodes */
2017  for (int n = 0; n < pbvh->totnode; n++) {
2018  PBVHNode *node = &pbvh->nodes[n];
2019 
2020  if (node->flag & PBVH_Leaf && node->flag & PBVH_UpdateTopology) {
2021  node->flag &= ~PBVH_UpdateTopology;
2022  }
2023  }
2024  BLI_buffer_free(&edge_loops);
2025  BLI_buffer_free(&deleted_faces);
2026 
2027 #ifdef USE_VERIFY
2028  pbvh_bmesh_verify(pbvh);
2029 #endif
2030 
2031  return modified;
2032 }
2033 
2034 /* In order to perform operations on the original node coordinates
2035  * (currently just raycast), store the node's triangles and vertices.
2036  *
2037  * Skips triangles that are hidden. */
2039 {
2040  /* Skip if original coords/triangles are already saved */
2041  if (node->bm_orco) {
2042  return;
2043  }
2044 
2045  const int totvert = BLI_gset_len(node->bm_unique_verts) + BLI_gset_len(node->bm_other_verts);
2046 
2047  const int tottri = BLI_gset_len(node->bm_faces);
2048 
2049  node->bm_orco = MEM_mallocN(sizeof(*node->bm_orco) * totvert, __func__);
2050  node->bm_ortri = MEM_mallocN(sizeof(*node->bm_ortri) * tottri, __func__);
2051 
2052  /* Copy out the vertices and assign a temporary index */
2053  int i = 0;
2054  GSetIterator gs_iter;
2055  GSET_ITER (gs_iter, node->bm_unique_verts) {
2056  BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
2057  copy_v3_v3(node->bm_orco[i], v->co);
2058  BM_elem_index_set(v, i); /* set_dirty! */
2059  i++;
2060  }
2061  GSET_ITER (gs_iter, node->bm_other_verts) {
2062  BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
2063  copy_v3_v3(node->bm_orco[i], v->co);
2064  BM_elem_index_set(v, i); /* set_dirty! */
2065  i++;
2066  }
2067  /* Likely this is already dirty. */
2069 
2070  /* Copy the triangles */
2071  i = 0;
2072  GSET_ITER (gs_iter, node->bm_faces) {
2073  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
2074 
2076  continue;
2077  }
2078 
2079 #if 0
2080  BMIter bm_iter;
2081  BMVert *v;
2082  int j = 0;
2083  BM_ITER_ELEM (v, &bm_iter, f, BM_VERTS_OF_FACE) {
2084  node->bm_ortri[i][j] = BM_elem_index_get(v);
2085  j++;
2086  }
2087 #else
2088  bm_face_as_array_index_tri(f, node->bm_ortri[i]);
2089 #endif
2090  i++;
2091  }
2092  node->bm_tot_ortri = i;
2093 }
2094 
2096 {
2097  for (int i = 0; i < pbvh->totnode; i++) {
2098  PBVHNode *n = &pbvh->nodes[i];
2099  if (n->flag & PBVH_Leaf) {
2100  /* Free orco/ortri data */
2102 
2103  /* Recursively split nodes that have gotten too many
2104  * elements */
2106  }
2107  }
2108 }
2109 
2110 void BKE_pbvh_bmesh_detail_size_set(PBVH *pbvh, float detail_size)
2111 {
2112  pbvh->bm_max_edge_len = detail_size;
2113  pbvh->bm_min_edge_len = pbvh->bm_max_edge_len * 0.4f;
2114 }
2115 
2117 {
2118  node->flag |= PBVH_UpdateTopology;
2119 }
2120 
2122 {
2123  return node->bm_unique_verts;
2124 }
2125 
2127 {
2128  return node->bm_other_verts;
2129 }
2130 
2132 {
2133  return node->bm_faces;
2134 }
2135 
2136 /****************************** Debugging *****************************/
2137 
2138 #if 0
2139 
2140 static void pbvh_bmesh_print(PBVH *pbvh)
2141 {
2142  fprintf(stderr, "\npbvh=%p\n", pbvh);
2143  fprintf(stderr, "bm_face_to_node:\n");
2144 
2145  BMIter iter;
2146  BMFace *f;
2147  BM_ITER_MESH (f, &iter, pbvh->bm, BM_FACES_OF_MESH) {
2148  fprintf(stderr, " %d -> %d\n", BM_elem_index_get(f), pbvh_bmesh_node_index_from_face(pbvh, f));
2149  }
2150 
2151  fprintf(stderr, "bm_vert_to_node:\n");
2152  BMVert *v;
2153  BM_ITER_MESH (v, &iter, pbvh->bm, BM_FACES_OF_MESH) {
2154  fprintf(stderr, " %d -> %d\n", BM_elem_index_get(v), pbvh_bmesh_node_index_from_vert(pbvh, v));
2155  }
2156 
2157  for (int n = 0; n < pbvh->totnode; n++) {
2158  PBVHNode *node = &pbvh->nodes[n];
2159  if (!(node->flag & PBVH_Leaf)) {
2160  continue;
2161  }
2162 
2163  GSetIterator gs_iter;
2164  fprintf(stderr, "node %d\n faces:\n", n);
2165  GSET_ITER (gs_iter, node->bm_faces)
2166  fprintf(stderr, " %d\n", BM_elem_index_get((BMFace *)BLI_gsetIterator_getKey(&gs_iter)));
2167  fprintf(stderr, " unique verts:\n");
2168  GSET_ITER (gs_iter, node->bm_unique_verts)
2169  fprintf(stderr, " %d\n", BM_elem_index_get((BMVert *)BLI_gsetIterator_getKey(&gs_iter)));
2170  fprintf(stderr, " other verts:\n");
2171  GSET_ITER (gs_iter, node->bm_other_verts)
2172  fprintf(stderr, " %d\n", BM_elem_index_get((BMVert *)BLI_gsetIterator_getKey(&gs_iter)));
2173  }
2174 }
2175 
2176 static void print_flag_factors(int flag)
2177 {
2178  printf("flag=0x%x:\n", flag);
2179  for (int i = 0; i < 32; i++) {
2180  if (flag & (1 << i)) {
2181  printf(" %d (1 << %d)\n", 1 << i, i);
2182  }
2183  }
2184 }
2185 #endif
2186 
2187 #ifdef USE_VERIFY
2188 
2189 static void pbvh_bmesh_verify(PBVH *pbvh)
2190 {
2191  /* build list of faces & verts to lookup */
2192  GSet *faces_all = BLI_gset_ptr_new_ex(__func__, pbvh->bm->totface);
2193  BMIter iter;
2194 
2195  {
2196  BMFace *f;
2197  BM_ITER_MESH (f, &iter, pbvh->bm, BM_FACES_OF_MESH) {
2199  BLI_gset_insert(faces_all, f);
2200  }
2201  }
2202 
2203  GSet *verts_all = BLI_gset_ptr_new_ex(__func__, pbvh->bm->totvert);
2204  {
2205  BMVert *v;
2206  BM_ITER_MESH (v, &iter, pbvh->bm, BM_VERTS_OF_MESH) {
2208  BLI_gset_insert(verts_all, v);
2209  }
2210  }
2211  }
2212 
2213  /* Check vert/face counts */
2214  {
2215  int totface = 0, totvert = 0;
2216  for (int i = 0; i < pbvh->totnode; i++) {
2217  PBVHNode *n = &pbvh->nodes[i];
2218  totface += n->bm_faces ? BLI_gset_len(n->bm_faces) : 0;
2219  totvert += n->bm_unique_verts ? BLI_gset_len(n->bm_unique_verts) : 0;
2220  }
2221 
2222  BLI_assert(totface == BLI_gset_len(faces_all));
2223  BLI_assert(totvert == BLI_gset_len(verts_all));
2224  }
2225 
2226  {
2227  BMFace *f;
2228  BM_ITER_MESH (f, &iter, pbvh->bm, BM_FACES_OF_MESH) {
2229  BMIter bm_iter;
2230  BMVert *v;
2231  PBVHNode *n = pbvh_bmesh_node_lookup(pbvh, f);
2232 
2233  /* Check that the face's node is a leaf */
2234  BLI_assert(n->flag & PBVH_Leaf);
2235 
2236  /* Check that the face's node knows it owns the face */
2238 
2239  /* Check the face's vertices... */
2240  BM_ITER_ELEM (v, &bm_iter, f, BM_VERTS_OF_FACE) {
2241  PBVHNode *nv;
2242 
2243  /* Check that the vertex is in the node */
2245 
2246  /* Check that the vertex has a node owner */
2247  nv = pbvh_bmesh_node_lookup(pbvh, v);
2248 
2249  /* Check that the vertex's node knows it owns the vert */
2251 
2252  /* Check that the vertex isn't duplicated as an 'other' vert */
2254  }
2255  }
2256  }
2257 
2258  /* Check verts */
2259  {
2260  BMVert *v;
2261  BM_ITER_MESH (v, &iter, pbvh->bm, BM_VERTS_OF_MESH) {
2262  /* vertex isn't tracked */
2264  continue;
2265  }
2266 
2267  PBVHNode *n = pbvh_bmesh_node_lookup(pbvh, v);
2268 
2269  /* Check that the vert's node is a leaf */
2270  BLI_assert(n->flag & PBVH_Leaf);
2271 
2272  /* Check that the vert's node knows it owns the vert */
2274 
2275  /* Check that the vertex isn't duplicated as an 'other' vert */
2277 
2278  /* Check that the vert's node also contains one of the vert's
2279  * adjacent faces */
2280  bool found = false;
2281  BMIter bm_iter;
2282  BMFace *f = NULL;
2283  BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) {
2284  if (pbvh_bmesh_node_lookup(pbvh, f) == n) {
2285  found = true;
2286  break;
2287  }
2288  }
2289  BLI_assert(found || f == NULL);
2290 
2291 # if 1
2292  /* total freak stuff, check if node exists somewhere else */
2293  /* Slow */
2294  for (int i = 0; i < pbvh->totnode; i++) {
2295  PBVHNode *n_other = &pbvh->nodes[i];
2296  if ((n != n_other) && (n_other->bm_unique_verts)) {
2298  }
2299  }
2300 # endif
2301  }
2302  }
2303 
2304 # if 0
2305  /* check that every vert belongs somewhere */
2306  /* Slow */
2307  BM_ITER_MESH (vi, &iter, pbvh->bm, BM_VERTS_OF_MESH) {
2308  bool has_unique = false;
2309  for (int i = 0; i < pbvh->totnode; i++) {
2310  PBVHNode *n = &pbvh->nodes[i];
2311  if ((n->bm_unique_verts != NULL) && BLI_gset_haskey(n->bm_unique_verts, vi)) {
2312  has_unique = true;
2313  }
2314  }
2315  BLI_assert(has_unique);
2316  vert_count++;
2317  }
2318 
2319  /* if totvert differs from number of verts inside the hash. hash-totvert is checked above */
2320  BLI_assert(vert_count == pbvh->bm->totvert);
2321 # endif
2322 
2323  /* Check that node elements are recorded in the top level */
2324  for (int i = 0; i < pbvh->totnode; i++) {
2325  PBVHNode *n = &pbvh->nodes[i];
2326  if (n->flag & PBVH_Leaf) {
2327  GSetIterator gs_iter;
2328 
2329  GSET_ITER (gs_iter, n->bm_faces) {
2330  BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
2331  PBVHNode *n_other = pbvh_bmesh_node_lookup(pbvh, f);
2332  BLI_assert(n == n_other);
2333  BLI_assert(BLI_gset_haskey(faces_all, f));
2334  }
2335 
2336  GSET_ITER (gs_iter, n->bm_unique_verts) {
2337  BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
2338  PBVHNode *n_other = pbvh_bmesh_node_lookup(pbvh, v);
2340  BLI_assert(n == n_other);
2341  BLI_assert(BLI_gset_haskey(verts_all, v));
2342  }
2343 
2344  GSET_ITER (gs_iter, n->bm_other_verts) {
2345  BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
2346  /* this happens sometimes and seems harmless */
2347  // BLI_assert(!BM_vert_face_check(v));
2348  BLI_assert(BLI_gset_haskey(verts_all, v));
2349  }
2350  }
2351  }
2352 
2353  BLI_gset_free(faces_all, NULL);
2354  BLI_gset_free(verts_all, NULL);
2355 }
2356 
2357 #endif
void CustomData_bmesh_set_default(struct CustomData *data, void **block)
Definition: customdata.c:3703
int CustomData_get_offset(const struct CustomData *data, int type)
A BVH for high poly meshes.
void BKE_pbvh_node_mark_rebuild_draw(PBVHNode *node)
Definition: pbvh.c:1754
PBVHTopologyUpdateMode
Definition: BKE_pbvh.h:243
@ PBVH_Collapse
Definition: BKE_pbvh.h:245
@ PBVH_Subdivide
Definition: BKE_pbvh.h:244
@ PBVH_BMESH
Definition: BKE_pbvh.h:212
void BKE_pbvh_node_fully_hidden_set(PBVHNode *node, int fully_hidden)
Definition: pbvh.c:1769
@ PBVH_UpdateDrawBuffers
Definition: BKE_pbvh.h:69
@ PBVH_UpdateNormals
Definition: BKE_pbvh.h:66
@ PBVH_UpdateTopology
Definition: BKE_pbvh.h:79
@ PBVH_FullyHidden
Definition: BKE_pbvh.h:75
@ PBVH_UpdateBB
Definition: BKE_pbvh.h:67
@ PBVH_Leaf
Definition: BKE_pbvh.h:64
#define BLI_assert(a)
Definition: BLI_assert.h:58
@ BLI_BUFFER_NOP
Definition: BLI_buffer.h:35
#define BLI_buffer_append(buffer_, type_, val_)
Definition: BLI_buffer.h:64
#define BLI_buffer_at(buffer_, type_, index_)
Definition: BLI_buffer.h:50
#define BLI_buffer_declare_static(type_, name_, flag_, static_count_)
Definition: BLI_buffer.h:39
void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
Definition: buffer.c:94
#define BLI_buffer_clear(buffer_)
Definition: BLI_buffer.h:68
#define BLI_buffer_free(name_)
Definition: BLI_buffer.h:92
#define BLI_INLINE
#define GSET_ITER_INDEX(gs_iter_, gset_, i_)
Definition: BLI_ghash.h:272
struct GSet GSet
Definition: BLI_ghash.h:189
unsigned int BLI_gset_len(GSet *gs) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1138
GSet * BLI_gset_ptr_new(const char *info)
void BLI_gset_insert(GSet *gs, void *key)
Definition: BLI_ghash.c:1147
#define GSET_ITER(gs_iter_, gset_)
Definition: BLI_ghash.h:268
bool BLI_gset_haskey(GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1216
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
GSet * BLI_gset_ptr_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void ** BLI_ghash_lookup_p(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:830
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
GHash * BLI_ghash_ptr_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
BLI_INLINE void * BLI_gsetIterator_getKey(GSetIterator *gsi)
Definition: BLI_ghash.h:255
bool BLI_gset_add(GSet *gs, void *key)
Definition: BLI_ghash.c:1160
bool BLI_gset_remove(GSet *gs, const void *key, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1211
A min-heap / priority queue ADT.
void BLI_heapsimple_free(HeapSimple *heap, HeapSimpleFreeFP ptrfreefp) ATTR_NONNULL(1)
HeapSimple * BLI_heapsimple_new(void) ATTR_WARN_UNUSED_RESULT
void * BLI_heapsimple_pop_min(HeapSimple *heap) ATTR_NONNULL(1)
bool BLI_heapsimple_is_empty(const HeapSimple *heap) ATTR_NONNULL(1)
void BLI_heapsimple_insert(HeapSimple *heap, float value, void *ptr) ATTR_NONNULL(1)
MINLINE float max_fff(float a, float b, float c)
MINLINE float max_ff(float a, float b)
MINLINE float square_f(float a)
void closest_on_tri_to_point_v3(float r[3], const float p[3], const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:1023
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 float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
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 copy_v3_v3(float r[3], const float a[3])
void project_plane_normalized_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
Definition: math_vector.c:757
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 add_v3_v3(float r[3], const float a[3])
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:109
#define BLI_MEMARENA_STD_BUFSIZE
Definition: BLI_memarena.h:36
void * BLI_memarena_alloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
Definition: BLI_memarena.c:131
struct MemArena * BLI_memarena_new(const size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2) ATTR_MALLOC
Definition: BLI_memarena.c:79
@ BLI_MEMPOOL_NOP
Definition: BLI_mempool.h:77
void BLI_mempool_free(BLI_mempool *pool, void *addr) ATTR_NONNULL(1
BLI_mempool * BLI_mempool_create(unsigned int esize, unsigned int totelem, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_mempool.c:268
void * BLI_mempool_alloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: BLI_mempool.c:334
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
Definition: BLI_mempool.c:757
#define ARRAY_SIZE(arr)
#define UNUSED_VARS(...)
#define UNLIKELY(x)
#define ELEM(...)
#define LIKELY(x)
@ CD_PAINT_MASK
#define DYNTOPO_NODE_NONE
NSNotificationCenter * center
void GPU_pbvh_buffers_free(GPU_PBVH_Buffers *buffers)
Definition: gpu_buffers.c:1110
_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 GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
_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 BM_ELEM_CD_SET_FLOAT(ele, offset, f)
Definition: bmesh_class.h:534
#define BM_ELEM_CD_GET_FLOAT(ele, offset)
Definition: bmesh_class.h:542
#define BM_ELEM_CD_GET_INT(ele, offset)
Definition: bmesh_class.h:518
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
#define BM_ELEM_CD_SET_INT(ele, offset, f)
Definition: bmesh_class.h:510
@ BM_ELEM_HIDDEN
Definition: bmesh_class.h:472
#define BM_FACE_FIRST_LOOP(p)
Definition: bmesh_class.h:553
BMVert * BM_vert_create(BMesh *bm, const float co[3], const BMVert *v_example, const eBMCreateFlag create_flag)
Main function for creating a new vertex.
Definition: bmesh_core.c:58
void BM_vert_kill(BMesh *bm, BMVert *v)
Definition: bmesh_core.c:1002
void BM_face_kill(BMesh *bm, BMFace *f)
Definition: bmesh_core.c:881
void BM_edge_kill(BMesh *bm, BMEdge *e)
Definition: bmesh_core.c:987
BMFace * BM_face_create(BMesh *bm, BMVert **verts, BMEdge **edges, const int len, const BMFace *f_example, const eBMCreateFlag create_flag)
Definition: bmesh_core.c:428
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
Definition: bmesh_core.c:147
@ BM_CREATE_NOP
Definition: bmesh_core.h:27
@ BM_CREATE_SKIP_CD
Definition: bmesh_core.h:33
@ BM_CREATE_NO_DOUBLE
Definition: bmesh_core.h:29
#define BM_elem_index_get(ele)
Definition: bmesh_inline.h:124
#define BM_elem_index_set(ele, index)
Definition: bmesh_inline.h:125
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:26
#define BM_elem_flag_test_bool(ele, hflag)
Definition: bmesh_inline.h:27
int BM_iter_as_array(BMesh *bm, const char itype, void *data, void **array, const int len)
Iterator as Array.
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
@ BM_FACES_OF_VERT
@ BM_VERTS_OF_MESH
@ BM_VERTS_OF_FACE
@ BM_FACES_OF_MESH
@ BM_LOOPS_OF_EDGE
@ BM_EDGES_OF_VERT
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_log_face_added(BMLog *log, BMFace *f)
Definition: bmesh_log.c:894
void BM_log_face_removed(BMLog *log, BMFace *f)
Definition: bmesh_log.c:965
void BM_log_vert_added(BMLog *log, BMVert *v, const int cd_vert_mask_offset)
Definition: bmesh_log.c:862
void BM_log_vert_removed(BMLog *log, BMVert *v, const int cd_vert_mask_offset)
Definition: bmesh_log.c:924
void BM_log_vert_before_modified(BMLog *log, BMVert *v, const int cd_vert_mask_offset)
Definition: bmesh_log.c:838
void BM_vert_normal_update(BMVert *v)
void BM_face_normal_update(BMFace *f)
void BM_face_as_array_vert_tri(BMFace *f, BMVert *r_verts[3])
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
Definition: bmesh_query.c:1995
bool BM_edge_loop_pair(BMEdge *e, BMLoop **r_la, BMLoop **r_lb)
Definition: bmesh_query.c:753
float BM_edge_calc_length_squared(const BMEdge *e)
Definition: bmesh_query.c:721
int BM_edge_face_count(const BMEdge *e)
Definition: bmesh_query.c:847
BMFace * BM_face_exists(BMVert **varr, int len)
Definition: bmesh_query.c:2070
bool BM_vert_face_check(const BMVert *v)
Definition: bmesh_query.c:901
#define BM_vert_face_count_is_equal(v, n)
Definition: bmesh_query.h:101
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
#define BM_vert_edge_count_is_over(v, n)
Definition: bmesh_query.h:92
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
OperationNode * node
static float verts[][3]
int count
#define sqrtf(x)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
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 unsigned c
Definition: RandGen.cpp:97
INLINE Rall1d< T, V, S > log(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:303
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
void BB_expand_with_bb(BB *bb, BB *bb2)
Definition: pbvh.c:91
int BB_widest_axis(const BB *bb)
Definition: pbvh.c:100
void pbvh_grow_nodes(PBVH *pbvh, int totnode)
Definition: pbvh.c:239
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 BBC_update_centroid(BBC *bbc)
Definition: pbvh.c:123
void BB_reset(BB *bb)
Definition: pbvh.c:75
void BB_expand(BB *bb, const float co[3])
Definition: pbvh.c:82
BLI_INLINE int pbvh_bmesh_node_index_from_vert(PBVH *pbvh, const BMVert *key)
Definition: pbvh_bmesh.c:462
BLI_INLINE int pbvh_bmesh_node_index_from_face(PBVH *pbvh, const BMFace *key)
Definition: pbvh_bmesh.c:470
#define BM_LOOPS_OF_VERT_ITER_END
Definition: pbvh_bmesh.c:96
#define BM_FACES_OF_VERT_ITER_BEGIN(f_iter_, v_)
Definition: pbvh_bmesh.c:109
void BKE_pbvh_bmesh_node_save_orig(BMesh *bm, PBVHNode *node)
Definition: pbvh_bmesh.c:2038
static void long_edge_queue_edge_add_recursive(EdgeQueueContext *eq_ctx, BMLoop *l_edge, BMLoop *l_end, const float len_sq, float limit_len)
Definition: pbvh_bmesh.c:873
static bool pbvh_bmesh_node_limit_ensure(PBVH *pbvh, int node_index)
Definition: pbvh_bmesh.c:385
static void edge_queue_insert(EdgeQueueContext *eq_ctx, BMEdge *e, float priority)
Definition: pbvh_bmesh.c:836
static void pbvh_bmesh_collapse_edge(PBVH *pbvh, BMEdge *e, BMVert *v1, BMVert *v2, GHash *deleted_verts, BLI_Buffer *deleted_faces, EdgeQueueContext *eq_ctx)
Definition: pbvh_bmesh.c:1288
#define BM_LOOPS_OF_VERT_ITER_BEGIN(l_iter_radial_, v_)
Definition: pbvh_bmesh.c:79
static BMFace * bm_face_exists_tri_from_loop_vert(BMLoop *l_radial_first, BMVert *v_opposite)
Definition: pbvh_bmesh.c:158
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
static void pbvh_bmesh_node_split(PBVH *pbvh, const BBC *bbc_array, int node_index)
Definition: pbvh_bmesh.c:259
GSet * BKE_pbvh_bmesh_node_unique_verts(PBVHNode *node)
Definition: pbvh_bmesh.c:2121
static void pbvh_bmesh_split_edge(EdgeQueueContext *eq_ctx, PBVH *pbvh, BMEdge *e, BLI_Buffer *edge_loops)
Definition: pbvh_bmesh.c:1117
GSet * BKE_pbvh_bmesh_node_other_verts(PBVHNode *node)
Definition: pbvh_bmesh.c:2126
BLI_INLINE PBVHNode * pbvh_bmesh_node_from_face(PBVH *pbvh, const BMFace *key)
Definition: pbvh_bmesh.c:483
static void long_edge_queue_face_add(EdgeQueueContext *eq_ctx, BMFace *f)
Definition: pbvh_bmesh.c:943
static void pbvh_bmesh_node_limit_ensure_fast(PBVH *pbvh, BMFace **nodeinfo, BBC *bbc_array, struct FastNodeBuildInfo *node, MemArena *arena)
Definition: pbvh_bmesh.c:1694
static bool pbvh_bmesh_collapse_short_edges(EdgeQueueContext *eq_ctx, PBVH *pbvh, BLI_Buffer *deleted_faces)
Definition: pbvh_bmesh.c:1455
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
static void long_edge_queue_edge_add(EdgeQueueContext *eq_ctx, BMEdge *e)
Definition: pbvh_bmesh.c:859
static void pbvh_bmesh_edge_loops(BLI_Buffer *buf, BMEdge *e)
Definition: pbvh_bmesh.c:698
static void pbvh_bmesh_node_drop_orig(PBVHNode *node)
Definition: pbvh_bmesh.c:714
bool BKE_pbvh_bmesh_update_topology(PBVH *pbvh, PBVHTopologyUpdateMode mode, const float center[3], const float view_normal[3], float radius, const bool use_frontface, const bool use_projected)
Definition: pbvh_bmesh.c:1957
#define EVEN_GENERATION_SCALE
static void long_edge_queue_create(EdgeQueueContext *eq_ctx, PBVH *pbvh, const float center[3], const float view_normal[3], float radius, const bool use_frontface, const bool use_projected)
Definition: pbvh_bmesh.c:1002
static BMVert * bm_vert_hash_lookup_chain(GHash *deleted_verts, BMVert *v)
Definition: pbvh_bmesh.c:178
#define EVEN_EDGELEN_THRESHOLD
static void short_edge_queue_face_add(EdgeQueueContext *eq_ctx, BMFace *f)
Definition: pbvh_bmesh.c:971
BLI_INLINE PBVHNode * pbvh_bmesh_node_from_vert(PBVH *pbvh, const BMVert *key)
Definition: pbvh_bmesh.c:478
static void pbvh_bmesh_vert_ownership_transfer(PBVH *pbvh, PBVHNode *new_owner, BMVert *v)
Definition: pbvh_bmesh.c:605
void BKE_pbvh_build_bmesh(PBVH *pbvh, BMesh *bm, bool smooth_shading, BMLog *log, const int cd_vert_node_offset, const int cd_face_node_offset)
Definition: pbvh_bmesh.c:1879
static bool edge_queue_tri_in_sphere(const EdgeQueue *q, BMFace *f)
Definition: pbvh_bmesh.c:797
static BMFace * pbvh_bmesh_face_create(PBVH *pbvh, int node_index, BMVert *v_tri[3], BMEdge *e_tri[3], const BMFace *f_example)
Definition: pbvh_bmesh.c:519
static void pbvh_bmesh_face_remove(PBVH *pbvh, BMFace *f)
Definition: pbvh_bmesh.c:658
static void short_edge_queue_create(EdgeQueueContext *eq_ctx, PBVH *pbvh, const float center[3], const float view_normal[3], float radius, const bool use_frontface, const bool use_projected)
Definition: pbvh_bmesh.c:1065
static void pbvh_bmesh_create_nodes_fast_recursive(PBVH *pbvh, BMFace **nodeinfo, BBC *bbc_array, struct FastNodeBuildInfo *node, int node_index)
Definition: pbvh_bmesh.c:1789
static bool check_mask(EdgeQueueContext *eq_ctx, BMVert *v)
Definition: pbvh_bmesh.c:831
bool BKE_pbvh_bmesh_node_raycast_detail(PBVHNode *node, const float ray_start[3], struct IsectRayPrecalc *isect_precalc, float *depth, float *r_edge_length)
Definition: pbvh_bmesh.c:1572
struct EdgeQueue EdgeQueue
#define pbvh_bmesh_node_vert_use_count_is_equal(pbvh, node, v, n)
Definition: pbvh_bmesh.c:562
#define BM_FACES_OF_VERT_ITER_END
Definition: pbvh_bmesh.c:115
struct GSet * BKE_pbvh_bmesh_node_faces(PBVHNode *node)
Definition: pbvh_bmesh.c:2131
static bool edge_queue_tri_in_circle(const EdgeQueue *q, BMFace *f)
Definition: pbvh_bmesh.c:811
static void pbvh_bmesh_vert_remove(PBVH *pbvh, BMVert *v)
Definition: pbvh_bmesh.c:626
static void bm_edges_from_tri(BMesh *bm, BMVert *v_tri[3], BMEdge *e_tri[3])
Definition: pbvh_bmesh.c:121
#define EDGE_QUEUE_DISABLE(e)
Definition: pbvh_bmesh.c:763
#define EDGE_QUEUE_ENABLE(e)
Definition: pbvh_bmesh.c:761
void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode)
Definition: pbvh_bmesh.c:1659
void BKE_pbvh_bmesh_after_stroke(PBVH *pbvh)
Definition: pbvh_bmesh.c:2095
static PBVHNode * pbvh_bmesh_vert_other_node_find(PBVH *pbvh, BMVert *v)
Definition: pbvh_bmesh.c:588
static void pbvh_bmesh_node_finalize(PBVH *pbvh, const int node_index, const int cd_vert_node_offset, const int cd_face_node_offset)
Definition: pbvh_bmesh.c:201
#define EDGE_QUEUE_TEST(e)
Definition: pbvh_bmesh.c:760
static int pbvh_bmesh_node_vert_use_count_at_most(PBVH *pbvh, PBVHNode *node, BMVert *v, const int count_max)
Definition: pbvh_bmesh.c:565
static void short_edge_queue_edge_add(EdgeQueueContext *eq_ctx, BMEdge *e)
Definition: pbvh_bmesh.c:930
void BKE_pbvh_bmesh_detail_size_set(PBVH *pbvh, float detail_size)
Definition: pbvh_bmesh.c:2110
void BKE_pbvh_node_mark_topology_update(PBVHNode *node)
Definition: pbvh_bmesh.c:2116
static BMVert * pbvh_bmesh_vert_create(PBVH *pbvh, int node_index, const float co[3], const float no[3], const int cd_vert_mask_offset)
Definition: pbvh_bmesh.c:488
BLI_INLINE void bm_face_as_array_index_tri(BMFace *f, int r_index[3])
Definition: pbvh_bmesh.c:128
static bool pbvh_bmesh_subdivide_long_edges(EdgeQueueContext *eq_ctx, PBVH *pbvh, BLI_Buffer *edge_loops)
Definition: pbvh_bmesh.c:1235
@ PBVH_DYNTOPO_SMOOTH_SHADING
Definition: pbvh_intern.h:113
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
void * data
Definition: BLI_buffer.h:28
size_t alloc_count
Definition: BLI_buffer.h:30
size_t count
Definition: BLI_buffer.h:30
BMHeader head
Definition: bmesh_class.h:255
int len
Definition: bmesh_class.h:279
BMHeader head
Definition: bmesh_class.h:267
float no[3]
Definition: bmesh_class.h:280
char htype
Definition: bmesh_class.h:76
char hflag
Definition: bmesh_class.h:78
void * data
Definition: bmesh_class.h:63
struct BMVert * v
Definition: bmesh_class.h:165
struct BMEdge * e
Definition: bmesh_class.h:176
struct BMLoop * radial_next
Definition: bmesh_class.h:216
struct BMLoop * prev
Definition: bmesh_class.h:245
struct BMFace * f
Definition: bmesh_class.h:183
struct BMLoop * next
Definition: bmesh_class.h:245
float co[3]
Definition: bmesh_class.h:99
float no[3]
Definition: bmesh_class.h:100
BMHeader head
Definition: bmesh_class.h:97
int totvert
Definition: bmesh_class.h:297
char elem_index_dirty
Definition: bmesh_class.h:305
CustomData vdata
Definition: bmesh_class.h:337
int totface
Definition: bmesh_class.h:297
BLI_mempool * pool
Definition: pbvh_bmesh.c:751
EdgeQueue * q
Definition: pbvh_bmesh.c:750
unsigned int use_view_normal
Definition: pbvh_bmesh.c:745
bool(* edge_queue_tri_in_range)(const struct EdgeQueue *q, BMFace *f)
Definition: pbvh_bmesh.c:741
const float * view_normal
Definition: pbvh_bmesh.c:743
const float * center
Definition: pbvh_bmesh.c:733
float radius_squared
Definition: pbvh_bmesh.c:735
float limit_len
Definition: pbvh_bmesh.c:738
float center_proj[3]
Definition: pbvh_bmesh.c:734
float limit_len_squared
Definition: pbvh_bmesh.c:736
HeapSimple * heap
Definition: pbvh_bmesh.c:732
struct FastNodeBuildInfo * child2
Definition: pbvh_bmesh.c:1686
struct FastNodeBuildInfo * child1
Definition: pbvh_bmesh.c:1685
GSet * bm_faces
Definition: pbvh_intern.h:101
struct GPU_PBVH_Buffers * draw_buffers
Definition: pbvh_intern.h:37
GSet * bm_unique_verts
Definition: pbvh_intern.h:102
BB orig_vb
Definition: pbvh_intern.h:41
float * layer_disp
Definition: pbvh_intern.h:95
int children_offset
Definition: pbvh_intern.h:45
PBVHNodeFlags flag
Definition: pbvh_intern.h:89
GSet * bm_other_verts
Definition: pbvh_intern.h:103
PBVHType type
Definition: pbvh_intern.h:119
int cd_face_node_offset
Definition: pbvh_intern.h:172
struct BMLog * bm_log
Definition: pbvh_intern.h:177
float bm_min_edge_len
Definition: pbvh_intern.h:170
int leaf_limit
Definition: pbvh_intern.h:129
PBVHFlags flags
Definition: pbvh_intern.h:120
float bm_max_edge_len
Definition: pbvh_intern.h:169
BMesh * bm
Definition: pbvh_intern.h:168
int totnode
Definition: pbvh_intern.h:123
int cd_vert_node_offset
Definition: pbvh_intern.h:171
PBVHNode * nodes
Definition: pbvh_intern.h:122
#define G(x, y, z)