[Bf-blender-cvs] [0fd72a98ac1] master: Cleanup: Avoid adding points to splines sequentially

Hans Goudey noreply at git.blender.org
Wed Dec 22 21:35:56 CET 2021


Commit: 0fd72a98ac1377a385b6558bf34a7cb372677ac1
Author: Hans Goudey
Date:   Wed Dec 22 14:35:46 2021 -0600
Branches: master
https://developer.blender.org/rB0fd72a98ac1377a385b6558bf34a7cb372677ac1

Cleanup: Avoid adding points to splines sequentially

This should be faster because it avoids reallocating the internal
vectors when the size is known beforehand, but it may also help
a potential refactor to a different data structure (see T94193).

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

M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_spiral.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_star.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc

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

diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
index a9829b64150..70b9888a375 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
@@ -79,44 +79,29 @@ static std::unique_ptr<CurveEval> create_bezier_segment_curve(
 {
   std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
   std::unique_ptr<BezierSpline> spline = std::make_unique<BezierSpline>();
+  spline->set_resolution(resolution);
+
+  spline->resize(2);
+  MutableSpan<float3> positions = spline->positions();
+  spline->handle_types_left().fill(BezierSpline::HandleType::Align);
+  spline->handle_types_right().fill(BezierSpline::HandleType::Align);
+  spline->radii().fill(1.0f);
+  spline->tilts().fill(0.0f);
+
+  positions.first() = start;
+  positions.last() = end;
 
   if (mode == GEO_NODE_CURVE_PRIMITIVE_BEZIER_SEGMENT_POSITION) {
-    spline->add_point(start,
-                      BezierSpline::HandleType::Align,
-                      2.0f * start - start_handle_right,
-                      BezierSpline::HandleType::Align,
-                      start_handle_right,
-                      1.0f,
-                      0.0f);
-    spline->add_point(end,
-                      BezierSpline::HandleType::Align,
-                      end_handle_left,
-                      BezierSpline::HandleType::Align,
-                      2.0f * end - end_handle_left,
-                      1.0f,
-                      0.0f);
+    spline->set_handle_position_right(0, start_handle_right);
+    spline->set_handle_position_left(1, end_handle_left);
   }
   else {
-    spline->add_point(start,
-                      BezierSpline::HandleType::Align,
-                      start - start_handle_right,
-                      BezierSpline::HandleType::Align,
-                      start + start_handle_right,
-                      1.0f,
-                      0.0f);
-    spline->add_point(end,
-                      BezierSpline::HandleType::Align,
-                      end + end_handle_left,
-                      BezierSpline::HandleType::Align,
-                      end - end_handle_left,
-                      1.0f,
-                      0.0f);
+    spline->set_handle_position_right(0, start + start_handle_right);
+    spline->set_handle_position_left(1, end + end_handle_left);
   }
 
-  spline->set_resolution(resolution);
-  spline->attributes.reallocate(spline->size());
   curve->add_spline(std::move(spline));
-  curve->attributes.reallocate(curve->splines().size());
+  curve->attributes.reallocate(1);
   return curve;
 }
 
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc
index 92a9b1b4966..4b2bd586db2 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc
@@ -50,15 +50,19 @@ static std::unique_ptr<CurveEval> create_quadratic_bezier_curve(const float3 p1,
   std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
   std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
 
+  spline->resize(resolution + 1);
+  MutableSpan<float3> positions = spline->positions();
+  spline->radii().fill(1.0f);
+  spline->tilts().fill(0.0f);
+
   const float step = 1.0f / resolution;
-  for (int i : IndexRange(resolution + 1)) {
+  for (const int i : IndexRange(resolution + 1)) {
     const float factor = step * i;
     const float3 q1 = float3::interpolate(p1, p2, factor);
     const float3 q2 = float3::interpolate(p2, p3, factor);
-    const float3 out = float3::interpolate(q1, q2, factor);
-    spline->add_point(out, 1.0f, 0.0f);
+    positions[i] = float3::interpolate(q1, q2, factor);
   }
-  spline->attributes.reallocate(spline->size());
+
   curve->add_spline(std::move(spline));
   curve->attributes.reallocate(curve->splines().size());
   return curve;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_spiral.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_spiral.cc
index d5ae3551904..8661ba1f067 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_spiral.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_spiral.cc
@@ -65,6 +65,11 @@ static std::unique_ptr<CurveEval> create_spiral_curve(const float rotations,
   const float delta_theta = (M_PI * 2 * rotations) / (float)totalpoints *
                             (direction ? 1.0f : -1.0f);
 
+  spline->resize(totalpoints + 1);
+  MutableSpan<float3> positions = spline->positions();
+  spline->radii().fill(1.0f);
+  spline->tilts().fill(0.0f);
+
   for (const int i : IndexRange(totalpoints + 1)) {
     const float theta = i * delta_theta;
     const float radius = start_radius + i * delta_radius;
@@ -72,10 +77,9 @@ static std::unique_ptr<CurveEval> create_spiral_curve(const float rotations,
     const float y = radius * sin(theta);
     const float z = delta_height * i;
 
-    spline->add_point(float3(x, y, z), 1.0f, 0.0f);
+    positions[i] = {x, y, z};
   }
 
-  spline->attributes.reallocate(spline->size());
   curve->add_spline(std::move(spline));
   curve->attributes.reallocate(curve->splines().size());
   return curve;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_star.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_star.cc
index 731be0f0f49..8841f558435 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_star.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_star.cc
@@ -54,19 +54,24 @@ static std::unique_ptr<CurveEval> create_star_curve(const float inner_radius,
 {
   std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
   std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
+  spline->set_cyclic(true);
+
+  spline->resize(points * 2);
+  MutableSpan<float3> positions = spline->positions();
+  spline->radii().fill(1.0f);
+  spline->tilts().fill(0.0f);
 
   const float theta_step = (2.0f * M_PI) / float(points);
-  for (int i : IndexRange(points)) {
+  for (const int i : IndexRange(points)) {
     const float x = outer_radius * cos(theta_step * i);
     const float y = outer_radius * sin(theta_step * i);
-    spline->add_point(float3(x, y, 0.0f), 1.0f, 0.0f);
+    positions[i * 2] = {x, y, 0.0f};
 
     const float inner_x = inner_radius * cos(theta_step * i + theta_step * 0.5f + twist);
     const float inner_y = inner_radius * sin(theta_step * i + theta_step * 0.5f + twist);
-    spline->add_point(float3(inner_x, inner_y, 0.0f), 1.0f, 0.0f);
+    positions[i * 2 + 1] = {inner_x, inner_y, 0.0f};
   }
-  spline->set_cyclic(true);
-  spline->attributes.reallocate(spline->size());
+
   curve->add_spline(std::move(spline));
   curve->attributes.reallocate(curve->splines().size());
 
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc
index 741dad95362..c11b4cb5cc8 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc
@@ -81,8 +81,11 @@ static SplinePtr resample_spline(const Spline &src, const int count)
   Spline::copy_base_settings(src, *dst);
 
   if (src.evaluated_edges_size() < 1 || count == 1) {
-    dst->add_point(src.positions().first(), src.radii().first(), src.tilts().first());
-    dst->attributes.reallocate(1);
+    dst->resize(1);
+    dst->positions().first() = src.positions().first();
+    dst->radii().first() = src.radii().first();
+    dst->tilts().first() = src.tilts().first();
+
     src.attributes.foreach_attribute(
         [&](const AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
           std::optional<GSpan> src_attribute = src.attributes.get_for_read(attribute_id);



More information about the Bf-blender-cvs mailing list