python - 球体体积内点的规则分布

标签 python numpy multidimensional-array computational-geometry

我试图在球体的体积内生成规则的 n 个点。我发现这个类似的答案( https://scicomp.stackexchange.com/questions/29959/uniform-dots-distribution-in-a-sphere )使用以下代码在球体表面生成均匀的规则 n 个点:

import numpy as np 

n = 5000
r = 1
z = []
y = []
x = []
alpha = 4.0*np.pi*r*r/n 
d = np.sqrt(alpha) 
m_nu = int(np.round(np.pi/d))
d_nu = np.pi/m_nu
d_phi = alpha/d_nu
count = 0
for m in range (0,m_nu):
    nu = np.pi*(m+0.5)/m_nu
    m_phi = int(np.round(2*np.pi*np.sin(nu)/d_phi))
    for n in range (0,m_phi):
        phi = 2*np.pi*n/m_phi
        xp = r*np.sin(nu)*np.cos(phi)
        yp = r*np.sin(nu)*np.sin(phi)
        zp = r*np.cos(nu)
        x.append(xp)
        y.append(yp)
        z.append(zp)
        count = count +1
它按预期工作:
enter image description here
如何修改它以在球体的体积中生成一组规则的 n 个点?

最佳答案

另一种方法来做到这一点,产生体积均匀:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

dim_len = 30
spacing = 2 / dim_len
point_cloud = np.mgrid[-1:1:spacing, -1:1:spacing, -1:1:spacing].reshape(3, -1).T

point_radius = np.linalg.norm(point_cloud, axis=1)
sphere_radius = 0.5
in_points = point_radius < sphere_radius

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(point_cloud[in_points, 0], point_cloud[in_points, 1], point_cloud[in_points, 2], )
plt.show()
输出(matplotlib 混淆了 View ,但它是一个均匀采样的球体(体积))
enter image description here

均匀采样,然后通过它们的半径检查点是否在球体中。
Uniform sampling reference [请参阅此答案的原始采样编辑历史记录]。

这种方法的缺点是会生成冗余点,然后将其丢弃。
它具有矢量化的优点,这可能弥补了缺点。我没查。
使用花哨的索引,可以生成与此方法相同的点,而不会生成冗余点,但我怀疑它是否可以轻松(或根本)矢量化。

关于python - 球体体积内点的规则分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67362634/

相关文章:

c++ - 将二维数组(指针)分配给对象中的变量以便在 C++ 中访问?

python - 递归函数 : Inversing word

Python numpy 索引超出轴零范围

python - python中噪声数据的线性变换

c - 更新二维数组时出现段错误

php - 在php中将mysql数据库结果插入多维数组

python - PEP-484 带有自己类型的类型注解

python - 获取 Python 中整数所需的字节大小

python - Whatsapp 自动机器人无法在 WhatsApp 联系人列表中搜索

python - 如何仅使用 numpy(而不是 sklearn LabelEncoder)创建标签编码器?