简体   繁体   English

获得每个点之间的距离,并找到曲线自身的位置

[英]Get the distance of each point with every other, and find where the curve approach itself

I am programming a random generated spline curve by first generating control points and then interpolate with spicy.splev. 我正在通过首先生成控制点,然后用辣。splev进行插值来编程随机生成的样条曲线。 Here is an example. 是一个例子。

Points are given like this: 给出的点是这样的:

np.array =[[  1.00000000e+01  -4.65000000e+02]
           [  1.78319153e+01  -4.60252579e+02]
          ...]

I now want to get the distance of every point with every other of the spline to see if at one point the spline comes too close to itself which includes self-collision. 现在,我想获得样条线每个点之间的距离,以查看样条线是否在某一点上太靠近自身(包括自碰撞)。

Before and after every point there should be an interval where points are ignored as these are always the closest points to each point: 在每个点之前和之后,应该有一个间隔,在该间隔中,将忽略所有点,因为这些点始终是最接近每个点的点:

def collision(splinePoints, interval):
    length = len(splinePoints)    
    mylist = []
    i = -1
    for item in splinePoints:
        i += 1
        first = item
        lowerLimit = i - interval
        uperLimit = i + interval
        if lowerLimit >= 0:
            for item in splinePoints[:lowerLimit]:
                mylist.append(first)
                mylist.append(item)
        if uperLimit <= length:
            for item in splinePoints[uperLimit:]:
                mylist.append(first)
                mylist.append(item)
    return np.amin(lengthOfLines(np.array(mylist)))

Lengths of lines is checked with this: 使用以下命令检查行长:

def lengthOfLines(points):
    return np.sqrt(np.sum(np.diff(points.T)**2, axis=0))

It somehow works, but not always. 它以某种方式起作用,但并非总是如此。 I am also struggling with debugging as the generated data is big and hard to read check or compare. 由于生成的数据很大且难以读取检查或比较,因此我也很难进行调试。 Any idea how to do it better? 知道如何做得更好吗?

All pairwise distances are obtained with pdist method of scipy.spatial package. 所有成对距离均使用scipy.spatial包的pdist方法获得。 It returns a flat array of distances, with redundancies eliminated. 它返回距离的平面数组,并消除了冗余。 The utility function squareform unpacks them to a symmetric square matrix, which is often more convenient. 效用函数squareform他们解包到一对称方阵,这常常是更方便。

You also want to find the nearest point that is not directly before or after the given point on the curve. 您还希望找到不在曲线上给定点之前或之后的最近点。 In the example below, I penalize the distances between neighbors (within 20 index values) by setting those distances to infinity. 在下面的示例中,我通过将邻居之间的距离设置为无穷大来惩罚它们之间的距离(在20个索引值之内)。 Then argmin find the nearest point for everyone, and I visualize it by drawing a red line to that nearest point. 然后argmin找到每个人的最近点,然后通过在该点附近画一条红线使它可视化。

import numpy as np
from scipy.spatial.distance import pdist, squareform

t = np.linspace(0, 10, 50)
points = np.stack(((t+5)*np.cos(t), (t+5)*np.sin(t)), axis=-1)  # for example

distances = squareform(pdist(points))    # distance matrix
i, j = np.meshgrid(np.arange(t.size), np.arange(t.size))
distances[np.abs(i-j) <= 20] = np.inf    # don't count neighbors
nearest = np.argmin(distances, axis=0)   # nearest to each

plt.plot(points[:, 0], points[:, 1])
for k in range(len(t)):
  npoint = points[nearest[k]]
  plt.plot([points[k, 0], npoint[0]], [points[k, 1], npoint[1]], 'r')
plt.axes().set_aspect('equal', 'datalim')
plt.show()

最近的

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

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