How To Redistribute Points Evenly Over A Curve
Solution 1:
First of all, thank you to Mr. John D'Errico for interparc. What a great job!
I too was facing this problem but am not familiar with the MATLAB engine API. Given that, I tried to convert part of the interparc Matlab code to Python (just including the linear interpolant because it would be enough to address my problem).
And so here is my code; hope it can help all the pythonics seeking something similar:
import numpy as np
def interpcurve(N,pX,pY):
#equally spaced in arclength
N=np.transpose(np.linspace(0,1,N))
#how many points will be uniformly interpolated?
nt=N.size
#number of points on the curve
n=pX.size
pxy=np.array((pX,pY)).T
p1=pxy[0,:]
pend=pxy[-1,:]
last_segment= np.linalg.norm(np.subtract(p1,pend))
epsilon= 10*np.finfo(float).eps
#IF the two end points are not close enough lets close the curve
if last_segment > epsilon*np.linalg.norm(np.amax(abs(pxy),axis=0)):
pxy=np.vstack((pxy,p1))
nt = nt + 1
else:
print('Contour already closed')
pt=np.zeros((nt,2))
#Compute the chordal arclength of each segment.
chordlen = (np.sum(np.diff(pxy,axis=0)**2,axis=1))**(1/2)
#Normalize the arclengths to a unit total
chordlen = chordlen/np.sum(chordlen)
#cumulative arclength
cumarc = np.append(0,np.cumsum(chordlen))
tbins= np.digitize(N,cumarc) # bin index in which each N is in#catch any problems at the ends
tbins[np.where(tbins<=0 | (N<=0))]=1
tbins[np.where(tbins >= n | (N >= 1))] = n - 1
s = np.divide((N - cumarc[tbins]),chordlen[tbins-1])
pt = pxy[tbins,:] + np.multiply((pxy[tbins,:] - pxy[tbins-1,:]),(np.vstack([s]*2)).T)
return pt
Solution 2:
Your "curve" is a bunch of line-segments that connect a bunch of points. Each line-segment has a length; the total length of your curve is the sum of these line-segments' lengths.
So calculate d = totalCurveLength / (numberOfPoints - 1)
, and split the curve into (numberOfPoints - 1)
chunks of length d
.
Solution 3:
Not sure I'm following, but instead of storing the actual data, maybe store the delta from point to point, then rebuild the curve from the deltas, so there wouldn't be any empty spots? This would, however, change the shape of the curve.
Post a Comment for "How To Redistribute Points Evenly Over A Curve"