从位置组织3D点,发现他们按距离 [英] Organize 3d points and found them by distance from a position

查看:176
本文介绍了从位置组织3D点,发现他们按距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在3D环境。我已经在这个领域的一些对象是谁再由X,Y,Z坐标psented $ P $。

I'm working in 3D context. I've some objects in this space who are represented by x, y, z position.

# My objects names (in my real context it's pheromone "point")
A = 1
B = 2
C = 3
D = 4

# My actual way to stock their positions
pheromones_positions = {
    (25, 25, 60): [A, D],
    (10, 90, 30): [B],
    (5, 85, 8): [C]
}

我的目标是找到了点(信息素)附近(距离)给定的进驻。我这样做只是有:

My objective is to found what points (pheromones) are near (with distance) a given emplacement. I do this simply with:

def calc_distance(a, b):
    return sqrt((a[0]-b[0])**2+(a[1]-b[1])**2+(a[2]-b[2])**2)

def found_in_dict(search, points, distance):
    for point in points:
        if calc_distance(search, point) <= distance:
            return points[point]

founds = found_in_dict((20, 20, 55), pheromones_positions, 10)
# found [1, 4] (A and D)

不过,有很多信息素是很慢的(测试一一...)。我该如何组织这些3D位置,以更迅速地找到从给定的位置由距离的位置? 是否存在算法或图书馆的(numpy的?)谁可以帮我用这种方式?

But, with a lot of pheromones it's very slow (test them one by one ...). How can i organize these 3D positions to found more quickly "positions by distance from given position" ? Does exist algorithms or librarys (numpy ?) who can help me in this way ?

推荐答案

您应该计算所有的(平方)的距离一次。随着NumPy的,你可以简单地减去了(NX3)×3大小的目标点的所有位置坐标的阵列和求和平方坐标差获得与n个元素的列表:

You should compute all (squared) distances at once. With NumPy you can simply subtract the target point of size 1x3 from the (nx3) array of all position coordinates and sum the squared coordinate differences to obtain a list with n elements:

squaredDistances = np.sum((np.array(pheromones_positions.keys()) - (20, 20, 55))**2, axis=1)
idx = np.where(squaredDistances < 10**2)[0]
print pheromones_positions.values()[idx]

输出:

[1, 4]


顺便说一句:既然你的返回语句for循环的全部点之内,它会停止找到第一个点之后迭代。所以,你可能会错过第二次或第三场比赛。


By the way: Since your return statement is within the for-loop over all points, it will stop iterating after finding a first point. So you might miss a second or third match.

这篇关于从位置组织3D点,发现他们按距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