[Bf-blender-cvs] [0d43e832235] temp-offset-array-ref: initial offset array

Jacques Lucke noreply at git.blender.org
Tue Jan 17 22:26:10 CET 2023


Commit: 0d43e832235e1abde3a2acfde2b64006b0549868
Author: Jacques Lucke
Date:   Tue Jan 17 22:25:47 2023 +0100
Branches: temp-offset-array-ref
https://developer.blender.org/rB0d43e832235e1abde3a2acfde2b64006b0549868

initial offset array

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

M	source/blender/blenkernel/BKE_curves.hh
A	source/blender/blenlib/BLI_offset_array_ref.hh
M	source/blender/geometry/intern/trim_curves.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_topology_points_of_curve.cc

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

diff --git a/source/blender/blenkernel/BKE_curves.hh b/source/blender/blenkernel/BKE_curves.hh
index 1ed872c8ab8..1530b2ea7eb 100644
--- a/source/blender/blenkernel/BKE_curves.hh
+++ b/source/blender/blenkernel/BKE_curves.hh
@@ -18,6 +18,7 @@
 #include "BLI_generic_virtual_array.hh"
 #include "BLI_index_mask.hh"
 #include "BLI_math_vector_types.hh"
+#include "BLI_offset_array_ref.hh"
 #include "BLI_shared_cache.hh"
 #include "BLI_span.hh"
 #include "BLI_task.hh"
@@ -176,6 +177,11 @@ class CurvesGeometry : public ::CurvesGeometry {
   Span<int> offsets() const;
   MutableSpan<int> offsets_for_write();
 
+  /**
+   * The offsets of every curve into arrays on the points domain.
+   */
+  OffsetArrayRef<int> points_by_curve() const;
+
   /**
    * Access a range of indices of point data for a specific curve.
    */
@@ -884,6 +890,11 @@ inline const std::array<int, CURVE_TYPES_NUM> &CurvesGeometry::curve_type_counts
   return this->runtime->type_counts;
 }
 
