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

2024-06-02 05:08:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的输入参数是:

  1. 圆柱体中心的position(例如[0,0,0]

  2. 圆柱体

  3. 气缸

  4. 气缸

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

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

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

我的输出参数:

  • 包含所有点坐标的数组

有谁能帮我,或者给我一个提示如何做这件事?在

非常感谢


Tags: 项目alphanumpy距离参数定义方向表面
1条回答
网友
1楼 · 发布于 2024-06-02 05:08:47

这是我以前的一个项目:

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)

绘制点:

^{pr2}$

Cylinder

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

相关问题 更多 >