def split(self, nsegments=2, length=None, k=1, s=0):
"""Split the branch into segments"""
if length is None:
sublength = self.length / nsegments # the target length of the segments
else:
sublength = float(length)
# get the data that requires interpolation
x, y, z, r = self['x'], self['y'], self['z'], self['radius']
# the cumulative length
t = numpy.r_[0, numpy.cumsum(((x[:-1] - x[1:]) ** 2 + (y[:-1] - y[1:]) ** 2) ** .5)]
# create interpolation coefficients
import scipy.interpolate
splinecoeffs, u = scipy.interpolate.splprep([x, y, z, r], u=t, k=k, s=s)
# create new parametrization array. It is supposed to consist of the old lengths
# plus points that cut the length into the proper segment size
tnew = t.tolist()
# the indices where to split the resulting tnew
indices = [0]
import bisect
for n in range(1, int(self.length / sublength) + 1):
index = bisect.bisect_left(tnew, n * sublength)
indices.append(index)
bisect.insort_left(tnew, n * sublength)
# append the end index for the last segment if last segment size is larger than eps
if tnew[-1] - tnew[-2] > 0.01:
indices.append(len(tnew))
# interpolate the parametrization
xn, yn, zn, rn = scipy.interpolate.splev(tnew, splinecoeffs)
branchgen = lambda i0, i1: Branch(x=xn[i0:i1], y=yn[i0:i1], z=zn[i0:i1], r=rn[i0:i1])
return [branchgen(i0, i1 + 1) for i0, i1 in zip(indices[:-1], indices[1:])]
评论列表
文章目录