From 98eecfcff0a4980ccb575a24d209736767964337 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 29 May 2024 08:58:04 -0400 Subject: [PATCH] Cleanup: Sculpt: Use C++ Vector for vertex neighbors --- source/blender/editors/sculpt_paint/sculpt.cc | 53 ++++--------------- .../editors/sculpt_paint/sculpt_expand.cc | 4 +- .../editors/sculpt_paint/sculpt_intern.hh | 19 +++---- .../editors/sculpt_paint/sculpt_ops.cc | 2 +- 4 files changed, 19 insertions(+), 59 deletions(-) diff --git a/source/blender/editors/sculpt_paint/sculpt.cc b/source/blender/editors/sculpt_paint/sculpt.cc index b08017c2e5c..a926d36c91d 100644 --- a/source/blender/editors/sculpt_paint/sculpt.cc +++ b/source/blender/editors/sculpt_paint/sculpt.cc @@ -661,39 +661,12 @@ static void sculpt_vertex_neighbor_add(SculptVertexNeighborIter *iter, PBVHVertRef neighbor, int neighbor_index) { - for (int i = 0; i < iter->size; i++) { - if (iter->neighbors[i].i == neighbor.i) { - return; - } + if (iter->neighbors.contains(neighbor)) { + return; } - if (iter->size >= iter->capacity) { - iter->capacity += SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY; - - if (iter->neighbors == iter->neighbors_fixed) { - iter->neighbors = static_cast( - MEM_mallocN(iter->capacity * sizeof(PBVHVertRef), "neighbor array")); - memcpy(iter->neighbors, iter->neighbors_fixed, sizeof(PBVHVertRef) * iter->size); - } - else { - iter->neighbors = static_cast(MEM_reallocN_id( - iter->neighbors, iter->capacity * sizeof(PBVHVertRef), "neighbor array")); - } - - if (iter->neighbor_indices == iter->neighbor_indices_fixed) { - iter->neighbor_indices = static_cast( - MEM_mallocN(iter->capacity * sizeof(int), "neighbor array")); - memcpy(iter->neighbor_indices, iter->neighbor_indices_fixed, sizeof(int) * iter->size); - } - else { - iter->neighbor_indices = static_cast( - MEM_reallocN_id(iter->neighbor_indices, iter->capacity * sizeof(int), "neighbor array")); - } - } - - iter->neighbors[iter->size] = neighbor; - iter->neighbor_indices[iter->size] = neighbor_index; - iter->size++; + iter->neighbors.append(neighbor); + iter->neighbor_indices.append(neighbor_index); } static void sculpt_vertex_neighbors_get_bmesh(PBVHVertRef vertex, SculptVertexNeighborIter *iter) @@ -701,11 +674,9 @@ static void sculpt_vertex_neighbors_get_bmesh(PBVHVertRef vertex, SculptVertexNe BMVert *v = (BMVert *)vertex.i; BMIter liter; BMLoop *l; - iter->size = 0; iter->num_duplicates = 0; - iter->capacity = SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY; - iter->neighbors = iter->neighbors_fixed; - iter->neighbor_indices = iter->neighbor_indices_fixed; + iter->neighbors.clear(); + iter->neighbor_indices.clear(); BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) { const BMVert *adj_v[2] = {l->prev->v, l->next->v}; @@ -723,11 +694,9 @@ static void sculpt_vertex_neighbors_get_faces(const SculptSession &ss, PBVHVertRef vertex, SculptVertexNeighborIter *iter) { - iter->size = 0; iter->num_duplicates = 0; - iter->capacity = SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY; - iter->neighbors = iter->neighbors_fixed; - iter->neighbor_indices = iter->neighbor_indices_fixed; + iter->neighbors.clear(); + iter->neighbor_indices.clear(); for (const int face_i : ss.vert_to_face_map[vertex.i]) { if (ss.hide_poly && ss.hide_poly[face_i]) { @@ -774,11 +743,9 @@ static void sculpt_vertex_neighbors_get_grids(const SculptSession &ss, SubdivCCGNeighbors neighbors; BKE_subdiv_ccg_neighbor_coords_get(*ss.subdiv_ccg, coord, include_duplicates, neighbors); - iter->size = 0; iter->num_duplicates = neighbors.num_duplicates; - iter->capacity = SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY; - iter->neighbors = iter->neighbors_fixed; - iter->neighbor_indices = iter->neighbor_indices_fixed; + iter->neighbors.clear(); + iter->neighbor_indices.clear(); for (const int i : neighbors.coords.index_range()) { int v = neighbors.coords[i].grid_index * key->grid_area + diff --git a/source/blender/editors/sculpt_paint/sculpt_expand.cc b/source/blender/editors/sculpt_paint/sculpt_expand.cc index df80a63e8f6..efc22703c7e 100644 --- a/source/blender/editors/sculpt_paint/sculpt_expand.cc +++ b/source/blender/editors/sculpt_paint/sculpt_expand.cc @@ -573,8 +573,8 @@ static Array sculpt_expand_normal_falloff_create(Object &ob, } SCULPT_VERTEX_NEIGHBORS_ITER_END(ni); - if (ni.size > 0.0f) { - dists[i] = avg / ni.size; + if (ni.neighbors.size() > 0.0f) { + dists[i] = avg / ni.neighbors.size(); } } } diff --git a/source/blender/editors/sculpt_paint/sculpt_intern.hh b/source/blender/editors/sculpt_paint/sculpt_intern.hh index 8c1b3bc03b5..5900b96ac9e 100644 --- a/source/blender/editors/sculpt_paint/sculpt_intern.hh +++ b/source/blender/editors/sculpt_paint/sculpt_intern.hh @@ -80,13 +80,8 @@ struct SculptCursorGeometryInfo { struct SculptVertexNeighborIter { /* Storage */ - PBVHVertRef *neighbors; - int *neighbor_indices; - int size; - int capacity; - - PBVHVertRef neighbors_fixed[SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY]; - int neighbor_indices_fixed[SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY]; + blender::Vector neighbors; + blender::Vector neighbor_indices; /* Internal iterator. */ int num_duplicates; @@ -893,7 +888,7 @@ void SCULPT_vertex_neighbors_get(const SculptSession &ss, /** Iterator over neighboring vertices. */ #define SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN(ss, v_index, neighbor_iterator) \ SCULPT_vertex_neighbors_get(ss, v_index, false, &neighbor_iterator); \ - for (neighbor_iterator.i = 0; neighbor_iterator.i < neighbor_iterator.size; \ + for (neighbor_iterator.i = 0; neighbor_iterator.i < neighbor_iterator.neighbors.size(); \ neighbor_iterator.i++) \ { \ neighbor_iterator.vertex = neighbor_iterator.neighbors[neighbor_iterator.i]; \ @@ -905,19 +900,17 @@ void SCULPT_vertex_neighbors_get(const SculptSession &ss, */ #define SCULPT_VERTEX_DUPLICATES_AND_NEIGHBORS_ITER_BEGIN(ss, v_index, neighbor_iterator) \ SCULPT_vertex_neighbors_get(ss, v_index, true, &neighbor_iterator); \ - for (neighbor_iterator.i = neighbor_iterator.size - 1; neighbor_iterator.i >= 0; \ + for (neighbor_iterator.i = neighbor_iterator.neighbors.size() - 1; neighbor_iterator.i >= 0; \ neighbor_iterator.i--) \ { \ neighbor_iterator.vertex = neighbor_iterator.neighbors[neighbor_iterator.i]; \ neighbor_iterator.index = neighbor_iterator.neighbor_indices[neighbor_iterator.i]; \ neighbor_iterator.is_duplicate = (neighbor_iterator.i >= \ - neighbor_iterator.size - neighbor_iterator.num_duplicates); + neighbor_iterator.neighbors.size() - \ + neighbor_iterator.num_duplicates); #define SCULPT_VERTEX_NEIGHBORS_ITER_END(neighbor_iterator) \ } \ - if (neighbor_iterator.neighbors != neighbor_iterator.neighbors_fixed) { \ - MEM_freeN(neighbor_iterator.neighbors); \ - } \ ((void)0) PBVHVertRef SCULPT_active_vertex_get(const SculptSession &ss); diff --git a/source/blender/editors/sculpt_paint/sculpt_ops.cc b/source/blender/editors/sculpt_paint/sculpt_ops.cc index 8a26c5493ad..2bb9301c8ba 100644 --- a/source/blender/editors/sculpt_paint/sculpt_ops.cc +++ b/source/blender/editors/sculpt_paint/sculpt_ops.cc @@ -612,7 +612,7 @@ void SCULPT_geometry_preview_lines_update(bContext *C, SculptSession &ss, float SculptVertexNeighborIter ni; SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, from_v, ni) { - if (totpoints + (ni.size * 2) < max_preview_verts) { + if (totpoints + (ni.neighbors.size() * 2) < max_preview_verts) { PBVHVertRef to_v = ni.vertex; int to_v_i = ni.index; ss.preview_vert_list[totpoints] = from_v;