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