+inline OffsetArrayRef<int> CurvesGeometry::points_by_curve() const
+{
+  return OffsetArrayRef<int>({this->curve_offsets, this->curve_num + 1});
+}
+
 inline IndexRange CurvesGeometry::points_for_curve(const int index) const
 {
   /* Offsets are not allocated when there are no curves. */
diff --git a/source/blender/blenlib/BLI_offset_array_ref.hh b/source/blender/blenlib/BLI_offset_array_ref.hh
new file mode 100644
index 00000000000..39d4386faab
--- /dev/null
+++ b/source/blender/blenlib/BLI_offset_array_ref.hh
@@ -0,0 +1,39 @@
+#pragma once
+
+#include "BLI_index_range.hh"
+#include "BLI_span.hh"
+
+namespace blender {
+
+template<typename T> class OffsetArrayRef {
+ private:
+  static_assert(std::is_integral_v<T>);
+
+  Span<T> offsets_;
+
+ public:
+  OffsetArrayRef(const Span<T> offsets) : offsets_(offsets)
+  {
+    BLI_assert(std::is_sorted(offsets_.begin(), offsets_.end()));
+  }
+
+  IndexRange operator[](const int64_t index) const
+  {
+    BLI_assert(index >= 0);
+    BLI_assert(index < offsets_.size() - 1);
+    const int64_t begin = offsets_[index];
+    const int64_t end = offsets_[index + 1];
+    const int64_t size = end - begin;
+    return IndexRange(begin, size);
+  }
+
+  IndexRange operator[](const IndexRange indices) const
+  {
+    const int64_t begin = offsets_[indices.start()];
+    const int64_t end = offsets_[indices.one_after_last()];
+    const int64_t size = end - begin;
+    return IndexRange(begin, size);
+  }
+};
+
+}  // namespace blender
diff --git a/source/blender/geometry/intern/trim_curves.cc b/source/blender/geometry/intern/trim_curves.cc
index 361415aa540..b4b2b3e7d5f 100644
--- a/source/blender/geometry/intern/trim_curves.cc
+++ b/source/blender/geometry/intern/trim_curves.cc
@@ -617,11 +617,13 @@ static void trim_polygonal_curves(const bke::CurvesGeometry &src_curves,
 {
   const Span<float3> src_positions = src_curves.positions();
   MutableSpan<float3> dst_positions = dst_curves.positions_for_write();
+  const OffsetArrayRef<int> src_points_by_curve = src_curves.points_by_curve();
+  const OffsetArrayRef<int> dst_points_by_curve = dst_curves.points_by_curve();
 
   threading::parallel_for(selection.index_range(), 512, [&](const IndexRange range) {
     for (const int64_t curve_i : selection.slice(range)) {
-      const IndexRange src_points = src_curves.points_for_curve(curve_i);
-      const IndexRange dst_points = dst_curves.points_for_curve(curve_i);
+      const IndexRange src_points = src_points_by_curve[curve_i];
+      const IndexRange dst_points = dst_points_by_curve[curve_i];
 
       sample_interval_linear<float3>(src_positions.slice(src_points),
                                      dst_positions,
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
index 9f0d40bb0d7..f63d12e6c03 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
@@ -55,9 +55,10 @@ static void select_by_handle_type(const bke::CurvesGeometry &curves,
   VArray<int8_t> curve_types = curves.curve_types();
   VArray<int8_t> left = curves.handle_types_left();
   VArray<int8_t> right = curves.handle_types_right();
+  const OffsetArrayRef<int> points_by_curve = curves.points_by_curve();
 
   for (const int i_curve : curves.curves_range()) {
-    const IndexRange points = curves.points_for_curve(i_curve);
+    const IndexRange points = points_by_curve[i_curve];
     if (curve_types[i_curve] != CURVE_TYPE_BEZIER) {
       r_selection.slice(points).fill(false);
     }
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc
index bd7a8c716ae..48dbace2463 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc
@@ -315,6 +315,7 @@ class SampleCurveFunction : public mf::MultiFunction {
     const VArray<int> curve_indices = params.readonly_single_input<int>(0, "Curve Index");
     const VArraySpan<float> lengths = params.readonly_single_input<float>(1, "Length");
     const VArray<bool> cyclic = curves.cyclic();
+    const OffsetArrayRef<int> points_by_curve = curves.points_by_curve();
 
     Array<int> indices;
     Array<float> factors;
@@ -377,7 +378,7 @@ class SampleCurveFunction : public mf::MultiFunction {
         }
       }
       if (!sampled_values.is_empty()) {
-        const IndexRange points = curves.points_for_curve(curve_i);
+        const IndexRange points = points_by_curve[curve_i];
         src_original_values.reinitialize(points.size());
         source_data_->materialize_compressed_to_uninitialized(points, src_original_values.data());
         src_evaluated_values.reinitialize(curves.evaluated_points_for_curve(curve_i).size());
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_topology_points_of_curve.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_topology_points_of_curve.cc
index 4ffada76497..944fb52da1a 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_topology_points_of_curve.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_topology_points_of_curve.cc
@@ -62,6 +62,7 @@ class PointsOfCurveInput final : public bke::CurvesFieldInput {
     point_evaluator.add(sort_weight_);
     point_evaluator.evaluate();
     const VArray<float> all_sort_weights = point_evaluator.get_evaluated<float>(0);
+    const OffsetArrayRef points_by_curve = curves.points_by_curve();
 
     Array<int> point_of_curve(mask.min_array_size());
     threading::parallel_for(mask.index_range(), 256, [&](const IndexRange range) {
@@ -77,7 +78,7 @@ class PointsOfCurveInput final : public bke::CurvesFieldInput {
           continue;
         }
 
-        const IndexRange points = curves.points_for_curve(curve_i);
+        const IndexRange points = points_by_curve[curve_i];
 
         /* Retrieve the weights for each point. */
         sort_weights.reinitialize(points.size());



More information about the Bf-blender-cvs mailing list