[Bf-blender-cvs] [34a61ceeaa0] master: Cleanup: Remove unnecessary NURBS optimization

Hans Goudey noreply at git.blender.org
Sun Mar 13 23:02:08 CET 2022


Commit: 34a61ceeaa0d9f859cccdd4357918447de6cdd13
Author: Hans Goudey
Date:   Fri Mar 11 17:48:33 2022 -0600
Branches: master
https://developer.blender.org/rB34a61ceeaa0d9f859cccdd4357918447de6cdd13

Cleanup: Remove unnecessary NURBS optimization

The step after calculating the NURBS basis for a single evaluated
point trimmed extra zeroes from the weights. However, in practice
this rarely did anything, only for the first and last evaluated point
of certain knot configurations. Remove it in order to simplify code.

Also use a separate span for the result, to clarify its length.

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

M	source/blender/blenkernel/intern/spline_nurbs.cc

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

diff --git a/source/blender/blenkernel/intern/spline_nurbs.cc b/source/blender/blenkernel/intern/spline_nurbs.cc
index 7dc5ac3ea12..bbc53ce586d 100644
--- a/source/blender/blenkernel/intern/spline_nurbs.cc
+++ b/source/blender/blenkernel/intern/spline_nurbs.cc
@@ -247,35 +247,34 @@ static void calculate_basis_for_point(const float parameter,
   }
   basis_buffer[size + degree] = 0.0f;
 
+  MutableSpan<float> weights = basis_buffer.slice(start, degree + 1);
+
   for (const int i_order : IndexRange(2, degree)) {
-    if (end + i_order >= size + degree + 1) {
+    if (end + i_order >= knots.size()) {
       end = size + degree - i_order;
     }
-    for (const int i : IndexRange(start, end - start + 1)) {
+    for (const int i : IndexRange(end - start + 1)) {
+      const int knot_index = start + i;
+
       float new_basis = 0.0f;
-      if (basis_buffer[i] != 0.0f) {
-        new_basis += ((t - knots[i]) * basis_buffer[i]) / (knots[i + i_order - 1] - knots[i]);
+      if (weights[i] != 0.0f) {
+        new_basis += ((t - knots[knot_index]) * basis_buffer[knot_index]) /
+                     (knots[knot_index + i_order - 1] - knots[knot_index]);
       }
 
-      if (basis_buffer[i + 1] != 0.0f) {
-        new_basis += ((knots[i + i_order] - t) * basis_buffer[i + 1]) /
-                     (knots[i + i_order] - knots[i + 1]);
+      if (basis_buffer[knot_index + 1] != 0.0f) {
+        new_basis += ((knots[knot_index + i_order] - t) * basis_buffer[knot_index + 1]) /
+                     (knots[knot_index + i_order] - knots[knot_index + 1]);
       }
 
-      basis_buffer[i] = new_basis;
+      weights[i] = new_basis;
     }
   }
 
-  /* Shrink the range of calculated values to avoid storing unnecessary zeros. */
-  while (basis_buffer[start] == 0.0f && start < end) {
-    start++;
-  }
-  while (basis_buffer[end] == 0.0f && end > start) {
-    end--;
-  }
+  weights.drop_front(end - start + 1).fill(0.0f);
 
   basis_cache.weights.clear();
-  basis_cache.weights.extend(basis_buffer.slice(start, end - start + 1));
+  basis_cache.weights.extend(weights);
   basis_cache.start_index = start;
 }
 
@@ -317,7 +316,7 @@ Span<NURBSpline::BasisCache> NURBSpline::calculate_basis_cache() const
     BasisCache &basis = basis_cache[i];
     calculate_basis_for_point(
         parameter, size + (is_cyclic_ ? degree : 0), degree, knots, basis_buffer, basis);
-    BLI_assert(basis.weights.size() <= order);
+    BLI_assert(basis.weights.size() == order);
 
     for (const int j : basis.weights.index_range()) {
       const int point_index = (basis.start_index + j) % size;



More information about the Bf-blender-cvs mailing list