Skip to content

Commit

Permalink
Merge pull request #4289 from nortikin/fix_derivatives
Browse files Browse the repository at this point in the history
Update curve derivatives API
  • Loading branch information
portnov authored Aug 18, 2021
2 parents e771935 + 1de8f81 commit 4e08717
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 138 deletions.
12 changes: 12 additions & 0 deletions nodes/curve/curvature.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, zip_long_repeat
from sverchok.utils.curve.core import DEFAULT_TANGENT_DELTA

class SvCurveCurvatureNode(bpy.types.Node, SverchCustomTreeNode):
"""
Expand All @@ -21,6 +22,17 @@ class SvCurveCurvatureNode(bpy.types.Node, SverchCustomTreeNode):
default = 0.5,
update = updateNode)

tangent_delta : FloatProperty(
name = "Tangent step",
description = "Derivatives calculation step; bigger values lead to more smooth results",
min = 0.0,
precision = 8,
default = DEFAULT_TANGENT_DELTA,
update = updateNode)

def draw_buttons_ext(self, context, layout):
layout.prop(self, 'tangent_delta')

def sv_init(self, context):
self.inputs.new('SvCurveSocket', "Curve")
self.inputs.new('SvStringsSocket', "T").prop_name = 't_value'
Expand Down
32 changes: 16 additions & 16 deletions utils/curve/bezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,40 +235,40 @@ def evaluate_array(self, ts):
coeffs = np.array(coeffs)
return np.dot(coeffs.T, self.points)

def tangent(self, t):
return self.tangent_array(np.array([t]))[0]
def tangent(self, t, tangent_delta=None):
return self.tangent_array(np.array([t]), tangent_delta=tangent_delta)[0]

def tangent_array(self, ts):
def tangent_array(self, ts, tangent_delta=None):
coeffs = [self.coeff_deriv1(k, ts) for k in range(len(self.points))]
coeffs = np.array(coeffs)
#print("C1", coeffs)
return np.dot(coeffs.T, self.points)

def second_derivative(self, t):
def second_derivative(self, t, tangent_delta=None):
return self.second_derivative_array(np.array([t]))[0]

def second_derivative_array(self, ts):
def second_derivative_array(self, ts, tangent_delta=None):
coeffs = [self.coeff_deriv2(k, ts) for k in range(len(self.points))]
coeffs = np.array(coeffs)
#print("C2", coeffs)
return np.dot(coeffs.T, self.points)

def third_derivative_array(self, ts):
def third_derivative_array(self, ts, tangent_delta=None):
coeffs = [self.coeff_deriv3(k, ts) for k in range(len(self.points))]
coeffs = np.array(coeffs)
#print("C3", coeffs)
return np.dot(coeffs.T, self.points)

def derivatives_array(self, n, ts):
def derivatives_array(self, n, ts, tangent_delta=None):
result = []
if n >= 1:
first = self.tangent_array(ts)
first = self.tangent_array(ts, tangent_delta=tangent_delta)
result.append(first)
if n >= 2:
second = self.second_derivative_array(ts)
second = self.second_derivative_array(ts, tangent_delta=tangent_delta)
result.append(second)
if n >= 3:
third = self.third_derivative_array(ts)
third = self.third_derivative_array(ts, tangent_delta=tangent_delta)
result.append(third)
return result

Expand Down Expand Up @@ -383,10 +383,10 @@ def evaluate_array(self, ts):

return c0*p0 + c1*p1 + c2*p2 + c3*p3

def tangent(self, t):
return self.tangent_array(np.array([t]))[0]
def tangent(self, t, tangent_delta=None):
return self.tangent_array(np.array([t]), tangent_delta=tangent_delta)[0]

def tangent_array(self, ts):
def tangent_array(self, ts, tangent_delta=None):
c0 = -3*(1 - ts)**2
c1 = 3*(1-ts)**2 - 6*(1-ts)*ts
c2 = 6*(1-ts)*ts - 3*ts**2
Expand All @@ -397,7 +397,7 @@ def tangent_array(self, ts):

return c0*p0 + c1*p1 + c2*p2 + c3*p3

def second_derivative(self, t):
def second_derivative(self, t, tangent_delta=None):
return self.second_derivative_array(np.array([t]))[0]

def second_derivative_array(self, ts):
Expand All @@ -410,15 +410,15 @@ def second_derivative_array(self, ts):

return c0*p0 + c1*p1 + c2*p2 + c3*p3

def third_derivative_array(self, ts):
def third_derivative_array(self, ts, tangent_delta=None):
c0 = np.full_like(ts, -6)[:,np.newaxis]
c1 = np.full_like(ts, 18)[:,np.newaxis]
c2 = np.full_like(ts, -18)[:,np.newaxis]
c3 = np.full_like(ts, 6)[:,np.newaxis]
p0, p1, p2, p3 = self.p0, self.p1, self.p2, self.p3
return c0*p0 + c1*p1 + c2*p2 + c3*p3

def derivatives_array(self, n, ts):
def derivatives_array(self, n, ts, tangent_delta=None):
result = []
if n >= 1:
first = self.tangent_array(ts)
Expand Down
Loading

0 comments on commit 4e08717

Please sign in to comment.