diff options
Diffstat (limited to 'source/blender/blenkernel/intern/pbvh_bmesh.c')
-rw-r--r-- | source/blender/blenkernel/intern/pbvh_bmesh.c | 379 |
1 files changed, 141 insertions, 238 deletions
diff --git a/source/blender/blenkernel/intern/pbvh_bmesh.c b/source/blender/blenkernel/intern/pbvh_bmesh.c index ff4cae09437..07f1874e1e4 100644 --- a/source/blender/blenkernel/intern/pbvh_bmesh.c +++ b/source/blender/blenkernel/intern/pbvh_bmesh.c @@ -87,17 +87,16 @@ static void pbvh_bmesh_node_finalize(PBVH *bvh, int node_index, const int cd_ver GSET_ITER (gs_iter, n->bm_faces) { BMFace *f = BLI_gsetIterator_getKey(&gs_iter); - BMLoop *l_iter; - BMLoop *l_first; - BMVert *v; /* Update ownership of faces */ BM_ELEM_CD_SET_INT(f, cd_face_node_offset, node_index); - /* Update vertices */ - l_iter = l_first = BM_FACE_FIRST_LOOP(f); + /* Update vertices */ + BMLoop *l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_iter = l_first; + do { - v = l_iter->v; + BMVert *v = l_iter->v; if (!BLI_gset_haskey(n->bm_unique_verts, v)) { if (BM_ELEM_CD_GET_INT(v, cd_vert_node_offset) != DYNTOPO_NODE_NONE) { BLI_gset_add(n->bm_other_verts, v); @@ -131,15 +130,9 @@ static void pbvh_bmesh_node_finalize(PBVH *bvh, int node_index, const int cd_ver /* Recursively split the node if it exceeds the leaf_limit */ static void pbvh_bmesh_node_split(PBVH *bvh, const BBC *bbc_array, int node_index) { - GSet *empty, *other; - GSetIterator gs_iter; - PBVHNode *n, *c1, *c2; - BB cb; - float mid; - int axis, children; const int cd_vert_node_offset = bvh->cd_vert_node_offset; const int cd_face_node_offset = bvh->cd_face_node_offset; - n = &bvh->nodes[node_index]; + PBVHNode *n = &bvh->nodes[node_index]; if (BLI_gset_size(n->bm_faces) <= bvh->leaf_limit) { /* Node limit not exceeded */ @@ -148,7 +141,9 @@ static void pbvh_bmesh_node_split(PBVH *bvh, const BBC *bbc_array, int node_inde } /* Calculate bounding box around primitive centroids */ + BB cb; BB_reset(&cb); + GSetIterator gs_iter; GSET_ITER (gs_iter, n->bm_faces) { const BMFace *f = BLI_gsetIterator_getKey(&gs_iter); const BBC *bbc = &bbc_array[BM_elem_index_get(f)]; @@ -157,11 +152,11 @@ static void pbvh_bmesh_node_split(PBVH *bvh, const BBC *bbc_array, int node_inde } /* Find widest axis and its midpoint */ - axis = BB_widest_axis(&cb); - mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f; + const int axis = BB_widest_axis(&cb); + const float mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f; /* Add two new child nodes */ - children = bvh->totnode; + const int children = bvh->totnode; n->children_offset = children; pbvh_grow_nodes(bvh, bvh->totnode + 2); @@ -169,8 +164,8 @@ static void pbvh_bmesh_node_split(PBVH *bvh, const BBC *bbc_array, int node_inde n = &bvh->nodes[node_index]; /* Initialize children */ - c1 = &bvh->nodes[children]; - c2 = &bvh->nodes[children + 1]; + PBVHNode *c1 = &bvh->nodes[children], + *c2 = &bvh->nodes[children + 1]; c1->flag |= PBVH_Leaf; c2->flag |= PBVH_Leaf; c1->bm_faces = BLI_gset_ptr_new_ex("bm_faces", BLI_gset_size(n->bm_faces) / 2); @@ -188,7 +183,7 @@ static void pbvh_bmesh_node_split(PBVH *bvh, const BBC *bbc_array, int node_inde } /* Enforce at least one primitive in each node */ - empty = NULL; + GSet *empty = NULL, *other; if (BLI_gset_size(c1->bm_faces) == 0) { empty = c1->bm_faces; other = c2->bm_faces; @@ -242,7 +237,6 @@ static void pbvh_bmesh_node_split(PBVH *bvh, const BBC *bbc_array, int node_inde n->flag &= ~PBVH_Leaf; /* Recurse */ - c1 = c2 = NULL; pbvh_bmesh_node_split(bvh, bbc_array, children); pbvh_bmesh_node_split(bvh, bbc_array, children + 1); @@ -259,30 +253,24 @@ static void pbvh_bmesh_node_split(PBVH *bvh, const BBC *bbc_array, int node_inde /* Recursively split the node if it exceeds the leaf_limit */ static bool pbvh_bmesh_node_limit_ensure(PBVH *bvh, int node_index) { - GSet *bm_faces; - int bm_faces_size; - GSetIterator gs_iter; - BBC *bbc_array; - unsigned int i; - - bm_faces = bvh->nodes[node_index].bm_faces; - bm_faces_size = BLI_gset_size(bm_faces); + GSet *bm_faces = bvh->nodes[node_index].bm_faces; + const int bm_faces_size = BLI_gset_size(bm_faces); if (bm_faces_size <= bvh->leaf_limit) { /* Node limit not exceeded */ return false; } /* For each BMFace, store the AABB and AABB centroid */ - bbc_array = MEM_mallocN(sizeof(BBC) * bm_faces_size, "BBC"); + BBC *bbc_array = MEM_mallocN(sizeof(BBC) * bm_faces_size, "BBC"); + GSetIterator gs_iter; GSET_ITER_INDEX (gs_iter, bm_faces, i) { BMFace *f = BLI_gsetIterator_getKey(&gs_iter); BBC *bbc = &bbc_array[i]; - BMLoop *l_iter; - BMLoop *l_first; BB_reset((BB *)bbc); - l_iter = l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_iter = l_first; do { BB_expand((BB *)bbc, l_iter->v->co); } while ((l_iter = l_iter->next) != l_first); @@ -347,12 +335,11 @@ static BMVert *pbvh_bmesh_vert_create( const int cd_vert_mask_offset) { PBVHNode *node = &bvh->nodes[node_index]; - BMVert *v; BLI_assert((bvh->totnode == 1 || node_index) && node_index <= bvh->totnode); /* avoid initializing customdata because its quite involved */ - v = BM_vert_create(bvh->bm, co, NULL, BM_CREATE_SKIP_CD); + BMVert *v = BM_vert_create(bvh->bm, co, NULL, BM_CREATE_SKIP_CD); CustomData_bmesh_set_default(&bvh->bm->vdata, &v->head.data); /* This value is logged below */ @@ -374,13 +361,12 @@ static BMFace *pbvh_bmesh_face_create( BMVert *v_tri[3], BMEdge *e_tri[3], const BMFace *f_example) { - BMFace *f; PBVHNode *node = &bvh->nodes[node_index]; /* ensure we never add existing face */ BLI_assert(BM_face_exists(v_tri, 3, NULL) == false); - f = BM_face_create(bvh->bm, v_tri, e_tri, 3, f_example, BM_CREATE_NO_DOUBLE); + BMFace *f = BM_face_create(bvh->bm, v_tri, e_tri, 3, f_example, BM_CREATE_NO_DOUBLE); f->head.hflag = f_example->head.hflag; BLI_gset_insert(node->bm_faces, f); @@ -442,14 +428,10 @@ static PBVHNode *pbvh_bmesh_vert_other_node_find(PBVH *bvh, BMVert *v) { BMIter bm_iter; BMFace *f; - PBVHNode *current_node; - - current_node = pbvh_bmesh_node_lookup(bvh, v); + PBVHNode *current_node = pbvh_bmesh_node_lookup(bvh, v); BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) { - PBVHNode *f_node; - - f_node = pbvh_bmesh_node_lookup(bvh, f); + PBVHNode *f_node = pbvh_bmesh_node_lookup(bvh, f); if (f_node != current_node) return f_node; @@ -462,13 +444,10 @@ static void pbvh_bmesh_vert_ownership_transfer( PBVH *bvh, PBVHNode *new_owner, BMVert *v) { - PBVHNode *current_owner; - - current_owner = pbvh_bmesh_node_lookup(bvh, v); + PBVHNode *current_owner = pbvh_bmesh_node_lookup(bvh, v); /* mark node for update */ current_owner->flag |= PBVH_UpdateDrawBuffers | PBVH_UpdateBB; - BLI_assert(current_owner != new_owner); /* Remove current ownership */ @@ -486,21 +465,18 @@ static void pbvh_bmesh_vert_ownership_transfer( static void pbvh_bmesh_vert_remove(PBVH *bvh, BMVert *v) { - PBVHNode *v_node; - BMIter bm_iter; - BMFace *f; - /* never match for first time */ int f_node_index_prev = DYNTOPO_NODE_NONE; - v_node = pbvh_bmesh_node_lookup(bvh, v); + PBVHNode *v_node = pbvh_bmesh_node_lookup(bvh, v); BLI_gset_remove(v_node->bm_unique_verts, v, NULL); BM_ELEM_CD_SET_INT(v, bvh->cd_vert_node_offset, DYNTOPO_NODE_NONE); /* Have to check each neighboring face's node */ + BMIter bm_iter; + BMFace *f; BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) { const int f_node_index = pbvh_bmesh_node_lookup_index(bvh, f); - PBVHNode *f_node; /* faces often share the same node, * quick check to avoid redundant #BLI_gset_remove calls */ @@ -508,7 +484,7 @@ static void pbvh_bmesh_vert_remove(PBVH *bvh, BMVert *v) continue; f_node_index_prev = f_node_index; - f_node = &bvh->nodes[f_node_index]; + PBVHNode *f_node = &bvh->nodes[f_node_index]; f_node->flag |= PBVH_UpdateDrawBuffers | PBVH_UpdateBB; /* Remove current ownership */ @@ -521,19 +497,14 @@ static void pbvh_bmesh_vert_remove(PBVH *bvh, BMVert *v) static void pbvh_bmesh_face_remove(PBVH *bvh, BMFace *f) { - PBVHNode *f_node; - BMVert *v; - - BMLoop *l_iter; - BMLoop *l_first; - - f_node = pbvh_bmesh_node_lookup(bvh, f); + PBVHNode *f_node = pbvh_bmesh_node_lookup(bvh, f); /* Check if any of this face's vertices need to be removed * from the node */ - l_iter = l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_iter = l_first; do { - v = l_iter->v; + BMVert *v = l_iter->v; if (pbvh_bmesh_node_vert_use_count_is_equal(bvh, f_node, v, 1)) { if (BLI_gset_haskey(f_node->bm_unique_verts, v)) { /* Find a different node that uses 'v' */ @@ -630,12 +601,10 @@ typedef struct { * (it's a requirement that edges enter and leave a clean tag state) */ static void pbvh_bmesh_edge_tag_verify(PBVH *bvh) { - int n; - - for (n = 0; n < bvh->totnode; n++) { + for (int n = 0; n < bvh->totnode; n++) { PBVHNode *node = &bvh->nodes[n]; - GSetIterator gs_iter; if (node->bm_faces) { + GSetIterator gs_iter; GSET_ITER (gs_iter, node->bm_faces) { BMFace *f = BLI_gsetIterator_getKey(&gs_iter); BMEdge *e_tri[3]; @@ -668,21 +637,19 @@ static bool edge_queue_tri_in_sphere(const EdgeQueue *q, BMFace *f) closest_on_tri_to_point_v3(c, q->center, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co); /* Check if triangle intersects the sphere */ - return ((len_squared_v3v3(q->center, c) <= q->radius_squared)); + return len_squared_v3v3(q->center, c) <= q->radius_squared; } /* Return true if the vertex mask is less than 1.0, false otherwise */ static bool check_mask(EdgeQueueContext *eq_ctx, BMVert *v) { - return (BM_ELEM_CD_GET_FLOAT(v, eq_ctx->cd_vert_mask_offset) < 1.0f); + return BM_ELEM_CD_GET_FLOAT(v, eq_ctx->cd_vert_mask_offset) < 1.0f; } static void edge_queue_insert( EdgeQueueContext *eq_ctx, BMEdge *e, float priority) { - BMVert **pair; - /* Don't let topology update affect fully masked vertices. This used to * have a 50% mask cutoff, with the reasoning that you can't do a 50% * topology update. But this gives an ugly border in the mesh. The mask @@ -694,7 +661,7 @@ static void edge_queue_insert( !(BM_elem_flag_test_bool(e->v1, BM_ELEM_HIDDEN) || BM_elem_flag_test_bool(e->v2, BM_ELEM_HIDDEN))) { - pair = BLI_mempool_alloc(eq_ctx->pool); + BMVert **pair = BLI_mempool_alloc(eq_ctx->pool); pair[0] = e->v1; pair[1] = e->v2; BLI_heap_insert(eq_ctx->q->heap, priority, pair); @@ -757,19 +724,15 @@ static void long_edge_queue_edge_add_recursive( #define EVEN_GENERATION_SCALE 1.6f const float len_sq_cmp = len_sq * EVEN_EDGELEN_THRESHOLD; - float limit_len_sq; - BMLoop *l_iter; limit_len *= EVEN_GENERATION_SCALE; - limit_len_sq = SQUARE(limit_len); + const float limit_len_sq = SQUARE(limit_len); - l_iter = l_edge; + BMLoop *l_iter = l_edge; do { - float len_sq_other; BMLoop *l_adjacent[2] = {l_iter->next, l_iter->prev}; - int i; - for (i = 0; i < ARRAY_SIZE(l_adjacent); i++) { - len_sq_other = BM_edge_calc_length_squared(l_adjacent[i]->e); + for (int i = 0; i < ARRAY_SIZE(l_adjacent); i++) { + float len_sq_other = BM_edge_calc_length_squared(l_adjacent[i]->e); if (len_sq_other > max_ff(len_sq_cmp, limit_len_sq)) { // edge_queue_insert(eq_ctx, l_adjacent[i]->e, -len_sq_other); long_edge_queue_edge_add_recursive( @@ -813,24 +776,20 @@ static void long_edge_queue_face_add( #endif if (edge_queue_tri_in_sphere(eq_ctx->q, f)) { - BMLoop *l_iter; - BMLoop *l_first; - /* Check each edge of the face */ - l_iter = l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_iter = l_first; do { - { #ifdef USE_EDGEQUEUE_EVEN_SUBDIV - const float len_sq = BM_edge_calc_length_squared(l_iter->e); - if (len_sq > eq_ctx->q->limit_len_squared) { - long_edge_queue_edge_add_recursive( - eq_ctx, l_iter->radial_next, l_iter, - len_sq, eq_ctx->q->limit_len); - } + const float len_sq = BM_edge_calc_length_squared(l_iter->e); + if (len_sq > eq_ctx->q->limit_len_squared) { + long_edge_queue_edge_add_recursive( + eq_ctx, l_iter->radial_next, l_iter, + len_sq, eq_ctx->q->limit_len); + } #else - long_edge_queue_edge_add(eq_ctx, l_iter->e); + long_edge_queue_edge_add(eq_ctx, l_iter->e); #endif - } } while ((l_iter = l_iter->next) != l_first); } } @@ -873,8 +832,6 @@ static void long_edge_queue_create( PBVH *bvh, const float center[3], const float view_normal[3], float radius) { - int n; - eq_ctx->q->heap = BLI_heap_new(); eq_ctx->q->center = center; eq_ctx->q->radius_squared = radius * radius; @@ -894,8 +851,7 @@ static void long_edge_queue_create( pbvh_bmesh_edge_tag_verify(bvh); #endif - - for (n = 0; n < bvh->totnode; n++) { + for (int n = 0; n < bvh->totnode; n++) { PBVHNode *node = &bvh->nodes[n]; /* Check leaf nodes marked for topology update */ @@ -929,8 +885,6 @@ static void short_edge_queue_create( PBVH *bvh, const float center[3], const float view_normal[3], float radius) { - int n; - eq_ctx->q->heap = BLI_heap_new(); eq_ctx->q->center = center; eq_ctx->q->radius_squared = radius * radius; @@ -946,7 +900,7 @@ static void short_edge_queue_create( UNUSED_VARS(view_normal); #endif - for (n = 0; n < bvh->totnode; n++) { + for (int n = 0; n < bvh->totnode; n++) { PBVHNode *node = &bvh->nodes[n]; /* Check leaf nodes marked for topology update */ @@ -979,9 +933,7 @@ static void pbvh_bmesh_split_edge( EdgeQueueContext *eq_ctx, PBVH *bvh, BMEdge *e, BLI_Buffer *edge_loops) { - BMVert *v_new; float co_mid[3], no_mid[3]; - int i, node_index; /* Get all faces adjacent to the edge */ pbvh_bmesh_edge_loops(edge_loops, e); @@ -991,8 +943,8 @@ static void pbvh_bmesh_split_edge( mid_v3_v3v3(no_mid, e->v1->no, e->v2->no); normalize_v3(no_mid); - node_index = BM_ELEM_CD_GET_INT(e->v1, eq_ctx->cd_vert_node_offset); - v_new = pbvh_bmesh_vert_create(bvh, node_index, co_mid, no_mid, eq_ctx->cd_vert_mask_offset); + int node_index = BM_ELEM_CD_GET_INT(e->v1, eq_ctx->cd_vert_node_offset); + BMVert *v_new = pbvh_bmesh_vert_create(bvh, node_index, co_mid, no_mid, eq_ctx->cd_vert_mask_offset); /* update paint mask */ if (eq_ctx->cd_vert_mask_offset != -1) { @@ -1004,17 +956,16 @@ static void pbvh_bmesh_split_edge( } /* For each face, add two new triangles and delete the original */ - for (i = 0; i < edge_loops->count; i++) { + for (int i = 0; i < edge_loops->count; i++) { BMLoop *l_adj = BLI_buffer_at(edge_loops, BMLoop *, i); BMFace *f_adj = l_adj->f; BMFace *f_new; BMVert *v_opp, *v1, *v2; BMVert *v_tri[3]; BMEdge *e_tri[3]; - int ni; BLI_assert(f_adj->len == 3); - ni = BM_ELEM_CD_GET_INT(f_adj, eq_ctx->cd_face_node_offset); + int ni = BM_ELEM_CD_GET_INT(f_adj, eq_ctx->cd_face_node_offset); /* Find the vertex not in the edge */ v_opp = l_adj->prev->v; @@ -1153,11 +1104,7 @@ static void pbvh_bmesh_collapse_edge( BLI_Buffer *deleted_faces, EdgeQueueContext *eq_ctx) { - BMIter bm_iter; - BMFace *f; - BMLoop *l_adj; BMVert *v_del, *v_conn; - int i; float mask_v1 = BM_ELEM_CD_GET_FLOAT(v1, eq_ctx->cd_vert_mask_offset); /* one of the two vertices may be masked, select the correct one for deletion */ @@ -1174,6 +1121,7 @@ static void pbvh_bmesh_collapse_edge( pbvh_bmesh_vert_remove(bvh, v_del); /* Remove all faces adjacent to the edge */ + BMLoop *l_adj; while ((l_adj = e->l)) { BMFace *f_adj = l_adj->f; @@ -1192,16 +1140,17 @@ static void pbvh_bmesh_collapse_edge( * really buy anything. */ BLI_buffer_empty(deleted_faces); + BMIter bm_iter; + BMFace *f; + BM_ITER_ELEM (f, &bm_iter, v_del, BM_FACES_OF_VERT) { BMVert *v_tri[3]; BMFace *existing_face; - PBVHNode *n; - int ni; /* Get vertices, replace use of v_del with v_conn */ // BM_iter_as_array(NULL, BM_VERTS_OF_FACE, f, (void **)v_tri, 3); BM_face_as_array_vert_tri(f, v_tri); - for (i = 0; i < 3; i++) { + for (int i = 0; i < 3; i++) { if (v_tri[i] == v_del) { v_tri[i] = v_conn; } @@ -1217,8 +1166,8 @@ static void pbvh_bmesh_collapse_edge( } else { BMEdge *e_tri[3]; - n = pbvh_bmesh_node_lookup(bvh, f); - ni = n - bvh->nodes; + PBVHNode *n = pbvh_bmesh_node_lookup(bvh, f); + int ni = n - bvh->nodes; bm_edges_from_tri(bvh->bm, v_tri, e_tri); pbvh_bmesh_face_create(bvh, ni, v_tri, e_tri, f); @@ -1232,16 +1181,14 @@ static void pbvh_bmesh_collapse_edge( } /* Delete the tagged faces */ - for (i = 0; i < deleted_faces->count; i++) { + for (int i = 0; i < deleted_faces->count; i++) { BMFace *f_del = BLI_buffer_at(deleted_faces, BMFace *, i); - BMLoop *l_iter; - BMVert *v_tri[3]; - BMEdge *e_tri[3]; - int j; /* Get vertices and edges of face */ BLI_assert(f_del->len == 3); - l_iter = BM_FACE_FIRST_LOOP(f_del); + BMLoop *l_iter = BM_FACE_FIRST_LOOP(f_del); + BMVert *v_tri[3]; + BMEdge *e_tri[3]; v_tri[0] = l_iter->v; e_tri[0] = l_iter->e; l_iter = l_iter->next; v_tri[1] = l_iter->v; e_tri[1] = l_iter->e; l_iter = l_iter->next; v_tri[2] = l_iter->v; e_tri[2] = l_iter->e; @@ -1252,14 +1199,14 @@ static void pbvh_bmesh_collapse_edge( /* Check if any of the face's edges are now unused by any * face, if so delete them */ - for (j = 0; j < 3; j++) { + for (int j = 0; j < 3; j++) { if (BM_edge_is_wire(e_tri[j])) BM_edge_kill(bvh->bm, e_tri[j]); } /* Check if any of the face's vertices are now unused, if so * remove them from the PBVH */ - for (j = 0; j < 3; j++) { + for (int j = 0; j < 3; j++) { if ((v_tri[j] != v_del) && (v_tri[j]->e == NULL)) { BLI_gset_insert(deleted_verts, v_tri[j]); pbvh_bmesh_vert_remove(bvh, v_tri[j]); @@ -1291,18 +1238,14 @@ static bool pbvh_bmesh_collapse_short_edges( PBVH *bvh, BLI_Buffer *deleted_faces) { - float min_len_squared = bvh->bm_min_edge_len * bvh->bm_min_edge_len; - GSet *deleted_verts; + const float min_len_squared = bvh->bm_min_edge_len * bvh->bm_min_edge_len; bool any_collapsed = false; - - deleted_verts = BLI_gset_ptr_new("deleted_verts"); + GSet *deleted_verts = BLI_gset_ptr_new("deleted_verts"); while (!BLI_heap_is_empty(eq_ctx->q->heap)) { BMVert **pair = BLI_heap_popmin(eq_ctx->q->heap); - BMVert *v1 = pair[0], *v2 = pair[1]; - BMEdge *e; - BLI_mempool_free(eq_ctx->pool, pair); + BMVert *v1 = pair[0], *v2 = pair[1]; pair = NULL; /* Check the verts still exist */ @@ -1313,6 +1256,7 @@ static bool pbvh_bmesh_collapse_short_edges( } /* Check that the edge still exists */ + BMEdge *e; if (!(e = BM_edge_exists(v1, v2))) { continue; } @@ -1355,8 +1299,7 @@ bool pbvh_bmesh_node_raycast( bool hit = false; if (use_original && node->bm_tot_ortri) { - int i; - for (i = 0; i < node->bm_tot_ortri; i++) { + for (int i = 0; i < node->bm_tot_ortri; i++) { const int *t = node->bm_ortri[i]; hit |= ray_face_intersection_tri( ray_start, ray_normal, @@ -1395,13 +1338,13 @@ bool BKE_pbvh_bmesh_node_raycast_detail( const float ray_start[3], const float ray_normal[3], float *dist, float *r_detail) { + if (node->flag & PBVH_FullyHidden) + return 0; + GSetIterator gs_iter; bool hit = false; BMFace *f_hit = NULL; - if (node->flag & PBVH_FullyHidden) - return 0; - GSET_ITER (gs_iter, node->bm_faces) { BMFace *f = BLI_gsetIterator_getKey(&gs_iter); @@ -1425,12 +1368,11 @@ bool BKE_pbvh_bmesh_node_raycast_detail( } if (hit) { - float len1, len2, len3; BMVert *v_tri[3]; BM_face_as_array_vert_tri(f_hit, v_tri); - len1 = len_squared_v3v3(v_tri[0]->co, v_tri[1]->co); - len2 = len_squared_v3v3(v_tri[1]->co, v_tri[2]->co); - len3 = len_squared_v3v3(v_tri[2]->co, v_tri[0]->co); + float len1 = len_squared_v3v3(v_tri[0]->co, v_tri[1]->co); + float len2 = len_squared_v3v3(v_tri[1]->co, v_tri[2]->co); + float len3 = len_squared_v3v3(v_tri[2]->co, v_tri[0]->co); /* detail returned will be set to the maximum allowed size, so take max here */ *r_detail = sqrtf(max_fff(len1, len2, len3)); @@ -1442,9 +1384,7 @@ bool BKE_pbvh_bmesh_node_raycast_detail( void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode) { - int n; - - for (n = 0; n < totnode; n++) { + for (int n = 0; n < totnode; n++) { PBVHNode *node = nodes[n]; if (node->flag & PBVH_UpdateNormals) { @@ -1476,25 +1416,18 @@ typedef struct FastNodeBuildInfo { * to a sub part of the arrays */ static void pbvh_bmesh_node_limit_ensure_fast(PBVH *bvh, BMFace **nodeinfo, BBC *bbc_array, FastNodeBuildInfo *node, MemArena *arena) { - BMFace *f; - BB cb; - BBC *bbc; - float mid; - int axis, i; - int end; FastNodeBuildInfo *child1, *child2; - int num_child1, num_child2; - BMFace *tmp; if (node->totface <= bvh->leaf_limit) { return; } /* Calculate bounding box around primitive centroids */ + BB cb; BB_reset(&cb); - for (i = 0; i < node->totface; i++) { - f = nodeinfo[i + node->start]; - bbc = &bbc_array[BM_elem_index_get(f)]; + for (int i = 0; i < node->totface; i++) { + BMFace *f = nodeinfo[i + node->start]; + BBC *bbc = &bbc_array[BM_elem_index_get(f)]; BB_expand(&cb, bbc->bcentroid); } @@ -1502,16 +1435,16 @@ static void pbvh_bmesh_node_limit_ensure_fast(PBVH *bvh, BMFace **nodeinfo, BBC /* initialize the children */ /* Find widest axis and its midpoint */ - axis = BB_widest_axis(&cb); - mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f; + const int axis = BB_widest_axis(&cb); + const float mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f; - num_child1 = 0, num_child2 = 0; + int num_child1 = 0, num_child2 = 0; /* split vertices along the middle line */ - end = node->start + node->totface; - for (i = node->start; i < end - num_child2; i++) { - f = nodeinfo[i]; - bbc = &bbc_array[BM_elem_index_get(f)]; + const int end = node->start + node->totface; + for (int i = node->start; i < end - num_child2; i++) { + BMFace *f = nodeinfo[i]; + BBC *bbc = &bbc_array[BM_elem_index_get(f)]; if (bbc->bcentroid[axis] > mid) { int i_iter = end - num_child2 - 1; @@ -1531,7 +1464,7 @@ static void pbvh_bmesh_node_limit_ensure_fast(PBVH *bvh, BMFace **nodeinfo, BBC } if (candidate != -1) { - tmp = nodeinfo[i]; + BMFace *tmp = nodeinfo[i]; nodeinfo[i] = nodeinfo[candidate]; nodeinfo[candidate] = tmp; /* increase both counts */ @@ -1574,8 +1507,6 @@ static void pbvh_bmesh_node_limit_ensure_fast(PBVH *bvh, BMFace **nodeinfo, BBC pbvh_bmesh_node_limit_ensure_fast(bvh, nodeinfo, bbc_array, child1, arena); pbvh_bmesh_node_limit_ensure_fast(bvh, nodeinfo, bbc_array, child2, arena); - - return; } static void pbvh_bmesh_create_nodes_fast_recursive(PBVH *bvh, BMFace **nodeinfo, BBC *bbc_array, FastNodeBuildInfo *node, int node_index) @@ -1605,12 +1536,6 @@ static void pbvh_bmesh_create_nodes_fast_recursive(PBVH *bvh, BMFace **nodeinfo, const int cd_face_node_offset = bvh->cd_face_node_offset; bool has_visible = false; - int i, end; - BMFace *f; - BMLoop *l_iter; - BMLoop *l_first; - BMVert *v; - BBC *bbc; n->flag = PBVH_Leaf; n->bm_faces = BLI_gset_ptr_new_ex("bm_faces", node->totface); @@ -1621,20 +1546,21 @@ static void pbvh_bmesh_create_nodes_fast_recursive(PBVH *bvh, BMFace **nodeinfo, BB_reset(&n->vb); - end = node->start + node->totface; + const int end = node->start + node->totface; - for (i = node->start; i < end; i++) { - f = nodeinfo[i]; - bbc = &bbc_array[BM_elem_index_get(f)]; + for (int i = node->start; i < end; i++) { + BMFace *f = nodeinfo[i]; + BBC *bbc = &bbc_array[BM_elem_index_get(f)]; /* Update ownership of faces */ BLI_gset_insert(n->bm_faces, f); BM_ELEM_CD_SET_INT(f, cd_face_node_offset, node_index); /* Update vertices */ - l_iter = l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_iter = l_first; do { - v = l_iter->v; + BMVert *v = l_iter->v; if (!BLI_gset_haskey(n->bm_unique_verts, v)) { if (BM_ELEM_CD_GET_INT(v, cd_vert_node_offset) != DYNTOPO_NODE_NONE) { BLI_gset_add(n->bm_other_verts, v); @@ -1654,8 +1580,8 @@ static void pbvh_bmesh_create_nodes_fast_recursive(PBVH *bvh, BMFace **nodeinfo, } BLI_assert(n->vb.bmin[0] <= n->vb.bmax[0] && - n->vb.bmin[1] <= n->vb.bmax[1] && - n->vb.bmin[2] <= n->vb.bmax[2]); + n->vb.bmin[1] <= n->vb.bmax[1] && + n->vb.bmin[2] <= n->vb.bmax[2]); n->orig_vb = n->vb; @@ -1675,16 +1601,6 @@ void BKE_pbvh_build_bmesh( PBVH *bvh, BMesh *bm, bool smooth_shading, BMLog *log, const int cd_vert_node_offset, const int cd_face_node_offset) { - BMIter iter; - BMFace *f; - BMVert *v; - int i; - /* bounding box array of all faces, no need to recalculate every time */ - BBC *bbc_array; - BMFace **nodeinfo; - FastNodeBuildInfo rootnode = {0}; - MemArena *arena; - bvh->cd_vert_node_offset = cd_vert_node_offset; bvh->cd_face_node_offset = cd_face_node_offset; bvh->bm = bm; @@ -1700,18 +1616,20 @@ void BKE_pbvh_build_bmesh( if (smooth_shading) bvh->flags |= PBVH_DYNTOPO_SMOOTH_SHADING; - /* calculate all bounding boxes once for all faces */ - bbc_array = MEM_mallocN(sizeof(BBC) * bm->totface, "BBC"); - nodeinfo = MEM_mallocN(sizeof(*nodeinfo) * bm->totface, "nodeinfo"); - arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "fast PBVH node storage"); + /* bounding box array of all faces, no need to recalculate every time */ + BBC *bbc_array = MEM_mallocN(sizeof(BBC) * bm->totface, "BBC"); + BMFace **nodeinfo = MEM_mallocN(sizeof(*nodeinfo) * bm->totface, "nodeinfo"); + MemArena *arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "fast PBVH node storage"); + BMIter iter; + BMFace *f; + int i; BM_ITER_MESH_INDEX(f, &iter, bm, BM_FACES_OF_MESH, i) { BBC *bbc = &bbc_array[i]; - BMLoop *l_iter; - BMLoop *l_first; + BMLoop *l_first = BM_FACE_FIRST_LOOP(f); + BMLoop *l_iter = l_first; BB_reset((BB *)bbc); - l_iter = l_first = BM_FACE_FIRST_LOOP(f); do { BB_expand((BB *)bbc, l_iter->v->co); } while ((l_iter = l_iter->next) != l_first); @@ -1723,6 +1641,7 @@ void BKE_pbvh_build_bmesh( BM_ELEM_CD_SET_INT(f, cd_face_node_offset, DYNTOPO_NODE_NONE); } + BMVert *v; BM_ITER_MESH(v, &iter, bm, BM_VERTS_OF_MESH) { BM_ELEM_CD_SET_INT(v, cd_vert_node_offset, DYNTOPO_NODE_NONE); } @@ -1731,6 +1650,7 @@ void BKE_pbvh_build_bmesh( bm->elem_index_dirty |= BM_FACE; /* setup root node */ + FastNodeBuildInfo rootnode = {0}; rootnode.totface = bm->totface; /* start recursion, assign faces to nodes accordingly */ @@ -1764,7 +1684,6 @@ bool BKE_pbvh_bmesh_update_topology( const int cd_face_node_offset = bvh->cd_face_node_offset; bool modified = false; - int n; if (view_normal) { BLI_assert(len_squared_v3(view_normal) != 0.0f); @@ -1796,7 +1715,7 @@ bool BKE_pbvh_bmesh_update_topology( } /* Unmark nodes */ - for (n = 0; n < bvh->totnode; n++) { + for (int n = 0; n < bvh->totnode; n++) { PBVHNode *node = &bvh->nodes[n]; if (node->flag & PBVH_Leaf && @@ -1832,23 +1751,21 @@ BLI_INLINE void bm_face_as_array_index_tri(BMFace *f, int r_index[3]) * Skips triangles that are hidden. */ void BKE_pbvh_bmesh_node_save_orig(PBVHNode *node) { - GSetIterator gs_iter; - int i, totvert, tottri; - /* Skip if original coords/triangles are already saved */ if (node->bm_orco) return; - totvert = (BLI_gset_size(node->bm_unique_verts) + - BLI_gset_size(node->bm_other_verts)); + const int totvert = BLI_gset_size(node->bm_unique_verts) + + BLI_gset_size(node->bm_other_verts); - tottri = BLI_gset_size(node->bm_faces); + const int tottri = BLI_gset_size(node->bm_faces); node->bm_orco = MEM_mallocN(sizeof(*node->bm_orco) * totvert, __func__); node->bm_ortri = MEM_mallocN(sizeof(*node->bm_ortri) * tottri, __func__); /* Copy out the vertices and assign a temporary index */ - i = 0; + int i = 0; + GSetIterator gs_iter; GSET_ITER (gs_iter, node->bm_unique_verts) { BMVert *v = BLI_gsetIterator_getKey(&gs_iter); copy_v3_v3(node->bm_orco[i], v->co); @@ -1888,9 +1805,7 @@ void BKE_pbvh_bmesh_node_save_orig(PBVHNode *node) void BKE_pbvh_bmesh_after_stroke(PBVH *bvh) { - int i; - - for (i = 0; i < bvh->totnode; i++) { + for (int i = 0; i < bvh->totnode; i++) { PBVHNode *n = &bvh->nodes[i]; if (n->flag & PBVH_Leaf) { /* Free orco/ortri data */ @@ -1935,15 +1850,11 @@ struct GSet *BKE_pbvh_bmesh_node_faces(PBVHNode *node) static void pbvh_bmesh_print(PBVH *bvh) { - GSetIterator gs_iter; - int n; - BMIter iter; - BMFace *f; - BMVert *v; - fprintf(stderr, "\npbvh=%p\n", bvh); fprintf(stderr, "bm_face_to_node:\n"); + BMIter iter; + BMFace *f; BM_ITER_MESH(f, &iter, bvh->bm, BM_FACES_OF_MESH) { fprintf(stderr, " %d -> %d\n", BM_elem_index_get(v), @@ -1951,17 +1862,19 @@ static void pbvh_bmesh_print(PBVH *bvh) } fprintf(stderr, "bm_vert_to_node:\n"); + BMVert *v; BM_ITER_MESH(v, &iter, bvh->bm, BM_FACES_OF_MESH) { fprintf(stderr, " %d -> %d\n", BM_elem_index_get(v), pbvh_bmesh_node_lookup_index(bvh, v)); } - for (n = 0; n < bvh->totnode; n++) { + for (int n = 0; n < bvh->totnode; n++) { PBVHNode *node = &bvh->nodes[n]; if (!(node->flag & PBVH_Leaf)) continue; + GSetIterator gs_iter; fprintf(stderr, "node %d\n faces:\n", n); GSET_ITER (gs_iter, node->bm_faces) fprintf(stderr, " %d\n", @@ -1979,9 +1892,8 @@ static void pbvh_bmesh_print(PBVH *bvh) static void print_flag_factors(int flag) { - int i; printf("flag=0x%x:\n", flag); - for (i = 0; i < 32; i++) { + for (int i = 0; i < 32; i++) { if (flag & (1 << i)) { printf(" %d (1 << %d)\n", 1 << i, i); } @@ -1994,23 +1906,16 @@ static void print_flag_factors(int flag) static void pbvh_bmesh_verify(PBVH *bvh) { - GSetIterator gs_iter; - int i; - BMIter iter; - BMFace *f; - BMVert *v; - - GSet *faces_all; - GSet *verts_all; - - /* build list of faces & verts to lookup */ - faces_all = BLI_gset_ptr_new_ex(__func__, bvh->bm->totface); - verts_all = BLI_gset_ptr_new_ex(__func__, bvh->bm->totvert); - + GSet *faces_all = BLI_gset_ptr_new_ex(__func__, bvh->bm->totface); + BMFace *f; + BMIter iter; BM_ITER_MESH(f, &iter, bvh->bm, BM_FACES_OF_MESH) { BLI_gset_insert(faces_all, f); } + + GSet *verts_all = BLI_gset_ptr_new_ex(__func__, bvh->bm->totvert); + BMVert *v; BM_ITER_MESH(v, &iter, bvh->bm, BM_VERTS_OF_MESH) { if (BM_ELEM_CD_GET_INT(v, bvh->cd_vert_node_offset) != DYNTOPO_NODE_NONE) { BLI_gset_insert(verts_all, v); @@ -2020,7 +1925,7 @@ static void pbvh_bmesh_verify(PBVH *bvh) /* Check vert/face counts */ { int totface = 0, totvert = 0; - for (i = 0; i < bvh->totnode; i++) { + for (int i = 0; i < bvh->totnode; i++) { PBVHNode *n = &bvh->nodes[i]; totface += n->bm_faces ? BLI_gset_size(n->bm_faces) : 0; totvert += n->bm_unique_verts ? BLI_gset_size(n->bm_unique_verts) : 0; @@ -2062,16 +1967,12 @@ static void pbvh_bmesh_verify(PBVH *bvh) /* Check verts */ BM_ITER_MESH(v, &iter, bvh->bm, BM_VERTS_OF_MESH) { - BMIter bm_iter; - PBVHNode *n; - bool found; - /* vertex isn't tracked */ if (BM_ELEM_CD_GET_INT(v, bvh->cd_vert_node_offset) == DYNTOPO_NODE_NONE) { continue; } - n = pbvh_bmesh_node_lookup(bvh, v); + PBVHNode *n = pbvh_bmesh_node_lookup(bvh, v); /* Check that the vert's node is a leaf */ BLI_assert(n->flag & PBVH_Leaf); @@ -2084,6 +1985,8 @@ static void pbvh_bmesh_verify(PBVH *bvh) /* Check that the vert's node also contains one of the vert's * adjacent faces */ + bool found = false; + BMIter bm_iter; BM_ITER_ELEM (f, &bm_iter, v, BM_FACES_OF_VERT) { if (pbvh_bmesh_node_lookup(bvh, f) == n) { found = true; @@ -2095,13 +1998,12 @@ static void pbvh_bmesh_verify(PBVH *bvh) #if 1 /* total freak stuff, check if node exists somewhere else */ /* Slow */ - for (i = 0; i < bvh->totnode; i++) { + for (int i = 0; i < bvh->totnode; i++) { PBVHNode *n_other = &bvh->nodes[i]; if ((n != n_other) && (n_other->bm_unique_verts)) { BLI_assert(!BLI_gset_haskey(n_other->bm_unique_verts, v)); } } - #endif } @@ -2110,7 +2012,7 @@ static void pbvh_bmesh_verify(PBVH *bvh) /* Slow */ BM_ITER_MESH (vi, &iter, bvh->bm, BM_VERTS_OF_MESH) { bool has_unique = false; - for (i = 0; i < bvh->totnode; i++) { + for (int i = 0; i < bvh->totnode; i++) { PBVHNode *n = &bvh->nodes[i]; if ((n->bm_unique_verts != NULL) && BLI_gset_haskey(n->bm_unique_verts, vi)) has_unique = true; @@ -2124,9 +2026,10 @@ static void pbvh_bmesh_verify(PBVH *bvh) #endif /* Check that node elements are recorded in the top level */ - for (i = 0; i < bvh->totnode; i++) { + for (int i = 0; i < bvh->totnode; i++) { PBVHNode *n = &bvh->nodes[i]; if (n->flag & PBVH_Leaf) { + GSetIterator gs_iter; GSET_ITER (gs_iter, n->bm_faces) { BMFace *f = BLI_gsetIterator_getKey(&gs_iter); |