[Bf-blender-cvs] [76673e5fcfc] master: Cleanup: Use OffsetIndices abstraction in duplicate elements node

Hans Goudey noreply at git.blender.org
Thu Jan 19 20:51:45 CET 2023


Commit: 76673e5fcfc814a8aca2ecd1f5d9cdf37a6a96d5
Author: Hans Goudey
Date:   Thu Jan 19 13:48:54 2023 -0600
Branches: master
https://developer.blender.org/rB76673e5fcfc814a8aca2ecd1f5d9cdf37a6a96d5

Cleanup: Use OffsetIndices abstraction in duplicate elements node

===================================================================

M	source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc

===================================================================

diff --git a/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc b/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc
index 480fd516360..90f089ae3df 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc
@@ -3,6 +3,7 @@
 #include "BLI_array_utils.hh"
 #include "BLI_map.hh"
 #include "BLI_noise.hh"
+#include "BLI_offset_indices.hh"
 #include "BLI_span.hh"
 #include "BLI_task.hh"
 
@@ -75,36 +76,27 @@ static Map<AttributeIDRef, AttributeKind> gather_attributes_without_id(
   return attributes;
 };
 
-static IndexRange range_for_offsets_index(const Span<int> offsets, const int index)
+static OffsetIndices<int> accumulate_counts_to_offsets(const IndexMask selection,
+                                                       const VArray<int> &counts,
+                                                       Array<int> &r_offset_data)
 {
-  return {offsets[index], offsets[index + 1] - offsets[index]};
-}
-
-static Array<int> accumulate_counts_to_offsets(const IndexMask selection,
-                                               const VArray<int> &counts)
-{
-  Array<int> offsets(selection.size() + 1);
-  int total = 0;
-  for (const int i : selection.index_range()) {
-    offsets[i] = total;
-    total += std::max(counts[selection[i]], 0);
-  }
-  offsets.last() = total;
-  return offsets;
+  r_offset_data.reinitialize(selection.size() + 1);
+  counts.materialize_compressed(selection, r_offset_data);
+  offset_indices::accumulate_counts_to_offsets(r_offset_data);
+  return OffsetIndices<int>(r_offset_data);
 }
 
 /* Utility functions for threaded copying of attribute data where possible. */
 template<typename T>
-static void threaded_slice_fill(Span<int> offsets,
+static void threaded_slice_fill(const OffsetIndices<int> offsets,
                                 const IndexMask selection,
-                                Span<T> src,
+                                const Span<T> src,
                                 MutableSpan<T> dst)
 {
-  BLI_assert(offsets.last() == dst.size());
-  BLI_assert(selection.size() == offsets.size() - 1);
-  threading::parallel_for(IndexRange(offsets.size() - 1), 512, [&](IndexRange range) {
+  BLI_assert(offsets.total_size() == dst.size());
+  threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
     for (const int i : range) {
-      dst.slice(range_for_offsets_index(offsets, i)).fill(src[selection[i]]);
+      dst.slice(offsets[i]).fill(src[selection[i]]);
     }
   });
 }
@@ -116,20 +108,20 @@ static void copy_hashed_ids(const Span<int> src, const int hash, MutableSpan<int
   }
 }
 
