BLI: Add "scatter" array_utils function

This function, the opposite of `gather`, would have been useful a
few times in the past. For now, just use it once in sculpt undo code.
This commit is contained in:
Hans Goudey 2023-11-02 16:32:30 +01:00
parent 0f5abc5a8b
commit 6935cd0798
2 changed files with 20 additions and 4 deletions

View File

@ -66,6 +66,23 @@ inline void copy(const Span<T> src,
[&](const int64_t i) { dst[i] = src[i]; });
}
/**
* Fill the specified indices of the destination with the values in the source span.
*/
template<typename T, typename IndexT>
inline void scatter(const Span<T> src,
const Span<IndexT> indices,
MutableSpan<T> dst,
const int64_t grain_size = 4096)
{
BLI_assert(indices.size() == src.size());
threading::parallel_for(indices.index_range(), grain_size, [&](const IndexRange range) {
for (const int64_t i : range) {
dst[indices[i]] = src[i];
}
});
}
/**
* Fill the destination span by gathering indexed values from the `src` array.
*/

View File

@ -1521,10 +1521,9 @@ static void paint_mesh_restore_node(Object *ob,
case SCULPT_UNDO_MASK: {
switch (BKE_pbvh_type(ss->pbvh)) {
case PBVH_FACES: {
const Span<int> verts = BKE_pbvh_node_get_unique_vert_indices(node);
for (const int i : verts.index_range()) {
mask_write.layer[verts[i]] = unode->mask[i];
}
blender::array_utils::scatter(unode->mask.as_span(),
BKE_pbvh_node_get_unique_vert_indices(node),
{mask_write.layer, ss->totvert});
break;
}
case PBVH_BMESH: {