如何用numpy旋转3D点?

时间:2015-07-07 16:41:08

标签: python numpy 3d coordinates polar-coordinates

我正在尝试在球体上分布斑点。有一个函数来定义一个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

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

#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]])

然后将这组斑点相加以制作包含具有np.sum(S_t, axis =0)的不同斑点的3D阵列。然后沿三个轴中的一个投影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变化时,情况并非如此:

phi varies

或当theta变化时:

enter image description here

感谢您的建议。

1 个答案:

答案 0 :(得分:1)

make_spot_3d_spherical函数中,您sin的定义中混淆了cosx0

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')

enter image description here