簡體   English   中英

給定 XYZ 網格點,計算球體內部的體積

[英]Given XYZ grid points, calculate volume of sphere inside

我有一個大的 3D 網格(~800,000 pts)均勻分布的 xyz 笛卡爾點,我想根據占據球體的點數找到球體內部的體積。 我目前正在使用 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

我想知道是否有一個已知的模塊適合這個從網格點測量球體的應用程序

嘿 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM