简体   繁体   English

查找点到轴的距离

[英]Finding the distance of points to an axis

I have an array of points in 3d Cartesian space:我在 3d 笛卡尔空间中有一组点:

P = np.random.random((10,3))

Now I'd like to find their distances to a given axis and on that given axis现在我想找到他们到给定轴和给定轴上的距离

Ax_support = array([3,2,1])
Ax_direction = array([1,2,3])

I've found a solution that first finds the vector from each point that is perpendicular to the direction vector... I feel however, that it is really complicated and that for such a standard problem there would be a numpy or scipy routine already out there (as there is to find the distance between points in scipy.spatial.distance)我找到了一个解决方案,首先从垂直于方向向量的每个点找到向量......然而,我觉得它真的很复杂,对于这样一个标准问题,已经有一个 numpy 或 scipy 例程了那里(因为在 scipy.spatial.distance 中找到点之间的距离)

I would be surprised to see such an operation among the standard operations of numpy/scipy. 我很惊讶在numpy / scipy的标准操作中看到这样的操作。 What you are looking for is the projection distance onto your line. 你要找的是你线上的投影距离。 Start by subtracting Ax_support : 首先减去Ax_support

P_centered = P - Ax_support

The points on the line through 0 with direction Ax_direction with the shortest distance in the L2 sense to each element of P_centered is given by 通过方向Ax_direction的线上的点与L2中意义上的最短距离对P_centered的每个元素给出的点由下式给出:

P_projected = P_centered.dot(np.linalg.pinv(
             Ax_direction[np.newaxis, :])).dot(Ax_direction[np.newaxis, :])

Thus the formula you are looking for is 因此,您正在寻找的公式是

distances = np.sqrt(((P_centered - P_projected) ** 2).sum(axis=1))

Yes, this is exactly what you propose, in a vectorized way of doing things, so it should be pretty fast for reasonably many data points. 是的,这正是你提出的建议,以矢量化的方式做事,所以对于相当多的数据点来说它应该非常快。

Note: If anybody knows a built-in function for this, I'd be very interested! 注意:如果有人知道这个的内置函数,我会非常感兴趣!

It's been bothering me that something like this does not exist, so I added haggis.math.segment_distance to the library of utilities I maintain, haggis .不存在这样的东西一直困扰着我,所以我将haggis.math.segment_distance添加到我维护的实用程序库haggis中。

One catch is that this function expects a line defined by two points rather than a support and direction.一个问题是这个函数需要一条由两点定义的线,而不是支撑和方向。 You can supply as many points and lines as you want, as long as the dimensions broadcast.只要尺寸广播,您可以提供任意数量的点和线。 The usual formats are many points projected on one line (as you have), or one point projected on many lines.通常的格式是将许多点投影在一条线上(如您所见),或者将一个点投影在多条线上。

 distances = haggis.math.segment_distance(P, Ax_support, Ax_support + ax_direction,
                                          axis=1, segment=False)

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

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