python - 如何在圆柱面上生成规则点

标签 python numpy

我是 Python 的初学者,我必须使用 Numpy 处理一个项目。 我需要在圆柱表面的一部分上生成一些点(例如一百万个)。这些点应该规则地分布在由给定角度定义的表面子区域上。我该怎么做呢?

我的输入参数是:

    圆柱体中心的
  1. 位置(例如 [0,0,0] )

  2. 圆柱体的方向

  3. 圆柱长度

  4. 圆柱体的半径

  5. angle(这定义了点应该分布在其上的圆柱部分。)对于 alpha = 360,整个表面

  6. delta_l为长度方向上每两点之间的距离

  7. delta_alphaalpha(旋转)方向上每两点之间的距离

我的输出参数:

  • 包含所有点坐标的数组

任何人都可以帮助我,或者给我一些关于如何做到这一点的提示吗?

非常感谢

最佳答案

这是取 self 之前的一个项目:

def make_cylinder(radius, length, nlength, alpha, nalpha, center, orientation):

    #Create the length array
    I = np.linspace(0, length, nlength)

    #Create alpha array avoid duplication of endpoints
    #Conditional should be changed to meet your requirements
    if int(alpha) == 360:
        A = np.linspace(0, alpha, num=nalpha, endpoint=False)/180*np.pi
    else:
        A = np.linspace(0, alpha, num=nalpha)/180*np.pi

    #Calculate X and Y
    X = radius * np.cos(A)
    Y = radius * np.sin(A)

    #Tile/repeat indices so all unique pairs are present
    pz = np.tile(I, nalpha)
    px = np.repeat(X, nlength)
    py = np.repeat(Y, nlength)

    points = np.vstack(( pz, px, py )).T

    #Shift to center
    shift = np.array(center) - np.mean(points, axis=0)
    points += shift

    #Orient tube to new vector

    #Grabbed from an old unutbu answer
    def rotation_matrix(axis,theta):
        a = np.cos(theta/2)
        b,c,d = -axis*np.sin(theta/2)
        return np.array([[a*a+b*b-c*c-d*d, 2*(b*c-a*d), 2*(b*d+a*c)],
                         [2*(b*c+a*d), a*a+c*c-b*b-d*d, 2*(c*d-a*b)],
                         [2*(b*d-a*c), 2*(c*d+a*b), a*a+d*d-b*b-c*c]])

    ovec = orientation / np.linalg.norm(orientation)
    cylvec = np.array([1,0,0])

    if np.allclose(cylvec, ovec):
        return points

    #Get orthogonal axis and rotation
    oaxis = np.cross(ovec, cylvec)
    rot = np.arccos(np.dot(ovec, cylvec))

    R = rotation_matrix(oaxis, rot)
    return points.dot(R)

绘制点:

points = make_cylinder(3, 5, 5, 360, 10, [0,2,0], [1,0,0])

Cylinder

旋转部分又快又脏——你应该仔细检查一下。欧拉-罗德里格斯公式感谢unutbu .

关于python - 如何在圆柱面上生成规则点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22285994/

相关文章:

python - 凯拉斯/ tensorflow : Combined Loss function for single output

python - 关于 Python 导入的一些困惑

python - 从 float32 到 float16 的 numpy astype

python - 处理 numpy 数组规范化中的零

python - ValueError : too many values to unpack (expected 1)?

python - 在 databricks 集群中使用 init 脚本安装 python 包

python - 使用 Paramiko invoke_shell() 执行的命令的输出是分页的(在 recv 中获取 "--more--")

python - numpy.where 的嵌套使用

python - Mac OS X Python GUI 管理员提示

python - 在内存中存储一​​个大的稀疏矩阵来计算特征值