Mayavi:如何掩盖三角形网格的彩色面?

时间:2015-04-15 20:36:42

标签: python mask mesh mayavi triangular

我正在使用Mayavi绘制带有彩色网格面的三角形网格。 首先,我将三角形网格绘制为线框,然后添加面部颜色。 (我使用的是another StackOverflow question

中提出的方法

我想屏蔽这些面部的一部分,并且可以选择遮盖这些面周围的线框。 Mayavi triangular_mesh提供了遮罩选项,但我不知道如何将它与组合的线框和表面一起使用。作为一种解决方法,我创建了一个具有完全透明的第一种颜色的自定义色彩映射,并将我想要遮罩的面的面值设置为零,以便不显示这些面。这有效,但没有给我选择"掩盖"线框网格的一部分。此外,对于某些角度,Mayavi渲染部分三角形面不透明而不是透明(参见example)。对于小零件,这不是问题,但对于具有> 10,000个三角形的图,这会妨碍分析并且看起来很难看。 有谁知道标准的Mayavi屏蔽功能是否解决了这个问题以及如何在这里实现它?

import numpy as np
from mayavi import mlab

# Create coordinate, triangular and color data
n = 29
t = np.linspace(-np.pi, np.pi, n)
z = np.exp(1j * t)
x = z.real.copy()
y = z.imag.copy()
z = np.zeros_like(x)

triangles = np.array([(0, i, i + 1) for i in range(1, n)])
x = np.r_[0, x]
y = np.r_[0, y]
z = np.r_[1, z]
face_values = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14]

# Create black wireframe mesh
wire_mesh = mlab.triangular_mesh(x, y, z, triangles, scalars=None, line_width=0.1, representation='wireframe',
                                 color=(0, 0, 0))

# Create face coloring
cell_data = wire_mesh.mlab_source.dataset.cell_data
cell_data.scalars = face_values
cell_data.scalars.name = 'Cell data'
cell_data.update()

# Plot triangular mesh with face coloring
mesh = mlab.pipeline.set_active_attribute(wire_mesh, cell_scalars='Cell data')
surf = mlab.pipeline.surface(mesh, colormap='jet')

# Retrieve the LUT colormap of the surf object. (256x4)
# this is an array of (R, G, B, A) values (each in range 0-255)
lut = surf.module_manager.scalar_lut_manager.lut.table.to_array()

# Modify alpha channel lut
lut[:, -1] = np.ones((lut.shape[0]))*255

# Add first row white color with full opacity
lut_mod = np.vstack((np.array([1, 1, 1, 0]), lut))

# Now use this colormap and show colorbar
surf.module_manager.scalar_lut_manager.lut.table = lut_mod
mlab.colorbar(surf)
mlab.show()

0 个答案:

没有答案
相关问题