Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active August 29, 2015 14:01
Show Gist options
  • Save zeffii/782e84fbdc8e8f23f64b to your computer and use it in GitHub Desktop.
Save zeffii/782e84fbdc8e8f23f64b to your computer and use it in GitHub Desktop.
import mathutils
from mathutils.geometry import interpolate_bezier as bezlerp
from mathutils import Vector
import darpa
from darpa import get_length
from darpa import get_verts_n_edges
def sv_main(verts=[], num_verts=20, new_divisions=20):
'''
verts is expecting 4 (unique) 3d coordinates
'''
in_sockets = [
['v', 'verts', verts],
['s', 'num_verts', num_verts],
['s', 'new_divisions', new_divisions]]
out_sockets = [
['v', 'Vecs', []],
['s', 'Edges', []],
['v', 'Vecs ctrl', []],
['s', 'Edges ctrl', []],
['v', 'Vecs norm', []],
['s', 'Edges norm', []]
]
if not verts:
return in_sockets, out_sockets
# defend against div by zero
new_divisions = max(new_divisions, 1)
# while developing, it can be useful to uncomment this
if 'get_length' in globals():
import imp
imp.reload(darpa)
from darpa import get_length, get_verts_n_edges
f = list(map(Vector, verts[0]))
knot1, ctrl_1, ctrl_2, knot2 = f
arc_verts = bezlerp(knot1, ctrl_1, ctrl_2, knot2, num_verts)
farc_verts = bezlerp(knot1, ctrl_1, ctrl_2, knot2, 870)
tlen, lengths = get_length(farc_verts)
# print(tlen)
segment_width = tlen/new_divisions
print('new div length = ', segment_width)
k = get_verts_n_edges(farc_verts, lengths, segment_width)
arc_verts = [v[:] for v in arc_verts]
arc_edges = [(n, n+1) for n in range(len(arc_verts)-1)]
norm_verts = [v[:] for v in k]
norm_edges = [(n, n+1) for n in range(len(k)-1)]
controls = verts[0]
control_edges = [[(0,1),(2,3)]]
out_sockets[0][2] = [arc_verts]
out_sockets[1][2] = [arc_edges]
out_sockets[2][2] = [controls]
out_sockets[3][2] = control_edges
out_sockets[4][2] = [norm_verts]
out_sockets[5][2] = [norm_edges]
return in_sockets, out_sockets
import mathutils
from mathutils import Vector
import math
def get_length(verts):
summed = 0
lengths = []
lengths_add = lengths.append
for idx in range(len(verts)-1):
segment = (verts[idx]-verts[idx+1]).length
summed += segment
lengths_add(segment)
return summed, lengths
def get_verts_n_edges(verts, lengths, seg_width):
K = seg_width
eps = 0.00001
new_points = []
add_point = new_points.append
def consume(K, A, idx, v1):
if idx > len(lengths)-2:
return
R = K - A
# close enough to start fresh segment
if (-eps <= R <= eps):
K = seg_width
idx += 1
add_point(verts[idx])
A = lengths[idx]
consume(K, A, idx, None)
# must divide segment, same idx
elif (R < -eps):
# rate = R / A
rate = K / A
if not v1:
v1 = verts[idx]
v2 = verts[idx+1]
vmid = v1.lerp(v2, rate)
add_point(vmid)
A = (vmid-v2).length
consume(seg_width, A, idx, v1)
# consume segment, update k, update idx
elif (R > eps):
A = lengths[idx+1]
consume(R, A, idx+1, None)
add_point(verts[0])
consume(K, lengths[0], 0, None)
add_point(verts[-1])
return new_points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment