2
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18
* ***** END GPL LICENSE BLOCK *****
21
#include "MEM_guardedalloc.h"
23
#include "BLI_buffer.h"
24
#include "BLI_ghash.h"
29
#include "BKE_DerivedMesh.h"
30
#include "BKE_global.h"
31
#include "BKE_paint.h"
34
#include "GPU_buffers.h"
37
#include "pbvh_intern.h"
41
/****************************** Building ******************************/
43
/* Update node data after splitting */
44
static void pbvh_bmesh_node_finalize(PBVH *bvh, int node_index)
46
GHashIterator gh_iter;
47
PBVHNode *n = &bvh->nodes[node_index];
49
/* Create vert hash sets */
50
n->bm_unique_verts = BLI_ghash_ptr_new("bm_unique_verts");
51
n->bm_other_verts = BLI_ghash_ptr_new("bm_other_verts");
55
GHASH_ITER (gh_iter, n->bm_faces) {
56
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
60
void *node_val = SET_INT_IN_POINTER(node_index);
62
/* Update ownership of faces */
63
BLI_ghash_insert(bvh->bm_face_to_node, f, node_val);
66
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
69
if (!BLI_ghash_haskey(n->bm_unique_verts, v)) {
70
if (BLI_ghash_haskey(bvh->bm_vert_to_node, v)) {
71
if (!BLI_ghash_haskey(n->bm_other_verts, v))
72
BLI_ghash_insert(n->bm_other_verts, v, NULL);
75
BLI_ghash_insert(n->bm_unique_verts, v, NULL);
76
BLI_ghash_insert(bvh->bm_vert_to_node, v, node_val);
79
/* Update node bounding box */
80
BB_expand(&n->vb, v->co);
81
} while ((l_iter = l_iter->next) != l_first);
84
BLI_assert(n->vb.bmin[0] <= n->vb.bmax[0] &&
85
n->vb.bmin[1] <= n->vb.bmax[1] &&
86
n->vb.bmin[2] <= n->vb.bmax[2]);
90
/* Build GPU buffers */
92
int smooth = bvh->flags & PBVH_DYNTOPO_SMOOTH_SHADING;
93
n->draw_buffers = GPU_build_bmesh_buffers(smooth);
94
n->flag |= PBVH_UpdateDrawBuffers;
98
/* Recursively split the node if it exceeds the leaf_limit */
99
static void pbvh_bmesh_node_split(PBVH *bvh, GHash *prim_bbc, int node_index)
101
GHash *empty, *other;
102
GHashIterator gh_iter;
103
PBVHNode *n, *c1, *c2;
108
n = &bvh->nodes[node_index];
110
if (BLI_ghash_size(n->bm_faces) <= bvh->leaf_limit) {
111
/* Node limit not exceeded */
112
pbvh_bmesh_node_finalize(bvh, node_index);
116
/* Calculate bounding box around primitive centroids */
118
GHASH_ITER (gh_iter, n->bm_faces) {
119
const BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
120
const BBC *bbc = BLI_ghash_lookup(prim_bbc, f);
122
BB_expand(&cb, bbc->bcentroid);
125
/* Find widest axis and its midpoint */
126
axis = BB_widest_axis(&cb);
127
mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f;
129
/* Add two new child nodes */
130
children = bvh->totnode;
131
n->children_offset = children;
132
pbvh_grow_nodes(bvh, bvh->totnode + 2);
134
/* Array reallocated, update current node pointer */
135
n = &bvh->nodes[node_index];
137
/* Initialize children */
138
c1 = &bvh->nodes[children];
139
c2 = &bvh->nodes[children + 1];
140
c1->flag |= PBVH_Leaf;
141
c2->flag |= PBVH_Leaf;
142
c1->bm_faces = BLI_ghash_ptr_new("bm_faces");
143
c2->bm_faces = BLI_ghash_ptr_new("bm_faces");
145
/* Partition the parent node's faces between the two children */
146
GHASH_ITER (gh_iter, n->bm_faces) {
147
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
148
const BBC *bbc = BLI_ghash_lookup(prim_bbc, f);
150
if (bbc->bcentroid[axis] < mid)
151
BLI_ghash_insert(c1->bm_faces, f, NULL);
153
BLI_ghash_insert(c2->bm_faces, f, NULL);
156
/* Enforce at least one primitive in each node */
158
if (BLI_ghash_size(c1->bm_faces) == 0) {
159
empty = c1->bm_faces;
160
other = c2->bm_faces;
162
else if (BLI_ghash_size(c2->bm_faces) == 0) {
163
empty = c2->bm_faces;
164
other = c1->bm_faces;
167
GHASH_ITER (gh_iter, other) {
168
void *key = BLI_ghashIterator_getKey(&gh_iter);
169
BLI_ghash_insert(empty, key, NULL);
170
BLI_ghash_remove(other, key, NULL, NULL);
175
/* Clear this node */
177
/* Mark this node's unique verts as unclaimed */
178
if (n->bm_unique_verts) {
179
GHASH_ITER (gh_iter, n->bm_unique_verts) {
180
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
181
BLI_ghash_remove(bvh->bm_vert_to_node, v, NULL, NULL);
183
BLI_ghash_free(n->bm_unique_verts, NULL, NULL);
187
GHASH_ITER (gh_iter, n->bm_faces) {
188
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
189
BLI_ghash_remove(bvh->bm_face_to_node, f, NULL, NULL);
191
BLI_ghash_free(n->bm_faces, NULL, NULL);
193
if (n->bm_other_verts)
194
BLI_ghash_free(n->bm_other_verts, NULL, NULL);
197
MEM_freeN(n->layer_disp);
200
n->bm_unique_verts = NULL;
201
n->bm_other_verts = NULL;
202
n->layer_disp = NULL;
204
if (n->draw_buffers) {
205
GPU_free_buffers(n->draw_buffers);
206
n->draw_buffers = NULL;
208
n->flag &= ~PBVH_Leaf;
212
pbvh_bmesh_node_split(bvh, prim_bbc, children);
213
pbvh_bmesh_node_split(bvh, prim_bbc, children + 1);
215
/* Array maybe reallocated, update current node pointer */
216
n = &bvh->nodes[node_index];
218
/* Update bounding box */
220
BB_expand_with_bb(&n->vb, &bvh->nodes[n->children_offset].vb);
221
BB_expand_with_bb(&n->vb, &bvh->nodes[n->children_offset + 1].vb);
225
/* Recursively split the node if it exceeds the leaf_limit */
226
static int pbvh_bmesh_node_limit_ensure(PBVH *bvh, int node_index)
229
GHashIterator gh_iter;
231
if (BLI_ghash_size(bvh->nodes[node_index].bm_faces) <= bvh->leaf_limit) {
232
/* Node limit not exceeded */
236
/* For each BMFace, store the AABB and AABB centroid */
237
prim_bbc = BLI_ghash_ptr_new("prim_bbc");
239
GHASH_ITER (gh_iter, bvh->nodes[node_index].bm_faces) {
240
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
241
BBC *bbc = MEM_callocN(sizeof(BBC), "BBC");
246
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
248
BB_expand((BB *)bbc, l_iter->v->co);
249
} while ((l_iter = l_iter->next) != l_first);
250
BBC_update_centroid(bbc);
252
BLI_ghash_insert(prim_bbc, f, bbc);
255
pbvh_bmesh_node_split(bvh, prim_bbc, node_index);
257
BLI_ghash_free(prim_bbc, NULL, (void *)MEM_freeN);
262
/**********************************************************************/
264
static PBVHNode *pbvh_bmesh_node_lookup(PBVH *bvh, GHash *map, void *key)
268
BLI_assert(BLI_ghash_haskey(map, key));
270
node_index = GET_INT_FROM_POINTER(BLI_ghash_lookup(map, key));
271
BLI_assert(node_index < bvh->totnode);
273
return &bvh->nodes[node_index];
276
static BMVert *pbvh_bmesh_vert_create(PBVH *bvh, int node_index,
278
const BMVert *example)
280
BMVert *v = BM_vert_create(bvh->bm, co, example, 0);
281
void *val = SET_INT_IN_POINTER(node_index);
283
BLI_assert((bvh->totnode == 1 || node_index) && node_index <= bvh->totnode);
285
BLI_ghash_insert(bvh->nodes[node_index].bm_unique_verts, v, NULL);
286
BLI_ghash_insert(bvh->bm_vert_to_node, v, val);
288
/* Log the new vertex */
289
BM_log_vert_added(bvh->bm, bvh->bm_log, v);
294
static BMFace *pbvh_bmesh_face_create(PBVH *bvh, int node_index,
295
BMVert *v1, BMVert *v2, BMVert *v3,
296
const BMFace *UNUSED(example))
299
void *val = SET_INT_IN_POINTER(node_index);
301
/* Note: passing NULL for the 'example' parameter, profiling shows
302
* a small performance bump */
303
f = BM_face_create_quad_tri(bvh->bm, v1, v2, v3, NULL, NULL, true);
304
if (!BLI_ghash_haskey(bvh->bm_face_to_node, f)) {
306
BLI_ghash_insert(bvh->nodes[node_index].bm_faces, f, NULL);
307
BLI_ghash_insert(bvh->bm_face_to_node, f, val);
309
/* Log the new face */
310
BM_log_face_added(bvh->bm_log, f);
316
/* Return the number of faces in 'node' that use vertex 'v' */
317
static int pbvh_bmesh_node_vert_use_count(PBVH *bvh, PBVHNode *node, BMVert *v)
323
BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) {
326
f_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_face_to_node, f);
335
/* Return a node that uses vertex 'v' other than its current owner */
336
static PBVHNode *pbvh_bmesh_vert_other_node_find(PBVH *bvh, BMVert *v)
340
PBVHNode *current_node;
342
current_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_vert_to_node, v);
344
BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) {
347
f_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_face_to_node, f);
349
if (f_node != current_node)
356
static void pbvh_bmesh_vert_ownership_transfer(PBVH *bvh, PBVHNode *new_owner,
359
PBVHNode *current_owner;
361
current_owner = pbvh_bmesh_node_lookup(bvh, bvh->bm_vert_to_node, v);
362
BLI_assert(current_owner != new_owner);
364
/* Remove current ownership */
365
BLI_ghash_remove(bvh->bm_vert_to_node, v, NULL, NULL);
366
BLI_ghash_remove(current_owner->bm_unique_verts, v, NULL, NULL);
368
/* Set new ownership */
369
BLI_ghash_insert(bvh->bm_vert_to_node, v,
370
SET_INT_IN_POINTER(new_owner - bvh->nodes));
371
BLI_ghash_insert(new_owner->bm_unique_verts, v, NULL);
372
BLI_ghash_remove(new_owner->bm_other_verts, v, NULL, NULL);
373
BLI_assert(!BLI_ghash_haskey(new_owner->bm_other_verts, v));
376
static void pbvh_bmesh_vert_remove(PBVH *bvh, BMVert *v)
382
BLI_assert(BLI_ghash_haskey(bvh->bm_vert_to_node, v));
383
v_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_vert_to_node, v);
384
BLI_ghash_remove(v_node->bm_unique_verts, v, NULL, NULL);
385
BLI_ghash_remove(bvh->bm_vert_to_node, v, NULL, NULL);
387
/* Have to check each neighboring face's node */
388
BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) {
389
PBVHNode *f_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_face_to_node, f);
391
BLI_ghash_remove(f_node->bm_unique_verts, v, NULL, NULL);
392
BLI_ghash_remove(f_node->bm_other_verts, v, NULL, NULL);
394
BLI_assert(!BLI_ghash_haskey(f_node->bm_unique_verts, v));
395
BLI_assert(!BLI_ghash_haskey(f_node->bm_other_verts, v));
399
static void pbvh_bmesh_face_remove(PBVH *bvh, BMFace *f)
407
f_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_face_to_node, f);
409
/* Check if any of this face's vertices need to be removed
411
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
414
if (pbvh_bmesh_node_vert_use_count(bvh, f_node, v) == 1) {
415
if (BLI_ghash_haskey(f_node->bm_unique_verts, v)) {
416
/* Find a different node that uses 'v' */
419
new_node = pbvh_bmesh_vert_other_node_find(bvh, v);
420
BLI_assert(new_node || BM_vert_face_count(v) == 1);
423
pbvh_bmesh_vert_ownership_transfer(bvh, new_node, v);
427
/* Remove from other verts */
428
BLI_ghash_remove(f_node->bm_other_verts, v, NULL, NULL);
431
} while ((l_iter = l_iter->next) != l_first);
433
/* Remove face from node and top level */
434
BLI_ghash_remove(f_node->bm_faces, f, NULL, NULL);
435
BLI_ghash_remove(bvh->bm_face_to_node, f, NULL, NULL);
437
/* Log removed face */
438
BM_log_face_removed(bvh->bm_log, f);
441
static void pbvh_bmesh_edge_loops(BLI_Buffer *buf, BMEdge *e)
443
/* fast-path for most common case where an edge has 2 faces,
444
* no need to iterate twice.
445
* This assumes that the buffer */
446
BMLoop **data = buf->data;
447
BLI_assert(buf->alloc_count >= 2);
448
if (LIKELY(BM_edge_loop_pair(e, &data[0], &data[1]))) {
452
BLI_buffer_resize(buf, BM_edge_face_count(e));
453
BM_iter_as_array(NULL, BM_LOOPS_OF_EDGE, e, buf->data, buf->count);
457
static void pbvh_bmesh_node_drop_orig(PBVHNode *node)
460
MEM_freeN(node->bm_orco);
462
MEM_freeN(node->bm_ortri);
463
node->bm_orco = NULL;
464
node->bm_ortri = NULL;
465
node->bm_tot_ortri = 0;
468
/****************************** EdgeQueue *****************************/
473
float radius_squared;
474
float limit_len_squared;
477
static int edge_queue_tri_in_sphere(const EdgeQueue *q, BMFace *f)
482
/* Get closest point in triangle to sphere center */
483
// BM_iter_as_array(NULL, BM_VERTS_OF_FACE, f, (void **)v_tri, 3);
484
BM_face_as_array_vert_tri(f, v_tri);
486
closest_on_tri_to_point_v3(c, q->center, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co);
488
/* Check if triangle intersects the sphere */
489
return ((len_squared_v3v3(q->center, c) <= q->radius_squared));
492
static void edge_queue_insert(EdgeQueue *q, BLI_mempool *pool, BMEdge *e,
497
pair = BLI_mempool_alloc(pool);
500
BLI_heap_insert(q->heap, priority, pair);
503
static void long_edge_queue_edge_add(EdgeQueue *q, BLI_mempool *pool,
506
const float len_sq = BM_edge_calc_length_squared(e);
507
if (len_sq > q->limit_len_squared)
508
edge_queue_insert(q, pool, e, 1.0f / len_sq);
511
static void short_edge_queue_edge_add(EdgeQueue *q, BLI_mempool *pool,
514
const float len_sq = BM_edge_calc_length_squared(e);
515
if (len_sq < q->limit_len_squared)
516
edge_queue_insert(q, pool, e, len_sq);
519
static void long_edge_queue_face_add(EdgeQueue *q, BLI_mempool *pool,
522
if (edge_queue_tri_in_sphere(q, f)) {
526
/* Check each edge of the face */
527
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
529
long_edge_queue_edge_add(q, pool, l_iter->e);
530
} while ((l_iter = l_iter->next) != l_first);
534
static void short_edge_queue_face_add(EdgeQueue *q, BLI_mempool *pool,
537
if (edge_queue_tri_in_sphere(q, f)) {
541
/* Check each edge of the face */
542
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
544
short_edge_queue_edge_add(q, pool, l_iter->e);
545
} while ((l_iter = l_iter->next) != l_first);
549
/* Create a priority queue containing vertex pairs connected by a long
550
* edge as defined by PBVH.bm_max_edge_len.
552
* Only nodes marked for topology update are checked, and in those
553
* nodes only edges used by a face intersecting the (center, radius)
554
* sphere are checked.
556
* The highest priority (lowest number) is given to the longest edge.
558
static void long_edge_queue_create(EdgeQueue *q, BLI_mempool *pool,
559
PBVH *bvh, const float center[3],
564
q->heap = BLI_heap_new();
566
q->radius_squared = radius * radius;
567
q->limit_len_squared = bvh->bm_max_edge_len * bvh->bm_max_edge_len;
569
for (n = 0; n < bvh->totnode; n++) {
570
PBVHNode *node = &bvh->nodes[n];
572
/* Check leaf nodes marked for topology update */
573
if ((node->flag & PBVH_Leaf) &&
574
(node->flag & PBVH_UpdateTopology))
576
GHashIterator gh_iter;
578
/* Check each face */
579
GHASH_ITER (gh_iter, node->bm_faces) {
580
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
582
long_edge_queue_face_add(q, pool, f);
588
/* Create a priority queue containing vertex pairs connected by a
589
* short edge as defined by PBVH.bm_min_edge_len.
591
* Only nodes marked for topology update are checked, and in those
592
* nodes only edges used by a face intersecting the (center, radius)
593
* sphere are checked.
595
* The highest priority (lowest number) is given to the shortest edge.
597
static void short_edge_queue_create(EdgeQueue *q, BLI_mempool *pool,
598
PBVH *bvh, const float center[3],
603
q->heap = BLI_heap_new();
605
q->radius_squared = radius * radius;
606
q->limit_len_squared = bvh->bm_min_edge_len * bvh->bm_min_edge_len;
608
for (n = 0; n < bvh->totnode; n++) {
609
PBVHNode *node = &bvh->nodes[n];
611
/* Check leaf nodes marked for topology update */
612
if ((node->flag & PBVH_Leaf) &&
613
(node->flag & PBVH_UpdateTopology))
615
GHashIterator gh_iter;
617
/* Check each face */
618
GHASH_ITER (gh_iter, node->bm_faces) {
619
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
621
short_edge_queue_face_add(q, pool, f);
627
/*************************** Topology update **************************/
629
static void pbvh_bmesh_split_edge(PBVH *bvh, EdgeQueue *q, BLI_mempool *pool,
630
BMEdge *e, BLI_Buffer *edge_loops)
636
/* Get all faces adjacent to the edge */
637
pbvh_bmesh_edge_loops(edge_loops, e);
639
/* Create a new vertex in current node at the edge's midpoint */
640
mid_v3_v3v3(mid, e->v1->co, e->v2->co);
642
node_index = GET_INT_FROM_POINTER(BLI_ghash_lookup(bvh->bm_vert_to_node,
644
v_new = pbvh_bmesh_vert_create(bvh, node_index, mid, e->v1);
646
/* For each face, add two new triangles and delete the original */
647
for (i = 0; i < edge_loops->count; i++) {
648
BMLoop *l_adj = BLI_buffer_at(edge_loops, BMLoop *, i);
649
BMFace *f_adj = l_adj->f;
651
BMVert *opp, *v1, *v2;
655
BLI_assert(f_adj->len == 3);
656
nip = BLI_ghash_lookup(bvh->bm_face_to_node, f_adj);
657
ni = GET_INT_FROM_POINTER(nip);
659
/* Ensure node gets redrawn */
660
bvh->nodes[ni].flag |= PBVH_UpdateDrawBuffers;
662
/* Find the vertex not in the edge */
663
opp = l_adj->prev->v;
665
/* Get e->v1 and e->v2 in the order they appear in the
666
* existing face so that the new faces' winding orders
671
if (ni != node_index && i == 0)
672
pbvh_bmesh_vert_ownership_transfer(bvh, &bvh->nodes[ni], v_new);
674
/* Create two new faces */
675
f_new = pbvh_bmesh_face_create(bvh, ni, v1, v_new, opp, f_adj);
676
long_edge_queue_face_add(q, pool, f_new);
677
f_new = pbvh_bmesh_face_create(bvh, ni, v_new, v2, opp, f_adj);
678
long_edge_queue_face_add(q, pool, f_new);
680
/* Delete original */
681
pbvh_bmesh_face_remove(bvh, f_adj);
682
BM_face_kill(bvh->bm, f_adj);
684
/* Ensure new vertex is in the node */
685
if (!BLI_ghash_haskey(bvh->nodes[ni].bm_unique_verts, v_new) &&
686
!BLI_ghash_haskey(bvh->nodes[ni].bm_other_verts, v_new))
688
BLI_ghash_insert(bvh->nodes[ni].bm_other_verts, v_new, NULL);
691
if (BM_vert_edge_count(opp) >= 9) {
695
BM_ITER_ELEM (e2, &bm_iter, opp, BM_EDGES_OF_VERT) {
696
long_edge_queue_edge_add(q, pool, e2);
701
BM_edge_kill(bvh->bm, e);
704
static int pbvh_bmesh_subdivide_long_edges(PBVH *bvh, EdgeQueue *q,
706
BLI_Buffer *edge_loops)
708
int any_subdivided = FALSE;
710
while (!BLI_heap_is_empty(q->heap)) {
711
BMVert **pair = BLI_heap_popmin(q->heap);
714
/* Check that the edge still exists */
715
if (!(e = BM_edge_exists(pair[0], pair[1]))) {
716
BLI_mempool_free(pool, pair);
720
BLI_mempool_free(pool, pair);
723
/* Check that the edge's vertices are still in the PBVH. It's
724
* possible that an edge collapse has deleted adjacent faces
725
* and the node has been split, thus leaving wire edges and
726
* associated vertices. */
727
if (!BLI_ghash_haskey(bvh->bm_vert_to_node, e->v1) ||
728
!BLI_ghash_haskey(bvh->bm_vert_to_node, e->v2))
733
if (BM_edge_calc_length_squared(e) <= q->limit_len_squared)
736
any_subdivided = TRUE;
738
pbvh_bmesh_split_edge(bvh, q, pool, e, edge_loops);
741
return any_subdivided;
744
static void pbvh_bmesh_collapse_edge(PBVH *bvh, BMEdge *e, BMVert *v1,
745
BMVert *v2, GHash *deleted_verts,
746
BLI_Buffer *edge_loops,
747
BLI_Buffer *deleted_faces)
753
/* Get all faces adjacent to the edge */
754
pbvh_bmesh_edge_loops(edge_loops, e);
756
/* Remove the merge vertex from the PBVH */
757
pbvh_bmesh_vert_remove(bvh, v2);
759
/* Remove all faces adjacent to the edge */
760
for (i = 0; i < edge_loops->count; i++) {
761
BMLoop *l_adj = BLI_buffer_at(edge_loops, BMLoop *, i);
762
BMFace *f_adj = l_adj->f;
764
pbvh_bmesh_face_remove(bvh, f_adj);
765
BM_face_kill(bvh->bm, f_adj);
769
BLI_assert(BM_edge_face_count(e) == 0);
770
BM_edge_kill(bvh->bm, e);
772
/* For all remaining faces of v2, create a new face that is the
773
* same except it uses v1 instead of v2 */
774
/* Note: this could be done with BM_vert_splice(), but that
775
* requires handling other issues like duplicate edges, so doesn't
776
* really buy anything. */
777
deleted_faces->count = 0;
778
BM_ITER_ELEM (f, &bm_iter, v2, BM_FACES_OF_VERT) {
780
BMFace *existing_face;
784
/* Get vertices, replace use of v2 with v1 */
785
// BM_iter_as_array(NULL, BM_VERTS_OF_FACE, f, (void **)v_tri, 3);
786
BM_face_as_array_vert_tri(f, v_tri);
787
for (i = 0; i < 3; i++) {
788
if (v_tri[i] == v2) {
793
/* Check if a face using these vertices already exists. If so,
794
* skip adding this face and mark the existing one for
795
* deletion as well. Prevents extraneous "flaps" from being
797
if (BM_face_exists(v_tri, 3, &existing_face)) {
798
BLI_assert(existing_face);
799
BLI_buffer_append(deleted_faces, BMFace *, existing_face);
802
n = pbvh_bmesh_node_lookup(bvh, bvh->bm_face_to_node, f);
804
pbvh_bmesh_face_create(bvh, ni, v_tri[0], v_tri[1], v_tri[2], f);
806
/* Ensure that v1 is in the new face's node */
807
if (!BLI_ghash_haskey(n->bm_unique_verts, v1) &&
808
!BLI_ghash_haskey(n->bm_other_verts, v1)) {
809
BLI_ghash_insert(n->bm_other_verts, v1, NULL);
813
BLI_buffer_append(deleted_faces, BMFace *, f);
816
/* Delete the tagged faces */
817
for (i = 0; i < deleted_faces->count; i++) {
818
BMFace *f_del = BLI_buffer_at(deleted_faces, BMFace *, i);
823
/* Get vertices and edges of face */
824
BM_face_as_array_vert_tri(f_del, v_tri);
825
for (j = 0; j < 3; j++)
826
e_tri[j] = BM_edge_exists(v_tri[j], v_tri[j == 2 ? 0 : j + 1]);
828
/* Check if any of the face's vertices are now unused, if so
829
* remove them from the PBVH */
830
for (j = 0; j < 3; j++) {
831
if (v_tri[j] != v2 && BM_vert_face_count(v_tri[j]) == 1) {
832
BLI_ghash_insert(deleted_verts, v_tri[j], NULL);
833
pbvh_bmesh_vert_remove(bvh, v_tri[j]);
840
/* Remove the face */
841
pbvh_bmesh_face_remove(bvh, f_del);
842
BM_face_kill(bvh->bm, f_del);
844
/* Check if any of the face's edges are now unused by any
845
* face, if so delete them */
846
for (j = 0; j < 3; j++) {
847
if (BM_edge_face_count(e_tri[j]) == 0)
848
BM_edge_kill(bvh->bm, e_tri[j]);
851
/* Delete unused vertices */
852
for (j = 0; j < 3; j++) {
854
BM_log_vert_removed(bvh->bm, bvh->bm_log, v_tri[j]);
855
BM_vert_kill(bvh->bm, v_tri[j]);
860
/* Move v1 to the midpoint of v1 and v2 (if v1 still exists, it
861
* may have been deleted above) */
862
if (!BLI_ghash_haskey(deleted_verts, v1)) {
863
BM_log_vert_before_modified(bvh->bm, bvh->bm_log, v1);
864
mid_v3_v3v3(v1->co, v1->co, v2->co);
868
BLI_assert(BM_vert_face_count(v2) == 0);
869
BLI_ghash_insert(deleted_verts, v2, NULL);
870
BM_log_vert_removed(bvh->bm, bvh->bm_log, v2);
871
BM_vert_kill(bvh->bm, v2);
874
static int pbvh_bmesh_collapse_short_edges(PBVH *bvh, EdgeQueue *q,
876
BLI_Buffer *edge_loops,
877
BLI_Buffer *deleted_faces)
879
float min_len_squared = bvh->bm_min_edge_len * bvh->bm_min_edge_len;
880
GHash *deleted_verts;
881
int any_collapsed = FALSE;
883
deleted_verts = BLI_ghash_ptr_new("deleted_verts");
885
while (!BLI_heap_is_empty(q->heap)) {
886
BMVert **pair = BLI_heap_popmin(q->heap);
892
BLI_mempool_free(pool, pair);
895
/* Check that the vertices/edge still exist */
896
if (BLI_ghash_haskey(deleted_verts, v1) ||
897
BLI_ghash_haskey(deleted_verts, v2) ||
898
!(e = BM_edge_exists(v1, v2)))
903
/* Check that the edge's vertices are still in the PBVH. It's
904
* possible that an edge collapse has deleted adjacent faces
905
* and the node has been split, thus leaving wire edges and
906
* associated vertices. */
907
if (!BLI_ghash_haskey(bvh->bm_vert_to_node, e->v1) ||
908
!BLI_ghash_haskey(bvh->bm_vert_to_node, e->v2))
913
if (BM_edge_calc_length_squared(e) >= min_len_squared)
916
any_collapsed = TRUE;
918
pbvh_bmesh_collapse_edge(bvh, e, v1, v2,
919
deleted_verts, edge_loops,
923
BLI_ghash_free(deleted_verts, NULL, NULL);
925
return any_collapsed;
928
/************************* Called from pbvh.c *************************/
930
int pbvh_bmesh_node_raycast(PBVHNode *node, const float ray_start[3],
931
const float ray_normal[3], float *dist,
934
GHashIterator gh_iter;
937
if (use_original && node->bm_tot_ortri) {
939
for (i = 0; i < node->bm_tot_ortri; i++) {
940
const int *t = node->bm_ortri[i];
941
hit |= ray_face_intersection(ray_start, ray_normal,
949
GHASH_ITER (gh_iter, node->bm_faces) {
950
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
952
BLI_assert(f->len == 3);
953
if (f->len == 3 && !paint_is_bmesh_face_hidden(f)) {
956
BM_face_as_array_vert_tri(f, v_tri);
957
hit |= ray_face_intersection(ray_start, ray_normal,
969
void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode)
973
for (n = 0; n < totnode; n++) {
974
PBVHNode *node = nodes[n];
975
GHashIterator gh_iter;
977
GHASH_ITER (gh_iter, node->bm_faces) {
978
BM_face_normal_update(BLI_ghashIterator_getKey(&gh_iter));
980
GHASH_ITER (gh_iter, node->bm_unique_verts) {
981
BM_vert_normal_update(BLI_ghashIterator_getKey(&gh_iter));
986
/***************************** Public API *****************************/
988
/* Build a PBVH from a BMesh */
989
void BKE_pbvh_build_bmesh(PBVH *bvh, BMesh *bm, int smooth_shading,
999
BKE_pbvh_bmesh_detail_size_set(bvh, 0.75);
1001
bvh->type = PBVH_BMESH;
1002
bvh->bm_face_to_node = BLI_ghash_ptr_new("bm_face_to_node");
1003
bvh->bm_vert_to_node = BLI_ghash_ptr_new("bm_vert_to_node");
1006
/* TODO: choose leaf limit better */
1007
bvh->leaf_limit = 100;
1010
bvh->flags |= PBVH_DYNTOPO_SMOOTH_SHADING;
1012
/* Start with all faces in the root node */
1013
n = bvh->nodes = MEM_callocN(sizeof(PBVHNode), "PBVHNode");
1015
n->flag = PBVH_Leaf;
1016
n->bm_faces = BLI_ghash_ptr_new("bm_faces");
1017
BM_ITER_MESH (f, &iter, bvh->bm, BM_FACES_OF_MESH) {
1018
BLI_ghash_insert(n->bm_faces, f, NULL);
1021
/* Recursively split the node until it is under the limit; if no
1022
* splitting occurs then finalize the existing leaf node */
1023
if (!pbvh_bmesh_node_limit_ensure(bvh, node_index))
1024
pbvh_bmesh_node_finalize(bvh, 0);
1027
/* Collapse short edges, subdivide long edges */
1028
int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
1029
const float center[3], float radius)
1031
/* 2 is enough for edge faces - manifold edge */
1032
BLI_buffer_declare_static(BMFace *, edge_loops, BLI_BUFFER_NOP, 2);
1033
BLI_buffer_declare_static(BMFace *, deleted_faces, BLI_BUFFER_NOP, 32);
1035
int modified = FALSE;
1038
if (mode & PBVH_Collapse) {
1040
BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert) * 2,
1042
short_edge_queue_create(&q, queue_pool, bvh, center, radius);
1043
pbvh_bmesh_collapse_short_edges(bvh, &q, queue_pool, &edge_loops,
1045
BLI_heap_free(q.heap, NULL);
1046
BLI_mempool_destroy(queue_pool);
1049
if (mode & PBVH_Subdivide) {
1051
BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert) * 2,
1053
long_edge_queue_create(&q, queue_pool, bvh, center, radius);
1054
pbvh_bmesh_subdivide_long_edges(bvh, &q, queue_pool, &edge_loops);
1055
BLI_heap_free(q.heap, NULL);
1056
BLI_mempool_destroy(queue_pool);
1060
for (n = 0; n < bvh->totnode; n++) {
1061
PBVHNode *node = &bvh->nodes[n];
1063
if (node->flag & PBVH_Leaf &&
1064
node->flag & PBVH_UpdateTopology)
1066
node->flag &= ~PBVH_UpdateTopology;
1069
BLI_buffer_free(&edge_loops);
1070
BLI_buffer_free(&deleted_faces);
1075
BLI_INLINE void bm_face_as_array_index_tri(BMFace *f, int r_index[3])
1077
BMLoop *l = BM_FACE_FIRST_LOOP(f);
1079
BLI_assert(f->len == 3);
1081
r_index[0] = BM_elem_index_get(l->v); l = l->next;
1082
r_index[1] = BM_elem_index_get(l->v); l = l->next;
1083
r_index[2] = BM_elem_index_get(l->v);
1086
/* In order to perform operations on the original node coordinates
1087
* (currently just raycast), store the node's triangles and vertices.
1089
* Skips triangles that are hidden. */
1090
void BKE_pbvh_bmesh_node_save_orig(PBVHNode *node)
1092
GHashIterator gh_iter;
1093
int i, totvert, tottri;
1095
/* Skip if original coords/triangles are already saved */
1099
totvert = (BLI_ghash_size(node->bm_unique_verts) +
1100
BLI_ghash_size(node->bm_other_verts));
1102
tottri = BLI_ghash_size(node->bm_faces);
1104
node->bm_orco = MEM_mallocN(sizeof(*node->bm_orco) * totvert, AT);
1105
node->bm_ortri = MEM_mallocN(sizeof(*node->bm_ortri) * tottri, AT);
1107
/* Copy out the vertices and assign a temporary index */
1109
GHASH_ITER (gh_iter, node->bm_unique_verts) {
1110
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1111
copy_v3_v3(node->bm_orco[i], v->co);
1112
BM_elem_index_set(v, i); /* set_dirty! */
1115
GHASH_ITER (gh_iter, node->bm_other_verts) {
1116
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1117
copy_v3_v3(node->bm_orco[i], v->co);
1118
BM_elem_index_set(v, i); /* set_dirty! */
1122
/* Copy the triangles */
1124
GHASH_ITER (gh_iter, node->bm_faces) {
1125
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
1127
if (paint_is_bmesh_face_hidden(f))
1134
BM_ITER_ELEM (v, &bm_iter, f, BM_VERTS_OF_FACE) {
1135
node->bm_ortri[i][j] = BM_elem_index_get(v);
1139
bm_face_as_array_index_tri(f, node->bm_ortri[i]);
1143
node->bm_tot_ortri = i;
1146
void BKE_pbvh_bmesh_after_stroke(PBVH *bvh)
1149
for (i = 0; i < bvh->totnode; i++) {
1150
PBVHNode *n = &bvh->nodes[i];
1151
if (n->flag & PBVH_Leaf) {
1152
/* Free orco/ortri data */
1153
pbvh_bmesh_node_drop_orig(n);
1155
/* Recursively split nodes that have gotten too many
1157
pbvh_bmesh_node_limit_ensure(bvh, i);
1162
void BKE_pbvh_bmesh_detail_size_set(PBVH *bvh, float detail_size)
1164
bvh->bm_max_edge_len = detail_size;
1165
bvh->bm_min_edge_len = bvh->bm_max_edge_len * 0.4f;
1168
void BKE_pbvh_node_mark_topology_update(PBVHNode *node)
1170
node->flag |= PBVH_UpdateTopology;
1173
GHash *BKE_pbvh_bmesh_node_unique_verts(PBVHNode *node)
1175
return node->bm_unique_verts;
1178
GHash *BKE_pbvh_bmesh_node_other_verts(PBVHNode *node)
1180
return node->bm_other_verts;
1183
/****************************** Debugging *****************************/
1186
void bli_ghash_duplicate_key_check(GHash *gh)
1188
GHashIterator gh_iter1, gh_iter2;
1190
GHASH_ITER (gh_iter1, gh) {
1191
void *key1 = BLI_ghashIterator_getKey(&gh_iter1);
1194
GHASH_ITER (gh_iter2, gh) {
1195
void *key2 = BLI_ghashIterator_getKey(&gh_iter2);
1200
BLI_assert(!"duplicate in hash");
1207
void bmesh_print(BMesh *bm)
1215
fprintf(stderr, "\nbm=%p, totvert=%d, totedge=%d, "
1216
"totloop=%d, totface=%d\n",
1217
bm, bm->totvert, bm->totedge,
1218
bm->totloop, bm->totface);
1220
fprintf(stderr, "vertices:\n");
1221
BM_ITER_MESH(v, &iter, bm, BM_VERTS_OF_MESH) {
1222
fprintf(stderr, " %d co=(%.3f %.3f %.3f) oflag=%x\n",
1223
BM_elem_index_get(v), v->co[0], v->co[1], v->co[2],
1224
v->oflags[bm->stackdepth - 1].f);
1227
fprintf(stderr, "edges:\n");
1228
BM_ITER_MESH(e, &iter, bm, BM_EDGES_OF_MESH) {
1229
fprintf(stderr, " %d v1=%d, v2=%d, oflag=%x\n",
1230
BM_elem_index_get(e),
1231
BM_elem_index_get(e->v1),
1232
BM_elem_index_get(e->v2),
1233
e->oflags[bm->stackdepth - 1].f);
1236
fprintf(stderr, "faces:\n");
1237
BM_ITER_MESH(f, &iter, bm, BM_FACES_OF_MESH) {
1238
fprintf(stderr, " %d len=%d, oflag=%x\n",
1239
BM_elem_index_get(f), f->len,
1240
f->oflags[bm->stackdepth - 1].f);
1242
fprintf(stderr, " v: ");
1243
BM_ITER_ELEM(v, &siter, f, BM_VERTS_OF_FACE) {
1244
fprintf(stderr, "%d ", BM_elem_index_get(v));
1246
fprintf(stderr, "\n");
1248
fprintf(stderr, " e: ");
1249
BM_ITER_ELEM(e, &siter, f, BM_EDGES_OF_FACE) {
1250
fprintf(stderr, "%d ", BM_elem_index_get(e));
1252
fprintf(stderr, "\n");
1254
fprintf(stderr, " l: ");
1255
BM_ITER_ELEM(l, &siter, f, BM_LOOPS_OF_FACE) {
1256
fprintf(stderr, "%d(v=%d, e=%d) ",
1257
BM_elem_index_get(l),
1258
BM_elem_index_get(l->v),
1259
BM_elem_index_get(l->e));
1261
fprintf(stderr, "\n");
1265
void pbvh_bmesh_print(PBVH *bvh)
1267
GHashIterator gh_iter;
1270
fprintf(stderr, "\npbvh=%p\n", bvh);
1271
fprintf(stderr, "bm_face_to_node:\n");
1272
GHASH_ITER (gh_iter, bvh->bm_face_to_node) {
1273
fprintf(stderr, " %d -> %d\n",
1274
BM_elem_index_get((BMFace*)BLI_ghashIterator_getKey(&gh_iter)),
1275
GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter)));
1278
fprintf(stderr, "bm_vert_to_node:\n");
1279
GHASH_ITER (gh_iter, bvh->bm_vert_to_node) {
1280
fprintf(stderr, " %d -> %d\n",
1281
BM_elem_index_get((BMVert*)BLI_ghashIterator_getKey(&gh_iter)),
1282
GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter)));
1285
for (n = 0; n < bvh->totnode; n++) {
1286
PBVHNode *node = &bvh->nodes[n];
1287
if (!(node->flag & PBVH_Leaf))
1290
fprintf(stderr, "node %d\n faces:\n", n);
1291
GHASH_ITER (gh_iter, node->bm_faces)
1292
fprintf(stderr, " %d\n",
1293
BM_elem_index_get((BMFace*)BLI_ghashIterator_getKey(&gh_iter)));
1294
fprintf(stderr, " unique verts:\n");
1295
GHASH_ITER (gh_iter, node->bm_unique_verts)
1296
fprintf(stderr, " %d\n",
1297
BM_elem_index_get((BMVert*)BLI_ghashIterator_getKey(&gh_iter)));
1298
fprintf(stderr, " other verts:\n");
1299
GHASH_ITER (gh_iter, node->bm_other_verts)
1300
fprintf(stderr, " %d\n",
1301
BM_elem_index_get((BMVert*)BLI_ghashIterator_getKey(&gh_iter)));
1305
void print_flag_factors(int flag)
1308
printf("flag=0x%x:\n", flag);
1309
for (i = 0; i < 32; i++) {
1310
if (flag & (1 << i)) {
1311
printf(" %d (1 << %d)\n", 1 << i, i);
1316
void pbvh_bmesh_verify(PBVH *bvh)
1318
GHashIterator gh_iter;
1322
BLI_assert(bvh->bm->totface == BLI_ghash_size(bvh->bm_face_to_node));
1323
GHASH_ITER (gh_iter, bvh->bm_face_to_node) {
1326
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
1327
void *nip = BLI_ghashIterator_getValue(&gh_iter);
1328
int ni = GET_INT_FROM_POINTER(nip);
1329
PBVHNode *n = &bvh->nodes[ni];
1331
/* Check that the face's node is a leaf */
1332
BLI_assert(n->flag & PBVH_Leaf);
1334
/* Check that the face's node knows it owns the face */
1335
BLI_assert(BLI_ghash_haskey(n->bm_faces, f));
1337
/* Check the face's vertices... */
1338
BM_ITER_ELEM (v, &bm_iter, f, BM_VERTS_OF_FACE) {
1341
/* Check that the vertex is in the node */
1342
BLI_assert(BLI_ghash_haskey(n->bm_unique_verts, v) ^
1343
BLI_ghash_haskey(n->bm_other_verts, v));
1345
/* Check that the vertex has a node owner */
1346
nv = pbvh_bmesh_node_lookup(bvh, bvh->bm_vert_to_node, v);
1348
/* Check that the vertex's node knows it owns the vert */
1349
BLI_assert(BLI_ghash_haskey(nv->bm_unique_verts, v));
1351
/* Check that the vertex isn't duplicated as an 'other' vert */
1352
BLI_assert(!BLI_ghash_haskey(nv->bm_other_verts, v));
1357
BLI_assert(bvh->bm->totvert == BLI_ghash_size(bvh->bm_vert_to_node));
1358
GHASH_ITER (gh_iter, bvh->bm_vert_to_node) {
1360
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1362
void *nip = BLI_ghashIterator_getValue(&gh_iter);
1363
int ni = GET_INT_FROM_POINTER(nip);
1364
PBVHNode *n = &bvh->nodes[ni];
1367
/* Check that the vert's node is a leaf */
1368
BLI_assert(n->flag & PBVH_Leaf);
1370
/* Check that the vert's node knows it owns the vert */
1371
BLI_assert(BLI_ghash_haskey(n->bm_unique_verts, v));
1373
/* Check that the vertex isn't duplicated as an 'other' vert */
1374
BLI_assert(!BLI_ghash_haskey(n->bm_other_verts, v));
1376
/* Check that the vert's node also contains one of the vert's
1378
BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) {
1379
if (BLI_ghash_lookup(bvh->bm_face_to_node, f) == nip) {
1387
/* Check that node elements are recorded in the top level */
1388
for (i = 0; i < bvh->totnode; i++) {
1389
PBVHNode *n = &bvh->nodes[i];
1390
if (n->flag & PBVH_Leaf) {
1391
/* Check for duplicate entries */
1394
bli_ghash_duplicate_key_check(n->bm_faces);
1395
bli_ghash_duplicate_key_check(n->bm_unique_verts);
1396
bli_ghash_duplicate_key_check(n->bm_other_verts);
1399
GHASH_ITER (gh_iter, n->bm_faces) {
1400
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
1401
void *nip = BLI_ghash_lookup(bvh->bm_face_to_node, f);
1402
BLI_assert(BLI_ghash_haskey(bvh->bm_face_to_node, f));
1403
BLI_assert(GET_INT_FROM_POINTER(nip) == (n - bvh->nodes));
1406
GHASH_ITER (gh_iter, n->bm_unique_verts) {
1407
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1408
void *nip = BLI_ghash_lookup(bvh->bm_vert_to_node, v);
1409
BLI_assert(BLI_ghash_haskey(bvh->bm_vert_to_node, v));
1410
BLI_assert(!BLI_ghash_haskey(n->bm_other_verts, v));
1411
BLI_assert(GET_INT_FROM_POINTER(nip) == (n - bvh->nodes));
1414
GHASH_ITER (gh_iter, n->bm_other_verts) {
1415
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1416
BLI_assert(BLI_ghash_haskey(bvh->bm_vert_to_node, v));
1417
BLI_assert(BM_vert_face_count(v) > 0);