-1

I have a NumPy array that can be used to create a curve the array looks like this.

curve=np.asarray([0,0,0], [0.5,0,0], [0.8,0.2,0], [1,0.5,0], [1.2,0.7,0])

If I connect them up it will look like the following figure

enter image description here

The curve is in meters. Is there a Numpythonic way to find the points, on the curve, that is 1m, 2m, 3m, 4m etc, away from the origin (the blue point).

Thanks a lot for your great help and advice.

Sincerely

Wilson

yihao ren
  • 369
  • 1
  • 4
  • 15

1 Answers1

0

For each 3D coordinate in the array, compute the norm of the difference to the previous coordinate. This will give you an array of distances. In your example you get [0, 0.5, 0.3606, 0.3606, 0.2828]. The cumulative sum of this will give you the "distance walked" in the curve since the origin, which in this case is [0, 0.5, 0.8606, 1.2211, 1.504].

You can use the code below

a = np.array([[0,0,0], [0.5,0,0], [0.8,0.2,0], [1,0.5,0], [1.2,0.7,0]])
diffs = np.concatenate([[0], np.linalg.norm(np.diff(a, axis=0), axis=1)])
cumsum = np.cumsum(diffs)

Now, for each of your points of interest (1m, 2m, etc) you can find the indexes in the cumsum variable where the point of interest falls into. For instance, for 1m it would be indexes 2 and 3. Now, in index 2 you have walked a total of 0.8606, meaning that you still need to walk a distance of 0.1394 to reach a total of 1m. Just use the original coordinates in indexes 2 and 3 to compute the line equation then use that to find the appropriate coordinate. You can do that with

v = a[3] - a[2]
v /= np.linalg.norm(v)
position_1m = a[2] + 0.1394 * v

With this, you get the coordinate for 1m as [0.8773, 0.316, 0.]. The plot below shows the position corresponding to 1m as a red star.

position of 1m in the curve

darcamo
  • 3,294
  • 1
  • 16
  • 27