获取两点之间xyz坐标列表的最快方法

我有 2 个点 A 和 B 以及它们的 xyz 坐标。我需要在这两个点之间的线上的所有 xyz 点的列表。Bresenham 的线算法对我来说太慢了。


A 和 B 的示例 xyz:


p = np.array([[ 275.5, 244.2, -27.3],

           [ 153.2, 184.3,  -0.3]])

预期输出:


x3 = p[0,0] + t*(p[1,0]-p[0,0])

y3 = p[0,1] + t*(p[1,1]-p[0,1])

z3 = p[0,2] + t*(p[1,2]-p[0,2])

p3 = [x3,y3,z3]

二维有一个非常快速的方法:


def connect(ends):

    d0, d1 = np.diff(ends, axis=0)[0]

    if np.abs(d0) > np.abs(d1): 

        return np.c_[np.arange(ends[0, 0], ends[1,0] + np.sign(d0), np.sign(d0), dtype=np.int32),

                     np.arange(ends[0, 1] * np.abs(d0) + np.abs(d0)//2,

                               ends[0, 1] * np.abs(d0) + np.abs(d0)//2 + (np.abs(d0)+1) * d1, d1, dtype=np.int32) // np.abs(d0)]

    else:

        return np.c_[np.arange(ends[0, 0] * np.abs(d1) + np.abs(d1)//2,

                               ends[0, 0] * np.abs(d1) + np.abs(d1)//2 + (np.abs(d1)+1) * d0, d0, dtype=np.int32) // np.abs(d1),

                     np.arange(ends[0, 1], ends[1,1] + np.sign(d1), np.sign(d1), dtype=np.int32)]


大话西游666
浏览 153回答 2
2回答

弑天下

不可能在一条线上给出“所有”点,因为有无数个点。不过,您可以对整数等离散数据类型执行此操作。如您的示例所示,我的回答采用浮点数。从技术上讲,浮点数以固定宽度的二进制格式存储,因此它们是离散的,但我会忽略这一事实,因为它很可能不是你想要的。正如您已经输入的问题,该行上的每个点 P 都满足此等式:P = P1 + t * (P2 - P1), 0 < t < 1我的版本使用 numpy 广播来规避显式循环。import numpy as npp = np.array([[ 275.5, 244.2, -27.3],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 153.2, 184.3,&nbsp; -0.3]])def connect(points, n_points):&nbsp; &nbsp; p1, p2 = points&nbsp; &nbsp; diff = p2 - p1&nbsp; &nbsp; t = np.linspace(0, 1, n_points+2)[1:-1]&nbsp; &nbsp; return p1[np.newaxis, :] + t[:, np.newaxis] * diff[np.newaxis, :]print(connect(p, n_points=4))# [[251.04 232.22 -21.9 ]#&nbsp; [226.58 220.24 -16.5 ]#&nbsp; [202.12 208.26 -11.1 ]#&nbsp; [177.66 196.28&nbsp; -5.7 ]]

肥皂起泡泡

也许我在这里误解了一些东西,但你不能只创建一个函数(就像你已经为一个点所做的那样)然后根据你想要多少点来创建一个点列表?我的意思是 2 点之间是无限数量的其他点,所以你必须定义一个数字或直接使用函数,它描述了点的位置。import numpy as npp = np.array([[ 275.5, 244.2, -27.3],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ 153.2, 184.3, -0.3]])def gen_line(p, n):&nbsp; &nbsp; points = []&nbsp; &nbsp; stepsize = 1/n&nbsp; &nbsp; for t in np.arange(0,1,stepsize):&nbsp; &nbsp; &nbsp; &nbsp; x = (p[1,0]-p[0,0])&nbsp; &nbsp; &nbsp; &nbsp; y = (p[1,1]-p[0,1])&nbsp; &nbsp; &nbsp; &nbsp; z = (p[1,2]-p[0,2])&nbsp; &nbsp; &nbsp; &nbsp; px = p[0,0]&nbsp; &nbsp; &nbsp; &nbsp; py = p[0,1]&nbsp; &nbsp; &nbsp; &nbsp; pz = p[0,2]&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; x3 = px + t*x&nbsp; &nbsp; &nbsp; &nbsp; y3 = py + t*y&nbsp; &nbsp; &nbsp; &nbsp; z3 = pz + t*z&nbsp; &nbsp; &nbsp; &nbsp; points.append([x3,y3,z3])&nbsp; &nbsp; return points# generates list of 30k pointsgen_line(p, 30000)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python