diff options
author | Jacques Lucke <jacques@blender.org> | 2022-09-24 12:41:08 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-09-24 12:41:08 +0300 |
commit | c25df02ac3036449081701349d36d2f16b2c92f2 (patch) | |
tree | 29603afd3baced747546f4a841cbd4ab73bc7052 /source/blender | |
parent | 8422da13c929f65850a723794a27baa924929377 (diff) |
Cleanup: simplify accessing mesh looptris
Diffstat (limited to 'source/blender')
13 files changed, 35 insertions, 51 deletions
diff --git a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc index 12f42dbc4ec..3c492e2e167 100644 --- a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc +++ b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc @@ -194,8 +194,7 @@ static openvdb::FloatGrid::Ptr remesh_voxel_level_set_create(const Mesh *mesh, { const Span<MVert> verts = mesh->verts(); const Span<MLoop> loops = mesh->loops(); - Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(mesh), - BKE_mesh_runtime_looptri_len(mesh)}; + const Span<MLoopTri> looptris = mesh->looptris(); std::vector<openvdb::Vec3s> points(mesh->totvert); std::vector<openvdb::Vec3I> triangles(looptris.size()); diff --git a/source/blender/blenkernel/intern/mesh_runtime.cc b/source/blender/blenkernel/intern/mesh_runtime.cc index 782657428f5..062d6cdb952 100644 --- a/source/blender/blenkernel/intern/mesh_runtime.cc +++ b/source/blender/blenkernel/intern/mesh_runtime.cc @@ -112,6 +112,13 @@ void BKE_mesh_runtime_clear_cache(Mesh *mesh) BKE_mesh_clear_derived_normals(mesh); } +blender::Span<MLoopTri> Mesh::looptris() const +{ + const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(this); + const int num_looptris = BKE_mesh_runtime_looptri_len(this); + return {looptris, num_looptris}; +} + /** * Ensure the array is large enough * diff --git a/source/blender/blenkernel/intern/mesh_sample.cc b/source/blender/blenkernel/intern/mesh_sample.cc index 1ddac19304d..ed7ae8113a7 100644 --- a/source/blender/blenkernel/intern/mesh_sample.cc +++ b/source/blender/blenkernel/intern/mesh_sample.cc @@ -22,8 +22,7 @@ BLI_NOINLINE static void sample_point_attribute(const Mesh &mesh, const MutableSpan<T> dst) { const Span<MLoop> loops = mesh.loops(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); for (const int i : mask) { const int looptri_index = looptri_indices[i]; @@ -69,8 +68,7 @@ BLI_NOINLINE static void sample_corner_attribute(const Mesh &mesh, const IndexMask mask, const MutableSpan<T> dst) { - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); for (const int i : mask) { const int looptri_index = looptri_indices[i]; @@ -115,8 +113,7 @@ void sample_face_attribute(const Mesh &mesh, const IndexMask mask, const MutableSpan<T> dst) { - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); for (const int i : mask) { const int looptri_index = looptri_indices[i]; @@ -161,8 +158,7 @@ Span<float3> MeshAttributeInterpolator::ensure_barycentric_coords() const Span<MVert> verts = mesh_->verts(); const Span<MLoop> loops = mesh_->loops(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(mesh_), - BKE_mesh_runtime_looptri_len(mesh_)}; + const Span<MLoopTri> looptris = mesh_->looptris(); for (const int i : mask_) { const int looptri_index = looptri_indices_[i]; @@ -191,8 +187,7 @@ Span<float3> MeshAttributeInterpolator::ensure_nearest_weights() const Span<MVert> verts = mesh_->verts(); const Span<MLoop> loops = mesh_->loops(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(mesh_), - BKE_mesh_runtime_looptri_len(mesh_)}; + const Span<MLoopTri> looptris = mesh_->looptris(); for (const int i : mask_) { const int looptri_index = looptri_indices_[i]; @@ -265,8 +260,7 @@ int sample_surface_points_spherical(RandomNumberGenerator &rng, { const Span<MVert> verts = mesh.verts(); const Span<MLoop> loops = mesh.loops(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); const float sample_radius_sq = pow2f(sample_radius); const float sample_plane_area = M_PI * sample_radius_sq; @@ -363,8 +357,7 @@ int sample_surface_points_projected( { const Span<MVert> verts = mesh.verts(); const Span<MLoop> loops = mesh.loops(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); int point_count = 0; for ([[maybe_unused]] const int _ : IndexRange(tries_num)) { diff --git a/source/blender/editors/curves/intern/curves_ops.cc b/source/blender/editors/curves/intern/curves_ops.cc index 54d91ccfc90..f52e11276e9 100644 --- a/source/blender/editors/curves/intern/curves_ops.cc +++ b/source/blender/editors/curves/intern/curves_ops.cc @@ -232,8 +232,7 @@ static void try_convert_single_object(Object &curves_ob, BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh); }); const Span<float3> positions_cu = curves.positions(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&surface_me), - BKE_mesh_runtime_looptri_len(&surface_me)}; + const Span<MLoopTri> looptris = surface_me.looptris(); if (looptris.is_empty()) { *r_could_not_convert_some_curves = true; @@ -545,8 +544,7 @@ static void snap_curves_to_surface_exec_object(Object &curves_ob, const Mesh &surface_mesh = *static_cast<const Mesh *>(surface_ob.data); const Span<MVert> verts = surface_mesh.verts(); const Span<MLoop> loops = surface_mesh.loops(); - const Span<MLoopTri> surface_looptris = {BKE_mesh_runtime_looptri_ensure(&surface_mesh), - BKE_mesh_runtime_looptri_len(&surface_mesh)}; + const Span<MLoopTri> surface_looptris = surface_mesh.looptris(); VArraySpan<float2> surface_uv_map; if (curves_id.surface_uv_map != nullptr) { const bke::AttributeAccessor surface_attributes = surface_mesh.attributes(); diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_add.cc b/source/blender/editors/sculpt_paint/curves_sculpt_add.cc index b5d739ae08e..00130e6d19e 100644 --- a/source/blender/editors/sculpt_paint/curves_sculpt_add.cc +++ b/source/blender/editors/sculpt_paint/curves_sculpt_add.cc @@ -144,8 +144,7 @@ struct AddOperationExecutor { } surface_verts_eval_ = surface_eval_->verts(); surface_loops_eval_ = surface_eval_->loops(); - surface_looptris_eval_ = {BKE_mesh_runtime_looptri_ensure(surface_eval_), - BKE_mesh_runtime_looptri_len(surface_eval_)}; + surface_looptris_eval_ = surface_eval_->looptris(); BKE_bvhtree_from_mesh_get(&surface_bvh_eval_, surface_eval_, BVHTREE_FROM_LOOPTRI, 2); BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh_eval_); }); @@ -206,8 +205,7 @@ struct AddOperationExecutor { return; } - const Span<MLoopTri> surface_looptris_orig = {BKE_mesh_runtime_looptri_ensure(&surface_orig), - BKE_mesh_runtime_looptri_len(&surface_orig)}; + const Span<MLoopTri> surface_looptris_orig = surface_orig.looptris(); /* Find normals. */ if (!CustomData_has_layer(&surface_orig.ldata, CD_NORMAL)) { diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_density.cc b/source/blender/editors/sculpt_paint/curves_sculpt_density.cc index a37eb4bb560..1e598e6bc5b 100644 --- a/source/blender/editors/sculpt_paint/curves_sculpt_density.cc +++ b/source/blender/editors/sculpt_paint/curves_sculpt_density.cc @@ -132,8 +132,7 @@ struct DensityAddOperationExecutor { BKE_bvhtree_from_mesh_get(&surface_bvh_eval_, surface_eval_, BVHTREE_FROM_LOOPTRI, 2); BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh_eval_); }); - surface_looptris_eval_ = {BKE_mesh_runtime_looptri_ensure(surface_eval_), - BKE_mesh_runtime_looptri_len(surface_eval_)}; + surface_looptris_eval_ = surface_eval_->looptris(); /* Find UV map. */ VArraySpan<float2> surface_uv_map; if (curves_id_orig_->surface_uv_map != nullptr) { @@ -265,8 +264,7 @@ struct DensityAddOperationExecutor { reinterpret_cast<const float3 *>(CustomData_get_layer(&surface_orig_->ldata, CD_NORMAL)), surface_orig_->totloop}; - const Span<MLoopTri> surface_looptris_orig = {BKE_mesh_runtime_looptri_ensure(surface_orig_), - BKE_mesh_runtime_looptri_len(surface_orig_)}; + const Span<MLoopTri> surface_looptris_orig = surface_orig_->looptris(); const geometry::ReverseUVSampler reverse_uv_sampler{surface_uv_map, surface_looptris_orig}; geometry::AddCurvesOnMeshInputs add_inputs; diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_puff.cc b/source/blender/editors/sculpt_paint/curves_sculpt_puff.cc index ec69aae372c..b4e949106e7 100644 --- a/source/blender/editors/sculpt_paint/curves_sculpt_puff.cc +++ b/source/blender/editors/sculpt_paint/curves_sculpt_puff.cc @@ -121,8 +121,7 @@ struct PuffOperationExecutor { surface_verts_ = surface_->verts(); surface_loops_ = surface_->loops(); - surface_looptris_ = {BKE_mesh_runtime_looptri_ensure(surface_), - BKE_mesh_runtime_looptri_len(surface_)}; + surface_looptris_ = surface_->looptris(); BKE_bvhtree_from_mesh_get(&surface_bvh_, surface_, BVHTREE_FROM_LOOPTRI, 2); BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh_); }); diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_slide.cc b/source/blender/editors/sculpt_paint/curves_sculpt_slide.cc index 1108f5c72a9..c607f8f3b69 100644 --- a/source/blender/editors/sculpt_paint/curves_sculpt_slide.cc +++ b/source/blender/editors/sculpt_paint/curves_sculpt_slide.cc @@ -178,8 +178,7 @@ struct SlideOperationExecutor { report_empty_original_surface(stroke_extension.reports); return; } - surface_looptris_orig_ = {BKE_mesh_runtime_looptri_ensure(surface_orig_), - BKE_mesh_runtime_looptri_len(surface_orig_)}; + surface_looptris_orig_ = surface_orig_->looptris(); surface_uv_map_orig_ = surface_orig_->attributes().lookup<float2>(uv_map_name, ATTR_DOMAIN_CORNER); if (surface_uv_map_orig_.is_empty()) { @@ -205,8 +204,7 @@ struct SlideOperationExecutor { report_empty_evaluated_surface(stroke_extension.reports); return; } - surface_looptris_eval_ = {BKE_mesh_runtime_looptri_ensure(surface_eval_), - BKE_mesh_runtime_looptri_len(surface_eval_)}; + surface_looptris_eval_ = surface_eval_->looptris(); surface_verts_eval_ = surface_eval_->verts(); surface_loops_eval_ = surface_eval_->loops(); surface_uv_map_eval_ = surface_eval_->attributes().lookup<float2>(uv_map_name, diff --git a/source/blender/geometry/intern/mesh_to_volume.cc b/source/blender/geometry/intern/mesh_to_volume.cc index b1c7be38609..580350bc547 100644 --- a/source/blender/geometry/intern/mesh_to_volume.cc +++ b/source/blender/geometry/intern/mesh_to_volume.cc @@ -30,12 +30,8 @@ class OpenVDBMeshAdapter { }; OpenVDBMeshAdapter::OpenVDBMeshAdapter(const Mesh &mesh, float4x4 transform) - : verts_(mesh.verts()), loops_(mesh.loops()), transform_(transform) + : verts_(mesh.verts()), loops_(mesh.loops()), looptris_(mesh.looptris()), transform_(transform) { - /* This only updates a cache and can be considered to be logically const. */ - const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(&mesh); - const int looptris_len = BKE_mesh_runtime_looptri_len(&mesh); - looptris_ = Span(looptris, looptris_len); } size_t OpenVDBMeshAdapter::polygonCount() const diff --git a/source/blender/makesdna/DNA_mesh_types.h b/source/blender/makesdna/DNA_mesh_types.h index ecf716d23ea..2a17dbab8b3 100644 --- a/source/blender/makesdna/DNA_mesh_types.h +++ b/source/blender/makesdna/DNA_mesh_types.h @@ -360,6 +360,10 @@ typedef struct Mesh { /** Write access to vertex group data. */ blender::MutableSpan<MDeformVert> deform_verts_for_write(); + /** + * Cached triangulation of the mesh. + */ + blender::Span<MLoopTri> looptris() const; #endif } Mesh; diff --git a/source/blender/nodes/geometry/nodes/node_geo_deform_curves_on_surface.cc b/source/blender/nodes/geometry/nodes/node_geo_deform_curves_on_surface.cc index 3f6155460ed..a12ae9bbb92 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_deform_curves_on_surface.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_deform_curves_on_surface.cc @@ -312,10 +312,8 @@ static void node_geo_exec(GeoNodeExecParams params) ATTR_DOMAIN_POINT); const Span<float2> surface_uv_coords = curves.surface_uv_coords(); - const Span<MLoopTri> looptris_orig{BKE_mesh_runtime_looptri_ensure(surface_mesh_orig), - BKE_mesh_runtime_looptri_len(surface_mesh_orig)}; - const Span<MLoopTri> looptris_eval{BKE_mesh_runtime_looptri_ensure(surface_mesh_eval), - BKE_mesh_runtime_looptri_len(surface_mesh_eval)}; + const Span<MLoopTri> looptris_orig = surface_mesh_orig->looptris(); + const Span<MLoopTri> looptris_eval = surface_mesh_eval->looptris(); const ReverseUVSampler reverse_uv_sampler_orig{uv_map_orig, looptris_orig}; const ReverseUVSampler reverse_uv_sampler_eval{uv_map_eval, looptris_eval}; diff --git a/source/blender/nodes/geometry/nodes/node_geo_distribute_points_on_faces.cc b/source/blender/nodes/geometry/nodes/node_geo_distribute_points_on_faces.cc index cdcb16985ac..2ea9958bbd8 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_distribute_points_on_faces.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_distribute_points_on_faces.cc @@ -107,8 +107,7 @@ static void sample_mesh_surface(const Mesh &mesh, { const Span<MVert> verts = mesh.verts(); const Span<MLoop> loops = mesh.loops(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); for (const int looptri_index : looptris.index_range()) { const MLoopTri &looptri = looptris[looptri_index]; @@ -204,8 +203,7 @@ BLI_NOINLINE static void update_elimination_mask_based_on_density_factors( const Span<int> looptri_indices, const MutableSpan<bool> elimination_mask) { - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); for (const int i : bary_coords.index_range()) { if (elimination_mask[i]) { continue; @@ -352,8 +350,7 @@ BLI_NOINLINE static void compute_attribute_outputs(const Mesh &mesh, const Span<MVert> verts = mesh.verts(); const Span<MLoop> loops = mesh.loops(); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); for (const int i : bary_coords.index_range()) { const int looptri_index = looptri_indices[i]; diff --git a/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc b/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc index 691e3101d3d..132b02031dc 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc @@ -149,8 +149,7 @@ static void get_closest_mesh_polys(const Mesh &mesh, Array<int> looptri_indices(positions.size()); get_closest_mesh_looptris(mesh, positions, mask, looptri_indices, r_distances_sq, r_positions); - const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&mesh), - BKE_mesh_runtime_looptri_len(&mesh)}; + const Span<MLoopTri> looptris = mesh.looptris(); for (const int i : mask) { const MLoopTri &looptri = looptris[looptri_indices[i]]; |