嘿,伙计们,现在我有一个表示体素栅格的numpy数组。我的函数得到坐标x,y,z,半径和一个值。我想把这些值加到坐标上,坐标是给定半径的球面的一部分。我尝试了一些方法,但都很慢:
def spheric Surface (x, y, z, r, value):
phi = 0
while phi <= (2*math.pi):
eta = math.pi * 2 / 3
while eta <= math.pi:
xx = x + r * math.sin(eta) * math.cos(phi)
yy = y + r * math.sin(eta) * math.sin(phi)
zz = z + r * math.cos(eta)
xx = int(xx*resoultion+0.5)
yy = int(yy*resolution+0.5)
zz = int(zz*resolution+0.5)
voxelGrid[xx][yy][zz] += value
eta += 1/10 * math.pi
phi += 1/10 * math.pi
第一种方法使用球坐标,半径越大,eta+=必须越小。。这个方法很慢。。你知道吗
def sphericSurface(x, y, z, r, value):
tol = 0.6
grenz = math.pi * 2 / 3
mask = (np.logical_and(np.logical_and((sx[:, None, None] - x) ** 2 + (sy[None, :, None] - y) ** 2 + (sz[None, None, :] - z) ** 2 <= (r + tol)**2,
(sx[:, None, None] - x) ** 2 + (sy[None, :, None] - y) ** 2 + (sz[None, None, :] - z) ** 2 >= (r - tol)**2),
(sz[None, None, :] - z) <= (r*math.cos(grenz))))
x, y, z = np.where(mask==True)
z *= 2
voxelGrid[x,y,z] += value
Secon方法使用掩码,但这也很慢。。有没有更好的办法?是的,我的极角应该只有2/3pi。。你知道吗
根据python - How do I move away from the “for-loop” school of thought? - Software Engineering Stack Exchange:
那么,让我们看看:
=>;(大写字母表示向量)
=>
对于} 的每个值,每个值都需要重复使用
phi
so ^{PHI
。你知道吗=>
需要为} 的每个值重复
eta
so ^{ETA
。你知道吗最终导致:
相关问题 更多 >
编程相关推荐