From 52bd028bd83a3656973c93f2fce32c53bec7f097 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 1 Jul 2024 16:38:35 -0400 Subject: [PATCH] Cleanup: Sculpt: Move utilities to gather grids and BMesh normals Turns out these functions are useful elsewhere too. --- .../editors/sculpt_paint/brushes/inflate.cc | 31 ++----------------- .../editors/sculpt_paint/mesh_brush_common.hh | 7 ++++- source/blender/editors/sculpt_paint/sculpt.cc | 26 ++++++++++++++++ 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/source/blender/editors/sculpt_paint/brushes/inflate.cc b/source/blender/editors/sculpt_paint/brushes/inflate.cc index c3d788f4f67..e30986ee909 100644 --- a/source/blender/editors/sculpt_paint/brushes/inflate.cc +++ b/source/blender/editors/sculpt_paint/brushes/inflate.cc @@ -90,23 +90,6 @@ static void calc_faces(const Sculpt &sd, write_translations(sd, object, positions_eval, verts, translations, positions_orig); } -static BLI_NOINLINE void gather_normals(const SubdivCCG &subdiv_ccg, - const Span grids, - const MutableSpan normals) -{ - const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg); - const Span elems = subdiv_ccg.grids; - BLI_assert(grids.size() * key.grid_area == normals.size()); - - for (const int i : grids.index_range()) { - CCGElem *elem = elems[grids[i]]; - const int start = i * key.grid_area; - for (const int offset : IndexRange(key.grid_area)) { - normals[start + offset] = CCG_elem_offset_no(key, elem, offset); - } - } -} - static void calc_grids(const Sculpt &sd, Object &object, const Brush &brush, @@ -149,7 +132,7 @@ static void calc_grids(const Sculpt &sd, tls.translations.reinitialize(grid_verts_num); const MutableSpan translations = tls.translations; - gather_normals(subdiv_ccg, grids, translations); + gather_grids_normals(subdiv_ccg, grids, translations); apply_scale(translations, scale); scale_translations(translations, factors); @@ -157,16 +140,6 @@ static void calc_grids(const Sculpt &sd, apply_translations(translations, grids, subdiv_ccg); } -static BLI_NOINLINE void gather_normals(const Set &verts, - const MutableSpan normals) -{ - int i = 0; - for (const BMVert *vert : verts) { - normals[i] = vert->no; - i++; - } -} - static void calc_bmesh(const Sculpt &sd, Object &object, const Brush &brush, @@ -206,7 +179,7 @@ static void calc_bmesh(const Sculpt &sd, tls.translations.reinitialize(verts.size()); const MutableSpan translations = tls.translations; - gather_normals(verts, translations); + gather_bmesh_normals(verts, translations); apply_scale(translations, scale); scale_translations(translations, factors); diff --git a/source/blender/editors/sculpt_paint/mesh_brush_common.hh b/source/blender/editors/sculpt_paint/mesh_brush_common.hh index 959058dd2d9..2c476bf63a5 100644 --- a/source/blender/editors/sculpt_paint/mesh_brush_common.hh +++ b/source/blender/editors/sculpt_paint/mesh_brush_common.hh @@ -65,9 +65,14 @@ void translations_from_offset_and_factors(const float3 &offset, void gather_grids_positions(const SubdivCCG &subdiv_ccg, Span grids, MutableSpan positions); - void gather_bmesh_positions(const Set &verts, MutableSpan positions); +/** Fill the output array with all normals in the grids referenced by the indices. */ +void gather_grids_normals(const SubdivCCG &subdiv_ccg, + Span grids, + MutableSpan normals); +void gather_bmesh_normals(const Set &verts, MutableSpan normals); + /** * Calculate initial influence factors based on vertex visibility. */ diff --git a/source/blender/editors/sculpt_paint/sculpt.cc b/source/blender/editors/sculpt_paint/sculpt.cc index dafef8747eb..53280257e88 100644 --- a/source/blender/editors/sculpt_paint/sculpt.cc +++ b/source/blender/editors/sculpt_paint/sculpt.cc @@ -6645,6 +6645,32 @@ void gather_bmesh_positions(const Set &verts, const MutableSpan grids, + const MutableSpan normals) +{ + const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg); + const Span elems = subdiv_ccg.grids; + BLI_assert(grids.size() * key.grid_area == normals.size()); + + for (const int i : grids.index_range()) { + CCGElem *elem = elems[grids[i]]; + const int start = i * key.grid_area; + for (const int offset : IndexRange(key.grid_area)) { + normals[start + offset] = CCG_elem_offset_no(key, elem, offset); + } + } +} + +void gather_bmesh_normals(const Set &verts, const MutableSpan normals) +{ + int i = 0; + for (const BMVert *vert : verts) { + normals[i] = vert->no; + i++; + } +} + void fill_factor_from_hide(const Mesh &mesh, const Span verts, const MutableSpan r_factors)