如何在 numpy 中创建体积均匀的点云?

How to create a uniform-in-volume point cloud in numpy?

这实际上是一个点云形式的网格(n, 3)。

这段代码完成了,剩下的就是矢量化,我正在努力。
也许这里需要一些步幅技巧 + 重复?

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


dim_len = 5

x = np.linspace(0., 1., dim_len)
y = np.linspace(0., 1., dim_len)
z = np.linspace(0., 1., dim_len)

n_points = dim_len ** 3
point_cloud = np.zeros((n_points, 3))
# TODO vectorize
point_ind = 0
for i in range(dim_len):
    for j in range(dim_len):
        for k in range(dim_len):
            point_cloud[point_ind, 0] = x[i]
            point_cloud[point_ind, 1] = y[j]
            point_cloud[point_ind, 2] = z[k]
            point_ind += 1

print(point_cloud)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(point_cloud[:, 0], point_cloud[:, 1], point_cloud[:, 2],)
plt.show()

,

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

我不删除这个问题,因为它 google 可以搜索。我花了一段时间才找到这个参考。