-static void threaded_id_offset_copy(const Span<int> offsets,
+static void threaded_id_offset_copy(const OffsetIndices<int> offsets,
                                     const Span<int> src,
-                                    MutableSpan<int> dst)
+                                    MutableSpan<int> all_dst)
 {
-  BLI_assert(offsets.last() == dst.size());
-  threading::parallel_for(IndexRange(offsets.size() - 1), 512, [&](IndexRange range) {
+  BLI_assert(offsets.total_size() == all_dst.size());
+  threading::parallel_for(IndexRange(offsets.ranges_num()), 512, [&](IndexRange range) {
     for (const int i : range) {
-      dst[offsets[i]] = src[i];
-      const int count = offsets[i + 1] - offsets[i];
-      if (count == 0) {
+      MutableSpan<int> dst = all_dst.slice(offsets[i]);
+      if (dst.is_empty()) {
         continue;
       }
-      for (const int i_duplicate : IndexRange(1, count - 1)) {
-        dst[offsets[i] + i_duplicate] = noise::hash(src[i], i_duplicate);
+      dst.first() = src[i];
+      for (const int i_duplicate : dst.index_range().drop_front(1)) {
+        dst[i_duplicate] = noise::hash(src[i], i_duplicate);
       }
     }
   });
@@ -140,13 +132,12 @@ static void create_duplicate_index_attribute(bke::MutableAttributeAccessor attri
                                              const eAttrDomain output_domain,
                                              const IndexMask selection,
                                              const IndexAttributes &attribute_outputs,
-                                             const Span<int> offsets)
+                                             const OffsetIndices<int> offsets)
 {
   SpanAttributeWriter<int> duplicate_indices = attributes.lookup_or_add_for_write_only_span<int>(
       attribute_outputs.duplicate_index.get(), output_domain);
   for (const int i : IndexRange(selection.size())) {
-    const IndexRange range = range_for_offsets_index(offsets, i);
-    MutableSpan<int> indices = duplicate_indices.span.slice(range);
+    MutableSpan<int> indices = duplicate_indices.span.slice(offsets[i]);
     for (const int i : indices.index_range()) {
       indices[i] = i;
     }
@@ -158,7 +149,7 @@ static void create_duplicate_index_attribute(bke::MutableAttributeAccessor attri
  * Copy the stable ids to the first duplicate and create new ids based on a hash of the original id
  * and the duplicate number. This function is used for the point domain elements.
  */
-static void copy_stable_id_point(const Span<int> offsets,
+static void copy_stable_id_point(const OffsetIndices<int> offsets,
                                  const bke::AttributeAccessor src_attributes,
                                  bke::MutableAttributeAccessor dst_attributes)
 {
@@ -181,7 +172,7 @@ static void copy_stable_id_point(const Span<int> offsets,
 static void copy_attributes_without_id(GeometrySet &geometry_set,
                                        const GeometryComponentType component_type,
                                        const eAttrDomain domain,
-                                       const Span<int> offsets,
+                                       const OffsetIndices<int> offsets,
                                        const IndexMask selection,
                                        const AnonymousAttributePropagationInfo &propagation_info,
                                        const bke::AttributeAccessor src_attributes,
@@ -228,7 +219,7 @@ static void copy_curve_attributes_without_id(
     const GeometrySet &geometry_set,
     const bke::CurvesGeometry &src_curves,
     const IndexMask selection,
-    const Span<int> curve_offsets,
+    const OffsetIndices<int> curve_offsets,
     const AnonymousAttributePropagationInfo &propagation_info,
     bke::CurvesGeometry &dst_curves)
 {
@@ -269,7 +260,7 @@ static void copy_curve_attributes_without_id(
             for (const int i_selection : range) {
               const int i_src_curve = selection[i_selection];
               const Span<T> curve_src = src.slice(src_points_by_curve[i_src_curve]);
-              for (const int i_dst_curve : range_for_offsets_index(curve_offsets, i_selection)) {
+              for (const int i_dst_curve : curve_offsets[i_selection]) {
                 dst.slice(dst_points_by_curve[i_dst_curve]).copy_from(curve_src);
               }
             }
@@ -291,7 +282,7 @@ static void copy_curve_attributes_without_id(
  */
 static void copy_stable_id_curves(const bke::CurvesGeometry &src_curves,
                                   const IndexMask selection,
-                                  const Span<int> curve_offsets,
+                                  const OffsetIndices<int> offsets,
                                   bke::CurvesGeometry &dst_curves)
 {
   GAttributeReader src_attribute = src_curves.attributes().lookup("id");
@@ -315,8 +306,8 @@ static void copy_stable_id_curves(const bke::CurvesGeometry &src_curves,
     for (const int i_selection : range) {
       const int i_src_curve = selection[i_selection];
       const Span<int> curve_src = src.slice(src_points_by_curve[i_src_curve]);
-      const IndexRange duplicates_range = range_for_offsets_index(curve_offsets, i_selection);
-      for (const int i_duplicate : IndexRange(duplicates_range.size()).drop_front(1)) {
+      const IndexRange duplicates_range = offsets[i_selection];
+      for (const int i_duplicate : IndexRange(offsets.size(i_selection)).drop_front(1)) {
         const int i_dst_curve = duplicates_range[i_duplicate];
         copy_hashed_ids(curve_src, i_duplicate, dst.slice(dst_points_by_curve[i_dst_curve]));
       }
@@ -352,20 +343,23 @@ static void duplicate_curves(GeometrySet &geometry_set,
   const OffsetIndices points_by_curve = curves.points_by_curve();
 
   /* The offset in the result curve domain at every selected input curve. */
-  Array<int> curve_offsets(selection.size() + 1);
-  Array<int> point_offsets(selection.size() + 1);
+  Array<int> curve_offset_data(selection.size() + 1);
+  Array<int> point_offset_data(selection.size() + 1);
 
   int dst_curves_num = 0;
   int dst_points_num = 0;
   for (const int i_curve : selection.index_range()) {
     const int count = std::max(counts[selection[i_curve]], 0);
-    curve_offsets[i_curve] = dst_curves_num;
-    point_offsets[i_curve] = dst_points_num;
+    curve_offset_data[i_curve] = dst_curves_num;
+    point_offset_data[i_curve] = dst_points_num;
     dst_curves_num += count;
     dst_points_num += count * points_by_curve.size(selection[i_curve]);
   }
-  curve_offsets.last() = dst_curves_num;
-  point_offsets.last() = dst_points_num;
+  curve_offset_data.last() = dst_curves_num;
+  point_offset_data.last() = dst_points_num;
+
+  const OffsetIndices<int> curve_offsets(curve_offset_data);
+  const OffsetIndices<int> point_offsets(point_offset_data);
 
   Curves *new_curves_id = bke::curves_new_nomain(dst_points_num, dst_curves_num);
   bke::curves_copy_parameters(curves_id, *new_curves_id);
@@ -376,10 +370,10 @@ static void duplicate_curves(GeometrySet &geometry_set,
     for (const int i_selection : range) {
       const int i_src_curve = selection[i_selection];
       const IndexRange src_curve_range = points_by_curve[i_src_curve];
-      const IndexRange dst_curves_range = range_for_offsets_index(curve_offsets, i_selection);
+      const IndexRange dst_curves_range = curve_offsets[i_selection];
       MutableSpan<int> dst_offsets = all_dst_offsets.slice(dst_curves_range);
       for (const int i_duplicate : IndexRange(dst

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list