一次在mayavi调用mlab.plot3d中绘制断开的线

时间:2019-01-11 09:50:12

标签: python 3d grid line mayavi

我已经开始使用mayavi,正在尝试绘制体素网格。为此,我使用mlab.plot3d绘制网格的每一行,这使得程序非常慢,因为必须为每一行调用渲染。有没有一种方法可以通过一次调用mlab.plot3d来绘制所有线条?主要问题是我正在绘制的线是断开连接的,如果我在同一数组中将一条线放在另一条线之后,渲染器将绘制我不需要的连接。我试图通过在两行之间插入无来连接两条未连接的线:

from threading import Thread
import time


class MyThread(Thread):
    def __init__(self, stop):
        Thread.__init__(self)
        self.stop = stop

    def run(self):
        stop = False
        while not stop:
            print("I'm running")
            time.sleep(1)
            # if the signal is stop, break `while loop` so the thread is over.
            stop = self.stop

m = MyThread(stop=False)
m.start()
while 1:
    i = input("input S to stop\n")
    if i == "S":
        m.stop = True
        break
    else:
        continue

但这不起作用。

我的逐行绘制网格的功能如下所示:

lines = [0.0, 3.0, None, 0.0, 6.0]

2 个答案:

答案 0 :(得分:1)

可以一次调用point3d连接点之间的任意线,而且速度非常快。使用protein example

中的以下代码
import mayavi.mlab as mlab
import numpy as np

connections = ((0,2),(3,5)) # point 0 and 2 and 3 and 5 are connected
x = np.random.randn(10)
y = np.random.randn(10)
z = np.random.randn(10)
pts = mlab.points3d(x, y, z)

pts.mlab_source.dataset.lines = np.array(connections)

tube = mlab.pipeline.tube(pts, tube_radius=0.15)
tube.filter.radius_factor = 1.
mlab.pipeline.surface(tube, color=(0.8, 0.8, 0))

mlab.show()

答案 1 :(得分:0)

感谢@Jannick的回答。

如果有人感兴趣,这里是新功能:

def draw_voxel_grid_cells(voxel_grid):

# Array for the points with the shape of the total number of points needed to define for drawing all the lines of the grid
points = np.zeros((((voxel_grid.nbr_cells_z+1)*(voxel_grid.nbr_cells_y+1) + (voxel_grid.nbr_cells_z+1)*(voxel_grid.nbr_cells_x+1) + (voxel_grid.nbr_cells_y+1)*(voxel_grid.nbr_cells_x+1))*2, 3))
i = 0
# Drawing lines parallel to x axis
for z_step in range(voxel_grid.nbr_cells_z+1):
    for y_step in range(voxel_grid.nbr_cells_y+1):
        points[i,:]    = [voxel_grid.min_grid_x, voxel_grid.min_grid_y+(voxel_grid.resolution*y_step), voxel_grid.min_grid_z+(voxel_grid.resolution*z_step)]
        points[i+1, :] = [voxel_grid.max_grid_x, voxel_grid.min_grid_y+(voxel_grid.resolution*y_step), voxel_grid.min_grid_z+(voxel_grid.resolution*z_step)]
        i += 2
# Drawing lines parallel to y axis
for z_step in range(voxel_grid.nbr_cells_z+1):
    for x_step in range(voxel_grid.nbr_cells_x+1):
        points[i,:]    = [voxel_grid.min_grid_x+(voxel_grid.resolution*x_step), voxel_grid.min_grid_y, voxel_grid.min_grid_z+(voxel_grid.resolution*z_step)]
        points[i+1, :] = [voxel_grid.min_grid_x+(voxel_grid.resolution*x_step), voxel_grid.max_grid_y, voxel_grid.min_grid_z+(voxel_grid.resolution*z_step)]
        i += 2
# Drawing lines parallel to z axis
for y_step in range(voxel_grid.nbr_cells_y+1):
    for x_step in range(voxel_grid.nbr_cells_x+1):
        points[i,:] =    [voxel_grid.min_grid_x+(voxel_grid.resolution*x_step), voxel_grid.min_grid_y+(voxel_grid.resolution*y_step), voxel_grid.min_grid_z]
        points[i+1, :] = [voxel_grid.min_grid_x+(voxel_grid.resolution*x_step), voxel_grid.min_grid_y+(voxel_grid.resolution*y_step), voxel_grid.max_grid_z]
        i += 2
connections = np.arange(0, ((voxel_grid.nbr_cells_z+1)*(voxel_grid.nbr_cells_y+1) + (voxel_grid.nbr_cells_z+1)*(voxel_grid.nbr_cells_x+1) + (voxel_grid.nbr_cells_y+1)*(voxel_grid.nbr_cells_x+1))*2)
connections = tuple(connections.reshape(-1,2))

# Plotting -------------------------------------------------
mlab.figure()
pts = mlab.points3d(points[:, 0], points[:, 1], points[:, 2], scale_factor=0.0001, color=(1, 0, 0))
pts.mlab_source.dataset.lines = np.array(connections)
tube = mlab.pipeline.tube(pts, tube_radius=0.001)
tube.filter.radius_factor = 1.
mlab.pipeline.surface(tube, color=(1, 0, 0))
mlab.show()
# ----------------------------------------------------------

return