I have a set of points that represent a 3D curve.
And I have a number of links/segments (n) and their length (L).
I want to split the 3D curve (starting from the first point of the curve) into n segments, so the euclidean distance between them is L. The curve is longer than n segments, so the last point of the segments is not at the end of the curve.
I currently have a solution for that but it's not optimized and takes a lot of time to compute (~110ms), using Python3.6
This my current solution:
- I'm using scipy.interpolate.splprep
to create the 3D curve
- Starting from parameter u=0, I am using the Bisection method to find the next u value that will give me a 3D point which is at a distance of L from the first points
- repeating these steps until all the n segments are found
This is the function that I wrote to do it:
- self.epsilon - The allowed error for the distance between the points
- self.link_N = Number of segments/links
- self.link_L - The length of the segments/links
def split_curve(self, curve):
joint_pos = curve[:, 0].reshape(3, 1)
tck, u = sc.splprep(curve, k=2, s=0)
a = 0
prev_pos = joint_pos
for i in range(self.link_N-1):
iter_count = 0
b = 1
error = self.epsilon + 1
while np.abs(error) > self.epsilon and iter_count < self.iter_max:
iter_count += 1
c = (a+b)/2
temp_pos = np.asarray([sc.splev(c, tck)]).T
error = self.link_L - np.linalg.norm(temp_pos - prev_pos)
if error > 0:
a = c
else:
b = c
u = c
joint_pos = np.hstack((joint_pos, temp_pos))
prev_pos = temp_pos
return joint_pos
My curve is changing fast (about 100-150hz) and this function (which takes about 100ms to compute) slows me down.
Is there a better solution for that problem rather than using the Bisection method?
question from:
https://stackoverflow.com/questions/65864253/python-split-3d-spline-curve-to-equally-spaced-euclidean-points 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…