简体   繁体   中英

Faster way to calculate distance between two 3D points

I have 4 lists of length 160000 as s, x, y, z. I made a list(points) of 3d array of x,y,z. I need to find the distance between all combinations of points for criteria and match the index of the points to that of list s, so that I get the s value of 2 points which satisfy it. I'm using the code below. Is there any faster way to do this?

import numpy as np

points = []
for i in range(len(xnew)):
    a = np.array((xnew[i],ynew[i],znew[i]))
    points.append(a)
for i in range(len(points)):
    for j in range(len(points)):
        d = np.sqrt(np.sum((points[i] - points[j]) ** 2))
        if d <= 4 and d >=3:
            print(s[i],s[j],d)

Idea is to use cdist and np.where to vectorize the processing

Code

import numpy as np
import scipy.spatial.distance

# Distance between all pairs of points
d = scipy.spatial.distance.cdist(points, points)
# Pairs within threshold
indexes = np.where(np.logical_and(d>=3, d<=4))

for i, j in indexes:
    if i < j: # since distance is symmetric, not reporting j, i
      print(s[i],s[j],d[i][j])

If d Matrix is too large to fit into memory, find the distance of each point to all other points

for i in range(len(points)):
    # Distance from point i to all other points
    d = scipy.spatial.distance.cdist(points,[points[i]])
    # Points within threshold
    indexes = np.where(np.logical_and(d>=3, d<=4))
    
    for ind in indexes:
      if ind.size > 0:
        for j in ind:
          if i < j:   # since distance is symmetric, not reporting j, i
            print(s[i], s[j], d[j][0])

Tests

points = [
  [1, 2, 3],
  [1.1, 2.2, 3.3],
  [4, 5, 6],
  [2, 3, 4]
]
s = [0, 1, 2, 3]

Output (both methods)

2 3 3.4641016151377544
points = np.array([x, y, z]).T                         

t1, t2 = np.triu_indices(len(points), k= 1)                  # triangular indices

p1 = points[t1]
p2 = points[t2]

d = p1 - p2                       # displacements from p1 to p2
d = np.linalg.norm(d, axis= -1)   # distances     from p1 to p2

mask = (3 <= d) & (d <= 4)
indx = np.where(mask)                     # indices where distance is between 4 and 3
ans = np.array([ s[t1[i]], s[t2[i]], d[i] ]).T

test run:

n = 10

x = np.random.randint(10, size= [n])          # dummy data
y = np.random.randint(10, size= [n])
z = np.random.randint(10, size= [n])

s = np.random.randint(10, size= [n])

after running the above code

points

>>> array([
       [9, 3, 5],
       [7, 8, 1],
       [0, 0, 2],
       [6, 7, 2],
       [4, 4, 3],
       [8, 0, 9],
       [5, 2, 6],
       [0, 8, 9],
       [2, 6, 9],
       [4, 8, 4]])
s

>>> array([4, 2, 9, 9, 8, 2, 7, 6, 0, 5])
for e in ans:
   print(*e)

>>> 9.0  8.0  3.7416573867739413
    9.0  5.0  3.0
    8.0  7.0  3.7416573867739413

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM