67
71
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
70
if (!BLI_ghash_haskey(n->bm_unique_verts, v)) {
74
if (!BLI_gset_haskey(n->bm_unique_verts, v)) {
71
75
if (BLI_ghash_haskey(bvh->bm_vert_to_node, v)) {
72
if (!BLI_ghash_haskey(n->bm_other_verts, v))
73
BLI_ghash_insert(n->bm_other_verts, v, NULL);
76
BLI_gset_reinsert(n->bm_other_verts, v, NULL);
76
BLI_ghash_insert(n->bm_unique_verts, v, NULL);
79
BLI_gset_insert(n->bm_unique_verts, v);
77
80
BLI_ghash_insert(bvh->bm_vert_to_node, v, node_val);
227
231
static int pbvh_bmesh_node_limit_ensure(PBVH *bvh, int node_index)
230
236
GHashIterator gh_iter;
232
if (BLI_ghash_size(bvh->nodes[node_index].bm_faces) <= bvh->leaf_limit) {
240
bm_faces = bvh->nodes[node_index].bm_faces;
241
bm_faces_size = BLI_ghash_size(bm_faces);
242
if (bm_faces_size <= bvh->leaf_limit) {
233
243
/* Node limit not exceeded */
237
247
/* For each BMFace, store the AABB and AABB centroid */
238
prim_bbc = BLI_ghash_ptr_new("prim_bbc");
248
prim_bbc = BLI_ghash_ptr_new_ex("prim_bbc", bm_faces_size);
249
bbc_array = MEM_callocN(sizeof(BBC) * bm_faces_size, "BBC");
240
GHASH_ITER (gh_iter, bvh->nodes[node_index].bm_faces) {
251
GHASH_ITER_INDEX (gh_iter, bm_faces, i) {
241
252
BMFace *f = BLI_ghashIterator_getKey(&gh_iter);
242
BBC *bbc = MEM_callocN(sizeof(BBC), "BBC");
253
BBC *bbc = &bbc_array[i];
302
314
/* ensure we never add existing face */
303
315
BLI_assert(BM_face_exists(v_tri, 3, NULL) == false);
305
/* Note: passing NULL for the 'example' parameter, profiling shows
306
* a small performance bump */
307
f = BM_face_create(bvh->bm, v_tri, e_tri, 3, 0);
317
f = BM_face_create(bvh->bm, v_tri, e_tri, 3, f_example, BM_CREATE_NOP);
308
319
if (!BLI_ghash_haskey(bvh->bm_face_to_node, f)) {
310
321
BLI_ghash_insert(bvh->nodes[node_index].bm_faces, f, NULL);
366
377
BLI_assert(current_owner != new_owner);
368
379
/* Remove current ownership */
369
BLI_ghash_remove(bvh->bm_vert_to_node, v, NULL, NULL);
370
BLI_ghash_remove(current_owner->bm_unique_verts, v, NULL, NULL);
380
BLI_gset_remove(current_owner->bm_unique_verts, v, NULL);
372
382
/* Set new ownership */
373
BLI_ghash_insert(bvh->bm_vert_to_node, v,
374
SET_INT_IN_POINTER(new_owner - bvh->nodes));
375
BLI_ghash_insert(new_owner->bm_unique_verts, v, NULL);
376
BLI_ghash_remove(new_owner->bm_other_verts, v, NULL, NULL);
377
BLI_assert(!BLI_ghash_haskey(new_owner->bm_other_verts, v));
383
BLI_ghash_reinsert(bvh->bm_vert_to_node, v,
384
SET_INT_IN_POINTER(new_owner - bvh->nodes), NULL, NULL);
385
BLI_gset_insert(new_owner->bm_unique_verts, v);
386
BLI_gset_remove(new_owner->bm_other_verts, v, NULL);
387
BLI_assert(!BLI_gset_haskey(new_owner->bm_other_verts, v));
380
390
static void pbvh_bmesh_vert_remove(PBVH *bvh, BMVert *v)
386
396
BLI_assert(BLI_ghash_haskey(bvh->bm_vert_to_node, v));
387
397
v_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_vert_to_node, v);
388
BLI_ghash_remove(v_node->bm_unique_verts, v, NULL, NULL);
398
BLI_gset_remove(v_node->bm_unique_verts, v, NULL);
389
399
BLI_ghash_remove(bvh->bm_vert_to_node, v, NULL, NULL);
391
401
/* Have to check each neighboring face's node */
392
402
BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) {
393
403
PBVHNode *f_node = pbvh_bmesh_node_lookup(bvh, bvh->bm_face_to_node, f);
395
BLI_ghash_remove(f_node->bm_unique_verts, v, NULL, NULL);
396
BLI_ghash_remove(f_node->bm_other_verts, v, NULL, NULL);
405
/* Remove current ownership */
406
/* Should be handled above by vert_to_node removal, leaving just in case - psy-fi */
407
//BLI_ghash_remove(f_node->bm_unique_verts, v, NULL, NULL);
408
BLI_gset_remove(f_node->bm_other_verts, v, NULL);
398
BLI_assert(!BLI_ghash_haskey(f_node->bm_unique_verts, v));
399
BLI_assert(!BLI_ghash_haskey(f_node->bm_other_verts, v));
410
BLI_assert(!BLI_gset_haskey(f_node->bm_unique_verts, v));
411
BLI_assert(!BLI_gset_haskey(f_node->bm_other_verts, v));
493
512
return ((len_squared_v3v3(q->center, c) <= q->radius_squared));
496
/* Return true if the vertex mask is less than 0.5, false otherwise */
497
static int check_mask_half(BMesh *bm, BMVert *v)
515
/* Return true if the vertex mask is less than 1.0, false otherwise */
516
static bool check_mask(EdgeQueueContext *eq_ctx, BMVert *v)
501
mask = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_PAINT_MASK);
502
return ((*mask) < 0.5f);
518
return (BM_ELEM_CD_GET_FLOAT(v, eq_ctx->cd_vert_mask_offset) < 1.0f);
505
static void edge_queue_insert(EdgeQueue *q, BLI_mempool *pool, BMEdge *e,
506
float priority, BMesh *bm)
521
static void edge_queue_insert(EdgeQueueContext *eq_ctx, BMEdge *e,
510
/* Don't let topology update affect masked vertices. Unlike with
511
* displacements, can't do 50% topology update, so instead set
512
* (arbitrary) cutoff: if both vertices' masks are less than 50%,
513
* topology update can happen. */
514
if (check_mask_half(bm, e->v1) && check_mask_half(bm, e->v2)) {
515
pair = BLI_mempool_alloc(pool);
526
/* Don't let topology update affect fully masked vertices. This used to
527
* have a 50% mask cutoff, with the reasoning that you can't do a 50%
528
* topology update. But this gives an ugly border in the mesh. The mask
529
* should already make the brush move the vertices only 50%, which means
530
* that topology updates will also happen less frequent, that should be
532
if (check_mask(eq_ctx, e->v1) || check_mask(eq_ctx, e->v2)) {
533
pair = BLI_mempool_alloc(eq_ctx->pool);
518
BLI_heap_insert(q->heap, priority, pair);
536
BLI_heap_insert(eq_ctx->q->heap, priority, pair);
522
static void long_edge_queue_edge_add(EdgeQueue *q, BLI_mempool *pool,
523
BMEdge *e, BMesh *bm)
525
const float len_sq = BM_edge_calc_length_squared(e);
526
if (len_sq > q->limit_len_squared)
527
edge_queue_insert(q, pool, e, 1.0f / len_sq, bm);
530
static void short_edge_queue_edge_add(EdgeQueue *q, BLI_mempool *pool,
531
BMEdge *e, BMesh *bm)
533
const float len_sq = BM_edge_calc_length_squared(e);
534
if (len_sq < q->limit_len_squared)
535
edge_queue_insert(q, pool, e, len_sq, bm);
538
static void long_edge_queue_face_add(EdgeQueue *q, BLI_mempool *pool,
539
BMFace *f, BMesh *bm)
541
if (edge_queue_tri_in_sphere(q, f)) {
540
static void long_edge_queue_edge_add(EdgeQueueContext *eq_ctx,
543
const float len_sq = BM_edge_calc_length_squared(e);
544
if (len_sq > eq_ctx->q->limit_len_squared)
545
edge_queue_insert(eq_ctx, e, 1.0f / len_sq);
548
static void short_edge_queue_edge_add(EdgeQueueContext *eq_ctx,
551
const float len_sq = BM_edge_calc_length_squared(e);
552
if (len_sq < eq_ctx->q->limit_len_squared)
553
edge_queue_insert(eq_ctx, e, len_sq);
556
static void long_edge_queue_face_add(EdgeQueueContext *eq_ctx,
559
if (edge_queue_tri_in_sphere(eq_ctx->q, f)) {
545
563
/* Check each edge of the face */
546
564
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
548
long_edge_queue_edge_add(q, pool, l_iter->e, bm);
566
long_edge_queue_edge_add(eq_ctx, l_iter->e);
549
567
} while ((l_iter = l_iter->next) != l_first);
553
static void short_edge_queue_face_add(EdgeQueue *q, BLI_mempool *pool,
554
BMFace *f, BMesh *bm)
571
static void short_edge_queue_face_add(EdgeQueueContext *eq_ctx,
556
if (edge_queue_tri_in_sphere(q, f)) {
574
if (edge_queue_tri_in_sphere(eq_ctx->q, f)) {
560
578
/* Check each edge of the face */
561
579
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
563
short_edge_queue_edge_add(q, pool, l_iter->e, bm);
581
short_edge_queue_edge_add(eq_ctx, l_iter->e);
564
582
} while ((l_iter = l_iter->next) != l_first);
575
593
* The highest priority (lowest number) is given to the longest edge.
577
static void long_edge_queue_create(EdgeQueue *q, BLI_mempool *pool,
595
static void long_edge_queue_create(EdgeQueueContext *eq_ctx,
578
596
PBVH *bvh, const float center[3],
583
q->heap = BLI_heap_new();
585
q->radius_squared = radius * radius;
586
q->limit_len_squared = bvh->bm_max_edge_len * bvh->bm_max_edge_len;
601
eq_ctx->q->heap = BLI_heap_new();
602
eq_ctx->q->center = center;
603
eq_ctx->q->radius_squared = radius * radius;
604
eq_ctx->q->limit_len_squared = bvh->bm_max_edge_len * bvh->bm_max_edge_len;
588
606
for (n = 0; n < bvh->totnode; n++) {
589
607
PBVHNode *node = &bvh->nodes[n];
614
632
* The highest priority (lowest number) is given to the shortest edge.
616
static void short_edge_queue_create(EdgeQueue *q, BLI_mempool *pool,
634
static void short_edge_queue_create(EdgeQueueContext *eq_ctx,
617
635
PBVH *bvh, const float center[3],
622
q->heap = BLI_heap_new();
624
q->radius_squared = radius * radius;
625
q->limit_len_squared = bvh->bm_min_edge_len * bvh->bm_min_edge_len;
640
eq_ctx->q->heap = BLI_heap_new();
641
eq_ctx->q->center = center;
642
eq_ctx->q->radius_squared = radius * radius;
643
eq_ctx->q->limit_len_squared = bvh->bm_min_edge_len * bvh->bm_min_edge_len;
627
645
for (n = 0; n < bvh->totnode; n++) {
628
646
PBVHNode *node = &bvh->nodes[n];
714
742
e_tri[2] = e_tri[1]; /* switched */
715
743
e_tri[1] = BM_edge_create(bvh->bm, v_tri[1], v_tri[2], NULL, BM_CREATE_NO_DOUBLE);
716
744
f_new = pbvh_bmesh_face_create(bvh, ni, v_tri, e_tri, f_adj);
717
long_edge_queue_face_add(q, pool, f_new, bvh->bm);
745
long_edge_queue_face_add(eq_ctx, f_new);
719
747
/* Delete original */
720
748
pbvh_bmesh_face_remove(bvh, f_adj);
721
749
BM_face_kill(bvh->bm, f_adj);
723
751
/* Ensure new vertex is in the node */
724
if (!BLI_ghash_haskey(bvh->nodes[ni].bm_unique_verts, v_new) &&
725
!BLI_ghash_haskey(bvh->nodes[ni].bm_other_verts, v_new))
752
if (!BLI_gset_haskey(bvh->nodes[ni].bm_unique_verts, v_new) &&
753
!BLI_gset_haskey(bvh->nodes[ni].bm_other_verts, v_new))
727
BLI_ghash_insert(bvh->nodes[ni].bm_other_verts, v_new, NULL);
755
BLI_gset_insert(bvh->nodes[ni].bm_other_verts, v_new);
730
758
if (BM_vert_edge_count(v_opp) >= 9) {
740
768
BM_edge_kill(bvh->bm, e);
743
static int pbvh_bmesh_subdivide_long_edges(PBVH *bvh, EdgeQueue *q,
771
static int pbvh_bmesh_subdivide_long_edges(EdgeQueueContext *eq_ctx, PBVH *bvh,
745
772
BLI_Buffer *edge_loops)
747
774
int any_subdivided = FALSE;
749
while (!BLI_heap_is_empty(q->heap)) {
750
BMVert **pair = BLI_heap_popmin(q->heap);
776
while (!BLI_heap_is_empty(eq_ctx->q->heap)) {
777
BMVert **pair = BLI_heap_popmin(eq_ctx->q->heap);
753
780
/* Check that the edge still exists */
754
781
if (!(e = BM_edge_exists(pair[0], pair[1]))) {
755
BLI_mempool_free(pool, pair);
782
BLI_mempool_free(eq_ctx->pool, pair);
759
BLI_mempool_free(pool, pair);
786
BLI_mempool_free(eq_ctx->pool, pair);
762
789
/* Check that the edge's vertices are still in the PBVH. It's
1014
1042
void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode)
1018
1046
for (n = 0; n < totnode; n++) {
1019
1047
PBVHNode *node = nodes[n];
1020
GHashIterator gh_iter;
1022
GHASH_ITER (gh_iter, node->bm_faces) {
1023
BM_face_normal_update(BLI_ghashIterator_getKey(&gh_iter));
1025
GHASH_ITER (gh_iter, node->bm_unique_verts) {
1026
BM_vert_normal_update(BLI_ghashIterator_getKey(&gh_iter));
1049
if (node->flag & PBVH_UpdateNormals) {
1050
GHashIterator gh_iter;
1051
GSetIterator gs_iter;
1053
GHASH_ITER (gh_iter, node->bm_faces) {
1054
BM_face_normal_update(BLI_ghashIterator_getKey(&gh_iter));
1056
GSET_ITER (gs_iter, node->bm_unique_verts) {
1057
BM_vert_normal_update(BLI_gsetIterator_getKey(&gs_iter));
1059
/* This should be unneeded normally */
1060
GSET_ITER (gs_iter, node->bm_other_verts) {
1061
BM_vert_normal_update(BLI_gsetIterator_getKey(&gs_iter));
1063
node->flag &= ~PBVH_UpdateNormals;
1076
1113
/* 2 is enough for edge faces - manifold edge */
1077
1114
BLI_buffer_declare_static(BMFace *, edge_loops, BLI_BUFFER_NOP, 2);
1078
1115
BLI_buffer_declare_static(BMFace *, deleted_faces, BLI_BUFFER_NOP, 32);
1116
const int cd_vert_mask_offset = CustomData_get_offset(&bvh->bm->vdata, CD_PAINT_MASK);
1080
1118
int modified = FALSE;
1083
1121
if (mode & PBVH_Collapse) {
1085
BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert) * 2,
1123
BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert *[2]),
1087
short_edge_queue_create(&q, queue_pool, bvh, center, radius);
1088
pbvh_bmesh_collapse_short_edges(bvh, &q, queue_pool, &edge_loops,
1125
EdgeQueueContext eq_ctx = {&q, queue_pool, bvh->bm, cd_vert_mask_offset};
1127
short_edge_queue_create(&eq_ctx, bvh, center, radius);
1128
pbvh_bmesh_collapse_short_edges(&eq_ctx, bvh, &edge_loops,
1089
1129
&deleted_faces);
1090
1130
BLI_heap_free(q.heap, NULL);
1091
1131
BLI_mempool_destroy(queue_pool);
1094
1134
if (mode & PBVH_Subdivide) {
1096
BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert) * 2,
1136
BLI_mempool *queue_pool = BLI_mempool_create(sizeof(BMVert *[2]),
1098
long_edge_queue_create(&q, queue_pool, bvh, center, radius);
1099
pbvh_bmesh_subdivide_long_edges(bvh, &q, queue_pool, &edge_loops);
1138
EdgeQueueContext eq_ctx = {&q, queue_pool, bvh->bm, cd_vert_mask_offset};
1140
long_edge_queue_create(&eq_ctx, bvh, center, radius);
1141
pbvh_bmesh_subdivide_long_edges(&eq_ctx, bvh, &edge_loops);
1100
1142
BLI_heap_free(q.heap, NULL);
1101
1143
BLI_mempool_destroy(queue_pool);
1152
1195
/* Copy out the vertices and assign a temporary index */
1154
GHASH_ITER (gh_iter, node->bm_unique_verts) {
1155
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1197
GSET_ITER (gs_iter, node->bm_unique_verts) {
1198
BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
1156
1199
copy_v3_v3(node->bm_orco[i], v->co);
1157
1200
BM_elem_index_set(v, i); /* set_dirty! */
1160
GHASH_ITER (gh_iter, node->bm_other_verts) {
1161
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1203
GSET_ITER (gs_iter, node->bm_other_verts) {
1204
BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
1162
1205
copy_v3_v3(node->bm_orco[i], v->co);
1163
1206
BM_elem_index_set(v, i); /* set_dirty! */
1263
1327
bm->totloop, bm->totface);
1265
1329
fprintf(stderr, "vertices:\n");
1266
BM_ITER_MESH(v, &iter, bm, BM_VERTS_OF_MESH) {
1330
BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1267
1331
fprintf(stderr, " %d co=(%.3f %.3f %.3f) oflag=%x\n",
1268
1332
BM_elem_index_get(v), v->co[0], v->co[1], v->co[2],
1269
1333
v->oflags[bm->stackdepth - 1].f);
1272
1336
fprintf(stderr, "edges:\n");
1273
BM_ITER_MESH(e, &iter, bm, BM_EDGES_OF_MESH) {
1337
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
1274
1338
fprintf(stderr, " %d v1=%d, v2=%d, oflag=%x\n",
1275
1339
BM_elem_index_get(e),
1276
1340
BM_elem_index_get(e->v1),
1386
1454
/* Check that the vertex is in the node */
1387
BLI_assert(BLI_ghash_haskey(n->bm_unique_verts, v) ^
1388
BLI_ghash_haskey(n->bm_other_verts, v));
1455
BLI_assert(BLI_gset_haskey(n->bm_unique_verts, v) ^
1456
BLI_gset_haskey(n->bm_other_verts, v));
1390
1458
/* Check that the vertex has a node owner */
1391
1459
nv = pbvh_bmesh_node_lookup(bvh, bvh->bm_vert_to_node, v);
1393
1461
/* Check that the vertex's node knows it owns the vert */
1394
BLI_assert(BLI_ghash_haskey(nv->bm_unique_verts, v));
1462
BLI_assert(BLI_gset_haskey(nv->bm_unique_verts, v));
1396
1464
/* Check that the vertex isn't duplicated as an 'other' vert */
1397
BLI_assert(!BLI_ghash_haskey(nv->bm_other_verts, v));
1465
BLI_assert(!BLI_gset_haskey(nv->bm_other_verts, v));
1429
1497
BLI_assert(found);
1500
/* total freak stuff, check if node exists somewhere else */
1502
for (i = 0; i < bvh->totnode; i++) {
1503
PBVHNode *n = &bvh->nodes[i];
1504
if (i != ni && n->bm_unique_verts)
1505
BLI_assert(!BLI_gset_haskey(n->bm_unique_verts, v));
1512
/* check that every vert belongs somewhere */
1514
BM_ITER_MESH (vi, &iter, bvh->bm, BM_VERTS_OF_MESH) {
1515
bool has_unique = false;
1516
for (i = 0; i < bvh->totnode; i++) {
1517
PBVHNode *n = &bvh->nodes[i];
1518
if ((n->bm_unique_verts != NULL) && BLI_gset_haskey(n->bm_unique_verts, vi))
1521
BLI_assert(has_unique);
1525
/* if totvert differs from number of verts inside the hash. hash-totvert is checked above */
1526
BLI_assert(vert_count == bvh->bm->totvert);
1432
1529
/* Check that node elements are recorded in the top level */
1433
1530
for (i = 0; i < bvh->totnode; i++) {
1448
1545
BLI_assert(GET_INT_FROM_POINTER(nip) == (n - bvh->nodes));
1451
GHASH_ITER (gh_iter, n->bm_unique_verts) {
1452
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1548
GSET_ITER (gs_iter, n->bm_unique_verts) {
1549
BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
1453
1550
void *nip = BLI_ghash_lookup(bvh->bm_vert_to_node, v);
1454
1551
BLI_assert(BLI_ghash_haskey(bvh->bm_vert_to_node, v));
1455
BLI_assert(!BLI_ghash_haskey(n->bm_other_verts, v));
1552
BLI_assert(!BLI_gset_haskey(n->bm_other_verts, v));
1456
1553
BLI_assert(GET_INT_FROM_POINTER(nip) == (n - bvh->nodes));
1459
GHASH_ITER (gh_iter, n->bm_other_verts) {
1460
BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
1556
GSET_ITER (gs_iter, n->bm_other_verts) {
1557
BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
1461
1558
BLI_assert(BLI_ghash_haskey(bvh->bm_vert_to_node, v));
1462
1559
BLI_assert(BM_vert_face_count(v) > 0);