![](/img/trans.png)
[英]Fastest way to calculate the shortest (euclidean) distance between points, in pandas dataframe
[英]The fastest way of checking the closest distance between points
我有2本字典。 两者都具有索引和世界空间位置的键值对。
就像是:
{
"vertices" :
{
1: "(0.004700, 130.417480, -13.546420)",
2: "(0.1, 152.4, 13.521)",
3: "(58.21, 998.412, -78.0051)"
}
}
词典1将始终具有大约20至100个条目,词典2将始终具有大约10,000个条目。
对于字典1中的每个点,我想找到字典2中最接近它的点。 最快的方法是什么? 对于字典1中的每个条目,循环浏览字典2中的所有条目 ,然后返回最接近的条目 。
一些未经测试的伪代码:
for point, distance in dict_1.iteritems():
closest_point = get_closest_point(dict_1.get(point))
def get_closest_point(self, start_point)
furthest_distance = 2000000
closest_point = 0
for index, end_point in dict_1.iteritems():
distance = get_distance(self, start_point, end_point)
if distance < furthest_distance:
furthest_distance = distance
closest_point = closest_point
return closest_point
我认为这样会起作用。 “问题”是,如果我在字典1中有100个条目,它将是100 x 10,000 = 1,000,000次迭代。 在我看来,这似乎并不很快或优雅。
在Maya / Python中是否有更好的方法?
编辑:只是想评论一下,我以前使用过closestPointOnMesh节点,它工作得很好,并且如果您要检查的点实际上是网格的一部分,则容易得多。 您可以执行以下操作:
selected_object = pm.PyNode(pm.selected()[0])
cpom = pm.createNode("closestPointOnMesh", name="cpom")
for vertex, distance in dict_1.iteritems():
selected_object.worldMesh >> cpom.inMesh
cpom.inPosition.set(dict_1.get(vertex))
print "closest vertex is %s " % cpom.closestVertexIndex.get()
来自该节点的即时答复,一切都很轻松。 但是,如果要检查的点列表不是网格的一部分,则不能使用它。 实际上是否可能/愿意:
您肯定需要非平凡数量点的加速结构。 您想要的是KD树或八叉树-KD树在搜索中性能更高,但构建速度较慢,并且可能更难编码。 同样,由于八进制是空间的而不是二进制的,因此它们可能会使进行琐碎的测试变得更加容易。
您可以在此处获取python八叉树: http : //code.activestate.com/recipes/498121-python-octree-implementation/
如果您要进行大量距离检查,则肯定要使用Maya API向量类进行实际的数学比较-尽管这将比同等的python快很多,但要快得多。 如果您不太了解API,则可以从pymel.datatypes
获取这些信息,尽管使用更新的API2版本非常容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.