Created
December 4, 2015 17:49
-
-
Save zeffii/8d931b967c4237411452 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bisect | |
import numpy as np | |
# spline function modifed from | |
# from looptools 4.5.2 done by Bart Crouch | |
# calculates natural cubic splines through all given knots | |
def cubic_spline(locs, tknots): | |
knots = list(range(len(locs))) | |
n = len(knots) | |
if n < 2: | |
return False | |
x = tknots[:] | |
result = [] | |
for j in range(3): | |
a = [] | |
for i in locs: | |
a.append(i[j]) | |
h = [] | |
for i in range(n-1): | |
if x[i+1] - x[i] == 0: | |
h.append(1e-8) | |
else: | |
h.append(x[i+1] - x[i]) | |
q = [False] | |
for i in range(1, n-1): | |
q.append(3/h[i]*(a[i+1]-a[i]) - 3/h[i-1]*(a[i]-a[i-1])) | |
l = [1.0] | |
u = [0.0] | |
z = [0.0] | |
for i in range(1, n-1): | |
l.append(2*(x[i+1]-x[i-1]) - h[i-1]*u[i-1]) | |
if l[i] == 0: | |
l[i] = 1e-8 | |
u.append(h[i] / l[i]) | |
z.append((q[i] - h[i-1] * z[i-1]) / l[i]) | |
l.append(1.0) | |
z.append(0.0) | |
b = [False for i in range(n-1)] | |
c = [False for i in range(n)] | |
d = [False for i in range(n-1)] | |
c[n-1] = 0.0 | |
for i in range(n-2, -1, -1): | |
c[i] = z[i] - u[i]*c[i+1] | |
b[i] = (a[i+1]-a[i])/h[i] - h[i]*(c[i+1]+2*c[i])/3 | |
d[i] = (c[i+1]-c[i]) / (3*h[i]) | |
for i in range(n-1): | |
result.append([a[i], b[i], c[i], d[i], x[i]]) | |
splines = [] | |
for i in range(len(knots)-1): | |
splines.append([result[i], result[i+n-1], result[i+(n-1)*2]]) | |
return(splines) | |
def eval_spline(splines, tknots, t_in): | |
out = [] | |
for t in t_in: | |
n = bisect.bisect(tknots, t, lo=0, hi=len(tknots))-1 | |
if n > len(splines)-1: | |
n = len(splines)-1 | |
if n < 0: | |
n = 0 | |
pt = [] | |
for i in range(3): | |
ax, bx, cx, dx, tx = splines[n][i] | |
x = ax + bx*(t-tx) + cx*(t-tx)**2 + dx*(t-tx)**3 | |
pt.append(x) | |
out.append(pt) | |
return out | |
def gen_verts(v, t_in): | |
pts = np.array(v).T | |
tmp = np.apply_along_axis(np.linalg.norm, 0, pts[:, :-1]-pts[:, 1:]) | |
t = np.insert(tmp, 0, 0).cumsum() | |
t = t/t[-1] | |
t_corr = [min(1, max(t_c, 0)) for t_c in t_in] | |
# this should also be numpy | |
spl = cubic_spline(v, t) | |
out = eval_spline(spl, t, t_corr) | |
return out | |
def sv_main(count=10): | |
verts_out = [] | |
in_sockets = [ | |
['s', 'count', count] | |
] | |
v = [[-2, 0, 0], [0, 0, 2], [2, 0, 0]] | |
t_in = np.linspace(0, 1.0, count) | |
verts_out.extend(gen_verts(v, t_in)) | |
out_sockets = [ | |
['v', 'verts', verts_out] | |
] | |
return in_sockets, out_sockets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment