[Bf-blender-cvs] [75ae328d629] master: Cleanup: Make curve trim node code more semantically correct

Hans Goudey noreply at git.blender.org
Mon Jul 19 01:21:22 CEST 2021


Commit: 75ae328d629aee6ed9b8adc38f6c2143824581f5
Author: Hans Goudey
Date:   Sun Jul 18 19:20:57 2021 -0400
Branches: master
https://developer.blender.org/rB75ae328d629aee6ed9b8adc38f6c2143824581f5

Cleanup: Make curve trim node code more semantically correct

The code used `Spline::LookupResult` in a way that referred to evaluated
points and control points interchangeably. That didn't affect the logic,
but the code became harder to read. Instead, introduce a local struct
to contain the data in a more obvious way.

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

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

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

diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_trim.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_trim.cc
index 91dd4af0de3..667cf26183f 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_trim.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_trim.cc
@@ -22,6 +22,8 @@
 
 #include "node_geometry_util.hh"
 
+using blender::attribute_math::mix2;
+
 static bNodeSocketTemplate geo_node_curve_trim_in[] = {
     {SOCK_GEOMETRY, N_("Curve")},
     {SOCK_FLOAT, N_("Start"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
@@ -69,6 +71,15 @@ static void geo_node_curve_trim_update(bNodeTree *UNUSED(ntree), bNode *node)
 
 namespace blender::nodes {
 
+struct TrimLocation {
+  /* Control point index at the start side of the trim location. */
+  int left_index;
+  /* Control point intex at the end of the trim location's segment. */
+  int right_index;
+  /* The factor between the left and right indices. */
+  float factor;
+};
+
 template<typename T>
 static void shift_slice_to_start(MutableSpan<T> data, const int start_index, const int size)
 {
@@ -78,51 +89,44 @@ static void shift_slice_to_start(MutableSpan<T> data, const int start_index, con
 
 /* Shift slice to start of span and modifies start and end data. */
 template<typename T>
-static void linear_trim_data(const Spline::LookupResult &start_lookup,
-                             const Spline::LookupResult &end_lookup,
-                             MutableSpan<T> input_data)
+static void linear_trim_data(const TrimLocation &start,
+                             const TrimLocation &end,
+                             MutableSpan<T> data)
 {
-  const int size = end_lookup.next_evaluated_index - start_lookup.evaluated_index + 1;
+  const int size = end.right_index - start.left_index + 1;
 
-  if (start_lookup.evaluated_index > 0) {
-    shift_slice_to_start<T>(input_data, start_lookup.evaluated_index, size);
+  if (start.left_index > 0) {
+    shift_slice_to_start<T>(data, start.left_index, size);
   }
 
-  const T start_data = blender::attribute_math::mix2<T>(
-      start_lookup.factor, input_data.first(), input_data[1]);
-  const T end_data = blender::attribute_math::mix2<T>(
-      end_lookup.factor, input_data[size - 2], input_data[size - 1]);
+  const T start_data = mix2<T>(start.factor, data.first(), data[1]);
+  const T end_data = mix2<T>(end.factor, data[size - 2], data[size - 1]);
 
-  input_data.first() = start_data;
-  input_data[size - 1] = end_data;
+  data.first() = start_data;
+  data[size - 1] = end_data;
 }
 
 /* Identical operation as #linear_trim_data, but opy data to a new MutableSpan rather than
  * modifying the original data. */
 template<typename T>
-static void linear_trim_to_output_data(const Spline::LookupResult &start_lookup,
-                                       const Spline::LookupResult &end_lookup,
-                                       Span<T> input_data,
-                                       MutableSpan<T> output_data)
+static void linear_trim_to_output_data(const TrimLocation &start,
+                                       const TrimLocation &end,
+                                       Span<T> src,
+                                       MutableSpan<T> dst)
 {
-  const int size = end_lookup.next_evaluated_index - start_lookup.evaluated_index + 1;
-
-  const T start_data = blender::attribute_math::mix2<T>(
-      start_lookup.factor,
-      input_data[start_lookup.evaluated_index],
-      input_data[start_lookup.next_evaluated_index]);
-  const T end_data = blender::attribute_math::mix2<T>(end_lookup.factor,
-                                                      input_data[end_lookup.evaluated_index],
-                                                      input_data[end_lookup.next_evaluated_index]);
-
-  output_data.copy_from(input_data.slice(start_lookup.evaluated_index, size));
-  output_data.first() = start_data;
-  output_data.last() = end_data;
+  const int size = end.right_index - start.left_index + 1;
+
+  const T start_data = mix2<T>(start.factor, src[start.left_index], src[start.right_index]);
+  const T end_data = mix2<T>(end.factor, src[end.left_index], src[end.right_index]);
+
+  dst.copy_from(src.slice(start.left_index, size));
+  dst.first() = start_data;
+  dst.last() = end_data;
 }
 
 /* Look up the control points to the left and right of factor, and get the factor between them. */
-static Spline::LookupResult lookup_control_point_position(Spline::LookupResult lookup,
-                                                          Span<int> control_point_offsets)
+static TrimLocation lookup_control_point_position(const Spline::LookupResult &lookup,
+                                                  Span<int> control_point_offsets)
 {
   const int *left_offset = std::lower_bound(
       control_point_offsets.begin(), control_point_offsets.end(), lookup.evaluated_index);
@@ -136,18 +140,24 @@ static Spline::LookupResult lookup_control_point_position(Spline::LookupResult l
       0.0f,
       1.0f);
 
-  return Spline::LookupResult{left, right, factor};
+  return {left, right, factor};
 }
 
 static void trim_poly_spline(Spline &spline,
                              const Spline::LookupResult &start_lookup,
                              const Spline::LookupResult &end_lookup)
 {
-  const int size = end_lookup.next_evaluated_index - start_lookup.evaluated_index + 1;
+  /* Poly splines have a 1 to 1 mapping between control points and evaluated points. */
+  const TrimLocation start = {
+      start_lookup.evaluated_index, start_lookup.next_evaluated_index, start_lookup.factor};
+  const TrimLocation end = {
+      end_lookup.evaluated_index, end_lookup.next_evaluated_index, end_lookup.factor};
 
-  linear_trim_data<float3>(start_lookup, end_lookup, spline.positions());
-  linear_trim_data<float>(start_lookup, end_lookup, spline.radii());
-  linear_trim_data<float>(start_lookup, end_lookup, spline.tilts());
+  const int size = end.right_index - start.left_index + 1;
+
+  linear_trim_data<float3>(start, end, spline.positions());
+  linear_trim_data<float>(start, end, spline.radii());
+  linear_trim_data<float>(start, end, spline.tilts());
 
   spline.attributes.foreach_attribute(
       [&](StringRefNull name, const AttributeMetaData &UNUSED(meta_data)) {
@@ -155,7 +165,7 @@ static void trim_poly_spline(Spline &spline,
         BLI_assert(src);
         attribute_math::convert_to_static_type(src->type(), [&](auto dummy) {
           using T = decltype(dummy);
-          linear_trim_data<T>(start_lookup, end_lookup, src->typed<T>());
+          linear_trim_data<T>(start, end, src->typed<T>());
         });
         return true;
       },
@@ -171,7 +181,13 @@ static PolySpline trim_nurbs_spline(const Spline &spline,
                                     const Spline::LookupResult &start_lookup,
                                     const Spline::LookupResult &end_lookup)
 {
-  const int size = end_lookup.next_evaluated_index - start_lookup.evaluated_index + 1;
+  /* Since this outputs a poly spline, the evaluated indices are the control point indices. */
+  const TrimLocation start = {
+      start_lookup.evaluated_index, start_lookup.next_evaluated_index, start_lookup.factor};
+  const TrimLocation end = {
+      end_lookup.evaluated_index, end_lookup.next_evaluated_index, end_lookup.factor};
+
+  const int size = end.right_index - start.left_index + 1;
 
   /* Create poly spline and copy trimmed data to it. */
   PolySpline new_spline;
@@ -193,22 +209,22 @@ static PolySpline trim_nurbs_spline(const Spline &spline,
           using T = decltype(dummy);
           GVArray_Typed<T> eval_data = spline.interpolate_to_evaluated<T>(src->typed<T>());
           linear_trim_to_output_data<T>(
-              start_lookup, end_lookup, eval_data->get_internal_span(), dst->typed<T>());
+              start, end, eval_data->get_internal_span(), dst->typed<T>());
         });
         return true;
       },
       ATTR_DOMAIN_POINT);
 
   linear_trim_to_output_data<float3>(
-      start_lookup, end_lookup, spline.evaluated_positions(), new_spline.positions());
+      start, end, spline.evaluated_positions(), new_spline.positions());
 
   GVArray_Typed<float> evaluated_radii = spline.interpolate_to_evaluated(spline.radii());
   linear_trim_to_output_data<float>(
-      start_lookup, end_lookup, evaluated_radii->get_internal_span(), new_spline.radii());
+      start, end, evaluated_radii->get_internal_span(), new_spline.radii());
 
   GVArray_Typed<float> evaluated_tilts = spline.interpolate_to_evaluated(spline.tilts());
   linear_trim_to_output_data<float>(
-      start_lookup, end_lookup, evaluated_tilts->get_internal_span(), new_spline.tilts());
+      start, end, evaluated_tilts->get_internal_span(), new_spline.tilts());
 
   return new_spline;
 }
@@ -224,65 +240,54 @@ static void trim_bezier_spline(Spline &spline,
   BezierSpline &bezier_spline = static_cast<BezierSpline &>(spline);
   Span<int> control_offsets = bezier_spline.control_point_offsets();
 
-  const Spline::LookupResult start_control_lookup = lookup_control_point_position(start_lookup,
-                                                                                  control_offsets);
-  Spline::LookupResult end_control_lookup = lookup_control_point_position(end_lookup,
-                                                                          control_offsets);
+  const TrimLocation start = lookup_control_point_position(start_lookup, control_offsets);
+  TrimLocation end = lookup_control_point_position(end_lookup, control_offsets);
 
   /* The number of control points in the resulting spline. */
-  const int size = end_control_lookup.next_evaluated_index - start_control_lookup.evaluated_index +
-                   1;
-
-  /* Trim the spline attributes. Done before end_control_lookup.factor recalculation as it needs
-   * the original end_control_lookup.factor value. */
-  linear_trim_data<float>(start_control_lookup, end_control_lookup, bezier_spline.radii());
-  linear_trim_data<float>(start_control_lookup, end_control_lookup, bezier_spline.tilts());
+  const int size = end.right_index - start.left_index + 1;
 
+  /* Trim the spline attributes. Done before end.f

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list