如何使用 numpy 旋转 3D 点?

How to rotate a 3D spot with numpy?

我正在尝试在球体上分布斑点。有一个函数可以用球坐标 rho, theta, phi 来定义一个点:

def make_spot_3d_spherical(bright, spread, rho, theta, phi):


    x0 = int(rho*np.sin(theta)*np.cos(phi))
    y0 = int(rho*np.sin(theta)*np.sin(phi))
    z0 = int(rho*np.cos(phi))

    # Create x and y indices
    x = np.linspace(-50, 50, 200)
    y = np.linspace(-50, 50, 200)
    z = np.linspace(-50, 50, 200)

    X, Y, Z = np.meshgrid(x, y, z)

    Intensity = np.uint16(bright*np.exp(-((X-x0)/spread)**2
                                        -((Y-y0)/spread)**2
                                        -((Z-z0)/spread)**2))

    return Intensity

通过为固定的 rho 值改变 theta 或 phi 来定义两组点(S_t 或 S_p):

#set of Spots defined by varying theta or phi
S_t = np.asarray([make_spot_3d_spherical(1000,2, 30,t,0) for t in [0,np.pi/6,np.pi/3,2*np.pi/3]])
S_p = np.asarray([make_spot_3d_spherical(1000,2, 30,0,phi) for phi in [0,np.pi/6,np.pi/3,2*np.pi/3]])

然后将一组点相加以制作一个 3D 数组,其中包含 np.sum(S_t, axis =0) 的不同点。然后沿三个轴之一投影 3D 数组:

set_of_St0 = np.sum(np.sum(S_t, axis =0), axis = 0)
set_of_St1 = np.sum(np.sum(S_t, axis =0), axis = 1)
set_of_St2 = np.sum(np.sum(S_t, axis =0), axis = 2)

set_of_Sp0 = np.sum(np.sum(S_p, axis =0), axis = 0)
set_of_Sp1 = np.sum(np.sum(S_p, axis =0), axis = 1)
set_of_Sp2 = np.sum(np.sum(S_p, axis =0), axis = 2)

最后显示不同的投影:

plt.subplot(131, xticks=[], yticks=[])
plt.imshow(set_of_Sp0, interpolation = 'nearest')

plt.subplot(132, xticks=[], yticks=[])
plt.imshow(set_of_Sp1, interpolation = 'nearest')

plt.subplot(133, xticks=[], yticks=[])
plt.imshow(set_of_Sp2, interpolation = 'nearest')
plt.show()

在生成的图像中,我正等着看沿着一些圆圈分布的斑点,但事实并非如此,当 phi 变化时:

或者当 theta 变化时:

感谢您的建议。

在函数 make_spot_3d_spherical 中,您在 x0 的定义中混淆了 sincos

x0 = int(rho*np.sin(theta)*np.cos(phi))

应该是

x0 = int(rho*np.cos(theta)*np.sin(phi))

现在可以了。这是如果你改变 phi:

S_p = np.asarray([make_spot_3d_spherical(1000,2, 30,0,phi) for phi in np.linspace(0, 2*np.pi, 20)])
set_of_Sp0 = np.sum(np.sum(S_p, axis =0), axis = 0)
plt.imshow(set_of_Sp0, interpolation = 'nearest')