简体   繁体   English

计算两个 3D 点之间距离的更快方法

[英]Faster way to calculate distance between two 3D points

I have 4 lists of length 160000 as s, x, y, z.我有 4 个长度为 160000 的列表,分别为 s、x、y、z。 I made a list(points) of 3d array of x,y,z.我列出了 x、y、z 的 3d 数组的列表(点)。 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.我需要找到标准点的所有组合之间的距离,并将点的索引与列表 s 的索引匹配,以便获得满足它的 2 个点的 s 值。 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想法是使用cdistnp.where来向量化处理

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如果 d 矩阵太大而无法放入 memory,求每个点到所有其他点的距离

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) Output (两种方法)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算 3D 中两点之间的距离? - How to calculate distance between two points in 3D? 计算两个 numpy 3D 点数组之间的最小距离的快速方法 - Fast way to calculate min distance between two numpy arrays of 3D points 从 3D 空间采样 N 个点的有效方法,约束条件是 Python 中两点之间的最小距离 - Efficient way to sample N points from a 3D space with the constraint of a minimum distance between two points in Python 如何计算 GeoDjango 中两点之间的 3D 距离(包括高度) - How to calculate 3D distance (including altitude) between two points in GeoDjango 有没有一种简单的方法可以在python中为整个数据集计算3D空间中两点的距离? - Is there a simple way to calculate the distance of two points in 3D space for a whole Data Set in python? 3D 中两点之间的距离 - 类、方法和对象、TypeError - Distance between two points in 3D - class, methods and objects, TypeError 求 3D 空间中两点之间的距离,Python - Finding the distance between two points in 3D space, Python 如何快速计算 3d 点之间的距离 - How to compute the distance between 3d points in a fast way 计算两个 3D 对象之间最小距离的简单方法 - Simple way to calculate minimum distance beween two 3D objects 大阵列的 3D 中两点之间的距离 - Distance between 2 points in 3D for a big array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM