【问题标题】:Given XYZ grid points, calculate volume of sphere inside给定 XYZ 网格点,计算球体内部的体积
【发布时间】:2020-01-21 00:15:17
【问题描述】:

我有一个由均匀分布的 xyz 笛卡尔点组成的大型 3D 网格(约 800,000 点),我想根据占据球体的点数找到球体内部的体积。我目前正在使用 scipy cKDTree 并使用 query_ball_point 检测原点(0,0,0)一定半径内的所有点来估计体积,但这个体积(grid_vol,下面)通常有很大不同(50% 错误或更大) 比真实体积 (sphere_vol)。

import numpy as np
from scipy import spatial
import math
#constants
xl = -3.15
xr =  1.75
yl = -2.0
yr =  2.0
zl = -1.15
zr = 3.9
spacing = 0.05
R = 3.5
cube = spacing ** 3  
#Create grid
x=np.arange(xl,xr,spacing)
y=np.arange(yl,yr,spacing)
z=np.arange(zl,zr,spacing)
x2,y2,z2=np.meshgrid(x,y,z,indexing='ij')
all_grid=np.array([x2.flatten(),y2.flatten(),z2.flatten()]).T
cube = spacing ** 3                  
point_tree = spatial.cKDTree(all_grid)                  # allgrid = evenly spaced rectangular grid
n_voxel = len(point_tree.query_ball_point((0,0,0), R))   # number of points in grid occupying sphere of radius R
grid_vol = n_voxel * cube                               # volume based on grid points
sphere_vol = 4 / 3 * math.pi * R ** 3                # vol of sphere w/ radius R to compare

在这种情况下:

grid_vol = 78.1275
sphere_vol = 179.5944

我想知道是否有一个已知的模块可以很好地用于从网格点测量球体的这种应用

【问题讨论】:

  • “非常不同”是什么意思?多少钱?从连续空间到体素化空间时,您总是会丢失信息
  • 试图将错误减少 50% 或更多

标签: python numpy scipy


【解决方案1】:

嘿,guilian,我试过你的代码,据我所知,它工作得很好(由于近似值造成的一些错误,但不是一个大错误

import numpy as np
from scipy import spatial
#define constants
l=-1
r=1
spacing=0.05
R=0.5
cube = spacing ** 3  
#Create grid
x=np.arange(l,r,spacing)
y=np.arange(l,r,spacing)
z=np.arange(l,r,spacing)
x2,y2,z2=np.meshgrid(x,y,z,indexing='ij')
all_grid=np.array([x2.flatten(),y2.flatten(),z2.flatten()]).T
# your code            
point_tree = spatial.cKDTree(all_grid)
n_voxel = len(point_tree.query_ball_point((0,0,0), R))   # number of points in grid occupying sphere of radius R
grid_vol = n_voxel * cube                               # volume based on grid points
sphere_volume = 4 / 3 * np.pi * R ** 3
print(grid_vol) # 0.5185000000000001
print(sphere_volume) # 0.5235987755982988

【讨论】:

  • 谢谢,我认为代码通常适用于较小的半径和较少的点,但我已经更新了我的问题以显示我通常使用的尺寸以及我可以得到的错误类型。
  • 再次查看我的问题后,我实际上发现我要测量的球体没有完全被网格封装,所以我什至无法准确测量球体......感谢您采取还是看看这个吧。
猜你喜欢
  • 2011-12-12
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 2016-06-28
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多