Assign values ​​to points in a three-dimensional array inside an ellipsoid

I need to assign a value to points in a three-dimensional array inside an ellipsoid. The ellipsoid equation should be something like this:

r=b.sin(u)
x=r.cos(v)
y=r.sin(v)
z=a.cos(u).

But I think this is only visual. I already tried something with a mask over a cubic array:

a, b = (size-1)/2, (size-1)/2
n = size
r = (size-1)/2

y,x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r        # circle mask

array = np.zeros((n, n, n))
array[mask] = 10

But this only creates a circle at x and y, which gives me: <w640/ . plot

This is not a sphere. (and I need an ellipsoid ).

Any ideas?

+4
source share
3 answers

mask = x*x + y*y <= r*r gives you a circle because it is an equation for a circle.

By the same reasoning,

mask = x*x + y*y + z*z <= r*r should provide you with scope and

mask = x*x/(a*a) + y*y/(b*b) + z*z/(c*c) <= r*r ellipsoid a, b c.

, z , x y.

+2

.

x = a * cos(u) * cos(v)
y = b * cos(u) * sin(v)
z = c * sin(u)

np.meshgrid, .

a, b, c = 4, 8, 6
space = np.linspace(0, 2 * np.pi, 50)
u, v = np.meshgrid(space)
x = a * np.cos(u) * np.cos(v)
y = b * np.cos(u) * np.sin(v)
z = c * np.sin(u)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
fig.show()

enter image description here

enter image description here

, , , . x ^ 2/a ^ 2 + y ^ 2/b ^ 2 + z ^ 2/c ^ 2 = 1

a, b, c = 4, 8, 6
xs, ys, zs = np.mgrid[-a + 1:a + 1:15j, -b + 1:b + 1:15j, -c + 1:c + 1:15j]
mask = xs**2/(a**2) + ys**2/(b**2) + zs**2/(c**2) <= 1
xs[~mask] = 0
ys[~mask] = 0
zs[~mask] = 0
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, ys, zs)
fig.show()

enter image description here

enter image description here

0

(0,0,0), / . :

[x ,y ,z ] - ellipsoid (rx,ry,rz)
[x',y',z'] - sphere (r)

, :

// sphere -> ellipsoid
x = x' * rx/r
y = y' * ry/r
z = z' * rz/r

// sphere <- ellipsoid
x' = x * r/rx
y' = y * r/ry
z' = z * r/rz

(rx,ry,rz)- the radii of the ellipsoid (in your case rx=ry), and r- any nonzero radius of the sphere (for example, r=1.0)

Thus, the test for an internal ellipsoid is as follows:

// scale constants
sx = 1/rx
sy = 1/ry
sz = 1/rz
// condition for inside ellipsoid
x*x*sx*sx + y*y*sy*sy + z*z*sz*sz <= 1.0
0
source

Source: https://habr.com/ru/post/1691645/


All Articles