From 4b2ea18ec90bf0ccacbcba07c603f4f69e2312be Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 4 Apr 2023 22:12:17 +0200 Subject: [PATCH] Cleanup: Deduplicate OffsetIndices utility for meshes and curves The "reverse map" of corners to faces and points to curves is the same for meshes and curves now. Move it to the offset indices header to reflect this. This unification can go further in the future, but I'd rather wait until the design is clearer for now. Pull Request: https://projects.blender.org/blender/blender/pulls/106570 --- source/blender/blenkernel/intern/curves_geometry.cc | 7 +------ source/blender/blenkernel/intern/mesh_mapping.cc | 6 +----- source/blender/blenlib/BLI_offset_indices.hh | 6 ++++++ source/blender/blenlib/intern/offset_indices.cc | 10 ++++++++++ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc index 7e3626da5ef..829bcfbf91b 100644 --- a/source/blender/blenkernel/intern/curves_geometry.cc +++ b/source/blender/blenkernel/intern/curves_geometry.cc @@ -549,13 +549,8 @@ IndexMask CurvesGeometry::indices_for_curve_type(const CurveType type, Array CurvesGeometry::point_to_curve_map() const { - const OffsetIndices points_by_curve = this->points_by_curve(); Array map(this->points_num()); - threading::parallel_for(this->curves_range(), 1024, [&](const IndexRange range) { - for (const int i_curve : range) { - map.as_mutable_span().slice(points_by_curve[i_curve]).fill(i_curve); - } - }); + offset_indices::build_reverse_map(this->points_by_curve(), map); return map; } diff --git a/source/blender/blenkernel/intern/mesh_mapping.cc b/source/blender/blenkernel/intern/mesh_mapping.cc index b57164fae1e..1710c0b98bb 100644 --- a/source/blender/blenkernel/intern/mesh_mapping.cc +++ b/source/blender/blenkernel/intern/mesh_mapping.cc @@ -526,11 +526,7 @@ namespace blender::bke::mesh_topology { Array build_loop_to_poly_map(const OffsetIndices polys) { Array map(polys.total_size()); - threading::parallel_for(polys.index_range(), 1024, [&](IndexRange range) { - for (const int64_t poly_i : range) { - map.as_mutable_span().slice(polys[poly_i]).fill(int(poly_i)); - } - }); + offset_indices::build_reverse_map(polys, map); return map; } diff --git a/source/blender/blenlib/BLI_offset_indices.hh b/source/blender/blenlib/BLI_offset_indices.hh index b374be8c43b..3fa8ce9dd2a 100644 --- a/source/blender/blenlib/BLI_offset_indices.hh +++ b/source/blender/blenlib/BLI_offset_indices.hh @@ -97,6 +97,12 @@ template class OffsetIndices { */ void accumulate_counts_to_offsets(MutableSpan counts_to_offsets, int start_offset = 0); +/** + * Create a map from indexed elements to the source indices, in other words from the larger array + * to the smaller array. + */ +void build_reverse_map(OffsetIndices offsets, MutableSpan r_map); + } // namespace blender::offset_indices namespace blender { diff --git a/source/blender/blenlib/intern/offset_indices.cc b/source/blender/blenlib/intern/offset_indices.cc index 2ac11fe631e..8b38d630b05 100644 --- a/source/blender/blenlib/intern/offset_indices.cc +++ b/source/blender/blenlib/intern/offset_indices.cc @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #include "BLI_offset_indices.hh" +#include "BLI_task.hh" namespace blender::offset_indices { @@ -16,4 +17,13 @@ void accumulate_counts_to_offsets(MutableSpan counts_to_offsets, const int counts_to_offsets.last() = offset; } +void build_reverse_map(OffsetIndices offsets, MutableSpan r_map) +{ + threading::parallel_for(offsets.index_range(), 1024, [&](const IndexRange range) { + for (const int64_t i : range) { + r_map.slice(offsets[i]).fill(i); + } + }); +} + } // namespace blender::offset_indices