免責事項:私はpymrt
の著者です。
あなただけの球を持っている必要がある場合は、あなたがpip
-installableモジュールpymrt
を使用し、特にpymrt.geometry.sphere()
できるが、例えば:
import pymrt as mrt
import pymrt.geometry
arr = mrt.geometry.sphere(3, 1)
array([[[False, False, False],
[False, True, False],
[False, False, False]],
[[False, True, False],
[ True, True, True],
[False, True, False]],
[[False, False, False],
[False, True, False],
[False, False, False]]], dtype=bool)
内部で、これはn次元超楕円発電機として実装されている、あなたは詳細はsource codeを確認できます。 簡単に言えば、(簡体字)のコードですが、次のように読み取ります
import numpy
def sphere(shape, radius, position):
# assume shape and position are both a 3-tuple of int or float
# the units are pixels/voxels (px for short)
# radius is a int or float in px
semisizes = (radius,) * 3
# genereate the grid for the support points
# centered at the position indicated by position
grid = [slice(-x0, dim - x0) for x0, dim in zip(position, shape)]
position = np.ogrid[grid]
# calculate the distance of all points from `position` center
# scaled by the radius
arr = np.zeros(shape, dtype=float)
for x_i, semisize in zip(position, semisizes):
arr += (np.abs(x_i/semisize) ** 2)
# the inner part of the sphere will have distance below 1
return arr <= 1.0
arr = sphere((256, 256, 256), 10, (127, 127, 127)))
# this will save a sphere in a boolean array
# the shape of the containing array is: (256, 256, 256)
# the position of the center is: (127, 127, 127)
# if you want is 0 and 1 just use .astype(int)
# for plotting it is likely that you want that
# just for fun you can check that the volume is matching what expected
np.sum(arr)
# gives: 4169
4/3 * np.pi * 10 ** 3
# gives: 4188.790204786391
# (the two numbers do not match exactly because of the discretization error)
私はあなたのコードが正確にどのように機能するかを取得することができないのですが、これは実際にあなたが試みることができる(自分の番号を使用して)球を生産していることを確認するために:
import pymrt as mrt
import pymrt.geometry
arr = mrt.geometry.sphere(256, 10, 0.5)
# plot in 3D
import matplotlib.pyplot as plt
from skimage import measure
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
verts, faces, normals, values = measure.marching_cubes(arr, 0.5, (2,) * 3)
ax.plot_trisurf(
verts[:, 0], verts[:, 1], faces, verts[:, 2], cmap='Spectral',
antialiased=False, linewidth=0.0)
plt.show()