0

这是使用 mayavi 生成随机大小球体的代码,

我想让球体通过表面或粘合线相互连接:

  1. 球体必须位于 3D 空间中的随机位置
  2. 球体必须具有相同的半径
from mayavi import mlab
import numpy as np

[phi,theta] = np.mgrid[0:2*np.pi:12j,0:np.pi:12j]
x = np.cos(phi)*np.sin(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(theta)

def plot_sphere(p):
    
    r,a,b,c = p
    r=1
    return mlab.mesh(r*x+a, r*y+b, r*z )  


for k in range(8):
    c = np.random.rand(4)
    c[0] /= 10.
    plot_sphere(c)

mlab.show()
4

2 回答 2

0

只使用 mayavipoints3d函数怎么样?默认情况下,该mode参数设置为球体,您可以使用该scale_factor参数设置直径。您还可以通过改变resolution参数来增加球体的分辨率。

这是代码:

def draw_sphere(
        center_coordinates,
        radius,
        figure_title,
        color,
        background,
        foreground
):
    sphere = mlab.figure(figure_title)
    sphere.scene.background = background
    sphere.scene.foreground = foreground

    mlab.points3d(
        center_coordinates[0],
        center_coordinates[1],
        center_coordinates[2],
        color=color,
        resolution=256,
        scale_factor=2*radius,
        figure=sphere
    )

关于表面问题相互联系,您的解释很差。也许你的意思只是切线球,但我需要更多细节。

于 2022-02-13T22:24:29.137 回答
0

从球体方程:

在此处输入图像描述

因此,当向我们传递参数时,mlab.mesh我们希望[x_0, y_0, z_0]为每个球体设置,例如它们与轴的位置不同。

问题是由 生成的数字np.random.rand(4)是随机的,但不是不同的。

让我们进行修改,使参数[x_0, y_0, z_0]是随机且不同的:

  1. 我们sample用来获取多维数据集中不同的索引号
  2. 我们使用index_to_3d索引转换为(x, y, z)坐标

半径 ,r可以调整为在球体之间具有或多或少的间距。

3D 空间中的球体

代码:

import random
from itertools import product

from mayavi import mlab
import numpy as np

[phi, theta] = np.mgrid[0:2 * np.pi:12j, 0:np.pi:12j]
x = np.cos(phi) * np.sin(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(theta)


def plot_sphere(x_0, y_0, z_0):
    r = 0.5
    return mlab.mesh(r * x + x_0, r * y + y_0, r * z + z_0)

SPHERES_NUMBER = 200

CUBE_SIZE = 10

def index_to_3d(i, SIZE):
    z = i // (SIZE * SIZE)
    i -= (z * SIZE * SIZE)
    y = i // SIZE
    x = i % SIZE
    return x, y, z

random_tuples = [index_to_3d(i, CUBE_SIZE) for i in random.sample(range(CUBE_SIZE ** 3), SPHERES_NUMBER)]

for k in range(SPHERES_NUMBER):
    x_0, y_0, z_0 = random_tuples[k]
    plot_sphere(x_0, y_0, z_0)

mlab.show()

输出:

在此处输入图像描述

球体集群

让我们利用gauss来为聚类点创建坐标。

代码:

import random
from itertools import product

from mayavi import mlab
import numpy as np

[phi, theta] = np.mgrid[0:2 * np.pi:12j, 0:np.pi:12j]
x = np.cos(phi) * np.sin(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(theta)


def plot_sphere(x_0, y_0, z_0):
    r = 0.5
    return mlab.mesh(r * x + x_0, r * y + y_0, r * z + z_0)

SPHERES_NUMBER = 200

def create_cluster(CLUSTER_SIZE):
    means_and_deviations = [(1, 1.5), (1, 1.5), (1, 1.5)]
    def generate_point(means_and_deviations):
        return tuple(random.gauss(mean, deviation) for mean, deviation in means_and_deviations)
    cluster_points = set()
    while len(cluster_points) < CLUSTER_SIZE:
        cluster_points.add(generate_point(means_and_deviations))
    return list(cluster_points)

cluster_points = create_cluster(SPHERES_NUMBER)

for k in range(SPHERES_NUMBER):
    x_0, y_0, z_0 = cluster_points[k]
    plot_sphere(x_0, y_0, z_0)

mlab.show()

输出:

在此处输入图像描述

于 2020-09-17T11:56:16.803 回答