3 d空间中点与线之间的最短距离 [英] Shortest distance between a point and a line in 3 d space

查看:229
本文介绍了3 d空间中点与线之间的最短距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy或python中的任何内容找到从点(x0,y0,z0)到由(x1,y1,z1)和(x2,y2,z2)连接的线的最小距离.不幸的是,我在网上可以找到的所有东西都与2d空间有关,而我对python还是很陌生.任何帮助将不胜感激.预先感谢!

I am trying to find the minimum distance from a point (x0,y0,z0) to a line joined by (x1,y1,z1) and (x2,y2,z2) using numpy or anything in python. Unfortunately, all i can find on the net is related to 2d spaces and i am fairly new to python. Any help will be appreciated. Thanks in advance!

推荐答案

StackOverflow不支持Latex,因此我将介绍一些数学知识.一种解决方案来自这样的想法:如果您的线跨越点pq,那么对于某些实值t,该线上的每个点都可以表示为t*(p-q)+q.然后,您希望最小化给定点r与该线上任何点之间的距离,并且距离通常是单个变量t的函数,因此标准演算技巧可以正常工作.考虑以下示例,该示例计算rpq所跨越的线之间的最小距离.手动,我们知道答案应该是1.

StackOverflow doesn't support Latex, so I'm going to gloss over some of the math. One solution comes from the idea that if your line spans the points p and q, then every point on that line can be represented as t*(p-q)+q for some real-valued t. You then want to minimize the distance between your given point r and any point on that line, and distance is conveniently a function of the single variable t, so standard calculus tricks work fine. Consider the following example, which calculates the minimum distance between r and the line spanned by p and q. By hand, we know the answer should be 1.

import numpy as np

p = np.array([0, 0, 0])
q = np.array([0, 0, 1])
r = np.array([0, 1, 1])

def t(p, q, r):
    x = p-q
    return np.dot(r-q, x)/np.dot(x, x)

def d(p, q, r):
    return np.linalg.norm(t(p, q, r)*(p-q)+q-r)

print(d(p, q, r))
# Prints 1.0

这在任何数量的维度(包括2、3和10亿)上都可以正常工作.唯一真正的限制是pq必须是不同的点,以便它们之间有一条唯一的线.

This works fine in any number of dimensions, including 2, 3, and a billion. The only real constraint is that p and q have to be distinct points so that there is a unique line between them.

在上面的示例中,我分解了代码,以显示由于我在数学上的思考方式而产生的两个截然不同的步骤(找到t然后计算距离).这不一定是最有效的方法,并且如果您想知道各种各样的点和同一条线的最小距离,也不一定.为了获得更有效的方法,请考虑以下因素:

I broke the code down in the above example in order to show the two distinct steps arising from the way I thought about it mathematically (finding t and then computing the distance). That isn't necessarily the most efficient approach, and it certainly isn't if you want to know the minimum distance for a wide variety of points and the same line -- doubly so if the number of dimensions is small. For a more efficient approach, consider the following:

import numpy as np

p = np.array([0, 0, 0])  # p and q can have shape (n,) for any
q = np.array([0, 0, 1])  # n>0, and rs can have shape (m,n)
rs = np.array([          # for any m,n>0.
    [0, 1, 1],
    [1, 0, 1],
    [1, 1, 1],
    [0, 2, 1],
])

def d(p, q, rs):
    x = p-q
    return np.linalg.norm(
        np.outer(np.dot(rs-q, x)/np.dot(x, x), x)+q-rs,
        axis=1)

print(d(p, q, rs))
# Prints array([1.        , 1.        , 1.41421356, 2.        ])

很可能我缺少一些简化,或者其他一些可以加快速度的事情,但至少应该是一个好的开始.

There may well be some simplifications I'm missing or other things that could speed that up, but it should be a good start at least.

这篇关于3 d空间中点与线之间的最短距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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